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