int main (int argc, const char * argv[]) { kern_return_t err; CFMutableDictionaryRef matchingDictionary = 0; // requires <IOKit/IOKitLib.h> SInt32 idVendor = 1351; SInt32 idProduct = 8193; CFNumberRef numberRef; io_iterator_t iterator = 0; io_service_t usbDeviceRef; err = IOMasterPort(MACH_PORT_NULL, &masterPort); if (err) { printf("USBSimpleExample: could not create master port, err = %08x\n", err); return err; } matchingDictionary = IOServiceMatching(kIOUSBDeviceClassName); // requires <IOKit/usb/IOUSBLib.h> if (!matchingDictionary) { printf("USBSimpleExample: could not create matching dictionary\n"); return -1; } numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &idVendor); if (!numberRef) { printf("USBSimpleExample: could not create CFNumberRef for vendor\n"); return -1; } CFDictionaryAddValue(matchingDictionary, CFSTR(kUSBVendorID), numberRef); CFRelease(numberRef); numberRef = 0; numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &idProduct); if (!numberRef) { printf("USBSimpleExample: could not create CFNumberRef for product\n"); return -1; } CFDictionaryAddValue(matchingDictionary, CFSTR(kUSBProductID), numberRef); CFRelease(numberRef); numberRef = 0; err = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator); matchingDictionary = 0; // this was consumed by the above call while ( (usbDeviceRef = IOIteratorNext(iterator)) ) { printf("Found device %p\n", (void*)usbDeviceRef); dealWithDevice(usbDeviceRef); IOObjectRelease(usbDeviceRef); // no longer need this reference } IOObjectRelease(iterator); iterator = 0; mach_port_deallocate(mach_task_self(), masterPort); return 0; }
int main (int argc, const char * argv[]) { int idVendor = 0x8f7; int idProduct = 1; int USBIndex, matchcount; int i; usb_dev_handle *udev=0; struct usb_bus *bus; struct usb_device *dev, *matchdev; /* if one argument is provided, it should be an index as to _which_ USB LabPro is to be opened providing a negative index enables time stamping as is used when this is run as a robot */ if (argc==2) { USBIndex=atoi(argv[1]); if (abs(USBIndex) < 1 || abs(USBIndex) > 255) { fprintf(stderr,"Bad USB index argument provided... should be 1<=index<=255 or negative to enable binary time stamps, got: %s\n", argv[1]); fprintf(stderr,"****EXITED****\n"); return 1; } if (USBIndex < 0) { USBIndex=-USBIndex; use_time_stamps=1; } USBIndex -=1; } else USBIndex=0; setbuf(stdout, 0); setbuf(stderr,0); usb_init(); #ifdef DEBUG usb_set_debug(DEBUG); #else usb_set_debug(0); #endif #ifdef DEBUG fprintf(stderr, "inited libusb\n"); fflush(0); #endif usb_find_busses(); #ifdef DEBUG fprintf(stderr, "found busses in libusb\n"); fflush(0); #endif usb_find_devices(); #ifdef DEBUG fprintf(stderr, "found devices in libusb\n"); fflush(0); #endif signal(SIGHUP, handle_signal); signal(SIGINT, handle_signal); signal(SIGQUIT, handle_signal); signal(SIGTERM, handle_signal); signal(SIGPIPE, handle_signal); matchcount=-1; for (bus = usb_get_busses(); bus && matchcount<USBIndex; bus = bus->next) { for (dev = bus->devices; dev && matchcount<USBIndex; dev = dev->next) { if(dev->descriptor.idVendor==idVendor && dev->descriptor.idProduct==idProduct) { matchcount++; matchdev=dev; } } } if(matchcount==USBIndex) { udev = usb_open(matchdev); if(udev) { fprintf(stderr, "Found device %p\n", (void*)udev); fflush(0); global_intf=udev; /* docs for 0.1.12 say usb_reset may cause re-enumeration, so don't do it here! */ /* usb_reset(udev); usb_reset(udev); */ /* make sure it's OK at the start */ dealWithDevice(udev); global_intf=0; /* don't need resets any more */ usb_reset(udev); usb_close(udev); } else { fprintf(stderr, "Found but couldn't open device %d... probably already open\n", USBIndex+1); } } else fprintf(stderr,"No LabPro Found at index %d\n", USBIndex+1); fprintf(stderr,"****EXITED****\n"); return 0; }