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