]> sigrok.org Git - sigrok-cli.git/blame - session.c
Enumerate output module options according to API change.
[sigrok-cli.git] / session.c
CommitLineData
2be182e6
BV
1/*
2 * This file is part of the sigrok-cli project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20fb52e0 20#include "sigrok-cli.h"
2be182e6
BV
21#include <glib.h>
22#include <glib/gstdio.h>
55de58a2
UH
23#include <string.h>
24#include <stdlib.h>
2be182e6 25
2be182e6 26static int default_output_format = FALSE;
2be182e6
BV
27static uint64_t limit_samples = 0;
28static uint64_t limit_frames = 0;
29
2be182e6
BV
30#ifdef HAVE_SRD
31extern struct srd_session *srd_sess;
32#endif
33
2be182e6
BV
34static int set_limit_time(const struct sr_dev_inst *sdi)
35{
36 GVariant *gvar;
37 uint64_t time_msec;
38 uint64_t samplerate;
39
40 if (!(time_msec = sr_parse_timestring(opt_time))) {
41 g_critical("Invalid time '%s'", opt_time);
42 return SR_ERR;
43 }
44
45 if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) {
46 gvar = g_variant_new_uint64(time_msec);
47 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
48 g_critical("Failed to configure time limit.");
49 return SR_ERR;
50 }
51 } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
52 /* Convert to samples based on the samplerate. */
53 sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar);
54 samplerate = g_variant_get_uint64(gvar);
55 g_variant_unref(gvar);
56 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
57 if (limit_samples == 0) {
58 g_critical("Not enough time at this samplerate.");
59 return SR_ERR;
60 }
61 gvar = g_variant_new_uint64(limit_samples);
62 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
63 g_critical("Failed to configure time-based sample limit.");
64 return SR_ERR;
65 }
66 } else {
67 g_critical("This device does not support time limits.");
68 return SR_ERR;
69 }
70
71 return SR_OK;
72}
73
b61a8850 74GHashTable *generic_arg_to_opt(const struct sr_option **opts, GHashTable *genargs)
9216694f 75{
ad6520c4
BV
76 GHashTable *hash;
77 GVariant *gvar;
78 const struct sr_option *opt;
79 char *s;
80
81 hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
82 (GDestroyNotify)g_variant_unref);
b61a8850 83 for (opt = opts[0]; opt; opt++) {
ad6520c4
BV
84 if (!(s = g_hash_table_lookup(genargs, opt->id)))
85 continue;
86 if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE_UINT32)) {
87 gvar = g_variant_new_uint32(strtoul(s, NULL, 10));
88 g_hash_table_insert(hash, g_strdup(opt->id),
89 g_variant_ref_sink(gvar));
a30daa03
BV
90 } else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE_DOUBLE)) {
91 gvar = g_variant_new_double(strtod(s, NULL));
92 g_hash_table_insert(hash, g_strdup(opt->id),
93 g_variant_ref_sink(gvar));
ad6520c4 94 } else {
a30daa03 95 g_critical("Don't know GVariant type for option '%s'!", opt->id);
ad6520c4 96 }
9216694f
BV
97 }
98
ad6520c4 99 return hash;
9216694f
BV
100}
101
ad6520c4 102const struct sr_output *setup_output_format(const struct sr_dev_inst *sdi)
2be182e6 103{
ad6520c4 104 const struct sr_output_module *omod;
b61a8850 105 const struct sr_option **opts;
ad6520c4
BV
106 const struct sr_output *o;
107 GHashTable *fmtargs, *fmtopts;
2be182e6
BV
108 char *fmtspec;
109
110 if (opt_output_format && !strcmp(opt_output_format, "sigrok")) {
111 /* Doesn't really exist as an output module - this is
112 * the session save mode. */
113 g_free(opt_output_format);
114 opt_output_format = NULL;
115 }
116
117 if (!opt_output_format) {
118 opt_output_format = DEFAULT_OUTPUT_FORMAT;
119 /* we'll need to remember this so when saving to a file
120 * later, sigrok session format will be used.
121 */
122 default_output_format = TRUE;
123 }
124
125 fmtargs = parse_generic_arg(opt_output_format, TRUE);
126 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
6709a694 127 if (!fmtspec)
2be182e6 128 g_critical("Invalid output format.");
ad6520c4 129 if (!(omod = sr_output_find(fmtspec)))
9216694f
BV
130 g_critical("Unknown output format '%s'.", fmtspec);
131 g_hash_table_remove(fmtargs, "sigrok_key");
ad6520c4
BV
132 if ((opts = sr_output_options_get(omod))) {
133 fmtopts = generic_arg_to_opt(opts, fmtargs);
b61a8850 134 sr_output_options_free(opts);
ad6520c4
BV
135 } else
136 fmtopts = NULL;
137 o = sr_output_new(omod, fmtopts, sdi);
138 if (fmtopts)
139 g_hash_table_destroy(fmtopts);
2be182e6
BV
140 g_hash_table_destroy(fmtargs);
141
6709a694 142 return o;
2be182e6
BV
143}
144
145void datafeed_in(const struct sr_dev_inst *sdi,
146 const struct sr_datafeed_packet *packet, void *cb_data)
147{
148 const struct sr_datafeed_meta *meta;
149 const struct sr_datafeed_logic *logic;
150 const struct sr_datafeed_analog *analog;
4bf77ec6 151 struct sr_session *session;
2be182e6 152 struct sr_config *src;
029d73fe 153 struct sr_channel *ch;
ad6520c4
BV
154 static const struct sr_output *o = NULL;
155 static const struct sr_output *oa = NULL;
6ea663a7
BV
156 static uint64_t rcvd_samples_logic = 0;
157 static uint64_t rcvd_samples_analog = 0;
158 static uint64_t samplerate = 0;
2be182e6
BV
159 static int triggered = 0;
160 static FILE *outfile = NULL;
161 GSList *l;
162 GString *out;
8b65cdcc 163 GVariant *gvar;
55de58a2 164 uint64_t end_sample;
667d4a18 165 uint64_t input_len;
6ea663a7 166 int i;
029d73fe 167 char **channels;
2be182e6 168
2be182e6
BV
169 /* If the first packet to come in isn't a header, don't even try. */
170 if (packet->type != SR_DF_HEADER && o == NULL)
171 return;
172
4bf77ec6 173 session = cb_data;
2be182e6
BV
174 switch (packet->type) {
175 case SR_DF_HEADER:
4ed1cdea 176 g_debug("cli: Received SR_DF_HEADER.");
e786e625
BV
177 if (!(o = setup_output_format(sdi)))
178 g_critical("Failed to initialize output module.");
2be182e6 179
9216694f 180 /* Set up backup analog output module. */
ad6520c4 181 oa = sr_output_new(sr_output_find("analog"), NULL, sdi);
9216694f 182
2be182e6
BV
183 /* Prepare non-stdout output. */
184 outfile = stdout;
185 if (opt_output_file) {
186 if (default_output_format) {
2be182e6 187 outfile = NULL;
2be182e6
BV
188 } else {
189 /* saving to a file in whatever format was set
190 * with --format, so all we need is a filehandle */
191 outfile = g_fopen(opt_output_file, "wb");
192 }
193 }
6ea663a7 194 rcvd_samples_logic = rcvd_samples_analog = 0;
2be182e6 195
8b65cdcc
BV
196 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
197 &gvar) == SR_OK) {
198 samplerate = g_variant_get_uint64(gvar);
199 g_variant_unref(gvar);
200 }
201
2be182e6 202#ifdef HAVE_SRD
ea6d6dec 203 if (opt_pds) {
8b65cdcc 204 if (samplerate) {
2be182e6
BV
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:
4ed1cdea 220 g_debug("cli: Received SR_DF_META.");
2be182e6
BV
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);
4ed1cdea 227 g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate);
2be182e6
BV
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);
4ed1cdea 239 g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate);
2be182e6
BV
240 break;
241 default:
242 /* Unknown metadata is not an error. */
243 break;
244 }
245 }
246 break;
247
248 case SR_DF_TRIGGER:
4ed1cdea 249 g_debug("cli: Received SR_DF_TRIGGER.");
2be182e6
BV
250 triggered = 1;
251 break;
252
253 case SR_DF_LOGIC:
254 logic = packet->payload;
4ed1cdea
UH
255 g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
256 logic->length, logic->unitsize);
2be182e6
BV
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
6ea663a7 264 if (limit_samples && rcvd_samples_logic >= limit_samples)
2be182e6
BV
265 break;
266
6ea663a7 267 end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
8667d3c2
DE
268 /* Cut off last packet according to the sample limit. */
269 if (limit_samples && end_sample > limit_samples)
270 end_sample = limit_samples;
6ea663a7 271 input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;
2be182e6
BV
272
273 if (opt_output_file && default_output_format) {
274 /* Saving to a session file. */
6ea663a7
BV
275 if (rcvd_samples_logic == 0) {
276 /* First packet with logic data, init session file. */
029d73fe
UH
277 channels = g_malloc(sizeof(char *) * g_slist_length(sdi->channels));
278 for (i = 0, l = sdi->channels; l; l = l->next) {
279 ch = l->data;
280 if (ch->enabled && ch->type == SR_CHANNEL_LOGIC)
281 channels[i++] = ch->name;
6ea663a7 282 }
029d73fe 283 channels[i] = NULL;
4bf77ec6
BV
284 sr_session_save_init(session, opt_output_file,
285 samplerate, channels);
029d73fe 286 g_free(channels);
6ea663a7 287 }
4bf77ec6 288 save_chunk_logic(session, logic->data, input_len, logic->unitsize);
2be182e6
BV
289 } else {
290 if (opt_pds) {
291#ifdef HAVE_SRD
6ea663a7 292 if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
8667d3c2 293 logic->data, input_len) != SRD_OK)
4bf77ec6 294 sr_session_stop(session);
2be182e6 295#endif
2be182e6
BV
296 }
297 }
2be182e6 298
6ea663a7 299 rcvd_samples_logic = end_sample;
2be182e6
BV
300 break;
301
302 case SR_DF_ANALOG:
303 analog = packet->payload;
4ed1cdea 304 g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples);
2be182e6
BV
305 if (analog->num_samples == 0)
306 break;
307
6ea663a7 308 if (limit_samples && rcvd_samples_analog >= limit_samples)
2be182e6
BV
309 break;
310
6ea663a7 311 rcvd_samples_analog += analog->num_samples;
2be182e6
BV
312 break;
313
314 case SR_DF_FRAME_BEGIN:
4ed1cdea 315 g_debug("cli: Received SR_DF_FRAME_BEGIN.");
2be182e6
BV
316 break;
317
318 case SR_DF_FRAME_END:
4ed1cdea 319 g_debug("cli: Received SR_DF_FRAME_END.");
2be182e6
BV
320 break;
321
322 default:
323 break;
324 }
325
f4ffd032 326 if (o && outfile && !opt_pds) {
9216694f
BV
327 if (sr_output_send(o, packet, &out) == SR_OK) {
328 if (!out || (out->len == 0 && default_output_format
329 && packet->type == SR_DF_ANALOG)) {
330 /* The user didn't specify an output module,
331 * but needs to see this analog data. */
332 sr_output_send(oa, packet, &out);
333 }
334 if (out && out->len > 0) {
335 fwrite(out->str, 1, out->len, outfile);
336 fflush(outfile);
337 }
338 if (out)
339 g_string_free(out, TRUE);
2be182e6
BV
340 }
341 }
342
343 /* SR_DF_END needs to be handled after the output module's receive()
6ea663a7 344 * is called, so it can properly clean up that module. */
2be182e6 345 if (packet->type == SR_DF_END) {
4ed1cdea 346 g_debug("cli: Received SR_DF_END.");
2be182e6 347
6709a694
BV
348 if (o)
349 sr_output_free(o);
2be182e6
BV
350 o = NULL;
351
9216694f
BV
352 sr_output_free(oa);
353 oa = NULL;
354
2be182e6
BV
355 if (outfile && outfile != stdout)
356 fclose(outfile);
357
6ea663a7
BV
358 if (opt_output_file && default_output_format)
359 /* Flush whatever is left out to the session file. */
4bf77ec6 360 save_chunk_logic(session, NULL, 0, 0);
6ea663a7
BV
361
362 if (limit_samples) {
363 if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
364 g_warning("Device only sent %" PRIu64 " samples.",
365 rcvd_samples_logic);
366 else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples)
367 g_warning("Device only sent %" PRIu64 " samples.",
368 rcvd_samples_analog);
2be182e6
BV
369 }
370 }
371
372}
373
ad54a1a6 374int opt_to_gvar(char *key, char *value, struct sr_config *src)
2be182e6
BV
375{
376 const struct sr_config_info *srci;
426d0cda 377 double tmp_double, dlow, dhigh;
2be182e6 378 uint64_t tmp_u64, p, q, low, high;
ad54a1a6 379 GVariant *rational[2], *range[2];
2be182e6 380 gboolean tmp_bool;
ad54a1a6 381 int ret;
2be182e6 382
ad54a1a6
BV
383 if (!(srci = sr_config_info_name_get(key))) {
384 g_critical("Unknown device option '%s'.", (char *) key);
385 return -1;
386 }
387 src->key = srci->key;
2be182e6 388
ad54a1a6
BV
389 if ((value == NULL) &&
390 (srci->datatype != SR_T_BOOL)) {
391 g_critical("Option '%s' needs a value.", (char *)key);
392 return -1;
393 }
394
395 ret = 0;
396 switch (srci->datatype) {
397 case SR_T_UINT64:
398 ret = sr_parse_sizestring(value, &tmp_u64);
399 if (ret != 0)
2be182e6 400 break;
ad54a1a6
BV
401 src->data = g_variant_new_uint64(tmp_u64);
402 break;
403 case SR_T_INT32:
404 ret = sr_parse_sizestring(value, &tmp_u64);
405 if (ret != 0)
2be182e6 406 break;
ad54a1a6
BV
407 src->data = g_variant_new_int32(tmp_u64);
408 break;
9db40e9f 409 case SR_T_STRING:
ad54a1a6
BV
410 src->data = g_variant_new_string(value);
411 break;
412 case SR_T_BOOL:
413 if (!value)
414 tmp_bool = TRUE;
415 else
416 tmp_bool = sr_parse_boolstring(value);
417 src->data = g_variant_new_boolean(tmp_bool);
418 break;
419 case SR_T_FLOAT:
420 tmp_double = strtof(value, NULL);
421 src->data = g_variant_new_double(tmp_double);
422 break;
423 case SR_T_RATIONAL_PERIOD:
424 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
2be182e6 425 break;
ad54a1a6
BV
426 rational[0] = g_variant_new_uint64(p);
427 rational[1] = g_variant_new_uint64(q);
428 src->data = g_variant_new_tuple(rational, 2);
429 break;
430 case SR_T_RATIONAL_VOLT:
431 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
2be182e6 432 break;
ad54a1a6
BV
433 rational[0] = g_variant_new_uint64(p);
434 rational[1] = g_variant_new_uint64(q);
435 src->data = g_variant_new_tuple(rational, 2);
436 break;
437 case SR_T_UINT64_RANGE:
438 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
439 ret = -1;
2be182e6 440 break;
ad54a1a6
BV
441 } else {
442 range[0] = g_variant_new_uint64(low);
443 range[1] = g_variant_new_uint64(high);
444 src->data = g_variant_new_tuple(range, 2);
2be182e6 445 }
ad54a1a6 446 break;
426d0cda
BV
447 case SR_T_DOUBLE_RANGE:
448 if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) {
449 ret = -1;
450 break;
451 } else {
452 range[0] = g_variant_new_double(dlow);
453 range[1] = g_variant_new_double(dhigh);
454 src->data = g_variant_new_tuple(range, 2);
455 }
456 break;
ad54a1a6
BV
457 default:
458 ret = -1;
459 }
460
461 return ret;
462}
463
464int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
465{
466 struct sr_config src;
ca50f4b3 467 struct sr_channel_group *cg;
ad54a1a6
BV
468 GHashTableIter iter;
469 gpointer key, value;
470 int ret;
471
472 g_hash_table_iter_init(&iter, args);
473 while (g_hash_table_iter_next(&iter, &key, &value)) {
474 if ((ret = opt_to_gvar(key, value, &src)) != 0)
475 return ret;
ca50f4b3
UH
476 cg = select_channel_group(sdi);
477 ret = sr_config_set(sdi, cg, src.key, src.data);
2be182e6
BV
478 if (ret != SR_OK) {
479 g_critical("Failed to set device option '%s'.", (char *)key);
480 return ret;
481 }
482 }
483
484 return SR_OK;
485}
486
487void run_session(void)
488{
489 GSList *devices;
490 GHashTable *devargs;
491 GVariant *gvar;
4bf77ec6
BV
492 struct sr_session *session;
493 struct sr_trigger *trigger;
2be182e6 494 struct sr_dev_inst *sdi;
02c65935 495 uint64_t min_samples, max_samples;
2be182e6
BV
496
497 devices = device_scan();
498 if (!devices) {
499 g_critical("No devices found.");
500 return;
501 }
502 if (g_slist_length(devices) > 1) {
503 g_critical("sigrok-cli only supports one device for capturing.");
b4eece7c 504 g_slist_free(devices);
2be182e6
BV
505 return;
506 }
507 sdi = devices->data;
b4eece7c 508 g_slist_free(devices);
2be182e6 509
4bf77ec6
BV
510 sr_session_new(&session);
511 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
2be182e6
BV
512
513 if (sr_dev_open(sdi) != SR_OK) {
514 g_critical("Failed to open device.");
515 return;
516 }
517
4bf77ec6 518 if (sr_session_dev_add(session, sdi) != SR_OK) {
2be182e6 519 g_critical("Failed to add device to session.");
4bf77ec6 520 sr_session_destroy(session);
2be182e6
BV
521 return;
522 }
523
524 if (opt_config) {
525 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
526 if (set_dev_options(sdi, devargs) != SR_OK)
527 return;
528 g_hash_table_destroy(devargs);
529 }
530 }
531
029d73fe
UH
532 if (select_channels(sdi) != SR_OK) {
533 g_critical("Failed to set channels.");
4bf77ec6 534 sr_session_destroy(session);
2be182e6
BV
535 return;
536 }
537
538 if (opt_triggers) {
4bf77ec6
BV
539 if (!parse_triggerstring(sdi, opt_triggers, &trigger)) {
540 sr_session_destroy(session);
541 return;
542 }
543 if (sr_session_trigger_set(session, trigger) != SR_OK) {
544 sr_session_destroy(session);
2be182e6
BV
545 return;
546 }
2be182e6
BV
547 }
548
549 if (opt_continuous) {
550 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
551 g_critical("This device does not support continuous sampling.");
4bf77ec6 552 sr_session_destroy(session);
2be182e6
BV
553 return;
554 }
555 }
556
557 if (opt_time) {
558 if (set_limit_time(sdi) != SR_OK) {
4bf77ec6 559 sr_session_destroy(session);
2be182e6
BV
560 return;
561 }
562 }
563
564 if (opt_samples) {
565 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
566 g_critical("Invalid sample limit '%s'.", opt_samples);
4bf77ec6 567 sr_session_destroy(session);
2be182e6
BV
568 return;
569 }
02c65935
BV
570 if (sr_config_list(sdi->driver, sdi, NULL,
571 SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) {
c7a5cb12
BV
572 /* The device has no compression, or compression is turned
573 * off, and publishes its sample memory size. */
02c65935 574 g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
be781321 575 g_variant_unref(gvar);
02c65935
BV
576 if (limit_samples < min_samples) {
577 g_critical("The device stores at least %"PRIu64
578 " samples with the current settings.", min_samples);
579 }
c7a5cb12
BV
580 if (limit_samples > max_samples) {
581 g_critical("The device can store only %"PRIu64
582 " samples with the current settings.", max_samples);
583 }
584 }
2be182e6
BV
585 gvar = g_variant_new_uint64(limit_samples);
586 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
587 g_critical("Failed to configure sample limit.");
4bf77ec6 588 sr_session_destroy(session);
2be182e6
BV
589 return;
590 }
591 }
592
593 if (opt_frames) {
594 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
595 g_critical("Invalid sample limit '%s'.", opt_samples);
4bf77ec6 596 sr_session_destroy(session);
2be182e6
BV
597 return;
598 }
599 gvar = g_variant_new_uint64(limit_frames);
600 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
601 g_critical("Failed to configure frame limit.");
4bf77ec6 602 sr_session_destroy(session);
2be182e6
BV
603 return;
604 }
605 }
606
4bf77ec6 607 if (sr_session_start(session) != SR_OK) {
2be182e6 608 g_critical("Failed to start session.");
4bf77ec6 609 sr_session_destroy(session);
2be182e6
BV
610 return;
611 }
612
613 if (opt_continuous)
4bf77ec6 614 add_anykey(session);
2be182e6 615
4bf77ec6 616 sr_session_run(session);
2be182e6
BV
617
618 if (opt_continuous)
619 clear_anykey();
620
4bf77ec6
BV
621 sr_session_datafeed_callback_remove_all(session);
622 sr_session_destroy(session);
2be182e6
BV
623
624}
625
4bf77ec6
BV
626void save_chunk_logic(struct sr_session *session, uint8_t *data,
627 uint64_t data_len, int unitsize)
6ea663a7
BV
628{
629 static uint8_t *buf = NULL;
630 static int buf_len = 0;
631 static int last_unitsize = 0;
632 int max;
633
634 if (!buf)
635 buf = g_malloc(SAVE_CHUNK_SIZE);
636
64c74f25
BV
637 while (data_len > SAVE_CHUNK_SIZE) {
638 save_chunk_logic(session, data, SAVE_CHUNK_SIZE, unitsize);
639 data += SAVE_CHUNK_SIZE;
640 data_len -= SAVE_CHUNK_SIZE;
641 }
642
6ea663a7
BV
643 if (buf_len + data_len > SAVE_CHUNK_SIZE) {
644 max = (SAVE_CHUNK_SIZE - buf_len) / unitsize * unitsize;
645 memcpy(buf + buf_len, data, max);
4bf77ec6 646 sr_session_append(session, opt_output_file, buf, unitsize,
6ea663a7
BV
647 (buf_len + max) / unitsize);
648 memcpy(buf, data + max, data_len - max);
649 buf_len = data_len - max;
efe1b57d 650 } else if (data_len == 0 && last_unitsize != 0) {
6ea663a7 651 /* End of data, flush the buffer out. */
4bf77ec6 652 sr_session_append(session, opt_output_file, buf, last_unitsize,
6ea663a7
BV
653 buf_len / last_unitsize);
654 } else {
655 /* Buffer chunk. */
656 memcpy(buf + buf_len, data, data_len);
657 buf_len += data_len;
658 }
659 last_unitsize = unitsize;
660
661}