Incompatible pointer error when trying to get a HID device reference from HID manager on Mac

StackOverflow https://stackoverflow.com/questions/21944327

  •  14-10-2022
  •  | 
  •  

Question

I'm trying to write a User-Space Device driver to pull some data from a custom HID device. I'm do the following to store a reference of the device in the HID manage into a variable.

CFSetRef device = IOHIDManagerCopyDevices(HIDManager);

after this I do the following to register my callback which is setting up the report read from the device (another area I'm struggling with.)

IOHIDDeviceRegisterInputReportCallback(device, report, ReadDailyDataResonposeSize, Handle_IOHIDDeviceIOHIDReportCallback, NULL);

I'm getting an error in the above function on the reference to 'device' saying "Incompatible pointer types passing CFSetRef"

If I try to change the type that device is when creating it though I get another saying that it needs to be a CFSetRef. So I'm a little confused, anyone have any advice. I'm very new to C as well as working on Macs. The documentation has come off pretty terse to me thus far.

EDIT: Here Is a link to the rest of my code for reference. http://pastebin.com/rFsHisdh

Was it helpful?

Solution

This is the signature of the IOHIDDeviceRegisterInputReportCallback function according to the documentation:

CF_EXPORT void IOHIDDeviceRegisterInputReportCallback(
    IOHIDDeviceRef device,
    uint8_t *report,
    CFIndex reportLength,
    IOHIDReportCallback callback,
    void *context) AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;

As you can see the first argument should be a IOHIDDeviceRef and you are passing in a CFSetRef which provide[s] support for the mathematical concept of a set as Martin R answer indicates.

To get the elements of the set and pass the device (if any) you should do the following:

CFSetRef devices = IOHIDManagerCopyDevices(HIDManager);
CFIndex size = CFSetGetCount(devices);
if(size > 0) {
    CFTypeRef array[size]; // array of IOHIDDeviceRef
    CFSetGetValues(devices, array);
    IOHIDDeviceRegisterInputReportCallback((IOHIDDeviceRef)array[0], report, ReadDailyDataResonposeSize, Handle_IOHIDDeviceIOHIDReportCallback, NULL);
}

Hope it helps.

OTHER TIPS

IOHIDManagerCopyDevices() returns a set of IOHIDDeviceRef elements. You have to get one element from the set and pass that to IOHIDDeviceRegisterInputReportCallback().

I ran into this problem. You were pasting from an Apple document that is no longer correct ( https://developer.apple.com/library/archive/technotes/tn2187/_index.html ). Your report variable is allocated in the wrong format, causing an memory error during runtime. Do the following:

uint8_t *report = (uint8_t *)malloc(reportSize);
IOHIDDeviceRegisterInputReportCallback(device, report, ReadDailyDataResonposeSize, Handle_IOHIDDeviceIOHIDReportCallback, NULL);```
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top