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