]>
Commit | Line | Data |
---|---|---|
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 | */ | |
48 | static int sr_parse_strict_bool(const char *str, gboolean *ret) | |
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)) { | |
59 | ||
60 | *ret = TRUE; | |
61 | return SR_OK; | |
62 | ||
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)) { | |
69 | ||
70 | *ret = FALSE; | |
71 | return SR_OK; | |
72 | } | |
73 | ||
74 | return SR_ERR; | |
75 | } | |
76 | ||
7b9d7320 DJ |
77 | /** |
78 | * Send a SCPI command. | |
79 | * | |
80 | * @param serial Previously initialized serial port structure. | |
81 | * @param command The SCPI command to send to the device. | |
82 | * | |
83 | * @return SR_OK on success, SR_ERR on failure. | |
84 | */ | |
85 | SR_PRIV int sr_scpi_send(struct sr_serial_dev_inst *serial, | |
86 | const char *command) | |
87 | { | |
88 | int len; | |
89 | int out; | |
90 | gchar *terminated_command; | |
91 | ||
92 | terminated_command = g_strconcat(command, "\n", NULL); | |
93 | len = strlen(terminated_command); | |
94 | ||
95 | out = serial_write(serial, terminated_command, | |
96 | strlen(terminated_command)); | |
97 | ||
98 | g_free(terminated_command); | |
99 | ||
100 | if (out != len) { | |
101 | sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out, | |
102 | len, command); | |
103 | return SR_ERR; | |
104 | } | |
105 | ||
106 | sr_spew("Successfully sent SCPI command: '%s'.", command); | |
107 | ||
108 | return SR_OK; | |
109 | } | |
110 | ||
111 | /** | |
112 | * Send a SCPI command, receive the reply and store the reply in scpi_response. | |
113 | * | |
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. | |
117 | * | |
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. | |
121 | */ | |
122 | SR_PRIV int sr_scpi_get_string(struct sr_serial_dev_inst *serial, | |
123 | const char *command, char **scpi_response) | |
124 | { | |
125 | int len; | |
126 | int ret; | |
127 | char buf[256]; | |
128 | unsigned int i; | |
129 | GString *response; | |
130 | ||
131 | if (command) | |
132 | if (sr_scpi_send(serial, command) != SR_OK) | |
133 | return SR_ERR; | |
134 | ||
135 | response = g_string_sized_new(1024); | |
136 | ||
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); | |
140 | ||
141 | if (response->len > 0 && | |
142 | response->str[response->len-1] == '\n') { | |
143 | sr_spew("Fetched full SCPI response"); | |
144 | break; | |
145 | } | |
146 | ||
147 | g_usleep(SCPI_READ_RETRY_TIMEOUT); | |
148 | } | |
149 | ||
150 | if (response->len == 0) { | |
151 | sr_dbg("No SCPI response received"); | |
152 | g_string_free(response, TRUE); | |
153 | *scpi_response = NULL; | |
154 | return SR_ERR; | |
155 | ||
156 | } else if (response->str[response->len-1] == '\n') { | |
157 | /* | |
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 | |
160 | * the length. | |
161 | */ | |
162 | response->str[--response->len] = '\0'; | |
163 | ret = SR_OK; | |
164 | ||
165 | } else { | |
166 | sr_warn("Incomplete SCPI response received!"); | |
167 | ret = SR_ERR; | |
168 | } | |
169 | ||
170 | /* Minor optimization: steal the string instead of copying. */ | |
171 | *scpi_response = response->str; | |
172 | ||
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); | |
176 | ||
177 | g_string_free(response, FALSE); | |
178 | ||
d730f70e DJ |
179 | return ret; |
180 | } | |
181 | ||
182 | /** | |
183 | * Send a SCPI command, read the reply, parse it as a bool value and store the | |
184 | * result in scpi_response. | |
185 | * | |
186 | * @param serial Previously initialized serial port structure. | |
187 | * @param command The SCPI command to send to the device (can be NULL). | |
188 | * @param scpi_response Pointer where to store the parsed result. | |
189 | * | |
190 | * @return SR_OK on success, SR_ERR on failure. | |
191 | */ | |
192 | SR_PRIV int sr_scpi_get_bool(struct sr_serial_dev_inst *serial, | |
193 | const char *command, gboolean *scpi_response) | |
194 | { | |
195 | int ret; | |
196 | char *response; | |
197 | ||
198 | response = NULL; | |
199 | ||
200 | if (sr_scpi_get_string(serial, command, &response) != SR_OK) | |
201 | if (!response) | |
202 | return SR_ERR; | |
203 | ||
204 | if (sr_parse_strict_bool(response, scpi_response) == SR_OK) | |
205 | ret = SR_OK; | |
206 | else | |
207 | ret = SR_ERR; | |
208 | ||
209 | g_free(response); | |
210 | ||
211 | return ret; | |
212 | } | |
213 | ||
214 | /** | |
215 | * Send a SCPI command, read the reply, parse it as an integer and store the | |
216 | * result in scpi_response. | |
217 | * | |
218 | * @param serial Previously initialized serial port structure. | |
219 | * @param command The SCPI command to send to the device (can be NULL). | |
220 | * @param scpi_response Pointer where to store the parsed result. | |
221 | * | |
222 | * @return SR_OK on success, SR_ERR on failure. | |
223 | */ | |
224 | SR_PRIV int sr_scpi_get_int(struct sr_serial_dev_inst *serial, | |
225 | const char *command, int *scpi_response) | |
226 | { | |
227 | int ret; | |
228 | char *response; | |
229 | ||
230 | response = NULL; | |
231 | ||
232 | if (sr_scpi_get_string(serial, command, &response) != SR_OK) | |
233 | if (!response) | |
234 | return SR_ERR; | |
235 | ||
236 | if (sr_atoi(response, scpi_response) == SR_OK) | |
237 | ret = SR_OK; | |
238 | else | |
239 | ret = SR_ERR; | |
240 | ||
241 | g_free(response); | |
242 | ||
243 | return ret; | |
244 | } | |
245 | ||
246 | /** | |
247 | * Send a SCPI command, read the reply, parse it as a float and store the | |
248 | * result in scpi_response. | |
249 | * | |
250 | * @param serial Previously initialized serial port structure. | |
251 | * @param command The SCPI command to send to the device (can be NULL). | |
252 | * @param scpi_response Pointer where to store the parsed result. | |
253 | * | |
254 | * @return SR_OK on success, SR_ERR on failure. | |
255 | */ | |
256 | SR_PRIV int sr_scpi_get_float(struct sr_serial_dev_inst *serial, | |
257 | const char *command, float *scpi_response) | |
258 | { | |
259 | int ret; | |
260 | char *response; | |
261 | ||
262 | response = NULL; | |
263 | ||
264 | if (sr_scpi_get_string(serial, command, &response) != SR_OK) | |
265 | if (!response) | |
266 | return SR_ERR; | |
267 | ||
268 | if (sr_atof(response, scpi_response) == SR_OK) | |
269 | ret = SR_OK; | |
270 | else | |
271 | ret = SR_ERR; | |
272 | ||
273 | g_free(response); | |
274 | ||
275 | return ret; | |
276 | } | |
277 | ||
278 | /** | |
279 | * Send a SCPI command, read the reply, parse it as a double and store the | |
280 | * result in scpi_response. | |
281 | * | |
282 | * @param serial Previously initialized serial port structure. | |
283 | * @param command The SCPI command to send to the device (can be NULL). | |
284 | * @param scpi_response Pointer where to store the parsed result. | |
285 | * | |
286 | * @return SR_OK on success, SR_ERR on failure. | |
287 | */ | |
288 | SR_PRIV int sr_scpi_get_double(struct sr_serial_dev_inst *serial, | |
289 | const char *command, double *scpi_response) | |
290 | { | |
291 | int ret; | |
292 | char *response; | |
293 | ||
294 | response = NULL; | |
295 | ||
296 | if (sr_scpi_get_string(serial, command, &response) != SR_OK) | |
297 | if (!response) | |
298 | return SR_ERR; | |
299 | ||
300 | if (sr_atod(response, scpi_response) == SR_OK) | |
301 | ret = SR_OK; | |
302 | else | |
303 | ret = SR_ERR; | |
304 | ||
305 | g_free(response); | |
306 | ||
7b9d7320 DJ |
307 | return ret; |
308 | } | |
309 | ||
f5922ade DJ |
310 | /** |
311 | * Send a SCPI *OPC? command, read the reply and return the result of the | |
312 | * command. | |
313 | * | |
314 | * @param serial Previously initialized serial port structure. | |
315 | * | |
316 | * @return SR_OK on success, SR_ERR on failure. | |
317 | */ | |
318 | SR_PRIV int sr_scpi_get_opc(struct sr_serial_dev_inst *serial) | |
319 | { | |
320 | unsigned int i; | |
321 | gboolean opc; | |
322 | ||
323 | for (i = 0; i < SCPI_READ_RETRIES; ++i) { | |
324 | sr_scpi_get_bool(serial, SCPI_CMD_OPC, &opc); | |
325 | ||
326 | if (opc) | |
327 | return SR_OK; | |
328 | ||
329 | g_usleep(SCPI_READ_RETRY_TIMEOUT); | |
330 | } | |
331 | ||
332 | return SR_ERR; | |
333 | } | |
334 | ||
8acbb89a DJ |
335 | /** |
336 | * Send a SCPI command, read the reply, parse it as comma separated list of | |
337 | * floats and store the as an result in scpi_response. | |
338 | * | |
339 | * @param serial Previously initialized serial port structure. | |
340 | * @param command The SCPI command to send to the device (can be NULL). | |
341 | * @param scpi_response Pointer where to store the parsed result. | |
342 | * | |
343 | * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing | |
344 | * error or upon no response. The allocated response must be freed by the caller | |
345 | * in the case of an SR_OK as well as in the case of parsing error. | |
346 | */ | |
347 | SR_PRIV int sr_scpi_get_floatv(struct sr_serial_dev_inst *serial, | |
348 | const char *command, GArray **scpi_response) | |
349 | { | |
350 | int ret; | |
351 | float tmp; | |
352 | char *response; | |
353 | ||
354 | gchar **ptr; | |
355 | gchar **tokens; | |
356 | GArray *response_array; | |
357 | ||
358 | ret = SR_OK; | |
359 | response = NULL; | |
360 | tokens = NULL; | |
361 | ||
362 | if (sr_scpi_get_string(serial, command, &response) != SR_OK) | |
363 | if (!response) | |
364 | return SR_ERR; | |
365 | ||
366 | tokens = g_strsplit(response, ",", 0); | |
367 | ptr = tokens; | |
368 | ||
369 | response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256); | |
370 | ||
371 | while(*ptr) { | |
372 | if (sr_atof(*ptr, &tmp) == SR_OK) | |
373 | response_array = g_array_append_val(response_array, | |
374 | tmp); | |
375 | else | |
376 | ret = SR_ERR; | |
377 | ||
378 | ptr++; | |
379 | } | |
380 | g_strfreev(tokens); | |
381 | g_free(response); | |
382 | ||
383 | if (ret == SR_ERR && response_array->len == 0) { | |
384 | g_array_free(response_array, TRUE); | |
385 | *scpi_response = NULL; | |
386 | return SR_ERR; | |
387 | } | |
388 | ||
389 | *scpi_response = response_array; | |
390 | ||
1a323dd8 DJ |
391 | return ret; |
392 | } | |
393 | ||
394 | /** | |
395 | * Send a SCPI command, read the reply, parse it as comma separated list of | |
396 | * unsigned 8 bit integers and store the as an result in scpi_response. | |
397 | * | |
398 | * @param serial Previously initialized serial port structure. | |
399 | * @param command The SCPI command to send to the device (can be NULL). | |
400 | * @param scpi_response Pointer where to store the parsed result. | |
401 | * | |
402 | * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing | |
403 | * error or upon no response. The allocated response must be freed by the caller | |
404 | * in the case of an SR_OK as well as in the case of parsing error. | |
405 | */ | |
406 | SR_PRIV int sr_scpi_get_uint8v(struct sr_serial_dev_inst *serial, | |
407 | const char *command, GArray **scpi_response) | |
408 | { | |
409 | int tmp; | |
410 | int ret; | |
411 | char *response; | |
412 | ||
413 | gchar **ptr; | |
414 | gchar **tokens; | |
415 | GArray *response_array; | |
416 | ||
417 | ret = SR_OK; | |
418 | response = NULL; | |
419 | tokens = NULL; | |
420 | ||
421 | if (sr_scpi_get_string(serial, command, &response) != SR_OK) | |
422 | if (!response) | |
423 | return SR_ERR; | |
424 | ||
425 | tokens = g_strsplit(response, ",", 0); | |
426 | ptr = tokens; | |
427 | ||
428 | response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256); | |
429 | ||
430 | while(*ptr) { | |
431 | if (sr_atoi(*ptr, &tmp) == SR_OK) | |
432 | response_array = g_array_append_val(response_array, | |
433 | tmp); | |
434 | else | |
435 | ret = SR_ERR; | |
436 | ||
437 | ptr++; | |
438 | } | |
439 | g_strfreev(tokens); | |
440 | g_free(response); | |
441 | ||
442 | if (response_array->len == 0) { | |
443 | g_array_free(response_array, TRUE); | |
444 | *scpi_response = NULL; | |
445 | return SR_ERR; | |
446 | } | |
447 | ||
448 | *scpi_response = response_array; | |
449 | ||
8acbb89a DJ |
450 | return ret; |
451 | } | |
452 | ||
7b9d7320 DJ |
453 | /** |
454 | * Send the *IDN? SCPI command, receive the reply, parse it and store the | |
455 | * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer. | |
456 | * | |
457 | * @param serial Previously initialized serial port structure. | |
458 | * @param scpi_response Pointer where to store the hw_info structure. | |
459 | * | |
460 | * @return SR_OK upon success, SR_ERR on failure. | |
461 | * The hw_info structure must be freed by the caller with sr_scpi_hw_info_free(). | |
462 | */ | |
463 | SR_PRIV int sr_scpi_get_hw_id(struct sr_serial_dev_inst *serial, | |
464 | struct sr_scpi_hw_info **scpi_response) | |
465 | { | |
466 | int num_tokens; | |
467 | char *response; | |
468 | gchar **tokens; | |
469 | ||
470 | struct sr_scpi_hw_info *hw_info; | |
471 | ||
472 | response = NULL; | |
473 | tokens = NULL; | |
474 | ||
475 | if (sr_scpi_get_string(serial, SCPI_CMD_IDN, &response) != SR_OK) | |
476 | if (!response) | |
477 | return SR_ERR; | |
478 | ||
479 | /* | |
480 | * The response to a '*IDN?' is specified by the SCPI spec. It contains | |
481 | * a comma-separated list containing the manufacturer name, instrument | |
482 | * model, serial number of the instrument and the firmware version. | |
483 | */ | |
484 | tokens = g_strsplit(response, ",", 0); | |
485 | ||
486 | for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++); | |
487 | ||
488 | if (num_tokens != 4) { | |
489 | sr_dbg("IDN response not according to spec: %80.s", response); | |
490 | g_strfreev(tokens); | |
491 | g_free(response); | |
492 | return SR_ERR; | |
493 | } | |
494 | g_free(response); | |
495 | ||
496 | hw_info = g_try_malloc(sizeof(struct sr_scpi_hw_info)); | |
497 | if (!hw_info) { | |
498 | g_strfreev(tokens); | |
499 | return SR_ERR_MALLOC; | |
500 | } | |
501 | ||
502 | hw_info->manufacturer = g_strdup(tokens[0]); | |
503 | hw_info->model = g_strdup(tokens[1]); | |
504 | hw_info->serial_number = g_strdup(tokens[2]); | |
505 | hw_info->firmware_version = g_strdup(tokens[3]); | |
506 | ||
507 | g_strfreev(tokens); | |
508 | ||
509 | *scpi_response = hw_info; | |
510 | ||
511 | return SR_OK; | |
512 | } | |
513 | ||
514 | /** | |
515 | * Free a sr_scpi_hw_info struct. | |
516 | * | |
517 | * @param hw_info Pointer to the struct to free. | |
518 | * | |
519 | * This function is safe to call with a NULL pointer. | |
520 | */ | |
521 | SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info) | |
522 | { | |
523 | if (hw_info) { | |
524 | g_free(hw_info->manufacturer); | |
525 | g_free(hw_info->model); | |
526 | g_free(hw_info->serial_number); | |
527 | g_free(hw_info->firmware_version); | |
528 | g_free(hw_info); | |
529 | } | |
530 | } |