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