1 #include <stdint.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <fcntl.h>
5 #include <errno.h>
6 #include <aos/kernel.h>
7 #include <aos/vfs.h>
8 #include <epoll.h>
9 #include "private/epoll_inner.h"
10
aos_epoll_create(int size)11 int aos_epoll_create(int size)
12 {
13 if (size < 0) {
14 errno = EINVAL;
15 return -1;
16 }
17 return aos_epoll_create1(0);
18 }
19
aos_epoll_create1(int flag)20 int aos_epoll_create1(int flag)
21 {
22 int ret = -1;
23
24 if (flag < 0) {
25 errno = EINVAL;
26 return ret;
27 }
28
29 ret = vfs_device_epoll_init();
30 if (ret < 0) {
31 printf("vfs epoll init failed ,%d\r\n", ret);
32 if (ret < -1) {
33 errno = -ret;
34 }
35 return -1;
36 }
37
38 #ifdef _WIN32
39 ret = aos_open("C:\\epoll.bin", flag == EPOLL_CLOEXEC ? O_CLOEXEC : 0);
40 #else
41 ret = aos_open("/dev/epoll", flag == EPOLL_CLOEXEC ? O_CLOEXEC : 0);
42 #endif
43 if (ret < 0) {
44 if (ret < -1) {
45 errno = -ret;
46 }
47 return -1;
48 }
49 return ret;
50 }
51
aos_epoll_ctl(int epid,int op,int fd,struct epoll_event * event)52 int aos_epoll_ctl(int epid, int op, int fd, struct epoll_event *event)
53 {
54
55 epoll_ctl_data_t data;
56 int ret = -1;
57
58 if (epid < 0 || fd < 0) {
59 errno = EINVAL;
60 return -1;
61 }
62 memset(&data, 0, sizeof(epoll_ctl_data_t));
63 data.op = op;
64 data.fd = fd;
65 data.event = event;
66
67 ret = aos_ioctl(epid, EPOLL_VFS_CTL, (uint32_t)&data);
68 if (ret < 0) {
69 if (ret < -1) {
70 errno = -ret;
71 }
72 return -1;
73 }
74 return ret;
75 }
76
77
aos_epoll_wait(int epid,struct epoll_event * events,int maxevents,int timeout)78 int aos_epoll_wait(int epid, struct epoll_event *events, int maxevents, int timeout)
79 {
80 int ret = -1;
81 epoll_wait_data_t data;
82 if (events == NULL || epid < 0 || maxevents <= 0) {
83 errno = EINVAL;
84 return -1;
85 }
86 memset(&data, 0, sizeof(epoll_wait_data_t));
87 data.events = events;
88 data.maxevents = maxevents;
89 data.timeout = timeout;
90 ret = aos_ioctl(epid, EPOLL_VFS_WAIT, (uint32_t)&data);
91 if (ret < 0) {
92 if (ret < -1) {
93 errno = -ret;
94 }
95 return -1;
96 }
97 return ret;
98 }
99