]> sigrok.org Git - libsigrok.git/commitdiff
scpi: factorize dev_inst_new calls out of individual drivers
authorAurelien Jacobs <redacted>
Sat, 11 Jan 2014 16:56:15 +0000 (17:56 +0100)
committerAurelien Jacobs <redacted>
Sat, 11 Jan 2014 21:36:46 +0000 (22:36 +0100)
hardware/common/scpi.c
hardware/hameg-hmo/api.c
hardware/rigol-ds/api.c
libsigrok-internal.h

index b2eb4ced958e37fda5f475cd6557cafed4a962e3..80287330f31d2d74d4f49be12f0a886c18f82ac6 100644 (file)
@@ -65,6 +65,45 @@ static int parse_strict_bool(const char *str, gboolean *ret)
        return SR_ERR;
 }
 
+SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(const char *resource,
+               const char *serialcomm)
+{
+       struct sr_scpi_dev_inst *scpi = NULL;
+       const char *usbtmc_prefix = "/dev/usbtmc";
+       const char *tcp_prefix = "tcp/";
+       const char *vxi_prefix = "vxi/";
+       gchar **tokens, *address, *port, *instrument;
+
+       if (strncmp(resource, usbtmc_prefix, strlen(usbtmc_prefix)) == 0) {
+               sr_dbg("Opening USBTMC device %s.", resource);
+               scpi = scpi_usbtmc_dev_inst_new(resource);
+       } else if (strncmp(resource, tcp_prefix, strlen(tcp_prefix)) == 0) {
+               sr_dbg("Opening TCP connection %s.", resource);
+               tokens = g_strsplit(resource + strlen(tcp_prefix), "/", 0);
+               address = tokens[0];
+               port = tokens[1];
+               if (address && port && !tokens[2])
+                       scpi = scpi_tcp_dev_inst_new(address, port);
+               else
+                       sr_err("Invalid parameters.");
+               g_strfreev(tokens);
+       } else if (HAVE_RPC && !strncmp(resource, vxi_prefix, strlen(vxi_prefix))) {
+               sr_dbg("Opening VXI connection %s.", resource);
+               tokens = g_strsplit(resource + strlen(tcp_prefix), "/", 0);
+               address = tokens[0];
+               instrument = tokens[1];
+               if (address && (!instrument || !tokens[2]))
+                       scpi = scpi_vxi_dev_inst_new(address, instrument);
+               else
+                       sr_err("Invalid parameters.");
+               g_strfreev(tokens);
+       } else {
+               sr_dbg("Opening serial device %s.", resource);
+               scpi = scpi_serial_dev_inst_new(resource, serialcomm);
+       }
+       return scpi;
+}
+
 /**
  * Open SCPI device.
  *
index ee7c74430b2ceb9ce63a2650b93918daa2cebaf4..bf3b868c0828610ce900f108607e1f01f664654f 100644 (file)
@@ -217,7 +217,7 @@ static struct sr_dev_inst *hmo_probe_serial_device(const char *serial_device,
        scpi = NULL;
        hw_info = NULL;
 
-       if (!(scpi = scpi_serial_dev_inst_new(serial_device, serial_options)))
+       if (!(scpi = scpi_dev_inst_new(serial_device, serial_options)))
                goto fail;
 
        sr_info("Probing %s.", serial_device);
index 9007ea18d0b70c51e534f24e1fb354df25269755..bea79c3eb5ab0c35f4065f3b84ab6a7a8b500801 100644 (file)
@@ -246,10 +246,6 @@ static int probe_port(const char *resource, const char *serialcomm, GSList **dev
 {
        struct dev_context *devc;
        struct sr_dev_inst *sdi;
-       const char *usbtmc_prefix = "/dev/usbtmc";
-       const char *tcp_prefix = "tcp/";
-       const char *vxi_prefix = "vxi/";
-       gchar **tokens, *address, *port, *instrument;
        struct sr_scpi_dev_inst *scpi;
        struct sr_scpi_hw_info *hw_info;
        struct sr_probe *probe;
@@ -259,43 +255,8 @@ static int probe_port(const char *resource, const char *serialcomm, GSList **dev
 
        *devices = NULL;
 
-       if (strncmp(resource, usbtmc_prefix, strlen(usbtmc_prefix)) == 0) {
-               sr_dbg("Opening USBTMC device %s.", resource);
-               if (!(scpi = scpi_usbtmc_dev_inst_new(resource)))
-                       return SR_ERR_MALLOC;
-       } else if (strncmp(resource, tcp_prefix, strlen(tcp_prefix)) == 0) {
-               sr_dbg("Opening TCP connection %s.", resource);
-               tokens = g_strsplit(resource + strlen(tcp_prefix), "/", 0);
-               address = tokens[0];
-               port = tokens[1];
-               if (!address || !port || tokens[2]) {
-                       sr_err("Invalid parameters.");
-                       g_strfreev(tokens);
-                       return SR_ERR_ARG;
-               }
-               scpi = scpi_tcp_dev_inst_new(address, port);
-               g_strfreev(tokens);
-               if (!scpi)
-                       return SR_ERR_MALLOC;
-       } else if (HAVE_RPC && !strncmp(resource, vxi_prefix, strlen(vxi_prefix))) {
-               sr_dbg("Opening VXI connection %s.", resource);
-               tokens = g_strsplit(resource + strlen(tcp_prefix), "/", 0);
-               address = tokens[0];
-               instrument = tokens[1];
-               if (!address) {
-                       sr_err("Invalid parameters.");
-                       g_strfreev(tokens);
-                       return SR_ERR_ARG;
-               }
-               scpi = scpi_vxi_dev_inst_new(address, instrument);
-               g_strfreev(tokens);
-               if (!scpi)
-                       return SR_ERR_MALLOC;
-       } else {
-               sr_dbg("Opening serial device %s.", resource);
-               if (!(scpi = scpi_serial_dev_inst_new(resource, serialcomm)))
-                       return SR_ERR_MALLOC;
-       }
+       if (!(scpi = scpi_dev_inst_new(resource, serialcomm)))
+               return SR_ERR;
 
        if (sr_scpi_open(scpi) != SR_OK) {
                sr_info("Couldn't open SCPI device.");
index e99e5473afdeead7a09160ee9bcd6c4727ce1e08..d72c4dc5d2b90e2c57f77243cf5492c57bc53bbe 100644 (file)
@@ -387,6 +387,8 @@ struct sr_scpi_dev_inst {
        void *priv;
 };
 
+SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(const char *resource,
+               const char *serialcomm);
 SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi);
 SR_PRIV int sr_scpi_source_add(struct sr_scpi_dev_inst *scpi, int events,
                int timeout, sr_receive_data_callback_t cb, void *cb_data);