]> sigrok.org Git - libsigrok.git/blame - src/session.c
SR_DF_ANALOG_OLD and sr_datafeed_analog_old renames.
[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 54/** Custom GLib event source for generic descriptor I/O.
5de0fc55 55 * @see https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html
c2bf5506
DE
56 * @internal
57 */
58struct fd_source {
59 GSource base;
62d7945f 60
c2bf5506
DE
61 int64_t timeout_us;
62 int64_t due_us;
92248e78 63
c2bf5506
DE
64 /* Meta-data needed to keep track of installed sources */
65 struct sr_session *session;
66 void *key;
544a4582 67
c2bf5506 68 GPollFD pollfd;
2726474a
ML
69};
70
c2bf5506 71/** FD event source prepare() method.
5de0fc55 72 * This is called immediately before poll().
c2bf5506
DE
73 */
74static gboolean fd_source_prepare(GSource *source, int *timeout)
75{
76 int64_t now_us;
77 struct fd_source *fsource;
78 int remaining_ms;
79
80 fsource = (struct fd_source *)source;
81
82 if (fsource->timeout_us >= 0) {
83 now_us = g_source_get_time(source);
84
85 if (fsource->due_us == 0) {
86 /* First-time initialization of the expiration time */
87 fsource->due_us = now_us + fsource->timeout_us;
88 }
89 remaining_ms = (MAX(0, fsource->due_us - now_us) + 999) / 1000;
90 } else {
91 remaining_ms = -1;
92 }
93 *timeout = remaining_ms;
94
95 return (remaining_ms == 0);
96}
97
98/** FD event source check() method.
5de0fc55 99 * This is called after poll() returns to check whether an event fired.
c2bf5506
DE
100 */
101static gboolean fd_source_check(GSource *source)
102{
103 struct fd_source *fsource;
104 unsigned int revents;
105
106 fsource = (struct fd_source *)source;
107 revents = fsource->pollfd.revents;
108
109 return (revents != 0 || (fsource->timeout_us >= 0
110 && fsource->due_us <= g_source_get_time(source)));
111}
112
113/** FD event source dispatch() method.
5de0fc55 114 * This is called if either prepare() or check() returned TRUE.
c2bf5506
DE
115 */
116static gboolean fd_source_dispatch(GSource *source,
117 GSourceFunc callback, void *user_data)
118{
119 struct fd_source *fsource;
120 const char *name;
121 unsigned int revents;
122 gboolean keep;
123
124 fsource = (struct fd_source *)source;
125 name = g_source_get_name(source);
126 revents = fsource->pollfd.revents;
127
128 if (revents != 0) {
129 sr_spew("%s: %s " G_POLLFD_FORMAT ", revents 0x%.2X",
130 __func__, name, fsource->pollfd.fd, revents);
131 } else {
132 sr_spew("%s: %s " G_POLLFD_FORMAT ", timed out",
133 __func__, name, fsource->pollfd.fd);
134 }
135 if (!callback) {
136 sr_err("Callback not set, cannot dispatch event.");
137 return G_SOURCE_REMOVE;
138 }
139 keep = (*(sr_receive_data_callback)callback)
140 (fsource->pollfd.fd, revents, user_data);
141
142 if (fsource->timeout_us >= 0 && G_LIKELY(keep)
143 && G_LIKELY(!g_source_is_destroyed(source)))
144 fsource->due_us = g_source_get_time(source)
145 + fsource->timeout_us;
146 return keep;
147}
148
149/** FD event source finalize() method.
150 */
151static void fd_source_finalize(GSource *source)
152{
153 struct fd_source *fsource;
154
155 fsource = (struct fd_source *)source;
156
157 sr_dbg("%s: key %p", __func__, fsource->key);
158
159 sr_session_source_destroyed(fsource->session, fsource->key, source);
160}
161
162/** Create an event source for I/O on a file descriptor.
163 *
164 * In order to maintain API compatibility, this event source also doubles
165 * as a timer event source.
166 *
167 * @param session The session the event source belongs to.
168 * @param key The key used to identify this source.
169 * @param fd The file descriptor or HANDLE.
170 * @param timeout_ms The timeout interval in ms, or -1 to wait indefinitely.
171 * @return A new event source object, or NULL on failure.
172 */
173static GSource *fd_source_new(struct sr_session *session, void *key,
174 gintptr fd, int events, int timeout_ms)
175{
176 static GSourceFuncs fd_source_funcs = {
177 .prepare = &fd_source_prepare,
178 .check = &fd_source_check,
179 .dispatch = &fd_source_dispatch,
180 .finalize = &fd_source_finalize
181 };
182 GSource *source;
183 struct fd_source *fsource;
184
185 source = g_source_new(&fd_source_funcs, sizeof(struct fd_source));
186 fsource = (struct fd_source *)source;
187
188 g_source_set_name(source, (fd < 0) ? "timer" : "fd");
189
190 if (timeout_ms >= 0) {
191 fsource->timeout_us = 1000 * (int64_t)timeout_ms;
192 fsource->due_us = 0;
193 } else {
194 fsource->timeout_us = -1;
195 fsource->due_us = INT64_MAX;
196 }
197 fsource->session = session;
198 fsource->key = key;
199
200 fsource->pollfd.fd = fd;
201 fsource->pollfd.events = events;
202 fsource->pollfd.revents = 0;
203
204 if (fd >= 0)
205 g_source_add_poll(source, &fsource->pollfd);
206
207 return source;
208}
209
9f45fb3a
UH
210/**
211 * Create a new session.
7efe889e 212 *
61e6e2da 213 * @param ctx The context in which to create the new session.
7efe889e
UH
214 * @param new_session This will contain a pointer to the newly created
215 * session if the return value is SR_OK, otherwise the value
216 * is undefined and should not be used. Must not be NULL.
9f45fb3a 217 *
0812c40e 218 * @retval SR_OK Success.
41de54ff 219 * @retval SR_ERR_ARG Invalid argument.
47117241 220 *
0812c40e 221 * @since 0.4.0
9f45fb3a 222 */
61e6e2da
ML
223SR_API int sr_session_new(struct sr_context *ctx,
224 struct sr_session **new_session)
a1bb33af 225{
3337e9a1 226 struct sr_session *session;
a1bb33af 227
41de54ff
UH
228 if (!new_session)
229 return SR_ERR_ARG;
230
3337e9a1 231 session = g_malloc0(sizeof(struct sr_session));
b7e94111 232
4ed5d21d 233 session->ctx = ctx;
faa5d7d9 234
c2bf5506 235 g_mutex_init(&session->main_mutex);
faa5d7d9 236
c2bf5506
DE
237 /* To maintain API compatibility, we need a lookup table
238 * which maps poll_object IDs to GSource* pointers.
239 */
240 session->event_sources = g_hash_table_new(NULL, NULL);
0812c40e 241
3337e9a1 242 *new_session = session;
0812c40e
ML
243
244 return SR_OK;
a1bb33af
UH
245}
246
9f45fb3a 247/**
0812c40e 248 * Destroy a session.
9f45fb3a
UH
249 * This frees up all memory used by the session.
250 *
7efe889e
UH
251 * @param session The session to destroy. Must not be NULL.
252 *
04cb9157 253 * @retval SR_OK Success.
0812c40e 254 * @retval SR_ERR_ARG Invalid session passed.
47117241 255 *
0812c40e 256 * @since 0.4.0
9f45fb3a 257 */
0812c40e 258SR_API int sr_session_destroy(struct sr_session *session)
a1bb33af 259{
9f45fb3a 260 if (!session) {
a421dc1d 261 sr_err("%s: session was NULL", __func__);
0812c40e 262 return SR_ERR_ARG;
9f45fb3a
UH
263 }
264
0812c40e 265 sr_session_dev_remove_all(session);
1de3cced
ML
266 g_slist_free_full(session->owned_devs, (GDestroyNotify)sr_dev_inst_free);
267
c2bf5506
DE
268 g_hash_table_unref(session->event_sources);
269
270 g_mutex_clear(&session->main_mutex);
faa5d7d9 271
a1bb33af 272 g_free(session);
0812c40e 273
e0508e67 274 return SR_OK;
a1bb33af
UH
275}
276
9f45fb3a 277/**
0812c40e 278 * Remove all the devices from a session.
9f45fb3a
UH
279 *
280 * The session itself (i.e., the struct sr_session) is not free'd and still
281 * exists after this function returns.
282 *
7efe889e
UH
283 * @param session The session to use. Must not be NULL.
284 *
04cb9157 285 * @retval SR_OK Success.
0812c40e 286 * @retval SR_ERR_BUG Invalid session passed.
47117241 287 *
0812c40e 288 * @since 0.4.0
9f45fb3a 289 */
0812c40e 290SR_API int sr_session_dev_remove_all(struct sr_session *session)
a1bb33af 291{
0812c40e
ML
292 struct sr_dev_inst *sdi;
293 GSList *l;
294
9f45fb3a 295 if (!session) {
a421dc1d 296 sr_err("%s: session was NULL", __func__);
0812c40e
ML
297 return SR_ERR_ARG;
298 }
299
300 for (l = session->devs; l; l = l->next) {
301 sdi = (struct sr_dev_inst *) l->data;
302 sdi->session = NULL;
9f45fb3a
UH
303 }
304
681803df 305 g_slist_free(session->devs);
bb7ef793 306 session->devs = NULL;
e0508e67
UH
307
308 return SR_OK;
a1bb33af
UH
309}
310
9f45fb3a 311/**
0812c40e 312 * Add a device instance to a session.
9f45fb3a 313 *
7efe889e 314 * @param session The session to add to. Must not be NULL.
0812c40e 315 * @param sdi The device instance to add to a session. Must not
de4d3f99
BV
316 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
317 * not be NULL.
9f45fb3a 318 *
04cb9157
MH
319 * @retval SR_OK Success.
320 * @retval SR_ERR_ARG Invalid argument.
47117241 321 *
0812c40e 322 * @since 0.4.0
9f45fb3a 323 */
0812c40e
ML
324SR_API int sr_session_dev_add(struct sr_session *session,
325 struct sr_dev_inst *sdi)
a1bb33af 326{
5451816f 327 int ret;
a1bb33af 328
de4d3f99 329 if (!sdi) {
a421dc1d 330 sr_err("%s: sdi was NULL", __func__);
9f45fb3a
UH
331 return SR_ERR_ARG;
332 }
333
d6eb0c33 334 if (!session) {
a421dc1d 335 sr_err("%s: session was NULL", __func__);
0812c40e
ML
336 return SR_ERR_ARG;
337 }
338
339 /* If sdi->session is not NULL, the device is already in this or
340 * another session. */
341 if (sdi->session) {
342 sr_err("%s: already assigned to session", __func__);
343 return SR_ERR_ARG;
d6eb0c33
UH
344 }
345
de4d3f99
BV
346 /* If sdi->driver is NULL, this is a virtual device. */
347 if (!sdi->driver) {
d6eb0c33 348 /* Just add the device, don't run dev_open(). */
5de0fc55 349 session->devs = g_slist_append(session->devs, sdi);
0812c40e 350 sdi->session = session;
d6eb0c33 351 return SR_OK;
9f45fb3a
UH
352 }
353
de4d3f99
BV
354 /* sdi->driver is non-NULL (i.e. we have a real device). */
355 if (!sdi->driver->dev_open) {
a421dc1d 356 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
8ec95d22 357 return SR_ERR_BUG;
9f45fb3a
UH
358 }
359
5de0fc55 360 session->devs = g_slist_append(session->devs, sdi);
0812c40e 361 sdi->session = session;
aa4b1107 362
5de0fc55
DE
363 /* TODO: This is invalid if the session runs in a different thread.
364 * The usage semantics and restrictions need to be documented.
365 */
5451816f 366 if (session->running) {
32b7cd4f
DE
367 /* Adding a device to a running session. Commit settings
368 * and start acquisition on that device now. */
369 if ((ret = sr_config_commit(sdi)) != SR_OK) {
370 sr_err("Failed to commit device settings before "
371 "starting acquisition in running session (%s)",
372 sr_strerror(ret));
373 return ret;
374 }
5451816f 375 if ((ret = sdi->driver->dev_acquisition_start(sdi,
5de0fc55 376 sdi)) != SR_OK) {
5451816f 377 sr_err("Failed to start acquisition of device in "
32b7cd4f
DE
378 "running session (%s)", sr_strerror(ret));
379 return ret;
380 }
5451816f
BV
381 }
382
e46b8fb1 383 return SR_OK;
a1bb33af
UH
384}
385
2bb311b4 386/**
0812c40e 387 * List all device instances attached to a session.
2bb311b4 388 *
7efe889e 389 * @param session The session to use. Must not be NULL.
2bb311b4
BV
390 * @param devlist A pointer where the device instance list will be
391 * stored on return. If no devices are in the session,
392 * this will be NULL. Each element in the list points
393 * to a struct sr_dev_inst *.
394 * The list must be freed by the caller, but not the
395 * elements pointed to.
396 *
04cb9157 397 * @retval SR_OK Success.
0812c40e 398 * @retval SR_ERR_ARG Invalid argument.
47117241 399 *
0812c40e 400 * @since 0.4.0
2bb311b4 401 */
0812c40e 402SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist)
2bb311b4 403{
2bb311b4 404 if (!session)
0812c40e
ML
405 return SR_ERR_ARG;
406
407 if (!devlist)
408 return SR_ERR_ARG;
2bb311b4
BV
409
410 *devlist = g_slist_copy(session->devs);
411
412 return SR_OK;
413}
414
9f45fb3a 415/**
0812c40e 416 * Remove all datafeed callbacks in a session.
9f45fb3a 417 *
7efe889e
UH
418 * @param session The session to use. Must not be NULL.
419 *
04cb9157 420 * @retval SR_OK Success.
0812c40e 421 * @retval SR_ERR_ARG Invalid session passed.
47117241 422 *
0812c40e 423 * @since 0.4.0
9f45fb3a 424 */
0812c40e 425SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session)
a1bb33af 426{
9f45fb3a 427 if (!session) {
a421dc1d 428 sr_err("%s: session was NULL", __func__);
0812c40e 429 return SR_ERR_ARG;
9f45fb3a
UH
430 }
431
2726474a 432 g_slist_free_full(session->datafeed_callbacks, g_free);
a1bb33af 433 session->datafeed_callbacks = NULL;
e0508e67
UH
434
435 return SR_OK;
a1bb33af
UH
436}
437
9f45fb3a 438/**
0812c40e 439 * Add a datafeed callback to a session.
9f45fb3a 440 *
7efe889e 441 * @param session The session to use. Must not be NULL.
d08490aa 442 * @param cb Function to call when a chunk of data is received.
0abee507 443 * Must not be NULL.
85222791 444 * @param cb_data Opaque pointer passed in by the caller.
a1645fcd 445 *
04cb9157
MH
446 * @retval SR_OK Success.
447 * @retval SR_ERR_BUG No session exists.
47117241
UH
448 *
449 * @since 0.3.0
9f45fb3a 450 */
0812c40e
ML
451SR_API int sr_session_datafeed_callback_add(struct sr_session *session,
452 sr_datafeed_callback cb, void *cb_data)
a1bb33af 453{
2726474a
ML
454 struct datafeed_callback *cb_struct;
455
9f45fb3a 456 if (!session) {
a421dc1d 457 sr_err("%s: session was NULL", __func__);
e0508e67 458 return SR_ERR_BUG;
9f45fb3a
UH
459 }
460
0abee507 461 if (!cb) {
a421dc1d 462 sr_err("%s: cb was NULL", __func__);
0abee507
UH
463 return SR_ERR_ARG;
464 }
9f45fb3a 465
91219afc 466 cb_struct = g_malloc0(sizeof(struct datafeed_callback));
2726474a
ML
467 cb_struct->cb = cb;
468 cb_struct->cb_data = cb_data;
469
62c82025 470 session->datafeed_callbacks =
2726474a 471 g_slist_append(session->datafeed_callbacks, cb_struct);
e0508e67
UH
472
473 return SR_OK;
a1bb33af
UH
474}
475
9f42e2e6
UH
476/**
477 * Get the trigger assigned to this session.
478 *
479 * @param session The session to use.
480 *
481 * @retval NULL Invalid (NULL) session was passed to the function.
482 * @retval other The trigger assigned to this session (can be NULL).
483 *
484 * @since 0.4.0
485 */
0812c40e 486SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session)
7b5e6d29 487{
9f42e2e6
UH
488 if (!session)
489 return NULL;
490
7b5e6d29
BV
491 return session->trigger;
492}
493
9f42e2e6
UH
494/**
495 * Set the trigger of this session.
496 *
497 * @param session The session to use. Must not be NULL.
498 * @param trig The trigger to assign to this session. Can be NULL.
499 *
500 * @retval SR_OK Success.
501 * @retval SR_ERR_ARG Invalid argument.
502 *
503 * @since 0.4.0
504 */
0812c40e 505SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig)
7b5e6d29 506{
9f42e2e6
UH
507 if (!session)
508 return SR_ERR_ARG;
509
7b5e6d29
BV
510 session->trigger = trig;
511
512 return SR_OK;
513}
514
7b5e6d29
BV
515static int verify_trigger(struct sr_trigger *trigger)
516{
517 struct sr_trigger_stage *stage;
518 struct sr_trigger_match *match;
519 GSList *l, *m;
520
521 if (!trigger->stages) {
522 sr_err("No trigger stages defined.");
523 return SR_ERR;
524 }
525
526 sr_spew("Checking trigger:");
527 for (l = trigger->stages; l; l = l->next) {
528 stage = l->data;
529 if (!stage->matches) {
530 sr_err("Stage %d has no matches defined.", stage->stage);
531 return SR_ERR;
532 }
533 for (m = stage->matches; m; m = m->next) {
534 match = m->data;
535 if (!match->channel) {
536 sr_err("Stage %d match has no channel.", stage->stage);
537 return SR_ERR;
538 }
539 if (!match->match) {
540 sr_err("Stage %d match is not defined.", stage->stage);
541 return SR_ERR;
542 }
543 sr_spew("Stage %d match on channel %s, match %d", stage->stage,
544 match->channel->name, match->match);
545 }
546 }
547
548 return SR_OK;
549}
1beccaed 550
c2bf5506
DE
551/** Set up the main context the session will be executing in.
552 *
553 * Must be called just before the session starts, by the thread which
554 * will execute the session main loop. Once acquired, the main context
555 * pointer is immutable for the duration of the session run.
556 */
557static int set_main_context(struct sr_session *session)
558{
2e5e3df4
DE
559 GMainContext *main_context;
560
561 g_mutex_lock(&session->main_mutex);
c2bf5506 562
5de0fc55
DE
563 /* May happen if sr_session_start() is called a second time
564 * while the session is still running.
c2bf5506 565 */
5de0fc55
DE
566 if (session->main_context) {
567 sr_err("Main context already set.");
2e5e3df4
DE
568
569 g_mutex_unlock(&session->main_mutex);
c2bf5506
DE
570 return SR_ERR;
571 }
2e5e3df4 572 main_context = g_main_context_ref_thread_default();
c2bf5506
DE
573 /*
574 * Try to use an existing main context if possible, but only if we
575 * can make it owned by the current thread. Otherwise, create our
576 * own main context so that event source callbacks can execute in
577 * the session thread.
578 */
2e5e3df4
DE
579 if (g_main_context_acquire(main_context)) {
580 g_main_context_release(main_context);
c2bf5506
DE
581
582 sr_dbg("Using thread-default main context.");
c2bf5506 583 } else {
2e5e3df4 584 g_main_context_unref(main_context);
c2bf5506 585
2e5e3df4
DE
586 sr_dbg("Creating our own main context.");
587 main_context = g_main_context_new();
c2bf5506 588 }
2e5e3df4
DE
589 session->main_context = main_context;
590
c2bf5506
DE
591 g_mutex_unlock(&session->main_mutex);
592
593 return SR_OK;
594}
595
596/** Unset the main context used for the current session run.
597 *
598 * Must be called right after stopping the session. Note that if the
599 * session is stopped asynchronously, the main loop may still be running
600 * after the main context has been unset. This is OK as long as no new
601 * event sources are created -- the main loop holds its own reference
602 * to the main context.
603 */
604static int unset_main_context(struct sr_session *session)
605{
606 int ret;
607
608 g_mutex_lock(&session->main_mutex);
609
610 if (session->main_context) {
2e5e3df4 611 g_main_context_unref(session->main_context);
c2bf5506
DE
612 session->main_context = NULL;
613 ret = SR_OK;
614 } else {
615 /* May happen if the set/unset calls are not matched.
616 */
617 sr_err("No main context to unset.");
618 ret = SR_ERR;
619 }
620 g_mutex_unlock(&session->main_mutex);
621
622 return ret;
623}
624
5de0fc55
DE
625static unsigned int session_source_attach(struct sr_session *session,
626 GSource *source)
627{
628 unsigned int id = 0;
629
630 g_mutex_lock(&session->main_mutex);
631
632 if (session->main_context)
633 id = g_source_attach(source, session->main_context);
634 else
635 sr_err("Cannot add event source without main context.");
636
637 g_mutex_unlock(&session->main_mutex);
638
639 return id;
640}
641
642/* Idle handler; invoked when the number of registered event sources
643 * for a running session drops to zero.
644 */
645static gboolean delayed_stop_check(void *data)
646{
647 struct sr_session *session;
648
649 session = data;
650 session->stop_check_id = 0;
651
652 /* Session already ended? */
653 if (!session->running)
654 return G_SOURCE_REMOVE;
655
656 /* New event sources may have been installed in the meantime. */
657 if (g_hash_table_size(session->event_sources) != 0)
658 return G_SOURCE_REMOVE;
659
660 session->running = FALSE;
661 unset_main_context(session);
662
663 sr_info("Stopped.");
664
665 /* This indicates a bug in user code, since it is not valid to
666 * restart or destroy a session while it may still be running.
667 */
668 if (!session->main_loop && !session->stopped_callback) {
669 sr_err("BUG: Session stop left unhandled.");
670 return G_SOURCE_REMOVE;
671 }
672 if (session->main_loop)
673 g_main_loop_quit(session->main_loop);
674
675 if (session->stopped_callback)
676 (*session->stopped_callback)(session->stopped_cb_data);
677
678 return G_SOURCE_REMOVE;
679}
680
681static int stop_check_later(struct sr_session *session)
682{
683 GSource *source;
684 unsigned int source_id;
685
686 if (session->stop_check_id != 0)
687 return SR_OK; /* idle handler already installed */
688
689 source = g_idle_source_new();
690 g_source_set_callback(source, &delayed_stop_check, session, NULL);
691
692 source_id = session_source_attach(session, source);
693 session->stop_check_id = source_id;
694
695 g_source_unref(source);
696
697 return (source_id != 0) ? SR_OK : SR_ERR;
698}
699
9f45fb3a
UH
700/**
701 * Start a session.
702 *
5de0fc55
DE
703 * When this function returns with a status code indicating success, the
704 * session is running. Use sr_session_stopped_callback_set() to receive
705 * notification upon completion, or call sr_session_run() to block until
706 * the session stops.
707 *
708 * Session events will be processed in the context of the current thread.
709 * If a thread-default GLib main context has been set, and is not owned by
710 * any other thread, it will be used. Otherwise, libsigrok will create its
711 * own main context for the current thread.
712 *
7efe889e
UH
713 * @param session The session to use. Must not be NULL.
714 *
04cb9157 715 * @retval SR_OK Success.
0812c40e 716 * @retval SR_ERR_ARG Invalid session passed.
5de0fc55 717 * @retval SR_ERR Other error.
47117241 718 *
0812c40e 719 * @since 0.4.0
9f45fb3a 720 */
0812c40e 721SR_API int sr_session_start(struct sr_session *session)
7d658874 722{
de4d3f99 723 struct sr_dev_inst *sdi;
013ec84b 724 struct sr_channel *ch;
5de0fc55
DE
725 GSList *l, *c, *lend;
726 int ret;
7d658874 727
9f45fb3a 728 if (!session) {
0812c40e
ML
729 sr_err("%s: session was NULL", __func__);
730 return SR_ERR_ARG;
9f45fb3a
UH
731 }
732
bb7ef793 733 if (!session->devs) {
a421dc1d 734 sr_err("%s: session->devs was NULL; a session "
9f45fb3a 735 "cannot be started without devices.", __func__);
0812c40e 736 return SR_ERR_ARG;
9f45fb3a
UH
737 }
738
5de0fc55
DE
739 if (session->running) {
740 sr_err("Cannot (re-)start session while it is still running.");
7b5e6d29 741 return SR_ERR;
5de0fc55 742 }
7b5e6d29 743
5de0fc55
DE
744 if (session->trigger) {
745 ret = verify_trigger(session->trigger);
746 if (ret != SR_OK)
747 return ret;
748 }
9f45fb3a 749
5de0fc55 750 /* Check enabled channels and commit settings of all devices. */
bb7ef793 751 for (l = session->devs; l; l = l->next) {
de4d3f99 752 sdi = l->data;
013ec84b
BV
753 for (c = sdi->channels; c; c = c->next) {
754 ch = c->data;
5de0fc55 755 if (ch->enabled)
013ec84b 756 break;
013ec84b 757 }
5de0fc55
DE
758 if (!c) {
759 sr_err("%s device %s has no enabled channels.",
760 sdi->driver->name, sdi->connection_id);
761 return SR_ERR;
013ec84b
BV
762 }
763
5de0fc55
DE
764 ret = sr_config_commit(sdi);
765 if (ret != SR_OK) {
766 sr_err("Failed to commit %s device %s settings "
767 "before starting acquisition.",
768 sdi->driver->name, sdi->connection_id);
769 return ret;
32b7cd4f 770 }
5de0fc55
DE
771 }
772
773 ret = set_main_context(session);
774 if (ret != SR_OK)
775 return ret;
776
777 sr_info("Starting.");
778
779 session->running = TRUE;
780
781 /* Have all devices start acquisition. */
782 for (l = session->devs; l; l = l->next) {
783 sdi = l->data;
784 ret = sdi->driver->dev_acquisition_start(sdi, sdi);
785 if (ret != SR_OK) {
786 sr_err("Could not start %s device %s acquisition.",
787 sdi->driver->name, sdi->connection_id);
7d658874 788 break;
9f45fb3a 789 }
7d658874
BV
790 }
791
c2bf5506 792 if (ret != SR_OK) {
5de0fc55
DE
793 /* If there are multiple devices, some of them may already have
794 * started successfully. Stop them now before returning. */
795 lend = l->next;
796 for (l = session->devs; l != lend; l = l->next) {
797 sdi = l->data;
798 if (sdi->driver->dev_acquisition_stop)
799 sdi->driver->dev_acquisition_stop(sdi, sdi);
800 }
801 /* TODO: Handle delayed stops. Need to iterate the event
802 * sources... */
c2bf5506 803 session->running = FALSE;
5de0fc55
DE
804
805 unset_main_context(session);
806 return ret;
c2bf5506 807 }
9f45fb3a 808
5de0fc55
DE
809 if (g_hash_table_size(session->event_sources) == 0)
810 stop_check_later(session);
811
812 return SR_OK;
7d658874
BV
813}
814
9f45fb3a 815/**
5de0fc55
DE
816 * Block until the running session stops.
817 *
818 * This is a convenience function which creates a GLib main loop and runs
819 * it to process session events until the session stops.
820 *
821 * Instead of using this function, applications may run their own GLib main
822 * loop, and use sr_session_stopped_callback_set() to receive notification
823 * when the session finished running.
9f45fb3a 824 *
7efe889e
UH
825 * @param session The session to use. Must not be NULL.
826 *
04cb9157 827 * @retval SR_OK Success.
0812c40e 828 * @retval SR_ERR_ARG Invalid session passed.
5de0fc55 829 * @retval SR_ERR Other error.
47117241 830 *
0812c40e 831 * @since 0.4.0
9f45fb3a 832 */
0812c40e 833SR_API int sr_session_run(struct sr_session *session)
7d658874 834{
9f45fb3a 835 if (!session) {
0812c40e
ML
836 sr_err("%s: session was NULL", __func__);
837 return SR_ERR_ARG;
9f45fb3a 838 }
5de0fc55
DE
839 if (!session->running) {
840 sr_err("No session running.");
841 return SR_ERR;
9f45fb3a 842 }
c2bf5506
DE
843 if (session->main_loop) {
844 sr_err("Main loop already created.");
845 return SR_ERR;
846 }
9f45fb3a 847
c2bf5506 848 g_mutex_lock(&session->main_mutex);
5de0fc55 849
c2bf5506
DE
850 if (!session->main_context) {
851 sr_err("Cannot run without main context.");
852 g_mutex_unlock(&session->main_mutex);
853 return SR_ERR;
854 }
c2bf5506 855 session->main_loop = g_main_loop_new(session->main_context, FALSE);
5de0fc55 856
c2bf5506
DE
857 g_mutex_unlock(&session->main_mutex);
858
859 g_main_loop_run(session->main_loop);
860
861 g_main_loop_unref(session->main_loop);
862 session->main_loop = NULL;
863
e0508e67 864 return SR_OK;
7d658874
BV
865}
866
c2bf5506 867static gboolean session_stop_sync(void *user_data)
a1bb33af 868{
c2bf5506 869 struct sr_session *session;
de4d3f99 870 struct sr_dev_inst *sdi;
c2bf5506 871 GSList *node;
a1bb33af 872
c2bf5506
DE
873 session = user_data;
874
875 if (!session->running)
876 return G_SOURCE_REMOVE;
9f45fb3a 877
a421dc1d 878 sr_info("Stopping.");
e0508e67 879
c2bf5506
DE
880 for (node = session->devs; node; node = node->next) {
881 sdi = node->data;
882 if (sdi->driver && sdi->driver->dev_acquisition_stop)
883 sdi->driver->dev_acquisition_stop(sdi, sdi);
a1bb33af 884 }
9f45fb3a 885
c2bf5506 886 return G_SOURCE_REMOVE;
a1bb33af
UH
887}
888
33c6e4c5 889/**
0812c40e 890 * Stop a session.
33c6e4c5 891 *
5de0fc55
DE
892 * This requests the drivers of each device participating in the session to
893 * abort the acquisition as soon as possible. Even after this function returns,
894 * event processing still continues until all devices have actually stopped.
895 *
896 * Use sr_session_stopped_callback_set() to receive notification when the event
897 * processing finished.
33c6e4c5 898 *
5de0fc55
DE
899 * This function is reentrant. That is, it may be called from a different
900 * thread than the one executing the session, as long as it can be ensured
901 * that the session object is valid.
902 *
903 * If the session is not running, sr_session_stop() silently does nothing.
33c6e4c5 904 *
7efe889e
UH
905 * @param session The session to use. Must not be NULL.
906 *
04cb9157 907 * @retval SR_OK Success.
0812c40e 908 * @retval SR_ERR_ARG Invalid session passed.
47117241 909 *
0812c40e 910 * @since 0.4.0
33c6e4c5 911 */
0812c40e 912SR_API int sr_session_stop(struct sr_session *session)
33c6e4c5 913{
5de0fc55
DE
914 GMainContext *main_context;
915
33c6e4c5
AG
916 if (!session) {
917 sr_err("%s: session was NULL", __func__);
c2bf5506 918 return SR_ERR_ARG;
33c6e4c5 919 }
5de0fc55 920
c2bf5506 921 g_mutex_lock(&session->main_mutex);
33c6e4c5 922
5de0fc55
DE
923 main_context = (session->main_context)
924 ? g_main_context_ref(session->main_context)
925 : NULL;
926
c2bf5506 927 g_mutex_unlock(&session->main_mutex);
33c6e4c5 928
5de0fc55
DE
929 if (!main_context) {
930 sr_dbg("No main context set; already stopped?");
931 /* Not an error; as it would be racy. */
932 return SR_OK;
933 }
934 g_main_context_invoke(main_context, &session_stop_sync, session);
935 g_main_context_unref(main_context);
936
937 return SR_OK;
938}
939
940/**
941 * Return whether the session is currently running.
942 *
943 * Note that this function should be called from the same thread
944 * the session was started in.
945 *
946 * @param session The session to use. Must not be NULL.
947 *
948 * @retval TRUE Session is running.
949 * @retval FALSE Session is not running.
950 * @retval SR_ERR_ARG Invalid session passed.
951 *
952 * @since 0.4.0
953 */
954SR_API int sr_session_is_running(struct sr_session *session)
955{
956 if (!session) {
957 sr_err("%s: session was NULL", __func__);
958 return SR_ERR_ARG;
959 }
960 return session->running;
961}
962
963/**
964 * Set the callback to be invoked after a session stopped running.
965 *
966 * Install a callback to receive notification when a session run stopped.
967 * This can be used to integrate session execution with an existing main
968 * loop, without having to block in sr_session_run().
969 *
970 * Note that the callback will be invoked in the context of the thread
971 * that calls sr_session_start().
972 *
973 * @param session The session to use. Must not be NULL.
974 * @param cb The callback to invoke on session stop. May be NULL to unset.
975 * @param cb_data User data pointer to be passed to the callback.
976 *
977 * @retval SR_OK Success.
978 * @retval SR_ERR_ARG Invalid session passed.
979 *
980 * @since 0.4.0
981 */
982SR_API int sr_session_stopped_callback_set(struct sr_session *session,
983 sr_session_stopped_callback cb, void *cb_data)
984{
985 if (!session) {
986 sr_err("%s: session was NULL", __func__);
987 return SR_ERR_ARG;
988 }
989 session->stopped_callback = cb;
990 session->stopped_cb_data = cb_data;
991
992 return SR_OK;
33c6e4c5
AG
993}
994
9f45fb3a 995/**
a1645fcd 996 * Debug helper.
9f45fb3a 997 *
996b0c72 998 * @param packet The packet to show debugging information for.
9f45fb3a 999 */
bf53457d 1000static void datafeed_dump(const struct sr_datafeed_packet *packet)
7d2afd6c 1001{
bf53457d 1002 const struct sr_datafeed_logic *logic;
5faebab2 1003 const struct sr_datafeed_analog_old *analog_old;
1954dfa9 1004 const struct sr_datafeed_analog2 *analog2;
7d2afd6c 1005
ca7dbb56 1006 /* Please use the same order as in libsigrok.h. */
7d2afd6c
BV
1007 switch (packet->type) {
1008 case SR_DF_HEADER:
a421dc1d 1009 sr_dbg("bus: Received SR_DF_HEADER packet.");
7d2afd6c 1010 break;
55c9f09d
UH
1011 case SR_DF_END:
1012 sr_dbg("bus: Received SR_DF_END packet.");
7d2afd6c 1013 break;
c71bac3b 1014 case SR_DF_META:
a421dc1d 1015 sr_dbg("bus: Received SR_DF_META packet.");
ee7489d2 1016 break;
55c9f09d
UH
1017 case SR_DF_TRIGGER:
1018 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
1019 break;
7d2afd6c
BV
1020 case SR_DF_LOGIC:
1021 logic = packet->payload;
7ea45862
UH
1022 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, "
1023 "unitsize = %d).", logic->length, logic->unitsize);
7d2afd6c 1024 break;
5faebab2
UH
1025 case SR_DF_ANALOG_OLD:
1026 analog_old = packet->payload;
1027 sr_dbg("bus: Received SR_DF_ANALOG_OLD packet (%d samples).",
1028 analog_old->num_samples);
ee7489d2 1029 break;
6ea7669c 1030 case SR_DF_FRAME_BEGIN:
a421dc1d 1031 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
6ea7669c
BV
1032 break;
1033 case SR_DF_FRAME_END:
a421dc1d 1034 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
6ea7669c 1035 break;
55c9f09d
UH
1036 case SR_DF_ANALOG2:
1037 analog2 = packet->payload;
1038 sr_dbg("bus: Received SR_DF_ANALOG2 packet (%d samples).",
1039 analog2->num_samples);
1040 break;
7d2afd6c 1041 default:
a421dc1d 1042 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
9f45fb3a 1043 break;
7d2afd6c 1044 }
7d2afd6c
BV
1045}
1046
9f45fb3a 1047/**
a1645fcd
BV
1048 * Send a packet to whatever is listening on the datafeed bus.
1049 *
1050 * Hardware drivers use this to send a data packet to the frontend.
9f45fb3a 1051 *
6b2d8d3e 1052 * @param sdi TODO.
31ccebc4 1053 * @param packet The datafeed packet to send to the session bus.
44dae539 1054 *
04cb9157
MH
1055 * @retval SR_OK Success.
1056 * @retval SR_ERR_ARG Invalid argument.
b4bd7088
UH
1057 *
1058 * @private
9f45fb3a 1059 */
de4d3f99 1060SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
8143cfdc 1061 const struct sr_datafeed_packet *packet)
a1bb33af
UH
1062{
1063 GSList *l;
2726474a 1064 struct datafeed_callback *cb_struct;
c0a1e532
UH
1065 struct sr_datafeed_packet *packet_in, *packet_out;
1066 struct sr_transform *t;
1067 int ret;
a1bb33af 1068
de4d3f99 1069 if (!sdi) {
a421dc1d 1070 sr_err("%s: sdi was NULL", __func__);
e0508e67 1071 return SR_ERR_ARG;
9f45fb3a
UH
1072 }
1073
e0508e67 1074 if (!packet) {
a421dc1d 1075 sr_err("%s: packet was NULL", __func__);
e0508e67 1076 return SR_ERR_ARG;
9f45fb3a
UH
1077 }
1078
79f92686
BV
1079 if (!sdi->session) {
1080 sr_err("%s: session was NULL", __func__);
1081 return SR_ERR_BUG;
1082 }
1083
5faebab2 1084 if (packet->type == SR_DF_ANALOG_OLD) {
ca79993b 1085 /* Convert to SR_DF_ANALOG2. */
5faebab2 1086 const struct sr_datafeed_analog_old *analog_old = packet->payload;
ca79993b
ML
1087 struct sr_analog_encoding encoding;
1088 struct sr_analog_meaning meaning;
1089 struct sr_analog_spec spec;
1090 struct sr_datafeed_analog2 analog2;
1091 struct sr_datafeed_packet a2_packet;
1092 a2_packet.type = SR_DF_ANALOG2;
1093 a2_packet.payload = &analog2;
5faebab2
UH
1094 analog2.data = analog_old->data;
1095 analog2.num_samples = analog_old->num_samples;
ca79993b
ML
1096 analog2.encoding = &encoding;
1097 analog2.meaning = &meaning;
1098 analog2.spec = &spec;
1099 encoding.unitsize = sizeof(float);
1100 encoding.is_signed = TRUE;
1101 encoding.is_float = TRUE;
1102#ifdef WORDS_BIGENDIAN
1103 encoding.is_bigendian = TRUE;
1104#else
1105 encoding.is_bigendian = FALSE;
1106#endif
1107 encoding.digits = 0;
1108 encoding.is_digits_decimal = FALSE;
1109 encoding.scale.p = 1;
1110 encoding.scale.q = 1;
1111 encoding.offset.p = 0;
1112 encoding.offset.q = 1;
5faebab2
UH
1113 meaning.mq = analog_old->mq;
1114 meaning.unit = analog_old->unit;
1115 meaning.mqflags = analog_old->mqflags;
1116 meaning.channels = analog_old->channels;
ca79993b
ML
1117 spec.spec_digits = 0;
1118 return sr_session_send(sdi, &a2_packet);
1119 }
1120
c0a1e532
UH
1121 /*
1122 * Pass the packet to the first transform module. If that returns
1123 * another packet (instead of NULL), pass that packet to the next
1124 * transform module in the list, and so on.
1125 */
1126 packet_in = (struct sr_datafeed_packet *)packet;
1127 for (l = sdi->session->transforms; l; l = l->next) {
1128 t = l->data;
1129 sr_spew("Running transform module '%s'.", t->module->id);
1130 ret = t->module->receive(t, packet_in, &packet_out);
1131 if (ret < 0) {
1132 sr_err("Error while running transform module: %d.", ret);
1133 return SR_ERR;
1134 }
1135 if (!packet_out) {
1136 /*
1137 * If any of the transforms don't return an output
1138 * packet, abort.
1139 */
1140 sr_spew("Transform module didn't return a packet, aborting.");
1141 return SR_OK;
1142 } else {
1143 /*
1144 * Use this transform module's output packet as input
1145 * for the next transform module.
1146 */
1147 packet_in = packet_out;
1148 }
1149 }
4ec436c4 1150 packet = packet_in;
c0a1e532
UH
1151
1152 /*
1153 * If the last transform did output a packet, pass it to all datafeed
1154 * callbacks.
1155 */
3337e9a1 1156 for (l = sdi->session->datafeed_callbacks; l; l = l->next) {
18beaeff
BV
1157 if (sr_log_loglevel_get() >= SR_LOG_DBG)
1158 datafeed_dump(packet);
2726474a
ML
1159 cb_struct = l->data;
1160 cb_struct->cb(sdi, packet, cb_struct->cb_data);
a1bb33af 1161 }
9f45fb3a 1162
e0508e67 1163 return SR_OK;
a1bb33af
UH
1164}
1165
6b2d8d3e
UH
1166/**
1167 * Add an event source for a file descriptor.
1168 *
7efe889e 1169 * @param session The session to use. Must not be NULL.
c2bf5506
DE
1170 * @param key The key which identifies the event source.
1171 * @param source An event source object. Must not be NULL.
ee9953ef 1172 *
04cb9157
MH
1173 * @retval SR_OK Success.
1174 * @retval SR_ERR_ARG Invalid argument.
c2bf5506
DE
1175 * @retval SR_ERR_BUG Event source with @a key already installed.
1176 * @retval SR_ERR Other error.
ee9953ef
DE
1177 *
1178 * @private
6b2d8d3e 1179 */
62d7945f 1180SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
c2bf5506 1181 void *key, GSource *source)
544a4582 1182{
c2bf5506
DE
1183 /*
1184 * This must not ever happen, since the source has already been
1185 * created and its finalize() method will remove the key for the
1186 * already installed source. (Well it would, if we did not have
1187 * another sanity check there.)
92248e78 1188 */
c2bf5506
DE
1189 if (g_hash_table_contains(session->event_sources, key)) {
1190 sr_err("Event source with key %p already exists.", key);
1191 return SR_ERR_BUG;
92248e78 1192 }
c2bf5506
DE
1193 g_hash_table_insert(session->event_sources, key, source);
1194
5de0fc55
DE
1195 if (session_source_attach(session, source) == 0)
1196 return SR_ERR;
faa5d7d9 1197
5de0fc55 1198 return SR_OK;
534b634c
DE
1199}
1200
cbc1413f
DE
1201SR_PRIV int sr_session_fd_source_add(struct sr_session *session,
1202 void *key, gintptr fd, int events, int timeout,
c2bf5506 1203 sr_receive_data_callback cb, void *cb_data)
534b634c 1204{
c2bf5506
DE
1205 GSource *source;
1206 int ret;
534b634c 1207
c2bf5506
DE
1208 source = fd_source_new(session, key, fd, events, timeout);
1209 if (!source)
534b634c 1210 return SR_ERR;
534b634c 1211
c2bf5506 1212 g_source_set_callback(source, (GSourceFunc)cb, cb_data, NULL);
534b634c 1213
c2bf5506
DE
1214 ret = sr_session_source_add_internal(session, key, source);
1215 g_source_unref(source);
544a4582 1216
c2bf5506 1217 return ret;
544a4582
BV
1218}
1219
9f45fb3a 1220/**
6b2d8d3e 1221 * Add an event source for a file descriptor.
9f45fb3a 1222 *
7efe889e 1223 * @param session The session to use. Must not be NULL.
534b634c 1224 * @param fd The file descriptor, or a negative value to create a timer source.
aac0ea25 1225 * @param events Events to check for.
faa5d7d9
DE
1226 * @param timeout Max time in ms to wait before the callback is called,
1227 * or -1 to wait indefinitely.
aac0ea25
LPC
1228 * @param cb Callback function to add. Must not be NULL.
1229 * @param cb_data Data for the callback function. Can be NULL.
9f45fb3a 1230 *
04cb9157
MH
1231 * @retval SR_OK Success.
1232 * @retval SR_ERR_ARG Invalid argument.
47117241
UH
1233 *
1234 * @since 0.3.0
ee9953ef 1235 * @private
aac0ea25 1236 */
ee9953ef 1237SR_PRIV int sr_session_source_add(struct sr_session *session, int fd,
0812c40e 1238 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
aac0ea25 1239{
faa5d7d9 1240 if (fd < 0 && timeout < 0) {
c2bf5506 1241 sr_err("Cannot create timer source without timeout.");
faa5d7d9
DE
1242 return SR_ERR_ARG;
1243 }
cbc1413f 1244 return sr_session_fd_source_add(session, GINT_TO_POINTER(fd),
c2bf5506 1245 fd, events, timeout, cb, cb_data);
aac0ea25
LPC
1246}
1247
1248/**
1a895c61 1249 * Add an event source for a GPollFD.
aac0ea25 1250 *
7efe889e 1251 * @param session The session to use. Must not be NULL.
faa5d7d9
DE
1252 * @param pollfd The GPollFD. Must not be NULL.
1253 * @param timeout Max time in ms to wait before the callback is called,
1254 * or -1 to wait indefinitely.
aac0ea25
LPC
1255 * @param cb Callback function to add. Must not be NULL.
1256 * @param cb_data Data for the callback function. Can be NULL.
44dae539 1257 *
04cb9157
MH
1258 * @retval SR_OK Success.
1259 * @retval SR_ERR_ARG Invalid argument.
47117241
UH
1260 *
1261 * @since 0.3.0
ee9953ef 1262 * @private
9f45fb3a 1263 */
ee9953ef 1264SR_PRIV int sr_session_source_add_pollfd(struct sr_session *session,
0812c40e
ML
1265 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
1266 void *cb_data)
aac0ea25 1267{
faa5d7d9
DE
1268 if (!pollfd) {
1269 sr_err("%s: pollfd was NULL", __func__);
1270 return SR_ERR_ARG;
1271 }
cbc1413f 1272 return sr_session_fd_source_add(session, pollfd, pollfd->fd,
c2bf5506 1273 pollfd->events, timeout, cb, cb_data);
aac0ea25
LPC
1274}
1275
1276/**
1a895c61 1277 * Add an event source for a GIOChannel.
aac0ea25 1278 *
7efe889e 1279 * @param session The session to use. Must not be NULL.
aac0ea25
LPC
1280 * @param channel The GIOChannel.
1281 * @param events Events to poll on.
faa5d7d9
DE
1282 * @param timeout Max time in ms to wait before the callback is called,
1283 * or -1 to wait indefinitely.
aac0ea25
LPC
1284 * @param cb Callback function to add. Must not be NULL.
1285 * @param cb_data Data for the callback function. Can be NULL.
1286 *
04cb9157
MH
1287 * @retval SR_OK Success.
1288 * @retval SR_ERR_ARG Invalid argument.
47117241
UH
1289 *
1290 * @since 0.3.0
ee9953ef 1291 * @private
aac0ea25 1292 */
ee9953ef 1293SR_PRIV int sr_session_source_add_channel(struct sr_session *session,
0812c40e
ML
1294 GIOChannel *channel, int events, int timeout,
1295 sr_receive_data_callback cb, void *cb_data)
aac0ea25 1296{
c2bf5506 1297 GPollFD pollfd;
aac0ea25 1298
c2bf5506
DE
1299 if (!channel) {
1300 sr_err("%s: channel was NULL", __func__);
1301 return SR_ERR_ARG;
1302 }
1303 /* We should be using g_io_create_watch(), but can't without
1304 * changing the driver API, as the callback signature is different.
1305 */
faa5d7d9 1306#ifdef G_OS_WIN32
c2bf5506 1307 g_io_channel_win32_make_pollfd(channel, events, &pollfd);
aac0ea25 1308#else
c2bf5506
DE
1309 pollfd.fd = g_io_channel_unix_get_fd(channel);
1310 pollfd.events = events;
aac0ea25 1311#endif
cbc1413f 1312 return sr_session_fd_source_add(session, channel, pollfd.fd,
c2bf5506 1313 pollfd.events, timeout, cb, cb_data);
aac0ea25
LPC
1314}
1315
6b2d8d3e 1316/**
92248e78 1317 * Remove the source identified by the specified poll object.
6b2d8d3e 1318 *
7efe889e 1319 * @param session The session to use. Must not be NULL.
c2bf5506 1320 * @param key The key by which the source is identified.
6b2d8d3e 1321 *
04cb9157 1322 * @retval SR_OK Success
92248e78 1323 * @retval SR_ERR_BUG No event source for poll_object found.
ee9953ef
DE
1324 *
1325 * @private
6b2d8d3e 1326 */
92248e78 1327SR_PRIV int sr_session_source_remove_internal(struct sr_session *session,
c2bf5506 1328 void *key)
544a4582 1329{
c2bf5506 1330 GSource *source;
544a4582 1331
c2bf5506
DE
1332 source = g_hash_table_lookup(session->event_sources, key);
1333 /*
1334 * Trying to remove an already removed event source is problematic
92248e78
DE
1335 * since the poll_object handle may have been reused in the meantime.
1336 */
534b634c 1337 if (!source) {
c2bf5506
DE
1338 sr_warn("Cannot remove non-existing event source %p.", key);
1339 return SR_ERR_BUG;
534b634c 1340 }
c2bf5506 1341 g_source_destroy(source);
534b634c 1342
c2bf5506 1343 return SR_OK;
534b634c
DE
1344}
1345
6b2d8d3e 1346/**
aac0ea25
LPC
1347 * Remove the source belonging to the specified file descriptor.
1348 *
7efe889e 1349 * @param session The session to use. Must not be NULL.
1a895c61 1350 * @param fd The file descriptor for which the source should be removed.
aac0ea25 1351 *
04cb9157
MH
1352 * @retval SR_OK Success
1353 * @retval SR_ERR_ARG Invalid argument
04cb9157 1354 * @retval SR_ERR_BUG Internal error.
47117241
UH
1355 *
1356 * @since 0.3.0
ee9953ef 1357 * @private
aac0ea25 1358 */
ee9953ef 1359SR_PRIV int sr_session_source_remove(struct sr_session *session, int fd)
aac0ea25 1360{
c2bf5506 1361 return sr_session_source_remove_internal(session, GINT_TO_POINTER(fd));
aac0ea25
LPC
1362}
1363
1364/**
1365 * Remove the source belonging to the specified poll descriptor.
1366 *
7efe889e 1367 * @param session The session to use. Must not be NULL.
aac0ea25 1368 * @param pollfd The poll descriptor for which the source should be removed.
faa5d7d9 1369 * Must not be NULL.
aac0ea25
LPC
1370 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
1371 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
1372 * internal errors.
47117241
UH
1373 *
1374 * @since 0.2.0
ee9953ef 1375 * @private
aac0ea25 1376 */
ee9953ef 1377SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session,
0812c40e 1378 GPollFD *pollfd)
aac0ea25 1379{
faa5d7d9
DE
1380 if (!pollfd) {
1381 sr_err("%s: pollfd was NULL", __func__);
1382 return SR_ERR_ARG;
1383 }
c2bf5506 1384 return sr_session_source_remove_internal(session, pollfd);
aac0ea25
LPC
1385}
1386
6b2d8d3e 1387/**
aac0ea25
LPC
1388 * Remove the source belonging to the specified channel.
1389 *
7efe889e 1390 * @param session The session to use. Must not be NULL.
1a895c61 1391 * @param channel The channel for which the source should be removed.
faa5d7d9 1392 * Must not be NULL.
04cb9157
MH
1393 * @retval SR_OK Success.
1394 * @retval SR_ERR_ARG Invalid argument.
04cb9157 1395 * @return SR_ERR_BUG Internal error.
47117241
UH
1396 *
1397 * @since 0.2.0
ee9953ef 1398 * @private
aac0ea25 1399 */
ee9953ef 1400SR_PRIV int sr_session_source_remove_channel(struct sr_session *session,
0812c40e 1401 GIOChannel *channel)
aac0ea25 1402{
faa5d7d9
DE
1403 if (!channel) {
1404 sr_err("%s: channel was NULL", __func__);
1405 return SR_ERR_ARG;
1406 }
c2bf5506
DE
1407 return sr_session_source_remove_internal(session, channel);
1408}
1409
1410/** Unregister an event source that has been destroyed.
1411 *
1412 * This is intended to be called from a source's finalize() method.
1413 *
1414 * @param session The session to use. Must not be NULL.
1415 * @param key The key used to identify @a source.
1416 * @param source The source object that was destroyed.
1417 *
1418 * @retval SR_OK Success.
1419 * @retval SR_ERR_BUG Event source for @a key does not match @a source.
5de0fc55 1420 * @retval SR_ERR Other error.
ee9953ef
DE
1421 *
1422 * @private
c2bf5506
DE
1423 */
1424SR_PRIV int sr_session_source_destroyed(struct sr_session *session,
1425 void *key, GSource *source)
1426{
1427 GSource *registered_source;
1428
1429 registered_source = g_hash_table_lookup(session->event_sources, key);
1430 /*
1431 * Trying to remove an already removed event source is problematic
1432 * since the poll_object handle may have been reused in the meantime.
1433 */
1434 if (!registered_source) {
1435 sr_err("No event source for key %p found.", key);
1436 return SR_ERR_BUG;
1437 }
1438 if (registered_source != source) {
1439 sr_err("Event source for key %p does not match"
1440 " destroyed source.", key);
1441 return SR_ERR_BUG;
1442 }
1443 g_hash_table_remove(session->event_sources, key);
5de0fc55
DE
1444
1445 if (g_hash_table_size(session->event_sources) > 0)
1446 return SR_OK;
1447
1448 /* If no event sources are left, consider the acquisition finished.
1449 * This is pretty crude, as it requires all event sources to be
1450 * registered via the libsigrok API.
c2bf5506 1451 */
5de0fc55 1452 return stop_check_later(session);
aac0ea25 1453}
7b870c38 1454
ee29d92e 1455static void copy_src(struct sr_config *src, struct sr_datafeed_meta *meta_copy)
8143cfdc 1456{
8143cfdc 1457 g_variant_ref(src->data);
ee29d92e
AJ
1458 meta_copy->config = g_slist_append(meta_copy->config,
1459 g_memdup(src, sizeof(struct sr_config)));
8143cfdc
BV
1460}
1461
1462SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet,
1463 struct sr_datafeed_packet **copy)
1464{
1465 const struct sr_datafeed_meta *meta;
1466 struct sr_datafeed_meta *meta_copy;
1467 const struct sr_datafeed_logic *logic;
1468 struct sr_datafeed_logic *logic_copy;
5faebab2
UH
1469 const struct sr_datafeed_analog_old *analog_old;
1470 struct sr_datafeed_analog_old *analog_old_copy;
dbdfa4fb
ML
1471 const struct sr_datafeed_analog2 *analog2;
1472 struct sr_datafeed_analog2 *analog2_copy;
8143cfdc
BV
1473 uint8_t *payload;
1474
1475 *copy = g_malloc0(sizeof(struct sr_datafeed_packet));
1476 (*copy)->type = packet->type;
1477
1478 switch (packet->type) {
1479 case SR_DF_TRIGGER:
1480 case SR_DF_END:
1481 /* No payload. */
1482 break;
1483 case SR_DF_HEADER:
1484 payload = g_malloc(sizeof(struct sr_datafeed_header));
1485 memcpy(payload, packet->payload, sizeof(struct sr_datafeed_header));
1486 (*copy)->payload = payload;
1487 break;
1488 case SR_DF_META:
1489 meta = packet->payload;
ee29d92e
AJ
1490 meta_copy = g_malloc0(sizeof(struct sr_datafeed_meta));
1491 g_slist_foreach(meta->config, (GFunc)copy_src, meta_copy->config);
8143cfdc
BV
1492 (*copy)->payload = meta_copy;
1493 break;
1494 case SR_DF_LOGIC:
1495 logic = packet->payload;
1496 logic_copy = g_malloc(sizeof(logic));
1497 logic_copy->length = logic->length;
1498 logic_copy->unitsize = logic->unitsize;
1499 memcpy(logic_copy->data, logic->data, logic->length * logic->unitsize);
1500 (*copy)->payload = logic_copy;
1501 break;
5faebab2
UH
1502 case SR_DF_ANALOG_OLD:
1503 analog_old = packet->payload;
1504 analog_old_copy = g_malloc(sizeof(analog_old));
1505 analog_old_copy->channels = g_slist_copy(analog_old->channels);
1506 analog_old_copy->num_samples = analog_old->num_samples;
1507 analog_old_copy->mq = analog_old->mq;
1508 analog_old_copy->unit = analog_old->unit;
1509 analog_old_copy->mqflags = analog_old->mqflags;
1510 analog_old_copy->data = g_malloc(analog_old->num_samples * sizeof(float));
1511 memcpy(analog_old_copy->data, analog_old->data,
1512 analog_old->num_samples * sizeof(float));
1513 (*copy)->payload = analog_old_copy;
8143cfdc 1514 break;
dbdfa4fb
ML
1515 case SR_DF_ANALOG2:
1516 analog2 = packet->payload;
1517 analog2_copy = g_malloc(sizeof(analog2));
1518 analog2_copy->data = g_malloc(
1519 analog2->encoding->unitsize * analog2->num_samples);
1520 memcpy(analog2_copy->data, analog2->data,
1521 analog2->encoding->unitsize * analog2->num_samples);
1522 analog2_copy->num_samples = analog2->num_samples;
1523 analog2_copy->encoding = g_memdup(analog2->encoding,
1524 sizeof(struct sr_analog_encoding));
1525 analog2_copy->meaning = g_memdup(analog2->meaning,
1526 sizeof(struct sr_analog_meaning));
1527 analog2_copy->meaning->channels = g_slist_copy(
1528 analog2->meaning->channels);
1529 analog2_copy->spec = g_memdup(analog2->spec,
1530 sizeof(struct sr_analog_spec));
1531 (*copy)->payload = analog2_copy;
1532 break;
8143cfdc
BV
1533 default:
1534 sr_err("Unknown packet type %d", packet->type);
1535 return SR_ERR;
1536 }
1537
1538 return SR_OK;
1539}
1540
1541void sr_packet_free(struct sr_datafeed_packet *packet)
1542{
1543 const struct sr_datafeed_meta *meta;
1544 const struct sr_datafeed_logic *logic;
5faebab2 1545 const struct sr_datafeed_analog_old *analog_old;
83c1dbd9 1546 const struct sr_datafeed_analog2 *analog2;
8143cfdc
BV
1547 struct sr_config *src;
1548 GSList *l;
1549
1550 switch (packet->type) {
1551 case SR_DF_TRIGGER:
1552 case SR_DF_END:
1553 /* No payload. */
1554 break;
1555 case SR_DF_HEADER:
1556 /* Payload is a simple struct. */
1557 g_free((void *)packet->payload);
1558 break;
1559 case SR_DF_META:
1560 meta = packet->payload;
1561 for (l = meta->config; l; l = l->next) {
1562 src = l->data;
1563 g_variant_unref(src->data);
1564 g_free(src);
1565 }
1566 g_slist_free(meta->config);
1567 g_free((void *)packet->payload);
1568 break;
1569 case SR_DF_LOGIC:
1570 logic = packet->payload;
1571 g_free(logic->data);
1572 g_free((void *)packet->payload);
1573 break;
5faebab2
UH
1574 case SR_DF_ANALOG_OLD:
1575 analog_old = packet->payload;
1576 g_slist_free(analog_old->channels);
1577 g_free(analog_old->data);
8143cfdc
BV
1578 g_free((void *)packet->payload);
1579 break;
83c1dbd9
ML
1580 case SR_DF_ANALOG2:
1581 analog2 = packet->payload;
1582 g_free(analog2->data);
1583 g_free(analog2->encoding);
1584 g_slist_free(analog2->meaning->channels);
1585 g_free(analog2->meaning);
1586 g_free(analog2->spec);
1587 g_free((void *)packet->payload);
1588 break;
8143cfdc
BV
1589 default:
1590 sr_err("Unknown packet type %d", packet->type);
1591 }
1592 g_free(packet);
1593
1594}
1595
7b870c38 1596/** @} */