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