]>
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 <config.h> | |
21 | #include <glib.h> | |
22 | #include <glib/gstdio.h> | |
23 | #include <string.h> | |
24 | #include <stdlib.h> | |
25 | #include "sigrok-cli.h" | |
26 | ||
27 | static uint64_t limit_samples = 0; | |
28 | static uint64_t limit_frames = 0; | |
29 | ||
30 | #ifdef HAVE_SRD | |
31 | extern struct srd_session *srd_sess; | |
32 | #endif | |
33 | ||
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; | |
39 | struct sr_dev_driver *driver; | |
40 | ||
41 | driver = sr_dev_inst_driver_get(sdi); | |
42 | ||
43 | if (!(time_msec = sr_parse_timestring(opt_time))) { | |
44 | g_critical("Invalid time '%s'", opt_time); | |
45 | return SR_ERR; | |
46 | } | |
47 | ||
48 | if (sr_dev_config_capabilities(sdi, NULL, SR_CONF_LIMIT_MSEC) | |
49 | & SR_CONF_SET) { | |
50 | gvar = g_variant_new_uint64(time_msec); | |
51 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) { | |
52 | g_critical("Failed to configure time limit."); | |
53 | return SR_ERR; | |
54 | } | |
55 | } else if (sr_dev_config_capabilities(sdi, NULL, SR_CONF_SAMPLERATE) | |
56 | & (SR_CONF_GET | SR_CONF_SET)) { | |
57 | /* Convert to samples based on the samplerate. */ | |
58 | sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar); | |
59 | samplerate = g_variant_get_uint64(gvar); | |
60 | g_variant_unref(gvar); | |
61 | limit_samples = (samplerate) * time_msec / (uint64_t)1000; | |
62 | if (limit_samples == 0) { | |
63 | g_critical("Not enough time at this samplerate."); | |
64 | return SR_ERR; | |
65 | } | |
66 | gvar = g_variant_new_uint64(limit_samples); | |
67 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { | |
68 | g_critical("Failed to configure time-based sample limit."); | |
69 | return SR_ERR; | |
70 | } | |
71 | } else { | |
72 | g_critical("This device does not support time limits."); | |
73 | return SR_ERR; | |
74 | } | |
75 | ||
76 | return SR_OK; | |
77 | } | |
78 | ||
79 | const struct sr_output *setup_output_format(const struct sr_dev_inst *sdi, FILE **outfile) | |
80 | { | |
81 | const struct sr_output_module *omod; | |
82 | const struct sr_option **options; | |
83 | const struct sr_output *o; | |
84 | GHashTable *fmtargs, *fmtopts; | |
85 | char *fmtspec; | |
86 | ||
87 | if (!opt_output_format) { | |
88 | if (opt_output_file) { | |
89 | opt_output_format = DEFAULT_OUTPUT_FORMAT_FILE; | |
90 | } else { | |
91 | opt_output_format = DEFAULT_OUTPUT_FORMAT_NOFILE; | |
92 | } | |
93 | } | |
94 | ||
95 | fmtargs = parse_generic_arg(opt_output_format, TRUE); | |
96 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); | |
97 | if (!fmtspec) | |
98 | g_critical("Invalid output format."); | |
99 | if (!(omod = sr_output_find(fmtspec))) | |
100 | g_critical("Unknown output module '%s'.", fmtspec); | |
101 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
102 | if ((options = sr_output_options_get(omod))) { | |
103 | fmtopts = generic_arg_to_opt(options, fmtargs); | |
104 | sr_output_options_free(options); | |
105 | } else | |
106 | fmtopts = NULL; | |
107 | o = sr_output_new(omod, fmtopts, sdi, opt_output_file); | |
108 | ||
109 | if (opt_output_file) { | |
110 | if (!sr_output_test_flag(omod, SR_OUTPUT_INTERNAL_IO_HANDLING)) | |
111 | *outfile = g_fopen(opt_output_file, "wb"); | |
112 | else | |
113 | *outfile = NULL; | |
114 | } else { | |
115 | *outfile = stdout; | |
116 | } | |
117 | ||
118 | if (fmtopts) | |
119 | g_hash_table_destroy(fmtopts); | |
120 | g_hash_table_destroy(fmtargs); | |
121 | ||
122 | return o; | |
123 | } | |
124 | ||
125 | const struct sr_transform *setup_transform_module(const struct sr_dev_inst *sdi) | |
126 | { | |
127 | const struct sr_transform_module *tmod; | |
128 | const struct sr_option **options; | |
129 | const struct sr_transform *t; | |
130 | GHashTable *fmtargs, *fmtopts; | |
131 | char *fmtspec; | |
132 | ||
133 | if (!opt_transform_module) | |
134 | opt_transform_module = "nop"; | |
135 | ||
136 | fmtargs = parse_generic_arg(opt_transform_module, TRUE); | |
137 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); | |
138 | if (!fmtspec) | |
139 | g_critical("Invalid transform module."); | |
140 | if (!(tmod = sr_transform_find(fmtspec))) | |
141 | g_critical("Unknown transform module '%s'.", fmtspec); | |
142 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
143 | if ((options = sr_transform_options_get(tmod))) { | |
144 | fmtopts = generic_arg_to_opt(options, fmtargs); | |
145 | sr_transform_options_free(options); | |
146 | } else | |
147 | fmtopts = NULL; | |
148 | t = sr_transform_new(tmod, fmtopts, sdi); | |
149 | if (fmtopts) | |
150 | g_hash_table_destroy(fmtopts); | |
151 | g_hash_table_destroy(fmtargs); | |
152 | ||
153 | return t; | |
154 | } | |
155 | ||
156 | void datafeed_in(const struct sr_dev_inst *sdi, | |
157 | const struct sr_datafeed_packet *packet, void *cb_data) | |
158 | { | |
159 | const struct sr_datafeed_meta *meta; | |
160 | const struct sr_datafeed_logic *logic; | |
161 | const struct sr_datafeed_analog *analog; | |
162 | struct sr_session *session; | |
163 | struct sr_config *src; | |
164 | static const struct sr_output *o = NULL; | |
165 | static const struct sr_output *oa = NULL; | |
166 | static uint64_t rcvd_samples_logic = 0; | |
167 | static uint64_t rcvd_samples_analog = 0; | |
168 | static uint64_t samplerate = 0; | |
169 | static int triggered = 0; | |
170 | static FILE *outfile = NULL; | |
171 | GSList *l; | |
172 | GString *out; | |
173 | GVariant *gvar; | |
174 | uint64_t end_sample; | |
175 | uint64_t input_len; | |
176 | struct sr_dev_driver *driver; | |
177 | ||
178 | driver = sr_dev_inst_driver_get(sdi); | |
179 | ||
180 | /* If the first packet to come in isn't a header, don't even try. */ | |
181 | if (packet->type != SR_DF_HEADER && !o) | |
182 | return; | |
183 | ||
184 | session = cb_data; | |
185 | switch (packet->type) { | |
186 | case SR_DF_HEADER: | |
187 | g_debug("cli: Received SR_DF_HEADER."); | |
188 | if (!(o = setup_output_format(sdi, &outfile))) | |
189 | g_critical("Failed to initialize output module."); | |
190 | ||
191 | /* Set up backup analog output module. */ | |
192 | oa = sr_output_new(sr_output_find("analog"), NULL, sdi, NULL); | |
193 | ||
194 | rcvd_samples_logic = rcvd_samples_analog = 0; | |
195 | ||
196 | if (maybe_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, | |
197 | &gvar) == SR_OK) { | |
198 | samplerate = g_variant_get_uint64(gvar); | |
199 | g_variant_unref(gvar); | |
200 | } | |
201 | ||
202 | #ifdef HAVE_SRD | |
203 | if (opt_pds) { | |
204 | if (samplerate) { | |
205 | if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE, | |
206 | g_variant_new_uint64(samplerate)) != SRD_OK) { | |
207 | g_critical("Failed to configure decode session."); | |
208 | break; | |
209 | } | |
210 | } | |
211 | if (srd_session_start(srd_sess) != SRD_OK) { | |
212 | g_critical("Failed to start decode session."); | |
213 | break; | |
214 | } | |
215 | } | |
216 | #endif | |
217 | break; | |
218 | ||
219 | case SR_DF_META: | |
220 | g_debug("cli: Received SR_DF_META."); | |
221 | meta = packet->payload; | |
222 | for (l = meta->config; l; l = l->next) { | |
223 | src = l->data; | |
224 | switch (src->key) { | |
225 | case SR_CONF_SAMPLERATE: | |
226 | samplerate = g_variant_get_uint64(src->data); | |
227 | g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate); | |
228 | #ifdef HAVE_SRD | |
229 | if (opt_pds) { | |
230 | if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE, | |
231 | g_variant_new_uint64(samplerate)) != SRD_OK) { | |
232 | g_critical("Failed to pass samplerate to decoder."); | |
233 | } | |
234 | } | |
235 | #endif | |
236 | break; | |
237 | case SR_CONF_SAMPLE_INTERVAL: | |
238 | samplerate = g_variant_get_uint64(src->data); | |
239 | g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate); | |
240 | break; | |
241 | default: | |
242 | /* Unknown metadata is not an error. */ | |
243 | break; | |
244 | } | |
245 | } | |
246 | break; | |
247 | ||
248 | case SR_DF_TRIGGER: | |
249 | g_debug("cli: Received SR_DF_TRIGGER."); | |
250 | triggered = 1; | |
251 | break; | |
252 | ||
253 | case SR_DF_LOGIC: | |
254 | logic = packet->payload; | |
255 | g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).", | |
256 | logic->length, logic->unitsize); | |
257 | if (logic->length == 0) | |
258 | break; | |
259 | ||
260 | /* Don't store any samples until triggered. */ | |
261 | if (opt_wait_trigger && !triggered) | |
262 | break; | |
263 | ||
264 | if (limit_samples && rcvd_samples_logic >= limit_samples) | |
265 | break; | |
266 | ||
267 | end_sample = rcvd_samples_logic + logic->length / logic->unitsize; | |
268 | /* Cut off last packet according to the sample limit. */ | |
269 | if (limit_samples && end_sample > limit_samples) | |
270 | end_sample = limit_samples; | |
271 | input_len = (end_sample - rcvd_samples_logic) * logic->unitsize; | |
272 | ||
273 | if (opt_pds) { | |
274 | #ifdef HAVE_SRD | |
275 | if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample, | |
276 | logic->data, input_len, logic->unitsize) != SRD_OK) | |
277 | sr_session_stop(session); | |
278 | #endif | |
279 | } | |
280 | ||
281 | rcvd_samples_logic = end_sample; | |
282 | break; | |
283 | ||
284 | case SR_DF_ANALOG: | |
285 | analog = packet->payload; | |
286 | g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples); | |
287 | if (analog->num_samples == 0) | |
288 | break; | |
289 | ||
290 | if (limit_samples && rcvd_samples_analog >= limit_samples) | |
291 | break; | |
292 | ||
293 | rcvd_samples_analog += analog->num_samples; | |
294 | break; | |
295 | ||
296 | case SR_DF_FRAME_BEGIN: | |
297 | g_debug("cli: Received SR_DF_FRAME_BEGIN."); | |
298 | break; | |
299 | ||
300 | case SR_DF_FRAME_END: | |
301 | g_debug("cli: Received SR_DF_FRAME_END."); | |
302 | break; | |
303 | ||
304 | default: | |
305 | break; | |
306 | } | |
307 | ||
308 | if (o && !opt_pds) { | |
309 | if (sr_output_send(o, packet, &out) == SR_OK) { | |
310 | if (!out || (out->len == 0 | |
311 | && !opt_output_format | |
312 | && packet->type == SR_DF_ANALOG)) { | |
313 | /* | |
314 | * The user didn't specify an output module, | |
315 | * but needs to see this analog data. | |
316 | */ | |
317 | sr_output_send(oa, packet, &out); | |
318 | } | |
319 | if (outfile && out && out->len > 0) { | |
320 | fwrite(out->str, 1, out->len, outfile); | |
321 | fflush(outfile); | |
322 | } | |
323 | if (out) | |
324 | g_string_free(out, TRUE); | |
325 | } | |
326 | } | |
327 | ||
328 | /* | |
329 | * SR_DF_END needs to be handled after the output module's receive() | |
330 | * is called, so it can properly clean up that module. | |
331 | */ | |
332 | if (packet->type == SR_DF_END) { | |
333 | g_debug("cli: Received SR_DF_END."); | |
334 | ||
335 | if (o) | |
336 | sr_output_free(o); | |
337 | o = NULL; | |
338 | ||
339 | sr_output_free(oa); | |
340 | oa = NULL; | |
341 | ||
342 | if (outfile && outfile != stdout) | |
343 | fclose(outfile); | |
344 | ||
345 | if (limit_samples) { | |
346 | if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples) | |
347 | g_warning("Device only sent %" PRIu64 " samples.", | |
348 | rcvd_samples_logic); | |
349 | else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples) | |
350 | g_warning("Device only sent %" PRIu64 " samples.", | |
351 | rcvd_samples_analog); | |
352 | } | |
353 | } | |
354 | ||
355 | } | |
356 | ||
357 | int opt_to_gvar(char *key, char *value, struct sr_config *src) | |
358 | { | |
359 | const struct sr_key_info *srci, *srmqi; | |
360 | double tmp_double, dlow, dhigh; | |
361 | uint64_t tmp_u64, p, q, low, high, mqflags; | |
362 | uint32_t mq; | |
363 | GVariant *rational[2], *range[2], *gtup[2]; | |
364 | GVariantBuilder *vbl; | |
365 | gboolean tmp_bool; | |
366 | gchar **keyval; | |
367 | int ret, i; | |
368 | ||
369 | if (!(srci = sr_key_info_name_get(SR_KEY_CONFIG, key))) { | |
370 | g_critical("Unknown device option '%s'.", (char *) key); | |
371 | return -1; | |
372 | } | |
373 | src->key = srci->key; | |
374 | ||
375 | if ((!value || strlen(value) == 0) && | |
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) | |
386 | break; | |
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) | |
392 | break; | |
393 | src->data = g_variant_new_int32(tmp_u64); | |
394 | break; | |
395 | case SR_T_STRING: | |
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) | |
411 | break; | |
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) | |
418 | break; | |
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; | |
426 | break; | |
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); | |
431 | } | |
432 | break; | |
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; | |
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; | |
458 | case SR_T_MQ: | |
459 | /* | |
460 | Argument is MQ id e.g. ("voltage") optionally followed by one | |
461 | or more /<mqflag> e.g. "/ac". | |
462 | */ | |
463 | keyval = g_strsplit(value, "/", 0); | |
464 | if (!keyval[0] || !(srmqi = sr_key_info_name_get(SR_KEY_MQ, keyval[0]))) { | |
465 | g_strfreev(keyval); | |
466 | ret = -1; | |
467 | break; | |
468 | } | |
469 | mq = srmqi->key; | |
470 | mqflags = 0; | |
471 | for (i = 1; keyval[i]; i++) { | |
472 | if (!(srmqi = sr_key_info_name_get(SR_KEY_MQFLAGS, keyval[i]))) { | |
473 | ret = -1; | |
474 | break; | |
475 | } | |
476 | mqflags |= srmqi->key; | |
477 | } | |
478 | g_strfreev(keyval); | |
479 | if (ret != -1) { | |
480 | gtup[0] = g_variant_new_uint32(mq); | |
481 | gtup[1] = g_variant_new_uint64(mqflags); | |
482 | src->data = g_variant_new_tuple(gtup, 2); | |
483 | } | |
484 | break; | |
485 | default: | |
486 | g_critical("Unknown data type specified for option '%s' " | |
487 | "(driver implementation bug?).", key); | |
488 | ret = -1; | |
489 | } | |
490 | ||
491 | if (ret < 0) | |
492 | g_critical("Invalid value: '%s' for option '%s'", value, key); | |
493 | ||
494 | return ret; | |
495 | } | |
496 | ||
497 | int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args) | |
498 | { | |
499 | struct sr_config src; | |
500 | struct sr_channel_group *cg; | |
501 | GHashTableIter iter; | |
502 | gpointer key, value; | |
503 | int ret; | |
504 | ||
505 | g_hash_table_iter_init(&iter, args); | |
506 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
507 | if ((ret = opt_to_gvar(key, value, &src)) != 0) | |
508 | return ret; | |
509 | cg = select_channel_group(sdi); | |
510 | if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg, | |
511 | src.key, src.data)) != SR_OK) { | |
512 | g_critical("Failed to set device option '%s': %s.", | |
513 | (char *)key, sr_strerror(ret)); | |
514 | return ret; | |
515 | } | |
516 | } | |
517 | ||
518 | return SR_OK; | |
519 | } | |
520 | ||
521 | void run_session(void) | |
522 | { | |
523 | GSList *devices, *real_devices, *sd; | |
524 | GHashTable *devargs; | |
525 | GVariant *gvar; | |
526 | struct sr_session *session; | |
527 | struct sr_trigger *trigger; | |
528 | struct sr_dev_inst *sdi; | |
529 | uint64_t min_samples, max_samples; | |
530 | GArray *dev_opts; | |
531 | guint i; | |
532 | int is_demo_dev; | |
533 | struct sr_dev_driver *driver; | |
534 | const struct sr_transform *t; | |
535 | GMainLoop *main_loop; | |
536 | ||
537 | devices = device_scan(); | |
538 | if (!devices) { | |
539 | g_critical("No devices found."); | |
540 | return; | |
541 | } | |
542 | ||
543 | real_devices = NULL; | |
544 | for (sd = devices; sd; sd = sd->next) { | |
545 | sdi = sd->data; | |
546 | ||
547 | driver = sr_dev_inst_driver_get(sdi); | |
548 | ||
549 | if (!(dev_opts = sr_dev_options(driver, sdi, NULL))) { | |
550 | g_critical("Failed to query list device options."); | |
551 | return; | |
552 | } | |
553 | ||
554 | is_demo_dev = 0; | |
555 | for (i = 0; i < dev_opts->len; i++) { | |
556 | if (g_array_index(dev_opts, uint32_t, i) == SR_CONF_DEMO_DEV) | |
557 | is_demo_dev = 1; | |
558 | } | |
559 | ||
560 | g_array_free(dev_opts, TRUE); | |
561 | ||
562 | if (!is_demo_dev) | |
563 | real_devices = g_slist_append(real_devices, sdi); | |
564 | } | |
565 | ||
566 | if (g_slist_length(devices) > 1) { | |
567 | if (g_slist_length(real_devices) != 1) { | |
568 | g_critical("sigrok-cli only supports one device for capturing."); | |
569 | return; | |
570 | } else { | |
571 | /* We only have one non-demo device. */ | |
572 | g_slist_free(devices); | |
573 | devices = real_devices; | |
574 | real_devices = NULL; | |
575 | } | |
576 | } | |
577 | ||
578 | sdi = devices->data; | |
579 | g_slist_free(devices); | |
580 | g_slist_free(real_devices); | |
581 | ||
582 | sr_session_new(sr_ctx, &session); | |
583 | sr_session_datafeed_callback_add(session, datafeed_in, NULL); | |
584 | ||
585 | if (sr_dev_open(sdi) != SR_OK) { | |
586 | g_critical("Failed to open device."); | |
587 | return; | |
588 | } | |
589 | ||
590 | if (sr_session_dev_add(session, sdi) != SR_OK) { | |
591 | g_critical("Failed to add device to session."); | |
592 | sr_session_destroy(session); | |
593 | return; | |
594 | } | |
595 | ||
596 | if (opt_config) { | |
597 | if ((devargs = parse_generic_arg(opt_config, FALSE))) { | |
598 | if (set_dev_options(sdi, devargs) != SR_OK) | |
599 | return; | |
600 | g_hash_table_destroy(devargs); | |
601 | } | |
602 | } | |
603 | ||
604 | if (select_channels(sdi) != SR_OK) { | |
605 | g_critical("Failed to set channels."); | |
606 | sr_session_destroy(session); | |
607 | return; | |
608 | } | |
609 | ||
610 | trigger = NULL; | |
611 | if (opt_triggers) { | |
612 | if (!parse_triggerstring(sdi, opt_triggers, &trigger)) { | |
613 | sr_session_destroy(session); | |
614 | return; | |
615 | } | |
616 | if (sr_session_trigger_set(session, trigger) != SR_OK) { | |
617 | sr_session_destroy(session); | |
618 | return; | |
619 | } | |
620 | } | |
621 | ||
622 | if (opt_continuous) { | |
623 | if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) { | |
624 | g_critical("This device does not support continuous sampling."); | |
625 | sr_session_destroy(session); | |
626 | return; | |
627 | } | |
628 | } | |
629 | ||
630 | if (opt_time) { | |
631 | if (set_limit_time(sdi) != SR_OK) { | |
632 | sr_session_destroy(session); | |
633 | return; | |
634 | } | |
635 | } | |
636 | ||
637 | if (opt_samples) { | |
638 | if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) { | |
639 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
640 | sr_session_destroy(session); | |
641 | return; | |
642 | } | |
643 | if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES, | |
644 | &gvar) == SR_OK) { | |
645 | /* | |
646 | * The device has no compression, or compression is turned | |
647 | * off, and publishes its sample memory size. | |
648 | */ | |
649 | g_variant_get(gvar, "(tt)", &min_samples, &max_samples); | |
650 | g_variant_unref(gvar); | |
651 | if (limit_samples < min_samples) { | |
652 | g_critical("The device stores at least %"PRIu64 | |
653 | " samples with the current settings.", min_samples); | |
654 | } | |
655 | if (limit_samples > max_samples) { | |
656 | g_critical("The device can store only %"PRIu64 | |
657 | " samples with the current settings.", max_samples); | |
658 | } | |
659 | } | |
660 | gvar = g_variant_new_uint64(limit_samples); | |
661 | if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { | |
662 | g_critical("Failed to configure sample limit."); | |
663 | sr_session_destroy(session); | |
664 | return; | |
665 | } | |
666 | } | |
667 | ||
668 | if (opt_frames) { | |
669 | if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) { | |
670 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
671 | sr_session_destroy(session); | |
672 | return; | |
673 | } | |
674 | gvar = g_variant_new_uint64(limit_frames); | |
675 | if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) { | |
676 | g_critical("Failed to configure frame limit."); | |
677 | sr_session_destroy(session); | |
678 | return; | |
679 | } | |
680 | } | |
681 | ||
682 | if (!(t = setup_transform_module(sdi))) | |
683 | g_critical("Failed to initialize transform module."); | |
684 | ||
685 | main_loop = g_main_loop_new(NULL, FALSE); | |
686 | ||
687 | sr_session_stopped_callback_set(session, | |
688 | (sr_session_stopped_callback)g_main_loop_quit, main_loop); | |
689 | ||
690 | if (sr_session_start(session) != SR_OK) { | |
691 | g_critical("Failed to start session."); | |
692 | g_main_loop_unref(main_loop); | |
693 | sr_session_destroy(session); | |
694 | return; | |
695 | } | |
696 | ||
697 | if (opt_continuous) | |
698 | add_anykey(session); | |
699 | ||
700 | g_main_loop_run(main_loop); | |
701 | ||
702 | if (opt_continuous) | |
703 | clear_anykey(); | |
704 | ||
705 | if (trigger) | |
706 | sr_trigger_free(trigger); | |
707 | ||
708 | sr_session_datafeed_callback_remove_all(session); | |
709 | g_main_loop_unref(main_loop); | |
710 | sr_session_destroy(session); | |
711 | } |