]> sigrok.org Git - libsigrok.git/blame - hardware/common/scpi.c
Make SCPI functions device independent, with separate serial backend.
[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
23f43dff
ML
149/**
150 * Close SCPI device.
151 *
152 * @param scpi Previously initialized SCPI device structure.
153 *
154 * @return SR_OK on success, SR_ERR on failure.
155 */
156SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi)
157{
158 return scpi->close(scpi->priv);
159}
7b9d7320 160
23f43dff
ML
161/**
162 * Free SCPI device.
163 *
164 * @param scpi Previously initialized SCPI device structure.
165 *
166 * @return SR_OK on success, SR_ERR on failure.
167 */
168SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi)
169{
170 scpi->free(scpi->priv);
171 g_free(scpi);
7b9d7320
DJ
172}
173
174/**
175 * Send a SCPI command, receive the reply and store the reply in scpi_response.
176 *
23f43dff 177 * @param scpi Previously initialised SCPI device structure.
7b9d7320 178 * @param command The SCPI command to send to the device (can be NULL).
d5976d8b 179 * @param scpi_response Pointer where to store the SCPI response.
7b9d7320 180 *
d5976d8b
UH
181 * @return SR_OK upon fetching a full SCPI response, SR_ERR upon fetching an
182 * incomplete or no response. The allocated response must be freed by
183 * the caller in the case of a full response as well in the case of
184 * an incomplete.
7b9d7320 185 */
23f43dff 186SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi,
d5976d8b 187 const char *command, char **scpi_response)
7b9d7320 188{
7b9d7320 189 if (command)
23f43dff 190 if (sr_scpi_send(scpi, command) != SR_OK)
7b9d7320
DJ
191 return SR_ERR;
192
23f43dff 193 return sr_scpi_receive(scpi, scpi_response);
d730f70e
DJ
194}
195
196/**
197 * Send a SCPI command, read the reply, parse it as a bool value and store the
198 * result in scpi_response.
199 *
23f43dff 200 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
201 * @param command The SCPI command to send to the device (can be NULL).
202 * @param scpi_response Pointer where to store the parsed result.
203 *
204 * @return SR_OK on success, SR_ERR on failure.
205 */
23f43dff 206SR_PRIV int sr_scpi_get_bool(struct sr_scpi_dev_inst *scpi,
d730f70e
DJ
207 const char *command, gboolean *scpi_response)
208{
209 int ret;
210 char *response;
211
212 response = NULL;
213
23f43dff 214 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
d730f70e
DJ
215 if (!response)
216 return SR_ERR;
217
d5976d8b 218 if (parse_strict_bool(response, scpi_response) == SR_OK)
d730f70e
DJ
219 ret = SR_OK;
220 else
221 ret = SR_ERR;
222
223 g_free(response);
224
225 return ret;
226}
227
228/**
229 * Send a SCPI command, read the reply, parse it as an integer and store the
230 * result in scpi_response.
231 *
23f43dff 232 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
233 * @param command The SCPI command to send to the device (can be NULL).
234 * @param scpi_response Pointer where to store the parsed result.
235 *
236 * @return SR_OK on success, SR_ERR on failure.
237 */
23f43dff 238SR_PRIV int sr_scpi_get_int(struct sr_scpi_dev_inst *scpi,
d5976d8b 239 const char *command, int *scpi_response)
d730f70e
DJ
240{
241 int ret;
242 char *response;
243
244 response = NULL;
245
23f43dff 246 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
d730f70e
DJ
247 if (!response)
248 return SR_ERR;
249
250 if (sr_atoi(response, scpi_response) == SR_OK)
251 ret = SR_OK;
252 else
253 ret = SR_ERR;
254
255 g_free(response);
256
257 return ret;
258}
259
260/**
261 * Send a SCPI command, read the reply, parse it as a float and store the
262 * result in scpi_response.
263 *
23f43dff 264 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
265 * @param command The SCPI command to send to the device (can be NULL).
266 * @param scpi_response Pointer where to store the parsed result.
267 *
268 * @return SR_OK on success, SR_ERR on failure.
269 */
23f43dff 270SR_PRIV int sr_scpi_get_float(struct sr_scpi_dev_inst *scpi,
d730f70e
DJ
271 const char *command, float *scpi_response)
272{
273 int ret;
274 char *response;
275
276 response = NULL;
277
23f43dff 278 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
d730f70e
DJ
279 if (!response)
280 return SR_ERR;
281
282 if (sr_atof(response, scpi_response) == SR_OK)
283 ret = SR_OK;
284 else
285 ret = SR_ERR;
286
287 g_free(response);
288
289 return ret;
290}
291
292/**
293 * Send a SCPI command, read the reply, parse it as a double and store the
294 * result in scpi_response.
295 *
23f43dff 296 * @param scpi Previously initialised SCPI device structure.
d730f70e
DJ
297 * @param command The SCPI command to send to the device (can be NULL).
298 * @param scpi_response Pointer where to store the parsed result.
299 *
300 * @return SR_OK on success, SR_ERR on failure.
301 */
23f43dff 302SR_PRIV int sr_scpi_get_double(struct sr_scpi_dev_inst *scpi,
d5976d8b 303 const char *command, double *scpi_response)
d730f70e
DJ
304{
305 int ret;
306 char *response;
307
308 response = NULL;
309
23f43dff 310 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
d730f70e
DJ
311 if (!response)
312 return SR_ERR;
313
314 if (sr_atod(response, scpi_response) == SR_OK)
315 ret = SR_OK;
316 else
317 ret = SR_ERR;
318
319 g_free(response);
320
7b9d7320
DJ
321 return ret;
322}
323
f5922ade
DJ
324/**
325 * Send a SCPI *OPC? command, read the reply and return the result of the
326 * command.
327 *
23f43dff 328 * @param scpi Previously initialised SCPI device structure.
f5922ade
DJ
329 *
330 * @return SR_OK on success, SR_ERR on failure.
331 */
23f43dff 332SR_PRIV int sr_scpi_get_opc(struct sr_scpi_dev_inst *scpi)
f5922ade
DJ
333{
334 unsigned int i;
335 gboolean opc;
336
337 for (i = 0; i < SCPI_READ_RETRIES; ++i) {
23f43dff 338 sr_scpi_get_bool(scpi, SCPI_CMD_OPC, &opc);
f5922ade
DJ
339 if (opc)
340 return SR_OK;
f5922ade
DJ
341 g_usleep(SCPI_READ_RETRY_TIMEOUT);
342 }
343
344 return SR_ERR;
345}
346
8acbb89a
DJ
347/**
348 * Send a SCPI command, read the reply, parse it as comma separated list of
349 * floats and store the as an result in scpi_response.
350 *
23f43dff 351 * @param scpi Previously initialised SCPI device structure.
8acbb89a
DJ
352 * @param command The SCPI command to send to the device (can be NULL).
353 * @param scpi_response Pointer where to store the parsed result.
354 *
355 * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing
d5976d8b
UH
356 * error or upon no response. The allocated response must be freed by
357 * the caller in the case of an SR_OK as well as in the case of
358 * parsing error.
8acbb89a 359 */
23f43dff 360SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi,
d5976d8b 361 const char *command, GArray **scpi_response)
8acbb89a
DJ
362{
363 int ret;
364 float tmp;
365 char *response;
d5976d8b 366 gchar **ptr, **tokens;
8acbb89a
DJ
367 GArray *response_array;
368
369 ret = SR_OK;
370 response = NULL;
371 tokens = NULL;
372
23f43dff 373 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
8acbb89a
DJ
374 if (!response)
375 return SR_ERR;
376
377 tokens = g_strsplit(response, ",", 0);
378 ptr = tokens;
379
380 response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256);
381
d5976d8b 382 while (*ptr) {
8acbb89a
DJ
383 if (sr_atof(*ptr, &tmp) == SR_OK)
384 response_array = g_array_append_val(response_array,
385 tmp);
386 else
387 ret = SR_ERR;
388
389 ptr++;
390 }
391 g_strfreev(tokens);
392 g_free(response);
393
394 if (ret == SR_ERR && response_array->len == 0) {
395 g_array_free(response_array, TRUE);
396 *scpi_response = NULL;
397 return SR_ERR;
398 }
399
400 *scpi_response = response_array;
401
1a323dd8
DJ
402 return ret;
403}
404
405/**
406 * Send a SCPI command, read the reply, parse it as comma separated list of
407 * unsigned 8 bit integers and store the as an result in scpi_response.
408 *
23f43dff 409 * @param scpi Previously initialised SCPI device structure.
1a323dd8
DJ
410 * @param command The SCPI command to send to the device (can be NULL).
411 * @param scpi_response Pointer where to store the parsed result.
412 *
413 * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing
d5976d8b
UH
414 * error or upon no response. The allocated response must be freed by
415 * the caller in the case of an SR_OK as well as in the case of
416 * parsing error.
1a323dd8 417 */
23f43dff 418SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi,
d5976d8b 419 const char *command, GArray **scpi_response)
1a323dd8 420{
d5976d8b 421 int tmp, ret;
1a323dd8 422 char *response;
d5976d8b 423 gchar **ptr, **tokens;
1a323dd8
DJ
424 GArray *response_array;
425
426 ret = SR_OK;
427 response = NULL;
428 tokens = NULL;
429
23f43dff 430 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
1a323dd8
DJ
431 if (!response)
432 return SR_ERR;
433
434 tokens = g_strsplit(response, ",", 0);
435 ptr = tokens;
436
437 response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256);
438
d5976d8b 439 while (*ptr) {
1a323dd8
DJ
440 if (sr_atoi(*ptr, &tmp) == SR_OK)
441 response_array = g_array_append_val(response_array,
442 tmp);
443 else
444 ret = SR_ERR;
445
446 ptr++;
447 }
448 g_strfreev(tokens);
449 g_free(response);
450
451 if (response_array->len == 0) {
452 g_array_free(response_array, TRUE);
453 *scpi_response = NULL;
454 return SR_ERR;
455 }
456
457 *scpi_response = response_array;
458
8acbb89a
DJ
459 return ret;
460}
461
7b9d7320
DJ
462/**
463 * Send the *IDN? SCPI command, receive the reply, parse it and store the
464 * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer.
465 *
d5976d8b
UH
466 * The hw_info structure must be freed by the caller via sr_scpi_hw_info_free().
467 *
23f43dff 468 * @param scpi Previously initialised SCPI device structure.
7b9d7320
DJ
469 * @param scpi_response Pointer where to store the hw_info structure.
470 *
471 * @return SR_OK upon success, SR_ERR on failure.
7b9d7320 472 */
23f43dff 473SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi,
7b9d7320
DJ
474 struct sr_scpi_hw_info **scpi_response)
475{
476 int num_tokens;
477 char *response;
478 gchar **tokens;
7b9d7320
DJ
479 struct sr_scpi_hw_info *hw_info;
480
481 response = NULL;
482 tokens = NULL;
483
23f43dff 484 if (sr_scpi_get_string(scpi, SCPI_CMD_IDN, &response) != SR_OK)
7b9d7320
DJ
485 if (!response)
486 return SR_ERR;
487
488 /*
489 * The response to a '*IDN?' is specified by the SCPI spec. It contains
490 * a comma-separated list containing the manufacturer name, instrument
491 * model, serial number of the instrument and the firmware version.
492 */
493 tokens = g_strsplit(response, ",", 0);
494
495 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
496
497 if (num_tokens != 4) {
d5976d8b 498 sr_dbg("IDN response not according to spec: %80.s.", response);
7b9d7320
DJ
499 g_strfreev(tokens);
500 g_free(response);
501 return SR_ERR;
502 }
503 g_free(response);
504
505 hw_info = g_try_malloc(sizeof(struct sr_scpi_hw_info));
506 if (!hw_info) {
507 g_strfreev(tokens);
508 return SR_ERR_MALLOC;
509 }
510
511 hw_info->manufacturer = g_strdup(tokens[0]);
512 hw_info->model = g_strdup(tokens[1]);
513 hw_info->serial_number = g_strdup(tokens[2]);
514 hw_info->firmware_version = g_strdup(tokens[3]);
515
516 g_strfreev(tokens);
517
518 *scpi_response = hw_info;
519
520 return SR_OK;
521}
522
523/**
524 * Free a sr_scpi_hw_info struct.
525 *
526 * @param hw_info Pointer to the struct to free.
527 *
528 * This function is safe to call with a NULL pointer.
529 */
530SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info)
531{
532 if (hw_info) {
533 g_free(hw_info->manufacturer);
534 g_free(hw_info->model);
535 g_free(hw_info->serial_number);
536 g_free(hw_info->firmware_version);
537 g_free(hw_info);
538 }
539}