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