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