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