2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
27 /* there can only be one session at a time */
28 struct session *session;
31 struct session *session_load(char *filename)
33 struct session *session;
42 struct session *session_new(void)
45 session = calloc(1, sizeof(struct session));
51 void session_destroy(void)
54 g_slist_free(session->devices);
56 /* TODO: loop over protocols and free them */
63 void session_device_clear(void)
66 g_slist_free(session->devices);
67 session->devices = NULL;
72 int session_device_add(struct device *device)
76 ret = device->plugin->open(device->plugin_index);
78 session->devices = g_slist_append(session->devices, device);
84 void session_pa_clear(void)
87 /* the protocols are pointers to the global set of PA plugins, so don't free them */
88 g_slist_free(session->analyzers);
89 session->analyzers = NULL;
94 void session_pa_add(struct analyzer *an)
97 session->analyzers = g_slist_append(session->analyzers, an);
102 void session_datafeed_callback_clear(void)
105 g_slist_free(session->datafeed_callbacks);
106 session->datafeed_callbacks = NULL;
111 void session_datafeed_callback_add(datafeed_callback callback)
114 session->datafeed_callbacks = g_slist_append(session->datafeed_callbacks, callback);
119 int session_start(void)
121 struct device *device;
125 g_message("starting acquisition");
126 for(l = session->devices; l; l = l->next)
129 if( (ret = device->plugin->start_acquisition(device->plugin_index, device)) != SIGROK_OK)
137 void session_stop(void)
139 struct device *device;
142 g_message("stopping acquisition");
143 for(l = session->devices; l; l = l->next)
146 device->plugin->stop_acquisition(device->plugin_index, device);
152 void session_bus(struct device *device, struct datafeed_packet *packet)
155 datafeed_callback cb;
157 /* TODO: send packet through PA pipe, and send the output of that to
158 * the callbacks as well
161 for(l = session->datafeed_callbacks; l; l = l->next)
170 void make_metadata(char *filename)
173 struct device *device;
178 f = fopen(filename, "wb");
184 for(l = session->devices; l; l = l->next) {
186 fprintf(f, "[device]\n");
187 fprintf(f, "driver = %s\n", device->plugin->name);
188 if(device->datastore)
189 fprintf(f, "capturefile = raw-%d\n", devcnt);
190 for(p = device->probes; p; p = p->next) {
194 fprintf(f, "probe %d", probe->index);
196 fprintf(f, " name \"%s\"", probe->name);
198 fprintf(f, " trigger \"%s\"", probe->trigger);
205 /* TODO: protocol analyzers */
212 int session_save(char *filename)
215 struct device *device;
216 struct datastore *ds;
218 struct zip_source *src;
219 int bufcnt, devcnt, tmpfile, ret, error;
220 char version[1], rawname[16], metafile[32], *buf;
222 /* quietly delete it first, libzip wants replace ops otherwise */
225 if( !(zipfile = zip_open(filename, ZIP_CREATE, &error)) )
230 if( !(src = zip_source_buffer(zipfile, version, 1, 0)) )
232 if(zip_add(zipfile, "version", src) == -1) {
233 g_message("error saving version into zipfile: %s", zip_strerror(zipfile));
238 strcpy(metafile, "sigrok-meta-XXXXXX");
239 if( (tmpfile = g_mkstemp(metafile)) == -1)
242 make_metadata(metafile);
243 if( !(src = zip_source_file(zipfile, metafile, 0, -1)) )
245 if(zip_add(zipfile, "metadata", src) == -1)
251 for(l = session->devices; l; l = l->next) {
253 ds = device->datastore;
255 buf = malloc(ds->num_units * ds->ds_unitsize + DATASTORE_CHUNKSIZE);
257 for(d = ds->chunklist; d; d = d->next) {
258 memcpy(buf + bufcnt, d->data, DATASTORE_CHUNKSIZE);
259 bufcnt += DATASTORE_CHUNKSIZE;
261 if( !(src = zip_source_buffer(zipfile, buf, ds->num_units * ds->ds_unitsize, TRUE)) )
263 snprintf(rawname, 15, "raw-%d", devcnt);
264 if(zip_add(zipfile, rawname, src) == -1)
270 if( (ret = zip_close(zipfile)) == -1) {
271 g_message("error saving zipfile: %s", zip_strerror(zipfile));