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

Commit 75836d5e authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes I3e6e5a22,I6eb3f066

* changes:
  adb: cleanup some portions of client usb interfaces
  adb: clang-format for adb.h and client/usb_windows.cpp
parents 6958506e 8f3b8873
Loading
Loading
Loading
Loading
+35 −38
Original line number Diff line number Diff line
@@ -67,8 +67,7 @@ struct amessage {
    uint32_t magic;       /* command ^ 0xffffffff             */
};

struct apacket
{
struct apacket {
    apacket* next;

    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
** transport (e.g. remote sockets, listeners, etc...)
*/
struct  adisconnect
{
struct adisconnect {
    void (*func)(void* opaque, atransport* t);
    void* opaque;
};


// A transport object models the connection to a remote device or emulator there
// is one transport per connected device/emulator. A "local transport" connects
// through TCP (for the emulator), while a "usb transport" through USB (for real
@@ -121,7 +118,6 @@ enum ConnectionState {
    kCsUnauthorized,
};


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
@@ -175,7 +171,9 @@ void put_apacket(apacket *p);
#define DEBUG_PACKETS 0

#if !DEBUG_PACKETS
#define print_packet(tag,p) do {} while (0)
#define print_packet(tag, p) \
    do {                     \
    } while (0)
#endif

#if ADB_HOST_ON_TARGET
@@ -193,7 +191,6 @@ void put_apacket(apacket *p);
#define ADB_SUBCLASS 0x42
#define ADB_PROTOCOL 0x1


void local_init(int port);
bool local_connect(int port);
int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
+1 −1
Original line number Diff line number Diff line
@@ -253,7 +253,7 @@ static void find_usb_device(const std::string& base,
                            continue;
                        }
                            /* aproto 01 needs 0 termination */
                        if (interface->bInterfaceProtocol == 0x01) {
                        if (interface->bInterfaceProtocol == ADB_PROTOCOL) {
                            max_packet_size = ep1->wMaxPacketSize;
                            zero_mask = ep1->wMaxPacketSize - 1;
                        }
+1 −1
Original line number Diff line number Diff line
@@ -174,7 +174,7 @@ AndroidInterfaceAdded(io_iterator_t iterator)
        kr = (*iface)->GetInterfaceClass(iface, &if_class);
        kr = (*iface)->GetInterfaceSubClass(iface, &subclass);
        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.
            LOG(DEBUG) << "Ignoring interface with incorrect class/subclass/protocol - " << if_class
                       << ", " << subclass << ", " << protocol;
+414 −447
Original line number Diff line number Diff line
@@ -18,8 +18,10 @@

#include "sysdeps.h"

// clang-format off
#include <winsock2.h>  // winsock.h *must* be included before windows.h.
#include <windows.h>
// clang-format on
#include <usb100.h>
#include <winerror.h>

@@ -77,8 +79,7 @@ static const GUID usb_class_id = ANDROID_USB_CLASS_ID;

/// List of opened usb handles
static usb_handle handle_list = {
  .prev = &handle_list,
  .next = &handle_list,
    .prev = &handle_list, .next = &handle_list,
};

/// 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.
        for (usb = handle_list.next; usb != &handle_list; usb = usb->next) {
            // In Windows names are not case sensetive!
      if((NULL != usb->interface_name) &&
         (0 == wcsicmp(usb->interface_name, dev_name))) {
            if ((NULL != usb->interface_name) && (0 == wcsicmp(usb->interface_name, dev_name))) {
                return 1;
            }
        }
@@ -158,8 +158,7 @@ int known_device(const wchar_t* dev_name) {
}

int register_new_device(usb_handle* handle) {
  if (NULL == handle)
    return 0;
    if (NULL == handle) return 0;

    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,
                                           LPARAM lParam) {
static LRESULT CALLBACK _power_window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_POWERBROADCAST:
            switch (wParam) {
@@ -217,8 +215,7 @@ static void _power_notification_thread() {
    adb_thread_setname("Power Notifier");

    // Window class names are process specific.
  static const WCHAR kPowerNotificationWindowClassName[] =
    L"PowerNotificationWindow";
    static const WCHAR kPowerNotificationWindowClassName[] = L"PowerNotificationWindow";

    // Get the HINSTANCE corresponding to the module that _power_window_proc
    // is in (the main module).
@@ -241,8 +238,8 @@ static void _power_notification_thread() {
    }

    if (!CreateWindowExW(WS_EX_NOACTIVATE, kPowerNotificationWindowClassName,
                       L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0,
                       NULL, NULL, instance, NULL)) {
                         L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0, NULL, NULL,
                         instance, NULL)) {
        fatal("CreateWindowExW failed: %s",
              android::base::SystemErrorCodeToString(GetLastError()).c_str());
    }
@@ -273,8 +270,7 @@ usb_handle* do_usb_open(const wchar_t* interface_name) {
    // Allocate our handle
    usb_handle* ret = (usb_handle*)calloc(1, sizeof(usb_handle));
    if (NULL == ret) {
    D("Could not allocate %u bytes for usb_handle: %s", sizeof(usb_handle),
      strerror(errno));
        D("Could not allocate %u bytes for usb_handle: %s", sizeof(usb_handle), strerror(errno));
        goto fail;
    }

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

    // Open read pipe (endpoint)
  ret->adb_read_pipe =
    AdbOpenDefaultBulkReadEndpoint(ret->adb_interface,
                                   AdbOpenAccessTypeReadWrite,
                                   AdbOpenSharingModeReadWrite);
    ret->adb_read_pipe = AdbOpenDefaultBulkReadEndpoint(
        ret->adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite);
    if (NULL == ret->adb_read_pipe) {
        D("AdbOpenDefaultBulkReadEndpoint failed: %s",
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
@@ -302,10 +296,8 @@ usb_handle* do_usb_open(const wchar_t* interface_name) {
    }

    // Open write pipe (endpoint)
  ret->adb_write_pipe =
    AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface,
                                    AdbOpenAccessTypeReadWrite,
                                    AdbOpenSharingModeReadWrite);
    ret->adb_write_pipe = AdbOpenDefaultBulkWriteEndpoint(
        ret->adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite);
    if (NULL == ret->adb_write_pipe) {
        D("AdbOpenDefaultBulkWriteEndpoint failed: %s",
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
@@ -314,10 +306,7 @@ usb_handle* do_usb_open(const wchar_t* interface_name) {

    // Save interface name
    // First get expected name length
  AdbGetInterfaceName(ret->adb_interface,
                      NULL,
                      &name_len,
                      false);
    AdbGetInterfaceName(ret->adb_interface, NULL, &name_len, false);
    if (0 == name_len) {
        D("AdbGetInterfaceName returned name length of zero: %s",
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
@@ -331,10 +320,7 @@ usb_handle* do_usb_open(const wchar_t* interface_name) {
    }

    // Now save the name
  if (!AdbGetInterfaceName(ret->adb_interface,
                           ret->interface_name,
                           &name_len,
                           false)) {
    if (!AdbGetInterfaceName(ret->adb_interface, ret->interface_name, &name_len, false)) {
        D("AdbGetInterfaceName failed: %s",
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
        goto fail;
@@ -365,10 +351,7 @@ int usb_write(usb_handle* handle, const void* data, int len) {
    }

    // Perform write
  if (!AdbWriteEndpointSync(handle->adb_write_pipe,
                            (void*)data,
                            (unsigned long)len,
                            &written,
    if (!AdbWriteEndpointSync(handle->adb_write_pipe, (void*)data, (unsigned long)len, &written,
                              time_out)) {
        D("AdbWriteEndpointSync failed: %s",
          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 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);
        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)) {
        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;
@@ -468,17 +446,13 @@ static void _adb_close_handle(ADBAPIHANDLE adb_handle) {
void usb_cleanup_handle(usb_handle* handle) {
    D("usb_cleanup_handle");
    if (NULL != handle) {
    if (NULL != handle->interface_name)
      free(handle->interface_name);
        if (NULL != handle->interface_name) free(handle->interface_name);
        // AdbCloseHandle(pipe) will break any threads out of pending IO calls and
        // wait until the pipe no longer uses the interface. Then we can
        // AdbCloseHandle() the interface.
    if (NULL != 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_interface)
      _adb_close_handle(handle->adb_interface);
        if (NULL != 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_interface) _adb_close_handle(handle->adb_interface);

        handle->interface_name = 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) {
  if (NULL == handle)
    return 0;
    if (NULL == handle) return 0;

    // Check vendor and product id first
    USB_DEVICE_DESCRIPTOR device_desc;

  if (!AdbGetUsbDeviceDescriptor(handle->adb_interface,
                                 &device_desc)) {
    if (!AdbGetUsbDeviceDescriptor(handle->adb_interface, &device_desc)) {
        D("AdbGetUsbDeviceDescriptor failed: %s",
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
        return 0;
@@ -548,8 +520,7 @@ int recognized_device(usb_handle* handle) {
    // Then check interface properties
    USB_INTERFACE_DESCRIPTOR interf_desc;

  if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface,
                                    &interf_desc)) {
    if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface, &interf_desc)) {
        D("AdbGetUsbInterfaceDescriptor failed: %s",
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
        return 0;
@@ -560,9 +531,11 @@ int recognized_device(usb_handle* handle) {
        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)) {
    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)) {
@@ -573,14 +546,10 @@ int recognized_device(usb_handle* handle) {
        D("AdbGetEndpointInformation failed: %s",
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
    }
    }

    return 1;
}

  return 0;
}

void find_devices() {
    usb_handle* handle = NULL;
    char entry_buffer[2048];
@@ -588,8 +557,7 @@ void find_devices() {
    unsigned long entry_buffer_size = sizeof(entry_buffer);

    // Enumerate all present and active interfaces.
  ADBAPIHANDLE enum_handle =
    AdbEnumInterfaces(usb_class_id, true, true, true);
    ADBAPIHANDLE enum_handle = AdbEnumInterfaces(usb_class_id, true, true, true);

    if (NULL == enum_handle) {
        D("AdbEnumInterfaces failed: %s",
@@ -607,17 +575,16 @@ void find_devices() {
                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.
                    // 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,
                    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)) {