]>
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; | |
36 | }; | |
37 | ||
38 | static int init(struct sr_output *o, GHashTable *options) | |
39 | { | |
40 | struct out_context *outc; | |
41 | ||
37875f75 SA |
42 | (void)options; |
43 | ||
98654c99 | 44 | if (!o->filename || o->filename[0] == '\0') { |
37875f75 | 45 | sr_info("srzip output module requires a file name, cannot save."); |
3250d8c7 | 46 | return SR_ERR_ARG; |
37875f75 | 47 | } |
7df31ae8 SA |
48 | |
49 | outc = g_malloc0(sizeof(struct out_context)); | |
37875f75 | 50 | outc->filename = g_strdup(o->filename); |
7df31ae8 | 51 | o->priv = outc; |
3250d8c7 BV |
52 | |
53 | return SR_OK; | |
54 | } | |
55 | ||
56 | static int zip_create(const struct sr_output *o) | |
57 | { | |
58 | struct out_context *outc; | |
3250d8c7 BV |
59 | struct zip *zipfile; |
60 | struct zip_source *versrc, *metasrc; | |
98654c99 | 61 | struct sr_channel *ch; |
3250d8c7 | 62 | GVariant *gvar; |
98654c99 | 63 | GKeyFile *meta; |
3250d8c7 | 64 | GSList *l; |
98654c99 DE |
65 | const char *devgroup; |
66 | char *s, *metabuf; | |
67 | gsize metalen; | |
82883219 | 68 | guint logic_channels = 0; |
3250d8c7 BV |
69 | |
70 | outc = o->priv; | |
98654c99 DE |
71 | |
72 | if (outc->samplerate == 0 && sr_config_get(o->sdi->driver, o->sdi, NULL, | |
73 | SR_CONF_SAMPLERATE, &gvar) == SR_OK) { | |
74 | outc->samplerate = g_variant_get_uint64(gvar); | |
75 | g_variant_unref(gvar); | |
3250d8c7 BV |
76 | } |
77 | ||
78 | /* Quietly delete it first, libzip wants replace ops otherwise. */ | |
98654c99 DE |
79 | g_unlink(outc->filename); |
80 | zipfile = zip_open(outc->filename, ZIP_CREATE, NULL); | |
81 | if (!zipfile) | |
3250d8c7 BV |
82 | return SR_ERR; |
83 | ||
84 | /* "version" */ | |
98654c99 | 85 | versrc = zip_source_buffer(zipfile, "2", 1, FALSE); |
eaa6a7a3 | 86 | if (zip_add(zipfile, "version", versrc) < 0) { |
98654c99 | 87 | sr_err("Error saving version into zipfile: %s", |
3250d8c7 | 88 | zip_strerror(zipfile)); |
98654c99 DE |
89 | zip_source_free(versrc); |
90 | zip_discard(zipfile); | |
3250d8c7 BV |
91 | return SR_ERR; |
92 | } | |
93 | ||
94 | /* init "metadata" */ | |
98654c99 DE |
95 | meta = g_key_file_new(); |
96 | ||
97 | g_key_file_set_string(meta, "global", "sigrok version", | |
98 | SR_PACKAGE_VERSION_STRING); | |
99 | ||
100 | devgroup = "device 1"; | |
101 | g_key_file_set_string(meta, devgroup, "capturefile", "logic-1"); | |
102 | ||
3250d8c7 | 103 | s = sr_samplerate_string(outc->samplerate); |
98654c99 | 104 | g_key_file_set_string(meta, devgroup, "samplerate", s); |
3250d8c7 BV |
105 | g_free(s); |
106 | ||
82883219 ML |
107 | for (l = o->sdi->channels; l; l = l->next) { |
108 | ch = l->data; | |
109 | if (ch->type == SR_CHANNEL_LOGIC) | |
110 | logic_channels++; | |
111 | } | |
112 | ||
113 | g_key_file_set_integer(meta, devgroup, "total probes", logic_channels); | |
114 | ||
3250d8c7 BV |
115 | for (l = o->sdi->channels; l; l = l->next) { |
116 | ch = l->data; | |
98654c99 DE |
117 | if (ch->enabled && ch->type == SR_CHANNEL_LOGIC) { |
118 | s = g_strdup_printf("probe%d", ch->index + 1); | |
119 | g_key_file_set_string(meta, devgroup, s, ch->name); | |
120 | g_free(s); | |
121 | } | |
3250d8c7 | 122 | } |
82883219 | 123 | |
98654c99 DE |
124 | metabuf = g_key_file_to_data(meta, &metalen, NULL); |
125 | g_key_file_free(meta); | |
3250d8c7 | 126 | |
98654c99 | 127 | metasrc = zip_source_buffer(zipfile, metabuf, metalen, FALSE); |
eaa6a7a3 | 128 | if (zip_add(zipfile, "metadata", metasrc) < 0) { |
98654c99 DE |
129 | sr_err("Error saving metadata into zipfile: %s", |
130 | zip_strerror(zipfile)); | |
131 | zip_source_free(metasrc); | |
132 | zip_discard(zipfile); | |
133 | g_free(metabuf); | |
3250d8c7 BV |
134 | return SR_ERR; |
135 | } | |
136 | ||
98654c99 DE |
137 | if (zip_close(zipfile) < 0) { |
138 | sr_err("Error saving zipfile: %s", zip_strerror(zipfile)); | |
139 | zip_discard(zipfile); | |
140 | g_free(metabuf); | |
3250d8c7 BV |
141 | return SR_ERR; |
142 | } | |
98654c99 | 143 | g_free(metabuf); |
3250d8c7 BV |
144 | |
145 | return SR_OK; | |
146 | } | |
147 | ||
148 | static int zip_append(const struct sr_output *o, unsigned char *buf, | |
149 | int unitsize, int length) | |
150 | { | |
151 | struct out_context *outc; | |
152 | struct zip *archive; | |
153 | struct zip_source *logicsrc; | |
98654c99 | 154 | int64_t i, num_files; |
3250d8c7 BV |
155 | struct zip_stat zs; |
156 | struct zip_source *metasrc; | |
157 | GKeyFile *kf; | |
158 | GError *error; | |
98654c99 | 159 | uint64_t chunk_num; |
3250d8c7 | 160 | const char *entry_name; |
98654c99 DE |
161 | char *metabuf; |
162 | gsize metalen; | |
163 | char *chunkname; | |
164 | unsigned int next_chunk_num; | |
3250d8c7 BV |
165 | |
166 | outc = o->priv; | |
98654c99 | 167 | if (!(archive = zip_open(outc->filename, 0, NULL))) |
3250d8c7 BV |
168 | return SR_ERR; |
169 | ||
98654c99 DE |
170 | if (zip_stat(archive, "metadata", 0, &zs) < 0) { |
171 | sr_err("Failed to open metadata: %s", zip_strerror(archive)); | |
172 | zip_discard(archive); | |
3250d8c7 | 173 | return SR_ERR; |
98654c99 DE |
174 | } |
175 | kf = sr_sessionfile_read_metadata(archive, &zs); | |
176 | if (!kf) { | |
177 | zip_discard(archive); | |
178 | return SR_ERR_DATA; | |
179 | } | |
3250d8c7 BV |
180 | /* |
181 | * If the file was only initialized but doesn't yet have any | |
182 | * data it in, it won't have a unitsize field in metadata yet. | |
183 | */ | |
184 | error = NULL; | |
98654c99 | 185 | metabuf = NULL; |
3250d8c7 BV |
186 | if (!g_key_file_has_key(kf, "device 1", "unitsize", &error)) { |
187 | if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) { | |
98654c99 DE |
188 | sr_err("Failed to check unitsize key: %s", error->message); |
189 | g_error_free(error); | |
190 | g_key_file_free(kf); | |
191 | zip_discard(archive); | |
3250d8c7 BV |
192 | return SR_ERR; |
193 | } | |
98654c99 DE |
194 | g_clear_error(&error); |
195 | ||
3250d8c7 BV |
196 | /* Add unitsize field. */ |
197 | g_key_file_set_integer(kf, "device 1", "unitsize", unitsize); | |
98654c99 DE |
198 | metabuf = g_key_file_to_data(kf, &metalen, NULL); |
199 | metasrc = zip_source_buffer(archive, metabuf, metalen, FALSE); | |
200 | ||
201 | if (zip_replace(archive, zs.index, metasrc) < 0) { | |
202 | sr_err("Failed to replace metadata: %s", | |
203 | zip_strerror(archive)); | |
204 | g_key_file_free(kf); | |
205 | zip_source_free(metasrc); | |
206 | zip_discard(archive); | |
207 | g_free(metabuf); | |
3250d8c7 BV |
208 | return SR_ERR; |
209 | } | |
3250d8c7 BV |
210 | } |
211 | g_key_file_free(kf); | |
212 | ||
213 | next_chunk_num = 1; | |
214 | num_files = zip_get_num_entries(archive, 0); | |
215 | for (i = 0; i < num_files; i++) { | |
216 | entry_name = zip_get_name(archive, i, 0); | |
98654c99 | 217 | if (!entry_name || strncmp(entry_name, "logic-1", 7) != 0) |
3250d8c7 | 218 | continue; |
98654c99 | 219 | if (entry_name[7] == '\0') { |
3250d8c7 BV |
220 | /* This file has no extra chunks, just a single "logic-1". |
221 | * Rename it to "logic-1-1" * and continue with chunk 2. */ | |
98654c99 DE |
222 | if (zip_rename(archive, i, "logic-1-1") < 0) { |
223 | sr_err("Failed to rename 'logic-1' to 'logic-1-1': %s", | |
224 | zip_strerror(archive)); | |
225 | zip_discard(archive); | |
226 | g_free(metabuf); | |
3250d8c7 BV |
227 | return SR_ERR; |
228 | } | |
229 | next_chunk_num = 2; | |
230 | break; | |
98654c99 DE |
231 | } else if (entry_name[7] == '-') { |
232 | chunk_num = g_ascii_strtoull(entry_name + 8, NULL, 10); | |
233 | if (chunk_num < G_MAXINT && chunk_num >= next_chunk_num) | |
3250d8c7 BV |
234 | next_chunk_num = chunk_num + 1; |
235 | } | |
236 | } | |
98654c99 DE |
237 | |
238 | if (length % unitsize != 0) { | |
239 | sr_warn("Chunk size %d not a multiple of the" | |
240 | " unit size %d.", length, unitsize); | |
3250d8c7 | 241 | } |
98654c99 DE |
242 | logicsrc = zip_source_buffer(archive, buf, length, FALSE); |
243 | chunkname = g_strdup_printf("logic-1-%u", next_chunk_num); | |
244 | i = zip_add(archive, chunkname, logicsrc); | |
245 | g_free(chunkname); | |
246 | if (i < 0) { | |
247 | sr_err("Failed to add chunk 'logic-1-%u': %s", | |
248 | next_chunk_num, zip_strerror(archive)); | |
249 | zip_source_free(logicsrc); | |
250 | zip_discard(archive); | |
251 | g_free(metabuf); | |
3250d8c7 BV |
252 | return SR_ERR; |
253 | } | |
98654c99 DE |
254 | if (zip_close(archive) < 0) { |
255 | sr_err("Error saving session file: %s", zip_strerror(archive)); | |
256 | zip_discard(archive); | |
257 | g_free(metabuf); | |
3250d8c7 BV |
258 | return SR_ERR; |
259 | } | |
98654c99 | 260 | g_free(metabuf); |
3250d8c7 BV |
261 | |
262 | return SR_OK; | |
263 | } | |
264 | ||
265 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, | |
266 | GString **out) | |
267 | { | |
268 | struct out_context *outc; | |
269 | const struct sr_datafeed_meta *meta; | |
270 | const struct sr_datafeed_logic *logic; | |
271 | const struct sr_config *src; | |
272 | GSList *l; | |
273 | ||
274 | int ret; | |
275 | ||
276 | *out = NULL; | |
277 | if (!o || !o->sdi || !(outc = o->priv)) | |
278 | return SR_ERR_ARG; | |
279 | ||
280 | switch (packet->type) { | |
281 | case SR_DF_META: | |
282 | meta = packet->payload; | |
283 | for (l = meta->config; l; l = l->next) { | |
284 | src = l->data; | |
285 | if (src->key != SR_CONF_SAMPLERATE) | |
286 | continue; | |
287 | outc->samplerate = g_variant_get_uint64(src->data); | |
288 | } | |
289 | break; | |
290 | case SR_DF_LOGIC: | |
291 | if (!outc->zip_created) { | |
292 | if ((ret = zip_create(o)) != SR_OK) | |
293 | return ret; | |
294 | outc->zip_created = TRUE; | |
295 | } | |
296 | logic = packet->payload; | |
297 | ret = zip_append(o, logic->data, logic->unitsize, logic->length); | |
e57057ae UH |
298 | if (ret != SR_OK) |
299 | return ret; | |
3250d8c7 BV |
300 | break; |
301 | } | |
302 | ||
303 | return SR_OK; | |
304 | } | |
305 | ||
3250d8c7 | 306 | static struct sr_option options[] = { |
3250d8c7 BV |
307 | ALL_ZERO |
308 | }; | |
309 | ||
310 | static const struct sr_option *get_options(void) | |
311 | { | |
312 | if (!options[0].def) | |
313 | options[0].def = g_variant_ref_sink(g_variant_new_string("")); | |
314 | ||
315 | return options; | |
316 | } | |
317 | ||
da3d141f SB |
318 | static int cleanup(struct sr_output *o) |
319 | { | |
320 | struct out_context *outc; | |
321 | ||
322 | outc = o->priv; | |
323 | g_variant_unref(options[0].def); | |
324 | g_free(outc->filename); | |
325 | g_free(outc); | |
326 | o->priv = NULL; | |
327 | ||
328 | return SR_OK; | |
329 | } | |
330 | ||
3250d8c7 BV |
331 | SR_PRIV struct sr_output_module output_srzip = { |
332 | .id = "srzip", | |
333 | .name = "srzip", | |
334 | .desc = "srzip session file", | |
8a174d23 | 335 | .exts = (const char*[]){"sr", NULL}, |
3cd4b381 | 336 | .flags = SR_OUTPUT_INTERNAL_IO_HANDLING, |
3250d8c7 BV |
337 | .options = get_options, |
338 | .init = init, | |
339 | .receive = receive, | |
340 | .cleanup = cleanup, | |
341 | }; |