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