2012-06-01 22 views
7

द्वारा यूएसबी डिवाइस का पता लगाना मै मैक के लिए एक फाउंडेशन टूल लिख रहा हूं और पता लगाने की कोशिश कर रहा हूं कि ऐप्पल डिवाइस कनेक्ट होने पर और यूएसबी के माध्यम से डिस्कनेक्ट हो गए हैं। मुझे में USBPrivateDataSample के साथ कुछ मदद मिली - लेकिन ऐसा लगता है कि अगर मैं एक विक्रेता आईडी और उत्पाद आईडी दोनों प्रदान करता हूं तो यह केवल काम कर रहा है। मैं उत्पाद आईडी को खत्म करना चाहता हूं और ऐप्पल उपकरणों (विक्रेता आईडी 1452) पर सभी यूएसबी घटनाओं का पता लगाना चाहता हूं। कोई मदद यहाँ? (मैं matchingDict के लिए एक उत्पाद आईडी जोड़ सकते हैं और इस तरह के एक उपकरण में कोई प्लग यदि यह डिवाइस कोई समस्या नहीं मिलेगा:कोको: विक्रेता आईडी

#include <CoreFoundation/CoreFoundation.h> 

#include <IOKit/IOKitLib.h> 
#include <IOKit/IOMessage.h> 
#include <IOKit/IOCFPlugIn.h> 
#include <IOKit/usb/IOUSBLib.h> 

#define kMyVendorID 1452 

int list_devices(void); 

int list_devices(void){ 
    CFMutableDictionaryRef matchingDict; 
    io_iterator_t iter; 
    kern_return_t kr; 
    io_service_t device; 
    CFNumberRef numberRef; 
    long usbVendor = kMyVendorID; 

    /* set up a matching dictionary for the class */ 
    matchingDict = IOServiceMatching(kIOUSBDeviceClassName); // Interested in instances of class 
    // IOUSBDevice and its subclasses 
    if (matchingDict == NULL) { 
     fprintf(stderr, "IOServiceMatching returned NULL.\n"); 
     return -1; 
    } 

    // We are interested in all USB devices (as opposed to USB interfaces). The Common Class Specification 
    // tells us that we need to specify the idVendor, idProduct, and bcdDevice fields, or, if we're not interested 
    // in particular bcdDevices, just the idVendor and idProduct. Note that if we were trying to match an 
    // IOUSBInterface, we would need to set more values in the matching dictionary (e.g. idVendor, idProduct, 
    // bInterfaceNumber and bConfigurationValue. 

    // Create a CFNumber for the idVendor and set the value in the dictionary 
    numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &usbVendor); 
    CFDictionarySetValue(matchingDict, 
         CFSTR(kUSBVendorID), 
         numberRef); 
    CFRelease(numberRef); 
    numberRef = NULL;  


    /* Now we have a dictionary, get an iterator.*/ 
    kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter); 
    if (kr != KERN_SUCCESS) 
     return -1; 

    /* iterate */ 
    while ((device = IOIteratorNext(iter))) 
    { 
     io_name_t  deviceName; 
     CFStringRef  deviceNameAsCFString; 

     /* do something with device, eg. check properties */ 
     /* ... */ 
     /* And free the reference taken before continuing to the next item */ 

     // Get the USB device's name. 
     kr = IORegistryEntryGetName(device, deviceName); 
     if (KERN_SUCCESS != kr) { 
      deviceName[0] = '\0'; 
     } 

     deviceNameAsCFString = CFStringCreateWithCString(kCFAllocatorDefault, deviceName, 
                 kCFStringEncodingASCII); 

     // Dump our data to stderr just to see what it looks like. 
     fprintf(stderr, "deviceName: "); 
     CFShow(deviceNameAsCFString); 

     IOObjectRelease(device); 
    } 

    /* Done, release the iterator */ 
    IOObjectRelease(iter); 

    return 1; 
} 

int main(int argc, char* argv[]) 
{ 
    while(1){ 
     list_devices(); 
     sleep(1); 
    } 
    return 0; 
} 
ध्यान दें

:

