2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 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/>.
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
28 /* Message logging helpers with driver-specific prefix string. */
29 #define DRIVER_LOG_DOMAIN "session: "
30 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
31 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
32 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
40 * Creating, using, or destroying libsigrok sessions.
44 * @defgroup grp_session Session handling
46 * Creating, using, or destroying libsigrok sessions.
53 sr_receive_data_callback_t cb;
56 /* This is used to keep track of the object (fd, pollfd or channel) which is
57 * being polled and will be used to match the source when removing it again.
62 /* There can only be one session at a time. */
63 /* 'session' is not static, it's used elsewhere (via 'extern'). */
64 struct sr_session *session;
67 * Create a new session.
69 * @todo Should it use the file-global "session" variable or take an argument?
70 * The same question applies to all the other session functions.
72 * @return A pointer to the newly allocated session, or NULL upon errors.
74 SR_API struct sr_session *sr_session_new(void)
76 if (!(session = g_try_malloc0(sizeof(struct sr_session)))) {
77 sr_err("Session malloc failed.");
81 session->source_timeout = -1;
87 * Destroy the current session.
89 * This frees up all memory used by the session.
91 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
93 SR_API int sr_session_destroy(void)
96 sr_err("%s: session was NULL", __func__);
100 sr_session_dev_remove_all();
102 /* TODO: Error checks needed? */
110 static void sr_dev_close(struct sr_dev_inst *sdi)
112 if (sdi->driver && sdi->driver->dev_close)
113 sdi->driver->dev_close(sdi);
117 * Remove all the devices from the current session.
119 * The session itself (i.e., the struct sr_session) is not free'd and still
120 * exists after this function returns.
122 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
124 SR_API int sr_session_dev_remove_all(void)
127 sr_err("%s: session was NULL", __func__);
131 g_slist_free_full(session->devs, (GDestroyNotify)sr_dev_close);
132 session->devs = NULL;
138 * Add a device instance to the current session.
140 * @param sdi The device instance to add to the current session. Must not
141 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
144 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
146 SR_API int sr_session_dev_add(const struct sr_dev_inst *sdi)
151 sr_err("%s: sdi was NULL", __func__);
156 sr_err("%s: session was NULL", __func__);
160 /* If sdi->driver is NULL, this is a virtual device. */
162 sr_dbg("%s: sdi->driver was NULL, this seems to be "
163 "a virtual device; continuing", __func__);
164 /* Just add the device, don't run dev_open(). */
165 session->devs = g_slist_append(session->devs, (gpointer)sdi);
169 /* sdi->driver is non-NULL (i.e. we have a real device). */
170 if (!sdi->driver->dev_open) {
171 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
175 if ((ret = sdi->driver->dev_open((struct sr_dev_inst *)sdi)) != SR_OK) {
176 sr_err("%s: dev_open failed (%d)", __func__, ret);
180 session->devs = g_slist_append(session->devs, (gpointer)sdi);
186 * Remove all datafeed callbacks in the current session.
188 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
190 SR_API int sr_session_datafeed_callback_remove_all(void)
193 sr_err("%s: session was NULL", __func__);
197 g_slist_free(session->datafeed_callbacks);
198 session->datafeed_callbacks = NULL;
204 * Add a datafeed callback to the current session.
206 * @param cb Function to call when a chunk of data is received.
209 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
211 SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
214 sr_err("%s: session was NULL", __func__);
219 sr_err("%s: cb was NULL", __func__);
223 session->datafeed_callbacks =
224 g_slist_append(session->datafeed_callbacks, cb);
229 static int sr_session_run_poll(void)
234 while (session->num_sources > 0) {
235 ret = g_poll(session->pollfds, session->num_sources,
236 session->source_timeout);
237 for (i = 0; i < session->num_sources; i++) {
238 if (session->pollfds[i].revents > 0 || (ret == 0
239 && session->source_timeout == session->sources[i].timeout)) {
241 * Invoke the source's callback on an event,
242 * or if the poll timed out and this source
243 * asked for that timeout.
245 if (!session->sources[i].cb(session->pollfds[i].fd,
246 session->pollfds[i].revents,
247 session->sources[i].cb_data))
248 sr_session_source_remove(session->sources[i].poll_object);
259 * There can only be one session at a time.
261 * @return SR_OK upon success, SR_ERR upon errors.
263 SR_API int sr_session_start(void)
265 struct sr_dev_inst *sdi;
270 sr_err("%s: session was NULL; a session must be "
271 "created before starting it.", __func__);
275 if (!session->devs) {
276 sr_err("%s: session->devs was NULL; a session "
277 "cannot be started without devices.", __func__);
281 sr_info("session: starting");
284 for (l = session->devs; l; l = l->next) {
286 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
287 sr_err("%s: could not start an acquisition "
288 "(%d)", __func__, ret);
293 /* TODO: What if there are multiple devices? Which return code? */
301 * @return SR_OK upon success, SR_ERR_BUG upon errors.
303 SR_API int sr_session_run(void)
306 sr_err("%s: session was NULL; a session must be "
307 "created first, before running it.", __func__);
311 if (!session->devs) {
312 /* TODO: Actually the case? */
313 sr_err("%s: session->devs was NULL; a session "
314 "cannot be run without devices.", __func__);
320 /* Do we have real sources? */
321 if (session->num_sources == 1 && session->pollfds[0].fd == -1) {
322 /* Dummy source, freewheel over it. */
323 while (session->num_sources)
324 session->sources[0].cb(-1, 0, session->sources[0].cb_data);
326 /* Real sources, use g_poll() main loop. */
327 sr_session_run_poll();
334 * Halt the current session.
336 * This function is deprecated and should not be used in new code, use
337 * sr_session_stop() instead. The behaviour of this function is identical to
340 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
342 SR_API int sr_session_halt(void)
344 return sr_session_stop();
348 * Stop the current session.
350 * The current session is stopped immediately, with all acquisition sessions
351 * being stopped and hardware drivers cleaned up.
353 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
355 SR_API int sr_session_stop(void)
357 struct sr_dev_inst *sdi;
361 sr_err("%s: session was NULL", __func__);
365 sr_info("Stopping.");
367 for (l = session->devs; l; l = l->next) {
370 if (sdi->driver->dev_acquisition_stop)
371 sdi->driver->dev_acquisition_stop(sdi, sdi);
376 * Some sources may not be necessarily associated with a device.
377 * Those sources may still be present even after stopping all devices.
378 * We need to make sure all sources are removed, or we risk running the
379 * session in an infinite loop.
381 while (session->num_sources)
382 sr_session_source_remove(session->sources[0].poll_object);
390 * @param packet The packet to show debugging information for.
392 static void datafeed_dump(const struct sr_datafeed_packet *packet)
394 const struct sr_datafeed_logic *logic;
395 const struct sr_datafeed_analog *analog;
397 switch (packet->type) {
399 sr_dbg("bus: Received SR_DF_HEADER packet.");
402 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
405 sr_dbg("bus: Received SR_DF_META packet.");
408 logic = packet->payload;
409 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes).",
413 analog = packet->payload;
414 sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).",
415 analog->num_samples);
418 sr_dbg("bus: Received SR_DF_END packet.");
420 case SR_DF_FRAME_BEGIN:
421 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
423 case SR_DF_FRAME_END:
424 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
427 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
433 * Send a packet to whatever is listening on the datafeed bus.
435 * Hardware drivers use this to send a data packet to the frontend.
438 * @param packet The datafeed packet to send to the session bus.
440 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
444 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
445 const struct sr_datafeed_packet *packet)
448 sr_datafeed_callback_t cb;
451 sr_err("%s: sdi was NULL", __func__);
456 sr_err("%s: packet was NULL", __func__);
460 for (l = session->datafeed_callbacks; l; l = l->next) {
461 if (sr_log_loglevel_get() >= SR_LOG_DBG)
462 datafeed_dump(packet);
471 * Add an event source for a file descriptor.
473 * @param pollfd The GPollFD.
474 * @param timeout Max time to wait before the callback is called, ignored if 0.
475 * @param cb Callback function to add. Must not be NULL.
476 * @param cb_data Data for the callback function. Can be NULL.
477 * @param poll_object TODO.
479 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
480 * SR_ERR_MALLOC upon memory allocation errors.
482 static int _sr_session_source_add(GPollFD *pollfd, int timeout,
483 sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object)
485 struct source *new_sources, *s;
486 GPollFD *new_pollfds;
489 sr_err("%s: cb was NULL", __func__);
493 /* Note: cb_data can be NULL, that's not a bug. */
495 new_pollfds = g_try_realloc(session->pollfds,
496 sizeof(GPollFD) * (session->num_sources + 1));
498 sr_err("%s: new_pollfds malloc failed", __func__);
499 return SR_ERR_MALLOC;
502 new_sources = g_try_realloc(session->sources, sizeof(struct source) *
503 (session->num_sources + 1));
505 sr_err("%s: new_sources malloc failed", __func__);
506 return SR_ERR_MALLOC;
509 new_pollfds[session->num_sources] = *pollfd;
510 s = &new_sources[session->num_sources++];
511 s->timeout = timeout;
513 s->cb_data = cb_data;
514 s->poll_object = poll_object;
515 session->pollfds = new_pollfds;
516 session->sources = new_sources;
518 if (timeout != session->source_timeout && timeout > 0
519 && (session->source_timeout == -1 || timeout < session->source_timeout))
520 session->source_timeout = timeout;
526 * Add an event source for a file descriptor.
528 * @param fd The file descriptor.
529 * @param events Events to check for.
530 * @param timeout Max time to wait before the callback is called, ignored if 0.
531 * @param cb Callback function to add. Must not be NULL.
532 * @param cb_data Data for the callback function. Can be NULL.
534 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
535 * SR_ERR_MALLOC upon memory allocation errors.
537 SR_API int sr_session_source_add(int fd, int events, int timeout,
538 sr_receive_data_callback_t cb, void *cb_data)
545 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd);
549 * Add an event source for a GPollFD.
551 * @param pollfd The GPollFD.
552 * @param timeout Max time to wait before the callback is called, ignored if 0.
553 * @param cb Callback function to add. Must not be NULL.
554 * @param cb_data Data for the callback function. Can be NULL.
556 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
557 * SR_ERR_MALLOC upon memory allocation errors.
559 SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout,
560 sr_receive_data_callback_t cb, void *cb_data)
562 return _sr_session_source_add(pollfd, timeout, cb,
563 cb_data, (gintptr)pollfd);
567 * Add an event source for a GIOChannel.
569 * @param channel The GIOChannel.
570 * @param events Events to poll on.
571 * @param timeout Max time to wait before the callback is called, ignored if 0.
572 * @param cb Callback function to add. Must not be NULL.
573 * @param cb_data Data for the callback function. Can be NULL.
575 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
576 * SR_ERR_MALLOC upon memory allocation errors.
578 SR_API int sr_session_source_add_channel(GIOChannel *channel, int events,
579 int timeout, sr_receive_data_callback_t cb, void *cb_data)
584 g_io_channel_win32_make_pollfd(channel, events, &p);
586 p.fd = g_io_channel_unix_get_fd(channel);
590 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel);
594 * Remove the source belonging to the specified channel.
596 * @todo Add more error checks and logging.
598 * @param channel The channel for which the source should be removed.
600 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
601 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
604 static int _sr_session_source_remove(gintptr poll_object)
606 struct source *new_sources;
607 GPollFD *new_pollfds;
610 if (!session->sources || !session->num_sources) {
611 sr_err("%s: sources was NULL", __func__);
615 for (old = 0; old < session->num_sources; old++) {
616 if (session->sources[old].poll_object == poll_object)
620 /* fd not found, nothing to do */
621 if (old == session->num_sources)
624 session->num_sources -= 1;
626 if (old != session->num_sources) {
627 memmove(&session->pollfds[old], &session->pollfds[old+1],
628 (session->num_sources - old) * sizeof(GPollFD));
629 memmove(&session->sources[old], &session->sources[old+1],
630 (session->num_sources - old) * sizeof(struct source));
633 new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
634 if (!new_pollfds && session->num_sources > 0) {
635 sr_err("%s: new_pollfds malloc failed", __func__);
636 return SR_ERR_MALLOC;
639 new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
640 if (!new_sources && session->num_sources > 0) {
641 sr_err("%s: new_sources malloc failed", __func__);
642 return SR_ERR_MALLOC;
645 session->pollfds = new_pollfds;
646 session->sources = new_sources;
652 * Remove the source belonging to the specified file descriptor.
654 * @param fd The file descriptor for which the source should be removed.
656 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
657 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
660 SR_API int sr_session_source_remove(int fd)
662 return _sr_session_source_remove((gintptr)fd);
666 * Remove the source belonging to the specified poll descriptor.
668 * @param pollfd The poll descriptor for which the source should be removed.
670 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
671 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
674 SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd)
676 return _sr_session_source_remove((gintptr)pollfd);
680 * Remove the source belonging to the specified channel.
682 * @param channel The channel for which the source should be removed.
684 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
685 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
688 SR_API int sr_session_source_remove_channel(GIOChannel *channel)
690 return _sr_session_source_remove((gintptr)channel);