]> sigrok.org Git - libsigrok.git/blame - src/scpi/scpi.c
serial: introduce more general "have serial comm" feature flag
[libsigrok.git] / src / scpi / scpi.c
CommitLineData
7b9d7320
DJ
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
fd20e59c 5 * Copyright (C) 2015 Bert Vermeulen <bert@biot.com>
7b9d7320
DJ
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
6ec6c43b 21#include <config.h>
7b9d7320
DJ
22#include <glib.h>
23#include <string.h>
c1aae900 24#include <libsigrok/libsigrok.h>
515ab088 25#include "libsigrok-internal.h"
5a1afc09 26#include "scpi.h"
7b9d7320 27
3544f848 28#define LOG_PREFIX "scpi"
7b9d7320
DJ
29
30#define SCPI_READ_RETRIES 100
1a46cc62 31#define SCPI_READ_RETRY_TIMEOUT_US (10 * 1000)
7b9d7320 32
fd20e59c 33static const char *scpi_vendors[][2] = {
fd20e59c 34 { "Agilent Technologies", "Agilent" },
fd20e59c
FS
35 { "CHROMA", "Chroma" },
36 { "Chroma ATE", "Chroma" },
21bc4353
GS
37 { "HEWLETT-PACKARD", "HP" },
38 { "Keysight Technologies", "Keysight" },
39 { "PHILIPS", "Philips" },
40 { "RIGOL TECHNOLOGIES", "Rigol" },
fd20e59c
FS
41};
42
aa1e3b40
DJ
43/**
44 * Parse a string representation of a boolean-like value into a gboolean.
45 * Similar to sr_parse_boolstring but rejects strings which do not represent
46 * a boolean-like value.
47 *
48 * @param str String to convert.
49 * @param ret Pointer to a gboolean where the result of the conversion will be
50 * stored.
51 *
52 * @return SR_OK on success, SR_ERR on failure.
53 */
d5976d8b 54static int parse_strict_bool(const char *str, gboolean *ret)
aa1e3b40
DJ
55{
56 if (!str)
57 return SR_ERR_ARG;
58
59 if (!g_strcmp0(str, "1") ||
60 !g_ascii_strncasecmp(str, "y", 1) ||
61 !g_ascii_strncasecmp(str, "t", 1) ||
62 !g_ascii_strncasecmp(str, "yes", 3) ||
63 !g_ascii_strncasecmp(str, "true", 4) ||
64 !g_ascii_strncasecmp(str, "on", 2)) {
aa1e3b40
DJ
65 *ret = TRUE;
66 return SR_OK;
aa1e3b40
DJ
67 } else if (!g_strcmp0(str, "0") ||
68 !g_ascii_strncasecmp(str, "n", 1) ||
69 !g_ascii_strncasecmp(str, "f", 1) ||
70 !g_ascii_strncasecmp(str, "no", 2) ||
71 !g_ascii_strncasecmp(str, "false", 5) ||
72 !g_ascii_strncasecmp(str, "off", 3)) {
aa1e3b40
DJ
73 *ret = FALSE;
74 return SR_OK;
75 }
76
77 return SR_ERR;
78}
79
f754c146 80SR_PRIV extern const struct sr_scpi_dev_inst scpi_serial_dev;
104ed125
AJ
81SR_PRIV extern const struct sr_scpi_dev_inst scpi_tcp_raw_dev;
82SR_PRIV extern const struct sr_scpi_dev_inst scpi_tcp_rigol_dev;
20ed3cee 83SR_PRIV extern const struct sr_scpi_dev_inst scpi_usbtmc_libusb_dev;
f754c146 84SR_PRIV extern const struct sr_scpi_dev_inst scpi_vxi_dev;
1fb2312f 85SR_PRIV extern const struct sr_scpi_dev_inst scpi_visa_dev;
7343ad1e 86SR_PRIV extern const struct sr_scpi_dev_inst scpi_libgpib_dev;
f754c146
AJ
87
88static const struct sr_scpi_dev_inst *scpi_devs[] = {
104ed125
AJ
89 &scpi_tcp_raw_dev,
90 &scpi_tcp_rigol_dev,
20ed3cee
AJ
91#ifdef HAVE_LIBUSB_1_0
92 &scpi_usbtmc_libusb_dev,
93#endif
613c1108 94#if HAVE_RPC
f754c146
AJ
95 &scpi_vxi_dev,
96#endif
1fb2312f
ML
97#ifdef HAVE_LIBREVISA
98 &scpi_visa_dev,
99#endif
bb2a4ed4 100#ifdef HAVE_LIBGPIB
7343ad1e 101 &scpi_libgpib_dev,
bb2a4ed4 102#endif
1df81f4b 103#ifdef HAVE_SERIAL_COMM
d9251a2c 104 &scpi_serial_dev, /* Must be last as it matches any resource. */
f754c146
AJ
105#endif
106};
107
85b69c2b 108static struct sr_dev_inst *sr_scpi_scan_resource(struct drv_context *drvc,
b541f837
AJ
109 const char *resource, const char *serialcomm,
110 struct sr_dev_inst *(*probe_device)(struct sr_scpi_dev_inst *scpi))
111{
112 struct sr_scpi_dev_inst *scpi;
113 struct sr_dev_inst *sdi;
114
115 if (!(scpi = scpi_dev_inst_new(drvc, resource, serialcomm)))
116 return NULL;
117
118 if (sr_scpi_open(scpi) != SR_OK) {
119 sr_info("Couldn't open SCPI device.");
120 sr_scpi_free(scpi);
121 return NULL;
122 };
123
a00106b7 124 sdi = probe_device(scpi);
b541f837
AJ
125
126 sr_scpi_close(scpi);
a00106b7
ML
127
128 if (sdi)
129 sdi->status = SR_ST_INACTIVE;
130 else
131 sr_scpi_free(scpi);
132
133 return sdi;
b541f837
AJ
134}
135
fd20e59c
FS
136/**
137 * Send a SCPI command with a variadic argument list without mutex.
138 *
139 * @param scpi Previously initialized SCPI device structure.
140 * @param format Format string.
141 * @param args Argument list.
142 *
143 * @return SR_OK on success, SR_ERR on failure.
144 */
145static int scpi_send_variadic(struct sr_scpi_dev_inst *scpi,
146 const char *format, va_list args)
147{
148 va_list args_copy;
149 char *buf;
150 int len, ret;
151
152 /* Get length of buffer required. */
153 va_copy(args_copy, args);
1c8901f7 154 len = sr_vsnprintf_ascii(NULL, 0, format, args_copy);
fd20e59c
FS
155 va_end(args_copy);
156
157 /* Allocate buffer and write out command. */
158 buf = g_malloc0(len + 2);
1c8901f7 159 sr_vsprintf_ascii(buf, format, args);
fd20e59c
FS
160 if (buf[len - 1] != '\n')
161 buf[len] = '\n';
162
163 /* Send command. */
164 ret = scpi->send(scpi->priv, buf);
165
166 /* Free command buffer. */
167 g_free(buf);
168
169 return ret;
170}
171
172/**
173 * Send a SCPI command without mutex.
174 *
175 * @param scpi Previously initialized SCPI device structure.
176 * @param format Format string, to be followed by any necessary arguments.
177 *
178 * @return SR_OK on success, SR_ERR on failure.
179 */
17a82e83 180static int scpi_send(struct sr_scpi_dev_inst *scpi, const char *format, ...)
fd20e59c 181{
17a82e83
FS
182 va_list args;
183 int ret;
184
185 va_start(args, format);
186 ret = scpi_send_variadic(scpi, format, args);
187 va_end(args);
188
189 return ret;
fd20e59c
FS
190}
191
192/**
193 * Send data to SCPI device without mutex.
194 *
195 * TODO: This is only implemented in TcpRaw, but never used.
196 * TODO: Use Mutex at all?
197 *
198 * @param scpi Previously initialised SCPI device structure.
199 * @param buf Buffer with data to send.
200 * @param len Number of bytes to send.
201 *
202 * @return Number of bytes read, or SR_ERR upon failure.
203 */
204static int scpi_write_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen)
205{
206 return scpi->write_data(scpi->priv, buf, maxlen);
207}
208
209/**
210 * Read part of a response from SCPI device without mutex.
211 *
212 * @param scpi Previously initialised SCPI device structure.
213 * @param buf Buffer to store result.
214 * @param maxlen Maximum number of bytes to read.
215 *
216 * @return Number of bytes read, or SR_ERR upon failure.
217 */
218static int scpi_read_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen)
219{
220 return scpi->read_data(scpi->priv, buf, maxlen);
221}
222
223/**
224 * Do a non-blocking read of up to the allocated length, and
225 * check if a timeout has occured, without mutex.
226 *
227 * @param scpi Previously initialised SCPI device structure.
228 * @param response Buffer to which the response is appended.
229 * @param abs_timeout_us Absolute timeout in microseconds
230 *
231 * @return read length on success, SR_ERR* on failure.
232 */
233static int scpi_read_response(struct sr_scpi_dev_inst *scpi,
234 GString *response, gint64 abs_timeout_us)
235{
236 int len, space;
237
238 space = response->allocated_len - response->len;
239 len = scpi->read_data(scpi->priv, &response->str[response->len], space);
240
241 if (len < 0) {
242 sr_err("Incompletely read SCPI response.");
243 return SR_ERR;
244 }
245
246 if (len > 0) {
247 g_string_set_size(response, response->len + len);
248 return len;
249 }
250
251 if (g_get_monotonic_time() > abs_timeout_us) {
252 sr_err("Timed out waiting for SCPI response.");
253 return SR_ERR_TIMEOUT;
254 }
255
256 return 0;
257}
258
259/**
260 * Send a SCPI command, receive the reply and store the reply in
261 * scpi_response, without mutex.
262 *
263 * @param scpi Previously initialised SCPI device structure.
264 * @param command The SCPI command to send to the device.
265 * @param scpi_response Pointer where to store the SCPI response.
266 *
267 * @return SR_OK on success, SR_ERR on failure.
268 */
269static int scpi_get_data(struct sr_scpi_dev_inst *scpi,
270 const char *command, GString **scpi_response)
271{
272 int ret;
273 GString *response;
274 int space;
275 gint64 timeout;
fd20e59c
FS
276
277 /* Optionally send caller provided command. */
278 if (command) {
17a82e83 279 if (scpi_send(scpi, command) != SR_OK)
fd20e59c
FS
280 return SR_ERR;
281 }
282
283 /* Initiate SCPI read operation. */
284 if (sr_scpi_read_begin(scpi) != SR_OK)
285 return SR_ERR;
286
287 /* Keep reading until completion or until timeout. */
288 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
289
290 response = *scpi_response;
291
292 while (!sr_scpi_read_complete(scpi)) {
293 /* Resize the buffer when free space drops below a threshold. */
294 space = response->allocated_len - response->len;
295 if (space < 128) {
296 int oldlen = response->len;
297 g_string_set_size(response, oldlen + 1024);
298 g_string_set_size(response, oldlen);
299 }
300
301 /* Read another chunk of the response. */
302 ret = scpi_read_response(scpi, response, timeout);
303
304 if (ret < 0)
305 return ret;
306 if (ret > 0)
307 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
308 }
309
310 return SR_OK;
311}
312
b541f837
AJ
313SR_PRIV GSList *sr_scpi_scan(struct drv_context *drvc, GSList *options,
314 struct sr_dev_inst *(*probe_device)(struct sr_scpi_dev_inst *scpi))
315{
85b69c2b
BV
316 GSList *resources, *l, *devices;
317 struct sr_dev_inst *sdi;
b541f837
AJ
318 const char *resource = NULL;
319 const char *serialcomm = NULL;
320 gchar **res;
321 unsigned i;
322
323 for (l = options; l; l = l->next) {
324 struct sr_config *src = l->data;
325 switch (src->key) {
326 case SR_CONF_CONN:
327 resource = g_variant_get_string(src->data, NULL);
328 break;
329 case SR_CONF_SERIALCOMM:
330 serialcomm = g_variant_get_string(src->data, NULL);
331 break;
332 }
333 }
334
85b69c2b 335 devices = NULL;
b541f837
AJ
336 for (i = 0; i < ARRAY_SIZE(scpi_devs); i++) {
337 if ((resource && strcmp(resource, scpi_devs[i]->prefix))
338 || !scpi_devs[i]->scan)
339 continue;
340 resources = scpi_devs[i]->scan(drvc);
341 for (l = resources; l; l = l->next) {
342 res = g_strsplit(l->data, ":", 2);
85b69c2b 343 if (res[0] && (sdi = sr_scpi_scan_resource(drvc, res[0],
b2c02b07 344 serialcomm ? serialcomm : res[1], probe_device))) {
85b69c2b 345 devices = g_slist_append(devices, sdi);
b2c02b07
SA
346 sdi->connection_id = g_strdup(l->data);
347 }
b541f837
AJ
348 g_strfreev(res);
349 }
350 g_slist_free_full(resources, g_free);
351 }
352
85b69c2b
BV
353 if (!devices && resource) {
354 sdi = sr_scpi_scan_resource(drvc, resource, serialcomm, probe_device);
cfd8ec53
BV
355 if (sdi)
356 devices = g_slist_append(NULL, sdi);
85b69c2b 357 }
b541f837
AJ
358
359 /* Tack a copy of the newly found devices onto the driver list. */
360 if (devices)
85b69c2b 361 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
b541f837
AJ
362
363 return devices;
364}
365
17bdda58
AJ
366SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(struct drv_context *drvc,
367 const char *resource, const char *serialcomm)
c3515cea
AJ
368{
369 struct sr_scpi_dev_inst *scpi = NULL;
f754c146
AJ
370 const struct sr_scpi_dev_inst *scpi_dev;
371 gchar **params;
372 unsigned i;
c3515cea 373
f754c146
AJ
374 for (i = 0; i < ARRAY_SIZE(scpi_devs); i++) {
375 scpi_dev = scpi_devs[i];
376 if (!strncmp(resource, scpi_dev->prefix, strlen(scpi_dev->prefix))) {
377 sr_dbg("Opening %s device %s.", scpi_dev->name, resource);
378 scpi = g_malloc(sizeof(*scpi));
379 *scpi = *scpi_dev;
380 scpi->priv = g_malloc0(scpi->priv_size);
37ef582d 381 scpi->read_timeout_us = 1000 * 1000;
f754c146 382 params = g_strsplit(resource, "/", 0);
17bdda58 383 if (scpi->dev_inst_new(scpi->priv, drvc, resource,
f754c146
AJ
384 params, serialcomm) != SR_OK) {
385 sr_scpi_free(scpi);
386 scpi = NULL;
387 }
388 g_strfreev(params);
389 break;
390 }
c3515cea 391 }
f754c146 392
c3515cea
AJ
393 return scpi;
394}
395
23f43dff
ML
396/**
397 * Open SCPI device.
398 *
399 * @param scpi Previously initialized SCPI device structure.
400 *
401 * @return SR_OK on success, SR_ERR on failure.
402 */
403SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi)
404{
fd20e59c
FS
405 g_mutex_init(&scpi->scpi_mutex);
406
04229f7b 407 return scpi->open(scpi);
23f43dff
ML
408}
409
8107a9a6
FS
410/**
411 * Get the connection ID of the SCPI device.
412 *
413 * @param scpi Previously initialized SCPI device structure.
414 * @param connection_id Pointer where to store the connection ID. The caller
415 * is responsible for g_free()ing the string when it is no longer needed.
416 *
417 * @return SR_OK on success, SR_ERR on failure.
418 */
419SR_PRIV int sr_scpi_connection_id(struct sr_scpi_dev_inst *scpi,
420 char **connection_id)
421{
422 return scpi->connection_id(scpi, connection_id);
423}
424
23f43dff
ML
425/**
426 * Add an event source for an SCPI device.
427 *
7efe889e 428 * @param session The session to add the event source to.
23f43dff
ML
429 * @param scpi Previously initialized SCPI device structure.
430 * @param events Events to check for.
431 * @param timeout Max time to wait before the callback is called, ignored if 0.
432 * @param cb Callback function to add. Must not be NULL.
433 * @param cb_data Data for the callback function. Can be NULL.
434 *
435 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
436 * SR_ERR_MALLOC upon memory allocation errors.
437 */
102f1239
BV
438SR_PRIV int sr_scpi_source_add(struct sr_session *session,
439 struct sr_scpi_dev_inst *scpi, int events, int timeout,
440 sr_receive_data_callback cb, void *cb_data)
23f43dff 441{
102f1239 442 return scpi->source_add(session, scpi->priv, events, timeout, cb, cb_data);
23f43dff
ML
443}
444
445/**
446 * Remove event source for an SCPI device.
447 *
7efe889e 448 * @param session The session to remove the event source from.
23f43dff
ML
449 * @param scpi Previously initialized SCPI device structure.
450 *
451 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
452 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
453 * internal errors.
454 */
102f1239
BV
455SR_PRIV int sr_scpi_source_remove(struct sr_session *session,
456 struct sr_scpi_dev_inst *scpi)
23f43dff 457{
102f1239 458 return scpi->source_remove(session, scpi->priv);
23f43dff
ML
459}
460
7b9d7320
DJ
461/**
462 * Send a SCPI command.
463 *
23f43dff 464 * @param scpi Previously initialized SCPI device structure.
504f40a5 465 * @param format Format string, to be followed by any necessary arguments.
7b9d7320
DJ
466 *
467 * @return SR_OK on success, SR_ERR on failure.
468 */
23f43dff 469SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi,
504f40a5 470 const char *format, ...)
7b9d7320 471{
87c41083
ML
472 va_list args;
473 int ret;
474
475 va_start(args, format);
fd20e59c
FS
476 g_mutex_lock(&scpi->scpi_mutex);
477 ret = scpi_send_variadic(scpi, format, args);
478 g_mutex_unlock(&scpi->scpi_mutex);
87c41083
ML
479 va_end(args);
480
481 return ret;
482}
483
484/**
485 * Send a SCPI command with a variadic argument list.
486 *
487 * @param scpi Previously initialized SCPI device structure.
488 * @param format Format string.
489 * @param args Argument list.
490 *
491 * @return SR_OK on success, SR_ERR on failure.
492 */
493SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi,
494 const char *format, va_list args)
495{
fd20e59c 496 int ret;
504f40a5 497
fd20e59c
FS
498 g_mutex_lock(&scpi->scpi_mutex);
499 ret = scpi_send_variadic(scpi, format, args);
500 g_mutex_unlock(&scpi->scpi_mutex);
504f40a5
ML
501
502 return ret;
23f43dff 503}
7b9d7320 504
23f43dff 505/**
05c644ea 506 * Begin receiving an SCPI reply.
23f43dff
ML
507 *
508 * @param scpi Previously initialised SCPI device structure.
23f43dff 509 *
05c644ea 510 * @return SR_OK on success, SR_ERR on failure.
23f43dff 511 */
05c644ea 512SR_PRIV int sr_scpi_read_begin(struct sr_scpi_dev_inst *scpi)
23f43dff 513{
05c644ea 514 return scpi->read_begin(scpi->priv);
23f43dff 515}
7b9d7320 516
a1ff9c18
ML
517/**
518 * Read part of a response from SCPI device.
519 *
520 * @param scpi Previously initialised SCPI device structure.
521 * @param buf Buffer to store result.
522 * @param maxlen Maximum number of bytes to read.
523 *
524 * @return Number of bytes read, or SR_ERR upon failure.
525 */
05c644ea 526SR_PRIV int sr_scpi_read_data(struct sr_scpi_dev_inst *scpi,
a1ff9c18
ML
527 char *buf, int maxlen)
528{
fd20e59c
FS
529 int ret;
530
531 g_mutex_lock(&scpi->scpi_mutex);
532 ret = scpi_read_data(scpi, buf, maxlen);
533 g_mutex_unlock(&scpi->scpi_mutex);
534
535 return ret;
05c644ea
ML
536}
537
b6be55ce
SS
538/**
539 * Send data to SCPI device.
540 *
fd20e59c
FS
541 * TODO: This is only implemented in TcpRaw, but never used.
542 * TODO: Use Mutex at all?
543 *
b6be55ce
SS
544 * @param scpi Previously initialised SCPI device structure.
545 * @param buf Buffer with data to send.
546 * @param len Number of bytes to send.
547 *
548 * @return Number of bytes read, or SR_ERR upon failure.
549 */
550SR_PRIV int sr_scpi_write_data(struct sr_scpi_dev_inst *scpi,
551 char *buf, int maxlen)
552{
fd20e59c
FS
553 int ret;
554
555 g_mutex_lock(&scpi->scpi_mutex);
556 ret = scpi_write_data(scpi, buf, maxlen);
557 g_mutex_unlock(&scpi->scpi_mutex);
558
559 return ret;
b6be55ce
SS
560}
561
05c644ea
ML
562/**
563 * Check whether a complete SCPI response has been received.
564 *
565 * @param scpi Previously initialised SCPI device structure.
566 *
567 * @return 1 if complete, 0 otherwise.
568 */
569SR_PRIV int sr_scpi_read_complete(struct sr_scpi_dev_inst *scpi)
570{
571 return scpi->read_complete(scpi->priv);
a1ff9c18
ML
572}
573
23f43dff
ML
574/**
575 * Close SCPI device.
576 *
577 * @param scpi Previously initialized SCPI device structure.
578 *
579 * @return SR_OK on success, SR_ERR on failure.
580 */
581SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi)
582{
fd20e59c
FS
583 int ret;
584
585 g_mutex_lock(&scpi->scpi_mutex);
586 ret = scpi->close(scpi);
587 g_mutex_unlock(&scpi->scpi_mutex);
588 g_mutex_clear(&scpi->scpi_mutex);
589
590 return ret;
23f43dff 591}
7b9d7320 592
23f43dff
ML
593/**
594 * Free SCPI device.
595 *
81568546
UH
596 * @param scpi Previously initialized SCPI device structure. If NULL,
597 * this function does nothing.
23f43dff
ML
598 */
599SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi)
600{
81568546
UH
601 if (!scpi)
602 return;
603
23f43dff 604 scpi->free(scpi->priv);
f754c146 605 g_free(scpi->priv);
3d118722 606 g_free(scpi->actual_channel_name);
23f43dff 607 g_free(scpi);
7b9d7320
DJ
608}
609
610/**
611 * Send a SCPI command, receive the reply and store the reply in scpi_response.
612 *
23f43dff 613 * @param scpi Previously initialised SCPI device structure.
7b9d7320 614 * @param command The SCPI command to send to the device (can be NULL).
d5976d8b 615 * @param scpi_response Pointer where to store the SCPI response.
7b9d7320 616 *
c0d25779 617 * @return SR_OK on success, SR_ERR* on failure.
7b9d7320 618 */
23f43dff 619SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi,
d5976d8b 620 const char *command, char **scpi_response)
7b9d7320 621{
ff01b0ea
SB
622 GString *response;
623 response = g_string_sized_new(1024);
624
625 if (sr_scpi_get_data(scpi, command, &response) != SR_OK) {
626 if (response)
627 g_string_free(response, TRUE);
628 return SR_ERR;
629 }
630
631 /* Get rid of trailing linefeed if present */
632 if (response->len >= 1 && response->str[response->len - 1] == '\n')
633 g_string_truncate(response, response->len - 1);
634
635 /* Get rid of trailing carriage return if present */
636 if (response->len >= 1 && response->str[response->len - 1] == '\r')
637 g_string_truncate(response, response->len - 1);
638
639 sr_spew("Got response: '%.70s', length %" G_GSIZE_FORMAT ".",
640 response->str, response->len);
641
642 *scpi_response = g_string_free(response, FALSE);
643
644 return SR_OK;
645}
646
d64be25b
SB
647/**
648 * Do a non-blocking read of up to the allocated length, and
649 * check if a timeout has occured.
650 *
651 * @param scpi Previously initialised SCPI device structure.
652 * @param response Buffer to which the response is appended.
653 * @param abs_timeout_us Absolute timeout in microseconds
654 *
655 * @return read length on success, SR_ERR* on failure.
656 */
657SR_PRIV int sr_scpi_read_response(struct sr_scpi_dev_inst *scpi,
658 GString *response, gint64 abs_timeout_us)
659{
fd20e59c 660 int ret;
d64be25b 661
fd20e59c
FS
662 g_mutex_lock(&scpi->scpi_mutex);
663 ret = scpi_read_response(scpi, response, abs_timeout_us);
664 g_mutex_unlock(&scpi->scpi_mutex);
d64be25b 665
fd20e59c 666 return ret;
d64be25b
SB
667}
668
ff01b0ea
SB
669SR_PRIV int sr_scpi_get_data(struct sr_scpi_dev_inst *scpi,
670 const char *command, GString **scpi_response)
671{
d64be25b 672 int ret;
37ef582d 673
fd20e59c
FS
674 g_mutex_lock(&scpi->scpi_mutex);
675 ret = scpi_get_data(scpi, command, scpi_response);
676 g_mutex_unlock(&scpi->scpi_mutex);
05c644ea 677
fd20e59c 678 return ret;
d730f70e
DJ
679}
680
681/**
682 * Send a SCPI command, read the reply, parse it as a bool value and store the
683 * result in scpi_response.
684 *
23f43dff 685 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
686 * @param command The SCPI command to send to the device (can be NULL).
687 * @param scpi_response Pointer where to store the parsed result.
688 *
c0d25779 689 * @return SR_OK on success, SR_ERR* on failure.
d730f70e 690 */
23f43dff 691SR_PRIV int sr_scpi_get_bool(struct sr_scpi_dev_inst *scpi,
d730f70e
DJ
692 const char *command, gboolean *scpi_response)
693{
694 int ret;
695 char *response;
696
697 response = NULL;
698
c0d25779
BV
699 ret = sr_scpi_get_string(scpi, command, &response);
700 if (ret != SR_OK && !response)
701 return ret;
d730f70e 702
d5976d8b 703 if (parse_strict_bool(response, scpi_response) == SR_OK)
d730f70e
DJ
704 ret = SR_OK;
705 else
c0d25779 706 ret = SR_ERR_DATA;
d730f70e
DJ
707
708 g_free(response);
709
710 return ret;
711}
712
713/**
714 * Send a SCPI command, read the reply, parse it as an integer and store the
715 * result in scpi_response.
716 *
23f43dff 717 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
718 * @param command The SCPI command to send to the device (can be NULL).
719 * @param scpi_response Pointer where to store the parsed result.
720 *
c0d25779 721 * @return SR_OK on success, SR_ERR* on failure.
d730f70e 722 */
23f43dff 723SR_PRIV int sr_scpi_get_int(struct sr_scpi_dev_inst *scpi,
d5976d8b 724 const char *command, int *scpi_response)
d730f70e
DJ
725{
726 int ret;
727 char *response;
728
729 response = NULL;
730
c0d25779
BV
731 ret = sr_scpi_get_string(scpi, command, &response);
732 if (ret != SR_OK && !response)
733 return ret;
d730f70e
DJ
734
735 if (sr_atoi(response, scpi_response) == SR_OK)
736 ret = SR_OK;
737 else
c0d25779 738 ret = SR_ERR_DATA;
d730f70e
DJ
739
740 g_free(response);
741
742 return ret;
743}
744
745/**
746 * Send a SCPI command, read the reply, parse it as a float and store the
747 * result in scpi_response.
748 *
23f43dff 749 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
750 * @param command The SCPI command to send to the device (can be NULL).
751 * @param scpi_response Pointer where to store the parsed result.
752 *
c0d25779 753 * @return SR_OK on success, SR_ERR* on failure.
d730f70e 754 */
23f43dff 755SR_PRIV int sr_scpi_get_float(struct sr_scpi_dev_inst *scpi,
d730f70e
DJ
756 const char *command, float *scpi_response)
757{
758 int ret;
759 char *response;
760
761 response = NULL;
762
c0d25779
BV
763 ret = sr_scpi_get_string(scpi, command, &response);
764 if (ret != SR_OK && !response)
765 return ret;
d730f70e 766
13dbd151 767 if (sr_atof_ascii(response, scpi_response) == SR_OK)
d730f70e
DJ
768 ret = SR_OK;
769 else
c0d25779 770 ret = SR_ERR_DATA;
d730f70e
DJ
771
772 g_free(response);
773
774 return ret;
775}
776
777/**
778 * Send a SCPI command, read the reply, parse it as a double and store the
779 * result in scpi_response.
780 *
23f43dff 781 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
782 * @param command The SCPI command to send to the device (can be NULL).
783 * @param scpi_response Pointer where to store the parsed result.
784 *
c0d25779 785 * @return SR_OK on success, SR_ERR* on failure.
d730f70e 786 */
23f43dff 787SR_PRIV int sr_scpi_get_double(struct sr_scpi_dev_inst *scpi,
d5976d8b 788 const char *command, double *scpi_response)
d730f70e
DJ
789{
790 int ret;
791 char *response;
792
793 response = NULL;
794
c0d25779
BV
795 ret = sr_scpi_get_string(scpi, command, &response);
796 if (ret != SR_OK && !response)
797 return ret;
d730f70e 798
4f0463a0 799 if (sr_atod_ascii(response, scpi_response) == SR_OK)
d730f70e
DJ
800 ret = SR_OK;
801 else
c0d25779 802 ret = SR_ERR_DATA;
d730f70e
DJ
803
804 g_free(response);
805
7b9d7320
DJ
806 return ret;
807}
808
f5922ade
DJ
809/**
810 * Send a SCPI *OPC? command, read the reply and return the result of the
811 * command.
812 *
23f43dff 813 * @param scpi Previously initialised SCPI device structure.
f5922ade 814 *
c0d25779 815 * @return SR_OK on success, SR_ERR* on failure.
f5922ade 816 */
23f43dff 817SR_PRIV int sr_scpi_get_opc(struct sr_scpi_dev_inst *scpi)
f5922ade
DJ
818{
819 unsigned int i;
820 gboolean opc;
821
0a1f7b09 822 for (i = 0; i < SCPI_READ_RETRIES; i++) {
da6f107e 823 opc = FALSE;
23f43dff 824 sr_scpi_get_bool(scpi, SCPI_CMD_OPC, &opc);
f5922ade
DJ
825 if (opc)
826 return SR_OK;
1a46cc62 827 g_usleep(SCPI_READ_RETRY_TIMEOUT_US);
f5922ade
DJ
828 }
829
830 return SR_ERR;
831}
832
8acbb89a
DJ
833/**
834 * Send a SCPI command, read the reply, parse it as comma separated list of
835 * floats and store the as an result in scpi_response.
836 *
23f43dff 837 * @param scpi Previously initialised SCPI device structure.
8acbb89a
DJ
838 * @param command The SCPI command to send to the device (can be NULL).
839 * @param scpi_response Pointer where to store the parsed result.
840 *
c0d25779 841 * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing
d5976d8b
UH
842 * error or upon no response. The allocated response must be freed by
843 * the caller in the case of an SR_OK as well as in the case of
844 * parsing error.
8acbb89a 845 */
23f43dff 846SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi,
d5976d8b 847 const char *command, GArray **scpi_response)
8acbb89a
DJ
848{
849 int ret;
850 float tmp;
851 char *response;
d5976d8b 852 gchar **ptr, **tokens;
8acbb89a
DJ
853 GArray *response_array;
854
8acbb89a
DJ
855 response = NULL;
856 tokens = NULL;
857
c0d25779
BV
858 ret = sr_scpi_get_string(scpi, command, &response);
859 if (ret != SR_OK && !response)
860 return ret;
8acbb89a
DJ
861
862 tokens = g_strsplit(response, ",", 0);
863 ptr = tokens;
864
865 response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256);
866
d5976d8b 867 while (*ptr) {
13dbd151 868 if (sr_atof_ascii(*ptr, &tmp) == SR_OK)
8acbb89a
DJ
869 response_array = g_array_append_val(response_array,
870 tmp);
871 else
c0d25779 872 ret = SR_ERR_DATA;
8acbb89a
DJ
873
874 ptr++;
875 }
876 g_strfreev(tokens);
877 g_free(response);
878
c0d25779 879 if (ret != SR_OK && response_array->len == 0) {
8acbb89a
DJ
880 g_array_free(response_array, TRUE);
881 *scpi_response = NULL;
c0d25779 882 return SR_ERR_DATA;
8acbb89a
DJ
883 }
884
885 *scpi_response = response_array;
886
1a323dd8
DJ
887 return ret;
888}
889
890/**
891 * Send a SCPI command, read the reply, parse it as comma separated list of
892 * unsigned 8 bit integers and store the as an result in scpi_response.
893 *
23f43dff 894 * @param scpi Previously initialised SCPI device structure.
1a323dd8
DJ
895 * @param command The SCPI command to send to the device (can be NULL).
896 * @param scpi_response Pointer where to store the parsed result.
897 *
c0d25779 898 * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing
d5976d8b
UH
899 * error or upon no response. The allocated response must be freed by
900 * the caller in the case of an SR_OK as well as in the case of
901 * parsing error.
1a323dd8 902 */
23f43dff 903SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi,
d5976d8b 904 const char *command, GArray **scpi_response)
1a323dd8 905{
d5976d8b 906 int tmp, ret;
1a323dd8 907 char *response;
d5976d8b 908 gchar **ptr, **tokens;
1a323dd8
DJ
909 GArray *response_array;
910
1a323dd8
DJ
911 response = NULL;
912 tokens = NULL;
913
c0d25779
BV
914 ret = sr_scpi_get_string(scpi, command, &response);
915 if (ret != SR_OK && !response)
916 return ret;
1a323dd8
DJ
917
918 tokens = g_strsplit(response, ",", 0);
919 ptr = tokens;
920
921 response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256);
922
d5976d8b 923 while (*ptr) {
1a323dd8
DJ
924 if (sr_atoi(*ptr, &tmp) == SR_OK)
925 response_array = g_array_append_val(response_array,
926 tmp);
927 else
c0d25779 928 ret = SR_ERR_DATA;
1a323dd8
DJ
929
930 ptr++;
931 }
932 g_strfreev(tokens);
933 g_free(response);
934
935 if (response_array->len == 0) {
936 g_array_free(response_array, TRUE);
937 *scpi_response = NULL;
c0d25779 938 return SR_ERR_DATA;
1a323dd8
DJ
939 }
940
941 *scpi_response = response_array;
942
8acbb89a
DJ
943 return ret;
944}
d3de86f3 945
ff01b0ea
SB
946/**
947 * Send a SCPI command, read the reply, parse it as binary data with a
948 * "definite length block" header and store the as an result in scpi_response.
949 *
950 * @param scpi Previously initialised SCPI device structure.
951 * @param command The SCPI command to send to the device (can be NULL).
952 * @param scpi_response Pointer where to store the parsed result.
953 *
954 * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing
955 * error or upon no response. The allocated response must be freed by
956 * the caller in the case of an SR_OK as well as in the case of
957 * parsing error.
958 */
959SR_PRIV int sr_scpi_get_block(struct sr_scpi_dev_inst *scpi,
960 const char *command, GByteArray **scpi_response)
961{
962 int ret;
963 GString* response;
26e8c6a2 964 char buf[10];
ff01b0ea
SB
965 long llen;
966 long datalen;
d64be25b 967 gint64 timeout;
fd20e59c
FS
968
969 g_mutex_lock(&scpi->scpi_mutex);
d64be25b
SB
970
971 if (command)
17a82e83 972 if (scpi_send(scpi, command) != SR_OK) {
fd20e59c 973 g_mutex_unlock(&scpi->scpi_mutex);
d64be25b 974 return SR_ERR;
fd20e59c 975 }
d64be25b 976
fd20e59c
FS
977 if (sr_scpi_read_begin(scpi) != SR_OK) {
978 g_mutex_unlock(&scpi->scpi_mutex);
d64be25b 979 return SR_ERR;
fd20e59c 980 }
ff01b0ea 981
26e8c6a2
GS
982 /*
983 * Assume an initial maximum length, optionally gets adjusted below.
984 * Prepare a NULL return value for when error paths will be taken.
985 */
ff01b0ea 986 response = g_string_sized_new(1024);
d64be25b
SB
987
988 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
989
ff01b0ea
SB
990 *scpi_response = NULL;
991
26e8c6a2 992 /* Get (the first chunk of) the response. */
d64be25b 993 while (response->len < 2) {
fd20e59c 994 ret = scpi_read_response(scpi, response, timeout);
d64be25b 995 if (ret < 0) {
fd20e59c 996 g_mutex_unlock(&scpi->scpi_mutex);
d64be25b
SB
997 g_string_free(response, TRUE);
998 return ret;
999 }
ff01b0ea
SB
1000 }
1001
26e8c6a2
GS
1002 /*
1003 * SCPI protocol data blocks are preceeded with a length spec.
1004 * The length spec consists of a '#' marker, one digit which
1005 * specifies the character count of the length spec, and the
1006 * respective number of characters which specify the data block's
1007 * length. Raw data bytes follow (thus one must no longer assume
1008 * that the received input stream would be an ASCIIZ string).
1009 *
1010 * Get the data block length, and strip off the length spec from
1011 * the input buffer, leaving just the data bytes.
1012 */
ff01b0ea 1013 if (response->str[0] != '#') {
fd20e59c 1014 g_mutex_unlock(&scpi->scpi_mutex);
ff01b0ea
SB
1015 g_string_free(response, TRUE);
1016 return SR_ERR_DATA;
1017 }
ff01b0ea 1018 buf[0] = response->str[1];
26e8c6a2 1019 buf[1] = '\0';
ff01b0ea
SB
1020 ret = sr_atol(buf, &llen);
1021 if ((ret != SR_OK) || (llen == 0)) {
fd20e59c 1022 g_mutex_unlock(&scpi->scpi_mutex);
ff01b0ea
SB
1023 g_string_free(response, TRUE);
1024 return ret;
1025 }
d64be25b
SB
1026
1027 while (response->len < (unsigned long)(2 + llen)) {
fd20e59c 1028 ret = scpi_read_response(scpi, response, timeout);
d64be25b 1029 if (ret < 0) {
fd20e59c 1030 g_mutex_unlock(&scpi->scpi_mutex);
d64be25b
SB
1031 g_string_free(response, TRUE);
1032 return ret;
1033 }
1034 }
1035
ff01b0ea 1036 memcpy(buf, &response->str[2], llen);
26e8c6a2 1037 buf[llen] = '\0';
ff01b0ea
SB
1038 ret = sr_atol(buf, &datalen);
1039 if ((ret != SR_OK) || (datalen == 0)) {
fd20e59c 1040 g_mutex_unlock(&scpi->scpi_mutex);
ff01b0ea
SB
1041 g_string_free(response, TRUE);
1042 return ret;
1043 }
ff01b0ea
SB
1044 g_string_erase(response, 0, 2 + llen);
1045
26e8c6a2
GS
1046 /*
1047 * If the initially assumed length does not cover the data block
1048 * length, then re-allocate the buffer size to the now known
1049 * length, and keep reading more chunks of response data.
1050 */
ff01b0ea
SB
1051 if (response->len < (unsigned long)(datalen)) {
1052 int oldlen = response->len;
1053 g_string_set_size(response, datalen);
1054 g_string_set_size(response, oldlen);
1055 }
d64be25b 1056
ff01b0ea 1057 while (response->len < (unsigned long)(datalen)) {
fd20e59c 1058 ret = scpi_read_response(scpi, response, timeout);
d64be25b 1059 if (ret < 0) {
fd20e59c 1060 g_mutex_unlock(&scpi->scpi_mutex);
ff01b0ea
SB
1061 g_string_free(response, TRUE);
1062 return ret;
1063 }
d64be25b
SB
1064 if (ret > 0)
1065 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
ff01b0ea
SB
1066 }
1067
fd20e59c
FS
1068 g_mutex_unlock(&scpi->scpi_mutex);
1069
26e8c6a2 1070 /* Convert received data to byte array. */
ff01b0ea
SB
1071 *scpi_response = g_byte_array_new_take(
1072 (guint8*)g_string_free(response, FALSE), datalen);
1073
d64be25b 1074 return SR_OK;
ff01b0ea
SB
1075}
1076
7b9d7320
DJ
1077/**
1078 * Send the *IDN? SCPI command, receive the reply, parse it and store the
1079 * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer.
1080 *
d5976d8b
UH
1081 * The hw_info structure must be freed by the caller via sr_scpi_hw_info_free().
1082 *
23f43dff 1083 * @param scpi Previously initialised SCPI device structure.
7b9d7320
DJ
1084 * @param scpi_response Pointer where to store the hw_info structure.
1085 *
c0d25779 1086 * @return SR_OK upon success, SR_ERR* on failure.
7b9d7320 1087 */
23f43dff 1088SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi,
7b9d7320
DJ
1089 struct sr_scpi_hw_info **scpi_response)
1090{
c0d25779 1091 int num_tokens, ret;
7b9d7320
DJ
1092 char *response;
1093 gchar **tokens;
7b9d7320 1094 struct sr_scpi_hw_info *hw_info;
47bbc4b5 1095 gchar *idn_substr;
7b9d7320
DJ
1096
1097 response = NULL;
1098 tokens = NULL;
1099
c0d25779
BV
1100 ret = sr_scpi_get_string(scpi, SCPI_CMD_IDN, &response);
1101 if (ret != SR_OK && !response)
1102 return ret;
7b9d7320
DJ
1103
1104 /*
1105 * The response to a '*IDN?' is specified by the SCPI spec. It contains
1106 * a comma-separated list containing the manufacturer name, instrument
1107 * model, serial number of the instrument and the firmware version.
1108 */
1109 tokens = g_strsplit(response, ",", 0);
a1ce15d4 1110 num_tokens = g_strv_length(tokens);
0c08023f 1111 if (num_tokens < 4) {
d5976d8b 1112 sr_dbg("IDN response not according to spec: %80.s.", response);
7b9d7320
DJ
1113 g_strfreev(tokens);
1114 g_free(response);
c0d25779 1115 return SR_ERR_DATA;
7b9d7320
DJ
1116 }
1117 g_free(response);
1118
91219afc 1119 hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info));
47bbc4b5
SP
1120
1121 idn_substr = g_strstr_len(tokens[0], -1, "IDN ");
1122 if (idn_substr == NULL)
1123 hw_info->manufacturer = g_strstrip(g_strdup(tokens[0]));
1124 else
1125 hw_info->manufacturer = g_strstrip(g_strdup(idn_substr + 4));
1126
558d438d
BV
1127 hw_info->model = g_strstrip(g_strdup(tokens[1]));
1128 hw_info->serial_number = g_strstrip(g_strdup(tokens[2]));
1129 hw_info->firmware_version = g_strstrip(g_strdup(tokens[3]));
7b9d7320
DJ
1130
1131 g_strfreev(tokens);
1132
1133 *scpi_response = hw_info;
1134
1135 return SR_OK;
1136}
1137
1138/**
1139 * Free a sr_scpi_hw_info struct.
1140 *
7b365c47
UH
1141 * @param hw_info Pointer to the struct to free. If NULL, this
1142 * function does nothing.
7b9d7320
DJ
1143 */
1144SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info)
1145{
7b365c47
UH
1146 if (!hw_info)
1147 return;
1148
1149 g_free(hw_info->manufacturer);
1150 g_free(hw_info->model);
1151 g_free(hw_info->serial_number);
1152 g_free(hw_info->firmware_version);
1153 g_free(hw_info);
7b9d7320 1154}
fd20e59c 1155
a019bc48
GS
1156/**
1157 * Remove potentially enclosing pairs of quotes, un-escape content.
1158 * This implementation modifies the caller's buffer when quotes are found
1159 * and doubled quote characters need to get removed from the content.
1160 *
1161 * @param[in, out] s The SCPI string to check and un-quote.
1162 *
1163 * @return The start of the un-quoted string.
1164 */
1165SR_PRIV const char *sr_scpi_unquote_string(char *s)
1166{
1167 size_t s_len;
1168 char quotes[3];
1169 char *rdptr;
1170
1171 /* Immediately bail out on invalid or short input. */
1172 if (!s || !*s)
1173 return s;
1174 s_len = strlen(s);
1175 if (s_len < 2)
1176 return s;
1177
1178 /* Check for matching quote characters front and back. */
1179 if (s[0] != '\'' && s[0] != '"')
1180 return s;
1181 if (s[0] != s[s_len - 1])
1182 return s;
1183
1184 /* Need to strip quotes, and un-double quote chars inside. */
1185 quotes[0] = quotes[1] = *s;
1186 quotes[2] = '\0';
1187 s[s_len - 1] = '\0';
1188 s++;
1189 rdptr = s;
1190 while ((rdptr = strstr(rdptr, quotes)) != NULL) {
1191 memmove(rdptr, rdptr + 1, strlen(rdptr));
1192 rdptr++;
1193 }
1194
1195 return s;
1196}
1197
fd20e59c
FS
1198SR_PRIV const char *sr_vendor_alias(const char *raw_vendor)
1199{
1200 unsigned int i;
1201
1202 for (i = 0; i < ARRAY_SIZE(scpi_vendors); i++) {
1203 if (!g_ascii_strcasecmp(raw_vendor, scpi_vendors[i][0]))
1204 return scpi_vendors[i][1];
1205 }
1206
1207 return raw_vendor;
1208}
1209
17a82e83
FS
1210SR_PRIV const char *sr_scpi_cmd_get(const struct scpi_command *cmdtable,
1211 int command)
fd20e59c
FS
1212{
1213 unsigned int i;
1214 const char *cmd;
1215
1216 if (!cmdtable)
1217 return NULL;
1218
1219 cmd = NULL;
1220 for (i = 0; cmdtable[i].string; i++) {
1221 if (cmdtable[i].command == command) {
1222 cmd = cmdtable[i].string;
1223 break;
1224 }
1225 }
1226
1227 return cmd;
1228}
1229
17a82e83
FS
1230SR_PRIV int sr_scpi_cmd(const struct sr_dev_inst *sdi,
1231 const struct scpi_command *cmdtable,
1232 int channel_command, const char *channel_name,
fd20e59c
FS
1233 int command, ...)
1234{
1235 struct sr_scpi_dev_inst *scpi;
1236 va_list args;
1237 int ret;
17a82e83 1238 const char *channel_cmd;
fd20e59c
FS
1239 const char *cmd;
1240
17a82e83
FS
1241 scpi = sdi->conn;
1242
fd20e59c
FS
1243 if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) {
1244 /* Device does not implement this command, that's OK. */
1245 return SR_OK;
1246 }
1247
17a82e83
FS
1248 g_mutex_lock(&scpi->scpi_mutex);
1249
1250 /* Select channel. */
1251 channel_cmd = sr_scpi_cmd_get(cmdtable, channel_command);
1252 if (channel_cmd && channel_name &&
1253 g_strcmp0(channel_name, scpi->actual_channel_name)) {
1254 sr_spew("sr_scpi_cmd(): new channel = %s", channel_name);
3d118722
ML
1255 g_free(scpi->actual_channel_name);
1256 scpi->actual_channel_name = g_strdup(channel_name);
17a82e83
FS
1257 ret = scpi_send(scpi, channel_cmd, channel_name);
1258 if (ret != SR_OK)
1259 return ret;
1260 }
1261
fd20e59c 1262 va_start(args, command);
17a82e83 1263 ret = scpi_send_variadic(scpi, cmd, args);
fd20e59c
FS
1264 va_end(args);
1265
17a82e83
FS
1266 g_mutex_unlock(&scpi->scpi_mutex);
1267
fd20e59c
FS
1268 return ret;
1269}
1270
1271SR_PRIV int sr_scpi_cmd_resp(const struct sr_dev_inst *sdi,
1272 const struct scpi_command *cmdtable,
17a82e83 1273 int channel_command, const char *channel_name,
fd20e59c
FS
1274 GVariant **gvar, const GVariantType *gvtype, int command, ...)
1275{
1276 struct sr_scpi_dev_inst *scpi;
1277 va_list args;
17a82e83 1278 const char *channel_cmd;
fd20e59c
FS
1279 const char *cmd;
1280 GString *response;
1281 char *s;
1282 gboolean b;
1283 double d;
1284 int ret;
1285
1286 scpi = sdi->conn;
1287
1288 if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) {
1289 /* Device does not implement this command. */
1290 return SR_ERR_NA;
1291 }
1292
1293 g_mutex_lock(&scpi->scpi_mutex);
1294
17a82e83
FS
1295 /* Select channel. */
1296 channel_cmd = sr_scpi_cmd_get(cmdtable, channel_command);
1297 if (channel_cmd && channel_name &&
1298 g_strcmp0(channel_name, scpi->actual_channel_name)) {
1299 sr_spew("sr_scpi_cmd_get(): new channel = %s", channel_name);
3d118722
ML
1300 g_free(scpi->actual_channel_name);
1301 scpi->actual_channel_name = g_strdup(channel_name);
17a82e83
FS
1302 ret = scpi_send(scpi, channel_cmd, channel_name);
1303 if (ret != SR_OK)
1304 return ret;
1305 }
1306
fd20e59c
FS
1307 va_start(args, command);
1308 ret = scpi_send_variadic(scpi, cmd, args);
1309 va_end(args);
1310 if (ret != SR_OK) {
1311 g_mutex_unlock(&scpi->scpi_mutex);
1312 return ret;
1313 }
1314
1315 response = g_string_sized_new(1024);
1316 ret = scpi_get_data(scpi, NULL, &response);
1317 if (ret != SR_OK) {
1318 g_mutex_unlock(&scpi->scpi_mutex);
1319 if (response)
1320 g_string_free(response, TRUE);
1321 return ret;
1322 }
1323
1324 g_mutex_unlock(&scpi->scpi_mutex);
1325
1326 /* Get rid of trailing linefeed if present */
1327 if (response->len >= 1 && response->str[response->len - 1] == '\n')
1328 g_string_truncate(response, response->len - 1);
1329
1330 /* Get rid of trailing carriage return if present */
1331 if (response->len >= 1 && response->str[response->len - 1] == '\r')
1332 g_string_truncate(response, response->len - 1);
1333
1334 s = g_string_free(response, FALSE);
1335
1336 ret = SR_OK;
1337 if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_BOOLEAN)) {
1338 if ((ret = parse_strict_bool(s, &b)) == SR_OK)
1339 *gvar = g_variant_new_boolean(b);
1340 } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_DOUBLE)) {
1341 if ((ret = sr_atod_ascii(s, &d)) == SR_OK)
1342 *gvar = g_variant_new_double(d);
1343 } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_STRING)) {
1344 *gvar = g_variant_new_string(s);
1345 } else {
1346 sr_err("Unable to convert to desired GVariant type.");
1347 ret = SR_ERR_NA;
1348 }
1349
1350 g_free(s);
1351
1352 return ret;
1353}