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