]> sigrok.org Git - libsigrok.git/commitdiff
Create & use new sr_usbtmc_dev_inst for Rigol DS driver.
authorMartin Ling <redacted>
Fri, 29 Nov 2013 00:48:42 +0000 (00:48 +0000)
committerMartin Ling <redacted>
Fri, 29 Nov 2013 01:13:22 +0000 (01:13 +0000)
device.c
hardware/rigol-ds/api.c
hardware/rigol-ds/protocol.c
libsigrok-internal.h
libsigrok.h
std.c

index cf2fd8781dcdfeebd5a9d82daa04bc6599b0e5df..0167a220cc390b289bbfd121e650c5a32f4cb74a 100644 (file)
--- a/device.c
+++ b/device.c
@@ -350,9 +350,34 @@ SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
        g_free(serial->serialcomm);
        g_free(serial);
 }
-
 #endif
 
+SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
+{
+       struct sr_usbtmc_dev_inst *usbtmc;
+
+       if (!device) {
+               sr_err("Device name required.");
+               return NULL;
+       }
+
+       if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) {
+               sr_err("USBTMC device instance malloc failed.");
+               return NULL;
+       }
+
+       usbtmc->device = g_strdup(device);
+       usbtmc->fd = -1;
+
+       return usbtmc;
+}
+
+SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
+{
+       g_free(usbtmc->device);
+       g_free(usbtmc);
+}
+
 /**
  * Get the list of devices/instances of the specified driver.
  *
index 0f997ebb01da02734b4a83b614c7ac6d15641aca..31342c23321280add9dd87be37ff033009ae713b 100644 (file)
@@ -217,7 +217,7 @@ static int probe_port(const char *port, GSList **devices)
 {
        struct dev_context *devc;
        struct sr_dev_inst *sdi;
-       struct sr_serial_dev_inst *serial;
+       struct sr_usbtmc_dev_inst *usbtmc;
        struct sr_probe *probe;
        unsigned int i;
        int len, num_tokens;
@@ -227,17 +227,17 @@ static int probe_port(const char *port, GSList **devices)
        gchar **tokens, *channel_name;
 
        *devices = NULL;
-       if (!(serial = sr_serial_dev_inst_new(port, NULL)))
+       if (!(usbtmc = sr_usbtmc_dev_inst_new(port)))
                return SR_ERR_MALLOC;
 
-       if (serial_open(serial, SERIAL_RDWR) != SR_OK)
+       if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
                return SR_ERR;
-       len = serial_write(serial, "*IDN?", 5);
-       len = serial_read(serial, buf, sizeof(buf));
-       if (serial_close(serial) != SR_OK)
+       len = write(usbtmc->fd, "*IDN?", 5);
+       len = read(usbtmc->fd, buf, sizeof(buf));
+       if (close(usbtmc->fd) < 0)
                return SR_ERR;
 
-       sr_serial_dev_inst_free(serial);
+       sr_usbtmc_dev_inst_free(usbtmc);
 
        if (len == 0)
                return SR_ERR_NA;
@@ -277,10 +277,10 @@ static int probe_port(const char *port, GSList **devices)
 
        g_strfreev(tokens);
 
-       if (!(sdi->conn = sr_serial_dev_inst_new(port, NULL)))
+       if (!(sdi->conn = sr_usbtmc_dev_inst_new(port)))
                return SR_ERR_MALLOC;
        sdi->driver = di;
-       sdi->inst_type = SR_INST_SERIAL;
+       sdi->inst_type = SR_INST_USBTMC;
 
        if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
                return SR_ERR_MALLOC;
@@ -399,8 +399,9 @@ static GSList *dev_list(void)
 
 static int dev_open(struct sr_dev_inst *sdi)
 {
+       struct sr_usbtmc_dev_inst *usbtmc = sdi->conn;
 
-       if (serial_open(sdi->conn, SERIAL_RDWR) != SR_OK)
+       if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
                return SR_ERR;
 
        if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
@@ -413,11 +414,12 @@ static int dev_open(struct sr_dev_inst *sdi)
 
 static int dev_close(struct sr_dev_inst *sdi)
 {
-       struct sr_serial_dev_inst *serial;
+       struct sr_usbtmc_dev_inst *usbtmc;
 
-       serial = sdi->conn;
-       if (serial && serial->fd != -1) {
-               serial_close(serial);
+       usbtmc = sdi->conn;
+       if (usbtmc && usbtmc->fd != -1) {
+               close(usbtmc->fd);
+               usbtmc->fd = -1;
                sdi->status = SR_ST_INACTIVE;
        }
 
@@ -737,7 +739,7 @@ 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 sr_usbtmc_dev_inst *usbtmc;
        struct dev_context *devc;
        struct sr_probe *probe;
        GSList *l;
@@ -746,7 +748,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       serial = sdi->conn;
+       usbtmc = sdi->conn;
        devc = sdi->priv;
 
        if (devc->data_source == DATA_SOURCE_LIVE) {
@@ -792,7 +794,7 @@ 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(serial->fd, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
+       sr_source_add(usbtmc->fd, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
 
        /* Send header packet to the session bus. */
        std_session_send_df_header(cb_data, LOG_PREFIX);
