]> sigrok.org Git - libsigrok.git/blame - session_file.c
probe names: Fix cosmetics, add docs, fix off-by-one.
[libsigrok.git] / session_file.c
CommitLineData
7d658874
BV
1/*
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
22b02383 20#include "config.h"
7d658874
BV
21#include <string.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <zip.h>
25#include <glib.h>
3bbd9849 26#include <glib/gstdio.h>
b7f09cf8
UH
27#include "sigrok.h"
28#include "sigrok-internal.h"
7d658874 29
2872d21e 30extern struct sr_session *session;
7d658874
BV
31extern struct sr_device_plugin session_driver;
32
8a2efef2 33int sr_session_load(const char *filename)
7d658874
BV
34{
35 GKeyFile *kf;
36 GPtrArray *capturefiles;
37 struct zip *archive;
38 struct zip_file *zf;
39 struct zip_stat zs;
2872d21e 40 struct sr_session *session;
7d658874 41 struct sr_device *device;
1afe8989 42 struct sr_probe *probe;
7d658874
BV
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;
c37d2b1b 46 char probename[SR_MAX_PROBENAME_LEN + 1];
7d658874
BV
47
48 if (!(archive = zip_open(filename, 0, &err))) {
b08024a8 49 sr_dbg("Failed to open session file: zip error %d", err);
7d658874
BV
50 return SR_ERR;
51 }
52
53 /* check "version" */
54 if (!(zf = zip_fopen(archive, "version", 0))) {
b08024a8 55 sr_dbg("Not a sigrok session file.");
7d658874
BV
56 return SR_ERR;
57 }
58 ret = zip_fread(zf, &c, 1);
59 if (ret != 1 || c != '1') {
b08024a8 60 sr_dbg("Not a valid sigrok session file.");
7d658874
BV
61 return SR_ERR;
62 }
63 zip_fclose(zf);
64
65 /* read "metadata" */
66 if (zip_stat(archive, "metadata", 0, &zs) == -1) {
b08024a8 67 sr_dbg("Not a valid sigrok session file.");
7d658874
BV
68 return SR_ERR;
69 }
b53738ba
UH
70
71 if (!(metafile = g_try_malloc(zs.size))) {
72 sr_err("session file: %s: metafile malloc failed", __func__);
73 return SR_ERR_MALLOC;
74 }
75
7d658874
BV
76 zf = zip_fopen_index(archive, zs.index, 0);
77 zip_fread(zf, metafile, zs.size);
78 zip_fclose(zf);
79
80 kf = g_key_file_new();
81 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
b08024a8 82 sr_dbg("Failed to parse metadata.");
7d658874
BV
83 return SR_ERR;
84 }
85
8a2efef2 86 session = sr_session_new();
7d658874
BV
87
88 devcnt = 0;
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 */
94 continue;
95 if (!strncmp(sections[i], "device ", 7)) {
96 /* device section */
97 device = NULL;
98 enabled_probes = 0;
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")) {
464d12c7 103 device = sr_device_new(&session_driver, devcnt);
7d658874
BV
104 if (devcnt == 0)
105 /* first device, init the plugin */
106 device->plugin->init((char *)filename);
8a2efef2 107 sr_session_device_add(device);
7d658874
BV
108 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTUREFILE, val);
109 g_ptr_array_add(capturefiles, val);
110 } else if (!strcmp(keys[j], "samplerate")) {
f64c1414 111 sr_parse_sizestring(val, &tmp_u64);
7d658874
BV
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);
464d12c7
KS
119 for (p = 0; p < total_probes; p++) {
120 snprintf(probename, SR_MAX_PROBENAME_LEN, "%" PRIu64, p);
121 sr_device_probe_add(device, probename);
122 }
7d658874
BV
123 } else if (!strncmp(keys[j], "probe", 5)) {
124 if (!device)
125 continue;
126 enabled_probes++;
127 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
2bf4aca6 128 sr_device_probe_name(device, tmp_u64, val);
7d658874
BV
129 } else if (!strncmp(keys[j], "trigger", 7)) {
130 probenum = strtoul(keys[j]+7, NULL, 10);
2bf4aca6 131 sr_device_trigger_set(device, probenum, val);
7d658874
BV
132 }
133 }
134 g_strfreev(keys);
135 for (p = enabled_probes; p < total_probes; p++) {
136 probe = g_slist_nth_data(device->probes, p);
137 probe->enabled = FALSE;
138 }
139 }
140 }
141 g_strfreev(sections);
142 g_key_file_free(kf);
143
144 return SR_OK;
145}
146
8225e921 147int sr_session_save(const char *filename)
7d658874
BV
148{
149 GSList *l, *p, *d;
150 FILE *meta;
151 struct sr_device *device;
1afe8989 152 struct sr_probe *probe;
c4911129 153 struct sr_datastore *ds;
7d658874
BV
154 struct zip *zipfile;
155 struct zip_source *versrc, *metasrc, *logicsrc;
156 int bufcnt, devcnt, tmpfile, ret, error, probecnt;
157 uint64_t samplerate;
4a1b18f8 158 char version[1], rawname[16], metafile[32], *buf, *s;
7d658874
BV
159
160 /* Quietly delete it first, libzip wants replace ops otherwise. */
4a1b18f8
BV
161 unlink(filename);
162 if (!(zipfile = zip_open(filename, ZIP_CREATE, &error)))
7d658874 163 return SR_ERR;
7d658874
BV
164
165 /* "version" */
166 version[0] = '1';
167 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
168 return SR_ERR;
169 if (zip_add(zipfile, "version", versrc) == -1) {
b08024a8
UH
170 sr_info("error saving version into zipfile: %s",
171 zip_strerror(zipfile));
7d658874
BV
172 return SR_ERR;
173 }
174
175 /* init "metadata" */
176 strcpy(metafile, "sigrok-meta-XXXXXX");
177 if ((tmpfile = g_mkstemp(metafile)) == -1)
178 return SR_ERR;
179 close(tmpfile);
868d8cef 180 meta = g_fopen(metafile, "wb");
7d658874
BV
181 fprintf(meta, "[global]\n");
182 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
183 /* TODO: save protocol decoders used */
184
185 /* all datastores in all devices */
186 devcnt = 1;
187 for (l = session->devices; l; l = l->next) {
188 device = l->data;
189 /* metadata */
190 fprintf(meta, "[device %d]\n", devcnt);
191 if (device->plugin)
192 fprintf(meta, "driver = %s\n", device->plugin->name);
193
194 ds = device->datastore;
195 if (ds) {
196 /* metadata */
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));
2bf4aca6 200 if (sr_device_has_hwcap(device, SR_HWCAP_SAMPLERATE)) {
7d658874
BV
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);
205 free(s);
206 }
207 probecnt = 1;
208 for (p = device->probes; p; p = p->next) {
209 probe = p->data;
210 if (probe->enabled) {
211 if (probe->name)
212 fprintf(meta, "probe%d = %s\n", probecnt, probe->name);
213 if (probe->trigger)
214 fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger);
215 probecnt++;
216 }
217 }
218
219 /* dump datastore into logic-n */
220 buf = malloc(ds->num_units * ds->ds_unitsize +
221 DATASTORE_CHUNKSIZE);
222 bufcnt = 0;
223 for (d = ds->chunklist; d; d = d->next) {
224 memcpy(buf + bufcnt, d->data,
225 DATASTORE_CHUNKSIZE);
226 bufcnt += DATASTORE_CHUNKSIZE;
227 }
228 if (!(logicsrc = zip_source_buffer(zipfile, buf,
229 ds->num_units * ds->ds_unitsize, TRUE)))
230 return SR_ERR;
231 snprintf(rawname, 15, "logic-%d", devcnt);
232 if (zip_add(zipfile, rawname, logicsrc) == -1)
233 return SR_ERR;
234 }
235 devcnt++;
236 }
237 fclose(meta);
238
239 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
240 return SR_ERR;
241 if (zip_add(zipfile, "metadata", metasrc) == -1)
242 return SR_ERR;
243
244 if ((ret = zip_close(zipfile)) == -1) {
b08024a8 245 sr_info("error saving zipfile: %s", zip_strerror(zipfile));
7d658874
BV
246 return SR_ERR;
247 }
248
249 unlink(metafile);
250
251 return SR_OK;
252}