]>
Commit | Line | Data |
---|---|---|
7d658874 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
7d658874 BV |
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 | ||
20 | #include <string.h> | |
21 | #include <stdlib.h> | |
22 | #include <unistd.h> | |
23 | #include <zip.h> | |
24 | #include <glib.h> | |
3bbd9849 | 25 | #include <glib/gstdio.h> |
1a081ca6 | 26 | #include "config.h" |
45c59c8b BV |
27 | #include "libsigrok.h" |
28 | #include "libsigrok-internal.h" | |
7d658874 | 29 | |
2872d21e | 30 | extern struct sr_session *session; |
c09f0b57 | 31 | extern SR_PRIV struct sr_dev_driver session_driver; |
7d658874 | 32 | |
9f45fb3a UH |
33 | /** |
34 | * Load the session from the specified filename. | |
35 | * | |
36 | * @param filename The name of the session file to load. Must not be NULL. | |
37 | * | |
38 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, | |
39 | * SR_ERR_MALLOC upon memory allocation errors, or SR_ERR upon | |
40 | * other errors. | |
41 | */ | |
1a081ca6 | 42 | SR_API int sr_session_load(const char *filename) |
7d658874 BV |
43 | { |
44 | GKeyFile *kf; | |
45 | GPtrArray *capturefiles; | |
46 | struct zip *archive; | |
47 | struct zip_file *zf; | |
48 | struct zip_stat zs; | |
4d684427 | 49 | struct sr_dev_inst *sdi; |
1afe8989 | 50 | struct sr_probe *probe; |
ebc34738 | 51 | int ret, probenum, devcnt, i, j; |
7d658874 BV |
52 | uint64_t tmp_u64, total_probes, enabled_probes, p; |
53 | char **sections, **keys, *metafile, *val, c; | |
c37d2b1b | 54 | char probename[SR_MAX_PROBENAME_LEN + 1]; |
7d658874 | 55 | |
9f45fb3a UH |
56 | if (!filename) { |
57 | sr_err("session file: %s: filename was NULL", __func__); | |
58 | return SR_ERR_ARG; | |
59 | } | |
60 | ||
ebc34738 | 61 | if (!(archive = zip_open(filename, 0, &ret))) { |
7b48d6e1 | 62 | sr_dbg("session file: Failed to open session file: zip " |
ebc34738 | 63 | "error %d", ret); |
7d658874 BV |
64 | return SR_ERR; |
65 | } | |
66 | ||
67 | /* check "version" */ | |
68 | if (!(zf = zip_fopen(archive, "version", 0))) { | |
7b48d6e1 | 69 | sr_dbg("session file: Not a sigrok session file."); |
7d658874 BV |
70 | return SR_ERR; |
71 | } | |
72 | ret = zip_fread(zf, &c, 1); | |
73 | if (ret != 1 || c != '1') { | |
7b48d6e1 | 74 | sr_dbg("session file: Not a valid sigrok session file."); |
7d658874 BV |
75 | return SR_ERR; |
76 | } | |
77 | zip_fclose(zf); | |
78 | ||
79 | /* read "metadata" */ | |
80 | if (zip_stat(archive, "metadata", 0, &zs) == -1) { | |
7b48d6e1 | 81 | sr_dbg("session file: Not a valid sigrok session file."); |
7d658874 BV |
82 | return SR_ERR; |
83 | } | |
b53738ba UH |
84 | |
85 | if (!(metafile = g_try_malloc(zs.size))) { | |
86 | sr_err("session file: %s: metafile malloc failed", __func__); | |
87 | return SR_ERR_MALLOC; | |
88 | } | |
89 | ||
7d658874 BV |
90 | zf = zip_fopen_index(archive, zs.index, 0); |
91 | zip_fread(zf, metafile, zs.size); | |
92 | zip_fclose(zf); | |
93 | ||
94 | kf = g_key_file_new(); | |
95 | if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) { | |
7b48d6e1 | 96 | sr_dbg("session file: Failed to parse metadata."); |
7d658874 BV |
97 | return SR_ERR; |
98 | } | |
99 | ||
2285cf9b | 100 | sr_session_new(); |
7d658874 BV |
101 | |
102 | devcnt = 0; | |
103 | capturefiles = g_ptr_array_new_with_free_func(g_free); | |
104 | sections = g_key_file_get_groups(kf, NULL); | |
105 | for (i = 0; sections[i]; i++) { | |
106 | if (!strcmp(sections[i], "global")) | |
107 | /* nothing really interesting in here yet */ | |
108 | continue; | |
109 | if (!strncmp(sections[i], "device ", 7)) { | |
110 | /* device section */ | |
4d684427 | 111 | sdi = NULL; |
7d658874 BV |
112 | enabled_probes = 0; |
113 | keys = g_key_file_get_keys(kf, sections[i], NULL, NULL); | |
114 | for (j = 0; keys[j]; j++) { | |
115 | val = g_key_file_get_string(kf, sections[i], keys[j], NULL); | |
116 | if (!strcmp(keys[j], "capturefile")) { | |
4d684427 BV |
117 | sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, NULL, NULL, NULL); |
118 | sdi->driver = &session_driver; | |
7d658874 | 119 | if (devcnt == 0) |
c09f0b57 | 120 | /* first device, init the driver */ |
4d684427 BV |
121 | sdi->driver->init(); |
122 | sr_session_dev_add(sdi); | |
123 | sdi->driver->dev_config_set(sdi, SR_HWCAP_SESSIONFILE, filename); | |
124 | sdi->driver->dev_config_set(sdi, SR_HWCAP_CAPTUREFILE, val); | |
7d658874 BV |
125 | g_ptr_array_add(capturefiles, val); |
126 | } else if (!strcmp(keys[j], "samplerate")) { | |
f64c1414 | 127 | sr_parse_sizestring(val, &tmp_u64); |
4d684427 | 128 | sdi->driver->dev_config_set(sdi, SR_HWCAP_SAMPLERATE, &tmp_u64); |
7d658874 BV |
129 | } else if (!strcmp(keys[j], "unitsize")) { |
130 | tmp_u64 = strtoull(val, NULL, 10); | |
4d684427 | 131 | sdi->driver->dev_config_set(sdi, SR_HWCAP_CAPTURE_UNITSIZE, &tmp_u64); |
7d658874 BV |
132 | } else if (!strcmp(keys[j], "total probes")) { |
133 | total_probes = strtoull(val, NULL, 10); | |
4d684427 | 134 | sdi->driver->dev_config_set(sdi, SR_HWCAP_CAPTURE_NUM_PROBES, &total_probes); |
464d12c7 KS |
135 | for (p = 0; p < total_probes; p++) { |
136 | snprintf(probename, SR_MAX_PROBENAME_LEN, "%" PRIu64, p); | |
fb381e4d | 137 | if (!(probe = sr_probe_new(p, SR_PROBE_LOGIC, TRUE, |
4d684427 BV |
138 | probename))) |
139 | return SR_ERR; | |
140 | sdi->probes = g_slist_append(sdi->probes, probe); | |
464d12c7 | 141 | } |
7d658874 | 142 | } else if (!strncmp(keys[j], "probe", 5)) { |
4d684427 | 143 | if (!sdi) |
7d658874 BV |
144 | continue; |
145 | enabled_probes++; | |
146 | tmp_u64 = strtoul(keys[j]+5, NULL, 10); | |
fb381e4d BV |
147 | /* sr_session_save() */ |
148 | sr_dev_probe_name_set(sdi, tmp_u64 - 1, val); | |
7d658874 BV |
149 | } else if (!strncmp(keys[j], "trigger", 7)) { |
150 | probenum = strtoul(keys[j]+7, NULL, 10); | |
4d684427 | 151 | sr_dev_trigger_set(sdi, probenum, val); |
7d658874 BV |
152 | } |
153 | } | |
154 | g_strfreev(keys); | |
fb381e4d | 155 | /* Disable probes not specifically listed. */ |
be5bf44d BV |
156 | for (p = enabled_probes; p < total_probes; p++) |
157 | sr_dev_probe_enable(sdi, p, FALSE); | |
7d658874 | 158 | } |
4d684427 | 159 | devcnt++; |
7d658874 BV |
160 | } |
161 | g_strfreev(sections); | |
162 | g_key_file_free(kf); | |
163 | ||
164 | return SR_OK; | |
165 | } | |
166 | ||
9f45fb3a UH |
167 | /** |
168 | * Save the current session to the specified file. | |
169 | * | |
170 | * @param filename The name of the file where to save the current session. | |
171 | * Must not be NULL. | |
172 | * | |
173 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR | |
174 | * upon other errors. | |
175 | */ | |
8225e921 | 176 | int sr_session_save(const char *filename) |
7d658874 BV |
177 | { |
178 | GSList *l, *p, *d; | |
179 | FILE *meta; | |
4d684427 | 180 | struct sr_dev_inst *sdi; |
1afe8989 | 181 | struct sr_probe *probe; |
c4911129 | 182 | struct sr_datastore *ds; |
7d658874 BV |
183 | struct zip *zipfile; |
184 | struct zip_source *versrc, *metasrc, *logicsrc; | |
ebc34738 | 185 | int bufcnt, devcnt, tmpfile, ret, probecnt; |
7d658874 | 186 | uint64_t samplerate; |
4a1b18f8 | 187 | char version[1], rawname[16], metafile[32], *buf, *s; |
7d658874 | 188 | |
9f45fb3a UH |
189 | if (!filename) { |
190 | sr_err("session file: %s: filename was NULL", __func__); | |
191 | return SR_ERR_ARG; | |
192 | } | |
193 | ||
7d658874 | 194 | /* Quietly delete it first, libzip wants replace ops otherwise. */ |
4a1b18f8 | 195 | unlink(filename); |
ebc34738 | 196 | if (!(zipfile = zip_open(filename, ZIP_CREATE, &ret))) |
7d658874 | 197 | return SR_ERR; |
7d658874 BV |
198 | |
199 | /* "version" */ | |
200 | version[0] = '1'; | |
201 | if (!(versrc = zip_source_buffer(zipfile, version, 1, 0))) | |
202 | return SR_ERR; | |
203 | if (zip_add(zipfile, "version", versrc) == -1) { | |
7b48d6e1 | 204 | sr_info("session file: error saving version into zipfile: %s", |
b08024a8 | 205 | zip_strerror(zipfile)); |
7d658874 BV |
206 | return SR_ERR; |
207 | } | |
208 | ||
209 | /* init "metadata" */ | |
210 | strcpy(metafile, "sigrok-meta-XXXXXX"); | |
211 | if ((tmpfile = g_mkstemp(metafile)) == -1) | |
212 | return SR_ERR; | |
213 | close(tmpfile); | |
868d8cef | 214 | meta = g_fopen(metafile, "wb"); |
7d658874 BV |
215 | fprintf(meta, "[global]\n"); |
216 | fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION); | |
217 | /* TODO: save protocol decoders used */ | |
218 | ||
219 | /* all datastores in all devices */ | |
220 | devcnt = 1; | |
bb7ef793 UH |
221 | for (l = session->devs; l; l = l->next) { |
222 | dev = l->data; | |
7d658874 BV |
223 | /* metadata */ |
224 | fprintf(meta, "[device %d]\n", devcnt); | |
c09f0b57 UH |
225 | if (dev->driver) |
226 | fprintf(meta, "driver = %s\n", dev->driver->name); | |
7d658874 | 227 | |
bb7ef793 | 228 | ds = dev->datastore; |
7d658874 BV |
229 | if (ds) { |
230 | /* metadata */ | |
231 | fprintf(meta, "capturefile = logic-%d\n", devcnt); | |
232 | fprintf(meta, "unitsize = %d\n", ds->ds_unitsize); | |
bb7ef793 UH |
233 | fprintf(meta, "total probes = %d\n", g_slist_length(dev->probes)); |
234 | if (sr_dev_has_hwcap(dev, SR_HWCAP_SAMPLERATE)) { | |
c09f0b57 UH |
235 | samplerate = *((uint64_t *) dev->driver->dev_info_get( |
236 | dev->driver_index, SR_DI_CUR_SAMPLERATE)); | |
7d658874 BV |
237 | s = sr_samplerate_string(samplerate); |
238 | fprintf(meta, "samplerate = %s\n", s); | |
133a37bf | 239 | g_free(s); |
7d658874 BV |
240 | } |
241 | probecnt = 1; | |
bb7ef793 | 242 | for (p = dev->probes; p; p = p->next) { |
7d658874 BV |
243 | probe = p->data; |
244 | if (probe->enabled) { | |
245 | if (probe->name) | |
246 | fprintf(meta, "probe%d = %s\n", probecnt, probe->name); | |
247 | if (probe->trigger) | |
248 | fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger); | |
249 | probecnt++; | |
250 | } | |
251 | } | |
252 | ||
253 | /* dump datastore into logic-n */ | |
133a37bf | 254 | buf = g_try_malloc(ds->num_units * ds->ds_unitsize + |
7d658874 | 255 | DATASTORE_CHUNKSIZE); |
133a37bf UH |
256 | if (!buf) { |
257 | sr_err("session file: %s: buf malloc failed", | |
258 | __func__); | |
259 | return SR_ERR_MALLOC; | |
260 | } | |
261 | ||
7d658874 BV |
262 | bufcnt = 0; |
263 | for (d = ds->chunklist; d; d = d->next) { | |
264 | memcpy(buf + bufcnt, d->data, | |
265 | DATASTORE_CHUNKSIZE); | |
266 | bufcnt += DATASTORE_CHUNKSIZE; | |
267 | } | |
268 | if (!(logicsrc = zip_source_buffer(zipfile, buf, | |
269 | ds->num_units * ds->ds_unitsize, TRUE))) | |
270 | return SR_ERR; | |
271 | snprintf(rawname, 15, "logic-%d", devcnt); | |
272 | if (zip_add(zipfile, rawname, logicsrc) == -1) | |
273 | return SR_ERR; | |
274 | } | |
275 | devcnt++; | |
276 | } | |
277 | fclose(meta); | |
278 | ||
279 | if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1))) | |
280 | return SR_ERR; | |
281 | if (zip_add(zipfile, "metadata", metasrc) == -1) | |
282 | return SR_ERR; | |
283 | ||
284 | if ((ret = zip_close(zipfile)) == -1) { | |
7b48d6e1 UH |
285 | sr_info("session file: error saving zipfile: %s", |
286 | zip_strerror(zipfile)); | |
7d658874 BV |
287 | return SR_ERR; |
288 | } | |
289 | ||
290 | unlink(metafile); | |
291 | ||
292 | return SR_OK; | |
293 | } |