]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/scpi_serial.c
remove unused static functions
[libsigrok.git] / hardware / common / scpi_serial.c
index dc0996349937a181aef67b71012ed23abe61fddc..9711c010a80fb9f5c92cfe60af7710d5e8760684 100644 (file)
 #define SCPI_READ_RETRIES 100
 #define SCPI_READ_RETRY_TIMEOUT 10000
 
-SR_PRIV int scpi_serial_open(void *priv)
+struct scpi_serial {
+       struct sr_serial_dev_inst *serial;
+       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;
+}
+
+static int scpi_serial_open(void *priv)
 {
-       struct sr_serial_dev_inst *serial = priv;
+       struct scpi_serial *sscpi = priv;
+       struct sr_serial_dev_inst *serial = sscpi->serial;
 
        if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
                return SR_ERR;
@@ -42,26 +61,29 @@ SR_PRIV int scpi_serial_open(void *priv)
        return SR_OK;
 }
 
-SR_PRIV int scpi_serial_source_add(void *priv, int events, int timeout,
+static int scpi_serial_source_add(void *priv, int events, int timeout,
                        sr_receive_data_callback_t cb, void *cb_data)
 {
-       struct sr_serial_dev_inst *serial = priv;
+       struct scpi_serial *sscpi = priv;
+       struct sr_serial_dev_inst *serial = sscpi->serial;
 
        return serial_source_add(serial, events, timeout, cb, cb_data);
 }
 
-SR_PRIV int scpi_serial_source_remove(void *priv)
+static int scpi_serial_source_remove(void *priv)
 {
-       struct sr_serial_dev_inst *serial = priv;
+       struct scpi_serial *sscpi = priv;
+       struct sr_serial_dev_inst *serial = sscpi->serial;
 
        return serial_source_remove(serial);
 }
 
-SR_PRIV int scpi_serial_send(void *priv, const char *command)
+static int scpi_serial_send(void *priv, const char *command)
 {
        int len, result, written;
        gchar *terminated_command;
-       struct sr_serial_dev_inst *serial = priv;
+       struct scpi_serial *sscpi = priv;
+       struct sr_serial_dev_inst *serial = sscpi->serial;
 
        terminated_command = g_strconcat(command, "\n", NULL);
        len = strlen(terminated_command);
@@ -83,96 +105,67 @@ SR_PRIV int scpi_serial_send(void *priv, const char *command)
        return SR_OK;
 }
 
-SR_PRIV int scpi_serial_receive(void *priv, char **scpi_response)
+static int scpi_serial_read_begin(void *priv)
 {
-       int len, ret;
-       char buf[256];
-       unsigned int i;
-       GString *response;
-       struct sr_serial_dev_inst *serial = priv;
-
-       response = g_string_sized_new(1024);
-
-       for (i = 0; i <= SCPI_READ_RETRIES; i++) {
-               while ((len = serial_read(serial, buf, sizeof(buf))) > 0)
-                       response = g_string_append_len(response, buf, len);
-
-               if (response->len > 0 &&
-                   response->str[response->len-1] == '\n') {
-                       sr_spew("Fetched full SCPI response.");
-                       break;
-               }
+       struct scpi_serial *sscpi = priv;
 
-               g_usleep(SCPI_READ_RETRY_TIMEOUT);
-       }
+       sscpi->last_character = '\0';
 
-       if (response->len == 0) {
-               sr_dbg("No SCPI response received.");
-               g_string_free(response, TRUE);
-               *scpi_response = NULL;
-               return SR_ERR;
-       } else if (response->str[response->len - 1] == '\n') {
-               /*
-                * The SCPI response contains a LF ('\n') at the end and we
-                * don't need this so replace it with a '\0' and decrement
-                * the length.
-                */
-               response->str[--response->len] = '\0';
-               ret = SR_OK;
-       } else {
-               sr_warn("Incomplete SCPI response received!");
-               ret = SR_ERR;
-       }
+       return SR_OK;
+}
 
-       /* Minor optimization: steal the string instead of copying. */
-       *scpi_response = response->str;
+static int scpi_serial_read_data(void *priv, char *buf, int maxlen)
+{
+       struct scpi_serial *sscpi = priv;
+       int ret;
+
+       ret = serial_read(sscpi->serial, buf, maxlen);
 
-       /* A SCPI response can be quite large, print at most 50 characters. */
-       sr_dbg("SCPI response received (length %d): '%.50s'",
-              response->len, response->str);
+       if (ret < 0)
+               return ret;
 
-       g_string_free(response, FALSE);
+       if (ret > 0) {
+               sscpi->last_character = buf[ret - 1];
+               if (sscpi->last_character == '\n')
+                       ret--;
+       }
 
        return ret;
 }
 
-/* Some stubs to keep the compiler from whining. */
-static int scpi_serial_read(void *priv, char *buf, int maxlen)
+static int scpi_serial_read_complete(void *priv)
 {
-       return serial_read(priv, buf, maxlen);
+       struct scpi_serial *sscpi = priv;
+
+       return (sscpi->last_character == '\n');
 }
+
 static int scpi_serial_close(void *priv)
 {
-       return serial_close(priv);
-}
-static void scpi_serial_free(void *priv)
-{
-       return sr_serial_dev_inst_free(priv);
+       struct scpi_serial *sscpi = priv;
+
+       return serial_close(sscpi->serial);
 }
 
-SR_PRIV struct sr_scpi_dev_inst *scpi_serial_dev_inst_new(const char *port,
-               const char *serialcomm)
+static void scpi_serial_free(void *priv)
 {
-       struct sr_scpi_dev_inst *scpi;
-       struct sr_serial_dev_inst *serial;
-
-       scpi = g_try_malloc(sizeof(struct sr_scpi_dev_inst));
+       struct scpi_serial *sscpi = priv;
 
-       if (!(serial = sr_serial_dev_inst_new(port, serialcomm)))
-       {
-               g_free(scpi);
-               return NULL;
-       }
-
-       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->receive = scpi_serial_receive;
-       scpi->read = scpi_serial_read;
-       scpi->close = scpi_serial_close;
-       scpi->free = scpi_serial_free;
-       scpi->priv = serial;
-
-       return scpi;
+       sr_serial_dev_inst_free(sscpi->serial);
 }
+
+SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = {
+       .name          = "serial",
+       .prefix        = "",
+       .priv_size     = sizeof(struct scpi_serial),
+       .dev_inst_new  = scpi_serial_dev_inst_new,
+       .open          = scpi_serial_open,
+       .source_add    = scpi_serial_source_add,
+       .source_remove = scpi_serial_source_remove,
+       .send          = scpi_serial_send,
+       .read_begin    = scpi_serial_read_begin,
+       .read_data     = scpi_serial_read_data,
+       .read_complete = scpi_serial_read_complete,
+       .close         = scpi_serial_close,
+       .free          = scpi_serial_free,
+};