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.
49 sr_receive_data_callback cb;
52 /* This is used to keep track of the object (fd, pollfd or channel) which is
53 * being polled and will be used to match the source when removing it again.
58 struct datafeed_callback {
59 sr_datafeed_callback cb;
64 * Create a new session.
66 * @param ctx The context in which to create the new session.
67 * @param new_session This will contain a pointer to the newly created
68 * session if the return value is SR_OK, otherwise the value
69 * is undefined and should not be used. Must not be NULL.
71 * @retval SR_OK Success.
72 * @retval SR_ERR_ARG Invalid argument.
76 SR_API int sr_session_new(struct sr_context *ctx,
77 struct sr_session **new_session)
79 struct sr_session *session;
84 session = g_malloc0(sizeof(struct sr_session));
87 session->source_timeout = -1;
88 session->running = FALSE;
89 session->abort_session = FALSE;
90 g_mutex_init(&session->stop_mutex);
92 *new_session = session;
99 * This frees up all memory used by the session.
101 * @param session The session to destroy. Must not be NULL.
103 * @retval SR_OK Success.
104 * @retval SR_ERR_ARG Invalid session passed.
108 SR_API int sr_session_destroy(struct sr_session *session)
111 sr_err("%s: session was NULL", __func__);
115 sr_session_dev_remove_all(session);
116 g_mutex_clear(&session->stop_mutex);
117 if (session->trigger)
118 sr_trigger_free(session->trigger);
120 g_slist_free_full(session->owned_devs, (GDestroyNotify)sr_dev_inst_free);
128 * Remove all the devices from a session.
130 * The session itself (i.e., the struct sr_session) is not free'd and still
131 * exists after this function returns.
133 * @param session The session to use. Must not be NULL.
135 * @retval SR_OK Success.
136 * @retval SR_ERR_BUG Invalid session passed.
140 SR_API int sr_session_dev_remove_all(struct sr_session *session)
142 struct sr_dev_inst *sdi;
146 sr_err("%s: session was NULL", __func__);
150 for (l = session->devs; l; l = l->next) {
151 sdi = (struct sr_dev_inst *) l->data;
155 g_slist_free(session->devs);
156 session->devs = NULL;
162 * Add a device instance to a session.
164 * @param session The session to add to. Must not be NULL.
165 * @param sdi The device instance to add to a session. Must not
166 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
169 * @retval SR_OK Success.
170 * @retval SR_ERR_ARG Invalid argument.
174 SR_API int sr_session_dev_add(struct sr_session *session,
175 struct sr_dev_inst *sdi)
180 sr_err("%s: sdi was NULL", __func__);
185 sr_err("%s: session was NULL", __func__);
189 /* If sdi->session is not NULL, the device is already in this or
190 * another session. */
192 sr_err("%s: already assigned to session", __func__);
196 /* If sdi->driver is NULL, this is a virtual device. */
198 /* Just add the device, don't run dev_open(). */
199 session->devs = g_slist_append(session->devs, (gpointer)sdi);
200 sdi->session = session;
204 /* sdi->driver is non-NULL (i.e. we have a real device). */
205 if (!sdi->driver->dev_open) {
206 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
210 session->devs = g_slist_append(session->devs, (gpointer)sdi);
211 sdi->session = session;
213 if (session->running) {
214 /* Adding a device to a running session. Commit settings
215 * and start acquisition on that device now. */
216 if ((ret = sr_config_commit(sdi)) != SR_OK) {
217 sr_err("Failed to commit device settings before "
218 "starting acquisition in running session (%s)",
222 if ((ret = sdi->driver->dev_acquisition_start(sdi,
223 (void *)sdi)) != SR_OK) {
224 sr_err("Failed to start acquisition of device in "
225 "running session (%s)", sr_strerror(ret));
234 * List all device instances attached to a session.
236 * @param session The session to use. Must not be NULL.
237 * @param devlist A pointer where the device instance list will be
238 * stored on return. If no devices are in the session,
239 * this will be NULL. Each element in the list points
240 * to a struct sr_dev_inst *.
241 * The list must be freed by the caller, but not the
242 * elements pointed to.
244 * @retval SR_OK Success.
245 * @retval SR_ERR_ARG Invalid argument.
249 SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist)
257 *devlist = g_slist_copy(session->devs);
263 * Remove all datafeed callbacks in a session.
265 * @param session The session to use. Must not be NULL.
267 * @retval SR_OK Success.
268 * @retval SR_ERR_ARG Invalid session passed.
272 SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session)
275 sr_err("%s: session was NULL", __func__);
279 g_slist_free_full(session->datafeed_callbacks, g_free);
280 session->datafeed_callbacks = NULL;
286 * Add a datafeed callback to a session.
288 * @param session The session to use. Must not be NULL.
289 * @param cb Function to call when a chunk of data is received.
291 * @param cb_data Opaque pointer passed in by the caller.
293 * @retval SR_OK Success.
294 * @retval SR_ERR_BUG No session exists.
298 SR_API int sr_session_datafeed_callback_add(struct sr_session *session,
299 sr_datafeed_callback cb, void *cb_data)
301 struct datafeed_callback *cb_struct;
304 sr_err("%s: session was NULL", __func__);
309 sr_err("%s: cb was NULL", __func__);
313 cb_struct = g_malloc0(sizeof(struct datafeed_callback));
315 cb_struct->cb_data = cb_data;
317 session->datafeed_callbacks =
318 g_slist_append(session->datafeed_callbacks, cb_struct);
324 * Get the trigger assigned to this session.
326 * @param session The session to use.
328 * @retval NULL Invalid (NULL) session was passed to the function.
329 * @retval other The trigger assigned to this session (can be NULL).
333 SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session)
338 return session->trigger;
342 * Set the trigger of this session.
344 * @param session The session to use. Must not be NULL.
345 * @param trig The trigger to assign to this session. Can be NULL.
347 * @retval SR_OK Success.
348 * @retval SR_ERR_ARG Invalid argument.
352 SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig)
357 session->trigger = trig;
362 static gboolean sr_session_check_aborted(struct sr_session *session)
366 g_mutex_lock(&session->stop_mutex);
367 stop = session->abort_session;
369 sr_session_stop_sync(session);
370 /* But once is enough. */
371 session->abort_session = FALSE;
373 g_mutex_unlock(&session->stop_mutex);
379 * Call every device in the current session's callback.
381 * For sessions not driven by select loops such as sr_session_run(),
382 * but driven by another scheduler, this can be used to poll the devices
383 * from within that scheduler.
385 * @param session The session to use. Must not be NULL.
386 * @param block If TRUE, this call will wait for any of the session's
387 * sources to fire an event on the file descriptors, or
388 * any of their timeouts to activate. In other words, this
389 * can be used as a select loop.
390 * If FALSE, return immediately if none of the sources has
392 * @retval SR_OK Success.
393 * @retval SR_ERR Error occurred.
395 static int sr_session_iteration(struct sr_session *session, gboolean block)
399 gboolean stop_checked;
401 #ifdef HAVE_LIBUSB_1_0
406 timeout = block ? session->source_timeout : 0;
408 #ifdef HAVE_LIBUSB_1_0
409 if (session->ctx->usb_source_present) {
410 ret = libusb_get_next_timeout(session->ctx->libusb_ctx, &tv);
412 sr_err("Error getting libusb timeout: %s",
413 libusb_error_name(ret));
415 } else if (ret == 1) {
416 usb_timeout = tv.tv_sec * 1000 + tv.tv_usec / 1000;
417 timeout = MIN(timeout, usb_timeout);
422 ret = g_poll(session->pollfds, session->num_sources, timeout);
423 if (ret < 0 && errno != EINTR) {
424 sr_err("Error in poll: %s", g_strerror(errno));
427 stop_checked = FALSE;
430 for (i = 0; i < session->num_sources; i++) {
431 if ((ret > 0 && session->pollfds[i].revents > 0) || (ret == 0
432 && session->source_timeout == session->sources[i].timeout)) {
434 * Invoke the source's callback on an event,
435 * or if the poll timed out and this source
436 * asked for that timeout.
438 if (!session->sources[i].cb(session->pollfds[i].fd,
439 session->pollfds[i].revents,
440 session->sources[i].cb_data))
441 sr_session_source_remove(session,
442 session->sources[i].poll_object);
444 * We want to take as little time as possible to stop
445 * the session if we have been told to do so. Therefore,
446 * we check the flag after processing every source, not
447 * just once per main event loop.
450 stopped = sr_session_check_aborted(session);
456 sr_session_check_aborted(session);
461 static int verify_trigger(struct sr_trigger *trigger)
463 struct sr_trigger_stage *stage;
464 struct sr_trigger_match *match;
467 if (!trigger->stages) {
468 sr_err("No trigger stages defined.");
472 sr_spew("Checking trigger:");
473 for (l = trigger->stages; l; l = l->next) {
475 if (!stage->matches) {
476 sr_err("Stage %d has no matches defined.", stage->stage);
479 for (m = stage->matches; m; m = m->next) {
481 if (!match->channel) {
482 sr_err("Stage %d match has no channel.", stage->stage);
486 sr_err("Stage %d match is not defined.", stage->stage);
489 sr_spew("Stage %d match on channel %s, match %d", stage->stage,
490 match->channel->name, match->match);
500 * @param session The session to use. Must not be NULL.
502 * @retval SR_OK Success.
503 * @retval SR_ERR_ARG Invalid session passed.
507 SR_API int sr_session_start(struct sr_session *session)
509 struct sr_dev_inst *sdi;
510 struct sr_channel *ch;
512 int enabled_channels, ret;
515 sr_err("%s: session was NULL", __func__);
519 if (!session->devs) {
520 sr_err("%s: session->devs was NULL; a session "
521 "cannot be started without devices.", __func__);
525 if (session->trigger && verify_trigger(session->trigger) != SR_OK)
528 sr_info("Starting.");
531 for (l = session->devs; l; l = l->next) {
533 enabled_channels = 0;
534 for (c = sdi->channels; c; c = c->next) {
541 if (enabled_channels == 0) {
543 sr_err("%s using connection %s has no enabled channels!",
544 sdi->driver->name, sdi->connection_id);
548 if ((ret = sr_config_commit(sdi)) != SR_OK) {
549 sr_err("Failed to commit device settings before "
550 "starting acquisition (%s)", sr_strerror(ret));
553 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
554 sr_err("%s: could not start an acquisition "
555 "(%s)", __func__, sr_strerror(ret));
560 /* TODO: What if there are multiple devices? Which return code? */
568 * @param session The session to use. Must not be NULL.
570 * @retval SR_OK Success.
571 * @retval SR_ERR_ARG Invalid session passed.
575 SR_API int sr_session_run(struct sr_session *session)
578 sr_err("%s: session was NULL", __func__);
582 if (!session->devs) {
583 /* TODO: Actually the case? */
584 sr_err("%s: session->devs was NULL; a session "
585 "cannot be run without devices.", __func__);
588 session->running = TRUE;
592 /* Do we have real sources? */
593 if (session->num_sources == 1 && session->pollfds[0].fd == -1) {
594 /* Dummy source, freewheel over it. */
595 while (session->num_sources)
596 session->sources[0].cb(-1, 0, session->sources[0].cb_data);
598 /* Real sources, use g_poll() main loop. */
599 while (session->num_sources)
600 sr_session_iteration(session, TRUE);
609 * The session is stopped immediately, with all acquisition sessions stopped
610 * and hardware drivers cleaned up.
612 * This must be called from within the session thread, to prevent freeing
613 * resources that the session thread will try to use.
615 * @param session The session to use. Must not be NULL.
617 * @retval SR_OK Success.
618 * @retval SR_ERR_ARG Invalid session passed.
622 SR_PRIV int sr_session_stop_sync(struct sr_session *session)
624 struct sr_dev_inst *sdi;
628 sr_err("%s: session was NULL", __func__);
632 sr_info("Stopping.");
634 for (l = session->devs; l; l = l->next) {
637 if (sdi->driver->dev_acquisition_stop)
638 sdi->driver->dev_acquisition_stop(sdi, sdi);
641 session->running = FALSE;
649 * The session is stopped immediately, with all acquisition sessions being
650 * stopped and hardware drivers cleaned up.
652 * If the session is run in a separate thread, this function will not block
653 * until the session is finished executing. It is the caller's responsibility
654 * to wait for the session thread to return before assuming that the session is
655 * completely decommissioned.
657 * @param session The session to use. Must not be NULL.
659 * @retval SR_OK Success.
660 * @retval SR_ERR_ARG Invalid session passed.
664 SR_API int sr_session_stop(struct sr_session *session)
667 sr_err("%s: session was NULL", __func__);
671 g_mutex_lock(&session->stop_mutex);
672 session->abort_session = TRUE;
673 g_mutex_unlock(&session->stop_mutex);
681 * @param packet The packet to show debugging information for.
683 static void datafeed_dump(const struct sr_datafeed_packet *packet)
685 const struct sr_datafeed_logic *logic;
686 const struct sr_datafeed_analog *analog;
687 const struct sr_datafeed_analog2 *analog2;
689 /* Please use the same order as in libsigrok.h. */
690 switch (packet->type) {
692 sr_dbg("bus: Received SR_DF_HEADER packet.");
695 sr_dbg("bus: Received SR_DF_END packet.");
698 sr_dbg("bus: Received SR_DF_META packet.");
701 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
704 logic = packet->payload;
705 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, "
706 "unitsize = %d).", logic->length, logic->unitsize);
709 analog = packet->payload;
710 sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).",
711 analog->num_samples);
713 case SR_DF_FRAME_BEGIN:
714 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
716 case SR_DF_FRAME_END:
717 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
720 analog2 = packet->payload;
721 sr_dbg("bus: Received SR_DF_ANALOG2 packet (%d samples).",
722 analog2->num_samples);
725 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
731 * Send a packet to whatever is listening on the datafeed bus.
733 * Hardware drivers use this to send a data packet to the frontend.
736 * @param packet The datafeed packet to send to the session bus.
738 * @retval SR_OK Success.
739 * @retval SR_ERR_ARG Invalid argument.
743 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
744 const struct sr_datafeed_packet *packet)
747 struct datafeed_callback *cb_struct;
748 struct sr_datafeed_packet *packet_in, *packet_out;
749 struct sr_transform *t;
753 sr_err("%s: sdi was NULL", __func__);
758 sr_err("%s: packet was NULL", __func__);
763 sr_err("%s: session was NULL", __func__);
768 * Pass the packet to the first transform module. If that returns
769 * another packet (instead of NULL), pass that packet to the next
770 * transform module in the list, and so on.
772 packet_in = (struct sr_datafeed_packet *)packet;
773 for (l = sdi->session->transforms; l; l = l->next) {
775 sr_spew("Running transform module '%s'.", t->module->id);
776 ret = t->module->receive(t, packet_in, &packet_out);
778 sr_err("Error while running transform module: %d.", ret);
783 * If any of the transforms don't return an output
786 sr_spew("Transform module didn't return a packet, aborting.");
790 * Use this transform module's output packet as input
791 * for the next transform module.
793 packet_in = packet_out;
799 * If the last transform did output a packet, pass it to all datafeed
802 for (l = sdi->session->datafeed_callbacks; l; l = l->next) {
803 if (sr_log_loglevel_get() >= SR_LOG_DBG)
804 datafeed_dump(packet);
806 cb_struct->cb(sdi, packet, cb_struct->cb_data);
813 * Add an event source for a file descriptor.
815 * @param session The session to use. Must not be NULL.
816 * @param pollfd The GPollFD.
817 * @param[in] timeout Max time to wait before the callback is called,
819 * @param cb Callback function to add. Must not be NULL.
820 * @param cb_data Data for the callback function. Can be NULL.
821 * @param poll_object TODO.
823 * @retval SR_OK Success.
824 * @retval SR_ERR_ARG Invalid argument.
826 static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd,
827 int timeout, sr_receive_data_callback cb, void *cb_data, gintptr poll_object)
829 struct source *new_sources, *s;
830 GPollFD *new_pollfds;
833 sr_err("%s: cb was NULL", __func__);
837 /* Note: cb_data can be NULL, that's not a bug. */
839 new_pollfds = g_realloc(session->pollfds,
840 sizeof(GPollFD) * (session->num_sources + 1));
841 new_sources = g_realloc(session->sources, sizeof(struct source) *
842 (session->num_sources + 1));
844 new_pollfds[session->num_sources] = *pollfd;
845 s = &new_sources[session->num_sources++];
846 s->timeout = timeout;
848 s->cb_data = cb_data;
849 s->poll_object = poll_object;
850 session->pollfds = new_pollfds;
851 session->sources = new_sources;
853 if (timeout != session->source_timeout && timeout > 0
854 && (session->source_timeout == -1 || timeout < session->source_timeout))
855 session->source_timeout = timeout;
861 * Add an event source for a file descriptor.
863 * @param session The session to use. Must not be NULL.
864 * @param fd The file descriptor.
865 * @param events Events to check for.
866 * @param timeout Max time to wait before the callback is called, ignored if 0.
867 * @param cb Callback function to add. Must not be NULL.
868 * @param cb_data Data for the callback function. Can be NULL.
870 * @retval SR_OK Success.
871 * @retval SR_ERR_ARG Invalid argument.
875 SR_API int sr_session_source_add(struct sr_session *session, int fd,
876 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
884 return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)fd);
888 * Add an event source for a GPollFD.
890 * @param session The session to use. Must not be NULL.
891 * @param pollfd The GPollFD.
892 * @param timeout Max time to wait before the callback is called, ignored if 0.
893 * @param cb Callback function to add. Must not be NULL.
894 * @param cb_data Data for the callback function. Can be NULL.
896 * @retval SR_OK Success.
897 * @retval SR_ERR_ARG Invalid argument.
901 SR_API int sr_session_source_add_pollfd(struct sr_session *session,
902 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
905 return _sr_session_source_add(session, pollfd, timeout, cb,
906 cb_data, (gintptr)pollfd);
910 * Add an event source for a GIOChannel.
912 * @param session The session to use. Must not be NULL.
913 * @param channel The GIOChannel.
914 * @param events Events to poll on.
915 * @param timeout Max time to wait before the callback is called, ignored if 0.
916 * @param cb Callback function to add. Must not be NULL.
917 * @param cb_data Data for the callback function. Can be NULL.
919 * @retval SR_OK Success.
920 * @retval SR_ERR_ARG Invalid argument.
924 SR_API int sr_session_source_add_channel(struct sr_session *session,
925 GIOChannel *channel, int events, int timeout,
926 sr_receive_data_callback cb, void *cb_data)
931 g_io_channel_win32_make_pollfd(channel, events, &p);
933 p.fd = g_io_channel_unix_get_fd(channel);
938 return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)channel);
942 * Remove the source belonging to the specified channel.
944 * @param session The session to use. Must not be NULL.
945 * @param poll_object The channel for which the source should be removed.
947 * @retval SR_OK Success
948 * @retval SR_ERR_ARG Invalid arguments
949 * @retval SR_ERR_BUG Internal error
951 static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object)
955 if (!session->sources || !session->num_sources) {
956 sr_err("%s: sources was NULL", __func__);
960 for (old = 0; old < session->num_sources; old++) {
961 if (session->sources[old].poll_object == poll_object)
965 /* fd not found, nothing to do */
966 if (old == session->num_sources)
969 session->num_sources--;
971 if (old != session->num_sources) {
972 memmove(&session->pollfds[old], &session->pollfds[old + 1],
973 (session->num_sources - old) * sizeof(GPollFD));
974 memmove(&session->sources[old], &session->sources[old + 1],
975 (session->num_sources - old) * sizeof(struct source));
978 session->pollfds = g_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
979 session->sources = g_realloc(session->sources, sizeof(struct source) * session->num_sources);
985 * Remove the source belonging to the specified file descriptor.
987 * @param session The session to use. Must not be NULL.
988 * @param fd The file descriptor for which the source should be removed.
990 * @retval SR_OK Success
991 * @retval SR_ERR_ARG Invalid argument
992 * @retval SR_ERR_BUG Internal error.
996 SR_API int sr_session_source_remove(struct sr_session *session, int fd)
998 return _sr_session_source_remove(session, (gintptr)fd);
1002 * Remove the source belonging to the specified poll descriptor.
1004 * @param session The session to use. Must not be NULL.
1005 * @param pollfd The poll descriptor for which the source should be removed.
1007 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
1008 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
1013 SR_API int sr_session_source_remove_pollfd(struct sr_session *session,
1016 return _sr_session_source_remove(session, (gintptr)pollfd);
1020 * Remove the source belonging to the specified channel.
1022 * @param session The session to use. Must not be NULL.
1023 * @param channel The channel for which the source should be removed.
1025 * @retval SR_OK Success.
1026 * @retval SR_ERR_ARG Invalid argument.
1027 * @return SR_ERR_BUG Internal error.
1031 SR_API int sr_session_source_remove_channel(struct sr_session *session,
1032 GIOChannel *channel)
1034 return _sr_session_source_remove(session, (gintptr)channel);
1037 static void copy_src(struct sr_config *src, struct sr_datafeed_meta *meta_copy)
1039 g_variant_ref(src->data);
1040 meta_copy->config = g_slist_append(meta_copy->config,
1041 g_memdup(src, sizeof(struct sr_config)));
1044 SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet,
1045 struct sr_datafeed_packet **copy)
1047 const struct sr_datafeed_meta *meta;
1048 struct sr_datafeed_meta *meta_copy;
1049 const struct sr_datafeed_logic *logic;
1050 struct sr_datafeed_logic *logic_copy;
1051 const struct sr_datafeed_analog *analog;
1052 struct sr_datafeed_analog *analog_copy;
1055 *copy = g_malloc0(sizeof(struct sr_datafeed_packet));
1056 (*copy)->type = packet->type;
1058 switch (packet->type) {
1064 payload = g_malloc(sizeof(struct sr_datafeed_header));
1065 memcpy(payload, packet->payload, sizeof(struct sr_datafeed_header));
1066 (*copy)->payload = payload;
1069 meta = packet->payload;
1070 meta_copy = g_malloc0(sizeof(struct sr_datafeed_meta));
1071 g_slist_foreach(meta->config, (GFunc)copy_src, meta_copy->config);
1072 (*copy)->payload = meta_copy;
1075 logic = packet->payload;
1076 logic_copy = g_malloc(sizeof(logic));
1077 logic_copy->length = logic->length;
1078 logic_copy->unitsize = logic->unitsize;
1079 memcpy(logic_copy->data, logic->data, logic->length * logic->unitsize);
1080 (*copy)->payload = logic_copy;
1083 analog = packet->payload;
1084 analog_copy = g_malloc(sizeof(analog));
1085 analog_copy->channels = g_slist_copy(analog->channels);
1086 analog_copy->num_samples = analog->num_samples;
1087 analog_copy->mq = analog->mq;
1088 analog_copy->unit = analog->unit;
1089 analog_copy->mqflags = analog->mqflags;
1090 memcpy(analog_copy->data, analog->data,
1091 analog->num_samples * sizeof(float));
1092 (*copy)->payload = analog_copy;
1095 sr_err("Unknown packet type %d", packet->type);
1102 void sr_packet_free(struct sr_datafeed_packet *packet)
1104 const struct sr_datafeed_meta *meta;
1105 const struct sr_datafeed_logic *logic;
1106 const struct sr_datafeed_analog *analog;
1107 struct sr_config *src;
1110 switch (packet->type) {
1116 /* Payload is a simple struct. */
1117 g_free((void *)packet->payload);
1120 meta = packet->payload;
1121 for (l = meta->config; l; l = l->next) {
1123 g_variant_unref(src->data);
1126 g_slist_free(meta->config);
1127 g_free((void *)packet->payload);
1130 logic = packet->payload;
1131 g_free(logic->data);
1132 g_free((void *)packet->payload);
1135 analog = packet->payload;
1136 g_slist_free(analog->channels);
1137 g_free(analog->data);
1138 g_free((void *)packet->payload);
1141 sr_err("Unknown packet type %d", packet->type);