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