]>
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 <stdlib.h> | |
21 | #include <unistd.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.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 | outc = g_malloc0(sizeof(struct out_context)); | |
43 | o->priv = outc; | |
44 | outc->filename = g_strdup(g_variant_get_string(g_hash_table_lookup(options, "filename"), NULL)); | |
45 | if (strlen(outc->filename) == 0) | |
46 | return SR_ERR_ARG; | |
47 | ||
48 | return SR_OK; | |
49 | } | |
50 | ||
51 | static int zip_create(const struct sr_output *o) | |
52 | { | |
53 | struct out_context *outc; | |
54 | struct sr_channel *ch; | |
55 | FILE *meta; | |
56 | struct zip *zipfile; | |
57 | struct zip_source *versrc, *metasrc; | |
58 | GVariant *gvar; | |
59 | GSList *l; | |
60 | int tmpfile, ret; | |
61 | char version[1], metafile[32], *s; | |
62 | ||
63 | outc = o->priv; | |
64 | if (outc->samplerate == 0) { | |
65 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, | |
66 | &gvar) == SR_OK) { | |
67 | outc->samplerate = g_variant_get_uint64(gvar); | |
68 | g_variant_unref(gvar); | |
69 | } | |
70 | } | |
71 | ||
72 | /* Quietly delete it first, libzip wants replace ops otherwise. */ | |
73 | unlink(outc->filename); | |
74 | if (!(zipfile = zip_open(outc->filename, ZIP_CREATE, &ret))) | |
75 | return SR_ERR; | |
76 | ||
77 | /* "version" */ | |
78 | version[0] = '2'; | |
79 | if (!(versrc = zip_source_buffer(zipfile, version, 1, 0))) | |
80 | return SR_ERR; | |
81 | if (zip_add(zipfile, "version", versrc) == -1) { | |
82 | sr_info("Error saving version into zipfile: %s.", | |
83 | zip_strerror(zipfile)); | |
84 | return SR_ERR; | |
85 | } | |
86 | ||
87 | /* init "metadata" */ | |
88 | strcpy(metafile, "sigrok-meta-XXXXXX"); | |
89 | if ((tmpfile = g_mkstemp(metafile)) == -1) | |
90 | return SR_ERR; | |
91 | close(tmpfile); | |
92 | meta = g_fopen(metafile, "wb"); | |
93 | fprintf(meta, "[global]\n"); | |
94 | fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION); | |
95 | fprintf(meta, "[device 1]\ncapturefile = logic-1\n"); | |
96 | fprintf(meta, "total probes = %d\n", g_slist_length(o->sdi->channels)); | |
97 | s = sr_samplerate_string(outc->samplerate); | |
98 | fprintf(meta, "samplerate = %s\n", s); | |
99 | g_free(s); | |
100 | ||
101 | for (l = o->sdi->channels; l; l = l->next) { | |
102 | ch = l->data; | |
103 | if (ch->type != SR_CHANNEL_LOGIC) | |
104 | continue; | |
105 | if (!ch->enabled) | |
106 | continue; | |
107 | fprintf(meta, "probe%d = %s\n", ch->index + 1, ch->name); | |
108 | } | |
109 | fclose(meta); | |
110 | ||
111 | if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1))) { | |
112 | unlink(metafile); | |
113 | return SR_ERR; | |
114 | } | |
115 | if (zip_add(zipfile, "metadata", metasrc) == -1) { | |
116 | unlink(metafile); | |
117 | return SR_ERR; | |
118 | } | |
119 | ||
120 | if ((ret = zip_close(zipfile)) == -1) { | |
121 | sr_info("Error saving zipfile: %s.", zip_strerror(zipfile)); | |
122 | unlink(metafile); | |
123 | return SR_ERR; | |
124 | } | |
125 | ||
126 | unlink(metafile); | |
127 | ||
128 | return SR_OK; | |
129 | } | |
130 | ||
131 | static int zip_append(const struct sr_output *o, unsigned char *buf, | |
132 | int unitsize, int length) | |
133 | { | |
134 | struct out_context *outc; | |
135 | struct zip *archive; | |
136 | struct zip_source *logicsrc; | |
137 | zip_int64_t num_files; | |
138 | struct zip_file *zf; | |
139 | struct zip_stat zs; | |
140 | struct zip_source *metasrc; | |
141 | GKeyFile *kf; | |
142 | GError *error; | |
143 | gsize len; | |
144 | int chunk_num, next_chunk_num, tmpfile, ret, i; | |
145 | const char *entry_name; | |
146 | char *metafile, tmpname[32], chunkname[16]; | |
147 | ||
148 | outc = o->priv; | |
149 | if (!(archive = zip_open(outc->filename, 0, &ret))) | |
150 | return SR_ERR; | |
151 | ||
152 | if (zip_stat(archive, "metadata", 0, &zs) == -1) | |
153 | return SR_ERR; | |
154 | ||
155 | metafile = g_malloc(zs.size); | |
156 | zf = zip_fopen_index(archive, zs.index, 0); | |
157 | zip_fread(zf, metafile, zs.size); | |
158 | zip_fclose(zf); | |
159 | ||
160 | /* | |
161 | * If the file was only initialized but doesn't yet have any | |
162 | * data it in, it won't have a unitsize field in metadata yet. | |
163 | */ | |
164 | error = NULL; | |
165 | kf = g_key_file_new(); | |
166 | if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, &error)) { | |
167 | sr_err("Failed to parse metadata: %s.", error->message); | |
168 | return SR_ERR; | |
169 | } | |
170 | g_free(metafile); | |
171 | tmpname[0] = '\0'; | |
172 | if (!g_key_file_has_key(kf, "device 1", "unitsize", &error)) { | |
173 | if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) { | |
174 | sr_err("Failed to check unitsize key: %s", error ? error->message : "?"); | |
175 | return SR_ERR; | |
176 | } | |
177 | /* Add unitsize field. */ | |
178 | g_key_file_set_integer(kf, "device 1", "unitsize", unitsize); | |
179 | metafile = g_key_file_to_data(kf, &len, &error); | |
180 | strcpy(tmpname, "sigrok-meta-XXXXXX"); | |
181 | if ((tmpfile = g_mkstemp(tmpname)) == -1) | |
182 | return SR_ERR; | |
183 | if (write(tmpfile, metafile, len) < 0) { | |
184 | sr_dbg("Failed to create new metadata: %s", strerror(errno)); | |
185 | g_free(metafile); | |
186 | unlink(tmpname); | |
187 | return SR_ERR; | |
188 | } | |
189 | close(tmpfile); | |
190 | if (!(metasrc = zip_source_file(archive, tmpname, 0, -1))) { | |
191 | sr_err("Failed to create zip source for metadata."); | |
192 | g_free(metafile); | |
193 | unlink(tmpname); | |
194 | return SR_ERR; | |
195 | } | |
196 | if (zip_replace(archive, zs.index, metasrc) == -1) { | |
197 | sr_err("Failed to replace metadata file."); | |
198 | g_free(metafile); | |
199 | unlink(tmpname); | |
200 | return SR_ERR; | |
201 | } | |
202 | g_free(metafile); | |
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 (strncmp(entry_name, "logic-1", 7)) | |
211 | continue; | |
212 | if (strlen(entry_name) == 7) { | |
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") == -1) { | |
216 | sr_err("Failed to rename 'logic-1' to 'logic-1-1'."); | |
217 | unlink(tmpname); | |
218 | return SR_ERR; | |
219 | } | |
220 | next_chunk_num = 2; | |
221 | break; | |
222 | } else if (strlen(entry_name) > 8 && entry_name[7] == '-') { | |
223 | chunk_num = strtoull(entry_name + 8, NULL, 10); | |
224 | if (chunk_num >= next_chunk_num) | |
225 | next_chunk_num = chunk_num + 1; | |
226 | } | |
227 | } | |
228 | snprintf(chunkname, 15, "logic-1-%d", next_chunk_num); | |
229 | if (!(logicsrc = zip_source_buffer(archive, buf, length, FALSE))) { | |
230 | unlink(tmpname); | |
231 | return SR_ERR; | |
232 | } | |
233 | if (zip_add(archive, chunkname, logicsrc) == -1) { | |
234 | unlink(tmpname); | |
235 | return SR_ERR; | |
236 | } | |
237 | if ((ret = zip_close(archive)) == -1) { | |
238 | sr_info("error saving session file: %s", zip_strerror(archive)); | |
239 | unlink(tmpname); | |
240 | return SR_ERR; | |
241 | } | |
242 | unlink(tmpname); | |
243 | ||
244 | return SR_OK; | |
245 | } | |
246 | ||
247 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, | |
248 | GString **out) | |
249 | { | |
250 | struct out_context *outc; | |
251 | const struct sr_datafeed_meta *meta; | |
252 | const struct sr_datafeed_logic *logic; | |
253 | const struct sr_config *src; | |
254 | GSList *l; | |
255 | ||
256 | int ret; | |
257 | ||
258 | *out = NULL; | |
259 | if (!o || !o->sdi || !(outc = o->priv)) | |
260 | return SR_ERR_ARG; | |
261 | ||
262 | switch (packet->type) { | |
263 | case SR_DF_META: | |
264 | meta = packet->payload; | |
265 | for (l = meta->config; l; l = l->next) { | |
266 | src = l->data; | |
267 | if (src->key != SR_CONF_SAMPLERATE) | |
268 | continue; | |
269 | outc->samplerate = g_variant_get_uint64(src->data); | |
270 | } | |
271 | break; | |
272 | case SR_DF_LOGIC: | |
273 | if (!outc->zip_created) { | |
274 | if ((ret = zip_create(o)) != SR_OK) | |
275 | return ret; | |
276 | outc->zip_created = TRUE; | |
277 | } | |
278 | logic = packet->payload; | |
279 | ret = zip_append(o, logic->data, logic->unitsize, logic->length); | |
280 | break; | |
281 | } | |
282 | ||
283 | return SR_OK; | |
284 | } | |
285 | ||
286 | static int cleanup(struct sr_output *o) | |
287 | { | |
288 | struct out_context *outc; | |
289 | ||
290 | outc = o->priv; | |
291 | g_free(outc->filename); | |
292 | g_free(outc); | |
293 | o->priv = NULL; | |
294 | ||
295 | return SR_OK; | |
296 | } | |
297 | ||
298 | static struct sr_option options[] = { | |
299 | { "filename", "Filename", "File to write", NULL, NULL }, | |
300 | ALL_ZERO | |
301 | }; | |
302 | ||
303 | static const struct sr_option *get_options(void) | |
304 | { | |
305 | if (!options[0].def) | |
306 | options[0].def = g_variant_ref_sink(g_variant_new_string("")); | |
307 | ||
308 | return options; | |
309 | } | |
310 | ||
311 | SR_PRIV struct sr_output_module output_srzip = { | |
312 | .id = "srzip", | |
313 | .name = "srzip", | |
314 | .desc = "srzip session file", | |
315 | .exts = (const char*[]){"sr", NULL}, | |
316 | .options = get_options, | |
317 | .init = init, | |
318 | .receive = receive, | |
319 | .cleanup = cleanup, | |
320 | }; |