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