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/>.
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
30 #define LOG_PREFIX "session"
36 * Creating, using, or destroying libsigrok sessions.
40 * @defgroup grp_session Session handling
42 * Creating, using, or destroying libsigrok sessions.
48 int64_t timeout; /* microseconds */
49 int64_t due; /* microseconds */
51 sr_receive_data_callback cb;
54 /* This is used to keep track of the object (fd, pollfd or channel) which is
55 * being polled and will be used to match the source when removing it again.
59 /* Number of fds to poll for this source. This will be 0 for timer
60 * sources, 1 for normal I/O sources, and 1 or more for libusb I/O
61 * sources on Unix platforms.
68 struct datafeed_callback {
69 sr_datafeed_callback cb;
74 * Create a new session.
76 * @param ctx The context in which to create the new session.
77 * @param new_session This will contain a pointer to the newly created
78 * session if the return value is SR_OK, otherwise the value
79 * is undefined and should not be used. Must not be NULL.
81 * @retval SR_OK Success.
82 * @retval SR_ERR_ARG Invalid argument.
86 SR_API int sr_session_new(struct sr_context *ctx,
87 struct sr_session **new_session)
89 struct sr_session *session;
94 session = g_malloc0(sizeof(struct sr_session));
98 session->sources = g_array_new(FALSE, FALSE, sizeof(struct source));
99 session->pollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
101 g_mutex_init(&session->stop_mutex);
103 *new_session = session;
110 * This frees up all memory used by the session.
112 * @param session The session to destroy. Must not be NULL.
114 * @retval SR_OK Success.
115 * @retval SR_ERR_ARG Invalid session passed.
119 SR_API int sr_session_destroy(struct sr_session *session)
122 sr_err("%s: session was NULL", __func__);
126 sr_session_dev_remove_all(session);
127 g_mutex_clear(&session->stop_mutex);
128 if (session->trigger)
129 sr_trigger_free(session->trigger);
131 g_slist_free_full(session->owned_devs, (GDestroyNotify)sr_dev_inst_free);
133 g_array_unref(session->pollfds);
134 g_array_unref(session->sources);
142 * Remove all the devices from a session.
144 * The session itself (i.e., the struct sr_session) is not free'd and still
145 * exists after this function returns.
147 * @param session The session to use. Must not be NULL.
149 * @retval SR_OK Success.
150 * @retval SR_ERR_BUG Invalid session passed.
154 SR_API int sr_session_dev_remove_all(struct sr_session *session)
156 struct sr_dev_inst *sdi;
160 sr_err("%s: session was NULL", __func__);
164 for (l = session->devs; l; l = l->next) {
165 sdi = (struct sr_dev_inst *) l->data;
169 g_slist_free(session->devs);
170 session->devs = NULL;
176 * Add a device instance to a session.
178 * @param session The session to add to. Must not be NULL.
179 * @param sdi The device instance to add to a session. Must not
180 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
183 * @retval SR_OK Success.
184 * @retval SR_ERR_ARG Invalid argument.
188 SR_API int sr_session_dev_add(struct sr_session *session,
189 struct sr_dev_inst *sdi)
194 sr_err("%s: sdi was NULL", __func__);
199 sr_err("%s: session was NULL", __func__);
203 /* If sdi->session is not NULL, the device is already in this or
204 * another session. */
206 sr_err("%s: already assigned to session", __func__);
210 /* If sdi->driver is NULL, this is a virtual device. */
212 /* Just add the device, don't run dev_open(). */
213 session->devs = g_slist_append(session->devs, (gpointer)sdi);
214 sdi->session = session;
218 /* sdi->driver is non-NULL (i.e. we have a real device). */
219 if (!sdi->driver->dev_open) {
220 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
224 session->devs = g_slist_append(session->devs, (gpointer)sdi);
225 sdi->session = session;
227 if (session->running) {
228 /* Adding a device to a running session. Commit settings
229 * and start acquisition on that device now. */
230 if ((ret = sr_config_commit(sdi)) != SR_OK) {
231 sr_err("Failed to commit device settings before "
232 "starting acquisition in running session (%s)",
236 if ((ret = sdi->driver->dev_acquisition_start(sdi,
237 (void *)sdi)) != SR_OK) {
238 sr_err("Failed to start acquisition of device in "
239 "running session (%s)", sr_strerror(ret));
248 * List all device instances attached to a session.
250 * @param session The session to use. Must not be NULL.
251 * @param devlist A pointer where the device instance list will be
252 * stored on return. If no devices are in the session,
253 * this will be NULL. Each element in the list points
254 * to a struct sr_dev_inst *.
255 * The list must be freed by the caller, but not the
256 * elements pointed to.
258 * @retval SR_OK Success.
259 * @retval SR_ERR_ARG Invalid argument.
263 SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist)
271 *devlist = g_slist_copy(session->devs);
277 * Remove all datafeed callbacks in a session.
279 * @param session The session to use. Must not be NULL.
281 * @retval SR_OK Success.
282 * @retval SR_ERR_ARG Invalid session passed.
286 SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session)
289 sr_err("%s: session was NULL", __func__);
293 g_slist_free_full(session->datafeed_callbacks, g_free);
294 session->datafeed_callbacks = NULL;
300 * Add a datafeed callback to a session.
302 * @param session The session to use. Must not be NULL.
303 * @param cb Function to call when a chunk of data is received.
305 * @param cb_data Opaque pointer passed in by the caller.
307 * @retval SR_OK Success.
308 * @retval SR_ERR_BUG No session exists.
312 SR_API int sr_session_datafeed_callback_add(struct sr_session *session,
313 sr_datafeed_callback cb, void *cb_data)
315 struct datafeed_callback *cb_struct;
318 sr_err("%s: session was NULL", __func__);
323 sr_err("%s: cb was NULL", __func__);
327 cb_struct = g_malloc0(sizeof(struct datafeed_callback));
329 cb_struct->cb_data = cb_data;
331 session->datafeed_callbacks =
332 g_slist_append(session->datafeed_callbacks, cb_struct);
338 * Get the trigger assigned to this session.
340 * @param session The session to use.
342 * @retval NULL Invalid (NULL) session was passed to the function.
343 * @retval other The trigger assigned to this session (can be NULL).
347 SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session)
352 return session->trigger;
356 * Set the trigger of this session.
358 * @param session The session to use. Must not be NULL.
359 * @param trig The trigger to assign to this session. Can be NULL.
361 * @retval SR_OK Success.
362 * @retval SR_ERR_ARG Invalid argument.
366 SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig)
371 session->trigger = trig;
376 static gboolean sr_session_check_aborted(struct sr_session *session)
380 g_mutex_lock(&session->stop_mutex);
381 stop = session->abort_session;
383 sr_session_stop_sync(session);
384 /* But once is enough. */
385 session->abort_session = FALSE;
387 g_mutex_unlock(&session->stop_mutex);
393 * Poll the session's event sources.
395 * @param session The session to use. Must not be NULL.
396 * @retval SR_OK Success.
397 * @retval SR_ERR Error occurred.
399 static int sr_session_iteration(struct sr_session *session)
401 int64_t start_time, stop_time, min_due, due;
408 gboolean triggered, stopped;
409 struct source *source;
412 #if HAVE_LIBUSB_1_0 && !defined(G_OS_WIN32)
417 if (session->sources->len == 0) {
418 sr_session_check_aborted(session);
421 start_time = g_get_monotonic_time();
424 for (i = 0; i < session->sources->len; ++i) {
425 source = &g_array_index(session->sources, struct source, i);
426 if (source->due < min_due)
427 min_due = source->due;
428 source->triggered = FALSE;
430 #if HAVE_LIBUSB_1_0 && !defined(G_OS_WIN32)
432 if (session->ctx->usb_source_present) {
433 ret = libusb_get_next_timeout(session->ctx->libusb_ctx, &tv);
435 sr_err("Error getting libusb timeout: %s",
436 libusb_error_name(ret));
438 } else if (ret == 1) {
439 usb_timeout = (int64_t)tv.tv_sec * G_USEC_PER_SEC
441 usb_due = start_time + usb_timeout;
442 if (usb_due < min_due)
445 sr_spew("poll: next USB timeout %g ms",
450 if (min_due == INT64_MAX)
452 else if (min_due > start_time)
453 timeout_ms = MIN((min_due - start_time + 999) / 1000, INT_MAX);
457 sr_spew("poll enter: %u sources, %u fds, %d ms timeout",
458 session->sources->len, session->pollfds->len, timeout_ms);
460 ret = g_poll((GPollFD *)session->pollfds->data,
461 session->pollfds->len, timeout_ms);
463 if (ret < 0 && errno != EINTR) {
464 sr_err("Error in poll: %s", g_strerror(errno));
469 sr_err("Error in poll: %d", ret);
473 stop_time = g_get_monotonic_time();
475 sr_spew("poll leave: %g ms elapsed, %d events",
476 1e-3 * (stop_time - start_time), ret);
482 for (i = 0; i < session->sources->len; ++i) {
483 source = &g_array_index(session->sources, struct source, i);
485 poll_object = source->poll_object;
486 fd = (int)poll_object;
489 for (k = 0; k < source->num_fds; ++k) {
490 pollfd = &g_array_index(session->pollfds,
491 GPollFD, fd_index + k);
493 revents |= pollfd->revents;
495 fd_index += source->num_fds;
497 if (source->triggered)
498 continue; /* already handled */
499 if (ret > 0 && revents == 0)
500 continue; /* skip timeouts if any I/O event occurred */
502 /* Make invalid to avoid confusion in case of multiple FDs. */
503 if (source->num_fds > 1)
509 #if HAVE_LIBUSB_1_0 && !defined(G_OS_WIN32)
510 if (usb_due < due && poll_object
511 == (gintptr)session->ctx->libusb_ctx)
514 if (revents == 0 && stop_time < due)
517 * The source may be gone after the callback returns,
518 * so access any data now that needs accessing.
520 if (source->timeout >= 0)
521 source->due = stop_time + source->timeout;
522 source->triggered = TRUE;
525 * Invoke the source's callback on an event or timeout.
527 if (!source->cb(fd, revents, source->cb_data))
528 sr_session_source_remove_internal(session, poll_object);
530 * We want to take as little time as possible to stop
531 * the session if we have been told to do so. Therefore,
532 * we check the flag after processing every source, not
533 * just once per main event loop.
536 stopped = sr_session_check_aborted(session);
538 /* Restart loop as the sources list may have changed. */
543 /* Check for abort at least once per iteration. */
545 sr_session_check_aborted(session);
550 static int verify_trigger(struct sr_trigger *trigger)
552 struct sr_trigger_stage *stage;
553 struct sr_trigger_match *match;
556 if (!trigger->stages) {
557 sr_err("No trigger stages defined.");
561 sr_spew("Checking trigger:");
562 for (l = trigger->stages; l; l = l->next) {
564 if (!stage->matches) {
565 sr_err("Stage %d has no matches defined.", stage->stage);
568 for (m = stage->matches; m; m = m->next) {
570 if (!match->channel) {
571 sr_err("Stage %d match has no channel.", stage->stage);
575 sr_err("Stage %d match is not defined.", stage->stage);
578 sr_spew("Stage %d match on channel %s, match %d", stage->stage,
579 match->channel->name, match->match);
589 * @param session The session to use. Must not be NULL.
591 * @retval SR_OK Success.
592 * @retval SR_ERR_ARG Invalid session passed.
596 SR_API int sr_session_start(struct sr_session *session)
598 struct sr_dev_inst *sdi;
599 struct sr_channel *ch;
601 int enabled_channels, ret;
604 sr_err("%s: session was NULL", __func__);
608 if (!session->devs) {
609 sr_err("%s: session->devs was NULL; a session "
610 "cannot be started without devices.", __func__);
614 if (session->trigger && verify_trigger(session->trigger) != SR_OK)
617 sr_info("Starting.");
620 for (l = session->devs; l; l = l->next) {
622 enabled_channels = 0;
623 for (c = sdi->channels; c; c = c->next) {
630 if (enabled_channels == 0) {
632 sr_err("%s using connection %s has no enabled channels!",
633 sdi->driver->name, sdi->connection_id);
637 if ((ret = sr_config_commit(sdi)) != SR_OK) {
638 sr_err("Failed to commit device settings before "
639 "starting acquisition (%s)", sr_strerror(ret));
642 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
643 sr_err("%s: could not start an acquisition "
644 "(%s)", __func__, sr_strerror(ret));
649 /* TODO: What if there are multiple devices? Which return code? */
657 * @param session The session to use. Must not be NULL.
659 * @retval SR_OK Success.
660 * @retval SR_ERR_ARG Invalid session passed.
661 * @retval SR_ERR Error during event processing.
665 SR_API int sr_session_run(struct sr_session *session)
670 sr_err("%s: session was NULL", __func__);
674 if (!session->devs) {
675 /* TODO: Actually the case? */
676 sr_err("%s: session->devs was NULL; a session "
677 "cannot be run without devices.", __func__);
680 session->running = TRUE;
684 /* Poll event sources until none are left. */
685 while (session->sources->len > 0) {
686 ret = sr_session_iteration(session);
696 * The session is stopped immediately, with all acquisition sessions stopped
697 * and hardware drivers cleaned up.
699 * This must be called from within the session thread, to prevent freeing
700 * resources that the session thread will try to use.
702 * @param session The session to use. Must not be NULL.
704 * @retval SR_OK Success.
705 * @retval SR_ERR_ARG Invalid session passed.
709 SR_PRIV int sr_session_stop_sync(struct sr_session *session)
711 struct sr_dev_inst *sdi;
715 sr_err("%s: session was NULL", __func__);
719 sr_info("Stopping.");
721 for (l = session->devs; l; l = l->next) {
724 if (sdi->driver->dev_acquisition_stop)
725 sdi->driver->dev_acquisition_stop(sdi, sdi);
728 session->running = FALSE;
736 * The session is stopped immediately, with all acquisition sessions being
737 * stopped and hardware drivers cleaned up.
739 * If the session is run in a separate thread, this function will not block
740 * until the session is finished executing. It is the caller's responsibility
741 * to wait for the session thread to return before assuming that the session is
742 * completely decommissioned.
744 * @param session The session to use. Must not be NULL.
746 * @retval SR_OK Success.
747 * @retval SR_ERR_ARG Invalid session passed.
751 SR_API int sr_session_stop(struct sr_session *session)
754 sr_err("%s: session was NULL", __func__);
758 g_mutex_lock(&session->stop_mutex);
759 session->abort_session = TRUE;
760 g_mutex_unlock(&session->stop_mutex);
768 * @param packet The packet to show debugging information for.
770 static void datafeed_dump(const struct sr_datafeed_packet *packet)
772 const struct sr_datafeed_logic *logic;
773 const struct sr_datafeed_analog *analog;
774 const struct sr_datafeed_analog2 *analog2;
776 /* Please use the same order as in libsigrok.h. */
777 switch (packet->type) {
779 sr_dbg("bus: Received SR_DF_HEADER packet.");
782 sr_dbg("bus: Received SR_DF_END packet.");
785 sr_dbg("bus: Received SR_DF_META packet.");
788 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
791 logic = packet->payload;
792 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, "
793 "unitsize = %d).", logic->length, logic->unitsize);
796 analog = packet->payload;
797 sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).",
798 analog->num_samples);
800 case SR_DF_FRAME_BEGIN:
801 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
803 case SR_DF_FRAME_END:
804 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
807 analog2 = packet->payload;
808 sr_dbg("bus: Received SR_DF_ANALOG2 packet (%d samples).",
809 analog2->num_samples);
812 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
818 * Send a packet to whatever is listening on the datafeed bus.
820 * Hardware drivers use this to send a data packet to the frontend.
823 * @param packet The datafeed packet to send to the session bus.
825 * @retval SR_OK Success.
826 * @retval SR_ERR_ARG Invalid argument.
830 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
831 const struct sr_datafeed_packet *packet)
834 struct datafeed_callback *cb_struct;
835 struct sr_datafeed_packet *packet_in, *packet_out;
836 struct sr_transform *t;
840 sr_err("%s: sdi was NULL", __func__);
845 sr_err("%s: packet was NULL", __func__);
850 sr_err("%s: session was NULL", __func__);
855 * Pass the packet to the first transform module. If that returns
856 * another packet (instead of NULL), pass that packet to the next
857 * transform module in the list, and so on.
859 packet_in = (struct sr_datafeed_packet *)packet;
860 for (l = sdi->session->transforms; l; l = l->next) {
862 sr_spew("Running transform module '%s'.", t->module->id);
863 ret = t->module->receive(t, packet_in, &packet_out);
865 sr_err("Error while running transform module: %d.", ret);
870 * If any of the transforms don't return an output
873 sr_spew("Transform module didn't return a packet, aborting.");
877 * Use this transform module's output packet as input
878 * for the next transform module.
880 packet_in = packet_out;
886 * If the last transform did output a packet, pass it to all datafeed
889 for (l = sdi->session->datafeed_callbacks; l; l = l->next) {
890 if (sr_log_loglevel_get() >= SR_LOG_DBG)
891 datafeed_dump(packet);
893 cb_struct->cb(sdi, packet, cb_struct->cb_data);
900 * Add an event source for a file descriptor.
902 * @param session The session to use. Must not be NULL.
903 * @param[in] pollfds The FDs to poll, or NULL if @a num_fds is 0.
904 * @param[in] num_fds Number of FDs in the array.
905 * @param[in] timeout Max time in ms to wait before the callback is called,
906 * or -1 to wait indefinitely.
907 * @param cb Callback function to add. Must not be NULL.
908 * @param cb_data Data for the callback function. Can be NULL.
909 * @param poll_object Handle by which the source is identified
911 * @retval SR_OK Success.
912 * @retval SR_ERR_ARG Invalid argument.
913 * @retval SR_ERR An event source for @a poll_object is already installed.
915 SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
916 const GPollFD *pollfds, int num_fds, int timeout,
917 sr_receive_data_callback cb, void *cb_data,
923 /* Note: cb_data can be NULL, that's not a bug. */
925 sr_err("%s: cb was NULL", __func__);
928 if (!pollfds && num_fds != 0) {
929 sr_err("%s: pollfds was NULL", __func__);
932 /* Make sure that poll_object is unique.
934 for (i = 0; i < session->sources->len; ++i) {
935 if (g_array_index(session->sources, struct source, i)
936 .poll_object == poll_object) {
937 sr_err("Event source for object %" G_GINTPTR_FORMAT
938 " already installed.", poll_object);
943 src.cb_data = cb_data;
944 src.poll_object = poll_object;
945 src.num_fds = num_fds;
946 src.triggered = FALSE;
949 src.timeout = INT64_C(1000) * timeout;
950 src.due = g_get_monotonic_time() + src.timeout;
955 g_array_append_val(session->sources, src);
958 g_array_append_vals(session->pollfds, pollfds, num_fds);
964 * Add an event source for a file descriptor.
966 * @param session The session to use. Must not be NULL.
967 * @param fd The file descriptor.
968 * @param events Events to check for.
969 * @param timeout Max time in ms to wait before the callback is called,
970 * or -1 to wait indefinitely.
971 * @param cb Callback function to add. Must not be NULL.
972 * @param cb_data Data for the callback function. Can be NULL.
974 * @retval SR_OK Success.
975 * @retval SR_ERR_ARG Invalid argument.
979 SR_API int sr_session_source_add(struct sr_session *session, int fd,
980 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
984 if (fd < 0 && timeout < 0) {
985 sr_err("Timer source without timeout would block indefinitely");
992 return sr_session_source_add_internal(session,
993 &p, (fd < 0) ? 0 : 1, timeout, cb, cb_data, fd);
997 * Add an event source for a GPollFD.
999 * @param session The session to use. Must not be NULL.
1000 * @param pollfd The GPollFD. Must not be NULL.
1001 * @param timeout Max time in ms to wait before the callback is called,
1002 * or -1 to wait indefinitely.
1003 * @param cb Callback function to add. Must not be NULL.
1004 * @param cb_data Data for the callback function. Can be NULL.
1006 * @retval SR_OK Success.
1007 * @retval SR_ERR_ARG Invalid argument.
1011 SR_API int sr_session_source_add_pollfd(struct sr_session *session,
1012 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
1016 sr_err("%s: pollfd was NULL", __func__);
1019 return sr_session_source_add_internal(session, pollfd, 1,
1020 timeout, cb, cb_data, (gintptr)pollfd);
1024 * Add an event source for a GIOChannel.
1026 * @param session The session to use. Must not be NULL.
1027 * @param channel The GIOChannel.
1028 * @param events Events to poll on.
1029 * @param timeout Max time in ms to wait before the callback is called,
1030 * or -1 to wait indefinitely.
1031 * @param cb Callback function to add. Must not be NULL.
1032 * @param cb_data Data for the callback function. Can be NULL.
1034 * @retval SR_OK Success.
1035 * @retval SR_ERR_ARG Invalid argument.
1039 SR_API int sr_session_source_add_channel(struct sr_session *session,
1040 GIOChannel *channel, int events, int timeout,
1041 sr_receive_data_callback cb, void *cb_data)
1046 g_io_channel_win32_make_pollfd(channel, events, &p);
1048 p.fd = g_io_channel_unix_get_fd(channel);
1052 return sr_session_source_add_internal(session, &p, 1,
1053 timeout, cb, cb_data, (gintptr)channel);
1057 * Remove the source identified by the specified poll object.
1059 * @param session The session to use. Must not be NULL.
1060 * @param poll_object The channel for which the source should be removed.
1062 * @retval SR_OK Success
1063 * @retval SR_ERR_BUG No event source for poll_object found.
1065 SR_PRIV int sr_session_source_remove_internal(struct sr_session *session,
1066 gintptr poll_object)
1068 struct source *source;
1072 for (i = 0; i < session->sources->len; ++i) {
1073 source = &g_array_index(session->sources, struct source, i);
1075 if (source->poll_object == poll_object) {
1076 if (source->num_fds > 0)
1077 g_array_remove_range(session->pollfds,
1078 fd_index, source->num_fds);
1079 g_array_remove_index(session->sources, i);
1081 * This is a bit of a hack. To be removed when
1082 * porting over to the GLib main loop.
1084 if (poll_object == (gintptr)session->ctx->libusb_ctx)
1085 session->ctx->usb_source_present = FALSE;
1088 fd_index += source->num_fds;
1090 /* Trying to remove an already removed event source is problematic
1091 * since the poll_object handle may have been reused in the meantime.
1093 sr_warn("Cannot remove non-existing event source for object %"
1094 G_GINTPTR_FORMAT ".", poll_object);
1100 * Remove the source belonging to the specified file descriptor.
1102 * @param session The session to use. Must not be NULL.
1103 * @param fd The file descriptor for which the source should be removed.
1105 * @retval SR_OK Success
1106 * @retval SR_ERR_ARG Invalid argument
1107 * @retval SR_ERR_BUG Internal error.
1111 SR_API int sr_session_source_remove(struct sr_session *session, int fd)
1113 return sr_session_source_remove_internal(session, fd);
1117 * Remove the source belonging to the specified poll descriptor.
1119 * @param session The session to use. Must not be NULL.
1120 * @param pollfd The poll descriptor for which the source should be removed.
1122 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
1123 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
1128 SR_API int sr_session_source_remove_pollfd(struct sr_session *session,
1132 sr_err("%s: pollfd was NULL", __func__);
1135 return sr_session_source_remove_internal(session, (gintptr)pollfd);
1139 * Remove the source belonging to the specified channel.
1141 * @param session The session to use. Must not be NULL.
1142 * @param channel The channel for which the source should be removed.
1144 * @retval SR_OK Success.
1145 * @retval SR_ERR_ARG Invalid argument.
1146 * @return SR_ERR_BUG Internal error.
1150 SR_API int sr_session_source_remove_channel(struct sr_session *session,
1151 GIOChannel *channel)
1154 sr_err("%s: channel was NULL", __func__);
1157 return sr_session_source_remove_internal(session, (gintptr)channel);
1160 static void copy_src(struct sr_config *src, struct sr_datafeed_meta *meta_copy)
1162 g_variant_ref(src->data);
1163 meta_copy->config = g_slist_append(meta_copy->config,
1164 g_memdup(src, sizeof(struct sr_config)));
1167 SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet,
1168 struct sr_datafeed_packet **copy)
1170 const struct sr_datafeed_meta *meta;
1171 struct sr_datafeed_meta *meta_copy;
1172 const struct sr_datafeed_logic *logic;
1173 struct sr_datafeed_logic *logic_copy;
1174 const struct sr_datafeed_analog *analog;
1175 struct sr_datafeed_analog *analog_copy;
1178 *copy = g_malloc0(sizeof(struct sr_datafeed_packet));
1179 (*copy)->type = packet->type;
1181 switch (packet->type) {
1187 payload = g_malloc(sizeof(struct sr_datafeed_header));
1188 memcpy(payload, packet->payload, sizeof(struct sr_datafeed_header));
1189 (*copy)->payload = payload;
1192 meta = packet->payload;
1193 meta_copy = g_malloc0(sizeof(struct sr_datafeed_meta));
1194 g_slist_foreach(meta->config, (GFunc)copy_src, meta_copy->config);
1195 (*copy)->payload = meta_copy;
1198 logic = packet->payload;
1199 logic_copy = g_malloc(sizeof(logic));
1200 logic_copy->length = logic->length;
1201 logic_copy->unitsize = logic->unitsize;
1202 memcpy(logic_copy->data, logic->data, logic->length * logic->unitsize);
1203 (*copy)->payload = logic_copy;
1206 analog = packet->payload;
1207 analog_copy = g_malloc(sizeof(analog));
1208 analog_copy->channels = g_slist_copy(analog->channels);
1209 analog_copy->num_samples = analog->num_samples;
1210 analog_copy->mq = analog->mq;
1211 analog_copy->unit = analog->unit;
1212 analog_copy->mqflags = analog->mqflags;
1213 memcpy(analog_copy->data, analog->data,
1214 analog->num_samples * sizeof(float));
1215 (*copy)->payload = analog_copy;
1218 sr_err("Unknown packet type %d", packet->type);
1225 void sr_packet_free(struct sr_datafeed_packet *packet)
1227 const struct sr_datafeed_meta *meta;
1228 const struct sr_datafeed_logic *logic;
1229 const struct sr_datafeed_analog *analog;
1230 struct sr_config *src;
1233 switch (packet->type) {
1239 /* Payload is a simple struct. */
1240 g_free((void *)packet->payload);
1243 meta = packet->payload;
1244 for (l = meta->config; l; l = l->next) {
1246 g_variant_unref(src->data);
1249 g_slist_free(meta->config);
1250 g_free((void *)packet->payload);
1253 logic = packet->payload;
1254 g_free(logic->data);
1255 g_free((void *)packet->payload);
1258 analog = packet->payload;
1259 g_slist_free(analog->channels);
1260 g_free(analog->data);
1261 g_free((void *)packet->payload);
1264 sr_err("Unknown packet type %d", packet->type);