Blame SOURCES/0006-convert-libosinfo-wrap-osinfo_os_get_all_devices.patch

3156cb
From 46f5885dba1b669588d98eb840af454564d07cae Mon Sep 17 00:00:00 2001
3156cb
From: Laszlo Ersek <lersek@redhat.com>
3156cb
Date: Thu, 6 Jan 2022 15:09:07 +0100
3156cb
Subject: [PATCH] convert/libosinfo: wrap osinfo_os_get_all_devices()
3156cb
3156cb
Introduce the "osinfo_os.get_devices" OCaml method, for wrapping the
3156cb
libosinfo API osinfo_os_get_all_devices().
3156cb
3156cb
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1942325
3156cb
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
3156cb
Message-Id: <20220106140910.13695-7-lersek@redhat.com>
3156cb
[lersek@redhat.com: call OCaml values "<something>v" (Rich)]
3156cb
Acked-by: Richard W.M. Jones <rjones@redhat.com>
3156cb
---
3156cb
 convert/libosinfo-c.c | 66 +++++++++++++++++++++++++++++++++++++++++++
3156cb
 convert/libosinfo.ml  | 14 +++++++++
3156cb
 convert/libosinfo.mli | 14 +++++++++
3156cb
 3 files changed, 94 insertions(+)
3156cb
3156cb
diff --git a/convert/libosinfo-c.c b/convert/libosinfo-c.c
3156cb
index 09cf588d..b8e78bec 100644
3156cb
--- a/convert/libosinfo-c.c
3156cb
+++ b/convert/libosinfo-c.c
3156cb
@@ -50,6 +50,7 @@
3156cb
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(OsinfoFilter, g_object_unref)
3156cb
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(OsinfoLoader, g_object_unref)
3156cb
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(OsinfoOsList, g_object_unref)
3156cb
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(OsinfoDeviceList, g_object_unref)
3156cb
 #endif
3156cb
 
3156cb
 typedef OsinfoDb *OsinfoDb_t;
3156cb
@@ -255,3 +256,68 @@ v2v_osinfo_os_get_device_drivers (value osv)
3156cb
 
3156cb
   CAMLreturn (rv);
3156cb
 }
3156cb
+
3156cb
+/* Collect OsinfoDevice properties from two levels:
3156cb
+ *
3156cb
+ * - The OSINFO_ENTITY_PROP_ID property, originating from the OsinfoEntity base
3156cb
+ *   class. This is a unique URI, identifying the device.
3156cb
+ *
3156cb
+ * - All currently known OSINFO_DEVICE_PROP_* properties, originating from the
3156cb
+ *   OsinfoDevice class.
3156cb
+ *
3156cb
+ * All of the above properties have string values. Thus, for uniformity, access
3156cb
+ * all these properties by their names at the OsinfoEntity level (i.e., forego
3156cb
+ * the class- and property-specific, dedicated property getter functions).
3156cb
+ */
3156cb
+static const char * const device_prop[] = {
3156cb
+  OSINFO_ENTITY_PROP_ID,
3156cb
+  OSINFO_DEVICE_PROP_VENDOR,
3156cb
+  OSINFO_DEVICE_PROP_VENDOR_ID,
3156cb
+  OSINFO_DEVICE_PROP_PRODUCT,
3156cb
+  OSINFO_DEVICE_PROP_PRODUCT_ID,
3156cb
+  OSINFO_DEVICE_PROP_NAME,
3156cb
+  OSINFO_DEVICE_PROP_CLASS,
3156cb
+  OSINFO_DEVICE_PROP_BUS_TYPE,
3156cb
+  OSINFO_DEVICE_PROP_SUBSYSTEM,
3156cb
+};
3156cb
+#define NUM_DEVICE_PROPS (sizeof device_prop / sizeof device_prop[0])
3156cb
+
3156cb
+value
3156cb
+v2v_osinfo_os_get_all_devices (value osv)
3156cb
+{
3156cb
+  CAMLparam1 (osv);
3156cb
+  CAMLlocal3 (retvalv, linkv, propsv);
3156cb
+  g_autoptr (OsinfoDeviceList) dev_list = NULL;
3156cb
+  OsinfoList *ent_list;
3156cb
+  gint ent_nr;
3156cb
+
3156cb
+  retvalv = Val_emptylist;
3156cb
+  dev_list = osinfo_os_get_all_devices (OsinfoOs_t_val (osv), NULL);
3156cb
+  ent_list = OSINFO_LIST (dev_list);
3156cb
+  ent_nr = osinfo_list_get_length (ent_list);
3156cb
+
3156cb
+  while (ent_nr > 0) {
3156cb
+    OsinfoEntity *ent;
3156cb
+    size_t prop_nr;
3156cb
+
3156cb
+    --ent_nr;
3156cb
+    ent = osinfo_list_get_nth (ent_list, ent_nr);
3156cb
+
3156cb
+    propsv = caml_alloc (NUM_DEVICE_PROPS, 0);
3156cb
+    for (prop_nr = 0; prop_nr < NUM_DEVICE_PROPS; ++prop_nr) {
3156cb
+      const gchar *prop_val;
3156cb
+
3156cb
+      prop_val = osinfo_entity_get_param_value (ent, device_prop[prop_nr]);
3156cb
+      if (prop_val == NULL)
3156cb
+        prop_val = "";
3156cb
+      Store_field (propsv, prop_nr, caml_copy_string (prop_val));
3156cb
+    }
3156cb
+
3156cb
+    linkv = caml_alloc (2, 0);
3156cb
+    Store_field (linkv, 0, propsv);
3156cb
+    Store_field (linkv, 1, retvalv);
3156cb
+    retvalv = linkv;
3156cb
+  }
3156cb
+
3156cb
+  CAMLreturn (retvalv);
3156cb
+}
3156cb
diff --git a/convert/libosinfo.ml b/convert/libosinfo.ml
3156cb
index bd9ca126..78271be2 100644
3156cb
--- a/convert/libosinfo.ml
3156cb
+++ b/convert/libosinfo.ml
3156cb
@@ -32,13 +32,27 @@ type osinfo_device_driver = {
3156cb
   files : string list;
3156cb
 }
