Skip to content

Commit bccb6c6

Browse files
Otpvondoiatsxiaoxiang781216
authored andcommitted
uorb:Added urob loop function module and supported epoll.
Signed-off-by: likun17 <[email protected]>
1 parent c03e3d0 commit bccb6c6

File tree

7 files changed

+495
-3
lines changed

7 files changed

+495
-3
lines changed

system/uorb/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ if(CONFIG_UORB)
2727

2828
nuttx_add_library(uorb STATIC)
2929

30-
file(GLOB_RECURSE CSRCS "sensor/*.c")
31-
list(APPEND CSRCS uORB/uORB.c)
30+
file(GLOB_RECURSE CSRCS "sensor/*.c" "uORB/*.c")
3231

3332
if(CONFIG_UORB_LISTENER)
3433
nuttx_add_application(

system/uorb/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ config UORB_TESTS
2626
bool "uorb unit tests"
2727
default n
2828

29+
config UORB_LOOP_MAX_EVENTS
30+
int "uorb loop max events"
31+
default 16
32+
2933
if UORB_TESTS
3034

3135
config UORB_SRORAGE_DIR

system/uorb/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
include $(APPDIR)/Make.defs
2222

23-
CSRCS += uORB/uORB.c
23+
CSRCS += $(wildcard uORB/*.c)
2424
CSRCS += $(wildcard sensor/*.c)
2525

2626
ifneq ($(CONFIG_UORB_LISTENER),)

system/uorb/uORB/epoll.c

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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+
}

system/uorb/uORB/internal.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/****************************************************************************
2+
* apps/system/uorb/uORB/internal.h
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+
#ifndef __APP_SYSTEM_UORB_UORB_INTERNAL_H
22+
#define __APP_SYSTEM_UORB_UORB_INTERNAL_H
23+
24+
/****************************************************************************
25+
* Included Files
26+
****************************************************************************/
27+
28+
#include <uORB/uORB.h>
29+
30+
/****************************************************************************
31+
* Public Data
32+
****************************************************************************/
33+
34+
extern const struct orb_loop_ops_s g_orb_loop_epoll_ops;
35+
36+
/****************************************************************************
37+
* Public Types
38+
****************************************************************************/
39+
40+
struct orb_loop_ops_s
41+
{
42+
CODE int (*init)(FAR struct orb_loop_s *loop);
43+
CODE int (*run)(FAR struct orb_loop_s *loop);
44+
CODE int (*uninit)(FAR struct orb_loop_s *loop);
45+
CODE int (*enable)(FAR struct orb_loop_s *loop,
46+
FAR struct orb_handle_s *handle, bool en);
47+
};
48+
49+
#endif /* __APP_SYSTEM_UORB_UORB_INTERNAL_H */

system/uorb/uORB/loop.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/****************************************************************************
2+
* apps/system/uorb/uORB/loop.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+
27+
#include "internal.h"
28+
29+
/****************************************************************************
30+
* Public Functions
31+
****************************************************************************/
32+
33+
int orb_loop_init(FAR struct orb_loop_s *loop, enum orb_loop_type_e type)
34+
{
35+
int ret = -EINVAL;
36+
37+
if (loop == NULL)
38+
{
39+
return ret;
40+
}
41+
42+
switch (type)
43+
{
44+
case ORB_EPOLL_TYPE:
45+
loop->ops = &g_orb_loop_epoll_ops;
46+
break;
47+
48+
default:
49+
uorberr("loop register type error! type:%d", type);
50+
return ret;
51+
}
52+
53+
ret = loop->ops->init(loop);
54+
if (ret < 0)
55+
{
56+
uorberr("loop init failed! ret:%d", ret);
57+
loop->ops = NULL;
58+
}
59+
60+
return ret;
61+
}
62+
63+
int orb_loop_run(FAR struct orb_loop_s *loop)
64+
{
65+
return loop->ops->run(loop);
66+
}
67+
68+
int orb_loop_deinit(FAR struct orb_loop_s *loop)
69+
{
70+
int ret;
71+
72+
ret = loop->ops->uninit(loop);
73+
if (ret >= 0)
74+
{
75+
loop->ops = NULL;
76+
}
77+
78+
return ret;
79+
}
80+
81+
int orb_handle_init(FAR struct orb_handle_s *handle, int fd, int events,
82+
FAR void *arg, orb_datain_cb_t datain_cb,
83+
orb_dataout_cb_t dataout_cb, orb_eventpri_cb_t pri_cb,
84+
orb_eventerr_cb_t err_cb)
85+
{
86+
if (fd < 0)
87+
{
88+
return -EINVAL;
89+
}
90+
91+
handle->fd = fd;
92+
handle->arg = arg;
93+
handle->events = events;
94+
handle->eventpri_cb = pri_cb;
95+
handle->eventerr_cb = err_cb;
96+
handle->datain_cb = datain_cb;
97+
handle->dataout_cb = dataout_cb;
98+
99+
return OK;
100+
}
101+
102+
int orb_handle_start(FAR struct orb_loop_s *loop,
103+
FAR struct orb_handle_s *handle)
104+
{
105+
return loop->ops->enable(loop, handle, true);
106+
}
107+
108+
int orb_handle_stop(FAR struct orb_loop_s *loop,
109+
FAR struct orb_handle_s *handle)
110+
{
111+
return loop->ops->enable(loop, handle, false);
112+
}

0 commit comments

Comments
 (0)