From 1808ffcc0f43aab4d712970ca68329bf8aa730d9 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Tue, 13 Sep 2022 11:34:36 +0100 Subject: Allow loading and saving only devices with specific tags This would allow us, for example, to only load devices in bootloader or runtime phases. --- gusb/gusb-context.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ gusb/gusb-context.h | 10 ++++++++++ gusb/libgusb.ver | 2 ++ 3 files changed, 60 insertions(+) diff --git a/gusb/gusb-context.c b/gusb/gusb-context.c index e52a265..5fb7977 100644 --- a/gusb/gusb-context.c +++ b/gusb/gusb-context.c @@ -376,6 +376,28 @@ g_usb_context_remove_device(GUsbContext *self, struct libusb_device *dev) **/ gboolean g_usb_context_load(GUsbContext *self, JsonObject *json_object, GError **error) +{ + return g_usb_context_load_with_tag(self, json_object, NULL, error); +} + +/** + * g_usb_context_load_with_tag: + * @self: a #GUsbContext + * @json_object: a #JsonObject + * @tag: a string tag, e.g. `runtime-reload`, or %NULL + * @error: a #GError, or %NULL + * + * Loads any devices with a specified tag into the context from a JSON object. + * + * Return value: %TRUE on success + * + * Since: 0.4.1 + **/ +gboolean +g_usb_context_load_with_tag(GUsbContext *self, + JsonObject *json_object, + const gchar *tag, + GError **error) { GUsbContextPrivate *priv = GET_PRIVATE(self); JsonArray *json_array; @@ -403,6 +425,8 @@ g_usb_context_load(GUsbContext *self, JsonObject *json_object, GError **error) g_object_new(G_USB_TYPE_DEVICE, "context", self, NULL); if (!_g_usb_device_load(device, obj_tmp, error)) return FALSE; + if (tag != NULL && !_g_usb_device_has_tag(device, tag)) + continue; g_ptr_array_add(priv->devices, g_object_ref(device)); g_signal_emit(self, signals[DEVICE_ADDED_SIGNAL], 0, device); } @@ -426,6 +450,28 @@ g_usb_context_load(GUsbContext *self, JsonObject *json_object, GError **error) **/ gboolean g_usb_context_save(GUsbContext *self, JsonBuilder *json_builder, GError **error) +{ + return g_usb_context_save_with_tag(self, json_builder, NULL, error); +} + +/** + * g_usb_context_save_with_tag: + * @self: a #GUsbContext + * @json_builder: a #JsonBuilder + * @tag: a string tag, e.g. `runtime-reload`, or %NULL + * @error: a #GError, or %NULL + * + * Saves any devices with a specified tag into an existing JSON builder. + * + * Return value: %TRUE on success + * + * Since: 0.4.1 + **/ +gboolean +g_usb_context_save_with_tag(GUsbContext *self, + JsonBuilder *json_builder, + const gchar *tag, + GError **error) { GUsbContextPrivate *priv = GET_PRIVATE(self); @@ -447,6 +493,8 @@ g_usb_context_save(GUsbContext *self, JsonBuilder *json_builder, GError **error) } for (guint i = 0; i < priv->devices->len; i++) { GUsbDevice *device = g_ptr_array_index(priv->devices, i); + if (tag != NULL && !_g_usb_device_has_tag(device, tag)) + continue; if (!_g_usb_device_save(device, json_builder, error)) return FALSE; } diff --git a/gusb/gusb-context.h b/gusb/gusb-context.h index 3352ce7..f633520 100644 --- a/gusb/gusb-context.h +++ b/gusb/gusb-context.h @@ -74,7 +74,17 @@ g_usb_context_enumerate(GUsbContext *self); gboolean g_usb_context_load(GUsbContext *self, JsonObject *json_object, GError **error); gboolean +g_usb_context_load_with_tag(GUsbContext *self, + JsonObject *json_object, + const gchar *tag, + GError **error); +gboolean g_usb_context_save(GUsbContext *self, JsonBuilder *json_builder, GError **error); +gboolean +g_usb_context_save_with_tag(GUsbContext *self, + JsonBuilder *json_builder, + const gchar *tag, + GError **error); void g_usb_context_set_debug(GUsbContext *self, GLogLevelFlags flags); diff --git a/gusb/libgusb.ver b/gusb/libgusb.ver index 061ac44..4a4a731 100644 --- a/gusb/libgusb.ver +++ b/gusb/libgusb.ver @@ -188,6 +188,8 @@ LIBGUSB_0.4.0 { LIBGUSB_0.4.1 { global: + g_usb_context_load_with_tag; + g_usb_context_save_with_tag; g_usb_device_add_tag; local: *; } LIBGUSB_0.4.0; -- cgit v1.2.1