void kill_io_pump(atransport * t, bool (*close_handle_func)(ADBAPIHANDLE)){ if (!t) { // Nothing to do. return; } #ifdef WIN32 usb_kick(t->usb, close_handle_func); #else usb_kick(t->usb); #endif }
ssize_t WindowsUsbTransport::Read(void* data, size_t len) { unsigned long time_out = 0; unsigned long read = 0; int ret; DBG("usb_read %d\n", len); if (nullptr != handle_) { while (1) { int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; ret = AdbReadEndpointSync(handle_->adb_read_pipe, data, xfer, &read, time_out); errno = GetLastError(); DBG("usb_read got: %ld, expected: %d, errno: %d\n", read, xfer, errno); if (ret) { return read; } else { // assume ERROR_INVALID_HANDLE indicates we are disconnected if (errno == ERROR_INVALID_HANDLE) usb_kick(handle_.get()); break; } // else we timed out - try again } } else { DBG("usb_read NULL handle\n"); SetLastError(ERROR_INVALID_HANDLE); } DBG("usb_read failed: %d\n", errno); return -1; }
int usb_write(usb_handle* handle, const void* data, int len) { unsigned long time_out = 5000; unsigned long written = 0; int err = 0; D("usb_write %d", len); if (nullptr == handle) { D("usb_write was passed NULL handle"); err = EINVAL; goto fail; } // Perform write if (!AdbWriteEndpointSync(handle->adb_write_pipe, (void*)data, (unsigned long)len, &written, time_out)) { D("AdbWriteEndpointSync failed: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); err = EIO; goto fail; } // Make sure that we've written what we were asked to write D("usb_write got: %ld, expected: %d", written, len); if (written != (unsigned long)len) { // If this occurs, this code should be changed to repeatedly call // AdbWriteEndpointSync() until all bytes are written. D("AdbWriteEndpointSync was supposed to write %d, but only wrote %ld", len, written); err = EIO; goto fail; } if (handle->zero_mask && (len & handle->zero_mask) == 0) { // Send a zero length packet unsigned long dummy; if (!AdbWriteEndpointSync(handle->adb_write_pipe, (void*)data, 0, &dummy, time_out)) { D("AdbWriteEndpointSync of zero length packet failed: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); err = EIO; goto fail; } } return written; fail: // Any failure should cause us to kick the device instead of leaving it a // zombie state with potential to hang. if (nullptr != handle) { D("Kicking device due to error in usb_write"); usb_kick(handle); } D("usb_write failed"); errno = err; return -1; }
static void AndroidInterfaceNotify(void *refCon, io_service_t service, natural_t messageType, void *messageArgument) { usb_handle *handle = (usb_handle *)refCon; if (messageType == kIOMessageServiceIsTerminated) { if (!handle) { DBG("ERR: NULL handle\n"); return; } DBG("AndroidInterfaceNotify\n"); IOObjectRelease(handle->usbNotification); usb_kick(handle); } }
int usb_write(usb_handle* handle, const void* data, int len) { unsigned long time_out = 5000; unsigned long written = 0; int ret; D("usb_write %d\n", len); if (NULL != handle) { D("Before bridge->AdbWriteEndpointSync\n"); // Perform write ret = i_bridge->AdbWriteEndpointSync(handle->adb_write_pipe, (void*)data, (unsigned long)len, &written, time_out); D("After bridge->AdbWriteEndpointSync\n"); int saved_errno = GetLastError(); if (ret) { // Make sure that we've written what we were asked to write D("usb_write got: %ld, expected: %d\n", written, len); if (written == (unsigned long)len) { if(handle->zero_mask && (len & handle->zero_mask) == 0) { // Send a zero length packet i_bridge->AdbWriteEndpointSync(handle->adb_write_pipe, (void*)data, 0, &written, time_out); } return 0; } } else { // assume ERROR_INVALID_HANDLE indicates we are disconnected if (saved_errno == ERROR_INVALID_HANDLE) usb_kick(handle, i_bridge->AdbCloseHandle); } errno = saved_errno; } else { D("usb_write NULL handle\n"); SetLastError(ERROR_INVALID_HANDLE); } D("usb_write failed: %d\n", errno); return -1; }
int usb_write(usb_handle* handle, const void* data, int len) { unsigned long time_out = 500 + len * 8; unsigned long written = 0; unsigned count = 0; int ret; DBG("usb_write %d\n", len); if (NULL != handle) { // Perform write while(len > 0) { int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; ret = AdbWriteEndpointSync(handle->adb_write_pipe, (void*)data, (unsigned long)xfer, &written, time_out); errno = GetLastError(); DBG("AdbWriteEndpointSync returned %d, errno: %d\n", ret, errno); if (ret == 0) { // assume ERROR_INVALID_HANDLE indicates we are disconnected if (errno == ERROR_INVALID_HANDLE) usb_kick(handle); return -1; } count += written; len -= written; data += written; if (len == 0) return count; } } else { DBG("usb_write NULL handle\n"); SetLastError(ERROR_INVALID_HANDLE); } DBG("usb_write failed: %d\n", errno); return -1; }
int usb_read(usb_handle* handle, void* data, int len) { unsigned long time_out = 0; unsigned long read = 0; int err = 0; int orig_len = len; D("usb_read %d", len); if (nullptr == handle) { D("usb_read was passed NULL handle"); err = EINVAL; goto fail; } while (len == orig_len) { if (!AdbReadEndpointSync(handle->adb_read_pipe, data, len, &read, time_out)) { D("AdbReadEndpointSync failed: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); err = EIO; goto fail; } D("usb_read got: %ld, expected: %d", read, len); data = (char*)data + read; len -= read; } return orig_len - len; fail: // Any failure should cause us to kick the device instead of leaving it a // zombie state with potential to hang. if (nullptr != handle) { D("Kicking device due to error in usb_read"); usb_kick(handle); } D("usb_read failed"); errno = err; return -1; }
ssize_t WindowsUsbTransport::Write(const void* data, size_t len) { unsigned long time_out = 5000; unsigned long written = 0; unsigned count = 0; int ret; DBG("usb_write %d\n", len); if (nullptr != handle_) { // Perform write while(len > 0) { int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; ret = AdbWriteEndpointSync(handle_->adb_write_pipe, const_cast<void*>(data), xfer, &written, time_out); errno = GetLastError(); DBG("AdbWriteEndpointSync returned %d, errno: %d\n", ret, errno); if (ret == 0) { // assume ERROR_INVALID_HANDLE indicates we are disconnected if (errno == ERROR_INVALID_HANDLE) usb_kick(handle_.get()); return -1; } count += written; len -= written; data = (const char *)data + written; if (len == 0) return count; } } else { DBG("usb_write NULL handle\n"); SetLastError(ERROR_INVALID_HANDLE); } DBG("usb_write failed: %d\n", errno); return -1; }
int usb_read(usb_handle *handle, void* data, int len) { unsigned long time_out = 500 + len * 8; unsigned long read = 0; int ret; DBG("usb_read %d\n", len); if (NULL != handle) { while (1) { int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; ret = AdbReadEndpointSync(handle->adb_read_pipe, (void*)data, (unsigned long)xfer, &read, time_out); errno = GetLastError(); DBG("usb_read got: %ld, expected: %d, errno: %d\n", read, xfer, errno); if (ret) { return read; } else if (errno != ERROR_SEM_TIMEOUT) { // assume ERROR_INVALID_HANDLE indicates we are disconnected if (errno == ERROR_INVALID_HANDLE) usb_kick(handle); break; } // else we timed out - try again } } else { DBG("usb_read NULL handle\n"); SetLastError(ERROR_INVALID_HANDLE); } DBG("usb_read failed: %d\n", errno); return -1; }
static void remote_kick(atransport *t) { usb_kick(t->usb); }
void UsbConnection::Close() { usb_kick(handle_); }
void UsbConnection::Reset() { usb_reset(handle_); usb_kick(handle_); }
void usb_reset(usb_handle* handle) { // Unimplemented on Windows. usb_kick(handle); }