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