blob: 19681a0281f80778be27ca5984ba3fb14e540dc1 [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"
b.liu9e8584b2024-11-06 19:21:28 +080028#include "mbtk_utils.h"
r.xiaoacea6422024-11-04 01:26:10 -080029
30static mbtk_lpm_handler_t lpm_init;
31
32static pthread_t lpm_t;
33static int epoll_fd_t = -1;
34static int fd_t = -1;
35static int socket_t[2];
36
37static int sleep_epoll_deregister(int epoll_fd,int fd )
38{
39 int ret;
40 do {
41 ret = epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd, NULL );
42 } while (ret < 0 && errno == EINTR);
43 return ret;
44}
45
46static int sleep_epoll_register(int epoll_fd, int fd)
47{
48 struct epoll_event ev;
49 int ret, flags;
50
51 /* important: make the fd non-blocking */
52 flags = fcntl(fd, F_GETFL);
53 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
54
55 ev.events = EPOLLIN;
56 ev.data.fd = fd;
57 do {
58 ret = epoll_ctl( epoll_fd, EPOLL_CTL_ADD, fd, &ev );
59 } while (ret < 0 && errno == EINTR);
b.liu9e8584b2024-11-06 19:21:28 +080060
r.xiaoacea6422024-11-04 01:26:10 -080061 return ret;
62}
63
64
65void *threadFunction(void *arg)
66{
67 int pinValue;
68 int i;
69 char buf[8] = {0};
70 struct input_event ev_input = { 0 };
71 const int size = sizeof(struct input_event);
72
73 epoll_fd_t = epoll_create(2);
74
75 fd_t = open("/dev/input/event2", O_RDONLY);
76 LOGI("init pthread_event2");
77
78 sleep_epoll_register(epoll_fd_t, fd_t);
79 sleep_epoll_register(epoll_fd_t, socket_t[1]);
80
81 while (true)
82 {
83 struct epoll_event events[2];
r.xiaoacea6422024-11-04 01:26:10 -080084
85 int numEvents = epoll_wait(epoll_fd_t, events, 2, -1);
86
87 for (i = 0; i < numEvents; ++i)
88 {
89 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP))
90 {
91 LOGE("Error on GPIO device.");
92 return NULL;
93 }
94 else if ((events[i].events & EPOLLIN) || (events[i].events & EPOLLET))
95 {
96 //handleInterrupt(events[i].data.fd);
97 if (events[i].data.fd == socket_t[1])
98 {
99 memset(buf, 0, sizeof(buf));
b.liu9e8584b2024-11-06 19:21:28 +0800100 mbtk_read(socket_t[1], buf, sizeof(buf));
r.xiaoacea6422024-11-04 01:26:10 -0800101 if (1 == atoi(buf))
102 {
103 if(close(fd_t) == 0)
104 LOGI("close(fd_t)ing");
105
106 sleep_epoll_deregister(epoll_fd_t, socket_t[1]);
107 sleep_epoll_deregister(epoll_fd_t, fd_t);
108
109 LOGI("do pthread_exit");
110 return NULL;
111 }
112 }
113 else if (events[i].data.fd == fd_t)
114 {
115 LOGI("go pthread_event");
116 memset(&ev_input, 0x00, size);
b.liu9e8584b2024-11-06 19:21:28 +0800117 mbtk_read(fd_t, &ev_input, size);
r.xiaoacea6422024-11-04 01:26:10 -0800118 LOGI("ev_input type = %x, code = %x, value = %x", ev_input.type, ev_input.code,ev_input.value);
119
120 if (ev_input.type == 4 && ev_input.code == 3)
121 {
122 LOGI(">>>>ev_input.value = [%d]",ev_input.value);
123 pinValue = (int)ev_input.value;
124 lpm_init(pinValue);
125 }
126 }
127 else
128 {
129 LOGE("Unknown events[i].data.fd = %d", events[i].data.fd);
130 }
131 }
132 }
133 }
134 return NULL;
135}
136
137int mbtk_lpm_init(mbtk_lpm_handler_t mbtk_lpm_handler)
138{
b.liu9e8584b2024-11-06 19:21:28 +0800139 if (socketpair( AF_LOCAL, SOCK_STREAM, 0, socket_t ) < 0 )
r.xiaoacea6422024-11-04 01:26:10 -0800140 {
141 LOGE("[mbtk_lpm_init] could not create thread control socket pair: %s", strerror(errno));
142
143 /*close the control socket pair && Retry again.*/
144 if(socket_t[0] > 0)
145 {
146 close(socket_t[0] );
147 socket_t[0] = -1;
148 }
b.liu9e8584b2024-11-06 19:21:28 +0800149
r.xiaoacea6422024-11-04 01:26:10 -0800150 if(socket_t[1] > 0)
151 {
152 close(socket_t[1] );
153 socket_t[1] = -1;
154 }
155 return -1;
156 }
157
158 lpm_init = mbtk_lpm_handler;
159
160 pthread_attr_t thread_attr;
161 pthread_attr_init(&thread_attr);
162
163 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
164 {
165 LOGE("pthread_attr_setdetachstate() fail");
166 return -1;
167 }
168
169 if(pthread_create(&lpm_t, &thread_attr, threadFunction, NULL))
170 {
171 LOGE("mbtk_lpm_init can't create thread");
172 return -1;
173 }
174
175 pthread_attr_destroy(&thread_attr);
176
177 return 0;
178}
179
180int mbtk_lpm_deinit(void)
181{
182 char buf[4]={0};
183
184 if (fd_t == -1)
185 return 0;
186
187 if (fd_t != -1)
188 {
189 //char cmd = 1;
190 strcpy(buf, "1");
b.liu9e8584b2024-11-06 19:21:28 +0800191// void* dummy = NULL;
192 mbtk_write( socket_t[0], buf, sizeof(buf) );
r.xiaoacea6422024-11-04 01:26:10 -0800193
194 sleep(1);
195 // close the control socket pair
196 if(socket_t[0] > 0)
197 {
198 close(socket_t[0] );
199 socket_t[0] = -1;
200 }
201 if(socket_t[1] > 0)
202 {
203 close(socket_t[1] );
204 socket_t[1] = -1;
205 }
206
207 //重置还原
208 fd_t = -1;
209
210 }
211
212 return 0;
213}
214
215