Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Looking for an example program / objective-C foundation tool which maintains a run loop & monitors USB.



Here you go, this is a simple example that will print the names of devices you plug in out. Obviously runs form the command line. just create a new C++ tool project in XCode, then don't forget to add the IOKit framework.

I suspect you can add/modify from here what you need.
(quickly threw this together...so as usual add proper error checking and such where required)


Kevin


#include <iostream> #include <CoreFoundation/CoreFoundation.h>

#include <IOKit/usb/IOUSBLib.h>

void deviceUSBMatchedCallBack(
void *refcon,
io_iterator_t iterator );
int main (int argc, char * const argv[]) {
mach_port_t masterPort;
IOReturn err;
err=IOMasterPort(MACH_PORT_NULL,&masterPort);
if(err!=kIOReturnSuccess)
{
std::cout<<"Could not get masterport."<<std::endl;
return(-1);
}
CFDictionaryRef matchingDict=IOServiceMatching("IOUSBDevice");
io_iterator_t it;
IONotificationPortRef notifyPort;
CFRunLoopSourceRef notifySource;
notifyPort = IONotificationPortCreate( masterPort );
notifySource = IONotificationPortGetRunLoopSource( notifyPort );
CFRunLoopAddSource( CFRunLoopGetCurrent(), notifySource,
kCFRunLoopDefaultMode );
bool t=false;


IOServiceAddMatchingNotification(notifyPort,kIOMatchedNotification,matchingDict,deviceUSBMatchedCallBack,&t,&it);
deviceUSBMatchedCallBack(&t,it);
t=true;
CFRunLoopRun();
return 0;
}


void deviceUSBMatchedCallBack(
   void *refcon,
   io_iterator_t iterator )
{
 bool t=*(bool*)refcon;

io_object_t device;
io_struct_inband_t buffer1;
unsigned int size1=4096;
while ( (device = IOIteratorNext( iterator ) ) != 0 )
{
if(t)
{
IORegistryEntryGetProperty(device,"USB Product Name",buffer1,&size1);
std::cout<<buffer1<<std::endl;
}
}
}


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Usb mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/usb/email@hidden

This email sent to email@hidden
References: 
 >Looking for an example program / objective-C foundation tool which maintains a run loop & monitors USB. (From: George Lawrence Storm <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.