]>
Commit | Line | Data |
---|---|---|
2be182e6 BV |
1 | /* |
2 | * This file is part of the sigrok-cli project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.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 | ||
d486cbdd | 20 | #include <config.h> |
2be182e6 BV |
21 | #include <glib.h> |
22 | #include <glib/gstdio.h> | |
55de58a2 UH |
23 | #include <string.h> |
24 | #include <stdlib.h> | |
662a1e27 | 25 | #include "sigrok-cli.h" |
2be182e6 | 26 | |
2be182e6 BV |
27 | static uint64_t limit_samples = 0; |
28 | static uint64_t limit_frames = 0; | |
29 | ||
2be182e6 BV |
30 | #ifdef HAVE_SRD |
31 | extern struct srd_session *srd_sess; | |
32 | #endif | |
33 | ||
2be182e6 BV |
34 | static int set_limit_time(const struct sr_dev_inst *sdi) |
35 | { | |
36 | GVariant *gvar; | |
37 | uint64_t time_msec; | |
38 | uint64_t samplerate; | |
c6fa2b2e UH |
39 | struct sr_dev_driver *driver; |
40 | ||
41 | driver = sr_dev_inst_driver_get(sdi); | |
2be182e6 BV |
42 | |
43 | if (!(time_msec = sr_parse_timestring(opt_time))) { | |
44 | g_critical("Invalid time '%s'", opt_time); | |
45 | return SR_ERR; | |
46 | } | |
47 | ||
24bd9719 | 48 | if (config_key_has_cap(driver, sdi, NULL, SR_CONF_LIMIT_MSEC, SR_CONF_SET)) { |
2be182e6 BV |
49 | gvar = g_variant_new_uint64(time_msec); |
50 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) { | |
51 | g_critical("Failed to configure time limit."); | |
52 | return SR_ERR; | |
53 | } | |
24bd9719 BV |
54 | } else if (config_key_has_cap(driver, sdi, NULL, SR_CONF_SAMPLERATE, |
55 | SR_CONF_GET | SR_CONF_SET)) { | |
23c40b60 | 56 | /* Convert to samples based on the samplerate. */ |
c6fa2b2e | 57 | sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar); |
2be182e6 BV |
58 | samplerate = g_variant_get_uint64(gvar); |
59 | g_variant_unref(gvar); | |
60 | limit_samples = (samplerate) * time_msec / (uint64_t)1000; | |
61 | if (limit_samples == 0) { | |
62 | g_critical("Not enough time at this samplerate."); | |
63 | return SR_ERR; | |
64 | } | |
65 | gvar = g_variant_new_uint64(limit_samples); | |
66 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { | |
67 | g_critical("Failed to configure time-based sample limit."); | |
68 | return SR_ERR; | |
69 | } | |
70 | } else { | |
71 | g_critical("This device does not support time limits."); | |
72 | return SR_ERR; | |
73 | } | |
74 | ||
75 | return SR_OK; | |
76 | } | |
77 | ||
cbbea087 | 78 | const struct sr_output *setup_output_format(const struct sr_dev_inst *sdi, FILE **outfile) |
2be182e6 | 79 | { |
ad6520c4 | 80 | const struct sr_output_module *omod; |
7c6a0420 | 81 | const struct sr_option **options; |
ad6520c4 BV |
82 | const struct sr_output *o; |
83 | GHashTable *fmtargs, *fmtopts; | |
2be182e6 BV |
84 | char *fmtspec; |
85 | ||
2be182e6 | 86 | if (!opt_output_format) { |
fea5acab | 87 | if (opt_output_file) { |
5d7604d1 | 88 | opt_output_format = DEFAULT_OUTPUT_FORMAT_FILE; |
fea5acab | 89 | } else { |
5d7604d1 | 90 | opt_output_format = DEFAULT_OUTPUT_FORMAT_NOFILE; |
fea5acab | 91 | } |
2be182e6 BV |
92 | } |
93 | ||
94 | fmtargs = parse_generic_arg(opt_output_format, TRUE); | |
95 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); | |
6709a694 | 96 | if (!fmtspec) |
2be182e6 | 97 | g_critical("Invalid output format."); |
ad6520c4 | 98 | if (!(omod = sr_output_find(fmtspec))) |
7c6a0420 | 99 | g_critical("Unknown output module '%s'.", fmtspec); |
9216694f | 100 | g_hash_table_remove(fmtargs, "sigrok_key"); |
7c6a0420 BV |
101 | if ((options = sr_output_options_get(omod))) { |
102 | fmtopts = generic_arg_to_opt(options, fmtargs); | |
103 | sr_output_options_free(options); | |
ad6520c4 BV |
104 | } else |
105 | fmtopts = NULL; | |
5d7604d1 SA |
106 | o = sr_output_new(omod, fmtopts, sdi, opt_output_file); |
107 | ||
cbbea087 SA |
108 | if (opt_output_file) { |
109 | if (!sr_output_test_flag(omod, SR_OUTPUT_INTERNAL_IO_HANDLING)) | |
110 | *outfile = g_fopen(opt_output_file, "wb"); | |
111 | else | |
112 | *outfile = NULL; | |
113 | } else { | |
114 | *outfile = stdout; | |
115 | } | |
116 | ||
ad6520c4 BV |
117 | if (fmtopts) |
118 | g_hash_table_destroy(fmtopts); | |
2be182e6 BV |
119 | g_hash_table_destroy(fmtargs); |
120 | ||
6709a694 | 121 | return o; |
2be182e6 BV |
122 | } |
123 | ||
fb995521 UH |
124 | const struct sr_transform *setup_transform_module(const struct sr_dev_inst *sdi) |
125 | { | |
126 | const struct sr_transform_module *tmod; | |
127 | const struct sr_option **options; | |
128 | const struct sr_transform *t; | |
129 | GHashTable *fmtargs, *fmtopts; | |
fb995521 UH |
130 | char *fmtspec; |
131 | ||
132 | if (!opt_transform_module) | |
133 | opt_transform_module = "nop"; | |
134 | ||
135 | fmtargs = parse_generic_arg(opt_transform_module, TRUE); | |
136 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); | |
137 | if (!fmtspec) | |
138 | g_critical("Invalid transform module."); | |
139 | if (!(tmod = sr_transform_find(fmtspec))) | |
140 | g_critical("Unknown transform module '%s'.", fmtspec); | |
141 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
142 | if ((options = sr_transform_options_get(tmod))) { | |
143 | fmtopts = generic_arg_to_opt(options, fmtargs); | |
144 | sr_transform_options_free(options); | |
145 | } else | |
146 | fmtopts = NULL; | |
147 | t = sr_transform_new(tmod, fmtopts, sdi); | |
148 | if (fmtopts) | |
149 | g_hash_table_destroy(fmtopts); | |
150 | g_hash_table_destroy(fmtargs); | |
151 | ||
152 | return t; | |
153 | } | |
154 | ||
2be182e6 BV |
155 | void datafeed_in(const struct sr_dev_inst *sdi, |
156 | const struct sr_datafeed_packet *packet, void *cb_data) | |
157 | { | |
158 | const struct sr_datafeed_meta *meta; | |
159 | const struct sr_datafeed_logic *logic; | |
160 | const struct sr_datafeed_analog *analog; | |
4bf77ec6 | 161 | struct sr_session *session; |
2be182e6 | 162 | struct sr_config *src; |
ad6520c4 BV |
163 | static const struct sr_output *o = NULL; |
164 | static const struct sr_output *oa = NULL; | |
6ea663a7 BV |
165 | static uint64_t rcvd_samples_logic = 0; |
166 | static uint64_t rcvd_samples_analog = 0; | |
167 | static uint64_t samplerate = 0; | |
2be182e6 BV |
168 | static int triggered = 0; |
169 | static FILE *outfile = NULL; | |
fea5acab | 170 | GSList *l; |
2be182e6 | 171 | GString *out; |
8b65cdcc | 172 | GVariant *gvar; |
55de58a2 | 173 | uint64_t end_sample; |
667d4a18 | 174 | uint64_t input_len; |
c6fa2b2e UH |
175 | struct sr_dev_driver *driver; |
176 | ||
177 | driver = sr_dev_inst_driver_get(sdi); | |
2be182e6 | 178 | |
2be182e6 | 179 | /* If the first packet to come in isn't a header, don't even try. */ |
23c40b60 | 180 | if (packet->type != SR_DF_HEADER && !o) |
2be182e6 BV |
181 | return; |
182 | ||
4bf77ec6 | 183 | session = cb_data; |
2be182e6 BV |
184 | switch (packet->type) { |
185 | case SR_DF_HEADER: | |
4ed1cdea | 186 | g_debug("cli: Received SR_DF_HEADER."); |
cbbea087 | 187 | if (!(o = setup_output_format(sdi, &outfile))) |
e786e625 | 188 | g_critical("Failed to initialize output module."); |
2be182e6 | 189 | |
9216694f | 190 | /* Set up backup analog output module. */ |
5d7604d1 | 191 | oa = sr_output_new(sr_output_find("analog"), NULL, sdi, NULL); |
9216694f | 192 | |
6ea663a7 | 193 | rcvd_samples_logic = rcvd_samples_analog = 0; |
2be182e6 | 194 | |
24bd9719 | 195 | if (maybe_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, |
8b65cdcc BV |
196 | &gvar) == SR_OK) { |
197 | samplerate = g_variant_get_uint64(gvar); | |
198 | g_variant_unref(gvar); | |
199 | } | |
200 | ||
2be182e6 | 201 | #ifdef HAVE_SRD |
ea6d6dec | 202 | if (opt_pds) { |
8b65cdcc | 203 | if (samplerate) { |
2be182e6 BV |
204 | if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE, |
205 | g_variant_new_uint64(samplerate)) != SRD_OK) { | |
206 | g_critical("Failed to configure decode session."); | |
207 | break; | |
208 | } | |
209 | } | |
210 | if (srd_session_start(srd_sess) != SRD_OK) { | |
211 | g_critical("Failed to start decode session."); | |
212 | break; | |
213 | } | |
214 | } | |
215 | #endif | |
216 | break; | |
217 | ||
218 | case SR_DF_META: | |
4ed1cdea | 219 | g_debug("cli: Received SR_DF_META."); |
2be182e6 BV |
220 | meta = packet->payload; |
221 | for (l = meta->config; l; l = l->next) { | |
222 | src = l->data; | |
223 | switch (src->key) { | |
224 | case SR_CONF_SAMPLERATE: | |
225 | samplerate = g_variant_get_uint64(src->data); | |
4ed1cdea | 226 | g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate); |
2be182e6 BV |
227 | #ifdef HAVE_SRD |
228 | if (opt_pds) { | |
229 | if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE, | |
230 | g_variant_new_uint64(samplerate)) != SRD_OK) { | |
231 | g_critical("Failed to pass samplerate to decoder."); | |
232 | } | |
233 | } | |
234 | #endif | |
235 | break; | |
236 | case SR_CONF_SAMPLE_INTERVAL: | |
237 | samplerate = g_variant_get_uint64(src->data); | |
4ed1cdea | 238 | g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate); |
2be182e6 BV |
239 | break; |
240 | default: | |
241 | /* Unknown metadata is not an error. */ | |
242 | break; | |
243 | } | |
244 | } | |
245 | break; | |
246 | ||
247 | case SR_DF_TRIGGER: | |
4ed1cdea | 248 | g_debug("cli: Received SR_DF_TRIGGER."); |
2be182e6 BV |
249 | triggered = 1; |
250 | break; | |
251 | ||
252 | case SR_DF_LOGIC: | |
253 | logic = packet->payload; | |
4ed1cdea UH |
254 | g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).", |
255 | logic->length, logic->unitsize); | |
2be182e6 BV |
256 | if (logic->length == 0) |
257 | break; | |
258 | ||
259 | /* Don't store any samples until triggered. */ | |
260 | if (opt_wait_trigger && !triggered) | |
261 | break; | |
262 | ||
6ea663a7 | 263 | if (limit_samples && rcvd_samples_logic >= limit_samples) |
2be182e6 BV |
264 | break; |
265 | ||
6ea663a7 | 266 | end_sample = rcvd_samples_logic + logic->length / logic->unitsize; |
8667d3c2 DE |
267 | /* Cut off last packet according to the sample limit. */ |
268 | if (limit_samples && end_sample > limit_samples) | |
269 | end_sample = limit_samples; | |
6ea663a7 | 270 | input_len = (end_sample - rcvd_samples_logic) * logic->unitsize; |
2be182e6 | 271 | |
fea5acab | 272 | if (opt_pds) { |
2be182e6 | 273 | #ifdef HAVE_SRD |
fea5acab | 274 | if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample, |
ee639fb4 | 275 | logic->data, input_len, logic->unitsize) != SRD_OK) |
fea5acab | 276 | sr_session_stop(session); |
2be182e6 | 277 | #endif |
2be182e6 | 278 | } |
2be182e6 | 279 | |
6ea663a7 | 280 | rcvd_samples_logic = end_sample; |
2be182e6 BV |
281 | break; |
282 | ||
283 | case SR_DF_ANALOG: | |
284 | analog = packet->payload; | |
4ed1cdea | 285 | g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples); |
2be182e6 BV |
286 | if (analog->num_samples == 0) |
287 | break; | |
288 | ||
6ea663a7 | 289 | if (limit_samples && rcvd_samples_analog >= limit_samples) |
2be182e6 BV |
290 | break; |
291 | ||
6ea663a7 | 292 | rcvd_samples_analog += analog->num_samples; |
2be182e6 BV |
293 | break; |
294 | ||
295 | case SR_DF_FRAME_BEGIN: | |
4ed1cdea | 296 | g_debug("cli: Received SR_DF_FRAME_BEGIN."); |
2be182e6 BV |
297 | break; |
298 | ||
299 | case SR_DF_FRAME_END: | |
4ed1cdea | 300 | g_debug("cli: Received SR_DF_FRAME_END."); |
2be182e6 BV |
301 | break; |
302 | ||
303 | default: | |
304 | break; | |
305 | } | |
306 | ||
cbbea087 | 307 | if (o && !opt_pds) { |
9216694f | 308 | if (sr_output_send(o, packet, &out) == SR_OK) { |
fea5acab BV |
309 | if (!out || (out->len == 0 |
310 | && !opt_output_format | |
9216694f | 311 | && packet->type == SR_DF_ANALOG)) { |
f0f54487 UH |
312 | /* |
313 | * The user didn't specify an output module, | |
314 | * but needs to see this analog data. | |
315 | */ | |
9216694f BV |
316 | sr_output_send(oa, packet, &out); |
317 | } | |
cbbea087 | 318 | if (outfile && out && out->len > 0) { |
9216694f BV |
319 | fwrite(out->str, 1, out->len, outfile); |
320 | fflush(outfile); | |
321 | } | |
322 | if (out) | |
323 | g_string_free(out, TRUE); | |
2be182e6 BV |
324 | } |
325 | } | |
326 | ||
f0f54487 UH |
327 | /* |
328 | * SR_DF_END needs to be handled after the output module's receive() | |
329 | * is called, so it can properly clean up that module. | |
330 | */ | |
2be182e6 | 331 | if (packet->type == SR_DF_END) { |
4ed1cdea | 332 | g_debug("cli: Received SR_DF_END."); |
2be182e6 | 333 | |
5d7604d1 | 334 | if (o) |
6709a694 | 335 | sr_output_free(o); |
2be182e6 BV |
336 | o = NULL; |
337 | ||
9216694f BV |
338 | sr_output_free(oa); |
339 | oa = NULL; | |
340 | ||
2be182e6 BV |
341 | if (outfile && outfile != stdout) |
342 | fclose(outfile); | |
343 | ||
6ea663a7 BV |
344 | if (limit_samples) { |
345 | if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples) | |
346 | g_warning("Device only sent %" PRIu64 " samples.", | |
347 | rcvd_samples_logic); | |
348 | else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples) | |
349 | g_warning("Device only sent %" PRIu64 " samples.", | |
350 | rcvd_samples_analog); | |
2be182e6 BV |
351 | } |
352 | } | |
353 | ||
354 | } | |
355 | ||
ad54a1a6 | 356 | int opt_to_gvar(char *key, char *value, struct sr_config *src) |
2be182e6 | 357 | { |
e9505599 | 358 | const struct sr_key_info *srci, *srmqi; |
426d0cda | 359 | double tmp_double, dlow, dhigh; |
e9505599 BV |
360 | uint64_t tmp_u64, p, q, low, high, mqflags; |
361 | uint32_t mq; | |
84b39e3e | 362 | GVariant *rational[2], *range[2], *gtup[2]; |
e4588937 | 363 | GVariantBuilder *vbl; |
2be182e6 | 364 | gboolean tmp_bool; |
e4588937 | 365 | gchar **keyval; |
e9505599 | 366 | int ret, i; |
2be182e6 | 367 | |
e4e4b472 | 368 | if (!(srci = sr_key_info_name_get(SR_KEY_CONFIG, key))) { |
ad54a1a6 BV |
369 | g_critical("Unknown device option '%s'.", (char *) key); |
370 | return -1; | |
371 | } | |
372 | src->key = srci->key; | |
2be182e6 | 373 | |
23c40b60 | 374 | if ((!value || strlen(value) == 0) && |
ad54a1a6 BV |
375 | (srci->datatype != SR_T_BOOL)) { |
376 | g_critical("Option '%s' needs a value.", (char *)key); | |
377 | return -1; | |
378 | } | |
379 | ||
380 | ret = 0; | |
381 | switch (srci->datatype) { | |
382 | case SR_T_UINT64: | |
383 | ret = sr_parse_sizestring(value, &tmp_u64); | |
384 | if (ret != 0) | |
2be182e6 | 385 | break; |
ad54a1a6 BV |
386 | src->data = g_variant_new_uint64(tmp_u64); |
387 | break; | |
388 | case SR_T_INT32: | |
389 | ret = sr_parse_sizestring(value, &tmp_u64); | |
390 | if (ret != 0) | |
2be182e6 | 391 | break; |
ad54a1a6 BV |
392 | src->data = g_variant_new_int32(tmp_u64); |
393 | break; | |
9db40e9f | 394 | case SR_T_STRING: |
ad54a1a6 BV |
395 | src->data = g_variant_new_string(value); |
396 | break; | |
397 | case SR_T_BOOL: | |
398 | if (!value) | |
399 | tmp_bool = TRUE; | |
400 | else | |
401 | tmp_bool = sr_parse_boolstring(value); | |
402 | src->data = g_variant_new_boolean(tmp_bool); | |
403 | break; | |
404 | case SR_T_FLOAT: | |
405 | tmp_double = strtof(value, NULL); | |
406 | src->data = g_variant_new_double(tmp_double); | |
407 | break; | |
408 | case SR_T_RATIONAL_PERIOD: | |
409 | if ((ret = sr_parse_period(value, &p, &q)) != SR_OK) | |
2be182e6 | 410 | break; |
ad54a1a6 BV |
411 | rational[0] = g_variant_new_uint64(p); |
412 | rational[1] = g_variant_new_uint64(q); | |
413 | src->data = g_variant_new_tuple(rational, 2); | |
414 | break; | |
415 | case SR_T_RATIONAL_VOLT: | |
416 | if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK) | |
2be182e6 | 417 | break; |
ad54a1a6 BV |
418 | rational[0] = g_variant_new_uint64(p); |
419 | rational[1] = g_variant_new_uint64(q); | |
420 | src->data = g_variant_new_tuple(rational, 2); | |
421 | break; | |
422 | case SR_T_UINT64_RANGE: | |
423 | if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) { | |
424 | ret = -1; | |
2be182e6 | 425 | break; |
ad54a1a6 BV |
426 | } else { |
427 | range[0] = g_variant_new_uint64(low); | |
428 | range[1] = g_variant_new_uint64(high); | |
429 | src->data = g_variant_new_tuple(range, 2); | |
2be182e6 | 430 | } |
ad54a1a6 | 431 | break; |
426d0cda BV |
432 | case SR_T_DOUBLE_RANGE: |
433 | if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) { | |
434 | ret = -1; | |
435 | break; | |
436 | } else { | |
437 | range[0] = g_variant_new_double(dlow); | |
438 | range[1] = g_variant_new_double(dhigh); | |
439 | src->data = g_variant_new_tuple(range, 2); | |
440 | } | |
441 | break; | |
e4588937 BG |
442 | case SR_T_KEYVALUE: |
443 | /* Expects the argument to be in the form of key=value. */ | |
444 | keyval = g_strsplit(value, "=", 2); | |
445 | if (!keyval[0] || !keyval[1]) { | |
446 | g_strfreev(keyval); | |
447 | ret = -1; | |
448 | break; | |
449 | } else { | |
450 | vbl = g_variant_builder_new(G_VARIANT_TYPE_DICTIONARY); | |
451 | g_variant_builder_add(vbl, "{ss}", | |
452 | keyval[0], keyval[1]); | |
453 | src->data = g_variant_builder_end(vbl); | |
454 | g_strfreev(keyval); | |
455 | } | |
456 | break; | |
84b39e3e | 457 | case SR_T_MQ: |
e9505599 BV |
458 | /* |
459 | Argument is MQ id e.g. ("voltage") optionally followed by one | |
460 | or more /<mqflag> e.g. "/ac". | |
461 | */ | |
462 | keyval = g_strsplit(value, "/", 0); | |
463 | if (!keyval[0] || !(srmqi = sr_key_info_name_get(SR_KEY_MQ, keyval[0]))) { | |
464 | g_strfreev(keyval); | |
465 | ret = -1; | |
466 | break; | |
467 | } | |
468 | mq = srmqi->key; | |
469 | mqflags = 0; | |
470 | for (i = 1; keyval[i]; i++) { | |
471 | if (!(srmqi = sr_key_info_name_get(SR_KEY_MQFLAGS, keyval[i]))) { | |
472 | ret = -1; | |
473 | break; | |
474 | } | |
475 | mqflags |= srmqi->key; | |
476 | } | |
477 | g_strfreev(keyval); | |
478 | if (ret != -1) { | |
e9505599 BV |
479 | gtup[0] = g_variant_new_uint32(mq); |
480 | gtup[1] = g_variant_new_uint64(mqflags); | |
84b39e3e | 481 | src->data = g_variant_new_tuple(gtup, 2); |
e9505599 BV |
482 | } |
483 | break; | |
ad54a1a6 | 484 | default: |
b262d8f2 BG |
485 | g_critical("Unknown data type specified for option '%s' " |
486 | "(driver implementation bug?).", key); | |
ad54a1a6 BV |
487 | ret = -1; |
488 | } | |
489 | ||
27d310f0 BG |
490 | if (ret < 0) |
491 | g_critical("Invalid value: '%s' for option '%s'", value, key); | |
492 | ||
ad54a1a6 BV |
493 | return ret; |
494 | } | |
495 | ||
496 | int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args) | |
497 | { | |
498 | struct sr_config src; | |
ca50f4b3 | 499 | struct sr_channel_group *cg; |
ad54a1a6 BV |
500 | GHashTableIter iter; |
501 | gpointer key, value; | |
502 | int ret; | |
503 | ||
504 | g_hash_table_iter_init(&iter, args); | |
505 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
506 | if ((ret = opt_to_gvar(key, value, &src)) != 0) | |
507 | return ret; | |
ca50f4b3 | 508 | cg = select_channel_group(sdi); |
24bd9719 BV |
509 | if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg, |
510 | src.key, src.data)) != SR_OK) { | |
28b0b84e BG |
511 | g_critical("Failed to set device option '%s': %s.", |
512 | (char *)key, sr_strerror(ret)); | |
2be182e6 BV |
513 | return ret; |
514 | } | |
515 | } | |
516 | ||
517 | return SR_OK; | |
518 | } | |
519 | ||
520 | void run_session(void) | |
521 | { | |
ac3dcd3e | 522 | GSList *devices, *real_devices, *sd; |
2be182e6 BV |
523 | GHashTable *devargs; |
524 | GVariant *gvar; | |
4bf77ec6 BV |
525 | struct sr_session *session; |
526 | struct sr_trigger *trigger; | |
2be182e6 | 527 | struct sr_dev_inst *sdi; |
02c65935 | 528 | uint64_t min_samples, max_samples; |
ac3dcd3e PZ |
529 | gsize n_elements, i; |
530 | const uint32_t *dev_opts; | |
531 | int is_demo_dev; | |
c6fa2b2e | 532 | struct sr_dev_driver *driver; |
fb995521 | 533 | const struct sr_transform *t; |
2be182e6 BV |
534 | |
535 | devices = device_scan(); | |
536 | if (!devices) { | |
537 | g_critical("No devices found."); | |
538 | return; | |
539 | } | |
ac3dcd3e PZ |
540 | |
541 | real_devices = NULL; | |
542 | for (sd = devices; sd; sd = sd->next) { | |
543 | sdi = sd->data; | |
544 | ||
c6fa2b2e UH |
545 | driver = sr_dev_inst_driver_get(sdi); |
546 | ||
547 | if (sr_config_list(driver, sdi, NULL, SR_CONF_DEVICE_OPTIONS, &gvar) != SR_OK) { | |
24bd9719 | 548 | g_critical("Failed to query list device options."); |
ac3dcd3e PZ |
549 | return; |
550 | } | |
551 | ||
552 | dev_opts = g_variant_get_fixed_array(gvar, &n_elements, sizeof(uint32_t)); | |
553 | ||
554 | is_demo_dev = 0; | |
555 | for (i = 0; i < n_elements; i++) { | |
556 | if (dev_opts[i] == SR_CONF_DEMO_DEV) | |
557 | is_demo_dev = 1; | |
558 | } | |
559 | ||
560 | g_variant_unref(gvar); | |
561 | ||
562 | if (!is_demo_dev) | |
563 | real_devices = g_slist_append(real_devices, sdi); | |
564 | } | |
565 | ||
2be182e6 | 566 | if (g_slist_length(devices) > 1) { |
ac3dcd3e PZ |
567 | if (g_slist_length(real_devices) != 1) { |
568 | g_critical("sigrok-cli only supports one device for capturing."); | |
569 | return; | |
570 | } else { | |
571 | /* We only have one non-demo device. */ | |
572 | g_slist_free(devices); | |
573 | devices = real_devices; | |
574 | real_devices = NULL; | |
575 | } | |
2be182e6 | 576 | } |
ac3dcd3e | 577 | |
2be182e6 | 578 | sdi = devices->data; |
b4eece7c | 579 | g_slist_free(devices); |
ac3dcd3e | 580 | g_slist_free(real_devices); |
2be182e6 | 581 | |
3d24ca2d | 582 | sr_session_new(sr_ctx, &session); |
4bf77ec6 | 583 | sr_session_datafeed_callback_add(session, datafeed_in, NULL); |
2be182e6 BV |
584 | |
585 | if (sr_dev_open(sdi) != SR_OK) { | |
586 | g_critical("Failed to open device."); | |
587 | return; | |
588 | } | |
589 | ||
4bf77ec6 | 590 | if (sr_session_dev_add(session, sdi) != SR_OK) { |
2be182e6 | 591 | g_critical("Failed to add device to session."); |
4bf77ec6 | 592 | sr_session_destroy(session); |
2be182e6 BV |
593 | return; |
594 | } | |
595 | ||
596 | if (opt_config) { | |
597 | if ((devargs = parse_generic_arg(opt_config, FALSE))) { | |
598 | if (set_dev_options(sdi, devargs) != SR_OK) | |
599 | return; | |
600 | g_hash_table_destroy(devargs); | |
601 | } | |
602 | } | |
603 | ||
029d73fe UH |
604 | if (select_channels(sdi) != SR_OK) { |
605 | g_critical("Failed to set channels."); | |
4bf77ec6 | 606 | sr_session_destroy(session); |
2be182e6 BV |
607 | return; |
608 | } | |
609 | ||
15a14bff | 610 | trigger = NULL; |
2be182e6 | 611 | if (opt_triggers) { |
4bf77ec6 BV |
612 | if (!parse_triggerstring(sdi, opt_triggers, &trigger)) { |
613 | sr_session_destroy(session); | |
614 | return; | |
615 | } | |
616 | if (sr_session_trigger_set(session, trigger) != SR_OK) { | |
617 | sr_session_destroy(session); | |
2be182e6 BV |
618 | return; |
619 | } | |
2be182e6 BV |
620 | } |
621 | ||
622 | if (opt_continuous) { | |
623 | if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) { | |
624 | g_critical("This device does not support continuous sampling."); | |
4bf77ec6 | 625 | sr_session_destroy(session); |
2be182e6 BV |
626 | return; |
627 | } | |
628 | } | |
629 | ||
630 | if (opt_time) { | |
631 | if (set_limit_time(sdi) != SR_OK) { | |
4bf77ec6 | 632 | sr_session_destroy(session); |
2be182e6 BV |
633 | return; |
634 | } | |
635 | } | |
636 | ||
637 | if (opt_samples) { | |
638 | if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) { | |
639 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
4bf77ec6 | 640 | sr_session_destroy(session); |
2be182e6 BV |
641 | return; |
642 | } | |
24bd9719 BV |
643 | if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES, |
644 | &gvar) == SR_OK) { | |
f0f54487 UH |
645 | /* |
646 | * The device has no compression, or compression is turned | |
647 | * off, and publishes its sample memory size. | |
648 | */ | |
02c65935 | 649 | g_variant_get(gvar, "(tt)", &min_samples, &max_samples); |
be781321 | 650 | g_variant_unref(gvar); |
02c65935 BV |
651 | if (limit_samples < min_samples) { |
652 | g_critical("The device stores at least %"PRIu64 | |
653 | " samples with the current settings.", min_samples); | |
654 | } | |
c7a5cb12 BV |
655 | if (limit_samples > max_samples) { |
656 | g_critical("The device can store only %"PRIu64 | |
657 | " samples with the current settings.", max_samples); | |
658 | } | |
659 | } | |
2be182e6 | 660 | gvar = g_variant_new_uint64(limit_samples); |
24bd9719 | 661 | if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { |
2be182e6 | 662 | g_critical("Failed to configure sample limit."); |
4bf77ec6 | 663 | sr_session_destroy(session); |
2be182e6 BV |
664 | return; |
665 | } | |
666 | } | |
667 | ||
668 | if (opt_frames) { | |
669 | if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) { | |
670 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
4bf77ec6 | 671 | sr_session_destroy(session); |
2be182e6 BV |
672 | return; |
673 | } | |
674 | gvar = g_variant_new_uint64(limit_frames); | |
24bd9719 | 675 | if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) { |
2be182e6 | 676 | g_critical("Failed to configure frame limit."); |
4bf77ec6 | 677 | sr_session_destroy(session); |
2be182e6 BV |
678 | return; |
679 | } | |
680 | } | |
681 | ||
fb995521 UH |
682 | if (!(t = setup_transform_module(sdi))) |
683 | g_critical("Failed to initialize transform module."); | |
684 | ||
4bf77ec6 | 685 | if (sr_session_start(session) != SR_OK) { |
2be182e6 | 686 | g_critical("Failed to start session."); |
4bf77ec6 | 687 | sr_session_destroy(session); |
2be182e6 BV |
688 | return; |
689 | } | |
690 | ||
691 | if (opt_continuous) | |
4bf77ec6 | 692 | add_anykey(session); |
2be182e6 | 693 | |
4bf77ec6 | 694 | sr_session_run(session); |
2be182e6 BV |
695 | |
696 | if (opt_continuous) | |
697 | clear_anykey(); | |
698 | ||
15a14bff BV |
699 | if (trigger) |
700 | sr_trigger_free(trigger); | |
701 | ||
4bf77ec6 BV |
702 | sr_session_datafeed_callback_remove_all(session); |
703 | sr_session_destroy(session); | |
2be182e6 BV |
704 | |
705 | } |