scpi: factorize dev_inst_new calls out of individual drivers

This commit is contained in:
Aurelien Jacobs 2014-01-11 17:56:15 +01:00
parent d5876cfb4a
commit c3515cea44
4 changed files with 44 additions and 42 deletions

View 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.
*

View 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);

View 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.");

View 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);