]> sigrok.org Git - libsigrok.git/blobdiff - hardware/rigol-ds1xx2/api.c
s/DRIVER_LOG_DOMAIN/LOG_PREFIX/.
[libsigrok.git] / hardware / rigol-ds1xx2 / api.c
index 55e9bae65a0bfa4b45251617cdf64601dd07d127..34081f3db8cf4fe67de51f54d62b610cf56e1901 100644 (file)
@@ -154,16 +154,16 @@ static int clear_instances(void)
        for (l = drvc->instances; l; l = l->next) {
                if (!(sdi = l->data))
                        continue;
-               if (!(devc = sdi->priv))
-                       continue;
 
-               g_free(devc->device);
-               g_free(devc->coupling[0]);
-               g_free(devc->coupling[1]);
-               g_free(devc->trigger_source);
-               g_free(devc->trigger_slope);
-               close(devc->fd);
+               if (sdi->conn)
+                       sr_serial_dev_inst_free(sdi->conn);
 
+               if ((devc = sdi->priv)) {
+                       g_free(devc->coupling[0]);
+                       g_free(devc->coupling[1]);
+                       g_free(devc->trigger_source);
+                       g_free(devc->trigger_slope);
+               }
                sr_dev_inst_free(sdi);
        }
 
@@ -175,16 +175,13 @@ static int clear_instances(void)
 
 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
 {
-       struct dev_context *devc;
        va_list args;
        char buf[256];
 
-       devc = sdi->priv;
-
        va_start(args, format);
        vsnprintf(buf, 255, format, args);
        va_end(args);
-       if (rigol_ds1xx2_send(devc, buf) != SR_OK)
+       if (rigol_ds1xx2_send(sdi, buf) != SR_OK)
                return SR_ERR;
 
        /* When setting a bunch of parameters in a row, the DS1052E scrambles
@@ -197,36 +194,41 @@ static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
 
 static int hw_init(struct sr_context *sr_ctx)
 {
-       return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
+       return std_hw_init(sr_ctx, di, LOG_PREFIX);
 }
 
-static int probe_device(struct drv_context *drvc, const char *device)
+static int probe_port(const char *port, GSList **devices)
 {
-       const gchar *idn_query = "*IDN?";
+       struct dev_context *devc;
+       struct sr_dev_inst *sdi;
+       struct sr_serial_dev_inst *serial;
+       struct sr_probe *probe;
        unsigned int i;
-       int len, num_tokens, fd;
-       const gchar *delimiter = ",";
-       gchar **tokens;
+       int len, num_tokens;
+       gboolean matched, has_digital;
        const char *manufacturer, *model, *version;
-       gboolean matched = FALSE;
-       struct sr_dev_inst *sdi;
-       struct dev_context *devc;
-       gboolean has_digital = FALSE;
        char buf[256];
-       gchar *channel_name;
-       struct sr_probe *probe;
+       gchar **tokens, *channel_name;
+
+       *devices = NULL;
+       if (!(serial = sr_serial_dev_inst_new(port, NULL)))
+               return SR_ERR_MALLOC;
+
+       if (serial_open(serial, SERIAL_RDWR) != SR_OK)
+               return SR_ERR;
+       len = serial_write(serial, "*IDN?", 5);
+       len = serial_read(serial, buf, sizeof(buf));
+       if (serial_close(serial) != SR_OK)
+               return SR_ERR;
+
+       sr_serial_dev_inst_free(serial);
 
-       fd = open(device, O_RDWR);
-       len = write(fd, idn_query, strlen(idn_query));
-       len = read(fd, buf, sizeof(buf));
-       close(fd);
        if (len == 0)
                return SR_ERR_NA;
 
        buf[len] = 0;
-       tokens = g_strsplit(buf, delimiter, 0);
-       close(fd);
-       sr_dbg("response: %s %d [%s]", device, len, buf);
+       tokens = g_strsplit(buf, ",", 0);
+       sr_dbg("response: %s [%s]", port, buf);
 
        for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
 
@@ -244,9 +246,10 @@ static int probe_device(struct drv_context *drvc, const char *device)
                return SR_ERR_NA;
        }
 
+       matched = has_digital = FALSE;
        for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
                if (!strcmp(model, supported_models[i])) {
-                       matched = 1;
+                       matched = TRUE;
                        has_digital = g_str_has_suffix(model, "D");
                        break;
                }
@@ -260,16 +263,14 @@ static int probe_device(struct drv_context *drvc, const char *device)
 
        g_strfreev(tokens);
 
-       if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
+       if (!(sdi->conn = sr_serial_dev_inst_new(port, NULL)))
                return SR_ERR_MALLOC;
+       sdi->driver = di;
 
-       devc->limit_frames = 0;
-       if (!(devc->device = g_strdup(device)))
+       if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
                return SR_ERR_MALLOC;
+       devc->limit_frames = 0;
        devc->has_digital = has_digital;
-       sdi->priv = devc;
-       sdi->driver = di;
-       drvc->instances = g_slist_append(drvc->instances, sdi);
 
        for (i = 0; i < 2; i++) {
                if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
@@ -289,6 +290,9 @@ static int probe_device(struct drv_context *drvc, const char *device)
                        sdi->probes = g_slist_append(sdi->probes, probe);
                }
        }
+       sdi->priv = devc;
+
+       *devices = g_slist_append(NULL, sdi);
 
        return SR_OK;
 }
@@ -296,52 +300,48 @@ static int probe_device(struct drv_context *drvc, const char *device)
 static GSList *hw_scan(GSList *options)
 {
        struct drv_context *drvc;
-       GSList *l, *devices;
        struct sr_config *src;
+       GSList *l, *devices;
        GDir *dir;
-       const gchar *dev_name;
-       const gchar *dev_dir = "/dev/";
-       const gchar *prefix = "usbtmc";
-       gchar *device = NULL;
        int ret;
+       const gchar *dev_name;
+       gchar *port = NULL;
 
        drvc = di->priv;
-       drvc->instances = NULL;
 
        for (l = options; l; l = l->next) {
                src = l->data;
                if (src->key == SR_CONF_CONN) {
-                       device = g_variant_get_string(src->data, NULL);
+                       port = (char *)g_variant_get_string(src->data, NULL);
                        break;
                }
        }
 
-       if (device) {
-               ret = probe_device(drvc, device);
-               if (ret == SR_ERR_MALLOC) {
-                       clear_instances();
+       devices = NULL;
+       if (port) {
+               if (probe_port(port, &devices) == SR_ERR_MALLOC)
                        return NULL;
-               }
        } else {
                if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
                        return NULL;
                while ((dev_name = g_dir_read_name(dir))) {
-                       if (strncmp(dev_name, prefix, strlen(prefix)))
+                       if (strncmp(dev_name, "usbtmc", 6))
                                continue;
-                       device = g_strconcat(dev_dir, dev_name, NULL);
-                       ret = probe_device(drvc, device);
-                       g_free(device);
+                       port = g_strconcat("/dev/", dev_name, NULL);
+                       ret = probe_port(port, &devices);
+                       g_free(port);
                        if (ret == SR_ERR_MALLOC) {
                                g_dir_close(dir);
-                               clear_instances();
                                return NULL;
                        }
                }
-
                g_dir_close(dir);
        }
 
-       devices = g_slist_copy(drvc->instances);
+       /* Tack a copy of the newly found devices onto the driver list. */
+       l = g_slist_copy(devices);
+       drvc->instances = g_slist_concat(drvc->instances, l);
+
        return devices;
 }
 
