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; }
bool TestInterface(const wchar_t* device_name) { printf("\n*** Test interface( %ws )", device_name); // Get ADB handle to the interface by its name ADBAPIHANDLE interface_handle = AdbCreateInterfaceByName(device_name); if (NULL == interface_handle) { printf(" FAILED:\nUnable to create interface by name: %u", GetLastError()); return false; } // Test it TestInterfaceHandle(interface_handle); AdbCloseHandle(interface_handle); return true; }
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; }
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; }