]>
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 | */ | |
d5976d8b | 48 | static int parse_strict_bool(const char *str, gboolean *ret) |
aa1e3b40 DJ |
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)) { | |
aa1e3b40 DJ |
59 | *ret = TRUE; |
60 | return SR_OK; | |
aa1e3b40 DJ |
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)) { | |
aa1e3b40 DJ |
67 | *ret = FALSE; |
68 | return SR_OK; | |
69 | } | |
70 | ||
71 | return SR_ERR; | |
72 | } | |
73 | ||
23f43dff ML |
74 | /** |
75 | * Open SCPI device. | |
76 | * | |
77 | * @param scpi Previously initialized SCPI device structure. | |
78 | * | |
79 | * @return SR_OK on success, SR_ERR on failure. | |
80 | */ | |
81 | SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi) | |
82 | { | |
83 | return scpi->open(scpi->priv); | |
84 | } | |
85 | ||
86 | /** | |
87 | * Add an event source for an SCPI device. | |
88 | * | |
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. | |
94 | * | |
95 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
96 | * SR_ERR_MALLOC upon memory allocation errors. | |
97 | */ | |
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) | |
100 | { | |
101 | return scpi->source_add(scpi->priv, events, timeout, cb, cb_data); | |
102 | } | |
103 | ||
104 | /** | |
105 | * Remove event source for an SCPI device. | |
106 | * | |
107 | * @param scpi Previously initialized SCPI device structure. | |
108 | * | |
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 | |
111 | * internal errors. | |
112 | */ | |
113 | SR_PRIV int sr_scpi_source_remove(struct sr_scpi_dev_inst *scpi) | |
114 | { | |
115 | return scpi->source_remove(scpi->priv); | |
116 | } | |
117 | ||
7b9d7320 DJ |
118 | /** |
119 | * Send a SCPI command. | |
120 | * | |
23f43dff | 121 | * @param scpi Previously initialized SCPI device structure. |
504f40a5 | 122 | * @param format Format string, to be followed by any necessary arguments. |
7b9d7320 DJ |
123 | * |
124 | * @return SR_OK on success, SR_ERR on failure. | |
125 | */ | |
23f43dff | 126 | SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi, |
504f40a5 | 127 | const char *format, ...) |
7b9d7320 | 128 | { |
87c41083 ML |
129 | va_list args; |
130 | int ret; | |
131 | ||
132 | va_start(args, format); | |
133 | ret = sr_scpi_send_variadic(scpi, format, args); | |
134 | va_end(args); | |
135 | ||
136 | return ret; | |
137 | } | |
138 | ||
139 | /** | |
140 | * Send a SCPI command with a variadic argument list. | |
141 | * | |
142 | * @param scpi Previously initialized SCPI device structure. | |
143 | * @param format Format string. | |
144 | * @param args Argument list. | |
145 | * | |
146 | * @return SR_OK on success, SR_ERR on failure. | |
147 | */ | |
148 | SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi, | |
149 | const char *format, va_list args) | |
150 | { | |
151 | va_list args_copy; | |
504f40a5 ML |
152 | char *buf; |
153 | int len, ret; | |
154 | ||
504f40a5 | 155 | /* Get length of buffer required. */ |
87c41083 ML |
156 | va_copy(args_copy, args); |
157 | len = vsnprintf(NULL, 0, format, args_copy); | |
158 | va_end(args_copy); | |
504f40a5 ML |
159 | |
160 | /* Allocate buffer and write out command. */ | |
161 | buf = g_malloc(len + 1); | |
87c41083 | 162 | vsprintf(buf, format, args); |
504f40a5 ML |
163 | |
164 | /* Send command. */ | |
165 | ret = scpi->send(scpi->priv, buf); | |
166 | ||
167 | /* Free command buffer. */ | |
168 | g_free(buf); | |
169 | ||
170 | return ret; | |
23f43dff | 171 | } |
7b9d7320 | 172 | |
23f43dff ML |
173 | /** |
174 | * Receive an SCPI reply and store the reply in scpi_response. | |
175 | * | |
176 | * @param scpi Previously initialised SCPI device structure. | |
177 | * @param scpi_response Pointer where to store the SCPI response. | |
178 | * | |
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 | |
182 | * an incomplete. | |
183 | */ | |
184 | SR_PRIV int sr_scpi_receive(struct sr_scpi_dev_inst *scpi, | |
185 | char **scpi_response) | |
186 | { | |
187 | return scpi->receive(scpi->priv, scpi_response); | |
188 | } | |
7b9d7320 | 189 | |
a1ff9c18 ML |
190 | /** |
191 | * Read part of a response from SCPI device. | |
192 | * | |
193 | * @param scpi Previously initialised SCPI device structure. | |
194 | * @param buf Buffer to store result. | |
195 | * @param maxlen Maximum number of bytes to read. | |
196 | * | |
197 | * @return Number of bytes read, or SR_ERR upon failure. | |
198 | */ | |
199 | SR_PRIV int sr_scpi_read(struct sr_scpi_dev_inst *scpi, | |
200 | char *buf, int maxlen) | |
201 | { | |
202 | return scpi->read(scpi->priv, buf, maxlen); | |
203 | } | |
204 | ||
23f43dff ML |
205 | /** |
206 | * Close SCPI device. | |
207 | * | |
208 | * @param scpi Previously initialized SCPI device structure. | |
209 | * | |
210 | * @return SR_OK on success, SR_ERR on failure. | |
211 | */ | |
212 | SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi) | |
213 | { | |
214 | return scpi->close(scpi->priv); | |
215 | } | |
7b9d7320 | 216 | |
23f43dff ML |
217 | /** |
218 | * Free SCPI device. | |
219 | * | |
220 | * @param scpi Previously initialized SCPI device structure. | |
221 | * | |
222 | * @return SR_OK on success, SR_ERR on failure. | |
223 | */ | |
224 | SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi) | |
225 | { | |
226 | scpi->free(scpi->priv); | |
227 | g_free(scpi); | |
7b9d7320 DJ |
228 | } |
229 | ||
230 | /** | |
231 | * Send a SCPI command, receive the reply and store the reply in scpi_response. | |
232 | * | |
23f43dff | 233 | * @param scpi Previously initialised SCPI device structure. |
7b9d7320 | 234 | * @param command The SCPI command to send to the device (can be NULL). |
d5976d8b | 235 | * @param scpi_response Pointer where to store the SCPI response. |
7b9d7320 | 236 | * |
d5976d8b UH |
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 | |
240 | * an incomplete. | |
7b9d7320 | 241 | */ |
23f43dff | 242 | SR_PRIV int sr_scpi_get_string(struct sr_scpi_dev_inst *scpi, |
d5976d8b | 243 | const char *command, char **scpi_response) |
7b9d7320 | 244 | { |
7b9d7320 | 245 | if (command) |
23f43dff | 246 | if (sr_scpi_send(scpi, command) != SR_OK) |
7b9d7320 DJ |
247 | return SR_ERR; |
248 | ||
23f43dff | 249 | return sr_scpi_receive(scpi, scpi_response); |
d730f70e DJ |
250 | } |
251 | ||
252 | /** | |
253 | * Send a SCPI command, read the reply, parse it as a bool value and store the | |
254 | * result in scpi_response. | |
255 | * | |
23f43dff | 256 | * @param scpi Previously initialised SCPI device structure. |
d730f70e DJ |
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. | |
259 | * | |
260 | * @return SR_OK on success, SR_ERR on failure. | |
261 | */ | |
23f43dff | 262 | SR_PRIV int sr_scpi_get_bool(struct sr_scpi_dev_inst *scpi, |
d730f70e DJ |
263 | const char *command, gboolean *scpi_response) |
264 | { | |
265 | int ret; | |
266 | char *response; | |
267 | ||
268 | response = NULL; | |
269 | ||
23f43dff | 270 | if (sr_scpi_get_string(scpi, command, &response) != SR_OK) |
d730f70e DJ |
271 | if (!response) |
272 | return SR_ERR; | |
273 | ||
d5976d8b | 274 | if (parse_strict_bool(response, scpi_response) == SR_OK) |
d730f70e DJ |
275 | ret = SR_OK; |
276 | else | |
277 | ret = SR_ERR; | |
278 | ||
279 | g_free(response); | |
280 | ||
281 | return ret; | |
282 | } | |
283 | ||
284 | /** | |
285 | * Send a SCPI command, read the reply, parse it as an integer and store the | |
286 | * result in scpi_response. | |
287 | * | |
23f43dff | 288 | * @param scpi Previously initialised SCPI device structure. |
d730f70e DJ |
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. | |
291 | * | |
292 | * @return SR_OK on success, SR_ERR on failure. | |
293 | */ | |
23f43dff | 294 | SR_PRIV int sr_scpi_get_int(struct sr_scpi_dev_inst *scpi, |
d5976d8b | 295 | const char *command, int *scpi_response) |
d730f70e DJ |
296 | { |
297 | int ret; | |
298 | char *response; | |
299 | ||
300 | response = NULL; | |
301 | ||
23f43dff | 302 | if (sr_scpi_get_string(scpi, command, &response) != SR_OK) |
d730f70e DJ |
303 | if (!response) |
304 | return SR_ERR; | |
305 | ||
306 | if (sr_atoi(response, scpi_response) == SR_OK) | |
307 | ret = SR_OK; | |
308 | else | |
309 | ret = SR_ERR; | |
310 | ||
311 | g_free(response); | |
312 | ||
313 | return ret; | |
314 | } | |
315 | ||
316 | /** | |
317 | * Send a SCPI command, read the reply, parse it as a float and store the | |
318 | * result in scpi_response. | |
319 | * | |
23f43dff | 320 | * @param scpi Previously initialised SCPI device structure. |
d730f70e DJ |
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. | |
323 | * | |
324 | * @return SR_OK on success, SR_ERR on failure. | |
325 | */ | |
23f43dff | 326 | SR_PRIV int sr_scpi_get_float(struct sr_scpi_dev_inst *scpi, |
d730f70e DJ |
327 | const char *command, float *scpi_response) |
328 | { | |
329 | int ret; | |
330 | char *response; | |
331 | ||
332 | response = NULL; | |
333 | ||
23f43dff | 334 | if (sr_scpi_get_string(scpi, command, &response) != SR_OK) |
d730f70e DJ |
335 | if (!response) |
336 | return SR_ERR; | |
337 | ||
338 | if (sr_atof(response, scpi_response) == SR_OK) | |
339 | ret = SR_OK; | |
340 | else | |
341 | ret = SR_ERR; | |
342 | ||
343 | g_free(response); | |
344 | ||
345 | return ret; | |
346 | } | |
347 | ||
348 | /** | |
349 | * Send a SCPI command, read the reply, parse it as a double and store the | |
350 | * result in scpi_response. | |
351 | * | |
23f43dff | 352 | * @param scpi Previously initialised SCPI device structure. |
d730f70e DJ |
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. | |
355 | * | |
356 | * @return SR_OK on success, SR_ERR on failure. | |
357 | */ | |
23f43dff | 358 | SR_PRIV int sr_scpi_get_double(struct sr_scpi_dev_inst *scpi, |
d5976d8b | 359 | const char *command, double *scpi_response) |
d730f70e DJ |
360 | { |
361 | int ret; | |
362 | char *response; | |
363 | ||
364 | response = NULL; | |
365 | ||
23f43dff | 366 | if (sr_scpi_get_string(scpi, command, &response) != SR_OK) |
d730f70e DJ |
367 | if (!response) |
368 | return SR_ERR; | |
369 | ||
370 | if (sr_atod(response, scpi_response) == SR_OK) | |
371 | ret = SR_OK; | |
372 | else | |
373 | ret = SR_ERR; | |
374 | ||
375 | g_free(response); | |
376 | ||
7b9d7320 DJ |
377 | return ret; |
378 | } | |
379 | ||
f5922ade DJ |
380 | /** |
381 | * Send a SCPI *OPC? command, read the reply and return the result of the | |
382 | * command. | |
383 | * | |
23f43dff | 384 | * @param scpi Previously initialised SCPI device structure. |
f5922ade DJ |
385 | * |
386 | * @return SR_OK on success, SR_ERR on failure. | |
387 | */ | |
23f43dff | 388 | SR_PRIV int sr_scpi_get_opc(struct sr_scpi_dev_inst *scpi) |
f5922ade DJ |
389 | { |
390 | unsigned int i; | |
391 | gboolean opc; | |
392 | ||
393 | for (i = 0; i < SCPI_READ_RETRIES; ++i) { | |
23f43dff | 394 | sr_scpi_get_bool(scpi, SCPI_CMD_OPC, &opc); |
f5922ade DJ |
395 | if (opc) |
396 | return SR_OK; | |
f5922ade DJ |
397 | g_usleep(SCPI_READ_RETRY_TIMEOUT); |
398 | } | |
399 | ||
400 | return SR_ERR; | |
401 | } | |
402 | ||
8acbb89a DJ |
403 | /** |
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. | |
406 | * | |
23f43dff | 407 | * @param scpi Previously initialised SCPI device structure. |
8acbb89a DJ |
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. | |
410 | * | |
411 | * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing | |
d5976d8b UH |
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 | |
414 | * parsing error. | |
8acbb89a | 415 | */ |
23f43dff | 416 | SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi, |
d5976d8b | 417 | const char *command, GArray **scpi_response) |
8acbb89a DJ |
418 | { |
419 | int ret; | |
420 | float tmp; | |
421 | char *response; | |
d5976d8b | 422 | gchar **ptr, **tokens; |
8acbb89a DJ |
423 | GArray *response_array; |
424 | ||
425 | ret = SR_OK; | |
426 | response = NULL; | |
427 | tokens = NULL; | |
428 | ||
23f43dff | 429 | if (sr_scpi_get_string(scpi, command, &response) != SR_OK) |
8acbb89a DJ |
430 | if (!response) |
431 | return SR_ERR; | |
432 | ||
433 | tokens = g_strsplit(response, ",", 0); | |
434 | ptr = tokens; | |
435 | ||
436 | response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256); | |
437 | ||
d5976d8b | 438 | while (*ptr) { |
8acbb89a DJ |
439 | if (sr_atof(*ptr, &tmp) == SR_OK) |
440 | response_array = g_array_append_val(response_array, | |
441 | tmp); | |
442 | else | |
443 | ret = SR_ERR; | |
444 | ||
445 | ptr++; | |
446 | } | |
447 | g_strfreev(tokens); | |
448 | g_free(response); | |
449 | ||
450 | if (ret == SR_ERR && response_array->len == 0) { | |
451 | g_array_free(response_array, TRUE); | |
452 | *scpi_response = NULL; | |
453 | return SR_ERR; | |
454 | } | |
455 | ||
456 | *scpi_response = response_array; | |
457 | ||
1a323dd8 DJ |
458 | return ret; |
459 | } | |
460 | ||
461 | /** | |
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. | |
464 | * | |
23f43dff | 465 | * @param scpi Previously initialised SCPI device structure. |
1a323dd8 DJ |
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. | |
468 | * | |
469 | * @return SR_OK upon successfully parsing all values, SR_ERR upon a parsing | |
d5976d8b UH |
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 | |
472 | * parsing error. | |
1a323dd8 | 473 | */ |
23f43dff | 474 | SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi, |
d5976d8b | 475 | const char *command, GArray **scpi_response) |
1a323dd8 | 476 | { |
d5976d8b | 477 | int tmp, ret; |
1a323dd8 | 478 | char *response; |
d5976d8b | 479 | gchar **ptr, **tokens; |
1a323dd8 DJ |
480 | GArray *response_array; |
481 | ||
482 | ret = SR_OK; | |
483 | response = NULL; | |
484 | tokens = NULL; | |
485 | ||
23f43dff | 486 | if (sr_scpi_get_string(scpi, command, &response) != SR_OK) |
1a323dd8 DJ |
487 | if (!response) |
488 | return SR_ERR; | |
489 | ||
490 | tokens = g_strsplit(response, ",", 0); | |
491 | ptr = tokens; | |
492 | ||
493 | response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256); | |
494 | ||
d5976d8b | 495 | while (*ptr) { |
1a323dd8 DJ |
496 | if (sr_atoi(*ptr, &tmp) == SR_OK) |
497 | response_array = g_array_append_val(response_array, | |
498 | tmp); | |
499 | else | |
500 | ret = SR_ERR; | |
501 | ||
502 | ptr++; | |
503 | } | |
504 | g_strfreev(tokens); | |
505 | g_free(response); | |
506 | ||
507 | if (response_array->len == 0) { | |
508 | g_array_free(response_array, TRUE); | |
509 | *scpi_response = NULL; | |
510 | return SR_ERR; | |
511 | } | |
512 | ||
513 | *scpi_response = response_array; | |
514 | ||
8acbb89a DJ |
515 | return ret; |
516 | } | |
517 | ||
7b9d7320 DJ |
518 | /** |
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. | |
521 | * | |
d5976d8b UH |
522 | * The hw_info structure must be freed by the caller via sr_scpi_hw_info_free(). |
523 | * | |
23f43dff | 524 | * @param scpi Previously initialised SCPI device structure. |
7b9d7320 DJ |
525 | * @param scpi_response Pointer where to store the hw_info structure. |
526 | * | |
527 | * @return SR_OK upon success, SR_ERR on failure. | |
7b9d7320 | 528 | */ |
23f43dff | 529 | SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi, |
7b9d7320 DJ |
530 | struct sr_scpi_hw_info **scpi_response) |
531 | { | |
532 | int num_tokens; | |
533 | char *response; | |
534 | gchar **tokens; | |
7b9d7320 DJ |
535 | struct sr_scpi_hw_info *hw_info; |
536 | ||
537 | response = NULL; | |
538 | tokens = NULL; | |
539 | ||
23f43dff | 540 | if (sr_scpi_get_string(scpi, SCPI_CMD_IDN, &response) != SR_OK) |
7b9d7320 DJ |
541 | if (!response) |
542 | return SR_ERR; | |
543 | ||
544 | /* | |
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. | |
548 | */ | |
549 | tokens = g_strsplit(response, ",", 0); | |
550 | ||
551 | for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++); | |
552 | ||
553 | if (num_tokens != 4) { | |
d5976d8b | 554 | sr_dbg("IDN response not according to spec: %80.s.", response); |
7b9d7320 DJ |
555 | g_strfreev(tokens); |
556 | g_free(response); | |
557 | return SR_ERR; | |
558 | } | |
559 | g_free(response); | |
560 | ||
561 | hw_info = g_try_malloc(sizeof(struct sr_scpi_hw_info)); | |
562 | if (!hw_info) { | |
563 | g_strfreev(tokens); | |
564 | return SR_ERR_MALLOC; | |
565 | } | |
566 | ||
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]); | |
571 | ||
572 | g_strfreev(tokens); | |
573 | ||
574 | *scpi_response = hw_info; | |
575 | ||
576 | return SR_OK; | |
577 | } | |
578 | ||
579 | /** | |
580 | * Free a sr_scpi_hw_info struct. | |
581 | * | |
582 | * @param hw_info Pointer to the struct to free. | |
583 | * | |
584 | * This function is safe to call with a NULL pointer. | |
585 | */ | |
586 | SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info) | |
587 | { | |
588 | if (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); | |
593 | g_free(hw_info); | |
594 | } | |
595 | } |