diff --git a/include/libusbp.h b/include/libusbp.h index be78bf6..bff9bec 100644 --- a/include/libusbp.h +++ b/include/libusbp.h @@ -109,6 +109,12 @@ 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, + + /*! The device does not have a name. */ + LIBUSBP_ERROR_NO_NAME = 10, }; /*! Attempts to copy an error. If you copy a NULL ::libusbp_error @@ -382,6 +388,22 @@ 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(). */ +LIBUSBP_API LIBUSBP_WARN_UNUSED +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 5d3bd91..81db243 100644 --- a/include/libusbp.hpp +++ b/include/libusbp.hpp @@ -377,6 +377,26 @@ 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_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 3e58b95..9ddb0e4 100644 --- a/src/mac/device_mac.c +++ b/src/mac/device_mac.c @@ -7,6 +7,8 @@ struct libusbp_device uint16_t vendor_id; uint16_t revision; char * serial_number; + char * manufacturer; + char * name; }; static libusbp_error * device_allocate(libusbp_device ** device) @@ -54,6 +56,16 @@ 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 device (product) name + if (error == NULL) { + error = get_string(service, CFSTR(kUSBProductString), &new_device->name); + } + // Get the ID. if (error == NULL) { @@ -100,6 +112,8 @@ 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. @@ -108,6 +122,18 @@ 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); + } + + // 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) { @@ -124,6 +150,8 @@ void libusbp_device_free(libusbp_device * device) if (device != NULL) { libusbp_string_free(device->serial_number); + libusbp_string_free(device->manufacturer); + libusbp_string_free(device->name); free(device); } } @@ -239,3 +267,55 @@ 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); +} + +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); +}