]> sigrok.org Git - libsigrok.git/blob - src/session.c
Doxygen: Add a few missing @param lines for sessions.
[libsigrok.git] / src / session.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
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>
24 #include <glib.h>
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
27
28 /** @cond PRIVATE */
29 #define LOG_PREFIX "session"
30 /** @endcond */
31
32 /**
33  * @file
34  *
35  * Creating, using, or destroying libsigrok sessions.
36  */
37
38 /**
39  * @defgroup grp_session Session handling
40  *
41  * Creating, using, or destroying libsigrok sessions.
42  *
43  * @{
44  */
45
46 struct source {
47         int timeout;
48         sr_receive_data_callback cb;
49         void *cb_data;
50
51         /* This is used to keep track of the object (fd, pollfd or channel) which is
52          * being polled and will be used to match the source when removing it again.
53          */
54         gintptr poll_object;
55 };
56
57 struct datafeed_callback {
58         sr_datafeed_callback cb;
59         void *cb_data;
60 };
61
62 /**
63  * Create a new session.
64  *
65  * @param new_session This will contain a pointer to the newly created
66  *                    session if the return value is SR_OK, otherwise the value
67  *                    is undefined and should not be used. Must not be NULL.
68  *
69  * @retval SR_OK Success.
70  * @retval SR_ERR_BUG A session exists already.
71  *
72  * @since 0.4.0
73  */
74 SR_API int sr_session_new(struct sr_session **new_session)
75 {
76         struct sr_session *session;
77
78         session = g_malloc0(sizeof(struct sr_session));
79
80         session->source_timeout = -1;
81         session->running = FALSE;
82         session->abort_session = FALSE;
83         g_mutex_init(&session->stop_mutex);
84
85         *new_session = session;
86
87         return SR_OK;
88 }
89
90 /**
91  * Destroy a session.
92  * This frees up all memory used by the session.
93  *
94  * @param session The session to destroy. Must not be NULL.
95  *
96  * @retval SR_OK Success.
97  * @retval SR_ERR_ARG Invalid session passed.
98  *
99  * @since 0.4.0
100  */
101 SR_API int sr_session_destroy(struct sr_session *session)
102 {
103         if (!session) {
104                 sr_err("%s: session was NULL", __func__);
105                 return SR_ERR_ARG;
106         }
107
108         sr_session_dev_remove_all(session);
109         g_mutex_clear(&session->stop_mutex);
110         if (session->trigger)
111                 sr_trigger_free(session->trigger);
112
113         g_free(session);
114
115         return SR_OK;
116 }
117
118 /**
119  * Remove all the devices from a session.
120  *
121  * The session itself (i.e., the struct sr_session) is not free'd and still
122  * exists after this function returns.
123  *
124  * @param session The session to use. Must not be NULL.
125  *
126  * @retval SR_OK Success.
127  * @retval SR_ERR_BUG Invalid session passed.
128  *
129  * @since 0.4.0
130  */
131 SR_API int sr_session_dev_remove_all(struct sr_session *session)
132 {
133         struct sr_dev_inst *sdi;
134         GSList *l;
135
136         if (!session) {
137                 sr_err("%s: session was NULL", __func__);
138                 return SR_ERR_ARG;
139         }
140
141         for (l = session->devs; l; l = l->next) {
142                 sdi = (struct sr_dev_inst *) l->data;
143                 sdi->session = NULL;
144         }
145
146         g_slist_free(session->devs);
147         session->devs = NULL;
148
149         return SR_OK;
150 }
151
152 /**
153  * Add a device instance to a session.
154  *
155  * @param session The session to add to. Must not be NULL.
156  * @param sdi The device instance to add to a session. Must not
157  *            be NULL. Also, sdi->driver and sdi->driver->dev_open must
158  *            not be NULL.
159  *
160  * @retval SR_OK Success.
161  * @retval SR_ERR_ARG Invalid argument.
162  *
163  * @since 0.4.0
164  */
165 SR_API int sr_session_dev_add(struct sr_session *session,
166                 struct sr_dev_inst *sdi)
167 {
168         int ret;
169
170         if (!sdi) {
171                 sr_err("%s: sdi was NULL", __func__);
172                 return SR_ERR_ARG;
173         }
174
175         if (!session) {
176                 sr_err("%s: session was NULL", __func__);
177                 return SR_ERR_ARG;
178         }
179
180         /* If sdi->session is not NULL, the device is already in this or
181          * another session. */
182         if (sdi->session) {
183                 sr_err("%s: already assigned to session", __func__);
184                 return SR_ERR_ARG;
185         }
186
187         /* If sdi->driver is NULL, this is a virtual device. */
188         if (!sdi->driver) {
189                 sr_dbg("%s: sdi->driver was NULL, this seems to be "
190                        "a virtual device; continuing", __func__);
191                 /* Just add the device, don't run dev_open(). */
192                 session->devs = g_slist_append(session->devs, (gpointer)sdi);
193                 sdi->session = session;
194                 return SR_OK;
195         }
196
197         /* sdi->driver is non-NULL (i.e. we have a real device). */
198         if (!sdi->driver->dev_open) {
199                 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
200                 return SR_ERR_BUG;
201         }
202
203         session->devs = g_slist_append(session->devs, (gpointer)sdi);
204         sdi->session = session;
205
206         if (session->running) {
207                 /* Adding a device to a running session. Commit settings
208                  * and start acquisition on that device now. */
209                 if ((ret = sr_config_commit(sdi)) != SR_OK) {
210                         sr_err("Failed to commit device settings before "
211                                "starting acquisition in running session (%s)",
212                                sr_strerror(ret));
213                         return ret;
214                 }
215                 if ((ret = sdi->driver->dev_acquisition_start(sdi,
216                                                 (void *)sdi)) != SR_OK) {
217                         sr_err("Failed to start acquisition of device in "
218                                "running session (%s)", sr_strerror(ret));
219                         return ret;
220                 }
221         }
222
223         return SR_OK;
224 }
225
226 /**
227  * List all device instances attached to a session.
228  *
229  * @param session The session to use. Must not be NULL.
230  * @param devlist A pointer where the device instance list will be
231  *                stored on return. If no devices are in the session,
232  *                this will be NULL. Each element in the list points
233  *                to a struct sr_dev_inst *.
234  *                The list must be freed by the caller, but not the
235  *                elements pointed to.
236  *
237  * @retval SR_OK Success.
238  * @retval SR_ERR_ARG Invalid argument.
239  *
240  * @since 0.4.0
241  */
242 SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist)
243 {
244         if (!session)
245                 return SR_ERR_ARG;
246
247         if (!devlist)
248                 return SR_ERR_ARG;
249
250         *devlist = g_slist_copy(session->devs);
251
252         return SR_OK;
253 }
254
255 /**
256  * Remove all datafeed callbacks in a session.
257  *
258  * @param session The session to use. Must not be NULL.
259  *
260  * @retval SR_OK Success.
261  * @retval SR_ERR_ARG Invalid session passed.
262  *
263  * @since 0.4.0
264  */
265 SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session)
266 {
267         if (!session) {
268                 sr_err("%s: session was NULL", __func__);
269                 return SR_ERR_ARG;
270         }
271
272         g_slist_free_full(session->datafeed_callbacks, g_free);
273         session->datafeed_callbacks = NULL;
274
275         return SR_OK;
276 }
277
278 /**
279  * Add a datafeed callback to a session.
280  *
281  * @param session The session to use. Must not be NULL.
282  * @param cb Function to call when a chunk of data is received.
283  *           Must not be NULL.
284  * @param cb_data Opaque pointer passed in by the caller.
285  *
286  * @retval SR_OK Success.
287  * @retval SR_ERR_BUG No session exists.
288  *
289  * @since 0.3.0
290  */
291 SR_API int sr_session_datafeed_callback_add(struct sr_session *session,
292                 sr_datafeed_callback cb, void *cb_data)
293 {
294         struct datafeed_callback *cb_struct;
295
296         if (!session) {
297                 sr_err("%s: session was NULL", __func__);
298                 return SR_ERR_BUG;
299         }
300
301         if (!cb) {
302                 sr_err("%s: cb was NULL", __func__);
303                 return SR_ERR_ARG;
304         }
305
306         if (!(cb_struct = g_try_malloc0(sizeof(struct datafeed_callback))))
307                 return SR_ERR_MALLOC;
308
309         cb_struct->cb = cb;
310         cb_struct->cb_data = cb_data;
311
312         session->datafeed_callbacks =
313             g_slist_append(session->datafeed_callbacks, cb_struct);
314
315         return SR_OK;
316 }
317
318 SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session)
319 {
320         return session->trigger;
321 }
322
323 SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig)
324 {
325         session->trigger = trig;
326
327         return SR_OK;
328 }
329
330 /**
331  * Call every device in the current session's callback.
332  *
333  * For sessions not driven by select loops such as sr_session_run(),
334  * but driven by another scheduler, this can be used to poll the devices
335  * from within that scheduler.
336  *
337  * @param session The session to use. Must not be NULL.
338  * @param block If TRUE, this call will wait for any of the session's
339  *              sources to fire an event on the file descriptors, or
340  *              any of their timeouts to activate. In other words, this
341  *              can be used as a select loop.
342  *              If FALSE, all sources have their callback run, regardless
343  *              of file descriptor or timeout status.
344  *
345  * @retval SR_OK Success.
346  * @retval SR_ERR Error occured.
347  */
348 static int sr_session_iteration(struct sr_session *session, gboolean block)
349 {
350         unsigned int i;
351         int ret;
352
353         ret = g_poll(session->pollfds, session->num_sources,
354                         block ? session->source_timeout : 0);
355         for (i = 0; i < session->num_sources; i++) {
356                 if (session->pollfds[i].revents > 0 || (ret == 0
357                         && session->source_timeout == session->sources[i].timeout)) {
358                         /*
359                          * Invoke the source's callback on an event,
360                          * or if the poll timed out and this source
361                          * asked for that timeout.
362                          */
363                         if (!session->sources[i].cb(session->pollfds[i].fd,
364                                         session->pollfds[i].revents,
365                                         session->sources[i].cb_data))
366                                 sr_session_source_remove(session,
367                                                 session->sources[i].poll_object);
368                 }
369                 /*
370                  * We want to take as little time as possible to stop
371                  * the session if we have been told to do so. Therefore,
372                  * we check the flag after processing every source, not
373                  * just once per main event loop.
374                  */
375                 g_mutex_lock(&session->stop_mutex);
376                 if (session->abort_session) {
377                         sr_session_stop_sync(session);
378                         /* But once is enough. */
379                         session->abort_session = FALSE;
380                 }
381                 g_mutex_unlock(&session->stop_mutex);
382         }
383
384         return SR_OK;
385 }
386
387
388 static int verify_trigger(struct sr_trigger *trigger)
389 {
390         struct sr_trigger_stage *stage;
391         struct sr_trigger_match *match;
392         GSList *l, *m;
393
394         if (!trigger->stages) {
395                 sr_err("No trigger stages defined.");
396                 return SR_ERR;
397         }
398
399         sr_spew("Checking trigger:");
400         for (l = trigger->stages; l; l = l->next) {
401                 stage = l->data;
402                 if (!stage->matches) {
403                         sr_err("Stage %d has no matches defined.", stage->stage);
404                         return SR_ERR;
405                 }
406                 for (m = stage->matches; m; m = m->next) {
407                         match = m->data;
408                         if (!match->channel) {
409                                 sr_err("Stage %d match has no channel.", stage->stage);
410                                 return SR_ERR;
411                         }
412                         if (!match->match) {
413                                 sr_err("Stage %d match is not defined.", stage->stage);
414                                 return SR_ERR;
415                         }
416                         sr_spew("Stage %d match on channel %s, match %d", stage->stage,
417                                         match->channel->name, match->match);
418                 }
419         }
420
421         return SR_OK;
422 }
423 /**
424  * Start a session.
425  *
426  * @param session The session to use. Must not be NULL.
427  *
428  * @retval SR_OK Success.
429  * @retval SR_ERR_ARG Invalid session passed.
430  *
431  * @since 0.4.0
432  */
433 SR_API int sr_session_start(struct sr_session *session)
434 {
435         struct sr_dev_inst *sdi;
436         GSList *l;
437         int ret;
438
439         if (!session) {
440                 sr_err("%s: session was NULL", __func__);
441                 return SR_ERR_ARG;
442         }
443
444         if (!session->devs) {
445                 sr_err("%s: session->devs was NULL; a session "
446                        "cannot be started without devices.", __func__);
447                 return SR_ERR_ARG;
448         }
449
450         if (session->trigger && verify_trigger(session->trigger) != SR_OK)
451                 return SR_ERR;
452
453         sr_info("Starting.");
454
455         ret = SR_OK;
456         for (l = session->devs; l; l = l->next) {
457                 sdi = l->data;
458                 if ((ret = sr_config_commit(sdi)) != SR_OK) {
459                         sr_err("Failed to commit device settings before "
460                                "starting acquisition (%s)", sr_strerror(ret));
461                         break;
462                 }
463                 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
464                         sr_err("%s: could not start an acquisition "
465                                "(%s)", __func__, sr_strerror(ret));
466                         break;
467                 }
468         }
469
470         /* TODO: What if there are multiple devices? Which return code? */
471
472         return ret;
473 }
474
475 /**
476  * Run a session.
477  *
478  * @param session The session to use. Must not be NULL.
479  *
480  * @retval SR_OK Success.
481  * @retval SR_ERR_ARG Invalid session passed.
482  *
483  * @since 0.4.0
484  */
485 SR_API int sr_session_run(struct sr_session *session)
486 {
487         if (!session) {
488                 sr_err("%s: session was NULL", __func__);
489                 return SR_ERR_ARG;
490         }
491
492         if (!session->devs) {
493                 /* TODO: Actually the case? */
494                 sr_err("%s: session->devs was NULL; a session "
495                        "cannot be run without devices.", __func__);
496                 return SR_ERR_ARG;
497         }
498         session->running = TRUE;
499
500         sr_info("Running.");
501
502         /* Do we have real sources? */
503         if (session->num_sources == 1 && session->pollfds[0].fd == -1) {
504                 /* Dummy source, freewheel over it. */
505                 while (session->num_sources)
506                         session->sources[0].cb(-1, 0, session->sources[0].cb_data);
507         } else {
508                 /* Real sources, use g_poll() main loop. */
509                 while (session->num_sources)
510                         sr_session_iteration(session, TRUE);
511         }
512
513         return SR_OK;
514 }
515
516 /**
517  * Stop a session.
518  *
519  * The session is stopped immediately, with all acquisition sessions stopped
520  * and hardware drivers cleaned up.
521  *
522  * This must be called from within the session thread, to prevent freeing
523  * resources that the session thread will try to use.
524  *
525  * @param session The session to use. Must not be NULL.
526  *
527  * @retval SR_OK Success.
528  * @retval SR_ERR_ARG Invalid session passed.
529  *
530  * @private
531  */
532 SR_PRIV int sr_session_stop_sync(struct sr_session *session)
533 {
534         struct sr_dev_inst *sdi;
535         GSList *l;
536
537         if (!session) {
538                 sr_err("%s: session was NULL", __func__);
539                 return SR_ERR_ARG;
540         }
541
542         sr_info("Stopping.");
543
544         for (l = session->devs; l; l = l->next) {
545                 sdi = l->data;
546                 if (sdi->driver) {
547                         if (sdi->driver->dev_acquisition_stop)
548                                 sdi->driver->dev_acquisition_stop(sdi, sdi);
549                 }
550         }
551         session->running = FALSE;
552
553         return SR_OK;
554 }
555
556 /**
557  * Stop a session.
558  *
559  * The session is stopped immediately, with all acquisition sessions being
560  * stopped and hardware drivers cleaned up.
561  *
562  * If the session is run in a separate thread, this function will not block
563  * until the session is finished executing. It is the caller's responsibility
564  * to wait for the session thread to return before assuming that the session is
565  * completely decommissioned.
566  *
567  * @param session The session to use. Must not be NULL.
568  *
569  * @retval SR_OK Success.
570  * @retval SR_ERR_ARG Invalid session passed.
571  *
572  * @since 0.4.0
573  */
574 SR_API int sr_session_stop(struct sr_session *session)
575 {
576         if (!session) {
577                 sr_err("%s: session was NULL", __func__);
578                 return SR_ERR_BUG;
579         }
580
581         g_mutex_lock(&session->stop_mutex);
582         session->abort_session = TRUE;
583         g_mutex_unlock(&session->stop_mutex);
584
585         return SR_OK;
586 }
587
588 /**
589  * Debug helper.
590  *
591  * @param packet The packet to show debugging information for.
592  */
593 static void datafeed_dump(const struct sr_datafeed_packet *packet)
594 {
595         const struct sr_datafeed_logic *logic;
596         const struct sr_datafeed_analog *analog;
597
598         switch (packet->type) {
599         case SR_DF_HEADER:
600                 sr_dbg("bus: Received SR_DF_HEADER packet.");
601                 break;
602         case SR_DF_TRIGGER:
603                 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
604                 break;
605         case SR_DF_META:
606                 sr_dbg("bus: Received SR_DF_META packet.");
607                 break;
608         case SR_DF_LOGIC:
609                 logic = packet->payload;
610                 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, "
611                        "unitsize = %d).", logic->length, logic->unitsize);
612                 break;
613         case SR_DF_ANALOG:
614                 analog = packet->payload;
615                 sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).",
616                        analog->num_samples);
617                 break;
618         case SR_DF_END:
619                 sr_dbg("bus: Received SR_DF_END packet.");
620                 break;
621         case SR_DF_FRAME_BEGIN:
622                 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
623                 break;
624         case SR_DF_FRAME_END:
625                 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
626                 break;
627         default:
628                 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
629                 break;
630         }
631 }
632
633 /**
634  * Send a packet to whatever is listening on the datafeed bus.
635  *
636  * Hardware drivers use this to send a data packet to the frontend.
637  *
638  * @param sdi TODO.
639  * @param packet The datafeed packet to send to the session bus.
640  *
641  * @retval SR_OK Success.
642  * @retval SR_ERR_ARG Invalid argument.
643  *
644  * @private
645  */
646 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
647                             const struct sr_datafeed_packet *packet)
648 {
649         GSList *l;
650         struct datafeed_callback *cb_struct;
651
652         if (!sdi) {
653                 sr_err("%s: sdi was NULL", __func__);
654                 return SR_ERR_ARG;
655         }
656
657         if (!packet) {
658                 sr_err("%s: packet was NULL", __func__);
659                 return SR_ERR_ARG;
660         }
661
662         for (l = sdi->session->datafeed_callbacks; l; l = l->next) {
663                 if (sr_log_loglevel_get() >= SR_LOG_DBG)
664                         datafeed_dump(packet);
665                 cb_struct = l->data;
666                 cb_struct->cb(sdi, packet, cb_struct->cb_data);
667         }
668
669         return SR_OK;
670 }
671
672 /**
673  * Add an event source for a file descriptor.
674  *
675  * @param session The session to use. Must not be NULL.
676  * @param pollfd The GPollFD.
677  * @param[in] timeout Max time to wait before the callback is called,
678  *              ignored if 0.
679  * @param cb Callback function to add. Must not be NULL.
680  * @param cb_data Data for the callback function. Can be NULL.
681  * @param poll_object TODO.
682  *
683  * @retval SR_OK Success.
684  * @retval SR_ERR_ARG Invalid argument.
685  * @retval SR_ERR_MALLOC Memory allocation error.
686  */
687 static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd,
688                 int timeout, sr_receive_data_callback cb, void *cb_data, gintptr poll_object)
689 {
690         struct source *new_sources, *s;
691         GPollFD *new_pollfds;
692
693         if (!cb) {
694                 sr_err("%s: cb was NULL", __func__);
695                 return SR_ERR_ARG;
696         }
697
698         /* Note: cb_data can be NULL, that's not a bug. */
699
700         new_pollfds = g_try_realloc(session->pollfds,
701                         sizeof(GPollFD) * (session->num_sources + 1));
702         if (!new_pollfds) {
703                 sr_err("%s: new_pollfds malloc failed", __func__);
704                 return SR_ERR_MALLOC;
705         }
706
707         new_sources = g_try_realloc(session->sources, sizeof(struct source) *
708                         (session->num_sources + 1));
709         if (!new_sources) {
710                 sr_err("%s: new_sources malloc failed", __func__);
711                 return SR_ERR_MALLOC;
712         }
713
714         new_pollfds[session->num_sources] = *pollfd;
715         s = &new_sources[session->num_sources++];
716         s->timeout = timeout;
717         s->cb = cb;
718         s->cb_data = cb_data;
719         s->poll_object = poll_object;
720         session->pollfds = new_pollfds;
721         session->sources = new_sources;
722
723         if (timeout != session->source_timeout && timeout > 0
724             && (session->source_timeout == -1 || timeout < session->source_timeout))
725                 session->source_timeout = timeout;
726
727         return SR_OK;
728 }
729
730 /**
731  * Add an event source for a file descriptor.
732  *
733  * @param session The session to use. Must not be NULL.
734  * @param fd The file descriptor.
735  * @param events Events to check for.
736  * @param timeout Max time to wait before the callback is called, ignored if 0.
737  * @param cb Callback function to add. Must not be NULL.
738  * @param cb_data Data for the callback function. Can be NULL.
739  *
740  * @retval SR_OK Success.
741  * @retval SR_ERR_ARG Invalid argument.
742  * @retval SR_ERR_MALLOC Memory allocation error.
743  *
744  * @since 0.3.0
745  */
746 SR_API int sr_session_source_add(struct sr_session *session, int fd,
747                 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
748 {
749         GPollFD p;
750
751         p.fd = fd;
752         p.events = events;
753
754         return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)fd);
755 }
756
757 /**
758  * Add an event source for a GPollFD.
759  *
760  * @param session The session to use. Must not be NULL.
761  * @param pollfd The GPollFD.
762  * @param timeout Max time to wait before the callback is called, ignored if 0.
763  * @param cb Callback function to add. Must not be NULL.
764  * @param cb_data Data for the callback function. Can be NULL.
765  *
766  * @retval SR_OK Success.
767  * @retval SR_ERR_ARG Invalid argument.
768  * @retval SR_ERR_MALLOC Memory allocation error.
769  *
770  * @since 0.3.0
771  */
772 SR_API int sr_session_source_add_pollfd(struct sr_session *session,
773                 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
774                 void *cb_data)
775 {
776         return _sr_session_source_add(session, pollfd, timeout, cb,
777                                       cb_data, (gintptr)pollfd);
778 }
779
780 /**
781  * Add an event source for a GIOChannel.
782  *
783  * @param session The session to use. Must not be NULL.
784  * @param channel The GIOChannel.
785  * @param events Events to poll on.
786  * @param timeout Max time to wait before the callback is called, ignored if 0.
787  * @param cb Callback function to add. Must not be NULL.
788  * @param cb_data Data for the callback function. Can be NULL.
789  *
790  * @retval SR_OK Success.
791  * @retval SR_ERR_ARG Invalid argument.
792  * @retval SR_ERR_MALLOC Memory allocation error.
793  *
794  * @since 0.3.0
795  */
796 SR_API int sr_session_source_add_channel(struct sr_session *session,
797                 GIOChannel *channel, int events, int timeout,
798                 sr_receive_data_callback cb, void *cb_data)
799 {
800         GPollFD p;
801
802 #ifdef _WIN32
803         g_io_channel_win32_make_pollfd(channel, events, &p);
804 #else
805         p.fd = g_io_channel_unix_get_fd(channel);
806         p.events = events;
807 #endif
808
809         return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)channel);
810 }
811
812 /**
813  * Remove the source belonging to the specified channel.
814  *
815  * @todo Add more error checks and logging.
816  *
817  * @param session The session to use. Must not be NULL.
818  * @param poll_object The channel for which the source should be removed.
819  *
820  * @retval SR_OK Success
821  * @retval SR_ERR_ARG Invalid arguments
822  * @retval SR_ERR_MALLOC Memory allocation error
823  * @retval SR_ERR_BUG Internal error
824  */
825 static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object)
826 {
827         struct source *new_sources;
828         GPollFD *new_pollfds;
829         unsigned int old;
830
831         if (!session->sources || !session->num_sources) {
832                 sr_err("%s: sources was NULL", __func__);
833                 return SR_ERR_BUG;
834         }
835
836         for (old = 0; old < session->num_sources; old++) {
837                 if (session->sources[old].poll_object == poll_object)
838                         break;
839         }
840
841         /* fd not found, nothing to do */
842         if (old == session->num_sources)
843                 return SR_OK;
844
845         session->num_sources -= 1;
846
847         if (old != session->num_sources) {
848                 memmove(&session->pollfds[old], &session->pollfds[old+1],
849                         (session->num_sources - old) * sizeof(GPollFD));
850                 memmove(&session->sources[old], &session->sources[old+1],
851                         (session->num_sources - old) * sizeof(struct source));
852         }
853
854         new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
855         if (!new_pollfds && session->num_sources > 0) {
856                 sr_err("%s: new_pollfds malloc failed", __func__);
857                 return SR_ERR_MALLOC;
858         }
859
860         new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
861         if (!new_sources && session->num_sources > 0) {
862                 sr_err("%s: new_sources malloc failed", __func__);
863                 return SR_ERR_MALLOC;
864         }
865
866         session->pollfds = new_pollfds;
867         session->sources = new_sources;
868
869         return SR_OK;
870 }
871
872 /**
873  * Remove the source belonging to the specified file descriptor.
874  *
875  * @param session The session to use. Must not be NULL.
876  * @param fd The file descriptor for which the source should be removed.
877  *
878  * @retval SR_OK Success
879  * @retval SR_ERR_ARG Invalid argument
880  * @retval SR_ERR_MALLOC Memory allocation error.
881  * @retval SR_ERR_BUG Internal error.
882  *
883  * @since 0.3.0
884  */
885 SR_API int sr_session_source_remove(struct sr_session *session, int fd)
886 {
887         return _sr_session_source_remove(session, (gintptr)fd);
888 }
889
890 /**
891  * Remove the source belonging to the specified poll descriptor.
892  *
893  * @param session The session to use. Must not be NULL.
894  * @param pollfd The poll descriptor for which the source should be removed.
895  *
896  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
897  *         SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
898  *         internal errors.
899  *
900  * @since 0.2.0
901  */
902 SR_API int sr_session_source_remove_pollfd(struct sr_session *session,
903                 GPollFD *pollfd)
904 {
905         return _sr_session_source_remove(session, (gintptr)pollfd);
906 }
907
908 /**
909  * Remove the source belonging to the specified channel.
910  *
911  * @param session The session to use. Must not be NULL.
912  * @param channel The channel for which the source should be removed.
913  *
914  * @retval SR_OK Success.
915  * @retval SR_ERR_ARG Invalid argument.
916  * @retval SR_ERR_MALLOC Memory allocation error.
917  * @return SR_ERR_BUG Internal error.
918  *
919  * @since 0.2.0
920  */
921 SR_API int sr_session_source_remove_channel(struct sr_session *session,
922                 GIOChannel *channel)
923 {
924         return _sr_session_source_remove(session, (gintptr)channel);
925 }
926
927 /** @} */