]> sigrok.org Git - libsigrok.git/blame - src/session.c
session: More poll spewing
[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>
a1bb33af
UH
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
32af282c 20#include <errno.h>
a1bb33af
UH
21#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <string.h>
544a4582 25#include <glib.h>
c1aae900 26#include <libsigrok/libsigrok.h>
45c59c8b 27#include "libsigrok-internal.h"
aa4b1107 28
2ad1deb8 29/** @cond PRIVATE */
3544f848 30#define LOG_PREFIX "session"
2ad1deb8 31/** @endcond */
a421dc1d 32
393fb9cb
UH
33/**
34 * @file
35 *
36 * Creating, using, or destroying libsigrok sessions.
37 */
38
7b870c38
UH
39/**
40 * @defgroup grp_session Session handling
41 *
42 * Creating, using, or destroying libsigrok sessions.
43 *
44 * @{
45 */
46
544a4582 47struct source {
faa5d7d9
DE
48 int64_t timeout; /* microseconds */
49 int64_t due; /* microseconds */
144f6660 50 sr_receive_data_callback cb;
1f9813eb 51 void *cb_data;
aac0ea25
LPC
52
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.
55 */
56 gintptr poll_object;
62d7945f 57
62d7945f 58 gboolean is_usb;
faa5d7d9 59 gboolean triggered;
544a4582
BV
60};
61
2726474a 62struct datafeed_callback {
144f6660 63 sr_datafeed_callback cb;
2726474a
ML
64 void *cb_data;
65};
66
9f45fb3a
UH
67/**
68 * Create a new session.
7efe889e 69 *
61e6e2da 70 * @param ctx The context in which to create the new session.
7efe889e
UH
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.
9f45fb3a 74 *
0812c40e 75 * @retval SR_OK Success.
41de54ff 76 * @retval SR_ERR_ARG Invalid argument.
47117241 77 *
0812c40e 78 * @since 0.4.0
9f45fb3a 79 */
61e6e2da
ML
80SR_API int sr_session_new(struct sr_context *ctx,
81 struct sr_session **new_session)
a1bb33af 82{
3337e9a1 83 struct sr_session *session;
a1bb33af 84
41de54ff
UH
85 if (!new_session)
86 return SR_ERR_ARG;
87
3337e9a1 88 session = g_malloc0(sizeof(struct sr_session));
b7e94111 89
4ed5d21d 90 session->ctx = ctx;
faa5d7d9
DE
91
92 session->sources = g_array_new(FALSE, FALSE, sizeof(struct source));
93 session->pollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
94
3337e9a1 95 g_mutex_init(&session->stop_mutex);
0812c40e 96
3337e9a1 97 *new_session = session;
0812c40e
ML
98
99 return SR_OK;
a1bb33af
UH
100}
101
9f45fb3a 102/**
0812c40e 103 * Destroy a session.
9f45fb3a
UH
104 * This frees up all memory used by the session.
105 *
7efe889e
UH
106 * @param session The session to destroy. Must not be NULL.
107 *
04cb9157 108 * @retval SR_OK Success.
0812c40e 109 * @retval SR_ERR_ARG Invalid session passed.
47117241 110 *
0812c40e 111 * @since 0.4.0
9f45fb3a 112 */
0812c40e 113SR_API int sr_session_destroy(struct sr_session *session)
a1bb33af 114{
9f45fb3a 115 if (!session) {
a421dc1d 116 sr_err("%s: session was NULL", __func__);
0812c40e 117 return SR_ERR_ARG;
9f45fb3a
UH
118 }
119
0812c40e 120 sr_session_dev_remove_all(session);
33c6e4c5 121 g_mutex_clear(&session->stop_mutex);
3d68b612
BV
122 if (session->trigger)
123 sr_trigger_free(session->trigger);
33c6e4c5 124
1de3cced
ML
125 g_slist_free_full(session->owned_devs, (GDestroyNotify)sr_dev_inst_free);
126
faa5d7d9
DE
127 g_array_unref(session->pollfds);
128 g_array_unref(session->sources);
129
a1bb33af 130 g_free(session);
0812c40e 131
e0508e67 132 return SR_OK;
a1bb33af
UH
133}
134
9f45fb3a 135/**
0812c40e 136 * Remove all the devices from a session.
9f45fb3a
UH
137 *
138 * The session itself (i.e., the struct sr_session) is not free'd and still
139 * exists after this function returns.
140 *
7efe889e
UH
141 * @param session The session to use. Must not be NULL.
142 *
04cb9157 143 * @retval SR_OK Success.
0812c40e 144 * @retval SR_ERR_BUG Invalid session passed.
47117241 145 *
0812c40e 146 * @since 0.4.0
9f45fb3a 147 */
0812c40e 148SR_API int sr_session_dev_remove_all(struct sr_session *session)
a1bb33af 149{
0812c40e
ML
150 struct sr_dev_inst *sdi;
151 GSList *l;
152
9f45fb3a 153 if (!session) {
a421dc1d 154 sr_err("%s: session was NULL", __func__);
0812c40e
ML
155 return SR_ERR_ARG;
156 }
157
158 for (l = session->devs; l; l = l->next) {
159 sdi = (struct sr_dev_inst *) l->data;
160 sdi->session = NULL;
9f45fb3a
UH
161 }
162
681803df 163 g_slist_free(session->devs);
bb7ef793 164 session->devs = NULL;
e0508e67
UH
165
166 return SR_OK;
a1bb33af
UH
167}
168
9f45fb3a 169/**
0812c40e 170 * Add a device instance to a session.
9f45fb3a 171 *
7efe889e 172 * @param session The session to add to. Must not be NULL.
0812c40e 173 * @param sdi The device instance to add to a session. Must not
de4d3f99
BV
174 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
175 * not be NULL.
9f45fb3a 176 *
04cb9157
MH
177 * @retval SR_OK Success.
178 * @retval SR_ERR_ARG Invalid argument.
47117241 179 *
0812c40e 180 * @since 0.4.0
9f45fb3a 181 */
0812c40e
ML
182SR_API int sr_session_dev_add(struct sr_session *session,
183 struct sr_dev_inst *sdi)
a1bb33af 184{
5451816f 185 int ret;
a1bb33af 186
de4d3f99 187 if (!sdi) {
a421dc1d 188 sr_err("%s: sdi was NULL", __func__);
9f45fb3a
UH
189 return SR_ERR_ARG;
190 }
191
d6eb0c33 192 if (!session) {
a421dc1d 193 sr_err("%s: session was NULL", __func__);
0812c40e
ML
194 return SR_ERR_ARG;
195 }
196
197 /* If sdi->session is not NULL, the device is already in this or
198 * another session. */
199 if (sdi->session) {
200 sr_err("%s: already assigned to session", __func__);
201 return SR_ERR_ARG;
d6eb0c33
UH
202 }
203
de4d3f99
BV
204 /* If sdi->driver is NULL, this is a virtual device. */
205 if (!sdi->driver) {
d6eb0c33 206 /* Just add the device, don't run dev_open(). */
de4d3f99 207 session->devs = g_slist_append(session->devs, (gpointer)sdi);
0812c40e 208 sdi->session = session;
d6eb0c33 209 return SR_OK;
9f45fb3a
UH
210 }
211
de4d3f99
BV
212 /* sdi->driver is non-NULL (i.e. we have a real device). */
213 if (!sdi->driver->dev_open) {
a421dc1d 214 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
8ec95d22 215 return SR_ERR_BUG;
9f45fb3a
UH
216 }
217
de4d3f99 218 session->devs = g_slist_append(session->devs, (gpointer)sdi);
0812c40e 219 sdi->session = session;
aa4b1107 220
5451816f 221 if (session->running) {
32b7cd4f
DE
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)",
227 sr_strerror(ret));
228 return ret;
229 }
5451816f 230 if ((ret = sdi->driver->dev_acquisition_start(sdi,
32b7cd4f 231 (void *)sdi)) != SR_OK) {
5451816f 232 sr_err("Failed to start acquisition of device in "
32b7cd4f
DE
233 "running session (%s)", sr_strerror(ret));
234 return ret;
235 }
5451816f
BV
236 }
237
e46b8fb1 238 return SR_OK;
a1bb33af
UH
239}
240
2bb311b4 241/**
0812c40e 242 * List all device instances attached to a session.
2bb311b4 243 *
7efe889e 244 * @param session The session to use. Must not be NULL.
2bb311b4
BV
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.
251 *
04cb9157 252 * @retval SR_OK Success.
0812c40e 253 * @retval SR_ERR_ARG Invalid argument.
47117241 254 *
0812c40e 255 * @since 0.4.0
2bb311b4 256 */
0812c40e 257SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist)
2bb311b4 258{
2bb311b4 259 if (!session)
0812c40e
ML
260 return SR_ERR_ARG;
261
262 if (!devlist)
263 return SR_ERR_ARG;
2bb311b4
BV
264
265 *devlist = g_slist_copy(session->devs);
266
267 return SR_OK;
268}
269
9f45fb3a 270/**
0812c40e 271 * Remove all datafeed callbacks in a session.
9f45fb3a 272 *
7efe889e
UH
273 * @param session The session to use. Must not be NULL.
274 *
04cb9157 275 * @retval SR_OK Success.
0812c40e 276 * @retval SR_ERR_ARG Invalid session passed.
47117241 277 *
0812c40e 278 * @since 0.4.0
9f45fb3a 279 */
0812c40e 280SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session)
a1bb33af 281{
9f45fb3a 282 if (!session) {
a421dc1d 283 sr_err("%s: session was NULL", __func__);
0812c40e 284 return SR_ERR_ARG;
9f45fb3a
UH
285 }
286
2726474a 287 g_slist_free_full(session->datafeed_callbacks, g_free);
a1bb33af 288 session->datafeed_callbacks = NULL;
e0508e67
UH
289
290 return SR_OK;
a1bb33af
UH
291}
292
9f45fb3a 293/**
0812c40e 294 * Add a datafeed callback to a session.
9f45fb3a 295 *
7efe889e 296 * @param session The session to use. Must not be NULL.
d08490aa 297 * @param cb Function to call when a chunk of data is received.
0abee507 298 * Must not be NULL.
85222791 299 * @param cb_data Opaque pointer passed in by the caller.
a1645fcd 300 *
04cb9157
MH
301 * @retval SR_OK Success.
302 * @retval SR_ERR_BUG No session exists.
47117241
UH
303 *
304 * @since 0.3.0
9f45fb3a 305 */
0812c40e
ML
306SR_API int sr_session_datafeed_callback_add(struct sr_session *session,
307 sr_datafeed_callback cb, void *cb_data)
a1bb33af 308{
2726474a
ML
309 struct datafeed_callback *cb_struct;
310
9f45fb3a 311 if (!session) {
a421dc1d 312 sr_err("%s: session was NULL", __func__);
e0508e67 313 return SR_ERR_BUG;
9f45fb3a
UH
314 }
315
0abee507 316 if (!cb) {
a421dc1d 317 sr_err("%s: cb was NULL", __func__);
0abee507
UH
318 return SR_ERR_ARG;
319 }
9f45fb3a 320
91219afc 321 cb_struct = g_malloc0(sizeof(struct datafeed_callback));
2726474a
ML
322 cb_struct->cb = cb;
323 cb_struct->cb_data = cb_data;
324
62c82025 325 session->datafeed_callbacks =
2726474a 326 g_slist_append(session->datafeed_callbacks, cb_struct);
e0508e67
UH
327
328 return SR_OK;
a1bb33af
UH
329}
330
9f42e2e6
UH
331/**
332 * Get the trigger assigned to this session.
333 *
334 * @param session The session to use.
335 *
336 * @retval NULL Invalid (NULL) session was passed to the function.
337 * @retval other The trigger assigned to this session (can be NULL).
338 *
339 * @since 0.4.0
340 */
0812c40e 341SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session)
7b5e6d29 342{
9f42e2e6
UH
343 if (!session)
344 return NULL;
345
7b5e6d29
BV
346 return session->trigger;
347}
348
9f42e2e6
UH
349/**
350 * Set the trigger of this session.
351 *
352 * @param session The session to use. Must not be NULL.
353 * @param trig The trigger to assign to this session. Can be NULL.
354 *
355 * @retval SR_OK Success.
356 * @retval SR_ERR_ARG Invalid argument.
357 *
358 * @since 0.4.0
359 */
0812c40e 360SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig)
7b5e6d29 361{
9f42e2e6
UH
362 if (!session)
363 return SR_ERR_ARG;
364
7b5e6d29
BV
365 session->trigger = trig;
366
367 return SR_OK;
368}
369
32af282c
DE
370static gboolean sr_session_check_aborted(struct sr_session *session)
371{
372 gboolean stop;
373
374 g_mutex_lock(&session->stop_mutex);
375 stop = session->abort_session;
376 if (stop) {
377 sr_session_stop_sync(session);
378 /* But once is enough. */
379 session->abort_session = FALSE;
380 }
381 g_mutex_unlock(&session->stop_mutex);
382
383 return stop;
384}
385
faa5d7d9
DE
386static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object);
387
b483be74 388/**
faa5d7d9 389 * Poll the session's event sources.
b483be74 390 *
7efe889e 391 * @param session The session to use. Must not be NULL.
04cb9157 392 * @retval SR_OK Success.
f3f19d11 393 * @retval SR_ERR Error occurred.
b483be74 394 */
62d7945f 395static int sr_session_iteration(struct sr_session *session)
544a4582 396{
faa5d7d9
DE
397 int64_t start_time, stop_time, min_due, due;
398 int timeout_ms;
b7e94111 399 unsigned int i;
faa5d7d9
DE
400 int ret;
401 int fd;
4399cc0f 402 int revents;
faa5d7d9 403 gboolean triggered, stopped;
4399cc0f
DE
404 struct source *source;
405 GPollFD *pollfd;
62d7945f 406 gintptr poll_object;
bb5f6110 407#ifdef HAVE_LIBUSB_1_0
7637fa35 408 int64_t usb_timeout;
62d7945f 409 int64_t usb_due;
bb5f6110
ML
410 struct timeval tv;
411#endif
faa5d7d9 412 if (session->sources->len == 0) {
62d7945f
DE
413 sr_session_check_aborted(session);
414 return SR_OK;
415 }
416 start_time = g_get_monotonic_time();
417 min_due = INT64_MAX;
bb5f6110 418
faa5d7d9
DE
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;
62d7945f 424 }
bb5f6110 425#ifdef HAVE_LIBUSB_1_0
62d7945f 426 usb_due = INT64_MAX;
1190c653 427 if (session->ctx->usb_source_present) {
1190c653
UH
428 ret = libusb_get_next_timeout(session->ctx->libusb_ctx, &tv);
429 if (ret < 0) {
430 sr_err("Error getting libusb timeout: %s",
431 libusb_error_name(ret));
432 return SR_ERR;
433 } else if (ret == 1) {
7637fa35
DE
434 usb_timeout = (int64_t)tv.tv_sec * G_USEC_PER_SEC
435 + tv.tv_usec;
436 usb_due = start_time + usb_timeout;
62d7945f
DE
437 if (usb_due < min_due)
438 min_due = usb_due;
7637fa35
DE
439
440 sr_spew("poll: next USB timeout %g ms",
441 1e-3 * usb_timeout);
1190c653 442 }
bb5f6110
ML
443 }
444#endif
faa5d7d9
DE
445 if (min_due == INT64_MAX)
446 timeout_ms = -1;
447 else if (min_due > start_time)
448 timeout_ms = MIN((min_due - start_time + 999) / 1000, INT_MAX);
62d7945f 449 else
faa5d7d9 450 timeout_ms = 0;
544a4582 451
7637fa35
DE
452 sr_spew("poll enter: %u sources, %u fds, %d ms timeout",
453 session->sources->len, session->pollfds->len, timeout_ms);
454
faa5d7d9
DE
455 ret = g_poll((GPollFD *)session->pollfds->data,
456 session->pollfds->len, timeout_ms);
4399cc0f 457#ifdef G_OS_UNIX
32af282c
DE
458 if (ret < 0 && errno != EINTR) {
459 sr_err("Error in poll: %s", g_strerror(errno));
460 return SR_ERR;
461 }
4399cc0f
DE
462#else
463 if (ret < 0) {
464 sr_err("Error in poll: %d", ret);
465 return SR_ERR;
466 }
467#endif
62d7945f 468 stop_time = g_get_monotonic_time();
b5887bd0 469
7637fa35
DE
470 sr_spew("poll leave: %g ms elapsed, %d events",
471 1e-3 * (stop_time - start_time), ret);
b5887bd0 472
faa5d7d9 473 triggered = FALSE;
32af282c
DE
474 stopped = FALSE;
475
faa5d7d9
DE
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 */
480
481 poll_object = source->poll_object;
482 fd = (int)poll_object;
483 revents = 0;
484
485 if (i < session->pollfds->len) {
486 pollfd = &g_array_index(session->pollfds, GPollFD, i);
487 fd = pollfd->fd;
488 if (ret > 0)
489 revents = pollfd->revents;
490 }
491 if (ret > 0 && revents == 0)
492 continue; /* skip timeouts if any I/O event occurred */
493
62d7945f
DE
494 due = source->due;
495#ifdef HAVE_LIBUSB_1_0
496 if (source->is_usb && usb_due < due)
497 due = usb_due;
498#endif
499 if (revents == 0 && stop_time < due)
500 continue;
501 /*
502 * The source may be gone after the callback returns,
503 * so access any data now that needs accessing.
504 */
faa5d7d9
DE
505 if (source->timeout >= 0)
506 source->due = stop_time + source->timeout;
507 source->triggered = TRUE;
508 triggered = TRUE;
62d7945f
DE
509 /*
510 * Invoke the source's callback on an event or timeout.
511 */
faa5d7d9
DE
512 if (!source->cb(fd, revents, source->cb_data))
513 _sr_session_source_remove(session, poll_object);
62d7945f
DE
514 /*
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.
519 */
faa5d7d9 520 if (!stopped)
62d7945f 521 stopped = sr_session_check_aborted(session);
faa5d7d9 522
62d7945f
DE
523 /* Restart loop as the sources list may have changed. */
524 i = 0;
544a4582 525 }
faa5d7d9
DE
526
527 /* Check for abort at least once per iteration. */
528 if (!triggered)
32af282c 529 sr_session_check_aborted(session);
e0508e67
UH
530
531 return SR_OK;
544a4582
BV
532}
533
7b5e6d29
BV
534static int verify_trigger(struct sr_trigger *trigger)
535{
536 struct sr_trigger_stage *stage;
537 struct sr_trigger_match *match;
538 GSList *l, *m;
539
540 if (!trigger->stages) {
541 sr_err("No trigger stages defined.");
542 return SR_ERR;
543 }
544
545 sr_spew("Checking trigger:");
546 for (l = trigger->stages; l; l = l->next) {
547 stage = l->data;
548 if (!stage->matches) {
549 sr_err("Stage %d has no matches defined.", stage->stage);
550 return SR_ERR;
551 }
552 for (m = stage->matches; m; m = m->next) {
553 match = m->data;
554 if (!match->channel) {
555 sr_err("Stage %d match has no channel.", stage->stage);
556 return SR_ERR;
557 }
558 if (!match->match) {
559 sr_err("Stage %d match is not defined.", stage->stage);
560 return SR_ERR;
561 }
562 sr_spew("Stage %d match on channel %s, match %d", stage->stage,
563 match->channel->name, match->match);
564 }
565 }
566
567 return SR_OK;
568}
1beccaed 569
9f45fb3a
UH
570/**
571 * Start a session.
572 *
7efe889e
UH
573 * @param session The session to use. Must not be NULL.
574 *
04cb9157 575 * @retval SR_OK Success.
0812c40e 576 * @retval SR_ERR_ARG Invalid session passed.
47117241 577 *
0812c40e 578 * @since 0.4.0
9f45fb3a 579 */
0812c40e 580SR_API int sr_session_start(struct sr_session *session)
7d658874 581{
de4d3f99 582 struct sr_dev_inst *sdi;
013ec84b
BV
583 struct sr_channel *ch;
584 GSList *l, *c;
585 int enabled_channels, ret;
7d658874 586
9f45fb3a 587 if (!session) {
0812c40e
ML
588 sr_err("%s: session was NULL", __func__);
589 return SR_ERR_ARG;
9f45fb3a
UH
590 }
591
bb7ef793 592 if (!session->devs) {
a421dc1d 593 sr_err("%s: session->devs was NULL; a session "
9f45fb3a 594 "cannot be started without devices.", __func__);
0812c40e 595 return SR_ERR_ARG;
9f45fb3a
UH
596 }
597
7b5e6d29
BV
598 if (session->trigger && verify_trigger(session->trigger) != SR_OK)
599 return SR_ERR;
600
c7142604 601 sr_info("Starting.");
9f45fb3a 602
b7c3e849 603 ret = SR_OK;
bb7ef793 604 for (l = session->devs; l; l = l->next) {
de4d3f99 605 sdi = l->data;
013ec84b
BV
606 enabled_channels = 0;
607 for (c = sdi->channels; c; c = c->next) {
608 ch = c->data;
609 if (ch->enabled) {
610 enabled_channels++;
611 break;
612 }
613 }
614 if (enabled_channels == 0) {
615 ret = SR_ERR;
1b9e567b
SA
616 sr_err("%s using connection %s has no enabled channels!",
617 sdi->driver->name, sdi->connection_id);
013ec84b
BV
618 break;
619 }
620
32b7cd4f
DE
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));
624 break;
625 }
de4d3f99 626 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
a421dc1d 627 sr_err("%s: could not start an acquisition "
568dcacc 628 "(%s)", __func__, sr_strerror(ret));
7d658874 629 break;
9f45fb3a 630 }
7d658874
BV
631 }
632
9f45fb3a
UH
633 /* TODO: What if there are multiple devices? Which return code? */
634
7d658874
BV
635 return ret;
636}
637
9f45fb3a 638/**
0812c40e 639 * Run a session.
9f45fb3a 640 *
7efe889e
UH
641 * @param session The session to use. Must not be NULL.
642 *
04cb9157 643 * @retval SR_OK Success.
0812c40e 644 * @retval SR_ERR_ARG Invalid session passed.
faa5d7d9 645 * @retval SR_ERR Error during event processing.
47117241 646 *
0812c40e 647 * @since 0.4.0
9f45fb3a 648 */
0812c40e 649SR_API int sr_session_run(struct sr_session *session)
7d658874 650{
faa5d7d9
DE
651 int ret;
652
9f45fb3a 653 if (!session) {
0812c40e
ML
654 sr_err("%s: session was NULL", __func__);
655 return SR_ERR_ARG;
9f45fb3a
UH
656 }
657
bb7ef793 658 if (!session->devs) {
9f45fb3a 659 /* TODO: Actually the case? */
a421dc1d 660 sr_err("%s: session->devs was NULL; a session "
9f45fb3a 661 "cannot be run without devices.", __func__);
0812c40e 662 return SR_ERR_ARG;
9f45fb3a 663 }
5451816f 664 session->running = TRUE;
9f45fb3a 665
a421dc1d 666 sr_info("Running.");
7d658874 667
faa5d7d9
DE
668 /* Poll event sources until none are left. */
669 while (session->sources->len > 0) {
670 ret = sr_session_iteration(session);
671 if (ret != SR_OK)
672 return ret;
9f45fb3a 673 }
e0508e67 674 return SR_OK;
7d658874
BV
675}
676
9f45fb3a 677/**
0812c40e 678 * Stop a session.
9f45fb3a 679 *
0812c40e
ML
680 * The session is stopped immediately, with all acquisition sessions stopped
681 * and hardware drivers cleaned up.
9f45fb3a 682 *
33c6e4c5
AG
683 * This must be called from within the session thread, to prevent freeing
684 * resources that the session thread will try to use.
685 *
7efe889e
UH
686 * @param session The session to use. Must not be NULL.
687 *
04cb9157 688 * @retval SR_OK Success.
0812c40e 689 * @retval SR_ERR_ARG Invalid session passed.
72a08bcc
BV
690 *
691 * @private
9f45fb3a 692 */
0812c40e 693SR_PRIV int sr_session_stop_sync(struct sr_session *session)
a1bb33af 694{
de4d3f99 695 struct sr_dev_inst *sdi;
a1bb33af
UH
696 GSList *l;
697
9f45fb3a 698 if (!session) {
a421dc1d 699 sr_err("%s: session was NULL", __func__);
0812c40e 700 return SR_ERR_ARG;
9f45fb3a
UH
701 }
702
a421dc1d 703 sr_info("Stopping.");
e0508e67 704
bb7ef793 705 for (l = session->devs; l; l = l->next) {
de4d3f99
BV
706 sdi = l->data;
707 if (sdi->driver) {
708 if (sdi->driver->dev_acquisition_stop)
709 sdi->driver->dev_acquisition_stop(sdi, sdi);
8c76be53 710 }
a1bb33af 711 }
5451816f 712 session->running = FALSE;
9f45fb3a 713
e0508e67 714 return SR_OK;
a1bb33af
UH
715}
716
33c6e4c5 717/**
0812c40e 718 * Stop a session.
33c6e4c5 719 *
0812c40e
ML
720 * The session is stopped immediately, with all acquisition sessions being
721 * stopped and hardware drivers cleaned up.
33c6e4c5
AG
722 *
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.
727 *
7efe889e
UH
728 * @param session The session to use. Must not be NULL.
729 *
04cb9157 730 * @retval SR_OK Success.
0812c40e 731 * @retval SR_ERR_ARG Invalid session passed.
47117241 732 *
0812c40e 733 * @since 0.4.0
33c6e4c5 734 */
0812c40e 735SR_API int sr_session_stop(struct sr_session *session)
33c6e4c5
AG
736{
737 if (!session) {
738 sr_err("%s: session was NULL", __func__);
739 return SR_ERR_BUG;
740 }
741
742 g_mutex_lock(&session->stop_mutex);
743 session->abort_session = TRUE;
744 g_mutex_unlock(&session->stop_mutex);
745
746 return SR_OK;
747}
748
9f45fb3a 749/**
a1645fcd 750 * Debug helper.
9f45fb3a 751 *
996b0c72 752 * @param packet The packet to show debugging information for.
9f45fb3a 753 */
bf53457d 754static void datafeed_dump(const struct sr_datafeed_packet *packet)
7d2afd6c 755{
bf53457d
JH
756 const struct sr_datafeed_logic *logic;
757 const struct sr_datafeed_analog *analog;
1954dfa9 758 const struct sr_datafeed_analog2 *analog2;
7d2afd6c 759
ca7dbb56 760 /* Please use the same order as in libsigrok.h. */
7d2afd6c
BV
761 switch (packet->type) {
762 case SR_DF_HEADER:
a421dc1d 763 sr_dbg("bus: Received SR_DF_HEADER packet.");
7d2afd6c 764 break;
55c9f09d
UH
765 case SR_DF_END:
766 sr_dbg("bus: Received SR_DF_END packet.");
7d2afd6c 767 break;
c71bac3b 768 case SR_DF_META:
a421dc1d 769 sr_dbg("bus: Received SR_DF_META packet.");
ee7489d2 770 break;
55c9f09d
UH
771 case SR_DF_TRIGGER:
772 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
773 break;
7d2afd6c
BV
774 case SR_DF_LOGIC:
775 logic = packet->payload;
7ea45862
UH
776 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, "
777 "unitsize = %d).", logic->length, logic->unitsize);
7d2afd6c 778 break;
ee7489d2
BV
779 case SR_DF_ANALOG:
780 analog = packet->payload;
a421dc1d
UH
781 sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).",
782 analog->num_samples);
ee7489d2 783 break;
6ea7669c 784 case SR_DF_FRAME_BEGIN:
a421dc1d 785 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
6ea7669c
BV
786 break;
787 case SR_DF_FRAME_END:
a421dc1d 788 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
6ea7669c 789 break;
55c9f09d
UH
790 case SR_DF_ANALOG2:
791 analog2 = packet->payload;
792 sr_dbg("bus: Received SR_DF_ANALOG2 packet (%d samples).",
793 analog2->num_samples);
794 break;
7d2afd6c 795 default:
a421dc1d 796 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
9f45fb3a 797 break;
7d2afd6c 798 }
7d2afd6c
BV
799}
800
9f45fb3a 801/**
a1645fcd
BV
802 * Send a packet to whatever is listening on the datafeed bus.
803 *
804 * Hardware drivers use this to send a data packet to the frontend.
9f45fb3a 805 *
6b2d8d3e 806 * @param sdi TODO.
31ccebc4 807 * @param packet The datafeed packet to send to the session bus.
44dae539 808 *
04cb9157
MH
809 * @retval SR_OK Success.
810 * @retval SR_ERR_ARG Invalid argument.
b4bd7088
UH
811 *
812 * @private
9f45fb3a 813 */
de4d3f99 814SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
8143cfdc 815 const struct sr_datafeed_packet *packet)
a1bb33af
UH
816{
817 GSList *l;
2726474a 818 struct datafeed_callback *cb_struct;
c0a1e532
UH
819 struct sr_datafeed_packet *packet_in, *packet_out;
820 struct sr_transform *t;
821 int ret;
a1bb33af 822
de4d3f99 823 if (!sdi) {
a421dc1d 824 sr_err("%s: sdi was NULL", __func__);
e0508e67 825 return SR_ERR_ARG;
9f45fb3a
UH
826 }
827
e0508e67 828 if (!packet) {
a421dc1d 829 sr_err("%s: packet was NULL", __func__);
e0508e67 830 return SR_ERR_ARG;
9f45fb3a
UH
831 }
832
79f92686
BV
833 if (!sdi->session) {
834 sr_err("%s: session was NULL", __func__);
835 return SR_ERR_BUG;
836 }
837
c0a1e532
UH
838 /*
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.
842 */
843 packet_in = (struct sr_datafeed_packet *)packet;
844 for (l = sdi->session->transforms; l; l = l->next) {
845 t = l->data;
846 sr_spew("Running transform module '%s'.", t->module->id);
847 ret = t->module->receive(t, packet_in, &packet_out);
848 if (ret < 0) {
849 sr_err("Error while running transform module: %d.", ret);
850 return SR_ERR;
851 }
852 if (!packet_out) {
853 /*
854 * If any of the transforms don't return an output
855 * packet, abort.
856 */
857 sr_spew("Transform module didn't return a packet, aborting.");
858 return SR_OK;
859 } else {
860 /*
861 * Use this transform module's output packet as input
862 * for the next transform module.
863 */
864 packet_in = packet_out;
865 }
866 }
4ec436c4 867 packet = packet_in;
c0a1e532
UH
868
869 /*
870 * If the last transform did output a packet, pass it to all datafeed
871 * callbacks.
872 */
3337e9a1 873 for (l = sdi->session->datafeed_callbacks; l; l = l->next) {
18beaeff
BV
874 if (sr_log_loglevel_get() >= SR_LOG_DBG)
875 datafeed_dump(packet);
2726474a
ML
876 cb_struct = l->data;
877 cb_struct->cb(sdi, packet, cb_struct->cb_data);
a1bb33af 878 }
9f45fb3a 879
e0508e67 880 return SR_OK;
a1bb33af
UH
881}
882
6b2d8d3e
UH
883/**
884 * Add an event source for a file descriptor.
885 *
7efe889e 886 * @param session The session to use. Must not be NULL.
6b2d8d3e 887 * @param pollfd The GPollFD.
faa5d7d9
DE
888 * @param[in] timeout Max time in ms to wait before the callback is called,
889 * or -1 to wait indefinitely.
6b2d8d3e
UH
890 * @param cb Callback function to add. Must not be NULL.
891 * @param cb_data Data for the callback function. Can be NULL.
62d7945f
DE
892 * @param poll_object Handle by which the source is identified
893 * @param is_usb TRUE for a libusb polling source
6b2d8d3e 894 *
04cb9157
MH
895 * @retval SR_OK Success.
896 * @retval SR_ERR_ARG Invalid argument.
6b2d8d3e 897 */
62d7945f
DE
898SR_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)
544a4582 901{
faa5d7d9 902 struct source src;
544a4582 903
d08490aa 904 if (!cb) {
a421dc1d 905 sr_err("%s: cb was NULL", __func__);
e0508e67 906 return SR_ERR_ARG;
9f45fb3a 907 }
1f9813eb 908 /* Note: cb_data can be NULL, that's not a bug. */
9f45fb3a 909
faa5d7d9
DE
910 src.cb = cb;
911 src.cb_data = cb_data;
912 src.poll_object = poll_object;
913 src.is_usb = is_usb;
914 src.triggered = FALSE;
544a4582 915
faa5d7d9
DE
916 if (timeout >= 0) {
917 src.timeout = INT64_C(1000) * timeout;
918 src.due = g_get_monotonic_time() + src.timeout;
919 } else {
920 src.timeout = -1;
921 src.due = INT64_MAX;
922 }
923
924 if (pollfd) {
925 /* I/O source */
926 g_array_insert_val(session->sources, session->pollfds->len, src);
927 g_array_append_vals(session->pollfds, pollfd, 1);
928 } else {
929 /* Timer source */
930 g_array_append_val(session->sources, src);
931 }
544a4582 932
e0508e67 933 return SR_OK;
544a4582
BV
934}
935
9f45fb3a 936/**
6b2d8d3e 937 * Add an event source for a file descriptor.
9f45fb3a 938 *
7efe889e 939 * @param session The session to use. Must not be NULL.
aac0ea25
LPC
940 * @param fd The file descriptor.
941 * @param events Events to check for.
faa5d7d9
DE
942 * @param timeout Max time in ms to wait before the callback is called,
943 * or -1 to wait indefinitely.
aac0ea25
LPC
944 * @param cb Callback function to add. Must not be NULL.
945 * @param cb_data Data for the callback function. Can be NULL.
9f45fb3a 946 *
04cb9157
MH
947 * @retval SR_OK Success.
948 * @retval SR_ERR_ARG Invalid argument.
47117241
UH
949 *
950 * @since 0.3.0
aac0ea25 951 */
0812c40e
ML
952SR_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)
aac0ea25
LPC
954{
955 GPollFD p;
956
faa5d7d9
DE
957 if (fd < 0 && timeout < 0) {
958 sr_err("Timer source without timeout would block indefinitely");
959 return SR_ERR_ARG;
960 }
aac0ea25
LPC
961 p.fd = fd;
962 p.events = events;
89efe064 963 p.revents = 0;
aac0ea25 964
faa5d7d9
DE
965 return sr_session_source_add_internal(session,
966 (fd < 0) ? NULL : &p, timeout, cb, cb_data, fd, FALSE);
aac0ea25
LPC
967}
968
969/**
1a895c61 970 * Add an event source for a GPollFD.
aac0ea25 971 *
7efe889e 972 * @param session The session to use. Must not be NULL.
faa5d7d9
DE
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.
aac0ea25
LPC
976 * @param cb Callback function to add. Must not be NULL.
977 * @param cb_data Data for the callback function. Can be NULL.
44dae539 978 *
04cb9157
MH
979 * @retval SR_OK Success.
980 * @retval SR_ERR_ARG Invalid argument.
47117241
UH
981 *
982 * @since 0.3.0
9f45fb3a 983 */
0812c40e
ML
984SR_API int sr_session_source_add_pollfd(struct sr_session *session,
985 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
986 void *cb_data)
aac0ea25 987{
faa5d7d9
DE
988 if (!pollfd) {
989 sr_err("%s: pollfd was NULL", __func__);
990 return SR_ERR_ARG;
991 }
62d7945f
DE
992 return sr_session_source_add_internal(session, pollfd, timeout,
993 cb, cb_data, (gintptr)pollfd, FALSE);
aac0ea25
LPC
994}
995
996/**
1a895c61 997 * Add an event source for a GIOChannel.
aac0ea25 998 *
7efe889e 999 * @param session The session to use. Must not be NULL.
aac0ea25
LPC
1000 * @param channel The GIOChannel.
1001 * @param events Events to poll on.
faa5d7d9
DE
1002 * @param timeout Max time in ms to wait before the callback is called,
1003 * or -1 to wait indefinitely.
aac0ea25
LPC
1004 * @param cb Callback function to add. Must not be NULL.
1005 * @param cb_data Data for the callback function. Can be NULL.
1006 *
04cb9157
MH
1007 * @retval SR_OK Success.
1008 * @retval SR_ERR_ARG Invalid argument.
47117241
UH
1009 *
1010 * @since 0.3.0
aac0ea25 1011 */
0812c40e
ML
1012SR_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)
aac0ea25
LPC
1015{
1016 GPollFD p;
1017
faa5d7d9 1018#ifdef G_OS_WIN32
6b2d8d3e 1019 g_io_channel_win32_make_pollfd(channel, events, &p);
aac0ea25
LPC
1020#else
1021 p.fd = g_io_channel_unix_get_fd(channel);
1022 p.events = events;
89efe064 1023 p.revents = 0;
aac0ea25 1024#endif
62d7945f
DE
1025 return sr_session_source_add_internal(session, &p, timeout, cb,
1026 cb_data, (gintptr)channel, FALSE);
aac0ea25
LPC
1027}
1028
6b2d8d3e
UH
1029/**
1030 * Remove the source belonging to the specified channel.
1031 *
7efe889e 1032 * @param session The session to use. Must not be NULL.
04cb9157 1033 * @param poll_object The channel for which the source should be removed.
6b2d8d3e 1034 *
04cb9157
MH
1035 * @retval SR_OK Success
1036 * @retval SR_ERR_ARG Invalid arguments
04cb9157 1037 * @retval SR_ERR_BUG Internal error
6b2d8d3e 1038 */
102f1239 1039static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object)
544a4582 1040{
faa5d7d9 1041 unsigned int i;
544a4582 1042
faa5d7d9
DE
1043 for (i = 0; i < session->sources->len; ++i) {
1044 if (g_array_index(session->sources, struct source, i)
1045 .poll_object == poll_object) {
e0508e67 1046
faa5d7d9
DE
1047 g_array_remove_index(session->sources, i);
1048 if (i < session->pollfds->len)
1049 g_array_remove_index(session->pollfds, i);
2bccd322 1050 break;
faa5d7d9 1051 }
9f45fb3a 1052 }
e0508e67 1053 return SR_OK;
544a4582 1054}
aac0ea25 1055
6b2d8d3e 1056/**
aac0ea25
LPC
1057 * Remove the source belonging to the specified file descriptor.
1058 *
7efe889e 1059 * @param session The session to use. Must not be NULL.
1a895c61 1060 * @param fd The file descriptor for which the source should be removed.
aac0ea25 1061 *
04cb9157
MH
1062 * @retval SR_OK Success
1063 * @retval SR_ERR_ARG Invalid argument
04cb9157 1064 * @retval SR_ERR_BUG Internal error.
47117241
UH
1065 *
1066 * @since 0.3.0
aac0ea25 1067 */
0812c40e 1068SR_API int sr_session_source_remove(struct sr_session *session, int fd)
aac0ea25 1069{
faa5d7d9 1070 return _sr_session_source_remove(session, fd);
aac0ea25
LPC
1071}
1072
1073/**
1074 * Remove the source belonging to the specified poll descriptor.
1075 *
7efe889e 1076 * @param session The session to use. Must not be NULL.
aac0ea25 1077 * @param pollfd The poll descriptor for which the source should be removed.
faa5d7d9 1078 * Must not be NULL.
aac0ea25
LPC
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
1081 * internal errors.
47117241
UH
1082 *
1083 * @since 0.2.0
aac0ea25 1084 */
0812c40e
ML
1085SR_API int sr_session_source_remove_pollfd(struct sr_session *session,
1086 GPollFD *pollfd)
aac0ea25 1087{
faa5d7d9
DE
1088 if (!pollfd) {
1089 sr_err("%s: pollfd was NULL", __func__);
1090 return SR_ERR_ARG;
1091 }
102f1239 1092 return _sr_session_source_remove(session, (gintptr)pollfd);
aac0ea25
LPC
1093}
1094
6b2d8d3e 1095/**
aac0ea25
LPC
1096 * Remove the source belonging to the specified channel.
1097 *
7efe889e 1098 * @param session The session to use. Must not be NULL.
1a895c61 1099 * @param channel The channel for which the source should be removed.
faa5d7d9 1100 * Must not be NULL.
04cb9157
MH
1101 * @retval SR_OK Success.
1102 * @retval SR_ERR_ARG Invalid argument.
04cb9157 1103 * @return SR_ERR_BUG Internal error.
47117241
UH
1104 *
1105 * @since 0.2.0
aac0ea25 1106 */
0812c40e
ML
1107SR_API int sr_session_source_remove_channel(struct sr_session *session,
1108 GIOChannel *channel)
aac0ea25 1109{
faa5d7d9
DE
1110 if (!channel) {
1111 sr_err("%s: channel was NULL", __func__);
1112 return SR_ERR_ARG;
1113 }
102f1239 1114 return _sr_session_source_remove(session, (gintptr)channel);
aac0ea25 1115}
7b870c38 1116
ee29d92e 1117static void copy_src(struct sr_config *src, struct sr_datafeed_meta *meta_copy)
8143cfdc 1118{
8143cfdc 1119 g_variant_ref(src->data);
ee29d92e
AJ
1120 meta_copy->config = g_slist_append(meta_copy->config,
1121 g_memdup(src, sizeof(struct sr_config)));
8143cfdc
BV
1122}
1123
1124SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet,
1125 struct sr_datafeed_packet **copy)
1126{
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;
1133 uint8_t *payload;
1134
1135 *copy = g_malloc0(sizeof(struct sr_datafeed_packet));
1136 (*copy)->type = packet->type;
1137
1138 switch (packet->type) {
1139 case SR_DF_TRIGGER:
1140 case SR_DF_END:
1141 /* No payload. */
1142 break;
1143 case SR_DF_HEADER:
1144 payload = g_malloc(sizeof(struct sr_datafeed_header));
1145 memcpy(payload, packet->payload, sizeof(struct sr_datafeed_header));
1146 (*copy)->payload = payload;
1147 break;
1148 case SR_DF_META:
1149 meta = packet->payload;
ee29d92e
AJ
1150 meta_copy = g_malloc0(sizeof(struct sr_datafeed_meta));
1151 g_slist_foreach(meta->config, (GFunc)copy_src, meta_copy->config);
8143cfdc
BV
1152 (*copy)->payload = meta_copy;
1153 break;
1154 case SR_DF_LOGIC:
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;
1161 break;
1162 case SR_DF_ANALOG:
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;
1173 break;
1174 default:
1175 sr_err("Unknown packet type %d", packet->type);
1176 return SR_ERR;
1177 }
1178
1179 return SR_OK;
1180}
1181
1182void sr_packet_free(struct sr_datafeed_packet *packet)
1183{
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;
1188 GSList *l;
1189
1190 switch (packet->type) {
1191 case SR_DF_TRIGGER:
1192 case SR_DF_END:
1193 /* No payload. */
1194 break;
1195 case SR_DF_HEADER:
1196 /* Payload is a simple struct. */
1197 g_free((void *)packet->payload);
1198 break;
1199 case SR_DF_META:
1200 meta = packet->payload;
1201 for (l = meta->config; l; l = l->next) {
1202 src = l->data;
1203 g_variant_unref(src->data);
1204 g_free(src);
1205 }
1206 g_slist_free(meta->config);
1207 g_free((void *)packet->payload);
1208 break;
1209 case SR_DF_LOGIC:
1210 logic = packet->payload;
1211 g_free(logic->data);
1212 g_free((void *)packet->payload);
1213 break;
1214 case SR_DF_ANALOG:
1215 analog = packet->payload;
1216 g_slist_free(analog->channels);
1217 g_free(analog->data);
1218 g_free((void *)packet->payload);
1219 break;
1220 default:
1221 sr_err("Unknown packet type %d", packet->type);
1222 }
1223 g_free(packet);
1224
1225}
1226
7b870c38 1227/** @} */