]> sigrok.org Git - libsigrok.git/blame - session.c
Doxygen: Various improvements in libsigrok.h.
[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 *
9f45fb3a
UH
54 * TODO: Should it use the file-global "session" variable or take an argument?
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
aa4b1107 89 /* TODO: Loop over protocol decoders and free them. */
a1bb33af
UH
90
91 g_free(session);
9f45fb3a 92 session = NULL;
e0508e67
UH
93
94 return SR_OK;
a1bb33af
UH
95}
96
de4d3f99 97static void sr_dev_close(struct sr_dev_inst *sdi)
ed229aaa 98{
e8d3d6c8 99 if (sdi->driver && sdi->driver->dev_close)
de4d3f99 100 sdi->driver->dev_close(sdi);
ed229aaa
LPC
101}
102
9f45fb3a
UH
103/**
104 * Remove all the devices from the current session. TODO?
105 *
106 * The session itself (i.e., the struct sr_session) is not free'd and still
107 * exists after this function returns.
108 *
e0508e67 109 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 110 */
01c3e9db 111SR_API int sr_session_dev_remove_all(void)
a1bb33af 112{
9f45fb3a 113 if (!session) {
133a37bf 114 sr_err("session: %s: session was NULL", __func__);
e0508e67 115 return SR_ERR_BUG;
9f45fb3a
UH
116 }
117
ed229aaa 118 g_slist_free_full(session->devs, (GDestroyNotify)sr_dev_close);
bb7ef793 119 session->devs = NULL;
e0508e67
UH
120
121 return SR_OK;
a1bb33af
UH
122}
123
9f45fb3a 124/**
9c5332d2 125 * Add a device instance to the current session.
9f45fb3a 126 *
9c5332d2 127 * @param sdi The device instance to add to the current session. Must not
de4d3f99
BV
128 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
129 * not be NULL.
9f45fb3a
UH
130 *
131 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
132 */
de4d3f99 133SR_API int sr_session_dev_add(const struct sr_dev_inst *sdi)
a1bb33af
UH
134{
135 int ret;
136
de4d3f99
BV
137 if (!sdi) {
138 sr_err("session: %s: sdi was NULL", __func__);
9f45fb3a
UH
139 return SR_ERR_ARG;
140 }
141
d6eb0c33
UH
142 if (!session) {
143 sr_err("session: %s: session was NULL", __func__);
144 return SR_ERR_BUG;
145 }
146
de4d3f99
BV
147 /* If sdi->driver is NULL, this is a virtual device. */
148 if (!sdi->driver) {
149 sr_dbg("session: %s: sdi->driver was NULL, this seems to be "
d6eb0c33
UH
150 "a virtual device; continuing", __func__);
151 /* Just add the device, don't run dev_open(). */
de4d3f99 152 session->devs = g_slist_append(session->devs, (gpointer)sdi);
d6eb0c33 153 return SR_OK;
9f45fb3a
UH
154 }
155
de4d3f99
BV
156 /* sdi->driver is non-NULL (i.e. we have a real device). */
157 if (!sdi->driver->dev_open) {
158 sr_err("session: %s: sdi->driver->dev_open was NULL", __func__);
8ec95d22 159 return SR_ERR_BUG;
9f45fb3a
UH
160 }
161
de4d3f99 162 if ((ret = sdi->driver->dev_open((struct sr_dev_inst *)sdi)) != SR_OK) {
e7eb703f 163 sr_err("session: %s: dev_open failed (%d)", __func__, ret);
9f45fb3a 164 return ret;
aa4b1107 165 }
a1bb33af 166
de4d3f99 167 session->devs = g_slist_append(session->devs, (gpointer)sdi);
aa4b1107 168
e46b8fb1 169 return SR_OK;
a1bb33af
UH
170}
171
9f45fb3a 172/**
01c3e9db 173 * Remove all datafeed callbacks in the current session.
9f45fb3a 174 *
e0508e67 175 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 176 */
01c3e9db 177SR_API int sr_session_datafeed_callback_remove_all(void)
a1bb33af 178{
9f45fb3a
UH
179 if (!session) {
180 sr_err("session: %s: session was NULL", __func__);
e0508e67 181 return SR_ERR_BUG;
9f45fb3a
UH
182 }
183
a1bb33af
UH
184 g_slist_free(session->datafeed_callbacks);
185 session->datafeed_callbacks = NULL;
e0508e67
UH
186
187 return SR_OK;
a1bb33af
UH
188}
189
9f45fb3a
UH
190/**
191 * Add a datafeed callback to the current session.
192 *
d08490aa 193 * @param cb Function to call when a chunk of data is received.
0abee507 194 * Must not be NULL.
a1645fcd 195 *
e0508e67 196 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 197 */
d08490aa 198SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
a1bb33af 199{
9f45fb3a
UH
200 if (!session) {
201 sr_err("session: %s: session was NULL", __func__);
e0508e67 202 return SR_ERR_BUG;
9f45fb3a
UH
203 }
204
0abee507
UH
205 if (!cb) {
206 sr_err("session: %s: cb was NULL", __func__);
207 return SR_ERR_ARG;
208 }
9f45fb3a 209
62c82025 210 session->datafeed_callbacks =
d08490aa 211 g_slist_append(session->datafeed_callbacks, cb);
e0508e67
UH
212
213 return SR_OK;
a1bb33af
UH
214}
215
e0508e67 216static int sr_session_run_poll(void)
544a4582 217{
b7e94111
LPC
218 unsigned int i;
219 int ret;
544a4582 220
2cbeb2b7
BV
221 while (session->num_sources > 0) {
222 ret = g_poll(session->pollfds, session->num_sources,
223 session->source_timeout);
b7e94111
LPC
224 for (i = 0; i < session->num_sources; i++) {
225 if (session->pollfds[i].revents > 0 || (ret == 0
226 && session->source_timeout == session->sources[i].timeout)) {
544a4582
BV
227 /*
228 * Invoke the source's callback on an event,
2cbeb2b7 229 * or if the poll timed out and this source
544a4582
BV
230 * asked for that timeout.
231 */
2cbeb2b7
BV
232 if (!session->sources[i].cb(session->pollfds[i].fd,
233 session->pollfds[i].revents,
234 session->sources[i].cb_data))
b7e94111 235 sr_session_source_remove(session->sources[i].poll_object);
544a4582
BV
236 }
237 }
238 }
e0508e67
UH
239
240 return SR_OK;
544a4582
BV
241}
242
9f45fb3a
UH
243/**
244 * Start a session.
245 *
a1645fcd 246 * There can only be one session at a time.
9f45fb3a
UH
247 *
248 * @return SR_OK upon success, SR_ERR upon errors.
249 */
1a081ca6 250SR_API int sr_session_start(void)
7d658874 251{
de4d3f99 252 struct sr_dev_inst *sdi;
7d658874
BV
253 GSList *l;
254 int ret;
255
9f45fb3a
UH
256 if (!session) {
257 sr_err("session: %s: session was NULL; a session must be "
de4d3f99 258 "created before starting it.", __func__);
0abee507 259 return SR_ERR_BUG;
9f45fb3a
UH
260 }
261
bb7ef793 262 if (!session->devs) {
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
b08024a8 268 sr_info("session: starting");
9f45fb3a 269
bb7ef793 270 for (l = session->devs; l; l = l->next) {
de4d3f99
BV
271 sdi = l->data;
272 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
446a0372 273 sr_err("session: %s: could not start an acquisition "
9f45fb3a 274 "(%d)", __func__, ret);
7d658874 275 break;
9f45fb3a 276 }
7d658874
BV
277 }
278
9f45fb3a
UH
279 /* TODO: What if there are multiple devices? Which return code? */
280
7d658874
BV
281 return ret;
282}
283
9f45fb3a
UH
284/**
285 * Run the session.
286 *
9f45fb3a
UH
287 * TODO: Various error checks etc.
288 *
e0508e67 289 * @return SR_OK upon success, SR_ERR_BUG upon errors.
9f45fb3a 290 */
1a081ca6 291SR_API int sr_session_run(void)
7d658874 292{
9f45fb3a
UH
293 if (!session) {
294 sr_err("session: %s: session was NULL; a session must be "
295 "created first, before running it.", __func__);
e0508e67 296 return SR_ERR_BUG;
9f45fb3a
UH
297 }
298
bb7ef793 299 if (!session->devs) {
9f45fb3a 300 /* TODO: Actually the case? */
bb7ef793 301 sr_err("session: %s: session->devs was NULL; a session "
9f45fb3a 302 "cannot be run without devices.", __func__);
e0508e67 303 return SR_ERR_BUG;
9f45fb3a
UH
304 }
305
b08024a8 306 sr_info("session: running");
7d658874 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. */
2cbeb2b7 311 while (session->num_sources)
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{
de4d3f99 345 struct sr_dev_inst *sdi;
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");
e0508e67 354
bb7ef793 355 for (l = session->devs; l; l = l->next) {
de4d3f99
BV
356 sdi = l->data;
357 if (sdi->driver) {
358 if (sdi->driver->dev_acquisition_stop)
359 sdi->driver->dev_acquisition_stop(sdi, sdi);
8c76be53 360 }
a1bb33af 361 }
9f45fb3a 362
e0508e67 363 return SR_OK;
a1bb33af
UH
364}
365
9f45fb3a 366/**
a1645fcd 367 * Debug helper.
9f45fb3a 368 *
996b0c72 369 * @param packet The packet to show debugging information for.
9f45fb3a 370 */
18beaeff 371static void datafeed_dump(struct sr_datafeed_packet *packet)
7d2afd6c
BV
372{
373 struct sr_datafeed_logic *logic;
ee7489d2 374 struct sr_datafeed_analog *analog;
7d2afd6c
BV
375
376 switch (packet->type) {
377 case SR_DF_HEADER:
378 sr_dbg("bus: received SR_DF_HEADER");
379 break;
380 case SR_DF_TRIGGER:
01469707 381 sr_dbg("bus: received SR_DF_TRIGGER");
7d2afd6c 382 break;
ee7489d2
BV
383 case SR_DF_META_LOGIC:
384 sr_dbg("bus: received SR_DF_META_LOGIC");
385 break;
7d2afd6c
BV
386 case SR_DF_LOGIC:
387 logic = packet->payload;
e0508e67 388 /* TODO: Check for logic != NULL. */
01469707 389 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
7d2afd6c 390 break;
ee7489d2 391 case SR_DF_META_ANALOG:
dfd8f56e 392 sr_dbg("bus: received SR_DF_META_ANALOG");
ee7489d2
BV
393 break;
394 case SR_DF_ANALOG:
395 analog = packet->payload;
396 /* TODO: Check for analog != NULL. */
397 sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples);
398 break;
7d2afd6c
BV
399 case SR_DF_END:
400 sr_dbg("bus: received SR_DF_END");
401 break;
6ea7669c
BV
402 case SR_DF_FRAME_BEGIN:
403 sr_dbg("bus: received SR_DF_FRAME_BEGIN");
404 break;
405 case SR_DF_FRAME_END:
406 sr_dbg("bus: received SR_DF_FRAME_END");
407 break;
7d2afd6c 408 default:
18beaeff 409 sr_dbg("bus: received unknown packet type %d", packet->type);
9f45fb3a 410 break;
7d2afd6c 411 }
7d2afd6c
BV
412}
413
9f45fb3a 414/**
a1645fcd
BV
415 * Send a packet to whatever is listening on the datafeed bus.
416 *
417 * Hardware drivers use this to send a data packet to the frontend.
9f45fb3a 418 *
bb7ef793 419 * @param dev TODO.
31ccebc4 420 * @param packet The datafeed packet to send to the session bus.
44dae539 421 *
e0508e67 422 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
b4bd7088
UH
423 *
424 * @private
9f45fb3a 425 */
de4d3f99 426SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
31ccebc4 427 struct sr_datafeed_packet *packet)
a1bb33af
UH
428{
429 GSList *l;
d08490aa 430 sr_datafeed_callback_t cb;
a1bb33af 431
de4d3f99
BV
432 if (!sdi) {
433 sr_err("session: %s: sdi 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;
de4d3f99 446 cb(sdi, packet);
a1bb33af 447 }
9f45fb3a 448
e0508e67 449 return SR_OK;
a1bb33af
UH
450}
451
aac0ea25 452static int _sr_session_source_add(GPollFD *pollfd, int timeout,
1a895c61 453 sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object)
544a4582
BV
454{
455 struct source *new_sources, *s;
aac0ea25 456 GPollFD *new_pollfds;
544a4582 457
d08490aa
UH
458 if (!cb) {
459 sr_err("session: %s: cb was NULL", __func__);
e0508e67 460 return SR_ERR_ARG;
9f45fb3a
UH
461 }
462
1f9813eb 463 /* Note: cb_data can be NULL, that's not a bug. */
9f45fb3a 464
2ac2e629
BV
465 new_pollfds = g_try_realloc(session->pollfds,
466 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 472 new_sources = g_try_realloc(session->sources, sizeof(struct source) *
2ac2e629 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}
7b870c38
UH
663
664/** @} */