USB device for mac
- 一.获得 I/O Kit 主端口
- 1>建立一个信号句柄,让我们从命令行中断时候可以清理,否则,这个runloop永远循环运行。
- sig_t oldHandler;
- oldHandler = signal(SIGINT, SignalHandler);
- if (oldHandler == SIG_ERR)
- printf("Could not establish new signal handler");
- 创建一个端口
- mach_port_t myMasterPort;
- kern_return_t result;
- result = IOMasterPort(MACH_PORT_NULL, &myMasterPort);
- //也可以
- /*
- IOServiceGetMatchingServices(myMasterPort, myMatchingDictionary,
- &myIterator);
- IOServiceGetMatchingServices(kIOMasterPortDefault, myMatchingDictionary,
- &myIterator);
- */
- 创建一个USB 设备dictionary 使用usbclass
- CFMutableDictionaryRef matchingDict;
- // Set up the matching criteria for the devices we're interested in
- atchingDict = IOServiceMatching(kIOUSBDeviceClassName);
- if (!matchingDict)
- {
- mach_port_deallocate(mach_task_self(), masterPort);
- return -1;
- }
- 2>.设置设备的字典(dictionary)对象和寻找设备(find devices)在字典里面添加我们设备的信息如productID,vendorID和bcdDevice(key and value)等.我们能够指定我们的设备信息。如:
- // Add our vendor and product IDs to the matching criteria
- CFDictionarySetValue(
- matchingDict,
- CFSTR(kUSBVendorID),
- CFNumberCreate(kCFAllocatorDefaul,kCFNumberSInt32Type, &usbVendor));
- CFDictionarySetValue(
- matchingDict,
- CFSTR(kUSBProductID),
- CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt32Type,&usbProduct));
- //还有很多key这些都是usbDevice 标准的属性
- 在IOKitKeys.h 文件中还有很多。可以参考
- // Keys for matching IOService properties by name
- #define kIOProviderClassKey "IOProviderClass"
- #define kIONameMatchKey "IONameMatch"
- #define kIOPropertyMatchKey "IOPropertyMatch"
- #define kIOPathMatchKey "IOPathMatch"
- #define kIOLocationMatchKey "IOLocationMatch"
- #define kIOResourceMatchKey "IOResourceMatch"
- 也可以通过 I/O Registry Explorer 查看设备的信息。
- 通过调用函数创建包含设备类名的属性字典(dictionary)
- CFMutableDictionaryRef myUSBMatchDictionary=NULL;
- myUSBMatchDictionary= IOServiceMatching(kIOUSBDeviceClassName);
- 通过函数创建一个包含设备名字的属性字典
- CFMutableDictionaryRef myCompanyDeviceMatchDictionary = NULL;
- myCompanyDeviceMatchDictionary = IOServiceNameMatching("MyCompany");
- 通过函数创建一个包含设备文件名的属于字典
- CFMutableDictionaryRef myBSDMatchDictionary=NULL;
- myBSDMatchDictionary = IOBSDNameMatching(kIOMasterPortDefault, 0, "disk1s8");
- 第三个参数不能是路径.
- 字典在不使用之后要释放。 CFRelease(matchDic);
- 对生成的字典你可以修改,删除,修改,增加key and value;构建你想要的设备属性字典
- 二.当设备到达和离去的时候
- 使用 IONotificationPortCreate 函数创建notification 对象能监听 I/O Kit notifications的通知消息.
- IONotificationPortRef notificationObject;
- mach_port_t masterPort;
- notificationObject = IONotificationPortCreate(masterPort);
- 创建run-loop
- CFRunLoopSourceRef notificationRunLoopSource;
- //Use the notification object received from IONotificationPortCreate
- notificationRunLoopSource=IONotificationPortGetRunLoopSource(notificationObject);
- //增加run-loop
- CFRunLoopAddSource(CFRunLoopGetCurrent(),notificationRunLoopSource,kCFRunLoopDefaultMode);
- io_iterator_t gAddedIter //获得设备指针列表-关键
- // Now set up a notification to be called when a device is first matched by I/O Kit.
- kern_return_t kr = IOServiceAddMatchingNotification(
- gNotifyPort, // notifyPort
- kIOFirstMatchNotification, // notificationType
- matchingDict, // matching
- DeviceAdded, // callback 函数
- NULL, // refCon
- &gAddedIter // notification
- );
USB device for mac相关推荐
- Vmware提示:host usb device connections disabled-(vmware 主机已禁用 usb 设备连接)
Vmware提示:host usb device connections disabled-(vmware 主机已禁用 usb 设备连接) VMware Workstation,提示提示Host US ...
- USB查看器 USB Device Tree Viewer(UsbTreeView.exe)的使用(重启Intel Realsense摄像头)
文章目录 简介 打开后界面 测试U盘弹出后是否能找回 测试挽回掉线的摄像头 简介 USB Device Tree Viewer是一个非常实用的USB设备查看器,它可以发现所有的usb接口的使用情况,并 ...
- StackOverflow How to programmatically unplug replug an arbitrary USB device? 如何以编程方式拔出并重新插入任意USB设备
文章目录 方法1:通过devcon工具重启usb hub 像这种情况,明明插了六个摄像头,它偏偏掉一个... 刚好,我们可以测试How to programmatically unplug & ...
- USB device如何进入suspend模式
1. 当没有使能usb device(usb_conf DEVEN没有置1),device处于L3状态 2. 当使能了usb device,但是没有连接到host,device处于L2(suspend ...
- S3C2440 WINCE6将USB DEVICE改成USB HOST,实现两个USB HOST
S3C2440一般默认的是一个USB DEVICE,一个USB HOST,即一个主口,一个从口,先来看看USB Device与USB Host相关知识. USB Host: 最底层就是USB Host ...
- STM32驱动开发(二)--USB Device RNDIS虚拟网卡(USB2.0 基础概念讲解)
STM32驱动开发(二)–USB Device RNDIS虚拟网卡(USB2.0基础概念讲解) 一.简介 本文基于stm32 Rndis实例,github开源, 使用STM32F407单板.结合协 ...
- Start booting from USB device boot failed 解决办法(老机器问题)
Start booting from USB device boot failed 解决办法 问题: 一般情况下,我们设置电脑BIOS USB 第一启动方式最重要的两步: 1.Removable De ...
- U盘做为系统盘安装系统,出现start booting from usb device和boot failed解决方案
U盘做为系统盘安装系统,出现start booting from usb device和boot failed解决方案 参考文章: (1)U盘做为系统盘安装系统,出现start booting fro ...
- 痞子衡嵌入式:可通过USB Device Path来唯一指定i.MXRT设备进行ROM/Flashloader通信
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是通过USB Device Path来唯一指定i.MXRT设备进行ROM/Flashloader通信. i.MXRT系列高性能微控制器从2 ...
最新文章
- bldc不同载波频率_广播百科 频率调制
- ARM 寄存器 和 工作模式了解
- 数据结构和算法分析:第二章 算法分析
- 大志非才不就,大才非学不成—博文资源汇总
- 爬取网页的通用代码框架
- 5g的负面影响_设计系统的实施是否会对早期概念产生负面影响?
- 使用 Roslyn 编译器服务
- Flink-Sink_将结果输出到Kafka_Redis_ES_Mysql中
- mitmproxy抓包 | Python疑难测试场景mock
- gulp通过http-proxy-middleware开启反向代理,实现跨域
- java 两个线程同步_Java 多线程(二)—— 线程的同步
- 同步工具类CyclicBarrier原理及使用
- 【点阵液晶编程连载一】写在前面
- qt 处理oracle事务,qt调用oracle存储过程,该怎么处理(2)
- UDS协议(史上最全)
- my games / BF3 / GTA5 / NFS18 / sanguowushuang6 / RA2 / KOF97 / FIFA
- “Flash闪存”基础 及 “SD NAND Flash”产品的测试
- php 降低采样率,讨论采样频率、采样深度(位深)、音量调节对音质的影响
- 安卓文件管理神器--X-plore
- 小米手机短信拦截转发失败原因
热门文章
- ATL SERVER
- HDCTF2023 Writeup
- 计算机开机后黑屏 只有鼠标,电脑黑屏只有鼠标箭头怎么办?最简单的解决方法告诉你...
- 小红书618品牌营销蓄水阶段告一段落,5月即将进入冲刺期
- 阿里linux内核月报201412
- Vodafone 移动终端声质量评价
- 编译 ORB-SLAM2/3的ROS工程造成(You should double-check your ROS_PACKAGE_PATH...)
- python sql_pandasql:让python讲SQL
- ios手机 按钮点击事件没反应(按钮是js生成的)
- eclipse 设置全部的背景颜色