]>
Commit | Line | Data |
---|---|---|
7d658874 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
7d658874 | 3 | * |
13d8e03c | 4 | * Copyright (C) 2013 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 | ||
6ec6c43b | 20 | #include <config.h> |
7d658874 BV |
21 | #include <string.h> |
22 | #include <stdlib.h> | |
7d658874 | 23 | #include <zip.h> |
5f9c4c8a | 24 | #include <errno.h> |
7d658874 | 25 | #include <glib.h> |
3bbd9849 | 26 | #include <glib/gstdio.h> |
c1aae900 | 27 | #include <libsigrok/libsigrok.h> |
45c59c8b | 28 | #include "libsigrok-internal.h" |
7d658874 | 29 | |
2ad1deb8 | 30 | /** @cond PRIVATE */ |
3544f848 | 31 | #define LOG_PREFIX "session-file" |
2ad1deb8 | 32 | /** @endcond */ |
a885ce3e | 33 | |
393fb9cb UH |
34 | /** |
35 | * @file | |
36 | * | |
37 | * Loading and saving libsigrok session files. | |
38 | */ | |
39 | ||
7b870c38 | 40 | /** |
777e2035 | 41 | * @addtogroup grp_session |
7b870c38 UH |
42 | * |
43 | * @{ | |
44 | */ | |
45 | ||
c09f0b57 | 46 | extern SR_PRIV struct sr_dev_driver session_driver; |
c6805a02 SA |
47 | static int session_driver_initialized = 0; |
48 | ||
a6dc3dac DE |
49 | #if !HAVE_ZIP_DISCARD |
50 | /* Replacement for zip_discard() if it isn't available. | |
51 | */ | |
52 | SR_PRIV void sr_zip_discard(struct zip *archive) | |
53 | { | |
54 | if (zip_unchange_all(archive) < 0 || zip_close(archive) < 0) | |
55 | sr_err("Failed to discard ZIP archive: %s", zip_strerror(archive)); | |
56 | } | |
57 | #endif | |
58 | ||
5e1fb334 DE |
59 | /** Read metadata entries from a session archive. |
60 | * @param[in] archive An open ZIP archive. | |
61 | * @param[in] entry Stat buffer filled in for the metadata archive member. | |
62 | * @return A new key/value store containing the session metadata. | |
63 | */ | |
64 | SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive, | |
65 | const struct zip_stat *entry) | |
66 | { | |
67 | GKeyFile *keyfile; | |
68 | GError *error; | |
69 | struct zip_file *zf; | |
70 | char *metabuf; | |
71 | int metalen; | |
72 | ||
73 | if (entry->size > G_MAXINT || !(metabuf = g_try_malloc(entry->size))) { | |
74 | sr_err("Metadata buffer allocation failed."); | |
75 | return NULL; | |
76 | } | |
77 | zf = zip_fopen_index(archive, entry->index, 0); | |
78 | if (!zf) { | |
79 | sr_err("Failed to open metadata: %s", zip_strerror(archive)); | |
80 | g_free(metabuf); | |
81 | return NULL; | |
82 | } | |
83 | metalen = zip_fread(zf, metabuf, entry->size); | |
84 | if (metalen < 0) { | |
85 | sr_err("Failed to read metadata: %s", zip_file_strerror(zf)); | |
86 | zip_fclose(zf); | |
87 | g_free(metabuf); | |
88 | return NULL; | |
89 | } | |
90 | zip_fclose(zf); | |
91 | ||
92 | keyfile = g_key_file_new(); | |
93 | error = NULL; | |
94 | g_key_file_load_from_data(keyfile, metabuf, metalen, | |
95 | G_KEY_FILE_NONE, &error); | |
96 | g_free(metabuf); | |
97 | ||
98 | if (error) { | |
99 | sr_err("Failed to parse metadata: %s", error->message); | |
100 | g_error_free(error); | |
101 | g_key_file_free(keyfile); | |
102 | return NULL; | |
103 | } | |
104 | return keyfile; | |
105 | } | |
106 | ||
72a08bcc | 107 | /** @private */ |
f438e0c9 | 108 | SR_PRIV int sr_sessionfile_check(const char *filename) |
7d658874 | 109 | { |
7d658874 BV |
110 | struct zip *archive; |
111 | struct zip_file *zf; | |
112 | struct zip_stat zs; | |
5e1fb334 DE |
113 | uint64_t version; |
114 | int ret; | |
f438e0c9 | 115 | char s[11]; |
7d658874 | 116 | |
f438e0c9 | 117 | if (!filename) |
9f45fb3a | 118 | return SR_ERR_ARG; |
9f45fb3a | 119 | |
5e1fb334 DE |
120 | if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) { |
121 | sr_err("Not a regular file: %s.", filename); | |
5f9c4c8a BV |
122 | return SR_ERR; |
123 | } | |
124 | ||
5e1fb334 | 125 | if (!(archive = zip_open(filename, 0, NULL))) |
f438e0c9 BV |
126 | /* No logging: this can be used just to check if it's |
127 | * a sigrok session file or not. */ | |
7d658874 | 128 | return SR_ERR; |
7d658874 BV |
129 | |
130 | /* check "version" */ | |
131 | if (!(zf = zip_fopen(archive, "version", 0))) { | |
f438e0c9 | 132 | sr_dbg("Not a sigrok session file: no version found."); |
5e1fb334 | 133 | zip_discard(archive); |
7d658874 BV |
134 | return SR_ERR; |
135 | } | |
5e1fb334 DE |
136 | ret = zip_fread(zf, s, sizeof(s) - 1); |
137 | if (ret < 0) { | |
138 | sr_err("Failed to read version file: %s", | |
139 | zip_file_strerror(zf)); | |
140 | zip_fclose(zf); | |
141 | zip_discard(archive); | |
7d658874 | 142 | return SR_ERR; |
5e1fb334 | 143 | } |
7d658874 | 144 | zip_fclose(zf); |
5e1fb334 DE |
145 | s[ret] = '\0'; |
146 | version = g_ascii_strtoull(s, NULL, 10); | |
147 | if (version == 0 || version > 2) { | |
148 | sr_dbg("Cannot handle sigrok session file version %" PRIu64 ".", | |
149 | version); | |
150 | zip_discard(archive); | |
c1864d55 BV |
151 | return SR_ERR; |
152 | } | |
5e1fb334 | 153 | sr_spew("Detected sigrok session file version %" PRIu64 ".", version); |
7d658874 BV |
154 | |
155 | /* read "metadata" */ | |
5e1fb334 | 156 | if (zip_stat(archive, "metadata", 0, &zs) < 0) { |
a885ce3e | 157 | sr_dbg("Not a valid sigrok session file."); |
5e1fb334 | 158 | zip_discard(archive); |
7d658874 BV |
159 | return SR_ERR; |
160 | } | |
5e1fb334 | 161 | zip_discard(archive); |
aba57f35 | 162 | |
f438e0c9 BV |
163 | return SR_OK; |
164 | } | |
165 | ||
166 | /** | |
167 | * Load the session from the specified filename. | |
168 | * | |
61e6e2da | 169 | * @param ctx The context in which to load the session. |
d386a52f BV |
170 | * @param filename The name of the session file to load. |
171 | * @param session The session to load the file into. | |
f438e0c9 | 172 | * |
d386a52f BV |
173 | * @retval SR_OK Success |
174 | * @retval SR_ERR_MALLOC Memory allocation error | |
175 | * @retval SR_ERR_DATA Malformed session file | |
176 | * @retval SR_ERR This is not a session file | |
f438e0c9 | 177 | */ |
61e6e2da ML |
178 | SR_API int sr_session_load(struct sr_context *ctx, const char *filename, |
179 | struct sr_session **session) | |
f438e0c9 BV |
180 | { |
181 | GKeyFile *kf; | |
5e1fb334 | 182 | GError *error; |
f438e0c9 | 183 | struct zip *archive; |
f438e0c9 BV |
184 | struct zip_stat zs; |
185 | struct sr_dev_inst *sdi; | |
6f1346fb | 186 | struct sr_channel *ch; |
c6805a02 | 187 | int ret, i, j; |
5e1fb334 DE |
188 | uint64_t tmp_u64; |
189 | int total_channels, k; | |
190 | int unitsize; | |
191 | char **sections, **keys, *val; | |
3f239f08 | 192 | char channelname[SR_MAX_CHANNELNAME_LEN + 1]; |
f438e0c9 BV |
193 | |
194 | if ((ret = sr_sessionfile_check(filename)) != SR_OK) | |
195 | return ret; | |
196 | ||
5e1fb334 | 197 | if (!(archive = zip_open(filename, 0, NULL))) |
f438e0c9 BV |
198 | return SR_ERR; |
199 | ||
5e1fb334 DE |
200 | if (zip_stat(archive, "metadata", 0, &zs) < 0) { |
201 | zip_discard(archive); | |
f438e0c9 | 202 | return SR_ERR; |
b53738ba | 203 | } |
5e1fb334 DE |
204 | kf = sr_sessionfile_read_metadata(archive, &zs); |
205 | zip_discard(archive); | |
206 | if (!kf) | |
207 | return SR_ERR_DATA; | |
b53738ba | 208 | |
5e1fb334 DE |
209 | if ((ret = sr_session_new(ctx, session)) != SR_OK) { |
210 | g_key_file_free(kf); | |
0812c40e | 211 | return ret; |
5e1fb334 | 212 | } |
7d658874 | 213 | |
5e1fb334 | 214 | error = NULL; |
d386a52f | 215 | ret = SR_OK; |
7d658874 | 216 | sections = g_key_file_get_groups(kf, NULL); |
d386a52f | 217 | for (i = 0; sections[i] && ret == SR_OK; i++) { |
7d658874 BV |
218 | if (!strcmp(sections[i], "global")) |
219 | /* nothing really interesting in here yet */ | |
220 | continue; | |
221 | if (!strncmp(sections[i], "device ", 7)) { | |
222 | /* device section */ | |
4d684427 | 223 | sdi = NULL; |
7d658874 BV |
224 | keys = g_key_file_get_keys(kf, sections[i], NULL, NULL); |
225 | for (j = 0; keys[j]; j++) { | |
7d658874 | 226 | if (!strcmp(keys[j], "capturefile")) { |
5e1fb334 DE |
227 | val = g_key_file_get_string(kf, sections[i], |
228 | keys[j], &error); | |
229 | if (!val) { | |
230 | ret = SR_ERR_DATA; | |
231 | break; | |
232 | } | |
aac29cc1 | 233 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
4d684427 | 234 | sdi->driver = &session_driver; |
0af636be | 235 | sdi->status = SR_ST_ACTIVE; |
c6805a02 | 236 | if (!session_driver_initialized) { |
c09f0b57 | 237 | /* first device, init the driver */ |
c6805a02 | 238 | session_driver_initialized = 1; |
4f840ce9 | 239 | sdi->driver->init(sdi->driver, NULL); |
c6805a02 | 240 | } |
bd7bfe8c | 241 | sr_dev_open(sdi); |
0812c40e | 242 | sr_session_dev_add(*session, sdi); |
1de3cced ML |
243 | (*session)->owned_devs = g_slist_append( |
244 | (*session)->owned_devs, sdi); | |
5e1fb334 DE |
245 | sr_config_set(sdi, NULL, SR_CONF_SESSIONFILE, |
246 | g_variant_new_string(filename)); | |
247 | sr_config_set(sdi, NULL, SR_CONF_CAPTUREFILE, | |
248 | g_variant_new_string(val)); | |
249 | g_free(val); | |
7d658874 | 250 | } else if (!strcmp(keys[j], "samplerate")) { |
5e1fb334 DE |
251 | val = g_key_file_get_string(kf, sections[i], |
252 | keys[j], &error); | |
253 | if (!sdi || !val || sr_parse_sizestring(val, | |
254 | &tmp_u64) != SR_OK) { | |
255 | g_free(val); | |
d386a52f BV |
256 | ret = SR_ERR_DATA; |
257 | break; | |
258 | } | |
5e1fb334 DE |
259 | g_free(val); |
260 | sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE, | |
261 | g_variant_new_uint64(tmp_u64)); | |
7d658874 | 262 | } else if (!strcmp(keys[j], "unitsize")) { |
5e1fb334 DE |
263 | unitsize = g_key_file_get_integer(kf, sections[i], |
264 | keys[j], &error); | |
265 | if (!sdi || unitsize <= 0 || error) { | |
d386a52f BV |
266 | ret = SR_ERR_DATA; |
267 | break; | |
268 | } | |
5e1fb334 DE |
269 | sr_config_set(sdi, NULL, SR_CONF_CAPTURE_UNITSIZE, |
270 | g_variant_new_uint64(unitsize)); | |
7d658874 | 271 | } else if (!strcmp(keys[j], "total probes")) { |
5e1fb334 DE |
272 | total_channels = g_key_file_get_integer(kf, |
273 | sections[i], keys[j], &error); | |
274 | if (!sdi || total_channels < 0 || error) { | |
d386a52f BV |
275 | ret = SR_ERR_DATA; |
276 | break; | |
277 | } | |
5e1fb334 | 278 | sr_config_set(sdi, NULL, SR_CONF_NUM_LOGIC_CHANNELS, |
2c38a41a | 279 | g_variant_new_int32(total_channels)); |
5e1fb334 DE |
280 | for (k = 0; k < total_channels; k++) { |
281 | g_snprintf(channelname, sizeof channelname, | |
282 | "%d", k); | |
283 | sr_channel_new(sdi, k, SR_CHANNEL_LOGIC, | |
284 | FALSE, channelname); | |
464d12c7 | 285 | } |
7d658874 | 286 | } else if (!strncmp(keys[j], "probe", 5)) { |
5e1fb334 DE |
287 | tmp_u64 = g_ascii_strtoull(keys[j]+5, NULL, 10); |
288 | if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) { | |
289 | ret = SR_ERR_DATA; | |
290 | break; | |
291 | } | |
292 | ch = g_slist_nth_data(sdi->channels, tmp_u64 - 1); | |
293 | if (!ch) { | |
294 | ret = SR_ERR_DATA; | |
295 | break; | |
296 | } | |
297 | val = g_key_file_get_string(kf, sections[i], | |
298 | keys[j], &error); | |
299 | if (!val) { | |
d386a52f BV |
300 | ret = SR_ERR_DATA; |
301 | break; | |
302 | } | |
fb381e4d | 303 | /* sr_session_save() */ |
6f1346fb | 304 | sr_dev_channel_name_set(ch, val); |
5e1fb334 | 305 | g_free(val); |
6f1346fb | 306 | sr_dev_channel_enable(ch, TRUE); |
7d658874 BV |
307 | } |
308 | } | |
309 | g_strfreev(keys); | |
7d658874 BV |
310 | } |
311 | } | |
312 | g_strfreev(sections); | |
313 | g_key_file_free(kf); | |
314 | ||
5e1fb334 DE |
315 | if (error) { |
316 | sr_err("Failed to parse metadata: %s", error->message); | |
317 | g_error_free(error); | |
a769b9f3 | 318 | } |
a769b9f3 BV |
319 | return ret; |
320 | } | |
321 | ||
7b870c38 | 322 | /** @} */ |