2 * This file is part of the libsigrok 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"
29 #define LOG_PREFIX "session"
35 * Creating, using, or destroying libsigrok sessions.
39 * @defgroup grp_session Session handling
41 * Creating, using, or destroying libsigrok sessions.
48 sr_receive_data_callback cb;
51 /* This is used to keep track of the object (fd, pollfd or channel) which is
52 * being polled and will be used to match the source when removing it again.
57 struct datafeed_callback {
58 sr_datafeed_callback cb;
63 * Create a new session.
64 * Currently, there can be only one session at a time within the same process.
66 * @retval SR_OK Success.
67 * @retval SR_ERR_BUG A session exists already.
71 SR_API int sr_session_new(struct sr_session **new_session)
73 struct sr_session *session;
75 session = g_malloc0(sizeof(struct sr_session));
77 session->source_timeout = -1;
78 session->running = FALSE;
79 session->abort_session = FALSE;
80 g_mutex_init(&session->stop_mutex);
82 *new_session = session;
89 * This frees up all memory used by the session.
91 * @retval SR_OK Success.
92 * @retval SR_ERR_ARG Invalid session passed.
96 SR_API int sr_session_destroy(struct sr_session *session)
99 sr_err("%s: session was NULL", __func__);
103 sr_session_dev_remove_all(session);
104 g_mutex_clear(&session->stop_mutex);
105 if (session->trigger)
106 sr_trigger_free(session->trigger);
114 * Remove all the devices from a session.
116 * The session itself (i.e., the struct sr_session) is not free'd and still
117 * exists after this function returns.
119 * @retval SR_OK Success.
120 * @retval SR_ERR_BUG Invalid session passed.
124 SR_API int sr_session_dev_remove_all(struct sr_session *session)
126 struct sr_dev_inst *sdi;
130 sr_err("%s: session was NULL", __func__);
134 for (l = session->devs; l; l = l->next) {
135 sdi = (struct sr_dev_inst *) l->data;
139 g_slist_free(session->devs);
140 session->devs = NULL;
146 * Add a device instance to a session.
148 * @param sdi The device instance to add to a session. Must not
149 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
152 * @retval SR_OK Success.
153 * @retval SR_ERR_ARG Invalid argument.
157 SR_API int sr_session_dev_add(struct sr_session *session,
158 struct sr_dev_inst *sdi)
163 sr_err("%s: sdi was NULL", __func__);
168 sr_err("%s: session was NULL", __func__);
172 /* If sdi->session is not NULL, the device is already in this or
173 * another session. */
175 sr_err("%s: already assigned to session", __func__);
179 /* If sdi->driver is NULL, this is a virtual device. */
181 sr_dbg("%s: sdi->driver was NULL, this seems to be "
182 "a virtual device; continuing", __func__);
183 /* Just add the device, don't run dev_open(). */
184 session->devs = g_slist_append(session->devs, (gpointer)sdi);
185 sdi->session = session;
189 /* sdi->driver is non-NULL (i.e. we have a real device). */
190 if (!sdi->driver->dev_open) {
191 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
195 session->devs = g_slist_append(session->devs, (gpointer)sdi);
196 sdi->session = session;
198 if (session->running) {
199 /* Adding a device to a running session. Commit settings
200 * and start acquisition on that device now. */
201 if ((ret = sr_config_commit(sdi)) != SR_OK) {
202 sr_err("Failed to commit device settings before "
203 "starting acquisition in running session (%s)",
207 if ((ret = sdi->driver->dev_acquisition_start(sdi,
208 (void *)sdi)) != SR_OK) {
209 sr_err("Failed to start acquisition of device in "
210 "running session (%s)", sr_strerror(ret));
219 * List all device instances attached to a session.
221 * @param devlist A pointer where the device instance list will be
222 * stored on return. If no devices are in the session,
223 * this will be NULL. Each element in the list points
224 * to a struct sr_dev_inst *.
225 * The list must be freed by the caller, but not the
226 * elements pointed to.
228 * @retval SR_OK Success.
229 * @retval SR_ERR_ARG Invalid argument.
233 SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist)
241 *devlist = g_slist_copy(session->devs);
247 * Remove all datafeed callbacks in a session.
249 * @retval SR_OK Success.
250 * @retval SR_ERR_ARG Invalid session passed.
254 SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session)
257 sr_err("%s: session was NULL", __func__);
261 g_slist_free_full(session->datafeed_callbacks, g_free);
262 session->datafeed_callbacks = NULL;
268 * Add a datafeed callback to a session.
270 * @param cb Function to call when a chunk of data is received.
272 * @param cb_data Opaque pointer passed in by the caller.
274 * @retval SR_OK Success.
275 * @retval SR_ERR_BUG No session exists.
279 SR_API int sr_session_datafeed_callback_add(struct sr_session *session,
280 sr_datafeed_callback cb, void *cb_data)
282 struct datafeed_callback *cb_struct;
285 sr_err("%s: session was NULL", __func__);
290 sr_err("%s: cb was NULL", __func__);
294 if (!(cb_struct = g_try_malloc0(sizeof(struct datafeed_callback))))
295 return SR_ERR_MALLOC;
298 cb_struct->cb_data = cb_data;
300 session->datafeed_callbacks =
301 g_slist_append(session->datafeed_callbacks, cb_struct);
306 SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session)
308 return session->trigger;
311 SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig)
313 session->trigger = trig;
319 * Call every device in the current session's callback.
321 * For sessions not driven by select loops such as sr_session_run(),
322 * but driven by another scheduler, this can be used to poll the devices
323 * from within that scheduler.
325 * @param block If TRUE, this call will wait for any of the session's
326 * sources to fire an event on the file descriptors, or
327 * any of their timeouts to activate. In other words, this
328 * can be used as a select loop.
329 * If FALSE, all sources have their callback run, regardless
330 * of file descriptor or timeout status.
332 * @retval SR_OK Success.
333 * @retval SR_ERR Error occured.
335 static int sr_session_iteration(struct sr_session *session, gboolean block)
340 ret = g_poll(session->pollfds, session->num_sources,
341 block ? session->source_timeout : 0);
342 for (i = 0; i < session->num_sources; i++) {
343 if (session->pollfds[i].revents > 0 || (ret == 0
344 && session->source_timeout == session->sources[i].timeout)) {
346 * Invoke the source's callback on an event,
347 * or if the poll timed out and this source
348 * asked for that timeout.
350 if (!session->sources[i].cb(session->pollfds[i].fd,
351 session->pollfds[i].revents,
352 session->sources[i].cb_data))
353 sr_session_source_remove(session,
354 session->sources[i].poll_object);
357 * We want to take as little time as possible to stop
358 * the session if we have been told to do so. Therefore,
359 * we check the flag after processing every source, not
360 * just once per main event loop.
362 g_mutex_lock(&session->stop_mutex);
363 if (session->abort_session) {
364 sr_session_stop_sync(session);
365 /* But once is enough. */
366 session->abort_session = FALSE;
368 g_mutex_unlock(&session->stop_mutex);
375 static int verify_trigger(struct sr_trigger *trigger)
377 struct sr_trigger_stage *stage;
378 struct sr_trigger_match *match;
381 if (!trigger->stages) {
382 sr_err("No trigger stages defined.");
386 sr_spew("Checking trigger:");
387 for (l = trigger->stages; l; l = l->next) {
389 if (!stage->matches) {
390 sr_err("Stage %d has no matches defined.", stage->stage);
393 for (m = stage->matches; m; m = m->next) {
395 if (!match->channel) {
396 sr_err("Stage %d match has no channel.", stage->stage);
400 sr_err("Stage %d match is not defined.", stage->stage);
403 sr_spew("Stage %d match on channel %s, match %d", stage->stage,
404 match->channel->name, match->match);
413 * @retval SR_OK Success.
414 * @retval SR_ERR_ARG Invalid session passed.
418 SR_API int sr_session_start(struct sr_session *session)
420 struct sr_dev_inst *sdi;
425 sr_err("%s: session was NULL", __func__);
429 if (!session->devs) {
430 sr_err("%s: session->devs was NULL; a session "
431 "cannot be started without devices.", __func__);
435 if (session->trigger && verify_trigger(session->trigger) != SR_OK)
438 sr_info("Starting.");
441 for (l = session->devs; l; l = l->next) {
443 if ((ret = sr_config_commit(sdi)) != SR_OK) {
444 sr_err("Failed to commit device settings before "
445 "starting acquisition (%s)", sr_strerror(ret));
448 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
449 sr_err("%s: could not start an acquisition "
450 "(%s)", __func__, sr_strerror(ret));
455 /* TODO: What if there are multiple devices? Which return code? */
463 * @retval SR_OK Success.
464 * @retval SR_ERR_ARG Invalid session passed.
468 SR_API int sr_session_run(struct sr_session *session)
471 sr_err("%s: session was NULL", __func__);
475 if (!session->devs) {
476 /* TODO: Actually the case? */
477 sr_err("%s: session->devs was NULL; a session "
478 "cannot be run without devices.", __func__);
481 session->running = TRUE;
485 /* Do we have real sources? */
486 if (session->num_sources == 1 && session->pollfds[0].fd == -1) {
487 /* Dummy source, freewheel over it. */
488 while (session->num_sources)
489 session->sources[0].cb(-1, 0, session->sources[0].cb_data);
491 /* Real sources, use g_poll() main loop. */
492 while (session->num_sources)
493 sr_session_iteration(session, TRUE);
502 * The session is stopped immediately, with all acquisition sessions stopped
503 * and hardware drivers cleaned up.
505 * This must be called from within the session thread, to prevent freeing
506 * resources that the session thread will try to use.
508 * @retval SR_OK Success.
509 * @retval SR_ERR_ARG Invalid session passed.
513 SR_PRIV int sr_session_stop_sync(struct sr_session *session)
515 struct sr_dev_inst *sdi;
519 sr_err("%s: session was NULL", __func__);
523 sr_info("Stopping.");
525 for (l = session->devs; l; l = l->next) {
528 if (sdi->driver->dev_acquisition_stop)
529 sdi->driver->dev_acquisition_stop(sdi, sdi);
532 session->running = FALSE;
540 * The session is stopped immediately, with all acquisition sessions being
541 * stopped and hardware drivers cleaned up.
543 * If the session is run in a separate thread, this function will not block
544 * until the session is finished executing. It is the caller's responsibility
545 * to wait for the session thread to return before assuming that the session is
546 * completely decommissioned.
548 * @retval SR_OK Success.
549 * @retval SR_ERR_ARG Invalid session passed.
553 SR_API int sr_session_stop(struct sr_session *session)
556 sr_err("%s: session was NULL", __func__);
560 g_mutex_lock(&session->stop_mutex);
561 session->abort_session = TRUE;
562 g_mutex_unlock(&session->stop_mutex);
570 * @param packet The packet to show debugging information for.
572 static void datafeed_dump(const struct sr_datafeed_packet *packet)
574 const struct sr_datafeed_logic *logic;
575 const struct sr_datafeed_analog *analog;
577 switch (packet->type) {
579 sr_dbg("bus: Received SR_DF_HEADER packet.");
582 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
585 sr_dbg("bus: Received SR_DF_META packet.");
588 logic = packet->payload;
589 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, "
590 "unitsize = %d).", logic->length, logic->unitsize);
593 analog = packet->payload;
594 sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).",
595 analog->num_samples);
598 sr_dbg("bus: Received SR_DF_END packet.");
600 case SR_DF_FRAME_BEGIN:
601 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
603 case SR_DF_FRAME_END:
604 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
607 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
613 * Send a packet to whatever is listening on the datafeed bus.
615 * Hardware drivers use this to send a data packet to the frontend.
618 * @param packet The datafeed packet to send to the session bus.
620 * @retval SR_OK Success.
621 * @retval SR_ERR_ARG Invalid argument.
625 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
626 const struct sr_datafeed_packet *packet)
629 struct datafeed_callback *cb_struct;
632 sr_err("%s: sdi was NULL", __func__);
637 sr_err("%s: packet was NULL", __func__);
641 for (l = sdi->session->datafeed_callbacks; l; l = l->next) {
642 if (sr_log_loglevel_get() >= SR_LOG_DBG)
643 datafeed_dump(packet);
645 cb_struct->cb(sdi, packet, cb_struct->cb_data);
652 * Add an event source for a file descriptor.
654 * @param pollfd The GPollFD.
655 * @param[in] timeout Max time to wait before the callback is called,
657 * @param cb Callback function to add. Must not be NULL.
658 * @param cb_data Data for the callback function. Can be NULL.
659 * @param poll_object TODO.
661 * @retval SR_OK Success.
662 * @retval SR_ERR_ARG Invalid argument.
663 * @retval SR_ERR_MALLOC Memory allocation error.
665 static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd,
666 int timeout, sr_receive_data_callback cb, void *cb_data, gintptr poll_object)
668 struct source *new_sources, *s;
669 GPollFD *new_pollfds;
672 sr_err("%s: cb was NULL", __func__);
676 /* Note: cb_data can be NULL, that's not a bug. */
678 new_pollfds = g_try_realloc(session->pollfds,
679 sizeof(GPollFD) * (session->num_sources + 1));
681 sr_err("%s: new_pollfds malloc failed", __func__);
682 return SR_ERR_MALLOC;
685 new_sources = g_try_realloc(session->sources, sizeof(struct source) *
686 (session->num_sources + 1));
688 sr_err("%s: new_sources malloc failed", __func__);
689 return SR_ERR_MALLOC;
692 new_pollfds[session->num_sources] = *pollfd;
693 s = &new_sources[session->num_sources++];
694 s->timeout = timeout;
696 s->cb_data = cb_data;
697 s->poll_object = poll_object;
698 session->pollfds = new_pollfds;
699 session->sources = new_sources;
701 if (timeout != session->source_timeout && timeout > 0
702 && (session->source_timeout == -1 || timeout < session->source_timeout))
703 session->source_timeout = timeout;
709 * Add an event source for a file descriptor.
711 * @param fd The file descriptor.
712 * @param events Events to check for.
713 * @param timeout Max time to wait before the callback is called, ignored if 0.
714 * @param cb Callback function to add. Must not be NULL.
715 * @param cb_data Data for the callback function. Can be NULL.
717 * @retval SR_OK Success.
718 * @retval SR_ERR_ARG Invalid argument.
719 * @retval SR_ERR_MALLOC Memory allocation error.
723 SR_API int sr_session_source_add(struct sr_session *session, int fd,
724 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
733 return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)fd);
737 * Add an event source for a GPollFD.
739 * @param pollfd The GPollFD.
740 * @param timeout Max time to wait before the callback is called, ignored if 0.
741 * @param cb Callback function to add. Must not be NULL.
742 * @param cb_data Data for the callback function. Can be NULL.
744 * @retval SR_OK Success.
745 * @retval SR_ERR_ARG Invalid argument.
746 * @retval SR_ERR_MALLOC Memory allocation error.
750 SR_API int sr_session_source_add_pollfd(struct sr_session *session,
751 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
756 return _sr_session_source_add(session, pollfd, timeout, cb,
757 cb_data, (gintptr)pollfd);
761 * Add an event source for a GIOChannel.
763 * @param channel The GIOChannel.
764 * @param events Events to poll on.
765 * @param timeout Max time to wait before the callback is called, ignored if 0.
766 * @param cb Callback function to add. Must not be NULL.
767 * @param cb_data Data for the callback function. Can be NULL.
769 * @retval SR_OK Success.
770 * @retval SR_ERR_ARG Invalid argument.
771 * @retval SR_ERR_MALLOC Memory allocation error.
775 SR_API int sr_session_source_add_channel(struct sr_session *session,
776 GIOChannel *channel, int events, int timeout,
777 sr_receive_data_callback cb, void *cb_data)
784 g_io_channel_win32_make_pollfd(channel, events, &p);
786 p.fd = g_io_channel_unix_get_fd(channel);
790 return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)channel);
794 * Remove the source belonging to the specified channel.
796 * @todo Add more error checks and logging.
798 * @param poll_object The channel for which the source should be removed.
800 * @retval SR_OK Success
801 * @retval SR_ERR_ARG Invalid arguments
802 * @retval SR_ERR_MALLOC Memory allocation error
803 * @retval SR_ERR_BUG Internal error
805 static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object)
807 struct source *new_sources;
808 GPollFD *new_pollfds;
811 if (!session->sources || !session->num_sources) {
812 sr_err("%s: sources was NULL", __func__);
816 for (old = 0; old < session->num_sources; old++) {
817 if (session->sources[old].poll_object == poll_object)
821 /* fd not found, nothing to do */
822 if (old == session->num_sources)
825 session->num_sources -= 1;
827 if (old != session->num_sources) {
828 memmove(&session->pollfds[old], &session->pollfds[old+1],
829 (session->num_sources - old) * sizeof(GPollFD));
830 memmove(&session->sources[old], &session->sources[old+1],
831 (session->num_sources - old) * sizeof(struct source));
834 new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
835 if (!new_pollfds && session->num_sources > 0) {
836 sr_err("%s: new_pollfds malloc failed", __func__);
837 return SR_ERR_MALLOC;
840 new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
841 if (!new_sources && session->num_sources > 0) {
842 sr_err("%s: new_sources malloc failed", __func__);
843 return SR_ERR_MALLOC;
846 session->pollfds = new_pollfds;
847 session->sources = new_sources;
853 * Remove the source belonging to the specified file descriptor.
855 * @param fd The file descriptor for which the source should be removed.
857 * @retval SR_OK Success
858 * @retval SR_ERR_ARG Invalid argument
859 * @retval SR_ERR_MALLOC Memory allocation error.
860 * @retval SR_ERR_BUG Internal error.
864 SR_API int sr_session_source_remove(struct sr_session *session, int fd)
868 return _sr_session_source_remove(session, (gintptr)fd);
872 * Remove the source belonging to the specified poll descriptor.
874 * @param pollfd The poll descriptor for which the source should be removed.
876 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
877 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
882 SR_API int sr_session_source_remove_pollfd(struct sr_session *session,
887 return _sr_session_source_remove(session, (gintptr)pollfd);
891 * Remove the source belonging to the specified channel.
893 * @param channel The channel for which the source should be removed.
895 * @retval SR_OK Success.
896 * @retval SR_ERR_ARG Invalid argument.
897 * @retval SR_ERR_MALLOC Memory allocation error.
898 * @return SR_ERR_BUG Internal error.
902 SR_API int sr_session_source_remove_channel(struct sr_session *session,
907 return _sr_session_source_remove(session, (gintptr)channel);