]> sigrok.org Git - libsigrok.git/blame_incremental - session_file.c
teleinfo: Minor cleanups.
[libsigrok.git] / session_file.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 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 <string.h>
21#include <stdlib.h>
22#include <unistd.h>
23#include <zip.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <errno.h>
27#include <glib.h>
28#include <glib/gstdio.h>
29#include "config.h" /* Needed for PACKAGE_VERSION and others. */
30#include "libsigrok.h"
31#include "libsigrok-internal.h"
32
33/* Message logging helpers with subsystem-specific prefix string. */
34#define LOG_PREFIX "session-file: "
35#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
36#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
37#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
38#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
39#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
40#define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
41
42/**
43 * @file
44 *
45 * Loading and saving libsigrok session files.
46 */
47
48/**
49 * @addtogroup grp_session
50 *
51 * @{
52 */
53
54extern struct sr_session *session;
55extern SR_PRIV struct sr_dev_driver session_driver;
56
57/** @private */
58SR_PRIV int sr_sessionfile_check(const char *filename)
59{
60 struct stat st;
61 struct zip *archive;
62 struct zip_file *zf;
63 struct zip_stat zs;
64 int version, ret;
65 char s[11];
66
67 if (!filename)
68 return SR_ERR_ARG;
69
70 if (stat(filename, &st) == -1) {
71 sr_err("Couldn't stat %s: %s", filename, strerror(errno));
72 return SR_ERR;
73 }
74
75 if (!(archive = zip_open(filename, 0, &ret)))
76 /* No logging: this can be used just to check if it's
77 * a sigrok session file or not. */
78 return SR_ERR;
79
80 /* check "version" */
81 version = 0;
82 if (!(zf = zip_fopen(archive, "version", 0))) {
83 sr_dbg("Not a sigrok session file: no version found.");
84 return SR_ERR;
85 }
86 if ((ret = zip_fread(zf, s, 10)) == -1)
87 return SR_ERR;
88 zip_fclose(zf);
89 s[ret] = 0;
90 version = strtoull(s, NULL, 10);
91 if (version != 1) {
92 sr_dbg("Cannot handle sigrok session file version %d.", version);
93 return SR_ERR;
94 }
95
96 /* read "metadata" */
97 if (zip_stat(archive, "metadata", 0, &zs) == -1) {
98 sr_dbg("Not a valid sigrok session file.");
99 return SR_ERR;
100 }
101
102 return SR_OK;
103}
104
105/**
106 * Load the session from the specified filename.
107 *
108 * @param filename The name of the session file to load. Must not be NULL.
109 *
110 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments,
111 * SR_ERR_MALLOC upon memory allocation errors, or SR_ERR upon
112 * other errors.
113 */
114SR_API int sr_session_load(const char *filename)
115{
116 GKeyFile *kf;
117 GPtrArray *capturefiles;
118 struct zip *archive;
119 struct zip_file *zf;
120 struct zip_stat zs;
121 struct sr_dev_inst *sdi;
122 struct sr_probe *probe;
123 int ret, probenum, devcnt, i, j;
124 uint64_t tmp_u64, total_probes, enabled_probes, p;
125 char **sections, **keys, *metafile, *val;
126 char probename[SR_MAX_PROBENAME_LEN + 1];
127
128 if ((ret = sr_sessionfile_check(filename)) != SR_OK)
129 return ret;
130
131 if (!(archive = zip_open(filename, 0, &ret)))
132 return SR_ERR;
133
134 if (zip_stat(archive, "metadata", 0, &zs) == -1)
135 return SR_ERR;
136
137 if (!(metafile = g_try_malloc(zs.size))) {
138 sr_err("%s: metafile malloc failed", __func__);
139 return SR_ERR_MALLOC;
140 }
141
142 zf = zip_fopen_index(archive, zs.index, 0);
143 zip_fread(zf, metafile, zs.size);
144 zip_fclose(zf);
145
146 kf = g_key_file_new();
147 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
148 sr_dbg("Failed to parse metadata.");
149 return SR_ERR;
150 }
151
152 sr_session_new();
153
154 devcnt = 0;
155 capturefiles = g_ptr_array_new_with_free_func(g_free);
156 sections = g_key_file_get_groups(kf, NULL);
157 for (i = 0; sections[i]; i++) {
158 if (!strcmp(sections[i], "global"))
159 /* nothing really interesting in here yet */
160 continue;
161 if (!strncmp(sections[i], "device ", 7)) {
162 /* device section */
163 sdi = NULL;
164 enabled_probes = total_probes = 0;
165 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
166 for (j = 0; keys[j]; j++) {
167 val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
168 if (!strcmp(keys[j], "capturefile")) {
169 sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, NULL, NULL, NULL);
170 sdi->driver = &session_driver;
171 if (devcnt == 0)
172 /* first device, init the driver */
173 sdi->driver->init(NULL);
174 sr_dev_open(sdi);
175 sr_session_dev_add(sdi);
176 sdi->driver->config_set(SR_CONF_SESSIONFILE,
177 g_variant_new_string(filename), sdi, NULL);
178 sdi->driver->config_set(SR_CONF_CAPTUREFILE,
179 g_variant_new_string(val), sdi, NULL);
180 g_ptr_array_add(capturefiles, val);
181 } else if (!strcmp(keys[j], "samplerate")) {
182 sr_parse_sizestring(val, &tmp_u64);
183 sdi->driver->config_set(SR_CONF_SAMPLERATE,
184 g_variant_new_uint64(tmp_u64), sdi, NULL);
185 } else if (!strcmp(keys[j], "unitsize")) {
186 tmp_u64 = strtoull(val, NULL, 10);
187 sdi->driver->config_set(SR_CONF_CAPTURE_UNITSIZE,
188 g_variant_new_uint64(tmp_u64), sdi, NULL);
189 } else if (!strcmp(keys[j], "total probes")) {
190 total_probes = strtoull(val, NULL, 10);
191 sdi->driver->config_set(SR_CONF_CAPTURE_NUM_PROBES,
192 g_variant_new_uint64(total_probes), sdi, NULL);
193 for (p = 0; p < total_probes; p++) {
194 snprintf(probename, SR_MAX_PROBENAME_LEN, "%" PRIu64, p);
195 if (!(probe = sr_probe_new(p, SR_PROBE_LOGIC, TRUE,
196 probename)))
197 return SR_ERR;
198 sdi->probes = g_slist_append(sdi->probes, probe);
199 }
200 } else if (!strncmp(keys[j], "probe", 5)) {
201 if (!sdi)
202 continue;
203 enabled_probes++;
204 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
205 /* sr_session_save() */
206 sr_dev_probe_name_set(sdi, tmp_u64 - 1, val);
207 } else if (!strncmp(keys[j], "trigger", 7)) {
208 probenum = strtoul(keys[j]+7, NULL, 10);
209 sr_dev_trigger_set(sdi, probenum, val);
210 }
211 }
212 g_strfreev(keys);
213 /* Disable probes not specifically listed. */
214 if (total_probes)
215 for (p = enabled_probes; p < total_probes; p++)
216 sr_dev_probe_enable(sdi, p, FALSE);
217 }
218 devcnt++;
219 }
220 g_strfreev(sections);
221 g_key_file_free(kf);
222
223 return SR_OK;
224}
225
226/**
227 * Save the current session to the specified file.
228 *
229 * @param filename The name of the filename to save the current session as.
230 * Must not be NULL.
231 * @param sdi The device instance from which the data was captured.
232 * @param buf The data to be saved.
233 * @param unitsize The number of bytes per sample.
234 * @param units The number of samples.
235 *
236 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
237 * upon other errors.
238 */
239SR_API int sr_session_save(const char *filename, const struct sr_dev_inst *sdi,
240 unsigned char *buf, int unitsize, int units)
241{
242 GSList *l;
243 GVariant *gvar;
244 FILE *meta;
245 struct sr_probe *probe;
246 struct zip *zipfile;
247 struct zip_source *versrc, *metasrc, *logicsrc;
248 int tmpfile, ret, probecnt;
249 uint64_t samplerate;
250 char version[1], rawname[16], metafile[32], *s;
251
252 if (!filename) {
253 sr_err("%s: filename was NULL", __func__);
254 return SR_ERR_ARG;
255 }
256
257 /* Quietly delete it first, libzip wants replace ops otherwise. */
258 unlink(filename);
259 if (!(zipfile = zip_open(filename, ZIP_CREATE, &ret)))
260 return SR_ERR;
261
262 /* "version" */
263 version[0] = '1';
264 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
265 return SR_ERR;
266 if (zip_add(zipfile, "version", versrc) == -1) {
267 sr_info("error saving version into zipfile: %s",
268 zip_strerror(zipfile));
269 return SR_ERR;
270 }
271
272 /* init "metadata" */
273 strcpy(metafile, "sigrok-meta-XXXXXX");
274 if ((tmpfile = g_mkstemp(metafile)) == -1)
275 return SR_ERR;
276 close(tmpfile);
277 meta = g_fopen(metafile, "wb");
278 fprintf(meta, "[global]\n");
279 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
280
281 /* metadata */
282 fprintf(meta, "[device 1]\n");
283 if (sdi->driver)
284 fprintf(meta, "driver = %s\n", sdi->driver->name);
285
286 /* metadata */
287 fprintf(meta, "capturefile = logic-1\n");
288 fprintf(meta, "unitsize = %d\n", unitsize);
289 fprintf(meta, "total probes = %d\n", g_slist_length(sdi->probes));
290 if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
291 if (sr_config_get(sdi->driver, sdi, NULL,
292 SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
293 samplerate = g_variant_get_uint64(gvar);
294 s = sr_samplerate_string(samplerate);
295 fprintf(meta, "samplerate = %s\n", s);
296 g_free(s);
297 g_variant_unref(gvar);
298 }
299 }
300 probecnt = 1;
301 for (l = sdi->probes; l; l = l->next) {
302 probe = l->data;
303 if (probe->enabled) {
304 if (probe->name)
305 fprintf(meta, "probe%d = %s\n", probecnt, probe->name);
306 if (probe->trigger)
307 fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger);
308 probecnt++;
309 }
310 }
311
312 if (!(logicsrc = zip_source_buffer(zipfile, buf,
313 units * unitsize, FALSE)))
314 return SR_ERR;
315 snprintf(rawname, 15, "logic-1");
316 if (zip_add(zipfile, rawname, logicsrc) == -1)
317 return SR_ERR;
318 fclose(meta);
319
320 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
321 return SR_ERR;
322 if (zip_add(zipfile, "metadata", metasrc) == -1)
323 return SR_ERR;
324
325 if ((ret = zip_close(zipfile)) == -1) {
326 sr_info("error saving zipfile: %s", zip_strerror(zipfile));
327 return SR_ERR;
328 }
329
330 unlink(metafile);
331
332 return SR_OK;
333}
334
335/**
336 * Append data to an existing session file.
337 *
338 * @param filename The name of the filename to append to. Must not be NULL.
339 * @param buf The data to be appended.
340 * @param unitsize The number of bytes per sample.
341 * @param units The number of samples.
342 *
343 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
344 * upon other errors.
345 */
346SR_API int sr_session_append(const char *filename, unsigned char *buf,
347 int unitsize, int units)
348{
349 struct zip *archive;
350 struct zip_source *logicsrc;
351 zip_int64_t num_files;
352 int chunk_num, next_chunk_num, ret, i;
353 const char *entry_name;
354 char chunkname[16];
355
356 if ((ret = sr_sessionfile_check(filename)) != SR_OK)
357 return ret;
358
359 if (!(archive = zip_open(filename, 0, &ret)))
360 return SR_ERR;
361
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 (strncmp(entry_name, "logic-1", 7))
367 continue;
368 if (strlen(entry_name) == 7) {
369 /* This file has no extra chunks, just a single "logic-1".
370 * Rename it to "logic-1-1" * and continue with chunk 2. */
371 if (zip_rename(archive, i, "logic-1-1") == -1) {
372 sr_err("Failed to rename 'logic-1' to 'logic-1-1'.");
373 return SR_ERR;
374 }
375 next_chunk_num = 2;
376 break;
377 } else if (strlen(entry_name) > 8 && entry_name[7] == '-') {
378 chunk_num = strtoull(entry_name + 8, NULL, 10);
379 if (chunk_num >= next_chunk_num)
380 next_chunk_num = chunk_num + 1;
381 }
382 }
383 snprintf(chunkname, 15, "logic-1-%d", next_chunk_num);
384 if (!(logicsrc = zip_source_buffer(archive, buf, units * unitsize, FALSE)))
385 return SR_ERR;
386 if (zip_add(archive, chunkname, logicsrc) == -1)
387 return SR_ERR;
388 if ((ret = zip_close(archive)) == -1) {
389 sr_info("error saving session file: %s", zip_strerror(archive));
390 return SR_ERR;
391 }
392
393 return SR_OK;
394}
395
396/** @} */