]> sigrok.org Git - libsigrok.git/blob - src/scpi/scpi.c
scpi: Synchronize read, write and write+read operations.
[libsigrok.git] / src / scpi / scpi.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5  * Copyright (C) 2015 Bert Vermeulen <bert@biot.com>
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <config.h>
22 #include <glib.h>
23 #include <string.h>
24 #include <libsigrok/libsigrok.h>
25 #include "libsigrok-internal.h"
26 #include "scpi.h"
27
28 #define LOG_PREFIX "scpi"
29
30 #define SCPI_READ_RETRIES 100
31 #define SCPI_READ_RETRY_TIMEOUT_US (10 * 1000)
32
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" },
40 };
41
42 /**
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.
46  *
47  * @param str String to convert.
48  * @param ret Pointer to a gboolean where the result of the conversion will be
49  * stored.
50  *
51  * @return SR_OK on success, SR_ERR on failure.
52  */
53 static int parse_strict_bool(const char *str, gboolean *ret)
54 {
55         if (!str)
56                 return SR_ERR_ARG;
57
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)) {
64                 *ret = TRUE;
65                 return SR_OK;
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)) {
72                 *ret = FALSE;
73                 return SR_OK;
74         }
75
76         return SR_ERR;
77 }
78
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;
86
87 static const struct sr_scpi_dev_inst *scpi_devs[] = {
88         &scpi_tcp_raw_dev,
89         &scpi_tcp_rigol_dev,
90 #ifdef HAVE_LIBUSB_1_0
91         &scpi_usbtmc_libusb_dev,
92 #endif
93 #if HAVE_RPC
94         &scpi_vxi_dev,
95 #endif
96 #ifdef HAVE_LIBREVISA
97         &scpi_visa_dev,
98 #endif
99 #ifdef HAVE_LIBGPIB
100         &scpi_libgpib_dev,
101 #endif
102 #ifdef HAVE_LIBSERIALPORT
103         &scpi_serial_dev, /* Must be last as it matches any resource. */
104 #endif
105 };
106
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))
110 {
111         struct sr_scpi_dev_inst *scpi;
112         struct sr_dev_inst *sdi;
113
114         if (!(scpi = scpi_dev_inst_new(drvc, resource, serialcomm)))
115                 return NULL;
116
117         if (sr_scpi_open(scpi) != SR_OK) {
118                 sr_info("Couldn't open SCPI device.");
119                 sr_scpi_free(scpi);
120                 return NULL;
121         };
122
123         sdi = probe_device(scpi);
124
125         sr_scpi_close(scpi);
126
127         if (sdi)
128                 sdi->status = SR_ST_INACTIVE;
129         else
130                 sr_scpi_free(scpi);
131
132         return sdi;
133 }
134
135 /**
136  * Send a SCPI command with a variadic argument list without mutex.
137  *
138  * @param scpi Previously initialized SCPI device structure.
139  * @param format Format string.
140  * @param args Argument list.
141  *
142  * @return SR_OK on success, SR_ERR on failure.
143  */
144 static int scpi_send_variadic(struct sr_scpi_dev_inst *scpi,
145                          const char *format, va_list args)
146 {
147         va_list args_copy;
148         char *buf;
149         int len, ret;
150
151         /* Get length of buffer required. */
152         va_copy(args_copy, args);
153         len = vsnprintf(NULL, 0, format, args_copy);
154         va_end(args_copy);
155
156         /* Allocate buffer and write out command. */
157         buf = g_malloc0(len + 2);
158         vsprintf(buf, format, args);
159         if (buf[len - 1] != '\n')
160                 buf[len] = '\n';
161
162         /* Send command. */
163         ret = scpi->send(scpi->priv, buf);
164
165         /* Free command buffer. */
166         g_free(buf);
167
168         return ret;
169 }
170
171 /**
172  * Send a SCPI command without mutex.
173  *
174  * @param scpi Previously initialized SCPI device structure.
175  * @param format Format string, to be followed by any necessary arguments.
176  *
177  * @return SR_OK on success, SR_ERR on failure.
178  */
179 static int scpi_send(struct sr_scpi_dev_inst *scpi, const char *format,
180                                         va_list args)
181 {
182         return scpi_send_variadic(scpi, format, args);
183 }
184
185 /**
186  * Send data to SCPI device without mutex.
187  *
188  * TODO: This is only implemented in TcpRaw, but never used.
189  * TODO: Use Mutex at all?
190  *
191  * @param scpi Previously initialised SCPI device structure.
192  * @param buf Buffer with data to send.
193  * @param len Number of bytes to send.
194  *
195  * @return Number of bytes read, or SR_ERR upon failure.
196  */
197 static int scpi_write_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen)
198 {
199         return scpi->write_data(scpi->priv, buf, maxlen);
200 }
201
202 /**
203  * Read part of a response from SCPI device without mutex.
204  *
205  * @param scpi Previously initialised SCPI device structure.
206  * @param buf Buffer to store result.
207  * @param maxlen Maximum number of bytes to read.
208  *
209  * @return Number of bytes read, or SR_ERR upon failure.
210  */
211 static int scpi_read_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen)
212 {
213         return scpi->read_data(scpi->priv, buf, maxlen);
214 }
215
216 /**
217  * Do a non-blocking read of up to the allocated length, and
218  * check if a timeout has occured, without mutex.
219  *
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
223  *
224  * @return read length on success, SR_ERR* on failure.
225  */
226 static int scpi_read_response(struct sr_scpi_dev_inst *scpi,
227                                 GString *response, gint64 abs_timeout_us)
228 {
229         int len, space;
230
231         space = response->allocated_len - response->len;
232         len = scpi->read_data(scpi->priv, &response->str[response->len], space);
233
234         if (len < 0) {
235                 sr_err("Incompletely read SCPI response.");
236                 return SR_ERR;
237         }
238
239         if (len > 0) {
240                 g_string_set_size(response, response->len + len);
241                 return len;
242         }
243
244         if (g_get_monotonic_time() > abs_timeout_us) {
245                 sr_err("Timed out waiting for SCPI response.");
246                 return SR_ERR_TIMEOUT;
247         }
248
249         return 0;
250 }
251
252 /**
253  * Send a SCPI command, receive the reply and store the reply in
254  * scpi_response, without mutex.
255  *
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.
259  *
260  * @return SR_OK on success, SR_ERR on failure.
261  */
262 static int scpi_get_data(struct sr_scpi_dev_inst *scpi,
263                                 const char *command, GString **scpi_response)
264 {
265         int ret;
266         GString *response;
267         int space;
268         gint64 timeout;
269         va_list empty_va_list;
270
271         /* Optionally send caller provided command. */
272         if (command) {
273                 if (scpi_send(scpi, command, empty_va_list) != SR_OK)
274                         return SR_ERR;
275         }
276
277         /* Initiate SCPI read operation. */
278         if (sr_scpi_read_begin(scpi) != SR_OK)
279                 return SR_ERR;
280
281         /* Keep reading until completion or until timeout. */
282         timeout = g_get_monotonic_time() + scpi->read_timeout_us;
283
284         response = *scpi_response;
285
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;
289                 if (space < 128) {
290                         int oldlen = response->len;
291                         g_string_set_size(response, oldlen + 1024);
292                         g_string_set_size(response, oldlen);
293                 }
294
295                 /* Read another chunk of the response. */
296                 ret = scpi_read_response(scpi, response, timeout);
297
298                 if (ret < 0)
299                         return ret;
300                 if (ret > 0)
301                         timeout = g_get_monotonic_time() + scpi->read_timeout_us;
302         }
303
304         return SR_OK;
305 }
306
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))
309 {
310         GSList *resources, *l, *devices;
311         struct sr_dev_inst *sdi;
312         const char *resource = NULL;
313         const char *serialcomm = NULL;
314         gchar **res;
315         unsigned i;
316
317         for (l = options; l; l = l->next) {
318                 struct sr_config *src = l->data;
319                 switch (src->key) {
320                 case SR_CONF_CONN:
321                         resource = g_variant_get_string(src->data, NULL);
322                         break;
323                 case SR_CONF_SERIALCOMM:
324                         serialcomm = g_variant_get_string(src->data, NULL);
325                         break;
326                 }
327         }
328
329         devices = 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)
333                         continue;
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);
341                         }
342                         g_strfreev(res);
343                 }
344                 g_slist_free_full(resources, g_free);
345         }
346
347         if (!devices && resource) {
348                 sdi = sr_scpi_scan_resource(drvc, resource, serialcomm, probe_device);
349                 if (sdi)
350                         devices = g_slist_append(NULL, sdi);
351         }
352
353         /* Tack a copy of the newly found devices onto the driver list. */
354         if (devices)
355                 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
356
357         return devices;
358 }
359
360 SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(struct drv_context *drvc,
361                 const char *resource, const char *serialcomm)
362 {
363         struct sr_scpi_dev_inst *scpi = NULL;
364         const struct sr_scpi_dev_inst *scpi_dev;
365         gchar **params;
366         unsigned i;
367
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));
373                         *scpi = *scpi_dev;
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) {
379                                 sr_scpi_free(scpi);
380                                 scpi = NULL;
381                         }
382                         g_strfreev(params);
383                         break;
384                 }
385         }
386
387         return scpi;
388 }
389
390 /**
391  * Open SCPI device.
392  *
393  * @param scpi Previously initialized SCPI device structure.
394  *
395  * @return SR_OK on success, SR_ERR on failure.
396  */
397 SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi)
398 {
399         g_mutex_init(&scpi->scpi_mutex);
400
401         return scpi->open(scpi);
402 }
403
404 /**
405  * Add an event source for an SCPI device.
406  *
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.
413  *
414  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
415  *         SR_ERR_MALLOC upon memory allocation errors.
416  */
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)
420 {
421         return scpi->source_add(session, scpi->priv, events, timeout, cb, cb_data);
422 }
423
424 /**
425  * Remove event source for an SCPI device.
426  *
427  * @param session The session to remove the event source from.
428  * @param scpi Previously initialized SCPI device structure.
429  *
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
432  *         internal errors.
433  */
434 SR_PRIV int sr_scpi_source_remove(struct sr_session *session,
435                 struct sr_scpi_dev_inst *scpi)
436 {
437         return scpi->source_remove(session, scpi->priv);
438 }
439
440 /**
441  * Send a SCPI command.
442  *
443  * @param scpi Previously initialized SCPI device structure.
444  * @param format Format string, to be followed by any necessary arguments.
445  *
446  * @return SR_OK on success, SR_ERR on failure.
447  */
448 SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi,
449                          const char *format, ...)
450 {
451         va_list args;
452         int ret;
453
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);
458         va_end(args);
459
460         return ret;
461 }
462
463 /**
464  * Send a SCPI command with a variadic argument list.
465  *
466  * @param scpi Previously initialized SCPI device structure.
467  * @param format Format string.
468  * @param args Argument list.
469  *
470  * @return SR_OK on success, SR_ERR on failure.
471  */
472 SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi,
473                          const char *format, va_list args)
474 {
475         int ret;
476
477         g_mutex_lock(&scpi->scpi_mutex);
478         ret = scpi_send_variadic(scpi, format, args);
479         g_mutex_unlock(&scpi->scpi_mutex);
480
481         return ret;
482 }
483
484 /**
485  * Begin receiving an SCPI reply.
486  *
487  * @param scpi Previously initialised SCPI device structure.
488  *
489  * @return SR_OK on success, SR_ERR on failure.
490  */
491 SR_PRIV int sr_scpi_read_begin(struct sr_scpi_dev_inst *scpi)
492 {
493         return scpi->read_begin(scpi->priv);
494 }
495
496 /**
497  * Read part of a response from SCPI device.
498  *
499  * @param scpi Previously initialised SCPI device structure.
500  * @param buf Buffer to store result.
501  * @param maxlen Maximum number of bytes to read.
502  *
503  * @return Number of bytes read, or SR_ERR upon failure.
504  */
505 SR_PRIV int sr_scpi_read_data(struct sr_scpi_dev_inst *scpi,
506                         char *buf, int maxlen)
507 {
508         int ret;
509
510         g_mutex_lock(&scpi->scpi_mutex);
511         ret = scpi_read_data(scpi, buf, maxlen);
512         g_mutex_unlock(&scpi->scpi_mutex);
513
514         return ret;
515 }
516
517 /**
518  * Send data to SCPI device.
519  *
520  * TODO: This is only implemented in TcpRaw, but never used.
521  * TODO: Use Mutex at all?
522  *
523  * @param scpi Previously initialised SCPI device structure.
524  * @param buf Buffer with data to send.
525  * @param len Number of bytes to send.
526  *
527  * @return Number of bytes read, or SR_ERR upon failure.
528  */
529 SR_PRIV int sr_scpi_write_data(struct sr_scpi_dev_inst *scpi,
530                         char *buf, int maxlen)
531 {
532         int ret;
533
534         g_mutex_lock(&scpi->scpi_mutex);
535         ret = scpi_write_data(scpi, buf, maxlen);
536         g_mutex_unlock(&scpi->scpi_mutex);
537
538         return ret;
539 }
540
541 /**
542  * Check whether a complete SCPI response has been received.
543  *
544  * @param scpi Previously initialised SCPI device structure.
545  *
546  * @return 1 if complete, 0 otherwise.
547  */
548 SR_PRIV int sr_scpi_read_complete(struct sr_scpi_dev_inst *scpi)
549 {
550         return scpi->read_complete(scpi->priv);
551 }
552
553 /**
554  * Close SCPI device.
555  *
556  * @param scpi Previously initialized SCPI device structure.
557  *
558  * @return SR_OK on success, SR_ERR on failure.
559  */
560 SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi)
561 {
562         int ret;
563
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);
568
569         return ret;
570 }
571
572 /**
573  * Free SCPI device.
574  *
575  * @param scpi Previously initialized SCPI device structure. If NULL,
576  *             this function does nothing.
577  */
578 SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi)
579 {
580         if (!scpi)
581                 return;
582
583         scpi->free(scpi->priv);
584         g_free(scpi->priv);
585         g_free(scpi);
586 }
587
588 /**
589  * Send a SCPI command, receive the reply and store the reply in scpi_response.
590  *
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.
594  *
595  * @return SR_OK on success, SR_ERR* on failure.
596  */
597 SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi,
598                                const char *command, char **scpi_response)
599 {
600         GString *response;
601         response = g_string_sized_new(1024);
602
603         if (sr_scpi_get_data(scpi, command, &response) != SR_OK) {
604                 if (response)
605                         g_string_free(response, TRUE);
606                 return SR_ERR;
607         }
608
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);
612
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);
616
617         sr_spew("Got response: '%.70s', length %" G_GSIZE_FORMAT ".",
618                 response->str, response->len);
619
620         *scpi_response = g_string_free(response, FALSE);
621
622         return SR_OK;
623 }
624
625 /**
626  * Do a non-blocking read of up to the allocated length, and
627  * check if a timeout has occured.
628  *
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
632  *
633  * @return read length on success, SR_ERR* on failure.
634  */
635 SR_PRIV int sr_scpi_read_response(struct sr_scpi_dev_inst *scpi,
636                                   GString *response, gint64 abs_timeout_us)
637 {
638         int ret;
639
640         g_mutex_lock(&scpi->scpi_mutex);
641         ret = scpi_read_response(scpi, response, abs_timeout_us);
642         g_mutex_unlock(&scpi->scpi_mutex);
643
644         return ret;
645 }
646
647 SR_PRIV int sr_scpi_get_data(struct sr_scpi_dev_inst *scpi,
648                              const char *command, GString **scpi_response)
649 {
650         int ret;
651
652         g_mutex_lock(&scpi->scpi_mutex);
653         ret = scpi_get_data(scpi, command, scpi_response);
654         g_mutex_unlock(&scpi->scpi_mutex);
655
656         return ret;
657 }
658
659 /**
660  * Send a SCPI command, read the reply, parse it as a bool value and store the
661  * result in scpi_response.
662  *
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.
666  *
667  * @return SR_OK on success, SR_ERR* on failure.
668  */
669 SR_PRIV int sr_scpi_get_bool(struct sr_scpi_dev_inst *scpi,
670                              const char *command, gboolean *scpi_response)
671 {
672         int ret;
673         char *response;
674
675         response = NULL;
676
677         ret = sr_scpi_get_string(scpi, command, &response);
678         if (ret != SR_OK && !response)
679                 return ret;
680
681         if (parse_strict_bool(response, scpi_response) == SR_OK)
682                 ret = SR_OK;
683         else
684                 ret = SR_ERR_DATA;
685
686         g_free(response);
687
688         return ret;
689 }
690
691 /**
692  * Send a SCPI command, read the reply, parse it as an integer and store the
693  * result in scpi_response.
694  *
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.
698  *
699  * @return SR_OK on success, SR_ERR* on failure.
700  */
701 SR_PRIV int sr_scpi_get_int(struct sr_scpi_dev_inst *scpi,
702                             const char *command, int *scpi_response)
703 {
704         int ret;
705         char *response;
706
707         response = NULL;
708
709         ret = sr_scpi_get_string(scpi, command, &response);
710         if (ret != SR_OK && !response)
711                 return ret;
712
713         if (sr_atoi(response, scpi_response) == SR_OK)
714                 ret = SR_OK;
715         else
716                 ret = SR_ERR_DATA;
717
718         g_free(response);
719
720         return ret;
721 }
722
723 /**
724  * Send a SCPI command, read the reply, parse it as a float and store the
725  * result in scpi_response.
726  *
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.
730  *
731  * @return SR_OK on success, SR_ERR* on failure.
732  */
733 SR_PRIV int sr_scpi_get_float(struct sr_scpi_dev_inst *scpi,
734                               const char *command, float *scpi_response)
735 {
736         int ret;
737         char *response;
738
739         response = NULL;
740
741         ret = sr_scpi_get_string(scpi, command, &response);
742         if (ret != SR_OK && !response)
743                 return ret;
744
745         if (sr_atof_ascii(response, scpi_response) == SR_OK)
746                 ret = SR_OK;
747         else
748                 ret = SR_ERR_DATA;
749
750         g_free(response);
751
752         return ret;
753 }
754
755 /**
756  * Send a SCPI command, read the reply, parse it as a double and store the
757  * result in scpi_response.
758  *
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.
762  *
763  * @return SR_OK on success, SR_ERR* on failure.
764  */
765 SR_PRIV int sr_scpi_get_double(struct sr_scpi_dev_inst *scpi,
766                                const char *command, double *scpi_response)
767 {
768         int ret;
769         char *response;
770
771         response = NULL;
772
773         ret = sr_scpi_get_string(scpi, command, &response);
774         if (ret != SR_OK && !response)
775                 return ret;
776
777         if (sr_atod_ascii(response, scpi_response) == SR_OK)
778                 ret = SR_OK;
779         else
780                 ret = SR_ERR_DATA;
781
782         g_free(response);
783
784         return ret;
785 }
786
787 /**
788  * Send a SCPI *OPC? command, read the reply and return the result of the
789  * command.
790  *
791  * @param scpi Previously initialised SCPI device structure.
792  *
793  * @return SR_OK on success, SR_ERR* on failure.
794  */
795 SR_PRIV int sr_scpi_get_opc(struct sr_scpi_dev_inst *scpi)
796 {
797         unsigned int i;
798         gboolean opc;
799
800         for (i = 0; i < SCPI_READ_RETRIES; i++) {
801                 opc = FALSE;
802                 sr_scpi_get_bool(scpi, SCPI_CMD_OPC, &opc);
803                 if (opc)
804                         return SR_OK;
805                 g_usleep(SCPI_READ_RETRY_TIMEOUT_US);
806         }
807
808         return SR_ERR;
809 }
810
811 /**
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.
814  *
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.
818  *
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
822  *         parsing error.
823  */
824 SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi,
825                                const char *command, GArray **scpi_response)
826 {
827         int ret;
828         float tmp;
829         char *response;
830         gchar **ptr, **tokens;
831         GArray *response_array;
832
833         response = NULL;
834         tokens = NULL;
835
836         ret = sr_scpi_get_string(scpi, command, &response);
837         if (ret != SR_OK && !response)
838                 return ret;
839
840         tokens = g_strsplit(response, ",", 0);
841         ptr = tokens;
842
843         response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256);
844
845         while (*ptr) {
846                 if (sr_atof_ascii(*ptr, &tmp) == SR_OK)
847                         response_array = g_array_append_val(response_array,
848                                                             tmp);
849                 else
850                         ret = SR_ERR_DATA;
851
852                 ptr++;
853         }
854         g_strfreev(tokens);
855         g_free(response);
856
857         if (ret != SR_OK && response_array->len == 0) {
858                 g_array_free(response_array, TRUE);
859                 *scpi_response = NULL;
860                 return SR_ERR_DATA;
861         }
862
863         *scpi_response = response_array;
864
865         return ret;
866 }
867
868 /**
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.
871  *
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.
875  *
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
879  *         parsing error.
880  */
881 SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi,
882                                const char *command, GArray **scpi_response)
883 {
884         int tmp, ret;
885         char *response;
886         gchar **ptr, **tokens;
887         GArray *response_array;
888
889         response = NULL;
890         tokens = NULL;
891
892         ret = sr_scpi_get_string(scpi, command, &response);
893         if (ret != SR_OK && !response)
894                 return ret;
895
896         tokens = g_strsplit(response, ",", 0);
897         ptr = tokens;
898
899         response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256);
900
901         while (*ptr) {
902                 if (sr_atoi(*ptr, &tmp) == SR_OK)
903                         response_array = g_array_append_val(response_array,
904                                                             tmp);
905                 else
906                         ret = SR_ERR_DATA;
907
908                 ptr++;
909         }
910         g_strfreev(tokens);
911         g_free(response);
912
913         if (response_array->len == 0) {
914                 g_array_free(response_array, TRUE);
915                 *scpi_response = NULL;
916                 return SR_ERR_DATA;
917         }
918
919         *scpi_response = response_array;
920
921         return ret;
922 }
923
924 /**
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.
927  *
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.
931  *
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
935  *         parsing error.
936  */
937 SR_PRIV int sr_scpi_get_block(struct sr_scpi_dev_inst *scpi,
938                                const char *command, GByteArray **scpi_response)
939 {
940         int ret;
941         GString* response;
942         char buf[10];
943         long llen;
944         long datalen;
945         gint64 timeout;
946         va_list empty_va_list;
947
948         g_mutex_lock(&scpi->scpi_mutex);
949
950         if (command)
951                 if (scpi_send(scpi, command, empty_va_list) != SR_OK) {
952                         g_mutex_unlock(&scpi->scpi_mutex);
953                         return SR_ERR;
954                 }
955
956         if (sr_scpi_read_begin(scpi) != SR_OK) {
957                 g_mutex_unlock(&scpi->scpi_mutex);
958                 return SR_ERR;
959         }
960
961         /*
962          * Assume an initial maximum length, optionally gets adjusted below.
963          * Prepare a NULL return value for when error paths will be taken.
964          */
965         response = g_string_sized_new(1024);
966
967         timeout = g_get_monotonic_time() + scpi->read_timeout_us;
968
969         *scpi_response = NULL;
970
971         /* Get (the first chunk of) the response. */
972         while (response->len < 2) {
973                 ret = scpi_read_response(scpi, response, timeout);
974                 if (ret < 0) {
975                         g_mutex_unlock(&scpi->scpi_mutex);
976                         g_string_free(response, TRUE);
977                         return ret;
978                 }
979         }
980
981         /*
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).
988          *
989          * Get the data block length, and strip off the length spec from
990          * the input buffer, leaving just the data bytes.
991          */
992         if (response->str[0] != '#') {
993                 g_mutex_unlock(&scpi->scpi_mutex);
994                 g_string_free(response, TRUE);
995                 return SR_ERR_DATA;
996         }
997         buf[0] = response->str[1];
998         buf[1] = '\0';
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);
1003                 return ret;
1004         }
1005
1006         while (response->len < (unsigned long)(2 + llen)) {
1007                 ret = scpi_read_response(scpi, response, timeout);
1008                 if (ret < 0) {
1009                         g_mutex_unlock(&scpi->scpi_mutex);
1010                         g_string_free(response, TRUE);
1011                         return ret;
1012                 }
1013         }
1014
1015         memcpy(buf, &response->str[2], llen);
1016         buf[llen] = '\0';
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);
1021                 return ret;
1022         }
1023         g_string_erase(response, 0, 2 + llen);
1024
1025         /*
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.
1029          */
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);
1034         }
1035
1036         while (response->len < (unsigned long)(datalen)) {
1037                 ret = scpi_read_response(scpi, response, timeout);
1038                 if (ret < 0) {
1039                         g_mutex_unlock(&scpi->scpi_mutex);
1040                         g_string_free(response, TRUE);
1041                         return ret;
1042                 }
1043                 if (ret > 0)
1044                         timeout = g_get_monotonic_time() + scpi->read_timeout_us;
1045         }
1046
1047         g_mutex_unlock(&scpi->scpi_mutex);
1048
1049         /* Convert received data to byte array. */
1050         *scpi_response = g_byte_array_new_take(
1051                 (guint8*)g_string_free(response, FALSE), datalen);
1052
1053         return SR_OK;
1054 }
1055
1056 /**
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.
1059  *
1060  * The hw_info structure must be freed by the caller via sr_scpi_hw_info_free().
1061  *
1062  * @param scpi Previously initialised SCPI device structure.
1063  * @param scpi_response Pointer where to store the hw_info structure.
1064  *
1065  * @return SR_OK upon success, SR_ERR* on failure.
1066  */
1067 SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi,
1068                               struct sr_scpi_hw_info **scpi_response)
1069 {
1070         int num_tokens, ret;
1071         char *response;
1072         gchar **tokens;
1073         struct sr_scpi_hw_info *hw_info;
1074         gchar *idn_substr;
1075
1076         response = NULL;
1077         tokens = NULL;
1078
1079         ret = sr_scpi_get_string(scpi, SCPI_CMD_IDN, &response);
1080         if (ret != SR_OK && !response)
1081                 return ret;
1082
1083         /*
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.
1087          */
1088         tokens = g_strsplit(response, ",", 0);
1089
1090         for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
1091
1092         if (num_tokens < 4) {
1093                 sr_dbg("IDN response not according to spec: %80.s.", response);
1094                 g_strfreev(tokens);
1095                 g_free(response);
1096                 return SR_ERR_DATA;
1097         }
1098         g_free(response);
1099
1100         hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info));
1101
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]));
1105         else
1106                 hw_info->manufacturer = g_strstrip(g_strdup(idn_substr + 4));
1107
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]));
1111
1112         g_strfreev(tokens);
1113
1114         *scpi_response = hw_info;
1115
1116         return SR_OK;
1117 }
1118
1119 /**
1120  * Free a sr_scpi_hw_info struct.
1121  *
1122  * @param hw_info Pointer to the struct to free. If NULL, this
1123  *                function does nothing.
1124  */
1125 SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info)
1126 {
1127         if (!hw_info)
1128                 return;
1129
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);
1134         g_free(hw_info);
1135 }
1136
1137 SR_PRIV const char *sr_vendor_alias(const char *raw_vendor)
1138 {
1139         unsigned int i;
1140
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];
1144         }
1145
1146         return raw_vendor;
1147 }
1148
1149 SR_PRIV const char *sr_scpi_cmd_get(const struct scpi_command *cmdtable, int command)
1150 {
1151         unsigned int i;
1152         const char *cmd;
1153
1154         if (!cmdtable)
1155                 return NULL;
1156
1157         cmd = NULL;
1158         for (i = 0; cmdtable[i].string; i++) {
1159                 if (cmdtable[i].command == command) {
1160                         cmd = cmdtable[i].string;
1161                         break;
1162                 }
1163         }
1164
1165         return cmd;
1166 }
1167
1168 SR_PRIV int sr_scpi_cmd(const struct sr_dev_inst *sdi, const struct scpi_command *cmdtable,
1169                 int command, ...)
1170 {
1171         struct sr_scpi_dev_inst *scpi;
1172         va_list args;
1173         int ret;
1174         const char *cmd;
1175
1176         if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) {
1177                 /* Device does not implement this command, that's OK. */
1178                 return SR_OK;
1179         }
1180
1181         scpi = sdi->conn;
1182         va_start(args, command);
1183         ret = sr_scpi_send_variadic(scpi, cmd, args);
1184         va_end(args);
1185
1186         return ret;
1187 }
1188
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, ...)
1192 {
1193         struct sr_scpi_dev_inst *scpi;
1194         va_list args;
1195         const char *cmd;
1196         GString *response;
1197         char *s;
1198         gboolean b;
1199         double d;
1200         int ret;
1201
1202         scpi = sdi->conn;
1203
1204         if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) {
1205                 /* Device does not implement this command. */
1206                 return SR_ERR_NA;
1207         }
1208
1209         g_mutex_lock(&scpi->scpi_mutex);
1210
1211         va_start(args, command);
1212         ret = scpi_send_variadic(scpi, cmd, args);
1213         va_end(args);
1214         if (ret != SR_OK) {
1215                 g_mutex_unlock(&scpi->scpi_mutex);
1216                 return ret;
1217         }
1218
1219         response = g_string_sized_new(1024);
1220         ret = scpi_get_data(scpi, NULL, &response);
1221         if (ret != SR_OK) {
1222                 g_mutex_unlock(&scpi->scpi_mutex);
1223                 if (response)
1224                         g_string_free(response, TRUE);
1225                 return ret;
1226         }
1227
1228         g_mutex_unlock(&scpi->scpi_mutex);
1229
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);
1233
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);
1237
1238         s = g_string_free(response, FALSE);
1239
1240         ret = SR_OK;
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);
1249         } else {
1250                 sr_err("Unable to convert to desired GVariant type.");
1251                 ret = SR_ERR_NA;
1252         }
1253
1254         g_free(s);
1255
1256         return ret;
1257 }