Raccogli informazioni sui dispositivi USB sospesi tramite IOUSBDeviceInterface245

StackOverflow https://stackoverflow.com/questions/7377168

  •  28-10-2019
  •  | 
  •  

Domanda

Sto cercando di ottenere alcune informazioni (come il nome di un prodotto) tramite IOUSBDeviceInterface245 (da IOKit su Mac OS X):
Codice n. 1

io_iterator_t iterator;
io_object_t   usb_device  
SInt32 score = 0;
kern_return_t kr = IOServiceGetMatchingServices(kIOMasterPortDefault,     
                                           IOServiceNameMatching("AppleUSBOHCI"),    
                                           &iterator);   
IOCFPlugInInterface     **plugin_interface = NULL; 
IOUSBDeviceInterface245 **usb_interface    = NULL;
// ...
// enumerate devices (usb_device = IOIteratorNext(iterator))
// ...
IOReturn return_code = IOCreatePlugInInterfaceForService(usb_device,  
                                                  kIOUSBDeviceUserClientTypeID, 
                                                  kIOCFPlugInInterfaceID,  
                                                  &plugin_interface,
                                                  &score);
HRESULT h_result = (*pluginInterface)->QueryInterface(pluginInterface,
                                  CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID245), 
                                  (LPVOID)&usb_interface);
IODestroyPlugInInterface(pluginInterface);

UInt8 product_string_index;
return_code = (*usb_interface)->USBGetProductStringIndex(usb_interface,  
                                                         &product_string_index);
// Next, getting a product string by string's index. To do so, we need to send  
// IOUSBDevRequest (with type "kUSBRqGetDescriptor") to the current USB-device.
// Code bellow - is a pseudocode:
char *product_name = malloc(128);
get_string_by_index(usb_interface, product_string_index, product_string, 128);

Questo codice funziona bene per tutti i dispositivi attualmente funzionanti, ma non funziona per i dispositivi sospesi ("dormienti")
dispositivi (product_name è solo una stringa vuota).

Ma, se utilizzo questo codice:
Codice n. 2

 CFMutableDictionaryRef child_properties = CFDictionaryCreateMutable(NULL, 0,  
                                                                NULL,NULL);
 kern_return_t kr;
 io_service_t io_service;
 // iterator = iterator for "AppleUSBOHCI" or "AppleUSBEHCI" services
 while((io_service = IOIteratorNext(iterator))) {      
    io_iterator_t   children_iterator;
    kernel_return = IORegistryEntryGetChildIterator(io_service, kIOServicePlane,  
                                                   &children_iterator);
    io_registry_entry_t child;
    while((child = IOIteratorNext(children_iterator))) {
      // Create a CFDictionary with usb-device's properties
      IORegistryEntryCreateCFProperties(child, &child_properties,  
                                        kCFAllocatorDefault, 
                                        kNilOptions);
      NSLog(@"%@",[(NSDictionary*)child_properties valueForKey:@"USB Product Name"]);
    }
  }

funziona per tutti i dispositivi (non so perché).

Ho provato a riattivare un dispositivo sospeso con questo codice (utilizzando con il codice n. 1):

(*usb_interface)->USBDeviceSuspend(usb_interface, false);

ma non è cambiato nulla.

Una cosa che ho notato: il codice n. 1 funziona perfettamente per tutte le proprietà di dispositivi addormentati, eccetto i valori di stringa (che vengono recuperati dai indice).

AGGIORNAMENTO:
Quindi, la mia domanda è come posso annullare la sospensione di un dispositivo per l'utilizzo con il primo blocco di codice ?
Grazie.

È stato utile?

Soluzione

Non puoi leggere la stringa da un dispositivo sospeso (come nel codice # 1) poiché viene letta direttamente dal dispositivo (che non può rispondere, poiché è sospeso).Per leggerli direttamente, dovrai prima riattivare il dispositivo (che potrebbe essere una cattiva idea se è stato sospeso da un driver, inoltre, ricordati di controllare se ha avuto successo)

Se vuoi leggere le stringhe standard, usa il secondo blocco di codice (che legge le proprietà, che erano già state recuperate dal dispositivo)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top