Created
June 9, 2010 04:31
-
-
Save JRHeaton/431040 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <IOKit/IOKitLib.h> | |
#include <stdint.h> | |
#include <sys/signal.h> | |
IONotificationPortRef muxNotifyPort = NULL; | |
io_object_t muxNotification = NULL; | |
void interupt(int signal) { | |
printf("Interupted\n"); | |
IONotificationPortDestroy(muxNotifyPort); | |
IOObjectRelease(muxNotification); | |
} | |
void muxCallback(void *refCon, io_iterator_t iterator) { | |
printf("Device connection\n"); | |
} | |
int startUSBMux() { | |
CFMutableDictionaryRef matching; | |
io_service_t muxService; | |
matching = IOServiceMatching("AppleUSBDeviceMux"); | |
if(!matching) { | |
printf("Failed to create matching dictionary\n"); | |
return -1; | |
} | |
muxService = IOServiceGetMatchingService(kIOMasterPortDefault, matching); | |
if(!muxService) { | |
printf("Failed to get AppleUSBDeviceMux service\n"); | |
return -1; | |
} | |
muxNotifyPort = IONotificationPortCreate(kIOMasterPortDefault); | |
CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(muxNotifyPort), kCFRunLoopDefaultMode); | |
if(IOServiceAddInterestNotification(muxNotifyPort, muxService, "IOGeneralInterest", muxCallback, NULL, &muxNotification) != KERN_SUCCESS) { | |
printf("Failed to register for mux notifications\n"); | |
IOObjectRelease(muxService); | |
IONotificationPortDestroy(muxNotifyPort); | |
return -1; | |
} | |
uint32_t speed; | |
CFTypeRef muxSpeed = IORegistryEntryCreateCFProperty((io_registry_entry_t)muxService, CFSTR("speed"), kCFAllocatorDefault, 0); | |
if(!muxSpeed || (CFGetTypeID(muxSpeed) != CFNumberGetTypeID())) { | |
printf("Mux speed property is retarded\n"); | |
} else { | |
CFNumberGetValue((CFNumberRef)muxSpeed, kCFNumberSInt32Type, &speed); | |
CFRelease(muxSpeed); | |
} | |
return 0; | |
} | |
int main() { | |
signal(SIGINT, interupt); | |
if(!startUSBMux()) { | |
CFRunLoopRun(); | |
} | |
printf("Failed\n"); | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment