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/>.
24 #include <sys/types.h>
28 #include <glib/gstdio.h>
29 #include "config.h" /* Needed for PACKAGE_VERSION and others. */
30 #include "libsigrok.h"
31 #include "libsigrok-internal.h"
33 /* Message logging helpers with subsystem-specific prefix string. */
34 #define LOG_PREFIX "session-file: "
35 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
36 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
37 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
38 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
39 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
40 #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
45 * Loading and saving libsigrok session files.
49 * @addtogroup grp_session
54 extern struct sr_session *session;
55 extern SR_PRIV struct sr_dev_driver session_driver;
58 SR_PRIV int sr_sessionfile_check(const char *filename)
70 if (stat(filename, &st) == -1) {
71 sr_err("Couldn't stat %s: %s", filename, strerror(errno));
75 if (!(archive = zip_open(filename, 0, &ret)))
76 /* No logging: this can be used just to check if it's
77 * a sigrok session file or not. */
82 if (!(zf = zip_fopen(archive, "version", 0))) {
83 sr_dbg("Not a sigrok session file: no version found.");
86 if ((ret = zip_fread(zf, s, 10)) == -1)
90 version = strtoull(s, NULL, 10);
92 sr_dbg("Cannot handle sigrok session file version %d.", version);
97 if (zip_stat(archive, "metadata", 0, &zs) == -1) {
98 sr_dbg("Not a valid sigrok session file.");
106 * Load the session from the specified filename.
108 * @param filename The name of the session file to load. Must not be NULL.
110 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments,
111 * SR_ERR_MALLOC upon memory allocation errors, or SR_ERR upon
114 SR_API int sr_session_load(const char *filename)
117 GPtrArray *capturefiles;
121 struct sr_dev_inst *sdi;
122 struct sr_probe *probe;
123 int ret, probenum, devcnt, i, j;
124 uint64_t tmp_u64, total_probes, enabled_probes, p;
125 char **sections, **keys, *metafile, *val;
126 char probename[SR_MAX_PROBENAME_LEN + 1];
128 if ((ret = sr_sessionfile_check(filename)) != SR_OK)
131 if (!(archive = zip_open(filename, 0, &ret)))
134 if (zip_stat(archive, "metadata", 0, &zs) == -1)
137 if (!(metafile = g_try_malloc(zs.size))) {
138 sr_err("%s: metafile malloc failed", __func__);
139 return SR_ERR_MALLOC;
142 zf = zip_fopen_index(archive, zs.index, 0);
143 zip_fread(zf, metafile, zs.size);
146 kf = g_key_file_new();
147 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
148 sr_dbg("Failed to parse metadata.");
155 capturefiles = g_ptr_array_new_with_free_func(g_free);
156 sections = g_key_file_get_groups(kf, NULL);
157 for (i = 0; sections[i]; i++) {
158 if (!strcmp(sections[i], "global"))
159 /* nothing really interesting in here yet */
161 if (!strncmp(sections[i], "device ", 7)) {
164 enabled_probes = total_probes = 0;
165 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
166 for (j = 0; keys[j]; j++) {
167 val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
168 if (!strcmp(keys[j], "capturefile")) {
169 sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, NULL, NULL, NULL);
170 sdi->driver = &session_driver;
172 /* first device, init the driver */
173 sdi->driver->init(NULL);
175 sr_session_dev_add(sdi);
176 sdi->driver->config_set(SR_CONF_SESSIONFILE,
177 g_variant_new_string(filename), sdi, NULL);
178 sdi->driver->config_set(SR_CONF_CAPTUREFILE,
179 g_variant_new_string(val), sdi, NULL);
180 g_ptr_array_add(capturefiles, val);
181 } else if (!strcmp(keys[j], "samplerate")) {
182 sr_parse_sizestring(val, &tmp_u64);
183 sdi->driver->config_set(SR_CONF_SAMPLERATE,
184 g_variant_new_uint64(tmp_u64), sdi, NULL);
185 } else if (!strcmp(keys[j], "unitsize")) {
186 tmp_u64 = strtoull(val, NULL, 10);
187 sdi->driver->config_set(SR_CONF_CAPTURE_UNITSIZE,
188 g_variant_new_uint64(tmp_u64), sdi, NULL);
189 } else if (!strcmp(keys[j], "total probes")) {
190 total_probes = strtoull(val, NULL, 10);
191 sdi->driver->config_set(SR_CONF_CAPTURE_NUM_PROBES,
192 g_variant_new_uint64(total_probes), sdi, NULL);
193 for (p = 0; p < total_probes; p++) {
194 snprintf(probename, SR_MAX_PROBENAME_LEN, "%" PRIu64, p);
195 if (!(probe = sr_probe_new(p, SR_PROBE_LOGIC, TRUE,
198 sdi->probes = g_slist_append(sdi->probes, probe);
200 } else if (!strncmp(keys[j], "probe", 5)) {
204 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
205 /* sr_session_save() */
206 sr_dev_probe_name_set(sdi, tmp_u64 - 1, val);
207 } else if (!strncmp(keys[j], "trigger", 7)) {
208 probenum = strtoul(keys[j]+7, NULL, 10);
209 sr_dev_trigger_set(sdi, probenum, val);
213 /* Disable probes not specifically listed. */
215 for (p = enabled_probes; p < total_probes; p++)
216 sr_dev_probe_enable(sdi, p, FALSE);
220 g_strfreev(sections);
227 * Save the current session to the specified file.
229 * @param filename The name of the filename to save the current session as.
231 * @param sdi The device instance from which the data was captured.
232 * @param buf The data to be saved.
233 * @param unitsize The number of bytes per sample.
234 * @param units The number of samples.
236 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
239 SR_API int sr_session_save(const char *filename, const struct sr_dev_inst *sdi,
240 unsigned char *buf, int unitsize, int units)
245 struct sr_probe *probe;
247 struct zip_source *versrc, *metasrc, *logicsrc;
248 int tmpfile, ret, probecnt;
250 char version[1], rawname[16], metafile[32], *s;
253 sr_err("%s: filename was NULL", __func__);
257 /* Quietly delete it first, libzip wants replace ops otherwise. */
259 if (!(zipfile = zip_open(filename, ZIP_CREATE, &ret)))
264 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
266 if (zip_add(zipfile, "version", versrc) == -1) {
267 sr_info("error saving version into zipfile: %s",
268 zip_strerror(zipfile));
272 /* init "metadata" */
273 strcpy(metafile, "sigrok-meta-XXXXXX");
274 if ((tmpfile = g_mkstemp(metafile)) == -1)
277 meta = g_fopen(metafile, "wb");
278 fprintf(meta, "[global]\n");
279 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
282 fprintf(meta, "[device 1]\n");
284 fprintf(meta, "driver = %s\n", sdi->driver->name);
287 fprintf(meta, "capturefile = logic-1\n");
288 fprintf(meta, "unitsize = %d\n", unitsize);
289 fprintf(meta, "total probes = %d\n", g_slist_length(sdi->probes));
290 if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
291 if (sr_config_get(sdi->driver, sdi, NULL,
292 SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
293 samplerate = g_variant_get_uint64(gvar);
294 s = sr_samplerate_string(samplerate);
295 fprintf(meta, "samplerate = %s\n", s);
297 g_variant_unref(gvar);
301 for (l = sdi->probes; l; l = l->next) {
303 if (probe->enabled) {
305 fprintf(meta, "probe%d = %s\n", probecnt, probe->name);
307 fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger);
312 if (!(logicsrc = zip_source_buffer(zipfile, buf,
313 units * unitsize, FALSE)))
315 snprintf(rawname, 15, "logic-1");
316 if (zip_add(zipfile, rawname, logicsrc) == -1)
320 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
322 if (zip_add(zipfile, "metadata", metasrc) == -1)
325 if ((ret = zip_close(zipfile)) == -1) {
326 sr_info("error saving zipfile: %s", zip_strerror(zipfile));
336 * Append data to an existing session file.
338 * @param filename The name of the filename to append to. Must not be NULL.
339 * @param buf The data to be appended.
340 * @param unitsize The number of bytes per sample.
341 * @param units The number of samples.
343 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
346 SR_API int sr_session_append(const char *filename, unsigned char *buf,
347 int unitsize, int units)
350 struct zip_source *logicsrc;
351 zip_int64_t num_files;
352 int chunk_num, next_chunk_num, ret, i;
353 const char *entry_name;
356 if ((ret = sr_sessionfile_check(filename)) != SR_OK)
359 if (!(archive = zip_open(filename, 0, &ret)))
363 num_files = zip_get_num_entries(archive, 0);
364 for (i = 0; i < num_files; i++) {
365 entry_name = zip_get_name(archive, i, 0);
366 if (strncmp(entry_name, "logic-1", 7))
368 if (strlen(entry_name) == 7) {
369 /* This file has no extra chunks, just a single "logic-1".
370 * Rename it to "logic-1-1" * and continue with chunk 2. */
371 if (zip_rename(archive, i, "logic-1-1") == -1) {
372 sr_err("Failed to rename 'logic-1' to 'logic-1-1'.");
377 } else if (strlen(entry_name) > 8 && entry_name[7] == '-') {
378 chunk_num = strtoull(entry_name + 8, NULL, 10);
379 if (chunk_num >= next_chunk_num)
380 next_chunk_num = chunk_num + 1;
383 snprintf(chunkname, 15, "logic-1-%d", next_chunk_num);
384 if (!(logicsrc = zip_source_buffer(archive, buf, units * unitsize, FALSE)))
386 if (zip_add(archive, chunkname, logicsrc) == -1)
388 if ((ret = zip_close(archive)) == -1) {
389 sr_info("error saving session file: %s", zip_strerror(archive));