blob: 5ea9bfa59658cc4ca67c26379c3294fadd95862a [file] [log] [blame]
b.liu5f950c52024-06-15 20:13:12 +08001/*
2* gnss_ipc.c
3*
4* MBTK GNSS IPC service source.
5*
6*/
7/******************************************************************************
8
9 EDIT HISTORY FOR FILE
10
11 WHEN WHO WHAT,WHERE,WHY
12-------- -------- -------------------------------------------------------
132024/6/15 LiuBin Initial version
14
15******************************************************************************/
16#include <stdio.h>
17#include <stdlib.h>
18#include <unistd.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <sys/socket.h>
22#include <sys/un.h>
23#include <netinet/in.h>
24#include <pthread.h>
25#include <sys/epoll.h>
26
27#include "mbtk_log.h"
28#include "mbtk_type.h"
29#include "gnss_info.h"
30
31#define GNSS_SOCK_PATH "/tmp/mbtk_gnss_sock"
32#define SOCK_CLIENT_MAX 3
33#define EPOLL_LISTEN_MAX 100
34
35static int sock_listen_fd = -1;
36static int sock_cli_fds[SOCK_CLIENT_MAX];
37
38int gnss_write(int fd, const void* buf, unsigned int buf_len);
39
40static int gnss_cli_fd_find(int fd)
41{
42 int i = 0;
43 while(i < SOCK_CLIENT_MAX) {
44 if(fd == sock_cli_fds[i])
45 return i;
46 i++;
47 }
48
49 return -1;
50}
51
52static void gnss_msg_process(int fd, const char *msg, int msg_len)
53{
b.liud0ba7152024-06-19 14:47:21 +080054 LOGD("CMD <%s>", msg);
b.liu5f950c52024-06-15 20:13:12 +080055 // gnss_init:x
yq.wang5c647e02024-08-10 00:01:11 -070056 usleep(10 * 1000); // sleep 10ms
b.liu5f950c52024-06-15 20:13:12 +080057 if(memcmp(msg, "gnss_init", 9) == 0) {
58 int init_mode = atoi(msg + 10);
b.liu5f950c52024-06-15 20:13:12 +080059 int ret = 0;
60 if(init_mode == 0) { // Close gnss.
61 ret = gnss_deinit();
62 } else {
b.liuc223a942024-07-18 15:44:12 +080063 // ARG error, no print nmea.
64 if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) != init_mode) {
65 init_mode = 0;
b.liu5f950c52024-06-15 20:13:12 +080066 }
b.liuc223a942024-07-18 15:44:12 +080067 ret = gnss_init(init_mode);
b.liu5f950c52024-06-15 20:13:12 +080068 }
69
70 char rsp[100] = {0};
b.liue77ac3a2024-07-17 17:36:57 +080071 sprintf(rsp, "%cgnss_init:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
b.liu5f950c52024-06-15 20:13:12 +080072 gnss_write(fd, rsp, strlen(rsp));
b.liud0ba7152024-06-19 14:47:21 +080073 } else if(memcmp(msg, "gnss_deinit", 11) == 0) { // gnss_deinit
b.liu5f950c52024-06-15 20:13:12 +080074 int ret = gnss_deinit();
75
76 char rsp[100] = {0};
b.liue77ac3a2024-07-17 17:36:57 +080077 sprintf(rsp, "%cgnss_deinit:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
b.liu5f950c52024-06-15 20:13:12 +080078 gnss_write(fd, rsp, strlen(rsp));
b.liud0ba7152024-06-19 14:47:21 +080079 } else if(memcmp(msg, "gnss_setting", 12) == 0) {// gnss_setting:cmd
80 int ret = gnss_set(msg + 13, strlen(msg + 13), NULL, 0);
81
82 char rsp[100] = {0};
b.liue77ac3a2024-07-17 17:36:57 +080083 sprintf(rsp, "%cgnss_setting:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
b.liud0ba7152024-06-19 14:47:21 +080084 gnss_write(fd, rsp, strlen(rsp));
b.liudbc3f4b2024-06-25 18:22:24 +080085 } else if(memcmp(msg, "gnss_dl", 7) == 0) {// gnss_dl:fw_name
86 int ret = gnss_dl_fw(msg + 8, NULL, 0);
87
88 char rsp[100] = {0};
b.liue77ac3a2024-07-17 17:36:57 +080089 sprintf(rsp, "%cgnss_dl:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
90 gnss_write(fd, rsp, strlen(rsp));
91 } else if(memcmp(msg, "gnss_ind", 8) == 0) {// gnss_ind:ind_type
92 int ind_type = atoi(msg + 9);
93 int ret = gnss_ind_set(fd, ind_type);
94
95 char rsp[100] = {0};
96 sprintf(rsp, "%cgnss_ind:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
b.liudbc3f4b2024-06-25 18:22:24 +080097 gnss_write(fd, rsp, strlen(rsp));
b.liu5f950c52024-06-15 20:13:12 +080098 } else {
99 LOGW("Unknown gnss msg : %s", msg);
100 }
101}
102
103static void* gnss_ser_pthread(void* arg)
104{
105 UNUSED(arg);
106 int epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1);
107 if(epoll_fd < 0)
108 {
109 LOGE("epoll_create() fail[%d].", errno);
110 return NULL;
111 }
112
113 int i = 0;
114 while(i < SOCK_CLIENT_MAX) {
115 sock_cli_fds[i++] = -1;
116 }
117
118 uint32 event = EPOLLIN | EPOLLET;
119 struct epoll_event ev;
120 ev.data.fd = sock_listen_fd;
121 ev.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
122 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock_listen_fd, &ev);
123
124 int nready = -1;
125 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
126 while(1)
127 {
128 nready = epoll_wait(epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
129 if(nready > 0)
130 {
131 for(i = 0; i < nready; i++)
132 {
133 LOGD("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
134 if(epoll_events[i].events & EPOLLHUP) // Client Close.
135 {
136 int index = gnss_cli_fd_find(epoll_events[i].data.fd);
137 if(index != -1)
138 {
139 memset(&ev,0,sizeof(struct epoll_event));
140 ev.data.fd = epoll_events[i].data.fd;
141 ev.events = EPOLLIN | EPOLLERR | EPOLLET;
142 epoll_ctl(epoll_fd, EPOLL_CTL_DEL, epoll_events[i].data.fd, &ev);
143
144 close(epoll_events[i].data.fd);
145 sock_cli_fds[index] = -1;
b.liue77ac3a2024-07-17 17:36:57 +0800146
147 // Clear IND flag.
148 gnss_ind_set(epoll_events[i].data.fd, 0);
b.liu5f950c52024-06-15 20:13:12 +0800149 }
150 else
151 {
152 LOGE("Unknown client[fd = %d].", epoll_events[i].data.fd);
153 }
154 }
155 else if(epoll_events[i].events & EPOLLIN)
156 {
157 if(epoll_events[i].data.fd == sock_listen_fd) // New clients connected.
158 {
159 int client_fd = -1;
160 while(1)
161 {
162 struct sockaddr_in cliaddr;
163 socklen_t clilen = sizeof(cliaddr);
164 client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen);
165 if(client_fd <= 0)
166 {
167 if(errno == EAGAIN)
168 {
169 LOGE("All client connect get.");
170 }
171 else
172 {
173 LOGE("accept() error[%d].", errno);
174 }
175 break;
176 } else {
177 i = 0;
178 while(i < SOCK_CLIENT_MAX) {
179 if(sock_cli_fds[i] <= 0) {
180 sock_cli_fds[i] = client_fd;
181 break;
182 }
183 i++;
184 }
185
186 if(i >= SOCK_CLIENT_MAX) {
187 LOGE("Client is full.");
188 break;
189 }
190 }
191 // Set O_NONBLOCK
192 int flags = fcntl(client_fd, F_GETFL, 0);
193 if (flags > 0)
194 {
195 flags |= O_NONBLOCK;
196 if (fcntl(client_fd, F_SETFL, flags) < 0)
197 {
198 LOGE("Set flags error:%d", errno);
199 }
200 }
201
202 memset(&ev,0,sizeof(struct epoll_event));
203 ev.data.fd = client_fd;
204 ev.events = event;//EPOLLIN | EPOLLERR | EPOLLET;
205 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
206 }
207 }
208 else if(epoll_events[i].data.fd > 0) // Client data arrive.
209 {
210 char buff[1024] = {0};
211 int len = read(epoll_events[i].data.fd, buff, sizeof(buff));
212 if(len > 0) {
213 gnss_msg_process(epoll_events[i].data.fd, buff, len);
214 }
215 }
216 else
217 {
218 LOGE("Unknown socket : %d", epoll_events[i].data.fd);
219 }
220 }
221 else
222 {
223 LOGE("Unknown event : %x", epoll_events[i].events);
224 }
225 }
226 }
227 else
228 {
229 LOGE("epoll_wait() fail[%d].", errno);
230 }
231 }
232
233 return NULL;
234}
235
236
237int gnss_ipc_service_start()
238{
239 struct sockaddr_un server_addr;
240 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
241 if(sock_listen_fd < 0)
242 {
243 LOGE("socket() fail[%d].", errno);
244 return -1;
245 }
246
247 // Set O_NONBLOCK
248 int flags = fcntl(sock_listen_fd, F_GETFL, 0);
249 if (flags < 0)
250 {
251 LOGE("Get flags error:%d", errno);
252 goto error;
253 }
254 flags |= O_NONBLOCK;
255 if (fcntl(sock_listen_fd, F_SETFL, flags) < 0)
256 {
257 LOGE("Set flags error:%d", errno);
258 goto error;
259 }
260
261 unlink(GNSS_SOCK_PATH);
262 memset(&server_addr, 0, sizeof(struct sockaddr_un));
263 server_addr.sun_family = AF_LOCAL;
264 strcpy(server_addr.sun_path, GNSS_SOCK_PATH);
265 if(bind(sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
266 {
267 LOGE("bind() fail[%d].", errno);
268 goto error;
269 }
270
271 if(listen(sock_listen_fd, SOCK_CLIENT_MAX))
272 {
273 LOGE("listen() fail[%d].", errno);
274 goto error;
275 }
276
277 pthread_t pid;
278 pthread_attr_t thread_attr;
279 pthread_attr_init(&thread_attr);
280 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
281 {
282 LOGE("pthread_attr_setdetachstate() fail.");
283 goto error;
284 }
285
286 if(pthread_create(&pid, &thread_attr, gnss_ser_pthread, NULL))
287 {
288 LOGE("pthread_create() fail.");
289 goto error;
290 }
291
292 LOGD("GNSS IPC service is running...");
293 return 0;
294error:
295 close(sock_listen_fd);
296 return -1;
297}
298