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"
27 /* Message logging helpers with subsystem-specific prefix string. */
28 #define LOG_PREFIX "scpi: "
29 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
30 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
31 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
32 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
33 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
35 #define SCPI_READ_RETRIES 100
36 #define SCPI_READ_RETRY_TIMEOUT 10000
39 * Parse a string representation of a boolean-like value into a gboolean.
40 * Similar to sr_parse_boolstring but rejects strings which do not represent
41 * a boolean-like value.
43 * @param str String to convert.
44 * @param ret Pointer to a gboolean where the result of the conversion will be
47 * @return SR_OK on success, SR_ERR on failure.
49 static int parse_strict_bool(const char *str, gboolean *ret)
54 if (!g_strcmp0(str, "1") ||
55 !g_ascii_strncasecmp(str, "y", 1) ||
56 !g_ascii_strncasecmp(str, "t", 1) ||
57 !g_ascii_strncasecmp(str, "yes", 3) ||
58 !g_ascii_strncasecmp(str, "true", 4) ||
59 !g_ascii_strncasecmp(str, "on", 2)) {
62 } else if (!g_strcmp0(str, "0") ||
63 !g_ascii_strncasecmp(str, "n", 1) ||
64 !g_ascii_strncasecmp(str, "f", 1) ||
65 !g_ascii_strncasecmp(str, "no", 2) ||
66 !g_ascii_strncasecmp(str, "false", 5) ||
67 !g_ascii_strncasecmp(str, "off", 3)) {
78 * @param scpi Previously initialized SCPI device structure.
80 * @return SR_OK on success, SR_ERR on failure.
82 SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi)
84 return scpi->open(scpi->priv);
88 * Add an event source for an SCPI device.
90 * @param scpi Previously initialized SCPI device structure.
91 * @param events Events to check for.
92 * @param timeout Max time to wait before the callback is called, ignored if 0.
93 * @param cb Callback function to add. Must not be NULL.
94 * @param cb_data Data for the callback function. Can be NULL.
96 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
97 * SR_ERR_MALLOC upon memory allocation errors.
99 SR_PRIV int sr_scpi_source_add(struct sr_scpi_dev_inst *scpi, int events,
100 int timeout, sr_receive_data_callback_t cb, void *cb_data)
102 return scpi->source_add(scpi->priv, events, timeout, cb, cb_data);
106 * Remove event source for an SCPI device.
108 * @param scpi Previously initialized SCPI device structure.
110 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
111 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
114 SR_PRIV int sr_scpi_source_remove(struct sr_scpi_dev_inst *scpi)
116 return scpi->source_remove(scpi->priv);
120 * Send a SCPI command.
122 * @param scpi Previously initialized SCPI device structure.
123 * @param format Format string, to be followed by any necessary arguments.
125 * @return SR_OK on success, SR_ERR on failure.
127 SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi,
128 const char *format, ...)
130 va_list args1, args2;
134 /* Copy arguments since we need to make two variadic calls. */
135 va_start(args1, format);
136 va_copy(args2, args1);
138 /* Get length of buffer required. */
139 len = vsnprintf(NULL, 0, format, args1);
142 /* Allocate buffer and write out command. */
143 buf = g_malloc(len + 1);
144 vsprintf(buf, format, args2);
148 ret = scpi->send(scpi->priv, buf);
150 /* Free command buffer. */
157 * Receive an SCPI reply and store the reply in scpi_response.
159 * @param scpi Previously initialised SCPI device structure.
160 * @param scpi_response Pointer where to store the SCPI response.
162 * @return SR_OK upon fetching a full SCPI response, SR_ERR upon fetching an
163 * incomplete or no response. The allocated response must be freed by
164 * the caller in the case of a full response as well in the case of
167 SR_PRIV int sr_scpi_receive(struct sr_scpi_dev_inst *scpi,
168 char **scpi_response)
170 return scpi->receive(scpi->priv, scpi_response);
174 * Read part of a response from SCPI device.
176 * @param scpi Previously initialised SCPI device structure.
177 * @param buf Buffer to store result.
178 * @param maxlen Maximum number of bytes to read.
180 * @return Number of bytes read, or SR_ERR upon failure.
182 SR_PRIV int sr_scpi_read(struct sr_scpi_dev_inst *scpi,
183 char *buf, int maxlen)
185 return scpi->read(scpi->priv, buf, maxlen);
191 * @param scpi Previously initialized SCPI device structure.
193 * @return SR_OK on success, SR_ERR on failure.
195 SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi)
197 return scpi->close(scpi->priv);
203 * @param scpi Previously initialized SCPI device structure.
205 * @return SR_OK on success, SR_ERR on failure.
207 SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi)
209 scpi->free(scpi->priv);
214 * Send a SCPI command, receive the reply and store the reply in scpi_response.
216 * @param scpi Previously initialised SCPI device structure.
217 * @param command The SCPI command to send to the device (can be NULL).
218 * @param scpi_response Pointer where to store the SCPI response.
220 * @return SR_OK upon fetching a full SCPI response, SR_ERR upon fetching an
221 * incomplete or no response. The allocated response must be freed by
222 * the caller in the case of a full response as well in the case of
225 SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi,
226 const char *command, char **scpi_response)
229 if (sr_scpi_send(scpi, command) != SR_OK)
232 return sr_scpi_receive(scpi, scpi_response);
236 * Send a SCPI command, read the reply, parse it as a bool value and store the
237 * result in scpi_response.
239 * @param scpi Previously initialised SCPI device structure.
240 * @param command The SCPI command to send to the device (can be NULL).
241 * @param scpi_response Pointer where to store the parsed result.
243 * @return SR_OK on success, SR_ERR on failure.
245 SR_PRIV int sr_scpi_get_bool(struct sr_scpi_dev_inst *scpi,
246 const char *command, gboolean *scpi_response)
253 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
257 if (parse_strict_bool(response, scpi_response) == SR_OK)
268 * Send a SCPI command, read the reply, parse it as an integer and store the
269 * result in scpi_response.
271 * @param scpi Previously initialised SCPI device structure.
272 * @param command The SCPI command to send to the device (can be NULL).
273 * @param scpi_response Pointer where to store the parsed result.
275 * @return SR_OK on success, SR_ERR on failure.
277 SR_PRIV int sr_scpi_get_int(struct sr_scpi_dev_inst *scpi,
278 const char *command, int *scpi_response)
285 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
289 if (sr_atoi(response, scpi_response) == SR_OK)
300 * Send a SCPI command, read the reply, parse it as a float and store the
301 * result in scpi_response.
303 * @param scpi Previously initialised SCPI device structure.
304 * @param command The SCPI command to send to the device (can be NULL).
305 * @param scpi_response Pointer where to store the parsed result.
307 * @return SR_OK on success, SR_ERR on failure.
309 SR_PRIV int sr_scpi_get_float(struct sr_scpi_dev_inst *scpi,
310 const char *command, float *scpi_response)
317 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
321 if (sr_atof(response, scpi_response) == SR_OK)
332 * Send a SCPI command, read the reply, parse it as a double and store the
333 * result in scpi_response.
335 * @param scpi Previously initialised SCPI device structure.
336 * @param command The SCPI command to send to the device (can be NULL).
337 * @param scpi_response Pointer where to store the parsed result.
339 * @return SR_OK on success, SR_ERR on failure.
341 SR_PRIV int sr_scpi_get_double(struct sr_scpi_dev_inst *scpi,
342 const char *command, double *scpi_response)
349 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
353 if (sr_atod(response, scpi_response) == SR_OK)
364 * Send a SCPI *OPC? command, read the reply and return the result of the
367 * @param scpi Previously initialised SCPI device structure.
369 * @return SR_OK on success, SR_ERR on failure.
371 SR_PRIV int sr_scpi_get_opc(struct sr_scpi_dev_inst *scpi)
376 for (i = 0; i < SCPI_READ_RETRIES; ++i) {
377 sr_scpi_get_bool(scpi, SCPI_CMD_OPC, &opc);
380 g_usleep(SCPI_READ_RETRY_TIMEOUT);
387 * Send a SCPI command, read the reply, parse it as comma separated list of
388 * floats and store the as an result in scpi_response.
390 * @param scpi Previously initialised SCPI device structure.
391 * @param command The SCPI command to send to the device (can be NULL).
392 * @param scpi_response Pointer where to store the parsed result.
394 * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing
395 * error or upon no response. The allocated response must be freed by
396 * the caller in the case of an SR_OK as well as in the case of
399 SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi,
400 const char *command, GArray **scpi_response)
405 gchar **ptr, **tokens;
406 GArray *response_array;
412 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
416 tokens = g_strsplit(response, ",", 0);
419 response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256);
422 if (sr_atof(*ptr, &tmp) == SR_OK)
423 response_array = g_array_append_val(response_array,
433 if (ret == SR_ERR && response_array->len == 0) {
434 g_array_free(response_array, TRUE);
435 *scpi_response = NULL;
439 *scpi_response = response_array;
445 * Send a SCPI command, read the reply, parse it as comma separated list of
446 * unsigned 8 bit integers and store the as an result in scpi_response.
448 * @param scpi Previously initialised SCPI device structure.
449 * @param command The SCPI command to send to the device (can be NULL).
450 * @param scpi_response Pointer where to store the parsed result.
452 * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing
453 * error or upon no response. The allocated response must be freed by
454 * the caller in the case of an SR_OK as well as in the case of
457 SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi,
458 const char *command, GArray **scpi_response)
462 gchar **ptr, **tokens;
463 GArray *response_array;
469 if (sr_scpi_get_string(scpi, command, &response) != SR_OK)
473 tokens = g_strsplit(response, ",", 0);
476 response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256);
479 if (sr_atoi(*ptr, &tmp) == SR_OK)
480 response_array = g_array_append_val(response_array,
490 if (response_array->len == 0) {
491 g_array_free(response_array, TRUE);
492 *scpi_response = NULL;
496 *scpi_response = response_array;
502 * Send the *IDN? SCPI command, receive the reply, parse it and store the
503 * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer.
505 * The hw_info structure must be freed by the caller via sr_scpi_hw_info_free().
507 * @param scpi Previously initialised SCPI device structure.
508 * @param scpi_response Pointer where to store the hw_info structure.
510 * @return SR_OK upon success, SR_ERR on failure.
512 SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi,
513 struct sr_scpi_hw_info **scpi_response)
518 struct sr_scpi_hw_info *hw_info;
523 if (sr_scpi_get_string(scpi, SCPI_CMD_IDN, &response) != SR_OK)
528 * The response to a '*IDN?' is specified by the SCPI spec. It contains
529 * a comma-separated list containing the manufacturer name, instrument
530 * model, serial number of the instrument and the firmware version.
532 tokens = g_strsplit(response, ",", 0);
534 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
536 if (num_tokens != 4) {
537 sr_dbg("IDN response not according to spec: %80.s.", response);
544 hw_info = g_try_malloc(sizeof(struct sr_scpi_hw_info));
547 return SR_ERR_MALLOC;
550 hw_info->manufacturer = g_strdup(tokens[0]);
551 hw_info->model = g_strdup(tokens[1]);
552 hw_info->serial_number = g_strdup(tokens[2]);
553 hw_info->firmware_version = g_strdup(tokens[3]);
557 *scpi_response = hw_info;
563 * Free a sr_scpi_hw_info struct.
565 * @param hw_info Pointer to the struct to free.
567 * This function is safe to call with a NULL pointer.
569 SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info)
572 g_free(hw_info->manufacturer);
573 g_free(hw_info->model);
574 g_free(hw_info->serial_number);
575 g_free(hw_info->firmware_version);