Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Vendor and Product name #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions include/libusbp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 **************************************************/

Expand Down
20 changes: 20 additions & 0 deletions include/libusbp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(). */
Expand Down
80 changes: 80 additions & 0 deletions src/mac/device_mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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.
Expand All @@ -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)
{
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}