IOHIDManagerOpen fails when application is relaunched

Hi,


I am working of USB HID devices. The IOHIDManagerOpen fails if the device is connected and application is launched. Below is the code where in I am registering for DeviceAttach and DeviceDetach event. I am able to recieve the attach and detach event, but IOHIDManagerOpen returns fails. In the event of failure I am not able to read or write to the device. Have I missed any thing while registering the device?


#include <IOKit/hid/IOHIDManager.h>
#include <IOKit/hid/IOHIDKeys.h>
#include <CoreFoundation/CoreFoundation.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>


//Reference : https://developer.apple.com/library/content/technotes/tn2187/_index.html


// this will be called when the HID Manager matches a new ( hot plugged ) HID device
static void Handle_DeviceMatchingCallback(
            void *          inContext,       // context from IOHIDManagerRegisterDeviceMatchingCallback
            IOReturn        inResult,        // the result of the matching operation
            void *          inSender,        // the IOHIDManagerRef for the new device
            IOHIDDeviceRef  inIOHIDDeviceRef // the new HID device
) {
    printf( "%s( context: %p, result: %x, sender: %p, device: %p ).\n",
        __PRETTY_FUNCTION__, inContext, inResult, inSender, ( void* ) inIOHIDDeviceRef );
}   // Handle_DeviceMatchingCallback


// this will be called when a HID device is removed ( unplugged )
static void Handle_RemovalCallback(
                void *         inContext,       // context from IOHIDManagerRegisterDeviceMatchingCallback
                IOReturn       inResult,        // the result of the removing operation
                void *         inSender,        // the IOHIDManagerRef for the device being removed
                IOHIDDeviceRef inIOHIDDeviceRef // the removed HID device
) {
    printf( "%s( context: %p, result: %x, sender: %p, device: %p ).\n",
        __PRETTY_FUNCTION__, inContext, inResult, inSender, ( void* ) inIOHIDDeviceRef );
}   // Handle_RemovalCallback


int main(int argc, char const *argv[]) {
  if (argc < 2) {
    printf("usage:\n\t%s <vid>\n", argv[0]);
    printf("for example: %s 0x04d8\n", argv[0]);
    return 1;
  }


  IOOptionBits options = kIOHIDManagerOptionNone;
  IOHIDManagerRef hidMgr = IOHIDManagerCreate(kCFAllocatorDefault, options);
  if (hidMgr != NULL && CFGetTypeID(hidMgr) == IOHIDManagerGetTypeID()) {
    printf("Successfully created HIDManager\n" );
    long vid = strtol(argv[1], NULL, 16);
    CFMutableDictionaryRef matchingDictRef = NULL;
    if(vid) {
      CFNumberRef vidRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &vid);
      if(vidRef) {
        matchingDictRef = CFDictionaryCreateMutable(kCFAllocatorDefault, 0 , &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
        if(matchingDictRef) {
          CFDictionarySetValue(matchingDictRef, CFSTR(kIOHIDVendorIDKey), vidRef);
        } else {
          printf("Failed to created mutable dictionary\n");
        }
        CFRelease(vidRef);
      } else {
        printf("Failed to create VID NumberRef\n");
      }
    }
    IOHIDManagerSetDeviceMatching(hidMgr, matchingDictRef);


    //Register for the call backs
    IOHIDManagerRegisterDeviceMatchingCallback(hidMgr, Handle_DeviceMatchingCallback, NULL);
    IOHIDManagerRegisterDeviceRemovalCallback(hidMgr, Handle_RemovalCallback, NULL);
    IOHIDManagerScheduleWithRunLoop(hidMgr, CFRunLoopGetMain(), kCFRunLoopDefaultMode);


    IOReturn tIOReturn = IOHIDManagerOpen( hidMgr, kIOHIDOptionsTypeNone );
    if(tIOReturn != kIOReturnSuccess) {
      printf("HIDManager faile to open -> %x\n",tIOReturn);
    } else  {
      printf("HIDManager opened Successfully\n" );
    }


  } else {
    return -1;
  }
  CFRunLoopRun();
  return 0;
}


Thanks,

RV

IOHIDManagerOpen fails when application is relaunched
 
 
Q