]> sigrok.org Git - libsigrok.git/blame - hardware/common/scpi.c
Only scpi_serial.c functions need HAVE_LIBSERIALPORT, not scpi.c.
[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
26/* Message logging helpers with subsystem-specific prefix string. */
27#define LOG_PREFIX "scpi: "
28#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
29#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
30#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
31#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
32#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
33
34#define SCPI_READ_RETRIES 100
35#define SCPI_READ_RETRY_TIMEOUT 10000
36
aa1e3b40
DJ
37/**
38 * Parse a string representation of a boolean-like value into a gboolean.
39 * Similar to sr_parse_boolstring but rejects strings which do not represent
40 * a boolean-like value.
41 *
42 * @param str String to convert.
43 * @param ret Pointer to a gboolean where the result of the conversion will be
44 * stored.
45 *
46 * @return SR_OK on success, SR_ERR on failure.
47 */
d5976d8b 48static int parse_strict_bool(const char *str, gboolean *ret)
aa1e3b40
DJ
49{
50 if (!str)
51 return SR_ERR_ARG;
52
53 if (!g_strcmp0(str, "1") ||
54 !g_ascii_strncasecmp(str, "y", 1) ||
55 !g_ascii_strncasecmp(str, "t", 1) ||
56 !g_ascii_strncasecmp(str, "yes", 3) ||
57 !g_ascii_strncasecmp(str, "true", 4) ||
58 !g_ascii_strncasecmp(str, "on", 2)) {
aa1e3b40
DJ
59 *ret = TRUE;
60 return SR_OK;
aa1e3b40
DJ
61 } else if (!g_strcmp0(str, "0") ||
62 !g_ascii_strncasecmp(str, "n", 1) ||
63 !g_ascii_strncasecmp(str, "f", 1) ||
64 !g_ascii_strncasecmp(str, "no", 2) ||
65 !g_ascii_strncasecmp(str, "false", 5) ||
66 !g_ascii_strncasecmp(str, "off", 3)) {
aa1e3b40
DJ
67 *ret = FALSE;
68 return SR_OK;
69 }
70
71 return SR_ERR;
72}
73
23f43dff
ML
74/**
75 * Open SCPI device.
76 *
77 * @param scpi Previously initialized SCPI device structure.
78 *
79 * @return SR_OK on success, SR_ERR on failure.
80 */
81SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi)
82{
83 return scpi->open(scpi->priv);
84}
85
86/**
87 * Add an event source for an SCPI device.
88 *
89 * @param scpi Previously initialized SCPI device structure.
90 * @param events Events to check for.
91 * @param timeout Max time to wait before the callback is called, ignored if 0.
92 * @param cb Callback function to add. Must not be NULL.
93 * @param cb_data Data for the callback function. Can be NULL.
94 *
95 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
96 * SR_ERR_MALLOC upon memory allocation errors.
97 */
98SR_PRIV int sr_scpi_source_add(struct sr_scpi_dev_inst *scpi, int events,
99 int timeout, sr_receive_data_callback_t cb, void *cb_data)
100{
101 return scpi->source_add(scpi->priv, events, timeout, cb, cb_data);
102}
103
104/**
105 * Remove event source for an SCPI device.
106 *
107 * @param scpi Previously initialized SCPI device structure.
108 *
109 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
110 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
111 * internal errors.
112 */
113SR_PRIV int sr_scpi_source_remove(struct sr_scpi_dev_inst *scpi)
114{
115 return scpi->source_remove(scpi->priv);
116}
117
7b9d7320
DJ
118/**
119 * Send a SCPI command.
120 *
23f43dff 121 * @param scpi Previously initialized SCPI device structure.
7b9d7320
DJ
122 * @param command The SCPI command to send to the device.
123 *
124 * @return SR_OK on success, SR_ERR on failure.
125 */
23f43dff 126SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi,
7b9d7320
DJ
127 const char *command)
128{
23f43dff
ML
129 return scpi->send(scpi->priv, command);
130}
7b9d7320 131
23f43dff
ML
132/**
133 * Receive an SCPI reply and store the reply in scpi_response.
134 *
135 * @param scpi Previously initialised SCPI device structure.
136 * @param scpi_response Pointer where to store the SCPI response.
137 *
138 * @return SR_OK upon fetching a full SCPI response, SR_ERR upon fetching an
139 * incomplete or no response. The allocated response must be freed by
140 * the caller in the case of a full response as well in the case of
141 * an incomplete.
142 */
143SR_PRIV int sr_scpi_receive(struct sr_scpi_dev_inst *scpi,
144 char **scpi_response)
145{
146 return scpi->receive(scpi->priv, scpi_response);
147}
7b9d7320 148
a1ff9c18
ML
149/**
150 * Read part of a response from SCPI device.
151 *
152 * @param scpi Previously initialised SCPI device structure.
153 * @param buf Buffer to store result.
154 * @param maxlen Maximum number of bytes to read.
155 *
156 * @return Number of bytes read, or SR_ERR upon failure.
157 */
158SR_PRIV int sr_scpi_read(struct sr_scpi_dev_inst *scpi,
159 char *buf, int maxlen)
160{
161 return scpi->read(scpi->priv, buf, maxlen);
162}
163
23f43dff
ML
164/**
165 * Close SCPI device.
166 *
167 * @param scpi Previously initialized SCPI device structure.
168 *
169 * @return SR_OK on success, SR_ERR on failure.
170 */
171SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi)
172{
173 return scpi->close(scpi->priv);
174}
7b9d7320 175
23f43dff
ML
176/**
177 * Free SCPI device.
178 *
179 * @param scpi Previously initialized SCPI device structure.
180 *
181 * @return SR_OK on success, SR_ERR on failure.
182 */
183SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi)
184{
185 scpi->free(scpi->priv);
186 g_free(scpi);
7b9d7320
DJ
187}
188
189/**
190 * Send a SCPI command, receive the reply and store the reply in scpi_response.
191 *
23f43dff 192 * @param scpi Previously initialised SCPI device structure.
7b9d7320 193 * @param command The SCPI command to send to the device (can be NULL).
d5976d8b 194 * @param scpi_response Pointer where to store the SCPI response.
7b9d7320 195 *
d5976d8b
UH
196 * @return SR_OK upon fetching a full SCPI response, SR_ERR upon fetching an
197 * incomplete or no response. The allocated response must be freed by
198 * the caller in the case of a full response as well in the case of
199 * an incomplete.
7b9d7320 200 */
23f43dff 201SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi,
d5976d8b 202 const char *command, char **scpi_response)
7b9d7320 203{
7b9d7320 204 if (command)
23f43dff 205 if (sr_scpi_send(scpi, command) != SR_OK)
7b9d7320
DJ
206 return SR_ERR;
207
23f43dff 208 return sr_scpi_receive(scpi, scpi_response);
d730f70e
DJ
209}
210
211/**
212 * Send a SCPI command, read the reply, parse it as a bool value and store the
213 * result in scpi_response.
214 *
23f43dff 215 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
216 * @param command The SCPI command to send to the device (can be NULL).
217 * @param scpi_response Pointer where to store the parsed result.
218 *
219 * @return SR_OK on success, SR_ERR on failure.
220 */
23f43dff 221SR_PRIV int sr_scpi_get_bool(struct sr_scpi_dev_inst *scpi,
d730f70e
DJ
222 const char *command, gboolean *scpi_response)
223{
224 int ret;
225 char *response;
226
227 response = NULL;
228
23f43dff 229 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
d730f70e
DJ
230 if (!response)
231 return SR_ERR;
232
d5976d8b 233 if (parse_strict_bool(response, scpi_response) == SR_OK)
d730f70e
DJ
234 ret = SR_OK;
235 else
236 ret = SR_ERR;
237
238 g_free(response);
239
240 return ret;
241}
242
243/**
244 * Send a SCPI command, read the reply, parse it as an integer and store the
245 * result in scpi_response.
246 *
23f43dff 247 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
248 * @param command The SCPI command to send to the device (can be NULL).
249 * @param scpi_response Pointer where to store the parsed result.
250 *
251 * @return SR_OK on success, SR_ERR on failure.
252 */
23f43dff 253SR_PRIV int sr_scpi_get_int(struct sr_scpi_dev_inst *scpi,
d5976d8b 254 const char *command, int *scpi_response)
d730f70e
DJ
255{
256 int ret;
257 char *response;
258
259 response = NULL;
260
23f43dff 261 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
d730f70e
DJ
262 if (!response)
263 return SR_ERR;
264
265 if (sr_atoi(response, scpi_response) == SR_OK)
266 ret = SR_OK;
267 else
268 ret = SR_ERR;
269
270 g_free(response);
271
272 return ret;
273}
274
275/**
276 * Send a SCPI command, read the reply, parse it as a float and store the
277 * result in scpi_response.
278 *
23f43dff 279 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
280 * @param command The SCPI command to send to the device (can be NULL).
281 * @param scpi_response Pointer where to store the parsed result.
282 *
283 * @return SR_OK on success, SR_ERR on failure.
284 */
23f43dff 285SR_PRIV int sr_scpi_get_float(struct sr_scpi_dev_inst *scpi,
d730f70e
DJ
286 const char *command, float *scpi_response)
287{
288 int ret;
289 char *response;
290
291 response = NULL;
292
23f43dff 293 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
d730f70e
DJ
294 if (!response)
295 return SR_ERR;
296
297 if (sr_atof(response, scpi_response) == SR_OK)
298 ret = SR_OK;
299 else
300 ret = SR_ERR;
301
302 g_free(response);
303
304 return ret;
305}
306
307/**
308 * Send a SCPI command, read the reply, parse it as a double and store the
309 * result in scpi_response.
310 *
23f43dff 311 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
312 * @param command The SCPI command to send to the device (can be NULL).
313 * @param scpi_response Pointer where to store the parsed result.
314 *
315 * @return SR_OK on success, SR_ERR on failure.
316 */
23f43dff 317SR_PRIV int sr_scpi_get_double(struct sr_scpi_dev_inst *scpi,
d5976d8b 318 const char *command, double *scpi_response)
d730f70e
DJ
319{
320 int ret;
321 char *response;
322
323 response = NULL;
324
23f43dff 325 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
d730f70e
DJ
326 if (!response)
327 return SR_ERR;
328
329 if (sr_atod(response, scpi_response) == SR_OK)
330 ret = SR_OK;
331 else
332 ret = SR_ERR;
333
334 g_free(response);
335
7b9d7320
DJ
336 return ret;
337}
338
f5922ade
DJ
339/**
340 * Send a SCPI *OPC? command, read the reply and return the result of the
341 * command.
342 *
23f43dff 343 * @param scpi Previously initialised SCPI device structure.
f5922ade
DJ
344 *
345 * @return SR_OK on success, SR_ERR on failure.
346 */
23f43dff 347SR_PRIV int sr_scpi_get_opc(struct sr_scpi_dev_inst *scpi)
f5922ade
DJ
348{
349 unsigned int i;
350 gboolean opc;
351
352 for (i = 0; i < SCPI_READ_RETRIES; ++i) {
23f43dff 353 sr_scpi_get_bool(scpi, SCPI_CMD_OPC, &opc);
f5922ade
DJ
354 if (opc)
355 return SR_OK;
f5922ade
DJ
356 g_usleep(SCPI_READ_RETRY_TIMEOUT);
357 }
358
359 return SR_ERR;
360}
361
8acbb89a
DJ
362/**
363 * Send a SCPI command, read the reply, parse it as comma separated list of
364 * floats and store the as an result in scpi_response.
365 *
23f43dff 366 * @param scpi Previously initialised SCPI device structure.
8acbb89a
DJ
367 * @param command The SCPI command to send to the device (can be NULL).
368 * @param scpi_response Pointer where to store the parsed result.
369 *
370 * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing
d5976d8b
UH
371 * error or upon no response. The allocated response must be freed by
372 * the caller in the case of an SR_OK as well as in the case of
373 * parsing error.
8acbb89a 374 */
23f43dff 375SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi,
d5976d8b 376 const char *command, GArray **scpi_response)
8acbb89a
DJ
377{
378 int ret;
379 float tmp;
380 char *response;
d5976d8b 381 gchar **ptr, **tokens;
8acbb89a
DJ
382 GArray *response_array;
383
384 ret = SR_OK;
385 response = NULL;
386 tokens = NULL;
387
23f43dff 388 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
8acbb89a
DJ
389 if (!response)
390 return SR_ERR;
391
392 tokens = g_strsplit(response, ",", 0);
393 ptr = tokens;
394
395 response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256);
396
d5976d8b 397 while (*ptr) {
8acbb89a
DJ
398 if (sr_atof(*ptr, &tmp) == SR_OK)
399 response_array = g_array_append_val(response_array,
400 tmp);
401 else
402 ret = SR_ERR;
403
404 ptr++;
405 }
406 g_strfreev(tokens);
407 g_free(response);
408
409 if (ret == SR_ERR && response_array->len == 0) {
410 g_array_free(response_array, TRUE);
411 *scpi_response = NULL;
412 return SR_ERR;
413 }
414
415 *scpi_response = response_array;
416
1a323dd8
DJ
417 return ret;
418}
419
420/**
421 * Send a SCPI command, read the reply, parse it as comma separated list of
422 * unsigned 8 bit integers and store the as an result in scpi_response.
423 *
23f43dff 424 * @param scpi Previously initialised SCPI device structure.
1a323dd8
DJ
425 * @param command The SCPI command to send to the device (can be NULL).
426 * @param scpi_response Pointer where to store the parsed result.
427 *
428 * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing
d5976d8b
UH
429 * error or upon no response. The allocated response must be freed by
430 * the caller in the case of an SR_OK as well as in the case of
431 * parsing error.
1a323dd8 432 */
23f43dff 433SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi,
d5976d8b 434 const char *command, GArray **scpi_response)
1a323dd8 435{
d5976d8b 436 int tmp, ret;
1a323dd8 437 char *response;
d5976d8b 438 gchar **ptr, **tokens;
1a323dd8
DJ
439 GArray *response_array;
440
441 ret = SR_OK;
442 response = NULL;
443 tokens = NULL;
444
23f43dff 445 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
1a323dd8
DJ
446 if (!response)
447 return SR_ERR;
448
449 tokens = g_strsplit(response, ",", 0);
450 ptr = tokens;
451
452 response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256);
453
d5976d8b 454 while (*ptr) {
1a323dd8
DJ
455 if (sr_atoi(*ptr, &tmp) == SR_OK)
456 response_array = g_array_append_val(response_array,
457 tmp);
458 else
459 ret = SR_ERR;
460
461 ptr++;
462 }
463 g_strfreev(tokens);
464 g_free(response);
465
466 if (response_array->len == 0) {
467 g_array_free(response_array, TRUE);
468 *scpi_response = NULL;
469 return SR_ERR;
470 }
471
472 *scpi_response = response_array;
473
8acbb89a
DJ
474 return ret;
475}
476
7b9d7320
DJ
477/**
478 * Send the *IDN? SCPI command, receive the reply, parse it and store the
479 * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer.
480 *
d5976d8b
UH
481 * The hw_info structure must be freed by the caller via sr_scpi_hw_info_free().
482 *
23f43dff 483 * @param scpi Previously initialised SCPI device structure.
7b9d7320
DJ
484 * @param scpi_response Pointer where to store the hw_info structure.
485 *
486 * @return SR_OK upon success, SR_ERR on failure.
7b9d7320 487 */
23f43dff 488SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi,
7b9d7320
DJ
489 struct sr_scpi_hw_info **scpi_response)
490{
491 int num_tokens;
492 char *response;
493 gchar **tokens;
7b9d7320
DJ
494 struct sr_scpi_hw_info *hw_info;
495
496 response = NULL;
497 tokens = NULL;
498
23f43dff 499 if (sr_scpi_get_string(scpi, SCPI_CMD_IDN, &response) != SR_OK)
7b9d7320
DJ
500 if (!response)
501 return SR_ERR;
502
503 /*
504 * The response to a '*IDN?' is specified by the SCPI spec. It contains
505 * a comma-separated list containing the manufacturer name, instrument
506 * model, serial number of the instrument and the firmware version.
507 */
508 tokens = g_strsplit(response, ",", 0);
509
510 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
511
512 if (num_tokens != 4) {
d5976d8b 513 sr_dbg("IDN response not according to spec: %80.s.", response);
7b9d7320
DJ
514 g_strfreev(tokens);
515 g_free(response);
516 return SR_ERR;
517 }
518 g_free(response);
519
520 hw_info = g_try_malloc(sizeof(struct sr_scpi_hw_info));
521 if (!hw_info) {
522 g_strfreev(tokens);
523 return SR_ERR_MALLOC;
524 }
525
526 hw_info->manufacturer = g_strdup(tokens[0]);
527 hw_info->model = g_strdup(tokens[1]);
528 hw_info->serial_number = g_strdup(tokens[2]);
529 hw_info->firmware_version = g_strdup(tokens[3]);
530
531 g_strfreev(tokens);
532
533 *scpi_response = hw_info;
534
535 return SR_OK;
536}
537
538/**
539 * Free a sr_scpi_hw_info struct.
540 *
541 * @param hw_info Pointer to the struct to free.
542 *
543 * This function is safe to call with a NULL pointer.
544 */
545SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info)
546{
547 if (hw_info) {
548 g_free(hw_info->manufacturer);
549 g_free(hw_info->model);
550 g_free(hw_info->serial_number);
551 g_free(hw_info->firmware_version);
552 g_free(hw_info);
553 }
554}