USB device for mac
2012-09-28 16:43
183 查看
一.获得 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
);
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
);
相关文章推荐
- Programming Guide for Linux USB Device Drivers
- mac osx terminal 报错:nohup: can't detach from console: Inappropriate ioctl for device
- VMware出现The connection for the USB device "###" was unsuccessful - Driver error原因
- Mac Linux USB Loade for Mac(U盘运行Linux)免注册版 v3.4.1最新破解版
- Enable Bootcamp to install Windows from usb for MAC
- Driver for device rausb0 has been compiled with version 22
- MediaKit reports not enough space on device for requested operation.Mac 格式化移动硬盘为Mac和Windows可用 error
- tenda u1 usb wireless device install driver for kali linux kernal Debian 4.3.3-7kali2 (2016-01-27) x86_64 GNU/Linux
- Driver for device rausb0 has been compiled with version 22
- mac 格式化移动硬盘 MediaKit reports not enough space on device for requested operation.
- configuration android USB device for Ubuntu
- Device Object Example for a USB Mass Storage Device
- Ubuntu下手机USB 显示??? insufficient permissions for device
- How to enable USB selective suspend and system wake in the UMDF driver for a USB device
- 今天在linux下连接平板usb,试用adb shell时出现error: insufficient permissions for device, 而且我们输入adb devices显示: xx
- Android USB vendor IDs for USB device on Linux
- mac 格式化移动硬盘 MediaKit reports not enough space on device for requested operation.
- 一个for mac os x 的 usb无线驱动的开发流程
- USBclean for Mac (U盘病毒查杀工具)激活版 v3.0破解版
- USB Device Descriptor for AMP Controller