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. );

USB device for mac相关推荐

  1. Vmware提示:host usb device connections disabled-(vmware 主机已禁用 usb 设备连接)

    Vmware提示:host usb device connections disabled-(vmware 主机已禁用 usb 设备连接) VMware Workstation,提示提示Host US ...

  2. USB查看器 USB Device Tree Viewer(UsbTreeView.exe)的使用(重启Intel Realsense摄像头)

    文章目录 简介 打开后界面 测试U盘弹出后是否能找回 测试挽回掉线的摄像头 简介 USB Device Tree Viewer是一个非常实用的USB设备查看器,它可以发现所有的usb接口的使用情况,并 ...

  3. StackOverflow How to programmatically unplug replug an arbitrary USB device? 如何以编程方式拔出并重新插入任意USB设备

    文章目录 方法1:通过devcon工具重启usb hub 像这种情况,明明插了六个摄像头,它偏偏掉一个... 刚好,我们可以测试How to programmatically unplug & ...

  4. USB device如何进入suspend模式

    1. 当没有使能usb device(usb_conf DEVEN没有置1),device处于L3状态 2. 当使能了usb device,但是没有连接到host,device处于L2(suspend ...

  5. S3C2440 WINCE6将USB DEVICE改成USB HOST,实现两个USB HOST

    S3C2440一般默认的是一个USB DEVICE,一个USB HOST,即一个主口,一个从口,先来看看USB Device与USB Host相关知识. USB Host: 最底层就是USB Host ...

  6. STM32驱动开发(二)--USB Device RNDIS虚拟网卡(USB2.0 基础概念讲解)

    STM32驱动开发(二)–USB Device RNDIS虚拟网卡(USB2.0基础概念讲解) 一.简介   本文基于stm32 Rndis实例,github开源, 使用STM32F407单板.结合协 ...

  7. Start booting from USB device boot failed 解决办法(老机器问题)

    Start booting from USB device boot failed 解决办法 问题: 一般情况下,我们设置电脑BIOS USB 第一启动方式最重要的两步: 1.Removable De ...

  8. U盘做为系统盘安装系统,出现start booting from usb device和boot failed解决方案

    U盘做为系统盘安装系统,出现start booting from usb device和boot failed解决方案 参考文章: (1)U盘做为系统盘安装系统,出现start booting fro ...

  9. 痞子衡嵌入式:可通过USB Device Path来唯一指定i.MXRT设备进行ROM/Flashloader通信

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是通过USB Device Path来唯一指定i.MXRT设备进行ROM/Flashloader通信. i.MXRT系列高性能微控制器从2 ...

最新文章

  1. bldc不同载波频率_广播百科 频率调制
  2. ARM 寄存器 和 工作模式了解
  3. 数据结构和算法分析:第二章 算法分析
  4. 大志非才不就,大才非学不成—博文资源汇总
  5. 爬取网页的通用代码框架
  6. 5g的负面影响_设计系统的实施是否会对早期概念产生负面影响?
  7. 使用 Roslyn 编译器服务
  8. Flink-Sink_将结果输出到Kafka_Redis_ES_Mysql中
  9. mitmproxy抓包 | Python疑难测试场景mock
  10. gulp通过http-proxy-middleware开启反向代理,实现跨域
  11. java 两个线程同步_Java 多线程(二)—— 线程的同步
  12. 同步工具类CyclicBarrier原理及使用
  13. 【点阵液晶编程连载一】写在前面
  14. qt 处理oracle事务,qt调用oracle存储过程,该怎么处理(2)
  15. UDS协议(史上最全)
  16. my games / BF3 / GTA5 / NFS18 / sanguowushuang6 / RA2 / KOF97 / FIFA
  17. “Flash闪存”基础 及 “SD NAND Flash”产品的测试
  18. php 降低采样率,讨论采样频率、采样深度(位深)、音量调节对音质的影响
  19. 安卓文件管理神器--X-plore
  20. 小米手机短信拦截转发失败原因

热门文章

  1. ATL SERVER
  2. HDCTF2023 Writeup
  3. 计算机开机后黑屏 只有鼠标,电脑黑屏只有鼠标箭头怎么办?最简单的解决方法告诉你...
  4. 小红书618品牌营销蓄水阶段告一段落,5月即将进入冲刺期
  5. 阿里linux内核月报201412
  6. Vodafone 移动终端声质量评价
  7. 编译 ORB-SLAM2/3的ROS工程造成(You should double-check your ROS_PACKAGE_PATH...)
  8. python sql_pandasql:让python讲SQL
  9. ios手机 按钮点击事件没反应(按钮是js生成的)
  10. eclipse 设置全部的背景颜色