3156cb
 
3156cb
+type osinfo_device = {
3156cb
+  id : string;
3156cb
+  vendor : string;
3156cb
+  vendor_id : string;
3156cb
+  product : string;
3156cb
+  product_id : string;
3156cb
+  name : string;
3156cb
+  class_ : string;
3156cb
+  bus_type : string;
3156cb
+  subsystem : string;
3156cb
+}
3156cb
+
3156cb
 external osinfo_os_get_id : osinfo_os_t -> string = "v2v_osinfo_os_get_id"
3156cb
 external osinfo_os_get_device_drivers : osinfo_os_t -> osinfo_device_driver list = "v2v_osinfo_os_get_device_drivers"
3156cb
+external osinfo_os_get_devices : osinfo_os_t -> osinfo_device list = "v2v_osinfo_os_get_all_devices"
3156cb
 
3156cb
 class osinfo_os h =
3156cb
   object (self)
3156cb
     method get_id () = osinfo_os_get_id h
3156cb
     method get_device_drivers () = osinfo_os_get_device_drivers h
3156cb
+    method get_devices () = osinfo_os_get_devices h
3156cb
 end
3156cb
 
3156cb
 external osinfo_db_load : unit -> osinfo_db_t = "v2v_osinfo_db_load"
3156cb
diff --git a/convert/libosinfo.mli b/convert/libosinfo.mli
3156cb
index 0428ef91..1ece7b41 100644
3156cb
--- a/convert/libosinfo.mli
3156cb
+++ b/convert/libosinfo.mli
3156cb
@@ -29,11 +29,25 @@ type osinfo_device_driver = {
3156cb
   files : string list;
3156cb
 }
3156cb
 
3156cb
+type osinfo_device = {
3156cb
+  id : string;
3156cb
+  vendor : string;
3156cb
+  vendor_id : string;
3156cb
+  product : string;
3156cb
+  product_id : string;
3156cb
+  name : string;
3156cb
+  class_ : string;
3156cb
+  bus_type : string;
3156cb
+  subsystem : string;
3156cb
+}
3156cb
+
3156cb
 class osinfo_os : osinfo_os_t -> object
3156cb
   method get_id : unit -> string
3156cb
   (** Return the ID. *)
3156cb
   method get_device_drivers : unit -> osinfo_device_driver list
3156cb
   (** Return the list of device drivers. *)
3156cb
+  method get_devices : unit -> osinfo_device list
3156cb
+  (** Return the list of devices. *)
3156cb
 end
3156cb
 (** Minimal OsinfoOs wrapper. *)
3156cb
 
3156cb
-- 
3156cb
2.31.1
3156cb