blob: 4d3a8dcf16016714b2607c7778f09ce3f247ff41 [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"
b.liu8f231a12024-05-31 17:55:06 +080022
23#define GNSS_DEBUG 1
b.liud0ba7152024-06-19 14:47:21 +080024#define GNSS_UBUS_ENABLE 1
b.liu8f231a12024-05-31 17:55:06 +080025
26#define GNSS_TAG "MBTK_GNSS"
27#define GNSS_BUFF_SIZE 2048
28#define MBTK_PROP_GNSS_LOG "persist.mbtk.gnss_log_enable"
29#define GNSS_PORT_PTY "/dev/tty_gnss_nmea"
30#define GNSS_PORT_USB_AT "/dev/ttyGS0"
31#define GNSS_PORT_USB_NMEA "/dev/ttymodem0"
32#define GNSS_PORT_UART_AT "/dev/ttyS1"
b.liue77ac3a2024-07-17 17:36:57 +080033#define GNSS_CLI_IND_MAX 10
b.liu8f231a12024-05-31 17:55:06 +080034
35#ifdef GNSS_DEBUG
36#define GNSS_NMEA_FILE_LOG "/tmp/mbtk_gnss_nmea.log"
b.liu8f231a12024-05-31 17:55:06 +080037#define GNSS_FILE_LOG "/tmp/mbtk_gnss.log"
b.liu99c645d2024-06-20 10:52:15 +080038#define GNSS_FILE_LOG_MAX 104857600 // 100MB
b.liu8f231a12024-05-31 17:55:06 +080039#endif
40
yq.wang1ddd1fd2024-07-25 23:00:14 -070041gnss_info_t gnss_info;
b.liu8f231a12024-05-31 17:55:06 +080042
b.liud0ba7152024-06-19 14:47:21 +080043#ifdef MBTK_GNSS_UBUS_ENABLE
b.liu8f231a12024-05-31 17:55:06 +080044struct ubus_context *gnss_ubus_init(void);
b.liu5f950c52024-06-15 20:13:12 +080045#else
46int gnss_ipc_service_start();
47#endif
48
b.liu8f231a12024-05-31 17:55:06 +080049int gnss_init_config(int fd);
50
wangyouqiangfa897f82024-06-27 09:44:55 +080051static char nmea_buff[GNSS_BUFF_SIZE*4] = {0};
52static char data_buff[GNSS_BUFF_SIZE*4] = {0};
b.liud0ba7152024-06-19 14:47:21 +080053static uint32 nmea_buff_len = 0;
54static uint32 data_buff_len = 0;
b.liue77ac3a2024-07-17 17:36:57 +080055static gnss_ind_info_t ind_info[GNSS_CLI_IND_MAX];
yq.wang1ddd1fd2024-07-25 23:00:14 -070056static gnss_ind_type_num_t ind_num;
b.liud0ba7152024-06-19 14:47:21 +080057
b.liu8f231a12024-05-31 17:55:06 +080058static bool nmea_found = FALSE;
59#ifdef GNSS_DEBUG
60static bool nmea_log_enable = FALSE;
61static int nmea_log_fd = -1;
b.liu99c645d2024-06-20 10:52:15 +080062static int debug_fd = -1;
63static int debug_fd_len = 0;
b.liu8f231a12024-05-31 17:55:06 +080064#endif
65static int gnss_pty_master_fd = -1;
66static int gnss_pty_slave_fd = -1;
67static int gnss_usb_at_port_fd = -1;
68static int gnss_usb_nmea_port_fd = -1;
69static int gnss_uart_at_port_fd = -1;
70static char *gnss_filter_info[] = {"RMC", "VTG", "GGA", "GSA", "GSV", "GLL", "ZDA", "GST", "TXT", "DHV", "DTM", NULL};
71
b.liue77ac3a2024-07-17 17:36:57 +080072int gnss_write(int fd, const void* buf, int buf_len);
73
b.liu8f231a12024-05-31 17:55:06 +080074static void help()
75{
b.liu42f558e2024-07-18 14:06:49 +080076 LOGD("mbtk_gnssd <6228/8122/5311/N50DB> <gnss_dev> <0/1>");
b.liu8f231a12024-05-31 17:55:06 +080077}
78
79static int arg_check(int argc, char *argv[])
80{
81 if(argc != 4) {
b.liu4ae41182024-06-28 16:30:15 +080082 LOGE("argc = %d", argc);
b.liu8f231a12024-05-31 17:55:06 +080083 goto check_fail;
84 }
85
b.liu99c645d2024-06-20 10:52:15 +080086 // Only support 6228/8122/5311.
b.liu42f558e2024-07-18 14:06:49 +080087 if(strcmp(argv[1], GNSS_ID_6228) && strcmp(argv[1], GNSS_ID_8122) && strcmp(argv[1], GNSS_ID_5311)
88 && strcmp(argv[1], GNSS_ID_N50DB)) {
b.liu4ae41182024-06-28 16:30:15 +080089 LOGE("argv[1] = %s", argv[1]);
b.liu8f231a12024-05-31 17:55:06 +080090 goto check_fail;
91 }
92
93 if(access(argv[2], R_OK | W_OK)) {
b.liu4ae41182024-06-28 16:30:15 +080094 LOGE("access(%s) rw fail. ", argv[2]);
b.liu8f231a12024-05-31 17:55:06 +080095 goto check_fail;
96 }
97
b.liuced8dd02024-06-28 13:28:29 +080098#if 0
99 int init_mode = atoi(argv[3]);
100 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 +0800101 goto check_fail;
102 }
b.liuced8dd02024-06-28 13:28:29 +0800103#endif
b.liu8f231a12024-05-31 17:55:06 +0800104
105 return 0;
106check_fail:
107 help();
108 return -1;
109}
110
111static int gnss_ports_open(uint32 print_port)
112{
b.liuced8dd02024-06-28 13:28:29 +0800113 // TTY AT change.
114 if((gnss_info.print_port & GNSS_PRINT_PORT_TTY_AT) != (print_port & GNSS_PRINT_PORT_TTY_AT)) {
115 if(print_port & GNSS_PRINT_PORT_TTY_AT) { // Open
116 if(gnss_pty_open(&gnss_pty_master_fd, &gnss_pty_slave_fd, GNSS_PORT_PTY)) {
117 return GNSS_ERR_OPEN_DEV;
118 }
119 LOGD("Open PTY port success.");
120 } else { // Close
121 if(gnss_pty_slave_fd > 0) {
122 close(gnss_pty_slave_fd);
123 gnss_pty_slave_fd = -1;
124 unlink(GNSS_PORT_PTY);
125 }
126 LOGD("Close PTY port success.");
b.liu8f231a12024-05-31 17:55:06 +0800127 }
b.liu8f231a12024-05-31 17:55:06 +0800128 }
129
b.liuced8dd02024-06-28 13:28:29 +0800130 // USB AT change.
131 if((gnss_info.print_port & GNSS_PRINT_PORT_USB_AT) != (print_port & GNSS_PRINT_PORT_USB_AT)) {
132 if(print_port & GNSS_PRINT_PORT_USB_AT) { // Open
b.liu978f5432024-07-01 18:04:18 +0800133 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 +0800134 return GNSS_ERR_OPEN_DEV;
135 }
136 LOGD("Open USB AT port success.");
137 } else { // Close
138 if(gnss_usb_at_port_fd > 0) {
139 close(gnss_usb_at_port_fd);
140 gnss_usb_at_port_fd = -1;
141 }
142 LOGD("Close USB AT port success.");
b.liu8f231a12024-05-31 17:55:06 +0800143 }
b.liu8f231a12024-05-31 17:55:06 +0800144 }
145
b.liuced8dd02024-06-28 13:28:29 +0800146 // USB NMEA change.
147 if((gnss_info.print_port & GNSS_PRINT_PORT_USB_NMEA) != (print_port & GNSS_PRINT_PORT_USB_NMEA)) {
148 if(print_port & GNSS_PRINT_PORT_USB_NMEA) { // Open
b.liu978f5432024-07-01 18:04:18 +0800149 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 +0800150 return GNSS_ERR_OPEN_DEV;
151 }
152 LOGD("Open USB NMEA port success.");
153 } else { // Close
154 if(gnss_usb_nmea_port_fd > 0) {
155 close(gnss_usb_nmea_port_fd);
156 gnss_usb_nmea_port_fd = -1;
157 }
158 LOGD("Close USB NMEA port success.");
b.liu8f231a12024-05-31 17:55:06 +0800159 }
b.liu8f231a12024-05-31 17:55:06 +0800160 }
161
b.liuced8dd02024-06-28 13:28:29 +0800162 // Uart AT change.
163 if((gnss_info.print_port & GNSS_PRINT_PORT_UART1) != (print_port & GNSS_PRINT_PORT_UART1)) {
164 if(print_port & GNSS_PRINT_PORT_UART1) { // Open
165 if((gnss_uart_at_port_fd = gnss_port_open(GNSS_PORT_UART_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) {
166 return GNSS_ERR_OPEN_DEV;
167 }
168 LOGD("Open UART AT port success.");
169 } else { // Close
170 if(gnss_uart_at_port_fd > 0) {
171 close(gnss_uart_at_port_fd);
172 gnss_uart_at_port_fd = -1;
173 }
174 LOGD("Close UART AT port success.");
b.liu8f231a12024-05-31 17:55:06 +0800175 }
b.liu8f231a12024-05-31 17:55:06 +0800176 }
177
b.liuced8dd02024-06-28 13:28:29 +0800178 gnss_info.print_port = print_port;
179
b.liudbc3f4b2024-06-25 18:22:24 +0800180 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800181}
182
183static int gnss_ports_close()
184{
185 if(gnss_usb_at_port_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800186 tcflush(gnss_usb_at_port_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800187 close(gnss_usb_at_port_fd);
188 gnss_usb_at_port_fd = -1;
189 }
190
191 if(gnss_usb_nmea_port_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800192 tcflush(gnss_usb_nmea_port_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800193 close(gnss_usb_nmea_port_fd);
194 gnss_usb_nmea_port_fd = -1;
195 }
196
197 if(gnss_uart_at_port_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800198 tcflush(gnss_uart_at_port_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800199 close(gnss_uart_at_port_fd);
200 gnss_uart_at_port_fd = -1;
201 }
202
203 if(gnss_pty_master_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800204 tcflush(gnss_pty_master_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800205 close(gnss_pty_master_fd);
206 gnss_pty_master_fd = -1;
207 }
208
209 if(gnss_pty_slave_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800210 tcflush(gnss_pty_slave_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800211 close(gnss_pty_slave_fd);
212 gnss_pty_slave_fd = -1;
213 unlink(GNSS_PORT_PTY);
214 }
215
b.liuced8dd02024-06-28 13:28:29 +0800216 gnss_info.print_port = 0;
217
b.liu8f231a12024-05-31 17:55:06 +0800218 return 0;
219}
220
b.liu8f231a12024-05-31 17:55:06 +0800221#ifdef GNSS_DEBUG
b.liu99c645d2024-06-20 10:52:15 +0800222static void log_save(int fd, const char *data, int data_len)
223{
b.liu8f231a12024-05-31 17:55:06 +0800224 if(nmea_log_enable){
b.liu99c645d2024-06-20 10:52:15 +0800225 if(0 /* debug_fd_len > GNSS_FILE_LOG_MAX */) {
226 LOGD("Reopen file:%s(len = %d)", GNSS_FILE_LOG, debug_fd_len);
227 close(debug_fd);
228 debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
229 if(debug_fd < 0) {
230 LOGE("Open debug fd fail.");
231 }
232 debug_fd_len = 0;
233
234 LOGD("Reopen file:%s", GNSS_NMEA_FILE_LOG);
b.liu8f231a12024-05-31 17:55:06 +0800235 close(nmea_log_fd);
236 nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
237 if(nmea_log_fd < 0) {
238 LOGE("Open debug fd fail.");
239 }
b.liu8f231a12024-05-31 17:55:06 +0800240 }
241
b.liu99c645d2024-06-20 10:52:15 +0800242 if(fd == nmea_log_fd) {
243 if(nmea_log_fd > 0) {
244 write(nmea_log_fd, data, data_len);
245 debug_fd_len += data_len;
246 }
247 } else if(fd == debug_fd) {
248 if(debug_fd > 0) {
249 write(debug_fd, data, data_len);
250 debug_fd_len += data_len;
251 }
b.liu8f231a12024-05-31 17:55:06 +0800252 }
253 }
b.liu99c645d2024-06-20 10:52:15 +0800254}
b.liu8f231a12024-05-31 17:55:06 +0800255#endif
256
b.liue77ac3a2024-07-17 17:36:57 +0800257static void ind_location_print(const char *data)
258{
259 int index = 0;
260 int buff_len = 0;
261 char buff[GNSS_BUFF_SIZE] = {0};
262 buff[0] = MBTK_IND_START_FLAG;
263 buff_len++;
264 memcpy(buff + 1, MBTK_IND_LOCATION_TAG, strlen(MBTK_IND_LOCATION_TAG));
265 buff_len += strlen(MBTK_IND_LOCATION_TAG);
266 memcpy(buff + strlen(buff), data, strlen(data));
267 buff_len += strlen(data);
268 buff[strlen(buff)] = MBTK_IND_END_FLAG;
269 buff_len++;
270
271 while(index < GNSS_CLI_IND_MAX) {
272 if(ind_info[index].cli_fd > 0 && (ind_info[index].ind_flag & MBTK_GNSS_IND_LOCATION)) {
273 gnss_write(ind_info[index].cli_fd, buff, buff_len);
274 }
275 index++;
276 }
277}
278
279static void ind_nmea_print(const char *data)
280{
281 int index = 0;
282 int buff_len = 0;
283 char buff[GNSS_BUFF_SIZE] = {0};
284 buff[0] = MBTK_IND_START_FLAG;
285 buff_len++;
286 memcpy(buff + 1, MBTK_IND_NMEA_TAG, strlen(MBTK_IND_NMEA_TAG));
287 buff_len += strlen(MBTK_IND_NMEA_TAG);
288 memcpy(buff + strlen(buff), data, strlen(data));
289 buff_len += strlen(data);
290 buff[strlen(buff)] = MBTK_IND_END_FLAG;
291 buff_len++;
292 while(index < GNSS_CLI_IND_MAX) {
293 if(ind_info[index].cli_fd > 0 && (ind_info[index].ind_flag & MBTK_GNSS_IND_NMEA)) {
294 gnss_write(ind_info[index].cli_fd, buff, buff_len);
295 }
296 index++;
297 }
298}
299
b.liu99c645d2024-06-20 10:52:15 +0800300static void nmea_print(const char *nmea, int nmea_len)
301{
yq.wang1ddd1fd2024-07-25 23:00:14 -0700302 int ret = -1;
b.liu8f231a12024-05-31 17:55:06 +0800303 if(gnss_usb_at_port_fd > 0) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700304 ret = write(gnss_usb_at_port_fd, nmea, nmea_len);
b.liu8f231a12024-05-31 17:55:06 +0800305 }
306
307 if(gnss_usb_nmea_port_fd > 0) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700308 ret = write(gnss_usb_nmea_port_fd, nmea, nmea_len);
b.liu8f231a12024-05-31 17:55:06 +0800309 }
310
311 if(gnss_uart_at_port_fd > 0) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700312 ret = write(gnss_uart_at_port_fd, nmea, nmea_len);
b.liu8f231a12024-05-31 17:55:06 +0800313 }
314
315 if(gnss_pty_master_fd > 0) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700316 ret = write(gnss_pty_master_fd, nmea, nmea_len);
b.liu8f231a12024-05-31 17:55:06 +0800317 }
b.liue77ac3a2024-07-17 17:36:57 +0800318
yq.wang1ddd1fd2024-07-25 23:00:14 -0700319 if(ind_num.nmea_cb_num > 0)
320 {
321 ind_nmea_print(nmea);
322 }
323
324 if(ind_num.locl_cb_num > 0)
325 {
326 ind_location_print(nmea);
327 }
b.liu8f231a12024-05-31 17:55:06 +0800328}
329
330static unsigned char nmea_checksum(const char *nmea)
331{
332 const char *p = nmea;
333 unsigned char chs = 0;
334
335 while (*p == '$') // skip '$'
336 p++;
337 while (*p != '*' && *p != 0)
338 chs ^= *p++;
339
340 return chs;
341}
342
343static bool nmea_check(const char *nmea, int nmea_len)
344{
345 char **ptr = gnss_filter_info;
346 while(*ptr) {
347 if(strstr(nmea, *ptr)) {
348 break;
349 }
350 ptr++;
351 }
352
353 if(*ptr == NULL) {
354 LOGD("Unknown NMEA[%d]:%s", nmea_len, nmea);
355 return FALSE;
356 }
357
358 char *checksum_str = strstr(nmea, "*");
359 checksum_str++; // Jump '*'
360 char checksum_buf[3] = {0};
361 snprintf(checksum_buf, 3, "%02x", nmea_checksum(nmea));
362 if(strncasecmp(checksum_buf, checksum_str, 2)) {
363 LOGD("Checksum error[%d](checksum - %s):%s", nmea_len, checksum_buf, nmea);
364 return FALSE;
365 }
366
367 return TRUE;
368}
369
370static void gnss_nmea_process(const char *data, int data_len)
371{
b.liu99c645d2024-06-20 10:52:15 +0800372 // LOGD("gnss_nmea_process() : data_len - %d", data_len);
373#if 0
b.liu8f231a12024-05-31 17:55:06 +0800374 char nmea[GNSS_BUFF_SIZE] = {0};
375 memcpy(nmea, data, data_len);
b.liu99c645d2024-06-20 10:52:15 +0800376#else
377 const char *nmea = data;
378#endif
b.liu8f231a12024-05-31 17:55:06 +0800379
380 if(!nmea_check(nmea, data_len)) {
b.liu42f558e2024-07-18 14:06:49 +0800381 // No print "$HOSTSLEEP".
382 if(memcmp(nmea, "$HOSTSLEEP", strlen("$HOSTSLEEP"))) {
383 LOGD("NO-NMEA:%s", nmea);
384 }
b.liu99c645d2024-06-20 10:52:15 +0800385#if GNSS_DEBUG
386 log_save(nmea_log_fd, "/**/", 4);
387 log_save(nmea_log_fd, nmea, data_len);
388#endif
389 if(gnss_info.gnss_set_cb)
390 gnss_info.gnss_set_cb(nmea, data_len);
b.liu8f231a12024-05-31 17:55:06 +0800391 return;
392 }
393
394#ifdef GNSS_DEBUG
395 if(nmea_log_enable) {
396 LOGD("NMEA[%d]:%s", data_len, nmea);
397 }
b.liu99c645d2024-06-20 10:52:15 +0800398
399 log_save(nmea_log_fd, nmea, data_len);
b.liu8f231a12024-05-31 17:55:06 +0800400#endif
401
402 nmea_print(nmea, data_len);
403}
404
405#if 0
406static void gnss_cmd_rsp_process(const char *data, int data_len)
407{
408 char rsp[GNSS_BUFF_SIZE] = {0};
409 memcpy(rsp, data, data_len);
410 LOGD("RSP[%d]:%s", data_len, rsp);
411}
412#endif
413
414static bool nmea_char_check(char ch)
415{
416 if(isalnum(ch) || ch == '$' || ch == '\r' || ch == '\n' || ch == '.'
yq.wang1ddd1fd2024-07-25 23:00:14 -0700417 || ch == ',' || ch == '*' || ch == '\0' || ch == '/' || ch == '_' || ch == '=' || ch == '-')
b.liu8f231a12024-05-31 17:55:06 +0800418 return TRUE;
419
420 return FALSE;
421}
422
423static void gnss_data_process(const char *data, int data_len)
424{
425 if(gnss_info.state == GNSS_STATE_OPEN) {
426 LOGD("GNSS_OPEN[%d]:%s", data_len, data);
427 } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) {
428 // LOGD("GNSS_DL[%d]:%s", data_len, data);
b.liu99c645d2024-06-20 10:52:15 +0800429 if(gnss_info.gnss_dl_read_cb) {
430 gnss_info.gnss_dl_read_cb(data, data_len);
431 }
b.liu8f231a12024-05-31 17:55:06 +0800432 } else if(gnss_info.state == GNSS_STATE_READY) {
433 int index = 0;
434 while(index < data_len) {
435 if(nmea_found) {
436 if(!nmea_char_check(data[index])) {
b.liud0ba7152024-06-19 14:47:21 +0800437 // Copy nmea_buff to data_buff
438 // Start with '$', but not nmea data, so copy to data_buff.
439 memcpy(data_buff + data_buff_len, nmea_buff, nmea_buff_len);
440 data_buff_len += nmea_buff_len;
441 data_buff[data_buff_len++] = data[index];
442
443 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800444 nmea_found = FALSE;
b.liu8f231a12024-05-31 17:55:06 +0800445 continue;
446 }
447
448 if(data[index] != '\0') {
b.liud0ba7152024-06-19 14:47:21 +0800449 nmea_buff[nmea_buff_len++] = data[index];
450 if(nmea_buff[nmea_buff_len - 1] == '\n') {
451 if(data_buff_len > 0) {
b.liu99c645d2024-06-20 10:52:15 +0800452#if GNSS_DEBUG
453 log_save(nmea_log_fd, "/**/", 4);
454 log_save(nmea_log_fd, data_buff, data_buff_len);
455#endif
456 if(gnss_info.gnss_set_cb) {
457 gnss_info.gnss_set_cb(data_buff, data_buff_len);
458 }
b.liud0ba7152024-06-19 14:47:21 +0800459 data_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800460 }
461
b.liud0ba7152024-06-19 14:47:21 +0800462 if(nmea_buff_len > 6 && nmea_buff[nmea_buff_len - 5] == '*') { // $XXX*YY\r\n
b.liu99c645d2024-06-20 10:52:15 +0800463 nmea_buff[nmea_buff_len] = '\0';
b.liud0ba7152024-06-19 14:47:21 +0800464 gnss_nmea_process(nmea_buff, nmea_buff_len);
b.liu99c645d2024-06-20 10:52:15 +0800465 } else if(nmea_buff_len > 0) {
466 nmea_buff[nmea_buff_len] = '\0';
b.liu42f558e2024-07-18 14:06:49 +0800467 if(memcmp(nmea_buff, "$HOSTSLEEP", strlen("$HOSTSLEEP"))) {
468 LOGD("NO-NMEA:%s", nmea_buff);
469 }
b.liu99c645d2024-06-20 10:52:15 +0800470#if GNSS_DEBUG
471 log_save(nmea_log_fd, "/**/", 4);
472 log_save(nmea_log_fd, nmea_buff, nmea_buff_len);
473#endif
b.liud0ba7152024-06-19 14:47:21 +0800474 }
475
476 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800477 nmea_found = FALSE;
478 }
479 }
480 } else {
481 if(data[index] == '$') {
b.liud0ba7152024-06-19 14:47:21 +0800482 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800483 nmea_found = TRUE;
b.liud0ba7152024-06-19 14:47:21 +0800484 nmea_buff[nmea_buff_len++] = data[index];
485 } else {
486 data_buff[data_buff_len++] = data[index];
b.liu8f231a12024-05-31 17:55:06 +0800487 }
488 }
489 index++;
490 }
491 } else {
492 LOGW("Unknown state : %d", gnss_info.state);
493 }
494}
495
496void* gnss_read_pthread(void* arg)
497{
498 LOGD("gnss_read_pthread enter.");
499 char buffer[GNSS_BUFF_SIZE];
500 int len = 0;
501 int ret = 0;
502 fd_set fdr, fdw;
503 int fd_max = 0;
504
b.liu8f231a12024-05-31 17:55:06 +0800505 fd_max = (gnss_info.fd > fd_max) ? gnss_info.fd : fd_max;
b.liu8f231a12024-05-31 17:55:06 +0800506 fd_max = (gnss_info.exit_fd[0] > fd_max) ? gnss_info.exit_fd[0] : fd_max;
b.liud0ba7152024-06-19 14:47:21 +0800507 memset(nmea_buff, 0, sizeof(nmea_buff));
508 memset(data_buff, 0, sizeof(data_buff));
509 nmea_buff_len = 0;
510 data_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800511#if GNSS_DEBUG
b.liu8f231a12024-05-31 17:55:06 +0800512 if(nmea_log_enable) {
513 debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
514 if(debug_fd < 0) {
515 LOGE("Open debug fd fail.");
516 }
517 nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
518 if(nmea_log_fd < 0) {
519 LOGE("Open nmea fd fail.");
520 }
b.liu99c645d2024-06-20 10:52:15 +0800521 debug_fd_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800522 }
523#endif
524
b.liudbc3f4b2024-06-25 18:22:24 +0800525 LOGD("uart_fd - %d, exit_fd - %d", gnss_info.fd, gnss_info.exit_fd[0]);
526
b.liu8f231a12024-05-31 17:55:06 +0800527 while(gnss_info.state >= GNSS_STATE_OPEN) {
wangyouqiang55d36bf2024-06-27 09:35:52 +0800528 FD_ZERO(&fdw);
529 FD_ZERO(&fdr);
530 FD_SET(gnss_info.fd, &fdr);
531 FD_SET(gnss_info.exit_fd[0], &fdr);
b.liu8f231a12024-05-31 17:55:06 +0800532 ret = select(fd_max + 1, &fdr, &fdw, 0, NULL);
533 //LOGD("select - %d", ret);
b.liuece0db02024-06-25 18:39:09 +0800534 if(gnss_info.state < GNSS_STATE_OPEN) {
535 LOGD("State = %d, ret = %d", gnss_info.state, ret);
536 if(ret > 0) {
537 if (FD_ISSET(gnss_info.fd, &fdr)) {
538 LOGD("gnss_fd can read.");
539 } else if (FD_ISSET(gnss_info.exit_fd[0], &fdr)) {
540 LOGD("exit_fd can read.");
541 } else {
542 LOGW("Unknown select event.");
543 }
544 }
545 break;
546 }
547
b.liu8f231a12024-05-31 17:55:06 +0800548 if (ret < 0)
549 {
550 if (errno == EINTR)
551 {
552 continue;
553 }
554 LOGE("select error, errno = %d (%s)", errno, strerror(errno));
555 break;
556 }
557 else if (ret == 0)
558 {
559 LOGE("select ret == 0");
560 break;
561 }
562
563 if (FD_ISSET(gnss_info.fd, &fdr))
564 {
565 memset(buffer, 0, GNSS_BUFF_SIZE);
b.liu99c645d2024-06-20 10:52:15 +0800566 len = read(gnss_info.fd, buffer, GNSS_BUFF_SIZE - 1);
b.liu8f231a12024-05-31 17:55:06 +0800567 if(len > 0) {
568 //log_hex("READ", buffer, len);
569
570#if GNSS_DEBUG
b.liu99c645d2024-06-20 10:52:15 +0800571 //LOGD("read data_len = %d", len);
572 log_save(debug_fd, buffer, len);
b.liu8f231a12024-05-31 17:55:06 +0800573#endif
574
575 gnss_data_process(buffer, len);
576
577 } else if(len ==0 ){
578 LOGE("Read end : len = 0");
579 break;
580 } else {
581 if(EAGAIN == errno) {
582 usleep(50000);
583 continue;
584 } else {
585 LOGD("Read ret = -1 ,errno = %d", errno);
586 break;
587 }
588 }
589 }
590 else if (FD_ISSET(gnss_info.exit_fd[0], &fdr))
591 {
b.liuece0db02024-06-25 18:39:09 +0800592 LOGD("exit_fd select event.");
b.liu8f231a12024-05-31 17:55:06 +0800593 memset(buffer, 0, GNSS_BUFF_SIZE);
b.liudbc3f4b2024-06-25 18:22:24 +0800594 len = read(gnss_info.exit_fd[0], buffer, GNSS_BUFF_SIZE);
b.liu8f231a12024-05-31 17:55:06 +0800595 if(len > 0) {
596 if(strcmp(buffer, "exit") == 0) {
597 LOGD("Get thread exit message.");
598 break;
599 }
600 }
601 }
602 else
603 {
604 LOGW("Unknown select event.");
605 continue;
606 }
607 }
608
609#if GNSS_DEBUG
610 if(debug_fd > 0) {
611 close(debug_fd);
612 debug_fd = -1;
613 }
614 if(nmea_log_fd > 0) {
615 close(nmea_log_fd);
616 nmea_log_fd = -1;
617 }
618#endif
619
620 gnss_info.state = GNSS_STATE_CLOSE;
621 LOGD("gnss_read_pthread exit.");
622 return NULL;
623}
624
625#if 0
626int gnss_write(int fd, const void *data, int data_len)
627{
628 int count = 0;
629 int len = 0;
630 while(1)
631 {
632 len = write(fd, data + count, data_len - count);
633 if (len > 0)
634 {
635 count += len;
636 }
637 else
638 {
639 LOGE("write() fail,ret = %d,errno = %d", len, errno);
640 break;
641 }
642
643 if (count == data_len)
644 break;
645 }
646
647 return count;
648}
649#else
b.liu778645e2024-06-21 16:47:42 +0800650int gnss_write(int fd, const void* buf, int buf_len)
b.liu8f231a12024-05-31 17:55:06 +0800651{
b.liu778645e2024-06-21 16:47:42 +0800652 ssize_t size;
653 ssize_t size_to_wr;
b.liu8f231a12024-05-31 17:55:06 +0800654 ssize_t size_written;
655 if(GNSS_BUFF_SIZE < buf_len)
656 {
657 return -1;
658 }
659 for(size = 0; size < buf_len;)
660 {
661 size_to_wr = buf_len - size;
662 if( size_to_wr > GNSS_BUFF_SIZE)
663 size_to_wr = GNSS_BUFF_SIZE;
664
b.liu778645e2024-06-21 16:47:42 +0800665 size_written = write(fd, (const uint8*)buf + size, size_to_wr);
b.liu8f231a12024-05-31 17:55:06 +0800666 if (size_written==-1)
667 {
668 return -1;
669 }
670 size += size_written;
671 if(size_written != size_to_wr)
672 {
673 return size;
674 }
675 }
676 // LOGD("SEND %d / %d", size, buf_len);
677 return size;
678}
679#endif
680
681int gnss_init(uint32 print_port)
682{
683 if(gnss_info.state != GNSS_STATE_CLOSE) {
684 LOGW("GNSS not close:%d", gnss_info.state);
b.liuced8dd02024-06-28 13:28:29 +0800685 if(gnss_info.state == GNSS_STATE_READY) {
686 LOGD("Reset print port : %d -> %d", gnss_info.print_port, print_port);
687 if(gnss_info.print_port != print_port) {
688 return gnss_ports_open(print_port);
689 } else {
690 return GNSS_ERR_OK;
691 }
692 } else {
693 return GNSS_ERR_OK;
694 }
b.liu8f231a12024-05-31 17:55:06 +0800695 }
696
697 int ret = 0;
b.liu99c645d2024-06-20 10:52:15 +0800698 if(gnss_info.dl_befor_open) {
699 //if(gnss_info.auto_dl_fw) {
700 gnss_info.state = GNSS_STATE_DOWNLOAD;
b.liudbc3f4b2024-06-25 18:22:24 +0800701 ret = gnss_info.gnss_fw_dl(gnss_info.fd, NULL, gnss_info.dev_name);
b.liu99c645d2024-06-20 10:52:15 +0800702 if(ret) {
703 LOGE("gnss_fw_dl() fail : %d", ret);
704 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800705 return GNSS_ERR_DL_FW;
b.liu99c645d2024-06-20 10:52:15 +0800706 }
b.liu8f231a12024-05-31 17:55:06 +0800707
b.liu99c645d2024-06-20 10:52:15 +0800708 gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name);
709 if(gnss_info.fd <= 0) {
710 LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd);
711 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800712 return GNSS_ERR_OPEN_DEV;
b.liu99c645d2024-06-20 10:52:15 +0800713 }
714 if(pipe(gnss_info.exit_fd)) {
715 LOGE("pipe() fail[%d].", errno);
b.liudbc3f4b2024-06-25 18:22:24 +0800716 return GNSS_ERR_UNKNOWN;
b.liu99c645d2024-06-20 10:52:15 +0800717 }
718 // GNSS is opened.
719 gnss_info.state = GNSS_STATE_OPEN;
b.liu8f231a12024-05-31 17:55:06 +0800720
b.liu99c645d2024-06-20 10:52:15 +0800721#if 0
722 // Start gnss read thread.
723 pthread_attr_t thread_attr;
724 pthread_attr_init(&thread_attr);
725 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
726 {
727 LOGE("pthread_attr_setdetachstate() fail.");
728 goto main_exit;
729 }
b.liu8f231a12024-05-31 17:55:06 +0800730
b.liu99c645d2024-06-20 10:52:15 +0800731 if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL))
b.liu8f231a12024-05-31 17:55:06 +0800732#else
b.liu99c645d2024-06-20 10:52:15 +0800733 if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL))
b.liu8f231a12024-05-31 17:55:06 +0800734#endif
b.liu99c645d2024-06-20 10:52:15 +0800735 {
736 LOGE("pthread_create() fail.");
737 goto exit_with_close;
738 }
b.liu8f231a12024-05-31 17:55:06 +0800739
b.liu99c645d2024-06-20 10:52:15 +0800740 ret = gnss_info.gnss_dev_open();
741 if(ret) {
742 LOGE("gnss_dev_open() fail : %d", ret);
743 goto exit_with_thread_exit;
744 }
745 //}
746 } else {
747 gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name);
748 if(gnss_info.fd <= 0) {
749 LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd);
750 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800751 return GNSS_ERR_OPEN_DEV;
b.liu99c645d2024-06-20 10:52:15 +0800752 }
753 if(pipe(gnss_info.exit_fd)) {
754 LOGE("pipe() fail[%d].", errno);
b.liudbc3f4b2024-06-25 18:22:24 +0800755 return GNSS_ERR_UNKNOWN;
b.liu99c645d2024-06-20 10:52:15 +0800756 }
757 // GNSS is opened.
758 gnss_info.state = GNSS_STATE_OPEN;
b.liu8f231a12024-05-31 17:55:06 +0800759
b.liu99c645d2024-06-20 10:52:15 +0800760#if 0
761 // Start gnss read thread.
762 pthread_attr_t thread_attr;
763 pthread_attr_init(&thread_attr);
764 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
765 {
766 LOGE("pthread_attr_setdetachstate() fail.");
767 goto main_exit;
768 }
769
770 if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL))
771#else
772 if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL))
773#endif
774 {
775 LOGE("pthread_create() fail.");
776 goto exit_with_close;
777 }
778
779 ret = gnss_info.gnss_dev_open();
b.liu8f231a12024-05-31 17:55:06 +0800780 if(ret) {
b.liu99c645d2024-06-20 10:52:15 +0800781 LOGE("gnss_dev_open() fail : %d", ret);
782 goto exit_with_thread_exit;
783 }
b.liu8f231a12024-05-31 17:55:06 +0800784 }
785
786 // GNSS is ready, NMEA can print from uart.
787 gnss_info.state = GNSS_STATE_READY;
b.liu8f231a12024-05-31 17:55:06 +0800788
789 LOGD("GNSS open success.");
790
b.liuced8dd02024-06-28 13:28:29 +0800791 return gnss_ports_open(print_port);
b.liu8f231a12024-05-31 17:55:06 +0800792
b.liu8f231a12024-05-31 17:55:06 +0800793exit_with_thread_exit:
794 gnss_info.state = GNSS_STATE_CLOSING;
795 // Wait for read thread exit.
796 ret = pthread_join(gnss_info.read_pid, NULL);
797 if(ret){
798 LOGE("pthrad_join fail(%d)",ret);
799 }
800exit_with_close:
801 if(gnss_info.gnss_close(gnss_info.fd)) {
802 LOGE("gnss_close() fail.");
803 }
804 if(gnss_info.exit_fd[0] > 0) {
805 close(gnss_info.exit_fd[0]);
806 gnss_info.exit_fd[0] = -1;
807 }
808 if(gnss_info.exit_fd[1] > 0) {
809 close(gnss_info.exit_fd[1]);
810 gnss_info.exit_fd[1] = -1;
811 }
812 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800813 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800814}
815
816int gnss_deinit()
817{
818 if(gnss_info.state == GNSS_STATE_CLOSE) {
819 LOGW("GNSS is closed.");
b.liudbc3f4b2024-06-25 18:22:24 +0800820 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800821 } else if(gnss_info.state == GNSS_STATE_CLOSING) {
822 LOGW("GNSS is closing...");
b.liudbc3f4b2024-06-25 18:22:24 +0800823 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800824 } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) {
825 LOGW("GNSS is downloading...");
b.liudbc3f4b2024-06-25 18:22:24 +0800826 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800827 }
828
b.liu978f5432024-07-01 18:04:18 +0800829 if(gnss_info.gnss_dev_close(gnss_info.fd)) {
830 LOGE("gnss_dev_close() fail.");
831 return GNSS_ERR_UNKNOWN;
832 }
833
b.liu8f231a12024-05-31 17:55:06 +0800834 // Wait for read thread exit.
835 if(gnss_info.exit_fd[1] > 0) {
836 write(gnss_info.exit_fd[1], "exit", 4);
837 }
838
839 gnss_info.state = GNSS_STATE_CLOSING;
840 int ret = pthread_join(gnss_info.read_pid, NULL);
841 if(ret){
842 LOGE("pthrad_join fail(%d)",ret);
b.liudbc3f4b2024-06-25 18:22:24 +0800843 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800844 }
845
846 if(gnss_info.gnss_close(gnss_info.fd)) {
847 LOGE("gnss_close() fail.");
b.liudbc3f4b2024-06-25 18:22:24 +0800848 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800849 }
850
b.liu8f231a12024-05-31 17:55:06 +0800851 if(gnss_ports_close()) {
852 LOGE("gnss_ports_close fail.");
b.liudbc3f4b2024-06-25 18:22:24 +0800853 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800854 }
855
b.liud0ba7152024-06-19 14:47:21 +0800856 LOGD("gnss_ports_close() complete.");
857
b.liu8f231a12024-05-31 17:55:06 +0800858 gnss_info.fd = -1;
859 if(gnss_info.exit_fd[0] > 0) {
860 close(gnss_info.exit_fd[0]);
861 gnss_info.exit_fd[0] = -1;
862 }
863 if(gnss_info.exit_fd[1] > 0) {
864 close(gnss_info.exit_fd[1]);
865 gnss_info.exit_fd[1] = -1;
866 }
867 gnss_info.state = GNSS_STATE_CLOSE;
868 LOGD("GNSS close success.");
b.liudbc3f4b2024-06-25 18:22:24 +0800869 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800870}
871
872int gnss_set(const void* buf, unsigned int buf_len, void *cmd_rsp, int cmd_rsp_len)
873{
b.liud0ba7152024-06-19 14:47:21 +0800874 if(buf && buf_len > 0) {
875 if(cmd_rsp && cmd_rsp_len > 0) {
876 memset(cmd_rsp, 0, cmd_rsp_len);
877 }
b.liu8f231a12024-05-31 17:55:06 +0800878 return gnss_info.gnss_set(gnss_info.fd, buf, cmd_rsp, cmd_rsp_len);
879 } else {
b.liudbc3f4b2024-06-25 18:22:24 +0800880 return GNSS_ERR_UNKNOWN;
881 }
882}
883
884int gnss_dl_fw(const char* fw_name, void *rsp, int rsp_len)
885{
b.liu42f558e2024-07-18 14:06:49 +0800886 // Only 8122 support download fw.
b.liudbc3f4b2024-06-25 18:22:24 +0800887 if(gnss_info.gnss_id != GNSS_TYPE_8122) {
888 return GNSS_ERR_UNSUPPORT;
889 }
890
891 if(rsp && rsp_len > 0) {
892 memset(rsp, 0, rsp_len);
893 }
894
895 if(gnss_info.gnss_fw_dl) {
896 if(GNSS_ERR_OK != gnss_deinit()) {
897 LOGE("Close gnss fail.");
898 return GNSS_ERR_UNKNOWN;
899 } else {
900 LOGD("Start gnss fw dl.");
901 return gnss_info.gnss_fw_dl(gnss_info.fd, fw_name, gnss_info.dev_name);
902 }
903 } else {
b.liu42f558e2024-07-18 14:06:49 +0800904 return GNSS_ERR_UNSUPPORT;
b.liu8f231a12024-05-31 17:55:06 +0800905 }
906}
907
b.liue77ac3a2024-07-17 17:36:57 +0800908int gnss_ind_set(int fd, int ind_type)
909{
910 int index = 0;
911 if(ind_type) { // Add IND flag.
912 while(index < GNSS_CLI_IND_MAX) {
913 if(ind_info[index].cli_fd == fd)
914 break;
915 index++;
916 }
917
918 if(index == GNSS_CLI_IND_MAX) { // Add flag
919 index = 0;
920 while(index < GNSS_CLI_IND_MAX) {
921 if(ind_info[index].cli_fd <= 0)
922 break;
923 index++;
924 }
yq.wang1ddd1fd2024-07-25 23:00:14 -0700925 if(index == GNSS_CLI_IND_MAX)
926 {
927 LOGE("ind flag is full.");
928 return GNSS_ERR_CLI_FULL;
929 }
b.liue77ac3a2024-07-17 17:36:57 +0800930 ind_info[index].cli_fd = fd;
931 ind_info[index].ind_flag = (uint32)ind_type;
932 } else { // Change flag
yq.wang1ddd1fd2024-07-25 23:00:14 -0700933 if(ind_info[index].ind_flag == MBTK_GNSS_IND_LOCATION)
934 {
935 ind_num.locl_cb_num--;
936 }
937 else if(ind_info[index].ind_flag == MBTK_GNSS_IND_NMEA)
938 {
939 ind_num.nmea_cb_num--;
940 }
941
b.liue77ac3a2024-07-17 17:36:57 +0800942 ind_info[index].cli_fd = fd;
943 ind_info[index].ind_flag = (uint32)ind_type;
944 }
yq.wang1ddd1fd2024-07-25 23:00:14 -0700945
946 if(ind_type == MBTK_GNSS_IND_LOCATION)
947 {
948 ind_num.locl_cb_num++;
949 }
950 else if(ind_type == MBTK_GNSS_IND_NMEA)
951 {
952 ind_num.nmea_cb_num++;
953 }
b.liue77ac3a2024-07-17 17:36:57 +0800954 } else { // Clear IND flag.
955 while(index < GNSS_CLI_IND_MAX) {
956 if(ind_info[index].cli_fd == fd)
957 break;
958 index++;
959 }
960
961 if(index == GNSS_CLI_IND_MAX) {
962 return GNSS_ERR_ARG;
963 }
964
yq.wang1ddd1fd2024-07-25 23:00:14 -0700965 if(ind_info[index].ind_flag == MBTK_GNSS_IND_LOCATION)
966 {
967 ind_num.locl_cb_num--;
968 }
969 else if(ind_info[index].ind_flag == MBTK_GNSS_IND_NMEA)
970 {
971 ind_num.nmea_cb_num--;
972 }
973
b.liue77ac3a2024-07-17 17:36:57 +0800974 ind_info[index].cli_fd = 0;
975 ind_info[index].ind_flag = 0;
976 }
977
978 return GNSS_ERR_OK;
979}
980
b.liu8f231a12024-05-31 17:55:06 +0800981static void sig_process(int sig)
982{
983 LOGI("I got signal %d\n", sig);
984 if(gnss_deinit()) {
985 LOGE("gnss_deinit() fail, no exist...");
986 return;
987 }
988
989 switch(sig)
990 {
991 case SIGINT: // Ctrl + C
992 {
993 LOGI("Exit by SIGINT.\n");
994 exit(0);
995 }
996 case SIGQUIT: // Ctrl + \ (ÀàËÆ SIGINT £¬µ«Òª²úÉúcoreÎļþ)
997 {
998 LOGI("Exit by SIGQUIT.\n");
999 exit(0);
1000 }
1001 case SIGTERM:// ĬÈÏkill (ͬ SIGKILL £¬µ« SIGKILL ²»¿É²¶»ñ)
1002 {
1003 LOGI("Exit by SIGTERM.\n");
1004 exit(0);
1005 }
1006 case SIGTSTP:// Ctrl + Z (ͬ SIGSTOP £¬µ« SIGSTOP ²»¿É²¶»ñ)
1007 {
1008 LOGI("Exit by SIGTSTP.\n");
1009 exit(0);
1010 }
1011 case SIGSEGV: // Èç¿ÕÖ¸Õë
1012 {
1013 LOGI("Exit by SIGSEGV.\n");
1014 exit(0);
1015 }
1016 default:
1017 {
1018 LOGI("Unknown sig:%d\n",sig);
1019 break;
1020 }
1021 }
1022}
1023
1024
b.liuced8dd02024-06-28 13:28:29 +08001025// mbtk_gnssd 6228 /dev/ttyS2 baud 0/1 <port_type>
b.liu8f231a12024-05-31 17:55:06 +08001026int main(int argc, char *argv[])
1027{
1028 mbtk_log_init("radio", GNSS_TAG);
1029
b.liubb590492024-06-13 16:42:08 +08001030#ifdef MBTK_DUMP_SUPPORT
1031 mbtk_debug_open(NULL, TRUE);
1032#endif
1033
b.liu8f231a12024-05-31 17:55:06 +08001034 signal(SIGINT, sig_process);
1035 signal(SIGQUIT, sig_process);
1036 signal(SIGTERM, sig_process);
1037
1038 if(arg_check(argc, argv)) {
1039 return -1;
1040 }
1041
1042#ifdef GNSS_DEBUG
1043 char buff[10];
1044 memset(buff, 0, 10);
1045 property_get(MBTK_PROP_GNSS_LOG, buff, "");
1046 if(strlen(buff) > 0 && atoi(buff) > 0) {
1047 nmea_log_enable = TRUE;
1048 }
1049#endif
1050
yq.wang1ddd1fd2024-07-25 23:00:14 -07001051 memset(&ind_num, 0x0, sizeof(gnss_ind_type_num_t));
b.liu8f231a12024-05-31 17:55:06 +08001052 memset(&gnss_info, 0, sizeof(gnss_info_t));
1053 memcpy(gnss_info.dev_name, argv[2], strlen(argv[2]));
1054 gnss_info.state = GNSS_STATE_CLOSE;
1055 if(!strcmp(argv[1], GNSS_ID_6228)) {
1056 gnss_info.gnss_id = GNSS_TYPE_6228;
1057 gnss_info.auto_open = (bool)atoi(argv[3]);
1058 gnss_info.auto_dl_fw = TRUE;
b.liu99c645d2024-06-20 10:52:15 +08001059 gnss_info.dl_befor_open = FALSE;
b.liu8f231a12024-05-31 17:55:06 +08001060 gnss_info.gnss_dev_open = gnss_6228_dev_open;
1061 gnss_info.gnss_dev_close = gnss_6228_dev_close;
1062 gnss_info.gnss_open = gnss_6228_open;
1063 gnss_info.gnss_close = gnss_6228_close;
1064 gnss_info.gnss_fw_dl = gnss_6228_fw_dl;
1065 gnss_info.gnss_dl_read_cb = gnss_6228_dl_read_cb;
1066 gnss_info.gnss_set = gnss_6228_set;
1067 gnss_info.gnss_set_cb = gnss_6228_set_cb;
b.liuf9fbfa12024-06-14 15:53:59 +08001068 } else if(!strcmp(argv[1], GNSS_ID_8122)) {
1069 gnss_info.gnss_id = GNSS_TYPE_8122;
1070 gnss_info.auto_open = (bool)atoi(argv[3]);
1071 gnss_info.auto_dl_fw = FALSE;
b.liu99c645d2024-06-20 10:52:15 +08001072 gnss_info.dl_befor_open = FALSE;
b.liuf9fbfa12024-06-14 15:53:59 +08001073 gnss_info.gnss_dev_open = gnss_8122_dev_open;
1074 gnss_info.gnss_dev_close = gnss_8122_dev_close;
1075 gnss_info.gnss_open = gnss_8122_open;
1076 gnss_info.gnss_close = gnss_8122_close;
1077 gnss_info.gnss_fw_dl = gnss_8122_fw_dl;
1078 gnss_info.gnss_dl_read_cb = NULL;
1079 gnss_info.gnss_set = gnss_8122_set;
b.liu5f950c52024-06-15 20:13:12 +08001080 gnss_info.gnss_set_cb = gnss_8122_set_cb;
b.liu99c645d2024-06-20 10:52:15 +08001081 } else if(!strcmp(argv[1], GNSS_ID_5311)) {
1082 gnss_info.gnss_id = GNSS_TYPE_5311;
1083 gnss_info.auto_open = (bool)atoi(argv[3]);
1084 gnss_info.auto_dl_fw = TRUE;
1085 gnss_info.dl_befor_open = TRUE;
1086 gnss_info.gnss_dev_open = gnss_5311_dev_open;
1087 gnss_info.gnss_dev_close = gnss_5311_dev_close;
1088 gnss_info.gnss_open = gnss_5311_open;
1089 gnss_info.gnss_close = gnss_5311_close;
1090 gnss_info.gnss_fw_dl = gnss_5311_fw_dl;
1091 gnss_info.gnss_dl_read_cb = NULL;
1092 gnss_info.gnss_set = gnss_5311_set;
1093 gnss_info.gnss_set_cb = gnss_5311_set_cb;
b.liu42f558e2024-07-18 14:06:49 +08001094 } else if(!strcmp(argv[1], GNSS_ID_N50DB)) {
1095 gnss_info.gnss_id = GNSS_TYPE_N50DB;
1096 gnss_info.auto_open = (bool)atoi(argv[3]);
1097 gnss_info.auto_dl_fw = FALSE;
1098 gnss_info.dl_befor_open = FALSE;
1099 gnss_info.gnss_dev_open = gnss_n50db_dev_open;
1100 gnss_info.gnss_dev_close = gnss_n50db_dev_close;
1101 gnss_info.gnss_open = gnss_n50db_open;
1102 gnss_info.gnss_close = gnss_n50db_close;
1103 gnss_info.gnss_fw_dl = gnss_n50db_fw_dl;
1104 gnss_info.gnss_dl_read_cb = NULL;
1105 gnss_info.gnss_set = gnss_n50db_set;
1106 gnss_info.gnss_set_cb = gnss_n50db_set_cb;
b.liu8f231a12024-05-31 17:55:06 +08001107 } else {
1108 LOGE("No support : %s", argv[1]);
1109 return -1;
1110 }
1111
1112 LOGD("GNSS : %s, Device: %s", argv[1], gnss_info.dev_name);
1113 // Auto open gnss.
1114 if(gnss_info.auto_open) {
b.liuced8dd02024-06-28 13:28:29 +08001115 int init_mode = atoi(argv[3]);
1116 if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) != init_mode) {
1117 init_mode = 0;
b.liu8f231a12024-05-31 17:55:06 +08001118 }
b.liuced8dd02024-06-28 13:28:29 +08001119 if(gnss_init((uint32)init_mode)) {
1120 LOGE("gnss_init() fail.");
1121 // return -1;
1122 }
1123 } else {
1124 gnss_info.print_port = 0;
b.liu8f231a12024-05-31 17:55:06 +08001125 }
1126
1127 // Init ubus and waitting IPC commands.
b.liud0ba7152024-06-19 14:47:21 +08001128#ifdef MBTK_GNSS_UBUS_ENABLE
b.liu8f231a12024-05-31 17:55:06 +08001129 if(gnss_ubus_init()) {
1130 LOGD("main() run...");
1131 uloop_run();
1132 } else {
1133 LOGE("gnss_ubus_init() fail.");
1134 }
b.liu5f950c52024-06-15 20:13:12 +08001135#else
1136 if(!gnss_ipc_service_start()) {
1137 LOGD("main() run...");
1138 while(1) {
1139 sleep(24 * 60 * 60);
1140 }
1141 } else {
1142 LOGE("gnss_ipc_service_start() fail.");
1143 }
1144#endif
b.liu8f231a12024-05-31 17:55:06 +08001145
1146 LOGD("main() exit.");
1147 return 0;
1148}