scpi: make the scpi_dev_inst_new more generic

This commit is contained in:
Aurelien Jacobs 2014-01-12 00:05:02 +01:00
parent c3515cea44
commit f754c14691
6 changed files with 172 additions and 171 deletions

View File

@ -65,42 +65,48 @@ static int parse_strict_bool(const char *str, gboolean *ret)
return SR_ERR; return SR_ERR;
} }
SR_PRIV extern const struct sr_scpi_dev_inst scpi_serial_dev;
SR_PRIV extern const struct sr_scpi_dev_inst scpi_tcp_dev;
SR_PRIV extern const struct sr_scpi_dev_inst scpi_usbtmc_dev;
SR_PRIV extern const struct sr_scpi_dev_inst scpi_vxi_dev;
static const struct sr_scpi_dev_inst *scpi_devs[] = {
&scpi_tcp_dev,
&scpi_usbtmc_dev,
#ifdef HAVE_RPC
&scpi_vxi_dev,
#endif
#ifdef HAVE_LIBSERIALPORT
&scpi_serial_dev, /* must be last as it matches any resource */
#endif
};
SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(const char *resource, SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(const char *resource,
const char *serialcomm) const char *serialcomm)
{ {
struct sr_scpi_dev_inst *scpi = NULL; struct sr_scpi_dev_inst *scpi = NULL;
const char *usbtmc_prefix = "/dev/usbtmc"; const struct sr_scpi_dev_inst *scpi_dev;
const char *tcp_prefix = "tcp/"; gchar **params;
const char *vxi_prefix = "vxi/"; unsigned i;
gchar **tokens, *address, *port, *instrument;
if (strncmp(resource, usbtmc_prefix, strlen(usbtmc_prefix)) == 0) { for (i = 0; i < ARRAY_SIZE(scpi_devs); i++) {
sr_dbg("Opening USBTMC device %s.", resource); scpi_dev = scpi_devs[i];
scpi = scpi_usbtmc_dev_inst_new(resource); if (!strncmp(resource, scpi_dev->prefix, strlen(scpi_dev->prefix))) {
} else if (strncmp(resource, tcp_prefix, strlen(tcp_prefix)) == 0) { sr_dbg("Opening %s device %s.", scpi_dev->name, resource);
sr_dbg("Opening TCP connection %s.", resource); scpi = g_malloc(sizeof(*scpi));
tokens = g_strsplit(resource + strlen(tcp_prefix), "/", 0); *scpi = *scpi_dev;
address = tokens[0]; scpi->priv = g_malloc0(scpi->priv_size);
port = tokens[1]; params = g_strsplit(resource, "/", 0);
if (address && port && !tokens[2]) if (scpi->dev_inst_new(scpi->priv, resource,
scpi = scpi_tcp_dev_inst_new(address, port); params, serialcomm) != SR_OK) {
else sr_scpi_free(scpi);
sr_err("Invalid parameters."); scpi = NULL;
g_strfreev(tokens); }
} else if (HAVE_RPC && !strncmp(resource, vxi_prefix, strlen(vxi_prefix))) { g_strfreev(params);
sr_dbg("Opening VXI connection %s.", resource); break;
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; return scpi;
} }
@ -264,6 +270,7 @@ SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi)
SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi) SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi)
{ {
scpi->free(scpi->priv); scpi->free(scpi->priv);
g_free(scpi->priv);
g_free(scpi); g_free(scpi);
} }

View File

