]> sigrok.org Git - libsigrok.git/blame - session_file.c
Add sr_ prefix for 'struct probe'.
[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>
7d658874 27#include <sigrok.h>
7d658874
BV
28
29extern struct session *session;
30extern struct sr_device_plugin session_driver;
31
32
33int session_load(const char *filename)
34{
35 GKeyFile *kf;
36 GPtrArray *capturefiles;
37 struct zip *archive;
38 struct zip_file *zf;
39 struct zip_stat zs;
40 struct session *session;
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;
46
47 if (!(archive = zip_open(filename, 0, &err))) {
48 g_debug("Failed to open session file: zip error %d", err);
49 return SR_ERR;
50 }
51
52 /* check "version" */
53 if (!(zf = zip_fopen(archive, "version", 0))) {
54 g_debug("Not a sigrok session file.");
55 return SR_ERR;
56 }
57 ret = zip_fread(zf, &c, 1);
58 if (ret != 1 || c != '1') {
59 g_debug("Not a valid sigrok session file.");
60 return SR_ERR;
61 }
62 zip_fclose(zf);
63
64 /* read "metadata" */
65 if (zip_stat(archive, "metadata", 0, &zs) == -1) {
66 g_debug("Not a valid sigrok session file.");
67 return SR_ERR;
68 }
69 metafile = g_malloc(zs.size);
70 zf = zip_fopen_index(archive, zs.index, 0);
71 zip_fread(zf, metafile, zs.size);
72 zip_fclose(zf);
73
74 kf = g_key_file_new();
75 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
76 g_debug("Failed to parse metadata.");
77 return SR_ERR;
78 }
79
80 session = session_new();
81
82 devcnt = 0;
83 capturefiles = g_ptr_array_new_with_free_func(g_free);
84 sections = g_key_file_get_groups(kf, NULL);
85 for (i = 0; sections[i]; i++) {
86 if (!strcmp(sections[i], "global"))
87 /* nothing really interesting in here yet */
88 continue;
89 if (!strncmp(sections[i], "device ", 7)) {
90 /* device section */
91 device = NULL;
92 enabled_probes = 0;
93 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
94 for (j = 0; keys[j]; j++) {
95 val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
96 if (!strcmp(keys[j], "capturefile")) {
97 device = device_new(&session_driver, devcnt, 0);
98 if (devcnt == 0)
99 /* first device, init the plugin */
100 device->plugin->init((char *)filename);
101 session_device_add(device);
102 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTUREFILE, val);
103 g_ptr_array_add(capturefiles, val);
104 } else if (!strcmp(keys[j], "samplerate")) {
105 tmp_u64 = sr_parse_sizestring(val);
106 device->plugin->set_configuration(devcnt, SR_HWCAP_SAMPLERATE, &tmp_u64);
107 } else if (!strcmp(keys[j], "unitsize")) {
108 tmp_u64 = strtoull(val, NULL, 10);
109 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTURE_UNITSIZE, &tmp_u64);
110 } else if (!strcmp(keys[j], "total probes")) {
111 total_probes = strtoull(val, NULL, 10);
112 device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTURE_NUM_PROBES, &total_probes);
113 for (p = 1; p <= total_probes; p++)
114 device_probe_add(device, NULL);
115 } else if (!strncmp(keys[j], "probe", 5)) {
116 if (!device)
117 continue;
118 enabled_probes++;
119 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
120 device_probe_name(device, tmp_u64, val);
121 } else if (!strncmp(keys[j], "trigger", 7)) {
122 probenum = strtoul(keys[j]+7, NULL, 10);
123 device_trigger_set(device, probenum, val);
124 }
125 }
126 g_strfreev(keys);
127 for (p = enabled_probes; p < total_probes; p++) {
128 probe = g_slist_nth_data(device->probes, p);
129 probe->enabled = FALSE;
130 }
131 }
132 }
133 g_strfreev(sections);
134 g_key_file_free(kf);
135
136 return SR_OK;
137}
138
139int session_save(char *filename)
140{
141 GSList *l, *p, *d;
142 FILE *meta;
143 struct sr_device *device;
1afe8989 144 struct sr_probe *probe;
7d658874
BV
145 struct datastore *ds;
146 struct zip *zipfile;
147 struct zip_source *versrc, *metasrc, *logicsrc;
148 int bufcnt, devcnt, tmpfile, ret, error, probecnt;
149 uint64_t samplerate;
4a1b18f8 150 char version[1], rawname[16], metafile[32], *buf, *s;
7d658874
BV
151
152 /* Quietly delete it first, libzip wants replace ops otherwise. */
4a1b18f8
BV
153 unlink(filename);
154 if (!(zipfile = zip_open(filename, ZIP_CREATE, &error)))
7d658874 155 return SR_ERR;
7d658874
BV
156
157 /* "version" */
158 version[0] = '1';
159 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
160 return SR_ERR;
161 if (zip_add(zipfile, "version", versrc) == -1) {
162 g_message("error saving version into zipfile: %s",
163 zip_strerror(zipfile));
164 return SR_ERR;
165 }
166
167 /* init "metadata" */
168 strcpy(metafile, "sigrok-meta-XXXXXX");
169 if ((tmpfile = g_mkstemp(metafile)) == -1)
170 return SR_ERR;
171 close(tmpfile);
868d8cef 172 meta = g_fopen(metafile, "wb");
7d658874
BV
173 fprintf(meta, "[global]\n");
174 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
175 /* TODO: save protocol decoders used */
176
177 /* all datastores in all devices */
178 devcnt = 1;
179 for (l = session->devices; l; l = l->next) {
180 device = l->data;
181 /* metadata */
182 fprintf(meta, "[device %d]\n", devcnt);
183 if (device->plugin)
184 fprintf(meta, "driver = %s\n", device->plugin->name);
185
186 ds = device->datastore;
187 if (ds) {
188 /* metadata */
189 fprintf(meta, "capturefile = logic-%d\n", devcnt);
190 fprintf(meta, "unitsize = %d\n", ds->ds_unitsize);
191 fprintf(meta, "total probes = %d\n", g_slist_length(device->probes));
192 if (device_has_hwcap(device, SR_HWCAP_SAMPLERATE)) {
193 samplerate = *((uint64_t *) device->plugin->get_device_info(
194 device->plugin_index, SR_DI_CUR_SAMPLERATE));
195 s = sr_samplerate_string(samplerate);
196 fprintf(meta, "samplerate = %s\n", s);
197 free(s);
198 }
199 probecnt = 1;
200 for (p = device->probes; p; p = p->next) {
201 probe = p->data;
202 if (probe->enabled) {
203 if (probe->name)
204 fprintf(meta, "probe%d = %s\n", probecnt, probe->name);
205 if (probe->trigger)
206 fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger);
207 probecnt++;
208 }
209 }
210
211 /* dump datastore into logic-n */
212 buf = malloc(ds->num_units * ds->ds_unitsize +
213 DATASTORE_CHUNKSIZE);
214 bufcnt = 0;
215 for (d = ds->chunklist; d; d = d->next) {
216 memcpy(buf + bufcnt, d->data,
217 DATASTORE_CHUNKSIZE);
218 bufcnt += DATASTORE_CHUNKSIZE;
219 }
220 if (!(logicsrc = zip_source_buffer(zipfile, buf,
221 ds->num_units * ds->ds_unitsize, TRUE)))
222 return SR_ERR;
223 snprintf(rawname, 15, "logic-%d", devcnt);
224 if (zip_add(zipfile, rawname, logicsrc) == -1)
225 return SR_ERR;
226 }
227 devcnt++;
228 }
229 fclose(meta);
230
231 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
232 return SR_ERR;
233 if (zip_add(zipfile, "metadata", metasrc) == -1)
234 return SR_ERR;
235
236 if ((ret = zip_close(zipfile)) == -1) {
237 g_message("error saving zipfile: %s", zip_strerror(zipfile));
238 return SR_ERR;
239 }
240
241 unlink(metafile);
242
243 return SR_OK;
244}
245