From: Aurelien Jacobs Date: Sat, 11 Jan 2014 16:56:15 +0000 (+0100) Subject: scpi: factorize dev_inst_new calls out of individual drivers X-Git-Tag: libsigrok-0.3.0~312 X-Git-Url: http://sigrok.org/gitweb/?a=commitdiff_plain;h=c3515cea44f7c3044fa56570e8d3225148c36a8f;p=libsigrok.git scpi: factorize dev_inst_new calls out of individual drivers --- diff --git a/hardware/common/scpi.c b/hardware/common/scpi.c index b2eb4ced..80287330 100644 --- a/hardware/common/scpi.c +++ b/hardware/common/scpi.c @@ -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. * diff --git a/hardware/hameg-hmo/api.c b/hardware/hameg-hmo/api.c index ee7c7443..bf3b868c 100644 --- a/hardware/hameg-hmo/api.c +++ b/hardware/hameg-hmo/api.c @@ -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); diff --git a/hardware/rigol-ds/api.c b/hardware/rigol-ds/api.c index 9007ea18..bea79c3e 100644 --- a/hardware/rigol-ds/api.c +++ b/hardware/rigol-ds/api.c @@ -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."); diff --git a/libsigrok-internal.h b/libsigrok-internal.h index e99e5473..d72c4dc5 100644 --- a/libsigrok-internal.h +++ b/libsigrok-internal.h @@ -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);