]> sigrok.org Git - libsigrok.git/blob - src/session_file.c
Enable loading of session files without the unit size defined
[libsigrok.git] / src / session_file.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 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 <config.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <zip.h>
24 #include <errno.h>
25 #include <glib.h>
26 #include <glib/gstdio.h>
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
29
30 /** @cond PRIVATE */
31 #define LOG_PREFIX "session-file"
32 /** @endcond */
33
34 /**
35  * @file
36  *
37  * Loading and saving libsigrok session files.
38  */
39
40 /**
41  * @addtogroup grp_session
42  *
43  * @{
44  */
45
46 extern SR_PRIV struct sr_dev_driver session_driver;
47 static int session_driver_initialized = 0;
48
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
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
107 /** @private */
108 SR_PRIV int sr_sessionfile_check(const char *filename)
109 {
110         struct zip *archive;
111         struct zip_file *zf;
112         struct zip_stat zs;
113         uint64_t version;
114         int ret;
115         char s[11];
116
117         if (!filename)
118                 return SR_ERR_ARG;
119
120         if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
121                 sr_err("Not a regular file: %s.", filename);
122                 return SR_ERR;
123         }
124
125         if (!(archive = zip_open(filename, 0, NULL)))
126                 /* No logging: this can be used just to check if it's
127                  * a sigrok session file or not. */
128                 return SR_ERR;
129
130         /* check "version" */
131         if (!(zf = zip_fopen(archive, "version", 0))) {
132                 sr_dbg("Not a sigrok session file: no version found.");
133                 zip_discard(archive);
134                 return SR_ERR;
135         }
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);
142                 return SR_ERR;
143         }
144         zip_fclose(zf);
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);
151                 return SR_ERR;
152         }
153         sr_spew("Detected sigrok session file version %" PRIu64 ".", version);
154
155         /* read "metadata" */
156         if (zip_stat(archive, "metadata", 0, &zs) < 0) {
157                 sr_dbg("Not a valid sigrok session file.");
158                 zip_discard(archive);
159                 return SR_ERR;
160         }
161         zip_discard(archive);
162
163         return SR_OK;
164 }
165
166 SR_PRIV struct sr_dev_inst *sr_session_prepare_sdi(const char *filename, struct sr_session **session)
167 {
168         struct sr_dev_inst *sdi = NULL;
169
170         sdi = g_malloc0(sizeof(struct sr_dev_inst));
171         sdi->driver = &session_driver;
172         sdi->status = SR_ST_ACTIVE;
173         if (!session_driver_initialized) {
174                 /* first device, init the driver */
175                 session_driver_initialized = 1;
176                 sdi->driver->init(sdi->driver, NULL);
177         }
178         sr_dev_open(sdi);
179         sr_session_dev_add(*session, sdi);
180         (*session)->owned_devs = g_slist_append((*session)->owned_devs, sdi);
181         sr_config_set(sdi, NULL, SR_CONF_SESSIONFILE,
182                         g_variant_new_string(filename));
183
184         return sdi;
185 }
186
187 /**
188  * Load the session from the specified filename.
189  *
190  * @param ctx The context in which to load the session.
191  * @param filename The name of the session file to load.
192  * @param session The session to load the file into.
193  *
194  * @retval SR_OK Success
195  * @retval SR_ERR_MALLOC Memory allocation error
196  * @retval SR_ERR_DATA Malformed session file
197  * @retval SR_ERR This is not a session file
198  */
199 SR_API int sr_session_load(struct sr_context *ctx, const char *filename,
200                 struct sr_session **session)
201 {
202         GKeyFile *kf;
203         GError *error;
204         struct zip *archive;
205         struct zip_stat zs;
206         struct sr_dev_inst *sdi;
207         struct sr_channel *ch;
208         int ret, i, j;
209         uint64_t tmp_u64;
210         int total_channels, total_analog, k;
211         GSList *l;
212         int unitsize;
213         char **sections, **keys, *val;
214         char channelname[SR_MAX_CHANNELNAME_LEN + 1];
215         gboolean file_has_logic;
216
217         if ((ret = sr_sessionfile_check(filename)) != SR_OK)
218                 return ret;
219
220         if (!(archive = zip_open(filename, 0, NULL)))
221                 return SR_ERR;
222
223         if (zip_stat(archive, "metadata", 0, &zs) < 0) {
224                 zip_discard(archive);
225                 return SR_ERR;
226         }
227         kf = sr_sessionfile_read_metadata(archive, &zs);
228         zip_discard(archive);
229         if (!kf)
230                 return SR_ERR_DATA;
231
232         if ((ret = sr_session_new(ctx, session)) != SR_OK) {
233                 g_key_file_free(kf);
234                 return ret;
235         }
236
237         error = NULL;
238         ret = SR_OK;
239         file_has_logic = FALSE;
240         sections = g_key_file_get_groups(kf, NULL);
241         for (i = 0; sections[i] && ret == SR_OK; i++) {
242                 if (!strcmp(sections[i], "global"))
243                         /* nothing really interesting in here yet */
244                         continue;
245                 if (!strncmp(sections[i], "device ", 7)) {
246                         /* device section */
247                         sdi = NULL;
248                         keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
249
250                         /* File contains analog data if there are analog channels. */
251                         total_analog = g_key_file_get_integer(kf, sections[i],
252                                         "total analog", &error);
253                         if (total_analog > 0 && !error)
254                                 sdi = sr_session_prepare_sdi(filename, session);
255                         g_clear_error(&error);
256
257                         /* File contains logic data if a capturefile is set. */
258                         val = g_key_file_get_string(kf, sections[i],
259                                 "capturefile", &error);
260                         if (val && !error) {
261                                 if (!sdi)
262                                         sdi = sr_session_prepare_sdi(filename, session);
263                                 sr_config_set(sdi, NULL, SR_CONF_CAPTUREFILE,
264                                                 g_variant_new_string(val));
265                                 g_free(val);
266                                 file_has_logic = TRUE;
267                         }
268                         g_clear_error(&error);
269
270                         for (j = 0; keys[j]; j++) {
271                                 if (!strcmp(keys[j], "samplerate")) {
272                                         val = g_key_file_get_string(kf, sections[i],
273                                                         keys[j], &error);
274                                         if (!sdi || !val || sr_parse_sizestring(val,
275                                                                 &tmp_u64) != SR_OK) {
276                                                 g_free(val);
277                                                 ret = SR_ERR_DATA;
278                                                 break;
279                                         }
280                                         g_free(val);
281                                         sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
282                                                         g_variant_new_uint64(tmp_u64));
283                                 } else if (!strcmp(keys[j], "unitsize") && file_has_logic) {
284                                         unitsize = g_key_file_get_integer(kf, sections[i],
285                                                         keys[j], &error);
286                                         if (!sdi || unitsize <= 0 || error) {
287                                                 ret = SR_ERR_DATA;
288                                                 break;
289                                         }
290                                         sr_config_set(sdi, NULL, SR_CONF_CAPTURE_UNITSIZE,
291                                                         g_variant_new_uint64(unitsize));
292                                 } else if (!strcmp(keys[j], "total probes")) {
293                                         total_channels = g_key_file_get_integer(kf,
294                                                         sections[i], keys[j], &error);
295                                         if (!sdi || total_channels < 0 || error) {
296                                                 ret = SR_ERR_DATA;
297                                                 break;
298                                         }
299                                         sr_config_set(sdi, NULL, SR_CONF_NUM_LOGIC_CHANNELS,
300                                                         g_variant_new_int32(total_channels));
301                                         for (k = 0; k < total_channels; k++) {
302                                                 g_snprintf(channelname, sizeof(channelname),
303                                                                 "%d", k);
304                                                 sr_channel_new(sdi, k, SR_CHANNEL_LOGIC,
305                                                                 FALSE, channelname);
306                                         }
307                                 } else if (!strcmp(keys[j], "total analog")) {
308                                         total_analog = g_key_file_get_integer(kf,
309                                                         sections[i], keys[j], &error);
310                                         if (!sdi || total_analog < 0 || error) {
311                                                 ret = SR_ERR_DATA;
312                                                 break;
313                                         }
314                                         sr_config_set(sdi, NULL, SR_CONF_NUM_ANALOG_CHANNELS,
315                                                         g_variant_new_int32(total_analog));
316                                         for (k = total_channels; k < (total_channels + total_analog); k++) {
317                                                 g_snprintf(channelname, sizeof(channelname),
318                                                                 "%d", k);
319                                                 sr_channel_new(sdi, k, SR_CHANNEL_ANALOG,
320                                                                 FALSE, channelname);
321                                         }
322                                 } else if (!strncmp(keys[j], "probe", 5)) {
323                                         tmp_u64 = g_ascii_strtoull(keys[j] + 5, NULL, 10);
324                                         if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
325                                                 ret = SR_ERR_DATA;
326                                                 break;
327                                         }
328                                         ch = g_slist_nth_data(sdi->channels, tmp_u64 - 1);
329                                         if (!ch) {
330                                                 ret = SR_ERR_DATA;
331                                                 break;
332                                         }
333                                         val = g_key_file_get_string(kf, sections[i],
334                                                         keys[j], &error);
335                                         if (!val) {
336                                                 ret = SR_ERR_DATA;
337                                                 break;
338                                         }
339                                         /* sr_session_save() */
340                                         sr_dev_channel_name_set(ch, val);
341                                         g_free(val);
342                                         sr_dev_channel_enable(ch, TRUE);
343                                 } else if (!strncmp(keys[j], "analog", 6)) {
344                                         tmp_u64 = g_ascii_strtoull(keys[j]+6, NULL, 10);
345                                         if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
346                                                 ret = SR_ERR_DATA;
347                                                 break;
348                                         }
349                                         ch = NULL;
350                                         for (l = sdi->channels; l; l = l->next) {
351                                                 ch = l->data;
352                                                 if ((guint64)ch->index == tmp_u64 - 1)
353                                                         break;
354                                                 else
355                                                         ch = NULL;
356                                         }
357                                         if (!ch) {
358                                                 ret = SR_ERR_DATA;
359                                                 break;
360                                         }
361                                         val = g_key_file_get_string(kf, sections[i],
362                                                         keys[j], &error);
363                                         if (!val) {
364                                                 ret = SR_ERR_DATA;
365                                                 break;
366                                         }
367                                         /* sr_session_save() */
368                                         sr_dev_channel_name_set(ch, val);
369                                         g_free(val);
370                                         sr_dev_channel_enable(ch, TRUE);
371                                 }
372                         }
373                         g_strfreev(keys);
374                 }
375         }
376         g_strfreev(sections);
377         g_key_file_free(kf);
378
379         if (error) {
380                 sr_err("Failed to parse metadata: %s", error->message);
381                 g_error_free(error);
382         }
383         return ret;
384 }
385
386 /** @} */