]> sigrok.org Git - libsigrok.git/blame - session_file.c
Code drop from DreamSourceLabs first source release.
[libsigrok.git] / session_file.c
CommitLineData
7d658874 1/*
50985c20 2 * This file is part of the libsigrok project.
7d658874 3 *
13d8e03c 4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
7d658874
BV
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>
3bbd9849 25#include <glib/gstdio.h>
545f9786 26#include "config.h" /* Needed for PACKAGE_VERSION and others. */
45c59c8b
BV
27#include "libsigrok.h"
28#include "libsigrok-internal.h"
7d658874 29
29a27196
UH
30/* Message logging helpers with subsystem-specific prefix string. */
31#define LOG_PREFIX "session-file: "
32#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
33#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
34#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
35#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
36#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
37#define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
a885ce3e 38
393fb9cb
UH
39/**
40 * @file
41 *
42 * Loading and saving libsigrok session files.
43 */
44
7b870c38 45/**
777e2035 46 * @addtogroup grp_session
7b870c38
UH
47 *
48 * @{
49 */
50
2872d21e 51extern struct sr_session *session;
c09f0b57 52extern SR_PRIV struct sr_dev_driver session_driver;
7d658874 53
9f45fb3a
UH
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 */
1a081ca6 63SR_API int sr_session_load(const char *filename)
7d658874
BV
64{
65 GKeyFile *kf;
66 GPtrArray *capturefiles;
67 struct zip *archive;
68 struct zip_file *zf;
69 struct zip_stat zs;
4d684427 70 struct sr_dev_inst *sdi;
1afe8989 71 struct sr_probe *probe;
c1864d55 72 int ret, probenum, devcnt, version, i, j;
7d658874 73 uint64_t tmp_u64, total_probes, enabled_probes, p;
c1864d55 74 char **sections, **keys, *metafile, *val, s[11];
c37d2b1b 75 char probename[SR_MAX_PROBENAME_LEN + 1];
7d658874 76
9f45fb3a 77 if (!filename) {
a885ce3e 78 sr_err("%s: filename was NULL", __func__);
9f45fb3a
UH
79 return SR_ERR_ARG;
80 }
81
ebc34738 82 if (!(archive = zip_open(filename, 0, &ret))) {
a885ce3e 83 sr_dbg("Failed to open session file: zip error %d", ret);
7d658874
BV
84 return SR_ERR;
85 }
86
7d658874 87 /* read "metadata" */
f1b296fc
BV
88 if (zip_stat(archive, "header", 0, &zs) == -1) {
89 sr_dbg("Not a valid DSLogic session file.");
7d658874
BV
90 return SR_ERR;
91 }
b53738ba
UH
92
93 if (!(metafile = g_try_malloc(zs.size))) {
a885ce3e 94 sr_err("%s: metafile malloc failed", __func__);
b53738ba
UH
95 return SR_ERR_MALLOC;
96 }
97
7d658874
BV
98 zf = zip_fopen_index(archive, zs.index, 0);
99 zip_fread(zf, metafile, zs.size);
100 zip_fclose(zf);
101
102 kf = g_key_file_new();
103 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
a885ce3e 104 sr_dbg("Failed to parse metadata.");
7d658874
BV
105 return SR_ERR;
106 }
107
2285cf9b 108 sr_session_new();
7d658874
BV
109
110 devcnt = 0;
111 capturefiles = g_ptr_array_new_with_free_func(g_free);
112 sections = g_key_file_get_groups(kf, NULL);
113 for (i = 0; sections[i]; i++) {
f1b296fc 114 if (!strcmp(sections[i], "version"))
7d658874
BV
115 /* nothing really interesting in here yet */
116 continue;
f1b296fc 117 if (!strncmp(sections[i], "header", 6)) {
7d658874 118 /* device section */
4d684427 119 sdi = NULL;
003595ac 120 enabled_probes = total_probes = 0;
7d658874
BV
121 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
122 for (j = 0; keys[j]; j++) {
123 val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
124 if (!strcmp(keys[j], "capturefile")) {
f1b296fc 125 sdi = sr_dev_inst_new(LOGIC, devcnt, SR_ST_ACTIVE, NULL, NULL, NULL);
4d684427 126 sdi->driver = &session_driver;
7d658874 127 if (devcnt == 0)
c09f0b57 128 /* first device, init the driver */
44fc870c 129 sdi->driver->init(NULL);
bd7bfe8c 130 sr_dev_open(sdi);
4d684427 131 sr_session_dev_add(sdi);
003595ac
BV
132 sdi->driver->config_set(SR_CONF_SESSIONFILE,
133 g_variant_new_string(filename), sdi);
134 sdi->driver->config_set(SR_CONF_CAPTUREFILE,
135 g_variant_new_string(val), sdi);
7d658874
BV
136 g_ptr_array_add(capturefiles, val);
137 } else if (!strcmp(keys[j], "samplerate")) {
f64c1414 138 sr_parse_sizestring(val, &tmp_u64);
003595ac
BV
139 sdi->driver->config_set(SR_CONF_SAMPLERATE,
140 g_variant_new_uint64(tmp_u64), sdi);
7d658874
BV
141 } else if (!strcmp(keys[j], "unitsize")) {
142 tmp_u64 = strtoull(val, NULL, 10);
003595ac
BV
143 sdi->driver->config_set(SR_CONF_CAPTURE_UNITSIZE,
144 g_variant_new_uint64(tmp_u64), sdi);
f1b296fc
BV
145 } else if (!strcmp(keys[j], "total samples")) {
146 tmp_u64 = strtoull(val, NULL, 10);
147 sdi->driver->config_set(SR_CONF_LIMIT_SAMPLES,
148 g_variant_new_uint64(tmp_u64), sdi);
7d658874
BV
149 } else if (!strcmp(keys[j], "total probes")) {
150 total_probes = strtoull(val, NULL, 10);
003595ac
BV
151 sdi->driver->config_set(SR_CONF_CAPTURE_NUM_PROBES,
152 g_variant_new_uint64(total_probes), sdi);
464d12c7
KS
153 for (p = 0; p < total_probes; p++) {
154 snprintf(probename, SR_MAX_PROBENAME_LEN, "%" PRIu64, p);
fb381e4d 155 if (!(probe = sr_probe_new(p, SR_PROBE_LOGIC, TRUE,
4d684427
BV
156 probename)))
157 return SR_ERR;
158 sdi->probes = g_slist_append(sdi->probes, probe);
464d12c7 159 }
7d658874 160 } else if (!strncmp(keys[j], "probe", 5)) {
4d684427 161 if (!sdi)
7d658874
BV
162 continue;
163 enabled_probes++;
164 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
fb381e4d
BV
165 /* sr_session_save() */
166 sr_dev_probe_name_set(sdi, tmp_u64 - 1, val);
7d658874
BV
167 } else if (!strncmp(keys[j], "trigger", 7)) {
168 probenum = strtoul(keys[j]+7, NULL, 10);
4d684427 169 sr_dev_trigger_set(sdi, probenum, val);
7d658874
BV
170 }
171 }
172 g_strfreev(keys);
fb381e4d 173 /* Disable probes not specifically listed. */
003595ac
BV
174 if (total_probes)
175 for (p = enabled_probes; p < total_probes; p++)
176 sr_dev_probe_enable(sdi, p, FALSE);
7d658874 177 }
4d684427 178 devcnt++;
7d658874
BV
179 }
180 g_strfreev(sections);
181 g_key_file_free(kf);
182
183 return SR_OK;
184}
185
9f45fb3a
UH
186/**
187 * Save the current session to the specified file.
188 *
18bc2704 189 * @param filename The name of the filename to save the current session as.
9f45fb3a 190 * Must not be NULL.
056be071 191 * @param sdi The device instance from which the data was captured.
18bc2704
BV
192 * @param buf The data to be saved.
193 * @param unitsize The number of bytes per sample.
194 * @param units The number of samples.
9f45fb3a
UH
195 *
196 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
197 * upon other errors.
198 */
18bc2704
BV
199SR_API int sr_session_save(const char *filename, const struct sr_dev_inst *sdi,
200 unsigned char *buf, int unitsize, int units)
7d658874 201{
f1b296fc
BV
202 GSList *l;
203 GVariant *gvar;
204 FILE *meta;
205 struct sr_probe *probe;
206 struct zip *zipfile;
207 struct zip_source *versrc, *metasrc, *logicsrc;
208 int tmpfile, ret, probecnt;
209 uint64_t samplerate;
210 char rawname[16], metafile[32], *s;
7d658874 211
9f45fb3a 212 if (!filename) {
a885ce3e 213 sr_err("%s: filename was NULL", __func__);
9f45fb3a
UH
214 return SR_ERR_ARG;
215 }
216
7d658874 217 /* Quietly delete it first, libzip wants replace ops otherwise. */
4a1b18f8 218 unlink(filename);
ebc34738 219 if (!(zipfile = zip_open(filename, ZIP_CREATE, &ret)))
7d658874 220 return SR_ERR;
7d658874 221
f1b296fc
BV
222 /* init "metadata" */
223 strcpy(metafile, "DSLogic-meta-XXXXXX");
224 if ((tmpfile = g_mkstemp(metafile)) == -1)
225 return SR_ERR;
226 close(tmpfile);
227 meta = g_fopen(metafile, "wb");
228 fprintf(meta, "[version]\n");
229 fprintf(meta, "DSLogic version = %s\n", PACKAGE_VERSION);
7d658874 230
f1b296fc
BV
231 /* metadata */
232 fprintf(meta, "[header]\n");
233 if (sdi->driver)
234 fprintf(meta, "driver = %s\n", sdi->driver->name);
7d658874 235
f1b296fc
BV
236 /* metadata */
237 fprintf(meta, "capturefile = data\n");
238 fprintf(meta, "unitsize = %d\n", unitsize);
239 fprintf(meta, "total samples = %d\n", units);
240 fprintf(meta, "total probes = %d\n", g_slist_length(sdi->probes));
241 if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
242 if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
243 &gvar, sdi) == SR_OK) {
244 samplerate = g_variant_get_uint64(gvar);
245 s = sr_samplerate_string(samplerate);
246 fprintf(meta, "samplerate = %s\n", s);
247 g_free(s);
248 g_variant_unref(gvar);
249 }
250 }
251 probecnt = 1;
252 for (l = sdi->probes; l; l = l->next) {
253 probe = l->data;
254 if (probe->enabled) {
255 if (probe->name)
256 fprintf(meta, "probe%d = %s\n", probecnt, probe->name);
257 if (probe->trigger)
258 fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger);
259 probecnt++;
260 }
261 }
7d658874 262
f1b296fc
BV
263 if (!(logicsrc = zip_source_buffer(zipfile, buf,
264 units * unitsize, FALSE)))
265 return SR_ERR;
266 snprintf(rawname, 15, "data");
267 if (zip_add(zipfile, rawname, logicsrc) == -1)
268 return SR_ERR;
269 fclose(meta);
7d658874 270
f1b296fc
BV
271 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
272 return SR_ERR;
273 if (zip_add(zipfile, "header", metasrc) == -1)
274 return SR_ERR;
7d658874 275
f1b296fc
BV
276 if ((ret = zip_close(zipfile)) == -1) {
277 sr_info("error saving zipfile: %s", zip_strerror(zipfile));
278 return SR_ERR;
279 }
7d658874 280
f1b296fc 281 unlink(metafile);
7d658874 282
f1b296fc 283 return SR_OK;
7d658874 284}
7b870c38
UH
285
286/** @} */