2 * This file is part of the sigrok project.
4 * Copyright (C) 2011 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>
28 #include "sigrok-internal.h"
30 extern struct sr_session *session;
31 extern struct sr_device_plugin session_driver;
33 int sr_session_load(const char *filename)
36 GPtrArray *capturefiles;
40 struct sr_session *session;
41 struct sr_device *device;
42 struct sr_probe *probe;
43 int ret, err, probenum, devcnt, i, j;
44 uint64_t tmp_u64, total_probes, enabled_probes, p;
45 char **sections, **keys, *metafile, *val, c;
46 char probename[SR_MAX_PROBENAME_LEN + 1];
48 if (!(archive = zip_open(filename, 0, &err))) {
49 sr_dbg("Failed to open session file: zip error %d", err);
54 if (!(zf = zip_fopen(archive, "version", 0))) {
55 sr_dbg("Not a sigrok session file.");
58 ret = zip_fread(zf, &c, 1);
59 if (ret != 1 || c != '1') {
60 sr_dbg("Not a valid sigrok session file.");
66 if (zip_stat(archive, "metadata", 0, &zs) == -1) {
67 sr_dbg("Not a valid sigrok session file.");
71 if (!(metafile = g_try_malloc(zs.size))) {
72 sr_err("session file: %s: metafile malloc failed", __func__);
76 zf = zip_fopen_index(archive, zs.index, 0);
77 zip_fread(zf, metafile, zs.size);
80 kf = g_key_file_new();
81 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
82 sr_dbg("Failed to parse metadata.");
86 session = sr_session_new();
89 capturefiles = g_ptr_array_new_with_free_func(g_free);
90 sections = g_key_file_get_groups(kf, NULL);
91 for (i = 0; sections[i]; i++) {
92 if (!strcmp(sections[i], "global"))
93 /* nothing really interesting in here yet */
95 if (!strncmp(sections[i], "device ", 7)) {
99 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
100 for (j = 0; keys[j]; j++) {
101 val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
102 if (!strcmp(keys[j], "capturefile")) {
103 device = sr_device_new(&session_driver, devcnt);
105 /* first device, init the plugin */
106 device->plugin->init((char *)filename);
107 sr_session_device_add(device);
108 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTUREFILE, val);
109 g_ptr_array_add(capturefiles, val);
110 } else if (!strcmp(keys[j], "samplerate")) {
111 sr_parse_sizestring(val, &tmp_u64);
112 device->plugin->set_configuration(devcnt, SR_HWCAP_SAMPLERATE, &tmp_u64);
113 } else if (!strcmp(keys[j], "unitsize")) {
114 tmp_u64 = strtoull(val, NULL, 10);
115 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTURE_UNITSIZE, &tmp_u64);
116 } else if (!strcmp(keys[j], "total probes")) {
117 total_probes = strtoull(val, NULL, 10);
118 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTURE_NUM_PROBES, &total_probes);
119 for (p = 0; p < total_probes; p++) {
120 snprintf(probename, SR_MAX_PROBENAME_LEN, "%" PRIu64, p);
121 sr_device_probe_add(device, probename);
123 } else if (!strncmp(keys[j], "probe", 5)) {
127 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
128 sr_device_probe_name(device, tmp_u64, val);
129 } else if (!strncmp(keys[j], "trigger", 7)) {
130 probenum = strtoul(keys[j]+7, NULL, 10);
131 sr_device_trigger_set(device, probenum, val);
135 for (p = enabled_probes; p < total_probes; p++) {
136 probe = g_slist_nth_data(device->probes, p);
137 probe->enabled = FALSE;
141 g_strfreev(sections);
147 int sr_session_save(const char *filename)
151 struct sr_device *device;
152 struct sr_probe *probe;
153 struct sr_datastore *ds;
155 struct zip_source *versrc, *metasrc, *logicsrc;
156 int bufcnt, devcnt, tmpfile, ret, error, probecnt;
158 char version[1], rawname[16], metafile[32], *buf, *s;
160 /* Quietly delete it first, libzip wants replace ops otherwise. */
162 if (!(zipfile = zip_open(filename, ZIP_CREATE, &error)))
167 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
169 if (zip_add(zipfile, "version", versrc) == -1) {
170 sr_info("error saving version into zipfile: %s",
171 zip_strerror(zipfile));
175 /* init "metadata" */
176 strcpy(metafile, "sigrok-meta-XXXXXX");
177 if ((tmpfile = g_mkstemp(metafile)) == -1)
180 meta = g_fopen(metafile, "wb");
181 fprintf(meta, "[global]\n");
182 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
183 /* TODO: save protocol decoders used */
185 /* all datastores in all devices */
187 for (l = session->devices; l; l = l->next) {
190 fprintf(meta, "[device %d]\n", devcnt);
192 fprintf(meta, "driver = %s\n", device->plugin->name);
194 ds = device->datastore;
197 fprintf(meta, "capturefile = logic-%d\n", devcnt);
198 fprintf(meta, "unitsize = %d\n", ds->ds_unitsize);
199 fprintf(meta, "total probes = %d\n", g_slist_length(device->probes));
200 if (sr_device_has_hwcap(device, SR_HWCAP_SAMPLERATE)) {
201 samplerate = *((uint64_t *) device->plugin->get_device_info(
202 device->plugin_index, SR_DI_CUR_SAMPLERATE));
203 s = sr_samplerate_string(samplerate);
204 fprintf(meta, "samplerate = %s\n", s);
208 for (p = device->probes; p; p = p->next) {
210 if (probe->enabled) {
212 fprintf(meta, "probe%d = %s\n", probecnt, probe->name);
214 fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger);
219 /* dump datastore into logic-n */
220 buf = malloc(ds->num_units * ds->ds_unitsize +
221 DATASTORE_CHUNKSIZE);
223 for (d = ds->chunklist; d; d = d->next) {
224 memcpy(buf + bufcnt, d->data,
225 DATASTORE_CHUNKSIZE);
226 bufcnt += DATASTORE_CHUNKSIZE;
228 if (!(logicsrc = zip_source_buffer(zipfile, buf,
229 ds->num_units * ds->ds_unitsize, TRUE)))
231 snprintf(rawname, 15, "logic-%d", devcnt);
232 if (zip_add(zipfile, rawname, logicsrc) == -1)
239 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
241 if (zip_add(zipfile, "metadata", metasrc) == -1)
244 if ((ret = zip_close(zipfile)) == -1) {
245 sr_info("error saving zipfile: %s", zip_strerror(zipfile));