@@ -352,29 +352,27 @@ static GSList *hw_dev_list(void)
 
 static int hw_dev_open(struct sr_dev_inst *sdi)
 {
-       struct dev_context *devc;
-       int fd;
 
-       devc = sdi->priv;
-
-       if ((fd = open(devc->device, O_RDWR)) == -1)
+       if (serial_open(sdi->conn, SERIAL_RDWR) != SR_OK)
                return SR_ERR;
-       devc->fd = fd;
 
        if (rigol_ds1xx2_get_dev_cfg(sdi) != SR_OK)
-               /* TODO: force configuration? */
                return SR_ERR;
 
+       sdi->status = SR_ST_ACTIVE;
+
        return SR_OK;
 }
 
 static int hw_dev_close(struct sr_dev_inst *sdi)
 {
-       struct dev_context *devc;
-
-       devc = sdi->priv;
+       struct sr_serial_dev_inst *serial;
 
-       close(devc->fd);
+       serial = sdi->conn;
+       if (serial && serial->fd != -1) {
+               serial_close(serial);
+               sdi->status = SR_ST_INACTIVE;
+       }
 
        return SR_OK;
 }
@@ -416,10 +414,8 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
 
        devc = sdi->priv;
 
-       if (sdi->status != SR_ST_ACTIVE) {
-               sr_err("Device inactive, can't set config options.");
-               return SR_ERR;
-       }
+       if (sdi->status != SR_ST_ACTIVE)
+               return SR_ERR_DEV_CLOSED;
 
        ret = SR_OK;
        switch (id) {
@@ -571,13 +567,16 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
 
 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 {
+       struct sr_serial_dev_inst *serial;
        struct dev_context *devc;
        struct sr_probe *probe;
        GSList *l;
        char cmd[256];
 
-       (void)cb_data;
+       if (sdi->status != SR_ST_ACTIVE)
+               return SR_ERR_DEV_CLOSED;
 
+       serial = sdi->conn;
        devc = sdi->priv;
 
        for (l = sdi->probes; l; l = l->next) {
@@ -591,7 +590,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                                /* Enabled channel is currently disabled, or vice versa. */
                                sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
                                                probe->enabled ? "ON" : "OFF");
-                               if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
+                               if (rigol_ds1xx2_send(sdi, cmd) != SR_OK)
                                        return SR_ERR;
                        }
                } else if (probe->type == SR_PROBE_LOGIC) {
@@ -602,7 +601,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                                /* Enabled channel is currently disabled, or vice versa. */
                                sprintf(cmd, ":DIG%d:TURN %s", probe->index,
                                                probe->enabled ? "ON" : "OFF");
-                               if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
+                               if (rigol_ds1xx2_send(sdi, cmd) != SR_OK)
                                        return SR_ERR;
                        }
                }
