Blob Blame History Raw
From 46f5885dba1b669588d98eb840af454564d07cae Mon Sep 17 00:00:00 2001
From: Laszlo Ersek <lersek@redhat.com>
Date: Thu, 6 Jan 2022 15:09:07 +0100
Subject: [PATCH] convert/libosinfo: wrap osinfo_os_get_all_devices()

Introduce the "osinfo_os.get_devices" OCaml method, for wrapping the
libosinfo API osinfo_os_get_all_devices().

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1942325
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20220106140910.13695-7-lersek@redhat.com>
[lersek@redhat.com: call OCaml values "<something>v" (Rich)]
Acked-by: Richard W.M. Jones <rjones@redhat.com>
---
 convert/libosinfo-c.c | 66 +++++++++++++++++++++++++++++++++++++++++++
 convert/libosinfo.ml  | 14 +++++++++
 convert/libosinfo.mli | 14 +++++++++
 3 files changed, 94 insertions(+)

diff --git a/convert/libosinfo-c.c b/convert/libosinfo-c.c
index 09cf588d..b8e78bec 100644
--- a/convert/libosinfo-c.c
+++ b/convert/libosinfo-c.c
@@ -50,6 +50,7 @@
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(OsinfoFilter, g_object_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(OsinfoLoader, g_object_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(OsinfoOsList, g_object_unref)
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(OsinfoDeviceList, g_object_unref)
 #endif
 
 typedef OsinfoDb *OsinfoDb_t;
@@ -255,3 +256,68 @@ v2v_osinfo_os_get_device_drivers (value osv)
 
   CAMLreturn (rv);
 }
+
+/* Collect OsinfoDevice properties from two levels:
+ *
+ * - The OSINFO_ENTITY_PROP_ID property, originating from the OsinfoEntity base
+ *   class. This is a unique URI, identifying the device.
+ *
+ * - All currently known OSINFO_DEVICE_PROP_* properties, originating from the
+ *   OsinfoDevice class.
+ *
+ * All of the above properties have string values. Thus, for uniformity, access
+ * all these properties by their names at the OsinfoEntity level (i.e., forego
+ * the class- and property-specific, dedicated property getter functions).
+ */
+static const char * const device_prop[] = {
+  OSINFO_ENTITY_PROP_ID,
+  OSINFO_DEVICE_PROP_VENDOR,
+  OSINFO_DEVICE_PROP_VENDOR_ID,
+  OSINFO_DEVICE_PROP_PRODUCT,
+  OSINFO_DEVICE_PROP_PRODUCT_ID,
+  OSINFO_DEVICE_PROP_NAME,
+  OSINFO_DEVICE_PROP_CLASS,
+  OSINFO_DEVICE_PROP_BUS_TYPE,
+  OSINFO_DEVICE_PROP_SUBSYSTEM,
+};
+#define NUM_DEVICE_PROPS (sizeof device_prop / sizeof device_prop[0])
+
+value
+v2v_osinfo_os_get_all_devices (value osv)
+{
+  CAMLparam1 (osv);
+  CAMLlocal3 (retvalv, linkv, propsv);
+  g_autoptr (OsinfoDeviceList) dev_list = NULL;
+  OsinfoList *ent_list;
+  gint ent_nr;
+
+  retvalv = Val_emptylist;
+  dev_list = osinfo_os_get_all_devices (OsinfoOs_t_val (osv), NULL);
+  ent_list = OSINFO_LIST (dev_list);
+  ent_nr = osinfo_list_get_length (ent_list);
+
+  while (ent_nr > 0) {
+    OsinfoEntity *ent;
+    size_t prop_nr;
+
+    --ent_nr;
+    ent = osinfo_list_get_nth (ent_list, ent_nr);
+
+    propsv = caml_alloc (NUM_DEVICE_PROPS, 0);
+    for (prop_nr = 0; prop_nr < NUM_DEVICE_PROPS; ++prop_nr) {
+      const gchar *prop_val;
+
+      prop_val = osinfo_entity_get_param_value (ent, device_prop[prop_nr]);
+      if (prop_val == NULL)
+        prop_val = "";
+      Store_field (propsv, prop_nr, caml_copy_string (prop_val));
+    }
+
+    linkv = caml_alloc (2, 0);
+    Store_field (linkv, 0, propsv);
+    Store_field (linkv, 1, retvalv);
+    retvalv = linkv;
+  }
+
+  CAMLreturn (retvalv);
+}
diff --git a/convert/libosinfo.ml b/convert/libosinfo.ml
index bd9ca126..78271be2 100644
--- a/convert/libosinfo.ml
+++ b/convert/libosinfo.ml
@@ -32,13 +32,27 @@ type osinfo_device_driver = {
   files : string list;
 }
 
+type osinfo_device = {
+  id : string;
+  vendor : string;
+  vendor_id : string;
+  product : string;
+  product_id : string;
+  name : string;
+  class_ : string;
+  bus_type : string;
+  subsystem : string;
+}
+
 external osinfo_os_get_id : osinfo_os_t -> string = "v2v_osinfo_os_get_id"
 external osinfo_os_get_device_drivers : osinfo_os_t -> osinfo_device_driver list = "v2v_osinfo_os_get_device_drivers"
+external osinfo_os_get_devices : osinfo_os_t -> osinfo_device list = "v2v_osinfo_os_get_all_devices"
 
 class osinfo_os h =
   object (self)
     method get_id () = osinfo_os_get_id h
     method get_device_drivers () = osinfo_os_get_device_drivers h
+    method get_devices () = osinfo_os_get_devices h
 end
 
 external osinfo_db_load : unit -> osinfo_db_t = "v2v_osinfo_db_load"
diff --git a/convert/libosinfo.mli b/convert/libosinfo.mli
index 0428ef91..1ece7b41 100644
--- a/convert/libosinfo.mli
+++ b/convert/libosinfo.mli
@@ -29,11 +29,25 @@ type osinfo_device_driver = {
   files : string list;
 }
 
+type osinfo_device = {
+  id : string;
+  vendor : string;
+  vendor_id : string;
+  product : string;
+  product_id : string;
+  name : string;
+  class_ : string;
+  bus_type : string;
+  subsystem : string;
+}
+
 class osinfo_os : osinfo_os_t -> object
   method get_id : unit -> string
   (** Return the ID. *)
   method get_device_drivers : unit -> osinfo_device_driver list
   (** Return the list of device drivers. *)
+  method get_devices : unit -> osinfo_device list
+  (** Return the list of devices. *)
 end
 (** Minimal OsinfoOs wrapper. *)
 
-- 
2.31.1