]>
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 "sigrok-cli.h" | |
21 | #include "config.h" | |
22 | #include <glib.h> | |
23 | #include <glib/gstdio.h> | |
24 | #include <string.h> | |
25 | #include <stdlib.h> | |
26 | ||
27 | static struct sr_output_format *output_format = NULL; | |
28 | static int default_output_format = FALSE; | |
29 | static char *output_format_param = NULL; | |
30 | static uint64_t limit_samples = 0; | |
31 | static uint64_t limit_frames = 0; | |
32 | ||
33 | extern gchar *opt_output_file; | |
34 | extern gchar *opt_output_format; | |
35 | extern gchar *opt_pds; | |
36 | extern gboolean opt_wait_trigger; | |
37 | extern gchar *opt_time; | |
38 | extern gchar *opt_samples; | |
39 | extern gchar *opt_frames; | |
40 | extern gchar *opt_continuous; | |
41 | extern gchar *opt_config; | |
42 | extern gchar *opt_triggers; | |
43 | #ifdef HAVE_SRD | |
44 | extern struct srd_session *srd_sess; | |
45 | #endif | |
46 | ||
47 | static int set_limit_time(const struct sr_dev_inst *sdi) | |
48 | { | |
49 | GVariant *gvar; | |
50 | uint64_t time_msec; | |
51 | uint64_t samplerate; | |
52 | ||
53 | if (!(time_msec = sr_parse_timestring(opt_time))) { | |
54 | g_critical("Invalid time '%s'", opt_time); | |
55 | return SR_ERR; | |
56 | } | |
57 | ||
58 | if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) { | |
59 | gvar = g_variant_new_uint64(time_msec); | |
60 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) { | |
61 | g_critical("Failed to configure time limit."); | |
62 | return SR_ERR; | |
63 | } | |
64 | } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) { | |
65 | /* Convert to samples based on the samplerate. */ | |
66 | sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar); | |
67 | samplerate = g_variant_get_uint64(gvar); | |
68 | g_variant_unref(gvar); | |
69 | limit_samples = (samplerate) * time_msec / (uint64_t)1000; | |
70 | if (limit_samples == 0) { | |
71 | g_critical("Not enough time at this samplerate."); | |
72 | return SR_ERR; | |
73 | } | |
74 | gvar = g_variant_new_uint64(limit_samples); | |
75 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { | |
76 | g_critical("Failed to configure time-based sample limit."); | |
77 | return SR_ERR; | |
78 | } | |
79 | } else { | |
80 | g_critical("This device does not support time limits."); | |
81 | return SR_ERR; | |
82 | } | |
83 | ||
84 | return SR_OK; | |
85 | } | |
86 | ||
87 | int setup_output_format(void) | |
88 | { | |
89 | GHashTable *fmtargs; | |
90 | GHashTableIter iter; | |
91 | gpointer key, value; | |
92 | struct sr_output_format **outputs; | |
93 | int i; | |
94 | char *fmtspec; | |
95 | ||
96 | if (opt_output_format && !strcmp(opt_output_format, "sigrok")) { | |
97 | /* Doesn't really exist as an output module - this is | |
98 | * the session save mode. */ | |
99 | g_free(opt_output_format); | |
100 | opt_output_format = NULL; | |
101 | } | |
102 | ||
103 | if (!opt_output_format) { | |
104 | opt_output_format = DEFAULT_OUTPUT_FORMAT; | |
105 | /* we'll need to remember this so when saving to a file | |
106 | * later, sigrok session format will be used. | |
107 | */ | |
108 | default_output_format = TRUE; | |
109 | } | |
110 | ||
111 | fmtargs = parse_generic_arg(opt_output_format, TRUE); | |
112 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); | |
113 | if (!fmtspec) { | |
114 | g_critical("Invalid output format."); | |
115 | return 1; | |
116 | } | |
117 | outputs = sr_output_list(); | |
118 | for (i = 0; outputs[i]; i++) { | |
119 | if (strcmp(outputs[i]->id, fmtspec)) | |
120 | continue; | |
121 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
122 | output_format = outputs[i]; | |
123 | g_hash_table_iter_init(&iter, fmtargs); | |
124 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
125 | /* only supporting one parameter per output module | |
126 | * for now, and only its value */ | |
127 | output_format_param = g_strdup(value); | |
128 | break; | |
129 | } | |
130 | break; | |
131 | } | |
132 | if (!output_format) { | |
133 | g_critical("Invalid output format %s.", opt_output_format); | |
134 | return 1; | |
135 | } | |
136 | g_hash_table_destroy(fmtargs); | |
137 | ||
138 | return 0; | |
139 | } | |
140 | ||
141 | void datafeed_in(const struct sr_dev_inst *sdi, | |
142 | const struct sr_datafeed_packet *packet, void *cb_data) | |
143 | { | |
144 | const struct sr_datafeed_meta *meta; | |
145 | const struct sr_datafeed_logic *logic; | |
146 | const struct sr_datafeed_analog *analog; | |
147 | struct sr_config *src; | |
148 | struct sr_probe *probe; | |
149 | static struct sr_output *o = NULL; | |
150 | static uint64_t rcvd_samples_logic = 0; | |
151 | static uint64_t rcvd_samples_analog = 0; | |
152 | static uint64_t samplerate = 0; | |
153 | static int triggered = 0; | |
154 | static FILE *outfile = NULL; | |
155 | GSList *l; | |
156 | GString *out; | |
157 | uint64_t end_sample; | |
158 | uint64_t output_len, input_len; | |
159 | uint8_t *output_buf; | |
160 | int i; | |
161 | char **probes; | |
162 | ||
163 | (void) cb_data; | |
164 | ||
165 | /* If the first packet to come in isn't a header, don't even try. */ | |
166 | if (packet->type != SR_DF_HEADER && o == NULL) | |
167 | return; | |
168 | ||
169 | switch (packet->type) { | |
170 | case SR_DF_HEADER: | |
171 | g_debug("cli: Received SR_DF_HEADER"); | |
172 | /* Initialize the output module. */ | |
173 | if (!(o = g_try_malloc(sizeof(struct sr_output)))) { | |
174 | g_critical("Output module malloc failed."); | |
175 | exit(1); | |
176 | } | |
177 | o->format = output_format; | |
178 | o->sdi = (struct sr_dev_inst *)sdi; | |
179 | o->param = output_format_param; | |
180 | if (o->format->init) { | |
181 | if (o->format->init(o) != SR_OK) { | |
182 | g_critical("Output format initialization failed."); | |
183 | exit(1); | |
184 | } | |
185 | } | |
186 | ||
187 | /* Prepare non-stdout output. */ | |
188 | outfile = stdout; | |
189 | if (opt_output_file) { | |
190 | if (default_output_format) { | |
191 | outfile = NULL; | |
192 | } else { | |
193 | /* saving to a file in whatever format was set | |
194 | * with --format, so all we need is a filehandle */ | |
195 | outfile = g_fopen(opt_output_file, "wb"); | |
196 | } | |
197 | } | |
198 | rcvd_samples_logic = rcvd_samples_analog = 0; | |
199 | ||
200 | #ifdef HAVE_SRD | |
201 | if (opt_pds) { | |
202 | GVariant *gvar; | |
203 | if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE, | |
204 | &gvar) == SR_OK) { | |
205 | samplerate = g_variant_get_uint64(gvar); | |
206 | g_variant_unref(gvar); | |
207 | if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE, | |
208 | g_variant_new_uint64(samplerate)) != SRD_OK) { | |
209 | g_critical("Failed to configure decode session."); | |
210 | break; | |
211 | } | |
212 | } | |
213 | if (srd_session_start(srd_sess) != SRD_OK) { | |
214 | g_critical("Failed to start decode session."); | |
215 | break; | |
216 | } | |
217 | } | |
218 | #endif | |
219 | break; | |
220 | ||
221 | case SR_DF_META: | |
222 | g_debug("cli: received SR_DF_META"); | |
223 | meta = packet->payload; | |
224 | for (l = meta->config; l; l = l->next) { | |
225 | src = l->data; | |
226 | switch (src->key) { | |
227 | case SR_CONF_SAMPLERATE: | |
228 | samplerate = g_variant_get_uint64(src->data); | |
229 | g_debug("cli: got samplerate %"PRIu64" Hz", samplerate); | |
230 | #ifdef HAVE_SRD | |
231 | if (opt_pds) { | |
232 | if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE, | |
233 | g_variant_new_uint64(samplerate)) != SRD_OK) { | |
234 | g_critical("Failed to pass samplerate to decoder."); | |
235 | } | |
236 | } | |
237 | #endif | |
238 | break; | |
239 | case SR_CONF_SAMPLE_INTERVAL: | |
240 | samplerate = g_variant_get_uint64(src->data); | |
241 | g_debug("cli: got sample interval %"PRIu64" ms", samplerate); | |
242 | break; | |
243 | default: | |
244 | /* Unknown metadata is not an error. */ | |
245 | break; | |
246 | } | |
247 | } | |
248 | break; | |
249 | ||
250 | case SR_DF_TRIGGER: | |
251 | g_debug("cli: received SR_DF_TRIGGER"); | |
252 | if (o->format->event) | |
253 | o->format->event(o, SR_DF_TRIGGER, &output_buf, | |
254 | &output_len); | |
255 | triggered = 1; | |
256 | break; | |
257 | ||
258 | case SR_DF_LOGIC: | |
259 | logic = packet->payload; | |
260 | g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length); | |
261 | if (logic->length == 0) | |
262 | break; | |
263 | ||
264 | /* Don't store any samples until triggered. */ | |
265 | if (opt_wait_trigger && !triggered) | |
266 | break; | |
267 | ||
268 | if (limit_samples && rcvd_samples_logic >= limit_samples) | |
269 | break; | |
270 | ||
271 | end_sample = rcvd_samples_logic + logic->length / logic->unitsize; | |
272 | /* Cut off last packet according to the sample limit. */ | |
273 | if (limit_samples && end_sample > limit_samples) | |
274 | end_sample = limit_samples; | |
275 | input_len = (end_sample - rcvd_samples_logic) * logic->unitsize; | |
276 | ||
277 | if (opt_output_file && default_output_format) { | |
278 | /* Saving to a session file. */ | |
279 | if (rcvd_samples_logic == 0) { | |
280 | /* First packet with logic data, init session file. */ | |
281 | probes = g_malloc(sizeof(char *) * g_slist_length(sdi->probes)); | |
282 | for (i = 0, l = sdi->probes; l; l = l->next) { | |
283 | probe = l->data; | |
284 | if (probe->enabled && probe->type == SR_PROBE_LOGIC) | |
285 | probes[i++] = probe->name; | |
286 | } | |
287 | probes[i] = NULL; | |
288 | sr_session_save_init(opt_output_file, samplerate, | |
289 | probes); | |
290 | g_free(probes); | |
291 | } | |
292 | save_chunk_logic(logic->data, input_len, logic->unitsize); | |
293 | } else { | |
294 | if (opt_pds) { | |
295 | #ifdef HAVE_SRD | |
296 | if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample, | |
297 | logic->data, input_len) != SRD_OK) | |
298 | sr_session_stop(); | |
299 | #endif | |
300 | } else { | |
301 | output_len = 0; | |
302 | if (o->format->data && packet->type == o->format->df_type) | |
303 | o->format->data(o, logic->data, input_len, | |
304 | &output_buf, &output_len); | |
305 | if (output_len) { | |
306 | fwrite(output_buf, 1, output_len, outfile); | |
307 | fflush(outfile); | |
308 | g_free(output_buf); | |
309 | } | |
310 | } | |
311 | } | |
312 | ||
313 | rcvd_samples_logic = end_sample; | |
314 | break; | |
315 | ||
316 | case SR_DF_ANALOG: | |
317 | analog = packet->payload; | |
318 | g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples); | |
319 | if (analog->num_samples == 0) | |
320 | break; | |
321 | ||
322 | if (limit_samples && rcvd_samples_analog >= limit_samples) | |
323 | break; | |
324 | ||
325 | if (o->format->data && packet->type == o->format->df_type) { | |
326 | o->format->data(o, (const uint8_t *)analog->data, | |
327 | analog->num_samples * sizeof(float), | |
328 | &output_buf, &output_len); | |
329 | if (output_buf) { | |
330 | fwrite(output_buf, 1, output_len, outfile); | |
331 | fflush(outfile); | |
332 | g_free(output_buf); | |
333 | } | |
334 | } | |
335 | ||
336 | rcvd_samples_analog += analog->num_samples; | |
337 | break; | |
338 | ||
339 | case SR_DF_FRAME_BEGIN: | |
340 | g_debug("cli: received SR_DF_FRAME_BEGIN"); | |
341 | if (o->format->event) { | |
342 | o->format->event(o, SR_DF_FRAME_BEGIN, &output_buf, | |
343 | &output_len); | |
344 | if (output_buf) { | |
345 | fwrite(output_buf, 1, output_len, outfile); | |
346 | fflush(outfile); | |
347 | g_free(output_buf); | |
348 | } | |
349 | } | |
350 | break; | |
351 | ||
352 | case SR_DF_FRAME_END: | |
353 | g_debug("cli: received SR_DF_FRAME_END"); | |
354 | if (o->format->event) { | |
355 | o->format->event(o, SR_DF_FRAME_END, &output_buf, | |
356 | &output_len); | |
357 | if (output_buf) { | |
358 | fwrite(output_buf, 1, output_len, outfile); | |
359 | fflush(outfile); | |
360 | g_free(output_buf); | |
361 | } | |
362 | } | |
363 | break; | |
364 | ||
365 | default: | |
366 | break; | |
367 | } | |
368 | ||
369 | if (o && o->format->receive) { | |
370 | if (o->format->receive(o, sdi, packet, &out) == SR_OK && out) { | |
371 | fwrite(out->str, 1, out->len, outfile); | |
372 | fflush(outfile); | |
373 | g_string_free(out, TRUE); | |
374 | } | |
375 | } | |
376 | ||
377 | /* SR_DF_END needs to be handled after the output module's receive() | |
378 | * is called, so it can properly clean up that module. */ | |
379 | if (packet->type == SR_DF_END) { | |
380 | g_debug("cli: Received SR_DF_END"); | |
381 | ||
382 | if (o->format->event) { | |
383 | o->format->event(o, SR_DF_END, &output_buf, &output_len); | |
384 | if (output_buf) { | |
385 | if (outfile) | |
386 | fwrite(output_buf, 1, output_len, outfile); | |
387 | g_free(output_buf); | |
388 | output_len = 0; | |
389 | } | |
390 | } | |
391 | ||
392 | if (o->format->cleanup) | |
393 | o->format->cleanup(o); | |
394 | g_free(o); | |
395 | o = NULL; | |
396 | ||
397 | if (outfile && outfile != stdout) | |
398 | fclose(outfile); | |
399 | ||
400 | if (opt_output_file && default_output_format) | |
401 | /* Flush whatever is left out to the session file. */ | |
402 | save_chunk_logic(NULL, 0, 0); | |
403 | ||
404 | if (limit_samples) { | |
405 | if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples) | |
406 | g_warning("Device only sent %" PRIu64 " samples.", | |
407 | rcvd_samples_logic); | |
408 | else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples) | |
409 | g_warning("Device only sent %" PRIu64 " samples.", | |
410 | rcvd_samples_analog); | |
411 | } | |
412 | } | |
413 | ||
414 | } | |
415 | ||
416 | int opt_to_gvar(char *key, char *value, struct sr_config *src) | |
417 | { | |
418 | const struct sr_config_info *srci; | |
419 | double tmp_double; | |
420 | uint64_t tmp_u64, p, q, low, high; | |
421 | GVariant *rational[2], *range[2]; | |
422 | gboolean tmp_bool; | |
423 | int ret; | |
424 | ||
425 | if (!(srci = sr_config_info_name_get(key))) { | |
426 | g_critical("Unknown device option '%s'.", (char *) key); | |
427 | return -1; | |
428 | } | |
429 | src->key = srci->key; | |
430 | ||
431 | if ((value == NULL) && | |
432 | (srci->datatype != SR_T_BOOL)) { | |
433 | g_critical("Option '%s' needs a value.", (char *)key); | |
434 | return -1; | |
435 | } | |
436 | ||
437 | ret = 0; | |
438 | switch (srci->datatype) { | |
439 | case SR_T_UINT64: | |
440 | ret = sr_parse_sizestring(value, &tmp_u64); | |
441 | if (ret != 0) | |
442 | break; | |
443 | src->data = g_variant_new_uint64(tmp_u64); | |
444 | break; | |
445 | case SR_T_INT32: | |
446 | ret = sr_parse_sizestring(value, &tmp_u64); | |
447 | if (ret != 0) | |
448 | break; | |
449 | src->data = g_variant_new_int32(tmp_u64); | |
450 | break; | |
451 | case SR_T_CHAR: | |
452 | src->data = g_variant_new_string(value); | |
453 | break; | |
454 | case SR_T_BOOL: | |
455 | if (!value) | |
456 | tmp_bool = TRUE; | |
457 | else | |
458 | tmp_bool = sr_parse_boolstring(value); | |
459 | src->data = g_variant_new_boolean(tmp_bool); | |
460 | break; | |
461 | case SR_T_FLOAT: | |
462 | tmp_double = strtof(value, NULL); | |
463 | src->data = g_variant_new_double(tmp_double); | |
464 | break; | |
465 | case SR_T_RATIONAL_PERIOD: | |
466 | if ((ret = sr_parse_period(value, &p, &q)) != SR_OK) | |
467 | break; | |
468 | rational[0] = g_variant_new_uint64(p); | |
469 | rational[1] = g_variant_new_uint64(q); | |
470 | src->data = g_variant_new_tuple(rational, 2); | |
471 | break; | |
472 | case SR_T_RATIONAL_VOLT: | |
473 | if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK) | |
474 | break; | |
475 | rational[0] = g_variant_new_uint64(p); | |
476 | rational[1] = g_variant_new_uint64(q); | |
477 | src->data = g_variant_new_tuple(rational, 2); | |
478 | break; | |
479 | case SR_T_UINT64_RANGE: | |
480 | if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) { | |
481 | ret = -1; | |
482 | break; | |
483 | } else { | |
484 | range[0] = g_variant_new_uint64(low); | |
485 | range[1] = g_variant_new_uint64(high); | |
486 | src->data = g_variant_new_tuple(range, 2); | |
487 | } | |
488 | break; | |
489 | default: | |
490 | ret = -1; | |
491 | } | |
492 | ||
493 | return ret; | |
494 | } | |
495 | ||
496 | int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args) | |
497 | { | |
498 | struct sr_config src; | |
499 | struct sr_probe_group *pg; | |
500 | GHashTableIter iter; | |
501 | gpointer key, value; | |
502 | int ret; | |
503 | ||
504 | g_hash_table_iter_init(&iter, args); | |
505 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
506 | if ((ret = opt_to_gvar(key, value, &src)) != 0) | |
507 | return ret; | |
508 | pg = select_probe_group(sdi); | |
509 | ret = sr_config_set(sdi, pg, src.key, src.data); | |
510 | if (ret != SR_OK) { | |
511 | g_critical("Failed to set device option '%s'.", (char *)key); | |
512 | return ret; | |
513 | } | |
514 | } | |
515 | ||
516 | return SR_OK; | |
517 | } | |
518 | ||
519 | void run_session(void) | |
520 | { | |
521 | GSList *devices; | |
522 | GHashTable *devargs; | |
523 | GVariant *gvar; | |
524 | struct sr_dev_inst *sdi; | |
525 | uint64_t min_samples, max_samples; | |
526 | int max_probes, i; | |
527 | char **triggerlist; | |
528 | ||
529 | devices = device_scan(); | |
530 | if (!devices) { | |
531 | g_critical("No devices found."); | |
532 | return; | |
533 | } | |
534 | if (g_slist_length(devices) > 1) { | |
535 | g_critical("sigrok-cli only supports one device for capturing."); | |
536 | return; | |
537 | } | |
538 | sdi = devices->data; | |
539 | ||
540 | sr_session_new(); | |
541 | sr_session_datafeed_callback_add(datafeed_in, NULL); | |
542 | ||
543 | if (sr_dev_open(sdi) != SR_OK) { | |
544 | g_critical("Failed to open device."); | |
545 | return; | |
546 | } | |
547 | ||
548 | if (sr_session_dev_add(sdi) != SR_OK) { | |
549 | g_critical("Failed to add device to session."); | |
550 | sr_session_destroy(); | |
551 | return; | |
552 | } | |
553 | ||
554 | if (opt_config) { | |
555 | if ((devargs = parse_generic_arg(opt_config, FALSE))) { | |
556 | if (set_dev_options(sdi, devargs) != SR_OK) | |
557 | return; | |
558 | g_hash_table_destroy(devargs); | |
559 | } | |
560 | } | |
561 | ||
562 | if (select_probes(sdi) != SR_OK) { | |
563 | g_critical("Failed to set probes."); | |
564 | sr_session_destroy(); | |
565 | return; | |
566 | } | |
567 | ||
568 | if (opt_triggers) { | |
569 | if (!(triggerlist = sr_parse_triggerstring(sdi, opt_triggers))) { | |
570 | sr_session_destroy(); | |
571 | return; | |
572 | } | |
573 | max_probes = g_slist_length(sdi->probes); | |
574 | for (i = 0; i < max_probes; i++) { | |
575 | if (triggerlist[i]) { | |
576 | sr_dev_trigger_set(sdi, i, triggerlist[i]); | |
577 | g_free(triggerlist[i]); | |
578 | } | |
579 | } | |
580 | g_free(triggerlist); | |
581 | } | |
582 | ||
583 | if (opt_continuous) { | |
584 | if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) { | |
585 | g_critical("This device does not support continuous sampling."); | |
586 | sr_session_destroy(); | |
587 | return; | |
588 | } | |
589 | } | |
590 | ||
591 | if (opt_time) { | |
592 | if (set_limit_time(sdi) != SR_OK) { | |
593 | sr_session_destroy(); | |
594 | return; | |
595 | } | |
596 | } | |
597 | ||
598 | if (opt_samples) { | |
599 | if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) { | |
600 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
601 | sr_session_destroy(); | |
602 | return; | |
603 | } | |
604 | if (sr_config_list(sdi->driver, sdi, NULL, | |
605 | SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) { | |
606 | /* The device has no compression, or compression is turned | |
607 | * off, and publishes its sample memory size. */ | |
608 | g_variant_get(gvar, "(tt)", &min_samples, &max_samples); | |
609 | g_variant_unref(gvar); | |
610 | if (limit_samples < min_samples) { | |
611 | g_critical("The device stores at least %"PRIu64 | |
612 | " samples with the current settings.", min_samples); | |
613 | } | |
614 | if (limit_samples > max_samples) { | |
615 | g_critical("The device can store only %"PRIu64 | |
616 | " samples with the current settings.", max_samples); | |
617 | } | |
618 | } | |
619 | gvar = g_variant_new_uint64(limit_samples); | |
620 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { | |
621 | g_critical("Failed to configure sample limit."); | |
622 | sr_session_destroy(); | |
623 | return; | |
624 | } | |
625 | } | |
626 | ||
627 | if (opt_frames) { | |
628 | if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) { | |
629 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
630 | sr_session_destroy(); | |
631 | return; | |
632 | } | |
633 | gvar = g_variant_new_uint64(limit_frames); | |
634 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) { | |
635 | g_critical("Failed to configure frame limit."); | |
636 | sr_session_destroy(); | |
637 | return; | |
638 | } | |
639 | } | |
640 | ||
641 | if (sr_session_start() != SR_OK) { | |
642 | g_critical("Failed to start session."); | |
643 | sr_session_destroy(); | |
644 | return; | |
645 | } | |
646 | ||
647 | if (opt_continuous) | |
648 | add_anykey(); | |
649 | ||
650 | sr_session_run(); | |
651 | ||
652 | if (opt_continuous) | |
653 | clear_anykey(); | |
654 | ||
655 | sr_session_datafeed_callback_remove_all(); | |
656 | sr_session_destroy(); | |
657 | g_slist_free(devices); | |
658 | ||
659 | } | |
660 | ||
661 | void save_chunk_logic(uint8_t *data, uint64_t data_len, int unitsize) | |
662 | { | |
663 | static uint8_t *buf = NULL; | |
664 | static int buf_len = 0; | |
665 | static int last_unitsize = 0; | |
666 | int max; | |
667 | ||
668 | if (!buf) | |
669 | buf = g_malloc(SAVE_CHUNK_SIZE); | |
670 | ||
671 | if (buf_len + data_len > SAVE_CHUNK_SIZE) { | |
672 | max = (SAVE_CHUNK_SIZE - buf_len) / unitsize * unitsize; | |
673 | memcpy(buf + buf_len, data, max); | |
674 | sr_session_append(opt_output_file, buf, unitsize, | |
675 | (buf_len + max) / unitsize); | |
676 | memcpy(buf, data + max, data_len - max); | |
677 | buf_len = data_len - max; | |
678 | } else if (data_len == 0) { | |
679 | /* End of data, flush the buffer out. */ | |
680 | sr_session_append(opt_output_file, buf, last_unitsize, | |
681 | buf_len / last_unitsize); | |
682 | } else { | |
683 | /* Buffer chunk. */ | |
684 | memcpy(buf + buf_len, data, data_len); | |
685 | buf_len += data_len; | |
686 | } | |
687 | last_unitsize = unitsize; | |
688 | ||
689 | } |