2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
26 #include <glib/gstdio.h>
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "session-file"
37 * Loading and saving libsigrok session files.
41 * @addtogroup grp_session
46 extern SR_PRIV struct sr_dev_driver session_driver;
47 static int session_driver_initialized = 0;
50 /* Replacement for zip_discard() if it isn't available.
52 SR_PRIV void sr_zip_discard(struct zip *archive)
54 if (zip_unchange_all(archive) < 0 || zip_close(archive) < 0)
55 sr_err("Failed to discard ZIP archive: %s", zip_strerror(archive));
59 /** Read metadata entries from a session archive.
60 * @param[in] archive An open ZIP archive.
61 * @param[in] entry Stat buffer filled in for the metadata archive member.
62 * @return A new key/value store containing the session metadata.
64 SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive,
65 const struct zip_stat *entry)
73 if (entry->size > G_MAXINT || !(metabuf = g_try_malloc(entry->size))) {
74 sr_err("Metadata buffer allocation failed.");
77 zf = zip_fopen_index(archive, entry->index, 0);
79 sr_err("Failed to open metadata: %s", zip_strerror(archive));
83 metalen = zip_fread(zf, metabuf, entry->size);
85 sr_err("Failed to read metadata: %s", zip_file_strerror(zf));
92 keyfile = g_key_file_new();
94 g_key_file_load_from_data(keyfile, metabuf, metalen,
95 G_KEY_FILE_NONE, &error);
99 sr_err("Failed to parse metadata: %s", error->message);
101 g_key_file_free(keyfile);
108 SR_PRIV int sr_sessionfile_check(const char *filename)
120 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
121 sr_err("Not a regular file: %s.", filename);
125 if (!(archive = zip_open(filename, 0, NULL)))
126 /* No logging: this can be used just to check if it's
127 * a sigrok session file or not. */
130 /* check "version" */
131 if (!(zf = zip_fopen(archive, "version", 0))) {
132 sr_dbg("Not a sigrok session file: no version found.");
133 zip_discard(archive);
136 ret = zip_fread(zf, s, sizeof(s) - 1);
138 sr_err("Failed to read version file: %s",
139 zip_file_strerror(zf));
141 zip_discard(archive);
146 version = g_ascii_strtoull(s, NULL, 10);
147 if (version == 0 || version > 2) {
148 sr_dbg("Cannot handle sigrok session file version %" PRIu64 ".",
150 zip_discard(archive);
153 sr_spew("Detected sigrok session file version %" PRIu64 ".", version);
155 /* read "metadata" */
156 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
157 sr_dbg("Not a valid sigrok session file.");
158 zip_discard(archive);
161 zip_discard(archive);
167 * Load the session from the specified filename.
169 * @param ctx The context in which to load the session.
170 * @param filename The name of the session file to load.
171 * @param session The session to load the file into.
173 * @retval SR_OK Success
174 * @retval SR_ERR_MALLOC Memory allocation error
175 * @retval SR_ERR_DATA Malformed session file
176 * @retval SR_ERR This is not a session file
178 SR_API int sr_session_load(struct sr_context *ctx, const char *filename,
179 struct sr_session **session)
185 struct sr_dev_inst *sdi;
186 struct sr_channel *ch;
189 int total_channels, total_analog, k;
192 char **sections, **keys, *val;
193 char channelname[SR_MAX_CHANNELNAME_LEN + 1];
195 if ((ret = sr_sessionfile_check(filename)) != SR_OK)
198 if (!(archive = zip_open(filename, 0, NULL)))
201 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
202 zip_discard(archive);
205 kf = sr_sessionfile_read_metadata(archive, &zs);
206 zip_discard(archive);
210 if ((ret = sr_session_new(ctx, session)) != SR_OK) {
217 sections = g_key_file_get_groups(kf, NULL);
218 for (i = 0; sections[i] && ret == SR_OK; i++) {
219 if (!strcmp(sections[i], "global"))
220 /* nothing really interesting in here yet */
222 if (!strncmp(sections[i], "device ", 7)) {
225 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
226 for (j = 0; keys[j]; j++) {
227 if (!strcmp(keys[j], "capturefile")) {
228 val = g_key_file_get_string(kf, sections[i],
234 sdi = g_malloc0(sizeof(struct sr_dev_inst));
235 sdi->driver = &session_driver;
236 sdi->status = SR_ST_ACTIVE;
237 if (!session_driver_initialized) {
238 /* first device, init the driver */
239 session_driver_initialized = 1;
240 sdi->driver->init(sdi->driver, NULL);
243 sr_session_dev_add(*session, sdi);
244 (*session)->owned_devs = g_slist_append(
245 (*session)->owned_devs, sdi);
246 sr_config_set(sdi, NULL, SR_CONF_SESSIONFILE,
247 g_variant_new_string(filename));
248 sr_config_set(sdi, NULL, SR_CONF_CAPTUREFILE,
249 g_variant_new_string(val));
251 } else if (!strcmp(keys[j], "samplerate")) {
252 val = g_key_file_get_string(kf, sections[i],
254 if (!sdi || !val || sr_parse_sizestring(val,
255 &tmp_u64) != SR_OK) {
261 sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
262 g_variant_new_uint64(tmp_u64));
263 } else if (!strcmp(keys[j], "unitsize")) {
264 unitsize = g_key_file_get_integer(kf, sections[i],
266 if (!sdi || unitsize <= 0 || error) {
270 sr_config_set(sdi, NULL, SR_CONF_CAPTURE_UNITSIZE,
271 g_variant_new_uint64(unitsize));
272 } else if (!strcmp(keys[j], "total probes")) {
273 total_channels = g_key_file_get_integer(kf,
274 sections[i], keys[j], &error);
275 if (!sdi || total_channels < 0 || error) {
279 sr_config_set(sdi, NULL, SR_CONF_NUM_LOGIC_CHANNELS,
280 g_variant_new_int32(total_channels));
281 for (k = 0; k < total_channels; k++) {
282 g_snprintf(channelname, sizeof(channelname),
284 sr_channel_new(sdi, k, SR_CHANNEL_LOGIC,
287 } else if (!strcmp(keys[j], "total analog")) {
288 total_analog = g_key_file_get_integer(kf,
289 sections[i], keys[j], &error);
290 if (!sdi || total_analog < 0 || error) {
294 sr_config_set(sdi, NULL, SR_CONF_NUM_ANALOG_CHANNELS,
295 g_variant_new_int32(total_analog));
296 for (k = 0; k < total_analog; k++) {
297 g_snprintf(channelname, sizeof(channelname),
299 sr_channel_new(sdi, k, SR_CHANNEL_ANALOG,
302 } else if (!strncmp(keys[j], "probe", 5)) {
303 tmp_u64 = g_ascii_strtoull(keys[j] + 5, NULL, 10);
304 if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
308 ch = g_slist_nth_data(sdi->channels, tmp_u64 - 1);
313 val = g_key_file_get_string(kf, sections[i],
319 /* sr_session_save() */
320 sr_dev_channel_name_set(ch, val);
322 sr_dev_channel_enable(ch, TRUE);
323 } else if (!strncmp(keys[j], "analog", 6)) {
324 tmp_u64 = g_ascii_strtoull(keys[j]+6, NULL, 10);
325 if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
330 for (l = sdi->channels; l; l = l->next) {
332 if ((guint64)ch->index == tmp_u64 - 1)
341 val = g_key_file_get_string(kf, sections[i],
347 /* sr_session_save() */
348 sr_dev_channel_name_set(ch, val);
350 sr_dev_channel_enable(ch, TRUE);
356 g_strfreev(sections);
360 sr_err("Failed to parse metadata: %s", error->message);