IOKit notificaciones de adición / eliminación del dispositivo: ¿solo se dispara una vez?
He estado tratando de recibir una notificación cuando se agrega o quita un dispositivo USB específico. He leído el documento 'Accediendo al hardware desde las aplicaciones' y tengo una aplicación de demostración básica, basada principalmente en el código provisto en ese documento.
Funciona la primera vez que se agrega o se quita un dispositivo, pero después de eso, nunca se llama a mis devoluciones de llamada. No puedo entender por qué? ¿Alguien puede ver dónde me equivoco?
(Proyecto Xcode si desea probar)http://monkeyfood.com/testIOKitNOtificaiton.zip
Gracias.
//
// 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