inference-engine/thirdparty/movidius/XLink/pc/protocols/usb_boot.c
/**
 * @brief Find usb device address
 * @param input_addr  Device name (address) which would be returned. If not empty, we will try to
 *                  find device with this name
 *
 * @details
 * Find any device (device = 0):
 * <br> 1) Any myriad device:                    vid = AUTO_VID & pid = AUTO_PID
 * <br> 2) Any not booted myriad device:         vid = AUTO_VID & pid = AUTO_UNBOOTED_PID
 * <br> 3) Any booted myriad device:             vid = AUTO_VID & pid = DEFAULT_OPENPID
 * <br> 4) Specific Myriad 2 or Myriad X device: vid = AUTO_VID & pid = DEFAULT_UNBOOTPID_2485 or DEFAULT_UNBOOTPID_2150
 * <br><br> Find specific device (device != 0):
 * <br> device arg should be not null, search by addr (name) and return device struct
 *
 * @note
 * Index can be used to iterate through all connected myriad devices and save their names.
 * It will loop only over suitable devices specified by vid and pid
 */
@brief Find usb device address
@param input_addr  Device name (address) which would be returned. If not empty, we will try to
find device with this name



@note
Index can be used to iterate through all connected myriad devices and save their names.
It will loop only over suitable devices specified by vid and pid
@brief Find usb device address @param input_addr Device name ( address ) which would be returned . If not empty we will try to find device with this name @note Index can be used to iterate through all connected myriad devices and save their names . It will loop only over suitable devices specified by vid and pid
usbBootError_t usb_find_device_with_bcd(unsigned idx, char *input_addr,
                                        unsigned addrsize, void **device, int vid, int pid, uint16_t* bcdusb) {
    if (pthread_mutex_lock(&globalMutex)) {
        mvLog(MVLOG_ERROR, "globalMutex lock failed");
        return USB_BOOT_ERROR;
    }
    int searchByName = 0;
    static libusb_device **devs = NULL;
    libusb_device *dev = NULL;
    struct libusb_device_descriptor desc;
    int count = 0;
    size_t i;
    int res;
    if (!initialized) {
        mvLog(MVLOG_ERROR, "Library has not been initialized when loaded");
        if (pthread_mutex_unlock(&globalMutex)) {
            mvLog(MVLOG_ERROR, "globalMutex unlock failed");
        }
        return USB_BOOT_ERROR;
    }
    if (strlen(input_addr) > 1) {
        searchByName = 1;
    }
    if (!devs || idx == 0) {
        if (devs) {
            libusb_free_device_list(devs, 1);
            devs = 0;
        }
        if ((res = libusb_get_device_list(NULL, &devs)) < 0) {
            mvLog(MVLOG_DEBUG, "Unable to get USB device list: %s", libusb_strerror(res));
            if (pthread_mutex_unlock(&globalMutex)) {
                mvLog(MVLOG_ERROR, "globalMutex unlock failed");
            }
            return USB_BOOT_ERROR;
        }
    }
    i = 0;
    while ((dev = devs[i++]) != NULL) {
        if ((res = libusb_get_device_descriptor(dev, &desc)) < 0) {
            mvLog(MVLOG_DEBUG, "Unable to get USB device descriptor: %s", libusb_strerror(res));
            continue;
        }
        if ( (desc.idVendor == vid && desc.idProduct == pid)
             || (vid == AUTO_VID && pid == AUTO_PID
                 && isMyriadDevice(desc.idVendor, desc.idProduct))
             || (vid == AUTO_VID && (pid == AUTO_UNBOOTED_PID)
                 && isNotBootedMyriadDevice(desc.idVendor, desc.idProduct))
             || (vid == AUTO_VID && pid == desc.idProduct
                 && isNotBootedMyriadDevice(desc.idVendor, desc.idProduct))
             || (vid == AUTO_VID && pid == DEFAULT_OPENPID
                 && isBootedMyriadDevice(desc.idVendor, desc.idProduct)) )
        {
            if (device) {
                const char *dev_addr = gen_addr(dev, get_pid_by_name(input_addr));
                if (!strcmp(dev_addr, input_addr)) {
#if 0 
                    mvLog(MVLOG_DEBUG, "Found Address: %s - VID/PID %04x:%04x",
                          input_addr, desc.idVendor, desc.idProduct);
#endif
                    libusb_ref_device(dev);
                    libusb_free_device_list(devs, 1);
                    if (bcdusb)
                        *bcdusb = desc.bcdUSB;
                    *device = dev;
                    devs = 0;
                    if (pthread_mutex_unlock(&globalMutex)) {
                        mvLog(MVLOG_ERROR, "globalMutex unlock failed");
                    }
                    return USB_BOOT_SUCCESS;
                }
            } else if (searchByName) {
                const char *dev_addr = gen_addr(dev, desc.idProduct);
                if (!strcmp(dev_addr, input_addr)) {
#if 0 
                    mvLog(MVLOG_DEBUG, "Found Address: %s - VID/PID %04x:%04x",
                          input_addr, desc.idVendor, desc.idProduct);
#endif
                    if (pthread_mutex_unlock(&globalMutex)) {
                        mvLog(MVLOG_ERROR, "globalMutex unlock failed");
                    }
                    return USB_BOOT_SUCCESS;
                }
            } else if (idx == count) {
                const char *caddr = gen_addr(dev, desc.idProduct);
#if 0 
                mvLog(MVLOG_DEBUG, "Device %d Address: %s - VID/PID %04x:%04x",
                      idx, caddr, desc.idVendor, desc.idProduct);
#endif
                mv_strncpy(input_addr, addrsize, caddr, addrsize - 1);
                if (pthread_mutex_unlock(&globalMutex)) {
                    mvLog(MVLOG_ERROR, "globalMutex unlock failed");
                }
                return USB_BOOT_SUCCESS;
            }
            count++;
        }
    }
    libusb_free_device_list(devs, 1);
    devs = 0;
    if (pthread_mutex_unlock(&globalMutex)) {
        mvLog(MVLOG_ERROR, "globalMutex unlock failed");
    }
    return USB_BOOT_DEVICE_NOT_FOUND;
}
usbBootError_t usb_find_device_with_bcd ( unsigned idx , char * input_addr , unsigned addrsize , void * * device , int vid , int pid , uint16_t * bcdusb ) { if ( pthread_mutex_lock ( & globalMutex ) ) { mvLog ( MVLOG_ERROR , " " ) ; return USB_BOOT_ERROR ; } int searchByName = 0 ; static libusb_device * * devs = NULL ; libusb_device * dev = NULL ; struct libusb_device_descriptor desc ; int count = 0 ; size_t i ; int res ; if ( ! initialized ) { mvLog ( MVLOG_ERROR , " " ) ; if ( pthread_mutex_unlock ( & globalMutex ) ) { mvLog ( MVLOG_ERROR , " " ) ; } return USB_BOOT_ERROR ; } if ( strlen ( input_addr ) > 1 ) { searchByName = 1 ; } if ( ! devs || idx == 0 ) { if ( devs ) { libusb_free_device_list ( devs , 1 ) ; devs = 0 ; } if ( ( res = libusb_get_device_list ( NULL , & devs ) ) < 0 ) { mvLog ( MVLOG_DEBUG , " " , libusb_strerror ( res ) ) ; if ( pthread_mutex_unlock ( & globalMutex ) ) { mvLog ( MVLOG_ERROR , " " ) ; } return USB_BOOT_ERROR ; } } i = 0 ; while ( ( dev = devs [ i ++ ] ) != NULL ) { if ( ( res = libusb_get_device_descriptor ( dev , & desc ) ) < 0 ) { mvLog ( MVLOG_DEBUG , " " , libusb_strerror ( res ) ) ; continue ; } if ( ( desc . idVendor == vid && desc . idProduct == pid ) || ( vid == AUTO_VID && pid == AUTO_PID && isMyriadDevice ( desc . idVendor , desc . idProduct ) ) || ( vid == AUTO_VID && ( pid == AUTO_UNBOOTED_PID ) && isNotBootedMyriadDevice ( desc . idVendor , desc . idProduct ) ) || ( vid == AUTO_VID && pid == desc . idProduct && isNotBootedMyriadDevice ( desc . idVendor , desc . idProduct ) ) || ( vid == AUTO_VID && pid == DEFAULT_OPENPID && isBootedMyriadDevice ( desc . idVendor , desc . idProduct ) ) ) { if ( device ) { const char * dev_addr = gen_addr ( dev , get_pid_by_name ( input_addr ) ) ; if ( ! strcmp ( dev_addr , input_addr ) ) { #if 0 mvLog ( MVLOG_DEBUG , " " , input_addr , desc . idVendor , desc . idProduct ) ; #endif libusb_ref_device ( dev ) ; libusb_free_device_list ( devs , 1 ) ; if ( bcdusb ) * bcdusb = desc . bcdUSB ; * device = dev ; devs = 0 ; if ( pthread_mutex_unlock ( & globalMutex ) ) { mvLog ( MVLOG_ERROR , " " ) ; } return USB_BOOT_SUCCESS ; } } else if ( searchByName ) { const char * dev_addr = gen_addr ( dev , desc . idProduct ) ; if ( ! strcmp ( dev_addr , input_addr ) ) { #if 0 mvLog ( MVLOG_DEBUG , " " , input_addr , desc . idVendor , desc . idProduct ) ; #endif if ( pthread_mutex_unlock ( & globalMutex ) ) { mvLog ( MVLOG_ERROR , " " ) ; } return USB_BOOT_SUCCESS ; } } else if ( idx == count ) { const char * caddr = gen_addr ( dev , desc . idProduct ) ; #if 0 mvLog ( MVLOG_DEBUG , " " , idx , caddr , desc . idVendor , desc . idProduct ) ; #endif mv_strncpy ( input_addr , addrsize , caddr , addrsize - 1 ) ; if ( pthread_mutex_unlock ( & globalMutex ) ) { mvLog ( MVLOG_ERROR , " " ) ; } return USB_BOOT_SUCCESS ; } count ++ ; } } libusb_free_device_list ( devs , 1 ) ; devs = 0 ; if ( pthread_mutex_unlock ( & globalMutex ) ) { mvLog ( MVLOG_ERROR , " " ) ; } return USB_BOOT_DEVICE_NOT_FOUND ; }
@brief Find usb device address
 @param input_addr  Device name (address) which would be returned.
@brief Find usb device address @param input_addr Device name ( address ) which would be returned .
// Update device list if empty or if indx 0 // Loop over all usb devices, increase count only if myriad device // If found device have the same id and vid as input // Any myriad device // Any not booted myriad device // Any not booted with specific pid // Any booted device // To avoid spam in Debug mode // If the same add as input // To avoid spam in Debug mode // To avoid spam in Debug mode