2

How should I write this code in objective-c:

kr = IOServiceAddMatchingNotification(gNotifyPort, kIOFirstMatchNotification, matchingDict, RawDeviceAdded, NULL, &gRawAddedIter); 

I tried this one:

kr = IOServiceAddMatchingNotification(gNotifyPort, kIOFirstMatchNotification, matchingDict, @selector(RawDeviceAdded), NULL, &gRawAddedIter); 

and the function look like:

(void)RawDeviceAdded:(void *)refCon iterator:(io_iterator_t)iterator { ..... } 

I am not sure if its right.

1 Answer 1

3

Short answer is: You can't do that directly.

This is because IOKit is a C-API so any callback functions it requires must be C, and not Objective-C.

That's not to say that you can't mix C and Objective-C, and use the C callback function to trampoline to your Objective-C method. It's simply a matter of getting the reference to the class to the C callback function; in this particular case that's using refCon.

SomeObjcClass.m:

// Objective-C method (note that refCon is not a parameter as that // has no meaning in the Objective-C method) -(void)RawDeviceAdded:(io_iterator_t)iterator { // Do something } // C callback "trampoline" function static void DeviceAdded(void *refCon, io_iterator_t iterator) { SomeObjcClass *obj = (SomeObjcClass *)refCon; [obj RawDeviceAdded:iterator]; } - (void)someMethod { // Call C-API, using a C callback function as a trampoline kr = IOServiceAddMatchingNotification(gNotifyPort, kIOFirstMatchNotification, matchingDict, DeviceAdded, // trampoline function self, // refCon to trampoline &gAddedIter ); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Tnx for answer, In this case is there any better way to communicate (read and write) with external USB device with another library than IOKit?
@NimaM Sorry I have no knowledge in that area.
@NimaM: What kind of device is it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.