Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5966ae2c authored by Mark Salyzyn's avatar Mark Salyzyn Committed by android-build-merger
Browse files

Merge changes I3e6e5a22,I6eb3f066

am: 75836d5e

Change-Id: I871260b9cdf7a1ab5f6d39cd8a491cd31cd94a4d
parents bdee15ef 75836d5e
Loading
Loading
Loading
Loading
+35 −38
Original line number Original line Diff line number Diff line
@@ -67,8 +67,7 @@ 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;
@@ -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,7 +118,6 @@ 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
@@ -175,7 +171,9 @@ void put_apacket(apacket *p);
#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
@@ -193,7 +191,6 @@ void put_apacket(apacket *p);
#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);
+1 −1
Original line number Original line Diff line number Diff line
@@ -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;
                        }
                        }
+1 −1
Original line number Original line Diff line number Diff line
@@ -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;
+414 −447
Original line number Original line Diff line number Diff line
@@ -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>


@@ -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
@@ -136,8 +137,7 @@ int known_device_locked(const wchar_t* 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;
            }
            }
        }
        }
@@ -158,8 +158,7 @@ int known_device(const wchar_t* dev_name) {
}
}


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);


@@ -187,8 +186,7 @@ void device_poll_thread() {
    }
    }
}
}


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) {
@@ -217,8 +215,7 @@ static void _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).
@@ -241,8 +238,8 @@ static void _power_notification_thread() {
    }
    }


    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());
    }
    }
@@ -273,8 +270,7 @@ usb_handle* do_usb_open(const wchar_t* interface_name) {
    // 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;
    }
    }


