IOKit设备添加/删除通知 - 只触发一次? [英] IOKit device adding/removal notifications - only fire once?
问题描述
我一直在尝试在添加或删除特定USB设备时收到通知。我已经阅读从应用程序访问硬件文档,并有一个裸体演示应用程序,主要基于该文档中提供的代码。
I have been trying to get notified when a specific USB device is added or removed. I have read 'Accessing Hardware From Applications' document and have a bare-bones demo application, mainly based on the code provided in that document.
它第一次工作a设备被添加或删除,但之后,我的回调从未被调用。我不能解决为什么?任何人都可以找到我错误的地方?
It works the first time a device is added or removed, but after that my callbacks never get called. I cannot work out why? Can anyone spot where I am going wrong?
(xcode项目,如果你想测试)
http://monkeyfood.com/testIOKitNOtificaiton.zip
(xcode project if you would like to test) http://monkeyfood.com/testIOKitNOtificaiton.zip
感谢。
//
// AppDelegate.m
// testIOKitNotification
//
// Created by Diggory Laycock on 23/07/2012.
// Copyright (c) 2012 MonkeyFood.com. All rights reserved.
//
#import "AppDelegate.h"
@implementation AppDelegate
// Arduino USB info
#define matchVendorID 0x2341
#define matchProductID 0x0043
#pragma mark -
#pragma mark C Callback functions
#pragma mark -
void usbDeviceAppeared(void *refCon, io_iterator_t iterator){
NSLog(@"Matching USB device appeared");
}
void usbDeviceDisappeared(void *refCon, io_iterator_t iterator){
NSLog(@"Matching USB device disappeared");
}
@synthesize window = _window;
#pragma mark -
#pragma mark Application Methods
#pragma mark -
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
io_iterator_t newDevicesIterator;
io_iterator_t lostDevicesIterator;
newDevicesIterator = 0;
lostDevicesIterator = 0;
NSLog(@" ");
NSMutableDictionary *matchingDict = (__bridge NSMutableDictionary *)IOServiceMatching(kIOUSBDeviceClassName);
if (matchingDict == nil){
NSLog(@"Could not create matching dictionary");
return;
}
[matchingDict setObject:[NSNumber numberWithShort:matchVendorID] forKey:(NSString *)CFSTR(kUSBVendorID)];
[matchingDict setObject:[NSNumber numberWithShort:matchProductID] forKey:(NSString *)CFSTR(kUSBProductID)];
// Add notification ports to runloop
IONotificationPortRef notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
CFRunLoopSourceRef notificationRunLoopSource = IONotificationPortGetRunLoopSource(notificationPort);
CFRunLoopAddSource([[NSRunLoop currentRunLoop] getCFRunLoop], notificationRunLoopSource, kCFRunLoopDefaultMode);
kern_return_t err;
err = IOServiceAddMatchingNotification(notificationPort,
kIOMatchedNotification,
(__bridge CFDictionaryRef)matchingDict,
usbDeviceAppeared,
(__bridge void *)self,
&newDevicesIterator);
if (err)
{
NSLog(@"error adding publish notification");
}
[self matchingDevicesAdded: newDevicesIterator];
NSMutableDictionary *matchingDictRemoved = (__bridge NSMutableDictionary *)IOServiceMatching(kIOUSBDeviceClassName);
if (matchingDictRemoved == nil){
NSLog(@"Could not create matching dictionary");
return;
}
[matchingDictRemoved setObject:[NSNumber numberWithShort:matchVendorID] forKey:(NSString *)CFSTR(kUSBVendorID)];
[matchingDictRemoved setObject:[NSNumber numberWithShort:matchProductID] forKey:(NSString *)CFSTR(kUSBProductID)];
err = IOServiceAddMatchingNotification(notificationPort,
kIOTerminatedNotification,
(__bridge CFDictionaryRef)matchingDictRemoved,
usbDeviceDisappeared,
(__bridge void *)self,
&lostDevicesIterator);
if (err)
{
NSLog(@"error adding removed notification");
}
[self matchingDevicesRemoved: lostDevicesIterator];
// CFRunLoopRun();
// [[NSRunLoop currentRunLoop] run];
}
#pragma mark -
#pragma mark ObjC Callback functions
#pragma mark -
- (void)matchingDevicesAdded:(io_iterator_t)devices
{
io_object_t thisObject;
while ( (thisObject = IOIteratorNext(devices))) {
NSLog(@"new Matching device added ");
IOObjectRelease(thisObject);
}
}
- (void)matchingDevicesRemoved:(io_iterator_t)devices
{
io_object_t thisObject;
while ( (thisObject = IOIteratorNext(devices))) {
NSLog(@"A matching device was removed ");
IOObjectRelease(thisObject);
}
}
@end
推荐答案
我已经弄清楚了什么是错的 - 我没有做任何事情在C Callback的迭代器。一个愚蠢的错误!
I've worked out what was wrong - I didn't do anything with the iterator in the C Callback. A stupid mistake!
这篇关于IOKit设备添加/删除通知 - 只触发一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!