@ -34,6 +34,19 @@ struct scpi_serial {
char last_character; char last_character;
}; };
static int scpi_serial_dev_inst_new(void *priv, const char *resource,
char **params, const char *serialcomm)
{
struct scpi_serial *sscpi = priv;
(void)params;
if (!(sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm)))
return SR_ERR;
return SR_OK;
}
SR_PRIV int scpi_serial_open(void *priv) SR_PRIV int scpi_serial_open(void *priv)
{ {
struct scpi_serial *sscpi = priv; struct scpi_serial *sscpi = priv;
@ -184,46 +197,29 @@ SR_PRIV int scpi_serial_read_complete(void *priv)
static int scpi_serial_close(void *priv) static int scpi_serial_close(void *priv)
{ {
struct scpi_serial *sscpi = priv; struct scpi_serial *sscpi = priv;
struct sr_serial_dev_inst *serial = sscpi->serial;
return serial_close(serial); return serial_close(sscpi->serial);
} }
static void scpi_serial_free(void *priv) static void scpi_serial_free(void *priv)
{ {
struct scpi_serial *sscpi = priv; struct scpi_serial *sscpi = priv;
struct sr_serial_dev_inst *serial = sscpi->serial;
sr_serial_dev_inst_free(serial); sr_serial_dev_inst_free(sscpi->serial);
g_free(sscpi);
} }
SR_PRIV struct sr_scpi_dev_inst *scpi_serial_dev_inst_new(const char *port, SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = {
const char *serialcomm) .name = "serial",
{ .prefix = "",
struct sr_scpi_dev_inst *scpi; .priv_size = sizeof(struct scpi_serial),
struct scpi_serial *sscpi; .dev_inst_new = scpi_serial_dev_inst_new,
struct sr_serial_dev_inst *serial; .open = scpi_serial_open,
.source_add = scpi_serial_source_add,
if (!(serial = sr_serial_dev_inst_new(port, serialcomm))) .source_remove = scpi_serial_source_remove,
return NULL; .send = scpi_serial_send,
.read_begin = scpi_serial_read_begin,
sscpi = g_malloc(sizeof(struct scpi_serial)); .read_data = scpi_serial_read_data,
.read_complete = scpi_serial_read_complete,
sscpi->serial = serial; .close = scpi_serial_close,
.free = scpi_serial_free,
scpi = g_malloc(sizeof(struct sr_scpi_dev_inst)); };
scpi->open = scpi_serial_open;
scpi->source_add = scpi_serial_source_add;
scpi->source_remove = scpi_serial_source_remove;
scpi->send = scpi_serial_send;
scpi->read_begin = scpi_serial_read_begin;
scpi->read_data = scpi_serial_read_data;
scpi->read_complete = scpi_serial_read_complete;
scpi->close = scpi_serial_close;
scpi->free = scpi_serial_free;
scpi->priv = sscpi;
return scpi;
}

View File

@ -51,6 +51,26 @@ struct scpi_tcp {
int response_bytes_read; int response_bytes_read;
}; };
static int scpi_tcp_dev_inst_new(void *priv, const char *resource,
char **params, const char *serialcomm)
{
struct scpi_tcp *tcp = priv;
(void)resource;
(void)serialcomm;
if (!params || !params[1] || !params[2]) {
sr_err("Invalid parameters.");
return SR_ERR;
}
tcp->address = g_strdup(params[1]);
tcp->port = g_strdup(params[2]);
tcp->socket = -1;
return SR_OK;
}
SR_PRIV int scpi_tcp_open(void *priv) SR_PRIV int scpi_tcp_open(void *priv)
{ {
struct scpi_tcp *tcp = priv; struct scpi_tcp *tcp = priv;
@ -205,32 +225,20 @@ SR_PRIV void scpi_tcp_free(void *priv)
g_free(tcp->address); g_free(tcp->address);
g_free(tcp->port); g_free(tcp->port);
g_free(tcp);
} }
SR_PRIV struct sr_scpi_dev_inst *scpi_tcp_dev_inst_new(const char *address, SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_dev = {
const char *port) .name = "TCP",
{ .prefix = "tcp",
struct sr_scpi_dev_inst *scpi; .priv_size = sizeof(struct scpi_tcp),
struct scpi_tcp *tcp; .dev_inst_new = scpi_tcp_dev_inst_new,
.open = scpi_tcp_open,
scpi = g_malloc(sizeof(struct sr_scpi_dev_inst)); .source_add = scpi_tcp_source_add,
tcp = g_malloc0(sizeof(struct scpi_tcp)); .source_remove = scpi_tcp_source_remove,
.send = scpi_tcp_send,
tcp->address = g_strdup(address); .read_begin = scpi_tcp_read_begin,
tcp->port = g_strdup(port); .read_data = scpi_tcp_read_data,
tcp->socket = -1; .read_complete = scpi_tcp_read_complete,
.close = scpi_tcp_close,
scpi->open = scpi_tcp_open; .free = scpi_tcp_free,
scpi->source_add = scpi_tcp_source_add; };
scpi->source_remove = scpi_tcp_source_remove;
scpi->send = scpi_tcp_send;
scpi->read_begin = scpi_tcp_read_begin;
scpi->read_data = scpi_tcp_read_data;
scpi->read_complete = scpi_tcp_read_complete;
scpi->close = scpi_tcp_close;
scpi->free = scpi_tcp_free;
scpi->priv = tcp;
return scpi;
}

View File

@ -37,6 +37,20 @@ struct usbtmc_scpi {
int response_bytes_read; int response_bytes_read;
}; };
static int scpi_usbtmc_dev_inst_new(void *priv, const char *resource,
char **params, const char *serialcomm)
{
struct usbtmc_scpi *uscpi = priv;
(void)params;
(void)serialcomm;
if (!(uscpi->usbtmc = sr_usbtmc_dev_inst_new(resource)))
return SR_ERR;
return SR_OK;
}
SR_PRIV int scpi_usbtmc_open(void *priv) SR_PRIV int scpi_usbtmc_open(void *priv)
{ {
struct usbtmc_scpi *uscpi = priv; struct usbtmc_scpi *uscpi = priv;
@ -147,9 +161,8 @@ SR_PRIV int scpi_usbtmc_read_complete(void *priv)
SR_PRIV int scpi_usbtmc_close(void *priv) SR_PRIV int scpi_usbtmc_close(void *priv)
{ {
struct usbtmc_scpi *uscpi = priv; struct usbtmc_scpi *uscpi = priv;
struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
if (close(usbtmc->fd) < 0) if (close(uscpi->usbtmc->fd) < 0)
return SR_ERR; return SR_ERR;
return SR_OK; return SR_OK;
@ -158,37 +171,22 @@ SR_PRIV int scpi_usbtmc_close(void *priv)
static void scpi_usbtmc_free(void *priv) static void scpi_usbtmc_free(void *priv)
{ {
struct usbtmc_scpi *uscpi = priv; struct usbtmc_scpi *uscpi = priv;
struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
g_free(uscpi); sr_usbtmc_dev_inst_free(uscpi->usbtmc);
sr_usbtmc_dev_inst_free(usbtmc);
} }
SR_PRIV struct sr_scpi_dev_inst *scpi_usbtmc_dev_inst_new(const char *device) SR_PRIV const struct sr_scpi_dev_inst scpi_usbtmc_dev = {
{ .name = "USBTMC",
struct sr_scpi_dev_inst *scpi; .prefix = "/dev/usbtmc",
struct usbtmc_scpi *uscpi; .priv_size = sizeof(struct usbtmc_scpi),
struct sr_usbtmc_dev_inst *usbtmc; .dev_inst_new = scpi_usbtmc_dev_inst_new,
.open = scpi_usbtmc_open,
if (!(usbtmc = sr_usbtmc_dev_inst_new(device))) .source_add = scpi_usbtmc_source_add,
return NULL; .source_remove = scpi_usbtmc_source_remove,
.send = scpi_usbtmc_send,
uscpi = g_malloc(sizeof(struct usbtmc_scpi)); .read_begin = scpi_usbtmc_read_begin,
.read_data = scpi_usbtmc_read_data,
uscpi->usbtmc = usbtmc; .read_complete = scpi_usbtmc_read_complete,
.close = scpi_usbtmc_close,
scpi = g_malloc(sizeof(struct sr_scpi_dev_inst)); .free = scpi_usbtmc_free,
};
scpi->open = scpi_usbtmc_open;
scpi->source_add = scpi_usbtmc_source_add;
scpi->source_remove = scpi_usbtmc_source_remove;
scpi->send = scpi_usbtmc_send;
scpi->read_begin = scpi_usbtmc_read_begin;
scpi->read_data = scpi_usbtmc_read_data;
scpi->read_complete = scpi_usbtmc_read_complete;
scpi->close = scpi_usbtmc_close;
scpi->free = scpi_usbtmc_free;
scpi->priv = uscpi;
return scpi;
}

View File

@ -37,6 +37,25 @@ struct scpi_vxi {
unsigned int read_complete; unsigned int read_complete;
}; };
static int scpi_vxi_dev_inst_new(void *priv, const char *resource,
char **params, const char *serialcomm)
{
struct scpi_vxi *vxi = priv;
(void)resource;
(void)serialcomm;
if (!params || !params[1]) {
sr_err("Invalid parameters.");
return SR_ERR;
}
vxi->address = g_strdup(params[1]);
vxi->instrument = g_strdup(params[2] ? params[2] : "inst0");
return SR_OK;
}
static int scpi_vxi_open(void *priv) static int scpi_vxi_open(void *priv)
{ {
struct scpi_vxi *vxi = priv; struct scpi_vxi *vxi = priv;
@ -193,31 +212,20 @@ static void scpi_vxi_free(void *priv)
g_free(vxi->address); g_free(vxi->address);
g_free(vxi->instrument); g_free(vxi->instrument);
g_free(vxi);
} }
SR_PRIV struct sr_scpi_dev_inst *scpi_vxi_dev_inst_new(const char *address, SR_PRIV const struct sr_scpi_dev_inst scpi_vxi_dev = {
const char *instrument) .name = "VXI",
{ .prefix = "vxi",
struct sr_scpi_dev_inst *scpi; .priv_size = sizeof(struct scpi_vxi),
struct scpi_vxi *vxi; .dev_inst_new = scpi_vxi_dev_inst_new,
.open = scpi_vxi_open,
scpi = g_malloc(sizeof(struct sr_scpi_dev_inst)); .source_add = scpi_vxi_source_add,
vxi = g_malloc0(sizeof(struct scpi_vxi)); .source_remove = scpi_vxi_source_remove,
.send = scpi_vxi_send,
vxi->address = g_strdup(address); .read_begin = scpi_vxi_read_begin,
vxi->instrument = g_strdup(instrument ? instrument : "inst0"); .read_data = scpi_vxi_read_data,
.read_complete = scpi_vxi_read_complete,
scpi->open = scpi_vxi_open; .close = scpi_vxi_close,
scpi->source_add = scpi_vxi_source_add; .free = scpi_vxi_free,
scpi->source_remove = scpi_vxi_source_remove; };
scpi->send = scpi_vxi_send;
scpi->read_begin = scpi_vxi_read_begin;
scpi->read_data = scpi_vxi_read_data;
scpi->read_complete = scpi_vxi_read_complete;
scpi->close = scpi_vxi_close;
scpi->free = scpi_vxi_free;
scpi->priv = vxi;
return scpi;
}

View File

@ -374,6 +374,11 @@ struct sr_scpi_hw_info {
}; };
struct sr_scpi_dev_inst { struct sr_scpi_dev_inst {
const char *name;
const char *prefix;
int priv_size;
int (*dev_inst_new)(void *priv, const char *resource, char **params,
const char *serialcomm);
int (*open)(void *priv); int (*open)(void *priv);
int (*source_add)(void *priv, int events, int (*source_add)(void *priv, int events,
int timeout, sr_receive_data_callback_t cb, void *cb_data); int timeout, sr_receive_data_callback_t cb, void *cb_data);
@ -422,27 +427,6 @@ SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi,
struct sr_scpi_hw_info **scpi_response); struct sr_scpi_hw_info **scpi_response);
SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info); SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info);
/*--- hardware/common/scpi_vxi.c --------------------------------------------*/
SR_PRIV struct sr_scpi_dev_inst *scpi_vxi_dev_inst_new(const char *address,
const char *instrument);
/*--- hardware/common/scpi_serial.c -----------------------------------------*/
#ifdef HAVE_LIBSERIALPORT
SR_PRIV struct sr_scpi_dev_inst *scpi_serial_dev_inst_new(const char *port,
const char *serialcomm);
#endif
/*--- hardware/common/scpi_tcp.c --------------------------------------------*/
SR_PRIV struct sr_scpi_dev_inst *scpi_tcp_dev_inst_new(const char *address,
const char *port);
/*--- hardware/common/scpi_usbtmc.c -----------------------------------------*/
SR_PRIV struct sr_scpi_dev_inst *scpi_usbtmc_dev_inst_new(const char *device);
/*--- hardware/common/dmm/es519xx.c -----------------------------------------*/ /*--- hardware/common/dmm/es519xx.c -----------------------------------------*/
/** /**