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