2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5 * Copyright (C) 2015 Bert Vermeulen <bert@biot.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <libsigrok/libsigrok.h>
25 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "scpi"
30 #define SCPI_READ_RETRIES 100
31 #define SCPI_READ_RETRY_TIMEOUT_US (10 * 1000)
33 static const char *scpi_vendors[][2] = {
34 { "HEWLETT-PACKARD", "HP" },
35 { "Agilent Technologies", "Agilent" },
36 { "RIGOL TECHNOLOGIES", "Rigol" },
37 { "PHILIPS", "Philips" },
38 { "CHROMA", "Chroma" },
39 { "Chroma ATE", "Chroma" },
43 * Parse a string representation of a boolean-like value into a gboolean.
44 * Similar to sr_parse_boolstring but rejects strings which do not represent
45 * a boolean-like value.
47 * @param str String to convert.
48 * @param ret Pointer to a gboolean where the result of the conversion will be
51 * @return SR_OK on success, SR_ERR on failure.
53 static int parse_strict_bool(const char *str, gboolean *ret)
58 if (!g_strcmp0(str, "1") ||
59 !g_ascii_strncasecmp(str, "y", 1) ||
60 !g_ascii_strncasecmp(str, "t", 1) ||
61 !g_ascii_strncasecmp(str, "yes", 3) ||
62 !g_ascii_strncasecmp(str, "true", 4) ||
63 !g_ascii_strncasecmp(str, "on", 2)) {
66 } else if (!g_strcmp0(str, "0") ||
67 !g_ascii_strncasecmp(str, "n", 1) ||
68 !g_ascii_strncasecmp(str, "f", 1) ||
69 !g_ascii_strncasecmp(str, "no", 2) ||
70 !g_ascii_strncasecmp(str, "false", 5) ||
71 !g_ascii_strncasecmp(str, "off", 3)) {
79 SR_PRIV extern const struct sr_scpi_dev_inst scpi_serial_dev;
80 SR_PRIV extern const struct sr_scpi_dev_inst scpi_tcp_raw_dev;
81 SR_PRIV extern const struct sr_scpi_dev_inst scpi_tcp_rigol_dev;
82 SR_PRIV extern const struct sr_scpi_dev_inst scpi_usbtmc_libusb_dev;
83 SR_PRIV extern const struct sr_scpi_dev_inst scpi_vxi_dev;
84 SR_PRIV extern const struct sr_scpi_dev_inst scpi_visa_dev;
85 SR_PRIV extern const struct sr_scpi_dev_inst scpi_libgpib_dev;
87 static const struct sr_scpi_dev_inst *scpi_devs[] = {
90 #ifdef HAVE_LIBUSB_1_0
91 &scpi_usbtmc_libusb_dev,
102 #ifdef HAVE_LIBSERIALPORT
103 &scpi_serial_dev, /* Must be last as it matches any resource. */
107 static struct sr_dev_inst *sr_scpi_scan_resource(struct drv_context *drvc,
108 const char *resource, const char *serialcomm,
109 struct sr_dev_inst *(*probe_device)(struct sr_scpi_dev_inst *scpi))
111 struct sr_scpi_dev_inst *scpi;
112 struct sr_dev_inst *sdi;
114 if (!(scpi = scpi_dev_inst_new(drvc, resource, serialcomm)))
117 if (sr_scpi_open(scpi) != SR_OK) {
118 sr_info("Couldn't open SCPI device.");
123 sdi = probe_device(scpi);
128 sdi->status = SR_ST_INACTIVE;
136 * Send a SCPI command with a variadic argument list without mutex.
138 * @param scpi Previously initialized SCPI device structure.
139 * @param format Format string.
140 * @param args Argument list.
142 * @return SR_OK on success, SR_ERR on failure.
144 static int scpi_send_variadic(struct sr_scpi_dev_inst *scpi,
145 const char *format, va_list args)
151 /* Get length of buffer required. */
152 va_copy(args_copy, args);
153 len = sr_vsnprintf_ascii(NULL, 0, format, args_copy);
156 /* Allocate buffer and write out command. */
157 buf = g_malloc0(len + 2);
158 sr_vsprintf_ascii(buf, format, args);
159 if (buf[len - 1] != '\n')
163 ret = scpi->send(scpi->priv, buf);
165 /* Free command buffer. */
172 * Send a SCPI command without mutex.
174 * @param scpi Previously initialized SCPI device structure.
175 * @param format Format string, to be followed by any necessary arguments.
177 * @return SR_OK on success, SR_ERR on failure.
179 static int scpi_send(struct sr_scpi_dev_inst *scpi, const char *format, ...)
184 va_start(args, format);
185 ret = scpi_send_variadic(scpi, format, args);
192 * Send data to SCPI device without mutex.
194 * TODO: This is only implemented in TcpRaw, but never used.
195 * TODO: Use Mutex at all?
197 * @param scpi Previously initialised SCPI device structure.
198 * @param buf Buffer with data to send.
199 * @param len Number of bytes to send.
201 * @return Number of bytes read, or SR_ERR upon failure.
203 static int scpi_write_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen)
205 return scpi->write_data(scpi->priv, buf, maxlen);
209 * Read part of a response from SCPI device without mutex.
211 * @param scpi Previously initialised SCPI device structure.
212 * @param buf Buffer to store result.
213 * @param maxlen Maximum number of bytes to read.
215 * @return Number of bytes read, or SR_ERR upon failure.
217 static int scpi_read_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen)
219 return scpi->read_data(scpi->priv, buf, maxlen);
223 * Do a non-blocking read of up to the allocated length, and
224 * check if a timeout has occured, without mutex.
226 * @param scpi Previously initialised SCPI device structure.
227 * @param response Buffer to which the response is appended.
228 * @param abs_timeout_us Absolute timeout in microseconds
230 * @return read length on success, SR_ERR* on failure.
232 static int scpi_read_response(struct sr_scpi_dev_inst *scpi,
233 GString *response, gint64 abs_timeout_us)
237 space = response->allocated_len - response->len;
238 len = scpi->read_data(scpi->priv, &response->str[response->len], space);
241 sr_err("Incompletely read SCPI response.");
246 g_string_set_size(response, response->len + len);
250 if (g_get_monotonic_time() > abs_timeout_us) {
251 sr_err("Timed out waiting for SCPI response.");
252 return SR_ERR_TIMEOUT;
259 * Send a SCPI command, receive the reply and store the reply in
260 * scpi_response, without mutex.
262 * @param scpi Previously initialised SCPI device structure.
263 * @param command The SCPI command to send to the device.
264 * @param scpi_response Pointer where to store the SCPI response.
266 * @return SR_OK on success, SR_ERR on failure.
268 static int scpi_get_data(struct sr_scpi_dev_inst *scpi,
269 const char *command, GString **scpi_response)
276 /* Optionally send caller provided command. */
278 if (scpi_send(scpi, command) != SR_OK)
282 /* Initiate SCPI read operation. */
283 if (sr_scpi_read_begin(scpi) != SR_OK)
286 /* Keep reading until completion or until timeout. */
287 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
289 response = *scpi_response;
291 while (!sr_scpi_read_complete(scpi)) {
292 /* Resize the buffer when free space drops below a threshold. */
293 space = response->allocated_len - response->len;
295 int oldlen = response->len;
296 g_string_set_size(response, oldlen + 1024);
297 g_string_set_size(response, oldlen);
300 /* Read another chunk of the response. */
301 ret = scpi_read_response(scpi, response, timeout);
306 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
312 SR_PRIV GSList *sr_scpi_scan(struct drv_context *drvc, GSList *options,
313 struct sr_dev_inst *(*probe_device)(struct sr_scpi_dev_inst *scpi))
315 GSList *resources, *l, *devices;
316 struct sr_dev_inst *sdi;
317 const char *resource = NULL;
318 const char *serialcomm = NULL;
322 for (l = options; l; l = l->next) {
323 struct sr_config *src = l->data;
326 resource = g_variant_get_string(src->data, NULL);
328 case SR_CONF_SERIALCOMM:
329 serialcomm = g_variant_get_string(src->data, NULL);
335 for (i = 0; i < ARRAY_SIZE(scpi_devs); i++) {
336 if ((resource && strcmp(resource, scpi_devs[i]->prefix))
337 || !scpi_devs[i]->scan)
339 resources = scpi_devs[i]->scan(drvc);
340 for (l = resources; l; l = l->next) {
341 res = g_strsplit(l->data, ":", 2);
342 if (res[0] && (sdi = sr_scpi_scan_resource(drvc, res[0],
343 serialcomm ? serialcomm : res[1], probe_device))) {
344 devices = g_slist_append(devices, sdi);
345 sdi->connection_id = g_strdup(l->data);
349 g_slist_free_full(resources, g_free);
352 if (!devices && resource) {
353 sdi = sr_scpi_scan_resource(drvc, resource, serialcomm, probe_device);
355 devices = g_slist_append(NULL, sdi);
358 /* Tack a copy of the newly found devices onto the driver list. */
360 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
365 SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(struct drv_context *drvc,
366 const char *resource, const char *serialcomm)
368 struct sr_scpi_dev_inst *scpi = NULL;
369 const struct sr_scpi_dev_inst *scpi_dev;
373 for (i = 0; i < ARRAY_SIZE(scpi_devs); i++) {
374 scpi_dev = scpi_devs[i];
375 if (!strncmp(resource, scpi_dev->prefix, strlen(scpi_dev->prefix))) {
376 sr_dbg("Opening %s device %s.", scpi_dev->name, resource);
377 scpi = g_malloc(sizeof(*scpi));
379 scpi->priv = g_malloc0(scpi->priv_size);
380 scpi->read_timeout_us = 1000 * 1000;
381 params = g_strsplit(resource, "/", 0);
382 if (scpi->dev_inst_new(scpi->priv, drvc, resource,
383 params, serialcomm) != SR_OK) {
398 * @param scpi Previously initialized SCPI device structure.
400 * @return SR_OK on success, SR_ERR on failure.
402 SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi)
404 g_mutex_init(&scpi->scpi_mutex);
406 return scpi->open(scpi);
410 * Add an event source for an SCPI device.
412 * @param session The session to add the event source to.
413 * @param scpi Previously initialized SCPI device structure.
414 * @param events Events to check for.
415 * @param timeout Max time to wait before the callback is called, ignored if 0.
416 * @param cb Callback function to add. Must not be NULL.
417 * @param cb_data Data for the callback function. Can be NULL.
419 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
420 * SR_ERR_MALLOC upon memory allocation errors.
422 SR_PRIV int sr_scpi_source_add(struct sr_session *session,
423 struct sr_scpi_dev_inst *scpi, int events, int timeout,
424 sr_receive_data_callback cb, void *cb_data)
426 return scpi->source_add(session, scpi->priv, events, timeout, cb, cb_data);
430 * Remove event source for an SCPI device.
432 * @param session The session to remove the event source from.
433 * @param scpi Previously initialized SCPI device structure.
435 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
436 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
439 SR_PRIV int sr_scpi_source_remove(struct sr_session *session,
440 struct sr_scpi_dev_inst *scpi)
442 return scpi->source_remove(session, scpi->priv);
446 * Send a SCPI command.
448 * @param scpi Previously initialized SCPI device structure.
449 * @param format Format string, to be followed by any necessary arguments.
451 * @return SR_OK on success, SR_ERR on failure.
453 SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi,
454 const char *format, ...)
459 va_start(args, format);
460 g_mutex_lock(&scpi->scpi_mutex);
461 ret = scpi_send_variadic(scpi, format, args);
462 g_mutex_unlock(&scpi->scpi_mutex);
469 * Send a SCPI command with a variadic argument list.
471 * @param scpi Previously initialized SCPI device structure.
472 * @param format Format string.
473 * @param args Argument list.
475 * @return SR_OK on success, SR_ERR on failure.
477 SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi,
478 const char *format, va_list args)
482 g_mutex_lock(&scpi->scpi_mutex);
483 ret = scpi_send_variadic(scpi, format, args);
484 g_mutex_unlock(&scpi->scpi_mutex);
490 * Begin receiving an SCPI reply.
492 * @param scpi Previously initialised SCPI device structure.
494 * @return SR_OK on success, SR_ERR on failure.
496 SR_PRIV int sr_scpi_read_begin(struct sr_scpi_dev_inst *scpi)
498 return scpi->read_begin(scpi->priv);
502 * Read part of a response from SCPI device.
504 * @param scpi Previously initialised SCPI device structure.
505 * @param buf Buffer to store result.
506 * @param maxlen Maximum number of bytes to read.
508 * @return Number of bytes read, or SR_ERR upon failure.
510 SR_PRIV int sr_scpi_read_data(struct sr_scpi_dev_inst *scpi,
511 char *buf, int maxlen)
515 g_mutex_lock(&scpi->scpi_mutex);
516 ret = scpi_read_data(scpi, buf, maxlen);
517 g_mutex_unlock(&scpi->scpi_mutex);
523 * Send data to SCPI device.
525 * TODO: This is only implemented in TcpRaw, but never used.
526 * TODO: Use Mutex at all?
528 * @param scpi Previously initialised SCPI device structure.
529 * @param buf Buffer with data to send.
530 * @param len Number of bytes to send.
532 * @return Number of bytes read, or SR_ERR upon failure.
534 SR_PRIV int sr_scpi_write_data(struct sr_scpi_dev_inst *scpi,
535 char *buf, int maxlen)
539 g_mutex_lock(&scpi->scpi_mutex);
540 ret = scpi_write_data(scpi, buf, maxlen);
541 g_mutex_unlock(&scpi->scpi_mutex);
547 * Check whether a complete SCPI response has been received.
549 * @param scpi Previously initialised SCPI device structure.
551 * @return 1 if complete, 0 otherwise.
553 SR_PRIV int sr_scpi_read_complete(struct sr_scpi_dev_inst *scpi)
555 return scpi->read_complete(scpi->priv);
561 * @param scpi Previously initialized SCPI device structure.
563 * @return SR_OK on success, SR_ERR on failure.
565 SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi)
569 g_mutex_lock(&scpi->scpi_mutex);
570 ret = scpi->close(scpi);
571 g_mutex_unlock(&scpi->scpi_mutex);
572 g_mutex_clear(&scpi->scpi_mutex);
580 * @param scpi Previously initialized SCPI device structure. If NULL,
581 * this function does nothing.
583 SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi)
588 scpi->free(scpi->priv);
590 g_free(scpi->actual_channel_name);
595 * Send a SCPI command, receive the reply and store the reply in scpi_response.
597 * @param scpi Previously initialised SCPI device structure.
598 * @param command The SCPI command to send to the device (can be NULL).
599 * @param scpi_response Pointer where to store the SCPI response.
601 * @return SR_OK on success, SR_ERR* on failure.
603 SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi,
604 const char *command, char **scpi_response)
607 response = g_string_sized_new(1024);
609 if (sr_scpi_get_data(scpi, command, &response) != SR_OK) {
611 g_string_free(response, TRUE);
615 /* Get rid of trailing linefeed if present */
616 if (response->len >= 1 && response->str[response->len - 1] == '\n')
617 g_string_truncate(response, response->len - 1);
619 /* Get rid of trailing carriage return if present */
620 if (response->len >= 1 && response->str[response->len - 1] == '\r')
621 g_string_truncate(response, response->len - 1);
623 sr_spew("Got response: '%.70s', length %" G_GSIZE_FORMAT ".",
624 response->str, response->len);
626 *scpi_response = g_string_free(response, FALSE);
632 * Do a non-blocking read of up to the allocated length, and
633 * check if a timeout has occured.
635 * @param scpi Previously initialised SCPI device structure.
636 * @param response Buffer to which the response is appended.
637 * @param abs_timeout_us Absolute timeout in microseconds
639 * @return read length on success, SR_ERR* on failure.
641 SR_PRIV int sr_scpi_read_response(struct sr_scpi_dev_inst *scpi,
642 GString *response, gint64 abs_timeout_us)
646 g_mutex_lock(&scpi->scpi_mutex);
647 ret = scpi_read_response(scpi, response, abs_timeout_us);
648 g_mutex_unlock(&scpi->scpi_mutex);
653 SR_PRIV int sr_scpi_get_data(struct sr_scpi_dev_inst *scpi,
654 const char *command, GString **scpi_response)
658 g_mutex_lock(&scpi->scpi_mutex);
659 ret = scpi_get_data(scpi, command, scpi_response);
660 g_mutex_unlock(&scpi->scpi_mutex);
666 * Send a SCPI command, read the reply, parse it as a bool value and store the
667 * result in scpi_response.
669 * @param scpi Previously initialised SCPI device structure.
670 * @param command The SCPI command to send to the device (can be NULL).
671 * @param scpi_response Pointer where to store the parsed result.
673 * @return SR_OK on success, SR_ERR* on failure.
675 SR_PRIV int sr_scpi_get_bool(struct sr_scpi_dev_inst *scpi,
676 const char *command, gboolean *scpi_response)
683 ret = sr_scpi_get_string(scpi, command, &response);
684 if (ret != SR_OK && !response)
687 if (parse_strict_bool(response, scpi_response) == SR_OK)
698 * Send a SCPI command, read the reply, parse it as an integer and store the
699 * result in scpi_response.
701 * @param scpi Previously initialised SCPI device structure.
702 * @param command The SCPI command to send to the device (can be NULL).
703 * @param scpi_response Pointer where to store the parsed result.
705 * @return SR_OK on success, SR_ERR* on failure.
707 SR_PRIV int sr_scpi_get_int(struct sr_scpi_dev_inst *scpi,
708 const char *command, int *scpi_response)
715 ret = sr_scpi_get_string(scpi, command, &response);
716 if (ret != SR_OK && !response)
719 if (sr_atoi(response, scpi_response) == SR_OK)
730 * Send a SCPI command, read the reply, parse it as a float and store the
731 * result in scpi_response.
733 * @param scpi Previously initialised SCPI device structure.
734 * @param command The SCPI command to send to the device (can be NULL).
735 * @param scpi_response Pointer where to store the parsed result.
737 * @return SR_OK on success, SR_ERR* on failure.
739 SR_PRIV int sr_scpi_get_float(struct sr_scpi_dev_inst *scpi,
740 const char *command, float *scpi_response)
747 ret = sr_scpi_get_string(scpi, command, &response);
748 if (ret != SR_OK && !response)
751 if (sr_atof_ascii(response, scpi_response) == SR_OK)
762 * Send a SCPI command, read the reply, parse it as a double and store the
763 * result in scpi_response.
765 * @param scpi Previously initialised SCPI device structure.
766 * @param command The SCPI command to send to the device (can be NULL).
767 * @param scpi_response Pointer where to store the parsed result.
769 * @return SR_OK on success, SR_ERR* on failure.
771 SR_PRIV int sr_scpi_get_double(struct sr_scpi_dev_inst *scpi,
772 const char *command, double *scpi_response)
779 ret = sr_scpi_get_string(scpi, command, &response);
780 if (ret != SR_OK && !response)
783 if (sr_atod_ascii(response, scpi_response) == SR_OK)
794 * Send a SCPI *OPC? command, read the reply and return the result of the
797 * @param scpi Previously initialised SCPI device structure.
799 * @return SR_OK on success, SR_ERR* on failure.
801 SR_PRIV int sr_scpi_get_opc(struct sr_scpi_dev_inst *scpi)
806 for (i = 0; i < SCPI_READ_RETRIES; i++) {
808 sr_scpi_get_bool(scpi, SCPI_CMD_OPC, &opc);
811 g_usleep(SCPI_READ_RETRY_TIMEOUT_US);
818 * Send a SCPI command, read the reply, parse it as comma separated list of
819 * floats and store the as an result in scpi_response.
821 * @param scpi Previously initialised SCPI device structure.
822 * @param command The SCPI command to send to the device (can be NULL).
823 * @param scpi_response Pointer where to store the parsed result.
825 * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing
826 * error or upon no response. The allocated response must be freed by
827 * the caller in the case of an SR_OK as well as in the case of
830 SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi,
831 const char *command, GArray **scpi_response)
836 gchar **ptr, **tokens;
837 GArray *response_array;
842 ret = sr_scpi_get_string(scpi, command, &response);
843 if (ret != SR_OK && !response)
846 tokens = g_strsplit(response, ",", 0);
849 response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256);
852 if (sr_atof_ascii(*ptr, &tmp) == SR_OK)
853 response_array = g_array_append_val(response_array,
863 if (ret != SR_OK && response_array->len == 0) {
864 g_array_free(response_array, TRUE);
865 *scpi_response = NULL;
869 *scpi_response = response_array;
875 * Send a SCPI command, read the reply, parse it as comma separated list of
876 * unsigned 8 bit integers and store the as an result in scpi_response.
878 * @param scpi Previously initialised SCPI device structure.
879 * @param command The SCPI command to send to the device (can be NULL).
880 * @param scpi_response Pointer where to store the parsed result.
882 * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing
883 * error or upon no response. The allocated response must be freed by
884 * the caller in the case of an SR_OK as well as in the case of
887 SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi,
888 const char *command, GArray **scpi_response)
892 gchar **ptr, **tokens;
893 GArray *response_array;
898 ret = sr_scpi_get_string(scpi, command, &response);
899 if (ret != SR_OK && !response)
902 tokens = g_strsplit(response, ",", 0);
905 response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256);
908 if (sr_atoi(*ptr, &tmp) == SR_OK)
909 response_array = g_array_append_val(response_array,
919 if (response_array->len == 0) {
920 g_array_free(response_array, TRUE);
921 *scpi_response = NULL;
925 *scpi_response = response_array;
931 * Send a SCPI command, read the reply, parse it as binary data with a
932 * "definite length block" header and store the as an result in scpi_response.
934 * @param scpi Previously initialised SCPI device structure.
935 * @param command The SCPI command to send to the device (can be NULL).
936 * @param scpi_response Pointer where to store the parsed result.
938 * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing
939 * error or upon no response. The allocated response must be freed by
940 * the caller in the case of an SR_OK as well as in the case of
943 SR_PRIV int sr_scpi_get_block(struct sr_scpi_dev_inst *scpi,
944 const char *command, GByteArray **scpi_response)
953 g_mutex_lock(&scpi->scpi_mutex);
956 if (scpi_send(scpi, command) != SR_OK) {
957 g_mutex_unlock(&scpi->scpi_mutex);
961 if (sr_scpi_read_begin(scpi) != SR_OK) {
962 g_mutex_unlock(&scpi->scpi_mutex);
967 * Assume an initial maximum length, optionally gets adjusted below.
968 * Prepare a NULL return value for when error paths will be taken.
970 response = g_string_sized_new(1024);
972 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
974 *scpi_response = NULL;
976 /* Get (the first chunk of) the response. */
977 while (response->len < 2) {
978 ret = scpi_read_response(scpi, response, timeout);
980 g_mutex_unlock(&scpi->scpi_mutex);
981 g_string_free(response, TRUE);
987 * SCPI protocol data blocks are preceeded with a length spec.
988 * The length spec consists of a '#' marker, one digit which
989 * specifies the character count of the length spec, and the
990 * respective number of characters which specify the data block's
991 * length. Raw data bytes follow (thus one must no longer assume
992 * that the received input stream would be an ASCIIZ string).
994 * Get the data block length, and strip off the length spec from
995 * the input buffer, leaving just the data bytes.
997 if (response->str[0] != '#') {
998 g_mutex_unlock(&scpi->scpi_mutex);
999 g_string_free(response, TRUE);
1002 buf[0] = response->str[1];
1004 ret = sr_atol(buf, &llen);
1005 if ((ret != SR_OK) || (llen == 0)) {
1006 g_mutex_unlock(&scpi->scpi_mutex);
1007 g_string_free(response, TRUE);
1011 while (response->len < (unsigned long)(2 + llen)) {
1012 ret = scpi_read_response(scpi, response, timeout);
1014 g_mutex_unlock(&scpi->scpi_mutex);
1015 g_string_free(response, TRUE);
1020 memcpy(buf, &response->str[2], llen);
1022 ret = sr_atol(buf, &datalen);
1023 if ((ret != SR_OK) || (datalen == 0)) {
1024 g_mutex_unlock(&scpi->scpi_mutex);
1025 g_string_free(response, TRUE);
1028 g_string_erase(response, 0, 2 + llen);
1031 * If the initially assumed length does not cover the data block
1032 * length, then re-allocate the buffer size to the now known
1033 * length, and keep reading more chunks of response data.
1035 if (response->len < (unsigned long)(datalen)) {
1036 int oldlen = response->len;
1037 g_string_set_size(response, datalen);
1038 g_string_set_size(response, oldlen);
1041 while (response->len < (unsigned long)(datalen)) {
1042 ret = scpi_read_response(scpi, response, timeout);
1044 g_mutex_unlock(&scpi->scpi_mutex);
1045 g_string_free(response, TRUE);
1049 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
1052 g_mutex_unlock(&scpi->scpi_mutex);
1054 /* Convert received data to byte array. */
1055 *scpi_response = g_byte_array_new_take(
1056 (guint8*)g_string_free(response, FALSE), datalen);
1062 * Send the *IDN? SCPI command, receive the reply, parse it and store the
1063 * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer.
1065 * The hw_info structure must be freed by the caller via sr_scpi_hw_info_free().
1067 * @param scpi Previously initialised SCPI device structure.
1068 * @param scpi_response Pointer where to store the hw_info structure.
1070 * @return SR_OK upon success, SR_ERR* on failure.
1072 SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi,
1073 struct sr_scpi_hw_info **scpi_response)
1075 int num_tokens, ret;
1078 struct sr_scpi_hw_info *hw_info;
1084 ret = sr_scpi_get_string(scpi, SCPI_CMD_IDN, &response);
1085 if (ret != SR_OK && !response)
1089 * The response to a '*IDN?' is specified by the SCPI spec. It contains
1090 * a comma-separated list containing the manufacturer name, instrument
1091 * model, serial number of the instrument and the firmware version.
1093 tokens = g_strsplit(response, ",", 0);
1095 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
1097 if (num_tokens < 4) {
1098 sr_dbg("IDN response not according to spec: %80.s.", response);
1105 hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info));
1107 idn_substr = g_strstr_len(tokens[0], -1, "IDN ");
1108 if (idn_substr == NULL)
1109 hw_info->manufacturer = g_strstrip(g_strdup(tokens[0]));
1111 hw_info->manufacturer = g_strstrip(g_strdup(idn_substr + 4));
1113 hw_info->model = g_strstrip(g_strdup(tokens[1]));
1114 hw_info->serial_number = g_strstrip(g_strdup(tokens[2]));
1115 hw_info->firmware_version = g_strstrip(g_strdup(tokens[3]));
1119 *scpi_response = hw_info;
1125 * Free a sr_scpi_hw_info struct.
1127 * @param hw_info Pointer to the struct to free. If NULL, this
1128 * function does nothing.
1130 SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info)
1135 g_free(hw_info->manufacturer);
1136 g_free(hw_info->model);
1137 g_free(hw_info->serial_number);
1138 g_free(hw_info->firmware_version);
1143 * Remove potentially enclosing pairs of quotes, un-escape content.
1144 * This implementation modifies the caller's buffer when quotes are found
1145 * and doubled quote characters need to get removed from the content.
1147 * @param[in, out] s The SCPI string to check and un-quote.
1149 * @return The start of the un-quoted string.
1151 SR_PRIV const char *sr_scpi_unquote_string(char *s)
1157 /* Immediately bail out on invalid or short input. */
1164 /* Check for matching quote characters front and back. */
1165 if (s[0] != '\'' && s[0] != '"')
1167 if (s[0] != s[s_len - 1])
1170 /* Need to strip quotes, and un-double quote chars inside. */
1171 quotes[0] = quotes[1] = *s;
1173 s[s_len - 1] = '\0';
1176 while ((rdptr = strstr(rdptr, quotes)) != NULL) {
1177 memmove(rdptr, rdptr + 1, strlen(rdptr));
1184 SR_PRIV const char *sr_vendor_alias(const char *raw_vendor)
1188 for (i = 0; i < ARRAY_SIZE(scpi_vendors); i++) {
1189 if (!g_ascii_strcasecmp(raw_vendor, scpi_vendors[i][0]))
1190 return scpi_vendors[i][1];
1196 SR_PRIV const char *sr_scpi_cmd_get(const struct scpi_command *cmdtable,
1206 for (i = 0; cmdtable[i].string; i++) {
1207 if (cmdtable[i].command == command) {
1208 cmd = cmdtable[i].string;
1216 SR_PRIV int sr_scpi_cmd(const struct sr_dev_inst *sdi,
1217 const struct scpi_command *cmdtable,
1218 int channel_command, const char *channel_name,
1221 struct sr_scpi_dev_inst *scpi;
1224 const char *channel_cmd;
1229 if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) {
1230 /* Device does not implement this command, that's OK. */
1234 g_mutex_lock(&scpi->scpi_mutex);
1236 /* Select channel. */
1237 channel_cmd = sr_scpi_cmd_get(cmdtable, channel_command);
1238 if (channel_cmd && channel_name &&
1239 g_strcmp0(channel_name, scpi->actual_channel_name)) {
1240 sr_spew("sr_scpi_cmd(): new channel = %s", channel_name);
1241 g_free(scpi->actual_channel_name);
1242 scpi->actual_channel_name = g_strdup(channel_name);
1243 ret = scpi_send(scpi, channel_cmd, channel_name);
1248 va_start(args, command);
1249 ret = scpi_send_variadic(scpi, cmd, args);
1252 g_mutex_unlock(&scpi->scpi_mutex);
1257 SR_PRIV int sr_scpi_cmd_resp(const struct sr_dev_inst *sdi,
1258 const struct scpi_command *cmdtable,
1259 int channel_command, const char *channel_name,
1260 GVariant **gvar, const GVariantType *gvtype, int command, ...)
1262 struct sr_scpi_dev_inst *scpi;
1264 const char *channel_cmd;
1274 if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) {
1275 /* Device does not implement this command. */
1279 g_mutex_lock(&scpi->scpi_mutex);
1281 /* Select channel. */
1282 channel_cmd = sr_scpi_cmd_get(cmdtable, channel_command);
1283 if (channel_cmd && channel_name &&
1284 g_strcmp0(channel_name, scpi->actual_channel_name)) {
1285 sr_spew("sr_scpi_cmd_get(): new channel = %s", channel_name);
1286 g_free(scpi->actual_channel_name);
1287 scpi->actual_channel_name = g_strdup(channel_name);
1288 ret = scpi_send(scpi, channel_cmd, channel_name);
1293 va_start(args, command);
1294 ret = scpi_send_variadic(scpi, cmd, args);
1297 g_mutex_unlock(&scpi->scpi_mutex);
1301 response = g_string_sized_new(1024);
1302 ret = scpi_get_data(scpi, NULL, &response);
1304 g_mutex_unlock(&scpi->scpi_mutex);
1306 g_string_free(response, TRUE);
1310 g_mutex_unlock(&scpi->scpi_mutex);
1312 /* Get rid of trailing linefeed if present */
1313 if (response->len >= 1 && response->str[response->len - 1] == '\n')
1314 g_string_truncate(response, response->len - 1);
1316 /* Get rid of trailing carriage return if present */
1317 if (response->len >= 1 && response->str[response->len - 1] == '\r')
1318 g_string_truncate(response, response->len - 1);
1320 s = g_string_free(response, FALSE);
1323 if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_BOOLEAN)) {
1324 if ((ret = parse_strict_bool(s, &b)) == SR_OK)
1325 *gvar = g_variant_new_boolean(b);
1326 } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_DOUBLE)) {
1327 if ((ret = sr_atod_ascii(s, &d)) == SR_OK)
1328 *gvar = g_variant_new_double(d);
1329 } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_STRING)) {
1330 *gvar = g_variant_new_string(s);
1332 sr_err("Unable to convert to desired GVariant type.");