2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
25 #include <glib/gstdio.h>
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
30 #define LOG_PREFIX "output/srzip"
36 gint first_analog_index;
37 gint *analog_index_map;
40 static int init(struct sr_output *o, GHashTable *options)
42 struct out_context *outc;
46 if (!o->filename || o->filename[0] == '\0') {
47 sr_info("srzip output module requires a file name, cannot save.");
51 outc = g_malloc0(sizeof(struct out_context));
52 outc->filename = g_strdup(o->filename);
58 static int zip_create(const struct sr_output *o)
60 struct out_context *outc;
62 struct zip_source *versrc, *metasrc;
63 struct sr_channel *ch;
70 guint logic_channels = 0, enabled_logic_channels = 0;
71 guint enabled_analog_channels = 0;
76 if (outc->samplerate == 0 && sr_config_get(o->sdi->driver, o->sdi, NULL,
77 SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
78 outc->samplerate = g_variant_get_uint64(gvar);
79 g_variant_unref(gvar);
82 /* Quietly delete it first, libzip wants replace ops otherwise. */
83 g_unlink(outc->filename);
84 zipfile = zip_open(outc->filename, ZIP_CREATE, NULL);
89 versrc = zip_source_buffer(zipfile, "2", 1, FALSE);
90 if (zip_add(zipfile, "version", versrc) < 0) {
91 sr_err("Error saving version into zipfile: %s",
92 zip_strerror(zipfile));
93 zip_source_free(versrc);
99 meta = g_key_file_new();
101 g_key_file_set_string(meta, "global", "sigrok version",
102 SR_PACKAGE_VERSION_STRING);
104 devgroup = "device 1";
106 for (l = o->sdi->channels; l; l = l->next) {
110 case SR_CHANNEL_LOGIC:
112 enabled_logic_channels++;
115 case SR_CHANNEL_ANALOG:
117 enabled_analog_channels++;
122 /* When reading the file, the first index of the analog channels
123 * can only be deduced through the "total probes" count, so the
124 * first analog index must follow the last logic one, enabled or not. */
125 if (enabled_logic_channels > 0)
126 outc->first_analog_index = logic_channels + 1;
128 outc->first_analog_index = 1;
130 /* Only set capturefile and probes if we will actually save logic data. */
131 if (enabled_logic_channels > 0) {
132 g_key_file_set_string(meta, devgroup, "capturefile", "logic-1");
133 g_key_file_set_integer(meta, devgroup, "total probes", logic_channels);
136 s = sr_samplerate_string(outc->samplerate);
137 g_key_file_set_string(meta, devgroup, "samplerate", s);
140 g_key_file_set_integer(meta, devgroup, "total analog", enabled_analog_channels);
142 /* Make the array one entry larger than needed so we can use the final
143 * 0 as terminator. */
144 outc->analog_index_map = g_malloc0(sizeof(gint) * (enabled_analog_channels + 1));
147 for (l = o->sdi->channels; l; l = l->next) {
153 case SR_CHANNEL_LOGIC:
154 s = g_strdup_printf("probe%d", ch->index + 1);
156 case SR_CHANNEL_ANALOG:
157 outc->analog_index_map[index] = ch->index;
158 s = g_strdup_printf("analog%d", outc->first_analog_index + index);
162 g_key_file_set_string(meta, devgroup, s, ch->name);
166 metabuf = g_key_file_to_data(meta, &metalen, NULL);
167 g_key_file_free(meta);
169 metasrc = zip_source_buffer(zipfile, metabuf, metalen, FALSE);
170 if (zip_add(zipfile, "metadata", metasrc) < 0) {
171 sr_err("Error saving metadata into zipfile: %s",
172 zip_strerror(zipfile));
173 zip_source_free(metasrc);
174 zip_discard(zipfile);
179 if (zip_close(zipfile) < 0) {
180 sr_err("Error saving zipfile: %s", zip_strerror(zipfile));
181 zip_discard(zipfile);
190 static int zip_append(const struct sr_output *o, unsigned char *buf,
191 int unitsize, int length)
193 struct out_context *outc;
195 struct zip_source *logicsrc;
196 int64_t i, num_files;
198 struct zip_source *metasrc;
202 const char *entry_name;
206 unsigned int next_chunk_num;
209 if (!(archive = zip_open(outc->filename, 0, NULL)))
212 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
213 sr_err("Failed to open metadata: %s", zip_strerror(archive));
214 zip_discard(archive);
217 kf = sr_sessionfile_read_metadata(archive, &zs);
219 zip_discard(archive);
223 * If the file was only initialized but doesn't yet have any
224 * data it in, it won't have a unitsize field in metadata yet.
228 if (!g_key_file_has_key(kf, "device 1", "unitsize", &error)) {
229 if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) {
230 sr_err("Failed to check unitsize key: %s", error->message);
233 zip_discard(archive);
236 g_clear_error(&error);
238 /* Add unitsize field. */
239 g_key_file_set_integer(kf, "device 1", "unitsize", unitsize);
240 metabuf = g_key_file_to_data(kf, &metalen, NULL);
241 metasrc = zip_source_buffer(archive, metabuf, metalen, FALSE);
243 if (zip_replace(archive, zs.index, metasrc) < 0) {
244 sr_err("Failed to replace metadata: %s",
245 zip_strerror(archive));
247 zip_source_free(metasrc);
248 zip_discard(archive);
256 num_files = zip_get_num_entries(archive, 0);
257 for (i = 0; i < num_files; i++) {
258 entry_name = zip_get_name(archive, i, 0);
259 if (!entry_name || strncmp(entry_name, "logic-1", 7) != 0)
261 if (entry_name[7] == '\0') {
262 /* This file has no extra chunks, just a single "logic-1".
263 * Rename it to "logic-1-1" * and continue with chunk 2. */
264 if (zip_rename(archive, i, "logic-1-1") < 0) {
265 sr_err("Failed to rename 'logic-1' to 'logic-1-1': %s",
266 zip_strerror(archive));
267 zip_discard(archive);
273 } else if (entry_name[7] == '-') {
274 chunk_num = g_ascii_strtoull(entry_name + 8, NULL, 10);
275 if (chunk_num < G_MAXINT && chunk_num >= next_chunk_num)
276 next_chunk_num = chunk_num + 1;
280 if (length % unitsize != 0) {
281 sr_warn("Chunk size %d not a multiple of the"
282 " unit size %d.", length, unitsize);
284 logicsrc = zip_source_buffer(archive, buf, length, FALSE);
285 chunkname = g_strdup_printf("logic-1-%u", next_chunk_num);
286 i = zip_add(archive, chunkname, logicsrc);
289 sr_err("Failed to add chunk 'logic-1-%u': %s",
290 next_chunk_num, zip_strerror(archive));
291 zip_source_free(logicsrc);
292 zip_discard(archive);
296 if (zip_close(archive) < 0) {
297 sr_err("Error saving session file: %s", zip_strerror(archive));
298 zip_discard(archive);
307 static int zip_append_analog(const struct sr_output *o,
308 const struct sr_datafeed_analog *analog)
310 struct out_context *outc;
312 struct zip_source *analogsrc;
313 int64_t i, num_files;
316 const char *entry_name;
319 struct sr_channel *channel;
323 unsigned int next_chunk_num, index;
326 if (!(archive = zip_open(outc->filename, 0, NULL)))
329 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
330 sr_err("Failed to open metadata: %s", zip_strerror(archive));
331 zip_discard(archive);
335 /* TODO: support packets covering multiple channels */
336 if (g_slist_length(analog->meaning->channels) != 1) {
337 sr_err("Analog packets covering multiple channels not supported yet");
340 channel = analog->meaning->channels->data;
342 /* When reading the file, analog channels must be consecutive.
343 * Thus we need a global channel index map as we don't know in
344 * which order the channel data comes in. */
345 for (index = 0; outc->analog_index_map[index]; index++)
346 if (outc->analog_index_map[index] == channel->index)
348 if (!outc->analog_index_map[index])
349 return SR_ERR_ARG; /* Channel index was not in the list */
351 index += outc->first_analog_index;
353 basename = g_strdup_printf("analog-1-%u", index);
354 baselen = strlen(basename);
356 num_files = zip_get_num_entries(archive, 0);
357 for (i = 0; i < num_files; i++) {
358 entry_name = zip_get_name(archive, i, 0);
359 if (!entry_name || strncmp(entry_name, basename, baselen) != 0) {
361 } else if (entry_name[baselen] == '-') {
362 chunk_num = g_ascii_strtoull(entry_name + baselen + 1, NULL, 10);
363 if (chunk_num < G_MAXINT && chunk_num >= next_chunk_num)
364 next_chunk_num = chunk_num + 1;
368 chunksize = sizeof(float) * analog->num_samples;
369 if (!(chunkbuf = g_try_malloc(chunksize)))
371 if (sr_analog_to_float(analog, chunkbuf) != SR_OK)
374 analogsrc = zip_source_buffer(archive, chunkbuf, chunksize, FALSE);
375 chunkname = g_strdup_printf("%s-%u", basename, next_chunk_num);
376 i = zip_add(archive, chunkname, analogsrc);
379 sr_err("Failed to add chunk '%s': %s", chunkname, zip_strerror(archive));
380 zip_source_free(analogsrc);
381 zip_discard(archive);
384 if (zip_close(archive) < 0) {
385 sr_err("Error saving session file: %s", zip_strerror(archive));
386 zip_discard(archive);
393 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
396 struct out_context *outc;
397 const struct sr_datafeed_meta *meta;
398 const struct sr_datafeed_logic *logic;
399 const struct sr_datafeed_analog *analog;
400 const struct sr_config *src;
406 if (!o || !o->sdi || !(outc = o->priv))
409 switch (packet->type) {
411 meta = packet->payload;
412 for (l = meta->config; l; l = l->next) {
414 if (src->key != SR_CONF_SAMPLERATE)
416 outc->samplerate = g_variant_get_uint64(src->data);
420 if (!outc->zip_created) {
421 if ((ret = zip_create(o)) != SR_OK)
423 outc->zip_created = TRUE;
425 logic = packet->payload;
426 ret = zip_append(o, logic->data, logic->unitsize, logic->length);
431 if (!outc->zip_created) {
432 if ((ret = zip_create(o)) != SR_OK)
434 outc->zip_created = TRUE;
436 analog = packet->payload;
437 ret = zip_append_analog(o, analog);
446 static struct sr_option options[] = {
450 static const struct sr_option *get_options(void)
453 options[0].def = g_variant_ref_sink(g_variant_new_string(""));
458 static int cleanup(struct sr_output *o)
460 struct out_context *outc;
463 g_variant_unref(options[0].def);
464 g_free(outc->analog_index_map);
465 g_free(outc->filename);
472 SR_PRIV struct sr_output_module output_srzip = {
475 .desc = "srzip session file",
476 .exts = (const char*[]){"sr", NULL},
477 .flags = SR_OUTPUT_INTERNAL_IO_HANDLING,
478 .options = get_options,