summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2022-09-13 11:34:36 +0100
committerRichard Hughes <richard@hughsie.com>2022-09-13 11:42:35 +0100
commit1808ffcc0f43aab4d712970ca68329bf8aa730d9 (patch)
treeffc85af0ed9ba6fe761ac7b22d70ac7d89324ce4
parent0d8148627f83fbe8a6deee828c6a4b5ccee3279a (diff)
downloadgusb-1808ffcc0f43aab4d712970ca68329bf8aa730d9.tar.gz
Allow loading and saving only devices with specific tags
This would allow us, for example, to only load devices in bootloader or runtime phases.
-rw-r--r--gusb/gusb-context.c48
-rw-r--r--gusb/gusb-context.h10
-rw-r--r--gusb/libgusb.ver2
3 files changed, 60 insertions, 0 deletions
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
@@ -377,6 +377,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);
}
@@ -427,6 +451,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);
g_return_val_if_fail(G_USB_IS_CONTEXT(self), FALSE);
@@ -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;