Merge changes I3e6e5a22,I6eb3f066
* changes: adb: cleanup some portions of client usb interfaces adb: clang-format for adb.h and client/usb_windows.cpp
This commit is contained in:
commit
75836d5e7f
4 changed files with 429 additions and 465 deletions
73
adb/adb.h
73
adb/adb.h
|
|
@ -67,9 +67,8 @@ struct amessage {
|
||||||
uint32_t magic; /* command ^ 0xffffffff */
|
uint32_t magic; /* command ^ 0xffffffff */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct apacket
|
struct apacket {
|
||||||
{
|
apacket* next;
|
||||||
apacket *next;
|
|
||||||
|
|
||||||
size_t len;
|
size_t len;
|
||||||
char* ptr;
|
char* ptr;
|
||||||
|
|
@ -85,13 +84,11 @@ uint32_t calculate_apacket_checksum(const apacket* packet);
|
||||||
** this should be used to cleanup objects that depend on the
|
** this should be used to cleanup objects that depend on the
|
||||||
** transport (e.g. remote sockets, listeners, etc...)
|
** transport (e.g. remote sockets, listeners, etc...)
|
||||||
*/
|
*/
|
||||||
struct adisconnect
|
struct adisconnect {
|
||||||
{
|
void (*func)(void* opaque, atransport* t);
|
||||||
void (*func)(void* opaque, atransport* t);
|
void* opaque;
|
||||||
void* opaque;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// A transport object models the connection to a remote device or emulator there
|
// A transport object models the connection to a remote device or emulator there
|
||||||
// is one transport per connected device/emulator. A "local transport" connects
|
// is one transport per connected device/emulator. A "local transport" connects
|
||||||
// through TCP (for the emulator), while a "usb transport" through USB (for real
|
// through TCP (for the emulator), while a "usb transport" through USB (for real
|
||||||
|
|
@ -121,15 +118,14 @@ enum ConnectionState {
|
||||||
kCsUnauthorized,
|
kCsUnauthorized,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void print_packet(const char* label, apacket* p);
|
||||||
void print_packet(const char *label, apacket *p);
|
|
||||||
|
|
||||||
// These use the system (v)fprintf, not the adb prefixed ones defined in sysdeps.h, so they
|
// These use the system (v)fprintf, not the adb prefixed ones defined in sysdeps.h, so they
|
||||||
// shouldn't be tagged with ADB_FORMAT_ARCHETYPE.
|
// shouldn't be tagged with ADB_FORMAT_ARCHETYPE.
|
||||||
void fatal(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
|
void fatal(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
|
||||||
void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
|
void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
|
||||||
|
|
||||||
void handle_packet(apacket *p, atransport *t);
|
void handle_packet(apacket* p, atransport* t);
|
||||||
|
|
||||||
int launch_server(const std::string& socket_spec);
|
int launch_server(const std::string& socket_spec);
|
||||||
int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd);
|
int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd);
|
||||||
|
|
@ -138,7 +134,7 @@ int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply
|
||||||
#if ADB_HOST
|
#if ADB_HOST
|
||||||
int get_available_local_transport_index();
|
int get_available_local_transport_index();
|
||||||
#endif
|
#endif
|
||||||
int init_socket_transport(atransport *t, int s, int port, int local);
|
int init_socket_transport(atransport* t, int s, int port, int local);
|
||||||
void init_usb_transport(atransport* t, usb_handle* usb);
|
void init_usb_transport(atransport* t, usb_handle* usb);
|
||||||
|
|
||||||
std::string getEmulatorSerialString(int console_port);
|
std::string getEmulatorSerialString(int console_port);
|
||||||
|
|
@ -153,77 +149,78 @@ asocket* host_service_to_socket(const char* name, const char* serial, TransportI
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !ADB_HOST
|
#if !ADB_HOST
|
||||||
int init_jdwp(void);
|
int init_jdwp(void);
|
||||||
asocket* create_jdwp_service_socket();
|
asocket* create_jdwp_service_socket();
|
||||||
asocket* create_jdwp_tracker_service_socket();
|
asocket* create_jdwp_tracker_service_socket();
|
||||||
int create_jdwp_connection_fd(int jdwp_pid);
|
int create_jdwp_connection_fd(int jdwp_pid);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int handle_forward_request(const char* service, TransportType type, const char* serial,
|
int handle_forward_request(const char* service, TransportType type, const char* serial,
|
||||||
TransportId transport_id, int reply_fd);
|
TransportId transport_id, int reply_fd);
|
||||||
|
|
||||||
#if !ADB_HOST
|
#if !ADB_HOST
|
||||||
void framebuffer_service(int fd, void *cookie);
|
void framebuffer_service(int fd, void* cookie);
|
||||||
void set_verity_enabled_state_service(int fd, void* cookie);
|
void set_verity_enabled_state_service(int fd, void* cookie);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* packet allocator */
|
/* packet allocator */
|
||||||
apacket *get_apacket(void);
|
apacket* get_apacket(void);
|
||||||
void put_apacket(apacket *p);
|
void put_apacket(apacket* p);
|
||||||
|
|
||||||
// Define it if you want to dump packets.
|
// Define it if you want to dump packets.
|
||||||
#define DEBUG_PACKETS 0
|
#define DEBUG_PACKETS 0
|
||||||
|
|
||||||
#if !DEBUG_PACKETS
|
#if !DEBUG_PACKETS
|
||||||
#define print_packet(tag,p) do {} while (0)
|
#define print_packet(tag, p) \
|
||||||
|
do { \
|
||||||
|
} while (0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if ADB_HOST_ON_TARGET
|
#if ADB_HOST_ON_TARGET
|
||||||
/* adb and adbd are coexisting on the target, so use 5038 for adb
|
/* adb and adbd are coexisting on the target, so use 5038 for adb
|
||||||
* to avoid conflicting with adbd's usage of 5037
|
* to avoid conflicting with adbd's usage of 5037
|
||||||
*/
|
*/
|
||||||
# define DEFAULT_ADB_PORT 5038
|
#define DEFAULT_ADB_PORT 5038
|
||||||
#else
|
#else
|
||||||
# define DEFAULT_ADB_PORT 5037
|
#define DEFAULT_ADB_PORT 5037
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
|
#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
|
||||||
|
|
||||||
#define ADB_CLASS 0xff
|
#define ADB_CLASS 0xff
|
||||||
#define ADB_SUBCLASS 0x42
|
#define ADB_SUBCLASS 0x42
|
||||||
#define ADB_PROTOCOL 0x1
|
#define ADB_PROTOCOL 0x1
|
||||||
|
|
||||||
|
|
||||||
void local_init(int port);
|
void local_init(int port);
|
||||||
bool local_connect(int port);
|
bool local_connect(int port);
|
||||||
int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
|
int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
|
||||||
|
|
||||||
ConnectionState connection_state(atransport *t);
|
ConnectionState connection_state(atransport* t);
|
||||||
|
|
||||||
extern const char* adb_device_banner;
|
extern const char* adb_device_banner;
|
||||||
|
|
||||||
#if !ADB_HOST
|
#if !ADB_HOST
|
||||||
extern int SHELL_EXIT_NOTIFY_FD;
|
extern int SHELL_EXIT_NOTIFY_FD;
|
||||||
#endif // !ADB_HOST
|
#endif // !ADB_HOST
|
||||||
|
|
||||||
#define CHUNK_SIZE (64*1024)
|
#define CHUNK_SIZE (64 * 1024)
|
||||||
|
|
||||||
#if !ADB_HOST
|
#if !ADB_HOST
|
||||||
#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
|
#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
|
||||||
#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
|
#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH #x
|
||||||
|
|
||||||
#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
|
#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
|
||||||
#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
|
#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
|
||||||
#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
|
#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int handle_host_request(const char* service, TransportType type, const char* serial,
|
int handle_host_request(const char* service, TransportType type, const char* serial,
|
||||||
TransportId transport_id, int reply_fd, asocket* s);
|
TransportId transport_id, int reply_fd, asocket* s);
|
||||||
|
|
||||||
void handle_online(atransport *t);
|
void handle_online(atransport* t);
|
||||||
void handle_offline(atransport *t);
|
void handle_offline(atransport* t);
|
||||||
|
|
||||||
void send_connect(atransport *t);
|
void send_connect(atransport* t);
|
||||||
|
|
||||||
void parse_banner(const std::string&, atransport* t);
|
void parse_banner(const std::string&, atransport* t);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -253,7 +253,7 @@ static void find_usb_device(const std::string& base,
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
/* aproto 01 needs 0 termination */
|
/* aproto 01 needs 0 termination */
|
||||||
if (interface->bInterfaceProtocol == 0x01) {
|
if (interface->bInterfaceProtocol == ADB_PROTOCOL) {
|
||||||
max_packet_size = ep1->wMaxPacketSize;
|
max_packet_size = ep1->wMaxPacketSize;
|
||||||
zero_mask = ep1->wMaxPacketSize - 1;
|
zero_mask = ep1->wMaxPacketSize - 1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -174,7 +174,7 @@ AndroidInterfaceAdded(io_iterator_t iterator)
|
||||||
kr = (*iface)->GetInterfaceClass(iface, &if_class);
|
kr = (*iface)->GetInterfaceClass(iface, &if_class);
|
||||||
kr = (*iface)->GetInterfaceSubClass(iface, &subclass);
|
kr = (*iface)->GetInterfaceSubClass(iface, &subclass);
|
||||||
kr = (*iface)->GetInterfaceProtocol(iface, &protocol);
|
kr = (*iface)->GetInterfaceProtocol(iface, &protocol);
|
||||||
if(if_class != ADB_CLASS || subclass != ADB_SUBCLASS || protocol != ADB_PROTOCOL) {
|
if (!is_adb_interface(if_class, subclass, protocol)) {
|
||||||
// Ignore non-ADB devices.
|
// Ignore non-ADB devices.
|
||||||
LOG(DEBUG) << "Ignoring interface with incorrect class/subclass/protocol - " << if_class
|
LOG(DEBUG) << "Ignoring interface with incorrect class/subclass/protocol - " << if_class
|
||||||
<< ", " << subclass << ", " << protocol;
|
<< ", " << subclass << ", " << protocol;
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,10 @@
|
||||||
|
|
||||||
#include "sysdeps.h"
|
#include "sysdeps.h"
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
#include <winsock2.h> // winsock.h *must* be included before windows.h.
|
#include <winsock2.h> // winsock.h *must* be included before windows.h.
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
// clang-format on
|
||||||
#include <usb100.h>
|
#include <usb100.h>
|
||||||
#include <winerror.h>
|
#include <winerror.h>
|
||||||
|
|
||||||
|
|
@ -47,29 +49,29 @@
|
||||||
ability to break a thread out of pipe IO.
|
ability to break a thread out of pipe IO.
|
||||||
*/
|
*/
|
||||||
struct usb_handle {
|
struct usb_handle {
|
||||||
/// Previous entry in the list of opened usb handles
|
/// Previous entry in the list of opened usb handles
|
||||||
usb_handle *prev;
|
usb_handle* prev;
|
||||||
|
|
||||||
/// Next entry in the list of opened usb handles
|
/// Next entry in the list of opened usb handles
|
||||||
usb_handle *next;
|
usb_handle* next;
|
||||||
|
|
||||||
/// Handle to USB interface
|
/// Handle to USB interface
|
||||||
ADBAPIHANDLE adb_interface;
|
ADBAPIHANDLE adb_interface;
|
||||||
|
|
||||||
/// Handle to USB read pipe (endpoint)
|
/// Handle to USB read pipe (endpoint)
|
||||||
ADBAPIHANDLE adb_read_pipe;
|
ADBAPIHANDLE adb_read_pipe;
|
||||||
|
|
||||||
/// Handle to USB write pipe (endpoint)
|
/// Handle to USB write pipe (endpoint)
|
||||||
ADBAPIHANDLE adb_write_pipe;
|
ADBAPIHANDLE adb_write_pipe;
|
||||||
|
|
||||||
/// Interface name
|
/// Interface name
|
||||||
wchar_t* interface_name;
|
wchar_t* interface_name;
|
||||||
|
|
||||||
/// Maximum packet size.
|
/// Maximum packet size.
|
||||||
unsigned max_packet_size;
|
unsigned max_packet_size;
|
||||||
|
|
||||||
/// Mask for determining when to use zero length packets
|
/// Mask for determining when to use zero length packets
|
||||||
unsigned zero_mask;
|
unsigned zero_mask;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Class ID assigned to the device by androidusb.sys
|
/// Class ID assigned to the device by androidusb.sys
|
||||||
|
|
@ -77,8 +79,7 @@ static const GUID usb_class_id = ANDROID_USB_CLASS_ID;
|
||||||
|
|
||||||
/// List of opened usb handles
|
/// List of opened usb handles
|
||||||
static usb_handle handle_list = {
|
static usb_handle handle_list = {
|
||||||
.prev = &handle_list,
|
.prev = &handle_list, .next = &handle_list,
|
||||||
.next = &handle_list,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Locker for the list of opened usb handles
|
/// Locker for the list of opened usb handles
|
||||||
|
|
@ -118,7 +119,7 @@ usb_handle* do_usb_open(const wchar_t* interface_name);
|
||||||
int usb_write(usb_handle* handle, const void* data, int len);
|
int usb_write(usb_handle* handle, const void* data, int len);
|
||||||
|
|
||||||
/// Reads data using the opened usb handle
|
/// Reads data using the opened usb handle
|
||||||
int usb_read(usb_handle *handle, void* data, int len);
|
int usb_read(usb_handle* handle, void* data, int len);
|
||||||
|
|
||||||
/// Cleans up opened usb handle
|
/// Cleans up opened usb handle
|
||||||
void usb_cleanup_handle(usb_handle* handle);
|
void usb_cleanup_handle(usb_handle* handle);
|
||||||
|
|
@ -130,401 +131,374 @@ void usb_kick(usb_handle* handle);
|
||||||
int usb_close(usb_handle* handle);
|
int usb_close(usb_handle* handle);
|
||||||
|
|
||||||
int known_device_locked(const wchar_t* dev_name) {
|
int known_device_locked(const wchar_t* dev_name) {
|
||||||
usb_handle* usb;
|
usb_handle* usb;
|
||||||
|
|
||||||
if (NULL != dev_name) {
|
if (NULL != dev_name) {
|
||||||
// Iterate through the list looking for the name match.
|
// Iterate through the list looking for the name match.
|
||||||
for(usb = handle_list.next; usb != &handle_list; usb = usb->next) {
|
for (usb = handle_list.next; usb != &handle_list; usb = usb->next) {
|
||||||
// In Windows names are not case sensetive!
|
// In Windows names are not case sensetive!
|
||||||
if((NULL != usb->interface_name) &&
|
if ((NULL != usb->interface_name) && (0 == wcsicmp(usb->interface_name, dev_name))) {
|
||||||
(0 == wcsicmp(usb->interface_name, dev_name))) {
|
return 1;
|
||||||
return 1;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int known_device(const wchar_t* dev_name) {
|
int known_device(const wchar_t* dev_name) {
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
if (NULL != dev_name) {
|
if (NULL != dev_name) {
|
||||||
std::lock_guard<std::mutex> lock(usb_lock);
|
std::lock_guard<std::mutex> lock(usb_lock);
|
||||||
ret = known_device_locked(dev_name);
|
ret = known_device_locked(dev_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int register_new_device(usb_handle* handle) {
|
int register_new_device(usb_handle* handle) {
|
||||||
if (NULL == handle)
|
if (NULL == handle) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(usb_lock);
|
std::lock_guard<std::mutex> lock(usb_lock);
|
||||||
|
|
||||||
// Check if device is already in the list
|
// Check if device is already in the list
|
||||||
if (known_device_locked(handle->interface_name)) {
|
if (known_device_locked(handle->interface_name)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not in the list. Add this handle to the list.
|
// Not in the list. Add this handle to the list.
|
||||||
handle->next = &handle_list;
|
handle->next = &handle_list;
|
||||||
handle->prev = handle_list.prev;
|
handle->prev = handle_list.prev;
|
||||||
handle->prev->next = handle;
|
handle->prev->next = handle;
|
||||||
handle->next->prev = handle;
|
handle->next->prev = handle;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void device_poll_thread() {
|
void device_poll_thread() {
|
||||||
adb_thread_setname("Device Poll");
|
adb_thread_setname("Device Poll");
|
||||||
D("Created device thread");
|
D("Created device thread");
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
find_devices();
|
find_devices();
|
||||||
std::this_thread::sleep_for(1s);
|
std::this_thread::sleep_for(1s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static LRESULT CALLBACK _power_window_proc(HWND hwnd, UINT uMsg, WPARAM wParam,
|
static LRESULT CALLBACK _power_window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
|
||||||
LPARAM lParam) {
|
switch (uMsg) {
|
||||||
switch (uMsg) {
|
case WM_POWERBROADCAST:
|
||||||
case WM_POWERBROADCAST:
|
switch (wParam) {
|
||||||
switch (wParam) {
|
case PBT_APMRESUMEAUTOMATIC:
|
||||||
case PBT_APMRESUMEAUTOMATIC:
|
// Resuming from sleep or hibernation, so kick all existing USB devices
|
||||||
// Resuming from sleep or hibernation, so kick all existing USB devices
|
// and then allow the device_poll_thread to redetect USB devices from
|
||||||
// and then allow the device_poll_thread to redetect USB devices from
|
// scratch. If we don't do this, existing USB devices will never respond
|
||||||
// scratch. If we don't do this, existing USB devices will never respond
|
// to us because they'll be waiting for the connect/auth handshake.
|
||||||
// to us because they'll be waiting for the connect/auth handshake.
|
D("Received (WM_POWERBROADCAST, PBT_APMRESUMEAUTOMATIC) notification, "
|
||||||
D("Received (WM_POWERBROADCAST, PBT_APMRESUMEAUTOMATIC) notification, "
|
"so kicking all USB devices\n");
|
||||||
"so kicking all USB devices\n");
|
kick_devices();
|
||||||
kick_devices();
|
return TRUE;
|
||||||
return TRUE;
|
}
|
||||||
}
|
}
|
||||||
}
|
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
|
||||||
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _power_notification_thread() {
|
static void _power_notification_thread() {
|
||||||
// This uses a thread with its own window message pump to get power
|
// This uses a thread with its own window message pump to get power
|
||||||
// notifications. If adb runs from a non-interactive service account, this
|
// notifications. If adb runs from a non-interactive service account, this
|
||||||
// might not work (not sure). If that happens to not work, we could use
|
// might not work (not sure). If that happens to not work, we could use
|
||||||
// heavyweight WMI APIs to get power notifications. But for the common case
|
// heavyweight WMI APIs to get power notifications. But for the common case
|
||||||
// of a developer's interactive session, a window message pump is more
|
// of a developer's interactive session, a window message pump is more
|
||||||
// appropriate.
|
// appropriate.
|
||||||
D("Created power notification thread");
|
D("Created power notification thread");
|
||||||
adb_thread_setname("Power Notifier");
|
adb_thread_setname("Power Notifier");
|
||||||
|
|
||||||
// Window class names are process specific.
|
// Window class names are process specific.
|
||||||
static const WCHAR kPowerNotificationWindowClassName[] =
|
static const WCHAR kPowerNotificationWindowClassName[] = L"PowerNotificationWindow";
|
||||||
L"PowerNotificationWindow";
|
|
||||||
|
|
||||||
// Get the HINSTANCE corresponding to the module that _power_window_proc
|
// Get the HINSTANCE corresponding to the module that _power_window_proc
|
||||||
// is in (the main module).
|
// is in (the main module).
|
||||||
const HINSTANCE instance = GetModuleHandleW(NULL);
|
const HINSTANCE instance = GetModuleHandleW(NULL);
|
||||||
if (!instance) {
|
if (!instance) {
|
||||||
// This is such a common API call that this should never fail.
|
// This is such a common API call that this should never fail.
|
||||||
fatal("GetModuleHandleW failed: %s",
|
fatal("GetModuleHandleW failed: %s",
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
WNDCLASSEXW wndclass;
|
WNDCLASSEXW wndclass;
|
||||||
memset(&wndclass, 0, sizeof(wndclass));
|
memset(&wndclass, 0, sizeof(wndclass));
|
||||||
wndclass.cbSize = sizeof(wndclass);
|
wndclass.cbSize = sizeof(wndclass);
|
||||||
wndclass.lpfnWndProc = _power_window_proc;
|
wndclass.lpfnWndProc = _power_window_proc;
|
||||||
wndclass.hInstance = instance;
|
wndclass.hInstance = instance;
|
||||||
wndclass.lpszClassName = kPowerNotificationWindowClassName;
|
wndclass.lpszClassName = kPowerNotificationWindowClassName;
|
||||||
if (!RegisterClassExW(&wndclass)) {
|
if (!RegisterClassExW(&wndclass)) {
|
||||||
fatal("RegisterClassExW failed: %s",
|
fatal("RegisterClassExW failed: %s",
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!CreateWindowExW(WS_EX_NOACTIVATE, kPowerNotificationWindowClassName,
|
if (!CreateWindowExW(WS_EX_NOACTIVATE, kPowerNotificationWindowClassName,
|
||||||
L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0,
|
L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0, NULL, NULL,
|
||||||
NULL, NULL, instance, NULL)) {
|
instance, NULL)) {
|
||||||
fatal("CreateWindowExW failed: %s",
|
fatal("CreateWindowExW failed: %s",
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
MSG msg;
|
MSG msg;
|
||||||
while (GetMessageW(&msg, NULL, 0, 0)) {
|
while (GetMessageW(&msg, NULL, 0, 0)) {
|
||||||
TranslateMessage(&msg);
|
TranslateMessage(&msg);
|
||||||
DispatchMessageW(&msg);
|
DispatchMessageW(&msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMessageW() will return false if a quit message is posted. We don't
|
// GetMessageW() will return false if a quit message is posted. We don't
|
||||||
// do that, but it might be possible for that to occur when logging off or
|
// do that, but it might be possible for that to occur when logging off or
|
||||||
// shutting down. Not a big deal since the whole process will be going away
|
// shutting down. Not a big deal since the whole process will be going away
|
||||||
// soon anyway.
|
// soon anyway.
|
||||||
D("Power notification thread exiting");
|
D("Power notification thread exiting");
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_init() {
|
void usb_init() {
|
||||||
std::thread(device_poll_thread).detach();
|
std::thread(device_poll_thread).detach();
|
||||||
std::thread(_power_notification_thread).detach();
|
std::thread(_power_notification_thread).detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_cleanup() {}
|
void usb_cleanup() {}
|
||||||
|
|
||||||
usb_handle* do_usb_open(const wchar_t* interface_name) {
|
usb_handle* do_usb_open(const wchar_t* interface_name) {
|
||||||
unsigned long name_len = 0;
|
unsigned long name_len = 0;
|
||||||
|
|
||||||
// Allocate our handle
|
// Allocate our handle
|
||||||
usb_handle* ret = (usb_handle*)calloc(1, sizeof(usb_handle));
|
usb_handle* ret = (usb_handle*)calloc(1, sizeof(usb_handle));
|
||||||
if (NULL == ret) {
|
if (NULL == ret) {
|
||||||
D("Could not allocate %u bytes for usb_handle: %s", sizeof(usb_handle),
|
D("Could not allocate %u bytes for usb_handle: %s", sizeof(usb_handle), strerror(errno));
|
||||||
strerror(errno));
|
goto fail;
|
||||||
goto fail;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Set linkers back to the handle
|
// Set linkers back to the handle
|
||||||
ret->next = ret;
|
ret->next = ret;
|
||||||
ret->prev = ret;
|
ret->prev = ret;
|
||||||
|
|
||||||
// Create interface.
|
// Create interface.
|
||||||
ret->adb_interface = AdbCreateInterfaceByName(interface_name);
|
ret->adb_interface = AdbCreateInterfaceByName(interface_name);
|
||||||
if (NULL == ret->adb_interface) {
|
if (NULL == ret->adb_interface) {
|
||||||
D("AdbCreateInterfaceByName failed: %s",
|
D("AdbCreateInterfaceByName failed: %s",
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open read pipe (endpoint)
|
// Open read pipe (endpoint)
|
||||||
ret->adb_read_pipe =
|
ret->adb_read_pipe = AdbOpenDefaultBulkReadEndpoint(
|
||||||
AdbOpenDefaultBulkReadEndpoint(ret->adb_interface,
|
ret->adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite);
|
||||||
AdbOpenAccessTypeReadWrite,
|
if (NULL == ret->adb_read_pipe) {
|
||||||
AdbOpenSharingModeReadWrite);
|
D("AdbOpenDefaultBulkReadEndpoint failed: %s",
|
||||||
if (NULL == ret->adb_read_pipe) {
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
D("AdbOpenDefaultBulkReadEndpoint failed: %s",
|
goto fail;
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
}
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open write pipe (endpoint)
|
// Open write pipe (endpoint)
|
||||||
ret->adb_write_pipe =
|
ret->adb_write_pipe = AdbOpenDefaultBulkWriteEndpoint(
|
||||||
AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface,
|
ret->adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite);
|
||||||
AdbOpenAccessTypeReadWrite,
|
if (NULL == ret->adb_write_pipe) {
|
||||||
AdbOpenSharingModeReadWrite);
|
D("AdbOpenDefaultBulkWriteEndpoint failed: %s",
|
||||||
if (NULL == ret->adb_write_pipe) {
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
D("AdbOpenDefaultBulkWriteEndpoint failed: %s",
|
goto fail;
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
}
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save interface name
|
// Save interface name
|
||||||
// First get expected name length
|
// First get expected name length
|
||||||
AdbGetInterfaceName(ret->adb_interface,
|
AdbGetInterfaceName(ret->adb_interface, NULL, &name_len, false);
|
||||||
NULL,
|
if (0 == name_len) {
|
||||||
&name_len,
|
D("AdbGetInterfaceName returned name length of zero: %s",
|
||||||
false);
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
if (0 == name_len) {
|
goto fail;
|
||||||
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]));
|
ret->interface_name = (wchar_t*)malloc(name_len * sizeof(ret->interface_name[0]));
|
||||||
if (NULL == ret->interface_name) {
|
if (NULL == ret->interface_name) {
|
||||||
D("Could not allocate %lu characters for interface_name: %s", name_len, strerror(errno));
|
D("Could not allocate %lu characters for interface_name: %s", name_len, strerror(errno));
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now save the name
|
// Now save the name
|
||||||
if (!AdbGetInterfaceName(ret->adb_interface,
|
if (!AdbGetInterfaceName(ret->adb_interface, ret->interface_name, &name_len, false)) {
|
||||||
ret->interface_name,
|
D("AdbGetInterfaceName failed: %s",
|
||||||
&name_len,
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
false)) {
|
goto fail;
|
||||||
D("AdbGetInterfaceName failed: %s",
|
}
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We're done at this point
|
// We're done at this point
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
if (NULL != ret) {
|
if (NULL != ret) {
|
||||||
usb_cleanup_handle(ret);
|
usb_cleanup_handle(ret);
|
||||||
free(ret);
|
free(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int usb_write(usb_handle* handle, const void* data, int len) {
|
int usb_write(usb_handle* handle, const void* data, int len) {
|
||||||
unsigned long time_out = 5000;
|
unsigned long time_out = 5000;
|
||||||
unsigned long written = 0;
|
unsigned long written = 0;
|
||||||
int err = 0;
|
int err = 0;
|
||||||
|
|
||||||
D("usb_write %d", len);
|
D("usb_write %d", len);
|
||||||
if (NULL == handle) {
|
if (NULL == handle) {
|
||||||
D("usb_write was passed NULL handle");
|
D("usb_write was passed NULL handle");
|
||||||
err = EINVAL;
|
err = EINVAL;
|
||||||
goto fail;
|
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
|
|
||||||
if (!AdbWriteEndpointSync(handle->adb_write_pipe,
|
|
||||||
(void*)data,
|
|
||||||
0,
|
|
||||||
&written,
|
|
||||||
time_out)) {
|
|
||||||
D("AdbWriteEndpointSync of zero length packet failed: %s",
|
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
|
||||||
err = EIO;
|
|
||||||
goto fail;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
// 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
|
||||||
|
if (!AdbWriteEndpointSync(handle->adb_write_pipe, (void*)data, 0, &written, time_out)) {
|
||||||
|
D("AdbWriteEndpointSync of zero length packet failed: %s",
|
||||||
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
|
err = EIO;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
// Any failure should cause us to kick the device instead of leaving it a
|
// Any failure should cause us to kick the device instead of leaving it a
|
||||||
// zombie state with potential to hang.
|
// zombie state with potential to hang.
|
||||||
if (NULL != handle) {
|
if (NULL != handle) {
|
||||||
D("Kicking device due to error in usb_write");
|
D("Kicking device due to error in usb_write");
|
||||||
usb_kick(handle);
|
usb_kick(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
D("usb_write failed");
|
D("usb_write failed");
|
||||||
errno = err;
|
errno = err;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int usb_read(usb_handle *handle, void* data, int len) {
|
int usb_read(usb_handle* handle, void* data, int len) {
|
||||||
unsigned long time_out = 0;
|
unsigned long time_out = 0;
|
||||||
unsigned long read = 0;
|
unsigned long read = 0;
|
||||||
int err = 0;
|
int err = 0;
|
||||||
int orig_len = len;
|
int orig_len = len;
|
||||||
|
|
||||||
D("usb_read %d", len);
|
D("usb_read %d", len);
|
||||||
if (NULL == handle) {
|
if (NULL == handle) {
|
||||||
D("usb_read was passed NULL handle");
|
D("usb_read was passed NULL handle");
|
||||||
err = EINVAL;
|
err = EINVAL;
|
||||||
goto fail;
|
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;
|
while (len == orig_len) {
|
||||||
len -= read;
|
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);
|
||||||
|
|
||||||
return orig_len - len;
|
data = (char*)data + read;
|
||||||
|
len -= read;
|
||||||
|
}
|
||||||
|
|
||||||
|
return orig_len - len;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
// Any failure should cause us to kick the device instead of leaving it a
|
// Any failure should cause us to kick the device instead of leaving it a
|
||||||
// zombie state with potential to hang.
|
// zombie state with potential to hang.
|
||||||
if (NULL != handle) {
|
if (NULL != handle) {
|
||||||
D("Kicking device due to error in usb_read");
|
D("Kicking device due to error in usb_read");
|
||||||
usb_kick(handle);
|
usb_kick(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
D("usb_read failed");
|
D("usb_read failed");
|
||||||
errno = err;
|
errno = err;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wrapper around AdbCloseHandle() that logs diagnostics.
|
// Wrapper around AdbCloseHandle() that logs diagnostics.
|
||||||
static void _adb_close_handle(ADBAPIHANDLE adb_handle) {
|
static void _adb_close_handle(ADBAPIHANDLE adb_handle) {
|
||||||
if (!AdbCloseHandle(adb_handle)) {
|
if (!AdbCloseHandle(adb_handle)) {
|
||||||
D("AdbCloseHandle(%p) failed: %s", adb_handle,
|
D("AdbCloseHandle(%p) failed: %s", adb_handle,
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_cleanup_handle(usb_handle* handle) {
|
void usb_cleanup_handle(usb_handle* handle) {
|
||||||
D("usb_cleanup_handle");
|
D("usb_cleanup_handle");
|
||||||
if (NULL != handle) {
|
if (NULL != handle) {
|
||||||
if (NULL != handle->interface_name)
|
if (NULL != handle->interface_name) free(handle->interface_name);
|
||||||
free(handle->interface_name);
|
// AdbCloseHandle(pipe) will break any threads out of pending IO calls and
|
||||||
// AdbCloseHandle(pipe) will break any threads out of pending IO calls and
|
// wait until the pipe no longer uses the interface. Then we can
|
||||||
// wait until the pipe no longer uses the interface. Then we can
|
// AdbCloseHandle() the interface.
|
||||||
// AdbCloseHandle() the interface.
|
if (NULL != handle->adb_write_pipe) _adb_close_handle(handle->adb_write_pipe);
|
||||||
if (NULL != handle->adb_write_pipe)
|
if (NULL != handle->adb_read_pipe) _adb_close_handle(handle->adb_read_pipe);
|
||||||
_adb_close_handle(handle->adb_write_pipe);
|
if (NULL != handle->adb_interface) _adb_close_handle(handle->adb_interface);
|
||||||
if (NULL != handle->adb_read_pipe)
|
|
||||||
_adb_close_handle(handle->adb_read_pipe);
|
|
||||||
if (NULL != handle->adb_interface)
|
|
||||||
_adb_close_handle(handle->adb_interface);
|
|
||||||
|
|
||||||
handle->interface_name = NULL;
|
handle->interface_name = NULL;
|
||||||
handle->adb_write_pipe = NULL;
|
handle->adb_write_pipe = NULL;
|
||||||
handle->adb_read_pipe = NULL;
|
handle->adb_read_pipe = NULL;
|
||||||
handle->adb_interface = NULL;
|
handle->adb_interface = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void usb_kick_locked(usb_handle* handle) {
|
static void usb_kick_locked(usb_handle* handle) {
|
||||||
// The reason the lock must be acquired before calling this function is in
|
// 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.
|
// case multiple threads are trying to kick the same device at the same time.
|
||||||
usb_cleanup_handle(handle);
|
usb_cleanup_handle(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_kick(usb_handle* handle) {
|
void usb_kick(usb_handle* handle) {
|
||||||
D("usb_kick");
|
D("usb_kick");
|
||||||
if (NULL != handle) {
|
if (NULL != handle) {
|
||||||
std::lock_guard<std::mutex> lock(usb_lock);
|
std::lock_guard<std::mutex> lock(usb_lock);
|
||||||
usb_kick_locked(handle);
|
usb_kick_locked(handle);
|
||||||
} else {
|
} else {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int usb_close(usb_handle* handle) {
|
int usb_close(usb_handle* handle) {
|
||||||
D("usb_close");
|
D("usb_close");
|
||||||
|
|
||||||
if (NULL != handle) {
|
if (NULL != handle) {
|
||||||
// Remove handle from the list
|
// Remove handle from the list
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(usb_lock);
|
std::lock_guard<std::mutex> lock(usb_lock);
|
||||||
|
|
||||||
if ((handle->next != handle) && (handle->prev != handle)) {
|
if ((handle->next != handle) && (handle->prev != handle)) {
|
||||||
handle->next->prev = handle->prev;
|
handle->next->prev = handle->prev;
|
||||||
handle->prev->next = handle->next;
|
handle->prev->next = handle->next;
|
||||||
handle->prev = handle;
|
handle->prev = handle;
|
||||||
handle->next = handle;
|
handle->next = handle;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup handle
|
||||||
|
usb_cleanup_handle(handle);
|
||||||
|
free(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup handle
|
return 0;
|
||||||
usb_cleanup_handle(handle);
|
|
||||||
free(handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t usb_get_max_packet_size(usb_handle* handle) {
|
size_t usb_get_max_packet_size(usb_handle* handle) {
|
||||||
|
|
@ -532,131 +506,124 @@ size_t usb_get_max_packet_size(usb_handle* handle) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int recognized_device(usb_handle* handle) {
|
int recognized_device(usb_handle* handle) {
|
||||||
if (NULL == handle)
|
if (NULL == handle) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
// Check vendor and product id first
|
// Check vendor and product id first
|
||||||
USB_DEVICE_DESCRIPTOR device_desc;
|
USB_DEVICE_DESCRIPTOR device_desc;
|
||||||
|
|
||||||
if (!AdbGetUsbDeviceDescriptor(handle->adb_interface,
|
if (!AdbGetUsbDeviceDescriptor(handle->adb_interface, &device_desc)) {
|
||||||
&device_desc)) {
|
D("AdbGetUsbDeviceDescriptor failed: %s",
|
||||||
D("AdbGetUsbDeviceDescriptor failed: %s",
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
return 0;
|
||||||
return 0;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Then check interface properties
|
// Then check interface properties
|
||||||
USB_INTERFACE_DESCRIPTOR interf_desc;
|
USB_INTERFACE_DESCRIPTOR interf_desc;
|
||||||
|
|
||||||
if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface,
|
if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface, &interf_desc)) {
|
||||||
&interf_desc)) {
|
D("AdbGetUsbInterfaceDescriptor failed: %s",
|
||||||
D("AdbGetUsbInterfaceDescriptor failed: %s",
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
return 0;
|
||||||
return 0;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Must have two endpoints
|
// Must have two endpoints
|
||||||
if (2 != interf_desc.bNumEndpoints) {
|
if (2 != interf_desc.bNumEndpoints) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_adb_interface(interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass,
|
if (!is_adb_interface(interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass,
|
||||||
interf_desc.bInterfaceProtocol)) {
|
interf_desc.bInterfaceProtocol)) {
|
||||||
if (interf_desc.bInterfaceProtocol == 0x01) {
|
return 0;
|
||||||
AdbEndpointInformation endpoint_info;
|
}
|
||||||
// assuming zero is a valid bulk endpoint ID
|
|
||||||
if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) {
|
AdbEndpointInformation endpoint_info;
|
||||||
|
// assuming zero is a valid bulk endpoint ID
|
||||||
|
if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) {
|
||||||
handle->max_packet_size = endpoint_info.max_packet_size;
|
handle->max_packet_size = endpoint_info.max_packet_size;
|
||||||
handle->zero_mask = endpoint_info.max_packet_size - 1;
|
handle->zero_mask = endpoint_info.max_packet_size - 1;
|
||||||
D("device zero_mask: 0x%x", handle->zero_mask);
|
D("device zero_mask: 0x%x", handle->zero_mask);
|
||||||
} else {
|
} else {
|
||||||
D("AdbGetEndpointInformation failed: %s",
|
D("AdbGetEndpointInformation failed: %s",
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void find_devices() {
|
void find_devices() {
|
||||||
usb_handle* handle = NULL;
|
usb_handle* handle = NULL;
|
||||||
char entry_buffer[2048];
|
char entry_buffer[2048];
|
||||||
AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]);
|
AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]);
|
||||||
unsigned long entry_buffer_size = sizeof(entry_buffer);
|
unsigned long entry_buffer_size = sizeof(entry_buffer);
|
||||||
|
|
||||||
// Enumerate all present and active interfaces.
|
// Enumerate all present and active interfaces.
|
||||||
ADBAPIHANDLE enum_handle =
|
ADBAPIHANDLE enum_handle = AdbEnumInterfaces(usb_class_id, true, true, true);
|
||||||
AdbEnumInterfaces(usb_class_id, true, true, true);
|
|
||||||
|
|
||||||
if (NULL == enum_handle) {
|
if (NULL == enum_handle) {
|
||||||
D("AdbEnumInterfaces failed: %s",
|
D("AdbEnumInterfaces failed: %s",
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
||||||
return;
|
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 (NULL != 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, NULL, 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);
|
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 (NULL != handle) {
|
||||||
|
// Lets see if this interface (device) belongs to us
|
||||||
|
if (recognized_device(handle)) {
|
||||||
|
D("adding a new device %ls", next_interface->device_name);
|
||||||
|
|
||||||
if (GetLastError() != ERROR_NO_MORE_ITEMS) {
|
// We don't request a wchar_t string from AdbGetSerialNumber() because of a bug
|
||||||
// Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration.
|
// in adb_winusb_interface.cpp:CopyMemory(buffer, ser_num->bString,
|
||||||
D("AdbNextInterface failed: %s",
|
// bytes_written) where the last parameter should be (str_len *
|
||||||
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
// 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, NULL, 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_adb_close_handle(enum_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 kick_devices() {
|
static void kick_devices() {
|
||||||
// Need to acquire lock to safely walk the list which might be modified
|
// Need to acquire lock to safely walk the list which might be modified
|
||||||
// by another thread.
|
// by another thread.
|
||||||
std::lock_guard<std::mutex> lock(usb_lock);
|
std::lock_guard<std::mutex> lock(usb_lock);
|
||||||
for (usb_handle* usb = handle_list.next; usb != &handle_list; usb = usb->next) {
|
for (usb_handle* usb = handle_list.next; usb != &handle_list; usb = usb->next) {
|
||||||
usb_kick_locked(usb);
|
usb_kick_locked(usb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue