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