blob: 8b91df22fe045b515e1284b3c94b1d538cf71430 [file] [log] [blame]
b.liu8f231a12024-05-31 17:55:06 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
5#include <pthread.h>
6#include <fcntl.h>
7#include <libubox/ustream.h>
8#include <libubus.h>
9#include <signal.h>
10#include <cutils/properties.h>
b.liu778645e2024-06-21 16:47:42 +080011#include <ctype.h>
b.liu978f5432024-07-01 18:04:18 +080012#include <termios.h>
b.liu8f231a12024-05-31 17:55:06 +080013
14#include "mbtk_type.h"
15#include "mbtk_log.h"
16#include "gnss_info.h"
b.liu778645e2024-06-21 16:47:42 +080017#include "gnss_utils.h"
b.liu8f231a12024-05-31 17:55:06 +080018#include "gnss_6228.h"
b.liuf9fbfa12024-06-14 15:53:59 +080019#include "gnss_hd8122.h"
b.liu99c645d2024-06-20 10:52:15 +080020#include "gnss_asr5311.h"
b.liu42f558e2024-07-18 14:06:49 +080021#include "gnss_n50db.h"
yq.wang069ea922024-09-08 19:29:35 -070022#include "gnss_log.h"
b.liu8f231a12024-05-31 17:55:06 +080023
24#define GNSS_DEBUG 1
b.liud0ba7152024-06-19 14:47:21 +080025#define GNSS_UBUS_ENABLE 1
b.liu8f231a12024-05-31 17:55:06 +080026
27#define GNSS_TAG "MBTK_GNSS"
28#define GNSS_BUFF_SIZE 2048
29#define MBTK_PROP_GNSS_LOG "persist.mbtk.gnss_log_enable"
30#define GNSS_PORT_PTY "/dev/tty_gnss_nmea"
31#define GNSS_PORT_USB_AT "/dev/ttyGS0"
32#define GNSS_PORT_USB_NMEA "/dev/ttymodem0"
33#define GNSS_PORT_UART_AT "/dev/ttyS1"
b.liue77ac3a2024-07-17 17:36:57 +080034#define GNSS_CLI_IND_MAX 10
b.liu8f231a12024-05-31 17:55:06 +080035
36#ifdef GNSS_DEBUG
37#define GNSS_NMEA_FILE_LOG "/tmp/mbtk_gnss_nmea.log"
b.liu8f231a12024-05-31 17:55:06 +080038#define GNSS_FILE_LOG "/tmp/mbtk_gnss.log"
b.liu99c645d2024-06-20 10:52:15 +080039#define GNSS_FILE_LOG_MAX 104857600 // 100MB
b.liu8f231a12024-05-31 17:55:06 +080040#endif
41
b.liua1b69262024-09-04 16:32:59 +080042#define MBTK_GNSS_PTY_AUTO_INIT 1
43
b.liubcf86c92024-08-19 19:48:28 +080044gnss_info_t gnss_info;
b.liu8f231a12024-05-31 17:55:06 +080045
b.liud0ba7152024-06-19 14:47:21 +080046#ifdef MBTK_GNSS_UBUS_ENABLE
b.liu8f231a12024-05-31 17:55:06 +080047struct ubus_context *gnss_ubus_init(void);
b.liu5f950c52024-06-15 20:13:12 +080048#else
49int gnss_ipc_service_start();
50#endif
51
b.liu8f231a12024-05-31 17:55:06 +080052int gnss_init_config(int fd);
53
wangyouqiangfa897f82024-06-27 09:44:55 +080054static char nmea_buff[GNSS_BUFF_SIZE*4] = {0};
55static char data_buff[GNSS_BUFF_SIZE*4] = {0};
b.liud0ba7152024-06-19 14:47:21 +080056static uint32 nmea_buff_len = 0;
57static uint32 data_buff_len = 0;
b.liue77ac3a2024-07-17 17:36:57 +080058static gnss_ind_info_t ind_info[GNSS_CLI_IND_MAX];
b.liud0ba7152024-06-19 14:47:21 +080059
b.liu8f231a12024-05-31 17:55:06 +080060static bool nmea_found = FALSE;
61#ifdef GNSS_DEBUG
62static bool nmea_log_enable = FALSE;
63static int nmea_log_fd = -1;
b.liu99c645d2024-06-20 10:52:15 +080064static int debug_fd = -1;
65static int debug_fd_len = 0;
b.liu8f231a12024-05-31 17:55:06 +080066#endif
yq.wang5c647e02024-08-10 00:01:11 -070067
b.liua1b69262024-09-04 16:32:59 +080068#if MBTK_GNSS_PTY_AUTO_INIT
69static bool gnss_pty_print_enable = FALSE;
70#endif
b.liu8f231a12024-05-31 17:55:06 +080071static int gnss_pty_master_fd = -1;
72static int gnss_pty_slave_fd = -1;
73static int gnss_usb_at_port_fd = -1;
74static int gnss_usb_nmea_port_fd = -1;
75static int gnss_uart_at_port_fd = -1;
76static char *gnss_filter_info[] = {"RMC", "VTG", "GGA", "GSA", "GSV", "GLL", "ZDA", "GST", "TXT", "DHV", "DTM", NULL};
77
b.liue77ac3a2024-07-17 17:36:57 +080078int gnss_write(int fd, const void* buf, int buf_len);
79
b.liu8f231a12024-05-31 17:55:06 +080080static void help()
81{
b.liu42f558e2024-07-18 14:06:49 +080082 LOGD("mbtk_gnssd <6228/8122/5311/N50DB> <gnss_dev> <0/1>");
b.liu8f231a12024-05-31 17:55:06 +080083}
84
85static int arg_check(int argc, char *argv[])
86{
87 if(argc != 4) {
b.liu4ae41182024-06-28 16:30:15 +080088 LOGE("argc = %d", argc);
b.liu8f231a12024-05-31 17:55:06 +080089 goto check_fail;
90 }
91
b.liu99c645d2024-06-20 10:52:15 +080092 // Only support 6228/8122/5311.
b.liu42f558e2024-07-18 14:06:49 +080093 if(strcmp(argv[1], GNSS_ID_6228) && strcmp(argv[1], GNSS_ID_8122) && strcmp(argv[1], GNSS_ID_5311)
94 && strcmp(argv[1], GNSS_ID_N50DB)) {
b.liu4ae41182024-06-28 16:30:15 +080095 LOGE("argv[1] = %s", argv[1]);
b.liu8f231a12024-05-31 17:55:06 +080096 goto check_fail;
97 }
98
99 if(access(argv[2], R_OK | W_OK)) {
b.liu4ae41182024-06-28 16:30:15 +0800100 LOGE("access(%s) rw fail. ", argv[2]);
b.liu8f231a12024-05-31 17:55:06 +0800101 goto check_fail;
102 }
103
b.liuced8dd02024-06-28 13:28:29 +0800104#if 0
105 int init_mode = atoi(argv[3]);
106 if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) != init_mode) {
b.liu8f231a12024-05-31 17:55:06 +0800107 goto check_fail;
108 }
b.liuced8dd02024-06-28 13:28:29 +0800109#endif
b.liu8f231a12024-05-31 17:55:06 +0800110
111 return 0;
112check_fail:
113 help();
114 return -1;
115}
116
117static int gnss_ports_open(uint32 print_port)
118{
b.liuced8dd02024-06-28 13:28:29 +0800119 // TTY AT change.
120 if((gnss_info.print_port & GNSS_PRINT_PORT_TTY_AT) != (print_port & GNSS_PRINT_PORT_TTY_AT)) {
121 if(print_port & GNSS_PRINT_PORT_TTY_AT) { // Open
b.liua1b69262024-09-04 16:32:59 +0800122#if MBTK_GNSS_PTY_AUTO_INIT
123 gnss_pty_print_enable = TRUE;
124#else
b.liuced8dd02024-06-28 13:28:29 +0800125 if(gnss_pty_open(&gnss_pty_master_fd, &gnss_pty_slave_fd, GNSS_PORT_PTY)) {
126 return GNSS_ERR_OPEN_DEV;
127 }
b.liua1b69262024-09-04 16:32:59 +0800128#endif
b.liuced8dd02024-06-28 13:28:29 +0800129 LOGD("Open PTY port success.");
130 } else { // Close
b.liua1b69262024-09-04 16:32:59 +0800131#if MBTK_GNSS_PTY_AUTO_INIT
132 gnss_pty_print_enable = FALSE;
133#else
b.liuced8dd02024-06-28 13:28:29 +0800134 if(gnss_pty_slave_fd > 0) {
135 close(gnss_pty_slave_fd);
136 gnss_pty_slave_fd = -1;
137 unlink(GNSS_PORT_PTY);
138 }
b.liua1b69262024-09-04 16:32:59 +0800139#endif
b.liuced8dd02024-06-28 13:28:29 +0800140 LOGD("Close PTY port success.");
b.liu8f231a12024-05-31 17:55:06 +0800141 }
b.liu8f231a12024-05-31 17:55:06 +0800142 }
143
b.liuced8dd02024-06-28 13:28:29 +0800144 // USB AT change.
145 if((gnss_info.print_port & GNSS_PRINT_PORT_USB_AT) != (print_port & GNSS_PRINT_PORT_USB_AT)) {
146 if(print_port & GNSS_PRINT_PORT_USB_AT) { // Open
b.liu978f5432024-07-01 18:04:18 +0800147 if((gnss_usb_at_port_fd = gnss_port_open(GNSS_PORT_USB_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) {
b.liuced8dd02024-06-28 13:28:29 +0800148 return GNSS_ERR_OPEN_DEV;
149 }
150 LOGD("Open USB AT port success.");
151 } else { // Close
152 if(gnss_usb_at_port_fd > 0) {
153 close(gnss_usb_at_port_fd);
154 gnss_usb_at_port_fd = -1;
155 }
156 LOGD("Close USB AT port success.");
b.liu8f231a12024-05-31 17:55:06 +0800157 }
b.liu8f231a12024-05-31 17:55:06 +0800158 }
159
b.liuced8dd02024-06-28 13:28:29 +0800160 // USB NMEA change.
161 if((gnss_info.print_port & GNSS_PRINT_PORT_USB_NMEA) != (print_port & GNSS_PRINT_PORT_USB_NMEA)) {
162 if(print_port & GNSS_PRINT_PORT_USB_NMEA) { // Open
b.liu978f5432024-07-01 18:04:18 +0800163 if((gnss_usb_nmea_port_fd = gnss_port_open(GNSS_PORT_USB_NMEA, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) {
b.liuced8dd02024-06-28 13:28:29 +0800164 return GNSS_ERR_OPEN_DEV;
165 }
166 LOGD("Open USB NMEA port success.");
167 } else { // Close
168 if(gnss_usb_nmea_port_fd > 0) {
169 close(gnss_usb_nmea_port_fd);
170 gnss_usb_nmea_port_fd = -1;
171 }
172 LOGD("Close USB NMEA port success.");
b.liu8f231a12024-05-31 17:55:06 +0800173 }
b.liu8f231a12024-05-31 17:55:06 +0800174 }
175
b.liuced8dd02024-06-28 13:28:29 +0800176 // Uart AT change.
177 if((gnss_info.print_port & GNSS_PRINT_PORT_UART1) != (print_port & GNSS_PRINT_PORT_UART1)) {
178 if(print_port & GNSS_PRINT_PORT_UART1) { // Open
179 if((gnss_uart_at_port_fd = gnss_port_open(GNSS_PORT_UART_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) {
180 return GNSS_ERR_OPEN_DEV;
181 }
182 LOGD("Open UART AT port success.");
183 } else { // Close
184 if(gnss_uart_at_port_fd > 0) {
185 close(gnss_uart_at_port_fd);
186 gnss_uart_at_port_fd = -1;
187 }
188 LOGD("Close UART AT port success.");
b.liu8f231a12024-05-31 17:55:06 +0800189 }
b.liu8f231a12024-05-31 17:55:06 +0800190 }
191
b.liuced8dd02024-06-28 13:28:29 +0800192 gnss_info.print_port = print_port;
193
b.liudbc3f4b2024-06-25 18:22:24 +0800194 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800195}
196
197static int gnss_ports_close()
198{
199 if(gnss_usb_at_port_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800200 tcflush(gnss_usb_at_port_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800201 close(gnss_usb_at_port_fd);
202 gnss_usb_at_port_fd = -1;
203 }
204
205 if(gnss_usb_nmea_port_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800206 tcflush(gnss_usb_nmea_port_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800207 close(gnss_usb_nmea_port_fd);
208 gnss_usb_nmea_port_fd = -1;
209 }
210
211 if(gnss_uart_at_port_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800212 tcflush(gnss_uart_at_port_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800213 close(gnss_uart_at_port_fd);
214 gnss_uart_at_port_fd = -1;
215 }
b.liua1b69262024-09-04 16:32:59 +0800216#if MBTK_GNSS_PTY_AUTO_INIT
217 gnss_pty_print_enable = FALSE;
218#else
b.liu8f231a12024-05-31 17:55:06 +0800219 if(gnss_pty_master_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800220 tcflush(gnss_pty_master_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800221 close(gnss_pty_master_fd);
222 gnss_pty_master_fd = -1;
223 }
224
225 if(gnss_pty_slave_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800226 tcflush(gnss_pty_slave_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800227 close(gnss_pty_slave_fd);
228 gnss_pty_slave_fd = -1;
229 unlink(GNSS_PORT_PTY);
230 }
b.liua1b69262024-09-04 16:32:59 +0800231#endif
b.liuced8dd02024-06-28 13:28:29 +0800232 gnss_info.print_port = 0;
233
b.liu8f231a12024-05-31 17:55:06 +0800234 return 0;
235}
236
b.liu8f231a12024-05-31 17:55:06 +0800237#ifdef GNSS_DEBUG
b.liu99c645d2024-06-20 10:52:15 +0800238static void log_save(int fd, const char *data, int data_len)
239{
b.liu8f231a12024-05-31 17:55:06 +0800240 if(nmea_log_enable){
b.liu99c645d2024-06-20 10:52:15 +0800241 if(0 /* debug_fd_len > GNSS_FILE_LOG_MAX */) {
242 LOGD("Reopen file:%s(len = %d)", GNSS_FILE_LOG, debug_fd_len);
243 close(debug_fd);
244 debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
245 if(debug_fd < 0) {
246 LOGE("Open debug fd fail.");
247 }
248 debug_fd_len = 0;
249
250 LOGD("Reopen file:%s", GNSS_NMEA_FILE_LOG);
b.liu8f231a12024-05-31 17:55:06 +0800251 close(nmea_log_fd);
252 nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
253 if(nmea_log_fd < 0) {
254 LOGE("Open debug fd fail.");
255 }
b.liu8f231a12024-05-31 17:55:06 +0800256 }
257
b.liu99c645d2024-06-20 10:52:15 +0800258 if(fd == nmea_log_fd) {
259 if(nmea_log_fd > 0) {
260 write(nmea_log_fd, data, data_len);
261 debug_fd_len += data_len;
262 }
263 } else if(fd == debug_fd) {
264 if(debug_fd > 0) {
265 write(debug_fd, data, data_len);
266 debug_fd_len += data_len;
267 }
b.liu8f231a12024-05-31 17:55:06 +0800268 }
269 }
b.liu99c645d2024-06-20 10:52:15 +0800270}
b.liu8f231a12024-05-31 17:55:06 +0800271#endif
272
b.liue77ac3a2024-07-17 17:36:57 +0800273static void ind_location_print(const char *data)
274{
275 int index = 0;
276 int buff_len = 0;
277 char buff[GNSS_BUFF_SIZE] = {0};
278 buff[0] = MBTK_IND_START_FLAG;
279 buff_len++;
280 memcpy(buff + 1, MBTK_IND_LOCATION_TAG, strlen(MBTK_IND_LOCATION_TAG));
281 buff_len += strlen(MBTK_IND_LOCATION_TAG);
282 memcpy(buff + strlen(buff), data, strlen(data));
283 buff_len += strlen(data);
284 buff[strlen(buff)] = MBTK_IND_END_FLAG;
285 buff_len++;
286
287 while(index < GNSS_CLI_IND_MAX) {
288 if(ind_info[index].cli_fd > 0 && (ind_info[index].ind_flag & MBTK_GNSS_IND_LOCATION)) {
289 gnss_write(ind_info[index].cli_fd, buff, buff_len);
290 }
291 index++;
292 }
293}
294
295static void ind_nmea_print(const char *data)
296{
297 int index = 0;
298 int buff_len = 0;
299 char buff[GNSS_BUFF_SIZE] = {0};
300 buff[0] = MBTK_IND_START_FLAG;
301 buff_len++;
302 memcpy(buff + 1, MBTK_IND_NMEA_TAG, strlen(MBTK_IND_NMEA_TAG));
303 buff_len += strlen(MBTK_IND_NMEA_TAG);
304 memcpy(buff + strlen(buff), data, strlen(data));
305 buff_len += strlen(data);
306 buff[strlen(buff)] = MBTK_IND_END_FLAG;
307 buff_len++;
308 while(index < GNSS_CLI_IND_MAX) {
309 if(ind_info[index].cli_fd > 0 && (ind_info[index].ind_flag & MBTK_GNSS_IND_NMEA)) {
310 gnss_write(ind_info[index].cli_fd, buff, buff_len);
311 }
312 index++;
313 }
314}
315
b.liu99c645d2024-06-20 10:52:15 +0800316static void nmea_print(const char *nmea, int nmea_len)
317{
yq.wang1ddd1fd2024-07-25 23:00:14 -0700318 int ret = -1;
b.liu8f231a12024-05-31 17:55:06 +0800319 if(gnss_usb_at_port_fd > 0) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700320 ret = write(gnss_usb_at_port_fd, nmea, nmea_len);
b.liu8f231a12024-05-31 17:55:06 +0800321 }
322
323 if(gnss_usb_nmea_port_fd > 0) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700324 ret = write(gnss_usb_nmea_port_fd, nmea, nmea_len);
b.liu8f231a12024-05-31 17:55:06 +0800325 }
326
327 if(gnss_uart_at_port_fd > 0) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700328 ret = write(gnss_uart_at_port_fd, nmea, nmea_len);
b.liu8f231a12024-05-31 17:55:06 +0800329 }
330
331 if(gnss_pty_master_fd > 0) {
b.liua1b69262024-09-04 16:32:59 +0800332#if MBTK_GNSS_PTY_AUTO_INIT
333 if(gnss_pty_print_enable) {
334 ret = write(gnss_pty_master_fd, nmea, nmea_len);
335 }
336#else
yq.wang1ddd1fd2024-07-25 23:00:14 -0700337 ret = write(gnss_pty_master_fd, nmea, nmea_len);
b.liua1b69262024-09-04 16:32:59 +0800338#endif
b.liu8f231a12024-05-31 17:55:06 +0800339 }
yq.wang937355d2024-07-26 01:24:42 -0700340 ind_nmea_print(nmea);
341 ind_location_print(nmea);
b.liu8f231a12024-05-31 17:55:06 +0800342}
343
344static unsigned char nmea_checksum(const char *nmea)
345{
346 const char *p = nmea;
347 unsigned char chs = 0;
348
349 while (*p == '$') // skip '$'
350 p++;
351 while (*p != '*' && *p != 0)
352 chs ^= *p++;
353
354 return chs;
355}
356
357static bool nmea_check(const char *nmea, int nmea_len)
358{
359 char **ptr = gnss_filter_info;
360 while(*ptr) {
361 if(strstr(nmea, *ptr)) {
362 break;
363 }
364 ptr++;
365 }
366
367 if(*ptr == NULL) {
368 LOGD("Unknown NMEA[%d]:%s", nmea_len, nmea);
369 return FALSE;
370 }
371
372 char *checksum_str = strstr(nmea, "*");
373 checksum_str++; // Jump '*'
374 char checksum_buf[3] = {0};
375 snprintf(checksum_buf, 3, "%02x", nmea_checksum(nmea));
376 if(strncasecmp(checksum_buf, checksum_str, 2)) {
377 LOGD("Checksum error[%d](checksum - %s):%s", nmea_len, checksum_buf, nmea);
378 return FALSE;
379 }
380
381 return TRUE;
382}
383
384static void gnss_nmea_process(const char *data, int data_len)
385{
luojiand7960682024-08-02 16:50:01 +0800386// LOGD("gnss_nmea_process() : data_len - %d", data_len);
b.liu99c645d2024-06-20 10:52:15 +0800387#if 0
b.liu8f231a12024-05-31 17:55:06 +0800388 char nmea[GNSS_BUFF_SIZE] = {0};
389 memcpy(nmea, data, data_len);
b.liu99c645d2024-06-20 10:52:15 +0800390#else
391 const char *nmea = data;
392#endif
b.liu8f231a12024-05-31 17:55:06 +0800393
394 if(!nmea_check(nmea, data_len)) {
b.liu42f558e2024-07-18 14:06:49 +0800395 // No print "$HOSTSLEEP".
396 if(memcmp(nmea, "$HOSTSLEEP", strlen("$HOSTSLEEP"))) {
397 LOGD("NO-NMEA:%s", nmea);
398 }
b.liu99c645d2024-06-20 10:52:15 +0800399#if GNSS_DEBUG
400 log_save(nmea_log_fd, "/**/", 4);
401 log_save(nmea_log_fd, nmea, data_len);
402#endif
403 if(gnss_info.gnss_set_cb)
404 gnss_info.gnss_set_cb(nmea, data_len);
b.liu8f231a12024-05-31 17:55:06 +0800405 return;
406 }
407
408#ifdef GNSS_DEBUG
409 if(nmea_log_enable) {
410 LOGD("NMEA[%d]:%s", data_len, nmea);
411 }
b.liu99c645d2024-06-20 10:52:15 +0800412
413 log_save(nmea_log_fd, nmea, data_len);
b.liu8f231a12024-05-31 17:55:06 +0800414#endif
415
yq.wang069ea922024-09-08 19:29:35 -0700416#if MBTK_GNSS_PARAM_PARSE
417 gnss_ind_nmea_parse(nmea, data_len);
418#endif
luojiand7960682024-08-02 16:50:01 +0800419
b.liu8f231a12024-05-31 17:55:06 +0800420 nmea_print(nmea, data_len);
421}
422
423#if 0
424static void gnss_cmd_rsp_process(const char *data, int data_len)
425{
426 char rsp[GNSS_BUFF_SIZE] = {0};
427 memcpy(rsp, data, data_len);
428 LOGD("RSP[%d]:%s", data_len, rsp);
429}
430#endif
431
432static bool nmea_char_check(char ch)
433{
434 if(isalnum(ch) || ch == '$' || ch == '\r' || ch == '\n' || ch == '.'
yq.wang1ddd1fd2024-07-25 23:00:14 -0700435 || ch == ',' || ch == '*' || ch == '\0' || ch == '/' || ch == '_' || ch == '=' || ch == '-')
b.liu8f231a12024-05-31 17:55:06 +0800436 return TRUE;
437
438 return FALSE;
439}
440
441static void gnss_data_process(const char *data, int data_len)
442{
443 if(gnss_info.state == GNSS_STATE_OPEN) {
444 LOGD("GNSS_OPEN[%d]:%s", data_len, data);
445 } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) {
446 // LOGD("GNSS_DL[%d]:%s", data_len, data);
b.liu99c645d2024-06-20 10:52:15 +0800447 if(gnss_info.gnss_dl_read_cb) {
448 gnss_info.gnss_dl_read_cb(data, data_len);
449 }
b.liu8f231a12024-05-31 17:55:06 +0800450 } else if(gnss_info.state == GNSS_STATE_READY) {
451 int index = 0;
452 while(index < data_len) {
453 if(nmea_found) {
454 if(!nmea_char_check(data[index])) {
b.liud0ba7152024-06-19 14:47:21 +0800455 // Copy nmea_buff to data_buff
456 // Start with '$', but not nmea data, so copy to data_buff.
457 memcpy(data_buff + data_buff_len, nmea_buff, nmea_buff_len);
458 data_buff_len += nmea_buff_len;
459 data_buff[data_buff_len++] = data[index];
460
461 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800462 nmea_found = FALSE;
b.liu8f231a12024-05-31 17:55:06 +0800463 continue;
464 }
465
466 if(data[index] != '\0') {
b.liud0ba7152024-06-19 14:47:21 +0800467 nmea_buff[nmea_buff_len++] = data[index];
468 if(nmea_buff[nmea_buff_len - 1] == '\n') {
469 if(data_buff_len > 0) {
b.liu99c645d2024-06-20 10:52:15 +0800470#if GNSS_DEBUG
471 log_save(nmea_log_fd, "/**/", 4);
472 log_save(nmea_log_fd, data_buff, data_buff_len);
473#endif
474 if(gnss_info.gnss_set_cb) {
475 gnss_info.gnss_set_cb(data_buff, data_buff_len);
476 }
b.liud0ba7152024-06-19 14:47:21 +0800477 data_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800478 }
479
b.liud0ba7152024-06-19 14:47:21 +0800480 if(nmea_buff_len > 6 && nmea_buff[nmea_buff_len - 5] == '*') { // $XXX*YY\r\n
b.liu99c645d2024-06-20 10:52:15 +0800481 nmea_buff[nmea_buff_len] = '\0';
b.liud0ba7152024-06-19 14:47:21 +0800482 gnss_nmea_process(nmea_buff, nmea_buff_len);
b.liu99c645d2024-06-20 10:52:15 +0800483 } else if(nmea_buff_len > 0) {
484 nmea_buff[nmea_buff_len] = '\0';
b.liu42f558e2024-07-18 14:06:49 +0800485 if(memcmp(nmea_buff, "$HOSTSLEEP", strlen("$HOSTSLEEP"))) {
486 LOGD("NO-NMEA:%s", nmea_buff);
487 }
b.liu99c645d2024-06-20 10:52:15 +0800488#if GNSS_DEBUG
489 log_save(nmea_log_fd, "/**/", 4);
490 log_save(nmea_log_fd, nmea_buff, nmea_buff_len);
491#endif
b.liud0ba7152024-06-19 14:47:21 +0800492 }
493
494 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800495 nmea_found = FALSE;
496 }
497 }
498 } else {
499 if(data[index] == '$') {
b.liud0ba7152024-06-19 14:47:21 +0800500 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800501 nmea_found = TRUE;
b.liud0ba7152024-06-19 14:47:21 +0800502 nmea_buff[nmea_buff_len++] = data[index];
503 } else {
504 data_buff[data_buff_len++] = data[index];
b.liu8f231a12024-05-31 17:55:06 +0800505 }
506 }
507 index++;
508 }
509 } else {
510 LOGW("Unknown state : %d", gnss_info.state);
511 }
512}
513
514void* gnss_read_pthread(void* arg)
515{
yq.wang069ea922024-09-08 19:29:35 -0700516#if MBTK_GNSS_TEST_LOG
517 gnss_test_log("gnss_read_pthread enter.");
518#endif
b.liu8f231a12024-05-31 17:55:06 +0800519 LOGD("gnss_read_pthread enter.");
yq.wang069ea922024-09-08 19:29:35 -0700520
b.liu8f231a12024-05-31 17:55:06 +0800521 char buffer[GNSS_BUFF_SIZE];
522 int len = 0;
523 int ret = 0;
524 fd_set fdr, fdw;
525 int fd_max = 0;
526
b.liu8f231a12024-05-31 17:55:06 +0800527 fd_max = (gnss_info.fd > fd_max) ? gnss_info.fd : fd_max;
b.liu8f231a12024-05-31 17:55:06 +0800528 fd_max = (gnss_info.exit_fd[0] > fd_max) ? gnss_info.exit_fd[0] : fd_max;
b.liud0ba7152024-06-19 14:47:21 +0800529 memset(nmea_buff, 0, sizeof(nmea_buff));
530 memset(data_buff, 0, sizeof(data_buff));
531 nmea_buff_len = 0;
532 data_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800533#if GNSS_DEBUG
b.liu8f231a12024-05-31 17:55:06 +0800534 if(nmea_log_enable) {
535 debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
536 if(debug_fd < 0) {
537 LOGE("Open debug fd fail.");
538 }
539 nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
540 if(nmea_log_fd < 0) {
541 LOGE("Open nmea fd fail.");
542 }
b.liu99c645d2024-06-20 10:52:15 +0800543 debug_fd_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800544 }
545#endif
546
yq.wang069ea922024-09-08 19:29:35 -0700547#if MBTK_GNSS_TEST_LOG
548 gnss_test_log("uart_fd - %d, exit_fd - %d", gnss_info.fd, gnss_info.exit_fd[0]);
549#endif
b.liudbc3f4b2024-06-25 18:22:24 +0800550 LOGD("uart_fd - %d, exit_fd - %d", gnss_info.fd, gnss_info.exit_fd[0]);
551
b.liu8f231a12024-05-31 17:55:06 +0800552 while(gnss_info.state >= GNSS_STATE_OPEN) {
wangyouqiang55d36bf2024-06-27 09:35:52 +0800553 FD_ZERO(&fdw);
554 FD_ZERO(&fdr);
555 FD_SET(gnss_info.fd, &fdr);
556 FD_SET(gnss_info.exit_fd[0], &fdr);
b.liu8f231a12024-05-31 17:55:06 +0800557 ret = select(fd_max + 1, &fdr, &fdw, 0, NULL);
558 //LOGD("select - %d", ret);
b.liuece0db02024-06-25 18:39:09 +0800559 if(gnss_info.state < GNSS_STATE_OPEN) {
560 LOGD("State = %d, ret = %d", gnss_info.state, ret);
561 if(ret > 0) {
562 if (FD_ISSET(gnss_info.fd, &fdr)) {
563 LOGD("gnss_fd can read.");
564 } else if (FD_ISSET(gnss_info.exit_fd[0], &fdr)) {
565 LOGD("exit_fd can read.");
566 } else {
567 LOGW("Unknown select event.");
568 }
569 }
570 break;
571 }
572
b.liu8f231a12024-05-31 17:55:06 +0800573 if (ret < 0)
574 {
575 if (errno == EINTR)
576 {
577 continue;
578 }
yq.wang069ea922024-09-08 19:29:35 -0700579#if MBTK_GNSS_TEST_LOG
580 gnss_test_log("select error, errno = %d (%s)", errno, strerror(errno));
581#endif
b.liu8f231a12024-05-31 17:55:06 +0800582 LOGE("select error, errno = %d (%s)", errno, strerror(errno));
583 break;
584 }
585 else if (ret == 0)
586 {
587 LOGE("select ret == 0");
588 break;
589 }
590
591 if (FD_ISSET(gnss_info.fd, &fdr))
592 {
593 memset(buffer, 0, GNSS_BUFF_SIZE);
b.liu99c645d2024-06-20 10:52:15 +0800594 len = read(gnss_info.fd, buffer, GNSS_BUFF_SIZE - 1);
b.liu8f231a12024-05-31 17:55:06 +0800595 if(len > 0) {
596 //log_hex("READ", buffer, len);
597
598#if GNSS_DEBUG
b.liu99c645d2024-06-20 10:52:15 +0800599 //LOGD("read data_len = %d", len);
600 log_save(debug_fd, buffer, len);
b.liu8f231a12024-05-31 17:55:06 +0800601#endif
602
603 gnss_data_process(buffer, len);
604
605 } else if(len ==0 ){
606 LOGE("Read end : len = 0");
607 break;
608 } else {
609 if(EAGAIN == errno) {
610 usleep(50000);
611 continue;
612 } else {
613 LOGD("Read ret = -1 ,errno = %d", errno);
614 break;
615 }
616 }
617 }
618 else if (FD_ISSET(gnss_info.exit_fd[0], &fdr))
619 {
b.liuece0db02024-06-25 18:39:09 +0800620 LOGD("exit_fd select event.");
b.liu8f231a12024-05-31 17:55:06 +0800621 memset(buffer, 0, GNSS_BUFF_SIZE);
b.liudbc3f4b2024-06-25 18:22:24 +0800622 len = read(gnss_info.exit_fd[0], buffer, GNSS_BUFF_SIZE);
b.liu8f231a12024-05-31 17:55:06 +0800623 if(len > 0) {
624 if(strcmp(buffer, "exit") == 0) {
625 LOGD("Get thread exit message.");
626 break;
627 }
628 }
629 }
630 else
631 {
yq.wang069ea922024-09-08 19:29:35 -0700632#if MBTK_GNSS_TEST_LOG
633 gnss_test_log("Unknown select event.");
634#endif
b.liu8f231a12024-05-31 17:55:06 +0800635 LOGW("Unknown select event.");
636 continue;
637 }
638 }
639
640#if GNSS_DEBUG
641 if(debug_fd > 0) {
642 close(debug_fd);
643 debug_fd = -1;
644 }
645 if(nmea_log_fd > 0) {
646 close(nmea_log_fd);
647 nmea_log_fd = -1;
648 }
649#endif
650
651 gnss_info.state = GNSS_STATE_CLOSE;
652 LOGD("gnss_read_pthread exit.");
653 return NULL;
654}
655
656#if 0
657int gnss_write(int fd, const void *data, int data_len)
658{
659 int count = 0;
660 int len = 0;
661 while(1)
662 {
663 len = write(fd, data + count, data_len - count);
664 if (len > 0)
665 {
666 count += len;
667 }
668 else
669 {
670 LOGE("write() fail,ret = %d,errno = %d", len, errno);
671 break;
672 }
673
674 if (count == data_len)
675 break;
676 }
677
678 return count;
679}
680#else
b.liu778645e2024-06-21 16:47:42 +0800681int gnss_write(int fd, const void* buf, int buf_len)
b.liu8f231a12024-05-31 17:55:06 +0800682{
b.liu778645e2024-06-21 16:47:42 +0800683 ssize_t size;
684 ssize_t size_to_wr;
b.liu8f231a12024-05-31 17:55:06 +0800685 ssize_t size_written;
686 if(GNSS_BUFF_SIZE < buf_len)
687 {
688 return -1;
689 }
690 for(size = 0; size < buf_len;)
691 {
692 size_to_wr = buf_len - size;
693 if( size_to_wr > GNSS_BUFF_SIZE)
694 size_to_wr = GNSS_BUFF_SIZE;
695
b.liu778645e2024-06-21 16:47:42 +0800696 size_written = write(fd, (const uint8*)buf + size, size_to_wr);
b.liu8f231a12024-05-31 17:55:06 +0800697 if (size_written==-1)
698 {
699 return -1;
700 }
701 size += size_written;
702 if(size_written != size_to_wr)
703 {
704 return size;
705 }
706 }
707 // LOGD("SEND %d / %d", size, buf_len);
708 return size;
709}
710#endif
711
712int gnss_init(uint32 print_port)
713{
yq.wang069ea922024-09-08 19:29:35 -0700714#if MBTK_GNSS_TEST_LOG
715 gnss_test_log("[gnss_init] gnss state [%d]", gnss_info.state);
716#endif
717
b.liu8f231a12024-05-31 17:55:06 +0800718 if(gnss_info.state != GNSS_STATE_CLOSE) {
719 LOGW("GNSS not close:%d", gnss_info.state);
b.liuced8dd02024-06-28 13:28:29 +0800720 if(gnss_info.state == GNSS_STATE_READY) {
721 LOGD("Reset print port : %d -> %d", gnss_info.print_port, print_port);
722 if(gnss_info.print_port != print_port) {
723 return gnss_ports_open(print_port);
724 } else {
725 return GNSS_ERR_OK;
726 }
727 } else {
728 return GNSS_ERR_OK;
729 }
b.liu8f231a12024-05-31 17:55:06 +0800730 }
731
732 int ret = 0;
b.liu99c645d2024-06-20 10:52:15 +0800733 if(gnss_info.dl_befor_open) {
734 //if(gnss_info.auto_dl_fw) {
735 gnss_info.state = GNSS_STATE_DOWNLOAD;
b.liudbc3f4b2024-06-25 18:22:24 +0800736 ret = gnss_info.gnss_fw_dl(gnss_info.fd, NULL, gnss_info.dev_name);
b.liu99c645d2024-06-20 10:52:15 +0800737 if(ret) {
738 LOGE("gnss_fw_dl() fail : %d", ret);
739 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800740 return GNSS_ERR_DL_FW;
b.liu99c645d2024-06-20 10:52:15 +0800741 }
b.liu8f231a12024-05-31 17:55:06 +0800742
b.liu99c645d2024-06-20 10:52:15 +0800743 gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name);
744 if(gnss_info.fd <= 0) {
745 LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd);
746 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800747 return GNSS_ERR_OPEN_DEV;
b.liu99c645d2024-06-20 10:52:15 +0800748 }
749 if(pipe(gnss_info.exit_fd)) {
750 LOGE("pipe() fail[%d].", errno);
b.liudbc3f4b2024-06-25 18:22:24 +0800751 return GNSS_ERR_UNKNOWN;
b.liu99c645d2024-06-20 10:52:15 +0800752 }
753 // GNSS is opened.
754 gnss_info.state = GNSS_STATE_OPEN;
b.liu8f231a12024-05-31 17:55:06 +0800755
b.liu99c645d2024-06-20 10:52:15 +0800756#if 0
757 // Start gnss read thread.
758 pthread_attr_t thread_attr;
759 pthread_attr_init(&thread_attr);
760 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
761 {
762 LOGE("pthread_attr_setdetachstate() fail.");
763 goto main_exit;
764 }
b.liu8f231a12024-05-31 17:55:06 +0800765
b.liu99c645d2024-06-20 10:52:15 +0800766 if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL))
b.liu8f231a12024-05-31 17:55:06 +0800767#else
b.liu99c645d2024-06-20 10:52:15 +0800768 if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL))
b.liu8f231a12024-05-31 17:55:06 +0800769#endif
b.liu99c645d2024-06-20 10:52:15 +0800770 {
771 LOGE("pthread_create() fail.");
772 goto exit_with_close;
773 }
b.liu8f231a12024-05-31 17:55:06 +0800774
b.liu99c645d2024-06-20 10:52:15 +0800775 ret = gnss_info.gnss_dev_open();
776 if(ret) {
777 LOGE("gnss_dev_open() fail : %d", ret);
778 goto exit_with_thread_exit;
779 }
780 //}
781 } else {
782 gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name);
783 if(gnss_info.fd <= 0) {
784 LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd);
785 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800786 return GNSS_ERR_OPEN_DEV;
b.liu99c645d2024-06-20 10:52:15 +0800787 }
788 if(pipe(gnss_info.exit_fd)) {
789 LOGE("pipe() fail[%d].", errno);
b.liudbc3f4b2024-06-25 18:22:24 +0800790 return GNSS_ERR_UNKNOWN;
b.liu99c645d2024-06-20 10:52:15 +0800791 }
792 // GNSS is opened.
793 gnss_info.state = GNSS_STATE_OPEN;
b.liu8f231a12024-05-31 17:55:06 +0800794
b.liu99c645d2024-06-20 10:52:15 +0800795#if 0
796 // Start gnss read thread.
797 pthread_attr_t thread_attr;
798 pthread_attr_init(&thread_attr);
799 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
800 {
801 LOGE("pthread_attr_setdetachstate() fail.");
802 goto main_exit;
803 }
804
805 if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL))
806#else
807 if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL))
808#endif
809 {
810 LOGE("pthread_create() fail.");
811 goto exit_with_close;
812 }
813
814 ret = gnss_info.gnss_dev_open();
b.liu8f231a12024-05-31 17:55:06 +0800815 if(ret) {
b.liu99c645d2024-06-20 10:52:15 +0800816 LOGE("gnss_dev_open() fail : %d", ret);
817 goto exit_with_thread_exit;
818 }
b.liu8f231a12024-05-31 17:55:06 +0800819 }
820
821 // GNSS is ready, NMEA can print from uart.
822 gnss_info.state = GNSS_STATE_READY;
b.liu8f231a12024-05-31 17:55:06 +0800823
824 LOGD("GNSS open success.");
825
yq.wang069ea922024-09-08 19:29:35 -0700826#if MBTK_GNSS_TEST_LOG
827 gnss_test_log("gnss open success.");
828#endif
829
b.liuced8dd02024-06-28 13:28:29 +0800830 return gnss_ports_open(print_port);
b.liu8f231a12024-05-31 17:55:06 +0800831
b.liu8f231a12024-05-31 17:55:06 +0800832exit_with_thread_exit:
833 gnss_info.state = GNSS_STATE_CLOSING;
834 // Wait for read thread exit.
835 ret = pthread_join(gnss_info.read_pid, NULL);
836 if(ret){
837 LOGE("pthrad_join fail(%d)",ret);
838 }
839exit_with_close:
840 if(gnss_info.gnss_close(gnss_info.fd)) {
841 LOGE("gnss_close() fail.");
842 }
843 if(gnss_info.exit_fd[0] > 0) {
844 close(gnss_info.exit_fd[0]);
845 gnss_info.exit_fd[0] = -1;
846 }
847 if(gnss_info.exit_fd[1] > 0) {
848 close(gnss_info.exit_fd[1]);
849 gnss_info.exit_fd[1] = -1;
850 }
851 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800852 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800853}
854
855int gnss_deinit()
856{
yq.wang069ea922024-09-08 19:29:35 -0700857#if MBTK_GNSS_TEST_LOG
858 gnss_test_log("[gnss_deinit] gnss state [%d]", gnss_info.state);
859#endif
860
b.liu8f231a12024-05-31 17:55:06 +0800861 if(gnss_info.state == GNSS_STATE_CLOSE) {
862 LOGW("GNSS is closed.");
b.liudbc3f4b2024-06-25 18:22:24 +0800863 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800864 } else if(gnss_info.state == GNSS_STATE_CLOSING) {
865 LOGW("GNSS is closing...");
b.liudbc3f4b2024-06-25 18:22:24 +0800866 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800867 } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) {
868 LOGW("GNSS is downloading...");
b.liudbc3f4b2024-06-25 18:22:24 +0800869 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800870 }
871
b.liu978f5432024-07-01 18:04:18 +0800872 if(gnss_info.gnss_dev_close(gnss_info.fd)) {
873 LOGE("gnss_dev_close() fail.");
874 return GNSS_ERR_UNKNOWN;
875 }
876
b.liu8f231a12024-05-31 17:55:06 +0800877 // Wait for read thread exit.
878 if(gnss_info.exit_fd[1] > 0) {
879 write(gnss_info.exit_fd[1], "exit", 4);
880 }
881
882 gnss_info.state = GNSS_STATE_CLOSING;
yq.wang069ea922024-09-08 19:29:35 -0700883
884#if MBTK_GNSS_TEST_LOG
885 gnss_test_log("wait gnss pthread exit...");
886#endif
b.liu8f231a12024-05-31 17:55:06 +0800887 int ret = pthread_join(gnss_info.read_pid, NULL);
888 if(ret){
889 LOGE("pthrad_join fail(%d)",ret);
b.liudbc3f4b2024-06-25 18:22:24 +0800890 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800891 }
892
893 if(gnss_info.gnss_close(gnss_info.fd)) {
894 LOGE("gnss_close() fail.");
b.liudbc3f4b2024-06-25 18:22:24 +0800895 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800896 }
897
b.liu8f231a12024-05-31 17:55:06 +0800898 if(gnss_ports_close()) {
899 LOGE("gnss_ports_close fail.");
b.liudbc3f4b2024-06-25 18:22:24 +0800900 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800901 }
902
yq.wang069ea922024-09-08 19:29:35 -0700903#if MBTK_GNSS_TEST_LOG
904 gnss_test_log("gnss_ports_close() complete.");
905#endif
b.liud0ba7152024-06-19 14:47:21 +0800906 LOGD("gnss_ports_close() complete.");
907
b.liu8f231a12024-05-31 17:55:06 +0800908 gnss_info.fd = -1;
909 if(gnss_info.exit_fd[0] > 0) {
910 close(gnss_info.exit_fd[0]);
911 gnss_info.exit_fd[0] = -1;
912 }
913 if(gnss_info.exit_fd[1] > 0) {
914 close(gnss_info.exit_fd[1]);
915 gnss_info.exit_fd[1] = -1;
916 }
917 gnss_info.state = GNSS_STATE_CLOSE;
yq.wang069ea922024-09-08 19:29:35 -0700918#if MBTK_GNSS_TEST_LOG
919 gnss_test_log("gnss close success.");
920#endif
b.liu8f231a12024-05-31 17:55:06 +0800921 LOGD("GNSS close success.");
yq.wang069ea922024-09-08 19:29:35 -0700922
b.liudbc3f4b2024-06-25 18:22:24 +0800923 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800924}
925
926int gnss_set(const void* buf, unsigned int buf_len, void *cmd_rsp, int cmd_rsp_len)
927{
b.liud0ba7152024-06-19 14:47:21 +0800928 if(buf && buf_len > 0) {
929 if(cmd_rsp && cmd_rsp_len > 0) {
930 memset(cmd_rsp, 0, cmd_rsp_len);
931 }
b.liu8f231a12024-05-31 17:55:06 +0800932 return gnss_info.gnss_set(gnss_info.fd, buf, cmd_rsp, cmd_rsp_len);
933 } else {
b.liudbc3f4b2024-06-25 18:22:24 +0800934 return GNSS_ERR_UNKNOWN;
935 }
936}
937
938int gnss_dl_fw(const char* fw_name, void *rsp, int rsp_len)
939{
b.liu42f558e2024-07-18 14:06:49 +0800940 // Only 8122 support download fw.
b.liudbc3f4b2024-06-25 18:22:24 +0800941 if(gnss_info.gnss_id != GNSS_TYPE_8122) {
942 return GNSS_ERR_UNSUPPORT;
943 }
944
945 if(rsp && rsp_len > 0) {
946 memset(rsp, 0, rsp_len);
947 }
948
949 if(gnss_info.gnss_fw_dl) {
950 if(GNSS_ERR_OK != gnss_deinit()) {
951 LOGE("Close gnss fail.");
952 return GNSS_ERR_UNKNOWN;
953 } else {
954 LOGD("Start gnss fw dl.");
955 return gnss_info.gnss_fw_dl(gnss_info.fd, fw_name, gnss_info.dev_name);
956 }
957 } else {
b.liu42f558e2024-07-18 14:06:49 +0800958 return GNSS_ERR_UNSUPPORT;
b.liu8f231a12024-05-31 17:55:06 +0800959 }
960}
961
yq.wang99db6f52024-09-12 01:58:48 -0700962int gnss_agnss_get_eph(const void* param)
963{
964 if(gnss_info.gnss_agnss_get_eph)
965 {
966 return gnss_info.gnss_agnss_get_eph(param);
967 }
968 else
969 {
970 return GNSS_ERR_UNSUPPORT;
971 }
972
973 return GNSS_ERR_OK;
974}
975
976int gnss_agnss_inject(void)
977{
978 if(gnss_info.state != GNSS_STATE_READY)
979 {
980 LOGE("gnss not ready");
981 return GNSS_ERR_UNKNOWN;
982 }
983
984 if(gnss_info.gnss_agnss_inject)
985 {
986 return gnss_info.gnss_agnss_inject(gnss_info.fd);
987 }
988 else
989 {
990 return GNSS_ERR_UNSUPPORT;
991 }
992
993 return GNSS_ERR_OK;
994}
995
b.liue77ac3a2024-07-17 17:36:57 +0800996int gnss_ind_set(int fd, int ind_type)
997{
998 int index = 0;
999 if(ind_type) { // Add IND flag.
1000 while(index < GNSS_CLI_IND_MAX) {
1001 if(ind_info[index].cli_fd == fd)
1002 break;
1003 index++;
1004 }
1005
1006 if(index == GNSS_CLI_IND_MAX) { // Add flag
1007 index = 0;
1008 while(index < GNSS_CLI_IND_MAX) {
1009 if(ind_info[index].cli_fd <= 0)
1010 break;
1011 index++;
1012 }
yq.wang1ddd1fd2024-07-25 23:00:14 -07001013 if(index == GNSS_CLI_IND_MAX)
1014 {
1015 LOGE("ind flag is full.");
1016 return GNSS_ERR_CLI_FULL;
1017 }
b.liue77ac3a2024-07-17 17:36:57 +08001018 ind_info[index].cli_fd = fd;
1019 ind_info[index].ind_flag = (uint32)ind_type;
b.liubcf86c92024-08-19 19:48:28 +08001020 } else { // Change flag
b.liue77ac3a2024-07-17 17:36:57 +08001021 ind_info[index].cli_fd = fd;
1022 ind_info[index].ind_flag = (uint32)ind_type;
1023 }
1024 } else { // Clear IND flag.
1025 while(index < GNSS_CLI_IND_MAX) {
1026 if(ind_info[index].cli_fd == fd)
1027 break;
1028 index++;
1029 }
1030
1031 if(index == GNSS_CLI_IND_MAX) {
1032 return GNSS_ERR_ARG;
b.liubcf86c92024-08-19 19:48:28 +08001033 }
b.liue77ac3a2024-07-17 17:36:57 +08001034 ind_info[index].cli_fd = 0;
1035 ind_info[index].ind_flag = 0;
1036 }
1037
1038 return GNSS_ERR_OK;
1039}
1040
b.liu8f231a12024-05-31 17:55:06 +08001041static void sig_process(int sig)
1042{
1043 LOGI("I got signal %d\n", sig);
1044 if(gnss_deinit()) {
1045 LOGE("gnss_deinit() fail, no exist...");
1046 return;
1047 }
1048
1049 switch(sig)
1050 {
1051 case SIGINT: // Ctrl + C
1052 {
1053 LOGI("Exit by SIGINT.\n");
1054 exit(0);
1055 }
luojiand7960682024-08-02 16:50:01 +08001056 case SIGQUIT: // Ctrl + \ (\C0\E0\CB\C6 SIGINT \A3\AC\B5\ABҪ\B2\FA\C9\FAcore\CEļ\FE)
b.liu8f231a12024-05-31 17:55:06 +08001057 {
1058 LOGI("Exit by SIGQUIT.\n");
1059 exit(0);
1060 }
luojiand7960682024-08-02 16:50:01 +08001061 case SIGTERM:// Ĭ\C8\CFkill (ͬ SIGKILL \A3\AC\B5\AB SIGKILL \B2\BB\BFɲ\B6\BB\F1)
b.liu8f231a12024-05-31 17:55:06 +08001062 {
1063 LOGI("Exit by SIGTERM.\n");
1064 exit(0);
1065 }
luojiand7960682024-08-02 16:50:01 +08001066 case SIGTSTP:// Ctrl + Z (ͬ SIGSTOP \A3\AC\B5\AB SIGSTOP \B2\BB\BFɲ\B6\BB\F1)
b.liu8f231a12024-05-31 17:55:06 +08001067 {
1068 LOGI("Exit by SIGTSTP.\n");
1069 exit(0);
1070 }
luojiand7960682024-08-02 16:50:01 +08001071 case SIGSEGV: // \C8\E7\BF\D5Ö¸\D5\EB
b.liu8f231a12024-05-31 17:55:06 +08001072 {
1073 LOGI("Exit by SIGSEGV.\n");
1074 exit(0);
1075 }
1076 default:
1077 {
1078 LOGI("Unknown sig:%d\n",sig);
1079 break;
1080 }
1081 }
1082}
1083
1084
b.liuced8dd02024-06-28 13:28:29 +08001085// mbtk_gnssd 6228 /dev/ttyS2 baud 0/1 <port_type>
b.liu8f231a12024-05-31 17:55:06 +08001086int main(int argc, char *argv[])
1087{
1088 mbtk_log_init("radio", GNSS_TAG);
1089
b.liubcf86c92024-08-19 19:48:28 +08001090 MBTK_SOURCE_INFO_PRINT("mbtk_gnssd");
1091
b.liubb590492024-06-13 16:42:08 +08001092#ifdef MBTK_DUMP_SUPPORT
1093 mbtk_debug_open(NULL, TRUE);
1094#endif
1095
b.liu8f231a12024-05-31 17:55:06 +08001096 signal(SIGINT, sig_process);
1097 signal(SIGQUIT, sig_process);
1098 signal(SIGTERM, sig_process);
1099
1100 if(arg_check(argc, argv)) {
1101 return -1;
1102 }
1103
1104#ifdef GNSS_DEBUG
1105 char buff[10];
1106 memset(buff, 0, 10);
1107 property_get(MBTK_PROP_GNSS_LOG, buff, "");
1108 if(strlen(buff) > 0 && atoi(buff) > 0) {
1109 nmea_log_enable = TRUE;
1110 }
1111#endif
1112
1113 memset(&gnss_info, 0, sizeof(gnss_info_t));
1114 memcpy(gnss_info.dev_name, argv[2], strlen(argv[2]));
1115 gnss_info.state = GNSS_STATE_CLOSE;
1116 if(!strcmp(argv[1], GNSS_ID_6228)) {
1117 gnss_info.gnss_id = GNSS_TYPE_6228;
1118 gnss_info.auto_open = (bool)atoi(argv[3]);
1119 gnss_info.auto_dl_fw = TRUE;
b.liu99c645d2024-06-20 10:52:15 +08001120 gnss_info.dl_befor_open = FALSE;
b.liu8f231a12024-05-31 17:55:06 +08001121 gnss_info.gnss_dev_open = gnss_6228_dev_open;
1122 gnss_info.gnss_dev_close = gnss_6228_dev_close;
1123 gnss_info.gnss_open = gnss_6228_open;
1124 gnss_info.gnss_close = gnss_6228_close;
1125 gnss_info.gnss_fw_dl = gnss_6228_fw_dl;
1126 gnss_info.gnss_dl_read_cb = gnss_6228_dl_read_cb;
1127 gnss_info.gnss_set = gnss_6228_set;
1128 gnss_info.gnss_set_cb = gnss_6228_set_cb;
yq.wang99db6f52024-09-12 01:58:48 -07001129 gnss_info.gnss_agnss_get_eph = NULL;
1130 gnss_info.gnss_agnss_inject = NULL;
b.liuf9fbfa12024-06-14 15:53:59 +08001131 } else if(!strcmp(argv[1], GNSS_ID_8122)) {
1132 gnss_info.gnss_id = GNSS_TYPE_8122;
1133 gnss_info.auto_open = (bool)atoi(argv[3]);
1134 gnss_info.auto_dl_fw = FALSE;
b.liu99c645d2024-06-20 10:52:15 +08001135 gnss_info.dl_befor_open = FALSE;
b.liuf9fbfa12024-06-14 15:53:59 +08001136 gnss_info.gnss_dev_open = gnss_8122_dev_open;
1137 gnss_info.gnss_dev_close = gnss_8122_dev_close;
1138 gnss_info.gnss_open = gnss_8122_open;
1139 gnss_info.gnss_close = gnss_8122_close;
1140 gnss_info.gnss_fw_dl = gnss_8122_fw_dl;
1141 gnss_info.gnss_dl_read_cb = NULL;
1142 gnss_info.gnss_set = gnss_8122_set;
b.liu5f950c52024-06-15 20:13:12 +08001143 gnss_info.gnss_set_cb = gnss_8122_set_cb;
yq.wang99db6f52024-09-12 01:58:48 -07001144 gnss_info.gnss_agnss_get_eph = gnss_8122_agnss_get_eph;
1145 gnss_info.gnss_agnss_inject = gnss_8122_agnss_inject;
b.liu99c645d2024-06-20 10:52:15 +08001146 } else if(!strcmp(argv[1], GNSS_ID_5311)) {
1147 gnss_info.gnss_id = GNSS_TYPE_5311;
1148 gnss_info.auto_open = (bool)atoi(argv[3]);
1149 gnss_info.auto_dl_fw = TRUE;
1150 gnss_info.dl_befor_open = TRUE;
1151 gnss_info.gnss_dev_open = gnss_5311_dev_open;
1152 gnss_info.gnss_dev_close = gnss_5311_dev_close;
1153 gnss_info.gnss_open = gnss_5311_open;
1154 gnss_info.gnss_close = gnss_5311_close;
1155 gnss_info.gnss_fw_dl = gnss_5311_fw_dl;
1156 gnss_info.gnss_dl_read_cb = NULL;
1157 gnss_info.gnss_set = gnss_5311_set;
1158 gnss_info.gnss_set_cb = gnss_5311_set_cb;
yq.wang99db6f52024-09-12 01:58:48 -07001159 gnss_info.gnss_agnss_get_eph = NULL;
1160 gnss_info.gnss_agnss_inject = NULL;
b.liu42f558e2024-07-18 14:06:49 +08001161 } else if(!strcmp(argv[1], GNSS_ID_N50DB)) {
1162 gnss_info.gnss_id = GNSS_TYPE_N50DB;
1163 gnss_info.auto_open = (bool)atoi(argv[3]);
1164 gnss_info.auto_dl_fw = FALSE;
1165 gnss_info.dl_befor_open = FALSE;
1166 gnss_info.gnss_dev_open = gnss_n50db_dev_open;
1167 gnss_info.gnss_dev_close = gnss_n50db_dev_close;
1168 gnss_info.gnss_open = gnss_n50db_open;
1169 gnss_info.gnss_close = gnss_n50db_close;
1170 gnss_info.gnss_fw_dl = gnss_n50db_fw_dl;
1171 gnss_info.gnss_dl_read_cb = NULL;
1172 gnss_info.gnss_set = gnss_n50db_set;
1173 gnss_info.gnss_set_cb = gnss_n50db_set_cb;
yq.wang99db6f52024-09-12 01:58:48 -07001174 gnss_info.gnss_agnss_get_eph = NULL;
1175 gnss_info.gnss_agnss_inject = NULL;
b.liu8f231a12024-05-31 17:55:06 +08001176 } else {
1177 LOGE("No support : %s", argv[1]);
1178 return -1;
1179 }
1180
1181 LOGD("GNSS : %s, Device: %s", argv[1], gnss_info.dev_name);
1182 // Auto open gnss.
1183 if(gnss_info.auto_open) {
b.liuced8dd02024-06-28 13:28:29 +08001184 int init_mode = atoi(argv[3]);
1185 if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) != init_mode) {
1186 init_mode = 0;
b.liu8f231a12024-05-31 17:55:06 +08001187 }
b.liuced8dd02024-06-28 13:28:29 +08001188 if(gnss_init((uint32)init_mode)) {
1189 LOGE("gnss_init() fail.");
1190 // return -1;
1191 }
1192 } else {
1193 gnss_info.print_port = 0;
b.liu8f231a12024-05-31 17:55:06 +08001194 }
1195
b.liua1b69262024-09-04 16:32:59 +08001196#if MBTK_GNSS_PTY_AUTO_INIT
1197 if(gnss_pty_open(&gnss_pty_master_fd, &gnss_pty_slave_fd, GNSS_PORT_PTY)) {
1198 LOGE("gnss_pty_open() fail.");
1199 return -1;
1200 }
1201#endif
1202
yq.wang069ea922024-09-08 19:29:35 -07001203#if MBTK_GNSS_TEST_LOG
1204 gnss_test_log("gnss init success.");
1205#endif
b.liu8f231a12024-05-31 17:55:06 +08001206 // Init ubus and waitting IPC commands.
b.liud0ba7152024-06-19 14:47:21 +08001207#ifdef MBTK_GNSS_UBUS_ENABLE
b.liu8f231a12024-05-31 17:55:06 +08001208 if(gnss_ubus_init()) {
1209 LOGD("main() run...");
1210 uloop_run();
1211 } else {
1212 LOGE("gnss_ubus_init() fail.");
1213 }
b.liu5f950c52024-06-15 20:13:12 +08001214#else
1215 if(!gnss_ipc_service_start()) {
1216 LOGD("main() run...");
1217 while(1) {
1218 sleep(24 * 60 * 60);
1219 }
1220 } else {
1221 LOGE("gnss_ipc_service_start() fail.");
1222 }
1223#endif
b.liu8f231a12024-05-31 17:55:06 +08001224
1225 LOGD("main() exit.");
1226 return 0;
1227}