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;
47 if (!(archive = zip_open(filename, 0, &err))) {
48 sr_dbg("Failed to open session file: zip error %d", err);
53 if (!(zf = zip_fopen(archive, "version", 0))) {
54 sr_dbg("Not a sigrok session file.");
57 ret = zip_fread(zf, &c, 1);
58 if (ret != 1 || c != '1') {
59 sr_dbg("Not a valid sigrok session file.");
65 if (zip_stat(archive, "metadata", 0, &zs) == -1) {
66 sr_dbg("Not a valid sigrok session file.");
70 if (!(metafile = g_try_malloc(zs.size))) {
71 sr_err("session file: %s: metafile malloc failed", __func__);
75 zf = zip_fopen_index(archive, zs.index, 0);
76 zip_fread(zf, metafile, zs.size);
79 kf = g_key_file_new();
80 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
81 sr_dbg("Failed to parse metadata.");
85 session = sr_session_new();
88 capturefiles = g_ptr_array_new_with_free_func(g_free);
89 sections = g_key_file_get_groups(kf, NULL);
90 for (i = 0; sections[i]; i++) {
91 if (!strcmp(sections[i], "global"))
92 /* nothing really interesting in here yet */
94 if (!strncmp(sections[i], "device ", 7)) {
98 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
99 for (j = 0; keys[j]; j++) {
100 val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
101 if (!strcmp(keys[j], "capturefile")) {
102 device = sr_device_new(&session_driver, devcnt, 0);
104 /* first device, init the plugin */
105 device->plugin->init((char *)filename);
106 sr_session_device_add(device);
107 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTUREFILE, val);
108 g_ptr_array_add(capturefiles, val);
109 } else if (!strcmp(keys[j], "samplerate")) {
110 sr_parse_sizestring(val, &tmp_u64);
111 device->plugin->set_configuration(devcnt, SR_HWCAP_SAMPLERATE, &tmp_u64);
112 } else if (!strcmp(keys[j], "unitsize")) {
113 tmp_u64 = strtoull(val, NULL, 10);
114 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTURE_UNITSIZE, &tmp_u64);
115 } else if (!strcmp(keys[j], "total probes")) {
116 total_probes = strtoull(val, NULL, 10);
117 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTURE_NUM_PROBES, &total_probes);
118 for (p = 1; p <= total_probes; p++)
119 sr_device_probe_add(device, NULL);
120 } else if (!strncmp(keys[j], "probe", 5)) {
124 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
125 sr_device_probe_name(device, tmp_u64, val);
126 } else if (!strncmp(keys[j], "trigger", 7)) {
127 probenum = strtoul(keys[j]+7, NULL, 10);
128 sr_device_trigger_set(device, probenum, val);
132 for (p = enabled_probes; p < total_probes; p++) {
133 probe = g_slist_nth_data(device->probes, p);
134 probe->enabled = FALSE;
138 g_strfreev(sections);
144 int sr_session_save(const char *filename)
148 struct sr_device *device;
149 struct sr_probe *probe;
150 struct sr_datastore *ds;
152 struct zip_source *versrc, *metasrc, *logicsrc;
153 int bufcnt, devcnt, tmpfile, ret, error, probecnt;
155 char version[1], rawname[16], metafile[32], *buf, *s;
157 /* Quietly delete it first, libzip wants replace ops otherwise. */
159 if (!(zipfile = zip_open(filename, ZIP_CREATE, &error)))
164 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
166 if (zip_add(zipfile, "version", versrc) == -1) {
167 sr_info("error saving version into zipfile: %s",
168 zip_strerror(zipfile));
172 /* init "metadata" */
173 strcpy(metafile, "sigrok-meta-XXXXXX");
174 if ((tmpfile = g_mkstemp(metafile)) == -1)
177 meta = g_fopen(metafile, "wb");
178 fprintf(meta, "[global]\n");
179 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
180 /* TODO: save protocol decoders used */
182 /* all datastores in all devices */
184 for (l = session->devices; l; l = l->next) {
187 fprintf(meta, "[device %d]\n", devcnt);
189 fprintf(meta, "driver = %s\n", device->plugin->name);
191 ds = device->datastore;
194 fprintf(meta, "capturefile = logic-%d\n", devcnt);
195 fprintf(meta, "unitsize = %d\n", ds->ds_unitsize);
196 fprintf(meta, "total probes = %d\n", g_slist_length(device->probes));
197 if (sr_device_has_hwcap(device, SR_HWCAP_SAMPLERATE)) {
198 samplerate = *((uint64_t *) device->plugin->get_device_info(
199 device->plugin_index, SR_DI_CUR_SAMPLERATE));
200 s = sr_samplerate_string(samplerate);
201 fprintf(meta, "samplerate = %s\n", s);
205 for (p = device->probes; p; p = p->next) {
207 if (probe->enabled) {
209 fprintf(meta, "probe%d = %s\n", probecnt, probe->name);
211 fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger);
216 /* dump datastore into logic-n */
217 buf = malloc(ds->num_units * ds->ds_unitsize +
218 DATASTORE_CHUNKSIZE);
220 for (d = ds->chunklist; d; d = d->next) {
221 memcpy(buf + bufcnt, d->data,
222 DATASTORE_CHUNKSIZE);
223 bufcnt += DATASTORE_CHUNKSIZE;
225 if (!(logicsrc = zip_source_buffer(zipfile, buf,
226 ds->num_units * ds->ds_unitsize, TRUE)))
228 snprintf(rawname, 15, "logic-%d", devcnt);
229 if (zip_add(zipfile, rawname, logicsrc) == -1)
236 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
238 if (zip_add(zipfile, "metadata", metasrc) == -1)
241 if ((ret = zip_close(zipfile)) == -1) {
242 sr_info("error saving zipfile: %s", zip_strerror(zipfile));