X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fcommon%2Fscpi.c;h=b2eb4ced958e37fda5f475cd6557cafed4a962e3;hb=fa0d6afe19c3a545f3f940933ed079966525d142;hp=f8810a59250bfebe12666754bd76d987b7409448;hpb=23f43dff15abf5202d81f36e062b0ae5b0ca01cd;p=libsigrok.git diff --git a/hardware/common/scpi.c b/hardware/common/scpi.c index f8810a59..b2eb4ced 100644 --- a/hardware/common/scpi.c +++ b/hardware/common/scpi.c @@ -23,13 +23,7 @@ #include #include -/* Message logging helpers with subsystem-specific prefix string. */ -#define LOG_PREFIX "scpi: " -#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args) -#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args) -#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args) -#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args) -#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args) +#define LOG_PREFIX "scpi" #define SCPI_READ_RETRIES 100 #define SCPI_READ_RETRY_TIMEOUT 10000 @@ -119,31 +113,94 @@ SR_PRIV int sr_scpi_source_remove(struct sr_scpi_dev_inst *scpi) * Send a SCPI command. * * @param scpi Previously initialized SCPI device structure. - * @param command The SCPI command to send to the device. + * @param format Format string, to be followed by any necessary arguments. * * @return SR_OK on success, SR_ERR on failure. */ SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi, - const char *command) + const char *format, ...) { - return scpi->send(scpi->priv, command); + va_list args; + int ret; + + va_start(args, format); + ret = sr_scpi_send_variadic(scpi, format, args); + va_end(args); + + return ret; } /** - * Receive an SCPI reply and store the reply in scpi_response. + * Send a SCPI command with a variadic argument list. + * + * @param scpi Previously initialized SCPI device structure. + * @param format Format string. + * @param args Argument list. + * + * @return SR_OK on success, SR_ERR on failure. + */ +SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi, + const char *format, va_list args) +{ + va_list args_copy; + char *buf; + int len, ret; + + /* Get length of buffer required. */ + va_copy(args_copy, args); + len = vsnprintf(NULL, 0, format, args_copy); + va_end(args_copy); + + /* Allocate buffer and write out command. */ + buf = g_malloc(len + 1); + vsprintf(buf, format, args); + + /* Send command. */ + ret = scpi->send(scpi->priv, buf); + + /* Free command buffer. */ + g_free(buf); + + return ret; +} + +/** + * Begin receiving an SCPI reply. * * @param scpi Previously initialised SCPI device structure. - * @param scpi_response Pointer where to store the SCPI response. * - * @return SR_OK upon fetching a full SCPI response, SR_ERR upon fetching an - * incomplete or no response. The allocated response must be freed by - * the caller in the case of a full response as well in the case of - * an incomplete. + * @return SR_OK on success, SR_ERR on failure. + */ +SR_PRIV int sr_scpi_read_begin(struct sr_scpi_dev_inst *scpi) +{ + return scpi->read_begin(scpi->priv); +} + +/** + * Read part of a response from SCPI device. + * + * @param scpi Previously initialised SCPI device structure. + * @param buf Buffer to store result. + * @param maxlen Maximum number of bytes to read. + * + * @return Number of bytes read, or SR_ERR upon failure. */ -SR_PRIV int sr_scpi_receive(struct sr_scpi_dev_inst *scpi, - char **scpi_response) +SR_PRIV int sr_scpi_read_data(struct sr_scpi_dev_inst *scpi, + char *buf, int maxlen) { - return scpi->receive(scpi->priv, scpi_response); + return scpi->read_data(scpi->priv, buf, maxlen); +} + +/** + * Check whether a complete SCPI response has been received. + * + * @param scpi Previously initialised SCPI device structure. + * + * @return 1 if complete, 0 otherwise. + */ +SR_PRIV int sr_scpi_read_complete(struct sr_scpi_dev_inst *scpi) +{ + return scpi->read_complete(scpi->priv); } /** @@ -178,19 +235,39 @@ SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi) * @param command The SCPI command to send to the device (can be NULL). * @param scpi_response Pointer where to store the SCPI response. * - * @return SR_OK upon fetching a full SCPI response, SR_ERR upon fetching an - * incomplete or no response. The allocated response must be freed by - * the caller in the case of a full response as well in the case of - * an incomplete. + * @return SR_OK on success, SR_ERR on failure. */ SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi, const char *command, char **scpi_response) { + char buf[256]; + int len; + GString *response; + if (command) if (sr_scpi_send(scpi, command) != SR_OK) return SR_ERR; - return sr_scpi_receive(scpi, scpi_response); + if (sr_scpi_read_begin(scpi) != SR_OK) + return SR_ERR; + + response = g_string_new(""); + + *scpi_response = NULL; + + while (!sr_scpi_read_complete(scpi)) { + len = sr_scpi_read_data(scpi, buf, sizeof(buf)); + if (len < 0) { + g_string_free(response, TRUE); + return SR_ERR; + } + g_string_append_len(response, buf, len); + } + + *scpi_response = response->str; + g_string_free(response, FALSE); + + return SR_OK; } /** @@ -475,6 +552,7 @@ SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi, { int num_tokens; char *response; + char *newline; gchar **tokens; struct sr_scpi_hw_info *hw_info; @@ -485,6 +563,12 @@ SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi, if (!response) return SR_ERR; + sr_info("Got IDN string: '%s'", response); + + /* Remove trailing newline if present. */ + if ((newline = g_strrstr(response, "\n"))) + newline[0] = '\0'; + /* * The response to a '*IDN?' is specified by the SCPI spec. It contains * a comma-separated list containing the manufacturer name, instrument