blob: 7c6191b81d2f149b5272b2e313eddccb5d6b7bb9 [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
41gnss_info_t gnss_info;
42
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];
b.liud0ba7152024-06-19 14:47:21 +080056
b.liu8f231a12024-05-31 17:55:06 +080057static bool nmea_found = FALSE;
58#ifdef GNSS_DEBUG
59static bool nmea_log_enable = FALSE;
60static int nmea_log_fd = -1;
b.liu99c645d2024-06-20 10:52:15 +080061static int debug_fd = -1;
62static int debug_fd_len = 0;
b.liu8f231a12024-05-31 17:55:06 +080063#endif
64static int gnss_pty_master_fd = -1;
65static int gnss_pty_slave_fd = -1;
66static int gnss_usb_at_port_fd = -1;
67static int gnss_usb_nmea_port_fd = -1;
68static int gnss_uart_at_port_fd = -1;
69static char *gnss_filter_info[] = {"RMC", "VTG", "GGA", "GSA", "GSV", "GLL", "ZDA", "GST", "TXT", "DHV", "DTM", NULL};
70
b.liue77ac3a2024-07-17 17:36:57 +080071int gnss_write(int fd, const void* buf, int buf_len);
72
b.liu8f231a12024-05-31 17:55:06 +080073static void help()
74{
b.liu42f558e2024-07-18 14:06:49 +080075 LOGD("mbtk_gnssd <6228/8122/5311/N50DB> <gnss_dev> <0/1>");
b.liu8f231a12024-05-31 17:55:06 +080076}
77
78static int arg_check(int argc, char *argv[])
79{
80 if(argc != 4) {
b.liu4ae41182024-06-28 16:30:15 +080081 LOGE("argc = %d", argc);
b.liu8f231a12024-05-31 17:55:06 +080082 goto check_fail;
83 }
84
b.liu99c645d2024-06-20 10:52:15 +080085 // Only support 6228/8122/5311.
b.liu42f558e2024-07-18 14:06:49 +080086 if(strcmp(argv[1], GNSS_ID_6228) && strcmp(argv[1], GNSS_ID_8122) && strcmp(argv[1], GNSS_ID_5311)
87 && strcmp(argv[1], GNSS_ID_N50DB)) {
b.liu4ae41182024-06-28 16:30:15 +080088 LOGE("argv[1] = %s", argv[1]);
b.liu8f231a12024-05-31 17:55:06 +080089 goto check_fail;
90 }
91
92 if(access(argv[2], R_OK | W_OK)) {
b.liu4ae41182024-06-28 16:30:15 +080093 LOGE("access(%s) rw fail. ", argv[2]);
b.liu8f231a12024-05-31 17:55:06 +080094 goto check_fail;
95 }
96
b.liuced8dd02024-06-28 13:28:29 +080097#if 0
98 int init_mode = atoi(argv[3]);
99 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 +0800100 goto check_fail;
101 }
b.liuced8dd02024-06-28 13:28:29 +0800102#endif
b.liu8f231a12024-05-31 17:55:06 +0800103
104 return 0;
105check_fail:
106 help();
107 return -1;
108}
109
110static int gnss_ports_open(uint32 print_port)
111{
b.liuced8dd02024-06-28 13:28:29 +0800112 // TTY AT change.
113 if((gnss_info.print_port & GNSS_PRINT_PORT_TTY_AT) != (print_port & GNSS_PRINT_PORT_TTY_AT)) {
114 if(print_port & GNSS_PRINT_PORT_TTY_AT) { // Open
115 if(gnss_pty_open(&gnss_pty_master_fd, &gnss_pty_slave_fd, GNSS_PORT_PTY)) {
116 return GNSS_ERR_OPEN_DEV;
117 }
118 LOGD("Open PTY port success.");
119 } else { // Close
120 if(gnss_pty_slave_fd > 0) {
121 close(gnss_pty_slave_fd);
122 gnss_pty_slave_fd = -1;
123 unlink(GNSS_PORT_PTY);
124 }
125 LOGD("Close PTY port success.");
b.liu8f231a12024-05-31 17:55:06 +0800126 }
b.liu8f231a12024-05-31 17:55:06 +0800127 }
128
b.liuced8dd02024-06-28 13:28:29 +0800129 // USB AT change.
130 if((gnss_info.print_port & GNSS_PRINT_PORT_USB_AT) != (print_port & GNSS_PRINT_PORT_USB_AT)) {
131 if(print_port & GNSS_PRINT_PORT_USB_AT) { // Open
b.liu978f5432024-07-01 18:04:18 +0800132 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 +0800133 return GNSS_ERR_OPEN_DEV;
134 }
135 LOGD("Open USB AT port success.");
136 } else { // Close
137 if(gnss_usb_at_port_fd > 0) {
138 close(gnss_usb_at_port_fd);
139 gnss_usb_at_port_fd = -1;
140 }
141 LOGD("Close USB AT port success.");
b.liu8f231a12024-05-31 17:55:06 +0800142 }
b.liu8f231a12024-05-31 17:55:06 +0800143 }
144
b.liuced8dd02024-06-28 13:28:29 +0800145 // USB NMEA change.
146 if((gnss_info.print_port & GNSS_PRINT_PORT_USB_NMEA) != (print_port & GNSS_PRINT_PORT_USB_NMEA)) {
147 if(print_port & GNSS_PRINT_PORT_USB_NMEA) { // Open
b.liu978f5432024-07-01 18:04:18 +0800148 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 +0800149 return GNSS_ERR_OPEN_DEV;
150 }
151 LOGD("Open USB NMEA port success.");
152 } else { // Close
153 if(gnss_usb_nmea_port_fd > 0) {
154 close(gnss_usb_nmea_port_fd);
155 gnss_usb_nmea_port_fd = -1;
156 }
157 LOGD("Close USB NMEA port success.");
b.liu8f231a12024-05-31 17:55:06 +0800158 }
b.liu8f231a12024-05-31 17:55:06 +0800159 }
160
b.liuced8dd02024-06-28 13:28:29 +0800161 // Uart AT change.
162 if((gnss_info.print_port & GNSS_PRINT_PORT_UART1) != (print_port & GNSS_PRINT_PORT_UART1)) {
163 if(print_port & GNSS_PRINT_PORT_UART1) { // Open
164 if((gnss_uart_at_port_fd = gnss_port_open(GNSS_PORT_UART_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) {
165 return GNSS_ERR_OPEN_DEV;
166 }
167 LOGD("Open UART AT port success.");
168 } else { // Close
169 if(gnss_uart_at_port_fd > 0) {
170 close(gnss_uart_at_port_fd);
171 gnss_uart_at_port_fd = -1;
172 }
173 LOGD("Close UART AT port success.");
b.liu8f231a12024-05-31 17:55:06 +0800174 }
b.liu8f231a12024-05-31 17:55:06 +0800175 }
176
b.liuced8dd02024-06-28 13:28:29 +0800177 gnss_info.print_port = print_port;
178
b.liudbc3f4b2024-06-25 18:22:24 +0800179 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800180}
181
182static int gnss_ports_close()
183{
184 if(gnss_usb_at_port_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800185 tcflush(gnss_usb_at_port_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800186 close(gnss_usb_at_port_fd);
187 gnss_usb_at_port_fd = -1;
188 }
189
190 if(gnss_usb_nmea_port_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800191 tcflush(gnss_usb_nmea_port_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800192 close(gnss_usb_nmea_port_fd);
193 gnss_usb_nmea_port_fd = -1;
194 }
195
196 if(gnss_uart_at_port_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800197 tcflush(gnss_uart_at_port_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800198 close(gnss_uart_at_port_fd);
199 gnss_uart_at_port_fd = -1;
200 }
201
202 if(gnss_pty_master_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800203 tcflush(gnss_pty_master_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800204 close(gnss_pty_master_fd);
205 gnss_pty_master_fd = -1;
206 }
207
208 if(gnss_pty_slave_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800209 tcflush(gnss_pty_slave_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800210 close(gnss_pty_slave_fd);
211 gnss_pty_slave_fd = -1;
212 unlink(GNSS_PORT_PTY);
213 }
214
b.liuced8dd02024-06-28 13:28:29 +0800215 gnss_info.print_port = 0;
216
b.liu8f231a12024-05-31 17:55:06 +0800217 return 0;
218}
219
b.liu8f231a12024-05-31 17:55:06 +0800220#ifdef GNSS_DEBUG
b.liu99c645d2024-06-20 10:52:15 +0800221static void log_save(int fd, const char *data, int data_len)
222{
b.liu8f231a12024-05-31 17:55:06 +0800223 if(nmea_log_enable){
b.liu99c645d2024-06-20 10:52:15 +0800224 if(0 /* debug_fd_len > GNSS_FILE_LOG_MAX */) {
225 LOGD("Reopen file:%s(len = %d)", GNSS_FILE_LOG, debug_fd_len);
226 close(debug_fd);
227 debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
228 if(debug_fd < 0) {
229 LOGE("Open debug fd fail.");
230 }
231 debug_fd_len = 0;
232
233 LOGD("Reopen file:%s", GNSS_NMEA_FILE_LOG);
b.liu8f231a12024-05-31 17:55:06 +0800234 close(nmea_log_fd);
235 nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
236 if(nmea_log_fd < 0) {
237 LOGE("Open debug fd fail.");
238 }
b.liu8f231a12024-05-31 17:55:06 +0800239 }
240
b.liu99c645d2024-06-20 10:52:15 +0800241 if(fd == nmea_log_fd) {
242 if(nmea_log_fd > 0) {
243 write(nmea_log_fd, data, data_len);
244 debug_fd_len += data_len;
245 }
246 } else if(fd == debug_fd) {
247 if(debug_fd > 0) {
248 write(debug_fd, data, data_len);
249 debug_fd_len += data_len;
250 }
b.liu8f231a12024-05-31 17:55:06 +0800251 }
252 }
b.liu99c645d2024-06-20 10:52:15 +0800253}
b.liu8f231a12024-05-31 17:55:06 +0800254#endif
255
b.liue77ac3a2024-07-17 17:36:57 +0800256static void ind_location_print(const char *data)
257{
258 int index = 0;
259 int buff_len = 0;
260 char buff[GNSS_BUFF_SIZE] = {0};
261 buff[0] = MBTK_IND_START_FLAG;
262 buff_len++;
263 memcpy(buff + 1, MBTK_IND_LOCATION_TAG, strlen(MBTK_IND_LOCATION_TAG));
264 buff_len += strlen(MBTK_IND_LOCATION_TAG);
265 memcpy(buff + strlen(buff), data, strlen(data));
266 buff_len += strlen(data);
267 buff[strlen(buff)] = MBTK_IND_END_FLAG;
268 buff_len++;
269
270 while(index < GNSS_CLI_IND_MAX) {
271 if(ind_info[index].cli_fd > 0 && (ind_info[index].ind_flag & MBTK_GNSS_IND_LOCATION)) {
272 gnss_write(ind_info[index].cli_fd, buff, buff_len);
273 }
274 index++;
275 }
276}
277
278static void ind_nmea_print(const char *data)
279{
280 int index = 0;
281 int buff_len = 0;
282 char buff[GNSS_BUFF_SIZE] = {0};
283 buff[0] = MBTK_IND_START_FLAG;
284 buff_len++;
285 memcpy(buff + 1, MBTK_IND_NMEA_TAG, strlen(MBTK_IND_NMEA_TAG));
286 buff_len += strlen(MBTK_IND_NMEA_TAG);
287 memcpy(buff + strlen(buff), data, strlen(data));
288 buff_len += strlen(data);
289 buff[strlen(buff)] = MBTK_IND_END_FLAG;
290 buff_len++;
291 while(index < GNSS_CLI_IND_MAX) {
292 if(ind_info[index].cli_fd > 0 && (ind_info[index].ind_flag & MBTK_GNSS_IND_NMEA)) {
293 gnss_write(ind_info[index].cli_fd, buff, buff_len);
294 }
295 index++;
296 }
297}
298
b.liu99c645d2024-06-20 10:52:15 +0800299static void nmea_print(const char *nmea, int nmea_len)
300{
b.liu8f231a12024-05-31 17:55:06 +0800301 if(gnss_usb_at_port_fd > 0) {
302 write(gnss_usb_at_port_fd, nmea, nmea_len);
303 }
304
305 if(gnss_usb_nmea_port_fd > 0) {
306 write(gnss_usb_nmea_port_fd, nmea, nmea_len);
307 }
308
309 if(gnss_uart_at_port_fd > 0) {
310 write(gnss_uart_at_port_fd, nmea, nmea_len);
311 }
312
313 if(gnss_pty_master_fd > 0) {
314 write(gnss_pty_master_fd, nmea, nmea_len);
315 }
b.liue77ac3a2024-07-17 17:36:57 +0800316
317 ind_nmea_print(nmea);
b.liu8f231a12024-05-31 17:55:06 +0800318}
319
320static unsigned char nmea_checksum(const char *nmea)
321{
322 const char *p = nmea;
323 unsigned char chs = 0;
324
325 while (*p == '$') // skip '$'
326 p++;
327 while (*p != '*' && *p != 0)
328 chs ^= *p++;
329
330 return chs;
331}
332
333static bool nmea_check(const char *nmea, int nmea_len)
334{
335 char **ptr = gnss_filter_info;
336 while(*ptr) {
337 if(strstr(nmea, *ptr)) {
338 break;
339 }
340 ptr++;
341 }
342
343 if(*ptr == NULL) {
344 LOGD("Unknown NMEA[%d]:%s", nmea_len, nmea);
345 return FALSE;
346 }
347
348 char *checksum_str = strstr(nmea, "*");
349 checksum_str++; // Jump '*'
350 char checksum_buf[3] = {0};
351 snprintf(checksum_buf, 3, "%02x", nmea_checksum(nmea));
352 if(strncasecmp(checksum_buf, checksum_str, 2)) {
353 LOGD("Checksum error[%d](checksum - %s):%s", nmea_len, checksum_buf, nmea);
354 return FALSE;
355 }
356
357 return TRUE;
358}
359
360static void gnss_nmea_process(const char *data, int data_len)
361{
b.liu99c645d2024-06-20 10:52:15 +0800362 // LOGD("gnss_nmea_process() : data_len - %d", data_len);
363#if 0
b.liu8f231a12024-05-31 17:55:06 +0800364 char nmea[GNSS_BUFF_SIZE] = {0};
365 memcpy(nmea, data, data_len);
b.liu99c645d2024-06-20 10:52:15 +0800366#else
367 const char *nmea = data;
368#endif
b.liu8f231a12024-05-31 17:55:06 +0800369
370 if(!nmea_check(nmea, data_len)) {
b.liu42f558e2024-07-18 14:06:49 +0800371 // No print "$HOSTSLEEP".
372 if(memcmp(nmea, "$HOSTSLEEP", strlen("$HOSTSLEEP"))) {
373 LOGD("NO-NMEA:%s", nmea);
374 }
b.liu99c645d2024-06-20 10:52:15 +0800375#if GNSS_DEBUG
376 log_save(nmea_log_fd, "/**/", 4);
377 log_save(nmea_log_fd, nmea, data_len);
378#endif
379 if(gnss_info.gnss_set_cb)
380 gnss_info.gnss_set_cb(nmea, data_len);
b.liu8f231a12024-05-31 17:55:06 +0800381 return;
382 }
383
384#ifdef GNSS_DEBUG
385 if(nmea_log_enable) {
386 LOGD("NMEA[%d]:%s", data_len, nmea);
387 }
b.liu99c645d2024-06-20 10:52:15 +0800388
389 log_save(nmea_log_fd, nmea, data_len);
b.liu8f231a12024-05-31 17:55:06 +0800390#endif
391
392 nmea_print(nmea, data_len);
393}
394
395#if 0
396static void gnss_cmd_rsp_process(const char *data, int data_len)
397{
398 char rsp[GNSS_BUFF_SIZE] = {0};
399 memcpy(rsp, data, data_len);
400 LOGD("RSP[%d]:%s", data_len, rsp);
401}
402#endif
403
404static bool nmea_char_check(char ch)
405{
406 if(isalnum(ch) || ch == '$' || ch == '\r' || ch == '\n' || ch == '.'
b.liu99c645d2024-06-20 10:52:15 +0800407 || ch == ',' || ch == '*' || ch == '\0' || ch == '/' || ch == '_' || ch == '=')
b.liu8f231a12024-05-31 17:55:06 +0800408 return TRUE;
409
410 return FALSE;
411}
412
413static void gnss_data_process(const char *data, int data_len)
414{
415 if(gnss_info.state == GNSS_STATE_OPEN) {
416 LOGD("GNSS_OPEN[%d]:%s", data_len, data);
417 } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) {
418 // LOGD("GNSS_DL[%d]:%s", data_len, data);
b.liu99c645d2024-06-20 10:52:15 +0800419 if(gnss_info.gnss_dl_read_cb) {
420 gnss_info.gnss_dl_read_cb(data, data_len);
421 }
b.liu8f231a12024-05-31 17:55:06 +0800422 } else if(gnss_info.state == GNSS_STATE_READY) {
423 int index = 0;
424 while(index < data_len) {
425 if(nmea_found) {
426 if(!nmea_char_check(data[index])) {
b.liud0ba7152024-06-19 14:47:21 +0800427 // Copy nmea_buff to data_buff
428 // Start with '$', but not nmea data, so copy to data_buff.
429 memcpy(data_buff + data_buff_len, nmea_buff, nmea_buff_len);
430 data_buff_len += nmea_buff_len;
431 data_buff[data_buff_len++] = data[index];
432
433 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800434 nmea_found = FALSE;
b.liu8f231a12024-05-31 17:55:06 +0800435 continue;
436 }
437
438 if(data[index] != '\0') {
b.liud0ba7152024-06-19 14:47:21 +0800439 nmea_buff[nmea_buff_len++] = data[index];
440 if(nmea_buff[nmea_buff_len - 1] == '\n') {
441 if(data_buff_len > 0) {
b.liu99c645d2024-06-20 10:52:15 +0800442#if GNSS_DEBUG
443 log_save(nmea_log_fd, "/**/", 4);
444 log_save(nmea_log_fd, data_buff, data_buff_len);
445#endif
446 if(gnss_info.gnss_set_cb) {
447 gnss_info.gnss_set_cb(data_buff, data_buff_len);
448 }
b.liud0ba7152024-06-19 14:47:21 +0800449 data_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800450 }
451
b.liud0ba7152024-06-19 14:47:21 +0800452 if(nmea_buff_len > 6 && nmea_buff[nmea_buff_len - 5] == '*') { // $XXX*YY\r\n
b.liu99c645d2024-06-20 10:52:15 +0800453 nmea_buff[nmea_buff_len] = '\0';
b.liud0ba7152024-06-19 14:47:21 +0800454 gnss_nmea_process(nmea_buff, nmea_buff_len);
b.liu99c645d2024-06-20 10:52:15 +0800455 } else if(nmea_buff_len > 0) {
456 nmea_buff[nmea_buff_len] = '\0';
b.liu42f558e2024-07-18 14:06:49 +0800457 if(memcmp(nmea_buff, "$HOSTSLEEP", strlen("$HOSTSLEEP"))) {
458 LOGD("NO-NMEA:%s", nmea_buff);
459 }
b.liu99c645d2024-06-20 10:52:15 +0800460#if GNSS_DEBUG
461 log_save(nmea_log_fd, "/**/", 4);
462 log_save(nmea_log_fd, nmea_buff, nmea_buff_len);
463#endif
b.liud0ba7152024-06-19 14:47:21 +0800464 }
465
466 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800467 nmea_found = FALSE;
468 }
469 }
470 } else {
471 if(data[index] == '$') {
b.liud0ba7152024-06-19 14:47:21 +0800472 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800473 nmea_found = TRUE;
b.liud0ba7152024-06-19 14:47:21 +0800474 nmea_buff[nmea_buff_len++] = data[index];
475 } else {
476 data_buff[data_buff_len++] = data[index];
b.liu8f231a12024-05-31 17:55:06 +0800477 }
478 }
479 index++;
480 }
481 } else {
482 LOGW("Unknown state : %d", gnss_info.state);
483 }
484}
485
486void* gnss_read_pthread(void* arg)
487{
488 LOGD("gnss_read_pthread enter.");
489 char buffer[GNSS_BUFF_SIZE];
490 int len = 0;
491 int ret = 0;
492 fd_set fdr, fdw;
493 int fd_max = 0;
494
b.liu8f231a12024-05-31 17:55:06 +0800495 fd_max = (gnss_info.fd > fd_max) ? gnss_info.fd : fd_max;
b.liu8f231a12024-05-31 17:55:06 +0800496 fd_max = (gnss_info.exit_fd[0] > fd_max) ? gnss_info.exit_fd[0] : fd_max;
b.liud0ba7152024-06-19 14:47:21 +0800497 memset(nmea_buff, 0, sizeof(nmea_buff));
498 memset(data_buff, 0, sizeof(data_buff));
499 nmea_buff_len = 0;
500 data_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800501#if GNSS_DEBUG
b.liu8f231a12024-05-31 17:55:06 +0800502 if(nmea_log_enable) {
503 debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
504 if(debug_fd < 0) {
505 LOGE("Open debug fd fail.");
506 }
507 nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
508 if(nmea_log_fd < 0) {
509 LOGE("Open nmea fd fail.");
510 }
b.liu99c645d2024-06-20 10:52:15 +0800511 debug_fd_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800512 }
513#endif
514
b.liudbc3f4b2024-06-25 18:22:24 +0800515 LOGD("uart_fd - %d, exit_fd - %d", gnss_info.fd, gnss_info.exit_fd[0]);
516
b.liu8f231a12024-05-31 17:55:06 +0800517 while(gnss_info.state >= GNSS_STATE_OPEN) {
wangyouqiang55d36bf2024-06-27 09:35:52 +0800518 FD_ZERO(&fdw);
519 FD_ZERO(&fdr);
520 FD_SET(gnss_info.fd, &fdr);
521 FD_SET(gnss_info.exit_fd[0], &fdr);
b.liu8f231a12024-05-31 17:55:06 +0800522 ret = select(fd_max + 1, &fdr, &fdw, 0, NULL);
523 //LOGD("select - %d", ret);
b.liuece0db02024-06-25 18:39:09 +0800524 if(gnss_info.state < GNSS_STATE_OPEN) {
525 LOGD("State = %d, ret = %d", gnss_info.state, ret);
526 if(ret > 0) {
527 if (FD_ISSET(gnss_info.fd, &fdr)) {
528 LOGD("gnss_fd can read.");
529 } else if (FD_ISSET(gnss_info.exit_fd[0], &fdr)) {
530 LOGD("exit_fd can read.");
531 } else {
532 LOGW("Unknown select event.");
533 }
534 }
535 break;
536 }
537
b.liu8f231a12024-05-31 17:55:06 +0800538 if (ret < 0)
539 {
540 if (errno == EINTR)
541 {
542 continue;
543 }
544 LOGE("select error, errno = %d (%s)", errno, strerror(errno));
545 break;
546 }
547 else if (ret == 0)
548 {
549 LOGE("select ret == 0");
550 break;
551 }
552
553 if (FD_ISSET(gnss_info.fd, &fdr))
554 {
555 memset(buffer, 0, GNSS_BUFF_SIZE);
b.liu99c645d2024-06-20 10:52:15 +0800556 len = read(gnss_info.fd, buffer, GNSS_BUFF_SIZE - 1);
b.liu8f231a12024-05-31 17:55:06 +0800557 if(len > 0) {
558 //log_hex("READ", buffer, len);
559
560#if GNSS_DEBUG
b.liu99c645d2024-06-20 10:52:15 +0800561 //LOGD("read data_len = %d", len);
562 log_save(debug_fd, buffer, len);
b.liu8f231a12024-05-31 17:55:06 +0800563#endif
564
565 gnss_data_process(buffer, len);
566
567 } else if(len ==0 ){
568 LOGE("Read end : len = 0");
569 break;
570 } else {
571 if(EAGAIN == errno) {
572 usleep(50000);
573 continue;
574 } else {
575 LOGD("Read ret = -1 ,errno = %d", errno);
576 break;
577 }
578 }
579 }
580 else if (FD_ISSET(gnss_info.exit_fd[0], &fdr))
581 {
b.liuece0db02024-06-25 18:39:09 +0800582 LOGD("exit_fd select event.");
b.liu8f231a12024-05-31 17:55:06 +0800583 memset(buffer, 0, GNSS_BUFF_SIZE);
b.liudbc3f4b2024-06-25 18:22:24 +0800584 len = read(gnss_info.exit_fd[0], buffer, GNSS_BUFF_SIZE);
b.liu8f231a12024-05-31 17:55:06 +0800585 if(len > 0) {
586 if(strcmp(buffer, "exit") == 0) {
587 LOGD("Get thread exit message.");
588 break;
589 }
590 }
591 }
592 else
593 {
594 LOGW("Unknown select event.");
595 continue;
596 }
597 }
598
599#if GNSS_DEBUG
600 if(debug_fd > 0) {
601 close(debug_fd);
602 debug_fd = -1;
603 }
604 if(nmea_log_fd > 0) {
605 close(nmea_log_fd);
606 nmea_log_fd = -1;
607 }
608#endif
609
610 gnss_info.state = GNSS_STATE_CLOSE;
611 LOGD("gnss_read_pthread exit.");
612 return NULL;
613}
614
615#if 0
616int gnss_write(int fd, const void *data, int data_len)
617{
618 int count = 0;
619 int len = 0;
620 while(1)
621 {
622 len = write(fd, data + count, data_len - count);
623 if (len > 0)
624 {
625 count += len;
626 }
627 else
628 {
629 LOGE("write() fail,ret = %d,errno = %d", len, errno);
630 break;
631 }
632
633 if (count == data_len)
634 break;
635 }
636
637 return count;
638}
639#else
b.liu778645e2024-06-21 16:47:42 +0800640int gnss_write(int fd, const void* buf, int buf_len)
b.liu8f231a12024-05-31 17:55:06 +0800641{
b.liu778645e2024-06-21 16:47:42 +0800642 ssize_t size;
643 ssize_t size_to_wr;
b.liu8f231a12024-05-31 17:55:06 +0800644 ssize_t size_written;
645 if(GNSS_BUFF_SIZE < buf_len)
646 {
647 return -1;
648 }
649 for(size = 0; size < buf_len;)
650 {
651 size_to_wr = buf_len - size;
652 if( size_to_wr > GNSS_BUFF_SIZE)
653 size_to_wr = GNSS_BUFF_SIZE;
654
b.liu778645e2024-06-21 16:47:42 +0800655 size_written = write(fd, (const uint8*)buf + size, size_to_wr);
b.liu8f231a12024-05-31 17:55:06 +0800656 if (size_written==-1)
657 {
658 return -1;
659 }
660 size += size_written;
661 if(size_written != size_to_wr)
662 {
663 return size;
664 }
665 }
666 // LOGD("SEND %d / %d", size, buf_len);
667 return size;
668}
669#endif
670
671int gnss_init(uint32 print_port)
672{
673 if(gnss_info.state != GNSS_STATE_CLOSE) {
674 LOGW("GNSS not close:%d", gnss_info.state);
b.liuced8dd02024-06-28 13:28:29 +0800675 if(gnss_info.state == GNSS_STATE_READY) {
676 LOGD("Reset print port : %d -> %d", gnss_info.print_port, print_port);
677 if(gnss_info.print_port != print_port) {
678 return gnss_ports_open(print_port);
679 } else {
680 return GNSS_ERR_OK;
681 }
682 } else {
683 return GNSS_ERR_OK;
684 }
b.liu8f231a12024-05-31 17:55:06 +0800685 }
686
687 int ret = 0;
b.liu99c645d2024-06-20 10:52:15 +0800688 if(gnss_info.dl_befor_open) {
689 //if(gnss_info.auto_dl_fw) {
690 gnss_info.state = GNSS_STATE_DOWNLOAD;
b.liudbc3f4b2024-06-25 18:22:24 +0800691 ret = gnss_info.gnss_fw_dl(gnss_info.fd, NULL, gnss_info.dev_name);
b.liu99c645d2024-06-20 10:52:15 +0800692 if(ret) {
693 LOGE("gnss_fw_dl() fail : %d", ret);
694 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800695 return GNSS_ERR_DL_FW;
b.liu99c645d2024-06-20 10:52:15 +0800696 }
b.liu8f231a12024-05-31 17:55:06 +0800697
b.liu99c645d2024-06-20 10:52:15 +0800698 gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name);
699 if(gnss_info.fd <= 0) {
700 LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd);
701 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800702 return GNSS_ERR_OPEN_DEV;
b.liu99c645d2024-06-20 10:52:15 +0800703 }
704 if(pipe(gnss_info.exit_fd)) {
705 LOGE("pipe() fail[%d].", errno);
b.liudbc3f4b2024-06-25 18:22:24 +0800706 return GNSS_ERR_UNKNOWN;
b.liu99c645d2024-06-20 10:52:15 +0800707 }
708 // GNSS is opened.
709 gnss_info.state = GNSS_STATE_OPEN;
b.liu8f231a12024-05-31 17:55:06 +0800710
b.liu99c645d2024-06-20 10:52:15 +0800711#if 0
712 // Start gnss read thread.
713 pthread_attr_t thread_attr;
714 pthread_attr_init(&thread_attr);
715 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
716 {
717 LOGE("pthread_attr_setdetachstate() fail.");
718 goto main_exit;
719 }
b.liu8f231a12024-05-31 17:55:06 +0800720
b.liu99c645d2024-06-20 10:52:15 +0800721 if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL))
b.liu8f231a12024-05-31 17:55:06 +0800722#else
b.liu99c645d2024-06-20 10:52:15 +0800723 if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL))
b.liu8f231a12024-05-31 17:55:06 +0800724#endif
b.liu99c645d2024-06-20 10:52:15 +0800725 {
726 LOGE("pthread_create() fail.");
727 goto exit_with_close;
728 }
b.liu8f231a12024-05-31 17:55:06 +0800729
b.liu99c645d2024-06-20 10:52:15 +0800730 ret = gnss_info.gnss_dev_open();
731 if(ret) {
732 LOGE("gnss_dev_open() fail : %d", ret);
733 goto exit_with_thread_exit;
734 }
735 //}
736 } else {
737 gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name);
738 if(gnss_info.fd <= 0) {
739 LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd);
740 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800741 return GNSS_ERR_OPEN_DEV;
b.liu99c645d2024-06-20 10:52:15 +0800742 }
743 if(pipe(gnss_info.exit_fd)) {
744 LOGE("pipe() fail[%d].", errno);
b.liudbc3f4b2024-06-25 18:22:24 +0800745 return GNSS_ERR_UNKNOWN;
b.liu99c645d2024-06-20 10:52:15 +0800746 }
747 // GNSS is opened.
748 gnss_info.state = GNSS_STATE_OPEN;
b.liu8f231a12024-05-31 17:55:06 +0800749
b.liu99c645d2024-06-20 10:52:15 +0800750#if 0
751 // Start gnss read thread.
752 pthread_attr_t thread_attr;
753 pthread_attr_init(&thread_attr);
754 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
755 {
756 LOGE("pthread_attr_setdetachstate() fail.");
757 goto main_exit;
758 }
759
760 if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL))
761#else
762 if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL))
763#endif
764 {
765 LOGE("pthread_create() fail.");
766 goto exit_with_close;
767 }
768
769 ret = gnss_info.gnss_dev_open();
b.liu8f231a12024-05-31 17:55:06 +0800770 if(ret) {
b.liu99c645d2024-06-20 10:52:15 +0800771 LOGE("gnss_dev_open() fail : %d", ret);
772 goto exit_with_thread_exit;
773 }
b.liu8f231a12024-05-31 17:55:06 +0800774 }
775
776 // GNSS is ready, NMEA can print from uart.
777 gnss_info.state = GNSS_STATE_READY;
b.liu8f231a12024-05-31 17:55:06 +0800778
779 LOGD("GNSS open success.");
780
b.liuced8dd02024-06-28 13:28:29 +0800781 return gnss_ports_open(print_port);
b.liu8f231a12024-05-31 17:55:06 +0800782
b.liu8f231a12024-05-31 17:55:06 +0800783exit_with_thread_exit:
784 gnss_info.state = GNSS_STATE_CLOSING;
785 // Wait for read thread exit.
786 ret = pthread_join(gnss_info.read_pid, NULL);
787 if(ret){
788 LOGE("pthrad_join fail(%d)",ret);
789 }
790exit_with_close:
791 if(gnss_info.gnss_close(gnss_info.fd)) {
792 LOGE("gnss_close() fail.");
793 }
794 if(gnss_info.exit_fd[0] > 0) {
795 close(gnss_info.exit_fd[0]);
796 gnss_info.exit_fd[0] = -1;
797 }
798 if(gnss_info.exit_fd[1] > 0) {
799 close(gnss_info.exit_fd[1]);
800 gnss_info.exit_fd[1] = -1;
801 }
802 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800803 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800804}
805
806int gnss_deinit()
807{
808 if(gnss_info.state == GNSS_STATE_CLOSE) {
809 LOGW("GNSS is closed.");
b.liudbc3f4b2024-06-25 18:22:24 +0800810 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800811 } else if(gnss_info.state == GNSS_STATE_CLOSING) {
812 LOGW("GNSS is closing...");
b.liudbc3f4b2024-06-25 18:22:24 +0800813 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800814 } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) {
815 LOGW("GNSS is downloading...");
b.liudbc3f4b2024-06-25 18:22:24 +0800816 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800817 }
818
b.liu978f5432024-07-01 18:04:18 +0800819 if(gnss_info.gnss_dev_close(gnss_info.fd)) {
820 LOGE("gnss_dev_close() fail.");
821 return GNSS_ERR_UNKNOWN;
822 }
823
b.liu8f231a12024-05-31 17:55:06 +0800824 // Wait for read thread exit.
825 if(gnss_info.exit_fd[1] > 0) {
826 write(gnss_info.exit_fd[1], "exit", 4);
827 }
828
829 gnss_info.state = GNSS_STATE_CLOSING;
830 int ret = pthread_join(gnss_info.read_pid, NULL);
831 if(ret){
832 LOGE("pthrad_join fail(%d)",ret);
b.liudbc3f4b2024-06-25 18:22:24 +0800833 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800834 }
835
836 if(gnss_info.gnss_close(gnss_info.fd)) {
837 LOGE("gnss_close() fail.");
b.liudbc3f4b2024-06-25 18:22:24 +0800838 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800839 }
840
b.liu8f231a12024-05-31 17:55:06 +0800841 if(gnss_ports_close()) {
842 LOGE("gnss_ports_close fail.");
b.liudbc3f4b2024-06-25 18:22:24 +0800843 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800844 }
845
b.liud0ba7152024-06-19 14:47:21 +0800846 LOGD("gnss_ports_close() complete.");
847
b.liu8f231a12024-05-31 17:55:06 +0800848 gnss_info.fd = -1;
849 if(gnss_info.exit_fd[0] > 0) {
850 close(gnss_info.exit_fd[0]);
851 gnss_info.exit_fd[0] = -1;
852 }
853 if(gnss_info.exit_fd[1] > 0) {
854 close(gnss_info.exit_fd[1]);
855 gnss_info.exit_fd[1] = -1;
856 }
857 gnss_info.state = GNSS_STATE_CLOSE;
858 LOGD("GNSS close success.");
b.liudbc3f4b2024-06-25 18:22:24 +0800859 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800860}
861
862int gnss_set(const void* buf, unsigned int buf_len, void *cmd_rsp, int cmd_rsp_len)
863{
b.liud0ba7152024-06-19 14:47:21 +0800864 if(buf && buf_len > 0) {
865 if(cmd_rsp && cmd_rsp_len > 0) {
866 memset(cmd_rsp, 0, cmd_rsp_len);
867 }
b.liu8f231a12024-05-31 17:55:06 +0800868 return gnss_info.gnss_set(gnss_info.fd, buf, cmd_rsp, cmd_rsp_len);
869 } else {
b.liudbc3f4b2024-06-25 18:22:24 +0800870 return GNSS_ERR_UNKNOWN;
871 }
872}
873
874int gnss_dl_fw(const char* fw_name, void *rsp, int rsp_len)
875{
b.liu42f558e2024-07-18 14:06:49 +0800876 // Only 8122 support download fw.
b.liudbc3f4b2024-06-25 18:22:24 +0800877 if(gnss_info.gnss_id != GNSS_TYPE_8122) {
878 return GNSS_ERR_UNSUPPORT;
879 }
880
881 if(rsp && rsp_len > 0) {
882 memset(rsp, 0, rsp_len);
883 }
884
885 if(gnss_info.gnss_fw_dl) {
886 if(GNSS_ERR_OK != gnss_deinit()) {
887 LOGE("Close gnss fail.");
888 return GNSS_ERR_UNKNOWN;
889 } else {
890 LOGD("Start gnss fw dl.");
891 return gnss_info.gnss_fw_dl(gnss_info.fd, fw_name, gnss_info.dev_name);
892 }
893 } else {
b.liu42f558e2024-07-18 14:06:49 +0800894 return GNSS_ERR_UNSUPPORT;
b.liu8f231a12024-05-31 17:55:06 +0800895 }
896}
897
b.liue77ac3a2024-07-17 17:36:57 +0800898int gnss_ind_set(int fd, int ind_type)
899{
900 int index = 0;
901 if(ind_type) { // Add IND flag.
902 while(index < GNSS_CLI_IND_MAX) {
903 if(ind_info[index].cli_fd == fd)
904 break;
905 index++;
906 }
907
908 if(index == GNSS_CLI_IND_MAX) { // Add flag
909 index = 0;
910 while(index < GNSS_CLI_IND_MAX) {
911 if(ind_info[index].cli_fd <= 0)
912 break;
913 index++;
914 }
915 ind_info[index].cli_fd = fd;
916 ind_info[index].ind_flag = (uint32)ind_type;
917 } else { // Change flag
918 ind_info[index].cli_fd = fd;
919 ind_info[index].ind_flag = (uint32)ind_type;
920 }
921 } else { // Clear IND flag.
922 while(index < GNSS_CLI_IND_MAX) {
923 if(ind_info[index].cli_fd == fd)
924 break;
925 index++;
926 }
927
928 if(index == GNSS_CLI_IND_MAX) {
929 return GNSS_ERR_ARG;
930 }
931
932 ind_info[index].cli_fd = 0;
933 ind_info[index].ind_flag = 0;
934 }
935
936 return GNSS_ERR_OK;
937}
938
b.liu8f231a12024-05-31 17:55:06 +0800939static void sig_process(int sig)
940{
941 LOGI("I got signal %d\n", sig);
942 if(gnss_deinit()) {
943 LOGE("gnss_deinit() fail, no exist...");
944 return;
945 }
946
947 switch(sig)
948 {
949 case SIGINT: // Ctrl + C
950 {
951 LOGI("Exit by SIGINT.\n");
952 exit(0);
953 }
954 case SIGQUIT: // Ctrl + \ (ÀàËÆ SIGINT £¬µ«Òª²úÉúcoreÎļþ)
955 {
956 LOGI("Exit by SIGQUIT.\n");
957 exit(0);
958 }
959 case SIGTERM:// ĬÈÏkill (ͬ SIGKILL £¬µ« SIGKILL ²»¿É²¶»ñ)
960 {
961 LOGI("Exit by SIGTERM.\n");
962 exit(0);
963 }
964 case SIGTSTP:// Ctrl + Z (ͬ SIGSTOP £¬µ« SIGSTOP ²»¿É²¶»ñ)
965 {
966 LOGI("Exit by SIGTSTP.\n");
967 exit(0);
968 }
969 case SIGSEGV: // Èç¿ÕÖ¸Õë
970 {
971 LOGI("Exit by SIGSEGV.\n");
972 exit(0);
973 }
974 default:
975 {
976 LOGI("Unknown sig:%d\n",sig);
977 break;
978 }
979 }
980}
981
982
b.liuced8dd02024-06-28 13:28:29 +0800983// mbtk_gnssd 6228 /dev/ttyS2 baud 0/1 <port_type>
b.liu8f231a12024-05-31 17:55:06 +0800984int main(int argc, char *argv[])
985{
986 mbtk_log_init("radio", GNSS_TAG);
987
b.liubb590492024-06-13 16:42:08 +0800988#ifdef MBTK_DUMP_SUPPORT
989 mbtk_debug_open(NULL, TRUE);
990#endif
991
b.liu8f231a12024-05-31 17:55:06 +0800992 signal(SIGINT, sig_process);
993 signal(SIGQUIT, sig_process);
994 signal(SIGTERM, sig_process);
995
996 if(arg_check(argc, argv)) {
997 return -1;
998 }
999
1000#ifdef GNSS_DEBUG
1001 char buff[10];
1002 memset(buff, 0, 10);
1003 property_get(MBTK_PROP_GNSS_LOG, buff, "");
1004 if(strlen(buff) > 0 && atoi(buff) > 0) {
1005 nmea_log_enable = TRUE;
1006 }
1007#endif
1008
1009 memset(&gnss_info, 0, sizeof(gnss_info_t));
1010 memcpy(gnss_info.dev_name, argv[2], strlen(argv[2]));
1011 gnss_info.state = GNSS_STATE_CLOSE;
1012 if(!strcmp(argv[1], GNSS_ID_6228)) {
1013 gnss_info.gnss_id = GNSS_TYPE_6228;
1014 gnss_info.auto_open = (bool)atoi(argv[3]);
1015 gnss_info.auto_dl_fw = TRUE;
b.liu99c645d2024-06-20 10:52:15 +08001016 gnss_info.dl_befor_open = FALSE;
b.liu8f231a12024-05-31 17:55:06 +08001017 gnss_info.gnss_dev_open = gnss_6228_dev_open;
1018 gnss_info.gnss_dev_close = gnss_6228_dev_close;
1019 gnss_info.gnss_open = gnss_6228_open;
1020 gnss_info.gnss_close = gnss_6228_close;
1021 gnss_info.gnss_fw_dl = gnss_6228_fw_dl;
1022 gnss_info.gnss_dl_read_cb = gnss_6228_dl_read_cb;
1023 gnss_info.gnss_set = gnss_6228_set;
1024 gnss_info.gnss_set_cb = gnss_6228_set_cb;
b.liuf9fbfa12024-06-14 15:53:59 +08001025 } else if(!strcmp(argv[1], GNSS_ID_8122)) {
1026 gnss_info.gnss_id = GNSS_TYPE_8122;
1027 gnss_info.auto_open = (bool)atoi(argv[3]);
1028 gnss_info.auto_dl_fw = FALSE;
b.liu99c645d2024-06-20 10:52:15 +08001029 gnss_info.dl_befor_open = FALSE;
b.liuf9fbfa12024-06-14 15:53:59 +08001030 gnss_info.gnss_dev_open = gnss_8122_dev_open;
1031 gnss_info.gnss_dev_close = gnss_8122_dev_close;
1032 gnss_info.gnss_open = gnss_8122_open;
1033 gnss_info.gnss_close = gnss_8122_close;
1034 gnss_info.gnss_fw_dl = gnss_8122_fw_dl;
1035 gnss_info.gnss_dl_read_cb = NULL;
1036 gnss_info.gnss_set = gnss_8122_set;
b.liu5f950c52024-06-15 20:13:12 +08001037 gnss_info.gnss_set_cb = gnss_8122_set_cb;
b.liu99c645d2024-06-20 10:52:15 +08001038 } else if(!strcmp(argv[1], GNSS_ID_5311)) {
1039 gnss_info.gnss_id = GNSS_TYPE_5311;
1040 gnss_info.auto_open = (bool)atoi(argv[3]);
1041 gnss_info.auto_dl_fw = TRUE;
1042 gnss_info.dl_befor_open = TRUE;
1043 gnss_info.gnss_dev_open = gnss_5311_dev_open;
1044 gnss_info.gnss_dev_close = gnss_5311_dev_close;
1045 gnss_info.gnss_open = gnss_5311_open;
1046 gnss_info.gnss_close = gnss_5311_close;
1047 gnss_info.gnss_fw_dl = gnss_5311_fw_dl;
1048 gnss_info.gnss_dl_read_cb = NULL;
1049 gnss_info.gnss_set = gnss_5311_set;
1050 gnss_info.gnss_set_cb = gnss_5311_set_cb;
b.liu42f558e2024-07-18 14:06:49 +08001051 } else if(!strcmp(argv[1], GNSS_ID_N50DB)) {
1052 gnss_info.gnss_id = GNSS_TYPE_N50DB;
1053 gnss_info.auto_open = (bool)atoi(argv[3]);
1054 gnss_info.auto_dl_fw = FALSE;
1055 gnss_info.dl_befor_open = FALSE;
1056 gnss_info.gnss_dev_open = gnss_n50db_dev_open;
1057 gnss_info.gnss_dev_close = gnss_n50db_dev_close;
1058 gnss_info.gnss_open = gnss_n50db_open;
1059 gnss_info.gnss_close = gnss_n50db_close;
1060 gnss_info.gnss_fw_dl = gnss_n50db_fw_dl;
1061 gnss_info.gnss_dl_read_cb = NULL;
1062 gnss_info.gnss_set = gnss_n50db_set;
1063 gnss_info.gnss_set_cb = gnss_n50db_set_cb;
b.liu8f231a12024-05-31 17:55:06 +08001064 } else {
1065 LOGE("No support : %s", argv[1]);
1066 return -1;
1067 }
1068
1069 LOGD("GNSS : %s, Device: %s", argv[1], gnss_info.dev_name);
1070 // Auto open gnss.
1071 if(gnss_info.auto_open) {
b.liuced8dd02024-06-28 13:28:29 +08001072 int init_mode = atoi(argv[3]);
1073 if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) != init_mode) {
1074 init_mode = 0;
b.liu8f231a12024-05-31 17:55:06 +08001075 }
b.liuced8dd02024-06-28 13:28:29 +08001076 if(gnss_init((uint32)init_mode)) {
1077 LOGE("gnss_init() fail.");
1078 // return -1;
1079 }
1080 } else {
1081 gnss_info.print_port = 0;
b.liu8f231a12024-05-31 17:55:06 +08001082 }
1083
1084 // Init ubus and waitting IPC commands.
b.liud0ba7152024-06-19 14:47:21 +08001085#ifdef MBTK_GNSS_UBUS_ENABLE
b.liu8f231a12024-05-31 17:55:06 +08001086 if(gnss_ubus_init()) {
1087 LOGD("main() run...");
1088 uloop_run();
1089 } else {
1090 LOGE("gnss_ubus_init() fail.");
1091 }
b.liu5f950c52024-06-15 20:13:12 +08001092#else
1093 if(!gnss_ipc_service_start()) {
1094 LOGD("main() run...");
1095 while(1) {
1096 sleep(24 * 60 * 60);
1097 }
1098 } else {
1099 LOGE("gnss_ipc_service_start() fail.");
1100 }
1101#endif
b.liu8f231a12024-05-31 17:55:06 +08001102
1103 LOGD("main() exit.");
1104 return 0;
1105}