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