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;
30 struct session *session_load(const char *filename)
32 struct session *session;
34 /* TODO: Implement. */
43 struct session *session_new(void)
45 session = calloc(1, sizeof(struct session));
50 void session_destroy(void)
52 g_slist_free(session->devices);
54 /* TODO: Loop over protocols and free them. */
59 void session_device_clear(void)
61 g_slist_free(session->devices);
62 session->devices = NULL;
65 int session_device_add(struct device *device)
69 ret = device->plugin->open(device->plugin_index);
71 session->devices = g_slist_append(session->devices, device);
76 void session_pa_clear(void)
79 * The protocols are pointers to the global set of PA plugins,
82 g_slist_free(session->analyzers);
83 session->analyzers = NULL;
86 void session_pa_add(struct analyzer *an)
88 session->analyzers = g_slist_append(session->analyzers, an);
91 void session_datafeed_callback_clear(void)
93 g_slist_free(session->datafeed_callbacks);
94 session->datafeed_callbacks = NULL;
97 void session_datafeed_callback_add(datafeed_callback callback)
99 session->datafeed_callbacks =
100 g_slist_append(session->datafeed_callbacks, callback);
103 int session_start(void)
105 struct device *device;
109 g_message("starting acquisition");
110 for (l = session->devices; l; l = l->next) {
112 if ((ret = device->plugin->start_acquisition(
113 device->plugin_index, device)) != SIGROK_OK)
120 void session_stop(void)
122 struct device *device;
125 g_message("stopping acquisition");
126 for (l = session->devices; l; l = l->next) {
128 device->plugin->stop_acquisition(device->plugin_index, device);
132 void session_bus(struct device *device, struct datafeed_packet *packet)
135 datafeed_callback cb;
138 * TODO: Send packet through PA pipe, and send the output of that to
139 * the callbacks as well.
141 for (l = session->datafeed_callbacks; l; l = l->next) {
147 void make_metadata(char *filename)
150 struct device *device;
155 f = fopen(filename, "wb");
161 for (l = session->devices; l; l = l->next) {
163 fprintf(f, "[device]\n");
164 fprintf(f, "driver = %s\n", device->plugin->name);
166 if (device->datastore)
167 fprintf(f, "capturefile = raw-%d\n", devcnt);
169 for (p = device->probes; p; p = p->next) {
171 if (probe->enabled) {
172 fprintf(f, "probe %d", probe->index);
174 fprintf(f, " name \"%s\"", probe->name);
176 fprintf(f, " trigger \"%s\"",
184 /* TODO: Protocol analyzers */
189 int session_save(char *filename)
192 struct device *device;
193 struct datastore *ds;
195 struct zip_source *src;
196 int bufcnt, devcnt, tmpfile, ret, error;
197 char version[1], rawname[16], metafile[32], *buf;
199 /* Quietly delete it first, libzip wants replace ops otherwise. */
202 if (!(zipfile = zip_open(filename, ZIP_CREATE, &error)))
207 if (!(src = zip_source_buffer(zipfile, version, 1, 0)))
209 if (zip_add(zipfile, "version", src) == -1) {
210 g_message("error saving version into zipfile: %s",
211 zip_strerror(zipfile));
216 strcpy(metafile, "sigrok-meta-XXXXXX");
217 if ((tmpfile = g_mkstemp(metafile)) == -1)
220 make_metadata(metafile);
221 if (!(src = zip_source_file(zipfile, metafile, 0, -1)))
223 if (zip_add(zipfile, "metadata", src) == -1)
229 for (l = session->devices; l; l = l->next) {
231 ds = device->datastore;
233 buf = malloc(ds->num_units * ds->ds_unitsize +
234 DATASTORE_CHUNKSIZE);
236 for (d = ds->chunklist; d; d = d->next) {
237 memcpy(buf + bufcnt, d->data,
238 DATASTORE_CHUNKSIZE);
239 bufcnt += DATASTORE_CHUNKSIZE;
241 if (!(src = zip_source_buffer(zipfile, buf,
242 ds->num_units * ds->ds_unitsize, TRUE)))
244 snprintf(rawname, 15, "raw-%d", devcnt);
245 if (zip_add(zipfile, rawname, src) == -1)
251 if ((ret = zip_close(zipfile)) == -1) {
252 g_message("error saving zipfile: %s", zip_strerror(zipfile));