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.
65 * @param new_session This will contain a pointer to the newly created
66 * session if the return value is SR_OK, otherwise the value
67 * is undefined and should not be used. Must not be NULL.
69 * @retval SR_OK Success.
70 * @retval SR_ERR_BUG A session exists already.
74 SR_API int sr_session_new(struct sr_session **new_session)
76 struct sr_session *session;
78 session = g_malloc0(sizeof(struct sr_session));
80 session->source_timeout = -1;
81 session->running = FALSE;
82 session->abort_session = FALSE;
83 g_mutex_init(&session->stop_mutex);
85 *new_session = session;
92 * This frees up all memory used by the session.
94 * @param session The session to destroy. Must not be NULL.
96 * @retval SR_OK Success.
97 * @retval SR_ERR_ARG Invalid session passed.
101 SR_API int sr_session_destroy(struct sr_session *session)
104 sr_err("%s: session was NULL", __func__);
108 sr_session_dev_remove_all(session);
109 g_mutex_clear(&session->stop_mutex);
110 if (session->trigger)
111 sr_trigger_free(session->trigger);
119 * Remove all the devices from a session.
121 * The session itself (i.e., the struct sr_session) is not free'd and still
122 * exists after this function returns.
124 * @param session The session to use. Must not be NULL.
126 * @retval SR_OK Success.
127 * @retval SR_ERR_BUG Invalid session passed.
131 SR_API int sr_session_dev_remove_all(struct sr_session *session)
133 struct sr_dev_inst *sdi;
137 sr_err("%s: session was NULL", __func__);
141 for (l = session->devs; l; l = l->next) {
142 sdi = (struct sr_dev_inst *) l->data;
146 g_slist_free(session->devs);
147 session->devs = NULL;
153 * Add a device instance to a session.
155 * @param session The session to add to. Must not be NULL.
156 * @param sdi The device instance to add to a session. Must not
157 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
160 * @retval SR_OK Success.
161 * @retval SR_ERR_ARG Invalid argument.
165 SR_API int sr_session_dev_add(struct sr_session *session,
166 struct sr_dev_inst *sdi)
171 sr_err("%s: sdi was NULL", __func__);
176 sr_err("%s: session was NULL", __func__);
180 /* If sdi->session is not NULL, the device is already in this or
181 * another session. */
183 sr_err("%s: already assigned to session", __func__);
187 /* If sdi->driver is NULL, this is a virtual device. */
189 sr_dbg("%s: sdi->driver was NULL, this seems to be "
190 "a virtual device; continuing", __func__);
191 /* Just add the device, don't run dev_open(). */
192 session->devs = g_slist_append(session->devs, (gpointer)sdi);
193 sdi->session = session;
197 /* sdi->driver is non-NULL (i.e. we have a real device). */
198 if (!sdi->driver->dev_open) {
199 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
203 session->devs = g_slist_append(session->devs, (gpointer)sdi);
204 sdi->session = session;
206 if (session->running) {
207 /* Adding a device to a running session. Commit settings
208 * and start acquisition on that device now. */
209 if ((ret = sr_config_commit(sdi)) != SR_OK) {
210 sr_err("Failed to commit device settings before "
211 "starting acquisition in running session (%s)",
215 if ((ret = sdi->driver->dev_acquisition_start(sdi,
216 (void *)sdi)) != SR_OK) {
217 sr_err("Failed to start acquisition of device in "
218 "running session (%s)", sr_strerror(ret));
227 * List all device instances attached to a session.
229 * @param session The session to use. Must not be NULL.
230 * @param devlist A pointer where the device instance list will be
231 * stored on return. If no devices are in the session,
232 * this will be NULL. Each element in the list points
233 * to a struct sr_dev_inst *.
234 * The list must be freed by the caller, but not the
235 * elements pointed to.
237 * @retval SR_OK Success.
238 * @retval SR_ERR_ARG Invalid argument.
242 SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist)
250 *devlist = g_slist_copy(session->devs);
256 * Remove all datafeed callbacks in a session.
258 * @param session The session to use. Must not be NULL.
260 * @retval SR_OK Success.
261 * @retval SR_ERR_ARG Invalid session passed.
265 SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session)
268 sr_err("%s: session was NULL", __func__);
272 g_slist_free_full(session->datafeed_callbacks, g_free);
273 session->datafeed_callbacks = NULL;
279 * Add a datafeed callback to a session.
281 * @param session The session to use. Must not be NULL.
282 * @param cb Function to call when a chunk of data is received.
284 * @param cb_data Opaque pointer passed in by the caller.
286 * @retval SR_OK Success.
287 * @retval SR_ERR_BUG No session exists.
291 SR_API int sr_session_datafeed_callback_add(struct sr_session *session,
292 sr_datafeed_callback cb, void *cb_data)
294 struct datafeed_callback *cb_struct;
297 sr_err("%s: session was NULL", __func__);
302 sr_err("%s: cb was NULL", __func__);
306 if (!(cb_struct = g_try_malloc0(sizeof(struct datafeed_callback))))
307 return SR_ERR_MALLOC;
310 cb_struct->cb_data = cb_data;
312 session->datafeed_callbacks =
313 g_slist_append(session->datafeed_callbacks, cb_struct);
318 SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session)
320 return session->trigger;
323 SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig)
325 session->trigger = trig;
331 * Call every device in the current session's callback.
333 * For sessions not driven by select loops such as sr_session_run(),
334 * but driven by another scheduler, this can be used to poll the devices
335 * from within that scheduler.
337 * @param session The session to use. Must not be NULL.
338 * @param block If TRUE, this call will wait for any of the session's
339 * sources to fire an event on the file descriptors, or
340 * any of their timeouts to activate. In other words, this
341 * can be used as a select loop.
342 * If FALSE, all sources have their callback run, regardless
343 * of file descriptor or timeout status.
345 * @retval SR_OK Success.
346 * @retval SR_ERR Error occured.
348 static int sr_session_iteration(struct sr_session *session, gboolean block)
353 ret = g_poll(session->pollfds, session->num_sources,
354 block ? session->source_timeout : 0);
355 for (i = 0; i < session->num_sources; i++) {
356 if (session->pollfds[i].revents > 0 || (ret == 0
357 && session->source_timeout == session->sources[i].timeout)) {
359 * Invoke the source's callback on an event,
360 * or if the poll timed out and this source
361 * asked for that timeout.
363 if (!session->sources[i].cb(session->pollfds[i].fd,
364 session->pollfds[i].revents,
365 session->sources[i].cb_data))
366 sr_session_source_remove(session,
367 session->sources[i].poll_object);
370 * We want to take as little time as possible to stop
371 * the session if we have been told to do so. Therefore,
372 * we check the flag after processing every source, not
373 * just once per main event loop.
375 g_mutex_lock(&session->stop_mutex);
376 if (session->abort_session) {
377 sr_session_stop_sync(session);
378 /* But once is enough. */
379 session->abort_session = FALSE;
381 g_mutex_unlock(&session->stop_mutex);
388 static int verify_trigger(struct sr_trigger *trigger)
390 struct sr_trigger_stage *stage;
391 struct sr_trigger_match *match;
394 if (!trigger->stages) {
395 sr_err("No trigger stages defined.");
399 sr_spew("Checking trigger:");
400 for (l = trigger->stages; l; l = l->next) {
402 if (!stage->matches) {
403 sr_err("Stage %d has no matches defined.", stage->stage);
406 for (m = stage->matches; m; m = m->next) {
408 if (!match->channel) {
409 sr_err("Stage %d match has no channel.", stage->stage);
413 sr_err("Stage %d match is not defined.", stage->stage);
416 sr_spew("Stage %d match on channel %s, match %d", stage->stage,
417 match->channel->name, match->match);
426 * @param session The session to use. Must not be NULL.
428 * @retval SR_OK Success.
429 * @retval SR_ERR_ARG Invalid session passed.
433 SR_API int sr_session_start(struct sr_session *session)
435 struct sr_dev_inst *sdi;
440 sr_err("%s: session was NULL", __func__);
444 if (!session->devs) {
445 sr_err("%s: session->devs was NULL; a session "
446 "cannot be started without devices.", __func__);
450 if (session->trigger && verify_trigger(session->trigger) != SR_OK)
453 sr_info("Starting.");
456 for (l = session->devs; l; l = l->next) {
458 if ((ret = sr_config_commit(sdi)) != SR_OK) {
459 sr_err("Failed to commit device settings before "
460 "starting acquisition (%s)", sr_strerror(ret));
463 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
464 sr_err("%s: could not start an acquisition "
465 "(%s)", __func__, sr_strerror(ret));
470 /* TODO: What if there are multiple devices? Which return code? */
478 * @param session The session to use. Must not be NULL.
480 * @retval SR_OK Success.
481 * @retval SR_ERR_ARG Invalid session passed.
485 SR_API int sr_session_run(struct sr_session *session)
488 sr_err("%s: session was NULL", __func__);
492 if (!session->devs) {
493 /* TODO: Actually the case? */
494 sr_err("%s: session->devs was NULL; a session "
495 "cannot be run without devices.", __func__);
498 session->running = TRUE;
502 /* Do we have real sources? */
503 if (session->num_sources == 1 && session->pollfds[0].fd == -1) {
504 /* Dummy source, freewheel over it. */
505 while (session->num_sources)
506 session->sources[0].cb(-1, 0, session->sources[0].cb_data);
508 /* Real sources, use g_poll() main loop. */
509 while (session->num_sources)
510 sr_session_iteration(session, TRUE);
519 * The session is stopped immediately, with all acquisition sessions stopped
520 * and hardware drivers cleaned up.
522 * This must be called from within the session thread, to prevent freeing
523 * resources that the session thread will try to use.
525 * @param session The session to use. Must not be NULL.
527 * @retval SR_OK Success.
528 * @retval SR_ERR_ARG Invalid session passed.
532 SR_PRIV int sr_session_stop_sync(struct sr_session *session)
534 struct sr_dev_inst *sdi;
538 sr_err("%s: session was NULL", __func__);
542 sr_info("Stopping.");
544 for (l = session->devs; l; l = l->next) {
547 if (sdi->driver->dev_acquisition_stop)
548 sdi->driver->dev_acquisition_stop(sdi, sdi);
551 session->running = FALSE;
559 * The session is stopped immediately, with all acquisition sessions being
560 * stopped and hardware drivers cleaned up.
562 * If the session is run in a separate thread, this function will not block
563 * until the session is finished executing. It is the caller's responsibility
564 * to wait for the session thread to return before assuming that the session is
565 * completely decommissioned.
567 * @param session The session to use. Must not be NULL.
569 * @retval SR_OK Success.
570 * @retval SR_ERR_ARG Invalid session passed.
574 SR_API int sr_session_stop(struct sr_session *session)
577 sr_err("%s: session was NULL", __func__);
581 g_mutex_lock(&session->stop_mutex);
582 session->abort_session = TRUE;
583 g_mutex_unlock(&session->stop_mutex);
591 * @param packet The packet to show debugging information for.
593 static void datafeed_dump(const struct sr_datafeed_packet *packet)
595 const struct sr_datafeed_logic *logic;
596 const struct sr_datafeed_analog *analog;
598 switch (packet->type) {
600 sr_dbg("bus: Received SR_DF_HEADER packet.");
603 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
606 sr_dbg("bus: Received SR_DF_META packet.");
609 logic = packet->payload;
610 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, "
611 "unitsize = %d).", logic->length, logic->unitsize);
614 analog = packet->payload;
615 sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).",
616 analog->num_samples);
619 sr_dbg("bus: Received SR_DF_END packet.");
621 case SR_DF_FRAME_BEGIN:
622 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
624 case SR_DF_FRAME_END:
625 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
628 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
634 * Send a packet to whatever is listening on the datafeed bus.
636 * Hardware drivers use this to send a data packet to the frontend.
639 * @param packet The datafeed packet to send to the session bus.
641 * @retval SR_OK Success.
642 * @retval SR_ERR_ARG Invalid argument.
646 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
647 const struct sr_datafeed_packet *packet)
650 struct datafeed_callback *cb_struct;
653 sr_err("%s: sdi was NULL", __func__);
658 sr_err("%s: packet was NULL", __func__);
662 for (l = sdi->session->datafeed_callbacks; l; l = l->next) {
663 if (sr_log_loglevel_get() >= SR_LOG_DBG)
664 datafeed_dump(packet);
666 cb_struct->cb(sdi, packet, cb_struct->cb_data);
673 * Add an event source for a file descriptor.
675 * @param session The session to use. Must not be NULL.
676 * @param pollfd The GPollFD.
677 * @param[in] timeout Max time to wait before the callback is called,
679 * @param cb Callback function to add. Must not be NULL.
680 * @param cb_data Data for the callback function. Can be NULL.
681 * @param poll_object TODO.
683 * @retval SR_OK Success.
684 * @retval SR_ERR_ARG Invalid argument.
685 * @retval SR_ERR_MALLOC Memory allocation error.
687 static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd,
688 int timeout, sr_receive_data_callback cb, void *cb_data, gintptr poll_object)
690 struct source *new_sources, *s;
691 GPollFD *new_pollfds;
694 sr_err("%s: cb was NULL", __func__);
698 /* Note: cb_data can be NULL, that's not a bug. */
700 new_pollfds = g_try_realloc(session->pollfds,
701 sizeof(GPollFD) * (session->num_sources + 1));
703 sr_err("%s: new_pollfds malloc failed", __func__);
704 return SR_ERR_MALLOC;
707 new_sources = g_try_realloc(session->sources, sizeof(struct source) *
708 (session->num_sources + 1));
710 sr_err("%s: new_sources malloc failed", __func__);
711 return SR_ERR_MALLOC;
714 new_pollfds[session->num_sources] = *pollfd;
715 s = &new_sources[session->num_sources++];
716 s->timeout = timeout;
718 s->cb_data = cb_data;
719 s->poll_object = poll_object;
720 session->pollfds = new_pollfds;
721 session->sources = new_sources;
723 if (timeout != session->source_timeout && timeout > 0
724 && (session->source_timeout == -1 || timeout < session->source_timeout))
725 session->source_timeout = timeout;
731 * Add an event source for a file descriptor.
733 * @param session The session to use. Must not be NULL.
734 * @param fd The file descriptor.
735 * @param events Events to check for.
736 * @param timeout Max time to wait before the callback is called, ignored if 0.
737 * @param cb Callback function to add. Must not be NULL.
738 * @param cb_data Data for the callback function. Can be NULL.
740 * @retval SR_OK Success.
741 * @retval SR_ERR_ARG Invalid argument.
742 * @retval SR_ERR_MALLOC Memory allocation error.
746 SR_API int sr_session_source_add(struct sr_session *session, int fd,
747 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
754 return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)fd);
758 * Add an event source for a GPollFD.
760 * @param session The session to use. Must not be NULL.
761 * @param pollfd The GPollFD.
762 * @param timeout Max time to wait before the callback is called, ignored if 0.
763 * @param cb Callback function to add. Must not be NULL.
764 * @param cb_data Data for the callback function. Can be NULL.
766 * @retval SR_OK Success.
767 * @retval SR_ERR_ARG Invalid argument.
768 * @retval SR_ERR_MALLOC Memory allocation error.
772 SR_API int sr_session_source_add_pollfd(struct sr_session *session,
773 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
776 return _sr_session_source_add(session, pollfd, timeout, cb,
777 cb_data, (gintptr)pollfd);
781 * Add an event source for a GIOChannel.
783 * @param session The session to use. Must not be NULL.
784 * @param channel The GIOChannel.
785 * @param events Events to poll on.
786 * @param timeout Max time to wait before the callback is called, ignored if 0.
787 * @param cb Callback function to add. Must not be NULL.
788 * @param cb_data Data for the callback function. Can be NULL.
790 * @retval SR_OK Success.
791 * @retval SR_ERR_ARG Invalid argument.
792 * @retval SR_ERR_MALLOC Memory allocation error.
796 SR_API int sr_session_source_add_channel(struct sr_session *session,
797 GIOChannel *channel, int events, int timeout,
798 sr_receive_data_callback cb, void *cb_data)
803 g_io_channel_win32_make_pollfd(channel, events, &p);
805 p.fd = g_io_channel_unix_get_fd(channel);
809 return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)channel);
813 * Remove the source belonging to the specified channel.
815 * @todo Add more error checks and logging.
817 * @param session The session to use. Must not be NULL.
818 * @param poll_object The channel for which the source should be removed.
820 * @retval SR_OK Success
821 * @retval SR_ERR_ARG Invalid arguments
822 * @retval SR_ERR_MALLOC Memory allocation error
823 * @retval SR_ERR_BUG Internal error
825 static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object)
827 struct source *new_sources;
828 GPollFD *new_pollfds;
831 if (!session->sources || !session->num_sources) {
832 sr_err("%s: sources was NULL", __func__);
836 for (old = 0; old < session->num_sources; old++) {
837 if (session->sources[old].poll_object == poll_object)
841 /* fd not found, nothing to do */
842 if (old == session->num_sources)
845 session->num_sources -= 1;
847 if (old != session->num_sources) {
848 memmove(&session->pollfds[old], &session->pollfds[old+1],
849 (session->num_sources - old) * sizeof(GPollFD));
850 memmove(&session->sources[old], &session->sources[old+1],
851 (session->num_sources - old) * sizeof(struct source));
854 new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
855 if (!new_pollfds && session->num_sources > 0) {
856 sr_err("%s: new_pollfds malloc failed", __func__);
857 return SR_ERR_MALLOC;
860 new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
861 if (!new_sources && session->num_sources > 0) {
862 sr_err("%s: new_sources malloc failed", __func__);
863 return SR_ERR_MALLOC;
866 session->pollfds = new_pollfds;
867 session->sources = new_sources;
873 * Remove the source belonging to the specified file descriptor.
875 * @param session The session to use. Must not be NULL.
876 * @param fd The file descriptor for which the source should be removed.
878 * @retval SR_OK Success
879 * @retval SR_ERR_ARG Invalid argument
880 * @retval SR_ERR_MALLOC Memory allocation error.
881 * @retval SR_ERR_BUG Internal error.
885 SR_API int sr_session_source_remove(struct sr_session *session, int fd)
887 return _sr_session_source_remove(session, (gintptr)fd);
891 * Remove the source belonging to the specified poll descriptor.
893 * @param session The session to use. Must not be NULL.
894 * @param pollfd The poll descriptor for which the source should be removed.
896 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
897 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
902 SR_API int sr_session_source_remove_pollfd(struct sr_session *session,
905 return _sr_session_source_remove(session, (gintptr)pollfd);
909 * Remove the source belonging to the specified channel.
911 * @param session The session to use. Must not be NULL.
912 * @param channel The channel for which the source should be removed.
914 * @retval SR_OK Success.
915 * @retval SR_ERR_ARG Invalid argument.
916 * @retval SR_ERR_MALLOC Memory allocation error.
917 * @return SR_ERR_BUG Internal error.
921 SR_API int sr_session_source_remove_channel(struct sr_session *session,
924 return _sr_session_source_remove(session, (gintptr)channel);