void usb_kick(usb_handle* handle) { if (NULL != handle) { usb_cleanup_handle(handle); } else { SetLastError(ERROR_INVALID_HANDLE); errno = ERROR_INVALID_HANDLE; } }
int usb_close(usb_handle* handle) { DBG("usb_close\n"); if (NULL != handle) { // Cleanup handle usb_cleanup_handle(handle); free(handle); } return 0; }
int WindowsUsbTransport::Close() { DBG("usb_close\n"); if (nullptr != handle_) { // Cleanup handle usb_cleanup_handle(handle_.get()); handle_.reset(); } return 0; }
void usb_kick(usb_handle *handle, bool (*close_handle_func)(ADBAPIHANDLE)) { if (NULL != handle) { adb_mutex_lock(&usb_lock); usb_cleanup_handle(handle, close_handle_func, "usb_kick2"); adb_mutex_unlock(&usb_lock); } else { SetLastError(ERROR_INVALID_HANDLE); errno = ERROR_INVALID_HANDLE; } }
std::unique_ptr<usb_handle> do_usb_open(const wchar_t* interface_name) { // Allocate our handle std::unique_ptr<usb_handle> ret(new usb_handle); // Create interface. ret->adb_interface = AdbCreateInterfaceByName(interface_name); if (nullptr == ret->adb_interface) { errno = GetLastError(); return nullptr; } // Open read pipe (endpoint) ret->adb_read_pipe = AdbOpenDefaultBulkReadEndpoint(ret->adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite); if (nullptr != ret->adb_read_pipe) { // Open write pipe (endpoint) ret->adb_write_pipe = AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite); if (nullptr != ret->adb_write_pipe) { // Save interface name unsigned long name_len = 0; // First get expected name length AdbGetInterfaceName(ret->adb_interface, nullptr, &name_len, true); if (0 != name_len) { // Now save the name ret->interface_name.resize(name_len); if (AdbGetInterfaceName(ret->adb_interface, &ret->interface_name[0], &name_len, true)) { // We're done at this point return ret; } } } } // Something went wrong. errno = GetLastError(); usb_cleanup_handle(ret.get()); SetLastError(errno); return nullptr; }
static usb_handle *find_usb_device(ifc_match_func callback) { usb_handle* handle = NULL; char entry_buffer[2048]; char interf_name[2048]; AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]); unsigned long entry_buffer_size = sizeof(entry_buffer); char* copy_name; // Enumerate all present and active interfaces. ADBAPIHANDLE enum_handle = AdbEnumInterfaces(usb_class_id, true, true, true); if (NULL == enum_handle) return NULL; while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) { // TODO(vchtchetkine): FIXME - temp hack converting wchar_t into char. // It would be better to change AdbNextInterface so it will return // interface name as single char string. const wchar_t* wchar_name = next_interface->device_name; for(copy_name = interf_name; L'\0' != *wchar_name; wchar_name++, copy_name++) { *copy_name = (char)(*wchar_name); } *copy_name = '\0'; handle = do_usb_open(next_interface->device_name); if (NULL != handle) { // Lets see if this interface (device) belongs to us if (recognized_device(handle, callback)) { // found it! break; } else { usb_cleanup_handle(handle); free(handle); handle = NULL; } } entry_buffer_size = sizeof(entry_buffer); } AdbCloseHandle(enum_handle); return handle; }
int usb_close(usb_handle* handle) { D("usb_close"); if (nullptr != handle) { // Remove handle from the list { std::lock_guard<std::mutex> lock(usb_lock); handle_list.erase(std::remove(handle_list.begin(), handle_list.end(), handle), handle_list.end()); } // Cleanup handle usb_cleanup_handle(handle); free(handle); } return 0; }
int usb_close(usb_handle* handle, bool (*close_handle_func)(ADBAPIHANDLE)) { D("usb_close\n"); if (NULL != handle) { // Remove handle from the list adb_mutex_lock(&usb_lock); if ((handle->next != handle) && (handle->prev != handle)) { handle->next->prev = handle->prev; handle->prev->next = handle->next; handle->prev = handle; handle->next = handle; } adb_mutex_unlock(&usb_lock); // Cleanup handle usb_cleanup_handle(handle, close_handle_func, "usb_close3"); free(handle); } return 0; }
usb_handle* do_usb_open(const wchar_t* interface_name) { // Allocate our handle usb_handle* ret = (usb_handle*)malloc(sizeof(usb_handle)); if (NULL == ret) return NULL; // Create interface. ret->adb_interface = AdbCreateInterfaceByName(interface_name); if (NULL == ret->adb_interface) { free(ret); errno = GetLastError(); return NULL; } // Open read pipe (endpoint) ret->adb_read_pipe = AdbOpenDefaultBulkReadEndpoint(ret->adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite); if (NULL != ret->adb_read_pipe) { // Open write pipe (endpoint) ret->adb_write_pipe = AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite); if (NULL != ret->adb_write_pipe) { // Save interface name unsigned long name_len = 0; // First get expected name length AdbGetInterfaceName(ret->adb_interface, NULL, &name_len, true); if (0 != name_len) { ret->interface_name = (char*)malloc(name_len); if (NULL != ret->interface_name) { // Now save the name if (AdbGetInterfaceName(ret->adb_interface, ret->interface_name, &name_len, true)) { // We're done at this point return ret; } } else { SetLastError(ERROR_OUTOFMEMORY); } } } } // Something went wrong. errno = GetLastError(); usb_cleanup_handle(ret); free(ret); SetLastError(errno); return NULL; }
void find_devices() { usb_handle* handle = NULL; char entry_buffer[2048]; char interf_name[2048]; AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]); unsigned long entry_buffer_size = sizeof(entry_buffer); char* copy_name; // Enumerate all present and active interfaces. ADBAPIHANDLE enum_handle = bridge->AdbEnumInterfaces(usb_class_id, true, true, true); if (NULL == enum_handle) return; while (bridge->AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) { // TODO: FIXME - temp hack converting wchar_t into char. // It would be better to change AdbNextInterface so it will return // interface name as single char string. const wchar_t* wchar_name = next_interface->device_name; for(copy_name = interf_name; L'\0' != *wchar_name; wchar_name++, copy_name++) { *copy_name = (char)(*wchar_name); } *copy_name = '\0'; // Lets see if we already have this device in the list if (!known_device(interf_name)) { // This seems to be a new device. Open it! handle = do_usb_open(next_interface->device_name); if (NULL != handle) { // Lets see if this interface (device) belongs to us if (recognized_device(handle)) { D("adding a new device %s\n", interf_name); char serial_number[512]; unsigned long serial_number_len = sizeof(serial_number); if (bridge->AdbGetSerialNumber(handle->adb_interface, serial_number, &serial_number_len, true)) { // Lets make sure that we don't duplicate this device if (register_new_device(handle)) { register_usb_transport(handle, serial_number, NULL, 1); } else { D("register_new_device failed for %s\n", interf_name); usb_cleanup_handle(handle, bridge->AdbCloseHandle, "bridge4"); free(handle); } } else { D("cannot get serial number\n"); usb_cleanup_handle(handle, bridge->AdbCloseHandle, "bridge5"); free(handle); } } else { usb_cleanup_handle(handle, bridge->AdbCloseHandle, "bridge6"); free(handle); } } } entry_buffer_size = sizeof(entry_buffer); } bridge->AdbCloseHandle(enum_handle); }
void find_devices() { usb_handle* handle = nullptr; char entry_buffer[2048]; AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]); unsigned long entry_buffer_size = sizeof(entry_buffer); // Enumerate all present and active interfaces. ADBAPIHANDLE enum_handle = AdbEnumInterfaces(usb_class_id, true, true, true); if (nullptr == enum_handle) { D("AdbEnumInterfaces failed: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); return; } while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) { // Lets see if we already have this device in the list if (!known_device(next_interface->device_name)) { // This seems to be a new device. Open it! handle = do_usb_open(next_interface->device_name); if (nullptr != handle) { // Lets see if this interface (device) belongs to us if (recognized_device(handle)) { D("adding a new device %ls", next_interface->device_name); // We don't request a wchar_t string from AdbGetSerialNumber() because of a bug // in adb_winusb_interface.cpp:CopyMemory(buffer, ser_num->bString, // bytes_written) where the last parameter should be (str_len * // sizeof(wchar_t)). The bug reads 2 bytes past the end of a stack buffer in the // best case, and in the unlikely case of a long serial number, it will read 2 // bytes past the end of a heap allocation. This doesn't affect the resulting // string, but we should avoid the bad reads in the first place. char serial_number[512]; unsigned long serial_number_len = sizeof(serial_number); if (AdbGetSerialNumber(handle->adb_interface, serial_number, &serial_number_len, true)) { // Lets make sure that we don't duplicate this device if (register_new_device(handle)) { register_usb_transport(handle, serial_number, nullptr, 1); } else { D("register_new_device failed for %ls", next_interface->device_name); usb_cleanup_handle(handle); free(handle); } } else { D("cannot get serial number: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); usb_cleanup_handle(handle); free(handle); } } else { usb_cleanup_handle(handle); free(handle); } } } entry_buffer_size = sizeof(entry_buffer); } if (GetLastError() != ERROR_NO_MORE_ITEMS) { // Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration. D("AdbNextInterface failed: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); } _adb_close_handle(enum_handle); }
static void usb_kick_locked(usb_handle* handle) { // The reason the lock must be acquired before calling this function is in // case multiple threads are trying to kick the same device at the same time. usb_cleanup_handle(handle); }
usb_handle* do_usb_open(const wchar_t* interface_name) { unsigned long name_len = 0; // Allocate our handle usb_handle* ret = (usb_handle*)calloc(1, sizeof(usb_handle)); if (nullptr == ret) { D("Could not allocate %u bytes for usb_handle: %s", sizeof(usb_handle), strerror(errno)); goto fail; } // Create interface. ret->adb_interface = AdbCreateInterfaceByName(interface_name); if (nullptr == ret->adb_interface) { D("AdbCreateInterfaceByName failed: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); goto fail; } // Open read pipe (endpoint) ret->adb_read_pipe = AdbOpenDefaultBulkReadEndpoint( ret->adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite); if (nullptr == ret->adb_read_pipe) { D("AdbOpenDefaultBulkReadEndpoint failed: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); goto fail; } // Open write pipe (endpoint) ret->adb_write_pipe = AdbOpenDefaultBulkWriteEndpoint( ret->adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite); if (nullptr == ret->adb_write_pipe) { D("AdbOpenDefaultBulkWriteEndpoint failed: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); goto fail; } // Save interface name // First get expected name length AdbGetInterfaceName(ret->adb_interface, nullptr, &name_len, false); if (0 == name_len) { D("AdbGetInterfaceName returned name length of zero: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); goto fail; } ret->interface_name = (wchar_t*)malloc(name_len * sizeof(ret->interface_name[0])); if (nullptr == ret->interface_name) { D("Could not allocate %lu characters for interface_name: %s", name_len, strerror(errno)); goto fail; } // Now save the name if (!AdbGetInterfaceName(ret->adb_interface, ret->interface_name, &name_len, false)) { D("AdbGetInterfaceName failed: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); goto fail; } // We're done at this point return ret; fail: if (nullptr != ret) { usb_cleanup_handle(ret); free(ret); } return nullptr; }