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