blob: 637e647edc7e5dcc80ac10d5714377ff2430dcae [file] [log] [blame]
Bert Vermeulen7d658872011-01-31 22:34:14 +01001/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
Uwe Hermann22b02382011-02-02 10:25:52 +010020#include "config.h"
Bert Vermeulen7d658872011-01-31 22:34:14 +010021#include <string.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <zip.h>
25#include <glib.h>
Uwe Hermann3bbd9842011-02-06 02:14:57 +010026#include <glib/gstdio.h>
Uwe Hermannb7f09cf2011-12-28 23:07:08 +010027#include "sigrok.h"
28#include "sigrok-internal.h"
Bert Vermeulen7d658872011-01-31 22:34:14 +010029
Uwe Hermann2872d212011-02-08 17:50:29 +010030extern struct sr_session *session;
Bert Vermeulen7d658872011-01-31 22:34:14 +010031extern struct sr_device_plugin session_driver;
32
Uwe Hermann8a2efef2011-02-08 18:00:49 +010033int sr_session_load(const char *filename)
Bert Vermeulen7d658872011-01-31 22:34:14 +010034{
35 GKeyFile *kf;
36 GPtrArray *capturefiles;
37 struct zip *archive;
38 struct zip_file *zf;
39 struct zip_stat zs;
Uwe Hermann2872d212011-02-08 17:50:29 +010040 struct sr_session *session;
Bert Vermeulen7d658872011-01-31 22:34:14 +010041 struct sr_device *device;
Uwe Hermann1afe8982011-02-08 17:47:38 +010042 struct sr_probe *probe;
Bert Vermeulen7d658872011-01-31 22:34:14 +010043 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
47 if (!(archive = zip_open(filename, 0, &err))) {
Uwe Hermannb08024a2011-04-14 09:46:53 +020048 sr_dbg("Failed to open session file: zip error %d", err);
Bert Vermeulen7d658872011-01-31 22:34:14 +010049 return SR_ERR;
50 }
51
52 /* check "version" */
53 if (!(zf = zip_fopen(archive, "version", 0))) {
Uwe Hermannb08024a2011-04-14 09:46:53 +020054 sr_dbg("Not a sigrok session file.");
Bert Vermeulen7d658872011-01-31 22:34:14 +010055 return SR_ERR;
56 }
57 ret = zip_fread(zf, &c, 1);
58 if (ret != 1 || c != '1') {
Uwe Hermannb08024a2011-04-14 09:46:53 +020059 sr_dbg("Not a valid sigrok session file.");
Bert Vermeulen7d658872011-01-31 22:34:14 +010060 return SR_ERR;
61 }
62 zip_fclose(zf);
63
64 /* read "metadata" */
65 if (zip_stat(archive, "metadata", 0, &zs) == -1) {
Uwe Hermannb08024a2011-04-14 09:46:53 +020066 sr_dbg("Not a valid sigrok session file.");
Bert Vermeulen7d658872011-01-31 22:34:14 +010067 return SR_ERR;
68 }
Uwe Hermannb53738b2011-04-16 14:17:51 +020069
70 if (!(metafile = g_try_malloc(zs.size))) {
71 sr_err("session file: %s: metafile malloc failed", __func__);
72 return SR_ERR_MALLOC;
73 }
74
Bert Vermeulen7d658872011-01-31 22:34:14 +010075 zf = zip_fopen_index(archive, zs.index, 0);
76 zip_fread(zf, metafile, zs.size);
77 zip_fclose(zf);
78
79 kf = g_key_file_new();
80 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
Uwe Hermannb08024a2011-04-14 09:46:53 +020081 sr_dbg("Failed to parse metadata.");
Bert Vermeulen7d658872011-01-31 22:34:14 +010082 return SR_ERR;
83 }
84
Uwe Hermann8a2efef2011-02-08 18:00:49 +010085 session = sr_session_new();
Bert Vermeulen7d658872011-01-31 22:34:14 +010086
87 devcnt = 0;
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 */
93 continue;
94 if (!strncmp(sections[i], "device ", 7)) {
95 /* device section */
96 device = NULL;
97 enabled_probes = 0;
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")) {
Uwe Hermann2bf4aca2011-02-08 18:19:38 +0100102 device = sr_device_new(&session_driver, devcnt, 0);
Bert Vermeulen7d658872011-01-31 22:34:14 +0100103 if (devcnt == 0)
104 /* first device, init the plugin */
105 device->plugin->init((char *)filename);
Uwe Hermann8a2efef2011-02-08 18:00:49 +0100106 sr_session_device_add(device);
Bert Vermeulen7d658872011-01-31 22:34:14 +0100107 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTUREFILE, val);
108 g_ptr_array_add(capturefiles, val);
109 } else if (!strcmp(keys[j], "samplerate")) {
Bert Vermeulenf64c1412011-11-27 19:31:25 +0100110 sr_parse_sizestring(val, &tmp_u64);
Bert Vermeulen7d658872011-01-31 22:34:14 +0100111 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++)
Uwe Hermann2bf4aca2011-02-08 18:19:38 +0100119 sr_device_probe_add(device, NULL);
Bert Vermeulen7d658872011-01-31 22:34:14 +0100120 } else if (!strncmp(keys[j], "probe", 5)) {
121 if (!device)
122 continue;
123 enabled_probes++;
124 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
Uwe Hermann2bf4aca2011-02-08 18:19:38 +0100125 sr_device_probe_name(device, tmp_u64, val);
Bert Vermeulen7d658872011-01-31 22:34:14 +0100126 } else if (!strncmp(keys[j], "trigger", 7)) {
127 probenum = strtoul(keys[j]+7, NULL, 10);
Uwe Hermann2bf4aca2011-02-08 18:19:38 +0100128 sr_device_trigger_set(device, probenum, val);
Bert Vermeulen7d658872011-01-31 22:34:14 +0100129 }
130 }
131 g_strfreev(keys);
132 for (p = enabled_probes; p < total_probes; p++) {
133 probe = g_slist_nth_data(device->probes, p);
134 probe->enabled = FALSE;
135 }
136 }
137 }
138 g_strfreev(sections);
139 g_key_file_free(kf);
140
141 return SR_OK;
142}
143
Uwe Hermann8225e922011-02-20 13:53:13 +0100144int sr_session_save(const char *filename)
Bert Vermeulen7d658872011-01-31 22:34:14 +0100145{
146 GSList *l, *p, *d;
147 FILE *meta;
148 struct sr_device *device;
Uwe Hermann1afe8982011-02-08 17:47:38 +0100149 struct sr_probe *probe;
Uwe Hermannc4911122011-02-08 21:22:10 +0100150 struct sr_datastore *ds;
Bert Vermeulen7d658872011-01-31 22:34:14 +0100151 struct zip *zipfile;
152 struct zip_source *versrc, *metasrc, *logicsrc;
153 int bufcnt, devcnt, tmpfile, ret, error, probecnt;
154 uint64_t samplerate;
Bert Vermeulen4a1b18f2011-02-01 01:41:33 +0100155 char version[1], rawname[16], metafile[32], *buf, *s;
Bert Vermeulen7d658872011-01-31 22:34:14 +0100156
157 /* Quietly delete it first, libzip wants replace ops otherwise. */
Bert Vermeulen4a1b18f2011-02-01 01:41:33 +0100158 unlink(filename);
159 if (!(zipfile = zip_open(filename, ZIP_CREATE, &error)))
Bert Vermeulen7d658872011-01-31 22:34:14 +0100160 return SR_ERR;
Bert Vermeulen7d658872011-01-31 22:34:14 +0100161
162 /* "version" */
163 version[0] = '1';
164 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
165 return SR_ERR;
166 if (zip_add(zipfile, "version", versrc) == -1) {
Uwe Hermannb08024a2011-04-14 09:46:53 +0200167 sr_info("error saving version into zipfile: %s",
168 zip_strerror(zipfile));
Bert Vermeulen7d658872011-01-31 22:34:14 +0100169 return SR_ERR;
170 }
171
172 /* init "metadata" */
173 strcpy(metafile, "sigrok-meta-XXXXXX");
174 if ((tmpfile = g_mkstemp(metafile)) == -1)
175 return SR_ERR;
176 close(tmpfile);
Uwe Hermann868d8ce2011-02-05 20:03:17 +0100177 meta = g_fopen(metafile, "wb");
Bert Vermeulen7d658872011-01-31 22:34:14 +0100178 fprintf(meta, "[global]\n");
179 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
180 /* TODO: save protocol decoders used */
181
182 /* all datastores in all devices */
183 devcnt = 1;
184 for (l = session->devices; l; l = l->next) {
185 device = l->data;
186 /* metadata */
187 fprintf(meta, "[device %d]\n", devcnt);
188 if (device->plugin)
189 fprintf(meta, "driver = %s\n", device->plugin->name);
190
191 ds = device->datastore;
192 if (ds) {
193 /* metadata */
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));
Uwe Hermann2bf4aca2011-02-08 18:19:38 +0100197 if (sr_device_has_hwcap(device, SR_HWCAP_SAMPLERATE)) {
Bert Vermeulen7d658872011-01-31 22:34:14 +0100198 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);
202 free(s);
203 }
204 probecnt = 1;
205 for (p = device->probes; p; p = p->next) {
206 probe = p->data;
207 if (probe->enabled) {
208 if (probe->name)
209 fprintf(meta, "probe%d = %s\n", probecnt, probe->name);
210 if (probe->trigger)
211 fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger);
212 probecnt++;
213 }
214 }
215
216 /* dump datastore into logic-n */
217 buf = malloc(ds->num_units * ds->ds_unitsize +
218 DATASTORE_CHUNKSIZE);
219 bufcnt = 0;
220 for (d = ds->chunklist; d; d = d->next) {
221 memcpy(buf + bufcnt, d->data,
222 DATASTORE_CHUNKSIZE);
223 bufcnt += DATASTORE_CHUNKSIZE;
224 }
225 if (!(logicsrc = zip_source_buffer(zipfile, buf,
226 ds->num_units * ds->ds_unitsize, TRUE)))
227 return SR_ERR;
228 snprintf(rawname, 15, "logic-%d", devcnt);
229 if (zip_add(zipfile, rawname, logicsrc) == -1)
230 return SR_ERR;
231 }
232 devcnt++;
233 }
234 fclose(meta);
235
236 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
237 return SR_ERR;
238 if (zip_add(zipfile, "metadata", metasrc) == -1)
239 return SR_ERR;
240
241 if ((ret = zip_close(zipfile)) == -1) {
Uwe Hermannb08024a2011-04-14 09:46:53 +0200242 sr_info("error saving zipfile: %s", zip_strerror(zipfile));
Bert Vermeulen7d658872011-01-31 22:34:14 +0100243 return SR_ERR;
244 }
245
246 unlink(metafile);
247
248 return SR_OK;
249}