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