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 = vsnprintf(NULL, 0, format, args_copy);
156 /* Allocate buffer and write out command. */
157 buf = g_malloc0(len + 2);
158 vsprintf(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,
182 return scpi_send_variadic(scpi, format, args);
186 * Send data to SCPI device without mutex.
188 * TODO: This is only implemented in TcpRaw, but never used.
189 * TODO: Use Mutex at all?
191 * @param scpi Previously initialised SCPI device structure.
192 * @param buf Buffer with data to send.
193 * @param len Number of bytes to send.
195 * @return Number of bytes read, or SR_ERR upon failure.
197 static int scpi_write_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen)
199 return scpi->write_data(scpi->priv, buf, maxlen);
203 * Read part of a response from SCPI device without mutex.
205 * @param scpi Previously initialised SCPI device structure.
206 * @param buf Buffer to store result.
207 * @param maxlen Maximum number of bytes to read.
209 * @return Number of bytes read, or SR_ERR upon failure.
211 static int scpi_read_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen)
213 return scpi->read_data(scpi->priv, buf, maxlen);
217 * Do a non-blocking read of up to the allocated length, and
218 * check if a timeout has occured, without mutex.
220 * @param scpi Previously initialised SCPI device structure.
221 * @param response Buffer to which the response is appended.
222 * @param abs_timeout_us Absolute timeout in microseconds
224 * @return read length on success, SR_ERR* on failure.
226 static int scpi_read_response(struct sr_scpi_dev_inst *scpi,
227 GString *response, gint64 abs_timeout_us)
231 space = response->allocated_len - response->len;
232 len = scpi->read_data(scpi->priv, &response->str[response->len], space);
235 sr_err("Incompletely read SCPI response.");
240 g_string_set_size(response, response->len + len);
244 if (g_get_monotonic_time() > abs_timeout_us) {
245 sr_err("Timed out waiting for SCPI response.");
246 return SR_ERR_TIMEOUT;
253 * Send a SCPI command, receive the reply and store the reply in
254 * scpi_response, without mutex.
256 * @param scpi Previously initialised SCPI device structure.
257 * @param command The SCPI command to send to the device.
258 * @param scpi_response Pointer where to store the SCPI response.
260 * @return SR_OK on success, SR_ERR on failure.
262 static int scpi_get_data(struct sr_scpi_dev_inst *scpi,
263 const char *command, GString **scpi_response)
269 va_list empty_va_list;
271 /* Optionally send caller provided command. */
273 if (scpi_send(scpi, command, empty_va_list) != SR_OK)
277 /* Initiate SCPI read operation. */
278 if (sr_scpi_read_begin(scpi) != SR_OK)
281 /* Keep reading until completion or until timeout. */
282 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
284 response = *scpi_response;
286 while (!sr_scpi_read_complete(scpi)) {
287 /* Resize the buffer when free space drops below a threshold. */
288 space = response->allocated_len - response->len;
290 int oldlen = response->len;
291 g_string_set_size(response, oldlen + 1024);
292 g_string_set_size(response, oldlen);
295 /* Read another chunk of the response. */
296 ret = scpi_read_response(scpi, response, timeout);
301 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
307 SR_PRIV GSList *sr_scpi_scan(struct drv_context *drvc, GSList *options,
308 struct sr_dev_inst *(*probe_device)(struct sr_scpi_dev_inst *scpi))
310 GSList *resources, *l, *devices;
311 struct sr_dev_inst *sdi;
312 const char *resource = NULL;
313 const char *serialcomm = NULL;
317 for (l = options; l; l = l->next) {
318 struct sr_config *src = l->data;
321 resource = g_variant_get_string(src->data, NULL);
323 case SR_CONF_SERIALCOMM:
324 serialcomm = g_variant_get_string(src->data, NULL);
330 for (i = 0; i < ARRAY_SIZE(scpi_devs); i++) {
331 if ((resource && strcmp(resource, scpi_devs[i]->prefix))
332 || !scpi_devs[i]->scan)
334 resources = scpi_devs[i]->scan(drvc);
335 for (l = resources; l; l = l->next) {
336 res = g_strsplit(l->data, ":", 2);
337 if (res[0] && (sdi = sr_scpi_scan_resource(drvc, res[0],
338 serialcomm ? serialcomm : res[1], probe_device))) {
339 devices = g_slist_append(devices, sdi);
340 sdi->connection_id = g_strdup(l->data);
344 g_slist_free_full(resources, g_free);
347 if (!devices && resource) {
348 sdi = sr_scpi_scan_resource(drvc, resource, serialcomm, probe_device);
350 devices = g_slist_append(NULL, sdi);
353 /* Tack a copy of the newly found devices onto the driver list. */
355 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
360 SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(struct drv_context *drvc,
361 const char *resource, const char *serialcomm)
363 struct sr_scpi_dev_inst *scpi = NULL;
364 const struct sr_scpi_dev_inst *scpi_dev;
368 for (i = 0; i < ARRAY_SIZE(scpi_devs); i++) {
369 scpi_dev = scpi_devs[i];
370 if (!strncmp(resource, scpi_dev->prefix, strlen(scpi_dev->prefix))) {
371 sr_dbg("Opening %s device %s.", scpi_dev->name, resource);
372 scpi = g_malloc(sizeof(*scpi));
374 scpi->priv = g_malloc0(scpi->priv_size);
375 scpi->read_timeout_us = 1000 * 1000;
376 params = g_strsplit(resource, "/", 0);
377 if (scpi->dev_inst_new(scpi->priv, drvc, resource,
378 params, serialcomm) != SR_OK) {
393 * @param scpi Previously initialized SCPI device structure.
395 * @return SR_OK on success, SR_ERR on failure.
397 SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi)
399 g_mutex_init(&scpi->scpi_mutex);
401 return scpi->open(scpi);
405 * Add an event source for an SCPI device.
407 * @param session The session to add the event source to.
408 * @param scpi Previously initialized SCPI device structure.
409 * @param events Events to check for.
410 * @param timeout Max time to wait before the callback is called, ignored if 0.
411 * @param cb Callback function to add. Must not be NULL.
412 * @param cb_data Data for the callback function. Can be NULL.
414 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
415 * SR_ERR_MALLOC upon memory allocation errors.
417 SR_PRIV int sr_scpi_source_add(struct sr_session *session,
418 struct sr_scpi_dev_inst *scpi, int events, int timeout,
419 sr_receive_data_callback cb, void *cb_data)
421 return scpi->source_add(session, scpi->priv, events, timeout, cb, cb_data);
425 * Remove event source for an SCPI device.
427 * @param session The session to remove the event source from.
428 * @param scpi Previously initialized SCPI device structure.
430 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
431 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
434 SR_PRIV int sr_scpi_source_remove(struct sr_session *session,
435 struct sr_scpi_dev_inst *scpi)
437 return scpi->source_remove(session, scpi->priv);
441 * Send a SCPI command.
443 * @param scpi Previously initialized SCPI device structure.
444 * @param format Format string, to be followed by any necessary arguments.
446 * @return SR_OK on success, SR_ERR on failure.
448 SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi,
449 const char *format, ...)
454 va_start(args, format);
455 g_mutex_lock(&scpi->scpi_mutex);
456 ret = scpi_send_variadic(scpi, format, args);
457 g_mutex_unlock(&scpi->scpi_mutex);
464 * Send a SCPI command with a variadic argument list.
466 * @param scpi Previously initialized SCPI device structure.
467 * @param format Format string.
468 * @param args Argument list.
470 * @return SR_OK on success, SR_ERR on failure.
472 SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi,
473 const char *format, va_list args)
477 g_mutex_lock(&scpi->scpi_mutex);
478 ret = scpi_send_variadic(scpi, format, args);
479 g_mutex_unlock(&scpi->scpi_mutex);
485 * Begin receiving an SCPI reply.
487 * @param scpi Previously initialised SCPI device structure.
489 * @return SR_OK on success, SR_ERR on failure.
491 SR_PRIV int sr_scpi_read_begin(struct sr_scpi_dev_inst *scpi)
493 return scpi->read_begin(scpi->priv);
497 * Read part of a response from SCPI device.
499 * @param scpi Previously initialised SCPI device structure.
500 * @param buf Buffer to store result.
501 * @param maxlen Maximum number of bytes to read.
503 * @return Number of bytes read, or SR_ERR upon failure.
505 SR_PRIV int sr_scpi_read_data(struct sr_scpi_dev_inst *scpi,
506 char *buf, int maxlen)
510 g_mutex_lock(&scpi->scpi_mutex);
511 ret = scpi_read_data(scpi, buf, maxlen);
512 g_mutex_unlock(&scpi->scpi_mutex);
518 * Send data to SCPI device.
520 * TODO: This is only implemented in TcpRaw, but never used.
521 * TODO: Use Mutex at all?
523 * @param scpi Previously initialised SCPI device structure.
524 * @param buf Buffer with data to send.
525 * @param len Number of bytes to send.
527 * @return Number of bytes read, or SR_ERR upon failure.
529 SR_PRIV int sr_scpi_write_data(struct sr_scpi_dev_inst *scpi,
530 char *buf, int maxlen)
534 g_mutex_lock(&scpi->scpi_mutex);
535 ret = scpi_write_data(scpi, buf, maxlen);
536 g_mutex_unlock(&scpi->scpi_mutex);
542 * Check whether a complete SCPI response has been received.
544 * @param scpi Previously initialised SCPI device structure.
546 * @return 1 if complete, 0 otherwise.
548 SR_PRIV int sr_scpi_read_complete(struct sr_scpi_dev_inst *scpi)
550 return scpi->read_complete(scpi->priv);
556 * @param scpi Previously initialized SCPI device structure.
558 * @return SR_OK on success, SR_ERR on failure.
560 SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi)
564 g_mutex_lock(&scpi->scpi_mutex);
565 ret = scpi->close(scpi);
566 g_mutex_unlock(&scpi->scpi_mutex);
567 g_mutex_clear(&scpi->scpi_mutex);
575 * @param scpi Previously initialized SCPI device structure. If NULL,
576 * this function does nothing.
578 SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi)
583 scpi->free(scpi->priv);
589 * Send a SCPI command, receive the reply and store the reply in scpi_response.
591 * @param scpi Previously initialised SCPI device structure.
592 * @param command The SCPI command to send to the device (can be NULL).
593 * @param scpi_response Pointer where to store the SCPI response.
595 * @return SR_OK on success, SR_ERR* on failure.
597 SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi,
598 const char *command, char **scpi_response)
601 response = g_string_sized_new(1024);
603 if (sr_scpi_get_data(scpi, command, &response) != SR_OK) {
605 g_string_free(response, TRUE);
609 /* Get rid of trailing linefeed if present */
610 if (response->len >= 1 && response->str[response->len - 1] == '\n')
611 g_string_truncate(response, response->len - 1);
613 /* Get rid of trailing carriage return if present */
614 if (response->len >= 1 && response->str[response->len - 1] == '\r')
615 g_string_truncate(response, response->len - 1);
617 sr_spew("Got response: '%.70s', length %" G_GSIZE_FORMAT ".",
618 response->str, response->len);
620 *scpi_response = g_string_free(response, FALSE);
626 * Do a non-blocking read of up to the allocated length, and
627 * check if a timeout has occured.
629 * @param scpi Previously initialised SCPI device structure.
630 * @param response Buffer to which the response is appended.
631 * @param abs_timeout_us Absolute timeout in microseconds
633 * @return read length on success, SR_ERR* on failure.
635 SR_PRIV int sr_scpi_read_response(struct sr_scpi_dev_inst *scpi,
636 GString *response, gint64 abs_timeout_us)
640 g_mutex_lock(&scpi->scpi_mutex);
641 ret = scpi_read_response(scpi, response, abs_timeout_us);
642 g_mutex_unlock(&scpi->scpi_mutex);
647 SR_PRIV int sr_scpi_get_data(struct sr_scpi_dev_inst *scpi,
648 const char *command, GString **scpi_response)
652 g_mutex_lock(&scpi->scpi_mutex);
653 ret = scpi_get_data(scpi, command, scpi_response);
654 g_mutex_unlock(&scpi->scpi_mutex);
660 * Send a SCPI command, read the reply, parse it as a bool value and store the
661 * result in scpi_response.
663 * @param scpi Previously initialised SCPI device structure.
664 * @param command The SCPI command to send to the device (can be NULL).
665 * @param scpi_response Pointer where to store the parsed result.
667 * @return SR_OK on success, SR_ERR* on failure.
669 SR_PRIV int sr_scpi_get_bool(struct sr_scpi_dev_inst *scpi,
670 const char *command, gboolean *scpi_response)
677 ret = sr_scpi_get_string(scpi, command, &response);
678 if (ret != SR_OK && !response)
681 if (parse_strict_bool(response, scpi_response) == SR_OK)
692 * Send a SCPI command, read the reply, parse it as an integer and store the
693 * result in scpi_response.
695 * @param scpi Previously initialised SCPI device structure.
696 * @param command The SCPI command to send to the device (can be NULL).
697 * @param scpi_response Pointer where to store the parsed result.
699 * @return SR_OK on success, SR_ERR* on failure.
701 SR_PRIV int sr_scpi_get_int(struct sr_scpi_dev_inst *scpi,
702 const char *command, int *scpi_response)
709 ret = sr_scpi_get_string(scpi, command, &response);
710 if (ret != SR_OK && !response)
713 if (sr_atoi(response, scpi_response) == SR_OK)
724 * Send a SCPI command, read the reply, parse it as a float and store the
725 * result in scpi_response.
727 * @param scpi Previously initialised SCPI device structure.
728 * @param command The SCPI command to send to the device (can be NULL).
729 * @param scpi_response Pointer where to store the parsed result.
731 * @return SR_OK on success, SR_ERR* on failure.
733 SR_PRIV int sr_scpi_get_float(struct sr_scpi_dev_inst *scpi,
734 const char *command, float *scpi_response)
741 ret = sr_scpi_get_string(scpi, command, &response);
742 if (ret != SR_OK && !response)
745 if (sr_atof_ascii(response, scpi_response) == SR_OK)
756 * Send a SCPI command, read the reply, parse it as a double and store the
757 * result in scpi_response.
759 * @param scpi Previously initialised SCPI device structure.
760 * @param command The SCPI command to send to the device (can be NULL).
761 * @param scpi_response Pointer where to store the parsed result.
763 * @return SR_OK on success, SR_ERR* on failure.
765 SR_PRIV int sr_scpi_get_double(struct sr_scpi_dev_inst *scpi,
766 const char *command, double *scpi_response)
773 ret = sr_scpi_get_string(scpi, command, &response);
774 if (ret != SR_OK && !response)
777 if (sr_atod_ascii(response, scpi_response) == SR_OK)
788 * Send a SCPI *OPC? command, read the reply and return the result of the
791 * @param scpi Previously initialised SCPI device structure.
793 * @return SR_OK on success, SR_ERR* on failure.
795 SR_PRIV int sr_scpi_get_opc(struct sr_scpi_dev_inst *scpi)
800 for (i = 0; i < SCPI_READ_RETRIES; i++) {
802 sr_scpi_get_bool(scpi, SCPI_CMD_OPC, &opc);
805 g_usleep(SCPI_READ_RETRY_TIMEOUT_US);
812 * Send a SCPI command, read the reply, parse it as comma separated list of
813 * floats and store the as an result in scpi_response.
815 * @param scpi Previously initialised SCPI device structure.
816 * @param command The SCPI command to send to the device (can be NULL).
817 * @param scpi_response Pointer where to store the parsed result.
819 * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing
820 * error or upon no response. The allocated response must be freed by
821 * the caller in the case of an SR_OK as well as in the case of
824 SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi,
825 const char *command, GArray **scpi_response)
830 gchar **ptr, **tokens;
831 GArray *response_array;
836 ret = sr_scpi_get_string(scpi, command, &response);
837 if (ret != SR_OK && !response)
840 tokens = g_strsplit(response, ",", 0);
843 response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256);
846 if (sr_atof_ascii(*ptr, &tmp) == SR_OK)
847 response_array = g_array_append_val(response_array,
857 if (ret != SR_OK && response_array->len == 0) {
858 g_array_free(response_array, TRUE);
859 *scpi_response = NULL;
863 *scpi_response = response_array;
869 * Send a SCPI command, read the reply, parse it as comma separated list of
870 * unsigned 8 bit integers and store the as an result in scpi_response.
872 * @param scpi Previously initialised SCPI device structure.
873 * @param command The SCPI command to send to the device (can be NULL).
874 * @param scpi_response Pointer where to store the parsed result.
876 * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing
877 * error or upon no response. The allocated response must be freed by
878 * the caller in the case of an SR_OK as well as in the case of
881 SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi,
882 const char *command, GArray **scpi_response)
886 gchar **ptr, **tokens;
887 GArray *response_array;
892 ret = sr_scpi_get_string(scpi, command, &response);
893 if (ret != SR_OK && !response)
896 tokens = g_strsplit(response, ",", 0);
899 response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256);
902 if (sr_atoi(*ptr, &tmp) == SR_OK)
903 response_array = g_array_append_val(response_array,
913 if (response_array->len == 0) {
914 g_array_free(response_array, TRUE);
915 *scpi_response = NULL;
919 *scpi_response = response_array;
925 * Send a SCPI command, read the reply, parse it as binary data with a
926 * "definite length block" header and store the as an result in scpi_response.
928 * @param scpi Previously initialised SCPI device structure.
929 * @param command The SCPI command to send to the device (can be NULL).
930 * @param scpi_response Pointer where to store the parsed result.
932 * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing
933 * error or upon no response. The allocated response must be freed by
934 * the caller in the case of an SR_OK as well as in the case of
937 SR_PRIV int sr_scpi_get_block(struct sr_scpi_dev_inst *scpi,
938 const char *command, GByteArray **scpi_response)
946 va_list empty_va_list;
948 g_mutex_lock(&scpi->scpi_mutex);
951 if (scpi_send(scpi, command, empty_va_list) != SR_OK) {
952 g_mutex_unlock(&scpi->scpi_mutex);
956 if (sr_scpi_read_begin(scpi) != SR_OK) {
957 g_mutex_unlock(&scpi->scpi_mutex);
962 * Assume an initial maximum length, optionally gets adjusted below.
963 * Prepare a NULL return value for when error paths will be taken.
965 response = g_string_sized_new(1024);
967 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
969 *scpi_response = NULL;
971 /* Get (the first chunk of) the response. */
972 while (response->len < 2) {
973 ret = scpi_read_response(scpi, response, timeout);
975 g_mutex_unlock(&scpi->scpi_mutex);
976 g_string_free(response, TRUE);
982 * SCPI protocol data blocks are preceeded with a length spec.
983 * The length spec consists of a '#' marker, one digit which
984 * specifies the character count of the length spec, and the
985 * respective number of characters which specify the data block's
986 * length. Raw data bytes follow (thus one must no longer assume
987 * that the received input stream would be an ASCIIZ string).
989 * Get the data block length, and strip off the length spec from
990 * the input buffer, leaving just the data bytes.
992 if (response->str[0] != '#') {
993 g_mutex_unlock(&scpi->scpi_mutex);
994 g_string_free(response, TRUE);
997 buf[0] = response->str[1];
999 ret = sr_atol(buf, &llen);
1000 if ((ret != SR_OK) || (llen == 0)) {
1001 g_mutex_unlock(&scpi->scpi_mutex);
1002 g_string_free(response, TRUE);
1006 while (response->len < (unsigned long)(2 + llen)) {
1007 ret = scpi_read_response(scpi, response, timeout);
1009 g_mutex_unlock(&scpi->scpi_mutex);
1010 g_string_free(response, TRUE);
1015 memcpy(buf, &response->str[2], llen);
1017 ret = sr_atol(buf, &datalen);
1018 if ((ret != SR_OK) || (datalen == 0)) {
1019 g_mutex_unlock(&scpi->scpi_mutex);
1020 g_string_free(response, TRUE);
1023 g_string_erase(response, 0, 2 + llen);
1026 * If the initially assumed length does not cover the data block
1027 * length, then re-allocate the buffer size to the now known
1028 * length, and keep reading more chunks of response data.
1030 if (response->len < (unsigned long)(datalen)) {
1031 int oldlen = response->len;
1032 g_string_set_size(response, datalen);
1033 g_string_set_size(response, oldlen);
1036 while (response->len < (unsigned long)(datalen)) {
1037 ret = scpi_read_response(scpi, response, timeout);
1039 g_mutex_unlock(&scpi->scpi_mutex);
1040 g_string_free(response, TRUE);
1044 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
1047 g_mutex_unlock(&scpi->scpi_mutex);
1049 /* Convert received data to byte array. */
1050 *scpi_response = g_byte_array_new_take(
1051 (guint8*)g_string_free(response, FALSE), datalen);
1057 * Send the *IDN? SCPI command, receive the reply, parse it and store the
1058 * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer.
1060 * The hw_info structure must be freed by the caller via sr_scpi_hw_info_free().
1062 * @param scpi Previously initialised SCPI device structure.
1063 * @param scpi_response Pointer where to store the hw_info structure.
1065 * @return SR_OK upon success, SR_ERR* on failure.
1067 SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi,
1068 struct sr_scpi_hw_info **scpi_response)
1070 int num_tokens, ret;
1073 struct sr_scpi_hw_info *hw_info;
1079 ret = sr_scpi_get_string(scpi, SCPI_CMD_IDN, &response);
1080 if (ret != SR_OK && !response)
1084 * The response to a '*IDN?' is specified by the SCPI spec. It contains
1085 * a comma-separated list containing the manufacturer name, instrument
1086 * model, serial number of the instrument and the firmware version.
1088 tokens = g_strsplit(response, ",", 0);
1090 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
1092 if (num_tokens < 4) {
1093 sr_dbg("IDN response not according to spec: %80.s.", response);
1100 hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info));
1102 idn_substr = g_strstr_len(tokens[0], -1, "IDN ");
1103 if (idn_substr == NULL)
1104 hw_info->manufacturer = g_strstrip(g_strdup(tokens[0]));
1106 hw_info->manufacturer = g_strstrip(g_strdup(idn_substr + 4));
1108 hw_info->model = g_strstrip(g_strdup(tokens[1]));
1109 hw_info->serial_number = g_strstrip(g_strdup(tokens[2]));
1110 hw_info->firmware_version = g_strstrip(g_strdup(tokens[3]));
1114 *scpi_response = hw_info;
1120 * Free a sr_scpi_hw_info struct.
1122 * @param hw_info Pointer to the struct to free. If NULL, this
1123 * function does nothing.
1125 SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info)
1130 g_free(hw_info->manufacturer);
1131 g_free(hw_info->model);
1132 g_free(hw_info->serial_number);
1133 g_free(hw_info->firmware_version);
1137 SR_PRIV const char *sr_vendor_alias(const char *raw_vendor)
1141 for (i = 0; i < ARRAY_SIZE(scpi_vendors); i++) {
1142 if (!g_ascii_strcasecmp(raw_vendor, scpi_vendors[i][0]))
1143 return scpi_vendors[i][1];
1149 SR_PRIV const char *sr_scpi_cmd_get(const struct scpi_command *cmdtable, int command)
1158 for (i = 0; cmdtable[i].string; i++) {
1159 if (cmdtable[i].command == command) {
1160 cmd = cmdtable[i].string;
1168 SR_PRIV int sr_scpi_cmd(const struct sr_dev_inst *sdi, const struct scpi_command *cmdtable,
1171 struct sr_scpi_dev_inst *scpi;
1176 if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) {
1177 /* Device does not implement this command, that's OK. */
1182 va_start(args, command);
1183 ret = sr_scpi_send_variadic(scpi, cmd, args);
1189 SR_PRIV int sr_scpi_cmd_resp(const struct sr_dev_inst *sdi,
1190 const struct scpi_command *cmdtable,
1191 GVariant **gvar, const GVariantType *gvtype, int command, ...)
1193 struct sr_scpi_dev_inst *scpi;
1204 if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) {
1205 /* Device does not implement this command. */
1209 g_mutex_lock(&scpi->scpi_mutex);
1211 va_start(args, command);
1212 ret = scpi_send_variadic(scpi, cmd, args);
1215 g_mutex_unlock(&scpi->scpi_mutex);
1219 response = g_string_sized_new(1024);
1220 ret = scpi_get_data(scpi, NULL, &response);
1222 g_mutex_unlock(&scpi->scpi_mutex);
1224 g_string_free(response, TRUE);
1228 g_mutex_unlock(&scpi->scpi_mutex);
1230 /* Get rid of trailing linefeed if present */
1231 if (response->len >= 1 && response->str[response->len - 1] == '\n')
1232 g_string_truncate(response, response->len - 1);
1234 /* Get rid of trailing carriage return if present */
1235 if (response->len >= 1 && response->str[response->len - 1] == '\r')
1236 g_string_truncate(response, response->len - 1);
1238 s = g_string_free(response, FALSE);
1241 if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_BOOLEAN)) {
1242 if ((ret = parse_strict_bool(s, &b)) == SR_OK)
1243 *gvar = g_variant_new_boolean(b);
1244 } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_DOUBLE)) {
1245 if ((ret = sr_atod_ascii(s, &d)) == SR_OK)
1246 *gvar = g_variant_new_double(d);
1247 } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_STRING)) {
1248 *gvar = g_variant_new_string(s);
1250 sr_err("Unable to convert to desired GVariant type.");