int open_dev_check_string(struct usb_device *dev,usb_dev_handle *handle) { int len1,len2; char string1[256]; char string2[256]; len1 = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string1, sizeof(string1)); len2 = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string2, sizeof(string2)); // return 1; if((len1<0)||(len2<0)) { return 0; } #if 1 printf("iManufacturer:%s\n",string1); printf("iProduct:%s\n",string2); #endif #ifdef MY_Manufacturer if (strcmp(string1, MY_Manufacturer) != 0) return 0; // 一致せず #endif #ifdef MY_Product if (strcmp(string2, MY_Product) != 0) { if (strcmp(string2, MY_Product2) != 0) { return 0; // 一致せず } } #endif return 1; //合致した. }
static usb_dev_handle *findDevice(void) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *handle = 0; usb_find_busses(); usb_find_devices(); for (bus = usb_busses; bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { if (dev->descriptor.idVendor == USBDEV_SHARED_VENDOR && dev->descriptor.idProduct == USBDEV_SHARED_PRODUCT) { char string[256]; int len; handle = usb_open(dev); /* we need to open the device in order to query strings */ if (!handle) { fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror()); continue; } if(ignore_dev) break; /* now find out whether the device actually is obdev's Remote Sensor: */ len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); if (len < 0) { fprintf( stderr, "warning: cannot query manufacturer for device: %s\n", usb_strerror()); goto skipDevice; } //fprintf(stderr, "seen device from vendor ->%s<-\n", string); if (strcmp(string, "www.mikrocontroller.net/articles/USB_IR_Remote_Receiver") != 0 ) goto skipDevice; len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); if (len < 0) { fprintf(stderr, "warning: cannot query product for device: %s\n", usb_strerror()); goto skipDevice; } //fprintf(stderr, "seen product ->%s<-\n", string); if (strcmp(string, "USB IR Remote Receiver") == 0) break; skipDevice: usb_close(handle); handle = NULL; } } if (handle) break; } if (!handle) fprintf(stderr, "Could not find USB device www.anyma.ch/uDMX\n"); return handle; }
void find_device(t_udmx *x) { usb_dev_handle *handle = NULL; struct usb_bus *bus; struct usb_device *dev; usb_init(); usb_find_busses(); usb_find_devices(); for(bus=usb_get_busses(); bus; bus=bus->next) { for(dev=bus->devices; dev; dev=dev->next) { if(dev->descriptor.idVendor == USBDEV_SHARED_VENDOR && dev->descriptor.idProduct == USBDEV_SHARED_PRODUCT) { char string[256]; int len; handle = usb_open(dev); /* we need to open the device in order to query strings */ if(!handle) { error ("Warning: cannot open USB device: %s", usb_strerror()); continue; } /* now find out whether the device actually is udmx */ len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); if(len < 0) { post("udmx: warning: cannot query manufacturer for device: %s", usb_strerror()); goto skipDevice; } // post("udmx: seen device from vendor ->%s<-", string); if(strcmp(string, "www.anyma.ch") != 0) goto skipDevice; len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); if(len < 0) { post("udmx: warning: cannot query product for device: %s", usb_strerror()); goto skipDevice; } // post("udmx: seen product ->%s<-", string); if(strcmp(string, "udmx") == 0) break; skipDevice: usb_close(handle); handle = NULL; } } if(handle) break; } if(!handle) { post("udmx: Could not find USB device www.anyma.ch/udmx"); x->dev_handle = NULL; } else { x->dev_handle = handle; post("udmx: Found USB device www.anyma.ch/udmx"); } }
//-------------------------------------------------------------------------- void find_device(t_heavybox *x) { usb_dev_handle *handle = NULL; struct usb_bus *bus; struct usb_device *dev; usb_init(); usb_find_busses(); usb_find_devices(); for(bus=usb_busses; bus; bus=bus->next){ for(dev=bus->devices; dev; dev=dev->next){ if(dev->descriptor.idVendor == USBDEV_SHARED_VENDOR && dev->descriptor.idProduct == USBDEV_SHARED_PRODUCT){ char string[256]; int len; handle = usb_open(dev); /* we need to open the device in order to query strings */ if(!handle){ error ("Warning: cannot open USB device: %s", usb_strerror()); continue; } /* now find out whether the device actually is heavybox */ len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); if(len < 0){ post("heavybox: warning: cannot query manufacturer for device: %s", usb_strerror()); goto skipDevice; } if(strcmp(string, USB_CFG_VENDOR_NAME) != 0) goto skipDevice; len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); if(len < 0){ post("heavybox: warning: cannot query product for device: %s", usb_strerror()); goto skipDevice; } if(strcmp(string, USB_CFG_DEVICE_NAME) == 0) break; skipDevice: usb_close(handle); handle = NULL; } } if(handle) break; } if(!handle){ post("Could not find USB device workinprogress.ca/heavybox"); x->dev_handle = NULL; } else { x->dev_handle = handle; post("Found USB device workinprogress.ca/heavybox"); } }
/* This project uses the free shared default VID/PID. If you want to see an * example device lookup where an individually reserved PID is used, see our * RemoteSensor reference implementation. */ static usb_dev_handle *findDevice(void) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *handle = 0; usb_find_busses(); usb_find_devices(); for(bus=usb_busses; bus; bus=bus->next){ for(dev=bus->devices; dev; dev=dev->next){ if(dev->descriptor.idVendor == USBDEV_SHARED_VENDOR && dev->descriptor.idProduct == USBDEV_SHARED_PRODUCT){ char string[256]; int len; handle = usb_open(dev); /* we need to open the device in order to query strings */ if(!handle){ fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror()); continue; } len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); if(len < 0){ fprintf(stderr, "warning: cannot query manufacturer for device: %s\n", usb_strerror()); goto skipDevice; } if(strcmp(string, "www.fischl.de") != 0) goto skipDevice; len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); if(len < 0){ fprintf(stderr, "warning: cannot query product for device: %s\n", usb_strerror()); goto skipDevice; } // fprintf(stderr, "seen product ->%s<-\n", string); if(strcmp(string, "AVRUSBBoot") == 0) break; skipDevice: usb_close(handle); handle = NULL; } } if(handle) break; } if(!handle) fprintf(stderr, "Could not find USB device www.fischl.de/AVRUSBBoot\n"); return handle; }
int usbOpenDevice(usb_dev_handle ** device, int vendorID, char *vendorNamePattern, int productID, char *productNamePattern, char *serialNamePattern, FILE * printMatchingDevicesFp, FILE * warningsFp) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *handle = NULL; int errorCode = USBOPEN_ERR_NOTFOUND; usb_find_busses(); usb_find_devices(); for (bus = usb_get_busses(); bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { /* iterate over * all devices * on all * busses */ if ((vendorID == 0 || dev->descriptor.idVendor == vendorID) && (productID == 0 || dev->descriptor.idProduct == productID)) { char vendor[256], product[256], serial[256]; int len; handle = usb_open(dev); /* we need to open the device in order * to query strings */ if (!handle) { errorCode = USBOPEN_ERR_ACCESS; if (warningsFp != NULL) fprintf(warningsFp, "Warning: cannot open VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); continue; } /* * now check whether the names match: */ len = vendor[0] = 0; if (dev->descriptor.iManufacturer > 0) { len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, vendor, sizeof(vendor)); } if (len < 0) { errorCode = USBOPEN_ERR_ACCESS; if (warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query manufacturer for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } else { errorCode = USBOPEN_ERR_NOTFOUND; /* * printf("seen device from vendor ->%s<-\n", vendor); */ if (shellStyleMatch(vendor, vendorNamePattern)) { len = product[0] = 0; if (dev->descriptor.iProduct > 0) { len = usbGetStringAscii(handle, dev->descriptor.iProduct, product, sizeof(product)); } if (len < 0) { errorCode = USBOPEN_ERR_ACCESS; if (warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query product for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } else { errorCode = USBOPEN_ERR_NOTFOUND; /* * printf("seen product ->%s<-\n", product); */ if (shellStyleMatch(product, productNamePattern)) { len = serial[0] = 0; if (dev->descriptor.iSerialNumber > 0) { len = usbGetStringAscii(handle, dev->descriptor. iSerialNumber, serial, sizeof(serial)); } if (len < 0) { errorCode = USBOPEN_ERR_ACCESS; if (warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query serial for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } if (shellStyleMatch(serial, serialNamePattern)) { if (printMatchingDevicesFp != NULL) { if (serial[0] == 0) { fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product); } else { fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\" serial=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product, serial); } } else { break; } } } } } } usb_close(handle); handle = NULL; } } if (handle) /* we have found a deice */ break; } if (handle != NULL) { errorCode = 0; *device = handle; } if (printMatchingDevicesFp != NULL) /* never return an error for listing * only */ errorCode = 0; return errorCode; }
static int usbOpenDevice(usb_dev_handle **device, int vendor, char *vendorName, int product, char *productName) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *handle = NULL; int errorCode = USB_ERROR_NOTFOUND; static int didUsbInit = 0; if(!didUsbInit) { didUsbInit = 1; usb_init(); } usb_find_busses(); usb_find_devices(); for(bus=usb_get_busses(); bus; bus=bus->next) { // iterate over busses for(dev=bus->devices; dev; dev=dev->next) { // iterate over devices if(dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product) { // if we have found our device char string[256]; int len; handle = usb_open(dev); // open it if(!handle) { // ooops errorCode = USB_ERROR_ACCESS; fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror()); continue; } if(vendorName == NULL && productName == NULL) { /* name does not matter */ break; } /* now check whether the names match: */ len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); if(len < 0) { errorCode = USB_ERROR_IO; fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror()); } else { errorCode = USB_ERROR_NOTFOUND; /* fprintf(stderr, "seen device from vendor ->%s<-\n", string); */ if(strcmp(string, vendorName) == 0) { len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); if(len < 0) { errorCode = USB_ERROR_IO; fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror()); } else { errorCode = USB_ERROR_NOTFOUND; /* fprintf(stderr, "seen product ->%s<-\n", string); */ if(strcmp(string, productName) == 0) break; } } } usb_close(handle); handle = NULL; } } if(handle) break; } if(handle != NULL) { errorCode = 0; *device = handle; } return errorCode; }
int usbOpenDevice(usb_dev_handle **device, int vendorID, char *vendorNamePattern, int productID, char *productNamePattern, char *serialNamePattern, FILE *printMatchingDevicesFp, FILE *warningsFp) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *handle = NULL; int errorCode = USBOPEN_ERR_NOTFOUND; usb_find_busses(); usb_find_devices(); for(bus = usb_get_busses(); bus; bus = bus->next) { for(dev = bus->devices; dev; dev = dev->next) { if((vendorID == 0 || dev->descriptor.idVendor == vendorID) && (productID == 0 || dev->descriptor.idProduct == productID)) { char vendor[256], product[256], serial[256]; int len; handle = usb_open(dev); if(!handle) { errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) { fprintf(warningsFp, "Warning: cannot open VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } continue; } len = vendor[0] = 0; if(dev->descriptor.iManufacturer > 0) { len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, vendor, sizeof(vendor)); } if(len < 0) { errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) { fprintf(warningsFp, "Warning: cannot query manufacturer for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } } else { errorCode = USBOPEN_ERR_NOTFOUND; if(shellStyleMatch(vendor, vendorNamePattern)) { len = product[0] = 0; if(dev->descriptor.iProduct > 0) { len = usbGetStringAscii(handle, dev->descriptor.iProduct, product, sizeof(product)); } if(len < 0) { errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query product for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } else { errorCode = USBOPEN_ERR_NOTFOUND; if(shellStyleMatch(product, productNamePattern)) { len = serial[0] = 0; if(dev->descriptor.iSerialNumber > 0) { len = usbGetStringAscii(handle, dev->descriptor.iSerialNumber, serial, sizeof(serial)); } if(len < 0) { errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query serial for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } if(shellStyleMatch(serial, serialNamePattern)) { if(printMatchingDevicesFp != NULL) { if(serial[0] == 0) { fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product); } else { fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\" serial=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product, serial); } } else { break; } } } } } } usb_close(handle); handle = NULL; } } if(handle) { break; } } if(handle != NULL) { errorCode = 0; *device = handle; } if(printMatchingDevicesFp != NULL) { errorCode = 0; } return errorCode; }
int usbOpenDevice(usbDevice_t **device, int vendor, char *vendorName, int product, char *productName, int _usesReportIDs) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *handle = NULL; int errorCode = USB_ERROR_NOTFOUND; static int didUsbInit = 0; if(!didUsbInit){ usb_init(); didUsbInit = 1; } usb_find_busses(); usb_find_devices(); for(bus=usb_get_busses(); bus; bus=bus->next){ for(dev=bus->devices; dev; dev=dev->next){ if(dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product){ char string[256]; int len; handle = usb_open(dev); /* we need to open the device in order to query strings */ if(!handle){ errorCode = USB_ERROR_ACCESS; fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror()); continue; } if(vendorName == NULL && productName == NULL){ /* name does not matter */ break; } /* now check whether the names match: */ len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); if(len < 0){ errorCode = USB_ERROR_IO; fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror()); }else{ errorCode = USB_ERROR_NOTFOUND; /* fprintf(stderr, "seen device from vendor ->%s<-\n", string); */ /* check product name , the is no vendor name*/ len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); if(len < 0){ errorCode = USB_ERROR_IO; fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror()); }else{ errorCode = USB_ERROR_NOTFOUND; // fprintf(stderr, "seen product ->%s<-\n", string); if(strcmp(string, productName) == 0) break; } } usb_close(handle); handle = NULL; } } if(handle) break; } if(handle != NULL){ int rval, retries = 3; if(usb_set_configuration(handle, 1)){ fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror()); } /* now try to claim the interface and detach the kernel HID driver on * linux and other operating systems which support the call. */ while((rval = usb_claim_interface(handle, 0)) != 0 && retries-- > 0){ #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP if(usb_detach_kernel_driver_np(handle, 0) < 0){ fprintf(stderr, "Warning: could not detach kernel HID driver: %s\n", usb_strerror()); } #endif } #ifndef __APPLE__ if(rval != 0) fprintf(stderr, "Warning: could not claim interface\n"); #endif /* Continue anyway, even if we could not claim the interface. Control transfers * should still work. */ errorCode = 0; *device = handle; usesReportIDs = _usesReportIDs; } return errorCode; }
void find_device(t_udmx *x) { usb_dev_handle *handle = NULL; struct usb_bus *bus; struct usb_device *dev; t_int16 device_count; usb_find_busses(); device_count = usb_find_devices(); if (device_count == x->usb_devices_seen) { return; } x->usb_devices_seen = device_count; for(bus=usb_busses; bus; bus=bus->next){ for(dev=bus->devices; dev; dev=dev->next){ if(isOurVIDandPID(dev)){ char string[256]; int len; handle = usb_open(dev); /* we need to open the device in order to query strings */ if(!handle){ error ("Warning: cannot open USB device: %s", usb_strerror()); continue; } /* now find out whether the device actually is obdev's Remote Sensor: */ len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); if(len < 0){ error("udmx: warning: cannot query manufacturer for device: %s", usb_strerror()); goto skipDevice; } /* post("udmx: seen device from vendor ->%s<-", string); */ if(strcmp(string, "www.anyma.ch") != 0) goto skipDevice; len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); if(len < 0){ error("udmx: warning: cannot query product for device: %s", usb_strerror()); goto skipDevice; } // post("udmx: seen product ->%s<-", string); if(strcmp(string, "udmx") == 0 || strcmp(string, "uDMX") == 0) { // we've found a udmx device. get serial number if (dev->descriptor.iSerialNumber) { len = usb_get_string_simple(handle, dev->descriptor.iSerialNumber, x->serial_number, sizeof (x->serial_number)); if (len == 0) { error("Unable to fetch serial number string"); } } //see if we're looking for a specific serial number if (x->bind_to[0]) { if (strcmp(x->bind_to,x->serial_number)) { post("Found device for another serial number: %s\n", x->serial_number); goto skipDevice; } } break; } skipDevice: usb_close(handle); handle = NULL; } } if(handle) break; } if(!handle){ udmx_message(x,gensym("Could not find USB device www.anyma.ch/udmx")); #ifdef PUREDATA // compiling for PUREDATA outlet_float(x->statusOutlet,0); #else // compiling for MaxMSP outlet_int(x->statusOutlet,0); #endif // Max/PD switch x->dev_handle = NULL; } else { x->dev_handle = handle; #ifdef PUREDATA // compiling for PUREDATA outlet_float(x->statusOutlet,1); #else // compiling for MaxMSP outlet_int(x->statusOutlet,1); #endif // Max/PD switch udmx_message(x,gensym("Found USB device www.anyma.ch/udmx")); } }
int usbOpenDevice(usb_dev_handle **device, int vendorID, char *vendorNamePattern, int productID, char *productNamePattern, char *serialNamePattern, FILE *printMatchingDevicesFp, FILE *warningsFp) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *handle = NULL; int errorCode = USBOPEN_ERR_NOTFOUND; usb_find_busses(); usb_find_devices(); for(bus = usb_get_busses(); bus; bus = bus->next){ for(dev = bus->devices; dev; dev = dev->next){ /* последовательный опрос всех устройств на всех шинах */ if((vendorID == 0 || dev->descriptor.idVendor == vendorID) && (productID == 0 || dev->descriptor.idProduct == productID)){ char vendor[256], product[256], serial[256]; int len; handle = usb_open(dev); /* Нам нужно открыть устройство в соответсвии строкам запроса */ if(!handle){ errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) fprintf(warningsFp, "Warning: cannot open VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); continue; } /* теперь проверяем имена на совпадение: */ len = vendor[0] = 0; if(dev->descriptor.iManufacturer > 0){ len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, vendor, sizeof(vendor)); } if(len < 0){ errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query manufacturer for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); }else{ errorCode = USBOPEN_ERR_NOTFOUND; /* printf("seen device from vendor ->%s<-\n", vendor); */ if(shellStyleMatch(vendor, vendorNamePattern)){ len = product[0] = 0; if(dev->descriptor.iProduct > 0){ len = usbGetStringAscii(handle, dev->descriptor.iProduct, product, sizeof(product)); } if(len < 0){ errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query product for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); }else{ errorCode = USBOPEN_ERR_NOTFOUND; /* printf("seen product ->%s<-\n", product); */ if(shellStyleMatch(product, productNamePattern)){ len = serial[0] = 0; if(dev->descriptor.iSerialNumber > 0){ len = usbGetStringAscii(handle, dev->descriptor.iSerialNumber, serial, sizeof(serial)); } if(len < 0){ errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query serial for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } if(shellStyleMatch(serial, serialNamePattern)){ if(printMatchingDevicesFp != NULL){ if(serial[0] == 0){ fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product); }else{ fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\" serial=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product, serial); } }else{ break; } } } } } } usb_close(handle); handle = NULL; } } if(handle) /* мы нашли устройство */ break; } if(handle != NULL){ errorCode = 0; *device = handle; } if(printMatchingDevicesFp != NULL) /* не возвращать ошибку только для листинга */ errorCode = 0; return errorCode; }
static int usbOpenDevice(usb_dev_handle ** device, int vendor, char *vendorName, int product, char *productName, int upperversion, int lowerversion) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *handle = NULL; int errorCode = USB_ERROR_NOTFOUND; int major, minor; static int didUsbInit = 0; if (!didUsbInit) { didUsbInit = 1; usb_init(); } usb_find_busses(); usb_find_devices(); for (bus = usb_get_busses(); bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product) { char string[256]; int len; handle = usb_open(dev); /* we need to open the device in order to query strings */ if (!handle) { errorCode = USB_ERROR_ACCESS; fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror()); continue; } if (vendorName == NULL && productName == NULL) { /* name does not matter */ break; } /* now check whether the names match: */ len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); if (len < 0) { errorCode = USB_ERROR_IO; fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror()); } else { errorCode = USB_ERROR_NOTFOUND; /* fprintf(stderr, "seen device from vendor ->%s<-\n", string); */ if (strcmp(string, vendorName) == 0) { len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); if (len < 0) { errorCode = USB_ERROR_IO; fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror()); } else { errorCode = USB_ERROR_NOTFOUND; /* fprintf(stderr, "seen product ->%s<-\n", string); */ if (strcmp(string, productName) == 0) break; } } } usb_close(handle); handle = NULL; } } if (handle) break; } if (handle != NULL) { errorCode = 0; *device = handle; minor = dev->descriptor.bcdDevice & 0xff; major = dev->descriptor.bcdDevice >> 8; if (major != USBDEV_VERSION_MAJOR) { fprintf(stderr, "Error: Firmware and host software version are not compatible\n" "Firmware: %2d.%02d\n Host: %2d.%02d\n" "Please upgrade the firmware or downgrade the CLI\n", major, minor, USBDEV_VERSION_MAJOR, USBDEV_VERSION_MINOR); errorCode = -1; } else if (minor != USBDEV_VERSION_MINOR) { fprintf(stderr, "Note: Firmware and host software version may be incompatible\n" "Firmware: %2d.%02d\n Host: %2d.%02d\n" "Please upgrade the firmware or downgrade the CLI\n", major, minor, USBDEV_VERSION_MAJOR, USBDEV_VERSION_MINOR); } }
int gnubl(void) { char string[256]; struct usb_bus *bus; struct usb_device *dev; int nBytes; int counter = 0; int doStartBootloader = 0; usb_dev_handle *handle = 0; usb_init(); usb_find_busses(); usb_find_devices(); for(bus=usb_busses; bus; bus=bus->next){ for(dev=bus->devices; dev; dev=dev->next){ if(gnubl_isOurVIDandPID(dev)){ int len; handle = usb_open(dev); /* we need to open the device in order to query strings */ if(!handle){ fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror()); continue; } /* now find out whether the device actually is obdev's Remote Sensor: */ len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); if(len < 0){ fprintf(stderr, "warning: cannot query manufacturer for device: %s\n", usb_strerror()); } /* fprintf(stderr, "seen device from vendor ->%s<-\n", string); */ if(strcmp(string, "www.anyma.ch") == 0) { len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); if(len < 0){ fprintf(stderr, "warning: cannot query product for device: %s\n", usb_strerror()); } else { doStartBootloader = 1; if (doStartBootloader) { nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, GNUSB_CMD_START_BOOTLOADER, 0, 0, NULL, 0, 5000); printf("--> Started bootloader on device \"%s\"\n", string); counter++; sleep(1); } } } usb_close(handle); handle = NULL; } } } if (!counter) { //printf ("\nNo devices found with manufacturer \"anyma.ch\".\n\n"); } else { sleep(1); // give some more time for USBasp to appear } printf("--> Uploading\n"); return 0; }
unsigned char usbOpenDevice(usb_dev_handle **device, int vendor, char *vendorName, int product, char *productName, char *usbSerialID) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *handle = NULL; unsigned char errorCode = USB_ERROR_NOTFOUND; static int didUsbInit = 0; if(!didUsbInit){ didUsbInit = 1; usb_init(); } usb_find_busses(); usb_find_devices(); for(bus=usb_get_busses(); bus; bus=bus->next){ for(dev=bus->devices; dev; dev=dev->next){ if(dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product){ char string[256]; int len; handle = usb_open(dev); /* we need to open the device in order to query strings */ if(!handle){ errorCode = USB_ERROR_ACCESS; fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror()); continue; } if(vendorName == NULL && productName == NULL){ /* name does not matter */ break; } /* now check whether the names match: */ len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); if(len < 0){ errorCode = USB_ERROR_IO; fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror()); }else{ errorCode = USB_ERROR_NOTFOUND; //fprintf(stderr, "seen device from vendor ->%s<-\n", string); if(strcmp(string, vendorName) == 0){ len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); if(len < 0){ errorCode = USB_ERROR_IO; fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror()); }else{ errorCode = USB_ERROR_NOTFOUND; //fprintf(stderr, "seen product ->%s<-\n", string); if(strcmp(string, productName) == 0) { len = usbGetStringAscii(handle, dev->descriptor.iSerialNumber, 0x0409, serialNumberString, sizeof(serialNumberString)); if (len < 0) { errorCode = USB_ERROR_IO; fprintf(stderr, "Warning: cannot query serial number for device: %s\n", usb_strerror()); }else{ errorCode = USB_ERROR_NOTFOUND; if ((usbSerialID == NULL) || (strcmp(serialNumberString, usbSerialID) == 0)) { break; } } } } } } usb_close(handle); handle = NULL; } } if(handle) break; } if(handle != NULL){ errorCode = USB_SUCCESS; *device = handle; } return errorCode; }
/* try to find a xu1541 cable */ int xu1541_init(void) { struct usb_bus *bus; struct usb_device *dev; unsigned char ret[4]; int len; xu1541_dbg(0, "Scanning usb ..."); usb.init(); usb.find_busses(); usb.find_devices(); /* usb_find_devices sets errno if some devices don't reply 100% correct. */ /* make lib ignore this as this has nothing to do with our device */ errno = 0; for(bus = usb.get_busses(); !xu1541_handle && bus; bus = bus->next) { xu1541_dbg(1, "Scanning bus %s", bus->dirname); for(dev = bus->devices; !xu1541_handle && dev; dev = dev->next) { xu1541_dbg(1, "Device %04x:%04x at %s", dev->descriptor.idVendor, dev->descriptor.idProduct, dev->filename); if((dev->descriptor.idVendor == XU1541_VID) && (dev->descriptor.idProduct == XU1541_PID)) { char string[256]; int len; xu1541_dbg(0, "Found xu1541 device on bus %s device %s.", bus->dirname, dev->filename); /* open device */ if(!(xu1541_handle = usb.open(dev))) fprintf(stderr, "Error: Cannot open USB device: %s\n", usb.strerror()); /* get device name and make sure the name is "xu1541" meaning */ /* that the device is not in boot loader mode */ len = usbGetStringAscii(xu1541_handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); if(len < 0){ fprintf(stderr, "warning: cannot query product " "name for device: %s\n", usb.strerror()); if(xu1541_handle) usb.close(xu1541_handle); xu1541_handle = NULL; } /* make sure the name matches what we expect */ if(strcmp(string, "xu1541") != 0) { fprintf(stderr, "Error: Found xu1541 in unexpected state," " please make sure device is _not_ in bootloader mode!\n"); if(xu1541_handle) usb.close(xu1541_handle); xu1541_handle = NULL; } } } } if(!xu1541_handle) { fprintf(stderr, "ERROR: No xu1541 device found\n"); return -1; } if (usb.set_configuration(xu1541_handle, 1) != 0) { fprintf(stderr, "USB error: %s\n", usb.strerror()); return -1; } /* Get exclusive access to interface 0. */ if (usb.claim_interface(xu1541_handle, 0) != 0) { fprintf(stderr, "USB error: %s\n", usb.strerror()); return -1; } /* check the devices version number as firmware x.06 changed everything */ len = usb.control_msg(xu1541_handle, USB_TYPE_CLASS | USB_ENDPOINT_IN, XU1541_INFO, 0, 0, (char*)ret, sizeof(ret), 1000); if(len < 0) { fprintf(stderr, "USB request for XU1541 info failed: %s!\n", usb.strerror()); return -1; } if(len != sizeof(ret)) { fprintf(stderr, "Unexpected number of bytes (%d) returned\n", len); return -1; } xu1541_dbg(0, "firmware version %x.%02x", ret[0], ret[1]); if(ret[1] < 8) { fprintf(stderr, "Device reports firmware version %x.%02x\n", ret[0], ret[1]); fprintf(stderr, "but this version of opencbm requires at least " "version x.08\n"); return -1; } return 0; }