Re: Looking for an example program / objective-C foundation tool which maintains a run loop & monitors USB.
Subject : Re: Looking for an example program / objective-C foundation tool which maintains a run loop & monitors USB.
From: Kevin Vanwulpen <email@hidden >
Date: Fri, 09 Sep 2005 20:16:02 -0700
Delivered-to: email@hidden
Delivered-to: email@hidden
Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=dk20050327; d=earthlink.net; b=W6213CAyT30hYyKqY2K8jvpJH51nz7zfQZKAIrtkhXT18EPRjhKAbiRvIA5EDiD1; h=Received:Message-ID:Date:From:User-Agent:X-Accept-Language:MIME-Version:To:Subject:References:In-Reply-To:Content-Type:Content-Transfer-Encoding:X-ELNK-Trace:X-Originating-IP;
User-agent: Mozilla Thunderbird 1.0.6 (Macintosh/20050716)
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
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.