blob: a8dece89e91248317875d117ae898ae77d1c7847 [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{
54 // gnss_init:x
55 if(memcmp(msg, "gnss_init", 9) == 0) {
56 int init_mode = atoi(msg + 10);
57 LOGD("init_mode=%d", init_mode);
58 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.
65 ret = gnss_init(0);
66 }
67 }
68
69 char rsp[100] = {0};
70 sprintf(rsp, "gnss_init:%d", ret);
71 gnss_write(fd, rsp, strlen(rsp));
72 } else if(memcmp(msg, "gnss_deinit", 11) == 0) {
73 LOGD("gnss_deinit");
74 int ret = gnss_deinit();
75
76 char rsp[100] = {0};
77 sprintf(rsp, "gnss_deinit:%d", ret);
78 gnss_write(fd, rsp, strlen(rsp));
79 } else {
80 LOGW("Unknown gnss msg : %s", msg);
81 }
82}
83
84static void* gnss_ser_pthread(void* arg)
85{
86 UNUSED(arg);
87 int epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1);
88 if(epoll_fd < 0)
89 {
90 LOGE("epoll_create() fail[%d].", errno);
91 return NULL;
92 }
93
94 int i = 0;
95 while(i < SOCK_CLIENT_MAX) {
96 sock_cli_fds[i++] = -1;
97 }
98
99 uint32 event = EPOLLIN | EPOLLET;
100 struct epoll_event ev;
101 ev.data.fd = sock_listen_fd;
102 ev.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
103 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock_listen_fd, &ev);
104
105 int nready = -1;
106 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
107 while(1)
108 {
109 nready = epoll_wait(epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
110 if(nready > 0)
111 {
112 for(i = 0; i < nready; i++)
113 {
114 LOGD("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
115 if(epoll_events[i].events & EPOLLHUP) // Client Close.
116 {
117 int index = gnss_cli_fd_find(epoll_events[i].data.fd);
118 if(index != -1)
119 {
120 memset(&ev,0,sizeof(struct epoll_event));
121 ev.data.fd = epoll_events[i].data.fd;
122 ev.events = EPOLLIN | EPOLLERR | EPOLLET;
123 epoll_ctl(epoll_fd, EPOLL_CTL_DEL, epoll_events[i].data.fd, &ev);
124
125 close(epoll_events[i].data.fd);
126 sock_cli_fds[index] = -1;
127 }
128 else
129 {
130 LOGE("Unknown client[fd = %d].", epoll_events[i].data.fd);
131 }
132 }
133 else if(epoll_events[i].events & EPOLLIN)
134 {
135 if(epoll_events[i].data.fd == sock_listen_fd) // New clients connected.
136 {
137 int client_fd = -1;
138 while(1)
139 {
140 struct sockaddr_in cliaddr;
141 socklen_t clilen = sizeof(cliaddr);
142 client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen);
143 if(client_fd <= 0)
144 {
145 if(errno == EAGAIN)
146 {
147 LOGE("All client connect get.");
148 }
149 else
150 {
151 LOGE("accept() error[%d].", errno);
152 }
153 break;
154 } else {
155 i = 0;
156 while(i < SOCK_CLIENT_MAX) {
157 if(sock_cli_fds[i] <= 0) {
158 sock_cli_fds[i] = client_fd;
159 break;
160 }
161 i++;
162 }
163
164 if(i >= SOCK_CLIENT_MAX) {
165 LOGE("Client is full.");
166 break;
167 }
168 }
169 // Set O_NONBLOCK
170 int flags = fcntl(client_fd, F_GETFL, 0);
171 if (flags > 0)
172 {
173 flags |= O_NONBLOCK;
174 if (fcntl(client_fd, F_SETFL, flags) < 0)
175 {
176 LOGE("Set flags error:%d", errno);
177 }
178 }
179
180 memset(&ev,0,sizeof(struct epoll_event));
181 ev.data.fd = client_fd;
182 ev.events = event;//EPOLLIN | EPOLLERR | EPOLLET;
183 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
184 }
185 }
186 else if(epoll_events[i].data.fd > 0) // Client data arrive.
187 {
188 char buff[1024] = {0};
189 int len = read(epoll_events[i].data.fd, buff, sizeof(buff));
190 if(len > 0) {
191 gnss_msg_process(epoll_events[i].data.fd, buff, len);
192 }
193 }
194 else
195 {
196 LOGE("Unknown socket : %d", epoll_events[i].data.fd);
197 }
198 }
199 else
200 {
201 LOGE("Unknown event : %x", epoll_events[i].events);
202 }
203 }
204 }
205 else
206 {
207 LOGE("epoll_wait() fail[%d].", errno);
208 }
209 }
210
211 return NULL;
212}
213
214
215int gnss_ipc_service_start()
216{
217 struct sockaddr_un server_addr;
218 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
219 if(sock_listen_fd < 0)
220 {
221 LOGE("socket() fail[%d].", errno);
222 return -1;
223 }
224
225 // Set O_NONBLOCK
226 int flags = fcntl(sock_listen_fd, F_GETFL, 0);
227 if (flags < 0)
228 {
229 LOGE("Get flags error:%d", errno);
230 goto error;
231 }
232 flags |= O_NONBLOCK;
233 if (fcntl(sock_listen_fd, F_SETFL, flags) < 0)
234 {
235 LOGE("Set flags error:%d", errno);
236 goto error;
237 }
238
239 unlink(GNSS_SOCK_PATH);
240 memset(&server_addr, 0, sizeof(struct sockaddr_un));
241 server_addr.sun_family = AF_LOCAL;
242 strcpy(server_addr.sun_path, GNSS_SOCK_PATH);
243 if(bind(sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
244 {
245 LOGE("bind() fail[%d].", errno);
246 goto error;
247 }
248
249 if(listen(sock_listen_fd, SOCK_CLIENT_MAX))
250 {
251 LOGE("listen() fail[%d].", errno);
252 goto error;
253 }
254
255 pthread_t pid;
256 pthread_attr_t thread_attr;
257 pthread_attr_init(&thread_attr);
258 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
259 {
260 LOGE("pthread_attr_setdetachstate() fail.");
261 goto error;
262 }
263
264 if(pthread_create(&pid, &thread_attr, gnss_ser_pthread, NULL))
265 {
266 LOGE("pthread_create() fail.");
267 goto error;
268 }
269
270 LOGD("GNSS IPC service is running...");
271 return 0;
272error:
273 close(sock_listen_fd);
274 return -1;
275}
276