]> sigrok.org Git - libsigrok.git/blame - src/session_file.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / 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
6ec6c43b 20#include <config.h>
7d658874
BV
21#include <string.h>
22#include <stdlib.h>
7d658874 23#include <zip.h>
5f9c4c8a 24#include <errno.h>
7d658874 25#include <glib.h>
3bbd9849 26#include <glib/gstdio.h>
c1aae900 27#include <libsigrok/libsigrok.h>
45c59c8b 28#include "libsigrok-internal.h"
7d658874 29
2ad1deb8 30/** @cond PRIVATE */
3544f848 31#define LOG_PREFIX "session-file"
2ad1deb8 32/** @endcond */
a885ce3e 33
393fb9cb
UH
34/**
35 * @file
36 *
37 * Loading and saving libsigrok session files.
38 */
39
7b870c38 40/**
777e2035 41 * @addtogroup grp_session
7b870c38
UH
42 *
43 * @{
44 */
45
82b9f3d1 46/** @cond PRIVATE */
c09f0b57 47extern SR_PRIV struct sr_dev_driver session_driver;
82b9f3d1 48/** @endcond */
c6805a02
SA
49static int session_driver_initialized = 0;
50
a6dc3dac 51#if !HAVE_ZIP_DISCARD
f200d59e
UH
52/* Replacement for zip_discard() if it isn't available. */
53/** @private */
a6dc3dac
DE
54SR_PRIV void sr_zip_discard(struct zip *archive)
55{
56 if (zip_unchange_all(archive) < 0 || zip_close(archive) < 0)
57 sr_err("Failed to discard ZIP archive: %s", zip_strerror(archive));
58}
59#endif
60
f200d59e
UH
61/**
62 * Read metadata entries from a session archive.
63 *
5e1fb334
DE
64 * @param[in] archive An open ZIP archive.
65 * @param[in] entry Stat buffer filled in for the metadata archive member.
f200d59e 66 *
5e1fb334 67 * @return A new key/value store containing the session metadata.
f200d59e
UH
68 *
69 * @private
5e1fb334
DE
70 */
71SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive,
72 const struct zip_stat *entry)
73{
74 GKeyFile *keyfile;
75 GError *error;
76 struct zip_file *zf;
77 char *metabuf;
78 int metalen;
79
80 if (entry->size > G_MAXINT || !(metabuf = g_try_malloc(entry->size))) {
81 sr_err("Metadata buffer allocation failed.");
82 return NULL;
83 }
84 zf = zip_fopen_index(archive, entry->index, 0);
85 if (!zf) {
86 sr_err("Failed to open metadata: %s", zip_strerror(archive));
87 g_free(metabuf);
88 return NULL;
89 }
90 metalen = zip_fread(zf, metabuf, entry->size);
91 if (metalen < 0) {
92 sr_err("Failed to read metadata: %s", zip_file_strerror(zf));
93 zip_fclose(zf);
94 g_free(metabuf);
95 return NULL;
96 }
97 zip_fclose(zf);
98
99 keyfile = g_key_file_new();
100 error = NULL;
101 g_key_file_load_from_data(keyfile, metabuf, metalen,
102 G_KEY_FILE_NONE, &error);
103 g_free(metabuf);
104
105 if (error) {
106 sr_err("Failed to parse metadata: %s", error->message);
107 g_error_free(error);
108 g_key_file_free(keyfile);
109 return NULL;
110 }
111 return keyfile;
112}
113
72a08bcc 114/** @private */
f438e0c9 115SR_PRIV int sr_sessionfile_check(const char *filename)
7d658874 116{
7d658874
BV
117 struct zip *archive;
118 struct zip_file *zf;
119 struct zip_stat zs;
5e1fb334
DE
120 uint64_t version;
121 int ret;
f438e0c9 122 char s[11];
7d658874 123
f438e0c9 124 if (!filename)
9f45fb3a 125 return SR_ERR_ARG;
9f45fb3a 126
5e1fb334
DE
127 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
128 sr_err("Not a regular file: %s.", filename);
5f9c4c8a
BV
129 return SR_ERR;
130 }
131
5e1fb334 132 if (!(archive = zip_open(filename, 0, NULL)))
f438e0c9
BV
133 /* No logging: this can be used just to check if it's
134 * a sigrok session file or not. */
7d658874 135 return SR_ERR;
7d658874
BV
136
137 /* check "version" */
138 if (!(zf = zip_fopen(archive, "version", 0))) {
f438e0c9 139 sr_dbg("Not a sigrok session file: no version found.");
5e1fb334 140 zip_discard(archive);
7d658874
BV
141 return SR_ERR;
142 }
5e1fb334
DE
143 ret = zip_fread(zf, s, sizeof(s) - 1);
144 if (ret < 0) {
145 sr_err("Failed to read version file: %s",
146 zip_file_strerror(zf));
147 zip_fclose(zf);
148 zip_discard(archive);
7d658874 149 return SR_ERR;
5e1fb334 150 }
7d658874 151 zip_fclose(zf);
5e1fb334
DE
152 s[ret] = '\0';
153 version = g_ascii_strtoull(s, NULL, 10);
154 if (version == 0 || version > 2) {
155 sr_dbg("Cannot handle sigrok session file version %" PRIu64 ".",
156 version);
157 zip_discard(archive);
c1864d55
BV
158 return SR_ERR;
159 }
5e1fb334 160 sr_spew("Detected sigrok session file version %" PRIu64 ".", version);
7d658874
BV
161
162 /* read "metadata" */
5e1fb334 163 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
a885ce3e 164 sr_dbg("Not a valid sigrok session file.");
5e1fb334 165 zip_discard(archive);
7d658874
BV
166 return SR_ERR;
167 }
5e1fb334 168 zip_discard(archive);
aba57f35 169
f438e0c9
BV
170 return SR_OK;
171}
07182332 172
f200d59e 173/** @private */
7c69b528
SA
174SR_PRIV struct sr_dev_inst *sr_session_prepare_sdi(const char *filename, struct sr_session **session)
175{
176 struct sr_dev_inst *sdi = NULL;
177
178 sdi = g_malloc0(sizeof(struct sr_dev_inst));
179 sdi->driver = &session_driver;
e3747867 180 sdi->status = SR_ST_INACTIVE;
7c69b528
SA
181 if (!session_driver_initialized) {
182 /* first device, init the driver */
183 session_driver_initialized = 1;
184 sdi->driver->init(sdi->driver, NULL);
185 }
186 sr_dev_open(sdi);
187 sr_session_dev_add(*session, sdi);
188 (*session)->owned_devs = g_slist_append((*session)->owned_devs, sdi);
189 sr_config_set(sdi, NULL, SR_CONF_SESSIONFILE,
190 g_variant_new_string(filename));
191
192 return sdi;
193}
194
f438e0c9
BV
195/**
196 * Load the session from the specified filename.
197 *
61e6e2da 198 * @param ctx The context in which to load the session.
d386a52f
BV
199 * @param filename The name of the session file to load.
200 * @param session The session to load the file into.
f438e0c9 201 *
d386a52f
BV
202 * @retval SR_OK Success
203 * @retval SR_ERR_MALLOC Memory allocation error
204 * @retval SR_ERR_DATA Malformed session file
205 * @retval SR_ERR This is not a session file
f438e0c9 206 */
61e6e2da
ML
207SR_API int sr_session_load(struct sr_context *ctx, const char *filename,
208 struct sr_session **session)
f438e0c9
BV
209{
210 GKeyFile *kf;
5e1fb334 211 GError *error;
f438e0c9 212 struct zip *archive;
f438e0c9
BV
213 struct zip_stat zs;
214 struct sr_dev_inst *sdi;
6f1346fb 215 struct sr_channel *ch;
c6805a02 216 int ret, i, j;
5e1fb334 217 uint64_t tmp_u64;
b317d1bb
ML
218 int total_channels, total_analog, k;
219 GSList *l;
5e1fb334
DE
220 int unitsize;
221 char **sections, **keys, *val;
3f239f08 222 char channelname[SR_MAX_CHANNELNAME_LEN + 1];
9cfc695f 223 gboolean file_has_logic;
f438e0c9
BV
224
225 if ((ret = sr_sessionfile_check(filename)) != SR_OK)
226 return ret;
227
5e1fb334 228 if (!(archive = zip_open(filename, 0, NULL)))
f438e0c9
BV
229 return SR_ERR;
230
5e1fb334
DE
231 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
232 zip_discard(archive);
f438e0c9 233 return SR_ERR;
b53738ba 234 }
5e1fb334
DE
235 kf = sr_sessionfile_read_metadata(archive, &zs);
236 zip_discard(archive);
237 if (!kf)
238 return SR_ERR_DATA;
b53738ba 239
5e1fb334
DE
240 if ((ret = sr_session_new(ctx, session)) != SR_OK) {
241 g_key_file_free(kf);
0812c40e 242 return ret;
5e1fb334 243 }
7d658874 244
f476dd2d
SA
245 total_channels = 0;
246
5e1fb334 247 error = NULL;
d386a52f 248 ret = SR_OK;
9cfc695f 249 file_has_logic = FALSE;
7d658874 250 sections = g_key_file_get_groups(kf, NULL);
d386a52f 251 for (i = 0; sections[i] && ret == SR_OK; i++) {
7d658874
BV
252 if (!strcmp(sections[i], "global"))
253 /* nothing really interesting in here yet */
254 continue;
255 if (!strncmp(sections[i], "device ", 7)) {
256 /* device section */
4d684427 257 sdi = NULL;
7d658874 258 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
7c69b528
SA
259
260 /* File contains analog data if there are analog channels. */
261 total_analog = g_key_file_get_integer(kf, sections[i],
262 "total analog", &error);
263 if (total_analog > 0 && !error)
264 sdi = sr_session_prepare_sdi(filename, session);
265 g_clear_error(&error);
266
267 /* File contains logic data if a capturefile is set. */
268 val = g_key_file_get_string(kf, sections[i],
269 "capturefile", &error);
270 if (val && !error) {
271 if (!sdi)
272 sdi = sr_session_prepare_sdi(filename, session);
273 sr_config_set(sdi, NULL, SR_CONF_CAPTUREFILE,
274 g_variant_new_string(val));
275 g_free(val);
9cfc695f 276 file_has_logic = TRUE;
7c69b528
SA
277 }
278 g_clear_error(&error);
279
7d658874 280 for (j = 0; keys[j]; j++) {
7c69b528 281 if (!strcmp(keys[j], "samplerate")) {
5e1fb334
DE
282 val = g_key_file_get_string(kf, sections[i],
283 keys[j], &error);
284 if (!sdi || !val || sr_parse_sizestring(val,
285 &tmp_u64) != SR_OK) {
286 g_free(val);
d386a52f
BV
287 ret = SR_ERR_DATA;
288 break;
289 }
5e1fb334
DE
290 g_free(val);
291 sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
292 g_variant_new_uint64(tmp_u64));
9cfc695f 293 } else if (!strcmp(keys[j], "unitsize") && file_has_logic) {
5e1fb334
DE
294 unitsize = g_key_file_get_integer(kf, sections[i],
295 keys[j], &error);
296 if (!sdi || unitsize <= 0 || error) {
d386a52f
BV
297 ret = SR_ERR_DATA;
298 break;
299 }
5e1fb334
DE
300 sr_config_set(sdi, NULL, SR_CONF_CAPTURE_UNITSIZE,
301 g_variant_new_uint64(unitsize));
7d658874 302 } else if (!strcmp(keys[j], "total probes")) {
5e1fb334
DE
303 total_channels = g_key_file_get_integer(kf,
304 sections[i], keys[j], &error);
305 if (!sdi || total_channels < 0 || error) {
d386a52f
BV
306 ret = SR_ERR_DATA;
307 break;
308 }
5e1fb334 309 sr_config_set(sdi, NULL, SR_CONF_NUM_LOGIC_CHANNELS,
2c38a41a 310 g_variant_new_int32(total_channels));
5e1fb334 311 for (k = 0; k < total_channels; k++) {
b3cfc6e9 312 g_snprintf(channelname, sizeof(channelname),
5e1fb334
DE
313 "%d", k);
314 sr_channel_new(sdi, k, SR_CHANNEL_LOGIC,
315 FALSE, channelname);
464d12c7 316 }
b317d1bb
ML
317 } else if (!strcmp(keys[j], "total analog")) {
318 total_analog = g_key_file_get_integer(kf,
319 sections[i], keys[j], &error);
320 if (!sdi || total_analog < 0 || error) {
321 ret = SR_ERR_DATA;
322 break;
323 }
324 sr_config_set(sdi, NULL, SR_CONF_NUM_ANALOG_CHANNELS,
325 g_variant_new_int32(total_analog));
e5b280e4 326 for (k = total_channels; k < (total_channels + total_analog); k++) {
b317d1bb
ML
327 g_snprintf(channelname, sizeof(channelname),
328 "%d", k);
329 sr_channel_new(sdi, k, SR_CHANNEL_ANALOG,
330 FALSE, channelname);
331 }
7d658874 332 } else if (!strncmp(keys[j], "probe", 5)) {
0cadb8a3 333 tmp_u64 = g_ascii_strtoull(keys[j] + 5, NULL, 10);
5e1fb334
DE
334 if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
335 ret = SR_ERR_DATA;
336 break;
337 }
338 ch = g_slist_nth_data(sdi->channels, tmp_u64 - 1);
339 if (!ch) {
340 ret = SR_ERR_DATA;
341 break;
342 }
343 val = g_key_file_get_string(kf, sections[i],
344 keys[j], &error);
345 if (!val) {
d386a52f
BV
346 ret = SR_ERR_DATA;
347 break;
348 }
fb381e4d 349 /* sr_session_save() */
6f1346fb 350 sr_dev_channel_name_set(ch, val);
5e1fb334 351 g_free(val);
6f1346fb 352 sr_dev_channel_enable(ch, TRUE);
b317d1bb
ML
353 } else if (!strncmp(keys[j], "analog", 6)) {
354 tmp_u64 = g_ascii_strtoull(keys[j]+6, NULL, 10);
355 if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
356 ret = SR_ERR_DATA;
357 break;
358 }
359 ch = NULL;
360 for (l = sdi->channels; l; l = l->next) {
361 ch = l->data;
362 if ((guint64)ch->index == tmp_u64 - 1)
363 break;
364 else
365 ch = NULL;
366 }
367 if (!ch) {
368 ret = SR_ERR_DATA;
369 break;
370 }
371 val = g_key_file_get_string(kf, sections[i],
372 keys[j], &error);
373 if (!val) {
374 ret = SR_ERR_DATA;
375 break;
376 }
377 /* sr_session_save() */
378 sr_dev_channel_name_set(ch, val);
379 g_free(val);
380 sr_dev_channel_enable(ch, TRUE);
7d658874
BV
381 }
382 }
383 g_strfreev(keys);
7d658874
BV
384 }
385 }
386 g_strfreev(sections);
387 g_key_file_free(kf);
388
5e1fb334
DE
389 if (error) {
390 sr_err("Failed to parse metadata: %s", error->message);
391 g_error_free(error);
a769b9f3 392 }
a769b9f3
BV
393 return ret;
394}
395
7b870c38 396/** @} */