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