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
47 extern SR_PRIV struct sr_dev_driver session_driver;
49 static int session_driver_initialized = 0;
52 /* Replacement for zip_discard() if it isn't available. */
54 SR_PRIV void sr_zip_discard(struct zip *archive)
56 if (zip_unchange_all(archive) < 0 || zip_close(archive) < 0)
57 sr_err("Failed to discard ZIP archive: %s", zip_strerror(archive));
62 * Read metadata entries from a session archive.
64 * @param[in] archive An open ZIP archive.
65 * @param[in] entry Stat buffer filled in for the metadata archive member.
67 * @return A new key/value store containing the session metadata.
71 SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive,
72 const struct zip_stat *entry)
80 if (entry->size > G_MAXINT || !(metabuf = g_try_malloc(entry->size))) {
81 sr_err("Metadata buffer allocation failed.");
84 zf = zip_fopen_index(archive, entry->index, 0);
86 sr_err("Failed to open metadata: %s", zip_strerror(archive));
90 metalen = zip_fread(zf, metabuf, entry->size);
92 sr_err("Failed to read metadata: %s", zip_file_strerror(zf));
99 keyfile = g_key_file_new();
101 g_key_file_load_from_data(keyfile, metabuf, metalen,
102 G_KEY_FILE_NONE, &error);
106 sr_err("Failed to parse metadata: %s", error->message);
108 g_key_file_free(keyfile);
115 SR_PRIV int sr_sessionfile_check(const char *filename)
127 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
128 sr_err("Not a regular file: %s.", filename);
132 if (!(archive = zip_open(filename, 0, NULL)))
133 /* No logging: this can be used just to check if it's
134 * a sigrok session file or not. */
137 /* check "version" */
138 if (!(zf = zip_fopen(archive, "version", 0))) {
139 sr_dbg("Not a sigrok session file: no version found.");
140 zip_discard(archive);
143 ret = zip_fread(zf, s, sizeof(s) - 1);
145 sr_err("Failed to read version file: %s",
146 zip_file_strerror(zf));
148 zip_discard(archive);
153 version = g_ascii_strtoull(s, NULL, 10);
154 if (version == 0 || version > 2) {
155 sr_dbg("Cannot handle sigrok session file version %" PRIu64 ".",
157 zip_discard(archive);
160 sr_spew("Detected sigrok session file version %" PRIu64 ".", version);
162 /* read "metadata" */
163 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
164 sr_dbg("Not a valid sigrok session file.");
165 zip_discard(archive);
168 zip_discard(archive);
174 SR_PRIV struct sr_dev_inst *sr_session_prepare_sdi(const char *filename, struct sr_session **session)
176 struct sr_dev_inst *sdi = NULL;
178 sdi = g_malloc0(sizeof(struct sr_dev_inst));
179 sdi->driver = &session_driver;
180 sdi->status = SR_ST_INACTIVE;
181 if (!session_driver_initialized) {
182 /* first device, init the driver */
183 session_driver_initialized = 1;
184 sdi->driver->init(sdi->driver, NULL);
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));
196 * Load the session from the specified filename.
198 * @param ctx The context in which to load the session.
199 * @param filename The name of the session file to load.
200 * @param session The session to load the file into.
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
207 SR_API int sr_session_load(struct sr_context *ctx, const char *filename,
208 struct sr_session **session)
214 struct sr_dev_inst *sdi;
215 struct sr_channel *ch;
218 int total_channels, total_analog, k;
221 char **sections, **keys, *val;
222 char channelname[SR_MAX_CHANNELNAME_LEN + 1];
223 gboolean file_has_logic;
225 if ((ret = sr_sessionfile_check(filename)) != SR_OK)
228 if (!(archive = zip_open(filename, 0, NULL)))
231 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
232 zip_discard(archive);
235 kf = sr_sessionfile_read_metadata(archive, &zs);
236 zip_discard(archive);
240 if ((ret = sr_session_new(ctx, session)) != SR_OK) {
249 file_has_logic = FALSE;
250 sections = g_key_file_get_groups(kf, NULL);
251 for (i = 0; sections[i] && ret == SR_OK; i++) {
252 if (!strcmp(sections[i], "global"))
253 /* nothing really interesting in here yet */
255 if (!strncmp(sections[i], "device ", 7)) {
258 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
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);
267 /* File contains logic data if a capturefile is set. */
268 val = g_key_file_get_string(kf, sections[i],
269 "capturefile", &error);
272 sdi = sr_session_prepare_sdi(filename, session);
273 sr_config_set(sdi, NULL, SR_CONF_CAPTUREFILE,
274 g_variant_new_string(val));
276 file_has_logic = TRUE;
278 g_clear_error(&error);
280 for (j = 0; keys[j]; j++) {
281 if (!strcmp(keys[j], "samplerate")) {
282 val = g_key_file_get_string(kf, sections[i],
284 if (!sdi || !val || sr_parse_sizestring(val,
285 &tmp_u64) != SR_OK) {
291 sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
292 g_variant_new_uint64(tmp_u64));
293 } else if (!strcmp(keys[j], "unitsize") && file_has_logic) {
294 unitsize = g_key_file_get_integer(kf, sections[i],
296 if (!sdi || unitsize <= 0 || error) {
300 sr_config_set(sdi, NULL, SR_CONF_CAPTURE_UNITSIZE,
301 g_variant_new_uint64(unitsize));
302 } else if (!strcmp(keys[j], "total probes")) {
303 total_channels = g_key_file_get_integer(kf,
304 sections[i], keys[j], &error);
305 if (!sdi || total_channels < 0 || error) {
309 sr_config_set(sdi, NULL, SR_CONF_NUM_LOGIC_CHANNELS,
310 g_variant_new_int32(total_channels));
311 for (k = 0; k < total_channels; k++) {
312 g_snprintf(channelname, sizeof(channelname),
314 sr_channel_new(sdi, k, SR_CHANNEL_LOGIC,
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) {
324 sr_config_set(sdi, NULL, SR_CONF_NUM_ANALOG_CHANNELS,
325 g_variant_new_int32(total_analog));
326 for (k = total_channels; k < (total_channels + total_analog); k++) {
327 g_snprintf(channelname, sizeof(channelname),
329 sr_channel_new(sdi, k, SR_CHANNEL_ANALOG,
332 } else if (!strncmp(keys[j], "probe", 5)) {
333 tmp_u64 = g_ascii_strtoull(keys[j] + 5, NULL, 10);
334 if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
338 ch = g_slist_nth_data(sdi->channels, tmp_u64 - 1);
343 val = g_key_file_get_string(kf, sections[i],
349 /* sr_session_save() */
350 sr_dev_channel_name_set(ch, val);
352 sr_dev_channel_enable(ch, TRUE);
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) {
360 for (l = sdi->channels; l; l = l->next) {
362 if ((guint64)ch->index == tmp_u64 - 1)
371 val = g_key_file_get_string(kf, sections[i],
377 /* sr_session_save() */
378 sr_dev_channel_name_set(ch, val);
380 sr_dev_channel_enable(ch, TRUE);
386 g_strfreev(sections);
390 sr_err("Failed to parse metadata: %s", error->message);