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 */
50 sr_receive_data_callback cb;
53 /* This is used to keep track of the object (fd, pollfd or channel) which is
54 * being polled and will be used to match the source when removing it again.
62 struct datafeed_callback {
63 sr_datafeed_callback cb;
68 * Create a new session.
70 * @param ctx The context in which to create the new session.
71 * @param new_session This will contain a pointer to the newly created
72 * session if the return value is SR_OK, otherwise the value
73 * is undefined and should not be used. Must not be NULL.
75 * @retval SR_OK Success.
76 * @retval SR_ERR_ARG Invalid argument.
80 SR_API int sr_session_new(struct sr_context *ctx,
81 struct sr_session **new_session)
83 struct sr_session *session;
88 session = g_malloc0(sizeof(struct sr_session));
92 session->sources = g_array_new(FALSE, FALSE, sizeof(struct source));
93 session->pollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
95 g_mutex_init(&session->stop_mutex);
97 *new_session = session;
104 * This frees up all memory used by the session.
106 * @param session The session to destroy. Must not be NULL.
108 * @retval SR_OK Success.
109 * @retval SR_ERR_ARG Invalid session passed.
113 SR_API int sr_session_destroy(struct sr_session *session)
116 sr_err("%s: session was NULL", __func__);
120 sr_session_dev_remove_all(session);
121 g_mutex_clear(&session->stop_mutex);
122 if (session->trigger)
123 sr_trigger_free(session->trigger);
125 g_slist_free_full(session->owned_devs, (GDestroyNotify)sr_dev_inst_free);
127 g_array_unref(session->pollfds);
128 g_array_unref(session->sources);
136 * Remove all the devices from a session.
138 * The session itself (i.e., the struct sr_session) is not free'd and still
139 * exists after this function returns.
141 * @param session The session to use. Must not be NULL.
143 * @retval SR_OK Success.
144 * @retval SR_ERR_BUG Invalid session passed.
148 SR_API int sr_session_dev_remove_all(struct sr_session *session)
150 struct sr_dev_inst *sdi;
154 sr_err("%s: session was NULL", __func__);
158 for (l = session->devs; l; l = l->next) {
159 sdi = (struct sr_dev_inst *) l->data;
163 g_slist_free(session->devs);
164 session->devs = NULL;
170 * Add a device instance to a session.
172 * @param session The session to add to. Must not be NULL.
173 * @param sdi The device instance to add to a session. Must not
174 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
177 * @retval SR_OK Success.
178 * @retval SR_ERR_ARG Invalid argument.
182 SR_API int sr_session_dev_add(struct sr_session *session,
183 struct sr_dev_inst *sdi)
188 sr_err("%s: sdi was NULL", __func__);
193 sr_err("%s: session was NULL", __func__);
197 /* If sdi->session is not NULL, the device is already in this or
198 * another session. */
200 sr_err("%s: already assigned to session", __func__);
204 /* If sdi->driver is NULL, this is a virtual device. */
206 /* Just add the device, don't run dev_open(). */
207 session->devs = g_slist_append(session->devs, (gpointer)sdi);
208 sdi->session = session;
212 /* sdi->driver is non-NULL (i.e. we have a real device). */
213 if (!sdi->driver->dev_open) {
214 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
218 session->devs = g_slist_append(session->devs, (gpointer)sdi);
219 sdi->session = session;
221 if (session->running) {
222 /* Adding a device to a running session. Commit settings
223 * and start acquisition on that device now. */
224 if ((ret = sr_config_commit(sdi)) != SR_OK) {
225 sr_err("Failed to commit device settings before "
226 "starting acquisition in running session (%s)",
230 if ((ret = sdi->driver->dev_acquisition_start(sdi,
231 (void *)sdi)) != SR_OK) {
232 sr_err("Failed to start acquisition of device in "
233 "running session (%s)", sr_strerror(ret));
242 * List all device instances attached to a session.
244 * @param session The session to use. Must not be NULL.
245 * @param devlist A pointer where the device instance list will be
246 * stored on return. If no devices are in the session,
247 * this will be NULL. Each element in the list points
248 * to a struct sr_dev_inst *.
249 * The list must be freed by the caller, but not the
250 * elements pointed to.
252 * @retval SR_OK Success.
253 * @retval SR_ERR_ARG Invalid argument.
257 SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist)
265 *devlist = g_slist_copy(session->devs);
271 * Remove all datafeed callbacks in a session.
273 * @param session The session to use. Must not be NULL.
275 * @retval SR_OK Success.
276 * @retval SR_ERR_ARG Invalid session passed.
280 SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session)
283 sr_err("%s: session was NULL", __func__);
287 g_slist_free_full(session->datafeed_callbacks, g_free);
288 session->datafeed_callbacks = NULL;
294 * Add a datafeed callback to a session.
296 * @param session The session to use. Must not be NULL.
297 * @param cb Function to call when a chunk of data is received.
299 * @param cb_data Opaque pointer passed in by the caller.
301 * @retval SR_OK Success.
302 * @retval SR_ERR_BUG No session exists.
306 SR_API int sr_session_datafeed_callback_add(struct sr_session *session,
307 sr_datafeed_callback cb, void *cb_data)
309 struct datafeed_callback *cb_struct;
312 sr_err("%s: session was NULL", __func__);
317 sr_err("%s: cb was NULL", __func__);
321 cb_struct = g_malloc0(sizeof(struct datafeed_callback));
323 cb_struct->cb_data = cb_data;
325 session->datafeed_callbacks =
326 g_slist_append(session->datafeed_callbacks, cb_struct);
332 * Get the trigger assigned to this session.
334 * @param session The session to use.
336 * @retval NULL Invalid (NULL) session was passed to the function.
337 * @retval other The trigger assigned to this session (can be NULL).
341 SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session)
346 return session->trigger;
350 * Set the trigger of this session.
352 * @param session The session to use. Must not be NULL.
353 * @param trig The trigger to assign to this session. Can be NULL.
355 * @retval SR_OK Success.
356 * @retval SR_ERR_ARG Invalid argument.
360 SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig)
365 session->trigger = trig;
370 static gboolean sr_session_check_aborted(struct sr_session *session)
374 g_mutex_lock(&session->stop_mutex);
375 stop = 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);
386 static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object);
389 * Poll the session's event sources.
391 * @param session The session to use. Must not be NULL.
392 * @retval SR_OK Success.
393 * @retval SR_ERR Error occurred.
395 static int sr_session_iteration(struct sr_session *session)
397 int64_t start_time, stop_time, min_due, due;
403 gboolean triggered, stopped;
404 struct source *source;
407 #ifdef HAVE_LIBUSB_1_0
412 if (session->sources->len == 0) {
413 sr_session_check_aborted(session);
416 start_time = g_get_monotonic_time();
419 for (i = 0; i < session->sources->len; ++i) {
420 source = &g_array_index(session->sources, struct source, i);
421 if (source->due < min_due)
422 min_due = source->due;
423 source->triggered = FALSE;
425 #ifdef HAVE_LIBUSB_1_0
427 if (session->ctx->usb_source_present) {
428 ret = libusb_get_next_timeout(session->ctx->libusb_ctx, &tv);
430 sr_err("Error getting libusb timeout: %s",
431 libusb_error_name(ret));
433 } else if (ret == 1) {
434 usb_timeout = (int64_t)tv.tv_sec * G_USEC_PER_SEC
436 usb_due = start_time + usb_timeout;
437 if (usb_due < min_due)
440 sr_spew("poll: next USB timeout %g ms",
445 if (min_due == INT64_MAX)
447 else if (min_due > start_time)
448 timeout_ms = MIN((min_due - start_time + 999) / 1000, INT_MAX);
452 sr_spew("poll enter: %u sources, %u fds, %d ms timeout",
453 session->sources->len, session->pollfds->len, timeout_ms);
455 ret = g_poll((GPollFD *)session->pollfds->data,
456 session->pollfds->len, timeout_ms);
458 if (ret < 0 && errno != EINTR) {
459 sr_err("Error in poll: %s", g_strerror(errno));
464 sr_err("Error in poll: %d", ret);
468 stop_time = g_get_monotonic_time();
470 sr_spew("poll leave: %g ms elapsed, %d events",
471 1e-3 * (stop_time - start_time), ret);
476 for (i = 0; i < session->sources->len; ++i) {
477 source = &g_array_index(session->sources, struct source, i);
478 if (source->triggered)
479 continue; /* already handled */
481 poll_object = source->poll_object;
482 fd = (int)poll_object;
485 if (i < session->pollfds->len) {
486 pollfd = &g_array_index(session->pollfds, GPollFD, i);
489 revents = pollfd->revents;
491 if (ret > 0 && revents == 0)
492 continue; /* skip timeouts if any I/O event occurred */
495 #ifdef HAVE_LIBUSB_1_0
496 if (source->is_usb && usb_due < due)
499 if (revents == 0 && stop_time < due)
502 * The source may be gone after the callback returns,
503 * so access any data now that needs accessing.
505 if (source->timeout >= 0)
506 source->due = stop_time + source->timeout;
507 source->triggered = TRUE;
510 * Invoke the source's callback on an event or timeout.
512 if (!source->cb(fd, revents, source->cb_data))
513 _sr_session_source_remove(session, poll_object);
515 * We want to take as little time as possible to stop
516 * the session if we have been told to do so. Therefore,
517 * we check the flag after processing every source, not
518 * just once per main event loop.
521 stopped = sr_session_check_aborted(session);
523 /* Restart loop as the sources list may have changed. */
527 /* Check for abort at least once per iteration. */
529 sr_session_check_aborted(session);
534 static int verify_trigger(struct sr_trigger *trigger)
536 struct sr_trigger_stage *stage;
537 struct sr_trigger_match *match;
540 if (!trigger->stages) {
541 sr_err("No trigger stages defined.");
545 sr_spew("Checking trigger:");
546 for (l = trigger->stages; l; l = l->next) {
548 if (!stage->matches) {
549 sr_err("Stage %d has no matches defined.", stage->stage);
552 for (m = stage->matches; m; m = m->next) {
554 if (!match->channel) {
555 sr_err("Stage %d match has no channel.", stage->stage);
559 sr_err("Stage %d match is not defined.", stage->stage);
562 sr_spew("Stage %d match on channel %s, match %d", stage->stage,
563 match->channel->name, match->match);
573 * @param session The session to use. Must not be NULL.
575 * @retval SR_OK Success.
576 * @retval SR_ERR_ARG Invalid session passed.
580 SR_API int sr_session_start(struct sr_session *session)
582 struct sr_dev_inst *sdi;
583 struct sr_channel *ch;
585 int enabled_channels, ret;
588 sr_err("%s: session was NULL", __func__);
592 if (!session->devs) {
593 sr_err("%s: session->devs was NULL; a session "
594 "cannot be started without devices.", __func__);
598 if (session->trigger && verify_trigger(session->trigger) != SR_OK)
601 sr_info("Starting.");
604 for (l = session->devs; l; l = l->next) {
606 enabled_channels = 0;
607 for (c = sdi->channels; c; c = c->next) {
614 if (enabled_channels == 0) {
616 sr_err("%s using connection %s has no enabled channels!",
617 sdi->driver->name, sdi->connection_id);
621 if ((ret = sr_config_commit(sdi)) != SR_OK) {
622 sr_err("Failed to commit device settings before "
623 "starting acquisition (%s)", sr_strerror(ret));
626 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
627 sr_err("%s: could not start an acquisition "
628 "(%s)", __func__, sr_strerror(ret));
633 /* TODO: What if there are multiple devices? Which return code? */
641 * @param session The session to use. Must not be NULL.
643 * @retval SR_OK Success.
644 * @retval SR_ERR_ARG Invalid session passed.
645 * @retval SR_ERR Error during event processing.
649 SR_API int sr_session_run(struct sr_session *session)
654 sr_err("%s: session was NULL", __func__);
658 if (!session->devs) {
659 /* TODO: Actually the case? */
660 sr_err("%s: session->devs was NULL; a session "
661 "cannot be run without devices.", __func__);
664 session->running = TRUE;
668 /* Poll event sources until none are left. */
669 while (session->sources->len > 0) {
670 ret = sr_session_iteration(session);
680 * The session is stopped immediately, with all acquisition sessions stopped
681 * and hardware drivers cleaned up.
683 * This must be called from within the session thread, to prevent freeing
684 * resources that the session thread will try to use.
686 * @param session The session to use. Must not be NULL.
688 * @retval SR_OK Success.
689 * @retval SR_ERR_ARG Invalid session passed.
693 SR_PRIV int sr_session_stop_sync(struct sr_session *session)
695 struct sr_dev_inst *sdi;
699 sr_err("%s: session was NULL", __func__);
703 sr_info("Stopping.");
705 for (l = session->devs; l; l = l->next) {
708 if (sdi->driver->dev_acquisition_stop)
709 sdi->driver->dev_acquisition_stop(sdi, sdi);
712 session->running = FALSE;
720 * The session is stopped immediately, with all acquisition sessions being
721 * stopped and hardware drivers cleaned up.
723 * If the session is run in a separate thread, this function will not block
724 * until the session is finished executing. It is the caller's responsibility
725 * to wait for the session thread to return before assuming that the session is
726 * completely decommissioned.
728 * @param session The session to use. Must not be NULL.
730 * @retval SR_OK Success.
731 * @retval SR_ERR_ARG Invalid session passed.
735 SR_API int sr_session_stop(struct sr_session *session)
738 sr_err("%s: session was NULL", __func__);
742 g_mutex_lock(&session->stop_mutex);
743 session->abort_session = TRUE;
744 g_mutex_unlock(&session->stop_mutex);
752 * @param packet The packet to show debugging information for.
754 static void datafeed_dump(const struct sr_datafeed_packet *packet)
756 const struct sr_datafeed_logic *logic;
757 const struct sr_datafeed_analog *analog;
758 const struct sr_datafeed_analog2 *analog2;
760 /* Please use the same order as in libsigrok.h. */
761 switch (packet->type) {
763 sr_dbg("bus: Received SR_DF_HEADER packet.");
766 sr_dbg("bus: Received SR_DF_END packet.");
769 sr_dbg("bus: Received SR_DF_META packet.");
772 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
775 logic = packet->payload;
776 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, "
777 "unitsize = %d).", logic->length, logic->unitsize);
780 analog = packet->payload;
781 sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).",
782 analog->num_samples);
784 case SR_DF_FRAME_BEGIN:
785 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
787 case SR_DF_FRAME_END:
788 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
791 analog2 = packet->payload;
792 sr_dbg("bus: Received SR_DF_ANALOG2 packet (%d samples).",
793 analog2->num_samples);
796 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
802 * Send a packet to whatever is listening on the datafeed bus.
804 * Hardware drivers use this to send a data packet to the frontend.
807 * @param packet The datafeed packet to send to the session bus.
809 * @retval SR_OK Success.
810 * @retval SR_ERR_ARG Invalid argument.
814 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
815 const struct sr_datafeed_packet *packet)
818 struct datafeed_callback *cb_struct;
819 struct sr_datafeed_packet *packet_in, *packet_out;
820 struct sr_transform *t;
824 sr_err("%s: sdi was NULL", __func__);
829 sr_err("%s: packet was NULL", __func__);
834 sr_err("%s: session was NULL", __func__);
839 * Pass the packet to the first transform module. If that returns
840 * another packet (instead of NULL), pass that packet to the next
841 * transform module in the list, and so on.
843 packet_in = (struct sr_datafeed_packet *)packet;
844 for (l = sdi->session->transforms; l; l = l->next) {
846 sr_spew("Running transform module '%s'.", t->module->id);
847 ret = t->module->receive(t, packet_in, &packet_out);
849 sr_err("Error while running transform module: %d.", ret);
854 * If any of the transforms don't return an output
857 sr_spew("Transform module didn't return a packet, aborting.");
861 * Use this transform module's output packet as input
862 * for the next transform module.
864 packet_in = packet_out;
870 * If the last transform did output a packet, pass it to all datafeed
873 for (l = sdi->session->datafeed_callbacks; l; l = l->next) {
874 if (sr_log_loglevel_get() >= SR_LOG_DBG)
875 datafeed_dump(packet);
877 cb_struct->cb(sdi, packet, cb_struct->cb_data);
884 * Add an event source for a file descriptor.
886 * @param session The session to use. Must not be NULL.
887 * @param pollfd The GPollFD.
888 * @param[in] timeout Max time in ms to wait before the callback is called,
889 * or -1 to wait indefinitely.
890 * @param cb Callback function to add. Must not be NULL.
891 * @param cb_data Data for the callback function. Can be NULL.
892 * @param poll_object Handle by which the source is identified
893 * @param is_usb TRUE for a libusb polling source
895 * @retval SR_OK Success.
896 * @retval SR_ERR_ARG Invalid argument.
898 SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
899 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
900 void *cb_data, gintptr poll_object, gboolean is_usb)
905 sr_err("%s: cb was NULL", __func__);
908 /* Note: cb_data can be NULL, that's not a bug. */
911 src.cb_data = cb_data;
912 src.poll_object = poll_object;
914 src.triggered = FALSE;
917 src.timeout = INT64_C(1000) * timeout;
918 src.due = g_get_monotonic_time() + src.timeout;
926 g_array_insert_val(session->sources, session->pollfds->len, src);
927 g_array_append_vals(session->pollfds, pollfd, 1);
930 g_array_append_val(session->sources, src);
937 * Add an event source for a file descriptor.
939 * @param session The session to use. Must not be NULL.
940 * @param fd The file descriptor.
941 * @param events Events to check for.
942 * @param timeout Max time in ms to wait before the callback is called,
943 * or -1 to wait indefinitely.
944 * @param cb Callback function to add. Must not be NULL.
945 * @param cb_data Data for the callback function. Can be NULL.
947 * @retval SR_OK Success.
948 * @retval SR_ERR_ARG Invalid argument.
952 SR_API int sr_session_source_add(struct sr_session *session, int fd,
953 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
957 if (fd < 0 && timeout < 0) {
958 sr_err("Timer source without timeout would block indefinitely");
965 return sr_session_source_add_internal(session,
966 (fd < 0) ? NULL : &p, timeout, cb, cb_data, fd, FALSE);
970 * Add an event source for a GPollFD.
972 * @param session The session to use. Must not be NULL.
973 * @param pollfd The GPollFD. Must not be NULL.
974 * @param timeout Max time in ms to wait before the callback is called,
975 * or -1 to wait indefinitely.
976 * @param cb Callback function to add. Must not be NULL.
977 * @param cb_data Data for the callback function. Can be NULL.
979 * @retval SR_OK Success.
980 * @retval SR_ERR_ARG Invalid argument.
984 SR_API int sr_session_source_add_pollfd(struct sr_session *session,
985 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
989 sr_err("%s: pollfd was NULL", __func__);
992 return sr_session_source_add_internal(session, pollfd, timeout,
993 cb, cb_data, (gintptr)pollfd, FALSE);
997 * Add an event source for a GIOChannel.
999 * @param session The session to use. Must not be NULL.
1000 * @param channel The GIOChannel.
1001 * @param events Events to poll on.
1002 * @param timeout Max time in ms to wait before the callback is called,
1003 * or -1 to wait indefinitely.
1004 * @param cb Callback function to add. Must not be NULL.
1005 * @param cb_data Data for the callback function. Can be NULL.
1007 * @retval SR_OK Success.
1008 * @retval SR_ERR_ARG Invalid argument.
1012 SR_API int sr_session_source_add_channel(struct sr_session *session,
1013 GIOChannel *channel, int events, int timeout,
1014 sr_receive_data_callback cb, void *cb_data)
1019 g_io_channel_win32_make_pollfd(channel, events, &p);
1021 p.fd = g_io_channel_unix_get_fd(channel);
1025 return sr_session_source_add_internal(session, &p, timeout, cb,
1026 cb_data, (gintptr)channel, FALSE);
1030 * Remove the source belonging to the specified channel.
1032 * @param session The session to use. Must not be NULL.
1033 * @param poll_object The channel for which the source should be removed.
1035 * @retval SR_OK Success
1036 * @retval SR_ERR_ARG Invalid arguments
1037 * @retval SR_ERR_BUG Internal error
1039 static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object)
1043 for (i = 0; i < session->sources->len; ++i) {
1044 if (g_array_index(session->sources, struct source, i)
1045 .poll_object == poll_object) {
1047 g_array_remove_index(session->sources, i);
1048 if (i < session->pollfds->len)
1049 g_array_remove_index(session->pollfds, i);
1057 * Remove the source belonging to the specified file descriptor.
1059 * @param session The session to use. Must not be NULL.
1060 * @param fd The file descriptor for which the source should be removed.
1062 * @retval SR_OK Success
1063 * @retval SR_ERR_ARG Invalid argument
1064 * @retval SR_ERR_BUG Internal error.
1068 SR_API int sr_session_source_remove(struct sr_session *session, int fd)
1070 return _sr_session_source_remove(session, fd);
1074 * Remove the source belonging to the specified poll descriptor.
1076 * @param session The session to use. Must not be NULL.
1077 * @param pollfd The poll descriptor for which the source should be removed.
1079 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
1080 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
1085 SR_API int sr_session_source_remove_pollfd(struct sr_session *session,
1089 sr_err("%s: pollfd was NULL", __func__);
1092 return _sr_session_source_remove(session, (gintptr)pollfd);
1096 * Remove the source belonging to the specified channel.
1098 * @param session The session to use. Must not be NULL.
1099 * @param channel The channel for which the source should be removed.
1101 * @retval SR_OK Success.
1102 * @retval SR_ERR_ARG Invalid argument.
1103 * @return SR_ERR_BUG Internal error.
1107 SR_API int sr_session_source_remove_channel(struct sr_session *session,
1108 GIOChannel *channel)
1111 sr_err("%s: channel was NULL", __func__);
1114 return _sr_session_source_remove(session, (gintptr)channel);
1117 static void copy_src(struct sr_config *src, struct sr_datafeed_meta *meta_copy)
1119 g_variant_ref(src->data);
1120 meta_copy->config = g_slist_append(meta_copy->config,
1121 g_memdup(src, sizeof(struct sr_config)));
1124 SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet,
1125 struct sr_datafeed_packet **copy)
1127 const struct sr_datafeed_meta *meta;
1128 struct sr_datafeed_meta *meta_copy;
1129 const struct sr_datafeed_logic *logic;
1130 struct sr_datafeed_logic *logic_copy;
1131 const struct sr_datafeed_analog *analog;
1132 struct sr_datafeed_analog *analog_copy;
1135 *copy = g_malloc0(sizeof(struct sr_datafeed_packet));
1136 (*copy)->type = packet->type;
1138 switch (packet->type) {
1144 payload = g_malloc(sizeof(struct sr_datafeed_header));
1145 memcpy(payload, packet->payload, sizeof(struct sr_datafeed_header));
1146 (*copy)->payload = payload;
1149 meta = packet->payload;
1150 meta_copy = g_malloc0(sizeof(struct sr_datafeed_meta));
1151 g_slist_foreach(meta->config, (GFunc)copy_src, meta_copy->config);
1152 (*copy)->payload = meta_copy;
1155 logic = packet->payload;
1156 logic_copy = g_malloc(sizeof(logic));
1157 logic_copy->length = logic->length;
1158 logic_copy->unitsize = logic->unitsize;
1159 memcpy(logic_copy->data, logic->data, logic->length * logic->unitsize);
1160 (*copy)->payload = logic_copy;
1163 analog = packet->payload;
1164 analog_copy = g_malloc(sizeof(analog));
1165 analog_copy->channels = g_slist_copy(analog->channels);
1166 analog_copy->num_samples = analog->num_samples;
1167 analog_copy->mq = analog->mq;
1168 analog_copy->unit = analog->unit;
1169 analog_copy->mqflags = analog->mqflags;
1170 memcpy(analog_copy->data, analog->data,
1171 analog->num_samples * sizeof(float));
1172 (*copy)->payload = analog_copy;
1175 sr_err("Unknown packet type %d", packet->type);
1182 void sr_packet_free(struct sr_datafeed_packet *packet)
1184 const struct sr_datafeed_meta *meta;
1185 const struct sr_datafeed_logic *logic;
1186 const struct sr_datafeed_analog *analog;
1187 struct sr_config *src;
1190 switch (packet->type) {
1196 /* Payload is a simple struct. */
1197 g_free((void *)packet->payload);
1200 meta = packet->payload;
1201 for (l = meta->config; l; l = l->next) {
1203 g_variant_unref(src->data);
1206 g_slist_free(meta->config);
1207 g_free((void *)packet->payload);
1210 logic = packet->payload;
1211 g_free(logic->data);
1212 g_free((void *)packet->payload);
1215 analog = packet->payload;
1216 g_slist_free(analog->channels);
1217 g_free(analog->data);
1218 g_free((void *)packet->payload);
1221 sr_err("Unknown packet type %d", packet->type);