From 7a10a24350330aceb254a45a91c0d8559f4a8eb7 Mon Sep 17 00:00:00 2001 From: Torfinn Berset Date: Mon, 26 Nov 2018 14:00:05 +0100 Subject: [PATCH 1/2] Add method to extract manufacturer (Vendor) name from USB Device. Only macOS implementation for now --- include/libusbp.h | 13 +++++++++++++ include/libusbp.hpp | 10 ++++++++++ src/mac/device_mac.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/include/libusbp.h b/include/libusbp.h index be78bf6..889a708 100644 --- a/include/libusbp.h +++ b/include/libusbp.h @@ -109,6 +109,9 @@ enum libusbp_error_code /*! The error might have been caused by the transfer getting cancelled from * the host side. Some data might have been transferred anyway. */ LIBUSBP_ERROR_CANCELLED = 8, + + /*! The device does not have a manufacturer. */ + LIBUSBP_ERROR_NO_MANUFACTURER = 9, }; /*! Attempts to copy an error. If you copy a NULL ::libusbp_error @@ -383,6 +386,16 @@ libusbp_error * libusbp_device_get_os_id( char ** id); + +/*! Gets the name of the device manufacturer as an ASCII-encoded string. + * + * You should free the returned string by calling libusbp_string_free(). */ +LIBUSBP_API LIBUSBP_WARN_UNUSED +libusbp_error * libusbp_device_get_manufacturer( + const libusbp_device * device, + char ** manufacturer); + + /** libusbp_generic_interface **************************************************/ /*! Represents a generic or vendor-defined interface of a USB device. A null diff --git a/include/libusbp.hpp b/include/libusbp.hpp index 5d3bd91..91fdfb9 100644 --- a/include/libusbp.hpp +++ b/include/libusbp.hpp @@ -377,6 +377,16 @@ namespace libusbp libusbp_string_free(str); return serial_number; } + + /*! Wrapper for libusbp_device_get_manufacturer(). */ + std::string get_manufacturer() const + { + char * str; + throw_if_needed(libusbp_device_get_manufacturer(pointer, &str)); + std::string manufacturer = str; + libusbp_string_free(str); + return manufacturer; + } }; /*! Wrapper for libusbp_list_connected_devices(). */ diff --git a/src/mac/device_mac.c b/src/mac/device_mac.c index 3e58b95..d74badd 100644 --- a/src/mac/device_mac.c +++ b/src/mac/device_mac.c @@ -7,6 +7,7 @@ struct libusbp_device uint16_t vendor_id; uint16_t revision; char * serial_number; + char * manufacturer; }; static libusbp_error * device_allocate(libusbp_device ** device) @@ -54,6 +55,11 @@ libusbp_error * create_device(io_service_t service, libusbp_device ** device) error = get_string(service, CFSTR(kUSBSerialNumberString), &new_device->serial_number); } + // Get the manufacturer (vendor) name + if (error == NULL) { + error = get_string(service, CFSTR(kUSBVendorString), &new_device->manufacturer); + } + // Get the ID. if (error == NULL) { @@ -100,6 +106,7 @@ libusbp_error * libusbp_device_copy(const libusbp_device * source, libusbp_devic { memcpy(new_device, source, sizeof(libusbp_device)); new_device->serial_number = NULL; + new_device->manufacturer = NULL; } // Copy the serial number. @@ -108,6 +115,12 @@ libusbp_error * libusbp_device_copy(const libusbp_device * source, libusbp_devic error = string_copy(source->serial_number, &new_device->serial_number); } + // Copy the manufacturer + if (error == NULL && source->manufacturer != NULL) + { + error = string_copy(source->manufacturer, &new_device->manufacturer); + } + // Pass the device to the caller. if (error == NULL) { @@ -124,6 +137,7 @@ void libusbp_device_free(libusbp_device * device) if (device != NULL) { libusbp_string_free(device->serial_number); + libusbp_string_free(device->manufacturer); free(device); } } @@ -239,3 +253,29 @@ libusbp_error * libusbp_device_get_os_id( return iokit_id_to_string(device->id, id); } + +libusbp_error * libusbp_device_get_manufacturer( + const libusbp_device * device, + char ** manufacturer) +{ + if (manufacturer == NULL) + { + return error_create("Manufacturer output pointer is null."); + } + + *manufacturer = NULL; + + if (device == NULL) + { + return error_create("Device is null."); + } + + if (device->manufacturer == NULL) + { + libusbp_error * error = error_create("Device does not have a manufacturer."); + error = error_add_code(error, LIBUSBP_ERROR_NO_MANUFACTURER); + return error; + } + + return string_copy(device->manufacturer, manufacturer); +} From 16a854a52009be54496d501faa27045ad049cf0e Mon Sep 17 00:00:00 2001 From: Torfinn Berset Date: Mon, 26 Nov 2018 14:20:52 +0100 Subject: [PATCH 2/2] Add method to extract (Product) name from USB Device. Only macOS implementation for now --- include/libusbp.h | 13 +++++++++++-- include/libusbp.hpp | 10 ++++++++++ src/mac/device_mac.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/include/libusbp.h b/include/libusbp.h index 889a708..bff9bec 100644 --- a/include/libusbp.h +++ b/include/libusbp.h @@ -112,6 +112,9 @@ enum libusbp_error_code /*! The device does not have a manufacturer. */ LIBUSBP_ERROR_NO_MANUFACTURER = 9, + + /*! The device does not have a name. */ + LIBUSBP_ERROR_NO_NAME = 10, }; /*! Attempts to copy an error. If you copy a NULL ::libusbp_error @@ -385,8 +388,6 @@ libusbp_error * libusbp_device_get_os_id( const libusbp_device *, char ** id); - - /*! Gets the name of the device manufacturer as an ASCII-encoded string. * * You should free the returned string by calling libusbp_string_free(). */ @@ -395,6 +396,14 @@ libusbp_error * libusbp_device_get_manufacturer( const libusbp_device * device, char ** manufacturer); +/*! Gets the name of the device as an ASCII-encoded string. + * + * You should free the returned string by calling libusbp_string_free(). */ +LIBUSBP_API LIBUSBP_WARN_UNUSED +libusbp_error * libusbp_device_get_name( + const libusbp_device * device, + char ** name); + /** libusbp_generic_interface **************************************************/ diff --git a/include/libusbp.hpp b/include/libusbp.hpp index 91fdfb9..81db243 100644 --- a/include/libusbp.hpp +++ b/include/libusbp.hpp @@ -387,6 +387,16 @@ namespace libusbp libusbp_string_free(str); return manufacturer; } + + /*! Wrapper for libusbp_device_get_name(). */ + std::string get_name() const + { + char * str; + throw_if_needed(libusbp_device_get_name(pointer, &str)); + std::string name = str; + libusbp_string_free(str); + return name; + } }; /*! Wrapper for libusbp_list_connected_devices(). */ diff --git a/src/mac/device_mac.c b/src/mac/device_mac.c index d74badd..9ddb0e4 100644 --- a/src/mac/device_mac.c +++ b/src/mac/device_mac.c @@ -8,6 +8,7 @@ struct libusbp_device uint16_t revision; char * serial_number; char * manufacturer; + char * name; }; static libusbp_error * device_allocate(libusbp_device ** device) @@ -60,6 +61,11 @@ libusbp_error * create_device(io_service_t service, libusbp_device ** device) error = get_string(service, CFSTR(kUSBVendorString), &new_device->manufacturer); } + // Get the device (product) name + if (error == NULL) { + error = get_string(service, CFSTR(kUSBProductString), &new_device->name); + } + // Get the ID. if (error == NULL) { @@ -107,6 +113,7 @@ libusbp_error * libusbp_device_copy(const libusbp_device * source, libusbp_devic memcpy(new_device, source, sizeof(libusbp_device)); new_device->serial_number = NULL; new_device->manufacturer = NULL; + new_device->name = NULL; } // Copy the serial number. @@ -121,6 +128,12 @@ libusbp_error * libusbp_device_copy(const libusbp_device * source, libusbp_devic error = string_copy(source->manufacturer, &new_device->manufacturer); } + // Copy the manufacturer + if (error == NULL && source->name != NULL) + { + error = string_copy(source->name, &new_device->name); + } + // Pass the device to the caller. if (error == NULL) { @@ -138,6 +151,7 @@ void libusbp_device_free(libusbp_device * device) { libusbp_string_free(device->serial_number); libusbp_string_free(device->manufacturer); + libusbp_string_free(device->name); free(device); } } @@ -279,3 +293,29 @@ libusbp_error * libusbp_device_get_manufacturer( return string_copy(device->manufacturer, manufacturer); } + +libusbp_error * libusbp_device_get_name( + const libusbp_device * device, + char ** name) +{ + if (name == NULL) + { + return error_create("Name output pointer is null."); + } + + *name = NULL; + + if (device == NULL) + { + return error_create("Device is null."); + } + + if (device->name == NULL) + { + libusbp_error * error = error_create("Device does not have a name."); + error = error_add_code(error, LIBUSBP_ERROR_NO_NAME); + return error; + } + + return string_copy(device->name, name); +}