@@ -610,20 +609,20 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
                return SR_ERR;
 
-       sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
+       sr_source_add(serial->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
 
        /* Send header packet to the session bus. */
-       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
+       std_session_send_df_header(cb_data, LOG_PREFIX);
 
        /* Fetch the first frame. */
        if (devc->enabled_analog_probes) {
                devc->channel_frame = devc->enabled_analog_probes->data;
-               if (rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%d",
+               if (rigol_ds1xx2_send(sdi, ":WAV:DATA? CHAN%d",
                                devc->channel_frame->index + 1) != SR_OK)
                        return SR_ERR;
        } else {
                devc->channel_frame = devc->enabled_digital_probes->data;
-               if (rigol_ds1xx2_send(devc, ":WAV:DATA? DIG") != SR_OK)
+               if (rigol_ds1xx2_send(sdi, ":WAV:DATA? DIG") != SR_OK)
                        return SR_ERR;
        }
 
@@ -635,6 +634,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
        struct dev_context *devc;
+       struct sr_serial_dev_inst *serial;
 
        (void)cb_data;
 
@@ -649,7 +649,8 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
        g_slist_free(devc->enabled_digital_probes);
        devc->enabled_analog_probes = NULL;
        devc->enabled_digital_probes = NULL;
-       sr_source_remove(devc->fd);
+       serial = sdi->conn;
+       sr_source_remove(serial->fd);
 
        return SR_OK;
 }