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