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