From 05e7967b2a8062d821f063ddcbf22d328c5ff5e0 Mon Sep 17 00:00:00 2001 From: jbeich Date: Fri, 19 Jun 2015 17:14:53 +0000 Subject: devel/android-tools-adb-devel: add new slave port for git master A temporary port to facilitate testing and updating master. While copying usb_libusb.c [1] under FILESDIR clean it up: - Add static keyword for file-local functions - Fix const usage exposed in C++ mode [2] - Fix casting 3rd argument of libusb_bulk_transfer(3) exposed in C++ mode - Fix inconsistent whitespace: trailing, tabs + spaces, bogus indentation - Fix typo in usb_kick() trace message - Fix fprintf(3) warning in scan_usb_devices() trace message - Convert err(3) usage to fatal_errno() - Drop BSDism of sysexits(3) - Drop unecessary struct keyword - Drop unused headers [2] - Explicitly include header And chase the following commits: https://android.googlesource.com/platform/system/core/+/506aea4%5E!/ [1] https://android.googlesource.com/platform/system/core/+/dc3b459%5E!/#F3 https://android.googlesource.com/platform/system/core/+/9b0f354%5E!/#F5 https://android.googlesource.com/platform/system/core/+/cf07494%5E!/#F8 https://android.googlesource.com/platform/system/core/+/3313426%5E!/#F20 https://android.googlesource.com/platform/system/core/+/7664901%5E!/#F18 Changes: https://android.googlesource.com/platform/system/core/+log/53d504f/adb Differential Revision: https://reviews.freebsd.org/D2870 Obtained from: devel/android-tools-fastboot-devel (based on) Obtained from: OpenBSD ports [2] Approved by: kevlo (maintainer) --- devel/Makefile | 1 + devel/android-tools-adb-devel/Makefile | 22 + devel/android-tools-adb-devel/distinfo | 4 + devel/android-tools-adb-devel/files/Makefile | 69 +++ .../files/get_my_path_freebsd.cpp | 35 ++ .../files/patch-adb_commandline.cpp | 24 + .../files/patch-base_file.cpp | 10 + .../files/patch-base_logging.cpp | 18 + .../files/patch-libcutils_threads.c | 38 ++ .../files/patch-liblog_logd__write.c | 12 + devel/android-tools-adb-devel/files/usb_libusb.cpp | 655 +++++++++++++++++++++ devel/android-tools-adb/Makefile | 11 +- 12 files changed, 896 insertions(+), 3 deletions(-) create mode 100644 devel/android-tools-adb-devel/Makefile create mode 100644 devel/android-tools-adb-devel/distinfo create mode 100644 devel/android-tools-adb-devel/files/Makefile create mode 100644 devel/android-tools-adb-devel/files/get_my_path_freebsd.cpp create mode 100644 devel/android-tools-adb-devel/files/patch-adb_commandline.cpp create mode 100644 devel/android-tools-adb-devel/files/patch-base_file.cpp create mode 100644 devel/android-tools-adb-devel/files/patch-base_logging.cpp create mode 100644 devel/android-tools-adb-devel/files/patch-libcutils_threads.c create mode 100644 devel/android-tools-adb-devel/files/patch-liblog_logd__write.c create mode 100644 devel/android-tools-adb-devel/files/usb_libusb.cpp (limited to 'devel') diff --git a/devel/Makefile b/devel/Makefile index aacd9d7cc0ba..665ebf008dbc 100644 --- a/devel/Makefile +++ b/devel/Makefile @@ -57,6 +57,7 @@ SUBDIR += amd64-gcc SUBDIR += amd64-xtoolchain-gcc SUBDIR += android-tools-adb + SUBDIR += android-tools-adb-devel SUBDIR += android-tools-fastboot SUBDIR += android-tools-fastboot-devel SUBDIR += anjuta diff --git a/devel/android-tools-adb-devel/Makefile b/devel/android-tools-adb-devel/Makefile new file mode 100644 index 000000000000..825f793ad31e --- /dev/null +++ b/devel/android-tools-adb-devel/Makefile @@ -0,0 +1,22 @@ +# $FreeBSD$ + +DISTVERSION= m-preview-122 +DISTVERSIONSUFFIX= -g53d504f +PORTREVISION= 0 +PKGNAMESUFFIX= -devel + +CONFLICTS_INSTALL= ${PORTNAME}-[0-9]* + +MASTERDIR= ${.CURDIR}/../android-tools-adb +DISTINFO_FILE= ${.CURDIR}/distinfo +MAKEFILE= ${.CURDIR}/files/Makefile +FILESDIR= ${.CURDIR}/files +PATCHDIR= ${.CURDIR}/files + +USES= compiler:c++11-lib +BUILD_WRKSRC= ${WRKSRC}/adb +INSTALL_WRKSRC= ${BUILD_WRKSRC} +MAKE_ENV= FILESDIR="${FILESDIR}" +CFLAGS+= -DADB_REVISION='\"${DISTVERSIONFULL}\"' + +.include "${MASTERDIR}/Makefile" diff --git a/devel/android-tools-adb-devel/distinfo b/devel/android-tools-adb-devel/distinfo new file mode 100644 index 000000000000..d3fa23af1299 --- /dev/null +++ b/devel/android-tools-adb-devel/distinfo @@ -0,0 +1,4 @@ +SHA256 (android-platform_system_core-android-m-preview-122-g53d504f_GH0.tar.gz) = 8d49065aff063fb02cc175c634384eb1dc9ed87b85d16e85f0c70bff01415948 +SIZE (android-platform_system_core-android-m-preview-122-g53d504f_GH0.tar.gz) = 1182976 +SHA256 (bash_completion.d/adb?id=2b8cfec) = 0d7989596a3e4937834d582eb0ddc88b8186505570772304fe51870d473916a7 +SIZE (bash_completion.d/adb?id=2b8cfec) = 5198 diff --git a/devel/android-tools-adb-devel/files/Makefile b/devel/android-tools-adb-devel/files/Makefile new file mode 100644 index 000000000000..a80be29605e5 --- /dev/null +++ b/devel/android-tools-adb-devel/files/Makefile @@ -0,0 +1,69 @@ +# $FreeBSD$ + +PROG_CXX=adb +NO_MAN= +BINDIR?=/usr/bin + +SRCS+= adb.cpp +SRCS+= adb_auth.cpp +SRCS+= adb_auth_host.cpp +SRCS+= adb_client.cpp +SRCS+= adb_io.cpp +SRCS+= adb_listeners.cpp +SRCS+= adb_utils.cpp +SRCS+= commandline.cpp +SRCS+= console.cpp +SRCS+= fdevent.cpp +SRCS+= file_sync_client.cpp +SRCS+= services.cpp +SRCS+= sockets.cpp +SRCS+= transport.cpp +SRCS+= transport_local.cpp +SRCS+= transport_usb.cpp + +.PATH: ${.CURDIR}/client +SRCS+= main.cpp + +.PATH: ${FILESDIR} +SRCS+= get_my_path_freebsd.cpp +SRCS+= usb_libusb.cpp + +.PATH: ${.CURDIR}/../base +SRCS+= file.cpp +SRCS+= logging.cpp +SRCS+= strings.cpp +SRCS+= stringprintf.cpp + +.PATH: ${.CURDIR}/../libcutils +SRCS+= socket_inaddr_any_server.c +SRCS+= socket_local_client.c +SRCS+= socket_local_server.c +SRCS+= socket_loopback_client.c +SRCS+= socket_loopback_server.c +SRCS+= socket_network_client.c +SRCS+= load_file.c +SRCS+= threads.c + +# required by base/file.cpp +.PATH: ${.CURDIR}/../liblog +SRCS+= fake_log_device.c +SRCS+= logd_write.c + +CFLAGS+= -DADB_HOST=1 +CFLAGS+= -DHAVE_FORKEXEC=1 +CFLAGS+= -DHAVE_SYMLINKS +CFLAGS+= -DHAVE_TERMIO_H +CFLAGS+= -DHAVE_SYS_SOCKET_H +CFLAGS+= -D_FILE_OFFSET_BITS=64 +CFLAGS+= -DFAKE_LOG_DEVICE=1 +CFLAGS+= -I${.CURDIR} +CFLAGS+= -I${.CURDIR}/../include +CFLAGS+= -I${.CURDIR}/../base/include + +CXXFLAGS+= -std=gnu++11 +CXXFLAGS+= ${CFLAGS:N-std*} + +LDADD+= -lcrypto -lpthread -lusb -lz +DPADD+= ${LIBCRYPTO} ${LIBPTHREAD} ${LIBUSB} ${LIBZ} + +.include diff --git a/devel/android-tools-adb-devel/files/get_my_path_freebsd.cpp b/devel/android-tools-adb-devel/files/get_my_path_freebsd.cpp new file mode 100644 index 000000000000..c935adf1292a --- /dev/null +++ b/devel/android-tools-adb-devel/files/get_my_path_freebsd.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2009 bsdroid project + * Alexey Tarasov + * + * Copyright (C) 2007 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 +#include +#include + +void +get_my_path(char *exe, size_t maxLen) +{ + int mib[4] = { + CTL_KERN, + KERN_PROC, + KERN_PROC_PATHNAME, + getpid() + }; + + sysctl(mib, 4, exe, &maxLen, NULL, 0); +} diff --git a/devel/android-tools-adb-devel/files/patch-adb_commandline.cpp b/devel/android-tools-adb-devel/files/patch-adb_commandline.cpp new file mode 100644 index 000000000000..20e9bea6545d --- /dev/null +++ b/devel/android-tools-adb-devel/files/patch-adb_commandline.cpp @@ -0,0 +1,24 @@ +--- adb/commandline.cpp.orig 2015-06-17 22:20:38 UTC ++++ adb/commandline.cpp +@@ -637,7 +637,7 @@ static int ppp(int argc, const char** ar + + // copy args + ppp_args = (const char **) alloca(sizeof(char *) * argc + 1); +- ppp_args[0] = "pppd"; ++ ppp_args[0] = "ppp"; + for (i = 2 ; i < argc ; i++) { + //argv[2] and beyond become ppp_args[1] and beyond + ppp_args[i - 1] = argv[i]; +@@ -651,10 +651,10 @@ static int ppp(int argc, const char** ar + adb_close(STDERR_FILENO); + adb_close(fd); + +- err = execvp("pppd", (char * const *)ppp_args); ++ err = execvp("ppp", (char * const *)ppp_args); + + if (err < 0) { +- perror("execing pppd"); ++ perror("execing ppp"); + } + exit(-1); + } else { diff --git a/devel/android-tools-adb-devel/files/patch-base_file.cpp b/devel/android-tools-adb-devel/files/patch-base_file.cpp new file mode 100644 index 000000000000..29638851e71d --- /dev/null +++ b/devel/android-tools-adb-devel/files/patch-base_file.cpp @@ -0,0 +1,10 @@ +--- base/file.cpp.orig 2015-05-27 20:24:08 UTC ++++ base/file.cpp +@@ -21,6 +21,7 @@ + #include + #include + ++#include // strerror + #include + + #include "base/macros.h" // For TEMP_FAILURE_RETRY on Darwin. diff --git a/devel/android-tools-adb-devel/files/patch-base_logging.cpp b/devel/android-tools-adb-devel/files/patch-base_logging.cpp new file mode 100644 index 000000000000..53bf6fdd0537 --- /dev/null +++ b/devel/android-tools-adb-devel/files/patch-base_logging.cpp @@ -0,0 +1,18 @@ +--- base/logging.cpp.orig 2015-06-17 22:20:38 UTC ++++ base/logging.cpp +@@ -23,12 +23,14 @@ + #include + + // For getprogname(3) or program_invocation_short_name. +-#if defined(__ANDROID__) || defined(__APPLE__) ++#if !defined(_WIN32) && !defined(__GLIBC__) + #include + #elif defined(__GLIBC__) + #include + #endif + ++#include // strlen ++#include // fprintf + #include + #include + #include diff --git a/devel/android-tools-adb-devel/files/patch-libcutils_threads.c b/devel/android-tools-adb-devel/files/patch-libcutils_threads.c new file mode 100644 index 000000000000..a275276f095d --- /dev/null +++ b/devel/android-tools-adb-devel/files/patch-libcutils_threads.c @@ -0,0 +1,38 @@ +--- libcutils/threads.c.orig 2015-06-17 22:20:38 UTC ++++ libcutils/threads.c +@@ -29,6 +29,14 @@ + #include + #elif defined(_WIN32) + #include ++#elif defined(__DragonFly__) ++#include ++#elif defined(__FreeBSD__) ++#include ++#include ++#include ++#elif defined(__NetBSD__) ++#include + #endif + + // No definition needed for Android because we'll just pick up bionic's copy. +@@ -40,6 +48,20 @@ pid_t gettid() { + return syscall(__NR_gettid); + #elif defined(_WIN32) + return GetCurrentThreadId(); ++#elif defined(__DragonFly__) ++ return lwp_gettid(); ++#elif defined(__NetBSD__) ++ return _lwp_self(); ++#elif defined(__FreeBSD__) ++# if __FreeBSD_version > 900030 ++ return pthread_getthreadid_np(); ++# else ++ long lwpid; ++ thr_self(&lwpid); ++ return lwpid; ++# endif ++#else ++ return (intptr_t) pthread_self(); + #endif + } + #endif // __ANDROID__ diff --git a/devel/android-tools-adb-devel/files/patch-liblog_logd__write.c b/devel/android-tools-adb-devel/files/patch-liblog_logd__write.c new file mode 100644 index 000000000000..d9a8890325d4 --- /dev/null +++ b/devel/android-tools-adb-devel/files/patch-liblog_logd__write.c @@ -0,0 +1,12 @@ +--- liblog/logd_write.c.orig 2015-05-27 20:24:08 UTC ++++ liblog/logd_write.c +@@ -22,7 +22,9 @@ + #include + #endif + #include ++#if (FAKE_LOG_DEVICE == 0) + #include ++#endif + #include + #include + #include diff --git a/devel/android-tools-adb-devel/files/usb_libusb.cpp b/devel/android-tools-adb-devel/files/usb_libusb.cpp new file mode 100644 index 000000000000..50a6854332ab --- /dev/null +++ b/devel/android-tools-adb-devel/files/usb_libusb.cpp @@ -0,0 +1,655 @@ +/* + * Copyright (C) 2009 bsdroid project + * Alexey Tarasov + * + * Copyright (C) 2007 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. + */ + +#define TRACE_TAG TRACE_USB + +#include "sysdeps.h" + +#include +#include +#include +#include +#include +#include + +#include "adb.h" +#include "transport.h" + +ADB_MUTEX_DEFINE( usb_lock ); +static libusb_context *ctx = NULL; + +struct usb_handle +{ + usb_handle *prev; + usb_handle *next; + + libusb_device *dev; + libusb_device_handle *devh; + int interface; + uint8_t dev_bus; + uint8_t dev_addr; + + int zero_mask; + unsigned char end_point_address[2]; + char serial[128]; + + adb_cond_t notify; + adb_mutex_t lock; +}; + +static usb_handle handle_list = { + .prev = &handle_list, + .next = &handle_list, +}; + +static void +usb_cleanup() +{ + libusb_exit(ctx); +} + +static void +report_bulk_libusb_error(int r) +{ + switch (r) { + case LIBUSB_ERROR_TIMEOUT: + D("Transfer timeout\n"); + break; + + case LIBUSB_ERROR_PIPE: + D("Control request is not supported\n"); + break; + + case LIBUSB_ERROR_OVERFLOW: + D("Device offered more data\n"); + break; + + case LIBUSB_ERROR_NO_DEVICE : + D("Device was disconnected\n"); + break; + + default: + D("Error %d during transfer\n", r); + break; + }; +} + +static int +usb_bulk_write(usb_handle *uh, const void *data, int len) +{ + int r = 0; + int transferred = 0; + + r = libusb_bulk_transfer(uh->devh, uh->end_point_address[1], + (unsigned char *)data, len, &transferred, 0); + + if (r != 0) { + D("usb_bulk_write(): "); + report_bulk_libusb_error(r); + return r; + } + + return (transferred); +} + +static int +usb_bulk_read(usb_handle *uh, void *data, int len) +{ + int r = 0; + int transferred = 0; + + r = libusb_bulk_transfer(uh->devh, uh->end_point_address[0], + (unsigned char *)data, len, + &transferred, 0); + + if (r != 0) { + D("usb_bulk_read(): "); + report_bulk_libusb_error(r); + return r; + } + + return (transferred); +} + +int +usb_write(usb_handle *uh, const void *_data, int len) +{ + unsigned char *data = (unsigned char*) _data; + int n; + int need_zero = 0; + + if (uh->zero_mask == 1) { + if (!(len & uh->zero_mask)) { + need_zero = 1; + } + } + + D("usb_write(): %p:%d -> transport %p\n", _data, len, uh); + + while (len > 0) { + int xfer = (len > 4096) ? 4096 : len; + + n = usb_bulk_write(uh, data, xfer); + + if (n != xfer) { + D("usb_write(): failed for transport %p (%d bytes left)\n", uh, len); + return -1; + } + + len -= xfer; + data += xfer; + } + + if (need_zero){ + n = usb_bulk_write(uh, _data, 0); + + if (n < 0) { + D("usb_write(): failed to finish operation for transport %p\n", uh); + } + return n; + } + + return 0; +} + +int +usb_read(usb_handle *uh, void *_data, int len) +{ + unsigned char *data = (unsigned char*)_data; + int n; + + D("usb_read(): %p:%d <- transport %p\n", _data, len, uh); + + while (len > 0) { + int xfer = (len > 4096) ? 4096 : len; + + n = usb_bulk_read(uh, data, xfer); + + if (n != xfer) { + if (n > 0) { + data += n; + len -= n; + continue; + } + + D("usb_read(): failed for transport %p (%d bytes left)\n", uh, len); + return -1; + } + + len -= xfer; + data += xfer; + } + + return 0; +} + +int +usb_close(usb_handle *h) +{ + D("usb_close(): closing transport %p\n", h); + adb_mutex_lock(&usb_lock); + + h->next->prev = h->prev; + h->prev->next = h->next; + h->prev = NULL; + h->next = NULL; + + libusb_release_interface(h->devh, h->interface); + libusb_close(h->devh); + libusb_unref_device(h->dev); + + adb_mutex_unlock(&usb_lock); + + free(h); + + return (0); +} + +void +usb_kick(usb_handle *h) +{ + D("usb_kick(): kicking transport %p\n", h); + + adb_mutex_lock(&h->lock); + unregister_usb_transport(h); + adb_mutex_unlock(&h->lock); + + h->next->prev = h->prev; + h->prev->next = h->next; + h->prev = NULL; + h->next = NULL; + + libusb_release_interface(h->devh, h->interface); + libusb_close(h->devh); + libusb_unref_device(h->dev); + free(h); +} + +static int +check_usb_interface(libusb_interface const *interface, + libusb_device_descriptor const *desc, + usb_handle *uh) +{ + int e; + + if (interface->num_altsetting == 0) { + D("check_usb_interface(): No interface settings\n"); + return -1; + } + + libusb_interface_descriptor const *idesc = &interface->altsetting[0]; + + if (idesc->bNumEndpoints != 2) { + D("check_usb_interface(): Interface have not 2 endpoints, ignoring\n"); + return -1; + } + + for (e = 0; e < idesc->bNumEndpoints; e++) { + libusb_endpoint_descriptor const *edesc = &idesc->endpoint[e]; + + if (edesc->bmAttributes != LIBUSB_TRANSFER_TYPE_BULK) { + D("check_usb_interface(): Endpoint (%u) is not bulk (%u), ignoring\n", + edesc->bmAttributes, LIBUSB_TRANSFER_TYPE_BULK); + return -1; + } + + if (edesc->bEndpointAddress & LIBUSB_ENDPOINT_IN) + uh->end_point_address[0] = edesc->bEndpointAddress; + else + uh->end_point_address[1] = edesc->bEndpointAddress; + + /* aproto 01 needs 0 termination */ + if (idesc->bInterfaceProtocol == 0x01) { + uh->zero_mask = edesc->wMaxPacketSize - 1; + D("check_usb_interface(): Forced Android interface protocol v.1\n"); + } + } + + D("check_usb_interface(): Device: %04x:%04x " + "iclass: %x, isclass: %x, iproto: %x ep: %x/%x-> ", + desc->idVendor, desc->idProduct, idesc->bInterfaceClass, + idesc->bInterfaceSubClass, idesc->bInterfaceProtocol, + uh->end_point_address[0], uh->end_point_address[1]); + + if (!is_adb_interface(desc->idVendor, desc->idProduct, + idesc->bInterfaceClass, idesc->bInterfaceSubClass, + idesc->bInterfaceProtocol)) + { + D("not matches\n"); + return -1; + } + + D("matches\n"); + return 1; +} + +static int +check_usb_interfaces(libusb_config_descriptor *config, + libusb_device_descriptor *desc, + usb_handle *uh) +{ + int i; + + for (i = 0; i < config->bNumInterfaces; ++i) { + if (check_usb_interface(&config->interface[i], desc, uh) != -1) { + /* found some interface and saved information about it */ + D("check_usb_interfaces(): Interface %d of %04x:%04x " + "matches Android device\n", i, desc->idVendor, + desc->idProduct); + + return i; + } + } + + return -1; +} + +static int +register_device(usb_handle *uh, const char *serial) +{ + D("register_device(): Registering %p [%s] as USB transport\n", + uh, serial); + + usb_handle* usb = reinterpret_cast(calloc(1, sizeof(usb_handle))); + if (usb == nullptr) fatal("couldn't allocate usb_handle"); + memcpy(usb, uh, sizeof(usb_handle)); + strcpy(usb->serial, uh->serial); + + adb_cond_init(&usb->notify, 0); + adb_mutex_init(&usb->lock, 0); + + adb_mutex_lock(&usb_lock); + + usb->next = &handle_list; + usb->prev = handle_list.prev; + usb->prev->next = usb; + usb->next->prev = usb; + + adb_mutex_unlock(&usb_lock); + + char devpath[64]; + snprintf(devpath, sizeof(devpath), "ugen%d.%d", + uh->dev_bus, uh->dev_addr); + + register_usb_transport(usb, serial, devpath, 1); + + return (1); +} + +static int +already_registered(usb_handle *uh) +{ + usb_handle *usb = NULL; + int exists = 0; + + adb_mutex_lock(&usb_lock); + + for (usb = handle_list.next; usb != &handle_list; usb = usb->next) { + if ((usb->dev_bus == uh->dev_bus) && + (usb->dev_addr == uh->dev_addr)) + { + exists = 1; + break; + } + } + + adb_mutex_unlock(&usb_lock); + + return exists; +} + +static void +check_device(libusb_device *dev) +{ + usb_handle uh; + int i = 0; + int found = -1; + char serial[256] = {0}; + + libusb_device_descriptor desc; + libusb_config_descriptor *config = NULL; + + int r = libusb_get_device_descriptor(dev, &desc); + + if (r != LIBUSB_SUCCESS) { + D("check_device(): Failed to get device descriptor\n"); + return; + } + + if ((desc.idVendor == 0) && (desc.idProduct == 0)) + return; + + D("check_device(): Probing usb device %04x:%04x\n", + desc.idVendor, desc.idProduct); + + if (!is_adb_interface(desc.idVendor, desc.idProduct, + ADB_CLASS, ADB_SUBCLASS, ADB_PROTOCOL)) + { + D("check_device(): Ignored due unknown vendor id\n"); + return; + } + + uh.dev_bus = libusb_get_bus_number(dev); + uh.dev_addr = libusb_get_device_address(dev); + + if (already_registered(&uh)) { + D("check_device(): Device (bus: %d, address: %d) " + "is already registered\n", uh.dev_bus, uh.dev_addr); + return; + } + + D("check_device(): Device bus: %d, address: %d\n", + uh.dev_bus, uh.dev_addr); + + r = libusb_get_active_config_descriptor(dev, &config); + + if (r != 0) { + if (r == LIBUSB_ERROR_NOT_FOUND) { + D("check_device(): Device %4x:%4x is unconfigured\n", + desc.idVendor, desc.idProduct); + return; + } + + D("check_device(): Failed to get configuration for %4x:%4x\n", + desc.idVendor, desc.idProduct); + return; + } + + if (config == NULL) { + D("check_device(): Sanity check failed after " + "getting active config\n"); + return; + } + + if (config->interface != NULL) { + found = check_usb_interfaces(config, &desc, &uh); + } + + /* not needed anymore */ + libusb_free_config_descriptor(config); + + r = libusb_open(dev, &uh.devh); + uh.dev = dev; + + if (r != 0) { + switch (r) { + case LIBUSB_ERROR_NO_MEM: + D("check_device(): Memory allocation problem\n"); + break; + + case LIBUSB_ERROR_ACCESS: + D("check_device(): Permissions problem, " + "current user priveleges are messed up?\n"); + break; + + case LIBUSB_ERROR_NO_DEVICE: + D("check_device(): Device disconected, bad cable?\n"); + break; + + default: + D("check_device(): libusb triggered error %d\n", r); + } + // skip rest + found = -1; + } + + if (found >= 0) { + D("check_device(): Device matches Android interface\n"); + // read the device's serial number + memset(serial, 0, sizeof(serial)); + uh.interface = found; + + r = libusb_claim_interface(uh.devh, uh.interface); + + if (r < 0) { + D("check_device(): Failed to claim interface %d\n", + uh.interface); + + goto fail; + } + + if (desc.iSerialNumber) { + // reading serial + uint16_t buffer[128] = {0}; + uint16_t languages[128] = {0}; + int languageCount = 0; + + memset(languages, 0, sizeof(languages)); + r = libusb_control_transfer(uh.devh, + LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_RECIPIENT_DEVICE, + LIBUSB_REQUEST_GET_DESCRIPTOR, LIBUSB_DT_STRING << 8, + 0, (uint8_t *)languages, sizeof(languages), 0); + + if (r <= 0) { + D("check_device(): Failed to get languages count\n"); + goto fail; + } + + languageCount = (r - 2) / 2; + + for (i = 1; i <= languageCount; ++i) { + memset(buffer, 0, sizeof(buffer)); + + r = libusb_control_transfer(uh.devh, + LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_RECIPIENT_DEVICE, + LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc.iSerialNumber, + languages[i], (uint8_t *)buffer, sizeof(buffer), 0); + + if (r > 0) { /* converting serial */ + int j = 0; + r /= 2; + + for (j = 1; j < r; ++j) + serial[j - 1] = buffer[j]; + + serial[j - 1] = '\0'; + break; /* languagesCount cycle */ + } + } + + if (register_device(&uh, serial) == 0) { + D("check_device(): Failed to register device\n"); + goto fail_interface; + } + + libusb_ref_device(dev); + } + } + + return; + +fail_interface: + libusb_release_interface(uh.devh, uh.interface); + +fail: + libusb_close(uh.devh); + uh.devh = NULL; +} + +static int +check_device_connected(usb_handle *uh) +{ + int r = libusb_kernel_driver_active(uh->devh, uh->interface); + + if (r == LIBUSB_ERROR_NO_DEVICE) + return 0; + + if (r < 0) + return -1; + + return 1; +} + +static void +kick_disconnected() +{ + usb_handle *usb = NULL; + + adb_mutex_lock(&usb_lock); + + for (usb = handle_list.next; usb != &handle_list; usb = usb->next) { + if (check_device_connected(usb) == 0) { + D("kick_disconnected(): Transport %p is not online anymore\n", + usb); + + usb_kick(usb); + } + } + + adb_mutex_unlock(&usb_lock); +} + +static void +scan_usb_devices() +{ + D("scan_usb_devices(): started\n"); + + libusb_device **devs= NULL; + libusb_device *dev= NULL; + ssize_t cnt = libusb_get_device_list(ctx, &devs); + + if (cnt < 0) { + D("scan_usb_devices(): Failed to get device list (error: %zd)\n", + cnt); + + return; + } + + int i = 0; + + while ((dev = devs[i++]) != NULL) { + check_device(dev); + } + + libusb_free_device_list(devs, 1); +} + +static void * +device_poll_thread(void* unused) +{ + D("device_poll_thread(): Created USB scan thread\n"); + + for (;;) { + sleep(5); + kick_disconnected(); + scan_usb_devices(); + } + + /* never reaching this point */ + return (NULL); +} + +static void +sigalrm_handler(int signo) +{ + /* nothing */ +} + +void +usb_init() +{ + D("usb_init(): started\n"); + struct sigaction actions; + + atexit(usb_cleanup); + int r = libusb_init(&ctx); + + if (r != LIBUSB_SUCCESS) { + fatal_errno("Failed to init libusb\n"); + } + + memset(&actions, 0, sizeof(actions)); + + sigemptyset(&actions.sa_mask); + + actions.sa_flags = 0; + actions.sa_handler = sigalrm_handler; + + sigaction(SIGALRM, &actions, NULL); + + /* initial device scan */ + scan_usb_devices(); + + /* starting USB event polling thread */ + if (!adb_thread_create(device_poll_thread, nullptr)) { + fatal_errno("cannot create USB scan thread\n"); + } + + D("usb_init(): finished\n"); +} diff --git a/devel/android-tools-adb/Makefile b/devel/android-tools-adb/Makefile index 1601db4e221c..9785042c8dea 100644 --- a/devel/android-tools-adb/Makefile +++ b/devel/android-tools-adb/Makefile @@ -2,7 +2,8 @@ PORTNAME= android-tools-adb DISTVERSIONPREFIX= android- -DISTVERSION= 5.1.1_r4 +DISTVERSION?= 5.1.1_r4 +PORTREVISION?= 0 CATEGORIES= devel MASTER_SITES= https://anonscm.debian.org/cgit/android-tools/android-tools.git/plain/debian/:bashcomp DISTFILES= ${DISTNAME}${EXTRACT_SUFX} \ @@ -18,10 +19,14 @@ USE_GITHUB= yes GH_ACCOUNT= android GH_PROJECT= platform_system_core -USES= uidfix +CONFLICTS_INSTALL?= ${PORTNAME}-devel-* + +USES+= uidfix +.ifndef BUILD_WRKSRC WRKSRC_SUBDIR= adb +.endif MAKEFILE= ${FILESDIR}/Makefile -MAKE_ENV= BINDIR="${PREFIX}/bin" +MAKE_ENV+= BINDIR="${PREFIX}/bin" PLIST_FILES= bin/adb \ %%BASH%%etc/bash_completion.d/adb -- cgit