]> sigrok.org Git - libsigrok.git/blame - src/output/srzip.c
output/srzip: Minor whitespace fixes.
[libsigrok.git] / src / output / srzip.c
CommitLineData
3250d8c7
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 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
6ec6c43b 20#include <config.h>
3250d8c7 21#include <stdlib.h>
3250d8c7
BV
22#include <string.h>
23#include <errno.h>
24#include <glib.h>
25#include <glib/gstdio.h>
26#include <zip.h>
c1aae900 27#include <libsigrok/libsigrok.h>
3250d8c7
BV
28#include "libsigrok-internal.h"
29
30#define LOG_PREFIX "output/srzip"
31
32struct out_context {
33 gboolean zip_created;
34 uint64_t samplerate;
35 char *filename;
26918dce 36 gint min_analog_index;
3250d8c7
BV
37};
38
39static int init(struct sr_output *o, GHashTable *options)
40{
41 struct out_context *outc;
42
37875f75
SA
43 (void)options;
44
98654c99 45 if (!o->filename || o->filename[0] == '\0') {
37875f75 46 sr_info("srzip output module requires a file name, cannot save.");
3250d8c7 47 return SR_ERR_ARG;
37875f75 48 }
7df31ae8
SA
49
50 outc = g_malloc0(sizeof(struct out_context));
37875f75 51 outc->filename = g_strdup(o->filename);
7df31ae8 52 o->priv = outc;
3250d8c7
BV
53
54 return SR_OK;
55}
56
57static int zip_create(const struct sr_output *o)
58{
59 struct out_context *outc;
3250d8c7
BV
60 struct zip *zipfile;
61 struct zip_source *versrc, *metasrc;
98654c99 62 struct sr_channel *ch;
3250d8c7 63 GVariant *gvar;
98654c99 64 GKeyFile *meta;
3250d8c7 65 GSList *l;
98654c99
DE
66 const char *devgroup;
67 char *s, *metabuf;
68 gsize metalen;
eab05299 69 guint logic_channels = 0, analog_channels = 0;
3250d8c7
BV
70
71 outc = o->priv;
98654c99
DE
72
73 if (outc->samplerate == 0 && sr_config_get(o->sdi->driver, o->sdi, NULL,
74 SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
75 outc->samplerate = g_variant_get_uint64(gvar);
76 g_variant_unref(gvar);
3250d8c7
BV
77 }
78
79 /* Quietly delete it first, libzip wants replace ops otherwise. */
98654c99
DE
80 g_unlink(outc->filename);
81 zipfile = zip_open(outc->filename, ZIP_CREATE, NULL);
82 if (!zipfile)
3250d8c7
BV
83 return SR_ERR;
84
85 /* "version" */
98654c99 86 versrc = zip_source_buffer(zipfile, "2", 1, FALSE);
eaa6a7a3 87 if (zip_add(zipfile, "version", versrc) < 0) {
98654c99 88 sr_err("Error saving version into zipfile: %s",
3250d8c7 89 zip_strerror(zipfile));
98654c99
DE
90 zip_source_free(versrc);
91 zip_discard(zipfile);
3250d8c7
BV
92 return SR_ERR;
93 }
94
95 /* init "metadata" */
98654c99
DE
96 meta = g_key_file_new();
97
98 g_key_file_set_string(meta, "global", "sigrok version",
99 SR_PACKAGE_VERSION_STRING);
100
101 devgroup = "device 1";
102 g_key_file_set_string(meta, devgroup, "capturefile", "logic-1");
103
3250d8c7 104 s = sr_samplerate_string(outc->samplerate);
98654c99 105 g_key_file_set_string(meta, devgroup, "samplerate", s);
3250d8c7
BV
106 g_free(s);
107
26918dce
ML
108 outc->min_analog_index = -1;
109
82883219
ML
110 for (l = o->sdi->channels; l; l = l->next) {
111 ch = l->data;
eab05299 112 switch (ch->type) {
74a9c0ad
UH
113 case SR_CHANNEL_LOGIC:
114 logic_channels++;
115 break;
116 case SR_CHANNEL_ANALOG:
117 if (outc->min_analog_index == -1 ||
118 ch->index < outc->min_analog_index)
119 outc->min_analog_index = ch->index;
120 analog_channels++;
121 break;
eab05299 122 }
82883219
ML
123 }
124
125 g_key_file_set_integer(meta, devgroup, "total probes", logic_channels);
eab05299 126 g_key_file_set_integer(meta, devgroup, "total analog", analog_channels);
82883219 127
3250d8c7
BV
128 for (l = o->sdi->channels; l; l = l->next) {
129 ch = l->data;
eab05299 130 switch (ch->type) {
74a9c0ad
UH
131 case SR_CHANNEL_LOGIC:
132 s = g_strdup_printf("probe%d", ch->index + 1);
133 break;
134 case SR_CHANNEL_ANALOG:
135 s = g_strdup_printf("analog%d",
136 ch->index - outc->min_analog_index + 1);
137 break;
98654c99 138 }
eab05299
ML
139 if (ch->enabled)
140 g_key_file_set_string(meta, devgroup, s, ch->name);
141 g_free(s);
3250d8c7 142 }
82883219 143
98654c99
DE
144 metabuf = g_key_file_to_data(meta, &metalen, NULL);
145 g_key_file_free(meta);
3250d8c7 146
98654c99 147 metasrc = zip_source_buffer(zipfile, metabuf, metalen, FALSE);
eaa6a7a3 148 if (zip_add(zipfile, "metadata", metasrc) < 0) {
98654c99
DE
149 sr_err("Error saving metadata into zipfile: %s",
150 zip_strerror(zipfile));
151 zip_source_free(metasrc);
152 zip_discard(zipfile);
153 g_free(metabuf);
3250d8c7
BV
154 return SR_ERR;
155 }
156
98654c99
DE
157 if (zip_close(zipfile) < 0) {
158 sr_err("Error saving zipfile: %s", zip_strerror(zipfile));
159 zip_discard(zipfile);
160 g_free(metabuf);
3250d8c7
BV
161 return SR_ERR;
162 }
98654c99 163 g_free(metabuf);
3250d8c7
BV
164
165 return SR_OK;
166}
167
168static int zip_append(const struct sr_output *o, unsigned char *buf,
169 int unitsize, int length)
170{
171 struct out_context *outc;
172 struct zip *archive;
173 struct zip_source *logicsrc;
98654c99 174 int64_t i, num_files;
3250d8c7
BV
175 struct zip_stat zs;
176 struct zip_source *metasrc;
177 GKeyFile *kf;
178 GError *error;
98654c99 179 uint64_t chunk_num;
3250d8c7 180 const char *entry_name;
98654c99
DE
181 char *metabuf;
182 gsize metalen;
183 char *chunkname;
184 unsigned int next_chunk_num;
3250d8c7
BV
185
186 outc = o->priv;
98654c99 187 if (!(archive = zip_open(outc->filename, 0, NULL)))
3250d8c7
BV
188 return SR_ERR;
189
98654c99
DE
190 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
191 sr_err("Failed to open metadata: %s", zip_strerror(archive));
192 zip_discard(archive);
3250d8c7 193 return SR_ERR;
98654c99
DE
194 }
195 kf = sr_sessionfile_read_metadata(archive, &zs);
196 if (!kf) {
197 zip_discard(archive);
198 return SR_ERR_DATA;
199 }
3250d8c7
BV
200 /*
201 * If the file was only initialized but doesn't yet have any
202 * data it in, it won't have a unitsize field in metadata yet.
203 */
204 error = NULL;
98654c99 205 metabuf = NULL;
3250d8c7
BV
206 if (!g_key_file_has_key(kf, "device 1", "unitsize", &error)) {
207 if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) {
98654c99
DE
208 sr_err("Failed to check unitsize key: %s", error->message);
209 g_error_free(error);
210 g_key_file_free(kf);
211 zip_discard(archive);
3250d8c7
BV
212 return SR_ERR;
213 }
98654c99
DE
214 g_clear_error(&error);
215
3250d8c7
BV
216 /* Add unitsize field. */
217 g_key_file_set_integer(kf, "device 1", "unitsize", unitsize);
98654c99
DE
218 metabuf = g_key_file_to_data(kf, &metalen, NULL);
219 metasrc = zip_source_buffer(archive, metabuf, metalen, FALSE);
220
221 if (zip_replace(archive, zs.index, metasrc) < 0) {
222 sr_err("Failed to replace metadata: %s",
223 zip_strerror(archive));
224 g_key_file_free(kf);
225 zip_source_free(metasrc);
226 zip_discard(archive);
227 g_free(metabuf);
3250d8c7
BV
228 return SR_ERR;
229 }
3250d8c7
BV
230 }
231 g_key_file_free(kf);
232
233 next_chunk_num = 1;
234 num_files = zip_get_num_entries(archive, 0);
235 for (i = 0; i < num_files; i++) {
236 entry_name = zip_get_name(archive, i, 0);
98654c99 237 if (!entry_name || strncmp(entry_name, "logic-1", 7) != 0)
3250d8c7 238 continue;
98654c99 239 if (entry_name[7] == '\0') {
3250d8c7
BV
240 /* This file has no extra chunks, just a single "logic-1".
241 * Rename it to "logic-1-1" * and continue with chunk 2. */
98654c99
DE
242 if (zip_rename(archive, i, "logic-1-1") < 0) {
243 sr_err("Failed to rename 'logic-1' to 'logic-1-1': %s",
244 zip_strerror(archive));
245 zip_discard(archive);
246 g_free(metabuf);
3250d8c7
BV
247 return SR_ERR;
248 }
249 next_chunk_num = 2;
250 break;
98654c99
DE
251 } else if (entry_name[7] == '-') {
252 chunk_num = g_ascii_strtoull(entry_name + 8, NULL, 10);
253 if (chunk_num < G_MAXINT && chunk_num >= next_chunk_num)
3250d8c7
BV
254 next_chunk_num = chunk_num + 1;
255 }
256 }
98654c99
DE
257
258 if (length % unitsize != 0) {
259 sr_warn("Chunk size %d not a multiple of the"
260 " unit size %d.", length, unitsize);
3250d8c7 261 }
98654c99
DE
262 logicsrc = zip_source_buffer(archive, buf, length, FALSE);
263 chunkname = g_strdup_printf("logic-1-%u", next_chunk_num);
264 i = zip_add(archive, chunkname, logicsrc);
265 g_free(chunkname);
266 if (i < 0) {
267 sr_err("Failed to add chunk 'logic-1-%u': %s",
268 next_chunk_num, zip_strerror(archive));
269 zip_source_free(logicsrc);
270 zip_discard(archive);
271 g_free(metabuf);
3250d8c7
BV
272 return SR_ERR;
273 }
98654c99
DE
274 if (zip_close(archive) < 0) {
275 sr_err("Error saving session file: %s", zip_strerror(archive));
276 zip_discard(archive);
277 g_free(metabuf);
3250d8c7
BV
278 return SR_ERR;
279 }
98654c99 280 g_free(metabuf);
3250d8c7
BV
281
282 return SR_OK;
283}
284
7163dcbe
ML
285static int zip_append_analog(const struct sr_output *o,
286 const struct sr_datafeed_analog *analog)
287{
288 struct out_context *outc;
289 struct zip *archive;
290 struct zip_source *analogsrc;
291 int64_t i, num_files;
292 struct zip_stat zs;
293 uint64_t chunk_num;
294 const char *entry_name;
295 char *basename;
296 gsize baselen;
297 struct sr_channel *channel;
298 float *chunkbuf;
299 gsize chunksize;
300 char *chunkname;
301 unsigned int next_chunk_num;
302
303 outc = o->priv;
304 if (!(archive = zip_open(outc->filename, 0, NULL)))
305 return SR_ERR;
306
307 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
308 sr_err("Failed to open metadata: %s", zip_strerror(archive));
309 zip_discard(archive);
310 return SR_ERR;
311 }
312
313 /* TODO: support packets covering multiple channels */
314 if (g_slist_length(analog->meaning->channels) != 1) {
315 sr_err("Analog packets covering multiple channels not supported yet");
316 return SR_ERR;
317 }
318 channel = analog->meaning->channels->data;
319
26918dce
ML
320 basename = g_strdup_printf("analog-1-%u",
321 channel->index - outc->min_analog_index + 1);
7163dcbe
ML
322 baselen = strlen(basename);
323 next_chunk_num = 1;
324 num_files = zip_get_num_entries(archive, 0);
325 for (i = 0; i < num_files; i++) {
326 entry_name = zip_get_name(archive, i, 0);
327 if (!entry_name || strncmp(entry_name, basename, baselen) != 0) {
328 continue;
329 } else if (entry_name[baselen] == '-') {
330 chunk_num = g_ascii_strtoull(entry_name + baselen + 1, NULL, 10);
331 if (chunk_num < G_MAXINT && chunk_num >= next_chunk_num)
332 next_chunk_num = chunk_num + 1;
333 }
334 }
335
336 chunksize = sizeof(float) * analog->num_samples;
337 if (!(chunkbuf = g_try_malloc(chunksize)))
338 return SR_ERR;
339 if (sr_analog_to_float(analog, chunkbuf) != SR_OK)
340 return SR_ERR;
341
342 analogsrc = zip_source_buffer(archive, chunkbuf, chunksize, FALSE);
343 chunkname = g_strdup_printf("%s-%u", basename, next_chunk_num);
344 i = zip_add(archive, chunkname, analogsrc);
345 g_free(chunkname);
346 if (i < 0) {
347 sr_err("Failed to add chunk '%s': %s", chunkname, zip_strerror(archive));
348 zip_source_free(analogsrc);
349 zip_discard(archive);
350 return SR_ERR;
351 }
352 if (zip_close(archive) < 0) {
353 sr_err("Error saving session file: %s", zip_strerror(archive));
354 zip_discard(archive);
355 return SR_ERR;
356 }
357
358 return SR_OK;
359}
360
3250d8c7
BV
361static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
362 GString **out)
363{
364 struct out_context *outc;
365 const struct sr_datafeed_meta *meta;
366 const struct sr_datafeed_logic *logic;
7163dcbe 367 const struct sr_datafeed_analog *analog;
3250d8c7
BV
368 const struct sr_config *src;
369 GSList *l;
370
371 int ret;
372
373 *out = NULL;
374 if (!o || !o->sdi || !(outc = o->priv))
375 return SR_ERR_ARG;
376
377 switch (packet->type) {
378 case SR_DF_META:
379 meta = packet->payload;
380 for (l = meta->config; l; l = l->next) {
381 src = l->data;
382 if (src->key != SR_CONF_SAMPLERATE)
383 continue;
384 outc->samplerate = g_variant_get_uint64(src->data);
385 }
386 break;
387 case SR_DF_LOGIC:
388 if (!outc->zip_created) {
389 if ((ret = zip_create(o)) != SR_OK)
390 return ret;
391 outc->zip_created = TRUE;
392 }
393 logic = packet->payload;
394 ret = zip_append(o, logic->data, logic->unitsize, logic->length);
e57057ae
UH
395 if (ret != SR_OK)
396 return ret;
3250d8c7 397 break;
7163dcbe
ML
398 case SR_DF_ANALOG:
399 if (!outc->zip_created) {
400 if ((ret = zip_create(o)) != SR_OK)
401 return ret;
402 outc->zip_created = TRUE;
403 }
404 analog = packet->payload;
405 ret = zip_append_analog(o, analog);
406 if (ret != SR_OK)
407 return ret;
408 break;
3250d8c7
BV
409 }
410
411 return SR_OK;
412}
413
3250d8c7 414static struct sr_option options[] = {
3250d8c7
BV
415 ALL_ZERO
416};
417
418static const struct sr_option *get_options(void)
419{
420 if (!options[0].def)
421 options[0].def = g_variant_ref_sink(g_variant_new_string(""));
422
423 return options;
424}
425
da3d141f
SB
426static int cleanup(struct sr_output *o)
427{
428 struct out_context *outc;
429
430 outc = o->priv;
431 g_variant_unref(options[0].def);
432 g_free(outc->filename);
433 g_free(outc);
434 o->priv = NULL;
435
436 return SR_OK;
437}
438
3250d8c7
BV
439SR_PRIV struct sr_output_module output_srzip = {
440 .id = "srzip",
441 .name = "srzip",
442 .desc = "srzip session file",
8a174d23 443 .exts = (const char*[]){"sr", NULL},
3cd4b381 444 .flags = SR_OUTPUT_INTERNAL_IO_HANDLING,
3250d8c7
BV
445 .options = get_options,
446 .init = init,
447 .receive = receive,
448 .cleanup = cleanup,
449};