From 9bc67208a25fa8e1739161b81af8160118945623 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Fri, 3 Feb 2023 15:10:36 +0000 Subject: Store the config-index as a GUsbDeviceEvent This allows emulating the Google Servo Micro. --- gusb/gusb-device.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/gusb/gusb-device.c b/gusb/gusb-device.c index c44079d..f36e91d 100644 --- a/gusb/gusb-device.c +++ b/gusb/gusb-device.c @@ -2980,21 +2980,42 @@ guint8 g_usb_device_get_configuration_index(GUsbDevice *self) { GUsbDevicePrivate *priv = GET_PRIVATE(self); + GUsbDeviceEvent *event = NULL; struct libusb_config_descriptor *config; gint rc; guint8 index; + g_autofree gchar *event_id = NULL; g_return_val_if_fail(G_USB_IS_DEVICE(self), 0); - /* sanity check */ - if (priv->device == NULL) - return 0x0; + /* build event key either for load or save */ + if (priv->device == NULL || + g_usb_context_get_flags(priv->context) & G_USB_CONTEXT_FLAGS_SAVE_EVENTS) + event_id = g_strdup_printf("GetConfigurationIndex"); + + /* emulated */ + if (priv->device == NULL) { + GBytes *bytes; + event = g_usb_device_load_event(self, event_id); + if (event == NULL) + return 0x0; + bytes = g_usb_device_event_get_bytes(event); + if (bytes == NULL && g_bytes_get_size(bytes) != 1) + return 0x0; + return ((const guint8 *) g_bytes_get_data(bytes, NULL))[0]; + } rc = libusb_get_active_config_descriptor(priv->device, &config); g_return_val_if_fail(rc == 0, 0); index = config->iConfiguration; + /* save */ + if (g_usb_context_get_flags(priv->context) & G_USB_CONTEXT_FLAGS_SAVE_EVENTS) { + event = g_usb_device_save_event(self, event_id); + _g_usb_device_event_set_bytes_raw(event, &index, sizeof(index)); + } + libusb_free_config_descriptor(config); return index; } -- cgit v1.2.1