Merge "Use glog for logging in the OSX USB layer"

This commit is contained in:
Siva Velusamy 2015-08-24 15:35:51 +00:00 committed by Gerrit Code Review
commit 9498fc68ec
2 changed files with 77 additions and 72 deletions

View file

@ -134,6 +134,7 @@ static void setup_daemon_logging(void) {
setvbuf(stderr, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0);
#endif #endif
fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid()); fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
LOG(INFO) << adb_version();
} }
int adb_main(int is_daemon, int server_port, int ack_reply_fd) { int adb_main(int is_daemon, int server_port, int ack_reply_fd) {

View file

@ -29,6 +29,9 @@
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <base/logging.h>
#include <base/stringprintf.h>
#include "adb.h" #include "adb.h"
#include "transport.h" #include "transport.h"
@ -81,7 +84,7 @@ InitUSB()
matchingDict = IOServiceMatching(kIOUSBInterfaceClassName); matchingDict = IOServiceMatching(kIOUSBInterfaceClassName);
if (!matchingDict) { if (!matchingDict) {
DBG("ERR: Couldn't create USB matching dictionary.\n"); LOG(ERROR) << "Couldn't create USB matching dictionary.";
return -1; return -1;
} }
@ -132,7 +135,7 @@ AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
&plugInInterface, &score); &plugInInterface, &score);
IOObjectRelease(usbInterface); IOObjectRelease(usbInterface);
if ((kIOReturnSuccess != kr) || (!plugInInterface)) { if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
DBG("ERR: Unable to create an interface plug-in (%08x)\n", kr); LOG(ERROR) << "Unable to create an interface plug-in (" << std::hex << kr << ")";
continue; continue;
} }
@ -143,7 +146,7 @@ AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
//* We only needed the plugin to get the interface, so discard it //* We only needed the plugin to get the interface, so discard it
(*plugInInterface)->Release(plugInInterface); (*plugInInterface)->Release(plugInInterface);
if (result || !iface) { if (result || !iface) {
DBG("ERR: Couldn't query the interface (%08x)\n", (int) result); LOG(ERROR) << "Couldn't query the interface (" << std::hex << result << ")";
continue; continue;
} }
@ -152,7 +155,8 @@ AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
kr = (*iface)->GetInterfaceProtocol(iface, &protocol); kr = (*iface)->GetInterfaceProtocol(iface, &protocol);
if(if_class != ADB_CLASS || subclass != ADB_SUBCLASS || protocol != ADB_PROTOCOL) { if(if_class != ADB_CLASS || subclass != ADB_SUBCLASS || protocol != ADB_PROTOCOL) {
// Ignore non-ADB devices. // Ignore non-ADB devices.
DBG("Ignoring interface with incorrect class/subclass/protocol - %d, %d, %d\n", if_class, subclass, protocol); LOG(DEBUG) << "Ignoring interface with incorrect class/subclass/protocol - " << if_class
<< ", " << subclass << ", " << protocol;
(*iface)->Release(iface); (*iface)->Release(iface);
continue; continue;
} }
@ -163,7 +167,7 @@ AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
//* Gotta love OS X //* Gotta love OS X
kr = (*iface)->GetDevice(iface, &usbDevice); kr = (*iface)->GetDevice(iface, &usbDevice);
if (kIOReturnSuccess != kr || !usbDevice) { if (kIOReturnSuccess != kr || !usbDevice) {
DBG("ERR: Couldn't grab device from interface (%08x)\n", kr); LOG(ERROR) << "Couldn't grab device from interface (" << std::hex << kr << ")";
continue; continue;
} }
@ -177,7 +181,7 @@ AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
//* only needed this to find the plugin //* only needed this to find the plugin
(void)IOObjectRelease(usbDevice); (void)IOObjectRelease(usbDevice);
if ((kIOReturnSuccess != kr) || (!plugInInterface)) { if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
DBG("ERR: Unable to create a device plug-in (%08x)\n", kr); LOG(ERROR) << "Unable to create a device plug-in (" << std::hex << kr << ")";
continue; continue;
} }
@ -186,8 +190,7 @@ AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
//* only needed this to query the plugin //* only needed this to query the plugin
(*plugInInterface)->Release(plugInInterface); (*plugInInterface)->Release(plugInInterface);
if (result || !dev) { if (result || !dev) {
DBG("ERR: Couldn't create a device interface (%08x)\n", LOG(ERROR) << "Couldn't create a device interface (" << std::hex << result << ")";
(int) result);
continue; continue;
} }
@ -197,74 +200,74 @@ AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
kr = (*dev)->GetDeviceProduct(dev, &product); kr = (*dev)->GetDeviceProduct(dev, &product);
kr = (*dev)->GetLocationID(dev, &locationId); kr = (*dev)->GetLocationID(dev, &locationId);
if (kr == 0) { if (kr == 0) {
snprintf(devpathBuf, sizeof(devpathBuf), "usb:%" PRIu32 "X", snprintf(devpathBuf, sizeof(devpathBuf), "usb:%" PRIu32 "X", locationId);
(unsigned int)locationId);
devpath = devpathBuf; devpath = devpathBuf;
} }
kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex); kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
if (serialIndex > 0) { if (serialIndex > 0) {
IOUSBDevRequest req; IOUSBDevRequest req;
UInt16 buffer[256]; UInt16 buffer[256];
UInt16 languages[128]; UInt16 languages[128];
memset(languages, 0, sizeof(languages)); memset(languages, 0, sizeof(languages));
req.bmRequestType = req.bmRequestType =
USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice); USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
req.bRequest = kUSBRqGetDescriptor; req.bRequest = kUSBRqGetDescriptor;
req.wValue = (kUSBStringDesc << 8) | 0; req.wValue = (kUSBStringDesc << 8) | 0;
req.wIndex = 0; req.wIndex = 0;
req.pData = languages; req.pData = languages;
req.wLength = sizeof(languages); req.wLength = sizeof(languages);
kr = (*dev)->DeviceRequest(dev, &req); kr = (*dev)->DeviceRequest(dev, &req);
if (kr == kIOReturnSuccess && req.wLenDone > 0) { if (kr == kIOReturnSuccess && req.wLenDone > 0) {
int langCount = (req.wLenDone - 2) / 2, lang; int langCount = (req.wLenDone - 2) / 2, lang;
for (lang = 1; lang <= langCount; lang++) { for (lang = 1; lang <= langCount; lang++) {
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
memset(&req, 0, sizeof(req)); memset(&req, 0, sizeof(req));
req.bmRequestType = req.bmRequestType =
USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice); USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
req.bRequest = kUSBRqGetDescriptor; req.bRequest = kUSBRqGetDescriptor;
req.wValue = (kUSBStringDesc << 8) | serialIndex; req.wValue = (kUSBStringDesc << 8) | serialIndex;
req.wIndex = languages[lang]; req.wIndex = languages[lang];
req.pData = buffer; req.pData = buffer;
req.wLength = sizeof(buffer); req.wLength = sizeof(buffer);
kr = (*dev)->DeviceRequest(dev, &req); kr = (*dev)->DeviceRequest(dev, &req);
if (kr == kIOReturnSuccess && req.wLenDone > 0) { if (kr == kIOReturnSuccess && req.wLenDone > 0) {
int i, count; int i, count;
// skip first word, and copy the rest to the serial string,
// changing shorts to bytes.
count = (req.wLenDone - 1) / 2;
for (i = 0; i < count; i++)
serial[i] = buffer[i + 1];
serial[i] = 0;
break;
}
}
}
}
// skip first word, and copy the rest to the serial string,
// changing shorts to bytes.
count = (req.wLenDone - 1) / 2;
for (i = 0; i < count; i++)
serial[i] = buffer[i + 1];
serial[i] = 0;
break;
}
}
}
}
(*dev)->Release(dev); (*dev)->Release(dev);
DBG("INFO: Found vid=%04x pid=%04x serial=%s\n", vendor, product, LOG(INFO) << android::base::StringPrintf("Found vid=%04x pid=%04x serial=%s\n",
serial); vendor, product, serial);
usb_handle* handle = CheckInterface((IOUSBInterfaceInterface190**)iface, usb_handle* handle = CheckInterface((IOUSBInterfaceInterface190**)iface,
vendor, product); vendor, product);
if (handle == NULL) { if (handle == NULL) {
DBG("ERR: Could not find device interface: %08x\n", kr); LOG(ERROR) << "Could not find device interface";
(*iface)->Release(iface); (*iface)->Release(iface);
continue; continue;
} }
DBG("AndroidDeviceAdded calling register_usb_transport\n"); LOG(DEBUG) << "AndroidDeviceAdded calling register_usb_transport";
register_usb_transport(handle, (serial[0] ? serial : NULL), devpath, 1); register_usb_transport(handle, (serial[0] ? serial : NULL), devpath, 1);
// Register for an interest notification of this device being removed. // Register for an interest notification of this device being removed.
@ -278,7 +281,7 @@ AndroidInterfaceAdded(void *refCon, io_iterator_t iterator)
&handle->usbNotification); &handle->usbNotification);
if (kIOReturnSuccess != kr) { if (kIOReturnSuccess != kr) {
DBG("ERR: Unable to create interest notification (%08x)\n", kr); LOG(ERROR) << "Unable to create interest notification (" << std::hex << kr << ")";
} }
} }
} }
@ -290,10 +293,10 @@ AndroidInterfaceNotify(void *refCon, io_service_t service, natural_t messageType
if (messageType == kIOMessageServiceIsTerminated) { if (messageType == kIOMessageServiceIsTerminated) {
if (!handle) { if (!handle) {
DBG("ERR: NULL handle\n"); LOG(ERROR) << "NULL handle";
return; return;
} }
DBG("AndroidInterfaceNotify\n"); LOG(DEBUG) << "AndroidInterfaceNotify";
IOObjectRelease(handle->usbNotification); IOObjectRelease(handle->usbNotification);
usb_kick(handle); usb_kick(handle);
} }
@ -305,7 +308,7 @@ AndroidInterfaceNotify(void *refCon, io_service_t service, natural_t messageType
static bool ClearPipeStallBothEnds(IOUSBInterfaceInterface190** interface, UInt8 bulkEp) { static bool ClearPipeStallBothEnds(IOUSBInterfaceInterface190** interface, UInt8 bulkEp) {
IOReturn rc = (*interface)->ClearPipeStallBothEnds(interface, bulkEp); IOReturn rc = (*interface)->ClearPipeStallBothEnds(interface, bulkEp);
if (rc != kIOReturnSuccess) { if (rc != kIOReturnSuccess) {
DBG("ERR: Could not clear pipe: (%08x)\n", rc); LOG(ERROR) << "Could not clear pipe stall both ends: " << std::hex << rc;
return false; return false;
} }
return true; return true;
@ -325,14 +328,14 @@ CheckInterface(IOUSBInterfaceInterface190 **interface, UInt16 vendor, UInt16 pro
//* the endpoints in the interface descriptor to be instantiated //* the endpoints in the interface descriptor to be instantiated
kr = (*interface)->USBInterfaceOpen(interface); kr = (*interface)->USBInterfaceOpen(interface);
if (kr != kIOReturnSuccess) { if (kr != kIOReturnSuccess) {
DBG("ERR: Could not open interface: (%08x)\n", kr); LOG(ERROR) << "Could not open interface: " << std::hex << kr;
return NULL; return NULL;
} }
//* Get the number of endpoints associated with this interface //* Get the number of endpoints associated with this interface
kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints); kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);
if (kr != kIOReturnSuccess) { if (kr != kIOReturnSuccess) {
DBG("ERR: Unable to get number of endpoints: (%08x)\n", kr); LOG(ERROR) << "Unable to get number of endpoints: " << std::hex << kr;
goto err_get_num_ep; goto err_get_num_ep;
} }
@ -340,7 +343,7 @@ CheckInterface(IOUSBInterfaceInterface190 **interface, UInt16 vendor, UInt16 pro
if ((*interface)->GetInterfaceClass(interface, &interfaceClass) != kIOReturnSuccess || if ((*interface)->GetInterfaceClass(interface, &interfaceClass) != kIOReturnSuccess ||
(*interface)->GetInterfaceSubClass(interface, &interfaceSubClass) != kIOReturnSuccess || (*interface)->GetInterfaceSubClass(interface, &interfaceSubClass) != kIOReturnSuccess ||
(*interface)->GetInterfaceProtocol(interface, &interfaceProtocol) != kIOReturnSuccess) { (*interface)->GetInterfaceProtocol(interface, &interfaceProtocol) != kIOReturnSuccess) {
DBG("ERR: Unable to get interface class, subclass and protocol\n"); LOG(ERROR) << "Unable to get interface class, subclass and protocol";
goto err_get_interface_class; goto err_get_interface_class;
} }
@ -365,7 +368,8 @@ CheckInterface(IOUSBInterfaceInterface190 **interface, UInt16 vendor, UInt16 pro
kr = (*interface)->GetPipeProperties(interface, endpoint, &direction, kr = (*interface)->GetPipeProperties(interface, endpoint, &direction,
&number, &transferType, &maxPacketSize, &interval); &number, &transferType, &maxPacketSize, &interval);
if (kr != kIOReturnSuccess) { if (kr != kIOReturnSuccess) {
DBG("ERR: FindDeviceInterface - could not get pipe properties (%08x)\n", kr); LOG(ERROR) << "FindDeviceInterface - could not get pipe properties: "
<< std::hex << kr;
goto err_get_pipe_props; goto err_get_pipe_props;
} }
@ -414,12 +418,12 @@ void* RunLoopThread(void* unused)
IOObjectRelease(notificationIterator); IOObjectRelease(notificationIterator);
IONotificationPortDestroy(notificationPort); IONotificationPortDestroy(notificationPort);
DBG("RunLoopThread done\n"); LOG(DEBUG) << "RunLoopThread done";
return NULL; return NULL;
} }
static void usb_cleanup() { static void usb_cleanup() {
DBG("usb_cleanup\n"); LOG(DEBUG) << "usb_cleanup";
close_usb_devices(); close_usb_devices();
if (currentRunLoop) if (currentRunLoop)
CFRunLoopStop(currentRunLoop); CFRunLoopStop(currentRunLoop);
@ -460,18 +464,17 @@ int usb_write(usb_handle *handle, const void *buf, int len)
return -1; return -1;
if (NULL == handle->interface) { if (NULL == handle->interface) {
DBG("ERR: usb_write interface was null\n"); LOG(ERROR) << "usb_write interface was null";
return -1; return -1;
} }
if (0 == handle->bulkOut) { if (0 == handle->bulkOut) {
DBG("ERR: bulkOut endpoint not assigned\n"); LOG(ERROR) << "bulkOut endpoint not assigned";
return -1; return -1;
} }
result = result =
(*handle->interface)->WritePipe( (*handle->interface)->WritePipe(handle->interface, handle->bulkOut, (void *)buf, len);
handle->interface, handle->bulkOut, (void *)buf, len);
if ((result == 0) && (handle->zero_mask)) { if ((result == 0) && (handle->zero_mask)) {
/* we need 0-markers and our transfer */ /* we need 0-markers and our transfer */
@ -485,7 +488,7 @@ int usb_write(usb_handle *handle, const void *buf, int len)
if (0 == result) if (0 == result)
return 0; return 0;
DBG("ERR: usb_write failed with status %d\n", result); LOG(ERROR) << "usb_write failed with status: " << std::hex << result;
return -1; return -1;
} }
@ -503,19 +506,19 @@ int usb_read(usb_handle *handle, void *buf, int len)
} }
if (NULL == handle->interface) { if (NULL == handle->interface) {
DBG("ERR: usb_read interface was null\n"); LOG(ERROR) << "usb_read interface was null";
return -1; return -1;
} }
if (0 == handle->bulkIn) { if (0 == handle->bulkIn) {
DBG("ERR: bulkIn endpoint not assigned\n"); LOG(ERROR) << "bulkIn endpoint not assigned";
return -1; return -1;
} }
result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes); result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
if (kIOUSBPipeStalled == result) { if (kIOUSBPipeStalled == result) {
DBG(" Pipe stalled, clearing stall.\n"); LOG(ERROR) << "Pipe stalled, clearing stall.\n";
(*handle->interface)->ClearPipeStall(handle->interface, handle->bulkIn); (*handle->interface)->ClearPipeStall(handle->interface, handle->bulkIn);
result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes); result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
} }
@ -523,7 +526,7 @@ int usb_read(usb_handle *handle, void *buf, int len)
if (kIOReturnSuccess == result) if (kIOReturnSuccess == result)
return 0; return 0;
else { else {
DBG("ERR: usb_read failed with status %x\n", result); LOG(ERROR) << "usb_read failed with status: " << std::hex << result;
} }
return -1; return -1;
@ -536,6 +539,7 @@ int usb_close(usb_handle *handle)
void usb_kick(usb_handle *handle) void usb_kick(usb_handle *handle)
{ {
LOG(INFO) << "Kicking handle";
/* release the interface */ /* release the interface */
if (!handle) if (!handle)
return; return;