]> sigrok.org Git - sigrok-cli.git/blob - session.c
Always get samplerate at the start of acquisition.
[sigrok-cli.git] / session.c
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_channel *ch;
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         GVariant *gvar;
158         uint64_t end_sample;
159         uint64_t output_len, input_len;
160         uint8_t *output_buf;
161         int i;
162         char **channels;
163
164         (void) cb_data;
165
166         /* If the first packet to come in isn't a header, don't even try. */
167         if (packet->type != SR_DF_HEADER && o == NULL)
168                 return;
169
170         switch (packet->type) {
171         case SR_DF_HEADER:
172                 g_debug("cli: Received SR_DF_HEADER");
173                 /* Initialize the output module. */
174                 if (!(o = g_try_malloc(sizeof(struct sr_output)))) {
175                         g_critical("Output module malloc failed.");
176                         exit(1);
177                 }
178                 o->format = output_format;
179                 o->sdi = (struct sr_dev_inst *)sdi;
180                 o->param = output_format_param;
181                 if (o->format->init) {
182                         if (o->format->init(o) != SR_OK) {
183                                 g_critical("Output format initialization failed.");
184                                 exit(1);
185                         }
186                 }
187
188                 /* Prepare non-stdout output. */
189                 outfile = stdout;
190                 if (opt_output_file) {
191                         if (default_output_format) {
192                                 outfile = NULL;
193                         } else {
194                                 /* saving to a file in whatever format was set
195                                  * with --format, so all we need is a filehandle */
196                                 outfile = g_fopen(opt_output_file, "wb");
197                         }
198                 }
199                 rcvd_samples_logic = rcvd_samples_analog = 0;
200
201                 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
202                                 &gvar) == SR_OK) {
203                         samplerate = g_variant_get_uint64(gvar);
204                         g_variant_unref(gvar);
205                 }
206
207 #ifdef HAVE_SRD
208                 if (opt_pds) {
209                         if (samplerate) {
210                                 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
211                                                 g_variant_new_uint64(samplerate)) != SRD_OK) {
212                                         g_critical("Failed to configure decode session.");
213                                         break;
214                                 }
215                         }
216                         if (srd_session_start(srd_sess) != SRD_OK) {
217                                 g_critical("Failed to start decode session.");
218                                 break;
219                         }
220                 }
221 #endif
222                 break;
223
224         case SR_DF_META:
225                 g_debug("cli: received SR_DF_META");
226                 meta = packet->payload;
227                 for (l = meta->config; l; l = l->next) {
228                         src = l->data;
229                         switch (src->key) {
230                         case SR_CONF_SAMPLERATE:
231                                 samplerate = g_variant_get_uint64(src->data);
232                                 g_debug("cli: got samplerate %"PRIu64" Hz", samplerate);
233 #ifdef HAVE_SRD
234                                 if (opt_pds) {
235                                         if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
236                                                         g_variant_new_uint64(samplerate)) != SRD_OK) {
237                                                 g_critical("Failed to pass samplerate to decoder.");
238                                         }
239                                 }
240 #endif
241                                 break;
242                         case SR_CONF_SAMPLE_INTERVAL:
243                                 samplerate = g_variant_get_uint64(src->data);
244                                 g_debug("cli: got sample interval %"PRIu64" ms", samplerate);
245                                 break;
246                         default:
247                                 /* Unknown metadata is not an error. */
248                                 break;
249                         }
250                 }
251                 break;
252
253         case SR_DF_TRIGGER:
254                 g_debug("cli: received SR_DF_TRIGGER");
255                 if (o->format->event)
256                         o->format->event(o, SR_DF_TRIGGER, &output_buf,
257                                          &output_len);
258                 triggered = 1;
259                 break;
260
261         case SR_DF_LOGIC:
262                 logic = packet->payload;
263                 g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length);
264                 if (logic->length == 0)
265                         break;
266
267                 /* Don't store any samples until triggered. */
268                 if (opt_wait_trigger && !triggered)
269                         break;
270
271                 if (limit_samples && rcvd_samples_logic >= limit_samples)
272                         break;
273
274                 end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
275                 /* Cut off last packet according to the sample limit. */
276                 if (limit_samples && end_sample > limit_samples)
277                         end_sample = limit_samples;
278                 input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;
279
280                 if (opt_output_file && default_output_format) {
281                         /* Saving to a session file. */
282                         if (rcvd_samples_logic == 0) {
283                                 /* First packet with logic data, init session file. */
284                                 channels = g_malloc(sizeof(char *) * g_slist_length(sdi->channels));
285                                 for (i = 0, l = sdi->channels; l; l = l->next) {
286                                         ch = l->data;
287                                         if (ch->enabled && ch->type == SR_CHANNEL_LOGIC)
288                                                 channels[i++] = ch->name;
289                                 }
290                                 channels[i] = NULL;
291                                 sr_session_save_init(opt_output_file, samplerate,
292                                                 channels);
293                                 g_free(channels);
294                         }
295                         save_chunk_logic(logic->data, input_len, logic->unitsize);
296                 } else {
297                         if (opt_pds) {
298 #ifdef HAVE_SRD
299                                 if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
300                                                 logic->data, input_len) != SRD_OK)
301                                         sr_session_stop();
302 #endif
303                         } else {
304                                 output_len = 0;
305                                 if (o->format->data && packet->type == o->format->df_type)
306                                         o->format->data(o, logic->data, input_len,
307                                                         &output_buf, &output_len);
308                                 if (output_len) {
309                                         fwrite(output_buf, 1, output_len, outfile);
310                                         fflush(outfile);
311                                         g_free(output_buf);
312                                 }
313                         }
314                 }
315
316                 rcvd_samples_logic = end_sample;
317                 break;
318
319         case SR_DF_ANALOG:
320                 analog = packet->payload;
321                 g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples);
322                 if (analog->num_samples == 0)
323                         break;
324
325                 if (limit_samples && rcvd_samples_analog >= limit_samples)
326                         break;
327
328                 if (o->format->data && packet->type == o->format->df_type) {
329                         o->format->data(o, (const uint8_t *)analog->data,
330                                         analog->num_samples * sizeof(float),
331                                         &output_buf, &output_len);
332                         if (output_buf) {
333                                 fwrite(output_buf, 1, output_len, outfile);
334                                 fflush(outfile);
335                                 g_free(output_buf);
336                         }
337                 }
338
339                 rcvd_samples_analog += analog->num_samples;
340                 break;
341
342         case SR_DF_FRAME_BEGIN:
343                 g_debug("cli: received SR_DF_FRAME_BEGIN");
344                 if (o->format->event) {
345                         o->format->event(o, SR_DF_FRAME_BEGIN, &output_buf,
346                                          &output_len);
347                         if (output_buf) {
348                                 fwrite(output_buf, 1, output_len, outfile);
349                                 fflush(outfile);
350                                 g_free(output_buf);
351                         }
352                 }
353                 break;
354
355         case SR_DF_FRAME_END:
356                 g_debug("cli: received SR_DF_FRAME_END");
357                 if (o->format->event) {
358                         o->format->event(o, SR_DF_FRAME_END, &output_buf,
359                                          &output_len);
360                         if (output_buf) {
361                                 fwrite(output_buf, 1, output_len, outfile);
362                                 fflush(outfile);
363                                 g_free(output_buf);
364                         }
365                 }
366                 break;
367
368         default:
369                 break;
370         }
371
372         if (o && o->format->receive) {
373                 if (o->format->receive(o, sdi, packet, &out) == SR_OK && out) {
374                         fwrite(out->str, 1, out->len, outfile);
375                         fflush(outfile);
376                         g_string_free(out, TRUE);
377                 }
378         }
379
380         /* SR_DF_END needs to be handled after the output module's receive()
381          * is called, so it can properly clean up that module. */
382         if (packet->type == SR_DF_END) {
383                 g_debug("cli: Received SR_DF_END");
384
385                 if (o->format->event) {
386                         o->format->event(o, SR_DF_END, &output_buf, &output_len);
387                         if (output_buf) {
388                                 if (outfile)
389                                         fwrite(output_buf, 1, output_len, outfile);
390                                 g_free(output_buf);
391                                 output_len = 0;
392                         }
393                 }
394
395                 if (o->format->cleanup)
396                         o->format->cleanup(o);
397                 g_free(o);
398                 o = NULL;
399
400                 if (outfile && outfile != stdout)
401                         fclose(outfile);
402
403                 if (opt_output_file && default_output_format)
404                         /* Flush whatever is left out to the session file. */
405                         save_chunk_logic(NULL, 0, 0);
406
407                 if (limit_samples) {
408                         if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
409                                 g_warning("Device only sent %" PRIu64 " samples.",
410                                            rcvd_samples_logic);
411                         else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples)
412                                 g_warning("Device only sent %" PRIu64 " samples.",
413                                            rcvd_samples_analog);
414                 }
415         }
416
417 }
418
419 int opt_to_gvar(char *key, char *value, struct sr_config *src)
420 {
421         const struct sr_config_info *srci;
422         double tmp_double, dlow, dhigh;
423         uint64_t tmp_u64, p, q, low, high;
424         GVariant *rational[2], *range[2];
425         gboolean tmp_bool;
426         int ret;
427
428         if (!(srci = sr_config_info_name_get(key))) {
429                 g_critical("Unknown device option '%s'.", (char *) key);
430                 return -1;
431         }
432         src->key = srci->key;
433
434         if ((value == NULL) &&
435                 (srci->datatype != SR_T_BOOL)) {
436                 g_critical("Option '%s' needs a value.", (char *)key);
437                 return -1;
438         }
439
440         ret = 0;
441         switch (srci->datatype) {
442         case SR_T_UINT64:
443                 ret = sr_parse_sizestring(value, &tmp_u64);
444                 if (ret != 0)
445                         break;
446                 src->data = g_variant_new_uint64(tmp_u64);
447                 break;
448         case SR_T_INT32:
449                 ret = sr_parse_sizestring(value, &tmp_u64);
450                 if (ret != 0)
451                         break;
452                 src->data = g_variant_new_int32(tmp_u64);
453                 break;
454         case SR_T_CHAR:
455                 src->data = g_variant_new_string(value);
456                 break;
457         case SR_T_BOOL:
458                 if (!value)
459                         tmp_bool = TRUE;
460                 else
461                         tmp_bool = sr_parse_boolstring(value);
462                 src->data = g_variant_new_boolean(tmp_bool);
463                 break;
464         case SR_T_FLOAT:
465                 tmp_double = strtof(value, NULL);
466                 src->data = g_variant_new_double(tmp_double);
467                 break;
468         case SR_T_RATIONAL_PERIOD:
469                 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
470                         break;
471                 rational[0] = g_variant_new_uint64(p);
472                 rational[1] = g_variant_new_uint64(q);
473                 src->data = g_variant_new_tuple(rational, 2);
474                 break;
475         case SR_T_RATIONAL_VOLT:
476                 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
477                         break;
478                 rational[0] = g_variant_new_uint64(p);
479                 rational[1] = g_variant_new_uint64(q);
480                 src->data = g_variant_new_tuple(rational, 2);
481                 break;
482         case SR_T_UINT64_RANGE:
483                 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
484                         ret = -1;
485                         break;
486                 } else {
487                         range[0] = g_variant_new_uint64(low);
488                         range[1] = g_variant_new_uint64(high);
489                         src->data = g_variant_new_tuple(range, 2);
490                 }
491                 break;
492         case SR_T_DOUBLE_RANGE:
493                 if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) {
494                         ret = -1;
495                         break;
496                 } else {
497                         range[0] = g_variant_new_double(dlow);
498                         range[1] = g_variant_new_double(dhigh);
499                         src->data = g_variant_new_tuple(range, 2);
500                 }
501                 break;
502         default:
503                 ret = -1;
504         }
505
506         return ret;
507 }
508
509 int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
510 {
511         struct sr_config src;
512         struct sr_channel_group *cg;
513         GHashTableIter iter;
514         gpointer key, value;
515         int ret;
516
517         g_hash_table_iter_init(&iter, args);
518         while (g_hash_table_iter_next(&iter, &key, &value)) {
519                 if ((ret = opt_to_gvar(key, value, &src)) != 0)
520                         return ret;
521                 cg = select_channel_group(sdi);
522                 ret = sr_config_set(sdi, cg, src.key, src.data);
523                 if (ret != SR_OK) {
524                         g_critical("Failed to set device option '%s'.", (char *)key);
525                         return ret;
526                 }
527         }
528
529         return SR_OK;
530 }
531
532 void run_session(void)
533 {
534         GSList *devices;
535         GHashTable *devargs;
536         GVariant *gvar;
537         struct sr_dev_inst *sdi;
538         uint64_t min_samples, max_samples;
539         int max_channels, i;
540         char **triggerlist;
541
542         devices = device_scan();
543         if (!devices) {
544                 g_critical("No devices found.");
545                 return;
546         }
547         if (g_slist_length(devices) > 1) {
548                 g_critical("sigrok-cli only supports one device for capturing.");
549                 return;
550         }
551         sdi = devices->data;
552
553         sr_session_new();
554         sr_session_datafeed_callback_add(datafeed_in, NULL);
555
556         if (sr_dev_open(sdi) != SR_OK) {
557                 g_critical("Failed to open device.");
558                 return;
559         }
560
561         if (sr_session_dev_add(sdi) != SR_OK) {
562                 g_critical("Failed to add device to session.");
563                 sr_session_destroy();
564                 return;
565         }
566
567         if (opt_config) {
568                 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
569                         if (set_dev_options(sdi, devargs) != SR_OK)
570                                 return;
571                         g_hash_table_destroy(devargs);
572                 }
573         }
574
575         if (select_channels(sdi) != SR_OK) {
576                 g_critical("Failed to set channels.");
577                 sr_session_destroy();
578                 return;
579         }
580
581         if (opt_triggers) {
582                 if (!(triggerlist = sr_parse_triggerstring(sdi, opt_triggers))) {
583                         sr_session_destroy();
584                         return;
585                 }
586                 max_channels = g_slist_length(sdi->channels);
587                 for (i = 0; i < max_channels; i++) {
588                         if (triggerlist[i]) {
589                                 sr_dev_trigger_set(sdi, i, triggerlist[i]);
590                                 g_free(triggerlist[i]);
591                         }
592                 }
593                 g_free(triggerlist);
594         }
595
596         if (opt_continuous) {
597                 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
598                         g_critical("This device does not support continuous sampling.");
599                         sr_session_destroy();
600                         return;
601                 }
602         }
603
604         if (opt_time) {
605                 if (set_limit_time(sdi) != SR_OK) {
606                         sr_session_destroy();
607                         return;
608                 }
609         }
610
611         if (opt_samples) {
612                 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
613                         g_critical("Invalid sample limit '%s'.", opt_samples);
614                         sr_session_destroy();
615                         return;
616                 }
617                 if (sr_config_list(sdi->driver, sdi, NULL,
618                                 SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) {
619                         /* The device has no compression, or compression is turned
620                          * off, and publishes its sample memory size. */
621                         g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
622                         g_variant_unref(gvar);
623                         if (limit_samples < min_samples) {
624                                 g_critical("The device stores at least %"PRIu64
625                                                 " samples with the current settings.", min_samples);
626                         }
627                         if (limit_samples > max_samples) {
628                                 g_critical("The device can store only %"PRIu64
629                                                 " samples with the current settings.", max_samples);
630                         }
631                 }
632                 gvar = g_variant_new_uint64(limit_samples);
633                 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
634                         g_critical("Failed to configure sample limit.");
635                         sr_session_destroy();
636                         return;
637                 }
638         }
639
640         if (opt_frames) {
641                 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
642                         g_critical("Invalid sample limit '%s'.", opt_samples);
643                         sr_session_destroy();
644                         return;
645                 }
646                 gvar = g_variant_new_uint64(limit_frames);
647                 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
648                         g_critical("Failed to configure frame limit.");
649                         sr_session_destroy();
650                         return;
651                 }
652         }
653
654         if (sr_session_start() != SR_OK) {
655                 g_critical("Failed to start session.");
656                 sr_session_destroy();
657                 return;
658         }
659
660         if (opt_continuous)
661                 add_anykey();
662
663         sr_session_run();
664
665         if (opt_continuous)
666                 clear_anykey();
667
668         sr_session_datafeed_callback_remove_all();
669         sr_session_destroy();
670         g_slist_free(devices);
671
672 }
673
674 void save_chunk_logic(uint8_t *data, uint64_t data_len, int unitsize)
675 {
676         static uint8_t *buf = NULL;
677         static int buf_len = 0;
678         static int last_unitsize = 0;
679         int max;
680
681         if (!buf)
682                 buf = g_malloc(SAVE_CHUNK_SIZE);
683
684         if (buf_len + data_len > SAVE_CHUNK_SIZE) {
685                 max = (SAVE_CHUNK_SIZE - buf_len) / unitsize * unitsize;
686                 memcpy(buf + buf_len, data, max);
687                 sr_session_append(opt_output_file, buf, unitsize,
688                                 (buf_len + max) / unitsize);
689                 memcpy(buf, data + max, data_len - max);
690                 buf_len = data_len - max;
691         } else if (data_len == 0 && last_unitsize != 0) {
692                 /* End of data, flush the buffer out. */
693                 sr_session_append(opt_output_file, buf, last_unitsize,
694                                 buf_len / last_unitsize);
695         } else {
696                 /* Buffer chunk. */
697                 memcpy(buf + buf_len, data, data_len);
698                 buf_len += data_len;
699         }
700         last_unitsize = unitsize;
701
702 }