Bladeren bron

usb: expose device serial

osy 1 week geleden
bovenliggende
commit
5e8a394212
2 gewijzigde bestanden met toevoegingen van 38 en 9 verwijderingen
  1. 31 6
      Sources/CocoaSpice/CSUSBDevice.m
  2. 7 3
      Sources/CocoaSpice/include/CSUSBDevice.h

+ 31 - 6
Sources/CocoaSpice/CSUSBDevice.m

@@ -22,6 +22,7 @@
 @interface CSUSBDevice ()
 
 @property (nonatomic, readwrite, nonnull) SpiceUsbDevice *device;
+@property (nonatomic) BOOL hasReadDescriptors;
 
 @end
 
@@ -29,6 +30,7 @@
 
 @synthesize usbManufacturerName = _usbManufacturerName;
 @synthesize usbProductName = _usbProductName;
+@synthesize usbSerial = _usbSerial;
 @synthesize usbVendorId = _usbVendorId;
 @synthesize usbProductId = _usbProductId;
 
@@ -56,6 +58,7 @@
     }
     _usbVendorId = ddesc.idVendor;
     _usbProductId = ddesc.idProduct;
+    self.hasReadDescriptors = YES;
     if (libusb_open(dev, &handle) == 0) {
         unsigned char name[64] = { 0 };
         libusb_get_string_descriptor_ascii(handle,
@@ -71,33 +74,47 @@
         if (name[0] != '\0') {
             _usbManufacturerName = [NSString stringWithCString:(char *)name encoding:NSASCIIStringEncoding];
         }
+        name[0] = '\0';
+        libusb_get_string_descriptor_ascii(handle,
+                                           ddesc.iSerialNumber,
+                                           name, sizeof(name));
+        if (name[0] != '\0') {
+            _usbSerial = [NSString stringWithCString:(char *)name encoding:NSASCIIStringEncoding];
+        }
         libusb_close(handle);
     }
 }
 
 - (NSString *)usbManufacturerName {
-    if (!_usbManufacturerName) {
+    if (!self.hasReadDescriptors) {
         [self readDescriptors];
     }
     return _usbManufacturerName;
 }
 
 - (NSString *)usbProductName {
-    if (!_usbProductName) {
+    if (!self.hasReadDescriptors) {
         [self readDescriptors];
     }
     return _usbProductName;
 }
 
+- (NSString *)usbSerial {
+    if (!self.hasReadDescriptors) {
+        [self readDescriptors];
+    }
+    return _usbSerial;
+}
+
 - (NSInteger)usbVendorId {
-    if (!_usbVendorId) {
+    if (!self.hasReadDescriptors) {
         [self readDescriptors];
     }
     return _usbVendorId;
 }
 
 - (NSInteger)usbProductId {
-    if (!_usbProductId) {
+    if (!self.hasReadDescriptors) {
         [self readDescriptors];
     }
     return _usbProductId;
@@ -132,8 +149,16 @@
 }
 
 - (BOOL)isEqualToUSBDevice:(CSUSBDevice *)usbDevice {
-    NSString *description = self.description;
-    return description.length > 0 && [description isEqualToString:usbDevice.description];
+    if (self.usbBusNumber == usbDevice.usbBusNumber &&
+        self.usbPortNumber == usbDevice.usbPortNumber &&
+        self.usbVendorId == usbDevice.usbVendorId &&
+        self.usbProductId == usbDevice.usbProductId &&
+        [self.usbManufacturerName isEqualToString:usbDevice.usbManufacturerName] &&
+        [self.usbProductName isEqualToString:usbDevice.usbProductName] &&
+        [self.usbSerial isEqualToString:usbDevice.usbSerial]) {
+        return YES;
+    }
+    return NO;
 }
 
 - (BOOL)isEqual:(id)object {

+ 7 - 3
Sources/CocoaSpice/include/CSUSBDevice.h

@@ -32,6 +32,9 @@ NS_ASSUME_NONNULL_BEGIN
 /// USB product if available
 @property (nonatomic, nullable, readonly) NSString *usbProductName;
 
+/// USB device serial if available
+@property (nonatomic, nullable, readonly) NSString *usbSerial;
+
 /// USB vendor ID
 @property (nonatomic, readonly) NSInteger usbVendorId;
 
@@ -52,9 +55,10 @@ NS_ASSUME_NONNULL_BEGIN
 ///
 /// 1. USB manufacturer
 /// 2. USB product
-/// 3. USB vendor id and product id
-/// 4. USB bus number
-/// 5. USB address
+/// 3. USB device serial
+/// 4. USB vendor id and product id
+/// 5. USB bus number
+/// 6. USB address
 ///
 /// @param usbDevice Other device
 /// @returns true if `usbDevice` is equal to this one