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