IOKit notifications

  • I apologize for the other two empty body messages with the same
    title, Im not sure what was happening with my mail client.

    Hi, Im trying to receive notifications of usb device connection and
    termination from within my cocoa apps.  Apples CF based example code
    works fine, but when I converted their code to work in my
    application, i never receive any notifications.  The relevent code is
    provided below

    - (void) awakeFromNib
    {
    kern_return_t  kr;

    NSMutableDictionary* matchingDict = IOServiceMatching
    (kIOUSBDeviceClassName);
    NSNumber* usbVendor = [NSNumber numberWithInt:1027];
    NSNumber* usbProduct = [NSNumber numberWithInt:24592];
    [matchingDict takeValue:usbVendor forKey:@"kUSBVendorID"];
    [matchingDict takeValue:usbProduct forKey:@"kUSBProductID"];

    notifyPort = IONotificationPortCreate(kIOMasterPortDefault);
    CFRunLoopSourceRef  runLoopSource;
    runLoopSource = IONotificationPortGetRunLoopSource(notifyPort);
    NSRunLoop* currentRunLoop = [NSRunLoop currentRunLoop];
    CFRunLoopRef CFRunLoop = [currentRunLoop getCFRunLoop];
    CFRunLoopAddSource(CFRunLoop, runLoopSource, kCFRunLoopDefaultMode);

    kr = IOServiceAddMatchingNotification( notifyPort,  // notifyPort
          kIOFirstMatchNotification, // notificationType
          matchingDict,  // matching
          DeviceAdded,  // callback
          NULL,    // refCon
          &gAddedIter
          );
    DeviceAdded(NULL,gAddedIter);
    }

    the usbVendor and USBProduct ids are correct for my device.
    notifyport is a member of the class
    IOServiceAddMatchingNotification does return KERN_SUCCESS

    thanks,
    Ed Despard
  • I do it slightly differently in my code. Though the thread I use for USB
    is not using Cocoa, I think the code below should work for you:

    NSNumber* usbVendor = [NSNumber numberWithInt:1027];
    NSNumber* usbProduct = [NSNumber numberWithInt:24592];

    NSMutableDictionary* matchingDict =
        IOServiceMatching(kIOUSBDeviceClassName);
    [matchingDict takeValue:usbVendor forKey:@"kUSBVendorID"];
    [matchingDict takeValue:usbProduct forKey:@"kUSBProductID"];

    mach_port_t masterPort;
    kern_return_t kr;
    CFRunLoopSourceRef runLoopSource;

    kr = IOMasterPort (MACH_PORT_NULL, &masterPort);
    if (!kr && masterPort) {
        notifyPort = IONotificationPortCreate (masterPort);
        runLoopSource = IONotificationPortGetRunLoopSource (notifyPort);

        CFRunLoopAddSource([[NSRunLoop currentRunLoop] getCurrentRunLoop],
                            runLoopSource,
                            kCFRunLoopDefaultMode);

        kr = IOServiceAddMatchingNotification(
                            notifyPort,
                            kIOFirstMatchNotification,
                            matchingDict,
                            DeviceAdded,
                            NULL,
                            &gAddedIter);

        DeviceAdded(NULL, gAddedIter);    // arm the notification

        mach_port_deallocate (mach_task_self(), masterPort);
    }


    I believe this may work better in your context. If this doesn't, you may
    want to join Apple's USB mailing list and ask the question to the
    experts there.

    HTH,
    Florent

    ed despard wrote:
    > I apologize for the other two empty body messages with the same title,
    > Im not sure what was happening with my mail client.
    >
    > Hi, Im trying to receive notifications of usb device connection and
    > termination from within my cocoa apps.  Apples CF based example code
    > works fine, but when I converted their code to work in my application, i
    > never receive any notifications.  The relevent code is provided below
    >
    >
    > - (void) awakeFromNib
    > {
    > kern_return_t        kr;
    >
    > NSMutableDictionary* matchingDict =
    > IOServiceMatching(kIOUSBDeviceClassName);
    > NSNumber* usbVendor = [NSNumber numberWithInt:1027];
    > NSNumber* usbProduct = [NSNumber numberWithInt:24592];
    > [matchingDict takeValue:usbVendor forKey:@"kUSBVendorID"];
    > [matchingDict takeValue:usbProduct forKey:@"kUSBProductID"];
    >
    > notifyPort = IONotificationPortCreate(kIOMasterPortDefault);
    > CFRunLoopSourceRef        runLoopSource;
    > runLoopSource = IONotificationPortGetRunLoopSource(notifyPort);
    > NSRunLoop* currentRunLoop = [NSRunLoop currentRunLoop];
    > CFRunLoopRef CFRunLoop = [currentRunLoop getCFRunLoop];
    > CFRunLoopAddSource(CFRunLoop, runLoopSource, kCFRunLoopDefaultMode);
    >
    > kr = IOServiceAddMatchingNotification(    notifyPort,            //
    > notifyPort
    > kIOFirstMatchNotification,    // notificationType
    > matchingDict,            // matching
    > DeviceAdded,            // callback
    > NULL,                // refCon
    > &gAddedIter
    > );
    > DeviceAdded(NULL,gAddedIter);
    > }
    >
    > the usbVendor and USBProduct ids are correct for my device.
    > notifyport is a member of the class
    > IOServiceAddMatchingNotification does return KERN_SUCCESS
    >
    >
    > thanks,
    > Ed Despard
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<florent...>
    m

    >
    >
    > This email sent to <florent...>

    --
    Florent Pillet                  http://www.florentpillet.com
    Software consultant - Mac OS X, Palm OS, Windows Mobile
    Skype callto://florent.pillet
previous month november 2006 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      
Go to today
MindNode
MindNode offered a free license !