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