Advertisement
Guest User

UserSpaceHidDriver

a guest
Feb 21st, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @implementation USBHIDAppDelegate
  2.  
  3. #pragma mark - Contants
  4.  
  5. static const UInt32 GDHEM6310FProductID = 0x0094;
  6. static const UInt32 GDHEM6310FVendorID = 0x0590;
  7.  
  8. static const uint8_t AccessEndOkCommand[] = { 0x02, 0x08, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07 };
  9. static const size_t AccessEndOkResonposeSize = 9;
  10.  
  11. static const uint8_t AccessEndErrCommand[] = { 0x02, 0x08, 0x0f, 0xff, 0xff, 0xff, 0x00, 0x00, 0xf8 };
  12. static const size_t AccessEndErrResonposeSize = 9;
  13.  
  14. static const uint8_t AccessStartCommand[] = { 0x02, 0x08, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x18 };
  15. static const size_t AccessStartResonposeSize = 25;
  16.  
  17. static const uint8_t ReadDailyDataCommand[] = { 0x02, 0x08, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00 };
  18. static const size_t ReadDailyDataResonposeSize = 57;
  19.  
  20. static const uint8_t ReadSettingIndexCommand[] = { 0x02, 0x08, 0x01, 0x00, 0x0F, 0x74, 0x1e, 0x00, 0x6c };
  21. static const size_t ReadSettingIndexResonposeSize = 57;
  22.  
  23. #pragma mark - Instance methods
  24.  
  25. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  26. {
  27.     SInt32 productID = GDHEM6310FProductID;
  28.     CFNumberRef idReference;
  29.     uint8_t report = malloc(ReadDailyDataResonposeSize);
  30.    
  31.     // Insert code here to initialize your application
  32.    
  33.     // Create an HID Manager
  34.     IOHIDManagerRef HIDManager = IOHIDManagerCreate(kCFAllocatorDefault,
  35.                                                     kIOHIDOptionsTypeNone);
  36.    
  37.     // Create a Matching Dictionary
  38.     CFMutableDictionaryRef matchDict = CFDictionaryCreateMutable(kCFAllocatorDefault,
  39.                                                                  2,
  40.                                                                  &kCFTypeDictionaryKeyCallBacks,
  41.                                                                  &kCFTypeDictionaryValueCallBacks);
  42.    
  43.     // Set up productID for the matching dict
  44.     idReference = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &productID);
  45.  
  46.     // Specify the product ID reference in the Matching Dictionary
  47.     CFDictionarySetValue(matchDict,
  48.                          CFSTR(kIOHIDProductIDKey),
  49.                          idReference);
  50.    
  51.     // Register the Matching Dictionary to the HID Manager
  52.     IOHIDManagerSetDeviceMatching(HIDManager, matchDict);
  53.    
  54.     // Register a callback for USB device detection with the HID Manager
  55.     IOHIDManagerRegisterDeviceMatchingCallback(HIDManager, &Handle_DeviceMatchingCallback, (__bridge void *)(self)); // was NULL
  56.    
  57.     // Register a callback for USB device removal with the HID Manager
  58.     IOHIDManagerRegisterDeviceRemovalCallback(HIDManager, &Handle_DeviceRemovalCallback, (__bridge void *)(self)); // was NULL
  59.    
  60.     // Register the HID Manager on our app’s run loop
  61.     IOHIDManagerScheduleWithRunLoop(HIDManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
  62.    
  63.     // Open the HID Manager
  64.     IOReturn IOReturn = IOHIDManagerOpen(HIDManager, kIOHIDOptionsTypeNone);
  65.     if(IOReturn) NSLog(@"IOHIDManagerOpen failed.");  //  Couldn't open the HID manager;
  66.    
  67.     CFSetRef device = IOHIDManagerCopyDevices(HIDManager);
  68.    
  69.     check_noerr(IOReturn);
  70.    
  71.     IOHIDDeviceRegisterInputReportCallback(device, report, ReadDailyDataResonposeSize, Handle_IOHIDDeviceIOHIDReportCallback, NULL);
  72.    
  73.    
  74. }
  75.  
  76. #pragma mark - Callback Functions
  77.  
  78. // New USB device specified in the matching dictionary has been added (callback function)
  79. static void Handle_DeviceMatchingCallback(void *inContext,
  80.                                           IOReturn inResult,
  81.                                           void *inSender,
  82.                                           IOHIDDeviceRef device){
  83.    
  84.     // Retrieve the device name & serial number
  85.     NSString *devName = [NSString stringWithUTF8String:CFStringGetCStringPtr(IOHIDDeviceGetProperty(device, CFSTR("Product")), kCFStringEncodingMacRoman)];
  86.    
  87.     NSString *devSerialNumber = [NSString stringWithUTF8String:CFStringGetCStringPtr(IOHIDDeviceGetProperty(device, CFSTR("SerialNumber")), kCFStringEncodingMacRoman)];
  88.    
  89.    
  90.     // Log the device Name, Serial Number & device count
  91.     NSLog(@"\nOMRON device added: %p\nModel: %@\nSerial Number:%@\nOMRON device count: %ld",
  92.           device,
  93.           devName,
  94.           devSerialNumber,
  95.           USBDeviceCount(inSender));
  96.    
  97.     // Open the Device
  98.     IOReturn err = IOHIDDeviceOpen(device, kIOHIDOptionsTypeSeizeDevice);
  99.     check_noerr(err);
  100.    
  101.     // Register the value callback
  102.     IOHIDDeviceRegisterInputValueCallback(device, Handle_IOHIDDeviceInputValueCallback, NULL);
  103.    
  104.     // Schedule the device with the current run loop
  105.     IOHIDDeviceScheduleWithRunLoop(device, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
  106. }
  107.  
  108. // USB device specified in the matching dictionary has been removed (callback function)
  109. static void Handle_DeviceRemovalCallback(void *inContext,
  110.                                          IOReturn inResult,
  111.                                          void *inSender,
  112.                                          IOHIDDeviceRef _deviceRef){
  113.    
  114.     // Log the device ID & device count
  115.     NSLog(@"\nOMRON device removed: %p\nOMRON device count: %ld",
  116.           (void *)_deviceRef,
  117.           USBDeviceCount(inSender));
  118.    
  119.     // TODO: make sure gateway doesnt try to do something with the removed device
  120. }
  121.  
  122. // Counts the number of devices in the device set (incudes all USB devices that match our dictionary)
  123. static long USBDeviceCount(IOHIDManagerRef HIDManager){
  124.    
  125.     // The device set includes all USB devices that match our matching dictionary. Fetch it.
  126.     CFSetRef devSet = IOHIDManagerCopyDevices(HIDManager);
  127.    
  128.     // The devSet will be NULL if there are 0 devices, so only try to count the devices if devSet exists
  129.     if(devSet)
  130.     {
  131.         return CFSetGetCount(devSet);
  132.     }
  133.     else
  134.     {
  135.         // There were no matching devices (devSet was NULL), so return a count of 0
  136.         NSLog(@"device not detected");
  137.        
  138.         return 0;
  139.     }
  140.    
  141. }
  142.  
  143. static void Handle_IOHIDDeviceInputValueCallback(void *inContext,
  144.                                                  IOReturn inResult,
  145.                                                  void *inSender,
  146.                                                  IOHIDValueRef inIOHIDValueRef){
  147.    
  148.     // Store our result for error checking
  149.     IOReturn err = inResult;
  150.    
  151.     printf("%s(context: %p, result: %p, sender: %p, value: %p).\n",
  152.            __PRETTY_FUNCTION__, inContext, (void *) inResult, inSender, (void*) inIOHIDValueRef);
  153.    
  154.     check_noerr(err);
  155. }
  156.  
  157. static void Handle_IOHIDDeviceIOHIDReportCallback(void *inContext,
  158.                                                   IOReturn *inResult,
  159.                                                   void *inSender,
  160.                                                   IOHIDReportType inType,
  161.                                                   uint32_t inReportID,
  162.                                                   uint8_t *inReport,
  163.                                                   CFIndex InReportLength){
  164.    
  165.     printf("%s(context: %p, result: %p, sender: %p," \
  166.            "type: %ld, id: %u, report: %p, length: %ld).\n",
  167.            __PRETTY_FUNCTION__, inContext, (void *) inResult, inSender,
  168.            (long) inType, inReportID, inReport, InReportLength);
  169.    
  170. }
  171.  
  172. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement