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