]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com> | |
5 | * Copyright (C) 2015 Bert Vermeulen <bert@biot.com> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include <glib.h> | |
23 | #include <string.h> | |
24 | #include <libsigrok/libsigrok.h> | |
25 | #include "libsigrok-internal.h" | |
26 | #include "scpi.h" | |
27 | ||
28 | #define LOG_PREFIX "scpi" | |
29 | ||
30 | #define SCPI_READ_RETRIES 100 | |
31 | #define SCPI_READ_RETRY_TIMEOUT_US (10 * 1000) | |
32 | ||
33 | static const char *scpi_vendors[][2] = { | |
34 | { "Agilent Technologies", "Agilent" }, | |
35 | { "CHROMA", "Chroma" }, | |
36 | { "Chroma ATE", "Chroma" }, | |
37 | { "HEWLETT-PACKARD", "HP" }, | |
38 | { "Keysight Technologies", "Keysight" }, | |
39 | { "PHILIPS", "Philips" }, | |
40 | { "RIGOL TECHNOLOGIES", "Rigol" }, | |
41 | { "Siglent Technologies", "Siglent" }, | |
42 | }; | |
43 | ||
44 | /** | |
45 | * Parse a string representation of a boolean-like value into a gboolean. | |
46 | * Similar to sr_parse_boolstring but rejects strings which do not represent | |
47 | * a boolean-like value. | |
48 | * | |
49 | * @param str String to convert. | |
50 | * @param ret Pointer to a gboolean where the result of the conversion will be | |
51 | * stored. | |
52 | * | |
53 | * @return SR_OK on success, SR_ERR on failure. | |
54 | */ | |
55 | static int parse_strict_bool(const char *str, gboolean *ret) | |
56 | { | |
57 | if (!str) | |
58 | return SR_ERR_ARG; | |
59 | ||
60 | if (!g_strcmp0(str, "1") || | |
61 | !g_ascii_strncasecmp(str, "y", 1) || | |
62 | !g_ascii_strncasecmp(str, "t", 1) || | |
63 | !g_ascii_strncasecmp(str, "yes", 3) || | |
64 | !g_ascii_strncasecmp(str, "true", 4) || | |
65 | !g_ascii_strncasecmp(str, "on", 2)) { | |
66 | *ret = TRUE; | |
67 | return SR_OK; | |
68 | } else if (!g_strcmp0(str, "0") || | |
69 | !g_ascii_strncasecmp(str, "n", 1) || | |
70 | !g_ascii_strncasecmp(str, "f", 1) || | |
71 | !g_ascii_strncasecmp(str, "no", 2) || | |
72 | !g_ascii_strncasecmp(str, "false", 5) || | |
73 | !g_ascii_strncasecmp(str, "off", 3)) { | |
74 | *ret = FALSE; | |
75 | return SR_OK; | |
76 | } | |
77 | ||
78 | return SR_ERR; | |
79 | } | |
80 | ||
81 | SR_PRIV extern const struct sr_scpi_dev_inst scpi_serial_dev; | |
82 | SR_PRIV extern const struct sr_scpi_dev_inst scpi_tcp_raw_dev; | |
83 | SR_PRIV extern const struct sr_scpi_dev_inst scpi_tcp_rigol_dev; | |
84 | SR_PRIV extern const struct sr_scpi_dev_inst scpi_usbtmc_libusb_dev; | |
85 | SR_PRIV extern const struct sr_scpi_dev_inst scpi_vxi_dev; | |
86 | SR_PRIV extern const struct sr_scpi_dev_inst scpi_visa_dev; | |
87 | SR_PRIV extern const struct sr_scpi_dev_inst scpi_libgpib_dev; | |
88 | ||
89 | static const struct sr_scpi_dev_inst *scpi_devs[] = { | |
90 | &scpi_tcp_raw_dev, | |
91 | &scpi_tcp_rigol_dev, | |
92 | #ifdef HAVE_LIBUSB_1_0 | |
93 | &scpi_usbtmc_libusb_dev, | |
94 | #endif | |
95 | #if HAVE_RPC | |
96 | &scpi_vxi_dev, | |
97 | #endif | |
98 | #ifdef HAVE_LIBREVISA | |
99 | &scpi_visa_dev, | |
100 | #endif | |
101 | #ifdef HAVE_LIBGPIB | |
102 | &scpi_libgpib_dev, | |
103 | #endif | |
104 | #ifdef HAVE_SERIAL_COMM | |
105 | &scpi_serial_dev, /* Must be last as it matches any resource. */ | |
106 | #endif | |
107 | }; | |
108 | ||
109 | static struct sr_dev_inst *sr_scpi_scan_resource(struct drv_context *drvc, | |
110 | const char *resource, const char *serialcomm, | |
111 | struct sr_dev_inst *(*probe_device)(struct sr_scpi_dev_inst *scpi)) | |
112 | { | |
113 | struct sr_scpi_dev_inst *scpi; | |
114 | struct sr_dev_inst *sdi; | |
115 | ||
116 | if (!(scpi = scpi_dev_inst_new(drvc, resource, serialcomm))) | |
117 | return NULL; | |
118 | ||
119 | if (sr_scpi_open(scpi) != SR_OK) { | |
120 | sr_info("Couldn't open SCPI device."); | |
121 | sr_scpi_free(scpi); | |
122 | return NULL; | |
123 | }; | |
124 | ||
125 | sdi = probe_device(scpi); | |
126 | ||
127 | sr_scpi_close(scpi); | |
128 | ||
129 | if (sdi) | |
130 | sdi->status = SR_ST_INACTIVE; | |
131 | else | |
132 | sr_scpi_free(scpi); | |
133 | ||
134 | return sdi; | |
135 | } | |
136 | ||
137 | /** | |
138 | * Send a SCPI command with a variadic argument list without mutex. | |
139 | * | |
140 | * @param scpi Previously initialized SCPI device structure. | |
141 | * @param format Format string. | |
142 | * @param args Argument list. | |
143 | * | |
144 | * @return SR_OK on success, SR_ERR on failure. | |
145 | */ | |
146 | static int scpi_send_variadic(struct sr_scpi_dev_inst *scpi, | |
147 | const char *format, va_list args) | |
148 | { | |
149 | va_list args_copy; | |
150 | char *buf; | |
151 | int len, ret; | |
152 | ||
153 | /* Get length of buffer required. */ | |
154 | va_copy(args_copy, args); | |
155 | len = sr_vsnprintf_ascii(NULL, 0, format, args_copy); | |
156 | va_end(args_copy); | |
157 | ||
158 | /* Allocate buffer and write out command. */ | |
159 | buf = g_malloc0(len + 2); | |
160 | sr_vsprintf_ascii(buf, format, args); | |
161 | if (buf[len - 1] != '\n') | |
162 | buf[len] = '\n'; | |
163 | ||
164 | /* Send command. */ | |
165 | ret = scpi->send(scpi->priv, buf); | |
166 | ||
167 | /* Free command buffer. */ | |
168 | g_free(buf); | |
169 | ||
170 | return ret; | |
171 | } | |
172 | ||
173 | /** | |
174 | * Send a SCPI command without mutex. | |
175 | * | |
176 | * @param scpi Previously initialized SCPI device structure. | |
177 | * @param format Format string, to be followed by any necessary arguments. | |
178 | * | |
179 | * @return SR_OK on success, SR_ERR on failure. | |
180 | */ | |
181 | static int scpi_send(struct sr_scpi_dev_inst *scpi, const char *format, ...) | |
182 | { | |
183 | va_list args; | |
184 | int ret; | |
185 | ||
186 | va_start(args, format); | |
187 | ret = scpi_send_variadic(scpi, format, args); | |
188 | va_end(args); | |
189 | ||
190 | return ret; | |
191 | } | |
192 | ||
193 | /** | |
194 | * Send data to SCPI device without mutex. | |
195 | * | |
196 | * TODO: This is only implemented in TcpRaw, but never used. | |
197 | * TODO: Use Mutex at all? | |
198 | * | |
199 | * @param scpi Previously initialised SCPI device structure. | |
200 | * @param buf Buffer with data to send. | |
201 | * @param len Number of bytes to send. | |
202 | * | |
203 | * @return Number of bytes read, or SR_ERR upon failure. | |
204 | */ | |
205 | static int scpi_write_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen) | |
206 | { | |
207 | return scpi->write_data(scpi->priv, buf, maxlen); | |
208 | } | |
209 | ||
210 | /** | |
211 | * Read part of a response from SCPI device without mutex. | |
212 | * | |
213 | * @param scpi Previously initialised SCPI device structure. | |
214 | * @param buf Buffer to store result. | |
215 | * @param maxlen Maximum number of bytes to read. | |
216 | * | |
217 | * @return Number of bytes read, or SR_ERR upon failure. | |
218 | */ | |
219 | static int scpi_read_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen) | |
220 | { | |
221 | return scpi->read_data(scpi->priv, buf, maxlen); | |
222 | } | |
223 | ||
224 | /** | |
225 | * Do a non-blocking read of up to the allocated length, and | |
226 | * check if a timeout has occured, without mutex. | |
227 | * | |
228 | * @param scpi Previously initialised SCPI device structure. | |
229 | * @param response Buffer to which the response is appended. | |
230 | * @param abs_timeout_us Absolute timeout in microseconds | |
231 | * | |
232 | * @return read length on success, SR_ERR* on failure. | |
233 | */ | |
234 | static int scpi_read_response(struct sr_scpi_dev_inst *scpi, | |
235 | GString *response, gint64 abs_timeout_us) | |
236 | { | |
237 | int len, space; | |
238 | ||
239 | space = response->allocated_len - response->len; | |
240 | len = scpi->read_data(scpi->priv, &response->str[response->len], space); | |
241 | ||
242 | if (len < 0) { | |
243 | sr_err("Incompletely read SCPI response."); | |
244 | return SR_ERR; | |
245 | } | |
246 | ||
247 | if (len > 0) { | |
248 | g_string_set_size(response, response->len + len); | |
249 | return len; | |
250 | } | |
251 | ||
252 | if (g_get_monotonic_time() > abs_timeout_us) { | |
253 | sr_err("Timed out waiting for SCPI response."); | |
254 | return SR_ERR_TIMEOUT; | |
255 | } | |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
260 | /** | |
261 | * Send a SCPI command, receive the reply and store the reply in | |
262 | * scpi_response, without mutex. | |
263 | * | |
264 | * @param scpi Previously initialised SCPI device structure. | |
265 | * @param command The SCPI command to send to the device. | |
266 | * @param scpi_response Pointer where to store the SCPI response. | |
267 | * | |
268 | * @return SR_OK on success, SR_ERR on failure. | |
269 | */ | |
270 | static int scpi_get_data(struct sr_scpi_dev_inst *scpi, | |
271 | const char *command, GString **scpi_response) | |
272 | { | |
273 | int ret; | |
274 | GString *response; | |
275 | int space; | |
276 | gint64 timeout; | |
277 | ||
278 | /* Optionally send caller provided command. */ | |
279 | if (command) { | |
280 | if (scpi_send(scpi, command) != SR_OK) | |
281 | return SR_ERR; | |
282 | } | |
283 | ||
284 | /* Initiate SCPI read operation. */ | |
285 | if (sr_scpi_read_begin(scpi) != SR_OK) | |
286 | return SR_ERR; | |
287 | ||
288 | /* Keep reading until completion or until timeout. */ | |
289 | timeout = g_get_monotonic_time() + scpi->read_timeout_us; | |
290 | ||
291 | response = *scpi_response; | |
292 | ||
293 | while (!sr_scpi_read_complete(scpi)) { | |
294 | /* Resize the buffer when free space drops below a threshold. */ | |
295 | space = response->allocated_len - response->len; | |
296 | if (space < 128) { | |
297 | int oldlen = response->len; | |
298 | g_string_set_size(response, oldlen + 1024); | |
299 | g_string_set_size(response, oldlen); | |
300 | } | |
301 | ||
302 | /* Read another chunk of the response. */ | |
303 | ret = scpi_read_response(scpi, response, timeout); | |
304 | ||
305 | if (ret < 0) | |
306 | return ret; | |
307 | if (ret > 0) | |
308 | timeout = g_get_monotonic_time() + scpi->read_timeout_us; | |
309 | } | |
310 | ||
311 | return SR_OK; | |
312 | } | |
313 | ||
314 | SR_PRIV GSList *sr_scpi_scan(struct drv_context *drvc, GSList *options, | |
315 | struct sr_dev_inst *(*probe_device)(struct sr_scpi_dev_inst *scpi)) | |
316 | { | |
317 | GSList *resources, *l, *devices; | |
318 | struct sr_dev_inst *sdi; | |
319 | const char *resource, *conn; | |
320 | const char *serialcomm, *comm; | |
321 | gchar **res; | |
322 | unsigned i; | |
323 | ||
324 | resource = NULL; | |
325 | serialcomm = NULL; | |
326 | (void)sr_serial_extract_options(options, &resource, &serialcomm); | |
327 | ||
328 | devices = NULL; | |
329 | for (i = 0; i < ARRAY_SIZE(scpi_devs); i++) { | |
330 | if (resource && strcmp(resource, scpi_devs[i]->prefix) != 0) | |
331 | continue; | |
332 | if (!scpi_devs[i]->scan) | |
333 | continue; | |
334 | resources = scpi_devs[i]->scan(drvc); | |
335 | for (l = resources; l; l = l->next) { | |
336 | res = g_strsplit(l->data, ":", 2); | |
337 | if (!res[0]) { | |
338 | g_strfreev(res); | |
339 | continue; | |
340 | } | |
341 | conn = res[0]; | |
342 | comm = serialcomm ? : res[1]; | |
343 | sdi = sr_scpi_scan_resource(drvc, conn, comm, probe_device); | |
344 | if (sdi) { | |
345 | devices = g_slist_append(devices, sdi); | |
346 | sdi->connection_id = g_strdup(l->data); | |
347 | } | |
348 | g_strfreev(res); | |
349 | } | |
350 | g_slist_free_full(resources, g_free); | |
351 | } | |
352 | ||
353 | if (!devices && resource) { | |
354 | sdi = sr_scpi_scan_resource(drvc, resource, serialcomm, probe_device); | |
355 | if (sdi) | |
356 | devices = g_slist_append(NULL, sdi); | |
357 | } | |
358 | ||
359 | /* Tack a copy of the newly found devices onto the driver list. */ | |
360 | if (devices) | |
361 | drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices)); | |
362 | ||
363 | return devices; | |
364 | } | |
365 | ||
366 | SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(struct drv_context *drvc, | |
367 | const char *resource, const char *serialcomm) | |
368 | { | |
369 | struct sr_scpi_dev_inst *scpi = NULL; | |
370 | const struct sr_scpi_dev_inst *scpi_dev; | |
371 | gchar **params; | |
372 | unsigned i; | |
373 | ||
374 | for (i = 0; i < ARRAY_SIZE(scpi_devs); i++) { | |
375 | scpi_dev = scpi_devs[i]; | |
376 | if (!strncmp(resource, scpi_dev->prefix, strlen(scpi_dev->prefix))) { | |
377 | sr_dbg("Opening %s device %s.", scpi_dev->name, resource); | |
378 | scpi = g_malloc(sizeof(*scpi)); | |
379 | *scpi = *scpi_dev; | |
380 | scpi->priv = g_malloc0(scpi->priv_size); | |
381 | scpi->read_timeout_us = 1000 * 1000; | |
382 | params = g_strsplit(resource, "/", 0); | |
383 | if (scpi->dev_inst_new(scpi->priv, drvc, resource, | |
384 | params, serialcomm) != SR_OK) { | |
385 | sr_scpi_free(scpi); | |
386 | scpi = NULL; | |
387 | } | |
388 | g_strfreev(params); | |
389 | break; | |
390 | } | |
391 | } | |
392 | ||
393 | return scpi; | |
394 | } | |
395 | ||
396 | /** | |
397 | * Open SCPI device. | |
398 | * | |
399 | * @param scpi Previously initialized SCPI device structure. | |
400 | * | |
401 | * @return SR_OK on success, SR_ERR on failure. | |
402 | */ | |
403 | SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi) | |
404 | { | |
405 | g_mutex_init(&scpi->scpi_mutex); | |
406 | ||
407 | return scpi->open(scpi); | |
408 | } | |
409 | ||
410 | /** | |
411 | * Get the connection ID of the SCPI device. | |
412 | * | |
413 | * Callers must free the allocated memory regardless of the routine's | |
414 | * return code. See @ref g_free(). | |
415 | * | |
416 | * @param[in] scpi Previously initialized SCPI device structure. | |
417 | * @param[out] connection_id Pointer where to store the connection ID. | |
418 | * | |
419 | * @return SR_OK on success, SR_ERR on failure. | |
420 | */ | |
421 | SR_PRIV int sr_scpi_connection_id(struct sr_scpi_dev_inst *scpi, | |
422 | char **connection_id) | |
423 | { | |
424 | return scpi->connection_id(scpi, connection_id); | |
425 | } | |
426 | ||
427 | /** | |
428 | * Add an event source for an SCPI device. | |
429 | * | |
430 | * @param session The session to add the event source to. | |
431 | * @param scpi Previously initialized SCPI device structure. | |
432 | * @param events Events to check for. | |
433 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
434 | * @param cb Callback function to add. Must not be NULL. | |
435 | * @param cb_data Data for the callback function. Can be NULL. | |
436 | * | |
437 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
438 | * SR_ERR_MALLOC upon memory allocation errors. | |
439 | */ | |
440 | SR_PRIV int sr_scpi_source_add(struct sr_session *session, | |
441 | struct sr_scpi_dev_inst *scpi, int events, int timeout, | |
442 | sr_receive_data_callback cb, void *cb_data) | |
443 | { | |
444 | return scpi->source_add(session, scpi->priv, events, timeout, cb, cb_data); | |
445 | } | |
446 | ||
447 | /** | |
448 | * Remove event source for an SCPI device. | |
449 | * | |
450 | * @param session The session to remove the event source from. | |
451 | * @param scpi Previously initialized SCPI device structure. | |
452 | * | |
453 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
454 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
455 | * internal errors. | |
456 | */ | |
457 | SR_PRIV int sr_scpi_source_remove(struct sr_session *session, | |
458 | struct sr_scpi_dev_inst *scpi) | |
459 | { | |
460 | return scpi->source_remove(session, scpi->priv); | |
461 | } | |
462 | ||
463 | /** | |
464 | * Send a SCPI command. | |
465 | * | |
466 | * @param scpi Previously initialized SCPI device structure. | |
467 | * @param format Format string, to be followed by any necessary arguments. | |
468 | * | |
469 | * @return SR_OK on success, SR_ERR on failure. | |
470 | */ | |
471 | SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi, | |
472 | const char *format, ...) | |
473 | { | |
474 | va_list args; | |
475 | int ret; | |
476 | ||
477 | va_start(args, format); | |
478 | g_mutex_lock(&scpi->scpi_mutex); | |
479 | ret = scpi_send_variadic(scpi, format, args); | |
480 | g_mutex_unlock(&scpi->scpi_mutex); | |
481 | va_end(args); | |
482 | ||
483 | return ret; | |
484 | } | |
485 | ||
486 | /** | |
487 | * Send a SCPI command with a variadic argument list. | |
488 | * | |
489 | * @param scpi Previously initialized SCPI device structure. | |
490 | * @param format Format string. | |
491 | * @param args Argument list. | |
492 | * | |
493 | * @return SR_OK on success, SR_ERR on failure. | |
494 | */ | |
495 | SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi, | |
496 | const char *format, va_list args) | |
497 | { | |
498 | int ret; | |
499 | ||
500 | g_mutex_lock(&scpi->scpi_mutex); | |
501 | ret = scpi_send_variadic(scpi, format, args); | |
502 | g_mutex_unlock(&scpi->scpi_mutex); | |
503 | ||
504 | return ret; | |
505 | } | |
506 | ||
507 | /** | |
508 | * Begin receiving an SCPI reply. | |
509 | * | |
510 | * @param scpi Previously initialised SCPI device structure. | |
511 | * | |
512 | * @return SR_OK on success, SR_ERR on failure. | |
513 | */ | |
514 | SR_PRIV int sr_scpi_read_begin(struct sr_scpi_dev_inst *scpi) | |
515 | { | |
516 | return scpi->read_begin(scpi->priv); | |
517 | } | |
518 | ||
519 | /** | |
520 | * Read part of a response from SCPI device. | |
521 | * | |
522 | * @param scpi Previously initialised SCPI device structure. | |
523 | * @param buf Buffer to store result. | |
524 | * @param maxlen Maximum number of bytes to read. | |
525 | * | |
526 | * @return Number of bytes read, or SR_ERR upon failure. | |
527 | */ | |
528 | SR_PRIV int sr_scpi_read_data(struct sr_scpi_dev_inst *scpi, | |
529 | char *buf, int maxlen) | |
530 | { | |
531 | int ret; | |
532 | ||
533 | g_mutex_lock(&scpi->scpi_mutex); | |
534 | ret = scpi_read_data(scpi, buf, maxlen); | |
535 | g_mutex_unlock(&scpi->scpi_mutex); | |
536 | ||
537 | return ret; | |
538 | } | |
539 | ||
540 | /** | |
541 | * Send data to SCPI device. | |
542 | * | |
543 | * TODO: This is only implemented in TcpRaw, but never used. | |
544 | * TODO: Use Mutex at all? | |
545 | * | |
546 | * @param scpi Previously initialised SCPI device structure. | |
547 | * @param buf Buffer with data to send. | |
548 | * @param len Number of bytes to send. | |
549 | * | |
550 | * @return Number of bytes read, or SR_ERR upon failure. | |
551 | */ | |
552 | SR_PRIV int sr_scpi_write_data(struct sr_scpi_dev_inst *scpi, | |
553 | char *buf, int maxlen) | |
554 | { | |
555 | int ret; | |
556 | ||
557 | g_mutex_lock(&scpi->scpi_mutex); | |
558 | ret = scpi_write_data(scpi, buf, maxlen); | |
559 | g_mutex_unlock(&scpi->scpi_mutex); | |
560 | ||
561 | return ret; | |
562 | } | |
563 | ||
564 | /** | |
565 | * Check whether a complete SCPI response has been received. | |
566 | * | |
567 | * @param scpi Previously initialised SCPI device structure. | |
568 | * | |
569 | * @return 1 if complete, 0 otherwise. | |
570 | */ | |
571 | SR_PRIV int sr_scpi_read_complete(struct sr_scpi_dev_inst *scpi) | |
572 | { | |
573 | return scpi->read_complete(scpi->priv); | |
574 | } | |
575 | ||
576 | /** | |
577 | * Close SCPI device. | |
578 | * | |
579 | * @param scpi Previously initialized SCPI device structure. | |
580 | * | |
581 | * @return SR_OK on success, SR_ERR on failure. | |
582 | */ | |
583 | SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi) | |
584 | { | |
585 | int ret; | |
586 | ||
587 | g_mutex_lock(&scpi->scpi_mutex); | |
588 | ret = scpi->close(scpi); | |
589 | g_mutex_unlock(&scpi->scpi_mutex); | |
590 | g_mutex_clear(&scpi->scpi_mutex); | |
591 | ||
592 | return ret; | |
593 | } | |
594 | ||
595 | /** | |
596 | * Free SCPI device. | |
597 | * | |
598 | * @param scpi Previously initialized SCPI device structure. If NULL, | |
599 | * this function does nothing. | |
600 | */ | |
601 | SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi) | |
602 | { | |
603 | if (!scpi) | |
604 | return; | |
605 | ||
606 | scpi->free(scpi->priv); | |
607 | g_free(scpi->priv); | |
608 | g_free(scpi->actual_channel_name); | |
609 | g_free(scpi); | |
610 | } | |
611 | ||
612 | /** | |
613 | * Send a SCPI command, receive the reply and store the reply in scpi_response. | |
614 | * | |
615 | * Callers must free the allocated memory regardless of the routine's | |
616 | * return code. See @ref g_free(). | |
617 | * | |
618 | * @param[in] scpi Previously initialised SCPI device structure. | |
619 | * @param[in] command The SCPI command to send to the device (can be NULL). | |
620 | * @param[out] scpi_response Pointer where to store the SCPI response. | |
621 | * | |
622 | * @return SR_OK on success, SR_ERR* on failure. | |
623 | */ | |
624 | SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi, | |
625 | const char *command, char **scpi_response) | |
626 | { | |
627 | GString *response; | |
628 | ||
629 | *scpi_response = NULL; | |
630 | ||
631 | response = g_string_sized_new(1024); | |
632 | if (sr_scpi_get_data(scpi, command, &response) != SR_OK) { | |
633 | if (response) | |
634 | g_string_free(response, TRUE); | |
635 | return SR_ERR; | |
636 | } | |
637 | ||
638 | /* Get rid of trailing linefeed if present */ | |
639 | if (response->len >= 1 && response->str[response->len - 1] == '\n') | |
640 | g_string_truncate(response, response->len - 1); | |
641 | ||
642 | /* Get rid of trailing carriage return if present */ | |
643 | if (response->len >= 1 && response->str[response->len - 1] == '\r') | |
644 | g_string_truncate(response, response->len - 1); | |
645 | ||
646 | sr_spew("Got response: '%.70s', length %" G_GSIZE_FORMAT ".", | |
647 | response->str, response->len); | |
648 | ||
649 | *scpi_response = g_string_free(response, FALSE); | |
650 | ||
651 | return SR_OK; | |
652 | } | |
653 | ||
654 | /** | |
655 | * Do a non-blocking read of up to the allocated length, and | |
656 | * check if a timeout has occured. | |
657 | * | |
658 | * @param scpi Previously initialised SCPI device structure. | |
659 | * @param response Buffer to which the response is appended. | |
660 | * @param abs_timeout_us Absolute timeout in microseconds | |
661 | * | |
662 | * @return read length on success, SR_ERR* on failure. | |
663 | */ | |
664 | SR_PRIV int sr_scpi_read_response(struct sr_scpi_dev_inst *scpi, | |
665 | GString *response, gint64 abs_timeout_us) | |
666 | { | |
667 | int ret; | |
668 | ||
669 | g_mutex_lock(&scpi->scpi_mutex); | |
670 | ret = scpi_read_response(scpi, response, abs_timeout_us); | |
671 | g_mutex_unlock(&scpi->scpi_mutex); | |
672 | ||
673 | return ret; | |
674 | } | |
675 | ||
676 | SR_PRIV int sr_scpi_get_data(struct sr_scpi_dev_inst *scpi, | |
677 | const char *command, GString **scpi_response) | |
678 | { | |
679 | int ret; | |
680 | ||
681 | g_mutex_lock(&scpi->scpi_mutex); | |
682 | ret = scpi_get_data(scpi, command, scpi_response); | |
683 | g_mutex_unlock(&scpi->scpi_mutex); | |
684 | ||
685 | return ret; | |
686 | } | |
687 | ||
688 | /** | |
689 | * Send a SCPI command, read the reply, parse it as a bool value and store the | |
690 | * result in scpi_response. | |
691 | * | |
692 | * @param scpi Previously initialised SCPI device structure. | |
693 | * @param command The SCPI command to send to the device (can be NULL). | |
694 | * @param scpi_response Pointer where to store the parsed result. | |
695 | * | |
696 | * @return SR_OK on success, SR_ERR* on failure. | |
697 | */ | |
698 | SR_PRIV int sr_scpi_get_bool(struct sr_scpi_dev_inst *scpi, | |
699 | const char *command, gboolean *scpi_response) | |
700 | { | |
701 | int ret; | |
702 | char *response; | |
703 | ||
704 | response = NULL; | |
705 | ||
706 | ret = sr_scpi_get_string(scpi, command, &response); | |
707 | if (ret != SR_OK && !response) | |
708 | return ret; | |
709 | ||
710 | if (parse_strict_bool(response, scpi_response) == SR_OK) | |
711 | ret = SR_OK; | |
712 | else | |
713 | ret = SR_ERR_DATA; | |
714 | ||
715 | g_free(response); | |
716 | ||
717 | return ret; | |
718 | } | |
719 | ||
720 | /** | |
721 | * Send a SCPI command, read the reply, parse it as an integer and store the | |
722 | * result in scpi_response. | |
723 | * | |
724 | * @param scpi Previously initialised SCPI device structure. | |
725 | * @param command The SCPI command to send to the device (can be NULL). | |
726 | * @param scpi_response Pointer where to store the parsed result. | |
727 | * | |
728 | * @return SR_OK on success, SR_ERR* on failure. | |
729 | */ | |
730 | SR_PRIV int sr_scpi_get_int(struct sr_scpi_dev_inst *scpi, | |
731 | const char *command, int *scpi_response) | |
732 | { | |
733 | int ret; | |
734 | struct sr_rational ret_rational; | |
735 | char *response; | |
736 | ||
737 | response = NULL; | |
738 | ||
739 | ret = sr_scpi_get_string(scpi, command, &response); | |
740 | if (ret != SR_OK && !response) | |
741 | return ret; | |
742 | ||
743 | ret = sr_parse_rational(response, &ret_rational); | |
744 | if (ret == SR_OK && (ret_rational.p % ret_rational.q) == 0) { | |
745 | *scpi_response = ret_rational.p / ret_rational.q; | |
746 | } else { | |
747 | sr_dbg("get_int: non-integer rational=%" PRId64 "/%" PRIu64, | |
748 | ret_rational.p, ret_rational.q); | |
749 | ret = SR_ERR_DATA; | |
750 | } | |
751 | ||
752 | g_free(response); | |
753 | ||
754 | return ret; | |
755 | } | |
756 | ||
757 | /** | |
758 | * Send a SCPI command, read the reply, parse it as a float and store the | |
759 | * result in scpi_response. | |
760 | * | |
761 | * @param scpi Previously initialised SCPI device structure. | |
762 | * @param command The SCPI command to send to the device (can be NULL). | |
763 | * @param scpi_response Pointer where to store the parsed result. | |
764 | * | |
765 | * @return SR_OK on success, SR_ERR* on failure. | |
766 | */ | |
767 | SR_PRIV int sr_scpi_get_float(struct sr_scpi_dev_inst *scpi, | |
768 | const char *command, float *scpi_response) | |
769 | { | |
770 | int ret; | |
771 | char *response; | |
772 | ||
773 | response = NULL; | |
774 | ||
775 | ret = sr_scpi_get_string(scpi, command, &response); | |
776 | if (ret != SR_OK && !response) | |
777 | return ret; | |
778 | ||
779 | if (sr_atof_ascii(response, scpi_response) == SR_OK) | |
780 | ret = SR_OK; | |
781 | else | |
782 | ret = SR_ERR_DATA; | |
783 | ||
784 | g_free(response); | |
785 | ||
786 | return ret; | |
787 | } | |
788 | ||
789 | /** | |
790 | * Send a SCPI command, read the reply, parse it as a double and store the | |
791 | * result in scpi_response. | |
792 | * | |
793 | * @param scpi Previously initialised SCPI device structure. | |
794 | * @param command The SCPI command to send to the device (can be NULL). | |
795 | * @param scpi_response Pointer where to store the parsed result. | |
796 | * | |
797 | * @return SR_OK on success, SR_ERR* on failure. | |
798 | */ | |
799 | SR_PRIV int sr_scpi_get_double(struct sr_scpi_dev_inst *scpi, | |
800 | const char *command, double *scpi_response) | |
801 | { | |
802 | int ret; | |
803 | char *response; | |
804 | ||
805 | response = NULL; | |
806 | ||
807 | ret = sr_scpi_get_string(scpi, command, &response); | |
808 | if (ret != SR_OK && !response) | |
809 | return ret; | |
810 | ||
811 | if (sr_atod_ascii(response, scpi_response) == SR_OK) | |
812 | ret = SR_OK; | |
813 | else | |
814 | ret = SR_ERR_DATA; | |
815 | ||
816 | g_free(response); | |
817 | ||
818 | return ret; | |
819 | } | |
820 | ||
821 | /** | |
822 | * Send a SCPI *OPC? command, read the reply and return the result of the | |
823 | * command. | |
824 | * | |
825 | * @param scpi Previously initialised SCPI device structure. | |
826 | * | |
827 | * @return SR_OK on success, SR_ERR* on failure. | |
828 | */ | |
829 | SR_PRIV int sr_scpi_get_opc(struct sr_scpi_dev_inst *scpi) | |
830 | { | |
831 | unsigned int i; | |
832 | gboolean opc; | |
833 | ||
834 | for (i = 0; i < SCPI_READ_RETRIES; i++) { | |
835 | opc = FALSE; | |
836 | sr_scpi_get_bool(scpi, SCPI_CMD_OPC, &opc); | |
837 | if (opc) | |
838 | return SR_OK; | |
839 | g_usleep(SCPI_READ_RETRY_TIMEOUT_US); | |
840 | } | |
841 | ||
842 | return SR_ERR; | |
843 | } | |
844 | ||
845 | /** | |
846 | * Send a SCPI command, read the reply, parse it as comma separated list of | |
847 | * floats and store the as an result in scpi_response. | |
848 | * | |
849 | * Callers must free the allocated memory (unless it's NULL) regardless of | |
850 | * the routine's return code. See @ref g_array_free(). | |
851 | * | |
852 | * @param[in] scpi Previously initialised SCPI device structure. | |
853 | * @param[in] command The SCPI command to send to the device (can be NULL). | |
854 | * @param[out] scpi_response Pointer where to store the parsed result. | |
855 | * | |
856 | * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing | |
857 | * error or upon no response. | |
858 | */ | |
859 | SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi, | |
860 | const char *command, GArray **scpi_response) | |
861 | { | |
862 | int ret; | |
863 | float tmp; | |
864 | char *response; | |
865 | gchar **ptr, **tokens; | |
866 | size_t token_count; | |
867 | GArray *response_array; | |
868 | ||
869 | *scpi_response = NULL; | |
870 | ||
871 | response = NULL; | |
872 | ret = sr_scpi_get_string(scpi, command, &response); | |
873 | if (ret != SR_OK && !response) | |
874 | return ret; | |
875 | ||
876 | tokens = g_strsplit(response, ",", 0); | |
877 | token_count = g_strv_length(tokens); | |
878 | ||
879 | response_array = g_array_sized_new(TRUE, FALSE, | |
880 | sizeof(float), token_count + 1); | |
881 | ||
882 | ptr = tokens; | |
883 | while (*ptr) { | |
884 | ret = sr_atof_ascii(*ptr, &tmp); | |
885 | if (ret != SR_OK) { | |
886 | ret = SR_ERR_DATA; | |
887 | break; | |
888 | } | |
889 | response_array = g_array_append_val(response_array, tmp); | |
890 | ptr++; | |
891 | } | |
892 | g_strfreev(tokens); | |
893 | g_free(response); | |
894 | ||
895 | if (ret != SR_OK && response_array->len == 0) { | |
896 | g_array_free(response_array, TRUE); | |
897 | return SR_ERR_DATA; | |
898 | } | |
899 | ||
900 | *scpi_response = response_array; | |
901 | ||
902 | return ret; | |
903 | } | |
904 | ||
905 | /** | |
906 | * Send a SCPI command, read the reply, parse it as comma separated list of | |
907 | * unsigned 8 bit integers and store the as an result in scpi_response. | |
908 | * | |
909 | * Callers must free the allocated memory (unless it's NULL) regardless of | |
910 | * the routine's return code. See @ref g_array_free(). | |
911 | * | |
912 | * @param[in] scpi Previously initialised SCPI device structure. | |
913 | * @param[in] command The SCPI command to send to the device (can be NULL). | |
914 | * @param[out] scpi_response Pointer where to store the parsed result. | |
915 | * | |
916 | * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing | |
917 | * error or upon no response. | |
918 | */ | |
919 | SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi, | |
920 | const char *command, GArray **scpi_response) | |
921 | { | |
922 | int tmp, ret; | |
923 | char *response; | |
924 | gchar **ptr, **tokens; | |
925 | size_t token_count; | |
926 | GArray *response_array; | |
927 | ||
928 | *scpi_response = NULL; | |
929 | ||
930 | response = NULL; | |
931 | ret = sr_scpi_get_string(scpi, command, &response); | |
932 | if (ret != SR_OK && !response) | |
933 | return ret; | |
934 | ||
935 | tokens = g_strsplit(response, ",", 0); | |
936 | token_count = g_strv_length(tokens); | |
937 | ||
938 | response_array = g_array_sized_new(TRUE, FALSE, | |
939 | sizeof(uint8_t), token_count + 1); | |
940 | ||
941 | ptr = tokens; | |
942 | while (*ptr) { | |
943 | ret = sr_atoi(*ptr, &tmp); | |
944 | if (ret != SR_OK) { | |
945 | ret = SR_ERR_DATA; | |
946 | break; | |
947 | } | |
948 | response_array = g_array_append_val(response_array, tmp); | |
949 | ptr++; | |
950 | } | |
951 | g_strfreev(tokens); | |
952 | g_free(response); | |
953 | ||
954 | if (response_array->len == 0) { | |
955 | g_array_free(response_array, TRUE); | |
956 | return SR_ERR_DATA; | |
957 | } | |
958 | ||
959 | *scpi_response = response_array; | |
960 | ||
961 | return ret; | |
962 | } | |
963 | ||
964 | /** | |
965 | * Send a SCPI command, read the reply, parse it as binary data with a | |
966 | * "definite length block" header and store the as an result in scpi_response. | |
967 | * | |
968 | * Callers must free the allocated memory (unless it's NULL) regardless of | |
969 | * the routine's return code. See @ref g_byte_array_free(). | |
970 | * | |
971 | * @param[in] scpi Previously initialised SCPI device structure. | |
972 | * @param[in] command The SCPI command to send to the device (can be NULL). | |
973 | * @param[out] scpi_response Pointer where to store the parsed result. | |
974 | * | |
975 | * @return SR_OK upon successfully parsing all values, SR_ERR* upon a parsing | |
976 | * error or upon no response. | |
977 | */ | |
978 | SR_PRIV int sr_scpi_get_block(struct sr_scpi_dev_inst *scpi, | |
979 | const char *command, GByteArray **scpi_response) | |
980 | { | |
981 | int ret; | |
982 | GString* response; | |
983 | gsize oldlen; | |
984 | char buf[10]; | |
985 | long llen; | |
986 | long datalen; | |
987 | gint64 timeout; | |
988 | ||
989 | *scpi_response = NULL; | |
990 | ||
991 | g_mutex_lock(&scpi->scpi_mutex); | |
992 | ||
993 | if (command) | |
994 | if (scpi_send(scpi, command) != SR_OK) { | |
995 | g_mutex_unlock(&scpi->scpi_mutex); | |
996 | return SR_ERR; | |
997 | } | |
998 | ||
999 | if (sr_scpi_read_begin(scpi) != SR_OK) { | |
1000 | g_mutex_unlock(&scpi->scpi_mutex); | |
1001 | return SR_ERR; | |
1002 | } | |
1003 | ||
1004 | /* | |
1005 | * Assume an initial maximum length, optionally gets adjusted below. | |
1006 | * Prepare a NULL return value for when error paths will be taken. | |
1007 | */ | |
1008 | response = g_string_sized_new(1024); | |
1009 | ||
1010 | timeout = g_get_monotonic_time() + scpi->read_timeout_us; | |
1011 | ||
1012 | /* Get (the first chunk of) the response. */ | |
1013 | do { | |
1014 | ret = scpi_read_response(scpi, response, timeout); | |
1015 | if (ret < 0) { | |
1016 | g_mutex_unlock(&scpi->scpi_mutex); | |
1017 | g_string_free(response, TRUE); | |
1018 | return ret; | |
1019 | } | |
1020 | } while (response->len < 2); | |
1021 | ||
1022 | /* | |
1023 | * SCPI protocol data blocks are preceeded with a length spec. | |
1024 | * The length spec consists of a '#' marker, one digit which | |
1025 | * specifies the character count of the length spec, and the | |
1026 | * respective number of characters which specify the data block's | |
1027 | * length. Raw data bytes follow (thus one must no longer assume | |
1028 | * that the received input stream would be an ASCIIZ string). | |
1029 | * | |
1030 | * Get the data block length, and strip off the length spec from | |
1031 | * the input buffer, leaving just the data bytes. | |
1032 | */ | |
1033 | if (response->str[0] != '#') { | |
1034 | g_mutex_unlock(&scpi->scpi_mutex); | |
1035 | g_string_free(response, TRUE); | |
1036 | return SR_ERR_DATA; | |
1037 | } | |
1038 | buf[0] = response->str[1]; | |
1039 | buf[1] = '\0'; | |
1040 | ret = sr_atol(buf, &llen); | |
1041 | /* | |
1042 | * The form "#0..." is legal, and does not mean "empty response", | |
1043 | * but means that the number of data bytes is not known (or was | |
1044 | * not communicated) at this time. Instead the block ends at an | |
1045 | * "END MESSAGE" termination sequence. Which translates to active | |
1046 | * EOI while a text line termination is sent (CR or LF, and this | |
1047 | * text line termination is not part of the block's data value). | |
1048 | * Since this kind of #0... response is considered rare, and | |
1049 | * depends on specific support in physical transports underneath | |
1050 | * the SCPI layer, let's flag the condition and bail out with an | |
1051 | * error here, until it's found to be a genuine issue in the field. | |
1052 | * | |
1053 | * The SCPI 1999.0 specification (see page 220 and following in | |
1054 | * the "HCOPy" description) references IEEE 488.2, especially | |
1055 | * section 8.7.9 for DEFINITE LENGTH and section 8.7.10 for | |
1056 | * INDEFINITE LENGTH ARBITRARY BLOCK RESPONSE DATA. The latter | |
1057 | * with a leading "#0" length and a trailing "NL^END" marker. | |
1058 | */ | |
1059 | if (ret == SR_OK && !llen) { | |
1060 | sr_err("unsupported INDEFINITE LENGTH ARBITRARY BLOCK RESPONSE"); | |
1061 | ret = SR_ERR_NA; | |
1062 | } | |
1063 | if (ret != SR_OK) { | |
1064 | g_mutex_unlock(&scpi->scpi_mutex); | |
1065 | g_string_free(response, TRUE); | |
1066 | return ret; | |
1067 | } | |
1068 | ||
1069 | while (response->len < (unsigned long)(2 + llen)) { | |
1070 | ret = scpi_read_response(scpi, response, timeout); | |
1071 | if (ret < 0) { | |
1072 | g_mutex_unlock(&scpi->scpi_mutex); | |
1073 | g_string_free(response, TRUE); | |
1074 | return ret; | |
1075 | } | |
1076 | } | |
1077 | ||
1078 | memcpy(buf, &response->str[2], llen); | |
1079 | buf[llen] = '\0'; | |
1080 | ret = sr_atol(buf, &datalen); | |
1081 | if ((ret != SR_OK) || (datalen == 0)) { | |
1082 | g_mutex_unlock(&scpi->scpi_mutex); | |
1083 | g_string_free(response, TRUE); | |
1084 | return ret; | |
1085 | } | |
1086 | g_string_erase(response, 0, 2 + llen); | |
1087 | ||
1088 | /* | |
1089 | * Re-allocate the buffer size to the now known length | |
1090 | * and keep reading more chunks of response data. | |
1091 | */ | |
1092 | oldlen = response->len; | |
1093 | g_string_set_size(response, datalen); | |
1094 | g_string_set_size(response, oldlen); | |
1095 | ||
1096 | if (oldlen < (unsigned long)(datalen)) { | |
1097 | do { | |
1098 | oldlen = response->len; | |
1099 | ret = scpi_read_response(scpi, response, timeout); | |
1100 | ||
1101 | /* On timeout truncate the buffer and send the partial response | |
1102 | * instead of getting stuck on timeouts... | |
1103 | */ | |
1104 | if (ret == SR_ERR_TIMEOUT) { | |
1105 | datalen = oldlen; | |
1106 | break; | |
1107 | } | |
1108 | if (ret < 0) { | |
1109 | g_mutex_unlock(&scpi->scpi_mutex); | |
1110 | g_string_free(response, TRUE); | |
1111 | return ret; | |
1112 | } | |
1113 | if (ret > 0) | |
1114 | timeout = g_get_monotonic_time() + scpi->read_timeout_us; | |
1115 | } while (response->len < (unsigned long)(datalen)); | |
1116 | } | |
1117 | ||
1118 | g_mutex_unlock(&scpi->scpi_mutex); | |
1119 | ||
1120 | /* Convert received data to byte array. */ | |
1121 | *scpi_response = g_byte_array_new_take( | |
1122 | (guint8*)g_string_free(response, FALSE), datalen); | |
1123 | ||
1124 | return SR_OK; | |
1125 | } | |
1126 | ||
1127 | /** | |
1128 | * Send the *IDN? SCPI command, receive the reply, parse it and store the | |
1129 | * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer. | |
1130 | * | |
1131 | * Callers must free the allocated memory regardless of the routine's | |
1132 | * return code. See @ref sr_scpi_hw_info_free(). | |
1133 | * | |
1134 | * @param[in] scpi Previously initialised SCPI device structure. | |
1135 | * @param[out] scpi_response Pointer where to store the hw_info structure. | |
1136 | * | |
1137 | * @return SR_OK upon success, SR_ERR* on failure. | |
1138 | */ | |
1139 | SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi, | |
1140 | struct sr_scpi_hw_info **scpi_response) | |
1141 | { | |
1142 | int num_tokens, ret; | |
1143 | char *response; | |
1144 | gchar **tokens; | |
1145 | struct sr_scpi_hw_info *hw_info; | |
1146 | gchar *idn_substr; | |
1147 | ||
1148 | *scpi_response = NULL; | |
1149 | response = NULL; | |
1150 | tokens = NULL; | |
1151 | ||
1152 | ret = sr_scpi_get_string(scpi, SCPI_CMD_IDN, &response); | |
1153 | if (ret != SR_OK && !response) | |
1154 | return ret; | |
1155 | ||
1156 | /* | |
1157 | * The response to a '*IDN?' is specified by the SCPI spec. It contains | |
1158 | * a comma-separated list containing the manufacturer name, instrument | |
1159 | * model, serial number of the instrument and the firmware version. | |
1160 | * | |
1161 | * BEWARE! Although strictly speaking a smaller field count is invalid, | |
1162 | * this implementation also accepts IDN responses with one field less, | |
1163 | * and assumes that the serial number is missing. Some GWInstek DMMs | |
1164 | * were found to do this. Keep warning about this condition, which may | |
1165 | * need more consideration later. | |
1166 | */ | |
1167 | tokens = g_strsplit(response, ",", 0); | |
1168 | num_tokens = g_strv_length(tokens); | |
1169 | if (num_tokens < 3) { | |
1170 | sr_dbg("IDN response not according to spec: '%s'", response); | |
1171 | g_strfreev(tokens); | |
1172 | g_free(response); | |
1173 | return SR_ERR_DATA; | |
1174 | } | |
1175 | if (num_tokens < 4) { | |
1176 | sr_warn("Short IDN response, assume missing serial number."); | |
1177 | } | |
1178 | g_free(response); | |
1179 | ||
1180 | hw_info = g_malloc0(sizeof(*hw_info)); | |
1181 | ||
1182 | idn_substr = g_strstr_len(tokens[0], -1, "IDN "); | |
1183 | if (idn_substr == NULL) | |
1184 | hw_info->manufacturer = g_strstrip(g_strdup(tokens[0])); | |
1185 | else | |
1186 | hw_info->manufacturer = g_strstrip(g_strdup(idn_substr + 4)); | |
1187 | ||
1188 | hw_info->model = g_strstrip(g_strdup(tokens[1])); | |
1189 | if (num_tokens < 4) { | |
1190 | hw_info->serial_number = g_strdup("Unknown"); | |
1191 | hw_info->firmware_version = g_strstrip(g_strdup(tokens[2])); | |
1192 | } else { | |
1193 | hw_info->serial_number = g_strstrip(g_strdup(tokens[2])); | |
1194 | hw_info->firmware_version = g_strstrip(g_strdup(tokens[3])); | |
1195 | } | |
1196 | ||
1197 | g_strfreev(tokens); | |
1198 | ||
1199 | *scpi_response = hw_info; | |
1200 | ||
1201 | return SR_OK; | |
1202 | } | |
1203 | ||
1204 | /** | |
1205 | * Free a sr_scpi_hw_info struct. | |
1206 | * | |
1207 | * @param hw_info Pointer to the struct to free. If NULL, this | |
1208 | * function does nothing. | |
1209 | */ | |
1210 | SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info) | |
1211 | { | |
1212 | if (!hw_info) | |
1213 | return; | |
1214 | ||
1215 | g_free(hw_info->manufacturer); | |
1216 | g_free(hw_info->model); | |
1217 | g_free(hw_info->serial_number); | |
1218 | g_free(hw_info->firmware_version); | |
1219 | g_free(hw_info); | |
1220 | } | |
1221 | ||
1222 | /** | |
1223 | * Remove potentially enclosing pairs of quotes, un-escape content. | |
1224 | * This implementation modifies the caller's buffer when quotes are found | |
1225 | * and doubled quote characters need to get removed from the content. | |
1226 | * | |
1227 | * @param[in, out] s The SCPI string to check and un-quote. | |
1228 | * | |
1229 | * @return The start of the un-quoted string. | |
1230 | */ | |
1231 | SR_PRIV const char *sr_scpi_unquote_string(char *s) | |
1232 | { | |
1233 | size_t s_len; | |
1234 | char quotes[3]; | |
1235 | char *rdptr; | |
1236 | ||
1237 | /* Immediately bail out on invalid or short input. */ | |
1238 | if (!s || !*s) | |
1239 | return s; | |
1240 | s_len = strlen(s); | |
1241 | if (s_len < 2) | |
1242 | return s; | |
1243 | ||
1244 | /* Check for matching quote characters front and back. */ | |
1245 | if (s[0] != '\'' && s[0] != '"') | |
1246 | return s; | |
1247 | if (s[0] != s[s_len - 1]) | |
1248 | return s; | |
1249 | ||
1250 | /* Need to strip quotes, and un-double quote chars inside. */ | |
1251 | quotes[0] = quotes[1] = *s; | |
1252 | quotes[2] = '\0'; | |
1253 | s[s_len - 1] = '\0'; | |
1254 | s++; | |
1255 | rdptr = s; | |
1256 | while ((rdptr = strstr(rdptr, quotes)) != NULL) { | |
1257 | memmove(rdptr, rdptr + 1, strlen(rdptr)); | |
1258 | rdptr++; | |
1259 | } | |
1260 | ||
1261 | return s; | |
1262 | } | |
1263 | ||
1264 | SR_PRIV const char *sr_vendor_alias(const char *raw_vendor) | |
1265 | { | |
1266 | unsigned int i; | |
1267 | ||
1268 | for (i = 0; i < ARRAY_SIZE(scpi_vendors); i++) { | |
1269 | if (!g_ascii_strcasecmp(raw_vendor, scpi_vendors[i][0])) | |
1270 | return scpi_vendors[i][1]; | |
1271 | } | |
1272 | ||
1273 | return raw_vendor; | |
1274 | } | |
1275 | ||
1276 | SR_PRIV const char *sr_scpi_cmd_get(const struct scpi_command *cmdtable, | |
1277 | int command) | |
1278 | { | |
1279 | unsigned int i; | |
1280 | const char *cmd; | |
1281 | ||
1282 | if (!cmdtable) | |
1283 | return NULL; | |
1284 | ||
1285 | cmd = NULL; | |
1286 | for (i = 0; cmdtable[i].string; i++) { | |
1287 | if (cmdtable[i].command == command) { | |
1288 | cmd = cmdtable[i].string; | |
1289 | break; | |
1290 | } | |
1291 | } | |
1292 | ||
1293 | return cmd; | |
1294 | } | |
1295 | ||
1296 | SR_PRIV int sr_scpi_cmd(const struct sr_dev_inst *sdi, | |
1297 | const struct scpi_command *cmdtable, | |
1298 | int channel_command, const char *channel_name, | |
1299 | int command, ...) | |
1300 | { | |
1301 | struct sr_scpi_dev_inst *scpi; | |
1302 | va_list args; | |
1303 | int ret; | |
1304 | const char *channel_cmd; | |
1305 | const char *cmd; | |
1306 | ||
1307 | scpi = sdi->conn; | |
1308 | ||
1309 | if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) { | |
1310 | /* Device does not implement this command, that's OK. */ | |
1311 | return SR_OK; | |
1312 | } | |
1313 | ||
1314 | g_mutex_lock(&scpi->scpi_mutex); | |
1315 | ||
1316 | /* Select channel. */ | |
1317 | channel_cmd = sr_scpi_cmd_get(cmdtable, channel_command); | |
1318 | if (channel_cmd && channel_name && | |
1319 | g_strcmp0(channel_name, scpi->actual_channel_name)) { | |
1320 | sr_spew("sr_scpi_cmd(): new channel = %s", channel_name); | |
1321 | g_free(scpi->actual_channel_name); | |
1322 | scpi->actual_channel_name = g_strdup(channel_name); | |
1323 | ret = scpi_send(scpi, channel_cmd, channel_name); | |
1324 | if (ret != SR_OK) | |
1325 | return ret; | |
1326 | } | |
1327 | ||
1328 | va_start(args, command); | |
1329 | ret = scpi_send_variadic(scpi, cmd, args); | |
1330 | va_end(args); | |
1331 | ||
1332 | g_mutex_unlock(&scpi->scpi_mutex); | |
1333 | ||
1334 | return ret; | |
1335 | } | |
1336 | ||
1337 | SR_PRIV int sr_scpi_cmd_resp(const struct sr_dev_inst *sdi, | |
1338 | const struct scpi_command *cmdtable, | |
1339 | int channel_command, const char *channel_name, | |
1340 | GVariant **gvar, const GVariantType *gvtype, int command, ...) | |
1341 | { | |
1342 | struct sr_scpi_dev_inst *scpi; | |
1343 | va_list args; | |
1344 | const char *channel_cmd; | |
1345 | const char *cmd; | |
1346 | GString *response; | |
1347 | char *s; | |
1348 | gboolean b; | |
1349 | double d; | |
1350 | int ret; | |
1351 | ||
1352 | scpi = sdi->conn; | |
1353 | ||
1354 | if (!(cmd = sr_scpi_cmd_get(cmdtable, command))) { | |
1355 | /* Device does not implement this command. */ | |
1356 | return SR_ERR_NA; | |
1357 | } | |
1358 | ||
1359 | g_mutex_lock(&scpi->scpi_mutex); | |
1360 | ||
1361 | /* Select channel. */ | |
1362 | channel_cmd = sr_scpi_cmd_get(cmdtable, channel_command); | |
1363 | if (channel_cmd && channel_name && | |
1364 | g_strcmp0(channel_name, scpi->actual_channel_name)) { | |
1365 | sr_spew("sr_scpi_cmd_get(): new channel = %s", channel_name); | |
1366 | g_free(scpi->actual_channel_name); | |
1367 | scpi->actual_channel_name = g_strdup(channel_name); | |
1368 | ret = scpi_send(scpi, channel_cmd, channel_name); | |
1369 | if (ret != SR_OK) | |
1370 | return ret; | |
1371 | } | |
1372 | ||
1373 | va_start(args, command); | |
1374 | ret = scpi_send_variadic(scpi, cmd, args); | |
1375 | va_end(args); | |
1376 | if (ret != SR_OK) { | |
1377 | g_mutex_unlock(&scpi->scpi_mutex); | |
1378 | return ret; | |
1379 | } | |
1380 | ||
1381 | response = g_string_sized_new(1024); | |
1382 | ret = scpi_get_data(scpi, NULL, &response); | |
1383 | if (ret != SR_OK) { | |
1384 | g_mutex_unlock(&scpi->scpi_mutex); | |
1385 | if (response) | |
1386 | g_string_free(response, TRUE); | |
1387 | return ret; | |
1388 | } | |
1389 | ||
1390 | g_mutex_unlock(&scpi->scpi_mutex); | |
1391 | ||
1392 | /* Get rid of trailing linefeed if present */ | |
1393 | if (response->len >= 1 && response->str[response->len - 1] == '\n') | |
1394 | g_string_truncate(response, response->len - 1); | |
1395 | ||
1396 | /* Get rid of trailing carriage return if present */ | |
1397 | if (response->len >= 1 && response->str[response->len - 1] == '\r') | |
1398 | g_string_truncate(response, response->len - 1); | |
1399 | ||
1400 | s = g_string_free(response, FALSE); | |
1401 | ||
1402 | ret = SR_OK; | |
1403 | if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_BOOLEAN)) { | |
1404 | if ((ret = parse_strict_bool(s, &b)) == SR_OK) | |
1405 | *gvar = g_variant_new_boolean(b); | |
1406 | } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_DOUBLE)) { | |
1407 | if ((ret = sr_atod_ascii(s, &d)) == SR_OK) | |
1408 | *gvar = g_variant_new_double(d); | |
1409 | } else if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_STRING)) { | |
1410 | *gvar = g_variant_new_string(s); | |
1411 | } else { | |
1412 | sr_err("Unable to convert to desired GVariant type."); | |
1413 | ret = SR_ERR_NA; | |
1414 | } | |
1415 | ||
1416 | g_free(s); | |
1417 | ||
1418 | return ret; | |
1419 | } |