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