]> sigrok.org Git - libsigrok.git/blame - session.c
Rename session_dev_id to cb_data everywhere.
[libsigrok.git] / session.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
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
a421dc1d
UH
28/* Message logging helpers with driver-specific prefix string. */
29#define DRIVER_LOG_DOMAIN "session: "
30#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
31#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
32#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
33#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
34#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
35#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
36
393fb9cb
UH
37/**
38 * @file
39 *
40 * Creating, using, or destroying libsigrok sessions.
41 */
42
7b870c38
UH
43/**
44 * @defgroup grp_session Session handling
45 *
46 * Creating, using, or destroying libsigrok sessions.
47 *
48 * @{
49 */
50
544a4582 51struct source {
544a4582 52 int timeout;
d08490aa 53 sr_receive_data_callback_t cb;
1f9813eb 54 void *cb_data;
aac0ea25
LPC
55
56 /* This is used to keep track of the object (fd, pollfd or channel) which is
57 * being polled and will be used to match the source when removing it again.
58 */
59 gintptr poll_object;
544a4582
BV
60};
61
7d658874 62/* There can only be one session at a time. */
a0ecd83b 63/* 'session' is not static, it's used elsewhere (via 'extern'). */
2872d21e 64struct sr_session *session;
544a4582 65
9f45fb3a
UH
66/**
67 * Create a new session.
68 *
6b2d8d3e 69 * @todo Should it use the file-global "session" variable or take an argument?
9f45fb3a
UH
70 * The same question applies to all the other session functions.
71 *
72 * @return A pointer to the newly allocated session, or NULL upon errors.
73 */
1a081ca6 74SR_API struct sr_session *sr_session_new(void)
a1bb33af 75{
133a37bf 76 if (!(session = g_try_malloc0(sizeof(struct sr_session)))) {
a421dc1d
UH
77 sr_err("Session malloc failed.");
78 return NULL;
9f45fb3a 79 }
a1bb33af 80
b7e94111
LPC
81 session->source_timeout = -1;
82
a1bb33af
UH
83 return session;
84}
85
9f45fb3a
UH
86/**
87 * Destroy the current session.
88 *
89 * This frees up all memory used by the session.
90 *
e0508e67 91 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 92 */
1a081ca6 93SR_API int sr_session_destroy(void)
a1bb33af 94{
9f45fb3a 95 if (!session) {
a421dc1d 96 sr_err("%s: session was NULL", __func__);
e0508e67 97 return SR_ERR_BUG;
9f45fb3a
UH
98 }
99
ed229aaa 100 sr_session_dev_remove_all();
a1bb33af 101
9f45fb3a
UH
102 /* TODO: Error checks needed? */
103
a1bb33af 104 g_free(session);
9f45fb3a 105 session = NULL;
e0508e67
UH
106
107 return SR_OK;
a1bb33af
UH
108}
109
961009b0
UH
110/**
111 * Close a device instance.
112 *
113 * @param sdi The device instance to close. Must not be NULL. Also,
114 * sdi->driver, sdi->driver->priv, and sdi->priv must not be NULL.
115 */
de4d3f99 116static void sr_dev_close(struct sr_dev_inst *sdi)
ed229aaa 117{
961009b0
UH
118 int ret;
119
120 if (!sdi) {
121 sr_err("Invalid device instance, can't close device.");
122 return;
123 }
124
125 /* In the drivers sdi->priv is a 'struct dev_context *devc'. */
126 if (!sdi->priv) {
127 /*
128 * Should be sr_err() in theory, but the 'demo' driver has
129 * NULL for sdi->priv, so we use sr_dbg() until that's fixed.
130 */
131 sr_dbg("Invalid device context, can't close device.");
132 return;
133 }
134
135 if (!sdi->driver) {
136 sr_err("Invalid driver, can't close device.");
137 return;
138 }
139
140 if (!sdi->driver->priv) {
141 sr_err("Driver not initialized, can't close device.");
142 return;
143 }
144
145 sr_spew("Closing '%s' device instance %d.", sdi->driver->name,
146 sdi->index);
147
148 if ((ret = sdi->driver->dev_close(sdi)) < 0)
149 sr_err("Failed to close device instance: %d.", ret);
ed229aaa
LPC
150}
151
9f45fb3a 152/**
6b2d8d3e 153 * Remove all the devices from the current session.
9f45fb3a
UH
154 *
155 * The session itself (i.e., the struct sr_session) is not free'd and still
156 * exists after this function returns.
157 *
e0508e67 158 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 159 */
01c3e9db 160SR_API int sr_session_dev_remove_all(void)
a1bb33af 161{
9f45fb3a 162 if (!session) {
a421dc1d 163 sr_err("%s: session was NULL", __func__);
e0508e67 164 return SR_ERR_BUG;
9f45fb3a
UH
165 }
166
ed229aaa 167 g_slist_free_full(session->devs, (GDestroyNotify)sr_dev_close);
bb7ef793 168 session->devs = NULL;
e0508e67
UH
169
170 return SR_OK;
a1bb33af
UH
171}
172
9f45fb3a 173/**
9c5332d2 174 * Add a device instance to the current session.
9f45fb3a 175 *
9c5332d2 176 * @param sdi The device instance to add to the current session. Must not
de4d3f99
BV
177 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
178 * not be NULL.
9f45fb3a
UH
179 *
180 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
181 */
de4d3f99 182SR_API int sr_session_dev_add(const struct sr_dev_inst *sdi)
a1bb33af
UH
183{
184 int ret;
185
de4d3f99 186 if (!sdi) {
a421dc1d 187 sr_err("%s: sdi was NULL", __func__);
9f45fb3a
UH
188 return SR_ERR_ARG;
189 }
190
d6eb0c33 191 if (!session) {
a421dc1d 192 sr_err("%s: session was NULL", __func__);
d6eb0c33
UH
193 return SR_ERR_BUG;
194 }
195
de4d3f99
BV
196 /* If sdi->driver is NULL, this is a virtual device. */
197 if (!sdi->driver) {
a421dc1d 198 sr_dbg("%s: sdi->driver was NULL, this seems to be "
d6eb0c33
UH
199 "a virtual device; continuing", __func__);
200 /* Just add the device, don't run dev_open(). */
de4d3f99 201 session->devs = g_slist_append(session->devs, (gpointer)sdi);
d6eb0c33 202 return SR_OK;
9f45fb3a
UH
203 }
204
de4d3f99
BV
205 /* sdi->driver is non-NULL (i.e. we have a real device). */
206 if (!sdi->driver->dev_open) {
a421dc1d 207 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
8ec95d22 208 return SR_ERR_BUG;
9f45fb3a
UH
209 }
210
de4d3f99 211 if ((ret = sdi->driver->dev_open((struct sr_dev_inst *)sdi)) != SR_OK) {
a421dc1d 212 sr_err("%s: dev_open failed (%d)", __func__, ret);
9f45fb3a 213 return ret;
aa4b1107 214 }
a1bb33af 215
de4d3f99 216 session->devs = g_slist_append(session->devs, (gpointer)sdi);
aa4b1107 217
e46b8fb1 218 return SR_OK;
a1bb33af
UH
219}
220
9f45fb3a 221/**
01c3e9db 222 * Remove all datafeed callbacks in the current session.
9f45fb3a 223 *
e0508e67 224 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 225 */
01c3e9db 226SR_API int sr_session_datafeed_callback_remove_all(void)
a1bb33af 227{
9f45fb3a 228 if (!session) {
a421dc1d 229 sr_err("%s: session was NULL", __func__);
e0508e67 230 return SR_ERR_BUG;
9f45fb3a
UH
231 }
232
a1bb33af
UH
233 g_slist_free(session->datafeed_callbacks);
234 session->datafeed_callbacks = NULL;
e0508e67
UH
235
236 return SR_OK;
a1bb33af
UH
237}
238
9f45fb3a
UH
239/**
240 * Add a datafeed callback to the current session.
241 *
d08490aa 242 * @param cb Function to call when a chunk of data is received.
0abee507 243 * Must not be NULL.
a1645fcd 244 *
e0508e67 245 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 246 */
d08490aa 247SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
a1bb33af 248{
9f45fb3a 249 if (!session) {
a421dc1d 250 sr_err("%s: session was NULL", __func__);
e0508e67 251 return SR_ERR_BUG;
9f45fb3a
UH
252 }
253
0abee507 254 if (!cb) {
a421dc1d 255 sr_err("%s: cb was NULL", __func__);
0abee507
UH
256 return SR_ERR_ARG;
257 }
9f45fb3a 258
62c82025 259 session->datafeed_callbacks =
d08490aa 260 g_slist_append(session->datafeed_callbacks, cb);
e0508e67
UH
261
262 return SR_OK;
a1bb33af
UH
263}
264
e0508e67 265static int sr_session_run_poll(void)
544a4582 266{
b7e94111
LPC
267 unsigned int i;
268 int ret;
544a4582 269
2cbeb2b7
BV
270 while (session->num_sources > 0) {
271 ret = g_poll(session->pollfds, session->num_sources,
272 session->source_timeout);
b7e94111
LPC
273 for (i = 0; i < session->num_sources; i++) {
274 if (session->pollfds[i].revents > 0 || (ret == 0
275 && session->source_timeout == session->sources[i].timeout)) {
544a4582
BV
276 /*
277 * Invoke the source's callback on an event,
2cbeb2b7 278 * or if the poll timed out and this source
544a4582
BV
279 * asked for that timeout.
280 */
2cbeb2b7
BV
281 if (!session->sources[i].cb(session->pollfds[i].fd,
282 session->pollfds[i].revents,
283 session->sources[i].cb_data))
b7e94111 284 sr_session_source_remove(session->sources[i].poll_object);
544a4582
BV
285 }
286 }
287 }
e0508e67
UH
288
289 return SR_OK;
544a4582
BV
290}
291
9f45fb3a
UH
292/**
293 * Start a session.
294 *
a1645fcd 295 * There can only be one session at a time.
9f45fb3a
UH
296 *
297 * @return SR_OK upon success, SR_ERR upon errors.
298 */
1a081ca6 299SR_API int sr_session_start(void)
7d658874 300{
de4d3f99 301 struct sr_dev_inst *sdi;
7d658874
BV
302 GSList *l;
303 int ret;
304
9f45fb3a 305 if (!session) {
a421dc1d 306 sr_err("%s: session was NULL; a session must be "
de4d3f99 307 "created before starting it.", __func__);
0abee507 308 return SR_ERR_BUG;
9f45fb3a
UH
309 }
310
bb7ef793 311 if (!session->devs) {
a421dc1d 312 sr_err("%s: session->devs was NULL; a session "
9f45fb3a 313 "cannot be started without devices.", __func__);
0abee507 314 return SR_ERR_BUG;
9f45fb3a
UH
315 }
316
b08024a8 317 sr_info("session: starting");
9f45fb3a 318
b7c3e849 319 ret = SR_OK;
bb7ef793 320 for (l = session->devs; l; l = l->next) {
de4d3f99
BV
321 sdi = l->data;
322 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
a421dc1d 323 sr_err("%s: could not start an acquisition "
9f45fb3a 324 "(%d)", __func__, ret);
7d658874 325 break;
9f45fb3a 326 }
7d658874
BV
327 }
328
9f45fb3a
UH
329 /* TODO: What if there are multiple devices? Which return code? */
330
7d658874
BV
331 return ret;
332}
333
9f45fb3a
UH
334/**
335 * Run the session.
336 *
e0508e67 337 * @return SR_OK upon success, SR_ERR_BUG upon errors.
9f45fb3a 338 */
1a081ca6 339SR_API int sr_session_run(void)
7d658874 340{
9f45fb3a 341 if (!session) {
a421dc1d 342 sr_err("%s: session was NULL; a session must be "
9f45fb3a 343 "created first, before running it.", __func__);
e0508e67 344 return SR_ERR_BUG;
9f45fb3a
UH
345 }
346
bb7ef793 347 if (!session->devs) {
9f45fb3a 348 /* TODO: Actually the case? */
a421dc1d 349 sr_err("%s: session->devs was NULL; a session "
9f45fb3a 350 "cannot be run without devices.", __func__);
e0508e67 351 return SR_ERR_BUG;
9f45fb3a
UH
352 }
353
a421dc1d 354 sr_info("Running.");
7d658874 355
9f45fb3a 356 /* Do we have real sources? */
b7e94111 357 if (session->num_sources == 1 && session->pollfds[0].fd == -1) {
9f45fb3a 358 /* Dummy source, freewheel over it. */
2cbeb2b7 359 while (session->num_sources)
b7e94111 360 session->sources[0].cb(-1, 0, session->sources[0].cb_data);
9f45fb3a
UH
361 } else {
362 /* Real sources, use g_poll() main loop. */
8a2efef2 363 sr_session_run_poll();
9f45fb3a
UH
364 }
365
e0508e67 366 return SR_OK;
7d658874
BV
367}
368
9f45fb3a
UH
369/**
370 * Halt the current session.
371 *
9ffbde0e
LPC
372 * This function is deprecated and should not be used in new code, use
373 * sr_session_stop() instead. The behaviour of this function is identical to
374 * sr_session_stop().
e0508e67
UH
375 *
376 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 377 */
1a081ca6 378SR_API int sr_session_halt(void)
544a4582 379{
9ffbde0e 380 return sr_session_stop();
544a4582
BV
381}
382
9f45fb3a
UH
383/**
384 * Stop the current session.
385 *
a1645fcd 386 * The current session is stopped immediately, with all acquisition sessions
c09f0b57 387 * being stopped and hardware drivers cleaned up.
9f45fb3a 388 *
e0508e67 389 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 390 */
1a081ca6 391SR_API int sr_session_stop(void)
a1bb33af 392{
de4d3f99 393 struct sr_dev_inst *sdi;
a1bb33af
UH
394 GSList *l;
395
9f45fb3a 396 if (!session) {
a421dc1d 397 sr_err("%s: session was NULL", __func__);
e0508e67 398 return SR_ERR_BUG;
9f45fb3a
UH
399 }
400
a421dc1d 401 sr_info("Stopping.");
e0508e67 402
bb7ef793 403 for (l = session->devs; l; l = l->next) {
de4d3f99
BV
404 sdi = l->data;
405 if (sdi->driver) {
406 if (sdi->driver->dev_acquisition_stop)
407 sdi->driver->dev_acquisition_stop(sdi, sdi);
8c76be53 408 }
a1bb33af 409 }
9f45fb3a 410
f1f7e62d
AG
411 /*
412 * Some sources may not be necessarily associated with a device.
413 * Those sources may still be present even after stopping all devices.
414 * We need to make sure all sources are removed, or we risk running the
415 * session in an infinite loop.
416 */
417 while (session->num_sources)
418 sr_session_source_remove(session->sources[0].poll_object);
419
e0508e67 420 return SR_OK;
a1bb33af
UH
421}
422
9f45fb3a 423/**
a1645fcd 424 * Debug helper.
9f45fb3a 425 *
996b0c72 426 * @param packet The packet to show debugging information for.
9f45fb3a 427 */
bf53457d 428static void datafeed_dump(const struct sr_datafeed_packet *packet)
7d2afd6c 429{
bf53457d
JH
430 const struct sr_datafeed_logic *logic;
431 const struct sr_datafeed_analog *analog;
7d2afd6c
BV
432
433 switch (packet->type) {
434 case SR_DF_HEADER:
a421dc1d 435 sr_dbg("bus: Received SR_DF_HEADER packet.");
7d2afd6c
BV
436 break;
437 case SR_DF_TRIGGER:
a421dc1d 438 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
7d2afd6c 439 break;
c71bac3b 440 case SR_DF_META:
a421dc1d 441 sr_dbg("bus: Received SR_DF_META packet.");
ee7489d2 442 break;
7d2afd6c
BV
443 case SR_DF_LOGIC:
444 logic = packet->payload;
a421dc1d
UH
445 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes).",
446 logic->length);
7d2afd6c 447 break;
ee7489d2
BV
448 case SR_DF_ANALOG:
449 analog = packet->payload;
a421dc1d
UH
450 sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).",
451 analog->num_samples);
ee7489d2 452 break;
7d2afd6c 453 case SR_DF_END:
a421dc1d 454 sr_dbg("bus: Received SR_DF_END packet.");
7d2afd6c 455 break;
6ea7669c 456 case SR_DF_FRAME_BEGIN:
a421dc1d 457 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
6ea7669c
BV
458 break;
459 case SR_DF_FRAME_END:
a421dc1d 460 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
6ea7669c 461 break;
7d2afd6c 462 default:
a421dc1d 463 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
9f45fb3a 464 break;
7d2afd6c 465 }
7d2afd6c
BV
466}
467
9f45fb3a 468/**
a1645fcd
BV
469 * Send a packet to whatever is listening on the datafeed bus.
470 *
471 * Hardware drivers use this to send a data packet to the frontend.
9f45fb3a 472 *
6b2d8d3e 473 * @param sdi TODO.
31ccebc4 474 * @param packet The datafeed packet to send to the session bus.
44dae539 475 *
e0508e67 476 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
b4bd7088
UH
477 *
478 * @private
9f45fb3a 479 */
de4d3f99 480SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
bf53457d 481 const struct sr_datafeed_packet *packet)
a1bb33af
UH
482{
483 GSList *l;
d08490aa 484 sr_datafeed_callback_t cb;
a1bb33af 485
de4d3f99 486 if (!sdi) {
a421dc1d 487 sr_err("%s: sdi was NULL", __func__);
e0508e67 488 return SR_ERR_ARG;
9f45fb3a
UH
489 }
490
e0508e67 491 if (!packet) {
a421dc1d 492 sr_err("%s: packet was NULL", __func__);
e0508e67 493 return SR_ERR_ARG;
9f45fb3a
UH
494 }
495
62c82025 496 for (l = session->datafeed_callbacks; l; l = l->next) {
18beaeff
BV
497 if (sr_log_loglevel_get() >= SR_LOG_DBG)
498 datafeed_dump(packet);
a1bb33af 499 cb = l->data;
de4d3f99 500 cb(sdi, packet);
a1bb33af 501 }
9f45fb3a 502
e0508e67 503 return SR_OK;
a1bb33af
UH
504}
505
6b2d8d3e
UH
506/**
507 * Add an event source for a file descriptor.
508 *
509 * @param pollfd The GPollFD.
510 * @param timeout Max time to wait before the callback is called, ignored if 0.
511 * @param cb Callback function to add. Must not be NULL.
512 * @param cb_data Data for the callback function. Can be NULL.
513 * @param poll_object TODO.
514 *
515 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
516 * SR_ERR_MALLOC upon memory allocation errors.
517 */
aac0ea25 518static int _sr_session_source_add(GPollFD *pollfd, int timeout,
1a895c61 519 sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object)
544a4582
BV
520{
521 struct source *new_sources, *s;
aac0ea25 522 GPollFD *new_pollfds;
544a4582 523
d08490aa 524 if (!cb) {
a421dc1d 525 sr_err("%s: cb was NULL", __func__);
e0508e67 526 return SR_ERR_ARG;
9f45fb3a
UH
527 }
528
1f9813eb 529 /* Note: cb_data can be NULL, that's not a bug. */
9f45fb3a 530
2ac2e629
BV
531 new_pollfds = g_try_realloc(session->pollfds,
532 sizeof(GPollFD) * (session->num_sources + 1));
0687dfcd 533 if (!new_pollfds) {
a421dc1d 534 sr_err("%s: new_pollfds malloc failed", __func__);
0687dfcd
LPC
535 return SR_ERR_MALLOC;
536 }
537
b7e94111 538 new_sources = g_try_realloc(session->sources, sizeof(struct source) *
2ac2e629 539 (session->num_sources + 1));
9f45fb3a 540 if (!new_sources) {
a421dc1d 541 sr_err("%s: new_sources malloc failed", __func__);
e0508e67 542 return SR_ERR_MALLOC;
9f45fb3a 543 }
544a4582 544
b7e94111
LPC
545 new_pollfds[session->num_sources] = *pollfd;
546 s = &new_sources[session->num_sources++];
544a4582 547 s->timeout = timeout;
d08490aa 548 s->cb = cb;
1f9813eb 549 s->cb_data = cb_data;
aac0ea25 550 s->poll_object = poll_object;
b7e94111
LPC
551 session->pollfds = new_pollfds;
552 session->sources = new_sources;
544a4582 553
b7e94111
LPC
554 if (timeout != session->source_timeout && timeout > 0
555 && (session->source_timeout == -1 || timeout < session->source_timeout))
556 session->source_timeout = timeout;
9f45fb3a 557
e0508e67 558 return SR_OK;
544a4582
BV
559}
560
9f45fb3a 561/**
6b2d8d3e 562 * Add an event source for a file descriptor.
9f45fb3a 563 *
aac0ea25
LPC
564 * @param fd The file descriptor.
565 * @param events Events to check for.
566 * @param timeout Max time to wait before the callback is called, ignored if 0.
567 * @param cb Callback function to add. Must not be NULL.
568 * @param cb_data Data for the callback function. Can be NULL.
9f45fb3a 569 *
aac0ea25
LPC
570 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
571 * SR_ERR_MALLOC upon memory allocation errors.
572 */
573SR_API int sr_session_source_add(int fd, int events, int timeout,
574 sr_receive_data_callback_t cb, void *cb_data)
575{
576 GPollFD p;
577
aac0ea25
LPC
578 p.fd = fd;
579 p.events = events;
aac0ea25
LPC
580
581 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd);
582}
583
584/**
1a895c61 585 * Add an event source for a GPollFD.
aac0ea25 586 *
aac0ea25
LPC
587 * @param pollfd The GPollFD.
588 * @param timeout Max time to wait before the callback is called, ignored if 0.
589 * @param cb Callback function to add. Must not be NULL.
590 * @param cb_data Data for the callback function. Can be NULL.
44dae539 591 *
e0508e67 592 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
aac0ea25 593 * SR_ERR_MALLOC upon memory allocation errors.
9f45fb3a 594 */
aac0ea25
LPC
595SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout,
596 sr_receive_data_callback_t cb, void *cb_data)
597{
1a895c61
UH
598 return _sr_session_source_add(pollfd, timeout, cb,
599 cb_data, (gintptr)pollfd);
aac0ea25
LPC
600}
601
602/**
1a895c61 603 * Add an event source for a GIOChannel.
aac0ea25 604 *
aac0ea25
LPC
605 * @param channel The GIOChannel.
606 * @param events Events to poll on.
607 * @param timeout Max time to wait before the callback is called, ignored if 0.
608 * @param cb Callback function to add. Must not be NULL.
609 * @param cb_data Data for the callback function. Can be NULL.
610 *
611 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
612 * SR_ERR_MALLOC upon memory allocation errors.
613 */
1a895c61
UH
614SR_API int sr_session_source_add_channel(GIOChannel *channel, int events,
615 int timeout, sr_receive_data_callback_t cb, void *cb_data)
aac0ea25
LPC
616{
617 GPollFD p;
618
619#ifdef _WIN32
6b2d8d3e 620 g_io_channel_win32_make_pollfd(channel, events, &p);
aac0ea25
LPC
621#else
622 p.fd = g_io_channel_unix_get_fd(channel);
623 p.events = events;
624#endif
625
626 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel);
627}
628
6b2d8d3e
UH
629/**
630 * Remove the source belonging to the specified channel.
631 *
632 * @todo Add more error checks and logging.
633 *
634 * @param channel The channel for which the source should be removed.
635 *
636 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
637 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
638 * internal errors.
639 */
aac0ea25 640static int _sr_session_source_remove(gintptr poll_object)
544a4582
BV
641{
642 struct source *new_sources;
0687dfcd 643 GPollFD *new_pollfds;
b7e94111 644 unsigned int old;
544a4582 645
b7e94111 646 if (!session->sources || !session->num_sources) {
a421dc1d 647 sr_err("%s: sources was NULL", __func__);
0abee507 648 return SR_ERR_BUG;
e0508e67
UH
649 }
650
b7e94111
LPC
651 for (old = 0; old < session->num_sources; old++) {
652 if (session->sources[old].poll_object == poll_object)
2bccd322 653 break;
9f45fb3a
UH
654 }
655
2bccd322 656 /* fd not found, nothing to do */
b7e94111 657 if (old == session->num_sources)
2bccd322
LPC
658 return SR_OK;
659
b7e94111 660 session->num_sources -= 1;
2bccd322 661
b7e94111
LPC
662 if (old != session->num_sources) {
663 memmove(&session->pollfds[old], &session->pollfds[old+1],
664 (session->num_sources - old) * sizeof(GPollFD));
665 memmove(&session->sources[old], &session->sources[old+1],
666 (session->num_sources - old) * sizeof(struct source));
9f45fb3a 667 }
544a4582 668
b7e94111
LPC
669 new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
670 if (!new_pollfds && session->num_sources > 0) {
a421dc1d 671 sr_err("%s: new_pollfds malloc failed", __func__);
0687dfcd
LPC
672 return SR_ERR_MALLOC;
673 }
674
b7e94111
LPC
675 new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
676 if (!new_sources && session->num_sources > 0) {
a421dc1d 677 sr_err("%s: new_sources malloc failed", __func__);
2bccd322 678 return SR_ERR_MALLOC;
544a4582 679 }
e0508e67 680
b7e94111
LPC
681 session->pollfds = new_pollfds;
682 session->sources = new_sources;
2bccd322 683
e0508e67 684 return SR_OK;
544a4582 685}
aac0ea25 686
6b2d8d3e 687/**
aac0ea25
LPC
688 * Remove the source belonging to the specified file descriptor.
689 *
1a895c61 690 * @param fd The file descriptor for which the source should be removed.
aac0ea25
LPC
691 *
692 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
693 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
694 * internal errors.
695 */
696SR_API int sr_session_source_remove(int fd)
697{
698 return _sr_session_source_remove((gintptr)fd);
699}
700
701/**
702 * Remove the source belonging to the specified poll descriptor.
703 *
aac0ea25
LPC
704 * @param pollfd The poll descriptor for which the source should be removed.
705 *
706 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
707 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
708 * internal errors.
709 */
710SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd)
711{
712 return _sr_session_source_remove((gintptr)pollfd);
713}
714
6b2d8d3e 715/**
aac0ea25
LPC
716 * Remove the source belonging to the specified channel.
717 *
1a895c61 718 * @param channel The channel for which the source should be removed.
aac0ea25
LPC
719 *
720 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
721 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
722 * internal errors.
723 */
724SR_API int sr_session_source_remove_channel(GIOChannel *channel)
725{
726 return _sr_session_source_remove((gintptr)channel);
727}
7b870c38
UH
728
729/** @} */