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/>.
30 /* There can only be one session at a time. */
31 struct session *session;
33 /* These live in hwplugin.c, for the frontend to override. */
34 extern source_callback_add source_cb_add;
35 extern source_callback_remove source_cb_remove;
41 receive_data_callback cb;
45 struct source *sources = NULL;
46 int source_timeout = -1;
51 struct session *session_load(const char *filename)
53 struct session *session;
55 /* Avoid compiler warnings. */
58 /* TODO: Implement. */
64 struct session *session_new(void)
66 session = calloc(1, sizeof(struct session));
71 void session_destroy(void)
73 g_slist_free(session->devices);
75 /* TODO: Loop over protocol decoders and free them. */
80 void session_device_clear(void)
82 g_slist_free(session->devices);
83 session->devices = NULL;
86 int session_device_add(struct device *device)
90 if (device->plugin && device->plugin->open) {
91 ret = device->plugin->open(device->plugin_index);
96 session->devices = g_slist_append(session->devices, device);
101 void session_pa_clear(void)
104 * The protocols are pointers to the global set of PA plugins,
105 * so don't free them.
107 g_slist_free(session->analyzers);
108 session->analyzers = NULL;
111 void session_pa_add(struct analyzer *an)
113 session->analyzers = g_slist_append(session->analyzers, an);
116 void session_datafeed_callback_clear(void)
118 g_slist_free(session->datafeed_callbacks);
119 session->datafeed_callbacks = NULL;
122 void session_datafeed_callback_add(datafeed_callback callback)
124 session->datafeed_callbacks =
125 g_slist_append(session->datafeed_callbacks, callback);
128 int session_start(void)
130 struct device *device;
134 g_message("starting session");
135 for (l = session->devices; l; l = l->next) {
137 if ((ret = device->plugin->start_acquisition(
138 device->plugin_index, device)) != SR_OK)
145 void session_run(void)
147 GPollFD *fds, my_gpollfd;
150 g_message("running session");
151 session->running = TRUE;
153 while (session->running) {
157 /* Construct g_poll()'s array. */
158 fds = malloc(sizeof(GPollFD) * num_sources);
159 for (i = 0; i < num_sources; i++) {
161 g_io_channel_win32_make_pollfd(&channels[0],
162 sources[i].events, &my_gpollfd);
164 my_gpollfd.fd = sources[i].fd;
165 my_gpollfd.events = sources[i].events;
170 ret = g_poll(fds, num_sources, source_timeout);
172 for (i = 0; i < num_sources; i++) {
173 if (fds[i].revents > 0 || (ret == 0
174 && source_timeout == sources[i].timeout)) {
176 * Invoke the source's callback on an event,
177 * or if the poll timeout out and this source
178 * asked for that timeout.
180 sources[i].cb(fds[i].fd, fds[i].revents,
181 sources[i].user_data);
189 void session_halt(void)
192 g_message("halting session");
193 session->running = FALSE;
197 void session_stop(void)
199 struct device *device;
202 g_message("stopping session");
203 session->running = FALSE;
204 for (l = session->devices; l; l = l->next) {
207 device->plugin->stop_acquisition(device->plugin_index, device);
212 void session_bus(struct device *device, struct sr_datafeed_packet *packet)
215 datafeed_callback cb;
218 * TODO: Send packet through PD pipe, and send the output of that to
219 * the callbacks as well.
221 for (l = session->datafeed_callbacks; l; l = l->next) {
227 int session_save(char *filename)
231 struct device *device;
233 struct datastore *ds;
235 struct zip_source *versrc, *metasrc, *logicsrc;
236 int bufcnt, devcnt, tmpfile, ret, error;
237 char version[1], rawname[16], metafile[32], *newfn, *buf;
239 newfn = g_malloc(strlen(filename) + 10);
240 strcpy(newfn, filename);
241 if (strstr(filename, ".sigrok") != filename+strlen(filename)-7)
242 strcat(newfn, ".sigrok");
244 /* Quietly delete it first, libzip wants replace ops otherwise. */
246 if (!(zipfile = zip_open(newfn, ZIP_CREATE, &error)))
252 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
254 if (zip_add(zipfile, "version", versrc) == -1) {
255 g_message("error saving version into zipfile: %s",
256 zip_strerror(zipfile));
260 /* init "metadata" */
261 strcpy(metafile, "sigrok-meta-XXXXXX");
262 if ((tmpfile = g_mkstemp(metafile)) == -1)
265 meta = fopen(metafile, "wb");
266 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
267 /* TODO: save protocol decoders used */
269 /* all datastores in all devices */
271 for (l = session->devices; l; l = l->next) {
274 fprintf(meta, "[device]\n");
276 fprintf(meta, "driver = %s\n", device->plugin->name);
278 ds = device->datastore;
281 fprintf(meta, "capturefile = logic-%d\n", devcnt);
282 for (p = device->probes; p; p = p->next) {
284 if (probe->enabled) {
285 fprintf(meta, "probe %d", probe->index);
287 fprintf(meta, " name \"%s\"", probe->name);
289 fprintf(meta, " trigger \"%s\"",
295 /* dump datastore into logic-n */
296 buf = malloc(ds->num_units * ds->ds_unitsize +
297 DATASTORE_CHUNKSIZE);
299 for (d = ds->chunklist; d; d = d->next) {
300 memcpy(buf + bufcnt, d->data,
301 DATASTORE_CHUNKSIZE);
302 bufcnt += DATASTORE_CHUNKSIZE;
304 if (!(logicsrc = zip_source_buffer(zipfile, buf,
305 ds->num_units * ds->ds_unitsize, TRUE)))
307 snprintf(rawname, 15, "logic-%d", devcnt);
308 if (zip_add(zipfile, rawname, logicsrc) == -1)
315 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
317 if (zip_add(zipfile, "metadata", metasrc) == -1)
320 if ((ret = zip_close(zipfile)) == -1) {
321 g_message("error saving zipfile: %s", zip_strerror(zipfile));
330 void session_source_add(int fd, int events, int timeout,
331 receive_data_callback callback, void *user_data)
333 struct source *new_sources, *s;
335 new_sources = calloc(1, sizeof(struct source) * (num_sources + 1));
338 memcpy(new_sources, sources,
339 sizeof(struct source) * num_sources);
343 s = &new_sources[num_sources++];
346 s->timeout = timeout;
348 s->user_data = user_data;
349 sources = new_sources;
351 if (timeout != source_timeout && timeout > 0
352 && (source_timeout == -1 || timeout < source_timeout))
353 source_timeout = timeout;
356 void session_source_remove(int fd)
358 struct source *new_sources;
364 new_sources = calloc(1, sizeof(struct source) * num_sources);
365 for (old = 0; old < num_sources; old++)
366 if (sources[old].fd != fd)
367 memcpy(&new_sources[new++], &sources[old],
368 sizeof(struct source));
372 sources = new_sources;
375 /* Target fd was not found. */