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