Merge "Support the full length of USB serial numbers."

This commit is contained in:
Elliott Hughes 2015-04-28 00:18:29 +00:00 committed by Gerrit Code Review
commit 06d2128f87
3 changed files with 45 additions and 89 deletions

View file

@ -167,6 +167,7 @@ LOCAL_STATIC_LIBRARIES := \
libbase \ libbase \
libcrypto_static \ libcrypto_static \
libcutils \ libcutils \
liblog \
$(EXTRA_STATIC_LIBS) \ $(EXTRA_STATIC_LIBS) \
# libc++ not available on windows yet # libc++ not available on windows yet

View file

@ -28,6 +28,8 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <string>
#include "adb_io.h" #include "adb_io.h"
static transport_type __adb_transport = kTransportAny; static transport_type __adb_transport = kTransportAny;
@ -108,44 +110,40 @@ const char *adb_error(void)
static int switch_socket_transport(int fd) static int switch_socket_transport(int fd)
{ {
char service[64]; std::string service;
char tmp[5]; if (__adb_serial) {
int len; service += "host:transport:";
service += __adb_serial;
if (__adb_serial) } else {
snprintf(service, sizeof service, "host:transport:%s", __adb_serial);
else {
const char* transport_type = "???"; const char* transport_type = "???";
switch (__adb_transport) {
switch (__adb_transport) { case kTransportUsb:
case kTransportUsb: transport_type = "transport-usb";
transport_type = "transport-usb"; break;
break; case kTransportLocal:
case kTransportLocal: transport_type = "transport-local";
transport_type = "transport-local"; break;
break; case kTransportAny:
case kTransportAny: transport_type = "transport-any";
transport_type = "transport-any"; break;
break; case kTransportHost:
case kTransportHost: // no switch necessary
// no switch necessary return 0;
return 0;
break;
} }
service += "host:";
snprintf(service, sizeof service, "host:%s", transport_type); service += transport_type;
} }
len = strlen(service);
snprintf(tmp, sizeof tmp, "%04x", len);
if(!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service, len)) { char tmp[5];
snprintf(tmp, sizeof(tmp), "%04zx", service.size());
if (!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service.c_str(), service.size())) {
strcpy(__adb_error, "write failure during connection"); strcpy(__adb_error, "write failure during connection");
adb_close(fd); adb_close(fd);
return -1; return -1;
} }
D("Switch transport in progress\n"); D("Switch transport in progress\n");
if(adb_status(fd)) { if (adb_status(fd)) {
adb_close(fd); adb_close(fd);
D("Switch transport failed\n"); D("Switch transport failed\n");
return -1; return -1;

View file

@ -31,11 +31,11 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
#include <linux/usb/ch9.h> #include <linux/usb/ch9.h>
#else
#include <linux/usb_ch9.h> #include <base/file.h>
#endif #include <base/stringprintf.h>
#include <base/strings.h>
#include "adb.h" #include "adb.h"
#include "transport.h" #include "transport.h"
@ -567,13 +567,10 @@ int usb_close(usb_handle *h)
return 0; return 0;
} }
static void register_device(const char *dev_name, const char *devpath, static void register_device(const char* dev_name, const char* dev_path,
unsigned char ep_in, unsigned char ep_out, unsigned char ep_in, unsigned char ep_out,
int interface, int serial_index, unsigned zero_mask) int interface, int serial_index, unsigned zero_mask)
{ {
int n = 0;
char serial[256];
/* Since Linux will not reassign the device ID (and dev_name) /* Since Linux will not reassign the device ID (and dev_name)
** as long as the device is open, we can add to the list here ** as long as the device is open, we can add to the list here
** once we open it and remove from the list when we're finally ** once we open it and remove from the list when we're finally
@ -621,8 +618,7 @@ static void register_device(const char *dev_name, const char *devpath,
D("[ usb opened %s%s, fd=%d]\n", usb->fname, (usb->writeable ? "" : " (read-only)"), usb->desc); D("[ usb opened %s%s, fd=%d]\n", usb->fname, (usb->writeable ? "" : " (read-only)"), usb->desc);
if (usb->writeable) { if (usb->writeable) {
n = ioctl(usb->desc, USBDEVFS_CLAIMINTERFACE, &interface); if (ioctl(usb->desc, USBDEVFS_CLAIMINTERFACE, &interface) != 0) {
if (n != 0) {
D("[ usb ioctl(%d, USBDEVFS_CLAIMINTERFACE) failed: %s]\n", usb->desc, strerror(errno)); D("[ usb ioctl(%d, USBDEVFS_CLAIMINTERFACE) failed: %s]\n", usb->desc, strerror(errno));
adb_close(usb->desc); adb_close(usb->desc);
free(usb); free(usb);
@ -630,56 +626,17 @@ static void register_device(const char *dev_name, const char *devpath,
} }
} }
/* read the device's serial number */ // Read the device's serial number.
serial[0] = 0; std::string serial_path =
memset(serial, 0, sizeof(serial)); android::base::StringPrintf("/sys/bus/usb/devices/%s/serial", dev_path + 4);
if (serial_index) { std::string serial;
struct usbdevfs_ctrltransfer ctrl; if (!android::base::ReadFileToString(serial_path, &serial)) {
__u16 buffer[128]; D("[ usb read %s failed: %s ]\n", serial_path.c_str(), strerror(errno));
__u16 languages[128]; adb_close(usb->desc);
int i, result; free(usb);
int languageCount = 0; return;
memset(languages, 0, sizeof(languages));
memset(&ctrl, 0, sizeof(ctrl));
// read list of supported languages
ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
ctrl.wValue = (USB_DT_STRING << 8) | 0;
ctrl.wIndex = 0;
ctrl.wLength = sizeof(languages);
ctrl.data = languages;
ctrl.timeout = 1000;
result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl);
if (result > 0)
languageCount = (result - 2) / 2;
for (i = 1; i <= languageCount; i++) {
memset(buffer, 0, sizeof(buffer));
memset(&ctrl, 0, sizeof(ctrl));
ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
ctrl.wValue = (USB_DT_STRING << 8) | serial_index;
ctrl.wIndex = __le16_to_cpu(languages[i]);
ctrl.wLength = sizeof(buffer);
ctrl.data = buffer;
ctrl.timeout = 1000;
result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl);
if (result > 0) {
int i;
// skip first word, and copy the rest to the serial string, changing shorts to bytes.
result /= 2;
for (i = 1; i < result; i++)
serial[i - 1] = __le16_to_cpu(buffer[i]);
serial[i - 1] = 0;
break;
}
}
} }
serial = android::base::Trim(serial);
/* add to the end of the active handles */ /* add to the end of the active handles */
adb_mutex_lock(&usb_lock); adb_mutex_lock(&usb_lock);
@ -689,10 +646,10 @@ static void register_device(const char *dev_name, const char *devpath,
usb->next->prev = usb; usb->next->prev = usb;
adb_mutex_unlock(&usb_lock); adb_mutex_unlock(&usb_lock);
register_usb_transport(usb, serial, devpath, usb->writeable); register_usb_transport(usb, serial.c_str(), dev_path, usb->writeable);
} }
void* device_poll_thread(void* unused) static void* device_poll_thread(void* unused)
{ {
D("Created device thread\n"); D("Created device thread\n");
for(;;) { for(;;) {