]> sigrok.org Git - libsigrok.git/blame - session_file.c
teleinfo: Minor cleanups.
[libsigrok.git] / session_file.c
CommitLineData
7d658874 1/*
50985c20 2 * This file is part of the libsigrok project.
7d658874 3 *
13d8e03c 4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
7d658874
BV
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>
5f9c4c8a
BV
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <errno.h>
7d658874 27#include <glib.h>
3bbd9849 28#include <glib/gstdio.h>
545f9786 29#include "config.h" /* Needed for PACKAGE_VERSION and others. */
45c59c8b
BV
30#include "libsigrok.h"
31#include "libsigrok-internal.h"
7d658874 32
29a27196
UH
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)
a885ce3e 41
393fb9cb
UH
42/**
43 * @file
44 *
45 * Loading and saving libsigrok session files.
46 */
47
7b870c38 48/**
777e2035 49 * @addtogroup grp_session
7b870c38
UH
50 *
51 * @{
52 */
53
2872d21e 54extern struct sr_session *session;
c09f0b57 55extern SR_PRIV struct sr_dev_driver session_driver;
7d658874 56
72a08bcc 57/** @private */
f438e0c9 58SR_PRIV int sr_sessionfile_check(const char *filename)
7d658874 59{
5f9c4c8a 60 struct stat st;
7d658874
BV
61 struct zip *archive;
62 struct zip_file *zf;
63 struct zip_stat zs;
f438e0c9
BV
64 int version, ret;
65 char s[11];
7d658874 66
f438e0c9 67 if (!filename)
9f45fb3a 68 return SR_ERR_ARG;
9f45fb3a 69
5f9c4c8a
BV
70 if (stat(filename, &st) == -1) {
71 sr_err("Couldn't stat %s: %s", filename, strerror(errno));
72 return SR_ERR;
73 }
74
f438e0c9
BV
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. */
7d658874 78 return SR_ERR;
7d658874
BV
79
80 /* check "version" */
c1864d55 81 version = 0;
7d658874 82 if (!(zf = zip_fopen(archive, "version", 0))) {
f438e0c9 83 sr_dbg("Not a sigrok session file: no version found.");
7d658874
BV
84 return SR_ERR;
85 }
f438e0c9 86 if ((ret = zip_fread(zf, s, 10)) == -1)
7d658874 87 return SR_ERR;
7d658874 88 zip_fclose(zf);
c1864d55
BV
89 s[ret] = 0;
90 version = strtoull(s, NULL, 10);
91 if (version != 1) {
f438e0c9 92 sr_dbg("Cannot handle sigrok session file version %d.", version);
c1864d55
BV
93 return SR_ERR;
94 }
7d658874
BV
95
96 /* read "metadata" */
97 if (zip_stat(archive, "metadata", 0, &zs) == -1) {
a885ce3e 98 sr_dbg("Not a valid sigrok session file.");
7d658874
BV
99 return SR_ERR;
100 }
b53738ba 101
f438e0c9
BV
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
b53738ba 137 if (!(metafile = g_try_malloc(zs.size))) {
a885ce3e 138 sr_err("%s: metafile malloc failed", __func__);
b53738ba
UH
139 return SR_ERR_MALLOC;
140 }
141
7d658874
BV
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)) {
a885ce3e 148 sr_dbg("Failed to parse metadata.");
7d658874
BV
149 return SR_ERR;
150 }
151
2285cf9b 152 sr_session_new();
7d658874
BV
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 */
4d684427 163 sdi = NULL;
003595ac 164 enabled_probes = total_probes = 0;
7d658874
BV
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")) {
4d684427
BV
169 sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, NULL, NULL, NULL);
170 sdi->driver = &session_driver;
7d658874 171 if (devcnt == 0)
c09f0b57 172 /* first device, init the driver */
44fc870c 173 sdi->driver->init(NULL);
bd7bfe8c 174 sr_dev_open(sdi);
4d684427 175 sr_session_dev_add(sdi);
003595ac 176 sdi->driver->config_set(SR_CONF_SESSIONFILE,
8f996b89 177 g_variant_new_string(filename), sdi, NULL);
003595ac 178 sdi->driver->config_set(SR_CONF_CAPTUREFILE,
8f996b89 179 g_variant_new_string(val), sdi, NULL);
7d658874
BV
180 g_ptr_array_add(capturefiles, val);
181 } else if (!strcmp(keys[j], "samplerate")) {
f64c1414 182 sr_parse_sizestring(val, &tmp_u64);
003595ac 183 sdi->driver->config_set(SR_CONF_SAMPLERATE,
8f996b89 184 g_variant_new_uint64(tmp_u64), sdi, NULL);
7d658874
BV
185 } else if (!strcmp(keys[j], "unitsize")) {
186 tmp_u64 = strtoull(val, NULL, 10);
003595ac 187 sdi->driver->config_set(SR_CONF_CAPTURE_UNITSIZE,
8f996b89 188 g_variant_new_uint64(tmp_u64), sdi, NULL);
7d658874
BV
189 } else if (!strcmp(keys[j], "total probes")) {
190 total_probes = strtoull(val, NULL, 10);
003595ac 191 sdi->driver->config_set(SR_CONF_CAPTURE_NUM_PROBES,
8f996b89 192 g_variant_new_uint64(total_probes), sdi, NULL);
464d12c7
KS
193 for (p = 0; p < total_probes; p++) {
194 snprintf(probename, SR_MAX_PROBENAME_LEN, "%" PRIu64, p);
fb381e4d 195 if (!(probe = sr_probe_new(p, SR_PROBE_LOGIC, TRUE,
4d684427
BV
196 probename)))
197 return SR_ERR;
198 sdi->probes = g_slist_append(sdi->probes, probe);
464d12c7 199 }
7d658874 200 } else if (!strncmp(keys[j], "probe", 5)) {
4d684427 201 if (!sdi)
7d658874
BV
202 continue;
203 enabled_probes++;
204 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
fb381e4d
BV
205 /* sr_session_save() */
206 sr_dev_probe_name_set(sdi, tmp_u64 - 1, val);
7d658874
BV
207 } else if (!strncmp(keys[j], "trigger", 7)) {
208 probenum = strtoul(keys[j]+7, NULL, 10);
4d684427 209 sr_dev_trigger_set(sdi, probenum, val);
7d658874
BV
210 }
211 }
212 g_strfreev(keys);
fb381e4d 213 /* Disable probes not specifically listed. */
003595ac
BV
214 if (total_probes)
215 for (p = enabled_probes; p < total_probes; p++)
216 sr_dev_probe_enable(sdi, p, FALSE);
7d658874 217 }
4d684427 218 devcnt++;
7d658874
BV
219 }
220 g_strfreev(sections);
221 g_key_file_free(kf);
222
223 return SR_OK;
224}
225
9f45fb3a
UH
226/**
227 * Save the current session to the specified file.
228 *
18bc2704 229 * @param filename The name of the filename to save the current session as.
9f45fb3a 230 * Must not be NULL.
056be071 231 * @param sdi The device instance from which the data was captured.
18bc2704
BV
232 * @param buf The data to be saved.
233 * @param unitsize The number of bytes per sample.
234 * @param units The number of samples.
9f45fb3a
UH
235 *
236 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
237 * upon other errors.
238 */
18bc2704
BV
239SR_API int sr_session_save(const char *filename, const struct sr_dev_inst *sdi,
240 unsigned char *buf, int unitsize, int units)
7d658874 241{
18bc2704 242 GSList *l;
003595ac 243 GVariant *gvar;
7d658874 244 FILE *meta;
1afe8989 245 struct sr_probe *probe;
7d658874
BV
246 struct zip *zipfile;
247 struct zip_source *versrc, *metasrc, *logicsrc;
18bc2704 248 int tmpfile, ret, probecnt;
003595ac 249 uint64_t samplerate;
18bc2704 250 char version[1], rawname[16], metafile[32], *s;
7d658874 251
9f45fb3a 252 if (!filename) {
a885ce3e 253 sr_err("%s: filename was NULL", __func__);
9f45fb3a
UH
254 return SR_ERR_ARG;
255 }
256
7d658874 257 /* Quietly delete it first, libzip wants replace ops otherwise. */
4a1b18f8 258 unlink(filename);
ebc34738 259 if (!(zipfile = zip_open(filename, ZIP_CREATE, &ret)))
7d658874 260 return SR_ERR;
7d658874
BV
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) {
a885ce3e 267 sr_info("error saving version into zipfile: %s",
b08024a8 268 zip_strerror(zipfile));
7d658874
BV
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);
868d8cef 277 meta = g_fopen(metafile, "wb");
7d658874
BV
278 fprintf(meta, "[global]\n");
279 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
7d658874 280
056be071
BV
281 /* metadata */
282 fprintf(meta, "[device 1]\n");
283 if (sdi->driver)
284 fprintf(meta, "driver = %s\n", sdi->driver->name);
7d658874 285
056be071
BV
286 /* metadata */
287 fprintf(meta, "capturefile = logic-1\n");
18bc2704 288 fprintf(meta, "unitsize = %d\n", unitsize);
056be071 289 fprintf(meta, "total probes = %d\n", g_slist_length(sdi->probes));
4d15e5c9 290 if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
8f996b89
ML
291 if (sr_config_get(sdi->driver, sdi, NULL,
292 SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
003595ac
BV
293 samplerate = g_variant_get_uint64(gvar);
294 s = sr_samplerate_string(samplerate);
056be071
BV
295 fprintf(meta, "samplerate = %s\n", s);
296 g_free(s);
003595ac 297 g_variant_unref(gvar);
056be071
BV
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 }
7d658874 311
056be071 312 if (!(logicsrc = zip_source_buffer(zipfile, buf,
18bc2704 313 units * unitsize, FALSE)))
056be071
BV
314 return SR_ERR;
315 snprintf(rawname, 15, "logic-1");
316 if (zip_add(zipfile, rawname, logicsrc) == -1)
317 return SR_ERR;
7d658874
BV
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) {
a885ce3e 326 sr_info("error saving zipfile: %s", zip_strerror(zipfile));
7d658874
BV
327 return SR_ERR;
328 }
329
330 unlink(metafile);
331
332 return SR_OK;
333}
7b870c38 334
f438e0c9
BV
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
7b870c38 396/** @} */