USB device for mac

来源:互联网 发布:php 记事本乱码 编辑:程序博客网 时间:2024/05/19 18:10
  1. 一.获得 I/O Kit 主端口
  2. 1>建立一个信号句柄,让我们从命令行中断时候可以清理,否则,这个runloop永远循环运行。
  3.     sig_t           oldHandler;
  4.     oldHandler = signal(SIGINT, SignalHandler);
  5.     if (oldHandler == SIG_ERR)
  6.     printf("Could not establish new signal handler");
  7. 创建一个端口
  8.     mach_port_t myMasterPort;
  9.     kern_return_t result;
  10.     result = IOMasterPort(MACH_PORT_NULL, &myMasterPort);
  11.     //也可以
  12.     /*
  13.         IOServiceGetMatchingServices(myMasterPort, myMatchingDictionary,
  14.                             &myIterator);
  15.         IOServiceGetMatchingServices(kIOMasterPortDefault, myMatchingDictionary,
  16.                             &myIterator);
  17.     */
  18. 创建一个USB 设备dictionary 使用usbclass
  19.     CFMutableDictionaryRef  matchingDict;
  20.     // Set up the matching criteria for the devices we're interested in
  21.     atchingDict = IOServiceMatching(kIOUSBDeviceClassName);
  22.     if (!matchingDict)
  23.     {
  24.         mach_port_deallocate(mach_task_self(), masterPort);
  25.         return -1;
  26.     }
  27. 2>.设置设备的字典(dictionary)对象和寻找设备(find devices)在字典里面添加我们设备的信息如productID,vendorID和bcdDevice(key and value)等.我们能够指定我们的设备信息。如:
  28.     // Add our vendor and product IDs to the matching criteria 
  29.     CFDictionarySetValue(
  30.             matchingDict,
  31.             CFSTR(kUSBVendorID),
  32.             CFNumberCreate(kCFAllocatorDefaul,kCFNumberSInt32Type, &usbVendor)); 
  33.     CFDictionarySetValue(
  34.             matchingDict,
  35.             CFSTR(kUSBProductID),
  36.             CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt32Type,&usbProduct)); 
  37. //还有很多key这些都是usbDevice 标准的属性
  38. 在IOKitKeys.h 文件中还有很多。可以参考
  39. // Keys for matching IOService properties by name
  40. #define kIOProviderClassKey "IOProviderClass"
  41. #define kIONameMatchKey "IONameMatch"
  42. #define kIOPropertyMatchKey "IOPropertyMatch"
  43. #define kIOPathMatchKey "IOPathMatch"
  44. #define kIOLocationMatchKey "IOLocationMatch"
  45. #define kIOResourceMatchKey "IOResourceMatch"
  46. 也可以通过  I/O Registry Explorer 查看设备的信息。
  47.     通过调用函数创建包含设备类名的属性字典(dictionary)
  48. CFMutableDictionaryRef myUSBMatchDictionary=NULL;
  49. myUSBMatchDictionary= IOServiceMatching(kIOUSBDeviceClassName);
  50.     通过函数创建一个包含设备名字的属性字典
  51. CFMutableDictionaryRef myCompanyDeviceMatchDictionary = NULL;
  52. myCompanyDeviceMatchDictionary = IOServiceNameMatching("MyCompany");
  53.     通过函数创建一个包含设备文件名的属于字典
  54. CFMutableDictionaryRef myBSDMatchDictionary=NULL;
  55. myBSDMatchDictionary = IOBSDNameMatching(kIOMasterPortDefault, 0,  "disk1s8");
  56. 第三个参数不能是路径.
  57.     字典在不使用之后要释放。 CFRelease(matchDic);
  58.     对生成的字典你可以修改,删除,修改,增加key and value;构建你想要的设备属性字典
  59. 二.当设备到达和离去的时候
  60.    使用 IONotificationPortCreate 函数创建notification 对象能监听 I/O Kit notifications的通知消息.
  61. IONotificationPortRef notificationObject;
  62. mach_port_t masterPort; 
  63. notificationObject = IONotificationPortCreate(masterPort);
  64. 创建run-loop
  65. CFRunLoopSourceRef notificationRunLoopSource;
  66. //Use the notification object received from IONotificationPortCreate  
  67. notificationRunLoopSource=IONotificationPortGetRunLoopSource(notificationObject);
  68. //增加run-loop
  69. CFRunLoopAddSource(CFRunLoopGetCurrent(),notificationRunLoopSource,kCFRunLoopDefaultMode);
  70. io_iterator_t  gAddedIter //获得设备指针列表-关键                           
  71. // Now set up a notification to be called when a device is first matched by I/O Kit.
  72. kern_return_t kr = IOServiceAddMatchingNotification(
  73. gNotifyPort,     // notifyPort
  74.                         kIOFirstMatchNotification, // notificationType
  75.                        matchingDict,           // matching
  76.                        DeviceAdded,            // callback 函数
  77.                        NULL,       // refCon
  78.                        &gAddedIter // notification
  79.                        );