blob: b18a66043d2c0a1726b5360ec5d35afe4869a987 [file] [log] [blame]
r.xiaoacea6422024-11-04 01:26:10 -08001#include <stdio.h>
2#include <time.h>
3#include <sys/time.h>
4#include <unistd.h>
5#include <sys/un.h>
6#include <sys/socket.h>
7#include <netinet/in.h>
8#include <arpa/inet.h>
9#include <errno.h>
10#include <sys/ioctl.h>
11#include <net/if.h>
12#include <string.h>
13#include <fcntl.h>
14#include <signal.h>
15#include <stdlib.h>
16#include <sys/stat.h>
17#include <sys/file.h>
18#include <stddef.h>
19#include <sys/types.h>
20#include <pthread.h>
21#include <sys/epoll.h>
22#include <linux/input.h>
23
24
25#include "mbtk_lpm.h"
26#include "mbtk_type.h"
27#include "mbtk_log.h"
28
29static mbtk_lpm_handler_t lpm_init;
30
31static pthread_t lpm_t;
32static int epoll_fd_t = -1;
33static int fd_t = -1;
34static int socket_t[2];
35
36static int sleep_epoll_deregister(int epoll_fd,int fd )
37{
38 int ret;
39 do {
40 ret = epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd, NULL );
41 } while (ret < 0 && errno == EINTR);
42 return ret;
43}
44
45static int sleep_epoll_register(int epoll_fd, int fd)
46{
47 struct epoll_event ev;
48 int ret, flags;
49
50 /* important: make the fd non-blocking */
51 flags = fcntl(fd, F_GETFL);
52 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
53
54 ev.events = EPOLLIN;
55 ev.data.fd = fd;
56 do {
57 ret = epoll_ctl( epoll_fd, EPOLL_CTL_ADD, fd, &ev );
58 } while (ret < 0 && errno == EINTR);
59
60 return ret;
61}
62
63
64void *threadFunction(void *arg)
65{
66 int pinValue;
67 int i;
68 char buf[8] = {0};
69 struct input_event ev_input = { 0 };
70 const int size = sizeof(struct input_event);
71
72 epoll_fd_t = epoll_create(2);
73
74 fd_t = open("/dev/input/event2", O_RDONLY);
75 LOGI("init pthread_event2");
76
77 sleep_epoll_register(epoll_fd_t, fd_t);
78 sleep_epoll_register(epoll_fd_t, socket_t[1]);
79
80 while (true)
81 {
82 struct epoll_event events[2];
83 struct epoll_event ev;
84 int cmd = 0;
85
86 int numEvents = epoll_wait(epoll_fd_t, events, 2, -1);
87
88 for (i = 0; i < numEvents; ++i)
89 {
90 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP))
91 {
92 LOGE("Error on GPIO device.");
93 return NULL;
94 }
95 else if ((events[i].events & EPOLLIN) || (events[i].events & EPOLLET))
96 {
97 //handleInterrupt(events[i].data.fd);
98 if (events[i].data.fd == socket_t[1])
99 {
100 memset(buf, 0, sizeof(buf));
101 read(socket_t[1], buf, sizeof(buf));
102 if (1 == atoi(buf))
103 {
104 if(close(fd_t) == 0)
105 LOGI("close(fd_t)ing");
106
107 sleep_epoll_deregister(epoll_fd_t, socket_t[1]);
108 sleep_epoll_deregister(epoll_fd_t, fd_t);
109
110 LOGI("do pthread_exit");
111 return NULL;
112 }
113 }
114 else if (events[i].data.fd == fd_t)
115 {
116 LOGI("go pthread_event");
117 memset(&ev_input, 0x00, size);
118 read(fd_t, &ev_input, size);
119 LOGI("ev_input type = %x, code = %x, value = %x", ev_input.type, ev_input.code,ev_input.value);
120
121 if (ev_input.type == 4 && ev_input.code == 3)
122 {
123 LOGI(">>>>ev_input.value = [%d]",ev_input.value);
124 pinValue = (int)ev_input.value;
125 lpm_init(pinValue);
126 }
127 }
128 else
129 {
130 LOGE("Unknown events[i].data.fd = %d", events[i].data.fd);
131 }
132 }
133 }
134 }
135 return NULL;
136}
137
138int mbtk_lpm_init(mbtk_lpm_handler_t mbtk_lpm_handler)
139{
140 if (socketpair( AF_LOCAL, SOCK_STREAM, 0, socket_t ) < 0 )
141 {
142 LOGE("[mbtk_lpm_init] could not create thread control socket pair: %s", strerror(errno));
143
144 /*close the control socket pair && Retry again.*/
145 if(socket_t[0] > 0)
146 {
147 close(socket_t[0] );
148 socket_t[0] = -1;
149 }
150
151 if(socket_t[1] > 0)
152 {
153 close(socket_t[1] );
154 socket_t[1] = -1;
155 }
156 return -1;
157 }
158
159 lpm_init = mbtk_lpm_handler;
160
161 pthread_attr_t thread_attr;
162 pthread_attr_init(&thread_attr);
163
164 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
165 {
166 LOGE("pthread_attr_setdetachstate() fail");
167 return -1;
168 }
169
170 if(pthread_create(&lpm_t, &thread_attr, threadFunction, NULL))
171 {
172 LOGE("mbtk_lpm_init can't create thread");
173 return -1;
174 }
175
176 pthread_attr_destroy(&thread_attr);
177
178 return 0;
179}
180
181int mbtk_lpm_deinit(void)
182{
183 char buf[4]={0};
184
185 if (fd_t == -1)
186 return 0;
187
188 if (fd_t != -1)
189 {
190 //char cmd = 1;
191 strcpy(buf, "1");
192 void* dummy = NULL;
193 write( socket_t[0], buf, sizeof(buf) );
194
195 sleep(1);
196 // close the control socket pair
197 if(socket_t[0] > 0)
198 {
199 close(socket_t[0] );
200 socket_t[0] = -1;
201 }
202 if(socket_t[1] > 0)
203 {
204 close(socket_t[1] );
205 socket_t[1] = -1;
206 }
207
208 //重置还原
209 fd_t = -1;
210
211 }
212
213 return 0;
214}
215
216