]> sigrok.org Git - libsigrok.git/blame - src/session.c
sr_analog_to_float: Support packets with multiple channels.
[libsigrok.git] / src / session.c
CommitLineData
a1bb33af 1/*
50985c20 2 * This file is part of the libsigrok project.
a1bb33af 3 *
c73d2ea4 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
c2bf5506 5 * Copyright (C) 2015 Daniel Elstner <daniel.kitta@gmail.com>
a1bb33af
UH
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
6ec6c43b 21#include <config.h>
32af282c 22#include <errno.h>
a1bb33af
UH
23#include <stdio.h>
24#include <stdlib.h>
25#include <unistd.h>
26#include <string.h>
544a4582 27#include <glib.h>
c1aae900 28#include <libsigrok/libsigrok.h>
45c59c8b 29#include "libsigrok-internal.h"
aa4b1107 30
2ad1deb8 31/** @cond PRIVATE */
3544f848 32#define LOG_PREFIX "session"
2ad1deb8 33/** @endcond */
a421dc1d 34
393fb9cb
UH
35/**
36 * @file
37 *
38 * Creating, using, or destroying libsigrok sessions.
39 */
40
7b870c38
UH
41/**
42 * @defgroup grp_session Session handling
43 *
44 * Creating, using, or destroying libsigrok sessions.
45 *
46 * @{
47 */
48
c2bf5506
DE
49struct datafeed_callback {
50 sr_datafeed_callback cb;
1f9813eb 51 void *cb_data;
c2bf5506 52};
aac0ea25 53
c2bf5506
DE
54/** Custom GLib event source for generic descriptor I/O.
55 * @internal
56 */
57struct fd_source {
58 GSource base;
62d7945f 59
c2bf5506
DE
60 int64_t timeout_us;
61 int64_t due_us;
92248e78 62
c2bf5506
DE
63 /* Meta-data needed to keep track of installed sources */
64 struct sr_session *session;
65 void *key;
544a4582 66
c2bf5506 67 GPollFD pollfd;
2726474a
ML
68};
69
c2bf5506
DE
70/** FD event source prepare() method.
71 */
72static gboolean fd_source_prepare(GSource *source, int *timeout)
73{
74 int64_t now_us;
75 struct fd_source *fsource;
76 int remaining_ms;
77
78 fsource = (struct fd_source *)source;
79
80 if (fsource->timeout_us >= 0) {
81 now_us = g_source_get_time(source);
82
83 if (fsource->due_us == 0) {
84 /* First-time initialization of the expiration time */
85 fsource->due_us = now_us + fsource->timeout_us;
86 }
87 remaining_ms = (MAX(0, fsource->due_us - now_us) + 999) / 1000;
88 } else {
89 remaining_ms = -1;
90 }
91 *timeout = remaining_ms;
92
93 return (remaining_ms == 0);
94}
95
96/** FD event source check() method.
97 */
98static gboolean fd_source_check(GSource *source)
99{
100 struct fd_source *fsource;
101 unsigned int revents;
102
103 fsource = (struct fd_source *)source;
104 revents = fsource->pollfd.revents;
105
106 return (revents != 0 || (fsource->timeout_us >= 0
107 && fsource->due_us <= g_source_get_time(source)));
108}
109
110/** FD event source dispatch() method.
111 */
112static gboolean fd_source_dispatch(GSource *source,
113 GSourceFunc callback, void *user_data)
114{
115 struct fd_source *fsource;
116 const char *name;
117 unsigned int revents;
118 gboolean keep;
119
120 fsource = (struct fd_source *)source;
121 name = g_source_get_name(source);
122 revents = fsource->pollfd.revents;
123
124 if (revents != 0) {
125 sr_spew("%s: %s " G_POLLFD_FORMAT ", revents 0x%.2X",
126 __func__, name, fsource->pollfd.fd, revents);
127 } else {
128 sr_spew("%s: %s " G_POLLFD_FORMAT ", timed out",
129 __func__, name, fsource->pollfd.fd);
130 }
131 if (!callback) {
132 sr_err("Callback not set, cannot dispatch event.");
133 return G_SOURCE_REMOVE;
134 }
135 keep = (*(sr_receive_data_callback)callback)
136 (fsource->pollfd.fd, revents, user_data);
137
138 if (fsource->timeout_us >= 0 && G_LIKELY(keep)
139 && G_LIKELY(!g_source_is_destroyed(source)))
140 fsource->due_us = g_source_get_time(source)
141 + fsource->timeout_us;
142 return keep;
143}
144
145/** FD event source finalize() method.
146 */
147static void fd_source_finalize(GSource *source)
148{
149 struct fd_source *fsource;
150
151 fsource = (struct fd_source *)source;
152
153 sr_dbg("%s: key %p", __func__, fsource->key);
154
155 sr_session_source_destroyed(fsource->session, fsource->key, source);
156}
157
158/** Create an event source for I/O on a file descriptor.
159 *
160 * In order to maintain API compatibility, this event source also doubles
161 * as a timer event source.
162 *
163 * @param session The session the event source belongs to.
164 * @param key The key used to identify this source.
165 * @param fd The file descriptor or HANDLE.
166 * @param timeout_ms The timeout interval in ms, or -1 to wait indefinitely.
167 * @return A new event source object, or NULL on failure.
168 */
169static GSource *fd_source_new(struct sr_session *session, void *key,
170 gintptr fd, int events, int timeout_ms)
171{
172 static GSourceFuncs fd_source_funcs = {
173 .prepare = &fd_source_prepare,
174 .check = &fd_source_check,
175 .dispatch = &fd_source_dispatch,
176 .finalize = &fd_source_finalize
177 };
178 GSource *source;
179 struct fd_source *fsource;
180
181 source = g_source_new(&fd_source_funcs, sizeof(struct fd_source));
182 fsource = (struct fd_source *)source;
183
184 g_source_set_name(source, (fd < 0) ? "timer" : "fd");
185
186 if (timeout_ms >= 0) {
187 fsource->timeout_us = 1000 * (int64_t)timeout_ms;
188 fsource->due_us = 0;
189 } else {
190 fsource->timeout_us = -1;
191 fsource->due_us = INT64_MAX;
192 }
193 fsource->session = session;
194 fsource->key = key;
195
196 fsource->pollfd.fd = fd;
197 fsource->pollfd.events = events;
198 fsource->pollfd.revents = 0;
199
200 if (fd >= 0)
201 g_source_add_poll(source, &fsource->pollfd);
202
203 return source;
204}
205
9f45fb3a
UH
206/**
207 * Create a new session.
7efe889e 208 *
61e6e2da 209 * @param ctx The context in which to create the new session.
7efe889e
UH
210 * @param new_session This will contain a pointer to the newly created
211 * session if the return value is SR_OK, otherwise the value
212 * is undefined and should not be used. Must not be NULL.
9f45fb3a 213 *
0812c40e 214 * @retval SR_OK Success.
41de54ff 215 * @retval SR_ERR_ARG Invalid argument.
47117241 216 *
0812c40e 217 * @since 0.4.0
9f45fb3a 218 */
61e6e2da
ML
219SR_API int sr_session_new(struct sr_context *ctx,
220 struct sr_session **new_session)
a1bb33af 221{
3337e9a1 222 struct sr_session *session;
a1bb33af 223
41de54ff
UH
224 if (!new_session)
225 return SR_ERR_ARG;
226
3337e9a1 227 session = g_malloc0(sizeof(struct sr_session));
b7e94111 228
4ed5d21d 229 session->ctx = ctx;
faa5d7d9 230
c2bf5506 231 g_mutex_init(&session->main_mutex);
faa5d7d9 232
c2bf5506
DE
233 /* To maintain API compatibility, we need a lookup table
234 * which maps poll_object IDs to GSource* pointers.
235 */
236 session->event_sources = g_hash_table_new(NULL, NULL);
0812c40e 237
3337e9a1 238 *new_session = session;
0812c40e
ML
239
240 return SR_OK;
a1bb33af
UH
241}
242
9f45fb3a 243/**
0812c40e 244 * Destroy a session.
9f45fb3a
UH
245 * This frees up all memory used by the session.
246 *
7efe889e
UH
247 * @param session The session to destroy. Must not be NULL.
248 *
04cb9157 249 * @retval SR_OK Success.
0812c40e 250 * @retval SR_ERR_ARG Invalid session passed.
47117241 251 *
0812c40e 252 * @since 0.4.0
9f45fb3a 253 */
0812c40e 254SR_API int sr_session_destroy(struct sr_session *session)
a1bb33af 255{
9f45fb3a 256 if (!session) {
a421dc1d 257 sr_err("%s: session was NULL", __func__);
0812c40e 258 return SR_ERR_ARG;
9f45fb3a
UH
259 }
260
0812c40e 261 sr_session_dev_remove_all(session);
1de3cced
ML
262 g_slist_free_full(session->owned_devs, (GDestroyNotify)sr_dev_inst_free);
263
c2bf5506
DE
264 g_hash_table_unref(session->event_sources);
265
266 g_mutex_clear(&session->main_mutex);
faa5d7d9 267
a1bb33af 268 g_free(session);
0812c40e 269
e0508e67 270 return SR_OK;
a1bb33af
UH
271}
272
9f45fb3a 273/**
0812c40e 274 * Remove all the devices from a session.
9f45fb3a
UH
275 *
276 * The session itself (i.e., the struct sr_session) is not free'd and still
277 * exists after this function returns.
278 *
7efe889e
UH
279 * @param session The session to use. Must not be NULL.
280 *
04cb9157 281 * @retval SR_OK Success.
0812c40e 282 * @retval SR_ERR_BUG Invalid session passed.
47117241 283 *
0812c40e 284 * @since 0.4.0
9f45fb3a 285 */
0812c40e 286SR_API int sr_session_dev_remove_all(struct sr_session *session)
a1bb33af 287{
0812c40e
ML
288 struct sr_dev_inst *sdi;
289 GSList *l;
290
9f45fb3a 291 if (!session) {
a421dc1d 292 sr_err("%s: session was NULL", __func__);
0812c40e
ML
293 return SR_ERR_ARG;
294 }
295
296 for (l = session->devs; l; l = l->next) {
297 sdi = (struct sr_dev_inst *) l->data;
298 sdi->session = NULL;
9f45fb3a
UH
299 }
300
681803df 301 g_slist_free(session->devs);
bb7ef793 302 session->devs = NULL;
e0508e67
UH
303
304 return SR_OK;
a1bb33af
UH
305}
306
9f45fb3a 307/**
0812c40e 308 * Add a device instance to a session.
9f45fb3a 309 *
7efe889e 310 * @param session The session to add to. Must not be NULL.
0812c40e 311 * @param sdi The device instance to add to a session. Must not
de4d3f99
BV
312 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
313 * not be NULL.
9f45fb3a 314 *
04cb9157
MH
315 * @retval SR_OK Success.
316 * @retval SR_ERR_ARG Invalid argument.
47117241 317 *
0812c40e 318 * @since 0.4.0
9f45fb3a 319 */
0812c40e
ML
320SR_API int sr_session_dev_add(struct sr_session *session,
321 struct sr_dev_inst *sdi)
a1bb33af 322{
5451816f 323 int ret;
a1bb33af 324
de4d3f99 325 if (!sdi) {
a421dc1d 326 sr_err("%s: sdi was NULL", __func__);
9f45fb3a
UH
327 return SR_ERR_ARG;
328 }
329
d6eb0c33 330 if (!session) {
a421dc1d 331 sr_err("%s: session was NULL", __func__);
0812c40e
ML
332 return SR_ERR_ARG;
333 }
334
335 /* If sdi->session is not NULL, the device is already in this or
336 * another session. */
337 if (sdi->session) {
338 sr_err("%s: already assigned to session", __func__);
339 return SR_ERR_ARG;
d6eb0c33
UH
340 }
341
de4d3f99
BV
342 /* If sdi->driver is NULL, this is a virtual device. */
343 if (!sdi->driver) {
d6eb0c33 344 /* Just add the device, don't run dev_open(). */
de4d3f99 345 session->devs = g_slist_append(session->devs, (gpointer)sdi);
0812c40e 346 sdi->session = session;
d6eb0c33 347 return SR_OK;
9f45fb3a
UH
348 }
349
de4d3f99
BV
350 /* sdi->driver is non-NULL (i.e. we have a real device). */
351 if (!sdi->driver->dev_open) {
a421dc1d 352 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
8ec95d22 353 return SR_ERR_BUG;
9f45fb3a
UH
354 }
355
de4d3f99 356 session->devs = g_slist_append(session->devs, (gpointer)sdi);
0812c40e 357 sdi->session = session;
aa4b1107 358
5451816f 359 if (session->running) {
32b7cd4f
DE
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)",
365 sr_strerror(ret));
366 return ret;
367 }
5451816f 368 if ((ret = sdi->driver->dev_acquisition_start(sdi,
32b7cd4f 369 (void *)sdi)) != SR_OK) {
5451816f 370 sr_err("Failed to start acquisition of device in "
32b7cd4f
DE
371 "running session (%s)", sr_strerror(ret));
372 return ret;
373 }
5451816f
BV
374 }
375
e46b8fb1 376 return SR_OK;
a1bb33af
UH
377}
378
2bb311b4 379/**
0812c40e 380 * List all device instances attached to a session.
2bb311b4 381 *
7efe889e 382 * @param session The session to use. Must not be NULL.
2bb311b4
BV
383 * @param devlist A pointer where the device instance list will be
384 * stored on return. If no devices are in the session,
385 * this will be NULL. Each element in the list points
386 * to a struct sr_dev_inst *.
387 * The list must be freed by the caller, but not the
388 * elements pointed to.
389 *
04cb9157 390 * @retval SR_OK Success.
0812c40e 391 * @retval SR_ERR_ARG Invalid argument.
47117241 392 *
0812c40e 393 * @since 0.4.0
2bb311b4 394 */
0812c40e 395SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist)
2bb311b4 396{
2bb311b4 397 if (!session)
0812c40e
ML
398 return SR_ERR_ARG;
399
400 if (!devlist)
401 return SR_ERR_ARG;
2bb311b4
BV
402
403 *devlist = g_slist_copy(session->devs);
404
405 return SR_OK;
406}
407
9f45fb3a 408/**
0812c40e 409 * Remove all datafeed callbacks in a session.
9f45fb3a 410 *
7efe889e
UH
411 * @param session The session to use. Must not be NULL.
412 *
04cb9157 413 * @retval SR_OK Success.
0812c40e 414 * @retval SR_ERR_ARG Invalid session passed.
47117241 415 *
0812c40e 416 * @since 0.4.0
9f45fb3a 417 */
0812c40e 418SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session)
a1bb33af 419{
9f45fb3a 420 if (!session) {
a421dc1d 421 sr_err("%s: session was NULL", __func__);
0812c40e 422 return SR_ERR_ARG;
9f45fb3a
UH
423 }
424
2726474a 425 g_slist_free_full(session->datafeed_callbacks, g_free);
a1bb33af 426 session->datafeed_callbacks = NULL;
e0508e67
UH
427
428 return SR_OK;
a1bb33af
UH
429}
430
9f45fb3a 431/**
0812c40e 432 * Add a datafeed callback to a session.
9f45fb3a 433 *
7efe889e 434 * @param session The session to use. Must not be NULL.
d08490aa 435 * @param cb Function to call when a chunk of data is received.
0abee507 436 * Must not be NULL.
85222791 437 * @param cb_data Opaque pointer passed in by the caller.
a1645fcd 438 *
04cb9157
MH
439 * @retval SR_OK Success.
440 * @retval SR_ERR_BUG No session exists.
47117241
UH
441 *
442 * @since 0.3.0
9f45fb3a 443 */
0812c40e
ML
444SR_API int sr_session_datafeed_callback_add(struct sr_session *session,
445 sr_datafeed_callback cb, void *cb_data)
a1bb33af 446{
2726474a
ML
447 struct datafeed_callback *cb_struct;
448
9f45fb3a 449 if (!session) {
a421dc1d 450 sr_err("%s: session was NULL", __func__);
e0508e67 451 return SR_ERR_BUG;
9f45fb3a
UH
452 }
453
0abee507 454 if (!cb) {
a421dc1d 455 sr_err("%s: cb was NULL", __func__);
0abee507
UH
456 return SR_ERR_ARG;
457 }
9f45fb3a 458
91219afc 459 cb_struct = g_malloc0(sizeof(struct datafeed_callback));
2726474a
ML
460 cb_struct->cb = cb;
461 cb_struct->cb_data = cb_data;
462
62c82025 463 session->datafeed_callbacks =
2726474a 464 g_slist_append(session->datafeed_callbacks, cb_struct);
e0508e67
UH
465
466 return SR_OK;
a1bb33af
UH
467}
468
9f42e2e6
UH
469/**
470 * Get the trigger assigned to this session.
471 *
472 * @param session The session to use.
473 *
474 * @retval NULL Invalid (NULL) session was passed to the function.
475 * @retval other The trigger assigned to this session (can be NULL).
476 *
477 * @since 0.4.0
478 */
0812c40e 479SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session)
7b5e6d29 480{
9f42e2e6
UH
481 if (!session)
482 return NULL;
483
7b5e6d29
BV
484 return session->trigger;
485}
486
9f42e2e6
UH
487/**
488 * Set the trigger of this session.
489 *
490 * @param session The session to use. Must not be NULL.
491 * @param trig The trigger to assign to this session. Can be NULL.
492 *
493 * @retval SR_OK Success.
494 * @retval SR_ERR_ARG Invalid argument.
495 *
496 * @since 0.4.0
497 */
0812c40e 498SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig)
7b5e6d29 499{
9f42e2e6
UH
500 if (!session)
501 return SR_ERR_ARG;
502
7b5e6d29
BV
503 session->trigger = trig;
504
505 return SR_OK;
506}
507
7b5e6d29
BV
508static int verify_trigger(struct sr_trigger *trigger)
509{
510 struct sr_trigger_stage *stage;
511 struct sr_trigger_match *match;
512 GSList *l, *m;
513
514 if (!trigger->stages) {
515 sr_err("No trigger stages defined.");
516 return SR_ERR;
517 }
518
519 sr_spew("Checking trigger:");
520 for (l = trigger->stages; l; l = l->next) {
521 stage = l->data;
522 if (!stage->matches) {
523 sr_err("Stage %d has no matches defined.", stage->stage);
524 return SR_ERR;
525 }
526 for (m = stage->matches; m; m = m->next) {
527 match = m->data;
528 if (!match->channel) {
529 sr_err("Stage %d match has no channel.", stage->stage);
530 return SR_ERR;
531 }
532 if (!match->match) {
533 sr_err("Stage %d match is not defined.", stage->stage);
534 return SR_ERR;
535 }
536 sr_spew("Stage %d match on channel %s, match %d", stage->stage,
537 match->channel->name, match->match);
538 }
539 }
540
541 return SR_OK;
542}
1beccaed 543
c2bf5506
DE
544/** Set up the main context the session will be executing in.
545 *
546 * Must be called just before the session starts, by the thread which
547 * will execute the session main loop. Once acquired, the main context
548 * pointer is immutable for the duration of the session run.
549 */
550static int set_main_context(struct sr_session *session)
551{
552 GMainContext *def_context;
553
554 /* May happen if sr_session_start() is called again after
555 * sr_session_run(), but the session hasn't been stopped yet.
556 */
557 if (session->main_loop) {
558 sr_err("Cannot set main context; main loop already created.");
559 return SR_ERR;
560 }
561
562 g_mutex_lock(&session->main_mutex);
563
564 def_context = g_main_context_get_thread_default();
565
566 if (!def_context)
567 def_context = g_main_context_default();
568 /*
569 * Try to use an existing main context if possible, but only if we
570 * can make it owned by the current thread. Otherwise, create our
571 * own main context so that event source callbacks can execute in
572 * the session thread.
573 */
574 if (g_main_context_acquire(def_context)) {
575 g_main_context_release(def_context);
576
577 sr_dbg("Using thread-default main context.");
578
579 session->main_context = def_context;
580 session->main_context_is_default = TRUE;
581 } else {
582 sr_dbg("Creating our own main context.");
583
584 session->main_context = g_main_context_new();
585 session->main_context_is_default = FALSE;
586 }
587 g_mutex_unlock(&session->main_mutex);
588
589 return SR_OK;
590}
591
592/** Unset the main context used for the current session run.
593 *
594 * Must be called right after stopping the session. Note that if the
595 * session is stopped asynchronously, the main loop may still be running
596 * after the main context has been unset. This is OK as long as no new
597 * event sources are created -- the main loop holds its own reference
598 * to the main context.
599 */
600static int unset_main_context(struct sr_session *session)
601{
602 int ret;
603
604 g_mutex_lock(&session->main_mutex);
605
606 if (session->main_context) {
607 if (!session->main_context_is_default)
608 g_main_context_unref(session->main_context);
609
610 session->main_context = NULL;
611 ret = SR_OK;
612 } else {
613 /* May happen if the set/unset calls are not matched.
614 */
615 sr_err("No main context to unset.");
616 ret = SR_ERR;
617 }
618 g_mutex_unlock(&session->main_mutex);
619
620 return ret;
621}
622
9f45fb3a
UH
623/**
624 * Start a session.
625 *
7efe889e
UH
626 * @param session The session to use. Must not be NULL.
627 *
04cb9157 628 * @retval SR_OK Success.
0812c40e 629 * @retval SR_ERR_ARG Invalid session passed.
47117241 630 *
0812c40e 631 * @since 0.4.0
9f45fb3a 632 */
0812c40e 633SR_API int sr_session_start(struct sr_session *session)
7d658874 634{
de4d3f99 635 struct sr_dev_inst *sdi;
013ec84b
BV
636 struct sr_channel *ch;
637 GSList *l, *c;
638 int enabled_channels, ret;
7d658874 639
9f45fb3a 640 if (!session) {
0812c40e
ML
641 sr_err("%s: session was NULL", __func__);
642 return SR_ERR_ARG;
9f45fb3a
UH
643 }
644
bb7ef793 645 if (!session->devs) {
a421dc1d 646 sr_err("%s: session->devs was NULL; a session "
9f45fb3a 647 "cannot be started without devices.", __func__);
0812c40e 648 return SR_ERR_ARG;
9f45fb3a
UH
649 }
650
7b5e6d29
BV
651 if (session->trigger && verify_trigger(session->trigger) != SR_OK)
652 return SR_ERR;
653
c2bf5506
DE
654 ret = set_main_context(session);
655 if (ret != SR_OK)
656 return ret;
657
658 session->running = TRUE;
659
c7142604 660 sr_info("Starting.");
9f45fb3a 661
bb7ef793 662 for (l = session->devs; l; l = l->next) {
de4d3f99 663 sdi = l->data;
013ec84b
BV
664 enabled_channels = 0;
665 for (c = sdi->channels; c; c = c->next) {
666 ch = c->data;
667 if (ch->enabled) {
668 enabled_channels++;
669 break;
670 }
671 }
672 if (enabled_channels == 0) {
673 ret = SR_ERR;
1b9e567b
SA
674 sr_err("%s using connection %s has no enabled channels!",
675 sdi->driver->name, sdi->connection_id);
013ec84b
BV
676 break;
677 }
678
32b7cd4f
DE
679 if ((ret = sr_config_commit(sdi)) != SR_OK) {
680 sr_err("Failed to commit device settings before "
681 "starting acquisition (%s)", sr_strerror(ret));
682 break;
683 }
de4d3f99 684 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
a421dc1d 685 sr_err("%s: could not start an acquisition "
568dcacc 686 "(%s)", __func__, sr_strerror(ret));
7d658874 687 break;
9f45fb3a 688 }
7d658874
BV
689 }
690
c2bf5506
DE
691 if (ret != SR_OK) {
692 unset_main_context(session);
693 session->running = FALSE;
694 }
9f45fb3a
UH
695 /* TODO: What if there are multiple devices? Which return code? */
696
7d658874
BV
697 return ret;
698}
699
9f45fb3a 700/**
0812c40e 701 * Run a session.
9f45fb3a 702 *
7efe889e
UH
703 * @param session The session to use. Must not be NULL.
704 *
04cb9157 705 * @retval SR_OK Success.
0812c40e 706 * @retval SR_ERR_ARG Invalid session passed.
faa5d7d9 707 * @retval SR_ERR Error during event processing.
47117241 708 *
0812c40e 709 * @since 0.4.0
9f45fb3a 710 */
0812c40e 711SR_API int sr_session_run(struct sr_session *session)
7d658874 712{
9f45fb3a 713 if (!session) {
0812c40e
ML
714 sr_err("%s: session was NULL", __func__);
715 return SR_ERR_ARG;
9f45fb3a 716 }
bb7ef793 717 if (!session->devs) {
9f45fb3a 718 /* TODO: Actually the case? */
a421dc1d 719 sr_err("%s: session->devs was NULL; a session "
9f45fb3a 720 "cannot be run without devices.", __func__);
0812c40e 721 return SR_ERR_ARG;
9f45fb3a 722 }
c2bf5506
DE
723 if (session->main_loop) {
724 sr_err("Main loop already created.");
725 return SR_ERR;
726 }
727 if (g_hash_table_size(session->event_sources) == 0) {
ab0b3458
DE
728 sr_warn("No event sources, returning immediately.");
729 return SR_OK;
c2bf5506 730 }
9f45fb3a 731
c2bf5506
DE
732 g_mutex_lock(&session->main_mutex);
733 if (!session->main_context) {
734 sr_err("Cannot run without main context.");
735 g_mutex_unlock(&session->main_mutex);
736 return SR_ERR;
737 }
a421dc1d 738 sr_info("Running.");
7d658874 739
c2bf5506
DE
740 session->main_loop = g_main_loop_new(session->main_context, FALSE);
741 g_mutex_unlock(&session->main_mutex);
742
743 g_main_loop_run(session->main_loop);
744
745 g_main_loop_unref(session->main_loop);
746 session->main_loop = NULL;
747
e0508e67 748 return SR_OK;
7d658874
BV
749}
750
c2bf5506 751static gboolean session_stop_sync(void *user_data)
a1bb33af 752{
c2bf5506 753 struct sr_session *session;
de4d3f99 754 struct sr_dev_inst *sdi;
c2bf5506 755 GSList *node;
a1bb33af 756
c2bf5506
DE
757 session = user_data;
758
759 if (!session->running)
760 return G_SOURCE_REMOVE;
9f45fb3a 761
a421dc1d 762 sr_info("Stopping.");
e0508e67 763
c2bf5506
DE
764 for (node = session->devs; node; node = node->next) {
765 sdi = node->data;
766 if (sdi->driver && sdi->driver->dev_acquisition_stop)
767 sdi->driver->dev_acquisition_stop(sdi, sdi);
a1bb33af 768 }
5451816f 769 session->running = FALSE;
9f45fb3a 770
c2bf5506 771 return G_SOURCE_REMOVE;
a1bb33af
UH
772}
773
33c6e4c5 774/**
0812c40e 775 * Stop a session.
33c6e4c5 776 *
0812c40e
ML
777 * The session is stopped immediately, with all acquisition sessions being
778 * stopped and hardware drivers cleaned up.
33c6e4c5
AG
779 *
780 * If the session is run in a separate thread, this function will not block
781 * until the session is finished executing. It is the caller's responsibility
782 * to wait for the session thread to return before assuming that the session is
783 * completely decommissioned.
784 *
7efe889e
UH
785 * @param session The session to use. Must not be NULL.
786 *
04cb9157 787 * @retval SR_OK Success.
0812c40e 788 * @retval SR_ERR_ARG Invalid session passed.
c2bf5506 789 * @retval SR_ERR Other error.
47117241 790 *
0812c40e 791 * @since 0.4.0
33c6e4c5 792 */
0812c40e 793SR_API int sr_session_stop(struct sr_session *session)
33c6e4c5
AG
794{
795 if (!session) {
796 sr_err("%s: session was NULL", __func__);
c2bf5506 797 return SR_ERR_ARG;
33c6e4c5 798 }
c2bf5506 799 g_mutex_lock(&session->main_mutex);
33c6e4c5 800
c2bf5506
DE
801 if (session->main_context) {
802 g_main_context_invoke(session->main_context,
803 &session_stop_sync, session);
804 } else {
805 sr_err("No main context set; already stopped?");
806 }
807 g_mutex_unlock(&session->main_mutex);
33c6e4c5 808
c2bf5506 809 return unset_main_context(session);
33c6e4c5
AG
810}
811
9f45fb3a 812/**
a1645fcd 813 * Debug helper.
9f45fb3a 814 *
996b0c72 815 * @param packet The packet to show debugging information for.
9f45fb3a 816 */
bf53457d 817static void datafeed_dump(const struct sr_datafeed_packet *packet)
7d2afd6c 818{
bf53457d
JH
819 const struct sr_datafeed_logic *logic;
820 const struct sr_datafeed_analog *analog;
1954dfa9 821 const struct sr_datafeed_analog2 *analog2;
7d2afd6c 822
ca7dbb56 823 /* Please use the same order as in libsigrok.h. */
7d2afd6c
BV
824 switch (packet->type) {
825 case SR_DF_HEADER:
a421dc1d 826 sr_dbg("bus: Received SR_DF_HEADER packet.");
7d2afd6c 827 break;
55c9f09d
UH
828 case SR_DF_END:
829 sr_dbg("bus: Received SR_DF_END packet.");
7d2afd6c 830 break;
c71bac3b 831 case SR_DF_META:
a421dc1d 832 sr_dbg("bus: Received SR_DF_META packet.");
ee7489d2 833 break;
55c9f09d
UH
834 case SR_DF_TRIGGER:
835 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
836 break;
7d2afd6c
BV
837 case SR_DF_LOGIC:
838 logic = packet->payload;
7ea45862
UH
839 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, "
840 "unitsize = %d).", logic->length, logic->unitsize);
7d2afd6c 841 break;
ee7489d2
BV
842 case SR_DF_ANALOG:
843 analog = packet->payload;
a421dc1d
UH
844 sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).",
845 analog->num_samples);
ee7489d2 846 break;
6ea7669c 847 case SR_DF_FRAME_BEGIN:
a421dc1d 848 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
6ea7669c
BV
849 break;
850 case SR_DF_FRAME_END:
a421dc1d 851 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
6ea7669c 852 break;
55c9f09d
UH
853 case SR_DF_ANALOG2:
854 analog2 = packet->payload;
855 sr_dbg("bus: Received SR_DF_ANALOG2 packet (%d samples).",
856 analog2->num_samples);
857 break;
7d2afd6c 858 default:
a421dc1d 859 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
9f45fb3a 860 break;
7d2afd6c 861 }
7d2afd6c
BV
862}
863
9f45fb3a 864/**
a1645fcd
BV
865 * Send a packet to whatever is listening on the datafeed bus.
866 *
867 * Hardware drivers use this to send a data packet to the frontend.
9f45fb3a 868 *
6b2d8d3e 869 * @param sdi TODO.
31ccebc4 870 * @param packet The datafeed packet to send to the session bus.
44dae539 871 *
04cb9157
MH
872 * @retval SR_OK Success.
873 * @retval SR_ERR_ARG Invalid argument.
b4bd7088
UH
874 *
875 * @private
9f45fb3a 876 */
de4d3f99 877SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
8143cfdc 878 const struct sr_datafeed_packet *packet)
a1bb33af
UH
879{
880 GSList *l;
2726474a 881 struct datafeed_callback *cb_struct;
c0a1e532
UH
882 struct sr_datafeed_packet *packet_in, *packet_out;
883 struct sr_transform *t;
884 int ret;
a1bb33af 885
de4d3f99 886 if (!sdi) {
a421dc1d 887 sr_err("%s: sdi was NULL", __func__);
e0508e67 888 return SR_ERR_ARG;
9f45fb3a
UH
889 }
890
e0508e67 891 if (!packet) {
a421dc1d 892 sr_err("%s: packet was NULL", __func__);
e0508e67 893 return SR_ERR_ARG;
9f45fb3a
UH
894 }
895
79f92686
BV
896 if (!sdi->session) {
897 sr_err("%s: session was NULL", __func__);
898 return SR_ERR_BUG;
899 }
900
c0a1e532
UH
901 /*
902 * Pass the packet to the first transform module. If that returns
903 * another packet (instead of NULL), pass that packet to the next
904 * transform module in the list, and so on.
905 */
906 packet_in = (struct sr_datafeed_packet *)packet;
907 for (l = sdi->session->transforms; l; l = l->next) {
908 t = l->data;
909 sr_spew("Running transform module '%s'.", t->module->id);
910 ret = t->module->receive(t, packet_in, &packet_out);
911 if (ret < 0) {
912 sr_err("Error while running transform module: %d.", ret);
913 return SR_ERR;
914 }
915 if (!packet_out) {
916 /*
917 * If any of the transforms don't return an output
918 * packet, abort.
919 */
920 sr_spew("Transform module didn't return a packet, aborting.");
921 return SR_OK;
922 } else {
923 /*
924 * Use this transform module's output packet as input
925 * for the next transform module.
926 */
927 packet_in = packet_out;
928 }
929 }
4ec436c4 930 packet = packet_in;
c0a1e532
UH
931
932 /*
933 * If the last transform did output a packet, pass it to all datafeed
934 * callbacks.
935 */
3337e9a1 936 for (l = sdi->session->datafeed_callbacks; l; l = l->next) {
18beaeff
BV
937 if (sr_log_loglevel_get() >= SR_LOG_DBG)
938 datafeed_dump(packet);
2726474a
ML
939 cb_struct = l->data;
940 cb_struct->cb(sdi, packet, cb_struct->cb_data);
a1bb33af 941 }
9f45fb3a 942
e0508e67 943 return SR_OK;
a1bb33af
UH
944}
945
6b2d8d3e
UH
946/**
947 * Add an event source for a file descriptor.
948 *
7efe889e 949 * @param session The session to use. Must not be NULL.
c2bf5506
DE
950 * @param key The key which identifies the event source.
951 * @param source An event source object. Must not be NULL.
04cb9157
MH
952 * @retval SR_OK Success.
953 * @retval SR_ERR_ARG Invalid argument.
c2bf5506
DE
954 * @retval SR_ERR_BUG Event source with @a key already installed.
955 * @retval SR_ERR Other error.
6b2d8d3e 956 */
62d7945f 957SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
c2bf5506 958 void *key, GSource *source)
544a4582 959{
c2bf5506
DE
960 int ret;
961 /*
962 * This must not ever happen, since the source has already been
963 * created and its finalize() method will remove the key for the
964 * already installed source. (Well it would, if we did not have
965 * another sanity check there.)
92248e78 966 */
c2bf5506
DE
967 if (g_hash_table_contains(session->event_sources, key)) {
968 sr_err("Event source with key %p already exists.", key);
969 return SR_ERR_BUG;
92248e78 970 }
c2bf5506
DE
971 g_hash_table_insert(session->event_sources, key, source);
972
973 g_mutex_lock(&session->main_mutex);
974
975 if (session->main_context) {
976 if (g_source_attach(source, session->main_context) > 0)
977 ret = SR_OK;
978 else
979 ret = SR_ERR;
faa5d7d9 980 } else {
c2bf5506
DE
981 sr_err("Cannot add event source without main context.");
982 ret = SR_ERR;
faa5d7d9 983 }
c2bf5506 984 g_mutex_unlock(&session->main_mutex);
faa5d7d9 985
c2bf5506 986 return ret;
534b634c
DE
987}
988
cbc1413f
DE
989SR_PRIV int sr_session_fd_source_add(struct sr_session *session,
990 void *key, gintptr fd, int events, int timeout,
c2bf5506 991 sr_receive_data_callback cb, void *cb_data)
534b634c 992{
c2bf5506
DE
993 GSource *source;
994 int ret;
534b634c 995
c2bf5506
DE
996 source = fd_source_new(session, key, fd, events, timeout);
997 if (!source)
534b634c 998 return SR_ERR;
534b634c 999
c2bf5506 1000 g_source_set_callback(source, (GSourceFunc)cb, cb_data, NULL);
534b634c 1001
c2bf5506
DE
1002 ret = sr_session_source_add_internal(session, key, source);
1003 g_source_unref(source);
544a4582 1004
c2bf5506 1005 return ret;
544a4582
BV
1006}
1007
9f45fb3a 1008/**
6b2d8d3e 1009 * Add an event source for a file descriptor.
9f45fb3a 1010 *
7efe889e 1011 * @param session The session to use. Must not be NULL.
534b634c 1012 * @param fd The file descriptor, or a negative value to create a timer source.
aac0ea25 1013 * @param events Events to check for.
faa5d7d9
DE
1014 * @param timeout Max time in ms to wait before the callback is called,
1015 * or -1 to wait indefinitely.
aac0ea25
LPC
1016 * @param cb Callback function to add. Must not be NULL.
1017 * @param cb_data Data for the callback function. Can be NULL.
9f45fb3a 1018 *
04cb9157
MH
1019 * @retval SR_OK Success.
1020 * @retval SR_ERR_ARG Invalid argument.
47117241
UH
1021 *
1022 * @since 0.3.0
aac0ea25 1023 */
0812c40e
ML
1024SR_API int sr_session_source_add(struct sr_session *session, int fd,
1025 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
aac0ea25 1026{
faa5d7d9 1027 if (fd < 0 && timeout < 0) {
c2bf5506 1028 sr_err("Cannot create timer source without timeout.");
faa5d7d9
DE
1029 return SR_ERR_ARG;
1030 }
cbc1413f 1031 return sr_session_fd_source_add(session, GINT_TO_POINTER(fd),
c2bf5506 1032 fd, events, timeout, cb, cb_data);
aac0ea25
LPC
1033}
1034
1035/**
1a895c61 1036 * Add an event source for a GPollFD.
aac0ea25 1037 *
7efe889e 1038 * @param session The session to use. Must not be NULL.
faa5d7d9
DE
1039 * @param pollfd The GPollFD. Must not be NULL.
1040 * @param timeout Max time in ms to wait before the callback is called,
1041 * or -1 to wait indefinitely.
aac0ea25
LPC
1042 * @param cb Callback function to add. Must not be NULL.
1043 * @param cb_data Data for the callback function. Can be NULL.
44dae539 1044 *
04cb9157
MH
1045 * @retval SR_OK Success.
1046 * @retval SR_ERR_ARG Invalid argument.
47117241
UH
1047 *
1048 * @since 0.3.0
9f45fb3a 1049 */
0812c40e
ML
1050SR_API int sr_session_source_add_pollfd(struct sr_session *session,
1051 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
1052 void *cb_data)
aac0ea25 1053{
faa5d7d9
DE
1054 if (!pollfd) {
1055 sr_err("%s: pollfd was NULL", __func__);
1056 return SR_ERR_ARG;
1057 }
cbc1413f 1058 return sr_session_fd_source_add(session, pollfd, pollfd->fd,
c2bf5506 1059 pollfd->events, timeout, cb, cb_data);
aac0ea25
LPC
1060}
1061
1062/**
1a895c61 1063 * Add an event source for a GIOChannel.
aac0ea25 1064 *
7efe889e 1065 * @param session The session to use. Must not be NULL.
aac0ea25
LPC
1066 * @param channel The GIOChannel.
1067 * @param events Events to poll on.
faa5d7d9
DE
1068 * @param timeout Max time in ms to wait before the callback is called,
1069 * or -1 to wait indefinitely.
aac0ea25
LPC
1070 * @param cb Callback function to add. Must not be NULL.
1071 * @param cb_data Data for the callback function. Can be NULL.
1072 *
04cb9157
MH
1073 * @retval SR_OK Success.
1074 * @retval SR_ERR_ARG Invalid argument.
47117241
UH
1075 *
1076 * @since 0.3.0
aac0ea25 1077 */
0812c40e
ML
1078SR_API int sr_session_source_add_channel(struct sr_session *session,
1079 GIOChannel *channel, int events, int timeout,
1080 sr_receive_data_callback cb, void *cb_data)
aac0ea25 1081{
c2bf5506 1082 GPollFD pollfd;
aac0ea25 1083
c2bf5506
DE
1084 if (!channel) {
1085 sr_err("%s: channel was NULL", __func__);
1086 return SR_ERR_ARG;
1087 }
1088 /* We should be using g_io_create_watch(), but can't without
1089 * changing the driver API, as the callback signature is different.
1090 */
faa5d7d9 1091#ifdef G_OS_WIN32
c2bf5506 1092 g_io_channel_win32_make_pollfd(channel, events, &pollfd);
aac0ea25 1093#else
c2bf5506
DE
1094 pollfd.fd = g_io_channel_unix_get_fd(channel);
1095 pollfd.events = events;
aac0ea25 1096#endif
cbc1413f 1097 return sr_session_fd_source_add(session, channel, pollfd.fd,
c2bf5506 1098 pollfd.events, timeout, cb, cb_data);
aac0ea25
LPC
1099}
1100
6b2d8d3e 1101/**
92248e78 1102 * Remove the source identified by the specified poll object.
6b2d8d3e 1103 *
7efe889e 1104 * @param session The session to use. Must not be NULL.
c2bf5506 1105 * @param key The key by which the source is identified.
6b2d8d3e 1106 *
04cb9157 1107 * @retval SR_OK Success
92248e78 1108 * @retval SR_ERR_BUG No event source for poll_object found.
6b2d8d3e 1109 */
92248e78 1110SR_PRIV int sr_session_source_remove_internal(struct sr_session *session,
c2bf5506 1111 void *key)
544a4582 1112{
c2bf5506 1113 GSource *source;
544a4582 1114
c2bf5506
DE
1115 source = g_hash_table_lookup(session->event_sources, key);
1116 /*
1117 * Trying to remove an already removed event source is problematic
92248e78
DE
1118 * since the poll_object handle may have been reused in the meantime.
1119 */
534b634c 1120 if (!source) {
c2bf5506
DE
1121 sr_warn("Cannot remove non-existing event source %p.", key);
1122 return SR_ERR_BUG;
534b634c 1123 }
c2bf5506 1124 g_source_destroy(source);
534b634c 1125
c2bf5506 1126 return SR_OK;
534b634c
DE
1127}
1128
6b2d8d3e 1129/**
aac0ea25
LPC
1130 * Remove the source belonging to the specified file descriptor.
1131 *
7efe889e 1132 * @param session The session to use. Must not be NULL.
1a895c61 1133 * @param fd The file descriptor for which the source should be removed.
aac0ea25 1134 *
04cb9157
MH
1135 * @retval SR_OK Success
1136 * @retval SR_ERR_ARG Invalid argument
04cb9157 1137 * @retval SR_ERR_BUG Internal error.
47117241
UH
1138 *
1139 * @since 0.3.0
aac0ea25 1140 */
0812c40e 1141SR_API int sr_session_source_remove(struct sr_session *session, int fd)
aac0ea25 1142{
c2bf5506 1143 return sr_session_source_remove_internal(session, GINT_TO_POINTER(fd));
aac0ea25
LPC
1144}
1145
1146/**
1147 * Remove the source belonging to the specified poll descriptor.
1148 *
7efe889e 1149 * @param session The session to use. Must not be NULL.
aac0ea25 1150 * @param pollfd The poll descriptor for which the source should be removed.
faa5d7d9 1151 * Must not be NULL.
aac0ea25
LPC
1152 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
1153 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
1154 * internal errors.
47117241
UH
1155 *
1156 * @since 0.2.0
aac0ea25 1157 */
0812c40e
ML
1158SR_API int sr_session_source_remove_pollfd(struct sr_session *session,
1159 GPollFD *pollfd)
aac0ea25 1160{
faa5d7d9
DE
1161 if (!pollfd) {
1162 sr_err("%s: pollfd was NULL", __func__);
1163 return SR_ERR_ARG;
1164 }
c2bf5506 1165 return sr_session_source_remove_internal(session, pollfd);
aac0ea25
LPC
1166}
1167
6b2d8d3e 1168/**
aac0ea25
LPC
1169 * Remove the source belonging to the specified channel.
1170 *
7efe889e 1171 * @param session The session to use. Must not be NULL.
1a895c61 1172 * @param channel The channel for which the source should be removed.
faa5d7d9 1173 * Must not be NULL.
04cb9157
MH
1174 * @retval SR_OK Success.
1175 * @retval SR_ERR_ARG Invalid argument.
04cb9157 1176 * @return SR_ERR_BUG Internal error.
47117241
UH
1177 *
1178 * @since 0.2.0
aac0ea25 1179 */
0812c40e
ML
1180SR_API int sr_session_source_remove_channel(struct sr_session *session,
1181 GIOChannel *channel)
aac0ea25 1182{
faa5d7d9
DE
1183 if (!channel) {
1184 sr_err("%s: channel was NULL", __func__);
1185 return SR_ERR_ARG;
1186 }
c2bf5506
DE
1187 return sr_session_source_remove_internal(session, channel);
1188}
1189
1190/** Unregister an event source that has been destroyed.
1191 *
1192 * This is intended to be called from a source's finalize() method.
1193 *
1194 * @param session The session to use. Must not be NULL.
1195 * @param key The key used to identify @a source.
1196 * @param source The source object that was destroyed.
1197 *
1198 * @retval SR_OK Success.
1199 * @retval SR_ERR_BUG Event source for @a key does not match @a source.
1200 */
1201SR_PRIV int sr_session_source_destroyed(struct sr_session *session,
1202 void *key, GSource *source)
1203{
1204 GSource *registered_source;
1205
1206 registered_source = g_hash_table_lookup(session->event_sources, key);
1207 /*
1208 * Trying to remove an already removed event source is problematic
1209 * since the poll_object handle may have been reused in the meantime.
1210 */
1211 if (!registered_source) {
1212 sr_err("No event source for key %p found.", key);
1213 return SR_ERR_BUG;
1214 }
1215 if (registered_source != source) {
1216 sr_err("Event source for key %p does not match"
1217 " destroyed source.", key);
1218 return SR_ERR_BUG;
1219 }
1220 g_hash_table_remove(session->event_sources, key);
1221 /*
1222 * Quit the main loop if we just removed the last event source.
1223 * TODO: This may need an idle callback depending on when event
1224 * sources are finalized. (The issue is remove followed by add
1225 * within the same main loop iteration.)
1226 */
1227 if (session->main_loop
1228 && g_hash_table_size(session->event_sources) == 0) {
1229 sr_dbg("Stopping main loop...");
1230 g_main_loop_quit(session->main_loop);
1231 }
1232 return SR_OK;
aac0ea25 1233}
7b870c38 1234
ee29d92e 1235static void copy_src(struct sr_config *src, struct sr_datafeed_meta *meta_copy)
8143cfdc 1236{
8143cfdc 1237 g_variant_ref(src->data);
ee29d92e
AJ
1238 meta_copy->config = g_slist_append(meta_copy->config,
1239 g_memdup(src, sizeof(struct sr_config)));
8143cfdc
BV
1240}
1241
1242SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet,
1243 struct sr_datafeed_packet **copy)
1244{
1245 const struct sr_datafeed_meta *meta;
1246 struct sr_datafeed_meta *meta_copy;
1247 const struct sr_datafeed_logic *logic;
1248 struct sr_datafeed_logic *logic_copy;
1249 const struct sr_datafeed_analog *analog;
1250 struct sr_datafeed_analog *analog_copy;
1251 uint8_t *payload;
1252
1253 *copy = g_malloc0(sizeof(struct sr_datafeed_packet));
1254 (*copy)->type = packet->type;
1255
1256 switch (packet->type) {
1257 case SR_DF_TRIGGER:
1258 case SR_DF_END:
1259 /* No payload. */
1260 break;
1261 case SR_DF_HEADER:
1262 payload = g_malloc(sizeof(struct sr_datafeed_header));
1263 memcpy(payload, packet->payload, sizeof(struct sr_datafeed_header));
1264 (*copy)->payload = payload;
1265 break;
1266 case SR_DF_META:
1267 meta = packet->payload;
ee29d92e
AJ
1268 meta_copy = g_malloc0(sizeof(struct sr_datafeed_meta));
1269 g_slist_foreach(meta->config, (GFunc)copy_src, meta_copy->config);
8143cfdc
BV
1270 (*copy)->payload = meta_copy;
1271 break;
1272 case SR_DF_LOGIC:
1273 logic = packet->payload;
1274 logic_copy = g_malloc(sizeof(logic));
1275 logic_copy->length = logic->length;
1276 logic_copy->unitsize = logic->unitsize;
1277 memcpy(logic_copy->data, logic->data, logic->length * logic->unitsize);
1278 (*copy)->payload = logic_copy;
1279 break;
1280 case SR_DF_ANALOG:
1281 analog = packet->payload;
1282 analog_copy = g_malloc(sizeof(analog));
1283 analog_copy->channels = g_slist_copy(analog->channels);
1284 analog_copy->num_samples = analog->num_samples;
1285 analog_copy->mq = analog->mq;
1286 analog_copy->unit = analog->unit;
1287 analog_copy->mqflags = analog->mqflags;
39e01135 1288 analog_copy->data = g_malloc(analog->num_samples * sizeof(float));
8143cfdc
BV
1289 memcpy(analog_copy->data, analog->data,
1290 analog->num_samples * sizeof(float));
1291 (*copy)->payload = analog_copy;
1292 break;
1293 default:
1294 sr_err("Unknown packet type %d", packet->type);
1295 return SR_ERR;
1296 }
1297
1298 return SR_OK;
1299}
1300
1301void sr_packet_free(struct sr_datafeed_packet *packet)
1302{
1303 const struct sr_datafeed_meta *meta;
1304 const struct sr_datafeed_logic *logic;
1305 const struct sr_datafeed_analog *analog;
1306 struct sr_config *src;
1307 GSList *l;
1308
1309 switch (packet->type) {
1310 case SR_DF_TRIGGER:
1311 case SR_DF_END:
1312 /* No payload. */
1313 break;
1314 case SR_DF_HEADER:
1315 /* Payload is a simple struct. */
1316 g_free((void *)packet->payload);
1317 break;
1318 case SR_DF_META:
1319 meta = packet->payload;
1320 for (l = meta->config; l; l = l->next) {
1321 src = l->data;
1322 g_variant_unref(src->data);
1323 g_free(src);
1324 }
1325 g_slist_free(meta->config);
1326 g_free((void *)packet->payload);
1327 break;
1328 case SR_DF_LOGIC:
1329 logic = packet->payload;
1330 g_free(logic->data);
1331 g_free((void *)packet->payload);
1332 break;
1333 case SR_DF_ANALOG:
1334 analog = packet->payload;
1335 g_slist_free(analog->channels);
1336 g_free(analog->data);
1337 g_free((void *)packet->payload);
1338 break;
1339 default:
1340 sr_err("Unknown packet type %d", packet->type);
1341 }
1342 g_free(packet);
1343
1344}
1345
7b870c38 1346/** @} */