From: Frank Stettner Date: Sat, 16 Mar 2019 22:44:38 +0000 (+0100) Subject: scpi: Add connenction_id() function to all scpi drivers. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=8107a9a650f4db08423c3fc79c1b7e7d826ffd7f;p=libsigrok.git scpi: Add connenction_id() function to all scpi drivers. --- diff --git a/src/device.c b/src/device.c index 34d80a9d..e7be567d 100644 --- a/src/device.c +++ b/src/device.c @@ -23,6 +23,7 @@ #include #include #include "libsigrok-internal.h" +#include "scpi.h" /** @cond PRIVATE */ #define LOG_PREFIX "device" @@ -808,19 +809,24 @@ SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi) #ifdef HAVE_LIBUSB_1_0 struct drv_context *drvc; int cnt, i, a, b; - char connection_id[64]; + char conn_id_usb[64]; struct sr_usb_dev_inst *usb; struct libusb_device **devlist; #endif +#ifdef HAVE_LIBSERIALPORT + struct sr_serial_dev_inst *serial; +#endif + + struct sr_scpi_dev_inst *scpi; + char *conn_id_scpi; + if (!sdi) return NULL; #ifdef HAVE_LIBSERIALPORT - struct sr_serial_dev_inst *serial; - if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) { - /* connection_id isn't populated, let's do that here. */ + /* connection_id isn't populated, let's do that for serial devices. */ serial = sdi->conn; ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port); @@ -829,7 +835,7 @@ SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi) #ifdef HAVE_LIBUSB_1_0 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) { - /* connection_id isn't populated, let's do that here. */ + /* connection_id isn't populated, let's do that for USB devices. */ drvc = sdi->driver->context; usb = sdi->conn; @@ -847,10 +853,10 @@ SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi) if (b != usb->bus || a != usb->address) continue; - if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0) + if (usb_get_port_path(devlist[i], conn_id_usb, sizeof(conn_id_usb)) < 0) continue; - ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id); + ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(conn_id_usb); break; } @@ -858,6 +864,15 @@ SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi) } #endif + if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SCPI)) { + /* connection_id isn't populated, let's do that for SCPI devices. */ + + scpi = sdi->conn; + sr_scpi_connection_id(scpi, &conn_id_scpi); + ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(conn_id_scpi); + g_free(conn_id_scpi); + } + return sdi->connection_id; } diff --git a/src/scpi.h b/src/scpi.h index eeee9a55..162c7a36 100644 --- a/src/scpi.h +++ b/src/scpi.h @@ -85,6 +85,7 @@ struct sr_scpi_dev_inst { int (*dev_inst_new)(void *priv, struct drv_context *drvc, const char *resource, char **params, const char *serialcomm); int (*open)(struct sr_scpi_dev_inst *scpi); + int (*connection_id)(struct sr_scpi_dev_inst *scpi, char **connection_id); int (*source_add)(struct sr_session *session, void *priv, int events, int timeout, sr_receive_data_callback cb, void *cb_data); int (*source_remove)(struct sr_session *session, void *priv); @@ -108,6 +109,8 @@ SR_PRIV GSList *sr_scpi_scan(struct drv_context *drvc, GSList *options, SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(struct drv_context *drvc, const char *resource, const char *serialcomm); SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi); +SR_PRIV int sr_scpi_connection_id(struct sr_scpi_dev_inst *scpi, + char **connection_id); SR_PRIV int sr_scpi_source_add(struct sr_session *session, struct sr_scpi_dev_inst *scpi, int events, int timeout, sr_receive_data_callback cb, void *cb_data); diff --git a/src/scpi/scpi.c b/src/scpi/scpi.c index 62e81beb..4f72d6e6 100644 --- a/src/scpi/scpi.c +++ b/src/scpi/scpi.c @@ -407,6 +407,21 @@ SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi) return scpi->open(scpi); } +/** + * Get the connection ID of the SCPI device. + * + * @param scpi Previously initialized SCPI device structure. + * @param connection_id Pointer where to store the connection ID. The caller + * is responsible for g_free()ing the string when it is no longer needed. + * + * @return SR_OK on success, SR_ERR on failure. + */ +SR_PRIV int sr_scpi_connection_id(struct sr_scpi_dev_inst *scpi, + char **connection_id) +{ + return scpi->connection_id(scpi, connection_id); +} + /** * Add an event source for an SCPI device. * diff --git a/src/scpi/scpi_libgpib.c b/src/scpi/scpi_libgpib.c index 8f200e7d..4519a4e7 100644 --- a/src/scpi/scpi_libgpib.c +++ b/src/scpi/scpi_libgpib.c @@ -59,6 +59,16 @@ static int scpi_gpib_open(struct sr_scpi_dev_inst *scpi) return SR_OK; } +static int scpi_gpib_connection_id(struct sr_scpi_dev_inst *scpi, + char **connection_id) +{ + struct scpi_gpib *gscpi = scpi->priv; + + *connection_id = g_strdup_printf("%s/%s", scpi->prefix, gscpi->name); + + return SR_OK; +} + static int scpi_gpib_source_add(struct sr_session *session, void *priv, int events, int timeout, sr_receive_data_callback cb, void *cb_data) { @@ -172,17 +182,18 @@ SR_PRIV int sr_scpi_gpib_spoll(struct sr_scpi_dev_inst *scpi, char *buf) } SR_PRIV const struct sr_scpi_dev_inst scpi_libgpib_dev = { - .name = "GPIB", - .prefix = "libgpib", - .priv_size = sizeof(struct scpi_gpib), - .dev_inst_new = scpi_gpib_dev_inst_new, - .open = scpi_gpib_open, - .source_add = scpi_gpib_source_add, + .name = "GPIB", + .prefix = "libgpib", + .priv_size = sizeof(struct scpi_gpib), + .dev_inst_new = scpi_gpib_dev_inst_new, + .open = scpi_gpib_open, + .connection_id = scpi_gpib_connection_id, + .source_add = scpi_gpib_source_add, .source_remove = scpi_gpib_source_remove, - .send = scpi_gpib_send, - .read_begin = scpi_gpib_read_begin, - .read_data = scpi_gpib_read_data, + .send = scpi_gpib_send, + .read_begin = scpi_gpib_read_begin, + .read_data = scpi_gpib_read_data, .read_complete = scpi_gpib_read_complete, - .close = scpi_gpib_close, - .free = scpi_gpib_free, + .close = scpi_gpib_close, + .free = scpi_gpib_free, }; diff --git a/src/scpi/scpi_serial.c b/src/scpi/scpi_serial.c index 23a5e3f0..25541674 100644 --- a/src/scpi/scpi_serial.c +++ b/src/scpi/scpi_serial.c @@ -98,6 +98,17 @@ static int scpi_serial_open(struct sr_scpi_dev_inst *scpi) return SR_OK; } +static int scpi_serial_connection_id(struct sr_scpi_dev_inst *scpi, + char **connection_id) +{ + struct scpi_serial *sscpi = scpi->priv; + struct sr_serial_dev_inst *serial = sscpi->serial; + + *connection_id = g_strdup(serial->port); + + return SR_OK; +} + static int scpi_serial_source_add(struct sr_session *session, void *priv, int events, int timeout, sr_receive_data_callback cb, void *cb_data) { @@ -192,6 +203,7 @@ SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = { .scan = scpi_serial_scan, .dev_inst_new = scpi_serial_dev_inst_new, .open = scpi_serial_open, + .connection_id = scpi_serial_connection_id, .source_add = scpi_serial_source_add, .source_remove = scpi_serial_source_remove, .send = scpi_serial_send, diff --git a/src/scpi/scpi_tcp.c b/src/scpi/scpi_tcp.c index c01cc560..6477c064 100644 --- a/src/scpi/scpi_tcp.c +++ b/src/scpi/scpi_tcp.c @@ -115,6 +115,17 @@ static int scpi_tcp_open(struct sr_scpi_dev_inst *scpi) return SR_OK; } +static int scpi_tcp_connection_id(struct sr_scpi_dev_inst *scpi, + char **connection_id) +{ + struct scpi_tcp *tcp = scpi->priv; + + *connection_id = g_strdup_printf("%s/%s:%s", + scpi->prefix, tcp->address, tcp->port); + + return SR_OK; +} + static int scpi_tcp_source_add(struct sr_session *session, void *priv, int events, int timeout, sr_receive_data_callback cb, void *cb_data) { @@ -266,6 +277,7 @@ SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_raw_dev = { .priv_size = sizeof(struct scpi_tcp), .dev_inst_new = scpi_tcp_dev_inst_new, .open = scpi_tcp_open, + .connection_id = scpi_tcp_connection_id, .source_add = scpi_tcp_source_add, .source_remove = scpi_tcp_source_remove, .send = scpi_tcp_send, @@ -283,6 +295,7 @@ SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_rigol_dev = { .priv_size = sizeof(struct scpi_tcp), .dev_inst_new = scpi_tcp_dev_inst_new, .open = scpi_tcp_open, + .connection_id = scpi_tcp_connection_id, .source_add = scpi_tcp_source_add, .source_remove = scpi_tcp_source_remove, .send = scpi_tcp_send, diff --git a/src/scpi/scpi_usbtmc_libusb.c b/src/scpi/scpi_usbtmc_libusb.c index c1d4029c..6b033444 100644 --- a/src/scpi/scpi_usbtmc_libusb.c +++ b/src/scpi/scpi_usbtmc_libusb.c @@ -18,6 +18,7 @@ */ #include +#include #include #include #include "libsigrok-internal.h" @@ -413,6 +414,18 @@ static int scpi_usbtmc_libusb_open(struct sr_scpi_dev_inst *scpi) return SR_OK; } +static int scpi_usbtmc_libusb_connection_id(struct sr_scpi_dev_inst *scpi, + char **connection_id) +{ + struct scpi_usbtmc_libusb *uscpi = scpi->priv; + struct sr_usb_dev_inst *usb = uscpi->usb; + + *connection_id = g_strdup_printf("%s/%" PRIu8 ".%" PRIu8 "", + scpi->prefix, usb->bus, usb->address); + + return SR_OK; +} + static int scpi_usbtmc_libusb_source_add(struct sr_session *session, void *priv, int events, int timeout, sr_receive_data_callback cb, void *cb_data) @@ -661,6 +674,7 @@ SR_PRIV const struct sr_scpi_dev_inst scpi_usbtmc_libusb_dev = { .scan = scpi_usbtmc_libusb_scan, .dev_inst_new = scpi_usbtmc_libusb_dev_inst_new, .open = scpi_usbtmc_libusb_open, + .connection_id = scpi_usbtmc_libusb_connection_id, .source_add = scpi_usbtmc_libusb_source_add, .source_remove = scpi_usbtmc_libusb_source_remove, .send = scpi_usbtmc_libusb_send, diff --git a/src/scpi/scpi_visa.c b/src/scpi/scpi_visa.c index a8f50e4f..a05b58d0 100644 --- a/src/scpi/scpi_visa.c +++ b/src/scpi/scpi_visa.c @@ -68,6 +68,16 @@ static int scpi_visa_open(struct sr_scpi_dev_inst *scpi) return SR_OK; } +static int scpi_visa_connection_id(struct sr_scpi_dev_inst *scpi, + char **connection_id) +{ + struct scpi_visa *vscpi = scpi->priv; + + *connection_id = g_strdup_printf("%s/%s", scpi->prefix, vscpi->resource); + + return SR_OK; +} + static int scpi_visa_source_add(struct sr_session *session, void *priv, int events, int timeout, sr_receive_data_callback cb, void *cb_data) { @@ -154,17 +164,18 @@ static void scpi_visa_free(void *priv) } SR_PRIV const struct sr_scpi_dev_inst scpi_visa_dev = { - .name = "VISA", - .prefix = "visa", - .priv_size = sizeof(struct scpi_visa), - .dev_inst_new = scpi_visa_dev_inst_new, - .open = scpi_visa_open, - .source_add = scpi_visa_source_add, + .name = "VISA", + .prefix = "visa", + .priv_size = sizeof(struct scpi_visa), + .dev_inst_new = scpi_visa_dev_inst_new, + .open = scpi_visa_open, + .connection_id = scpi_visa_connection_id, + .source_add = scpi_visa_source_add, .source_remove = scpi_visa_source_remove, - .send = scpi_visa_send, - .read_begin = scpi_visa_read_begin, - .read_data = scpi_visa_read_data, + .send = scpi_visa_send, + .read_begin = scpi_visa_read_begin, + .read_data = scpi_visa_read_data, .read_complete = scpi_visa_read_complete, - .close = scpi_visa_close, - .free = scpi_visa_free, + .close = scpi_visa_close, + .free = scpi_visa_free, }; diff --git a/src/scpi/scpi_vxi.c b/src/scpi/scpi_vxi.c index 51861ee8..234219c6 100644 --- a/src/scpi/scpi_vxi.c +++ b/src/scpi/scpi_vxi.c @@ -92,6 +92,16 @@ static int scpi_vxi_open(struct sr_scpi_dev_inst *scpi) return SR_OK; } +static int scpi_vxi_connection_id(struct sr_scpi_dev_inst *scpi, + char **connection_id) +{ + struct scpi_vxi *vxi = scpi->priv; + + *connection_id = g_strdup_printf("%s/%s", scpi->prefix, vxi->address); + + return SR_OK; +} + static int scpi_vxi_source_add(struct sr_session *session, void *priv, int events, int timeout, sr_receive_data_callback cb, void *cb_data) { @@ -224,6 +234,7 @@ SR_PRIV const struct sr_scpi_dev_inst scpi_vxi_dev = { .priv_size = sizeof(struct scpi_vxi), .dev_inst_new = scpi_vxi_dev_inst_new, .open = scpi_vxi_open, + .connection_id = scpi_vxi_connection_id, .source_add = scpi_vxi_source_add, .source_remove = scpi_vxi_source_remove, .send = scpi_vxi_send,