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