开发者

C Callback in Objective-C (IOKIT)

I am trying to write some code that interacts with an USB device in Objective C, and I got stuck on setting the callback function for incoming reports. In my case it's an IOKIT function but I think the problem is more general as I (apparently) don't know how to correctly set a C callback function in Objective-C. I've got a Class "USBController" that handles the io functions

USBController.m:

#include <CoreFoundation/CoreFoundation.h>
#include <Carbon/Carbon.h>
#include <IOKit/hid/IOHIDLib.h>
#import "USBController.h"

static void Handle_IOHIDDeviceIOHIDReportCallback(
                                              void *          inContext,          // context from IOHIDDeviceRegisterInputReportCallback
                                              IOReturn        inResult,           // completion result for the input report operation
                                              void *          inSender,           // IOHIDDeviceRef of the device this report is from
                                              IOHIDReportType inType,             // the report type
                                              uint32_t        inReportID,         // the report ID
                                              uint8_t *       inRe开发者_StackOverflowport,           // pointer to the report data
                                              CFIndex         InReportLength)     // the actual size of the input report
{
    printf("hello"); //just to see if the function is called
}

@implementation USBController
- (void)ConnectToDevice {
    ...
    IOHIDDeviceRegisterInputReportCallback(tIOHIDDeviceRefs[0], report, reportSize,
          Handle_IOHIDDeviceIOHIDReportCallback,(void*)self);
    ...
}
...
@end

All the functions are also declared in the header file.

I think I did pretty much the same as what I've found here, but it doesn't work. The project compiles nicely and everything works up till the moment there is input and the callback function is to be called. Then I get an "EXC_BAD_ACCESS" error. The first three arguments of the function are correct. I'm not so sure about the context.. What did I do wrong?


I am not sure at all that your EXEC_BAD_ACCESS depends on your callback. Indeed, if you say that it is called (I suppose you see the log) and since it only logs a message, there should be no problem with this.

EXEC_BAD_ACCESS is caused by an attempt to access an already deallocated object. You can get more information in two ways:

  1. execute the program in debug mode, so when it crashes you will be able to see the stack content;

  2. activate NSZombies or run the program using the performance tool Zombies; this will tell you exactly which object was accessed after its deallocation.


I know how to fix this. When calling this:

IOHIDDeviceRegisterInputReportCallback(tIOHIDDeviceRefs[0], report, reportSize,
          Handle_IOHIDDeviceIOHIDReportCallback,(void*)self);

You don't include the code for the creation/type of the value called report. However the method name "Handle_IOHIDDeviceIOHIDReportCallback" comes from an Apple document where there is an error in the creation of the report value. https://developer.apple.com/library/archive/technotes/tn2187/_index.html

CFIndex reportSize = 64; 
uint8_t report = malloc( reportSize );  // <---- WRONG 
IOHIDDeviceRegisterInputReportCallback( deviceRef, 
                                        report,  
                                        reportSize,
                                        Handle_IOHIDDeviceIOHIDReportCallback,   
                                        context ); 

Instead do this:

uint8_t *report = (uint8_t *)malloc(reportSize);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