2 * This file is part of the libsigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2015 Daniel Elstner <daniel.kitta@gmail.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
32 #define LOG_PREFIX "session"
38 * Creating, using, or destroying libsigrok sessions.
42 * @defgroup grp_session Session handling
44 * Creating, using, or destroying libsigrok sessions.
49 struct datafeed_callback {
50 sr_datafeed_callback cb;
54 /** Custom GLib event source for generic descriptor I/O.
55 * @see https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html
64 /* Meta-data needed to keep track of installed sources */
65 struct sr_session *session;
71 /** FD event source prepare() method.
72 * This is called immediately before poll().
74 static gboolean fd_source_prepare(GSource *source, int *timeout)
77 struct fd_source *fsource;
80 fsource = (struct fd_source *)source;
82 if (fsource->timeout_us >= 0) {
83 now_us = g_source_get_time(source);
85 if (fsource->due_us == 0) {
86 /* First-time initialization of the expiration time */
87 fsource->due_us = now_us + fsource->timeout_us;
89 remaining_ms = (MAX(0, fsource->due_us - now_us) + 999) / 1000;
93 *timeout = remaining_ms;
95 return (remaining_ms == 0);
98 /** FD event source check() method.
99 * This is called after poll() returns to check whether an event fired.
101 static gboolean fd_source_check(GSource *source)
103 struct fd_source *fsource;
104 unsigned int revents;
106 fsource = (struct fd_source *)source;
107 revents = fsource->pollfd.revents;
109 return (revents != 0 || (fsource->timeout_us >= 0
110 && fsource->due_us <= g_source_get_time(source)));
113 /** FD event source dispatch() method.
114 * This is called if either prepare() or check() returned TRUE.
116 static gboolean fd_source_dispatch(GSource *source,
117 GSourceFunc callback, void *user_data)
119 struct fd_source *fsource;
120 unsigned int revents;
123 fsource = (struct fd_source *)source;
124 revents = fsource->pollfd.revents;
127 sr_err("Callback not set, cannot dispatch event.");
128 return G_SOURCE_REMOVE;
130 keep = (*(sr_receive_data_callback)callback)
131 (fsource->pollfd.fd, revents, user_data);
133 if (fsource->timeout_us >= 0 && G_LIKELY(keep)
134 && G_LIKELY(!g_source_is_destroyed(source)))
135 fsource->due_us = g_source_get_time(source)
136 + fsource->timeout_us;
140 /** FD event source finalize() method.
142 static void fd_source_finalize(GSource *source)
144 struct fd_source *fsource;
146 fsource = (struct fd_source *)source;
148 sr_dbg("%s: key %p", __func__, fsource->key);
150 sr_session_source_destroyed(fsource->session, fsource->key, source);
153 /** Create an event source for I/O on a file descriptor.
155 * In order to maintain API compatibility, this event source also doubles
156 * as a timer event source.
158 * @param session The session the event source belongs to.
159 * @param key The key used to identify this source.
160 * @param fd The file descriptor or HANDLE.
161 * @param timeout_ms The timeout interval in ms, or -1 to wait indefinitely.
162 * @return A new event source object, or NULL on failure.
164 static GSource *fd_source_new(struct sr_session *session, void *key,
165 gintptr fd, int events, int timeout_ms)
167 static GSourceFuncs fd_source_funcs = {
168 .prepare = &fd_source_prepare,
169 .check = &fd_source_check,
170 .dispatch = &fd_source_dispatch,
171 .finalize = &fd_source_finalize
174 struct fd_source *fsource;
176 source = g_source_new(&fd_source_funcs, sizeof(struct fd_source));
177 fsource = (struct fd_source *)source;
179 g_source_set_name(source, (fd < 0) ? "timer" : "fd");
181 if (timeout_ms >= 0) {
182 fsource->timeout_us = 1000 * (int64_t)timeout_ms;
185 fsource->timeout_us = -1;
186 fsource->due_us = INT64_MAX;
188 fsource->session = session;
191 fsource->pollfd.fd = fd;
192 fsource->pollfd.events = events;
193 fsource->pollfd.revents = 0;
196 g_source_add_poll(source, &fsource->pollfd);
202 * Create a new session.
204 * @param ctx The context in which to create the new session.
205 * @param new_session This will contain a pointer to the newly created
206 * session if the return value is SR_OK, otherwise the value
207 * is undefined and should not be used. Must not be NULL.
209 * @retval SR_OK Success.
210 * @retval SR_ERR_ARG Invalid argument.
214 SR_API int sr_session_new(struct sr_context *ctx,
215 struct sr_session **new_session)
217 struct sr_session *session;
222 session = g_malloc0(sizeof(struct sr_session));
226 g_mutex_init(&session->main_mutex);
228 /* To maintain API compatibility, we need a lookup table
229 * which maps poll_object IDs to GSource* pointers.
231 session->event_sources = g_hash_table_new(NULL, NULL);
233 *new_session = session;
240 * This frees up all memory used by the session.
242 * @param session The session to destroy. Must not be NULL.
244 * @retval SR_OK Success.
245 * @retval SR_ERR_ARG Invalid session passed.
249 SR_API int sr_session_destroy(struct sr_session *session)
252 sr_err("%s: session was NULL", __func__);
256 sr_session_dev_remove_all(session);
257 g_slist_free_full(session->owned_devs, (GDestroyNotify)sr_dev_inst_free);
259 sr_session_datafeed_callback_remove_all(session);
261 g_hash_table_unref(session->event_sources);
263 g_mutex_clear(&session->main_mutex);
271 * Remove all the devices from a session.
273 * The session itself (i.e., the struct sr_session) is not free'd and still
274 * exists after this function returns.
276 * @param session The session to use. Must not be NULL.
278 * @retval SR_OK Success.
279 * @retval SR_ERR_BUG Invalid session passed.
283 SR_API int sr_session_dev_remove_all(struct sr_session *session)
285 struct sr_dev_inst *sdi;
289 sr_err("%s: session was NULL", __func__);
293 for (l = session->devs; l; l = l->next) {
294 sdi = (struct sr_dev_inst *) l->data;
298 g_slist_free(session->devs);
299 session->devs = NULL;
305 * Add a device instance to a session.
307 * @param session The session to add to. Must not be NULL.
308 * @param sdi The device instance to add to a session. Must not
309 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
312 * @retval SR_OK Success.
313 * @retval SR_ERR_ARG Invalid argument.
317 SR_API int sr_session_dev_add(struct sr_session *session,
318 struct sr_dev_inst *sdi)
323 sr_err("%s: sdi was NULL", __func__);
328 sr_err("%s: session was NULL", __func__);
332 /* If sdi->session is not NULL, the device is already in this or
333 * another session. */
335 sr_err("%s: already assigned to session", __func__);
339 /* If sdi->driver is NULL, this is a virtual device. */
341 /* Just add the device, don't run dev_open(). */
342 session->devs = g_slist_append(session->devs, sdi);
343 sdi->session = session;
347 /* sdi->driver is non-NULL (i.e. we have a real device). */
348 if (!sdi->driver->dev_open) {
349 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
353 session->devs = g_slist_append(session->devs, sdi);
354 sdi->session = session;
356 /* TODO: This is invalid if the session runs in a different thread.
357 * The usage semantics and restrictions need to be documented.
359 if (session->running) {
360 /* Adding a device to a running session. Commit settings
361 * and start acquisition on that device now. */
362 if ((ret = sr_config_commit(sdi)) != SR_OK) {
363 sr_err("Failed to commit device settings before "
364 "starting acquisition in running session (%s)",
368 if ((ret = sdi->driver->dev_acquisition_start(sdi)) != SR_OK) {
369 sr_err("Failed to start acquisition of device in "
370 "running session (%s)", sr_strerror(ret));
379 * List all device instances attached to a session.
381 * @param session The session to use. Must not be NULL.
382 * @param devlist A pointer where the device instance list will be
383 * stored on return. If no devices are in the session,
384 * this will be NULL. Each element in the list points
385 * to a struct sr_dev_inst *.
386 * The list must be freed by the caller, but not the
387 * elements pointed to.
389 * @retval SR_OK Success.
390 * @retval SR_ERR_ARG Invalid argument.
394 SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist)
402 *devlist = g_slist_copy(session->devs);
408 * Remove a device instance from a session.
410 * @param session The session to remove from. Must not be NULL.
411 * @param sdi The device instance to remove from a session. Must not
412 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
415 * @retval SR_OK Success.
416 * @retval SR_ERR_ARG Invalid argument.
420 SR_API int sr_session_dev_remove(struct sr_session *session,
421 struct sr_dev_inst *sdi)
424 sr_err("%s: sdi was NULL", __func__);
429 sr_err("%s: session was NULL", __func__);
433 /* If sdi->session is not session, the device is not in this
435 if (sdi->session != session) {
436 sr_err("%s: not assigned to this session", __func__);
440 session->devs = g_slist_remove(session->devs, sdi);
447 * Remove all datafeed callbacks in a session.
449 * @param session The session to use. Must not be NULL.
451 * @retval SR_OK Success.
452 * @retval SR_ERR_ARG Invalid session passed.
456 SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session)
459 sr_err("%s: session was NULL", __func__);
463 g_slist_free_full(session->datafeed_callbacks, g_free);
464 session->datafeed_callbacks = NULL;
470 * Add a datafeed callback to a session.
472 * @param session The session to use. Must not be NULL.
473 * @param cb Function to call when a chunk of data is received.
475 * @param cb_data Opaque pointer passed in by the caller.
477 * @retval SR_OK Success.
478 * @retval SR_ERR_BUG No session exists.
482 SR_API int sr_session_datafeed_callback_add(struct sr_session *session,
483 sr_datafeed_callback cb, void *cb_data)
485 struct datafeed_callback *cb_struct;
488 sr_err("%s: session was NULL", __func__);
493 sr_err("%s: cb was NULL", __func__);
497 cb_struct = g_malloc0(sizeof(struct datafeed_callback));
499 cb_struct->cb_data = cb_data;
501 session->datafeed_callbacks =
502 g_slist_append(session->datafeed_callbacks, cb_struct);
508 * Get the trigger assigned to this session.
510 * @param session The session to use.
512 * @retval NULL Invalid (NULL) session was passed to the function.
513 * @retval other The trigger assigned to this session (can be NULL).
517 SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session)
522 return session->trigger;
526 * Set the trigger of this session.
528 * @param session The session to use. Must not be NULL.
529 * @param trig The trigger to assign to this session. Can be NULL.
531 * @retval SR_OK Success.
532 * @retval SR_ERR_ARG Invalid argument.
536 SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig)
541 session->trigger = trig;
546 static int verify_trigger(struct sr_trigger *trigger)
548 struct sr_trigger_stage *stage;
549 struct sr_trigger_match *match;
552 if (!trigger->stages) {
553 sr_err("No trigger stages defined.");
557 sr_spew("Checking trigger:");
558 for (l = trigger->stages; l; l = l->next) {
560 if (!stage->matches) {
561 sr_err("Stage %d has no matches defined.", stage->stage);
564 for (m = stage->matches; m; m = m->next) {
566 if (!match->channel) {
567 sr_err("Stage %d match has no channel.", stage->stage);
571 sr_err("Stage %d match is not defined.", stage->stage);
574 sr_spew("Stage %d match on channel %s, match %d", stage->stage,
575 match->channel->name, match->match);
582 /** Set up the main context the session will be executing in.
584 * Must be called just before the session starts, by the thread which
585 * will execute the session main loop. Once acquired, the main context
586 * pointer is immutable for the duration of the session run.
588 static int set_main_context(struct sr_session *session)
590 GMainContext *main_context;
592 g_mutex_lock(&session->main_mutex);
594 /* May happen if sr_session_start() is called a second time
595 * while the session is still running.
597 if (session->main_context) {
598 sr_err("Main context already set.");
600 g_mutex_unlock(&session->main_mutex);
603 main_context = g_main_context_ref_thread_default();
605 * Try to use an existing main context if possible, but only if we
606 * can make it owned by the current thread. Otherwise, create our
607 * own main context so that event source callbacks can execute in
608 * the session thread.
610 if (g_main_context_acquire(main_context)) {
611 g_main_context_release(main_context);
613 sr_dbg("Using thread-default main context.");
615 g_main_context_unref(main_context);
617 sr_dbg("Creating our own main context.");
618 main_context = g_main_context_new();
620 session->main_context = main_context;
622 g_mutex_unlock(&session->main_mutex);
627 /** Unset the main context used for the current session run.
629 * Must be called right after stopping the session. Note that if the
630 * session is stopped asynchronously, the main loop may still be running
631 * after the main context has been unset. This is OK as long as no new
632 * event sources are created -- the main loop holds its own reference
633 * to the main context.
635 static int unset_main_context(struct sr_session *session)
639 g_mutex_lock(&session->main_mutex);
641 if (session->main_context) {
642 g_main_context_unref(session->main_context);
643 session->main_context = NULL;
646 /* May happen if the set/unset calls are not matched.
648 sr_err("No main context to unset.");
651 g_mutex_unlock(&session->main_mutex);
656 static unsigned int session_source_attach(struct sr_session *session,
661 g_mutex_lock(&session->main_mutex);
663 if (session->main_context)
664 id = g_source_attach(source, session->main_context);
666 sr_err("Cannot add event source without main context.");
668 g_mutex_unlock(&session->main_mutex);
673 /* Idle handler; invoked when the number of registered event sources
674 * for a running session drops to zero.
676 static gboolean delayed_stop_check(void *data)
678 struct sr_session *session;
681 session->stop_check_id = 0;
683 /* Session already ended? */
684 if (!session->running)
685 return G_SOURCE_REMOVE;
687 /* New event sources may have been installed in the meantime. */
688 if (g_hash_table_size(session->event_sources) != 0)
689 return G_SOURCE_REMOVE;
691 session->running = FALSE;
692 unset_main_context(session);
696 /* This indicates a bug in user code, since it is not valid to
697 * restart or destroy a session while it may still be running.
699 if (!session->main_loop && !session->stopped_callback) {
700 sr_err("BUG: Session stop left unhandled.");
701 return G_SOURCE_REMOVE;
703 if (session->main_loop)
704 g_main_loop_quit(session->main_loop);
706 if (session->stopped_callback)
707 (*session->stopped_callback)(session->stopped_cb_data);
709 return G_SOURCE_REMOVE;
712 static int stop_check_later(struct sr_session *session)
715 unsigned int source_id;
717 if (session->stop_check_id != 0)
718 return SR_OK; /* idle handler already installed */
720 source = g_idle_source_new();
721 g_source_set_callback(source, &delayed_stop_check, session, NULL);
723 source_id = session_source_attach(session, source);
724 session->stop_check_id = source_id;
726 g_source_unref(source);
728 return (source_id != 0) ? SR_OK : SR_ERR;
734 * When this function returns with a status code indicating success, the
735 * session is running. Use sr_session_stopped_callback_set() to receive
736 * notification upon completion, or call sr_session_run() to block until
739 * Session events will be processed in the context of the current thread.
740 * If a thread-default GLib main context has been set, and is not owned by
741 * any other thread, it will be used. Otherwise, libsigrok will create its
742 * own main context for the current thread.
744 * @param session The session to use. Must not be NULL.
746 * @retval SR_OK Success.
747 * @retval SR_ERR_ARG Invalid session passed.
748 * @retval SR_ERR Other error.
752 SR_API int sr_session_start(struct sr_session *session)
754 struct sr_dev_inst *sdi;
755 struct sr_channel *ch;
756 GSList *l, *c, *lend;
760 sr_err("%s: session was NULL", __func__);
764 if (!session->devs) {
765 sr_err("%s: session->devs was NULL; a session "
766 "cannot be started without devices.", __func__);
770 if (session->running) {
771 sr_err("Cannot (re-)start session while it is still running.");
775 if (session->trigger) {
776 ret = verify_trigger(session->trigger);
781 /* Check enabled channels and commit settings of all devices. */
782 for (l = session->devs; l; l = l->next) {
784 for (c = sdi->channels; c; c = c->next) {
790 sr_err("%s device %s has no enabled channels.",
791 sdi->driver->name, sdi->connection_id);
795 ret = sr_config_commit(sdi);
797 sr_err("Failed to commit %s device %s settings "
798 "before starting acquisition.",
799 sdi->driver->name, sdi->connection_id);
804 ret = set_main_context(session);
808 sr_info("Starting.");
810 session->running = TRUE;
812 /* Have all devices start acquisition. */
813 for (l = session->devs; l; l = l->next) {
814 if (!(sdi = l->data)) {
815 sr_err("Device sdi was NULL, can't start session.");
819 ret = sdi->driver->dev_acquisition_start(sdi);
821 sr_err("Could not start %s device %s acquisition.",
822 sdi->driver->name, sdi->connection_id);
828 /* If there are multiple devices, some of them may already have
829 * started successfully. Stop them now before returning. */
831 for (l = session->devs; l != lend; l = l->next) {
833 if (sdi->driver->dev_acquisition_stop)
834 sdi->driver->dev_acquisition_stop(sdi);
836 /* TODO: Handle delayed stops. Need to iterate the event
838 session->running = FALSE;
840 unset_main_context(session);
844 if (g_hash_table_size(session->event_sources) == 0)
845 stop_check_later(session);
851 * Block until the running session stops.
853 * This is a convenience function which creates a GLib main loop and runs
854 * it to process session events until the session stops.
856 * Instead of using this function, applications may run their own GLib main
857 * loop, and use sr_session_stopped_callback_set() to receive notification
858 * when the session finished running.
860 * @param session The session to use. Must not be NULL.
862 * @retval SR_OK Success.
863 * @retval SR_ERR_ARG Invalid session passed.
864 * @retval SR_ERR Other error.
868 SR_API int sr_session_run(struct sr_session *session)
871 sr_err("%s: session was NULL", __func__);
874 if (!session->running) {
875 sr_err("No session running.");
878 if (session->main_loop) {
879 sr_err("Main loop already created.");
883 g_mutex_lock(&session->main_mutex);
885 if (!session->main_context) {
886 sr_err("Cannot run without main context.");
887 g_mutex_unlock(&session->main_mutex);
890 session->main_loop = g_main_loop_new(session->main_context, FALSE);
892 g_mutex_unlock(&session->main_mutex);
894 g_main_loop_run(session->main_loop);
896 g_main_loop_unref(session->main_loop);
897 session->main_loop = NULL;
902 static gboolean session_stop_sync(void *user_data)
904 struct sr_session *session;
905 struct sr_dev_inst *sdi;
910 if (!session->running)
911 return G_SOURCE_REMOVE;
913 sr_info("Stopping.");
915 for (node = session->devs; node; node = node->next) {
917 if (sdi->driver && sdi->driver->dev_acquisition_stop)
918 sdi->driver->dev_acquisition_stop(sdi);
921 return G_SOURCE_REMOVE;
927 * This requests the drivers of each device participating in the session to
928 * abort the acquisition as soon as possible. Even after this function returns,
929 * event processing still continues until all devices have actually stopped.
931 * Use sr_session_stopped_callback_set() to receive notification when the event
932 * processing finished.
934 * This function is reentrant. That is, it may be called from a different
935 * thread than the one executing the session, as long as it can be ensured
936 * that the session object is valid.
938 * If the session is not running, sr_session_stop() silently does nothing.
940 * @param session The session to use. Must not be NULL.
942 * @retval SR_OK Success.
943 * @retval SR_ERR_ARG Invalid session passed.
947 SR_API int sr_session_stop(struct sr_session *session)
949 GMainContext *main_context;
952 sr_err("%s: session was NULL", __func__);
956 g_mutex_lock(&session->main_mutex);
958 main_context = (session->main_context)
959 ? g_main_context_ref(session->main_context)
962 g_mutex_unlock(&session->main_mutex);
965 sr_dbg("No main context set; already stopped?");
966 /* Not an error; as it would be racy. */
969 g_main_context_invoke(main_context, &session_stop_sync, session);
970 g_main_context_unref(main_context);
976 * Return whether the session is currently running.
978 * Note that this function should be called from the same thread
979 * the session was started in.
981 * @param session The session to use. Must not be NULL.
983 * @retval TRUE Session is running.
984 * @retval FALSE Session is not running.
985 * @retval SR_ERR_ARG Invalid session passed.
989 SR_API int sr_session_is_running(struct sr_session *session)
992 sr_err("%s: session was NULL", __func__);
995 return session->running;
999 * Set the callback to be invoked after a session stopped running.
1001 * Install a callback to receive notification when a session run stopped.
1002 * This can be used to integrate session execution with an existing main
1003 * loop, without having to block in sr_session_run().
1005 * Note that the callback will be invoked in the context of the thread
1006 * that calls sr_session_start().
1008 * @param session The session to use. Must not be NULL.
1009 * @param cb The callback to invoke on session stop. May be NULL to unset.
1010 * @param cb_data User data pointer to be passed to the callback.
1012 * @retval SR_OK Success.
1013 * @retval SR_ERR_ARG Invalid session passed.
1017 SR_API int sr_session_stopped_callback_set(struct sr_session *session,
1018 sr_session_stopped_callback cb, void *cb_data)
1021 sr_err("%s: session was NULL", __func__);
1024 session->stopped_callback = cb;
1025 session->stopped_cb_data = cb_data;
1033 * @param packet The packet to show debugging information for.
1035 static void datafeed_dump(const struct sr_datafeed_packet *packet)
1037 const struct sr_datafeed_logic *logic;
1038 const struct sr_datafeed_analog_old *analog_old;
1039 const struct sr_datafeed_analog *analog;
1041 /* Please use the same order as in libsigrok.h. */
1042 switch (packet->type) {
1044 sr_dbg("bus: Received SR_DF_HEADER packet.");
1047 sr_dbg("bus: Received SR_DF_END packet.");
1050 sr_dbg("bus: Received SR_DF_META packet.");
1053 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
1056 logic = packet->payload;
1057 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, "
1058 "unitsize = %d).", logic->length, logic->unitsize);
1060 case SR_DF_ANALOG_OLD:
1061 analog_old = packet->payload;
1062 sr_dbg("bus: Received SR_DF_ANALOG_OLD packet (%d samples).",
1063 analog_old->num_samples);
1065 case SR_DF_FRAME_BEGIN:
1066 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
1068 case SR_DF_FRAME_END:
1069 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
1072 analog = packet->payload;
1073 sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).",
1074 analog->num_samples);
1077 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
1083 * Send a packet to whatever is listening on the datafeed bus.
1085 * Hardware drivers use this to send a data packet to the frontend.
1088 * @param packet The datafeed packet to send to the session bus.
1090 * @retval SR_OK Success.
1091 * @retval SR_ERR_ARG Invalid argument.
1095 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
1096 const struct sr_datafeed_packet *packet)
1099 struct datafeed_callback *cb_struct;
1100 struct sr_datafeed_packet *packet_in, *packet_out;
1101 struct sr_transform *t;
1105 sr_err("%s: sdi was NULL", __func__);
1110 sr_err("%s: packet was NULL", __func__);
1114 if (!sdi->session) {
1115 sr_err("%s: session was NULL", __func__);
1119 if (packet->type == SR_DF_ANALOG_OLD) {
1120 /* Convert to SR_DF_ANALOG. */
1121 const struct sr_datafeed_analog_old *analog_old = packet->payload;
1122 struct sr_analog_encoding encoding;
1123 struct sr_analog_meaning meaning;
1124 struct sr_analog_spec spec;
1125 struct sr_datafeed_analog analog;
1126 struct sr_datafeed_packet new_packet;
1127 new_packet.type = SR_DF_ANALOG;
1128 new_packet.payload = &analog;
1129 analog.data = analog_old->data;
1130 analog.num_samples = analog_old->num_samples;
1131 analog.encoding = &encoding;
1132 analog.meaning = &meaning;
1133 analog.spec = &spec;
1134 encoding.unitsize = sizeof(float);
1135 encoding.is_signed = TRUE;
1136 encoding.is_float = TRUE;
1137 #ifdef WORDS_BIGENDIAN
1138 encoding.is_bigendian = TRUE;
1140 encoding.is_bigendian = FALSE;
1142 encoding.digits = 0;
1143 encoding.is_digits_decimal = FALSE;
1144 encoding.scale.p = 1;
1145 encoding.scale.q = 1;
1146 encoding.offset.p = 0;
1147 encoding.offset.q = 1;
1148 meaning.mq = analog_old->mq;
1149 meaning.unit = analog_old->unit;
1150 meaning.mqflags = analog_old->mqflags;
1151 meaning.channels = analog_old->channels;
1152 spec.spec_digits = 0;
1153 return sr_session_send(sdi, &new_packet);
1157 * Pass the packet to the first transform module. If that returns
1158 * another packet (instead of NULL), pass that packet to the next
1159 * transform module in the list, and so on.
1161 packet_in = (struct sr_datafeed_packet *)packet;
1162 for (l = sdi->session->transforms; l; l = l->next) {
1164 sr_spew("Running transform module '%s'.", t->module->id);
1165 ret = t->module->receive(t, packet_in, &packet_out);
1167 sr_err("Error while running transform module: %d.", ret);
1172 * If any of the transforms don't return an output
1175 sr_spew("Transform module didn't return a packet, aborting.");
1179 * Use this transform module's output packet as input
1180 * for the next transform module.
1182 packet_in = packet_out;
1188 * If the last transform did output a packet, pass it to all datafeed
1191 for (l = sdi->session->datafeed_callbacks; l; l = l->next) {
1192 if (sr_log_loglevel_get() >= SR_LOG_DBG)
1193 datafeed_dump(packet);
1194 cb_struct = l->data;
1195 cb_struct->cb(sdi, packet, cb_struct->cb_data);
1202 * Add an event source for a file descriptor.
1204 * @param session The session to use. Must not be NULL.
1205 * @param key The key which identifies the event source.
1206 * @param source An event source object. Must not be NULL.
1208 * @retval SR_OK Success.
1209 * @retval SR_ERR_ARG Invalid argument.
1210 * @retval SR_ERR_BUG Event source with @a key already installed.
1211 * @retval SR_ERR Other error.
1215 SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
1216 void *key, GSource *source)
1219 * This must not ever happen, since the source has already been
1220 * created and its finalize() method will remove the key for the
1221 * already installed source. (Well it would, if we did not have
1222 * another sanity check there.)
1224 if (g_hash_table_contains(session->event_sources, key)) {
1225 sr_err("Event source with key %p already exists.", key);
1228 g_hash_table_insert(session->event_sources, key, source);
1230 if (session_source_attach(session, source) == 0)
1236 SR_PRIV int sr_session_fd_source_add(struct sr_session *session,
1237 void *key, gintptr fd, int events, int timeout,
1238 sr_receive_data_callback cb, void *cb_data)
1243 source = fd_source_new(session, key, fd, events, timeout);
1247 g_source_set_callback(source, (GSourceFunc)cb, cb_data, NULL);
1249 ret = sr_session_source_add_internal(session, key, source);
1250 g_source_unref(source);
1256 * Add an event source for a file descriptor.
1258 * @param session The session to use. Must not be NULL.
1259 * @param fd The file descriptor, or a negative value to create a timer source.
1260 * @param events Events to check for.
1261 * @param timeout Max time in ms to wait before the callback is called,
1262 * or -1 to wait indefinitely.
1263 * @param cb Callback function to add. Must not be NULL.
1264 * @param cb_data Data for the callback function. Can be NULL.
1266 * @retval SR_OK Success.
1267 * @retval SR_ERR_ARG Invalid argument.
1272 SR_PRIV int sr_session_source_add(struct sr_session *session, int fd,
1273 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
1275 if (fd < 0 && timeout < 0) {
1276 sr_err("Cannot create timer source without timeout.");
1279 return sr_session_fd_source_add(session, GINT_TO_POINTER(fd),
1280 fd, events, timeout, cb, cb_data);
1284 * Add an event source for a GPollFD.
1286 * @param session The session to use. Must not be NULL.
1287 * @param pollfd The GPollFD. Must not be NULL.
1288 * @param timeout Max time in ms to wait before the callback is called,
1289 * or -1 to wait indefinitely.
1290 * @param cb Callback function to add. Must not be NULL.
1291 * @param cb_data Data for the callback function. Can be NULL.
1293 * @retval SR_OK Success.
1294 * @retval SR_ERR_ARG Invalid argument.
1299 SR_PRIV int sr_session_source_add_pollfd(struct sr_session *session,
1300 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
1304 sr_err("%s: pollfd was NULL", __func__);
1307 return sr_session_fd_source_add(session, pollfd, pollfd->fd,
1308 pollfd->events, timeout, cb, cb_data);
1312 * Add an event source for a GIOChannel.
1314 * @param session The session to use. Must not be NULL.
1315 * @param channel The GIOChannel.
1316 * @param events Events to poll on.
1317 * @param timeout Max time in ms to wait before the callback is called,
1318 * or -1 to wait indefinitely.
1319 * @param cb Callback function to add. Must not be NULL.
1320 * @param cb_data Data for the callback function. Can be NULL.
1322 * @retval SR_OK Success.
1323 * @retval SR_ERR_ARG Invalid argument.
1328 SR_PRIV int sr_session_source_add_channel(struct sr_session *session,
1329 GIOChannel *channel, int events, int timeout,
1330 sr_receive_data_callback cb, void *cb_data)
1335 sr_err("%s: channel was NULL", __func__);
1338 /* We should be using g_io_create_watch(), but can't without
1339 * changing the driver API, as the callback signature is different.
1342 g_io_channel_win32_make_pollfd(channel, events, &pollfd);
1344 pollfd.fd = g_io_channel_unix_get_fd(channel);
1345 pollfd.events = events;
1347 return sr_session_fd_source_add(session, channel, pollfd.fd,
1348 pollfd.events, timeout, cb, cb_data);
1352 * Remove the source identified by the specified poll object.
1354 * @param session The session to use. Must not be NULL.
1355 * @param key The key by which the source is identified.
1357 * @retval SR_OK Success
1358 * @retval SR_ERR_BUG No event source for poll_object found.
1362 SR_PRIV int sr_session_source_remove_internal(struct sr_session *session,
1367 source = g_hash_table_lookup(session->event_sources, key);
1369 * Trying to remove an already removed event source is problematic
1370 * since the poll_object handle may have been reused in the meantime.
1373 sr_warn("Cannot remove non-existing event source %p.", key);
1376 g_source_destroy(source);
1382 * Remove the source belonging to the specified file descriptor.
1384 * @param session The session to use. Must not be NULL.
1385 * @param fd The file descriptor for which the source should be removed.
1387 * @retval SR_OK Success
1388 * @retval SR_ERR_ARG Invalid argument
1389 * @retval SR_ERR_BUG Internal error.
1394 SR_PRIV int sr_session_source_remove(struct sr_session *session, int fd)
1396 return sr_session_source_remove_internal(session, GINT_TO_POINTER(fd));
1400 * Remove the source belonging to the specified poll descriptor.
1402 * @param session The session to use. Must not be NULL.
1403 * @param pollfd The poll descriptor for which the source should be removed.
1405 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
1406 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
1412 SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session,
1416 sr_err("%s: pollfd was NULL", __func__);
1419 return sr_session_source_remove_internal(session, pollfd);
1423 * Remove the source belonging to the specified channel.
1425 * @param session The session to use. Must not be NULL.
1426 * @param channel The channel for which the source should be removed.
1428 * @retval SR_OK Success.
1429 * @retval SR_ERR_ARG Invalid argument.
1430 * @return SR_ERR_BUG Internal error.
1435 SR_PRIV int sr_session_source_remove_channel(struct sr_session *session,
1436 GIOChannel *channel)
1439 sr_err("%s: channel was NULL", __func__);
1442 return sr_session_source_remove_internal(session, channel);
1445 /** Unregister an event source that has been destroyed.
1447 * This is intended to be called from a source's finalize() method.
1449 * @param session The session to use. Must not be NULL.
1450 * @param key The key used to identify @a source.
1451 * @param source The source object that was destroyed.
1453 * @retval SR_OK Success.
1454 * @retval SR_ERR_BUG Event source for @a key does not match @a source.
1455 * @retval SR_ERR Other error.
1459 SR_PRIV int sr_session_source_destroyed(struct sr_session *session,
1460 void *key, GSource *source)
1462 GSource *registered_source;
1464 registered_source = g_hash_table_lookup(session->event_sources, key);
1466 * Trying to remove an already removed event source is problematic
1467 * since the poll_object handle may have been reused in the meantime.
1469 if (!registered_source) {
1470 sr_err("No event source for key %p found.", key);
1473 if (registered_source != source) {
1474 sr_err("Event source for key %p does not match"
1475 " destroyed source.", key);
1478 g_hash_table_remove(session->event_sources, key);
1480 if (g_hash_table_size(session->event_sources) > 0)
1483 /* If no event sources are left, consider the acquisition finished.
1484 * This is pretty crude, as it requires all event sources to be
1485 * registered via the libsigrok API.
1487 return stop_check_later(session);
1490 static void copy_src(struct sr_config *src, struct sr_datafeed_meta *meta_copy)
1492 g_variant_ref(src->data);
1493 meta_copy->config = g_slist_append(meta_copy->config,
1494 g_memdup(src, sizeof(struct sr_config)));
1497 SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet,
1498 struct sr_datafeed_packet **copy)
1500 const struct sr_datafeed_meta *meta;
1501 struct sr_datafeed_meta *meta_copy;
1502 const struct sr_datafeed_logic *logic;
1503 struct sr_datafeed_logic *logic_copy;
1504 const struct sr_datafeed_analog_old *analog_old;
1505 struct sr_datafeed_analog_old *analog_old_copy;
1506 const struct sr_datafeed_analog *analog;
1507 struct sr_datafeed_analog *analog_copy;
1510 *copy = g_malloc0(sizeof(struct sr_datafeed_packet));
1511 (*copy)->type = packet->type;
1513 switch (packet->type) {
1519 payload = g_malloc(sizeof(struct sr_datafeed_header));
1520 memcpy(payload, packet->payload, sizeof(struct sr_datafeed_header));
1521 (*copy)->payload = payload;
1524 meta = packet->payload;
1525 meta_copy = g_malloc0(sizeof(struct sr_datafeed_meta));
1526 g_slist_foreach(meta->config, (GFunc)copy_src, meta_copy->config);
1527 (*copy)->payload = meta_copy;
1530 logic = packet->payload;
1531 logic_copy = g_malloc(sizeof(*logic_copy));
1532 logic_copy->length = logic->length;
1533 logic_copy->unitsize = logic->unitsize;
1534 memcpy(logic_copy->data, logic->data, logic->length * logic->unitsize);
1535 (*copy)->payload = logic_copy;
1537 case SR_DF_ANALOG_OLD:
1538 analog_old = packet->payload;
1539 analog_old_copy = g_malloc(sizeof(*analog_old_copy));
1540 analog_old_copy->channels = g_slist_copy(analog_old->channels);
1541 analog_old_copy->num_samples = analog_old->num_samples;
1542 analog_old_copy->mq = analog_old->mq;
1543 analog_old_copy->unit = analog_old->unit;
1544 analog_old_copy->mqflags = analog_old->mqflags;
1545 analog_old_copy->data = g_malloc(analog_old->num_samples * sizeof(float));
1546 memcpy(analog_old_copy->data, analog_old->data,
1547 analog_old->num_samples * sizeof(float));
1548 (*copy)->payload = analog_old_copy;
1551 analog = packet->payload;
1552 analog_copy = g_malloc(sizeof(*analog_copy));
1553 analog_copy->data = g_malloc(
1554 analog->encoding->unitsize * analog->num_samples);
1555 memcpy(analog_copy->data, analog->data,
1556 analog->encoding->unitsize * analog->num_samples);
1557 analog_copy->num_samples = analog->num_samples;
1558 analog_copy->encoding = g_memdup(analog->encoding,
1559 sizeof(struct sr_analog_encoding));
1560 analog_copy->meaning = g_memdup(analog->meaning,
1561 sizeof(struct sr_analog_meaning));
1562 analog_copy->meaning->channels = g_slist_copy(
1563 analog->meaning->channels);
1564 analog_copy->spec = g_memdup(analog->spec,
1565 sizeof(struct sr_analog_spec));
1566 (*copy)->payload = analog_copy;
1569 sr_err("Unknown packet type %d", packet->type);
1576 void sr_packet_free(struct sr_datafeed_packet *packet)
1578 const struct sr_datafeed_meta *meta;
1579 const struct sr_datafeed_logic *logic;
1580 const struct sr_datafeed_analog_old *analog_old;
1581 const struct sr_datafeed_analog *analog;
1582 struct sr_config *src;
1585 switch (packet->type) {
1591 /* Payload is a simple struct. */
1592 g_free((void *)packet->payload);
1595 meta = packet->payload;
1596 for (l = meta->config; l; l = l->next) {
1598 g_variant_unref(src->data);
1601 g_slist_free(meta->config);
1602 g_free((void *)packet->payload);
1605 logic = packet->payload;
1606 g_free(logic->data);
1607 g_free((void *)packet->payload);
1609 case SR_DF_ANALOG_OLD:
1610 analog_old = packet->payload;
1611 g_slist_free(analog_old->channels);
1612 g_free(analog_old->data);
1613 g_free((void *)packet->payload);
1616 analog = packet->payload;
1617 g_free(analog->data);
1618 g_free(analog->encoding);
1619 g_slist_free(analog->meaning->channels);
1620 g_free(analog->meaning);
1621 g_free(analog->spec);
1622 g_free((void *)packet->payload);
1625 sr_err("Unknown packet type %d", packet->type);