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