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;
49 /** Read metadata entries from a session archive.
50 * @param[in] archive An open ZIP archive.
51 * @param[in] entry Stat buffer filled in for the metadata archive member.
52 * @return A new key/value store containing the session metadata.
54 SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive,
55 const struct zip_stat *entry)
63 if (entry->size > G_MAXINT || !(metabuf = g_try_malloc(entry->size))) {
64 sr_err("Metadata buffer allocation failed.");
67 zf = zip_fopen_index(archive, entry->index, 0);
69 sr_err("Failed to open metadata: %s", zip_strerror(archive));
73 metalen = zip_fread(zf, metabuf, entry->size);
75 sr_err("Failed to read metadata: %s", zip_file_strerror(zf));
82 keyfile = g_key_file_new();
84 g_key_file_load_from_data(keyfile, metabuf, metalen,
85 G_KEY_FILE_NONE, &error);
89 sr_err("Failed to parse metadata: %s", error->message);
91 g_key_file_free(keyfile);
98 SR_PRIV int sr_sessionfile_check(const char *filename)
110 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
111 sr_err("Not a regular file: %s.", filename);
115 if (!(archive = zip_open(filename, 0, NULL)))
116 /* No logging: this can be used just to check if it's
117 * a sigrok session file or not. */
120 /* check "version" */
121 if (!(zf = zip_fopen(archive, "version", 0))) {
122 sr_dbg("Not a sigrok session file: no version found.");
123 zip_discard(archive);
126 ret = zip_fread(zf, s, sizeof(s) - 1);
128 sr_err("Failed to read version file: %s",
129 zip_file_strerror(zf));
131 zip_discard(archive);
136 version = g_ascii_strtoull(s, NULL, 10);
137 if (version == 0 || version > 2) {
138 sr_dbg("Cannot handle sigrok session file version %" PRIu64 ".",
140 zip_discard(archive);
143 sr_spew("Detected sigrok session file version %" PRIu64 ".", version);
145 /* read "metadata" */
146 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
147 sr_dbg("Not a valid sigrok session file.");
148 zip_discard(archive);
151 zip_discard(archive);
157 * Load the session from the specified filename.
159 * @param ctx The context in which to load the session.
160 * @param filename The name of the session file to load.
161 * @param session The session to load the file into.
163 * @retval SR_OK Success
164 * @retval SR_ERR_MALLOC Memory allocation error
165 * @retval SR_ERR_DATA Malformed session file
166 * @retval SR_ERR This is not a session file
168 SR_API int sr_session_load(struct sr_context *ctx, const char *filename,
169 struct sr_session **session)
175 struct sr_dev_inst *sdi;
176 struct sr_channel *ch;
179 int total_channels, k;
181 char **sections, **keys, *val;
182 char channelname[SR_MAX_CHANNELNAME_LEN + 1];
184 if ((ret = sr_sessionfile_check(filename)) != SR_OK)
187 if (!(archive = zip_open(filename, 0, NULL)))
190 if (zip_stat(archive, "metadata", 0, &zs) < 0) {
191 zip_discard(archive);
194 kf = sr_sessionfile_read_metadata(archive, &zs);
195 zip_discard(archive);
199 if ((ret = sr_session_new(ctx, session)) != SR_OK) {
206 sections = g_key_file_get_groups(kf, NULL);
207 for (i = 0; sections[i] && ret == SR_OK; i++) {
208 if (!strcmp(sections[i], "global"))
209 /* nothing really interesting in here yet */
211 if (!strncmp(sections[i], "device ", 7)) {
214 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
215 for (j = 0; keys[j]; j++) {
216 if (!strcmp(keys[j], "capturefile")) {
217 val = g_key_file_get_string(kf, sections[i],
223 sdi = g_malloc0(sizeof(struct sr_dev_inst));
224 sdi->driver = &session_driver;
225 sdi->status = SR_ST_ACTIVE;
226 if (!session_driver_initialized) {
227 /* first device, init the driver */
228 session_driver_initialized = 1;
229 sdi->driver->init(sdi->driver, NULL);
232 sr_session_dev_add(*session, sdi);
233 (*session)->owned_devs = g_slist_append(
234 (*session)->owned_devs, sdi);
235 sr_config_set(sdi, NULL, SR_CONF_SESSIONFILE,
236 g_variant_new_string(filename));
237 sr_config_set(sdi, NULL, SR_CONF_CAPTUREFILE,
238 g_variant_new_string(val));
240 } else if (!strcmp(keys[j], "samplerate")) {
241 val = g_key_file_get_string(kf, sections[i],
243 if (!sdi || !val || sr_parse_sizestring(val,
244 &tmp_u64) != SR_OK) {
250 sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
251 g_variant_new_uint64(tmp_u64));
252 } else if (!strcmp(keys[j], "unitsize")) {
253 unitsize = g_key_file_get_integer(kf, sections[i],
255 if (!sdi || unitsize <= 0 || error) {
259 sr_config_set(sdi, NULL, SR_CONF_CAPTURE_UNITSIZE,
260 g_variant_new_uint64(unitsize));
261 } else if (!strcmp(keys[j], "total probes")) {
262 total_channels = g_key_file_get_integer(kf,
263 sections[i], keys[j], &error);
264 if (!sdi || total_channels < 0 || error) {
268 sr_config_set(sdi, NULL, SR_CONF_NUM_LOGIC_CHANNELS,
269 g_variant_new_int32(total_channels));
270 for (k = 0; k < total_channels; k++) {
271 g_snprintf(channelname, sizeof channelname,
273 sr_channel_new(sdi, k, SR_CHANNEL_LOGIC,
276 } else if (!strncmp(keys[j], "probe", 5)) {
277 tmp_u64 = g_ascii_strtoull(keys[j]+5, NULL, 10);
278 if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
282 ch = g_slist_nth_data(sdi->channels, tmp_u64 - 1);
287 val = g_key_file_get_string(kf, sections[i],
293 /* sr_session_save() */
294 sr_dev_channel_name_set(ch, val);
296 sr_dev_channel_enable(ch, TRUE);
302 g_strfreev(sections);
306 sr_err("Failed to parse metadata: %s", error->message);