⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb_osx.c

📁 Android 一些工具
💻 C
📖 第 1 页 / 共 2 页
字号:
    kr = (*dev)->CreateInterfaceIterator(dev, &request, &iterator);    if (kr != kIOReturnSuccess) {        DBG("ERR: Couldn't create a device interface iterator: (%08x)\n", kr);        return NULL;    }    while ((usbInterface = IOIteratorNext(iterator))) {    //* Create an intermediate plugin        kr = IOCreatePlugInInterfaceForService(                usbInterface,                kIOUSBInterfaceUserClientTypeID,                kIOCFPlugInInterfaceID,                &plugInInterface,                &score);        //* No longer need the usbInterface object now that we have the plugin        (void) IOObjectRelease(usbInterface);        if ((kr != kIOReturnSuccess) || (!plugInInterface)) {            DBG("ERR: Unable to create plugin (%08x)\n", kr);            break;        }        //* Now create the interface interface for the interface        result = (*plugInInterface)->QueryInterface(                plugInInterface,                CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID),                (LPVOID) &interface);        //* No longer need the intermediate plugin        (*plugInInterface)->Release(plugInInterface);        if (result || !interface) {            DBG("ERR: Couldn't create interface interface: (%08x)\n",               (unsigned int) result);            break;        }        //* Now open the interface.  This will cause the pipes associated with        //* the endpoints in the interface descriptor to be instantiated        kr = (*interface)->USBInterfaceOpen(interface);        if (kr != kIOReturnSuccess)        {            DBG("ERR: Could not open interface: (%08x)\n", kr);            (void) (*interface)->Release(interface);            //* continue so we can try the next interface            continue;        }        //* Get the number of endpoints associated with this interface        kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);        if (kr != kIOReturnSuccess) {            DBG("ERR: Unable to get number of endpoints: (%08x)\n", kr);            goto next_interface;        }        //* Get interface class, subclass and protocol        if ((*interface)->GetInterfaceClass(interface, &interfaceClass) != kIOReturnSuccess ||            (*interface)->GetInterfaceSubClass(interface, &interfaceSubClass) != kIOReturnSuccess ||            (*interface)->GetInterfaceProtocol(interface, &interfaceProtocol) != kIOReturnSuccess)        {            DBG("ERR: Unable to get interface class, subclass and protocol\n");            goto next_interface;        }        //* check to make sure interface class, subclass and protocol match ADB        //* avoid opening mass storage endpoints        if (is_adb_interface(vendor, product, interfaceClass, interfaceSubClass, interfaceProtocol)) {            handle = calloc(1, sizeof(usb_handle));            //* Iterate over the endpoints for this interface and find the first            //* bulk in/out pipes available.  These will be our read/write pipes.            for (endpoint = 0; endpoint <= interfaceNumEndpoints; endpoint++) {                UInt8   transferType;                UInt16  maxPacketSize;                UInt8   interval;                UInt8   number;                UInt8   direction;                kr = (*interface)->GetPipeProperties(interface, endpoint, &direction,                        &number, &transferType, &maxPacketSize, &interval);                if (kIOReturnSuccess == kr) {                    if (kUSBBulk != transferType)                        continue;                    if (kUSBIn == direction)                        handle->bulkIn = endpoint;                    if (kUSBOut == direction)                        handle->bulkOut = endpoint;                    if (interfaceProtocol == 0x01) {                        handle->zero_mask = maxPacketSize - 1;                    }                } else {                    DBG("ERR: FindDeviceInterface - could not get pipe properties\n");                }            }            handle->interface = interface;            break;        }next_interface:        (*interface)->USBInterfaceClose(interface);        (*interface)->Release(interface);    }    return handle;}void* RunLoopThread(void* unused){    int i;    InitUSB();    currentRunLoop = CFRunLoopGetCurrent();    // Signal the parent that we are running    adb_mutex_lock(&start_lock);    adb_cond_signal(&start_cond);    adb_mutex_unlock(&start_lock);    CFRunLoopRun();    currentRunLoop = 0;    for (i = 0; i < kSupportedDeviceCount; i++) {	    IOObjectRelease(notificationIterators[i]);	}    IONotificationPortDestroy(notificationPort);    DBG("RunLoopThread done\n");    return NULL;    }static int initialized = 0;void usb_init(){    if (!initialized)    {        adb_thread_t    tid;        adb_mutex_init(&start_lock, NULL);        adb_cond_init(&start_cond, NULL);        if(adb_thread_create(&tid, RunLoopThread, NULL))            fatal_errno("cannot create input thread");        // Wait for initialization to finish        adb_mutex_lock(&start_lock);        adb_cond_wait(&start_cond, &start_lock);        adb_mutex_unlock(&start_lock);        adb_mutex_destroy(&start_lock);        adb_cond_destroy(&start_cond);        initialized = 1;    }}void usb_cleanup(){    DBG("usb_cleanup\n");    close_usb_devices();    if (currentRunLoop)        CFRunLoopStop(currentRunLoop);}int usb_write(usb_handle *handle, const void *buf, int len){    IOReturn    result;    if (!len)        return 0;    if (!handle)        return -1;    if (NULL == handle->interface) {        DBG("ERR: usb_write interface was null\n");        return -1;    }    if (0 == handle->bulkOut) {        DBG("ERR: bulkOut endpoint not assigned\n");        return -1;    }    result =        (*handle->interface)->WritePipe(                              handle->interface, handle->bulkOut, (void *)buf, len);    if ((result == 0) && (handle->zero_mask)) {        /* we need 0-markers and our transfer */        if(!(len & handle->zero_mask)) {            result =                (*handle->interface)->WritePipe(                        handle->interface, handle->bulkOut, (void *)buf, 0);        }    }    if (0 == result)        return 0;    DBG("ERR: usb_write failed with status %d\n", result);    return -1;}int usb_read(usb_handle *handle, void *buf, int len){    IOReturn result;    UInt32  numBytes = len;    if (!len) {        return 0;    }    if (!handle) {        return -1;    }    if (NULL == handle->interface) {        DBG("ERR: usb_read interface was null\n");        return -1;    }    if (0 == handle->bulkIn) {        DBG("ERR: bulkIn endpoint not assigned\n");        return -1;    }    result =      (*handle->interface)->ReadPipe(handle->interface,                                    handle->bulkIn, buf, &numBytes);    if (0 == result)        return 0;    else {        DBG("ERR: usb_read failed with status %d\n", result);    }    return -1;}int usb_close(usb_handle *handle){    return 0;}void usb_kick(usb_handle *handle){    /* release the interface */    if (handle->interface)    {        (*handle->interface)->USBInterfaceClose(handle->interface);        (*handle->interface)->Release(handle->interface);        handle->interface = 0;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -