]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/scpi_usbtmc.c
README.devices: Various updates and fixes.
[libsigrok.git] / hardware / common / scpi_usbtmc.c
index c734890ad46a8b4fad996625e254cc1389e26d17..8254f8019be990ceee5bf874635b8b77be1c1456 100644 (file)
@@ -37,11 +37,35 @@ struct usbtmc_scpi {
        int response_bytes_read;
 };
 
-static int scpi_usbtmc_dev_inst_new(void *priv, const char *resource,
-               char **params, const char *serialcomm)
+static GSList *scpi_usbtmc_scan(struct drv_context *drvc)
+{
+       GSList *resources = NULL;
+       GDir *dir;
+       const char *dev_name;
+       char *resource;
+
+       (void)drvc;
+
+       if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
+               if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
+                       return NULL;
+       while ((dev_name = g_dir_read_name(dir))) {
+               if (strncmp(dev_name, "usbtmc", 6))
+                       continue;
+               resource = g_strconcat("/dev/", dev_name, NULL);
+               resources = g_slist_append(resources, resource);
+       }
+       g_dir_close(dir);
+
+       return resources;
+}
+
+static int scpi_usbtmc_dev_inst_new(void *priv, struct drv_context *drvc,
+               const char *resource, char **params, const char *serialcomm)
 {
        struct usbtmc_scpi *uscpi = priv;
 
+       (void)drvc;
        (void)params;
        (void)serialcomm;
 
@@ -51,7 +75,7 @@ static int scpi_usbtmc_dev_inst_new(void *priv, const char *resource,
        return SR_OK;
 }
 
-SR_PRIV int scpi_usbtmc_open(void *priv)
+static int scpi_usbtmc_open(void *priv)
 {
        struct usbtmc_scpi *uscpi = priv;
        struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
@@ -62,8 +86,8 @@ SR_PRIV int scpi_usbtmc_open(void *priv)
        return SR_OK;
 }
 
-SR_PRIV int scpi_usbtmc_source_add(void *priv, int events, int timeout,
-                       sr_receive_data_callback_t cb, void *cb_data)
+static int scpi_usbtmc_source_add(void *priv, int events, int timeout,
+                       sr_receive_data_callback cb, void *cb_data)
 {
        struct usbtmc_scpi *uscpi = priv;
        struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
@@ -71,7 +95,7 @@ SR_PRIV int scpi_usbtmc_source_add(void *priv, int events, int timeout,
        return sr_source_add(usbtmc->fd, events, timeout, cb, cb_data);
 }
 
-SR_PRIV int scpi_usbtmc_source_remove(void *priv)
+static int scpi_usbtmc_source_remove(void *priv)
 {
        struct usbtmc_scpi *uscpi = priv;
        struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
@@ -79,7 +103,7 @@ SR_PRIV int scpi_usbtmc_source_remove(void *priv)
        return sr_source_remove(usbtmc->fd);
 }
 
-SR_PRIV int scpi_usbtmc_send(void *priv, const char *command)
+static int scpi_usbtmc_send(void *priv, const char *command)
 {
        struct usbtmc_scpi *uscpi = priv;
        struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
@@ -103,7 +127,7 @@ SR_PRIV int scpi_usbtmc_send(void *priv, const char *command)
        return SR_OK;
 }
 
-SR_PRIV int scpi_usbtmc_read_begin(void *priv)
+static int scpi_usbtmc_read_begin(void *priv)
 {
        struct usbtmc_scpi *uscpi = priv;
        struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
@@ -119,21 +143,28 @@ SR_PRIV int scpi_usbtmc_read_begin(void *priv)
        uscpi->response_length = len;
        uscpi->response_bytes_read = 0;
 
+       sr_spew("Read %d bytes from device into buffer", len);
+
        return SR_OK;
 }
 
-SR_PRIV int scpi_usbtmc_read_data(void *priv, char *buf, int maxlen)
+static int scpi_usbtmc_read_data(void *priv, char *buf, int maxlen)
 {
        struct usbtmc_scpi *uscpi = priv;
        int read_length;
 
-       if (uscpi->response_length == MAX_READ_LENGTH
-           && uscpi->response_bytes_read == uscpi->response_length)
-               if (scpi_usbtmc_read_begin(uscpi) != SR_OK)
-                       return SR_ERR;
+       sr_spew("%d bytes requested", maxlen);
 
-       if (uscpi->response_bytes_read >= uscpi->response_length)
-               return SR_ERR;
+       if (uscpi->response_bytes_read == uscpi->response_length) {
+               sr_spew("Buffer is empty.");
+               if (uscpi->response_length == MAX_READ_LENGTH) {
+                       sr_spew("Previous read was of maximum length, reading again.");
+                       if (scpi_usbtmc_read_begin(uscpi) != SR_OK)
+                               return SR_ERR;
+               } else {
+                       return SR_ERR;
+               }
+       }
 
        read_length = uscpi->response_length - uscpi->response_bytes_read;
 
@@ -144,10 +175,13 @@ SR_PRIV int scpi_usbtmc_read_data(void *priv, char *buf, int maxlen)
 
        uscpi->response_bytes_read += read_length;
 
+       sr_spew("Returned %d bytes from buffer, %d/%d bytes of buffer now read",
+                       read_length, uscpi->response_bytes_read, uscpi->response_length);
+
        return read_length;
 }
 
-SR_PRIV int scpi_usbtmc_read_complete(void *priv)
+static int scpi_usbtmc_read_complete(void *priv)
 {
        struct usbtmc_scpi *uscpi = priv;
 
@@ -158,7 +192,7 @@ SR_PRIV int scpi_usbtmc_read_complete(void *priv)
        return (uscpi->response_bytes_read >= uscpi->response_length);
 }
 
-SR_PRIV int scpi_usbtmc_close(void *priv)
+static int scpi_usbtmc_close(void *priv)
 {
        struct usbtmc_scpi *uscpi = priv;
 
@@ -179,6 +213,7 @@ SR_PRIV const struct sr_scpi_dev_inst scpi_usbtmc_dev = {
        .name          = "USBTMC",
        .prefix        = "/dev/usbtmc",
        .priv_size     = sizeof(struct usbtmc_scpi),
+       .scan          = scpi_usbtmc_scan,
        .dev_inst_new  = scpi_usbtmc_dev_inst_new,
        .open          = scpi_usbtmc_open,
        .source_add    = scpi_usbtmc_source_add,