1 /*
2  * Copyright (C) 2020-2021 Alibaba Group Holding Limited
3  */
4 #include <stdlib.h>
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include "select.h"
8 #include "aos/vfs.h"
9 #if AOS_COMP_CLI
10 #include "aos/cli.h"
11 #endif
12 
13 extern int vfs_test_device_init(void);
14 extern int vfs_test_device_deinit(void);
15 
select_example()16 static void select_example()
17 {
18     int ret;
19     int test_fd;
20     int maxfd;
21     int write_val = 1;
22     fd_set readset;
23     fd_set writeset;
24     fd_set exceptset;
25 
26     struct timeval tv = {
27         .tv_sec  = 2,
28         .tv_usec = 0,
29     };
30 
31     FD_ZERO(&readset);
32     FD_ZERO(&writeset);
33     FD_ZERO(&exceptset);
34 
35     test_fd = aos_open("/dev/test", O_RDWR);
36     if(test_fd < 0) {
37        printf("open test device err\r\n");
38        return;
39     }
40 
41     FD_SET(test_fd, &readset);
42     maxfd = test_fd + 1;
43 
44     ret = aos_select(maxfd, &readset, &writeset, &exceptset, &tv);
45     printf("aos_select timeout:ret = %d\r\n", ret);
46     aos_write(test_fd, &write_val, sizeof(write_val));
47     FD_SET(test_fd, &readset);
48     ret = aos_select(maxfd, &readset, &writeset, &exceptset, &tv);
49     printf("aos_select ret = %d\r\n", ret);
50     if (FD_ISSET(test_fd, &readset)) {
51         int data = 0;
52         int len = aos_read(test_fd, &data, sizeof(data));
53         if(len > 0) {
54             printf(" read fd =%d,data = %d\r\n", test_fd, data);
55         } else {
56             printf("err: read no data\r\n");
57         }
58     }
59     aos_close(test_fd);
60 }
61 
poll_example()62 static void poll_example()
63 {
64     int ret;
65     int test_fd;
66     int write_val = 1;
67     struct pollfd poll_array[2] = {0};
68 
69     test_fd = aos_open("/dev/test", O_RDWR);
70     if(test_fd < 0) {
71        printf("open test device err\r\n");
72        return;
73     }
74 
75     poll_array[0].events = POLLIN;
76     poll_array[0].fd = test_fd;
77 
78 
79     ret = aos_poll(poll_array, 2, 1000);
80 
81     printf("aos_poll timeout, ret = %d \r\n", ret);
82 
83     /* test write event1 */
84     ret = aos_write(test_fd, &write_val, sizeof(write_val));
85     ret = aos_poll(poll_array, 2, 1000);
86     printf("aos_poll ret = %d \r\n", ret);
87     if (poll_array[0].revents & POLLIN) {
88         int data = 0;
89         int len = aos_read(test_fd, &data, sizeof(data));
90         if(len > 0) {
91             printf(" read fd =%d,data = %d\r\n", test_fd, data);
92         } else {
93             printf("err: read no data\r\n");
94         }
95     }
96     aos_close(test_fd);
97 }
98 
99 
select_comp_example(int argc,char ** argv)100 static void select_comp_example(int argc, char **argv)
101 {
102     /* init a test vfs device for poll and select */
103     vfs_test_device_init();
104 
105     /* select test */
106     printf("+++++++select example+++++++++\r\n");
107     select_example();
108 
109     /* poll test */
110     printf("+++++++poll example+++++++++\r\n");
111     poll_example();
112 
113     vfs_test_device_deinit();
114     return;
115 }
116 
117 #if AOS_COMP_CLI
118 /* reg args: fun, cmd, description*/
119 ALIOS_CLI_CMD_REGISTER(select_comp_example, select_example, select and poll API base example)
120 #endif
121