Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7d700f8b authored by Mike Lockwood's avatar Mike Lockwood
Browse files

libusbhost: Add usb_device_get_name_from_unique_id()



This allows converting from unique IDs to USB device names

Change-Id: I8e3b300790c01383aed8e3d4f547b54bcb7c2cd4
Signed-off-by: default avatarMike Lockwood <lockwood@android.com>
parent 12353895
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -96,13 +96,20 @@ int usb_device_get_fd(struct usb_device *device);
 */
const char* usb_device_get_name(struct usb_device *device);

/* Returns a unique ID for the device.  Currently this is generated from the
 * dev_name path.
/* Returns a unique ID for the device.
 *Currently this is generated from the dev_name path.
 */
int usb_device_get_unique_id(struct usb_device *device);

/* Returns a unique ID for the device name.
 * Currently this is generated from the device path.
 */
int usb_device_get_unique_id_from_name(const char* name);

/* Returns the device name for the unique ID.
 * Call free() to deallocate the returned string */
char* usb_device_get_name_from_unique_id(int id);

/* Returns the USB vendor ID from the device descriptor for the USB device */
uint16_t usb_device_get_vendor_id(struct usb_device *device);

+10 −0
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@

#define USB_FS_DIR "/dev/bus/usb"
#define USB_FS_ID_SCANNER   "/dev/bus/usb/%d/%d"
#define USB_FS_ID_FORMAT    "/dev/bus/usb/%03d/%03d"


struct usb_host_context {
@@ -314,6 +315,15 @@ int usb_device_get_unique_id_from_name(const char* name)
    return bus * 1000 + dev;
}

char* usb_device_get_name_from_unique_id(int id)
{
    int bus = id / 1000;
    int dev = id % 1000;
    char* result = (char *)calloc(1, strlen(USB_FS_ID_FORMAT));
    snprintf(result, strlen(USB_FS_ID_FORMAT) - 1, USB_FS_ID_FORMAT, bus, dev);
    return result;
}

uint16_t usb_device_get_vendor_id(struct usb_device *device)
{
    struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc;