2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 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/>.
25 #include <glib/gstdio.h>
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
30 extern struct sr_session *session;
31 extern SR_PRIV struct sr_dev_driver session_driver;
34 * Load the session from the specified filename.
36 * @param filename The name of the session file to load. Must not be NULL.
38 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments,
39 * SR_ERR_MALLOC upon memory allocation errors, or SR_ERR upon
42 SR_API int sr_session_load(const char *filename)
45 GPtrArray *capturefiles;
49 struct sr_dev_inst *sdi;
50 struct sr_probe *probe;
51 int ret, probenum, devcnt, i, j;
52 uint64_t tmp_u64, total_probes, enabled_probes, p;
53 char **sections, **keys, *metafile, *val, c;
54 char probename[SR_MAX_PROBENAME_LEN + 1];
57 sr_err("session file: %s: filename was NULL", __func__);
61 if (!(archive = zip_open(filename, 0, &ret))) {
62 sr_dbg("session file: Failed to open session file: zip "
68 if (!(zf = zip_fopen(archive, "version", 0))) {
69 sr_dbg("session file: Not a sigrok session file.");
72 ret = zip_fread(zf, &c, 1);
73 if (ret != 1 || c != '1') {
74 sr_dbg("session file: Not a valid sigrok session file.");
80 if (zip_stat(archive, "metadata", 0, &zs) == -1) {
81 sr_dbg("session file: Not a valid sigrok session file.");
85 if (!(metafile = g_try_malloc(zs.size))) {
86 sr_err("session file: %s: metafile malloc failed", __func__);
90 zf = zip_fopen_index(archive, zs.index, 0);
91 zip_fread(zf, metafile, zs.size);
94 kf = g_key_file_new();
95 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
96 sr_dbg("session file: Failed to parse metadata.");
103 capturefiles = g_ptr_array_new_with_free_func(g_free);
104 sections = g_key_file_get_groups(kf, NULL);
105 for (i = 0; sections[i]; i++) {
106 if (!strcmp(sections[i], "global"))
107 /* nothing really interesting in here yet */
109 if (!strncmp(sections[i], "device ", 7)) {
113 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
114 for (j = 0; keys[j]; j++) {
115 val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
116 if (!strcmp(keys[j], "capturefile")) {
117 sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, NULL, NULL, NULL);
118 sdi->driver = &session_driver;
120 /* first device, init the driver */
122 sr_session_dev_add(sdi);
123 sdi->driver->dev_config_set(sdi, SR_HWCAP_SESSIONFILE, filename);
124 sdi->driver->dev_config_set(sdi, SR_HWCAP_CAPTUREFILE, val);
125 g_ptr_array_add(capturefiles, val);
126 } else if (!strcmp(keys[j], "samplerate")) {
127 sr_parse_sizestring(val, &tmp_u64);
128 sdi->driver->dev_config_set(sdi, SR_HWCAP_SAMPLERATE, &tmp_u64);
129 } else if (!strcmp(keys[j], "unitsize")) {
130 tmp_u64 = strtoull(val, NULL, 10);
131 sdi->driver->dev_config_set(sdi, SR_HWCAP_CAPTURE_UNITSIZE, &tmp_u64);
132 } else if (!strcmp(keys[j], "total probes")) {
133 total_probes = strtoull(val, NULL, 10);
134 sdi->driver->dev_config_set(sdi, SR_HWCAP_CAPTURE_NUM_PROBES, &total_probes);
135 for (p = 0; p < total_probes; p++) {
136 snprintf(probename, SR_MAX_PROBENAME_LEN, "%" PRIu64, p);
137 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
140 sdi->probes = g_slist_append(sdi->probes, probe);
142 } else if (!strncmp(keys[j], "probe", 5)) {
146 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
147 sr_dev_probe_name_set(sdi, tmp_u64, val);
148 } else if (!strncmp(keys[j], "trigger", 7)) {
149 probenum = strtoul(keys[j]+7, NULL, 10);
150 sr_dev_trigger_set(sdi, probenum, val);
154 for (p = enabled_probes; p < total_probes; p++)
155 sr_dev_probe_enable(sdi, p, FALSE);
159 g_strfreev(sections);
166 * Save the current session to the specified file.
168 * @param filename The name of the file where to save the current session.
171 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
174 int sr_session_save(const char *filename)
178 struct sr_dev_inst *sdi;
179 struct sr_probe *probe;
180 struct sr_datastore *ds;
182 struct zip_source *versrc, *metasrc, *logicsrc;
183 int bufcnt, devcnt, tmpfile, ret, probecnt;
185 char version[1], rawname[16], metafile[32], *buf, *s;
188 sr_err("session file: %s: filename was NULL", __func__);
192 /* Quietly delete it first, libzip wants replace ops otherwise. */
194 if (!(zipfile = zip_open(filename, ZIP_CREATE, &ret)))
199 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
201 if (zip_add(zipfile, "version", versrc) == -1) {
202 sr_info("session file: error saving version into zipfile: %s",
203 zip_strerror(zipfile));
207 /* init "metadata" */
208 strcpy(metafile, "sigrok-meta-XXXXXX");
209 if ((tmpfile = g_mkstemp(metafile)) == -1)
212 meta = g_fopen(metafile, "wb");
213 fprintf(meta, "[global]\n");
214 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
215 /* TODO: save protocol decoders used */
217 /* all datastores in all devices */
219 for (l = session->devs; l; l = l->next) {
222 fprintf(meta, "[device %d]\n", devcnt);
224 fprintf(meta, "driver = %s\n", dev->driver->name);
229 fprintf(meta, "capturefile = logic-%d\n", devcnt);
230 fprintf(meta, "unitsize = %d\n", ds->ds_unitsize);
231 fprintf(meta, "total probes = %d\n", g_slist_length(dev->probes));
232 if (sr_dev_has_hwcap(dev, SR_HWCAP_SAMPLERATE)) {
233 samplerate = *((uint64_t *) dev->driver->dev_info_get(
234 dev->driver_index, SR_DI_CUR_SAMPLERATE));
235 s = sr_samplerate_string(samplerate);
236 fprintf(meta, "samplerate = %s\n", s);
240 for (p = dev->probes; p; p = p->next) {
242 if (probe->enabled) {
244 fprintf(meta, "probe%d = %s\n", probecnt, probe->name);
246 fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger);
251 /* dump datastore into logic-n */
252 buf = g_try_malloc(ds->num_units * ds->ds_unitsize +
253 DATASTORE_CHUNKSIZE);
255 sr_err("session file: %s: buf malloc failed",
257 return SR_ERR_MALLOC;
261 for (d = ds->chunklist; d; d = d->next) {
262 memcpy(buf + bufcnt, d->data,
263 DATASTORE_CHUNKSIZE);
264 bufcnt += DATASTORE_CHUNKSIZE;
266 if (!(logicsrc = zip_source_buffer(zipfile, buf,
267 ds->num_units * ds->ds_unitsize, TRUE)))
269 snprintf(rawname, 15, "logic-%d", devcnt);
270 if (zip_add(zipfile, rawname, logicsrc) == -1)
277 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
279 if (zip_add(zipfile, "metadata", metasrc) == -1)
282 if ((ret = zip_close(zipfile)) == -1) {
283 sr_info("session file: error saving zipfile: %s",
284 zip_strerror(zipfile));