From 4e118c154dde70e196c4381bd97790a9413c3552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Ca=C3=B1uelo?= Date: Thu, 30 Sep 2021 08:22:21 +0200 Subject: Add g_usb_device_get_string_descriptor_bytes_full Some devices won't answer to a request with a 128-byte data buffer, this allows the user to specify the size of the request data buffer. g_usb_device_get_string_descriptor_bytes still uses the default buffer size (128 bytes). --- gusb/gusb-device.c | 47 +++++++++++++++++++++++++++++++++++++++-------- gusb/gusb-device.h | 19 ++++++++++++------- gusb/libgusb.ver | 6 ++++++ 3 files changed, 57 insertions(+), 15 deletions(-) (limited to 'gusb') diff --git a/gusb/gusb-device.c b/gusb/gusb-device.c index 16a5527..184bc39 100644 --- a/gusb/gusb-device.c +++ b/gusb/gusb-device.c @@ -916,9 +916,10 @@ g_usb_device_get_string_descriptor (GUsbDevice *device, } /** - * g_usb_device_get_string_descriptor_bytes: + * g_usb_device_get_string_descriptor_bytes_full: * @desc_index: the index for the string descriptor to retrieve * @langid: the language ID + * @length: size of the request data buffer * @error: a #GError, or %NULL * * Get a raw string descriptor from the device. The returned string should be freed @@ -926,16 +927,17 @@ g_usb_device_get_string_descriptor (GUsbDevice *device, * * Return value: (transfer full): a possibly UTF-16 string, or NULL on error. * - * Since: 0.3.6 + * Since: 0.3.8 **/ GBytes * -g_usb_device_get_string_descriptor_bytes (GUsbDevice *device, - guint8 desc_index, - guint16 langid, - GError **error) +g_usb_device_get_string_descriptor_bytes_full (GUsbDevice *device, + guint8 desc_index, + guint16 langid, + gsize length, + GError **error) { gint rc; - unsigned char buf[128]; + g_autofree guint8 *buf = g_malloc0(length); g_return_val_if_fail (G_USB_IS_DEVICE (device), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); @@ -947,7 +949,7 @@ g_usb_device_get_string_descriptor_bytes (GUsbDevice *device, rc = libusb_get_string_descriptor (device->priv->handle, desc_index, langid, - buf, sizeof(buf)); + buf, length); if (rc < 0) { g_usb_device_libusb_error_to_gerror (device, rc, error); return NULL; @@ -956,6 +958,35 @@ g_usb_device_get_string_descriptor_bytes (GUsbDevice *device, return g_bytes_new (buf, rc); } +/** + * g_usb_device_get_string_descriptor_bytes: + * @desc_index: the index for the string descriptor to retrieve + * @langid: the language ID + * @error: a #GError, or %NULL + * + * Get a raw string descriptor from the device. The returned string should be freed + * with g_bytes_unref() when no longer needed. + * The descriptor will be at most 128 btes in length, if you need to + * issue a request with either a smaller or larger descriptor, you can + * use g_usb_device_get_string_descriptor_bytes_full instead. + * + * Return value: (transfer full): a possibly UTF-16 string, or NULL on error. + * + * Since: 0.3.6 + **/ +GBytes * +g_usb_device_get_string_descriptor_bytes (GUsbDevice *device, + guint8 desc_index, + guint16 langid, + GError **error) +{ + return g_usb_device_get_string_descriptor_bytes_full(device, + desc_index, + langid, + 128, + error); +} + typedef gssize (GUsbDeviceTransferFinishFunc) (GUsbDevice *device, GAsyncResult *res, GError **error); typedef struct { diff --git a/gusb/gusb-device.h b/gusb/gusb-device.h index 5430fb6..722104a 100644 --- a/gusb/gusb-device.h +++ b/gusb/gusb-device.h @@ -212,13 +212,18 @@ gboolean g_usb_device_set_interface_alt (GUsbDevice *device, guint8 alt, GError **error); -gchar *g_usb_device_get_string_descriptor (GUsbDevice *device, - guint8 desc_index, - GError **error); -GBytes *g_usb_device_get_string_descriptor_bytes (GUsbDevice *device, - guint8 desc_index, - guint16 langid, - GError **error); +gchar *g_usb_device_get_string_descriptor (GUsbDevice *device, + guint8 desc_index, + GError **error); +GBytes *g_usb_device_get_string_descriptor_bytes (GUsbDevice *device, + guint8 desc_index, + guint16 langid, + GError **error); +GBytes *g_usb_device_get_string_descriptor_bytes_full (GUsbDevice *device, + guint8 desc_index, + guint16 langid, + gsize length, + GError **error); /* sync -- TODO: use GCancellable and GUsbSource */ gboolean g_usb_device_control_transfer (GUsbDevice *device, diff --git a/gusb/libgusb.ver b/gusb/libgusb.ver index 7c9e044..f59d1a9 100644 --- a/gusb/libgusb.ver +++ b/gusb/libgusb.ver @@ -153,3 +153,9 @@ LIBGUSB_0.3.6 { g_usb_device_get_string_descriptor_bytes; local: *; } LIBGUSB_0.3.5; + +LIBGUSB_0.3.8 { + global: + g_usb_device_get_string_descriptor_bytes_full; + local: *; +} LIBGUSB_0.3.6; -- cgit v1.2.1