]> sigrok.org Git - libsigrok.git/blob - session_file.c
implement session loading based on a virtual device driver
[libsigrok.git] / session_file.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2011 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 <sigrok.h>
26 #include <config.h>
27
28
29 extern struct session *session;
30 extern struct sr_device_plugin session_driver;
31
32
33 int session_load(const char *filename)
34 {
35         GKeyFile *kf;
36         GPtrArray *capturefiles;
37         struct zip *archive;
38         struct zip_file *zf;
39         struct zip_stat zs;
40         struct session *session;
41         struct sr_device *device;
42         struct probe *probe;
43         int ret, err, probenum, devcnt, i, j;
44         uint64_t tmp_u64, total_probes, enabled_probes, p;
45         char **sections, **keys, *metafile, *val, c;
46
47         if (!(archive = zip_open(filename, 0, &err))) {
48                 g_debug("Failed to open session file: zip error %d", err);
49                 return SR_ERR;
50         }
51
52         /* check "version" */
53         if (!(zf = zip_fopen(archive, "version", 0))) {
54                 g_debug("Not a sigrok session file.");
55                 return SR_ERR;
56         }
57         ret = zip_fread(zf, &c, 1);
58         if (ret != 1 || c != '1') {
59                 g_debug("Not a valid sigrok session file.");
60                 return SR_ERR;
61         }
62         zip_fclose(zf);
63
64         /* read "metadata" */
65         if (zip_stat(archive, "metadata", 0, &zs) == -1) {
66                 g_debug("Not a valid sigrok session file.");
67                 return SR_ERR;
68         }
69         metafile = g_malloc(zs.size);
70         zf = zip_fopen_index(archive, zs.index, 0);
71         zip_fread(zf, metafile, zs.size);
72         zip_fclose(zf);
73
74         kf = g_key_file_new();
75         if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
76                 g_debug("Failed to parse metadata.");
77                 return SR_ERR;
78         }
79
80         session = session_new();
81
82         devcnt = 0;
83         capturefiles = g_ptr_array_new_with_free_func(g_free);
84         sections = g_key_file_get_groups(kf, NULL);
85         for (i = 0; sections[i]; i++) {
86                 if (!strcmp(sections[i], "global"))
87                         /* nothing really interesting in here yet */
88                         continue;
89                 if (!strncmp(sections[i], "device ", 7)) {
90                         /* device section */
91                         device = NULL;
92                         enabled_probes = 0;
93                         keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
94                         for (j = 0; keys[j]; j++) {
95                                 val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
96                                 if (!strcmp(keys[j], "capturefile")) {
97                                         device = device_new(&session_driver, devcnt, 0);
98                                         if (devcnt == 0)
99                                                 /* first device, init the plugin */
100                                                 device->plugin->init((char *)filename);
101                                         session_device_add(device);
102                                         device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTUREFILE, val);
103                                         g_ptr_array_add(capturefiles, val);
104                                 } else if (!strcmp(keys[j], "samplerate")) {
105                                         tmp_u64 = sr_parse_sizestring(val);
106                                         device->plugin->set_configuration(devcnt, SR_HWCAP_SAMPLERATE, &tmp_u64);
107                                 } else if (!strcmp(keys[j], "unitsize")) {
108                                         tmp_u64 = strtoull(val, NULL, 10);
109                                         device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTURE_UNITSIZE, &tmp_u64);
110                                 } else if (!strcmp(keys[j], "total probes")) {
111                                         total_probes = strtoull(val, NULL, 10);
112                                         device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTURE_NUM_PROBES, &total_probes);
113                                         for (p = 1; p <= total_probes; p++)
114                                                 device_probe_add(device, NULL);
115                                 } else if (!strncmp(keys[j], "probe", 5)) {
116                                         if (!device)
117                                                 continue;
118                                         enabled_probes++;
119                                         tmp_u64 = strtoul(keys[j]+5, NULL, 10);
120                                         device_probe_name(device, tmp_u64, val);
121                                 } else if (!strncmp(keys[j], "trigger", 7)) {
122                                         probenum = strtoul(keys[j]+7, NULL, 10);
123                                         device_trigger_set(device, probenum, val);
124                                 }
125                         }
126                         g_strfreev(keys);
127                         for (p = enabled_probes; p < total_probes; p++) {
128                                 probe = g_slist_nth_data(device->probes, p);
129                                 probe->enabled = FALSE;
130                         }
131                 }
132         }
133         g_strfreev(sections);
134         g_key_file_free(kf);
135
136         return SR_OK;
137 }
138
139 int session_save(char *filename)
140 {
141         GSList *l, *p, *d;
142         FILE *meta;
143         struct sr_device *device;
144         struct probe *probe;
145         struct datastore *ds;
146         struct zip *zipfile;
147         struct zip_source *versrc, *metasrc, *logicsrc;
148         int bufcnt, devcnt, tmpfile, ret, error, probecnt;
149         uint64_t samplerate;
150         char version[1], rawname[16], metafile[32], *newfn, *buf, *s;
151
152         newfn = g_malloc(strlen(filename) + 10);
153         strcpy(newfn, filename);
154         if (strstr(filename, ".sigrok") != filename+strlen(filename)-7)
155                 strcat(newfn, ".sigrok");
156
157         /* Quietly delete it first, libzip wants replace ops otherwise. */
158         unlink(newfn);
159         if (!(zipfile = zip_open(newfn, ZIP_CREATE, &error)))
160                 return SR_ERR;
161         g_free(newfn);
162
163         /* "version" */
164         version[0] = '1';
165         if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
166                 return SR_ERR;
167         if (zip_add(zipfile, "version", versrc) == -1) {
168                 g_message("error saving version into zipfile: %s",
169                           zip_strerror(zipfile));
170                 return SR_ERR;
171         }
172
173         /* init "metadata" */
174         strcpy(metafile, "sigrok-meta-XXXXXX");
175         if ((tmpfile = g_mkstemp(metafile)) == -1)
176                 return SR_ERR;
177         close(tmpfile);
178         meta = fopen(metafile, "wb");
179         fprintf(meta, "[global]\n");
180         fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
181         /* TODO: save protocol decoders used */
182
183         /* all datastores in all devices */
184         devcnt = 1;
185         for (l = session->devices; l; l = l->next) {
186                 device = l->data;
187                 /* metadata */
188                 fprintf(meta, "[device %d]\n", devcnt);
189                 if (device->plugin)
190                         fprintf(meta, "driver = %s\n", device->plugin->name);
191
192                 ds = device->datastore;
193                 if (ds) {
194                         /* metadata */
195                         fprintf(meta, "capturefile = logic-%d\n", devcnt);
196                         fprintf(meta, "unitsize = %d\n", ds->ds_unitsize);
197                         fprintf(meta, "total probes = %d\n", g_slist_length(device->probes));
198                         if (device_has_hwcap(device, SR_HWCAP_SAMPLERATE)) {
199                                 samplerate = *((uint64_t *) device->plugin->get_device_info(
200                                                 device->plugin_index, SR_DI_CUR_SAMPLERATE));
201                                 s = sr_samplerate_string(samplerate);
202                                 fprintf(meta, "samplerate = %s\n", s);
203                                 free(s);
204                         }
205                         probecnt = 1;
206                         for (p = device->probes; p; p = p->next) {
207                                 probe = p->data;
208                                 if (probe->enabled) {
209                                         if (probe->name)
210                                                 fprintf(meta, "probe%d = %s\n", probecnt, probe->name);
211                                         if (probe->trigger)
212                                                 fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger);
213                                         probecnt++;
214                                 }
215                         }
216
217                         /* dump datastore into logic-n */
218                         buf = malloc(ds->num_units * ds->ds_unitsize +
219                                    DATASTORE_CHUNKSIZE);
220                         bufcnt = 0;
221                         for (d = ds->chunklist; d; d = d->next) {
222                                 memcpy(buf + bufcnt, d->data,
223                                        DATASTORE_CHUNKSIZE);
224                                 bufcnt += DATASTORE_CHUNKSIZE;
225                         }
226                         if (!(logicsrc = zip_source_buffer(zipfile, buf,
227                                        ds->num_units * ds->ds_unitsize, TRUE)))
228                                 return SR_ERR;
229                         snprintf(rawname, 15, "logic-%d", devcnt);
230                         if (zip_add(zipfile, rawname, logicsrc) == -1)
231                                 return SR_ERR;
232                 }
233                 devcnt++;
234         }
235         fclose(meta);
236
237         if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
238                 return SR_ERR;
239         if (zip_add(zipfile, "metadata", metasrc) == -1)
240                 return SR_ERR;
241
242         if ((ret = zip_close(zipfile)) == -1) {
243                 g_message("error saving zipfile: %s", zip_strerror(zipfile));
244                 return SR_ERR;
245         }
246
247         unlink(metafile);
248
249         return SR_OK;
250 }
251