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