]> sigrok.org Git - libsigrok.git/blame - session_file.c
Doxyfile: Set version to 0.2.2.
[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>
24#include <glib.h>
3bbd9849 25#include <glib/gstdio.h>
545f9786 26#include "config.h" /* Needed for PACKAGE_VERSION and others. */
45c59c8b
BV
27#include "libsigrok.h"
28#include "libsigrok-internal.h"
7d658874 29
29a27196
UH
30/* Message logging helpers with subsystem-specific prefix string. */
31#define LOG_PREFIX "session-file: "
32#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
33#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
34#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
35#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
36#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
37#define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
a885ce3e 38
393fb9cb
UH
39/**
40 * @file
41 *
42 * Loading and saving libsigrok session files.
43 */
44
7b870c38 45/**
777e2035 46 * @addtogroup grp_session
7b870c38
UH
47 *
48 * @{
49 */
50
2872d21e 51extern struct sr_session *session;
c09f0b57 52extern SR_PRIV struct sr_dev_driver session_driver;
7d658874 53
23285d06
UH
54/**
55 * Load the session from the specified filename.
56 *
57 * @param filename The name of the session file to load. Must not be NULL.
58 *
59 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments,
60 * SR_ERR_MALLOC upon memory allocation errors, or SR_ERR upon
61 * other errors.
62 */
63SR_API int sr_session_load(const char *filename)
7d658874 64{
23285d06
UH
65 GKeyFile *kf;
66 GPtrArray *capturefiles;
7d658874
BV
67 struct zip *archive;
68 struct zip_file *zf;
69 struct zip_stat zs;
23285d06
UH
70 struct sr_dev_inst *sdi;
71 struct sr_probe *probe;
72 int ret, probenum, devcnt, version, i, j;
73 uint64_t tmp_u64, total_probes, enabled_probes, p;
74 char **sections, **keys, *metafile, *val, s[11];
75 char probename[SR_MAX_PROBENAME_LEN + 1];
7d658874 76
23285d06
UH
77 if (!filename) {
78 sr_err("%s: filename was NULL", __func__);
9f45fb3a 79 return SR_ERR_ARG;
23285d06 80 }
9f45fb3a 81
23285d06
UH
82 if (!(archive = zip_open(filename, 0, &ret))) {
83 sr_dbg("Failed to open session file: zip error %d", ret);
7d658874 84 return SR_ERR;
23285d06 85 }
7d658874
BV
86
87 /* check "version" */
c1864d55 88 version = 0;
7d658874 89 if (!(zf = zip_fopen(archive, "version", 0))) {
23285d06 90 sr_dbg("Not a sigrok session file.");
7d658874
BV
91 return SR_ERR;
92 }
23285d06
UH
93 if ((ret = zip_fread(zf, s, 10)) == -1) {
94 sr_dbg("Not a valid sigrok session file.");
7d658874 95 return SR_ERR;
23285d06 96 }
7d658874 97 zip_fclose(zf);
c1864d55
BV
98 s[ret] = 0;
99 version = strtoull(s, NULL, 10);
100 if (version != 1) {
23285d06 101 sr_dbg("Not a valid sigrok session file version.");
c1864d55
BV
102 return SR_ERR;
103 }
7d658874
BV
104
105 /* read "metadata" */
106 if (zip_stat(archive, "metadata", 0, &zs) == -1) {
a885ce3e 107 sr_dbg("Not a valid sigrok session file.");
7d658874
BV
108 return SR_ERR;
109 }
b53738ba
UH
110
111 if (!(metafile = g_try_malloc(zs.size))) {
a885ce3e 112 sr_err("%s: metafile malloc failed", __func__);
b53738ba
UH
113 return SR_ERR_MALLOC;
114 }
115
7d658874
BV
116 zf = zip_fopen_index(archive, zs.index, 0);
117 zip_fread(zf, metafile, zs.size);
118 zip_fclose(zf);
119
120 kf = g_key_file_new();
121 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
a885ce3e 122 sr_dbg("Failed to parse metadata.");
7d658874
BV
123 return SR_ERR;
124 }
125
2285cf9b 126 sr_session_new();
7d658874
BV
127
128 devcnt = 0;
129 capturefiles = g_ptr_array_new_with_free_func(g_free);
130 sections = g_key_file_get_groups(kf, NULL);
131 for (i = 0; sections[i]; i++) {
132 if (!strcmp(sections[i], "global"))
133 /* nothing really interesting in here yet */
134 continue;
135 if (!strncmp(sections[i], "device ", 7)) {
136 /* device section */
4d684427 137 sdi = NULL;
003595ac 138 enabled_probes = total_probes = 0;
7d658874
BV
139 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
140 for (j = 0; keys[j]; j++) {
141 val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
142 if (!strcmp(keys[j], "capturefile")) {
4d684427
BV
143 sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, NULL, NULL, NULL);
144 sdi->driver = &session_driver;
7d658874 145 if (devcnt == 0)
c09f0b57 146 /* first device, init the driver */
44fc870c 147 sdi->driver->init(NULL);
bd7bfe8c 148 sr_dev_open(sdi);
4d684427 149 sr_session_dev_add(sdi);
003595ac
BV
150 sdi->driver->config_set(SR_CONF_SESSIONFILE,
151 g_variant_new_string(filename), sdi);
152 sdi->driver->config_set(SR_CONF_CAPTUREFILE,
153 g_variant_new_string(val), sdi);
7d658874
BV
154 g_ptr_array_add(capturefiles, val);
155 } else if (!strcmp(keys[j], "samplerate")) {
f64c1414 156 sr_parse_sizestring(val, &tmp_u64);
003595ac
BV
157 sdi->driver->config_set(SR_CONF_SAMPLERATE,
158 g_variant_new_uint64(tmp_u64), sdi);
7d658874
BV
159 } else if (!strcmp(keys[j], "unitsize")) {
160 tmp_u64 = strtoull(val, NULL, 10);
003595ac
BV
161 sdi->driver->config_set(SR_CONF_CAPTURE_UNITSIZE,
162 g_variant_new_uint64(tmp_u64), sdi);
7d658874
BV
163 } else if (!strcmp(keys[j], "total probes")) {
164 total_probes = strtoull(val, NULL, 10);
003595ac
BV
165 sdi->driver->config_set(SR_CONF_CAPTURE_NUM_PROBES,
166 g_variant_new_uint64(total_probes), sdi);
464d12c7
KS
167 for (p = 0; p < total_probes; p++) {
168 snprintf(probename, SR_MAX_PROBENAME_LEN, "%" PRIu64, p);
fb381e4d 169 if (!(probe = sr_probe_new(p, SR_PROBE_LOGIC, TRUE,
4d684427
BV
170 probename)))
171 return SR_ERR;
172 sdi->probes = g_slist_append(sdi->probes, probe);
464d12c7 173 }
7d658874 174 } else if (!strncmp(keys[j], "probe", 5)) {
4d684427 175 if (!sdi)
7d658874
BV
176 continue;
177 enabled_probes++;
178 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
fb381e4d
BV
179 /* sr_session_save() */
180 sr_dev_probe_name_set(sdi, tmp_u64 - 1, val);
7d658874
BV
181 } else if (!strncmp(keys[j], "trigger", 7)) {
182 probenum = strtoul(keys[j]+7, NULL, 10);
4d684427 183 sr_dev_trigger_set(sdi, probenum, val);
7d658874
BV
184 }
185 }
186 g_strfreev(keys);
fb381e4d 187 /* Disable probes not specifically listed. */
003595ac
BV
188 if (total_probes)
189 for (p = enabled_probes; p < total_probes; p++)
190 sr_dev_probe_enable(sdi, p, FALSE);
7d658874 191 }
4d684427 192 devcnt++;
7d658874
BV
193 }
194 g_strfreev(sections);
195 g_key_file_free(kf);
196
197 return SR_OK;
198}
199
9f45fb3a
UH
200/**
201 * Save the current session to the specified file.
202 *
18bc2704 203 * @param filename The name of the filename to save the current session as.
9f45fb3a 204 * Must not be NULL.
056be071 205 * @param sdi The device instance from which the data was captured.
18bc2704
BV
206 * @param buf The data to be saved.
207 * @param unitsize The number of bytes per sample.
208 * @param units The number of samples.
9f45fb3a
UH
209 *
210 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
211 * upon other errors.
212 */
18bc2704
BV
213SR_API int sr_session_save(const char *filename, const struct sr_dev_inst *sdi,
214 unsigned char *buf, int unitsize, int units)
7d658874 215{
18bc2704 216 GSList *l;
003595ac 217 GVariant *gvar;
7d658874 218 FILE *meta;
1afe8989 219 struct sr_probe *probe;
7d658874
BV
220 struct zip *zipfile;
221 struct zip_source *versrc, *metasrc, *logicsrc;
18bc2704 222 int tmpfile, ret, probecnt;
003595ac 223 uint64_t samplerate;
18bc2704 224 char version[1], rawname[16], metafile[32], *s;
7d658874 225
9f45fb3a 226 if (!filename) {
a885ce3e 227 sr_err("%s: filename was NULL", __func__);
9f45fb3a
UH
228 return SR_ERR_ARG;
229 }
230
7d658874 231 /* Quietly delete it first, libzip wants replace ops otherwise. */
4a1b18f8 232 unlink(filename);
ebc34738 233 if (!(zipfile = zip_open(filename, ZIP_CREATE, &ret)))
7d658874 234 return SR_ERR;
7d658874
BV
235
236 /* "version" */
237 version[0] = '1';
238 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
239 return SR_ERR;
240 if (zip_add(zipfile, "version", versrc) == -1) {
a885ce3e 241 sr_info("error saving version into zipfile: %s",
b08024a8 242 zip_strerror(zipfile));
7d658874
BV
243 return SR_ERR;
244 }
245
246 /* init "metadata" */
247 strcpy(metafile, "sigrok-meta-XXXXXX");
248 if ((tmpfile = g_mkstemp(metafile)) == -1)
249 return SR_ERR;
250 close(tmpfile);
868d8cef 251 meta = g_fopen(metafile, "wb");
7d658874
BV
252 fprintf(meta, "[global]\n");
253 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
7d658874 254
056be071
BV
255 /* metadata */
256 fprintf(meta, "[device 1]\n");
257 if (sdi->driver)
258 fprintf(meta, "driver = %s\n", sdi->driver->name);
7d658874 259
056be071
BV
260 /* metadata */
261 fprintf(meta, "capturefile = logic-1\n");
18bc2704 262 fprintf(meta, "unitsize = %d\n", unitsize);
056be071 263 fprintf(meta, "total probes = %d\n", g_slist_length(sdi->probes));
4d15e5c9 264 if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
ddd9b41f 265 if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
003595ac
BV
266 &gvar, sdi) == SR_OK) {
267 samplerate = g_variant_get_uint64(gvar);
268 s = sr_samplerate_string(samplerate);
056be071
BV
269 fprintf(meta, "samplerate = %s\n", s);
270 g_free(s);
003595ac 271 g_variant_unref(gvar);
056be071
BV
272 }
273 }
274 probecnt = 1;
275 for (l = sdi->probes; l; l = l->next) {
276 probe = l->data;
277 if (probe->enabled) {
278 if (probe->name)
279 fprintf(meta, "probe%d = %s\n", probecnt, probe->name);
280 if (probe->trigger)
281 fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger);
282 probecnt++;
283 }
284 }
7d658874 285
056be071 286 if (!(logicsrc = zip_source_buffer(zipfile, buf,
18bc2704 287 units * unitsize, FALSE)))
056be071
BV
288 return SR_ERR;
289 snprintf(rawname, 15, "logic-1");
290 if (zip_add(zipfile, rawname, logicsrc) == -1)
291 return SR_ERR;
7d658874
BV
292 fclose(meta);
293
294 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
295 return SR_ERR;
296 if (zip_add(zipfile, "metadata", metasrc) == -1)
297 return SR_ERR;
298
299 if ((ret = zip_close(zipfile)) == -1) {
a885ce3e 300 sr_info("error saving zipfile: %s", zip_strerror(zipfile));
7d658874
BV
301 return SR_ERR;
302 }
303
304 unlink(metafile);
305
306 return SR_OK;
307}
7b870c38
UH
308
309/** @} */