]> sigrok.org Git - libsigrok.git/blame - hardware/common/scpi.c
Don't define names ending with _t (POSIX reserved).
[libsigrok.git] / hardware / common / 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>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "libsigrok.h"
21#include "libsigrok-internal.h"
22
23#include <glib.h>
24#include <string.h>
25
3544f848 26#define LOG_PREFIX "scpi"
7b9d7320
DJ
27
28#define SCPI_READ_RETRIES 100
29#define SCPI_READ_RETRY_TIMEOUT 10000
30
aa1e3b40
DJ
31/**
32 * Parse a string representation of a boolean-like value into a gboolean.
33 * Similar to sr_parse_boolstring but rejects strings which do not represent
34 * a boolean-like value.
35 *
36 * @param str String to convert.
37 * @param ret Pointer to a gboolean where the result of the conversion will be
38 * stored.
39 *
40 * @return SR_OK on success, SR_ERR on failure.
41 */
d5976d8b 42static int parse_strict_bool(const char *str, gboolean *ret)
aa1e3b40
DJ
43{
44 if (!str)
45 return SR_ERR_ARG;
46
47 if (!g_strcmp0(str, "1") ||
48 !g_ascii_strncasecmp(str, "y", 1) ||
49 !g_ascii_strncasecmp(str, "t", 1) ||
50 !g_ascii_strncasecmp(str, "yes", 3) ||
51 !g_ascii_strncasecmp(str, "true", 4) ||
52 !g_ascii_strncasecmp(str, "on", 2)) {
aa1e3b40
DJ
53 *ret = TRUE;
54 return SR_OK;
aa1e3b40
DJ
55 } else if (!g_strcmp0(str, "0") ||
56 !g_ascii_strncasecmp(str, "n", 1) ||
57 !g_ascii_strncasecmp(str, "f", 1) ||
58 !g_ascii_strncasecmp(str, "no", 2) ||
59 !g_ascii_strncasecmp(str, "false", 5) ||
60 !g_ascii_strncasecmp(str, "off", 3)) {
aa1e3b40
DJ
61 *ret = FALSE;
62 return SR_OK;
63 }
64
65 return SR_ERR;
66}
67
f754c146 68SR_PRIV extern const struct sr_scpi_dev_inst scpi_serial_dev;
104ed125
AJ
69SR_PRIV extern const struct sr_scpi_dev_inst scpi_tcp_raw_dev;
70SR_PRIV extern const struct sr_scpi_dev_inst scpi_tcp_rigol_dev;
ed840c86 71/* SR_PRIV extern const struct sr_scpi_dev_inst scpi_usbtmc_dev; */
20ed3cee 72SR_PRIV extern const struct sr_scpi_dev_inst scpi_usbtmc_libusb_dev;
f754c146 73SR_PRIV extern const struct sr_scpi_dev_inst scpi_vxi_dev;
1fb2312f 74SR_PRIV extern const struct sr_scpi_dev_inst scpi_visa_dev;
f754c146
AJ
75
76static const struct sr_scpi_dev_inst *scpi_devs[] = {
104ed125
AJ
77 &scpi_tcp_raw_dev,
78 &scpi_tcp_rigol_dev,
ed840c86 79 /* &scpi_usbtmc_dev, */
20ed3cee
AJ
80#ifdef HAVE_LIBUSB_1_0
81 &scpi_usbtmc_libusb_dev,
82#endif
613c1108 83#if HAVE_RPC
f754c146
AJ
84 &scpi_vxi_dev,
85#endif
1fb2312f
ML
86#ifdef HAVE_LIBREVISA
87 &scpi_visa_dev,
88#endif
f754c146
AJ
89#ifdef HAVE_LIBSERIALPORT
90 &scpi_serial_dev, /* must be last as it matches any resource */
91#endif
92};
93
b541f837
AJ
94static GSList *sr_scpi_scan_resource(struct drv_context *drvc,
95 const char *resource, const char *serialcomm,
96 struct sr_dev_inst *(*probe_device)(struct sr_scpi_dev_inst *scpi))
97{
98 struct sr_scpi_dev_inst *scpi;
99 struct sr_dev_inst *sdi;
100
101 if (!(scpi = scpi_dev_inst_new(drvc, resource, serialcomm)))
102 return NULL;
103
104 if (sr_scpi_open(scpi) != SR_OK) {
105 sr_info("Couldn't open SCPI device.");
106 sr_scpi_free(scpi);
107 return NULL;
108 };
109
110 if ((sdi = probe_device(scpi)))
111 return g_slist_append(NULL, sdi);
112
113 sr_scpi_close(scpi);
114 sr_scpi_free(scpi);
115 return NULL;
116}
117
118SR_PRIV GSList *sr_scpi_scan(struct drv_context *drvc, GSList *options,
119 struct sr_dev_inst *(*probe_device)(struct sr_scpi_dev_inst *scpi))
120{
121 GSList *resources, *l, *d, *devices = NULL;
122 const char *resource = NULL;
123 const char *serialcomm = NULL;
124 gchar **res;
125 unsigned i;
126
127 for (l = options; l; l = l->next) {
128 struct sr_config *src = l->data;
129 switch (src->key) {
130 case SR_CONF_CONN:
131 resource = g_variant_get_string(src->data, NULL);
132 break;
133 case SR_CONF_SERIALCOMM:
134 serialcomm = g_variant_get_string(src->data, NULL);
135 break;
136 }
137 }
138
139 for (i = 0; i < ARRAY_SIZE(scpi_devs); i++) {
140 if ((resource && strcmp(resource, scpi_devs[i]->prefix))
141 || !scpi_devs[i]->scan)
142 continue;
143 resources = scpi_devs[i]->scan(drvc);
144 for (l = resources; l; l = l->next) {
145 res = g_strsplit(l->data, ":", 2);
146 if (res[0] && (d = sr_scpi_scan_resource(drvc, res[0],
147 serialcomm ? serialcomm : res[1], probe_device)))
148 devices = g_slist_concat(devices, d);
149 g_strfreev(res);
150 }
151 g_slist_free_full(resources, g_free);
152 }
153
154 if (!devices && resource)
155 devices = sr_scpi_scan_resource(drvc, resource, serialcomm,
156 probe_device);
157
158 /* Tack a copy of the newly found devices onto the driver list. */
159 if (devices)
160 drvc->instances = g_slist_concat(drvc->instances,
161 g_slist_copy(devices));
162
163 return devices;
164}
165
17bdda58
AJ
166SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(struct drv_context *drvc,
167 const char *resource, const char *serialcomm)
c3515cea
AJ
168{
169 struct sr_scpi_dev_inst *scpi = NULL;
f754c146
AJ
170 const struct sr_scpi_dev_inst *scpi_dev;
171 gchar **params;
172 unsigned i;
c3515cea 173
f754c146
AJ
174 for (i = 0; i < ARRAY_SIZE(scpi_devs); i++) {
175 scpi_dev = scpi_devs[i];
176 if (!strncmp(resource, scpi_dev->prefix, strlen(scpi_dev->prefix))) {
177 sr_dbg("Opening %s device %s.", scpi_dev->name, resource);
178 scpi = g_malloc(sizeof(*scpi));
179 *scpi = *scpi_dev;
180 scpi->priv = g_malloc0(scpi->priv_size);
181 params = g_strsplit(resource, "/", 0);
17bdda58 182 if (scpi->dev_inst_new(scpi->priv, drvc, resource,
f754c146
AJ
183 params, serialcomm) != SR_OK) {
184 sr_scpi_free(scpi);
185 scpi = NULL;
186 }
187 g_strfreev(params);
188 break;
189 }
c3515cea 190 }
f754c146 191
c3515cea
AJ
192 return scpi;
193}
194
23f43dff
ML
195/**
196 * Open SCPI device.
197 *
198 * @param scpi Previously initialized SCPI device structure.
199 *
200 * @return SR_OK on success, SR_ERR on failure.
201 */
202SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi)
203{
204 return scpi->open(scpi->priv);
205}
206
207/**
208 * Add an event source for an SCPI device.
209 *
210 * @param scpi Previously initialized SCPI device structure.
211 * @param events Events to check for.
212 * @param timeout Max time to wait before the callback is called, ignored if 0.
213 * @param cb Callback function to add. Must not be NULL.
214 * @param cb_data Data for the callback function. Can be NULL.
215 *
216 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
217 * SR_ERR_MALLOC upon memory allocation errors.
218 */
219SR_PRIV int sr_scpi_source_add(struct sr_scpi_dev_inst *scpi, int events,
144f6660 220 int timeout, sr_receive_data_callback cb, void *cb_data)
23f43dff
ML
221{
222 return scpi->source_add(scpi->priv, events, timeout, cb, cb_data);
223}
224
225/**
226 * Remove event source for an SCPI device.
227 *
228 * @param scpi Previously initialized SCPI device structure.
229 *
230 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
231 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
232 * internal errors.
233 */
234SR_PRIV int sr_scpi_source_remove(struct sr_scpi_dev_inst *scpi)
235{
236 return scpi->source_remove(scpi->priv);
237}
238
7b9d7320
DJ
239/**
240 * Send a SCPI command.
241 *
23f43dff 242 * @param scpi Previously initialized SCPI device structure.
504f40a5 243 * @param format Format string, to be followed by any necessary arguments.
7b9d7320
DJ
244 *
245 * @return SR_OK on success, SR_ERR on failure.
246 */
23f43dff 247SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi,
504f40a5 248 const char *format, ...)
7b9d7320 249{
87c41083
ML
250 va_list args;
251 int ret;
252
253 va_start(args, format);
254 ret = sr_scpi_send_variadic(scpi, format, args);
255 va_end(args);
256
257 return ret;
258}
259
260/**
261 * Send a SCPI command with a variadic argument list.
262 *
263 * @param scpi Previously initialized SCPI device structure.
264 * @param format Format string.
265 * @param args Argument list.
266 *
267 * @return SR_OK on success, SR_ERR on failure.
268 */
269SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi,
270 const char *format, va_list args)
271{
272 va_list args_copy;
504f40a5
ML
273 char *buf;
274 int len, ret;
275
504f40a5 276 /* Get length of buffer required. */
87c41083
ML
277 va_copy(args_copy, args);
278 len = vsnprintf(NULL, 0, format, args_copy);
279 va_end(args_copy);
504f40a5
ML
280
281 /* Allocate buffer and write out command. */
282 buf = g_malloc(len + 1);
87c41083 283 vsprintf(buf, format, args);
504f40a5
ML
284
285 /* Send command. */
286 ret = scpi->send(scpi->priv, buf);
287
288 /* Free command buffer. */
289 g_free(buf);
290
291 return ret;
23f43dff 292}
7b9d7320 293
23f43dff 294/**
05c644ea 295 * Begin receiving an SCPI reply.
23f43dff
ML
296 *
297 * @param scpi Previously initialised SCPI device structure.
23f43dff 298 *
05c644ea 299 * @return SR_OK on success, SR_ERR on failure.
23f43dff 300 */
05c644ea 301SR_PRIV int sr_scpi_read_begin(struct sr_scpi_dev_inst *scpi)
23f43dff 302{
05c644ea 303 return scpi->read_begin(scpi->priv);
23f43dff 304}
7b9d7320 305
a1ff9c18
ML
306/**
307 * Read part of a response from SCPI device.
308 *
309 * @param scpi Previously initialised SCPI device structure.
310 * @param buf Buffer to store result.
311 * @param maxlen Maximum number of bytes to read.
312 *
313 * @return Number of bytes read, or SR_ERR upon failure.
314 */
05c644ea 315SR_PRIV int sr_scpi_read_data(struct sr_scpi_dev_inst *scpi,
a1ff9c18
ML
316 char *buf, int maxlen)
317{
05c644ea
ML
318 return scpi->read_data(scpi->priv, buf, maxlen);
319}
320
321/**
322 * Check whether a complete SCPI response has been received.
323 *
324 * @param scpi Previously initialised SCPI device structure.
325 *
326 * @return 1 if complete, 0 otherwise.
327 */
328SR_PRIV int sr_scpi_read_complete(struct sr_scpi_dev_inst *scpi)
329{
330 return scpi->read_complete(scpi->priv);
a1ff9c18
ML
331}
332
23f43dff
ML
333/**
334 * Close SCPI device.
335 *
336 * @param scpi Previously initialized SCPI device structure.
337 *
338 * @return SR_OK on success, SR_ERR on failure.
339 */
340SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi)
341{
342 return scpi->close(scpi->priv);
343}
7b9d7320 344
23f43dff
ML
345/**
346 * Free SCPI device.
347 *
348 * @param scpi Previously initialized SCPI device structure.
349 *
350 * @return SR_OK on success, SR_ERR on failure.
351 */
352SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi)
353{
354 scpi->free(scpi->priv);
f754c146 355 g_free(scpi->priv);
23f43dff 356 g_free(scpi);
7b9d7320
DJ
357}
358
359/**
360 * Send a SCPI command, receive the reply and store the reply in scpi_response.
361 *
23f43dff 362 * @param scpi Previously initialised SCPI device structure.
7b9d7320 363 * @param command The SCPI command to send to the device (can be NULL).
d5976d8b 364 * @param scpi_response Pointer where to store the SCPI response.
7b9d7320 365 *
05c644ea 366 * @return SR_OK on success, SR_ERR on failure.
7b9d7320 367 */
23f43dff 368SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi,
d5976d8b 369 const char *command, char **scpi_response)
7b9d7320 370{
05c644ea
ML
371 char buf[256];
372 int len;
373 GString *response;
374
7b9d7320 375 if (command)
23f43dff 376 if (sr_scpi_send(scpi, command) != SR_OK)
7b9d7320
DJ
377 return SR_ERR;
378
05c644ea
ML
379 if (sr_scpi_read_begin(scpi) != SR_OK)
380 return SR_ERR;
381
382 response = g_string_new("");
383
384 *scpi_response = NULL;
385
386 while (!sr_scpi_read_complete(scpi)) {
387 len = sr_scpi_read_data(scpi, buf, sizeof(buf));
388 if (len < 0) {
389 g_string_free(response, TRUE);
390 return SR_ERR;
391 }
392 g_string_append_len(response, buf, len);
393 }
394
d03dfac6
ML
395 /* Get rid of trailing linefeed if present */
396 if (response->len >= 1 && response->str[response->len - 1] == '\n')
397 g_string_truncate(response, response->len - 1);
398
05c644ea
ML
399 *scpi_response = response->str;
400 g_string_free(response, FALSE);
401
1c873c11 402 sr_spew("Got response: '%.70s'.", *scpi_response);
cf9f4bc5 403
05c644ea 404 return SR_OK;
d730f70e
DJ
405}
406
407/**
408 * Send a SCPI command, read the reply, parse it as a bool value and store the
409 * result in scpi_response.
410 *
23f43dff 411 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
412 * @param command The SCPI command to send to the device (can be NULL).
413 * @param scpi_response Pointer where to store the parsed result.
414 *
415 * @return SR_OK on success, SR_ERR on failure.
416 */
23f43dff 417SR_PRIV int sr_scpi_get_bool(struct sr_scpi_dev_inst *scpi,
d730f70e
DJ
418 const char *command, gboolean *scpi_response)
419{
420 int ret;
421 char *response;
422
423 response = NULL;
424
23f43dff 425 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
d730f70e
DJ
426 if (!response)
427 return SR_ERR;
428
d5976d8b 429 if (parse_strict_bool(response, scpi_response) == SR_OK)
d730f70e
DJ
430 ret = SR_OK;
431 else
432 ret = SR_ERR;
433
434 g_free(response);
435
436 return ret;
437}
438
439/**
440 * Send a SCPI command, read the reply, parse it as an integer and store the
441 * result in scpi_response.
442 *
23f43dff 443 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
444 * @param command The SCPI command to send to the device (can be NULL).
445 * @param scpi_response Pointer where to store the parsed result.
446 *
447 * @return SR_OK on success, SR_ERR on failure.
448 */
23f43dff 449SR_PRIV int sr_scpi_get_int(struct sr_scpi_dev_inst *scpi,
d5976d8b 450 const char *command, int *scpi_response)
d730f70e
DJ
451{
452 int ret;
453 char *response;
454
455 response = NULL;
456
23f43dff 457 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
d730f70e
DJ
458 if (!response)
459 return SR_ERR;
460
461 if (sr_atoi(response, scpi_response) == SR_OK)
462 ret = SR_OK;
463 else
464 ret = SR_ERR;
465
466 g_free(response);
467
468 return ret;
469}
470
471/**
472 * Send a SCPI command, read the reply, parse it as a float and store the
473 * result in scpi_response.
474 *
23f43dff 475 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
476 * @param command The SCPI command to send to the device (can be NULL).
477 * @param scpi_response Pointer where to store the parsed result.
478 *
479 * @return SR_OK on success, SR_ERR on failure.
480 */
23f43dff 481SR_PRIV int sr_scpi_get_float(struct sr_scpi_dev_inst *scpi,
d730f70e
DJ
482 const char *command, float *scpi_response)
483{
484 int ret;
485 char *response;
486
487 response = NULL;
488
23f43dff 489 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
d730f70e
DJ
490 if (!response)
491 return SR_ERR;
492
13dbd151 493 if (sr_atof_ascii(response, scpi_response) == SR_OK)
d730f70e
DJ
494 ret = SR_OK;
495 else
496 ret = SR_ERR;
497
498 g_free(response);
499
500 return ret;
501}
502
503/**
504 * Send a SCPI command, read the reply, parse it as a double and store the
505 * result in scpi_response.
506 *
23f43dff 507 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
508 * @param command The SCPI command to send to the device (can be NULL).
509 * @param scpi_response Pointer where to store the parsed result.
510 *
511 * @return SR_OK on success, SR_ERR on failure.
512 */
23f43dff 513SR_PRIV int sr_scpi_get_double(struct sr_scpi_dev_inst *scpi,
d5976d8b 514 const char *command, double *scpi_response)
d730f70e
DJ
515{
516 int ret;
517 char *response;
518
519 response = NULL;
520
23f43dff 521 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
d730f70e
DJ
522 if (!response)
523 return SR_ERR;
524
525 if (sr_atod(response, scpi_response) == SR_OK)
526 ret = SR_OK;
527 else
528 ret = SR_ERR;
529
530 g_free(response);
531
7b9d7320
DJ
532 return ret;
533}
534
f5922ade
DJ
535/**
536 * Send a SCPI *OPC? command, read the reply and return the result of the
537 * command.
538 *
23f43dff 539 * @param scpi Previously initialised SCPI device structure.
f5922ade
DJ
540 *
541 * @return SR_OK on success, SR_ERR on failure.
542 */
23f43dff 543SR_PRIV int sr_scpi_get_opc(struct sr_scpi_dev_inst *scpi)
f5922ade
DJ
544{
545 unsigned int i;
546 gboolean opc;
547
548 for (i = 0; i < SCPI_READ_RETRIES; ++i) {
23f43dff 549 sr_scpi_get_bool(scpi, SCPI_CMD_OPC, &opc);
f5922ade
DJ
550 if (opc)
551 return SR_OK;
f5922ade
DJ
552 g_usleep(SCPI_READ_RETRY_TIMEOUT);
553 }
554
555 return SR_ERR;
556}
557
8acbb89a
DJ
558/**
559 * Send a SCPI command, read the reply, parse it as comma separated list of
560 * floats and store the as an result in scpi_response.
561 *
23f43dff 562 * @param scpi Previously initialised SCPI device structure.
8acbb89a
DJ
563 * @param command The SCPI command to send to the device (can be NULL).
564 * @param scpi_response Pointer where to store the parsed result.
565 *
566 * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing
d5976d8b
UH
567 * error or upon no response. The allocated response must be freed by
568 * the caller in the case of an SR_OK as well as in the case of
569 * parsing error.
8acbb89a 570 */
23f43dff 571SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi,
d5976d8b 572 const char *command, GArray **scpi_response)
8acbb89a
DJ
573{
574 int ret;
575 float tmp;
576 char *response;
d5976d8b 577 gchar **ptr, **tokens;
8acbb89a
DJ
578 GArray *response_array;
579
580 ret = SR_OK;
581 response = NULL;
582 tokens = NULL;
583
23f43dff 584 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
8acbb89a
DJ
585 if (!response)
586 return SR_ERR;
587
588 tokens = g_strsplit(response, ",", 0);
589 ptr = tokens;
590
591 response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256);
592
d5976d8b 593 while (*ptr) {
13dbd151 594 if (sr_atof_ascii(*ptr, &tmp) == SR_OK)
8acbb89a
DJ
595 response_array = g_array_append_val(response_array,
596 tmp);
597 else
598 ret = SR_ERR;
599
600 ptr++;
601 }
602 g_strfreev(tokens);
603 g_free(response);
604
605 if (ret == SR_ERR && response_array->len == 0) {
606 g_array_free(response_array, TRUE);
607 *scpi_response = NULL;
608 return SR_ERR;
609 }
610
611 *scpi_response = response_array;
612
1a323dd8
DJ
613 return ret;
614}
615
616/**
617 * Send a SCPI command, read the reply, parse it as comma separated list of
618 * unsigned 8 bit integers and store the as an result in scpi_response.
619 *
23f43dff 620 * @param scpi Previously initialised SCPI device structure.
1a323dd8
DJ
621 * @param command The SCPI command to send to the device (can be NULL).
622 * @param scpi_response Pointer where to store the parsed result.
623 *
624 * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing
d5976d8b
UH
625 * error or upon no response. The allocated response must be freed by
626 * the caller in the case of an SR_OK as well as in the case of
627 * parsing error.
1a323dd8 628 */
23f43dff 629SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi,
d5976d8b 630 const char *command, GArray **scpi_response)
1a323dd8 631{
d5976d8b 632 int tmp, ret;
1a323dd8 633 char *response;
d5976d8b 634 gchar **ptr, **tokens;
1a323dd8
DJ
635 GArray *response_array;
636
637 ret = SR_OK;
638 response = NULL;
639 tokens = NULL;
640
23f43dff 641 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
1a323dd8
DJ
642 if (!response)
643 return SR_ERR;
644
645 tokens = g_strsplit(response, ",", 0);
646 ptr = tokens;
647
648 response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256);
649
d5976d8b 650 while (*ptr) {
1a323dd8
DJ
651 if (sr_atoi(*ptr, &tmp) == SR_OK)
652 response_array = g_array_append_val(response_array,
653 tmp);
654 else
655 ret = SR_ERR;
656
657 ptr++;
658 }
659 g_strfreev(tokens);
660 g_free(response);
661
662 if (response_array->len == 0) {
663 g_array_free(response_array, TRUE);
664 *scpi_response = NULL;
665 return SR_ERR;
666 }
667
668 *scpi_response = response_array;
669
8acbb89a
DJ
670 return ret;
671}
672
7b9d7320
DJ
673/**
674 * Send the *IDN? SCPI command, receive the reply, parse it and store the
675 * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer.
676 *
d5976d8b
UH
677 * The hw_info structure must be freed by the caller via sr_scpi_hw_info_free().
678 *
23f43dff 679 * @param scpi Previously initialised SCPI device structure.
7b9d7320
DJ
680 * @param scpi_response Pointer where to store the hw_info structure.
681 *
682 * @return SR_OK upon success, SR_ERR on failure.
7b9d7320 683 */
23f43dff 684SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi,
7b9d7320
DJ
685 struct sr_scpi_hw_info **scpi_response)
686{
687 int num_tokens;
688 char *response;
689 gchar **tokens;
7b9d7320
DJ
690 struct sr_scpi_hw_info *hw_info;
691
692 response = NULL;
693 tokens = NULL;
694
23f43dff 695 if (sr_scpi_get_string(scpi, SCPI_CMD_IDN, &response) != SR_OK)
7b9d7320
DJ
696 if (!response)
697 return SR_ERR;
698
77c16c04
ML
699 sr_info("Got IDN string: '%s'", response);
700
7b9d7320
DJ
701 /*
702 * The response to a '*IDN?' is specified by the SCPI spec. It contains
703 * a comma-separated list containing the manufacturer name, instrument
704 * model, serial number of the instrument and the firmware version.
705 */
706 tokens = g_strsplit(response, ",", 0);
707
708 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
709
710 if (num_tokens != 4) {
d5976d8b 711 sr_dbg("IDN response not according to spec: %80.s.", response);
7b9d7320
DJ
712 g_strfreev(tokens);
713 g_free(response);
714 return SR_ERR;
715 }
716 g_free(response);
717
718 hw_info = g_try_malloc(sizeof(struct sr_scpi_hw_info));
719 if (!hw_info) {
720 g_strfreev(tokens);
721 return SR_ERR_MALLOC;
722 }
723
724 hw_info->manufacturer = g_strdup(tokens[0]);
725 hw_info->model = g_strdup(tokens[1]);
726 hw_info->serial_number = g_strdup(tokens[2]);
727 hw_info->firmware_version = g_strdup(tokens[3]);
728
729 g_strfreev(tokens);
730
731 *scpi_response = hw_info;
732
733 return SR_OK;
734}
735
736/**
737 * Free a sr_scpi_hw_info struct.
738 *
739 * @param hw_info Pointer to the struct to free.
740 *
741 * This function is safe to call with a NULL pointer.
742 */
743SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info)
744{
745 if (hw_info) {
746 g_free(hw_info->manufacturer);
747 g_free(hw_info->model);
748 g_free(hw_info->serial_number);
749 g_free(hw_info->firmware_version);
750 g_free(hw_info);
751 }
752}