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 extern GIOChannel channels[2];
32 /* There can only be one session at a time. */
33 struct session *session;
35 /* These live in hwplugin.c, for the frontend to override. */
36 extern source_callback_add source_cb_add;
37 extern source_callback_remove source_cb_remove;
43 receive_data_callback cb;
47 struct source *sources = NULL;
48 int source_timeout = -1;
53 struct session *session_load(const char *filename)
55 struct session *session;
57 /* Avoid compiler warnings. */
60 /* TODO: Implement. */
66 struct session *session_new(void)
68 session = calloc(1, sizeof(struct session));
73 void session_destroy(void)
75 g_slist_free(session->devices);
77 /* TODO: Loop over protocol decoders and free them. */
82 void session_device_clear(void)
84 g_slist_free(session->devices);
85 session->devices = NULL;
88 int session_device_add(struct sr_device *device)
92 if (device->plugin && device->plugin->open) {
93 ret = device->plugin->open(device->plugin_index);
98 session->devices = g_slist_append(session->devices, device);
103 void session_pa_clear(void)
106 * The protocols are pointers to the global set of PA plugins,
107 * so don't free them.
109 g_slist_free(session->analyzers);
110 session->analyzers = NULL;
113 void session_pa_add(struct analyzer *an)
115 session->analyzers = g_slist_append(session->analyzers, an);
118 void session_datafeed_callback_clear(void)
120 g_slist_free(session->datafeed_callbacks);
121 session->datafeed_callbacks = NULL;
124 void session_datafeed_callback_add(datafeed_callback callback)
126 session->datafeed_callbacks =
127 g_slist_append(session->datafeed_callbacks, callback);
130 int session_start(void)
132 struct sr_device *device;
136 g_message("starting session");
137 for (l = session->devices; l; l = l->next) {
139 if ((ret = device->plugin->start_acquisition(
140 device->plugin_index, device)) != SR_OK)
147 void session_run(void)
149 GPollFD *fds, my_gpollfd;
152 g_message("running session");
153 session->running = TRUE;
155 while (session->running) {
159 /* Construct g_poll()'s array. */
160 fds = malloc(sizeof(GPollFD) * num_sources);
161 for (i = 0; i < num_sources; i++) {
163 g_io_channel_win32_make_pollfd(&channels[0],
164 sources[i].events, &my_gpollfd);
166 my_gpollfd.fd = sources[i].fd;
167 my_gpollfd.events = sources[i].events;
172 ret = g_poll(fds, num_sources, source_timeout);
174 for (i = 0; i < num_sources; i++) {
175 if (fds[i].revents > 0 || (ret == 0
176 && source_timeout == sources[i].timeout)) {
178 * Invoke the source's callback on an event,
179 * or if the poll timeout out and this source
180 * asked for that timeout.
182 sources[i].cb(fds[i].fd, fds[i].revents,
183 sources[i].user_data);
191 void session_halt(void)
194 g_message("halting session");
195 session->running = FALSE;
199 void session_stop(void)
201 struct sr_device *device;
204 g_message("stopping session");
205 session->running = FALSE;
206 for (l = session->devices; l; l = l->next) {
209 device->plugin->stop_acquisition(device->plugin_index, device);
214 void session_bus(struct sr_device *device, struct sr_datafeed_packet *packet)
217 datafeed_callback cb;
220 * TODO: Send packet through PD pipe, and send the output of that to
221 * the callbacks as well.
223 for (l = session->datafeed_callbacks; l; l = l->next) {
229 int session_save(char *filename)
233 struct sr_device *device;
235 struct datastore *ds;
237 struct zip_source *versrc, *metasrc, *logicsrc;
238 int bufcnt, devcnt, tmpfile, ret, error;
239 char version[1], rawname[16], metafile[32], *newfn, *buf;
241 newfn = g_malloc(strlen(filename) + 10);
242 strcpy(newfn, filename);
243 if (strstr(filename, ".sigrok") != filename+strlen(filename)-7)
244 strcat(newfn, ".sigrok");
246 /* Quietly delete it first, libzip wants replace ops otherwise. */
248 if (!(zipfile = zip_open(newfn, ZIP_CREATE, &error)))
254 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
256 if (zip_add(zipfile, "version", versrc) == -1) {
257 g_message("error saving version into zipfile: %s",
258 zip_strerror(zipfile));
262 /* init "metadata" */
263 strcpy(metafile, "sigrok-meta-XXXXXX");
264 if ((tmpfile = g_mkstemp(metafile)) == -1)
267 meta = fopen(metafile, "wb");
268 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
269 /* TODO: save protocol decoders used */
271 /* all datastores in all devices */
273 for (l = session->devices; l; l = l->next) {
276 fprintf(meta, "[device]\n");
278 fprintf(meta, "driver = %s\n", device->plugin->name);
280 ds = device->datastore;
283 fprintf(meta, "capturefile = logic-%d\n", devcnt);
284 for (p = device->probes; p; p = p->next) {
286 if (probe->enabled) {
287 fprintf(meta, "probe %d", probe->index);
289 fprintf(meta, " name \"%s\"", probe->name);
291 fprintf(meta, " trigger \"%s\"",
297 /* dump datastore into logic-n */
298 buf = malloc(ds->num_units * ds->ds_unitsize +
299 DATASTORE_CHUNKSIZE);
301 for (d = ds->chunklist; d; d = d->next) {
302 memcpy(buf + bufcnt, d->data,
303 DATASTORE_CHUNKSIZE);
304 bufcnt += DATASTORE_CHUNKSIZE;
306 if (!(logicsrc = zip_source_buffer(zipfile, buf,
307 ds->num_units * ds->ds_unitsize, TRUE)))
309 snprintf(rawname, 15, "logic-%d", devcnt);
310 if (zip_add(zipfile, rawname, logicsrc) == -1)
317 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
319 if (zip_add(zipfile, "metadata", metasrc) == -1)
322 if ((ret = zip_close(zipfile)) == -1) {
323 g_message("error saving zipfile: %s", zip_strerror(zipfile));
332 void session_source_add(int fd, int events, int timeout,
333 receive_data_callback callback, void *user_data)
335 struct source *new_sources, *s;
337 new_sources = calloc(1, sizeof(struct source) * (num_sources + 1));
340 memcpy(new_sources, sources,
341 sizeof(struct source) * num_sources);
345 s = &new_sources[num_sources++];
348 s->timeout = timeout;
350 s->user_data = user_data;
351 sources = new_sources;
353 if (timeout != source_timeout && timeout > 0
354 && (source_timeout == -1 || timeout < source_timeout))
355 source_timeout = timeout;
358 void session_source_remove(int fd)
360 struct source *new_sources;
366 new_sources = calloc(1, sizeof(struct source) * num_sources);
367 for (old = 0; old < num_sources; old++)
368 if (sources[old].fd != fd)
369 memcpy(&new_sources[new++], &sources[old],
370 sizeof(struct source));
374 sources = new_sources;
377 /* Target fd was not found. */