|
| 1 | +/**************************************************************************** |
| 2 | + * apps/system/uorb/uORB/epoll.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <errno.h> |
| 26 | +#include <unistd.h> |
| 27 | +#include <sys/epoll.h> |
| 28 | + |
| 29 | +#include "internal.h" |
| 30 | + |
| 31 | +/**************************************************************************** |
| 32 | + * Private Function Prototypes |
| 33 | + ****************************************************************************/ |
| 34 | + |
| 35 | +static int orb_loop_epoll_init(FAR struct orb_loop_s *loop); |
| 36 | +static int orb_loop_epoll_run(FAR struct orb_loop_s *loop); |
| 37 | +static int orb_loop_epoll_uninit(FAR struct orb_loop_s *loop); |
| 38 | +static int orb_loop_epoll_enable(FAR struct orb_loop_s *loop, |
| 39 | + FAR struct orb_handle_s *handle, bool en); |
| 40 | + |
| 41 | +/**************************************************************************** |
| 42 | + * Public Data |
| 43 | + ****************************************************************************/ |
| 44 | + |
| 45 | +const struct orb_loop_ops_s g_orb_loop_epoll_ops = |
| 46 | +{ |
| 47 | + .init = orb_loop_epoll_init, |
| 48 | + .run = orb_loop_epoll_run, |
| 49 | + .uninit = orb_loop_epoll_uninit, |
| 50 | + .enable = orb_loop_epoll_enable, |
| 51 | +}; |
| 52 | + |
| 53 | +/**************************************************************************** |
| 54 | + * Private Functions |
| 55 | + ****************************************************************************/ |
| 56 | + |
| 57 | +static int orb_loop_epoll_init(FAR struct orb_loop_s *loop) |
| 58 | +{ |
| 59 | + loop->running = false; |
| 60 | + loop->fd = epoll_create1(EPOLL_CLOEXEC); |
| 61 | + if (loop->fd < 0) |
| 62 | + { |
| 63 | + return -errno; |
| 64 | + } |
| 65 | + |
| 66 | + return OK; |
| 67 | +} |
| 68 | + |
| 69 | +static int orb_loop_epoll_run(FAR struct orb_loop_s *loop) |
| 70 | +{ |
| 71 | + struct epoll_event et[CONFIG_UORB_LOOP_MAX_EVENTS]; |
| 72 | + FAR struct orb_handle_s *handle; |
| 73 | + int nfds; |
| 74 | + int i; |
| 75 | + |
| 76 | + if (loop->running) |
| 77 | + { |
| 78 | + return -EBUSY; |
| 79 | + } |
| 80 | + |
| 81 | + loop->running = true; |
| 82 | + while (loop->running) |
| 83 | + { |
| 84 | + nfds = epoll_wait(loop->fd, et, CONFIG_UORB_LOOP_MAX_EVENTS, -1); |
| 85 | + if (nfds == -1 && errno != EINTR) |
| 86 | + { |
| 87 | + return -errno; |
| 88 | + } |
| 89 | + |
| 90 | + for (i = 0; i < nfds; i++) |
| 91 | + { |
| 92 | + handle = et[i].data.ptr; |
| 93 | + if (handle == NULL) |
| 94 | + { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + if (et[i].events & EPOLLIN) |
| 99 | + { |
| 100 | + if (handle->datain_cb != NULL) |
| 101 | + { |
| 102 | + handle->datain_cb(handle, handle->arg); |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + uorberr("epoll wait data in error! fd:%d", handle->fd); |
| 107 | + } |
| 108 | + } |
| 109 | + else if (et[i].events & EPOLLOUT) |
| 110 | + { |
| 111 | + if (handle->dataout_cb != NULL) |
| 112 | + { |
| 113 | + handle->dataout_cb(handle, handle->arg); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + uorberr("epoll wait data out error! fd:%d", handle->fd); |
| 118 | + } |
| 119 | + } |
| 120 | + else if (et[i].events & EPOLLPRI) |
| 121 | + { |
| 122 | + if (handle->eventpri_cb != NULL) |
| 123 | + { |
| 124 | + handle->eventpri_cb(handle, handle->arg); |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + uorberr("epoll wait events pri error! fd:%d", handle->fd); |
| 129 | + } |
| 130 | + } |
| 131 | + else if (et[i].events & EPOLLERR) |
| 132 | + { |
| 133 | + if (handle->eventerr_cb != NULL) |
| 134 | + { |
| 135 | + handle->eventerr_cb(handle, handle->arg); |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + uorberr("epoll wait events error! fd:%d", handle->fd); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return OK; |
| 146 | +} |
| 147 | + |
| 148 | +static int orb_loop_epoll_uninit(FAR struct orb_loop_s *loop) |
| 149 | +{ |
| 150 | + int ret; |
| 151 | + |
| 152 | + loop->running = false; |
| 153 | + ret = close(loop->fd); |
| 154 | + if (ret < 0) |
| 155 | + { |
| 156 | + return -errno; |
| 157 | + } |
| 158 | + |
| 159 | + return ret; |
| 160 | +} |
| 161 | + |
| 162 | +static int orb_loop_epoll_enable(FAR struct orb_loop_s *loop, |
| 163 | + FAR struct orb_handle_s *handle, bool en) |
| 164 | +{ |
| 165 | + struct epoll_event ev; |
| 166 | + int ret; |
| 167 | + |
| 168 | + if (en) |
| 169 | + { |
| 170 | + ev.events = handle->events; |
| 171 | + ev.data.ptr = handle; |
| 172 | + ret = epoll_ctl(loop->fd, EPOLL_CTL_ADD, handle->fd, &ev); |
| 173 | + } |
| 174 | + else |
| 175 | + { |
| 176 | + ret = epoll_ctl(loop->fd, EPOLL_CTL_DEL, handle->fd, NULL); |
| 177 | + } |
| 178 | + |
| 179 | + if (ret < 0) |
| 180 | + { |
| 181 | + return -errno; |
| 182 | + } |
| 183 | + |
| 184 | + return ret; |
| 185 | +} |
0 commit comments