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 sr_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)) {
63 } else if (!g_strcmp0(str, "0") ||
64 !g_ascii_strncasecmp(str, "n", 1) ||
65 !g_ascii_strncasecmp(str, "f", 1) ||
66 !g_ascii_strncasecmp(str, "no", 2) ||
67 !g_ascii_strncasecmp(str, "false", 5) ||
68 !g_ascii_strncasecmp(str, "off", 3)) {
78 * Send a SCPI command.
80 * @param serial Previously initialized serial port structure.
81 * @param command The SCPI command to send to the device.
83 * @return SR_OK on success, SR_ERR on failure.
85 SR_PRIV int sr_scpi_send(struct sr_serial_dev_inst *serial,
90 gchar *terminated_command;
92 terminated_command = g_strconcat(command, "\n", NULL);
93 len = strlen(terminated_command);
95 out = serial_write(serial, terminated_command,
96 strlen(terminated_command));
98 g_free(terminated_command);
101 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
106 sr_spew("Successfully sent SCPI command: '%s'.", command);
112 * Send a SCPI command, receive the reply and store the reply in scpi_response.
114 * @param serial Previously initialized serial port structure.
115 * @param command The SCPI command to send to the device (can be NULL).
116 * @param scpi_response Pointer where to store the scpi response.
118 * @return SR_OK upon fetching a full SCPI response, SR_ERR upon fetching a
119 * incomplete or no response. The allocated response must be freed by the caller
120 * in the case of a full response as well in the case of an incomplete.
122 SR_PRIV int sr_scpi_get_string(struct sr_serial_dev_inst *serial,
123 const char *command, char **scpi_response)
132 if (sr_scpi_send(serial, command) != SR_OK)
135 response = g_string_sized_new(1024);
137 for (i = 0; i <= SCPI_READ_RETRIES; i++) {
138 while ((len = serial_read(serial, buf, sizeof(buf))) > 0)
139 response = g_string_append_len(response, buf, len);
141 if (response->len > 0 &&
142 response->str[response->len-1] == '\n') {
143 sr_spew("Fetched full SCPI response");
147 g_usleep(SCPI_READ_RETRY_TIMEOUT);
150 if (response->len == 0) {
151 sr_dbg("No SCPI response received");
152 g_string_free(response, TRUE);
153 *scpi_response = NULL;
156 } else if (response->str[response->len-1] == '\n') {
158 * The SCPI response contains a LF ('\n') at the end and we
159 * don't need this so replace it with a '\0' and decrement
162 response->str[--response->len] = '\0';
166 sr_warn("Incomplete SCPI response received!");
170 /* Minor optimization: steal the string instead of copying. */
171 *scpi_response = response->str;
173 /* A SCPI response can be quite large, print at most 50 characters */
174 sr_dbg("SCPI response for command %s received (length %d): '%.50s'",
175 command, response->len, response->str);
177 g_string_free(response, FALSE);
183 * Send the *IDN? SCPI command, receive the reply, parse it and store the
184 * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer.
186 * @param serial Previously initialized serial port structure.
187 * @param scpi_response Pointer where to store the hw_info structure.
189 * @return SR_OK upon success, SR_ERR on failure.
190 * The hw_info structure must be freed by the caller with sr_scpi_hw_info_free().
192 SR_PRIV int sr_scpi_get_hw_id(struct sr_serial_dev_inst *serial,
193 struct sr_scpi_hw_info **scpi_response)
199 struct sr_scpi_hw_info *hw_info;
204 if (sr_scpi_get_string(serial, SCPI_CMD_IDN, &response) != SR_OK)
209 * The response to a '*IDN?' is specified by the SCPI spec. It contains
210 * a comma-separated list containing the manufacturer name, instrument
211 * model, serial number of the instrument and the firmware version.
213 tokens = g_strsplit(response, ",", 0);
215 for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
217 if (num_tokens != 4) {
218 sr_dbg("IDN response not according to spec: %80.s", response);
225 hw_info = g_try_malloc(sizeof(struct sr_scpi_hw_info));
228 return SR_ERR_MALLOC;
231 hw_info->manufacturer = g_strdup(tokens[0]);
232 hw_info->model = g_strdup(tokens[1]);
233 hw_info->serial_number = g_strdup(tokens[2]);
234 hw_info->firmware_version = g_strdup(tokens[3]);
238 *scpi_response = hw_info;
244 * Free a sr_scpi_hw_info struct.
246 * @param hw_info Pointer to the struct to free.
248 * This function is safe to call with a NULL pointer.
250 SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info)
253 g_free(hw_info->manufacturer);
254 g_free(hw_info->model);
255 g_free(hw_info->serial_number);
256 g_free(hw_info->firmware_version);