blob: 54ba82323499064560aa5ab5f9e2bcf320cfba6c [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
56 if(memcmp(msg, "gnss_init", 9) == 0) {
57 int init_mode = atoi(msg + 10);
b.liu5f950c52024-06-15 20:13:12 +080058 int ret = 0;
59 if(init_mode == 0) { // Close gnss.
60 ret = gnss_deinit();
61 } else {
62 if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) == init_mode) {
63 ret = gnss_init(init_mode);
64 } else { // ARG error, no print nmea.
b.liuced8dd02024-06-28 13:28:29 +080065 ret = GNSS_ERR_ARG;
b.liu5f950c52024-06-15 20:13:12 +080066 }
67 }
68
69 char rsp[100] = {0};
b.liue77ac3a2024-07-17 17:36:57 +080070 sprintf(rsp, "%cgnss_init:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
b.liu5f950c52024-06-15 20:13:12 +080071 gnss_write(fd, rsp, strlen(rsp));
b.liud0ba7152024-06-19 14:47:21 +080072 } else if(memcmp(msg, "gnss_deinit", 11) == 0) { // gnss_deinit
b.liu5f950c52024-06-15 20:13:12 +080073 int ret = gnss_deinit();
74
75 char rsp[100] = {0};
b.liue77ac3a2024-07-17 17:36:57 +080076 sprintf(rsp, "%cgnss_deinit:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
b.liu5f950c52024-06-15 20:13:12 +080077 gnss_write(fd, rsp, strlen(rsp));
b.liud0ba7152024-06-19 14:47:21 +080078 } else if(memcmp(msg, "gnss_setting", 12) == 0) {// gnss_setting:cmd
79 int ret = gnss_set(msg + 13, strlen(msg + 13), NULL, 0);
80
81 char rsp[100] = {0};
b.liue77ac3a2024-07-17 17:36:57 +080082 sprintf(rsp, "%cgnss_setting:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
b.liud0ba7152024-06-19 14:47:21 +080083 gnss_write(fd, rsp, strlen(rsp));
b.liudbc3f4b2024-06-25 18:22:24 +080084 } else if(memcmp(msg, "gnss_dl", 7) == 0) {// gnss_dl:fw_name
85 int ret = gnss_dl_fw(msg + 8, NULL, 0);
86
87 char rsp[100] = {0};
b.liue77ac3a2024-07-17 17:36:57 +080088 sprintf(rsp, "%cgnss_dl:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
89 gnss_write(fd, rsp, strlen(rsp));
90 } else if(memcmp(msg, "gnss_ind", 8) == 0) {// gnss_ind:ind_type
91 int ind_type = atoi(msg + 9);
92 int ret = gnss_ind_set(fd, ind_type);
93
94 char rsp[100] = {0};
95 sprintf(rsp, "%cgnss_ind:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG);
b.liudbc3f4b2024-06-25 18:22:24 +080096 gnss_write(fd, rsp, strlen(rsp));
b.liu5f950c52024-06-15 20:13:12 +080097 } else {
98 LOGW("Unknown gnss msg : %s", msg);
99 }
100}
101
102static void* gnss_ser_pthread(void* arg)
103{
104 UNUSED(arg);
105 int epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1);
106 if(epoll_fd < 0)
107 {
108 LOGE("epoll_create() fail[%d].", errno);
109 return NULL;
110 }
111
112 int i = 0;
113 while(i < SOCK_CLIENT_MAX) {
114 sock_cli_fds[i++] = -1;
115 }
116
117 uint32 event = EPOLLIN | EPOLLET;
118 struct epoll_event ev;
119 ev.data.fd = sock_listen_fd;
120 ev.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
121 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock_listen_fd, &ev);
122
123 int nready = -1;
124 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
125 while(1)
126 {
127 nready = epoll_wait(epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
128 if(nready > 0)
129 {
130 for(i = 0; i < nready; i++)
131 {
132 LOGD("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
133 if(epoll_events[i].events & EPOLLHUP) // Client Close.
134 {
135 int index = gnss_cli_fd_find(epoll_events[i].data.fd);
136 if(index != -1)
137 {
138 memset(&ev,0,sizeof(struct epoll_event));
139 ev.data.fd = epoll_events[i].data.fd;
140 ev.events = EPOLLIN | EPOLLERR | EPOLLET;
141 epoll_ctl(epoll_fd, EPOLL_CTL_DEL, epoll_events[i].data.fd, &ev);
142
143 close(epoll_events[i].data.fd);
144 sock_cli_fds[index] = -1;
b.liue77ac3a2024-07-17 17:36:57 +0800145
146 // Clear IND flag.
147 gnss_ind_set(epoll_events[i].data.fd, 0);
b.liu5f950c52024-06-15 20:13:12 +0800148 }
149 else
150 {
151 LOGE("Unknown client[fd = %d].", epoll_events[i].data.fd);
152 }
153 }
154 else if(epoll_events[i].events & EPOLLIN)
155 {
156 if(epoll_events[i].data.fd == sock_listen_fd) // New clients connected.
157 {
158 int client_fd = -1;
159 while(1)
160 {
161 struct sockaddr_in cliaddr;
162 socklen_t clilen = sizeof(cliaddr);
163 client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen);
164 if(client_fd <= 0)
165 {
166 if(errno == EAGAIN)
167 {
168 LOGE("All client connect get.");
169 }
170 else
171 {
172 LOGE("accept() error[%d].", errno);
173 }
174 break;
175 } else {
176 i = 0;
177 while(i < SOCK_CLIENT_MAX) {
178 if(sock_cli_fds[i] <= 0) {
179 sock_cli_fds[i] = client_fd;
180 break;
181 }
182 i++;
183 }
184
185 if(i >= SOCK_CLIENT_MAX) {
186 LOGE("Client is full.");
187 break;
188 }
189 }
190 // Set O_NONBLOCK
191 int flags = fcntl(client_fd, F_GETFL, 0);
192 if (flags > 0)
193 {
194 flags |= O_NONBLOCK;
195 if (fcntl(client_fd, F_SETFL, flags) < 0)
196 {
197 LOGE("Set flags error:%d", errno);
198 }
199 }
200
201 memset(&ev,0,sizeof(struct epoll_event));
202 ev.data.fd = client_fd;
203 ev.events = event;//EPOLLIN | EPOLLERR | EPOLLET;
204 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
205 }
206 }
207 else if(epoll_events[i].data.fd > 0) // Client data arrive.
208 {
209 char buff[1024] = {0};
210 int len = read(epoll_events[i].data.fd, buff, sizeof(buff));
211 if(len > 0) {
212 gnss_msg_process(epoll_events[i].data.fd, buff, len);
213 }
214 }
215 else
216 {
217 LOGE("Unknown socket : %d", epoll_events[i].data.fd);
218 }
219 }
220 else
221 {
222 LOGE("Unknown event : %x", epoll_events[i].events);
223 }
224 }
225 }
226 else
227 {
228 LOGE("epoll_wait() fail[%d].", errno);
229 }
230 }
231
232 return NULL;
233}
234
235
236int gnss_ipc_service_start()
237{
238 struct sockaddr_un server_addr;
239 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
240 if(sock_listen_fd < 0)
241 {
242 LOGE("socket() fail[%d].", errno);
243 return -1;
244 }
245
246 // Set O_NONBLOCK
247 int flags = fcntl(sock_listen_fd, F_GETFL, 0);
248 if (flags < 0)
249 {
250 LOGE("Get flags error:%d", errno);
251 goto error;
252 }
253 flags |= O_NONBLOCK;
254 if (fcntl(sock_listen_fd, F_SETFL, flags) < 0)
255 {
256 LOGE("Set flags error:%d", errno);
257 goto error;
258 }
259
260 unlink(GNSS_SOCK_PATH);
261 memset(&server_addr, 0, sizeof(struct sockaddr_un));
262 server_addr.sun_family = AF_LOCAL;
263 strcpy(server_addr.sun_path, GNSS_SOCK_PATH);
264 if(bind(sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
265 {
266 LOGE("bind() fail[%d].", errno);
267 goto error;
268 }
269
270 if(listen(sock_listen_fd, SOCK_CLIENT_MAX))
271 {
272 LOGE("listen() fail[%d].", errno);
273 goto error;
274 }
275
276 pthread_t pid;
277 pthread_attr_t thread_attr;
278 pthread_attr_init(&thread_attr);
279 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
280 {
281 LOGE("pthread_attr_setdetachstate() fail.");
282 goto error;
283 }
284
285 if(pthread_create(&pid, &thread_attr, gnss_ser_pthread, NULL))
286 {
287 LOGE("pthread_create() fail.");
288 goto error;
289 }
290
291 LOGD("GNSS IPC service is running...");
292 return 0;
293error:
294 close(sock_listen_fd);
295 return -1;
296}
297