void usb_cleanup_handle(usb_handle* handle) { if (NULL != handle) { if (NULL != handle->adb_write_pipe) AdbCloseHandle(handle->adb_write_pipe); if (NULL != handle->adb_read_pipe) AdbCloseHandle(handle->adb_read_pipe); if (NULL != handle->adb_interface) AdbCloseHandle(handle->adb_interface); handle->interface_name.clear(); handle->adb_write_pipe = NULL; handle->adb_read_pipe = NULL; handle->adb_interface = NULL; } }
bool TestInterfaces() { // Enumerate interfaces ADBAPIHANDLE enum_handle = AdbEnumInterfaces(kAdbInterfaceId, true, true, true); if (NULL == enum_handle) { printf("\nTest interfaces failure:"); printf("\nUnable to enumerate ADB interfaces: %u", GetLastError()); return false; } // Unite interface info structure and buffer big enough to contain the // largest structure. union { AdbInterfaceInfo interface_info; char buf[4096]; }; unsigned long buf_size = sizeof(buf); // Test each found interface while (AdbNextInterface(enum_handle, &interface_info, &buf_size)) { TestInterface(interface_info.device_name); buf_size = sizeof(buf); }; AdbCloseHandle(enum_handle); // Create interface by VID/PID/MI ADBAPIHANDLE interface_handle = AdbCreateInterface(kAdbInterfaceId, DEVICE_VENDOR_ID, DEVICE_COMPOSITE_PRODUCT_ID, DEVICE_INTERFACE_ID); if (NULL == interface_handle) { printf("\nUnable to create interface by VID/PID: %u", GetLastError()); return false; } // Test it TestInterfaceHandle(interface_handle); AdbCloseHandle(interface_handle); return true; }
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; }
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; }
bool TestEnumInterfaces() { // Enumerate interfaces ADBAPIHANDLE enum_handle = AdbEnumInterfaces(kAdbInterfaceId, true, true, true); if (NULL == enum_handle) { printf("\nEnum interfaces failure:"); printf("\nUnable to enumerate ADB interfaces: %u", GetLastError()); return false; } // Unite interface info structure and buffer big enough to contain the // largest structure. union { AdbInterfaceInfo interface_info; char buf[4096]; }; unsigned long buf_size = sizeof(buf); // Enumerate (and count) interfaces, printing information for each found // interface. interface_count = 0; while (AdbNextInterface(enum_handle, &interface_info, &buf_size)) { interface_count++; printf("\nFound interface %ws:", interface_info.device_name); if (interface_info.flags & SPINT_ACTIVE) printf(" ACTIVE"); if (interface_info.flags & SPINT_DEFAULT) printf(" DEFAULT"); if (interface_info.flags & SPINT_REMOVED) printf(" REMOVED"); buf_size = sizeof(buf);; }; AdbCloseHandle(enum_handle); return true; }
bool DeviceHandShake(ADBAPIHANDLE adb_interface) { // Get interface name char interf_name[512]; unsigned long name_size = sizeof(interf_name); if (!AdbGetInterfaceName(adb_interface, interf_name, &name_size, true)) { printf("\nDeviceHandShake: AdbGetInterfaceName returned error %u", GetLastError()); return false; } printf("\n\nDeviceHandShake on %s", interf_name); char* ser_num = NULL; name_size = 0; if (!AdbGetSerialNumber(adb_interface, ser_num, &name_size, true)) { ser_num = reinterpret_cast<char*>(malloc(name_size)); if (NULL != ser_num) { if (!AdbGetSerialNumber(adb_interface, ser_num, &name_size, true)) { printf("\n AdbGetSerialNumber returned error %u", GetLastError()); AdbCloseHandle(adb_interface); return false; } printf("\nInterface serial number is %s", ser_num); free(ser_num); } } // Get default read endpoint ADBAPIHANDLE adb_read = AdbOpenDefaultBulkReadEndpoint(adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite); if (NULL == adb_read) { printf("\n AdbOpenDefaultBulkReadEndpoint returned error %u", GetLastError()); return false; } // Get default write endpoint ADBAPIHANDLE adb_write = AdbOpenDefaultBulkWriteEndpoint(adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite); if (NULL == adb_write) { printf("\n AdbOpenDefaultBulkWriteEndpoint returned error %u", GetLastError()); AdbCloseHandle(adb_read); return false; } // Send connect message message msg_send; msg_send.command = A_CNXN; msg_send.arg0 = A_VERSION; msg_send.arg1 = MAX_PAYLOAD; msg_send.data_length = 0; msg_send.data_crc32 = 0; msg_send.magic = msg_send.command ^ 0xffffffff; ULONG written_bytes = 0; bool write_res = AdbWriteEndpointSync(adb_write, &msg_send, sizeof(msg_send), &written_bytes, 500); if (!write_res) { printf("\n AdbWriteEndpointSync returned error %u", GetLastError()); AdbCloseHandle(adb_write); AdbCloseHandle(adb_read); return false; } // Receive handshake message msg_rcv; ULONG read_bytes = 0; bool read_res = AdbReadEndpointSync(adb_read, &msg_rcv, sizeof(msg_rcv), &read_bytes, 512); if (!read_res) { printf("\n AdbReadEndpointSync returned error %u", GetLastError()); AdbCloseHandle(adb_write); AdbCloseHandle(adb_read); return false; } printf("\n Read handshake: %u bytes received", read_bytes); char* cmd_ansi = reinterpret_cast<char*>(&msg_rcv.command); printf("\n command = %08X (%c%c%c%c)", msg_rcv.command, cmd_ansi[0], cmd_ansi[1], cmd_ansi[2], cmd_ansi[3]); printf("\n arg0 = %08X", msg_rcv.arg0); printf("\n arg1 = %08X", msg_rcv.arg1); printf("\n data_length = %u", msg_rcv.data_length); printf("\n data_crc32 = %08X", msg_rcv.data_crc32); printf("\n magic = %08X", msg_rcv.magic); if (0 != msg_rcv.data_length) { char* buf = reinterpret_cast<char*>(malloc(msg_rcv.data_length)); read_res = AdbReadEndpointSync(adb_read, buf, msg_rcv.data_length, &read_bytes, 512); if (!read_res) { printf("\n AdbReadEndpointSync (data) returned error %u", GetLastError()); free(buf); AdbCloseHandle(adb_write); AdbCloseHandle(adb_read); return false; } for (ULONG n = 0; n < read_bytes; n++) { if (0 == (n % 16)) printf("\n "); printf("%02X ", buf[n]); } printf("\n %s", buf); delete buf; } AdbCloseHandle(adb_write); AdbCloseHandle(adb_read); return true; }
// Wrapper around AdbCloseHandle() that logs diagnostics. static void _adb_close_handle(ADBAPIHANDLE adb_handle) { if (!AdbCloseHandle(adb_handle)) { D("AdbCloseHandle(%p) failed: %s", adb_handle, android::base::SystemErrorCodeToString(GetLastError()).c_str()); } }