]> sigrok.org Git - libsigrok.git/blame - src/scpi/scpi.c
scpi: readability nits in vector getters, style nit in malloc call
[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 *
9b915e3a
GS
413 * Callers must free the allocated memory regardless of the routine's
414 * return code. See @ref g_free().
415 *
416 * @param[in] scpi Previously initialized SCPI device structure.
417 * @param[out] connection_id Pointer where to store the connection ID.
8107a9a6
FS
418 *
419 * @return SR_OK on success, SR_ERR on failure.
420 */
421SR_PRIV int sr_scpi_connection_id(struct sr_scpi_dev_inst *scpi,
422 char **connection_id)
423{
424 return scpi->connection_id(scpi, connection_id);
425}
426
23f43dff
ML
427/**
428 * Add an event source for an SCPI device.
429 *
7efe889e 430 * @param session The session to add the event source to.
23f43dff
ML
431 * @param scpi Previously initialized SCPI device structure.
432 * @param events Events to check for.
433 * @param timeout Max time to wait before the callback is called, ignored if 0.
434 * @param cb Callback function to add. Must not be NULL.
435 * @param cb_data Data for the callback function. Can be NULL.
436 *
437 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
438 * SR_ERR_MALLOC upon memory allocation errors.
439 */
102f1239
BV
440SR_PRIV int sr_scpi_source_add(struct sr_session *session,
441 struct sr_scpi_dev_inst *scpi, int events, int timeout,
442 sr_receive_data_callback cb, void *cb_data)
23f43dff 443{
102f1239 444 return scpi->source_add(session, scpi->priv, events, timeout, cb, cb_data);
23f43dff
ML
445}
446
447/**
448 * Remove event source for an SCPI device.
449 *
7efe889e 450 * @param session The session to remove the event source from.
23f43dff
ML
451 * @param scpi Previously initialized SCPI device structure.
452 *
453 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
454 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
455 * internal errors.
456 */
102f1239
BV
457SR_PRIV int sr_scpi_source_remove(struct sr_session *session,
458 struct sr_scpi_dev_inst *scpi)
23f43dff 459{
102f1239 460 return scpi->source_remove(session, scpi->priv);
23f43dff
ML
461}
462
7b9d7320
DJ
463/**
464 * Send a SCPI command.
465 *
23f43dff 466 * @param scpi Previously initialized SCPI device structure.
504f40a5 467 * @param format Format string, to be followed by any necessary arguments.
7b9d7320
DJ
468 *
469 * @return SR_OK on success, SR_ERR on failure.
470 */
23f43dff 471SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi,
504f40a5 472 const char *format, ...)
7b9d7320 473{
87c41083
ML
474 va_list args;
475 int ret;
476
477 va_start(args, format);
fd20e59c
FS
478 g_mutex_lock(&scpi->scpi_mutex);
479 ret = scpi_send_variadic(scpi, format, args);
480 g_mutex_unlock(&scpi->scpi_mutex);
87c41083
ML
481 va_end(args);
482
483 return ret;
484}
485
486/**
487 * Send a SCPI command with a variadic argument list.
488 *
489 * @param scpi Previously initialized SCPI device structure.
490 * @param format Format string.
491 * @param args Argument list.
492 *
493 * @return SR_OK on success, SR_ERR on failure.
494 */
495SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi,
496 const char *format, va_list args)
497{
fd20e59c 498 int ret;
504f40a5 499
fd20e59c
FS
500 g_mutex_lock(&scpi->scpi_mutex);
501 ret = scpi_send_variadic(scpi, format, args);
502 g_mutex_unlock(&scpi->scpi_mutex);
504f40a5
ML
503
504 return ret;
23f43dff 505}
7b9d7320 506
23f43dff 507/**
05c644ea 508 * Begin receiving an SCPI reply.
23f43dff
ML
509 *
510 * @param scpi Previously initialised SCPI device structure.
23f43dff 511 *
05c644ea 512 * @return SR_OK on success, SR_ERR on failure.
23f43dff 513 */
05c644ea 514SR_PRIV int sr_scpi_read_begin(struct sr_scpi_dev_inst *scpi)
23f43dff 515{
05c644ea 516 return scpi->read_begin(scpi->priv);
23f43dff 517}
7b9d7320 518
a1ff9c18
ML
519/**
520 * Read part of a response from SCPI device.
521 *
522 * @param scpi Previously initialised SCPI device structure.
523 * @param buf Buffer to store result.
524 * @param maxlen Maximum number of bytes to read.
525 *
526 * @return Number of bytes read, or SR_ERR upon failure.
527 */
05c644ea 528SR_PRIV int sr_scpi_read_data(struct sr_scpi_dev_inst *scpi,
a1ff9c18
ML
529 char *buf, int maxlen)
530{
fd20e59c
FS
531 int ret;
532
533 g_mutex_lock(&scpi->scpi_mutex);
534 ret = scpi_read_data(scpi, buf, maxlen);
535 g_mutex_unlock(&scpi->scpi_mutex);
536
537 return ret;
05c644ea
ML
538}
539
b6be55ce
SS
540/**
541 * Send data to SCPI device.
542 *
fd20e59c
FS
543 * TODO: This is only implemented in TcpRaw, but never used.
544 * TODO: Use Mutex at all?
545 *
b6be55ce
SS
546 * @param scpi Previously initialised SCPI device structure.
547 * @param buf Buffer with data to send.
548 * @param len Number of bytes to send.
549 *
550 * @return Number of bytes read, or SR_ERR upon failure.
551 */
552SR_PRIV int sr_scpi_write_data(struct sr_scpi_dev_inst *scpi,
553 char *buf, int maxlen)
554{
fd20e59c
FS
555 int ret;
556
557 g_mutex_lock(&scpi->scpi_mutex);
558 ret = scpi_write_data(scpi, buf, maxlen);
559 g_mutex_unlock(&scpi->scpi_mutex);
560
561 return ret;
b6be55ce
SS
562}
563
05c644ea
ML
564/**
565 * Check whether a complete SCPI response has been received.
566 *
567 * @param scpi Previously initialised SCPI device structure.
568 *
569 * @return 1 if complete, 0 otherwise.
570 */
571SR_PRIV int sr_scpi_read_complete(struct sr_scpi_dev_inst *scpi)
572{
573 return scpi->read_complete(scpi->priv);
a1ff9c18
ML
574}
575
23f43dff
ML
576/**
577 * Close SCPI device.
578 *
579 * @param scpi Previously initialized SCPI device structure.
580 *
581 * @return SR_OK on success, SR_ERR on failure.
582 */
583SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi)
584{
fd20e59c
FS
585 int ret;
586
587 g_mutex_lock(&scpi->scpi_mutex);
588 ret = scpi->close(scpi);
589 g_mutex_unlock(&scpi->scpi_mutex);
590 g_mutex_clear(&scpi->scpi_mutex);
591
592 return ret;
23f43dff 593}
7b9d7320 594
23f43dff
ML
595/**
596 * Free SCPI device.
597 *
81568546
UH
598 * @param scpi Previously initialized SCPI device structure. If NULL,
599 * this function does nothing.
23f43dff
ML
600 */
601SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi)
602{
81568546
UH
603 if (!scpi)
604 return;
605
23f43dff 606 scpi->free(scpi->priv);
f754c146 607 g_free(scpi->priv);
3d118722 608 g_free(scpi->actual_channel_name);
23f43dff 609 g_free(scpi);
7b9d7320
DJ
610}
611
612/**
613 * Send a SCPI command, receive the reply and store the reply in scpi_response.
614 *
9b915e3a
GS
615 * Callers must free the allocated memory regardless of the routine's
616 * return code. See @ref g_free().
617 *
618 * @param[in] scpi Previously initialised SCPI device structure.
619 * @param[in] command The SCPI command to send to the device (can be NULL).
620 * @param[out] scpi_response Pointer where to store the SCPI response.
7b9d7320 621 *
c0d25779 622 * @return SR_OK on success, SR_ERR* on failure.
7b9d7320 623 */
23f43dff 624SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi,
d5976d8b 625 const char *command, char **scpi_response)
7b9d7320 626{
ff01b0ea 627 GString *response;
ff01b0ea 628
4c729664
GS
629 *scpi_response = NULL;
630
631 response = g_string_sized_new(1024);
ff01b0ea
SB
632 if (sr_scpi_get_data(scpi, command, &response) != SR_OK) {
633 if (response)
634 g_string_free(response, TRUE);
635 return SR_ERR;
636 }
637
638 /* Get rid of trailing linefeed if present */
639 if (response->len >= 1 && response->str[response->len - 1] == '\n')
640 g_string_truncate(response, response->len - 1);
641
642 /* Get rid of trailing carriage return if present */
643 if (response->len >= 1 && response->str[response->len - 1] == '\r')
644 g_string_truncate(response, response->len - 1);
645
646 sr_spew("Got response: '%.70s', length %" G_GSIZE_FORMAT ".",
647 response->str, response->len);
648
649 *scpi_response = g_string_free(response, FALSE);
650
651 return SR_OK;
652}
653
d64be25b
SB
654/**
655 * Do a non-blocking read of up to the allocated length, and
656 * check if a timeout has occured.
657 *
658 * @param scpi Previously initialised SCPI device structure.
659 * @param response Buffer to which the response is appended.
660 * @param abs_timeout_us Absolute timeout in microseconds
661 *
662 * @return read length on success, SR_ERR* on failure.
663 */
664SR_PRIV int sr_scpi_read_response(struct sr_scpi_dev_inst *scpi,
665 GString *response, gint64 abs_timeout_us)
666{
fd20e59c 667 int ret;
d64be25b 668
fd20e59c
FS
669 g_mutex_lock(&scpi->scpi_mutex);
670 ret = scpi_read_response(scpi, response, abs_timeout_us);
671 g_mutex_unlock(&scpi->scpi_mutex);
d64be25b 672
fd20e59c 673 return ret;
d64be25b
SB
674}
675
ff01b0ea
SB
676SR_PRIV int sr_scpi_get_data(struct sr_scpi_dev_inst *scpi,
677 const char *command, GString **scpi_response)
678{
d64be25b 679 int ret;
37ef582d 680
fd20e59c
FS
681 g_mutex_lock(&scpi->scpi_mutex);
682 ret = scpi_get_data(scpi, command, scpi_response);
683 g_mutex_unlock(&scpi->scpi_mutex);
05c644ea 684
fd20e59c 685 return ret;
d730f70e
DJ
686}
687
688/**
689 * Send a SCPI command, read the reply, parse it as a bool value and store the
690 * result in scpi_response.
691 *
23f43dff 692 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
693 * @param command The SCPI command to send to the device (can be NULL).
694 * @param scpi_response Pointer where to store the parsed result.
695 *
c0d25779 696 * @return SR_OK on success, SR_ERR* on failure.
d730f70e 697 */
23f43dff 698SR_PRIV int sr_scpi_get_bool(struct sr_scpi_dev_inst *scpi,
d730f70e
DJ
699 const char *command, gboolean *scpi_response)
700{
701 int ret;
702 char *response;
703
704 response = NULL;
705
c0d25779
BV
706 ret = sr_scpi_get_string(scpi, command, &response);
707 if (ret != SR_OK && !response)
708 return ret;
d730f70e 709
d5976d8b 710 if (parse_strict_bool(response, scpi_response) == SR_OK)
d730f70e
DJ
711 ret = SR_OK;
712 else
c0d25779 713 ret = SR_ERR_DATA;
d730f70e
DJ
714
715 g_free(response);
716
717 return ret;
718}
719
720/**
721 * Send a SCPI command, read the reply, parse it as an integer and store the
722 * result in scpi_response.
723 *
23f43dff 724 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
725 * @param command The SCPI command to send to the device (can be NULL).
726 * @param scpi_response Pointer where to store the parsed result.
727 *
c0d25779 728 * @return SR_OK on success, SR_ERR* on failure.
d730f70e 729 */
23f43dff 730SR_PRIV int sr_scpi_get_int(struct sr_scpi_dev_inst *scpi,
d5976d8b 731 const char *command, int *scpi_response)
d730f70e
DJ
732{
733 int ret;
2dddd5bd 734 struct sr_rational ret_rational;
d730f70e
DJ
735 char *response;
736
737 response = NULL;
738
c0d25779
BV
739 ret = sr_scpi_get_string(scpi, command, &response);
740 if (ret != SR_OK && !response)
741 return ret;
d730f70e 742
2dddd5bd
R
743 ret = sr_parse_rational(response, &ret_rational);
744 if (ret == SR_OK && (ret_rational.p % ret_rational.q) == 0) {
745 *scpi_response = ret_rational.p / ret_rational.q;
746 } else {
747 sr_dbg("get_int: non-integer rational=%" PRId64 "/%" PRIu64,
748 ret_rational.p, ret_rational.q);
c0d25779 749 ret = SR_ERR_DATA;
2dddd5bd 750 }
d730f70e
DJ
751
752 g_free(response);
753
754 return ret;
755}
756
757/**
758 * Send a SCPI command, read the reply, parse it as a float and store the
759 * result in scpi_response.
760 *
23f43dff 761 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
762 * @param command The SCPI command to send to the device (can be NULL).
763 * @param scpi_response Pointer where to store the parsed result.
764 *
c0d25779 765 * @return SR_OK on success, SR_ERR* on failure.
d730f70e 766 */
23f43dff 767SR_PRIV int sr_scpi_get_float(struct sr_scpi_dev_inst *scpi,
d730f70e
DJ
768 const char *command, float *scpi_response)
769{
770 int ret;
771 char *response;
772
773 response = NULL;
774
c0d25779
BV
775 ret = sr_scpi_get_string(scpi, command, &response);
776 if (ret != SR_OK && !response)
777 return ret;
d730f70e 778
13dbd151 779 if (sr_atof_ascii(response, scpi_response) == SR_OK)
d730f70e
DJ
780 ret = SR_OK;
781 else
c0d25779 782 ret = SR_ERR_DATA;
d730f70e
DJ
783
784 g_free(response);
785
786 return ret;
787}
788
789/**
790 * Send a SCPI command, read the reply, parse it as a double and store the
791 * result in scpi_response.
792 *
23f43dff 793 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
794 * @param command The SCPI command to send to the device (can be NULL).
795 * @param scpi_response Pointer where to store the parsed result.
796 *
c0d25779 797 * @return SR_OK on success, SR_ERR* on failure.
d730f70e 798 */
23f43dff 799SR_PRIV int sr_scpi_get_double(struct sr_scpi_dev_inst *scpi,
d5976d8b 800 const char *command, double *scpi_response)
d730f70e
DJ
801{
802 int ret;
803 char *response;
804
805 response = NULL;
806
c0d25779
BV
807 ret = sr_scpi_get_string(scpi, command, &response);
808 if (ret != SR_OK && !response)
809 return ret;
d730f70e 810
4f0463a0 811 if (sr_atod_ascii(response, scpi_response) == SR_OK)
d730f70e
DJ
812 ret = SR_OK;
813 else
c0d25779 814 ret = SR_ERR_DATA;
d730f70e
DJ
815
816 g_free(response);
817
7b9d7320
DJ
818 return ret;
819}
820
f5922ade
DJ
821/**
822 * Send a SCPI *OPC? command, read the reply and return the result of the
823 * command.
824 *
23f43dff 825 * @param scpi Previously initialised SCPI device structure.
f5922ade 826 *
c0d25779 827 * @return SR_OK on success, SR_ERR* on failure.
f5922ade 828 */
23f43dff 829SR_PRIV int sr_scpi_get_opc(struct sr_scpi_dev_inst *scpi)
f5922ade
DJ
830{
831 unsigned int i;
832 gboolean opc;
833
0a1f7b09 834 for (i = 0; i < SCPI_READ_RETRIES; i++) {
da6f107e 835 opc = FALSE;
23f43dff 836 sr_scpi_get_bool(scpi, SCPI_CMD_OPC, &opc);
f5922ade
DJ
837 if (opc)
838 return SR_OK;
1a46cc62 839 g_usleep(SCPI_READ_RETRY_TIMEOUT_US);
f5922ade
DJ
840 }
841
842 return SR_ERR;
843}
844
8acbb89a
DJ
845/**
846 * Send a SCPI command, read the reply, parse it as comma separated list of
847 * floats and store the as an result in scpi_response.
848 *
9b915e3a
GS
849 * Callers must free the allocated memory (unless it's NULL) regardless of
850 * the routine's return code. See @ref g_array_free().
851 *
852 * @param[in] scpi Previously initialised SCPI device structure.
853 * @param[in] command The SCPI command to send to the device (can be NULL).
854 * @param[out] scpi_response Pointer where to store the parsed result.
8acbb89a 855 *
c0d25779 856 * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing
9b915e3a 857 * error or upon no response.
8acbb89a 858 */
23f43dff 859SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi,
d5976d8b 860 const char *command, GArray **scpi_response)
8acbb89a
DJ
861{
862 int ret;
863 float tmp;
864 char *response;
d5976d8b 865 gchar **ptr, **tokens;
191af3d9 866 size_t token_count;
8acbb89a
DJ
867 GArray *response_array;
868
4c729664 869 *scpi_response = NULL;
8acbb89a 870
191af3d9 871 response = NULL;
c0d25779
BV
872 ret = sr_scpi_get_string(scpi, command, &response);
873 if (ret != SR_OK && !response)
874 return ret;
8acbb89a
DJ
875
876 tokens = g_strsplit(response, ",", 0);
191af3d9 877 token_count = g_strv_length(tokens);
8acbb89a 878
191af3d9
GS
879 response_array = g_array_sized_new(TRUE, FALSE,
880 sizeof(float), token_count + 1);
8acbb89a 881
191af3d9 882 ptr = tokens;
d5976d8b 883 while (*ptr) {
191af3d9
GS
884 ret = sr_atof_ascii(*ptr, &tmp);
885 if (ret != SR_OK) {
c0d25779 886 ret = SR_ERR_DATA;
191af3d9
GS
887 break;
888 }
889 response_array = g_array_append_val(response_array, tmp);
8acbb89a
DJ
890 ptr++;
891 }
892 g_strfreev(tokens);
893 g_free(response);
894
c0d25779 895 if (ret != SR_OK && response_array->len == 0) {
8acbb89a 896 g_array_free(response_array, TRUE);
c0d25779 897 return SR_ERR_DATA;
8acbb89a
DJ
898 }
899
900 *scpi_response = response_array;
901
1a323dd8
DJ
902 return ret;
903}
904
905/**
906 * Send a SCPI command, read the reply, parse it as comma separated list of
907 * unsigned 8 bit integers and store the as an result in scpi_response.
908 *
9b915e3a
GS
909 * Callers must free the allocated memory (unless it's NULL) regardless of
910 * the routine's return code. See @ref g_array_free().
911 *
912 * @param[in] scpi Previously initialised SCPI device structure.
913 * @param[in] command The SCPI command to send to the device (can be NULL).
914 * @param[out] scpi_response Pointer where to store the parsed result.
1a323dd8 915 *
c0d25779 916 * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing
9b915e3a 917 * error or upon no response.
1a323dd8 918 */
23f43dff 919SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi,
d5976d8b 920 const char *command, GArray **scpi_response)
1a323dd8 921{
d5976d8b 922 int tmp, ret;
1a323dd8 923 char *response;
d5976d8b 924 gchar **ptr, **tokens;
191af3d9 925 size_t token_count;
1a323dd8
DJ
926 GArray *response_array;
927
4c729664 928 *scpi_response = NULL;
1a323dd8 929
191af3d9 930 response = NULL;
c0d25779
BV
931 ret = sr_scpi_get_string(scpi, command, &response);
932 if (ret != SR_OK && !response)
933 return ret;
1a323dd8
DJ
934
935 tokens = g_strsplit(response, ",", 0);
191af3d9 936 token_count = g_strv_length(tokens);
1a323dd8 937
191af3d9
GS
938 response_array = g_array_sized_new(TRUE, FALSE,
939 sizeof(uint8_t), token_count + 1);
1a323dd8 940
191af3d9 941 ptr = tokens;
d5976d8b 942 while (*ptr) {
191af3d9
GS
943 ret = sr_atoi(*ptr, &tmp);
944 if (ret != SR_OK) {
c0d25779 945 ret = SR_ERR_DATA;
191af3d9
GS
946 break;
947 }
948 response_array = g_array_append_val(response_array, tmp);
1a323dd8
DJ
949 ptr++;
950 }
951 g_strfreev(tokens);
952 g_free(response);
953
954 if (response_array->len == 0) {
955 g_array_free(response_array, TRUE);
c0d25779 956 return SR_ERR_DATA;
1a323dd8
DJ
957 }
958
959 *scpi_response = response_array;
960
8acbb89a
DJ
961 return ret;
962}
d3de86f3 963
ff01b0ea
SB
964/**
965 * Send a SCPI command, read the reply, parse it as binary data with a
966 * "definite length block" header and store the as an result in scpi_response.
967 *
9b915e3a
GS
968 * Callers must free the allocated memory (unless it's NULL) regardless of
969 * the routine's return code. See @ref g_byte_array_free().
970 *
971 * @param[in] scpi Previously initialised SCPI device structure.
972 * @param[in] command The SCPI command to send to the device (can be NULL).
973 * @param[out] scpi_response Pointer where to store the parsed result.
ff01b0ea
SB
974 *
975 * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing
9b915e3a 976 * error or upon no response.
ff01b0ea
SB
977 */
978SR_PRIV int sr_scpi_get_block(struct sr_scpi_dev_inst *scpi,
979 const char *command, GByteArray **scpi_response)
980{
981 int ret;
982 GString* response;
bb066586 983 gsize oldlen;
26e8c6a2 984 char buf[10];
ff01b0ea
SB
985 long llen;
986 long datalen;
d64be25b 987 gint64 timeout;
fd20e59c 988
4c729664
GS
989 *scpi_response = NULL;
990
fd20e59c 991 g_mutex_lock(&scpi->scpi_mutex);
d64be25b
SB
992
993 if (command)
17a82e83 994 if (scpi_send(scpi, command) != SR_OK) {
fd20e59c 995 g_mutex_unlock(&scpi->scpi_mutex);
d64be25b 996 return SR_ERR;
fd20e59c 997 }
d64be25b 998
fd20e59c
FS
999 if (sr_scpi_read_begin(scpi) != SR_OK) {
1000 g_mutex_unlock(&scpi->scpi_mutex);
d64be25b 1001 return SR_ERR;
fd20e59c 1002 }
ff01b0ea 1003
26e8c6a2
GS
1004 /*
1005 * Assume an initial maximum length, optionally gets adjusted below.
1006 * Prepare a NULL return value for when error paths will be taken.
1007 */
ff01b0ea 1008 response = g_string_sized_new(1024);
d64be25b
SB
1009
1010 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
1011
26e8c6a2 1012 /* Get (the first chunk of) the response. */
bb066586 1013 do {
fd20e59c 1014 ret = scpi_read_response(scpi, response, timeout);
d64be25b 1015 if (ret < 0) {
fd20e59c 1016 g_mutex_unlock(&scpi->scpi_mutex);
d64be25b
SB
1017 g_string_free(response, TRUE);
1018 return ret;
1019 }
bb066586 1020 } while (response->len < 2);
ff01b0ea 1021
26e8c6a2
GS
1022 /*
1023 * SCPI protocol data blocks are preceeded with a length spec.
1024 * The length spec consists of a '#' marker, one digit which
1025 * specifies the character count of the length spec, and the
1026 * respective number of characters which specify the data block's
1027 * length. Raw data bytes follow (thus one must no longer assume
1028 * that the received input stream would be an ASCIIZ string).
1029 *
1030 * Get the data block length, and strip off the length spec from
1031 * the input buffer, leaving just the data bytes.
1032 */
ff01b0ea 1033 if (response->str[0] != '#') {
fd20e59c 1034 g_mutex_unlock(&scpi->scpi_mutex);
ff01b0ea
SB
1035 g_string_free(response, TRUE);
1036 return SR_ERR_DATA;
1037 }
ff01b0ea 1038 buf[0] = response->str[1];
26e8c6a2 1039 buf[1] = '\0';
ff01b0ea
SB
1040 ret = sr_atol(buf, &llen);
1041 if ((ret != SR_OK) || (llen == 0)) {
fd20e59c 1042 g_mutex_unlock(&scpi->scpi_mutex);
ff01b0ea
SB
1043 g_string_free(response, TRUE);
1044 return ret;
1045 }
d64be25b
SB
1046
1047 while (response->len < (unsigned long)(2 + llen)) {
fd20e59c 1048 ret = scpi_read_response(scpi, response, timeout);
d64be25b 1049 if (ret < 0) {
fd20e59c 1050 g_mutex_unlock(&scpi->scpi_mutex);
d64be25b
SB
1051 g_string_free(response, TRUE);
1052 return ret;
1053 }
1054 }
1055
ff01b0ea 1056 memcpy(buf, &response->str[2], llen);
26e8c6a2 1057 buf[llen] = '\0';
ff01b0ea
SB
1058 ret = sr_atol(buf, &datalen);
1059 if ((ret != SR_OK) || (datalen == 0)) {
fd20e59c 1060 g_mutex_unlock(&scpi->scpi_mutex);
ff01b0ea
SB
1061 g_string_free(response, TRUE);
1062 return ret;
1063 }
ff01b0ea
SB
1064 g_string_erase(response, 0, 2 + llen);
1065
26e8c6a2 1066 /*
bb066586
GT
1067 * Re-allocate the buffer size to the now known length
1068 * and keep reading more chunks of response data.
26e8c6a2 1069 */
bb066586
GT
1070 oldlen = response->len;
1071 g_string_set_size(response, datalen);
1072 g_string_set_size(response, oldlen);
1073
1074 if (oldlen < (unsigned long)(datalen)) {
1075 do {
1076 oldlen = response->len;
1077 ret = scpi_read_response(scpi, response, timeout);
1078
1079 /* On timeout truncate the buffer and send the partial response
1080 * instead of getting stuck on timeouts...
1081 */
1082 if (ret == SR_ERR_TIMEOUT) {
1083 datalen = oldlen;
1084 break;
1085 }
1086 if (ret < 0) {
1087 g_mutex_unlock(&scpi->scpi_mutex);
1088 g_string_free(response, TRUE);
1089 return ret;
1090 }
1091 if (ret > 0)
1092 timeout = g_get_monotonic_time() + scpi->read_timeout_us;
1093 } while (response->len < (unsigned long)(datalen));
ff01b0ea
SB
1094 }
1095
fd20e59c
FS
1096 g_mutex_unlock(&scpi->scpi_mutex);
1097
26e8c6a2 1098 /* Convert received data to byte array. */
ff01b0ea
SB
1099 *scpi_response = g_byte_array_new_take(
1100 (guint8*)g_string_free(response, FALSE), datalen);
1101
d64be25b 1102 return SR_OK;
ff01b0ea
SB
1103}
1104
7b9d7320
DJ
1105/**
1106 * Send the *IDN? SCPI command, receive the reply, parse it and store the
1107 * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer.
1108 *
9b915e3a
GS
1109 * Callers must free the allocated memory regardless of the routine's
1110 * return code. See @ref sr_scpi_hw_info_free().
d5976d8b 1111 *
9b915e3a
GS
1112 * @param[in] scpi Previously initialised SCPI device structure.
1113 * @param[out] scpi_response Pointer where to store the hw_info structure.
7b9d7320 1114 *
c0d25779 1115 * @return SR_OK upon success, SR_ERR* on failure.
7b9d7320 1116 */
23f43dff 1117SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi,
7b9d7320
DJ
1118 struct sr_scpi_hw_info **scpi_response)
1119{
c0d25779 1120 int num_tokens, ret;
7b9d7320
DJ
1121 char *response;
1122 gchar **tokens;
7b9d7320 1123 struct sr_scpi_hw_info *hw_info;
47bbc4b5 1124 gchar *idn_substr;
7b9d7320 1125
4c729664 1126 *scpi_response = NULL;
7b9d7320
DJ
1127 response = NULL;
1128 tokens = NULL;
1129
c0d25779
BV
1130 ret = sr_scpi_get_string(scpi, SCPI_CMD_IDN, &response);
1131 if (ret != SR_OK && !response)
1132 return ret;
7b9d7320
DJ
1133
1134 /*
1135 * The response to a '*IDN?' is specified by the SCPI spec. It contains
1136 * a comma-separated list containing the manufacturer name, instrument
1137 * model, serial number of the instrument and the firmware version.
72cd558d
GS
1138 *
1139 * BEWARE! Although strictly speaking a smaller field count is invalid,
1140 * this implementation also accepts IDN responses with one field less,
1141 * and assumes that the serial number is missing. Some GWInstek DMMs
1142 * were found to do this. Keep warning about this condition, which may
1143 * need more consideration later.
7b9d7320
DJ
1144 */
1145 tokens = g_strsplit(response, ",", 0);
a1ce15d4 1146 num_tokens = g_strv_length(tokens);
47e7a639
TK
1147 if (num_tokens < 3) {
1148 sr_dbg("IDN response not according to spec: '%s'", response);
7b9d7320
DJ
1149 g_strfreev(tokens);
1150 g_free(response);
c0d25779 1151 return SR_ERR_DATA;
7b9d7320 1152 }
72cd558d
GS
1153 if (num_tokens < 4) {
1154 sr_warn("Short IDN response, assume missing serial number.");
1155 }
7b9d7320
DJ
1156 g_free(response);
1157
191af3d9 1158 hw_info = g_malloc0(sizeof(*hw_info));
47bbc4b5
SP
1159
1160 idn_substr = g_strstr_len(tokens[0], -1, "IDN ");
1161 if (idn_substr == NULL)
1162 hw_info->manufacturer = g_strstrip(g_strdup(tokens[0]));
1163 else
1164 hw_info->manufacturer = g_strstrip(g_strdup(idn_substr + 4));
1165
558d438d 1166 hw_info->model = g_strstrip(g_strdup(tokens[1]));
47e7a639
TK
1167 if (num_tokens < 4) {
1168 hw_info->serial_number = g_strdup("Unknown");
1169 hw_info->firmware_version = g_strstrip(g_strdup(tokens[2]));
1170 } else {
1171 hw_info->serial_number = g_strstrip(g_strdup(tokens[2]));
1172 hw_info->firmware_version = g_strstrip(g_strdup(tokens[3]));
1173 }
7b9d7320
DJ
1174
1175 g_strfreev(tokens);
1176
1177 *scpi_response = hw_info;
1178
1179 return SR_OK;
1180}
1181
1182/**
1183 * Free a sr_scpi_hw_info struct.
1184 *
7b365c47
UH
1185 * @param hw_info Pointer to the struct to free. If NULL, this
1186 * function does nothing.
7b9d7320
DJ
1187 */
1188SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info)
1189{
7b365c47
UH
1190 if (!hw_info)
1191 return;
1192
1193 g_free(hw_info->manufacturer);
1194 g_free(hw_info->model);
1195 g_free(hw_info->serial_number);
1196 g_free(hw_info->firmware_version);
1197 g_free(hw_info);
7b9d7320 1198}
fd20e59c 1199
a019bc48
GS
1200/**
1201 * Remove potentially enclosing pairs of quotes, un-escape content.
1202 * This implementation modifies the caller's buffer when quotes are found
1203 * and doubled quote characters need to get removed from the content.
1204 *
1205 * @param[in, out] s The SCPI string to check and un-quote.
1206 *
1207 * @return The start of the un-quoted string.
1208 */
1209SR_PRIV const char *sr_scpi_unquote_string(char *s)
1210{
1211 size_t s_len;
1212 char quotes[3];
1213 char *rdptr;
1214
1215 /* Immediately bail out on invalid or short input. */
1216 if (!s || !*s)
1217 return s;
1218 s_len = strlen(s);
1219 if (s_len < 2)
1220 return s;
1221
1222 /* Check for matching quote characters front and back. */
1223 if (s[0] != '\'' && s[0] != '"')
1224 return s;
1225 if (s[0] != s[s_len - 1])
1226 return s;
1227
1228 /* Need to strip quotes, and un-double quote chars inside. */
1229 quotes[0] = quotes[1] = *s;
1230 quotes[2] = '\0';
1231 s[s_len - 1] = '\0';
1232 s++;
1233 rdptr = s;
1234 while ((rdptr = strstr(rdptr, quotes)) != NULL) {
1235 memmove(rdptr, rdptr + 1, strlen(rdptr));
1236 rdptr++;
1237 }
1238
1239 return s;
1240}
1241
fd20e59c
FS
1242SR_PRIV const char *sr_vendor_alias(const char *raw_vendor)
1243{
1244 unsigned int i;
1245
1246 for (i = 0; i < ARRAY_SIZE(scpi_vendors); i++) {
1247 if (!g_ascii_strcasecmp(raw_vendor, scpi_vendors[i][0]))
1248 return scpi_vendors[i][1];
1249 }
1250
1251 return raw_vendor;
1252}
1253
17a82e83
FS
1254SR_PRIV const char *sr_scpi_cmd_get(const struct scpi_command *cmdtable,
1255 int command)
fd20e59c
FS
1256{
1257 unsigned int i;
1258 const char *cmd;
1259
1260 if (!cmdtable)
1261 return NULL;
1262
1263 cmd = NULL;
1264 for (i = 0; cmdtable[i].string; i++) {
1265 if (cmdtable[i].command == command) {
1266 cmd = cmdtable[i].string;
1267 break;
1268 }
1269 }
1270
1271 return cmd;
1272}
1273
17a82e83
FS
1274SR_PRIV int sr_scpi_cmd(const struct sr_dev_inst *sdi,
1275 const struct scpi_command *cmdtable,
1276 int channel_command, const char *channel_name,
fd20e59c
FS
1277 int command, ...)
1278{
1279 struct sr_scpi_dev_inst *scpi;
1280 va_list args;
1281 int ret;
17a82e83 1282 const char *channel_cmd;
fd20e59c
FS
1283 const char *cmd;
1284
17a82e83
FS
1285 scpi = sdi->conn;
1286
fd20e59c
FS
1287 if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) {
1288 /* Device does not implement this command, that's OK. */
1289 return SR_OK;
1290 }
1291
17a82e83
FS
1292 g_mutex_lock(&scpi->scpi_mutex);
1293
1294 /* Select channel. */
1295 channel_cmd = sr_scpi_cmd_get(cmdtable, channel_command);
1296 if (channel_cmd && channel_name &&
1297 g_strcmp0(channel_name, scpi->actual_channel_name)) {
1298 sr_spew("sr_scpi_cmd(): new channel = %s", channel_name);
3d118722
ML
1299 g_free(scpi->actual_channel_name);
1300 scpi->actual_channel_name = g_strdup(channel_name);
17a82e83
FS
1301 ret = scpi_send(scpi, channel_cmd, channel_name);
1302 if (ret != SR_OK)
1303 return ret;
1304 }
1305
fd20e59c 1306 va_start(args, command);
17a82e83 1307 ret = scpi_send_variadic(scpi, cmd, args);
fd20e59c
FS
1308 va_end(args);
1309
17a82e83
FS
1310 g_mutex_unlock(&scpi->scpi_mutex);
1311
fd20e59c
FS
1312 return ret;
1313}
1314
1315SR_PRIV int sr_scpi_cmd_resp(const struct sr_dev_inst *sdi,
1316 const struct scpi_command *cmdtable,
17a82e83 1317 int channel_command, const char *channel_name,
fd20e59c
FS
1318 GVariant **gvar, const GVariantType *gvtype, int command, ...)
1319{
1320 struct sr_scpi_dev_inst *scpi;
1321 va_list args;
17a82e83 1322 const char *channel_cmd;
fd20e59c
FS
1323 const char *cmd;
1324 GString *response;
1325 char *s;
1326 gboolean b;
1327 double d;
1328 int ret;
1329
1330 scpi = sdi->conn;
1331
1332 if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) {
1333 /* Device does not implement this command. */
1334 return SR_ERR_NA;
1335 }
1336
1337 g_mutex_lock(&scpi->scpi_mutex);
1338
17a82e83
FS
1339 /* Select channel. */
1340 channel_cmd = sr_scpi_cmd_get(cmdtable, channel_command);
1341 if (channel_cmd && channel_name &&
1342 g_strcmp0(channel_name, scpi->actual_channel_name)) {
1343 sr_spew("sr_scpi_cmd_get(): new channel = %s", channel_name);
3d118722
ML
1344 g_free(scpi->actual_channel_name);
1345 scpi->actual_channel_name = g_strdup(channel_name);
17a82e83
FS
1346 ret = scpi_send(scpi, channel_cmd, channel_name);
1347 if (ret != SR_OK)
1348 return ret;
1349 }
1350
fd20e59c
FS
1351 va_start(args, command);
1352 ret = scpi_send_variadic(scpi, cmd, args);
1353 va_end(args);
1354 if (ret != SR_OK) {
1355 g_mutex_unlock(&scpi->scpi_mutex);
1356 return ret;
1357 }
1358
1359 response = g_string_sized_new(1024);
1360 ret = scpi_get_data(scpi, NULL, &response);
1361 if (ret != SR_OK) {
1362 g_mutex_unlock(&scpi->scpi_mutex);
1363 if (response)
1364 g_string_free(response, TRUE);
1365 return ret;
1366 }
1367
1368 g_mutex_unlock(&scpi->scpi_mutex);
1369
1370 /* Get rid of trailing linefeed if present */
1371 if (response->len >= 1 && response->str[response->len - 1] == '\n')
1372 g_string_truncate(response, response->len - 1);
1373
1374 /* Get rid of trailing carriage return if present */
1375 if (response->len >= 1 && response->str[response->len - 1] == '\r')
1376 g_string_truncate(response, response->len - 1);
1377
1378 s = g_string_free(response, FALSE);
1379
1380 ret = SR_OK;
1381 if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_BOOLEAN)) {
1382 if ((ret = parse_strict_bool(s, &b)) == SR_OK)
1383 *gvar = g_variant_new_boolean(b);
1384 } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_DOUBLE)) {
1385 if ((ret = sr_atod_ascii(s, &d)) == SR_OK)
1386 *gvar = g_variant_new_double(d);
1387 } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_STRING)) {
1388 *gvar = g_variant_new_string(s);
1389 } else {
1390 sr_err("Unable to convert to desired GVariant type.");
1391 ret = SR_ERR_NA;
1392 }
1393
1394 g_free(s);
1395
1396 return ret;
1397}