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 #include "sigrok-internal.h"
30 extern GIOChannel channels[2];
36 sr_receive_data_callback cb;
40 /* There can only be one session at a time. */
41 /* 'session' is not static, it's used elsewhere (via 'extern'). */
42 struct sr_session *session;
43 static int num_sources = 0;
45 static struct source *sources = NULL;
46 static int source_timeout = -1;
49 * Create a new session.
53 * TODO: Should return int?
54 * TODO: Should it use the file-global "session" variable or take an argument?
55 * The same question applies to all the other session functions.
57 * @return A pointer to the newly allocated session, or NULL upon errors.
59 struct sr_session *sr_session_new(void)
61 if (!(session = calloc(1, sizeof(struct sr_session)))) {
62 sr_err("session: %s: session malloc failed", __func__);
63 return NULL; /* TODO: SR_ERR_MALLOC? */
70 * Destroy the current session.
72 * This frees up all memory used by the session.
74 * TODO: Should return int?
76 void sr_session_destroy(void)
79 sr_warn("session: %s: session was NULL", __func__);
80 return; /* TODO: SR_ERR_BUG? */
83 g_slist_free(session->devices);
85 /* TODO: Error checks needed? */
87 /* TODO: Loop over protocol decoders and free them. */
95 * Remove all the devices from the current session. TODO?
97 * The session itself (i.e., the struct sr_session) is not free'd and still
98 * exists after this function returns.
102 void sr_session_device_clear(void)
105 sr_warn("session: %s: session was NULL", __func__);
106 return; /* TODO: SR_ERR_BUG? */
109 g_slist_free(session->devices);
110 session->devices = NULL;
114 * Add a device to the current session.
116 * @param device The device to add to the current session. Must not be NULL.
117 * Also, device->plugin and device->plugin->opendev must not
120 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
122 int sr_session_device_add(struct sr_device *device)
127 sr_err("session: %s: device was NULL", __func__);
131 if (!device->plugin) {
132 sr_err("session: %s: device->plugin was NULL", __func__);
136 if (!device->plugin->opendev) {
137 sr_err("session: %s: device->plugin->opendev was NULL",
143 sr_err("session: %s: session was NULL", __func__);
144 return SR_ERR; /* TODO: SR_ERR_BUG? */
147 if ((ret = device->plugin->opendev(device->plugin_index)) != SR_OK) {
148 sr_err("session: %s: opendev failed (%d)", __func__, ret);
152 session->devices = g_slist_append(session->devices, device);
158 void sr_session_pa_clear(void)
161 * The protocols are pointers to the global set of PA plugins,
162 * so don't free them.
164 g_slist_free(session->analyzers);
165 session->analyzers = NULL;
168 void sr_session_pa_add(struct sr_analyzer *an)
170 session->analyzers = g_slist_append(session->analyzers, an);
175 * Clear all datafeed callbacks in the current session.
177 * TODO: Should return int?
181 void sr_session_datafeed_callback_clear(void)
184 sr_err("session: %s: session was NULL", __func__);
185 return; /* TODO: SR_ERR_BUG? */
188 g_slist_free(session->datafeed_callbacks);
189 session->datafeed_callbacks = NULL;
193 * Add a datafeed callback to the current session.
195 * TODO: Should return int?
197 * @param callback TODO
199 void sr_session_datafeed_callback_add(sr_datafeed_callback callback)
202 sr_err("session: %s: session was NULL", __func__);
203 return; /* TODO: SR_ERR_BUG? */
206 /* TODO: Is 'callback' allowed to be NULL? */
208 session->datafeed_callbacks =
209 g_slist_append(session->datafeed_callbacks, callback);
215 static void sr_session_run_poll(void)
217 GPollFD *fds, my_gpollfd;
221 while (session->running) {
225 /* Construct g_poll()'s array. */
226 /* TODO: Check malloc return value. */
227 fds = malloc(sizeof(GPollFD) * num_sources);
228 for (i = 0; i < num_sources; i++) {
230 g_io_channel_win32_make_pollfd(&channels[0],
231 sources[i].events, &my_gpollfd);
233 my_gpollfd.fd = sources[i].fd;
234 my_gpollfd.events = sources[i].events;
239 ret = g_poll(fds, num_sources, source_timeout);
241 for (i = 0; i < num_sources; i++) {
242 if (fds[i].revents > 0 || (ret == 0
243 && source_timeout == sources[i].timeout)) {
245 * Invoke the source's callback on an event,
246 * or if the poll timeout out and this source
247 * asked for that timeout.
249 if (!sources[i].cb(fds[i].fd, fds[i].revents,
250 sources[i].user_data))
251 sr_session_source_remove(sources[i].fd);
261 * There can only be one session at a time. TODO
263 * @return SR_OK upon success, SR_ERR upon errors.
265 int sr_session_start(void)
267 struct sr_device *device;
272 sr_err("session: %s: session was NULL; a session must be "
273 "created first, before starting it.", __func__);
274 return SR_ERR; /* TODO: SR_ERR_BUG? */
277 if (!session->devices) {
278 /* TODO: Actually the case? */
279 sr_err("session: %s: session->devices was NULL; a session "
280 "cannot be started without devices.", __func__);
281 return SR_ERR; /* TODO: SR_ERR_BUG? */
284 /* TODO: Check plugin_index validity? */
286 sr_info("session: starting");
288 for (l = session->devices; l; l = l->next) {
290 /* TODO: Check for device != NULL. */
291 if ((ret = device->plugin->start_acquisition(
292 device->plugin_index, device)) != SR_OK) {
293 sr_err("session: %s: could not start an acquisition ",
294 "(%d)", __func__, ret);
299 /* TODO: What if there are multiple devices? Which return code? */
307 * TODO: Should return int.
308 * TODO: Various error checks etc.
312 void sr_session_run(void)
315 sr_err("session: %s: session was NULL; a session must be "
316 "created first, before running it.", __func__);
317 return; /* TODO: SR_ERR or SR_ERR_BUG? */
320 if (!session->devices) {
321 /* TODO: Actually the case? */
322 sr_err("session: %s: session->devices was NULL; a session "
323 "cannot be run without devices.", __func__);
324 return; /* TODO: SR_ERR or SR_ERR_BUG? */
327 sr_info("session: running");
328 session->running = TRUE;
330 /* Do we have real sources? */
331 if (num_sources == 1 && sources[0].fd == -1) {
332 /* Dummy source, freewheel over it. */
333 while (session->running)
334 sources[0].cb(-1, 0, sources[0].user_data);
336 /* Real sources, use g_poll() main loop. */
337 sr_session_run_poll();
340 /* TODO: return SR_OK; */
344 * Halt the current session.
350 void sr_session_halt(void)
353 sr_err("session: %s: session was NULL", __func__);
354 return; /* TODO: SR_ERR; or SR_ERR_BUG? */
357 sr_info("session: halting");
358 session->running = FALSE;
360 /* TODO: return SR_OK; */
364 * Stop the current session.
366 * TODO: Difference to halt?
370 void sr_session_stop(void)
372 struct sr_device *device;
376 sr_err("session: %s: session was NULL", __func__);
377 return; /* TODO: SR_ERR or SR_ERR_BUG? */
380 sr_info("session: stopping");
381 session->running = FALSE;
382 for (l = session->devices; l; l = l->next) {
384 if (device->plugin && device->plugin->stop_acquisition)
385 device->plugin->stop_acquisition(device->plugin_index, device);
388 /* TODO: return SR_OK; */
394 * TODO: Should return int?
395 * TODO: Various error checks.
397 * @param packet TODO.
399 static void datafeed_dump(struct sr_datafeed_packet *packet)
401 struct sr_datafeed_logic *logic;
403 switch (packet->type) {
405 sr_dbg("bus: received SR_DF_HEADER");
408 sr_dbg("bus: received SR_DF_TRIGGER at %lu ms",
409 packet->timeoffset / 1000000);
412 logic = packet->payload;
413 sr_dbg("bus: received SR_DF_LOGIC at %f ms duration %f ms, "
414 "%" PRIu64 " bytes", packet->timeoffset / 1000000.0,
415 packet->duration / 1000000.0, logic->length);
418 sr_dbg("bus: received SR_DF_END");
421 /* TODO: sr_err() and abort? */
422 sr_dbg("bus: received unknown packet type %d", packet->type);
430 * TODO: Should return int?
432 * @param device TODO.
433 * @param packet TODO.
435 void sr_session_bus(struct sr_device *device, struct sr_datafeed_packet *packet)
438 sr_datafeed_callback cb;
441 sr_err("session: %s: device was NULL", __func__);
442 return; /* TODO: SR_ERR_ARG */
445 if (!device->plugin) {
446 sr_err("session: %s: device->plugin was NULL", __func__);
447 return; /* TODO: SR_ERR_ARG */
451 * TODO: Send packet through PD pipe, and send the output of that to
452 * the callbacks as well.
454 for (l = session->datafeed_callbacks; l; l = l->next) {
456 /* TODO: Check for cb != NULL. */
457 datafeed_dump(packet);
461 /* TODO: return SR_OK; */
467 * TODO: Should return int?
468 * TODO: Switch to g_try_malloc0() / g_free().
469 * TODO: More error checks etc.
472 * @param events TODO.
473 * @param timeout TODO.
474 * @param callback TODO.
475 * @param user_data TODO.
477 void sr_session_source_add(int fd, int events, int timeout,
478 sr_receive_data_callback callback, void *user_data)
480 struct source *new_sources, *s;
483 sr_err("session: %s: callback was NULL", __func__);
484 return; /* TODO: SR_ERR_ARG */
487 /* Note: user_data can be NULL, that's not a bug. */
489 new_sources = calloc(1, sizeof(struct source) * (num_sources + 1));
491 sr_err("session: %s: new_sources malloc failed", __func__);
492 return; /* TODO: SR_ERR_MALLOC */
496 memcpy(new_sources, sources,
497 sizeof(struct source) * num_sources);
501 s = &new_sources[num_sources++];
504 s->timeout = timeout;
506 s->user_data = user_data;
507 sources = new_sources;
509 if (timeout != source_timeout && timeout > 0
510 && (source_timeout == -1 || timeout < source_timeout))
511 source_timeout = timeout;
513 /* TODO: return SR_OK; */
517 * Remove the source belonging to the specified file descriptor.
519 * TODO: Should return int.
520 * TODO: More error checks.
521 * TODO: Switch to g_try_malloc0() / g_free().
525 void sr_session_source_remove(int fd)
527 struct source *new_sources;
534 new_sources = calloc(1, sizeof(struct source) * num_sources);
536 sr_err("session: %s: new_sources malloc failed", __func__);
537 return; /* TODO: SR_ERR_MALLOC */
540 for (old = 0, new = 0; old < num_sources; old++) {
541 if (sources[old].fd != fd)
542 memcpy(&new_sources[new++], &sources[old],
543 sizeof(struct source));
548 sources = new_sources;
551 /* Target fd was not found. */