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

Commit 1c70e1bc authored by Josh Gao's avatar Josh Gao
Browse files

adb: add libusb implementation for Linux/Darwin.

Add a libusb-based implementation alongside the existing native
implementations, controlled by the ADB_LIBUSB environment variable.

Windows will need more work for the usb driver.

Bug: http://b/31321337
Test: python test_device.py on linux/darwin, with ADB_LIBUSB=0 and 1
Change-Id: Ib68fb2c6c05475eae3ff4cc19f55802a6f489bb7
parent 4c38a921
Loading
Loading
Loading
Loading
+15 −3
Original line number Diff line number Diff line
@@ -81,10 +81,14 @@ LIBADB_windows_CFLAGS := \

LIBADB_darwin_SRC_FILES := \
    sysdeps_unix.cpp \
    client/usb_dispatch.cpp \
    client/usb_libusb.cpp \
    client/usb_osx.cpp \

LIBADB_linux_SRC_FILES := \
    sysdeps_unix.cpp \
    client/usb_dispatch.cpp \
    client/usb_libusb.cpp \
    client/usb_linux.cpp \

LIBADB_windows_SRC_FILES := \
@@ -150,6 +154,8 @@ LOCAL_SANITIZE := $(adb_host_sanitize)
# Even though we're building a static library (and thus there's no link step for
# this to take effect), this adds the includes to our path.
LOCAL_STATIC_LIBRARIES := libcrypto_utils libcrypto libbase
LOCAL_STATIC_LIBRARIES_linux := libusb
LOCAL_STATIC_LIBRARIES_darwin := libusb

LOCAL_C_INCLUDES_windows := development/host/windows/usb/api/
LOCAL_MULTILIB := first
@@ -169,7 +175,7 @@ LOCAL_SRC_FILES := \
    shell_service_test.cpp \

LOCAL_SANITIZE := $(adb_target_sanitize)
LOCAL_STATIC_LIBRARIES := libadbd libcrypto_utils libcrypto
LOCAL_STATIC_LIBRARIES := libadbd libcrypto_utils libcrypto libusb
LOCAL_SHARED_LIBRARIES := liblog libbase libcutils
include $(BUILD_NATIVE_TEST)

@@ -219,10 +225,13 @@ LOCAL_STATIC_LIBRARIES := \
    libdiagnose_usb \
    libgmock_host \

LOCAL_STATIC_LIBRARIES_linux := libusb
LOCAL_STATIC_LIBRARIES_darwin := libusb

# Set entrypoint to wmain from sysdeps_win32.cpp instead of main
LOCAL_LDFLAGS_windows := -municode
LOCAL_LDLIBS_linux := -lrt -ldl -lpthread
LOCAL_LDLIBS_darwin := -framework CoreFoundation -framework IOKit
LOCAL_LDLIBS_darwin := -framework CoreFoundation -framework IOKit -lobjc
LOCAL_LDLIBS_windows := -lws2_32 -luserenv
LOCAL_STATIC_LIBRARIES_windows := AdbWinApi

@@ -236,7 +245,7 @@ include $(CLEAR_VARS)

LOCAL_LDLIBS_linux := -lrt -ldl -lpthread

LOCAL_LDLIBS_darwin := -lpthread -framework CoreFoundation -framework IOKit -framework Carbon
LOCAL_LDLIBS_darwin := -lpthread -framework CoreFoundation -framework IOKit -framework Carbon -lobjc

# Use wmain instead of main
LOCAL_LDFLAGS_windows := -municode
@@ -287,6 +296,9 @@ LOCAL_STATIC_LIBRARIES := \
LOCAL_STATIC_LIBRARIES_darwin := libcutils
LOCAL_STATIC_LIBRARIES_linux := libcutils

LOCAL_STATIC_LIBRARIES_darwin += libusb
LOCAL_STATIC_LIBRARIES_linux += libusb

LOCAL_CXX_STL := libc++_static

# Don't add anything here, we don't want additional shared dependencies
+2 −1
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@
#include <android-base/logging.h>
#include <android-base/macros.h>
#include <android-base/parsenetaddress.h>
#include <android-base/quick_exit.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>

@@ -1047,7 +1048,7 @@ int handle_host_request(const char* service, TransportType type,
        // may not read the OKAY sent above.
        adb_shutdown(reply_fd);

        exit(0);
        android::base::quick_exit(0);
    }

#if ADB_HOST
+1 −13
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#include "adb_trace.h"
#include "fdevent.h"
#include "socket.h"
#include "usb.h"

constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
constexpr size_t MAX_PAYLOAD_V2 = 256 * 1024;
@@ -54,7 +55,6 @@ std::string adb_version();
#define ADB_SERVER_VERSION 38

class atransport;
struct usb_handle;

struct amessage {
    uint32_t command;     /* command identifier constant      */
@@ -195,18 +195,6 @@ void local_init(int port);
bool local_connect(int port);
int  local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);

// USB host/client interface.
void usb_init();
int usb_write(usb_handle *h, const void *data, int len);
int usb_read(usb_handle *h, void *data, int len);
int usb_close(usb_handle *h);
void usb_kick(usb_handle *h);

// USB device detection.
#if ADB_HOST
int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol);
#endif

ConnectionState connection_state(atransport *t);

extern const char* adb_device_banner;
+6 −1
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#include <android-base/errors.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/quick_exit.h>
#include <android-base/stringprintf.h>

#include "adb.h"
@@ -86,7 +87,7 @@ static void setup_daemon_logging(void) {
static BOOL WINAPI ctrlc_handler(DWORD type) {
    // TODO: Consider trying to kill a starting up adb server (if we're in
    // launch_server) by calling GenerateConsoleCtrlEvent().
    exit(STATUS_CONTROL_C_EXIT);
    android::base::quick_exit(STATUS_CONTROL_C_EXIT);
    return TRUE;
}
#endif
@@ -108,6 +109,10 @@ int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply
    }

    SetConsoleCtrlHandler(ctrlc_handler, TRUE);
#else
    signal(SIGINT, [](int) {
        android::base::quick_exit(0);
    });
#endif

    init_transport_registration();
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android-base/logging.h>
#include "usb.h"

static bool should_use_libusb() {
    static bool enable = getenv("ADB_LIBUSB") && strcmp(getenv("ADB_LIBUSB"), "1") == 0;
    return enable;
}

void usb_init() {
    if (should_use_libusb()) {
        LOG(INFO) << "using libusb backend";
        libusb::usb_init();
    } else {
        LOG(INFO) << "using native backend";
        native::usb_init();
    }
}

int usb_write(usb_handle* h, const void* data, int len) {
    return should_use_libusb()
               ? libusb::usb_write(reinterpret_cast<libusb::usb_handle*>(h), data, len)
               : native::usb_write(reinterpret_cast<native::usb_handle*>(h), data, len);
}

int usb_read(usb_handle* h, void* data, int len) {
    return should_use_libusb()
               ? libusb::usb_read(reinterpret_cast<libusb::usb_handle*>(h), data, len)
               : native::usb_read(reinterpret_cast<native::usb_handle*>(h), data, len);
}

int usb_close(usb_handle* h) {
    return should_use_libusb() ? libusb::usb_close(reinterpret_cast<libusb::usb_handle*>(h))
                               : native::usb_close(reinterpret_cast<native::usb_handle*>(h));
}

void usb_kick(usb_handle* h) {
    should_use_libusb() ? libusb::usb_kick(reinterpret_cast<libusb::usb_handle*>(h))
                        : native::usb_kick(reinterpret_cast<native::usb_handle*>(h));
}
Loading