vchtchetkine | 8267511 | 2009-07-24 11:30:41 -0700 | [diff] [blame] | 1 | /*
|
| 2 | * Copyright (C) 2009 The Android Open Source Project
|
| 3 | *
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 | * you may not use this file except in compliance with the License.
|
| 6 | * You may obtain a copy of the License at
|
| 7 | *
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
| 9 | *
|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 | * See the License for the specific language governing permissions and
|
| 14 | * limitations under the License.
|
| 15 | */
|
| 16 |
|
| 17 | /** \file
|
| 18 | This file consists of implementation of class AdbWinUsbIOCompletion that
|
| 19 | encapsulates a wrapper around OVERLAPPED Win32 structure returned from
|
| 20 | asynchronous I/O requests issued via WinUsb API.
|
| 21 | */
|
| 22 |
|
| 23 | #include "stdafx.h"
|
| 24 | #include "adb_winusb_io_completion.h"
|
| 25 |
|
| 26 | AdbWinUsbIOCompletion::AdbWinUsbIOCompletion(
|
| 27 | AdbWinUsbEndpointObject* parent_io_obj,
|
| 28 | ULONG expected_trans_size,
|
| 29 | HANDLE event_hndl)
|
| 30 | : AdbIOCompletion(parent_io_obj, expected_trans_size, event_hndl) {
|
| 31 | }
|
| 32 |
|
| 33 | AdbWinUsbIOCompletion::~AdbWinUsbIOCompletion() {
|
| 34 | }
|
| 35 |
|
vchtchetkine | 8267511 | 2009-07-24 11:30:41 -0700 | [diff] [blame] | 36 | bool AdbWinUsbIOCompletion::GetOvelappedIoResult(LPOVERLAPPED ovl_data,
|
| 37 | ULONG* bytes_transferred,
|
| 38 | bool wait) {
|
| 39 | if (NULL != bytes_transferred) {
|
| 40 | *bytes_transferred = 0;
|
| 41 | }
|
| 42 |
|
| 43 | if (!IsOpened()) {
|
| 44 | SetLastError(ERROR_INVALID_HANDLE);
|
| 45 | return false;
|
| 46 | }
|
| 47 |
|
| 48 | ULONG transfer;
|
| 49 | bool ret = WinUsb_GetOverlappedResult(
|
| 50 | parent_winusb_io_object()->winusb_handle(),
|
| 51 | overlapped(),
|
| 52 | &transfer,
|
| 53 | wait ? TRUE : FALSE) ? true : false;
|
| 54 |
|
| 55 | // TODO: This is bizzare but I've seen it happening
|
| 56 | // that GetOverlappedResult with wait set to true returns "prematurely",
|
| 57 | // with wrong transferred bytes value and GetLastError reporting
|
| 58 | // ERROR_IO_PENDING. So, lets give it an up to a 20 ms loop!
|
| 59 | ULONG error = GetLastError();
|
| 60 |
|
| 61 | if (wait && ret && (0 == transfer) && (0 != expected_transfer_size_) &&
|
| 62 | ((ERROR_IO_INCOMPLETE == error) || (ERROR_IO_PENDING == error))) {
|
| 63 | for (int trying = 0; trying < 10; trying++) {
|
| 64 | Sleep(2);
|
| 65 | ret = WinUsb_GetOverlappedResult(
|
| 66 | parent_winusb_io_object()->winusb_handle(),
|
| 67 | overlapped(),
|
| 68 | &transfer,
|
| 69 | wait ? TRUE : FALSE) ? true : false;
|
| 70 | error = GetLastError();
|
| 71 | if (!ret || (0 != transfer) ||
|
| 72 | ((ERROR_IO_INCOMPLETE != error) && (ERROR_IO_PENDING != error))) {
|
| 73 | break;
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | if (NULL != ovl_data) {
|
| 79 | CopyMemory(ovl_data, overlapped(), sizeof(OVERLAPPED));
|
| 80 | }
|
| 81 |
|
| 82 | if (NULL != bytes_transferred) {
|
| 83 | *bytes_transferred = transfer;
|
| 84 | }
|
| 85 |
|
| 86 | return ret;
|
| 87 | }
|