]> sigrok.org Git - libsigrok.git/blobdiff - device.c
ols: Always use glib's memory allocation functions.
[libsigrok.git] / device.c
index 84cf6d1f87c48c8cfd96d839c5e9a55cb6f5a009..0cc5e55256978230eb565aa879ad22b74d31ff2e 100644 (file)
--- a/device.c
+++ b/device.c
 #include <stdio.h>
 #include <glib.h>
 #include <sigrok.h>
+#include <sigrok-internal.h>
 
-extern struct sigrok_global *global;
+extern struct sr_global *global;
 
 GSList *devices = NULL;
 
-void device_scan(void)
+void sr_device_scan(void)
 {
        GSList *plugins, *l;
-       struct device_plugin *plugin;
-       int num_devices, num_probes, i;
+       struct sr_device_plugin *plugin;
 
-       plugins = list_hwplugins();
+       plugins = sr_list_hwplugins();
 
        /*
         * Initialize all plugins first. Since the init() call may involve
@@ -40,55 +40,68 @@ void device_scan(void)
         */
        for (l = plugins; l; l = l->next) {
                plugin = l->data;
-               g_message("initializing %s plugin", plugin->name);
-               num_devices = plugin->init(NULL);
-               for (i = 0; i < num_devices; i++) {
-                       num_probes
-                         = (int)(unsigned long)plugin->get_device_info(i,
-                           DI_NUM_PROBES);
-                       device_new(plugin, i, num_probes);
-               }
+               sr_device_plugin_init(plugin);
        }
+
 }
 
-void device_close_all(void)
+int sr_device_plugin_init(struct sr_device_plugin *plugin)
 {
-       struct device *device;
+       int num_devices, num_probes, i;
+
+       sr_info("initializing %s plugin", plugin->name);
+       num_devices = plugin->init(NULL);
+       for (i = 0; i < num_devices; i++) {
+               num_probes = (int)plugin->get_device_info(i, SR_DI_NUM_PROBES);
+               sr_device_new(plugin, i, num_probes);
+       }
+
+       return num_devices;
+}
+
+void sr_device_close_all(void)
+{
+       struct sr_device *device;
 
        while (devices) {
                device = devices->data;
-               if (device->plugin)
+               if (device->plugin && device->plugin->close)
                        device->plugin->close(device->plugin_index);
-               device_destroy(device);
+               sr_device_destroy(device);
        }
 }
 
-GSList *device_list(void)
+GSList *sr_device_list(void)
 {
+
+       if (!devices)
+               sr_device_scan();
+
        return devices;
 }
 
-struct device *device_new(struct device_plugin *plugin, int plugin_index,
-                         int num_probes)
+struct sr_device *sr_device_new(struct sr_device_plugin *plugin, int plugin_index,
+                            int num_probes)
 {
-       struct device *device;
+       struct sr_device *device;
        int i;
-       char probename[16];
 
-       device = g_malloc0(sizeof(struct device));
+       if (!(device = g_try_malloc0(sizeof(struct sr_device)))) {
+               sr_err("dev: %s: device malloc failed", __func__);
+               return NULL;
+       }
+
        device->plugin = plugin;
        device->plugin_index = plugin_index;
        devices = g_slist_append(devices, device);
 
-       for (i = 0; i < num_probes; i++) {
-               snprintf(probename, 16, "%d", i + 1);
-               device_probe_add(device, probename);
-       }
+       for (i = 0; i < num_probes; i++)
+               sr_device_probe_add(device, NULL);
 
        return device;
 }
 
-void device_clear(struct device *device)
+void sr_device_clear(struct sr_device *device)
 {
        unsigned int pnum;
 
@@ -98,10 +111,10 @@ void device_clear(struct device *device)
                return;
 
        for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
-               device_probe_clear(device, pnum);
+               sr_device_probe_clear(device, pnum);
 }
 
-void device_destroy(struct device *device)
+void sr_device_destroy(struct sr_device *device)
 {
        unsigned int pnum;
 
@@ -113,17 +126,17 @@ void device_destroy(struct device *device)
        devices = g_slist_remove(devices, device);
        if (device->probes) {
                for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
-                       device_probe_clear(device, pnum);
+                       sr_device_probe_clear(device, pnum);
                g_slist_free(device->probes);
        }
        g_free(device);
 }
 
