File: adb_winusb_endpoint_object.cpp

package info (click to toggle)
android-platform-development 7.0.0%2Br33-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 30,092 kB
  • sloc: ansic: 161,291; java: 15,681; cpp: 7,721; xml: 6,419; python: 5,456; sh: 1,748; lisp: 261; ruby: 183; asm: 132; perl: 88; makefile: 22
file content (239 lines) | stat: -rwxr-xr-x 8,568 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** \file
  This file consists of implementation of class AdbWinUsbEndpointObject that
  encapsulates a handle opened to a WinUsb endpoint on our device.
*/

#include "stdafx.h"
#include "adb_winusb_endpoint_object.h"
#include "adb_winusb_io_completion.h"

AdbWinUsbEndpointObject::AdbWinUsbEndpointObject(
    AdbWinUsbInterfaceObject* parent_interf,
    UCHAR endpoint_id,
    UCHAR endpoint_index)
    : AdbEndpointObject(parent_interf, endpoint_id, endpoint_index),
    lock_(), is_closing_(false), pending_io_count_(0) {
}

AdbWinUsbEndpointObject::~AdbWinUsbEndpointObject() {
}

LONG AdbWinUsbEndpointObject::Release() {
  ATLASSERT(ref_count_ > 0);
  LONG ret = InterlockedDecrement(&ref_count_);
  ATLASSERT(ret >= 0);
  if (0 == ret) {
    LastReferenceReleased();
    delete this;
  }
  return ret;
}

bool AdbWinUsbEndpointObject::CloseHandle() {
  // This method only returns once all pending IOs are aborted and after
  // preventing future pending IOs. This means that once CloseHandle()
  // returns, threads using this object won't be using
  // parent_winusb_interface()->winusb_handle(), so it can then be safely
  // released.
  lock_.Lock();
  if (!is_closing_) {
    // Set flag to prevent new I/Os from starting up.
    is_closing_ = true;
  }

  // While there are pending IOs, keep aborting the pipe. We have to do this
  // repeatedly because pending_ios_ is incremented before the IO has actually
  // started, and abort (probably) only works if the IO has been started.
  while (pending_io_count_ > 0) {
    lock_.Unlock();

    // It has been noticed that on Windows 7, if you only call
    // WinUsb_AbortPipe(), without first calling WinUsb_ResetPipe(), the call
    // to WinUsb_AbortPipe() hangs.
    if (!WinUsb_ResetPipe(parent_winusb_interface()->winusb_handle(),
                          endpoint_id()) ||
        !WinUsb_AbortPipe(parent_winusb_interface()->winusb_handle(),
                          endpoint_id())) {
      // Reset or Abort failed for unexpected reason. We might not be able to
      // abort pending IOs, so we shouldn't keep polling pending_io_count_ or
      // else we might hang forever waiting for the IOs to abort. In this
      // situation it is preferable to risk a race condition (which may or may
      // not crash) and just break now.
      lock_.Lock();
      break;
    }

    // Give the IO threads time to break out of I/O calls and decrement
    // pending_io_count_. They should finish up pretty quick. The amount of time
    // "wasted" here (as opposed to if we did synchronization with an event)
    // doesn't really matter since this is an uncommon corner-case.
    Sleep(16);  // 16 ms, old default OS scheduler granularity

    lock_.Lock();
  }

  lock_.Unlock();

  return AdbEndpointObject::CloseHandle();
}

