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