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