+++ /dev/null
-/*
- * This file is part of the libsigrok project.
- *
- * Copyright (C) 2015 Bert Vermeulen <bert@biot.com>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <config.h>
-#include <strings.h>
-#include <libsigrok/libsigrok.h>
-#include "libsigrok-internal.h"
-#include "scpi.h"
-
-#define LOG_PREFIX "scpi/helpers"
-
-static const char *scpi_vendors[][2] = {
- { "HEWLETT-PACKARD", "HP" },
- { "Agilent Technologies", "Agilent" },
- { "RIGOL TECHNOLOGIES", "Rigol" },
- { "PHILIPS", "Philips" },
- { "CHROMA", "Chroma" },
- { "Chroma ATE", "Chroma" },
-};
-
-SR_PRIV const char *sr_vendor_alias(const char *raw_vendor)
-{
- unsigned int i;
-
- for (i = 0; i < ARRAY_SIZE(scpi_vendors); i++) {
- if (!g_ascii_strcasecmp(raw_vendor, scpi_vendors[i][0]))
- return scpi_vendors[i][1];
- }
-
- return raw_vendor;
-}
-
-SR_PRIV const char *scpi_cmd_get(const struct scpi_command *cmdtable, int command)
-{
- unsigned int i;
- const char *cmd;
-
- if (!cmdtable)
- return NULL;
-
- cmd = NULL;
- for (i = 0; cmdtable[i].string; i++) {
- if (cmdtable[i].command == command) {
- cmd = cmdtable[i].string;
- break;
- }
- }
-
- return cmd;
-}
-
-SR_PRIV int scpi_cmd(const struct sr_dev_inst *sdi, const struct scpi_command *cmdtable,
- int command, ...)
-{
- struct sr_scpi_dev_inst *scpi;
- va_list args;
- int ret;
- const char *cmd;
-
- if (!(cmd = scpi_cmd_get(cmdtable, command))) {
- /* Device does not implement this command, that's OK. */
- return SR_OK;
- }
-
- scpi = sdi->conn;
- va_start(args, command);
- ret = sr_scpi_send_variadic(scpi, cmd, args);
- va_end(args);
-
- return ret;
-}
-
-SR_PRIV int scpi_cmd_resp(const struct sr_dev_inst *sdi, const struct scpi_command *cmdtable,
- GVariant **gvar, const GVariantType *gvtype, int command, ...)
-{
- struct sr_scpi_dev_inst *scpi;
- va_list args;
- double d;
- int ret;
- char *s;
- const char *cmd;
-
- if (!(cmd = scpi_cmd_get(cmdtable, command))) {
- /* Device does not implement this command. */
- return SR_ERR_NA;
- }
-
- scpi = sdi->conn;
- va_start(args, command);
- ret = sr_scpi_send_variadic(scpi, cmd, args);
- va_end(args);
- if (ret != SR_OK)
- return ret;
-
- /* Straight SCPI getters to GVariant types. */
- if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_BOOLEAN)) {
- if ((ret = sr_scpi_get_string(scpi, NULL, &s)) != SR_OK)
- return ret;
- if (!g_ascii_strcasecmp(s, "ON") || !g_ascii_strcasecmp(s, "1")
- || !g_ascii_strcasecmp(s, "YES"))
- *gvar = g_variant_new_boolean(TRUE);
- else if (!g_ascii_strcasecmp(s, "OFF") || !g_ascii_strcasecmp(s, "0")
- || !g_ascii_strcasecmp(s, "NO"))
- *gvar = g_variant_new_boolean(FALSE);
- else
- ret = SR_ERR;
- g_free(s);
- } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_DOUBLE)) {
- if ((ret = sr_scpi_get_double(scpi, NULL, &d)) == SR_OK)
- *gvar = g_variant_new_double(d);
- } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_STRING)) {
- if ((ret = sr_scpi_get_string(scpi, NULL, &s)) == SR_OK)
- *gvar = g_variant_new_string(s);
- } else {
- sr_err("Unable to convert to desired GVariant type.");
- ret = SR_ERR_NA;
- }
-
- return ret;
-}
* This file is part of the libsigrok project.
*
* Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
+ * Copyright (C) 2015 Bert Vermeulen <bert@biot.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#define SCPI_READ_RETRIES 100
#define SCPI_READ_RETRY_TIMEOUT_US (10 * 1000)
+static const char *scpi_vendors[][2] = {
+ { "HEWLETT-PACKARD", "HP" },
+ { "Agilent Technologies", "Agilent" },
+ { "RIGOL TECHNOLOGIES", "Rigol" },
+ { "PHILIPS", "Philips" },
+ { "CHROMA", "Chroma" },
+ { "Chroma ATE", "Chroma" },
+};
+
/**
* Parse a string representation of a boolean-like value into a gboolean.
* Similar to sr_parse_boolstring but rejects strings which do not represent
return sdi;
}
+/**
+ * Send a SCPI command with a variadic argument list without mutex.
+ *
+ * @param scpi Previously initialized SCPI device structure.
+ * @param format Format string.
+ * @param args Argument list.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+static int 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_malloc0(len + 2);
+ vsprintf(buf, format, args);
+ if (buf[len - 1] != '\n')
+ buf[len] = '\n';
+
+ /* Send command. */
+ ret = scpi->send(scpi->priv, buf);
+
+ /* Free command buffer. */
+ g_free(buf);
+
+ return ret;
+}
+
+/**
+ * Send a SCPI command without mutex.
+ *
+ * @param scpi Previously initialized SCPI device structure.
+ * @param format Format string, to be followed by any necessary arguments.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+static int scpi_send(struct sr_scpi_dev_inst *scpi, const char *format,
+ va_list args)
+{
+ return scpi_send_variadic(scpi, format, args);
+}
+
+/**
+ * Send data to SCPI device without mutex.
+ *
+ * TODO: This is only implemented in TcpRaw, but never used.
+ * TODO: Use Mutex at all?
+ *
+ * @param scpi Previously initialised SCPI device structure.
+ * @param buf Buffer with data to send.
+ * @param len Number of bytes to send.
+ *
+ * @return Number of bytes read, or SR_ERR upon failure.
+ */
+static int scpi_write_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen)
+{
+ return scpi->write_data(scpi->priv, buf, maxlen);
+}
+
+/**
+ * Read part of a response from SCPI device without mutex.
+ *
+ * @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.
+ */
+static int scpi_read_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen)
+{
+ return scpi->read_data(scpi->priv, buf, maxlen);
+}
+
+/**
+ * Do a non-blocking read of up to the allocated length, and
+ * check if a timeout has occured, without mutex.
+ *
+ * @param scpi Previously initialised SCPI device structure.
+ * @param response Buffer to which the response is appended.
+ * @param abs_timeout_us Absolute timeout in microseconds
+ *
+ * @return read length on success, SR_ERR* on failure.
+ */
+static int scpi_read_response(struct sr_scpi_dev_inst *scpi,
+ GString *response, gint64 abs_timeout_us)
+{
+ int len, space;
+
+ space = response->allocated_len - response->len;
+ len = scpi->read_data(scpi->priv, &response->str[response->len], space);
+
+ if (len < 0) {
+ sr_err("Incompletely read SCPI response.");
+ return SR_ERR;
+ }
+
+ if (len > 0) {
+ g_string_set_size(response, response->len + len);
+ return len;
+ }
+
+ if (g_get_monotonic_time() > abs_timeout_us) {
+ sr_err("Timed out waiting for SCPI response.");
+ return SR_ERR_TIMEOUT;
+ }
+
+ return 0;
+}
+
+/**
+ * Send a SCPI command, receive the reply and store the reply in
+ * scpi_response, without mutex.
+ *
+ * @param scpi Previously initialised SCPI device structure.
+ * @param command The SCPI command to send to the device.
+ * @param scpi_response Pointer where to store the SCPI response.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+static int scpi_get_data(struct sr_scpi_dev_inst *scpi,
+ const char *command, GString **scpi_response)
+{
+ int ret;
+ GString *response;
+ int space;
+ gint64 timeout;
+ va_list empty_va_list;
+
+ /* Optionally send caller provided command. */
+ if (command) {
+ if (scpi_send(scpi, command, empty_va_list) != SR_OK)
+ return SR_ERR;
+ }
+
+ /* Initiate SCPI read operation. */
+ if (sr_scpi_read_begin(scpi) != SR_OK)
+ return SR_ERR;
+
+ /* Keep reading until completion or until timeout. */
+ timeout = g_get_monotonic_time() + scpi->read_timeout_us;
+
+ response = *scpi_response;
+
+ while (!sr_scpi_read_complete(scpi)) {
+ /* Resize the buffer when free space drops below a threshold. */
+ space = response->allocated_len - response->len;
+ if (space < 128) {
+ int oldlen = response->len;
+ g_string_set_size(response, oldlen + 1024);
+ g_string_set_size(response, oldlen);
+ }
+
+ /* Read another chunk of the response. */
+ ret = scpi_read_response(scpi, response, timeout);
+
+ if (ret < 0)
+ return ret;
+ if (ret > 0)
+ timeout = g_get_monotonic_time() + scpi->read_timeout_us;
+ }
+
+ return SR_OK;
+}
+
SR_PRIV GSList *sr_scpi_scan(struct drv_context *drvc, GSList *options,
struct sr_dev_inst *(*probe_device)(struct sr_scpi_dev_inst *scpi))
{
*/
SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi)
{
+ g_mutex_init(&scpi->scpi_mutex);
+
return scpi->open(scpi);
}
int ret;
va_start(args, format);
- ret = sr_scpi_send_variadic(scpi, format, args);
+ g_mutex_lock(&scpi->scpi_mutex);
+ ret = scpi_send_variadic(scpi, format, args);
+ g_mutex_unlock(&scpi->scpi_mutex);
va_end(args);
return ret;
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_malloc0(len + 2);
- vsprintf(buf, format, args);
- if (buf[len - 1] != '\n')
- buf[len] = '\n';
-
- /* Send command. */
- ret = scpi->send(scpi->priv, buf);
+ int ret;
- /* Free command buffer. */
- g_free(buf);
+ g_mutex_lock(&scpi->scpi_mutex);
+ ret = scpi_send_variadic(scpi, format, args);
+ g_mutex_unlock(&scpi->scpi_mutex);
return ret;
}
SR_PRIV int sr_scpi_read_data(struct sr_scpi_dev_inst *scpi,
char *buf, int maxlen)
{
- return scpi->read_data(scpi->priv, buf, maxlen);
+ int ret;
+
+ g_mutex_lock(&scpi->scpi_mutex);
+ ret = scpi_read_data(scpi, buf, maxlen);
+ g_mutex_unlock(&scpi->scpi_mutex);
+
+ return ret;
}
/**
* Send data to SCPI device.
*
+ * TODO: This is only implemented in TcpRaw, but never used.
+ * TODO: Use Mutex at all?
+ *
* @param scpi Previously initialised SCPI device structure.
* @param buf Buffer with data to send.
* @param len Number of bytes to send.
SR_PRIV int sr_scpi_write_data(struct sr_scpi_dev_inst *scpi,
char *buf, int maxlen)
{
- return scpi->write_data(scpi->priv, buf, maxlen);
+ int ret;
+
+ g_mutex_lock(&scpi->scpi_mutex);
+ ret = scpi_write_data(scpi, buf, maxlen);
+ g_mutex_unlock(&scpi->scpi_mutex);
+
+ return ret;
}
/**
*/
SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi)
{
- return scpi->close(scpi);
+ int ret;
+
+ g_mutex_lock(&scpi->scpi_mutex);
+ ret = scpi->close(scpi);
+ g_mutex_unlock(&scpi->scpi_mutex);
+ g_mutex_clear(&scpi->scpi_mutex);
+
+ return ret;
}
/**
SR_PRIV int sr_scpi_read_response(struct sr_scpi_dev_inst *scpi,
GString *response, gint64 abs_timeout_us)
{
- int len, space;
-
- space = response->allocated_len - response->len;
- len = sr_scpi_read_data(scpi, &response->str[response->len], space);
-
- if (len < 0) {
- sr_err("Incompletely read SCPI response.");
- return SR_ERR;
- }
-
- if (len > 0) {
- g_string_set_size(response, response->len + len);
- return len;
- }
+ int ret;
- if (g_get_monotonic_time() > abs_timeout_us) {
- sr_err("Timed out waiting for SCPI response.");
- return SR_ERR_TIMEOUT;
- }
+ g_mutex_lock(&scpi->scpi_mutex);
+ ret = scpi_read_response(scpi, response, abs_timeout_us);
+ g_mutex_unlock(&scpi->scpi_mutex);
- return 0;
+ return ret;
}
SR_PRIV int sr_scpi_get_data(struct sr_scpi_dev_inst *scpi,
const char *command, GString **scpi_response)
{
int ret;
- GString *response;
- int space;
- gint64 timeout;
-
- /* Optionally send caller provided command. */
- if (command) {
- if (sr_scpi_send(scpi, command) != SR_OK)
- return SR_ERR;
- }
-
- /* Initiate SCPI read operation. */
- if (sr_scpi_read_begin(scpi) != SR_OK)
- return SR_ERR;
-
- /* Keep reading until completion or until timeout. */
- timeout = g_get_monotonic_time() + scpi->read_timeout_us;
-
- response = *scpi_response;
-
- while (!sr_scpi_read_complete(scpi)) {
- /* Resize the buffer when free space drops below a threshold. */
- space = response->allocated_len - response->len;
- if (space < 128) {
- int oldlen = response->len;
- g_string_set_size(response, oldlen + 1024);
- g_string_set_size(response, oldlen);
- }
-
- /* Read another chunk of the response. */
- ret = sr_scpi_read_response(scpi, response, timeout);
- if (ret < 0)
- return ret;
- if (ret > 0)
- timeout = g_get_monotonic_time() + scpi->read_timeout_us;
- }
+ g_mutex_lock(&scpi->scpi_mutex);
+ ret = scpi_get_data(scpi, command, scpi_response);
+ g_mutex_unlock(&scpi->scpi_mutex);
- return SR_OK;
+ return ret;
}
/**
long llen;
long datalen;
gint64 timeout;
+ va_list empty_va_list;
+
+ g_mutex_lock(&scpi->scpi_mutex);
if (command)
- if (sr_scpi_send(scpi, command) != SR_OK)
+ if (scpi_send(scpi, command, empty_va_list) != SR_OK) {
+ g_mutex_unlock(&scpi->scpi_mutex);
return SR_ERR;
+ }
- if (sr_scpi_read_begin(scpi) != SR_OK)
+ if (sr_scpi_read_begin(scpi) != SR_OK) {
+ g_mutex_unlock(&scpi->scpi_mutex);
return SR_ERR;
+ }
/*
* Assume an initial maximum length, optionally gets adjusted below.
/* Get (the first chunk of) the response. */
while (response->len < 2) {
- ret = sr_scpi_read_response(scpi, response, timeout);
+ ret = scpi_read_response(scpi, response, timeout);
if (ret < 0) {
+ g_mutex_unlock(&scpi->scpi_mutex);
g_string_free(response, TRUE);
return ret;
}
* the input buffer, leaving just the data bytes.
*/
if (response->str[0] != '#') {
+ g_mutex_unlock(&scpi->scpi_mutex);
g_string_free(response, TRUE);
return SR_ERR_DATA;
}
buf[1] = '\0';
ret = sr_atol(buf, &llen);
if ((ret != SR_OK) || (llen == 0)) {
+ g_mutex_unlock(&scpi->scpi_mutex);
g_string_free(response, TRUE);
return ret;
}
while (response->len < (unsigned long)(2 + llen)) {
- ret = sr_scpi_read_response(scpi, response, timeout);
+ ret = scpi_read_response(scpi, response, timeout);
if (ret < 0) {
+ g_mutex_unlock(&scpi->scpi_mutex);
g_string_free(response, TRUE);
return ret;
}
buf[llen] = '\0';
ret = sr_atol(buf, &datalen);
if ((ret != SR_OK) || (datalen == 0)) {
+ g_mutex_unlock(&scpi->scpi_mutex);
g_string_free(response, TRUE);
return ret;
}
}
while (response->len < (unsigned long)(datalen)) {
- ret = sr_scpi_read_response(scpi, response, timeout);
+ ret = scpi_read_response(scpi, response, timeout);
if (ret < 0) {
+ g_mutex_unlock(&scpi->scpi_mutex);
g_string_free(response, TRUE);
return ret;
}
timeout = g_get_monotonic_time() + scpi->read_timeout_us;
}
+ g_mutex_unlock(&scpi->scpi_mutex);
+
/* Convert received data to byte array. */
*scpi_response = g_byte_array_new_take(
(guint8*)g_string_free(response, FALSE), datalen);
g_free(hw_info->firmware_version);
g_free(hw_info);
}
+
+SR_PRIV const char *sr_vendor_alias(const char *raw_vendor)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(scpi_vendors); i++) {
+ if (!g_ascii_strcasecmp(raw_vendor, scpi_vendors[i][0]))
+ return scpi_vendors[i][1];
+ }
+
+ return raw_vendor;
+}
+
+SR_PRIV const char *sr_scpi_cmd_get(const struct scpi_command *cmdtable, int command)
+{
+ unsigned int i;
+ const char *cmd;
+
+ if (!cmdtable)
+ return NULL;
+
+ cmd = NULL;
+ for (i = 0; cmdtable[i].string; i++) {
+ if (cmdtable[i].command == command) {
+ cmd = cmdtable[i].string;
+ break;
+ }
+ }
+
+ return cmd;
+}
+
+SR_PRIV int sr_scpi_cmd(const struct sr_dev_inst *sdi, const struct scpi_command *cmdtable,
+ int command, ...)
+{
+ struct sr_scpi_dev_inst *scpi;
+ va_list args;
+ int ret;
+ const char *cmd;
+
+ if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) {
+ /* Device does not implement this command, that's OK. */
+ return SR_OK;
+ }
+
+ scpi = sdi->conn;
+ va_start(args, command);
+ ret = sr_scpi_send_variadic(scpi, cmd, args);
+ va_end(args);
+
+ return ret;
+}
+
+SR_PRIV int sr_scpi_cmd_resp(const struct sr_dev_inst *sdi,
+ const struct scpi_command *cmdtable,
+ GVariant **gvar, const GVariantType *gvtype, int command, ...)
+{
+ struct sr_scpi_dev_inst *scpi;
+ va_list args;
+ const char *cmd;
+ GString *response;
+ char *s;
+ gboolean b;
+ double d;
+ int ret;
+
+ scpi = sdi->conn;
+
+ if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) {
+ /* Device does not implement this command. */
+ return SR_ERR_NA;
+ }
+
+ g_mutex_lock(&scpi->scpi_mutex);
+
+ va_start(args, command);
+ ret = scpi_send_variadic(scpi, cmd, args);
+ va_end(args);
+ if (ret != SR_OK) {
+ g_mutex_unlock(&scpi->scpi_mutex);
+ return ret;
+ }
+
+ response = g_string_sized_new(1024);
+ ret = scpi_get_data(scpi, NULL, &response);
+ if (ret != SR_OK) {
+ g_mutex_unlock(&scpi->scpi_mutex);
+ if (response)
+ g_string_free(response, TRUE);
+ return ret;
+ }
+
+ g_mutex_unlock(&scpi->scpi_mutex);
+
+ /* Get rid of trailing linefeed if present */
+ if (response->len >= 1 && response->str[response->len - 1] == '\n')
+ g_string_truncate(response, response->len - 1);
+
+ /* Get rid of trailing carriage return if present */
+ if (response->len >= 1 && response->str[response->len - 1] == '\r')
+ g_string_truncate(response, response->len - 1);
+
+ s = g_string_free(response, FALSE);
+
+ ret = SR_OK;
+ if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_BOOLEAN)) {
+ if ((ret = parse_strict_bool(s, &b)) == SR_OK)
+ *gvar = g_variant_new_boolean(b);
+ } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_DOUBLE)) {
+ if ((ret = sr_atod_ascii(s, &d)) == SR_OK)
+ *gvar = g_variant_new_double(d);
+ } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_STRING)) {
+ *gvar = g_variant_new_string(s);
+ } else {
+ sr_err("Unable to convert to desired GVariant type.");
+ ret = SR_ERR_NA;
+ }
+
+ g_free(s);
+
+ return ret;
+}