2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
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.
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.
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/>.
20 #include "libsigrok.h"
21 #include "libsigrok-internal.h"
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)
34 #define SCPI_READ_RETRIES 100
35 #define SCPI_READ_RETRY_TIMEOUT 10000
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.
42 * @param str String to convert.
43 * @param ret Pointer to a gboolean where the result of the conversion will be
46 * @return SR_OK on success, SR_ERR on failure.
48 static int parse_strict_bool(const char *str, gboolean *ret)
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)) {
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)) {
77 * @param scpi Previously initialized SCPI device structure.
79 * @return SR_OK on success, SR_ERR on failure.
81 SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi)
83 return scpi->open(scpi->priv);
87 * Add an event source for an SCPI device.
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.
95 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
96 * SR_ERR_MALLOC upon memory allocation errors.
98 SR_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)
101 return scpi->source_add(scpi->priv, events, timeout, cb, cb_data);
105 * Remove event source for an SCPI device.
107 * @param scpi Previously initialized SCPI device structure.
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
113 SR_PRIV int sr_scpi_source_remove(struct sr_scpi_dev_inst *scpi)
115 return scpi->source_remove(scpi->priv);
119 * Send a SCPI command.
121 * @param scpi Previously initialized SCPI device structure.
122 * @param format Format string, to be followed by any necessary arguments.
124 * @return SR_OK on success, SR_ERR on failure.
126 SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi,
127 const char *format, ...)
132 va_start(args, format);
133 ret = sr_scpi_send_variadic(scpi, format, args);
140 * Send a SCPI command with a variadic argument list.
142 * @param scpi Previously initialized SCPI device structure.
143 * @param format Format string.
144 * @param args Argument list.
146 * @return SR_OK on success, SR_ERR on failure.
148 SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi,
149 const char *format, va_list args)
155 /* Get length of buffer required. */
156 va_copy(args_copy, args);
157 len = vsnprintf(NULL, 0, format, args_copy);
160 /* Allocate buffer and write out command. */
161 buf = g_malloc(len + 1);
162 vsprintf(buf, format, args);
165 ret = scpi->send(scpi->priv, buf);
167 /* Free command buffer. */
174 * Receive an SCPI reply and store the reply in scpi_response.
176 * @param scpi Previously initialised SCPI device structure.
177 * @param scpi_response Pointer where to store the SCPI response.
179 * @return SR_OK upon fetching a full SCPI response, SR_ERR upon fetching an
180 * incomplete or no response. The allocated response must be freed by
181 * the caller in the case of a full response as well in the case of
184 SR_PRIV int sr_scpi_receive(struct sr_scpi_dev_inst *scpi,
185 char **scpi_response)
187 return scpi->receive(scpi->priv, scpi_response);
191 * Read part of a response from SCPI device.
193 * @param scpi Previously initialised SCPI device structure.
194 * @param buf Buffer to store result.
195 * @param maxlen Maximum number of bytes to read.
197 * @return Number of bytes read, or SR_ERR upon failure.
199 SR_PRIV int sr_scpi_read(struct sr_scpi_dev_inst *scpi,
200 char *buf, int maxlen)
202 return scpi->read(scpi->priv, buf, maxlen);
208 * @param scpi Previously initialized SCPI device structure.
210 * @return SR_OK on success, SR_ERR on failure.
212 SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi)
214 return scpi->close(scpi->priv);
220 * @param scpi Previously initialized SCPI device structure.
222 * @return SR_OK on success, SR_ERR on failure.
224 SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi)
226 scpi->free(scpi->priv);
231 * Send a SCPI command, receive the reply and store the reply in scpi_response.
233 * @param scpi Previously initialised SCPI device structure.
234 * @param command The SCPI command to send to the device (can be NULL).
235 * @param scpi_response Pointer where to store the SCPI response.
237 * @return SR_OK upon fetching a full SCPI response, SR_ERR upon fetching an
238 * incomplete or no response. The allocated response must be freed by
239 * the caller in the case of a full response as well in the case of
242 SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi,
243 const char *command, char **scpi_response)
246 if (sr_scpi_send(scpi, command) != SR_OK)
249 return sr_scpi_receive(scpi, scpi_response);
253 * Send a SCPI command, read the reply, parse it as a bool value and store the
254 * result in scpi_response.
256 * @param scpi Previously initialised SCPI device structure.
257 * @param command The SCPI command to send to the device (can be NULL).
258 * @param scpi_response Pointer where to store the parsed result.
260 * @return SR_OK on success, SR_ERR on failure.
262 SR_PRIV int sr_scpi_get_bool(struct sr_scpi_dev_inst *scpi,
263 const char *command, gboolean *scpi_response)
270 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
274 if (parse_strict_bool(response, scpi_response) == SR_OK)
285 * Send a SCPI command, read the reply, parse it as an integer and store the
286 * result in scpi_response.
288 * @param scpi Previously initialised SCPI device structure.
289 * @param command The SCPI command to send to the device (can be NULL).
290 * @param scpi_response Pointer where to store the parsed result.
292 * @return SR_OK on success, SR_ERR on failure.
294 SR_PRIV int sr_scpi_get_int(struct sr_scpi_dev_inst *scpi,
295 const char *command, int *scpi_response)
302 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
306 if (sr_atoi(response, scpi_response) == SR_OK)
317 * Send a SCPI command, read the reply, parse it as a float and store the
318 * result in scpi_response.
320 * @param scpi Previously initialised SCPI device structure.
321 * @param command The SCPI command to send to the device (can be NULL).
322 * @param scpi_response Pointer where to store the parsed result.
324 * @return SR_OK on success, SR_ERR on failure.
326 SR_PRIV int sr_scpi_get_float(struct sr_scpi_dev_inst *scpi,
327 const char *command, float *scpi_response)
334 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
338 if (sr_atof(response, scpi_response) == SR_OK)
349 * Send a SCPI command, read the reply, parse it as a double and store the
350 * result in scpi_response.
352 * @param scpi Previously initialised SCPI device structure.
353 * @param command The SCPI command to send to the device (can be NULL).
354 * @param scpi_response Pointer where to store the parsed result.
356 * @return SR_OK on success, SR_ERR on failure.
358 SR_PRIV int sr_scpi_get_double(struct sr_scpi_dev_inst *scpi,
359 const char *command, double *scpi_response)
366 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
370 if (sr_atod(response, scpi_response) == SR_OK)
381 * Send a SCPI *OPC? command, read the reply and return the result of the
384 * @param scpi Previously initialised SCPI device structure.
386 * @return SR_OK on success, SR_ERR on failure.
388 SR_PRIV int sr_scpi_get_opc(struct sr_scpi_dev_inst *scpi)
393 for (i = 0; i < SCPI_READ_RETRIES; ++i) {
394 sr_scpi_get_bool(scpi, SCPI_CMD_OPC, &opc);
397 g_usleep(SCPI_READ_RETRY_TIMEOUT);
404 * Send a SCPI command, read the reply, parse it as comma separated list of
405 * floats and store the as an result in scpi_response.
407 * @param scpi Previously initialised SCPI device structure.
408 * @param command The SCPI command to send to the device (can be NULL).
409 * @param scpi_response Pointer where to store the parsed result.
411 * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing
412 * error or upon no response. The allocated response must be freed by
413 * the caller in the case of an SR_OK as well as in the case of
416 SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi,
417 const char *command, GArray **scpi_response)
422 gchar **ptr, **tokens;
423 GArray *response_array;
429 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
433 tokens = g_strsplit(response, ",", 0);
436 response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256);
439 if (sr_atof(*ptr, &tmp) == SR_OK)
440 response_array = g_array_append_val(response_array,
450 if (ret == SR_ERR && response_array->len == 0) {
451 g_array_free(response_array, TRUE);
452 *scpi_response = NULL;
456 *scpi_response = response_array;
462 * Send a SCPI command, read the reply, parse it as comma separated list of
463 * unsigned 8 bit integers and store the as an result in scpi_response.
465 * @param scpi Previously initialised SCPI device structure.
466 * @param command The SCPI command to send to the device (can be NULL).
467 * @param scpi_response Pointer where to store the parsed result.
469 * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing
470 * error or upon no response. The allocated response must be freed by
471 * the caller in the case of an SR_OK as well as in the case of
474 SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi,
475 const char *command, GArray **scpi_response)
479 gchar **ptr, **tokens;
480 GArray *response_array;
486 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
490 tokens = g_strsplit(response, ",", 0);
493 response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256);
496 if (sr_atoi(*ptr, &tmp) == SR_OK)
497 response_array = g_array_append_val(response_array,
507 if (response_array->len == 0) {
508 g_array_free(response_array, TRUE);
509 *scpi_response = NULL;
513 *scpi_response = response_array;
519 * Send the *IDN? SCPI command, receive the reply, parse it and store the
520 * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer.
522 * The hw_info structure must be freed by the caller via sr_scpi_hw_info_free().
524 * @param scpi Previously initialised SCPI device structure.
525 * @param scpi_response Pointer where to store the hw_info structure.
527 * @return SR_OK upon success, SR_ERR on failure.
529 SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi,
530 struct sr_scpi_hw_info **scpi_response)
535 struct sr_scpi_hw_info *hw_info;
540 if (sr_scpi_get_string(scpi, SCPI_CMD_IDN, &response) != SR_OK)
545 * The response to a '*IDN?' is specified by the SCPI spec. It contains
546 * a comma-separated list containing the manufacturer name, instrument
547 * model, serial number of the instrument and the firmware version.
549 tokens = g_strsplit(response, ",", 0);
551 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
553 if (num_tokens != 4) {
554 sr_dbg("IDN response not according to spec: %80.s.", response);
561 hw_info = g_try_malloc(sizeof(struct sr_scpi_hw_info));
564 return SR_ERR_MALLOC;
567 hw_info->manufacturer = g_strdup(tokens[0]);
568 hw_info->model = g_strdup(tokens[1]);
569 hw_info->serial_number = g_strdup(tokens[2]);
570 hw_info->firmware_version = g_strdup(tokens[3]);
574 *scpi_response = hw_info;
580 * Free a sr_scpi_hw_info struct.
582 * @param hw_info Pointer to the struct to free.
584 * This function is safe to call with a NULL pointer.
586 SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info)
589 g_free(hw_info->manufacturer);
590 g_free(hw_info->model);
591 g_free(hw_info->serial_number);
592 g_free(hw_info->firmware_version);