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/>.
28 extern struct session *session;
29 extern struct sr_device_plugin session_driver;
32 int session_load(const char *filename)
35 GPtrArray *capturefiles;
39 struct session *session;
40 struct sr_device *device;
42 int ret, err, probenum, devcnt, i, j;
43 uint64_t tmp_u64, total_probes, enabled_probes, p;
44 char **sections, **keys, *metafile, *val, c;
46 if (!(archive = zip_open(filename, 0, &err))) {
47 g_debug("Failed to open session file: zip error %d", err);
52 if (!(zf = zip_fopen(archive, "version", 0))) {
53 g_debug("Not a sigrok session file.");
56 ret = zip_fread(zf, &c, 1);
57 if (ret != 1 || c != '1') {
58 g_debug("Not a valid sigrok session file.");
64 if (zip_stat(archive, "metadata", 0, &zs) == -1) {
65 g_debug("Not a valid sigrok session file.");
68 metafile = g_malloc(zs.size);
69 zf = zip_fopen_index(archive, zs.index, 0);
70 zip_fread(zf, metafile, zs.size);
73 kf = g_key_file_new();
74 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
75 g_debug("Failed to parse metadata.");
79 session = session_new();
82 capturefiles = g_ptr_array_new_with_free_func(g_free);
83 sections = g_key_file_get_groups(kf, NULL);
84 for (i = 0; sections[i]; i++) {
85 if (!strcmp(sections[i], "global"))
86 /* nothing really interesting in here yet */
88 if (!strncmp(sections[i], "device ", 7)) {
92 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
93 for (j = 0; keys[j]; j++) {
94 val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
95 if (!strcmp(keys[j], "capturefile")) {
96 device = device_new(&session_driver, devcnt, 0);
98 /* first device, init the plugin */
99 device->plugin->init((char *)filename);
100 session_device_add(device);
101 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTUREFILE, val);
102 g_ptr_array_add(capturefiles, val);
103 } else if (!strcmp(keys[j], "samplerate")) {
104 tmp_u64 = sr_parse_sizestring(val);
105 device->plugin->set_configuration(devcnt, SR_HWCAP_SAMPLERATE, &tmp_u64);
106 } else if (!strcmp(keys[j], "unitsize")) {
107 tmp_u64 = strtoull(val, NULL, 10);
108 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTURE_UNITSIZE, &tmp_u64);
109 } else if (!strcmp(keys[j], "total probes")) {
110 total_probes = strtoull(val, NULL, 10);
111 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTURE_NUM_PROBES, &total_probes);
112 for (p = 1; p <= total_probes; p++)
113 device_probe_add(device, NULL);
114 } else if (!strncmp(keys[j], "probe", 5)) {
118 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
119 device_probe_name(device, tmp_u64, val);
120 } else if (!strncmp(keys[j], "trigger", 7)) {
121 probenum = strtoul(keys[j]+7, NULL, 10);
122 device_trigger_set(device, probenum, val);
126 for (p = enabled_probes; p < total_probes; p++) {
127 probe = g_slist_nth_data(device->probes, p);
128 probe->enabled = FALSE;
132 g_strfreev(sections);
138 int session_save(char *filename)
142 struct sr_device *device;
144 struct datastore *ds;
146 struct zip_source *versrc, *metasrc, *logicsrc;
147 int bufcnt, devcnt, tmpfile, ret, error, probecnt;
149 char version[1], rawname[16], metafile[32], *buf, *s;
151 /* Quietly delete it first, libzip wants replace ops otherwise. */
153 if (!(zipfile = zip_open(filename, ZIP_CREATE, &error)))
158 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
160 if (zip_add(zipfile, "version", versrc) == -1) {
161 g_message("error saving version into zipfile: %s",
162 zip_strerror(zipfile));
166 /* init "metadata" */
167 strcpy(metafile, "sigrok-meta-XXXXXX");
168 if ((tmpfile = g_mkstemp(metafile)) == -1)
171 meta = g_fopen(metafile, "wb");
172 fprintf(meta, "[global]\n");
173 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
174 /* TODO: save protocol decoders used */
176 /* all datastores in all devices */
178 for (l = session->devices; l; l = l->next) {
181 fprintf(meta, "[device %d]\n", devcnt);
183 fprintf(meta, "driver = %s\n", device->plugin->name);
185 ds = device->datastore;
188 fprintf(meta, "capturefile = logic-%d\n", devcnt);
189 fprintf(meta, "unitsize = %d\n", ds->ds_unitsize);
190 fprintf(meta, "total probes = %d\n", g_slist_length(device->probes));
191 if (device_has_hwcap(device, SR_HWCAP_SAMPLERATE)) {
192 samplerate = *((uint64_t *) device->plugin->get_device_info(
193 device->plugin_index, SR_DI_CUR_SAMPLERATE));
194 s = sr_samplerate_string(samplerate);
195 fprintf(meta, "samplerate = %s\n", s);
199 for (p = device->probes; p; p = p->next) {
201 if (probe->enabled) {
203 fprintf(meta, "probe%d = %s\n", probecnt, probe->name);
205 fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger);
210 /* dump datastore into logic-n */
211 buf = malloc(ds->num_units * ds->ds_unitsize +
212 DATASTORE_CHUNKSIZE);
214 for (d = ds->chunklist; d; d = d->next) {
215 memcpy(buf + bufcnt, d->data,
216 DATASTORE_CHUNKSIZE);
217 bufcnt += DATASTORE_CHUNKSIZE;
219 if (!(logicsrc = zip_source_buffer(zipfile, buf,
220 ds->num_units * ds->ds_unitsize, TRUE)))
222 snprintf(rawname, 15, "logic-%d", devcnt);
223 if (zip_add(zipfile, rawname, logicsrc) == -1)
230 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
232 if (zip_add(zipfile, "metadata", metasrc) == -1)
235 if ((ret = zip_close(zipfile)) == -1) {
236 g_message("error saving zipfile: %s", zip_strerror(zipfile));