ADBAPIHANDLE AdbWinUsbEndpointObject::CommonAsyncReadWrite(
    bool is_read,
    void* buffer,
    ULONG bytes_to_transfer,
    ULONG* bytes_transferred,
    HANDLE event_handle,
    ULONG time_out) {
  // TODO: Do synchronization with is_closing_ and pending_io_count_ like
  // CommonSyncReadWrite(). This is not yet implemented because there are no
  // callers to Adb{Read,Write}EndpointAsync() in AOSP, and hence no testing.
  if (!SetTimeout(time_out))
    return false;

  // Create completion i/o object
  AdbIOCompletion* adb_io_completion = NULL;

  try {
    adb_io_completion = new AdbWinUsbIOCompletion(this,
                                                  bytes_to_transfer,
                                                  event_handle);
  } catch (... ) {
    SetLastError(ERROR_OUTOFMEMORY);
    return NULL;
  }

  // Create a handle for it
  ADBAPIHANDLE ret = adb_io_completion->CreateHandle();
  ULONG transferred = 0;
  if (NULL != ret) {
    BOOL res = TRUE;
    // Go the read / write file way
    res = is_read ?
        WinUsb_ReadPipe(parent_winusb_interface()->winusb_handle(),
                        endpoint_id(),
                        reinterpret_cast<PUCHAR>(buffer),
                        bytes_to_transfer,
                        &transferred,
                        adb_io_completion->overlapped()) :
        WinUsb_WritePipe(parent_winusb_interface()->winusb_handle(),
                         endpoint_id(),
                         reinterpret_cast<PUCHAR>(buffer),
                         bytes_to_transfer,
                         &transferred,
                         adb_io_completion->overlapped());

    if (NULL != bytes_transferred)
      *bytes_transferred = transferred;

    ULONG error = GetLastError();
    if (!res && (ERROR_IO_PENDING != error)) {
      // I/O failed immediatelly. We need to close i/o completion object
      // before we return NULL to the caller.
      adb_io_completion->CloseHandle();
      ret = NULL;
      SetLastError(error);
    }
  }

  // Offseting 'new'
  adb_io_completion->Release();

  return ret;
}

bool AdbWinUsbEndpointObject::CommonSyncReadWrite(bool is_read,
                                                  void* buffer,
                                                  ULONG bytes_to_transfer,
                                                  ULONG* bytes_transferred,
                                                  ULONG time_out) {
  lock_.Lock();
  if (is_closing_) {
    lock_.Unlock();
    // AdbCloseHandle() is in progress, so don't start up any new IOs.
    SetLastError(ERROR_HANDLES_CLOSED);
    return false;
  } else {
    // Not closing down, so record the fact that we're doing IO. This will
    // prevent CloseHandle() from returning until our IO completes or it aborts
    // our IO.
    ++pending_io_count_;
    lock_.Unlock();
  }

  // Because we've incremented pending_ios_, do the matching decrement when this
  // object goes out of scope.
  DecrementPendingIO dec(this);

  if (!SetTimeout(time_out))
    return false;

  // This is synchronous I/O. Since we always open I/O items for
  // overlapped I/O we're obligated to always provide OVERLAPPED
  // structure to read / write routines. Prepare it now.
  OVERLAPPED overlapped;
  ZeroMemory(&overlapped, sizeof(overlapped));
  overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

  BOOL ret = TRUE;
  ULONG transferred = 0;
  // Go the read / write file way
  ret = is_read ?
        WinUsb_ReadPipe(parent_winusb_interface()->winusb_handle(),
                        endpoint_id(),
                        reinterpret_cast<PUCHAR>(buffer),
                        bytes_to_transfer,
                        &transferred,
                        &overlapped) :
        WinUsb_WritePipe(parent_winusb_interface()->winusb_handle(),
                         endpoint_id(),
                         reinterpret_cast<PUCHAR>(buffer),
                         bytes_to_transfer,
                         &transferred,
                         &overlapped);

  // Lets see the result
  if (!ret && (ERROR_IO_PENDING != GetLastError())) {
    // I/O failed.
    if (NULL != overlapped.hEvent)
      ::CloseHandle(overlapped.hEvent);
    return false;
  }

  // Lets wait till I/O completes
  ret = WinUsb_GetOverlappedResult(parent_winusb_interface()->winusb_handle(), &overlapped,
                                   &transferred, TRUE);
  if (ret && (NULL != bytes_transferred)) {
    *bytes_transferred = transferred;
  }

  if (NULL != overlapped.hEvent)
    ::CloseHandle(overlapped.hEvent);

  return ret ? true : false;
}

bool AdbWinUsbEndpointObject::SetTimeout(ULONG timeout) {
  if (!WinUsb_SetPipePolicy(parent_winusb_interface()->winusb_handle(),
                            endpoint_id(), PIPE_TRANSFER_TIMEOUT,
                            sizeof(ULONG), &timeout)) {
    return false;
  }

  return true;
}