]> sigrok.org Git - libsigrok.git/commitdiff
scpi: Add connenction_id() function to all scpi drivers.
authorFrank Stettner <redacted>
Sat, 16 Mar 2019 22:44:38 +0000 (23:44 +0100)
committerUwe Hermann <redacted>
Fri, 29 Mar 2019 15:15:37 +0000 (16:15 +0100)
src/device.c
src/scpi.h
src/scpi/scpi.c
src/scpi/scpi_libgpib.c
src/scpi/scpi_serial.c
src/scpi/scpi_tcp.c
src/scpi/scpi_usbtmc_libusb.c
src/scpi/scpi_visa.c
src/scpi/scpi_vxi.c

index 34d80a9dadf45771b3751c1ba1a5917aba034900..e7be567d20ad705ffdd3da315198e37494d2684b 100644 (file)
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <libsigrok/libsigrok.h>
 #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;
 }
 
index eeee9a553214048fa67ea17424654dbcd108c20b..162c7a3657ec649f08d923b927605de37bd5317d 100644 (file)
@@ -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);
index 62e81bebeedab94822786daa317a6d7151b8bdcf..4f72d6e65e01bfc5853efa0cc2cd5615f5676952 100644 (file)
@@ -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.
  *
index 8f200e7d964ab247c41d727c56d0a43e8612bb99..4519a4e70e2da78589c35c779e12287432f28595 100644 (file)
@@ -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,
 };
index 23a5e3f02cb31acf784242d418dd1821f36ae57b..255416743cfda05e37b370b5da3efee1ddcd945a 100644 (file)
@@ -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,
index c01cc5606f62a57a627fd5c895a230cb262d294c..6477c064dcd92aa57eeec5e00d49558a16033b9e 100644 (file)
@@ -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,
index c1d4029ca1e89fedaec057fb7a28d8e7639a3805..6b0334440e94e9ce993ca534f1b4c58d16abd804 100644 (file)
@@ -18,6 +18,7 @@
  */
 
 #include <config.h>
+#include <inttypes.h>
 #include <string.h>
 #include <libsigrok/libsigrok.h>
 #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,
index a8f50e4f65c71fde5451d19032e80a78e610d0e8..a05b58d04a7859d827881a63bcdd689d81555e16 100644 (file)
@@ -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,
 };
index 51861ee8b67d027d94b11e9dbf2bc5e5d5dfff65..234219c60f511f0be1673da5fa0568a670de0165 100644 (file)
@@ -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,