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));
60 * Read metadata entries from a session archive.
62 * @param[in] archive An open ZIP archive.
63 * @param[in] entry Stat buffer filled in for the metadata archive member.
65 * @return A new key/value store containing the session metadata.
69 SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive,
70 const struct zip_stat *entry)
78 if (entry->size > G_MAXINT || !(metabuf = g_try_malloc(entry->size))) {
79 sr_err("Metadata buffer allocation failed.");
82 zf = zip_fopen_index(archive, entry->index, 0);
84 sr_err("Failed to open metadata: %s", zip_strerror(archive));
88 metalen = zip_fread(zf, metabuf, entry->size);
90 sr_err("Failed to read metadata: %s", zip_file_strerror(zf));
97 keyfile = g_key_file_new();
99 g_key_file_load_from_data(keyfile, metabuf, metalen,
100 G_KEY_FILE_NONE, &error);
104 sr_err("Failed to parse metadata: %s", error->message);
106 g_key_file_free(keyfile);
113 SR_PRIV int sr_sessionfile_check(const char *filename)
125 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
126 sr_err("Not a regular file: %s.", filename);
130 if (!(archive = zip_open(filename, 0, NULL)))
131 /* No logging: this can be used just to check if it's
132 * a sigrok session file or not. */
135 /* check "version" */
136 if (!(zf = zip_fopen(archive, "version", 0))) {
137 sr_dbg("Not a sigrok session file: no version found.");
138 zip_discard(archive);
141 ret = zip_fread(zf, s, sizeof(s) - 1);
143 sr_err("Failed to read version file: %s",
144 zip_file_strerror(zf));
146 zip_discard(archive);
151 version = g_ascii_strtoull(s, NULL, 10);
152 if (version == 0 || version > 2) {
153 sr_dbg("Cannot handle sigrok session file version %" PRIu64 ".",
155 zip_discard(archive);
158 sr_spew("Detected sigrok session file version %" PRIu64 ".", version);
160 /* read "metadata" */
161 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
162 sr_dbg("Not a valid sigrok session file.");
163 zip_discard(archive);
166 zip_discard(archive);
172 SR_PRIV struct sr_dev_inst *sr_session_prepare_sdi(const char *filename, struct sr_session **session)
174 struct sr_dev_inst *sdi = NULL;
176 sdi = g_malloc0(sizeof(struct sr_dev_inst));
177 sdi->driver = &session_driver;
178 sdi->status = SR_ST_INACTIVE;
179 if (!session_driver_initialized) {
180 /* first device, init the driver */
181 session_driver_initialized = 1;
182 sdi->driver->init(sdi->driver, NULL);
185 sr_session_dev_add(*session, sdi);
186 (*session)->owned_devs = g_slist_append((*session)->owned_devs, sdi);
187 sr_config_set(sdi, NULL, SR_CONF_SESSIONFILE,
188 g_variant_new_string(filename));
194 * Load the session from the specified filename.
196 * @param ctx The context in which to load the session.
197 * @param filename The name of the session file to load.
198 * @param session The session to load the file into.
200 * @retval SR_OK Success
201 * @retval SR_ERR_MALLOC Memory allocation error
202 * @retval SR_ERR_DATA Malformed session file
203 * @retval SR_ERR This is not a session file
205 SR_API int sr_session_load(struct sr_context *ctx, const char *filename,
206 struct sr_session **session)
212 struct sr_dev_inst *sdi;
213 struct sr_channel *ch;
216 int total_channels, total_analog, k;
219 char **sections, **keys, *val;
220 char channelname[SR_MAX_CHANNELNAME_LEN + 1];
221 gboolean file_has_logic;
223 if ((ret = sr_sessionfile_check(filename)) != SR_OK)
226 if (!(archive = zip_open(filename, 0, NULL)))
229 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
230 zip_discard(archive);
233 kf = sr_sessionfile_read_metadata(archive, &zs);
234 zip_discard(archive);
238 if ((ret = sr_session_new(ctx, session)) != SR_OK) {
247 file_has_logic = FALSE;
248 sections = g_key_file_get_groups(kf, NULL);
249 for (i = 0; sections[i] && ret == SR_OK; i++) {
250 if (!strcmp(sections[i], "global"))
251 /* nothing really interesting in here yet */
253 if (!strncmp(sections[i], "device ", 7)) {
256 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
258 /* File contains analog data if there are analog channels. */
259 total_analog = g_key_file_get_integer(kf, sections[i],
260 "total analog", &error);
261 if (total_analog > 0 && !error)
262 sdi = sr_session_prepare_sdi(filename, session);
263 g_clear_error(&error);
265 /* File contains logic data if a capturefile is set. */
266 val = g_key_file_get_string(kf, sections[i],
267 "capturefile", &error);
270 sdi = sr_session_prepare_sdi(filename, session);
271 sr_config_set(sdi, NULL, SR_CONF_CAPTUREFILE,
272 g_variant_new_string(val));
274 file_has_logic = TRUE;
276 g_clear_error(&error);
278 for (j = 0; keys[j]; j++) {
279 if (!strcmp(keys[j], "samplerate")) {
280 val = g_key_file_get_string(kf, sections[i],
282 if (!sdi || !val || sr_parse_sizestring(val,
283 &tmp_u64) != SR_OK) {
289 sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
290 g_variant_new_uint64(tmp_u64));
291 } else if (!strcmp(keys[j], "unitsize") && file_has_logic) {
292 unitsize = g_key_file_get_integer(kf, sections[i],
294 if (!sdi || unitsize <= 0 || error) {
298 sr_config_set(sdi, NULL, SR_CONF_CAPTURE_UNITSIZE,
299 g_variant_new_uint64(unitsize));
300 } else if (!strcmp(keys[j], "total probes")) {
301 total_channels = g_key_file_get_integer(kf,
302 sections[i], keys[j], &error);
303 if (!sdi || total_channels < 0 || error) {
307 sr_config_set(sdi, NULL, SR_CONF_NUM_LOGIC_CHANNELS,
308 g_variant_new_int32(total_channels));
309 for (k = 0; k < total_channels; k++) {
310 g_snprintf(channelname, sizeof(channelname),
312 sr_channel_new(sdi, k, SR_CHANNEL_LOGIC,
315 } else if (!strcmp(keys[j], "total analog")) {
316 total_analog = g_key_file_get_integer(kf,
317 sections[i], keys[j], &error);
318 if (!sdi || total_analog < 0 || error) {
322 sr_config_set(sdi, NULL, SR_CONF_NUM_ANALOG_CHANNELS,
323 g_variant_new_int32(total_analog));
324 for (k = total_channels; k < (total_channels + total_analog); k++) {
325 g_snprintf(channelname, sizeof(channelname),
327 sr_channel_new(sdi, k, SR_CHANNEL_ANALOG,
330 } else if (!strncmp(keys[j], "probe", 5)) {
331 tmp_u64 = g_ascii_strtoull(keys[j] + 5, NULL, 10);
332 if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
336 ch = g_slist_nth_data(sdi->channels, 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);
351 } else if (!strncmp(keys[j], "analog", 6)) {
352 tmp_u64 = g_ascii_strtoull(keys[j]+6, NULL, 10);
353 if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
358 for (l = sdi->channels; l; l = l->next) {
360 if ((guint64)ch->index == tmp_u64 - 1)
369 val = g_key_file_get_string(kf, sections[i],
375 /* sr_session_save() */
376 sr_dev_channel_name_set(ch, val);
378 sr_dev_channel_enable(ch, TRUE);
384 g_strfreev(sections);
388 sr_err("Failed to parse metadata: %s", error->message);