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