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_ARG Invalid argument.
74 SR_API int sr_session_new(struct sr_session **new_session)
76 struct sr_session *session;
81 session = g_malloc0(sizeof(struct sr_session));
83 session->source_timeout = -1;
84 session->running = FALSE;
85 session->abort_session = FALSE;
86 g_mutex_init(&session->stop_mutex);
88 *new_session = session;
95 * This frees up all memory used by the session.
97 * @param session The session to destroy. Must not be NULL.
99 * @retval SR_OK Success.
100 * @retval SR_ERR_ARG Invalid session passed.
104 SR_API int sr_session_destroy(struct sr_session *session)
107 sr_err("%s: session was NULL", __func__);
111 sr_session_dev_remove_all(session);
112 g_mutex_clear(&session->stop_mutex);
113 if (session->trigger)
114 sr_trigger_free(session->trigger);
122 * Remove all the devices from a session.
124 * The session itself (i.e., the struct sr_session) is not free'd and still
125 * exists after this function returns.
127 * @param session The session to use. Must not be NULL.
129 * @retval SR_OK Success.
130 * @retval SR_ERR_BUG Invalid session passed.
134 SR_API int sr_session_dev_remove_all(struct sr_session *session)
136 struct sr_dev_inst *sdi;
140 sr_err("%s: session was NULL", __func__);
144 for (l = session->devs; l; l = l->next) {
145 sdi = (struct sr_dev_inst *) l->data;
149 g_slist_free(session->devs);
150 session->devs = NULL;
156 * Add a device instance to a session.
158 * @param session The session to add to. Must not be NULL.
159 * @param sdi The device instance to add to a session. Must not
160 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
163 * @retval SR_OK Success.
164 * @retval SR_ERR_ARG Invalid argument.
168 SR_API int sr_session_dev_add(struct sr_session *session,
169 struct sr_dev_inst *sdi)
174 sr_err("%s: sdi was NULL", __func__);
179 sr_err("%s: session was NULL", __func__);
183 /* If sdi->session is not NULL, the device is already in this or
184 * another session. */
186 sr_err("%s: already assigned to session", __func__);
190 /* If sdi->driver is NULL, this is a virtual device. */
192 /* Just add the device, don't run dev_open(). */
193 session->devs = g_slist_append(session->devs, (gpointer)sdi);
194 sdi->session = session;
198 /* sdi->driver is non-NULL (i.e. we have a real device). */
199 if (!sdi->driver->dev_open) {
200 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
204 session->devs = g_slist_append(session->devs, (gpointer)sdi);
205 sdi->session = session;
207 if (session->running) {
208 /* Adding a device to a running session. Commit settings
209 * and start acquisition on that device now. */
210 if ((ret = sr_config_commit(sdi)) != SR_OK) {
211 sr_err("Failed to commit device settings before "
212 "starting acquisition in running session (%s)",
216 if ((ret = sdi->driver->dev_acquisition_start(sdi,
217 (void *)sdi)) != SR_OK) {
218 sr_err("Failed to start acquisition of device in "
219 "running session (%s)", sr_strerror(ret));
228 * List all device instances attached to a session.
230 * @param session The session to use. Must not be NULL.
231 * @param devlist A pointer where the device instance list will be
232 * stored on return. If no devices are in the session,
233 * this will be NULL. Each element in the list points
234 * to a struct sr_dev_inst *.
235 * The list must be freed by the caller, but not the
236 * elements pointed to.
238 * @retval SR_OK Success.
239 * @retval SR_ERR_ARG Invalid argument.
243 SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist)
251 *devlist = g_slist_copy(session->devs);
257 * Remove all datafeed callbacks in a session.
259 * @param session The session to use. Must not be NULL.
261 * @retval SR_OK Success.
262 * @retval SR_ERR_ARG Invalid session passed.
266 SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session)
269 sr_err("%s: session was NULL", __func__);
273 g_slist_free_full(session->datafeed_callbacks, g_free);
274 session->datafeed_callbacks = NULL;
280 * Add a datafeed callback to a session.
282 * @param session The session to use. Must not be NULL.
283 * @param cb Function to call when a chunk of data is received.
285 * @param cb_data Opaque pointer passed in by the caller.
287 * @retval SR_OK Success.
288 * @retval SR_ERR_BUG No session exists.
292 SR_API int sr_session_datafeed_callback_add(struct sr_session *session,
293 sr_datafeed_callback cb, void *cb_data)
295 struct datafeed_callback *cb_struct;
298 sr_err("%s: session was NULL", __func__);
303 sr_err("%s: cb was NULL", __func__);
307 if (!(cb_struct = g_try_malloc0(sizeof(struct datafeed_callback))))
308 return SR_ERR_MALLOC;
311 cb_struct->cb_data = cb_data;
313 session->datafeed_callbacks =
314 g_slist_append(session->datafeed_callbacks, cb_struct);
319 SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session)
321 return session->trigger;
324 SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig)
326 session->trigger = trig;
332 * Call every device in the current session's callback.
334 * For sessions not driven by select loops such as sr_session_run(),
335 * but driven by another scheduler, this can be used to poll the devices
336 * from within that scheduler.
338 * @param session The session to use. Must not be NULL.
339 * @param block If TRUE, this call will wait for any of the session's
340 * sources to fire an event on the file descriptors, or
341 * any of their timeouts to activate. In other words, this
342 * can be used as a select loop.
343 * If FALSE, all sources have their callback run, regardless
344 * of file descriptor or timeout status.
346 * @retval SR_OK Success.
347 * @retval SR_ERR Error occured.
349 static int sr_session_iteration(struct sr_session *session, gboolean block)
354 ret = g_poll(session->pollfds, session->num_sources,
355 block ? session->source_timeout : 0);
356 for (i = 0; i < session->num_sources; i++) {
357 if (session->pollfds[i].revents > 0 || (ret == 0
358 && session->source_timeout == session->sources[i].timeout)) {
360 * Invoke the source's callback on an event,
361 * or if the poll timed out and this source
362 * asked for that timeout.
364 if (!session->sources[i].cb(session->pollfds[i].fd,
365 session->pollfds[i].revents,
366 session->sources[i].cb_data))
367 sr_session_source_remove(session,
368 session->sources[i].poll_object);
371 * We want to take as little time as possible to stop
372 * the session if we have been told to do so. Therefore,
373 * we check the flag after processing every source, not
374 * just once per main event loop.
376 g_mutex_lock(&session->stop_mutex);
377 if (session->abort_session) {
378 sr_session_stop_sync(session);
379 /* But once is enough. */
380 session->abort_session = FALSE;
382 g_mutex_unlock(&session->stop_mutex);
389 static int verify_trigger(struct sr_trigger *trigger)
391 struct sr_trigger_stage *stage;
392 struct sr_trigger_match *match;
395 if (!trigger->stages) {
396 sr_err("No trigger stages defined.");
400 sr_spew("Checking trigger:");
401 for (l = trigger->stages; l; l = l->next) {
403 if (!stage->matches) {
404 sr_err("Stage %d has no matches defined.", stage->stage);
407 for (m = stage->matches; m; m = m->next) {
409 if (!match->channel) {
410 sr_err("Stage %d match has no channel.", stage->stage);
414 sr_err("Stage %d match is not defined.", stage->stage);
417 sr_spew("Stage %d match on channel %s, match %d", stage->stage,
418 match->channel->name, match->match);
427 * @param session The session to use. Must not be NULL.
429 * @retval SR_OK Success.
430 * @retval SR_ERR_ARG Invalid session passed.
434 SR_API int sr_session_start(struct sr_session *session)
436 struct sr_dev_inst *sdi;
437 struct sr_channel *ch;
439 int enabled_channels, ret;
442 sr_err("%s: session was NULL", __func__);
446 if (!session->devs) {
447 sr_err("%s: session->devs was NULL; a session "
448 "cannot be started without devices.", __func__);
452 if (session->trigger && verify_trigger(session->trigger) != SR_OK)
455 sr_info("Starting.");
458 for (l = session->devs; l; l = l->next) {
460 enabled_channels = 0;
461 for (c = sdi->channels; c; c = c->next) {
468 if (enabled_channels == 0) {
470 sr_err("%s instance %d has no enabled channels!",
471 sdi->driver->name, sdi->index);
475 if ((ret = sr_config_commit(sdi)) != SR_OK) {
476 sr_err("Failed to commit device settings before "
477 "starting acquisition (%s)", sr_strerror(ret));
480 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
481 sr_err("%s: could not start an acquisition "
482 "(%s)", __func__, sr_strerror(ret));
487 /* TODO: What if there are multiple devices? Which return code? */
495 * @param session The session to use. Must not be NULL.
497 * @retval SR_OK Success.
498 * @retval SR_ERR_ARG Invalid session passed.
502 SR_API int sr_session_run(struct sr_session *session)
505 sr_err("%s: session was NULL", __func__);
509 if (!session->devs) {
510 /* TODO: Actually the case? */
511 sr_err("%s: session->devs was NULL; a session "
512 "cannot be run without devices.", __func__);
515 session->running = TRUE;
519 /* Do we have real sources? */
520 if (session->num_sources == 1 && session->pollfds[0].fd == -1) {
521 /* Dummy source, freewheel over it. */
522 while (session->num_sources)
523 session->sources[0].cb(-1, 0, session->sources[0].cb_data);
525 /* Real sources, use g_poll() main loop. */
526 while (session->num_sources)
527 sr_session_iteration(session, TRUE);
536 * The session is stopped immediately, with all acquisition sessions stopped
537 * and hardware drivers cleaned up.
539 * This must be called from within the session thread, to prevent freeing
540 * resources that the session thread will try to use.
542 * @param session The session to use. Must not be NULL.
544 * @retval SR_OK Success.
545 * @retval SR_ERR_ARG Invalid session passed.
549 SR_PRIV int sr_session_stop_sync(struct sr_session *session)
551 struct sr_dev_inst *sdi;
555 sr_err("%s: session was NULL", __func__);
559 sr_info("Stopping.");
561 for (l = session->devs; l; l = l->next) {
564 if (sdi->driver->dev_acquisition_stop)
565 sdi->driver->dev_acquisition_stop(sdi, sdi);
568 session->running = FALSE;
576 * The session is stopped immediately, with all acquisition sessions being
577 * stopped and hardware drivers cleaned up.
579 * If the session is run in a separate thread, this function will not block
580 * until the session is finished executing. It is the caller's responsibility
581 * to wait for the session thread to return before assuming that the session is
582 * completely decommissioned.
584 * @param session The session to use. Must not be NULL.
586 * @retval SR_OK Success.
587 * @retval SR_ERR_ARG Invalid session passed.
591 SR_API int sr_session_stop(struct sr_session *session)
594 sr_err("%s: session was NULL", __func__);
598 g_mutex_lock(&session->stop_mutex);
599 session->abort_session = TRUE;
600 g_mutex_unlock(&session->stop_mutex);
608 * @param packet The packet to show debugging information for.
610 static void datafeed_dump(const struct sr_datafeed_packet *packet)
612 const struct sr_datafeed_logic *logic;
613 const struct sr_datafeed_analog *analog;
615 switch (packet->type) {
617 sr_dbg("bus: Received SR_DF_HEADER packet.");
620 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
623 sr_dbg("bus: Received SR_DF_META packet.");
626 logic = packet->payload;
627 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, "
628 "unitsize = %d).", logic->length, logic->unitsize);
631 analog = packet->payload;
632 sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).",
633 analog->num_samples);
636 sr_dbg("bus: Received SR_DF_END packet.");
638 case SR_DF_FRAME_BEGIN:
639 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
641 case SR_DF_FRAME_END:
642 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
645 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
651 * Send a packet to whatever is listening on the datafeed bus.
653 * Hardware drivers use this to send a data packet to the frontend.
656 * @param packet The datafeed packet to send to the session bus.
658 * @retval SR_OK Success.
659 * @retval SR_ERR_ARG Invalid argument.
663 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
664 const struct sr_datafeed_packet *packet)
667 struct datafeed_callback *cb_struct;
670 sr_err("%s: sdi was NULL", __func__);
675 sr_err("%s: packet was NULL", __func__);
679 for (l = sdi->session->datafeed_callbacks; l; l = l->next) {
680 if (sr_log_loglevel_get() >= SR_LOG_DBG)
681 datafeed_dump(packet);
683 cb_struct->cb(sdi, packet, cb_struct->cb_data);
690 * Add an event source for a file descriptor.
692 * @param session The session to use. Must not be NULL.
693 * @param pollfd The GPollFD.
694 * @param[in] timeout Max time to wait before the callback is called,
696 * @param cb Callback function to add. Must not be NULL.
697 * @param cb_data Data for the callback function. Can be NULL.
698 * @param poll_object TODO.
700 * @retval SR_OK Success.
701 * @retval SR_ERR_ARG Invalid argument.
702 * @retval SR_ERR_MALLOC Memory allocation error.
704 static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd,
705 int timeout, sr_receive_data_callback cb, void *cb_data, gintptr poll_object)
707 struct source *new_sources, *s;
708 GPollFD *new_pollfds;
711 sr_err("%s: cb was NULL", __func__);
715 /* Note: cb_data can be NULL, that's not a bug. */
717 new_pollfds = g_try_realloc(session->pollfds,
718 sizeof(GPollFD) * (session->num_sources + 1));
720 sr_err("%s: new_pollfds malloc failed", __func__);
721 return SR_ERR_MALLOC;
724 new_sources = g_try_realloc(session->sources, sizeof(struct source) *
725 (session->num_sources + 1));
727 sr_err("%s: new_sources malloc failed", __func__);
728 return SR_ERR_MALLOC;
731 new_pollfds[session->num_sources] = *pollfd;
732 s = &new_sources[session->num_sources++];
733 s->timeout = timeout;
735 s->cb_data = cb_data;
736 s->poll_object = poll_object;
737 session->pollfds = new_pollfds;
738 session->sources = new_sources;
740 if (timeout != session->source_timeout && timeout > 0
741 && (session->source_timeout == -1 || timeout < session->source_timeout))
742 session->source_timeout = timeout;
748 * Add an event source for a file descriptor.
750 * @param session The session to use. Must not be NULL.
751 * @param fd The file descriptor.
752 * @param events Events to check for.
753 * @param timeout Max time to wait before the callback is called, ignored if 0.
754 * @param cb Callback function to add. Must not be NULL.
755 * @param cb_data Data for the callback function. Can be NULL.
757 * @retval SR_OK Success.
758 * @retval SR_ERR_ARG Invalid argument.
759 * @retval SR_ERR_MALLOC Memory allocation error.
763 SR_API int sr_session_source_add(struct sr_session *session, int fd,
764 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
771 return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)fd);
775 * Add an event source for a GPollFD.
777 * @param session The session to use. Must not be NULL.
778 * @param pollfd The GPollFD.
779 * @param timeout Max time to wait before the callback is called, ignored if 0.
780 * @param cb Callback function to add. Must not be NULL.
781 * @param cb_data Data for the callback function. Can be NULL.
783 * @retval SR_OK Success.
784 * @retval SR_ERR_ARG Invalid argument.
785 * @retval SR_ERR_MALLOC Memory allocation error.
789 SR_API int sr_session_source_add_pollfd(struct sr_session *session,
790 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
793 return _sr_session_source_add(session, pollfd, timeout, cb,
794 cb_data, (gintptr)pollfd);
798 * Add an event source for a GIOChannel.
800 * @param session The session to use. Must not be NULL.
801 * @param channel The GIOChannel.
802 * @param events Events to poll on.
803 * @param timeout Max time to wait before the callback is called, ignored if 0.
804 * @param cb Callback function to add. Must not be NULL.
805 * @param cb_data Data for the callback function. Can be NULL.
807 * @retval SR_OK Success.
808 * @retval SR_ERR_ARG Invalid argument.
809 * @retval SR_ERR_MALLOC Memory allocation error.
813 SR_API int sr_session_source_add_channel(struct sr_session *session,
814 GIOChannel *channel, int events, int timeout,
815 sr_receive_data_callback cb, void *cb_data)
820 g_io_channel_win32_make_pollfd(channel, events, &p);
822 p.fd = g_io_channel_unix_get_fd(channel);
826 return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)channel);
830 * Remove the source belonging to the specified channel.
832 * @todo Add more error checks and logging.
834 * @param session The session to use. Must not be NULL.
835 * @param poll_object The channel for which the source should be removed.
837 * @retval SR_OK Success
838 * @retval SR_ERR_ARG Invalid arguments
839 * @retval SR_ERR_MALLOC Memory allocation error
840 * @retval SR_ERR_BUG Internal error
842 static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object)
844 struct source *new_sources;
845 GPollFD *new_pollfds;
848 if (!session->sources || !session->num_sources) {
849 sr_err("%s: sources was NULL", __func__);
853 for (old = 0; old < session->num_sources; old++) {
854 if (session->sources[old].poll_object == poll_object)
858 /* fd not found, nothing to do */
859 if (old == session->num_sources)
862 session->num_sources -= 1;
864 if (old != session->num_sources) {
865 memmove(&session->pollfds[old], &session->pollfds[old+1],
866 (session->num_sources - old) * sizeof(GPollFD));
867 memmove(&session->sources[old], &session->sources[old+1],
868 (session->num_sources - old) * sizeof(struct source));
871 new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
872 if (!new_pollfds && session->num_sources > 0) {
873 sr_err("%s: new_pollfds malloc failed", __func__);
874 return SR_ERR_MALLOC;
877 new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
878 if (!new_sources && session->num_sources > 0) {
879 sr_err("%s: new_sources malloc failed", __func__);
880 return SR_ERR_MALLOC;
883 session->pollfds = new_pollfds;
884 session->sources = new_sources;
890 * Remove the source belonging to the specified file descriptor.
892 * @param session The session to use. Must not be NULL.
893 * @param fd The file descriptor 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 * @retval SR_ERR_BUG Internal error.
902 SR_API int sr_session_source_remove(struct sr_session *session, int fd)
904 return _sr_session_source_remove(session, (gintptr)fd);
908 * Remove the source belonging to the specified poll descriptor.
910 * @param session The session to use. Must not be NULL.
911 * @param pollfd The poll descriptor for which the source should be removed.
913 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
914 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
919 SR_API int sr_session_source_remove_pollfd(struct sr_session *session,
922 return _sr_session_source_remove(session, (gintptr)pollfd);
926 * Remove the source belonging to the specified channel.
928 * @param session The session to use. Must not be NULL.
929 * @param channel The channel for which the source should be removed.
931 * @retval SR_OK Success.
932 * @retval SR_ERR_ARG Invalid argument.
933 * @retval SR_ERR_MALLOC Memory allocation error.
934 * @return SR_ERR_BUG Internal error.
938 SR_API int sr_session_source_remove_channel(struct sr_session *session,
941 return _sr_session_source_remove(session, (gintptr)channel);