]>
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 | ||
20fb52e0 | 20 | #include "sigrok-cli.h" |
2be182e6 BV |
21 | #include <glib.h> |
22 | #include <glib/gstdio.h> | |
55de58a2 UH |
23 | #include <string.h> |
24 | #include <stdlib.h> | |
2be182e6 | 25 | |
2be182e6 BV |
26 | static uint64_t limit_samples = 0; |
27 | static uint64_t limit_frames = 0; | |
fea5acab | 28 | static char *srzip_and_filename = NULL; |
2be182e6 | 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)) { | |
2be182e6 | 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 | ||
ad6520c4 | 78 | const struct sr_output *setup_output_format(const struct sr_dev_inst *sdi) |
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; | |
fea5acab | 84 | int size; |
2be182e6 BV |
85 | char *fmtspec; |
86 | ||
2be182e6 | 87 | if (!opt_output_format) { |
fea5acab BV |
88 | if (opt_output_file) { |
89 | size = strlen(opt_output_file) + 32; | |
90 | srzip_and_filename = g_malloc(size); | |
91 | snprintf(srzip_and_filename, size, "srzip:filename=%s", opt_output_file); | |
92 | opt_output_format = srzip_and_filename; | |
93 | } else { | |
94 | opt_output_format = DEFAULT_OUTPUT_FORMAT; | |
95 | } | |
2be182e6 BV |
96 | } |
97 | ||
98 | fmtargs = parse_generic_arg(opt_output_format, TRUE); | |
99 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); | |
6709a694 | 100 | if (!fmtspec) |
2be182e6 | 101 | g_critical("Invalid output format."); |
ad6520c4 | 102 | if (!(omod = sr_output_find(fmtspec))) |
7c6a0420 | 103 | g_critical("Unknown output module '%s'.", fmtspec); |
9216694f | 104 | g_hash_table_remove(fmtargs, "sigrok_key"); |
7c6a0420 BV |
105 | if ((options = sr_output_options_get(omod))) { |
106 | fmtopts = generic_arg_to_opt(options, fmtargs); | |
107 | sr_output_options_free(options); | |
ad6520c4 BV |
108 | } else |
109 | fmtopts = NULL; | |
110 | o = sr_output_new(omod, fmtopts, sdi); | |
111 | if (fmtopts) | |
112 | g_hash_table_destroy(fmtopts); | |
2be182e6 BV |
113 | g_hash_table_destroy(fmtargs); |
114 | ||
6709a694 | 115 | return o; |
2be182e6 BV |
116 | } |
117 | ||
fb995521 UH |
118 | const struct sr_transform *setup_transform_module(const struct sr_dev_inst *sdi) |
119 | { | |
120 | const struct sr_transform_module *tmod; | |
121 | const struct sr_option **options; | |
122 | const struct sr_transform *t; | |
123 | GHashTable *fmtargs, *fmtopts; | |
124 | int size; | |
125 | char *fmtspec; | |
126 | ||
127 | if (!opt_transform_module) | |
128 | opt_transform_module = "nop"; | |
129 | ||
130 | fmtargs = parse_generic_arg(opt_transform_module, TRUE); | |
131 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); | |
132 | if (!fmtspec) | |
133 | g_critical("Invalid transform module."); | |
134 | if (!(tmod = sr_transform_find(fmtspec))) | |
135 | g_critical("Unknown transform module '%s'.", fmtspec); | |
136 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
137 | if ((options = sr_transform_options_get(tmod))) { | |
138 | fmtopts = generic_arg_to_opt(options, fmtargs); | |
139 | sr_transform_options_free(options); | |
140 | } else | |
141 | fmtopts = NULL; | |
142 | t = sr_transform_new(tmod, fmtopts, sdi); | |
143 | if (fmtopts) | |
144 | g_hash_table_destroy(fmtopts); | |
145 | g_hash_table_destroy(fmtargs); | |
146 | ||
147 | return t; | |
148 | } | |
149 | ||
2be182e6 BV |
150 | void datafeed_in(const struct sr_dev_inst *sdi, |
151 | const struct sr_datafeed_packet *packet, void *cb_data) | |
152 | { | |
153 | const struct sr_datafeed_meta *meta; | |
154 | const struct sr_datafeed_logic *logic; | |
155 | const struct sr_datafeed_analog *analog; | |
4bf77ec6 | 156 | struct sr_session *session; |
2be182e6 | 157 | struct sr_config *src; |
ad6520c4 BV |
158 | static const struct sr_output *o = NULL; |
159 | static const struct sr_output *oa = NULL; | |
6ea663a7 BV |
160 | static uint64_t rcvd_samples_logic = 0; |
161 | static uint64_t rcvd_samples_analog = 0; | |
162 | static uint64_t samplerate = 0; | |
2be182e6 BV |
163 | static int triggered = 0; |
164 | static FILE *outfile = NULL; | |
fea5acab | 165 | GSList *l; |
2be182e6 | 166 | GString *out; |
8b65cdcc | 167 | GVariant *gvar; |
55de58a2 | 168 | uint64_t end_sample; |
667d4a18 | 169 | uint64_t input_len; |
c6fa2b2e UH |
170 | struct sr_dev_driver *driver; |
171 | ||
172 | driver = sr_dev_inst_driver_get(sdi); | |
2be182e6 | 173 | |
2be182e6 BV |
174 | /* If the first packet to come in isn't a header, don't even try. */ |
175 | if (packet->type != SR_DF_HEADER && o == NULL) | |
176 | return; | |
177 | ||
4bf77ec6 | 178 | session = cb_data; |
2be182e6 BV |
179 | switch (packet->type) { |
180 | case SR_DF_HEADER: | |
4ed1cdea | 181 | g_debug("cli: Received SR_DF_HEADER."); |
e786e625 BV |
182 | if (!(o = setup_output_format(sdi))) |
183 | g_critical("Failed to initialize output module."); | |
2be182e6 | 184 | |
9216694f | 185 | /* Set up backup analog output module. */ |
ad6520c4 | 186 | oa = sr_output_new(sr_output_find("analog"), NULL, sdi); |
9216694f | 187 | |
fea5acab BV |
188 | if (opt_output_file) |
189 | outfile = g_fopen(opt_output_file, "wb"); | |
190 | else | |
191 | outfile = stdout; | |
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 BV |
274 | if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample, |
275 | logic->data, input_len) != SRD_OK) | |
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 | ||
f4ffd032 | 307 | if (o && outfile && !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 BV |
311 | && packet->type == SR_DF_ANALOG)) { |
312 | /* The user didn't specify an output module, | |
313 | * but needs to see this analog data. */ | |
314 | sr_output_send(oa, packet, &out); | |
315 | } | |
316 | if (out && out->len > 0) { | |
317 | fwrite(out->str, 1, out->len, outfile); | |
318 | fflush(outfile); | |
319 | } | |
320 | if (out) | |
321 | g_string_free(out, TRUE); | |
2be182e6 BV |
322 | } |
323 | } | |
324 | ||
325 | /* SR_DF_END needs to be handled after the output module's receive() | |
6ea663a7 | 326 | * is called, so it can properly clean up that module. */ |
2be182e6 | 327 | if (packet->type == SR_DF_END) { |
4ed1cdea | 328 | g_debug("cli: Received SR_DF_END."); |
2be182e6 | 329 | |
fea5acab | 330 | if (o) { |
6709a694 | 331 | sr_output_free(o); |
fea5acab BV |
332 | if (srzip_and_filename) |
333 | g_free(srzip_and_filename); | |
334 | } | |
2be182e6 BV |
335 | o = NULL; |
336 | ||
9216694f BV |
337 | sr_output_free(oa); |
338 | oa = NULL; | |
339 | ||
2be182e6 BV |
340 | if (outfile && outfile != stdout) |
341 | fclose(outfile); | |
342 | ||
6ea663a7 BV |
343 | if (limit_samples) { |
344 | if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples) | |
345 | g_warning("Device only sent %" PRIu64 " samples.", | |
346 | rcvd_samples_logic); | |
347 | else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples) | |
348 | g_warning("Device only sent %" PRIu64 " samples.", | |
349 | rcvd_samples_analog); | |
2be182e6 BV |
350 | } |
351 | } | |
352 | ||
353 | } | |
354 | ||
ad54a1a6 | 355 | int opt_to_gvar(char *key, char *value, struct sr_config *src) |
2be182e6 BV |
356 | { |
357 | const struct sr_config_info *srci; | |
426d0cda | 358 | double tmp_double, dlow, dhigh; |
2be182e6 | 359 | uint64_t tmp_u64, p, q, low, high; |
ad54a1a6 | 360 | GVariant *rational[2], *range[2]; |
e4588937 | 361 | GVariantBuilder *vbl; |
2be182e6 | 362 | gboolean tmp_bool; |
e4588937 | 363 | gchar **keyval; |
ad54a1a6 | 364 | int ret; |
2be182e6 | 365 | |
ad54a1a6 BV |
366 | if (!(srci = sr_config_info_name_get(key))) { |
367 | g_critical("Unknown device option '%s'.", (char *) key); | |
368 | return -1; | |
369 | } | |
370 | src->key = srci->key; | |
2be182e6 | 371 | |
27d310f0 | 372 | if ((value == NULL || strlen(value) == 0) && |
ad54a1a6 BV |
373 | (srci->datatype != SR_T_BOOL)) { |
374 | g_critical("Option '%s' needs a value.", (char *)key); | |
375 | return -1; | |
376 | } | |
377 | ||
378 | ret = 0; | |
379 | switch (srci->datatype) { | |
380 | case SR_T_UINT64: | |
381 | ret = sr_parse_sizestring(value, &tmp_u64); | |
382 | if (ret != 0) | |
2be182e6 | 383 | break; |
ad54a1a6 BV |
384 | src->data = g_variant_new_uint64(tmp_u64); |
385 | break; | |
386 | case SR_T_INT32: | |
387 | ret = sr_parse_sizestring(value, &tmp_u64); | |
388 | if (ret != 0) | |
2be182e6 | 389 | break; |
ad54a1a6 BV |
390 | src->data = g_variant_new_int32(tmp_u64); |
391 | break; | |
9db40e9f | 392 | case SR_T_STRING: |
ad54a1a6 BV |
393 | src->data = g_variant_new_string(value); |
394 | break; | |
395 | case SR_T_BOOL: | |
396 | if (!value) | |
397 | tmp_bool = TRUE; | |
398 | else | |
399 | tmp_bool = sr_parse_boolstring(value); | |
400 | src->data = g_variant_new_boolean(tmp_bool); | |
401 | break; | |
402 | case SR_T_FLOAT: | |
403 | tmp_double = strtof(value, NULL); | |
404 | src->data = g_variant_new_double(tmp_double); | |
405 | break; | |
406 | case SR_T_RATIONAL_PERIOD: | |
407 | if ((ret = sr_parse_period(value, &p, &q)) != SR_OK) | |
2be182e6 | 408 | break; |
ad54a1a6 BV |
409 | rational[0] = g_variant_new_uint64(p); |
410 | rational[1] = g_variant_new_uint64(q); | |
411 | src->data = g_variant_new_tuple(rational, 2); | |
412 | break; | |
413 | case SR_T_RATIONAL_VOLT: | |
414 | if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK) | |
2be182e6 | 415 | break; |
ad54a1a6 BV |
416 | rational[0] = g_variant_new_uint64(p); |
417 | rational[1] = g_variant_new_uint64(q); | |
418 | src->data = g_variant_new_tuple(rational, 2); | |
419 | break; | |
420 | case SR_T_UINT64_RANGE: | |
421 | if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) { | |
422 | ret = -1; | |
2be182e6 | 423 | break; |
ad54a1a6 BV |
424 | } else { |
425 | range[0] = g_variant_new_uint64(low); | |
426 | range[1] = g_variant_new_uint64(high); | |
427 | src->data = g_variant_new_tuple(range, 2); | |
2be182e6 | 428 | } |
ad54a1a6 | 429 | break; |
426d0cda BV |
430 | case SR_T_DOUBLE_RANGE: |
431 | if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) { | |
432 | ret = -1; | |
433 | break; | |
434 | } else { | |
435 | range[0] = g_variant_new_double(dlow); | |
436 | range[1] = g_variant_new_double(dhigh); | |
437 | src->data = g_variant_new_tuple(range, 2); | |
438 | } | |
439 | break; | |
e4588937 BG |
440 | case SR_T_KEYVALUE: |
441 | /* Expects the argument to be in the form of key=value. */ | |
442 | keyval = g_strsplit(value, "=", 2); | |
443 | if (!keyval[0] || !keyval[1]) { | |
444 | g_strfreev(keyval); | |
445 | ret = -1; | |
446 | break; | |
447 | } else { | |
448 | vbl = g_variant_builder_new(G_VARIANT_TYPE_DICTIONARY); | |
449 | g_variant_builder_add(vbl, "{ss}", | |
450 | keyval[0], keyval[1]); | |
451 | src->data = g_variant_builder_end(vbl); | |
452 | g_strfreev(keyval); | |
453 | } | |
454 | break; | |
ad54a1a6 | 455 | default: |
b262d8f2 BG |
456 | g_critical("Unknown data type specified for option '%s' " |
457 | "(driver implementation bug?).", key); | |
ad54a1a6 BV |
458 | ret = -1; |
459 | } | |
460 | ||
27d310f0 BG |
461 | if (ret < 0) |
462 | g_critical("Invalid value: '%s' for option '%s'", value, key); | |
463 | ||
ad54a1a6 BV |
464 | return ret; |
465 | } | |
466 | ||
467 | int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args) | |
468 | { | |
469 | struct sr_config src; | |
ca50f4b3 | 470 | struct sr_channel_group *cg; |
ad54a1a6 BV |
471 | GHashTableIter iter; |
472 | gpointer key, value; | |
473 | int ret; | |
474 | ||
475 | g_hash_table_iter_init(&iter, args); | |
476 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
477 | if ((ret = opt_to_gvar(key, value, &src)) != 0) | |
478 | return ret; | |
ca50f4b3 | 479 | cg = select_channel_group(sdi); |
24bd9719 BV |
480 | if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg, |
481 | src.key, src.data)) != SR_OK) { | |
2be182e6 BV |
482 | g_critical("Failed to set device option '%s'.", (char *)key); |
483 | return ret; | |
484 | } | |
485 | } | |
486 | ||
487 | return SR_OK; | |
488 | } | |
489 | ||
490 | void run_session(void) | |
491 | { | |
ac3dcd3e | 492 | GSList *devices, *real_devices, *sd; |
2be182e6 BV |
493 | GHashTable *devargs; |
494 | GVariant *gvar; | |
4bf77ec6 BV |
495 | struct sr_session *session; |
496 | struct sr_trigger *trigger; | |
2be182e6 | 497 | struct sr_dev_inst *sdi; |
02c65935 | 498 | uint64_t min_samples, max_samples; |
ac3dcd3e PZ |
499 | gsize n_elements, i; |
500 | const uint32_t *dev_opts; | |
501 | int is_demo_dev; | |
c6fa2b2e | 502 | struct sr_dev_driver *driver; |
fb995521 | 503 | const struct sr_transform *t; |
2be182e6 BV |
504 | |
505 | devices = device_scan(); | |
506 | if (!devices) { | |
507 | g_critical("No devices found."); | |
508 | return; | |
509 | } | |
ac3dcd3e PZ |
510 | |
511 | real_devices = NULL; | |
512 | for (sd = devices; sd; sd = sd->next) { | |
513 | sdi = sd->data; | |
514 | ||
c6fa2b2e UH |
515 | driver = sr_dev_inst_driver_get(sdi); |
516 | ||
517 | if (sr_config_list(driver, sdi, NULL, SR_CONF_DEVICE_OPTIONS, &gvar) != SR_OK) { | |
24bd9719 | 518 | g_critical("Failed to query list device options."); |
ac3dcd3e PZ |
519 | return; |
520 | } | |
521 | ||
522 | dev_opts = g_variant_get_fixed_array(gvar, &n_elements, sizeof(uint32_t)); | |
523 | ||
524 | is_demo_dev = 0; | |
525 | for (i = 0; i < n_elements; i++) { | |
526 | if (dev_opts[i] == SR_CONF_DEMO_DEV) | |
527 | is_demo_dev = 1; | |
528 | } | |
529 | ||
530 | g_variant_unref(gvar); | |
531 | ||
532 | if (!is_demo_dev) | |
533 | real_devices = g_slist_append(real_devices, sdi); | |
534 | } | |
535 | ||
2be182e6 | 536 | if (g_slist_length(devices) > 1) { |
ac3dcd3e PZ |
537 | if (g_slist_length(real_devices) != 1) { |
538 | g_critical("sigrok-cli only supports one device for capturing."); | |
539 | return; | |
540 | } else { | |
541 | /* We only have one non-demo device. */ | |
542 | g_slist_free(devices); | |
543 | devices = real_devices; | |
544 | real_devices = NULL; | |
545 | } | |
2be182e6 | 546 | } |
ac3dcd3e | 547 | |
2be182e6 | 548 | sdi = devices->data; |
b4eece7c | 549 | g_slist_free(devices); |
ac3dcd3e | 550 | g_slist_free(real_devices); |
2be182e6 | 551 | |
4bf77ec6 BV |
552 | sr_session_new(&session); |
553 | sr_session_datafeed_callback_add(session, datafeed_in, NULL); | |
2be182e6 BV |
554 | |
555 | if (sr_dev_open(sdi) != SR_OK) { | |
556 | g_critical("Failed to open device."); | |
557 | return; | |
558 | } | |
559 | ||
4bf77ec6 | 560 | if (sr_session_dev_add(session, sdi) != SR_OK) { |
2be182e6 | 561 | g_critical("Failed to add device to session."); |
4bf77ec6 | 562 | sr_session_destroy(session); |
2be182e6 BV |
563 | return; |
564 | } | |
565 | ||
566 | if (opt_config) { | |
567 | if ((devargs = parse_generic_arg(opt_config, FALSE))) { | |
568 | if (set_dev_options(sdi, devargs) != SR_OK) | |
569 | return; | |
570 | g_hash_table_destroy(devargs); | |
571 | } | |
572 | } | |
573 | ||
029d73fe UH |
574 | if (select_channels(sdi) != SR_OK) { |
575 | g_critical("Failed to set channels."); | |
4bf77ec6 | 576 | sr_session_destroy(session); |
2be182e6 BV |
577 | return; |
578 | } | |
579 | ||
580 | if (opt_triggers) { | |
4bf77ec6 BV |
581 | if (!parse_triggerstring(sdi, opt_triggers, &trigger)) { |
582 | sr_session_destroy(session); | |
583 | return; | |
584 | } | |
585 | if (sr_session_trigger_set(session, trigger) != SR_OK) { | |
586 | sr_session_destroy(session); | |
2be182e6 BV |
587 | return; |
588 | } | |
2be182e6 BV |
589 | } |
590 | ||
591 | if (opt_continuous) { | |
592 | if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) { | |
593 | g_critical("This device does not support continuous sampling."); | |
4bf77ec6 | 594 | sr_session_destroy(session); |
2be182e6 BV |
595 | return; |
596 | } | |
597 | } | |
598 | ||
599 | if (opt_time) { | |
600 | if (set_limit_time(sdi) != SR_OK) { | |
4bf77ec6 | 601 | sr_session_destroy(session); |
2be182e6 BV |
602 | return; |
603 | } | |
604 | } | |
605 | ||
606 | if (opt_samples) { | |
607 | if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) { | |
608 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
4bf77ec6 | 609 | sr_session_destroy(session); |
2be182e6 BV |
610 | return; |
611 | } | |
24bd9719 BV |
612 | if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES, |
613 | &gvar) == SR_OK) { | |
c7a5cb12 BV |
614 | /* The device has no compression, or compression is turned |
615 | * off, and publishes its sample memory size. */ | |
02c65935 | 616 | g_variant_get(gvar, "(tt)", &min_samples, &max_samples); |
be781321 | 617 | g_variant_unref(gvar); |
02c65935 BV |
618 | if (limit_samples < min_samples) { |
619 | g_critical("The device stores at least %"PRIu64 | |
620 | " samples with the current settings.", min_samples); | |
621 | } | |
c7a5cb12 BV |
622 | if (limit_samples > max_samples) { |
623 | g_critical("The device can store only %"PRIu64 | |
624 | " samples with the current settings.", max_samples); | |
625 | } | |
626 | } | |
2be182e6 | 627 | gvar = g_variant_new_uint64(limit_samples); |
24bd9719 | 628 | if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { |
2be182e6 | 629 | g_critical("Failed to configure sample limit."); |
4bf77ec6 | 630 | sr_session_destroy(session); |
2be182e6 BV |
631 | return; |
632 | } | |
633 | } | |
634 | ||
635 | if (opt_frames) { | |
636 | if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) { | |
637 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
4bf77ec6 | 638 | sr_session_destroy(session); |
2be182e6 BV |
639 | return; |
640 | } | |
641 | gvar = g_variant_new_uint64(limit_frames); | |
24bd9719 | 642 | if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) { |
2be182e6 | 643 | g_critical("Failed to configure frame limit."); |
4bf77ec6 | 644 | sr_session_destroy(session); |
2be182e6 BV |
645 | return; |
646 | } | |
647 | } | |
648 | ||
fb995521 UH |
649 | if (!(t = setup_transform_module(sdi))) |
650 | g_critical("Failed to initialize transform module."); | |
651 | ||
4bf77ec6 | 652 | if (sr_session_start(session) != SR_OK) { |
2be182e6 | 653 | g_critical("Failed to start session."); |
4bf77ec6 | 654 | sr_session_destroy(session); |
2be182e6 BV |
655 | return; |
656 | } | |
657 | ||
658 | if (opt_continuous) | |
4bf77ec6 | 659 | add_anykey(session); |
2be182e6 | 660 | |
4bf77ec6 | 661 | sr_session_run(session); |
2be182e6 BV |
662 | |
663 | if (opt_continuous) | |
664 | clear_anykey(); | |
665 | ||
4bf77ec6 BV |
666 | sr_session_datafeed_callback_remove_all(session); |
667 | sr_session_destroy(session); | |
2be182e6 BV |
668 | |
669 | } | |
670 |