-void device_probe_clear(struct device *device, int probenum)
+void sr_device_probe_clear(struct sr_device *device, int probenum)
 {
-       struct probe *p;
+       struct sr_probe *p;
 
-       p = probe_find(device, probenum);
+       p = sr_device_probe_find(device, probenum);
        if (!p)
                return;
 
@@ -138,22 +151,36 @@ void device_probe_clear(struct device *device, int probenum)
        }
 }
 
-void device_probe_add(struct device *device, char *name)
+void sr_device_probe_add(struct sr_device *device, const char *name)
 {
-       struct probe *p;
+       struct sr_probe *p;
+       char probename[16];
+       int probenum;
 
-       p = g_malloc0(sizeof(struct probe));
-       p->index = g_slist_length(device->probes) + 1;
+       probenum = g_slist_length(device->probes) + 1;
+
+       if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) {
+               sr_err("dev: %s: p malloc failed", __func__);
+               // return SR_ERR_MALLOC;
+               return; /* FIXME: should return int. */
+       }
+
+       p->index = probenum;
        p->enabled = TRUE;
-       p->name = g_strdup(name);
+       if (name) {
+               p->name = g_strdup(name);
+       } else {
+               snprintf(probename, 16, "%d", probenum);
+               p->name = g_strdup(probename);
+       }
        p->trigger = NULL;
        device->probes = g_slist_append(device->probes, p);
 }
 
-struct probe *probe_find(struct device *device, int probenum)
+struct sr_probe *sr_device_probe_find(struct sr_device *device, int probenum)
 {
        GSList *l;
-       struct probe *p, *found_probe;
+       struct sr_probe *p, *found_probe;
 
        found_probe = NULL;
        for (l = device->probes; l; l = l->next) {
@@ -167,11 +194,13 @@ struct probe *probe_find(struct device *device, int probenum)
        return found_probe;
 }
 
-void device_probe_name(struct device *device, int probenum, char *name)
+/* TODO: return SIGROK_ERR if probenum not found */
+void sr_device_probe_name(struct sr_device *device, int probenum,
+                         const char *name)
 {
-       struct probe *p;
+       struct sr_probe *p;
 
-       p = probe_find(device, probenum);
+       p = sr_device_probe_find(device, probenum);
        if (!p)
                return;
 
@@ -180,16 +209,17 @@ void device_probe_name(struct device *device, int probenum, char *name)
        p->name = g_strdup(name);
 }
 
-void device_trigger_clear(struct device *device)
+/* TODO: return SIGROK_ERR if probenum not found */
+void sr_device_trigger_clear(struct sr_device *device)
 {
-       struct probe *p;
+       struct sr_probe *p;
        unsigned int pnum;
 
        if (!device->probes)
                return;
 
        for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
-               p = probe_find(device, pnum);
+               p = sr_device_probe_find(device, pnum);
                if (p && p->trigger) {
                        g_free(p->trigger);
                        p->trigger = NULL;
@@ -197,11 +227,13 @@ void device_trigger_clear(struct device *device)
        }
 }
 
-void device_trigger_set(struct device *device, int probenum, char *trigger)
+/* TODO: return SIGROK_ERR if probenum not found */
+void sr_device_trigger_set(struct sr_device *device, int probenum,
+                          const char *trigger)
 {
-       struct probe *p;
+       struct sr_probe *p;
 
-       p = probe_find(device, probenum);
+       p = sr_device_probe_find(device, probenum);
        if (!p)
                return;
 
@@ -209,4 +241,20 @@ void device_trigger_set(struct device *device, int probenum, char *trigger)
                g_free(p->trigger);
 
        p->trigger = g_strdup(trigger);
+
+}
+
+gboolean sr_device_has_hwcap(struct sr_device *device, int hwcap)
+{
+       int *capabilities, i;
+
+       if (!device || !device->plugin)
+               return FALSE;
+
+       if ((capabilities = device->plugin->get_capabilities()))
+               for (i = 0; capabilities[i]; i++)
+                       if (capabilities[i] == hwcap)
+                               return TRUE;
+
+       return FALSE;
 }