यहाँ मेरी कोड है कि किसी भी उपकरणों का पता लगाने में प्रतीत नहीं होता है विक्रेता आईडी को बदलने के बिना)। लेकिन मैं इसे केवल विक्रेता आईडी के साथ नहीं ढूंढ सकता।

+0

मैं यूएसबी उपकरणों के लिए ऐसा करने का एक तरीका है के बारे में पता नहीं कर रहा हूँ। पीसीआई उपकरणों के साथ, आईओकिट आपको अपने विक्रेता और उत्पाद आईडी के साथ थोड़ा सा मुखौटा प्रदान करने की अनुमति देता है, लेकिन यह यूएसबी के मामले में प्रतीत नहीं होता है। – pmdj

उत्तर

4

सभी उत्पादों है कि विशेष विक्रेता आप उत्पाद ID फ़ील्ड ए नमूना मिलान हालत में वाइल्डकार्ड का उपयोग कर सकते हैं के अंतर्गत आता है की सूची प्राप्त करने के रूप में नीचे है:

  long vid = 1452; //Apple vendor ID 
     CFNumberRef refVendorId = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &vid); 
     CFDictionarySetValue (matchingDict, CFSTR ("idVendor"), refVendorId); 
     CFRelease(refVendorId); 
     //all product by same vendor 
     CFDictionarySetValue (matchingDict, CFSTR ("idProduct"), CFSTR("*")); 
+3

+1, मैं ऐप्पल की [यूएसबी ड्राइवर मिलान पर युक्तियाँ] (http://developer.apple.com/library/mac/#qa/qa1076/_index.html) पृष्ठ को भी नोट करना चाहूंगा, जिसमें एक विवरण है मिलान नियम कैसे काम करते हैं। इसमें वाइल्डकार्ड और मास्क का उपयोग करने की जानकारी शामिल है। – Hasturkun

1

केवल एक VID प्रविष्टि से मेल खाना चाहिए के साथ एक शब्दकोश फ़िल्टर बनाने का उस विक्रेता के लिए सभी पीआईडी। मैं आपके कोड में मतदान के बजाए डिवाइस सम्मिलन कॉलबैक के लिए पंजीकरण करने की अनुशंसा करता हूं। ओएस को पहचान को संभालने दें और अपने ऐप को असीमित रूप से सूचित करें।

इस कोड मेरे लिए काम करता है:

#import "IOKit/hid/IOHIDManager.h" 
#include <IOKit/usb/IOUSBLib.h> 

@implementation MyApp 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 

    IOHIDManagerRef HIDManager = IOHIDManagerCreate(kCFAllocatorDefault, 
                kIOHIDOptionsTypeNone); 
    CFMutableDictionaryRef matchDict = CFDictionaryCreateMutable(
                kCFAllocatorDefault, 
                2, 
                &kCFTypeDictionaryKeyCallBacks, 
                &kCFTypeDictionaryValueCallBacks); 
    int vid = 0x1234; // ToDo: Place your VID here 
    CFNumberRef vid_num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &vid); 
    CFDictionarySetValue(matchDict, CFSTR(kUSBVendorID), vid_num); 
    CFRelease(vid_num); 

    IOHIDManagerSetDeviceMatching(HIDManager, matchDict); 
    // Here we use the same callback for insertion & removal. 
    // Use separate handlers if desired. 
    IOHIDManagerRegisterDeviceMatchingCallback(HIDManager, Handle_UsbDetectionCallback, (__bridge void*)self); 
    IOHIDManagerRegisterDeviceRemovalCallback(HIDManager, Handle_UsbDetectionCallback, (__bridge void*)self); 
    IOHIDManagerScheduleWithRunLoop(HIDManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 
    IOHIDManagerOpen(HIDManager, kIOHIDOptionsTypeNone); 
} 

// New USB device specified in the matching dictionary has been added (callback function) 
static void Handle_UsbDetectionCallback(void *inContext, IOReturn inResult, void *inSender, IOHIDDeviceRef inIOHIDDeviceRef) { 
    //ToDo: Code for dealing with the USB device 
} 

@end