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