IOKit设备添加/删除通知 - 只触发一次?

7
我一直在尝试获取特定USB设备添加或移除的通知。我已经阅读了“从应用程序访问硬件”文档,并拥有一个基于该文档提供的代码的简单演示应用程序。
第一次添加或删除设备时它可以工作,但之后我的回调函数就再也没有被调用过了。我无法弄清楚原因在哪里?有人能发现我错在哪里吗?
(如果您想测试,则为xcode项目) 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
2个回答

2

虽然已经有一个被接受的答案,但我还是来晚了。为了解决这个问题,你需要在回调函数中迭代匹配设备的迭代器。但这并不容易,所以这里提供代码来实现这一点,因为回调函数是一个C函数,你想要将其桥接到你的Objective C方法中。

void usbDeviceAppeared(void *refCon, io_iterator_t iterator){
    NSLog(@"Matching USB device appeared");
    SerialMonitor *monitor = (__bridge SerialMonitor *)refCon;
    [monitor matchingDevicesAdded:iterator];
    [monitor reload];
}

void usbDeviceDisappeared(void *refCon, io_iterator_t iterator){
    NSLog(@"Matching USB device disappeared");
    SerialMonitor *monitor = (__bridge SerialMonitor *)refCon;
    [monitor matchingDevicesRemoved:iterator];
    [monitor reload];
}

SerialMonitor 是我的USB设备管理器类,reload是一个方法,用于执行适当的USB设备初始化(获取特征报告和交换数据)。


1
我已经找出了问题所在 - 我在 C 回调函数中没有对迭代器进行任何操作。这是一个愚蠢的错误!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接