@@ -843,7 +845,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;
+       struct sr_usbtmc_dev_inst *usbtmc;
 
        (void)cb_data;
 
@@ -858,8 +860,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;
-       serial = sdi->conn;
-       sr_source_remove(serial->fd);
+       usbtmc = sdi->conn;
+       sr_source_remove(usbtmc->fd);
 
        return SR_OK;
 }
index afcbf4a2f904a3292f3258c4cbcf68b201dc590e..e8f611bcfc3ac3822ee7767b89536fc7a9819593 100644 (file)
@@ -346,13 +346,13 @@ SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi)
 }
 
 /* Read the header of a data block */
-static int rigol_ds_read_header(struct sr_serial_dev_inst *serial)
+static int rigol_ds_read_header(struct sr_usbtmc_dev_inst *usbtmc)
 {
        char start[3], length[10];
        int len, tmp;
 
        /* Read the hashsign and length digit. */
-       tmp = serial_read(serial, start, 2);
+       tmp = read(usbtmc->fd, start, 2);
        start[2] = '\0';
        if (tmp != 2)
        {
@@ -367,7 +367,7 @@ static int rigol_ds_read_header(struct sr_serial_dev_inst *serial)
        len = atoi(start + 1);
 
        /* Read the data length. */
-       tmp = serial_read(serial, length, len);
+       tmp = read(usbtmc->fd, length, len);
        length[len] = '\0';
        if (tmp != len)
        {
@@ -388,7 +388,7 @@ static int rigol_ds_read_header(struct sr_serial_dev_inst *serial)
 SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
 {
        struct sr_dev_inst *sdi;
-       struct sr_serial_dev_inst *serial;
+       struct sr_usbtmc_dev_inst *usbtmc;
        struct dev_context *devc;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
@@ -405,7 +405,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
        if (!(devc = sdi->priv))
                return TRUE;
 
-       serial = sdi->conn;
+       usbtmc = sdi->conn;
 
        if (revents == G_IO_IN) {
                if (devc->model->protocol == PROTOCOL_IEEE488_2) {
@@ -442,7 +442,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                                sr_dbg("New block header expected");
                                if (rigol_ds_send(sdi, ":WAV:DATA?") != SR_OK)
                                        return TRUE;
-                               len = rigol_ds_read_header(serial);
+                               len = rigol_ds_read_header(usbtmc);
                                if (len == -1)
                                        return TRUE;
                                /* At slow timebases in live capture the DS2072
@@ -454,7 +454,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                                if (devc->data_source == DATA_SOURCE_LIVE
                                    && (unsigned)len < devc->num_frame_bytes) {
                                        sr_dbg("Discarding short data block");
-                                       serial_read(serial, devc->buffer, len + 1);
+                                       read(usbtmc->fd, devc->buffer, len + 1);
                                        return TRUE;
                                }
                                devc->num_block_bytes = len;
@@ -465,12 +465,12 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                probe = devc->channel_frame;
                if (devc->model->protocol == PROTOCOL_IEEE488_2) {
                        len = devc->num_block_bytes - devc->num_block_read;
-                       len = serial_read(serial, devc->buffer,
+                       len = read(usbtmc->fd, devc->buffer,
                                        len < ACQ_BUFFER_SIZE ? len : ACQ_BUFFER_SIZE);
                } else {
                        waveform_size = probe->type == SR_PROBE_ANALOG ?
                                        DS1000_ANALOG_LIVE_WAVEFORM_SIZE : DIGITAL_WAVEFORM_SIZE;
-                       len = serial_read(serial, devc->buffer, waveform_size - devc->num_frame_bytes);
+                       len = read(usbtmc->fd, devc->buffer, waveform_size - devc->num_frame_bytes);
                }
                sr_dbg("Received %d bytes.", len);
                if (len == -1)
@@ -510,7 +510,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
                                        sr_dbg("Block has been completed");
                                        /* Discard the terminating linefeed and prepare for
                                           possible next block */
-                                       serial_read(serial, devc->buffer, 1);
+                                       read(usbtmc->fd, devc->buffer, 1);
                                        devc->num_block_bytes = 0;
                                        if (devc->data_source != DATA_SOURCE_LIVE)
                                                rigol_ds_set_wait_event(devc, WAIT_BLOCK);
@@ -611,13 +611,14 @@ SR_PRIV int rigol_ds_send(const struct sr_dev_inst *sdi, const char *format, ...
        va_list args;
        char buf[256];
        int len, out, ret;
+       struct sr_usbtmc_dev_inst *usbtmc = sdi->conn;
 
        va_start(args, format);
        len = vsnprintf(buf, 255, format, args);
        va_end(args);
        strcat(buf, "\n");
        len++;
-       out = serial_write(sdi->conn, buf, len);
+       out = write(usbtmc->fd, buf, len);
        buf[len - 1] = '\0';
        if (out != len) {
                sr_dbg("Only sent %d/%d bytes of '%s'.", out, len, buf);
@@ -634,11 +635,12 @@ static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply, size_t
 {
        int len;
        struct dev_context *devc = sdi->priv;
+       struct sr_usbtmc_dev_inst *usbtmc = sdi->conn;
 
        if (rigol_ds_send(sdi, cmd) != SR_OK)
                return SR_ERR;
 
-       if ((len = serial_read(sdi->conn, reply, maxlen - 1)) < 0)
+       if ((len = read(usbtmc->fd, reply, maxlen - 1)) < 0)
                return SR_ERR;
        reply[len] = '\0';
 
index f742e127518233c3304d7459a7496e660ee07866..376e657e05c59e7d7c6617bb6dffed810bfa5015 100644 (file)
@@ -79,6 +79,11 @@ struct sr_serial_dev_inst {
 };
 #endif
 
+struct sr_usbtmc_dev_inst {
+       char *device;
+       int fd;
+};
+
 /* Private driver context. */
 struct drv_context {
        struct sr_context *sr_ctx;
@@ -119,6 +124,9 @@ SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial);
 #endif
 
+/* USBTMC-specific instances */
+SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device);
+SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc);
 
 /*--- hwdriver.c ------------------------------------------------------------*/
 
index f0f6d0cc60085053e49207eaba1590265512461f..1e45a5e1a1d4828f1a579490a43afc440a7c5a20 100644 (file)
@@ -803,6 +803,8 @@ enum {
        SR_INST_USB = 10000,
        /** Device instance type for serial port devices. */
        SR_INST_SERIAL,
+       /** Device instance type for USBTMC devices. */
+       SR_INST_USBTMC,
 };
 
 /** Device instance status. */
diff --git a/std.c b/std.c
index 44435be22520745d460dc66ffe4d64a80449568c..d1ff84e037c29cdb70044ac68d36c33be1ee7328 100644 (file)
--- a/std.c
+++ b/std.c
@@ -215,6 +215,8 @@ SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver,
                        if (sdi->inst_type == SR_INST_USB)
                                sr_usb_dev_inst_free(sdi->conn);
 #endif
+                       if (sdi->inst_type == SR_INST_USBTMC)
+                               sr_usbtmc_dev_inst_free(sdi->conn);
                }
                if (clear_private)
                        clear_private(sdi->priv);