Update copyright dates
[src/app-framework-binder.git] / src / fdev-systemd.c
1 /*
2  * Copyright (C) 2015-2020 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <errno.h>
19
20 #include <systemd/sd-event.h>
21
22 #define FDEV_PROVIDER
23 #include "fdev.h"
24 #include "fdev-systemd.h"
25
26 static int handler(sd_event_source *s, int fd, uint32_t revents, void *userdata)
27 {
28         struct fdev *fdev = userdata;
29         fdev_dispatch(fdev, revents);
30         return 0;
31 }
32
33 static void unref(void *closure)
34 {
35         sd_event_source *source = closure;
36         sd_event_source_unref(source);
37 }
38
39 static void disable(void *closure, const struct fdev *fdev)
40 {
41         sd_event_source *source = closure;
42         sd_event_source_set_enabled(source, SD_EVENT_OFF);
43 }
44
45 static void enable(void *closure, const struct fdev *fdev)
46 {
47         sd_event_source *source = closure;
48         sd_event_source_set_io_events(source, fdev_events(fdev));
49         sd_event_source_set_enabled(source, SD_EVENT_ON);
50 }
51
52 static struct fdev_itf itf =
53 {
54         .unref = unref,
55         .disable = disable,
56         .enable = enable,
57         .update = enable
58 };
59
60 struct fdev *fdev_systemd_create(struct sd_event *eloop, int fd)
61 {
62         int rc;
63         sd_event_source *source;
64         struct fdev *fdev;
65
66         fdev = fdev_create(fd);
67         if (fdev) {
68                 rc = sd_event_add_io(eloop, &source, fd, 0, handler, fdev);
69                 if (rc < 0) {
70                         fdev_unref(fdev);
71                         fdev = 0;
72                         errno = -rc;
73                 } else {
74                         sd_event_source_set_enabled(source, SD_EVENT_OFF);
75                         fdev_set_itf(fdev, &itf, source);
76                 }
77         }
78         return fdev;
79 }
80