Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @implementation USBHIDAppDelegate
- #pragma mark - Contants
- static const UInt32 GDHEM6310FProductID = 0x0094;
- static const UInt32 GDHEM6310FVendorID = 0x0590;
- static const uint8_t AccessEndOkCommand[] = { 0x02, 0x08, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07 };
- static const size_t AccessEndOkResonposeSize = 9;
- static const uint8_t AccessEndErrCommand[] = { 0x02, 0x08, 0x0f, 0xff, 0xff, 0xff, 0x00, 0x00, 0xf8 };
- static const size_t AccessEndErrResonposeSize = 9;
- static const uint8_t AccessStartCommand[] = { 0x02, 0x08, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x18 };
- static const size_t AccessStartResonposeSize = 25;
- static const uint8_t ReadDailyDataCommand[] = { 0x02, 0x08, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00 };
- static const size_t ReadDailyDataResonposeSize = 57;
- static const uint8_t ReadSettingIndexCommand[] = { 0x02, 0x08, 0x01, 0x00, 0x0F, 0x74, 0x1e, 0x00, 0x6c };
- static const size_t ReadSettingIndexResonposeSize = 57;
- #pragma mark - Instance methods
- - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
- {
- SInt32 productID = GDHEM6310FProductID;
- CFNumberRef idReference;
- uint8_t report = malloc(ReadDailyDataResonposeSize);
- // Insert code here to initialize your application
- // Create an HID Manager
- IOHIDManagerRef HIDManager = IOHIDManagerCreate(kCFAllocatorDefault,
- kIOHIDOptionsTypeNone);
- // Create a Matching Dictionary
- CFMutableDictionaryRef matchDict = CFDictionaryCreateMutable(kCFAllocatorDefault,
- 2,
- &kCFTypeDictionaryKeyCallBacks,
- &kCFTypeDictionaryValueCallBacks);
- // Set up productID for the matching dict
- idReference = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &productID);
- // Specify the product ID reference in the Matching Dictionary
- CFDictionarySetValue(matchDict,
- CFSTR(kIOHIDProductIDKey),
- idReference);
- // Register the Matching Dictionary to the HID Manager
- IOHIDManagerSetDeviceMatching(HIDManager, matchDict);
- // Register a callback for USB device detection with the HID Manager
- IOHIDManagerRegisterDeviceMatchingCallback(HIDManager, &Handle_DeviceMatchingCallback, (__bridge void *)(self)); // was NULL
- // Register a callback for USB device removal with the HID Manager
- IOHIDManagerRegisterDeviceRemovalCallback(HIDManager, &Handle_DeviceRemovalCallback, (__bridge void *)(self)); // was NULL
- // Register the HID Manager on our app’s run loop
- IOHIDManagerScheduleWithRunLoop(HIDManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
- // Open the HID Manager
- IOReturn IOReturn = IOHIDManagerOpen(HIDManager, kIOHIDOptionsTypeNone);
- if(IOReturn) NSLog(@"IOHIDManagerOpen failed."); // Couldn't open the HID manager;
- CFSetRef device = IOHIDManagerCopyDevices(HIDManager);
- check_noerr(IOReturn);
- IOHIDDeviceRegisterInputReportCallback(device, report, ReadDailyDataResonposeSize, Handle_IOHIDDeviceIOHIDReportCallback, NULL);
- }
- #pragma mark - Callback Functions
- // New USB device specified in the matching dictionary has been added (callback function)
- static void Handle_DeviceMatchingCallback(void *inContext,
- IOReturn inResult,
- void *inSender,
- IOHIDDeviceRef device){
- // Retrieve the device name & serial number
- NSString *devName = [NSString stringWithUTF8String:CFStringGetCStringPtr(IOHIDDeviceGetProperty(device, CFSTR("Product")), kCFStringEncodingMacRoman)];
- NSString *devSerialNumber = [NSString stringWithUTF8String:CFStringGetCStringPtr(IOHIDDeviceGetProperty(device, CFSTR("SerialNumber")), kCFStringEncodingMacRoman)];
- // Log the device Name, Serial Number & device count
- NSLog(@"\nOMRON device added: %p\nModel: %@\nSerial Number:%@\nOMRON device count: %ld",
- device,
- devName,
- devSerialNumber,
- USBDeviceCount(inSender));
- // Open the Device
- IOReturn err = IOHIDDeviceOpen(device, kIOHIDOptionsTypeSeizeDevice);
- check_noerr(err);
- // Register the value callback
- IOHIDDeviceRegisterInputValueCallback(device, Handle_IOHIDDeviceInputValueCallback, NULL);
- // Schedule the device with the current run loop
- IOHIDDeviceScheduleWithRunLoop(device, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
- }
- // USB device specified in the matching dictionary has been removed (callback function)
- static void Handle_DeviceRemovalCallback(void *inContext,
- IOReturn inResult,
- void *inSender,
- IOHIDDeviceRef _deviceRef){
- // Log the device ID & device count
- NSLog(@"\nOMRON device removed: %p\nOMRON device count: %ld",
- (void *)_deviceRef,
- USBDeviceCount(inSender));
- // TODO: make sure gateway doesnt try to do something with the removed device
- }
- // Counts the number of devices in the device set (incudes all USB devices that match our dictionary)
- static long USBDeviceCount(IOHIDManagerRef HIDManager){
- // The device set includes all USB devices that match our matching dictionary. Fetch it.
- CFSetRef devSet = IOHIDManagerCopyDevices(HIDManager);
- // The devSet will be NULL if there are 0 devices, so only try to count the devices if devSet exists
- if(devSet)
- {
- return CFSetGetCount(devSet);
- }
- else
- {
- // There were no matching devices (devSet was NULL), so return a count of 0
- NSLog(@"device not detected");
- return 0;
- }
- }
- static void Handle_IOHIDDeviceInputValueCallback(void *inContext,
- IOReturn inResult,
- void *inSender,
- IOHIDValueRef inIOHIDValueRef){
- // Store our result for error checking
- IOReturn err = inResult;
- printf("%s(context: %p, result: %p, sender: %p, value: %p).\n",
- __PRETTY_FUNCTION__, inContext, (void *) inResult, inSender, (void*) inIOHIDValueRef);
- check_noerr(err);
- }
- static void Handle_IOHIDDeviceIOHIDReportCallback(void *inContext,
- IOReturn *inResult,
- void *inSender,
- IOHIDReportType inType,
- uint32_t inReportID,
- uint8_t *inReport,
- CFIndex InReportLength){
- printf("%s(context: %p, result: %p, sender: %p," \
- "type: %ld, id: %u, report: %p, length: %ld).\n",
- __PRETTY_FUNCTION__, inContext, (void *) inResult, inSender,
- (long) inType, inReportID, inReport, InReportLength);
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement