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