@@ -291,10 +287,8 @@ usb_handle* do_usb_open(const wchar_t* interface_name) {
    }
    }


    // 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,
                                   AdbOpenSharingModeReadWrite);
    if (NULL == ret->adb_read_pipe) {
    if (NULL == ret->adb_read_pipe) {
        D("AdbOpenDefaultBulkReadEndpoint failed: %s",
        D("AdbOpenDefaultBulkReadEndpoint failed: %s",
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
@@ -302,10 +296,8 @@ usb_handle* do_usb_open(const wchar_t* interface_name) {
    }
    }


    // 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,
                                    AdbOpenSharingModeReadWrite);
    if (NULL == ret->adb_write_pipe) {
    if (NULL == ret->adb_write_pipe) {
        D("AdbOpenDefaultBulkWriteEndpoint failed: %s",
        D("AdbOpenDefaultBulkWriteEndpoint failed: %s",
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
@@ -314,10 +306,7 @@ usb_handle* do_usb_open(const wchar_t* interface_name) {


    // 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,
                      &name_len,
                      false);
    if (0 == name_len) {
    if (0 == name_len) {
        D("AdbGetInterfaceName returned name length of zero: %s",
        D("AdbGetInterfaceName returned name length of zero: %s",
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
@@ -331,10 +320,7 @@ usb_handle* do_usb_open(const wchar_t* interface_name) {
    }
    }


    // 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,
                           &name_len,
                           false)) {
        D("AdbGetInterfaceName failed: %s",
        D("AdbGetInterfaceName failed: %s",
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
        goto fail;
        goto fail;
@@ -365,10 +351,7 @@ int usb_write(usb_handle* handle, const void* data, int len) {
    }
    }


    // Perform write
    // Perform write
  if (!AdbWriteEndpointSync(handle->adb_write_pipe,
    if (!AdbWriteEndpointSync(handle->adb_write_pipe, (void*)data, (unsigned long)len, &written,
                            (void*)data,
                            (unsigned long)len,
                            &written,
                              time_out)) {
                              time_out)) {
        D("AdbWriteEndpointSync failed: %s",
        D("AdbWriteEndpointSync failed: %s",
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
@@ -381,19 +364,14 @@ int usb_write(usb_handle* handle, const void* data, int len) {
    if (written != (unsigned long)len) {
    if (written != (unsigned long)len) {
        // If this occurs, this code should be changed to repeatedly call
        // If this occurs, this code should be changed to repeatedly call
        // AdbWriteEndpointSync() until all bytes are written.
        // AdbWriteEndpointSync() until all bytes are written.
    D("AdbWriteEndpointSync was supposed to write %d, but only wrote %ld",
        D("AdbWriteEndpointSync was supposed to write %d, but only wrote %ld", len, written);
      len, written);
        err = EIO;
        err = EIO;
        goto fail;
        goto fail;
    }
    }


    if (handle->zero_mask && (len & handle->zero_mask) == 0) {
    if (handle->zero_mask && (len & handle->zero_mask) == 0) {
        // Send a zero length packet
        // Send a zero length packet
    if (!AdbWriteEndpointSync(handle->adb_write_pipe,
        if (!AdbWriteEndpointSync(handle->adb_write_pipe, (void*)data, 0, &written, time_out)) {
                              (void*)data,
                              0,
                              &written,
                              time_out)) {
            D("AdbWriteEndpointSync of zero length packet failed: %s",
            D("AdbWriteEndpointSync of zero length packet failed: %s",
              android::base::SystemErrorCodeToString(GetLastError()).c_str());
              android::base::SystemErrorCodeToString(GetLastError()).c_str());
            err = EIO;
            err = EIO;
@@ -468,17 +446,13 @@ static void _adb_close_handle(ADBAPIHANDLE adb_handle) {
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)
        if (NULL != handle->adb_write_pipe) _adb_close_handle(handle->adb_write_pipe);
      _adb_close_handle(handle->adb_write_pipe);
        if (NULL != handle->adb_read_pipe) _adb_close_handle(handle->adb_read_pipe);
    if (NULL != handle->adb_read_pipe)
        if (NULL != handle->adb_interface) _adb_close_handle(handle->adb_interface);
      _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;
@@ -532,14 +506,12 @@ 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;
@@ -548,8 +520,7 @@ int recognized_device(usb_handle* handle) {
    // 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;
@@ -560,9 +531,11 @@ int recognized_device(usb_handle* handle) {
        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;
    AdbEndpointInformation endpoint_info;
    // assuming zero is a valid bulk endpoint ID
    // assuming zero is a valid bulk endpoint ID
    if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) {
    if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) {
@@ -573,14 +546,10 @@ int recognized_device(usb_handle* handle) {
        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];
@@ -588,8 +557,7 @@ void find_devices() {
    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",
@@ -607,17 +575,16 @@ void find_devices() {
                if (recognized_device(handle)) {
                if (recognized_device(handle)) {
                    D("adding a new device %ls", next_interface->device_name);
                    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
                    // We don't request a wchar_t string from AdbGetSerialNumber() because of a bug
          // adb_winusb_interface.cpp:CopyMemory(buffer, ser_num->bString, bytes_written) where the
                    // in adb_winusb_interface.cpp:CopyMemory(buffer, ser_num->bString,
          // last parameter should be (str_len * sizeof(wchar_t)). The bug reads 2 bytes past the
                    // bytes_written) where the last parameter should be (str_len *
          // end of a stack buffer in the best case, and in the unlikely case of a long serial
                    // sizeof(wchar_t)). The bug reads 2 bytes past the end of a stack buffer in the
          // number, it will read 2 bytes past the end of a heap allocation. This doesn't affect the
                    // best case, and in the unlikely case of a long serial number, it will read 2
          // resulting string, but we should avoid the bad reads in the first place.
                    // 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];
                    char serial_number[512];
                    unsigned long serial_number_len = sizeof(serial_number);
                    unsigned long serial_number_len = sizeof(serial_number);
          if (AdbGetSerialNumber(handle->adb_interface,
                    if (AdbGetSerialNumber(handle->adb_interface, serial_number, &serial_number_len,
                                serial_number,
                                &serial_number_len,
                                           true)) {
                                           true)) {
                        // Lets make sure that we don't duplicate this device
                        // Lets make sure that we don't duplicate this device
                        if (register_new_device(handle)) {
                        if (register_new_device(handle)) {