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