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