blob: 59589b5e45c0ad8692f5e5dea82afc2627f2706f [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];
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{
yq.wang1ddd1fd2024-07-25 23:00:14 -0700301 int ret = -1;
b.liu8f231a12024-05-31 17:55:06 +0800302 if(gnss_usb_at_port_fd > 0) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700303 ret = write(gnss_usb_at_port_fd, nmea, nmea_len);
b.liu8f231a12024-05-31 17:55:06 +0800304 }
305
306 if(gnss_usb_nmea_port_fd > 0) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700307 ret = write(gnss_usb_nmea_port_fd, nmea, nmea_len);
b.liu8f231a12024-05-31 17:55:06 +0800308 }
309
310 if(gnss_uart_at_port_fd > 0) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700311 ret = write(gnss_uart_at_port_fd, nmea, nmea_len);
b.liu8f231a12024-05-31 17:55:06 +0800312 }
313
314 if(gnss_pty_master_fd > 0) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700315 ret = write(gnss_pty_master_fd, nmea, nmea_len);
b.liu8f231a12024-05-31 17:55:06 +0800316 }
yq.wang937355d2024-07-26 01:24:42 -0700317 ind_nmea_print(nmea);
318 ind_location_print(nmea);
b.liu8f231a12024-05-31 17:55:06 +0800319}
320
321static unsigned char nmea_checksum(const char *nmea)
322{
323 const char *p = nmea;
324 unsigned char chs = 0;
325
326 while (*p == '$') // skip '$'
327 p++;
328 while (*p != '*' && *p != 0)
329 chs ^= *p++;
330
331 return chs;
332}
333
334static bool nmea_check(const char *nmea, int nmea_len)
335{
336 char **ptr = gnss_filter_info;
337 while(*ptr) {
338 if(strstr(nmea, *ptr)) {
339 break;
340 }
341 ptr++;
342 }
343
344 if(*ptr == NULL) {
345 LOGD("Unknown NMEA[%d]:%s", nmea_len, nmea);
346 return FALSE;
347 }
348
349 char *checksum_str = strstr(nmea, "*");
350 checksum_str++; // Jump '*'
351 char checksum_buf[3] = {0};
352 snprintf(checksum_buf, 3, "%02x", nmea_checksum(nmea));
353 if(strncasecmp(checksum_buf, checksum_str, 2)) {
354 LOGD("Checksum error[%d](checksum - %s):%s", nmea_len, checksum_buf, nmea);
355 return FALSE;
356 }
357
358 return TRUE;
359}
360
361static void gnss_nmea_process(const char *data, int data_len)
362{
b.liu99c645d2024-06-20 10:52:15 +0800363 // LOGD("gnss_nmea_process() : data_len - %d", data_len);
364#if 0
b.liu8f231a12024-05-31 17:55:06 +0800365 char nmea[GNSS_BUFF_SIZE] = {0};
366 memcpy(nmea, data, data_len);
b.liu99c645d2024-06-20 10:52:15 +0800367#else
368 const char *nmea = data;
369#endif
b.liu8f231a12024-05-31 17:55:06 +0800370
371 if(!nmea_check(nmea, data_len)) {
b.liu42f558e2024-07-18 14:06:49 +0800372 // No print "$HOSTSLEEP".
373 if(memcmp(nmea, "$HOSTSLEEP", strlen("$HOSTSLEEP"))) {
374 LOGD("NO-NMEA:%s", nmea);
375 }
b.liu99c645d2024-06-20 10:52:15 +0800376#if GNSS_DEBUG
377 log_save(nmea_log_fd, "/**/", 4);
378 log_save(nmea_log_fd, nmea, data_len);
379#endif
380 if(gnss_info.gnss_set_cb)
381 gnss_info.gnss_set_cb(nmea, data_len);
b.liu8f231a12024-05-31 17:55:06 +0800382 return;
383 }
384
385#ifdef GNSS_DEBUG
386 if(nmea_log_enable) {
387 LOGD("NMEA[%d]:%s", data_len, nmea);
388 }
b.liu99c645d2024-06-20 10:52:15 +0800389
390 log_save(nmea_log_fd, nmea, data_len);
b.liu8f231a12024-05-31 17:55:06 +0800391#endif
392
393 nmea_print(nmea, data_len);
394}
395
396#if 0
397static void gnss_cmd_rsp_process(const char *data, int data_len)
398{
399 char rsp[GNSS_BUFF_SIZE] = {0};
400 memcpy(rsp, data, data_len);
401 LOGD("RSP[%d]:%s", data_len, rsp);
402}
403#endif
404
405static bool nmea_char_check(char ch)
406{
407 if(isalnum(ch) || ch == '$' || ch == '\r' || ch == '\n' || ch == '.'
yq.wang1ddd1fd2024-07-25 23:00:14 -0700408 || ch == ',' || ch == '*' || ch == '\0' || ch == '/' || ch == '_' || ch == '=' || ch == '-')
b.liu8f231a12024-05-31 17:55:06 +0800409 return TRUE;
410
411 return FALSE;
412}
413
414static void gnss_data_process(const char *data, int data_len)
415{
416 if(gnss_info.state == GNSS_STATE_OPEN) {
417 LOGD("GNSS_OPEN[%d]:%s", data_len, data);
418 } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) {
419 // LOGD("GNSS_DL[%d]:%s", data_len, data);
b.liu99c645d2024-06-20 10:52:15 +0800420 if(gnss_info.gnss_dl_read_cb) {
421 gnss_info.gnss_dl_read_cb(data, data_len);
422 }
b.liu8f231a12024-05-31 17:55:06 +0800423 } else if(gnss_info.state == GNSS_STATE_READY) {
424 int index = 0;
425 while(index < data_len) {
426 if(nmea_found) {
427 if(!nmea_char_check(data[index])) {
b.liud0ba7152024-06-19 14:47:21 +0800428 // Copy nmea_buff to data_buff
429 // Start with '$', but not nmea data, so copy to data_buff.
430 memcpy(data_buff + data_buff_len, nmea_buff, nmea_buff_len);
431 data_buff_len += nmea_buff_len;
432 data_buff[data_buff_len++] = data[index];
433
434 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800435 nmea_found = FALSE;
b.liu8f231a12024-05-31 17:55:06 +0800436 continue;
437 }
438
439 if(data[index] != '\0') {
b.liud0ba7152024-06-19 14:47:21 +0800440 nmea_buff[nmea_buff_len++] = data[index];
441 if(nmea_buff[nmea_buff_len - 1] == '\n') {
442 if(data_buff_len > 0) {
b.liu99c645d2024-06-20 10:52:15 +0800443#if GNSS_DEBUG
444 log_save(nmea_log_fd, "/**/", 4);
445 log_save(nmea_log_fd, data_buff, data_buff_len);
446#endif
447 if(gnss_info.gnss_set_cb) {
448 gnss_info.gnss_set_cb(data_buff, data_buff_len);
449 }
b.liud0ba7152024-06-19 14:47:21 +0800450 data_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800451 }
452
b.liud0ba7152024-06-19 14:47:21 +0800453 if(nmea_buff_len > 6 && nmea_buff[nmea_buff_len - 5] == '*') { // $XXX*YY\r\n
b.liu99c645d2024-06-20 10:52:15 +0800454 nmea_buff[nmea_buff_len] = '\0';
b.liud0ba7152024-06-19 14:47:21 +0800455 gnss_nmea_process(nmea_buff, nmea_buff_len);
b.liu99c645d2024-06-20 10:52:15 +0800456 } else if(nmea_buff_len > 0) {
457 nmea_buff[nmea_buff_len] = '\0';
b.liu42f558e2024-07-18 14:06:49 +0800458 if(memcmp(nmea_buff, "$HOSTSLEEP", strlen("$HOSTSLEEP"))) {
459 LOGD("NO-NMEA:%s", nmea_buff);
460 }
b.liu99c645d2024-06-20 10:52:15 +0800461#if GNSS_DEBUG
462 log_save(nmea_log_fd, "/**/", 4);
463 log_save(nmea_log_fd, nmea_buff, nmea_buff_len);
464#endif
b.liud0ba7152024-06-19 14:47:21 +0800465 }
466
467 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800468 nmea_found = FALSE;
469 }
470 }
471 } else {
472 if(data[index] == '$') {
b.liud0ba7152024-06-19 14:47:21 +0800473 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800474 nmea_found = TRUE;
b.liud0ba7152024-06-19 14:47:21 +0800475 nmea_buff[nmea_buff_len++] = data[index];
476 } else {
477 data_buff[data_buff_len++] = data[index];
b.liu8f231a12024-05-31 17:55:06 +0800478 }
479 }
480 index++;
481 }
482 } else {
483 LOGW("Unknown state : %d", gnss_info.state);
484 }
485}
486
487void* gnss_read_pthread(void* arg)
488{
489 LOGD("gnss_read_pthread enter.");
490 char buffer[GNSS_BUFF_SIZE];
491 int len = 0;
492 int ret = 0;
493 fd_set fdr, fdw;
494 int fd_max = 0;
495
b.liu8f231a12024-05-31 17:55:06 +0800496 fd_max = (gnss_info.fd > fd_max) ? gnss_info.fd : fd_max;
b.liu8f231a12024-05-31 17:55:06 +0800497 fd_max = (gnss_info.exit_fd[0] > fd_max) ? gnss_info.exit_fd[0] : fd_max;
b.liud0ba7152024-06-19 14:47:21 +0800498 memset(nmea_buff, 0, sizeof(nmea_buff));
499 memset(data_buff, 0, sizeof(data_buff));
500 nmea_buff_len = 0;
501 data_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800502#if GNSS_DEBUG
b.liu8f231a12024-05-31 17:55:06 +0800503 if(nmea_log_enable) {
504 debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
505 if(debug_fd < 0) {
506 LOGE("Open debug fd fail.");
507 }
508 nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
509 if(nmea_log_fd < 0) {
510 LOGE("Open nmea fd fail.");
511 }
b.liu99c645d2024-06-20 10:52:15 +0800512 debug_fd_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800513 }
514#endif
515
b.liudbc3f4b2024-06-25 18:22:24 +0800516 LOGD("uart_fd - %d, exit_fd - %d", gnss_info.fd, gnss_info.exit_fd[0]);
517
b.liu8f231a12024-05-31 17:55:06 +0800518 while(gnss_info.state >= GNSS_STATE_OPEN) {
wangyouqiang55d36bf2024-06-27 09:35:52 +0800519 FD_ZERO(&fdw);
520 FD_ZERO(&fdr);
521 FD_SET(gnss_info.fd, &fdr);
522 FD_SET(gnss_info.exit_fd[0], &fdr);
b.liu8f231a12024-05-31 17:55:06 +0800523 ret = select(fd_max + 1, &fdr, &fdw, 0, NULL);
524 //LOGD("select - %d", ret);
b.liuece0db02024-06-25 18:39:09 +0800525 if(gnss_info.state < GNSS_STATE_OPEN) {
526 LOGD("State = %d, ret = %d", gnss_info.state, ret);
527 if(ret > 0) {
528 if (FD_ISSET(gnss_info.fd, &fdr)) {
529 LOGD("gnss_fd can read.");
530 } else if (FD_ISSET(gnss_info.exit_fd[0], &fdr)) {
531 LOGD("exit_fd can read.");
532 } else {
533 LOGW("Unknown select event.");
534 }
535 }
536 break;
537 }
538
b.liu8f231a12024-05-31 17:55:06 +0800539 if (ret < 0)
540 {
541 if (errno == EINTR)
542 {
543 continue;
544 }
545 LOGE("select error, errno = %d (%s)", errno, strerror(errno));
546 break;
547 }
548 else if (ret == 0)
549 {
550 LOGE("select ret == 0");
551 break;
552 }
553
554 if (FD_ISSET(gnss_info.fd, &fdr))
555 {
556 memset(buffer, 0, GNSS_BUFF_SIZE);
b.liu99c645d2024-06-20 10:52:15 +0800557 len = read(gnss_info.fd, buffer, GNSS_BUFF_SIZE - 1);
b.liu8f231a12024-05-31 17:55:06 +0800558 if(len > 0) {
559 //log_hex("READ", buffer, len);
560
561#if GNSS_DEBUG
b.liu99c645d2024-06-20 10:52:15 +0800562 //LOGD("read data_len = %d", len);
563 log_save(debug_fd, buffer, len);
b.liu8f231a12024-05-31 17:55:06 +0800564#endif
565
566 gnss_data_process(buffer, len);
567
568 } else if(len ==0 ){
569 LOGE("Read end : len = 0");
570 break;
571 } else {
572 if(EAGAIN == errno) {
573 usleep(50000);
574 continue;
575 } else {
576 LOGD("Read ret = -1 ,errno = %d", errno);
577 break;
578 }
579 }
580 }
581 else if (FD_ISSET(gnss_info.exit_fd[0], &fdr))
582 {
b.liuece0db02024-06-25 18:39:09 +0800583 LOGD("exit_fd select event.");
b.liu8f231a12024-05-31 17:55:06 +0800584 memset(buffer, 0, GNSS_BUFF_SIZE);
b.liudbc3f4b2024-06-25 18:22:24 +0800585 len = read(gnss_info.exit_fd[0], buffer, GNSS_BUFF_SIZE);
b.liu8f231a12024-05-31 17:55:06 +0800586 if(len > 0) {
587 if(strcmp(buffer, "exit") == 0) {
588 LOGD("Get thread exit message.");
589 break;
590 }
591 }
592 }
593 else
594 {
595 LOGW("Unknown select event.");
596 continue;
597 }
598 }
599
600#if GNSS_DEBUG
601 if(debug_fd > 0) {
602 close(debug_fd);
603 debug_fd = -1;
604 }
605 if(nmea_log_fd > 0) {
606 close(nmea_log_fd);
607 nmea_log_fd = -1;
608 }
609#endif
610
611 gnss_info.state = GNSS_STATE_CLOSE;
612 LOGD("gnss_read_pthread exit.");
613 return NULL;
614}
615
616#if 0
617int gnss_write(int fd, const void *data, int data_len)
618{
619 int count = 0;
620 int len = 0;
621 while(1)
622 {
623 len = write(fd, data + count, data_len - count);
624 if (len > 0)
625 {
626 count += len;
627 }
628 else
629 {
630 LOGE("write() fail,ret = %d,errno = %d", len, errno);
631 break;
632 }
633
634 if (count == data_len)
635 break;
636 }
637
638 return count;
639}
640#else
b.liu778645e2024-06-21 16:47:42 +0800641int gnss_write(int fd, const void* buf, int buf_len)
b.liu8f231a12024-05-31 17:55:06 +0800642{
b.liu778645e2024-06-21 16:47:42 +0800643 ssize_t size;
644 ssize_t size_to_wr;
b.liu8f231a12024-05-31 17:55:06 +0800645 ssize_t size_written;
646 if(GNSS_BUFF_SIZE < buf_len)
647 {
648 return -1;
649 }
650 for(size = 0; size < buf_len;)
651 {
652 size_to_wr = buf_len - size;
653 if( size_to_wr > GNSS_BUFF_SIZE)
654 size_to_wr = GNSS_BUFF_SIZE;
655
b.liu778645e2024-06-21 16:47:42 +0800656 size_written = write(fd, (const uint8*)buf + size, size_to_wr);
b.liu8f231a12024-05-31 17:55:06 +0800657 if (size_written==-1)
658 {
659 return -1;
660 }
661 size += size_written;
662 if(size_written != size_to_wr)
663 {
664 return size;
665 }
666 }
667 // LOGD("SEND %d / %d", size, buf_len);
668 return size;
669}
670#endif
671
672int gnss_init(uint32 print_port)
673{
674 if(gnss_info.state != GNSS_STATE_CLOSE) {
675 LOGW("GNSS not close:%d", gnss_info.state);
b.liuced8dd02024-06-28 13:28:29 +0800676 if(gnss_info.state == GNSS_STATE_READY) {
677 LOGD("Reset print port : %d -> %d", gnss_info.print_port, print_port);
678 if(gnss_info.print_port != print_port) {
679 return gnss_ports_open(print_port);
680 } else {
681 return GNSS_ERR_OK;
682 }
683 } else {
684 return GNSS_ERR_OK;
685 }
b.liu8f231a12024-05-31 17:55:06 +0800686 }
687
688 int ret = 0;
b.liu99c645d2024-06-20 10:52:15 +0800689 if(gnss_info.dl_befor_open) {
690 //if(gnss_info.auto_dl_fw) {
691 gnss_info.state = GNSS_STATE_DOWNLOAD;
b.liudbc3f4b2024-06-25 18:22:24 +0800692 ret = gnss_info.gnss_fw_dl(gnss_info.fd, NULL, gnss_info.dev_name);
b.liu99c645d2024-06-20 10:52:15 +0800693 if(ret) {
694 LOGE("gnss_fw_dl() fail : %d", ret);
695 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800696 return GNSS_ERR_DL_FW;
b.liu99c645d2024-06-20 10:52:15 +0800697 }
b.liu8f231a12024-05-31 17:55:06 +0800698
b.liu99c645d2024-06-20 10:52:15 +0800699 gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name);
700 if(gnss_info.fd <= 0) {
701 LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd);
702 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800703 return GNSS_ERR_OPEN_DEV;
b.liu99c645d2024-06-20 10:52:15 +0800704 }
705 if(pipe(gnss_info.exit_fd)) {
706 LOGE("pipe() fail[%d].", errno);
b.liudbc3f4b2024-06-25 18:22:24 +0800707 return GNSS_ERR_UNKNOWN;
b.liu99c645d2024-06-20 10:52:15 +0800708 }
709 // GNSS is opened.
710 gnss_info.state = GNSS_STATE_OPEN;
b.liu8f231a12024-05-31 17:55:06 +0800711
b.liu99c645d2024-06-20 10:52:15 +0800712#if 0
713 // Start gnss read thread.
714 pthread_attr_t thread_attr;
715 pthread_attr_init(&thread_attr);
716 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
717 {
718 LOGE("pthread_attr_setdetachstate() fail.");
719 goto main_exit;
720 }
b.liu8f231a12024-05-31 17:55:06 +0800721
b.liu99c645d2024-06-20 10:52:15 +0800722 if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL))
b.liu8f231a12024-05-31 17:55:06 +0800723#else
b.liu99c645d2024-06-20 10:52:15 +0800724 if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL))
b.liu8f231a12024-05-31 17:55:06 +0800725#endif
b.liu99c645d2024-06-20 10:52:15 +0800726 {
727 LOGE("pthread_create() fail.");
728 goto exit_with_close;
729 }
b.liu8f231a12024-05-31 17:55:06 +0800730
b.liu99c645d2024-06-20 10:52:15 +0800731 ret = gnss_info.gnss_dev_open();
732 if(ret) {
733 LOGE("gnss_dev_open() fail : %d", ret);
734 goto exit_with_thread_exit;
735 }
736 //}
737 } else {
738 gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name);
739 if(gnss_info.fd <= 0) {
740 LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd);
741 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800742 return GNSS_ERR_OPEN_DEV;
b.liu99c645d2024-06-20 10:52:15 +0800743 }
744 if(pipe(gnss_info.exit_fd)) {
745 LOGE("pipe() fail[%d].", errno);
b.liudbc3f4b2024-06-25 18:22:24 +0800746 return GNSS_ERR_UNKNOWN;
b.liu99c645d2024-06-20 10:52:15 +0800747 }
748 // GNSS is opened.
749 gnss_info.state = GNSS_STATE_OPEN;
b.liu8f231a12024-05-31 17:55:06 +0800750
b.liu99c645d2024-06-20 10:52:15 +0800751#if 0
752 // Start gnss read thread.
753 pthread_attr_t thread_attr;
754 pthread_attr_init(&thread_attr);
755 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
756 {
757 LOGE("pthread_attr_setdetachstate() fail.");
758 goto main_exit;
759 }
760
761 if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL))
762#else
763 if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL))
764#endif
765 {
766 LOGE("pthread_create() fail.");
767 goto exit_with_close;
768 }
769
770 ret = gnss_info.gnss_dev_open();
b.liu8f231a12024-05-31 17:55:06 +0800771 if(ret) {
b.liu99c645d2024-06-20 10:52:15 +0800772 LOGE("gnss_dev_open() fail : %d", ret);
773 goto exit_with_thread_exit;
774 }
b.liu8f231a12024-05-31 17:55:06 +0800775 }
776
777 // GNSS is ready, NMEA can print from uart.
778 gnss_info.state = GNSS_STATE_READY;
b.liu8f231a12024-05-31 17:55:06 +0800779
780 LOGD("GNSS open success.");
781
b.liuced8dd02024-06-28 13:28:29 +0800782 return gnss_ports_open(print_port);
b.liu8f231a12024-05-31 17:55:06 +0800783
b.liu8f231a12024-05-31 17:55:06 +0800784exit_with_thread_exit:
785 gnss_info.state = GNSS_STATE_CLOSING;
786 // Wait for read thread exit.
787 ret = pthread_join(gnss_info.read_pid, NULL);
788 if(ret){
789 LOGE("pthrad_join fail(%d)",ret);
790 }
791exit_with_close:
792 if(gnss_info.gnss_close(gnss_info.fd)) {
793 LOGE("gnss_close() fail.");
794 }
795 if(gnss_info.exit_fd[0] > 0) {
796 close(gnss_info.exit_fd[0]);
797 gnss_info.exit_fd[0] = -1;
798 }
799 if(gnss_info.exit_fd[1] > 0) {
800 close(gnss_info.exit_fd[1]);
801 gnss_info.exit_fd[1] = -1;
802 }
803 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800804 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800805}
806
807int gnss_deinit()
808{
809 if(gnss_info.state == GNSS_STATE_CLOSE) {
810 LOGW("GNSS is closed.");
b.liudbc3f4b2024-06-25 18:22:24 +0800811 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800812 } else if(gnss_info.state == GNSS_STATE_CLOSING) {
813 LOGW("GNSS is closing...");
b.liudbc3f4b2024-06-25 18:22:24 +0800814 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800815 } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) {
816 LOGW("GNSS is downloading...");
b.liudbc3f4b2024-06-25 18:22:24 +0800817 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800818 }
819
b.liu978f5432024-07-01 18:04:18 +0800820 if(gnss_info.gnss_dev_close(gnss_info.fd)) {
821 LOGE("gnss_dev_close() fail.");
822 return GNSS_ERR_UNKNOWN;
823 }
824
b.liu8f231a12024-05-31 17:55:06 +0800825 // Wait for read thread exit.
826 if(gnss_info.exit_fd[1] > 0) {
827 write(gnss_info.exit_fd[1], "exit", 4);
828 }
829
830 gnss_info.state = GNSS_STATE_CLOSING;
831 int ret = pthread_join(gnss_info.read_pid, NULL);
832 if(ret){
833 LOGE("pthrad_join fail(%d)",ret);
b.liudbc3f4b2024-06-25 18:22:24 +0800834 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800835 }
836
837 if(gnss_info.gnss_close(gnss_info.fd)) {
838 LOGE("gnss_close() fail.");
b.liudbc3f4b2024-06-25 18:22:24 +0800839 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800840 }
841
b.liu8f231a12024-05-31 17:55:06 +0800842 if(gnss_ports_close()) {
843 LOGE("gnss_ports_close fail.");
b.liudbc3f4b2024-06-25 18:22:24 +0800844 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800845 }
846
b.liud0ba7152024-06-19 14:47:21 +0800847 LOGD("gnss_ports_close() complete.");
848
b.liu8f231a12024-05-31 17:55:06 +0800849 gnss_info.fd = -1;
850 if(gnss_info.exit_fd[0] > 0) {
851 close(gnss_info.exit_fd[0]);
852 gnss_info.exit_fd[0] = -1;
853 }
854 if(gnss_info.exit_fd[1] > 0) {
855 close(gnss_info.exit_fd[1]);
856 gnss_info.exit_fd[1] = -1;
857 }
858 gnss_info.state = GNSS_STATE_CLOSE;
859 LOGD("GNSS close success.");
b.liudbc3f4b2024-06-25 18:22:24 +0800860 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800861}
862
863int gnss_set(const void* buf, unsigned int buf_len, void *cmd_rsp, int cmd_rsp_len)
864{
b.liud0ba7152024-06-19 14:47:21 +0800865 if(buf && buf_len > 0) {
866 if(cmd_rsp && cmd_rsp_len > 0) {
867 memset(cmd_rsp, 0, cmd_rsp_len);
868 }
b.liu8f231a12024-05-31 17:55:06 +0800869 return gnss_info.gnss_set(gnss_info.fd, buf, cmd_rsp, cmd_rsp_len);
870 } else {
b.liudbc3f4b2024-06-25 18:22:24 +0800871 return GNSS_ERR_UNKNOWN;
872 }
873}
874
875int gnss_dl_fw(const char* fw_name, void *rsp, int rsp_len)
876{
b.liu42f558e2024-07-18 14:06:49 +0800877 // Only 8122 support download fw.
b.liudbc3f4b2024-06-25 18:22:24 +0800878 if(gnss_info.gnss_id != GNSS_TYPE_8122) {
879 return GNSS_ERR_UNSUPPORT;
880 }
881
882 if(rsp && rsp_len > 0) {
883 memset(rsp, 0, rsp_len);
884 }
885
886 if(gnss_info.gnss_fw_dl) {
887 if(GNSS_ERR_OK != gnss_deinit()) {
888 LOGE("Close gnss fail.");
889 return GNSS_ERR_UNKNOWN;
890 } else {
891 LOGD("Start gnss fw dl.");
892 return gnss_info.gnss_fw_dl(gnss_info.fd, fw_name, gnss_info.dev_name);
893 }
894 } else {
b.liu42f558e2024-07-18 14:06:49 +0800895 return GNSS_ERR_UNSUPPORT;
b.liu8f231a12024-05-31 17:55:06 +0800896 }
897}
898
b.liue77ac3a2024-07-17 17:36:57 +0800899int gnss_ind_set(int fd, int ind_type)
900{
901 int index = 0;
902 if(ind_type) { // Add IND flag.
903 while(index < GNSS_CLI_IND_MAX) {
904 if(ind_info[index].cli_fd == fd)
905 break;
906 index++;
907 }
908
909 if(index == GNSS_CLI_IND_MAX) { // Add flag
910 index = 0;
911 while(index < GNSS_CLI_IND_MAX) {
912 if(ind_info[index].cli_fd <= 0)
913 break;
914 index++;
915 }
yq.wang1ddd1fd2024-07-25 23:00:14 -0700916 if(index == GNSS_CLI_IND_MAX)
917 {
918 LOGE("ind flag is full.");
919 return GNSS_ERR_CLI_FULL;
920 }
b.liue77ac3a2024-07-17 17:36:57 +0800921 ind_info[index].cli_fd = fd;
922 ind_info[index].ind_flag = (uint32)ind_type;
yq.wang937355d2024-07-26 01:24:42 -0700923 } else { // Change flag
b.liue77ac3a2024-07-17 17:36:57 +0800924 ind_info[index].cli_fd = fd;
925 ind_info[index].ind_flag = (uint32)ind_type;
926 }
927 } else { // Clear IND flag.
928 while(index < GNSS_CLI_IND_MAX) {
929 if(ind_info[index].cli_fd == fd)
930 break;
931 index++;
932 }
933
934 if(index == GNSS_CLI_IND_MAX) {
935 return GNSS_ERR_ARG;
yq.wang937355d2024-07-26 01:24:42 -0700936 }
b.liue77ac3a2024-07-17 17:36:57 +0800937 ind_info[index].cli_fd = 0;
938 ind_info[index].ind_flag = 0;
939 }
940
941 return GNSS_ERR_OK;
942}
943
b.liu8f231a12024-05-31 17:55:06 +0800944static void sig_process(int sig)
945{
946 LOGI("I got signal %d\n", sig);
947 if(gnss_deinit()) {
948 LOGE("gnss_deinit() fail, no exist...");
949 return;
950 }
951
952 switch(sig)
953 {
954 case SIGINT: // Ctrl + C
955 {
956 LOGI("Exit by SIGINT.\n");
957 exit(0);
958 }
959 case SIGQUIT: // Ctrl + \ (ÀàËÆ SIGINT £¬µ«Òª²úÉúcoreÎļþ)
960 {
961 LOGI("Exit by SIGQUIT.\n");
962 exit(0);
963 }
964 case SIGTERM:// ĬÈÏkill (ͬ SIGKILL £¬µ« SIGKILL ²»¿É²¶»ñ)
965 {
966 LOGI("Exit by SIGTERM.\n");
967 exit(0);
968 }
969 case SIGTSTP:// Ctrl + Z (ͬ SIGSTOP £¬µ« SIGSTOP ²»¿É²¶»ñ)
970 {
971 LOGI("Exit by SIGTSTP.\n");
972 exit(0);
973 }
974 case SIGSEGV: // Èç¿ÕÖ¸Õë
975 {
976 LOGI("Exit by SIGSEGV.\n");
977 exit(0);
978 }
979 default:
980 {
981 LOGI("Unknown sig:%d\n",sig);
982 break;
983 }
984 }
985}
986
987
b.liuced8dd02024-06-28 13:28:29 +0800988// mbtk_gnssd 6228 /dev/ttyS2 baud 0/1 <port_type>
b.liu8f231a12024-05-31 17:55:06 +0800989int main(int argc, char *argv[])
990{
991 mbtk_log_init("radio", GNSS_TAG);
992
b.liubb590492024-06-13 16:42:08 +0800993#ifdef MBTK_DUMP_SUPPORT
994 mbtk_debug_open(NULL, TRUE);
995#endif
996
b.liu8f231a12024-05-31 17:55:06 +0800997 signal(SIGINT, sig_process);
998 signal(SIGQUIT, sig_process);
999 signal(SIGTERM, sig_process);
1000
1001 if(arg_check(argc, argv)) {
1002 return -1;
1003 }
1004
1005#ifdef GNSS_DEBUG
1006 char buff[10];
1007 memset(buff, 0, 10);
1008 property_get(MBTK_PROP_GNSS_LOG, buff, "");
1009 if(strlen(buff) > 0 && atoi(buff) > 0) {
1010 nmea_log_enable = TRUE;
1011 }
1012#endif
1013
1014 memset(&gnss_info, 0, sizeof(gnss_info_t));
1015 memcpy(gnss_info.dev_name, argv[2], strlen(argv[2]));
1016 gnss_info.state = GNSS_STATE_CLOSE;
1017 if(!strcmp(argv[1], GNSS_ID_6228)) {
1018 gnss_info.gnss_id = GNSS_TYPE_6228;
1019 gnss_info.auto_open = (bool)atoi(argv[3]);
1020 gnss_info.auto_dl_fw = TRUE;
b.liu99c645d2024-06-20 10:52:15 +08001021 gnss_info.dl_befor_open = FALSE;
b.liu8f231a12024-05-31 17:55:06 +08001022 gnss_info.gnss_dev_open = gnss_6228_dev_open;
1023 gnss_info.gnss_dev_close = gnss_6228_dev_close;
1024 gnss_info.gnss_open = gnss_6228_open;
1025 gnss_info.gnss_close = gnss_6228_close;
1026 gnss_info.gnss_fw_dl = gnss_6228_fw_dl;
1027 gnss_info.gnss_dl_read_cb = gnss_6228_dl_read_cb;
1028 gnss_info.gnss_set = gnss_6228_set;
1029 gnss_info.gnss_set_cb = gnss_6228_set_cb;
b.liuf9fbfa12024-06-14 15:53:59 +08001030 } else if(!strcmp(argv[1], GNSS_ID_8122)) {
1031 gnss_info.gnss_id = GNSS_TYPE_8122;
1032 gnss_info.auto_open = (bool)atoi(argv[3]);
1033 gnss_info.auto_dl_fw = FALSE;
b.liu99c645d2024-06-20 10:52:15 +08001034 gnss_info.dl_befor_open = FALSE;
b.liuf9fbfa12024-06-14 15:53:59 +08001035 gnss_info.gnss_dev_open = gnss_8122_dev_open;
1036 gnss_info.gnss_dev_close = gnss_8122_dev_close;
1037 gnss_info.gnss_open = gnss_8122_open;
1038 gnss_info.gnss_close = gnss_8122_close;
1039 gnss_info.gnss_fw_dl = gnss_8122_fw_dl;
1040 gnss_info.gnss_dl_read_cb = NULL;
1041 gnss_info.gnss_set = gnss_8122_set;
b.liu5f950c52024-06-15 20:13:12 +08001042 gnss_info.gnss_set_cb = gnss_8122_set_cb;
b.liu99c645d2024-06-20 10:52:15 +08001043 } else if(!strcmp(argv[1], GNSS_ID_5311)) {
1044 gnss_info.gnss_id = GNSS_TYPE_5311;
1045 gnss_info.auto_open = (bool)atoi(argv[3]);
1046 gnss_info.auto_dl_fw = TRUE;
1047 gnss_info.dl_befor_open = TRUE;
1048 gnss_info.gnss_dev_open = gnss_5311_dev_open;
1049 gnss_info.gnss_dev_close = gnss_5311_dev_close;
1050 gnss_info.gnss_open = gnss_5311_open;
1051 gnss_info.gnss_close = gnss_5311_close;
1052 gnss_info.gnss_fw_dl = gnss_5311_fw_dl;
1053 gnss_info.gnss_dl_read_cb = NULL;
1054 gnss_info.gnss_set = gnss_5311_set;
1055 gnss_info.gnss_set_cb = gnss_5311_set_cb;
b.liu42f558e2024-07-18 14:06:49 +08001056 } else if(!strcmp(argv[1], GNSS_ID_N50DB)) {
1057 gnss_info.gnss_id = GNSS_TYPE_N50DB;
1058 gnss_info.auto_open = (bool)atoi(argv[3]);
1059 gnss_info.auto_dl_fw = FALSE;
1060 gnss_info.dl_befor_open = FALSE;
1061 gnss_info.gnss_dev_open = gnss_n50db_dev_open;
1062 gnss_info.gnss_dev_close = gnss_n50db_dev_close;
1063 gnss_info.gnss_open = gnss_n50db_open;
1064 gnss_info.gnss_close = gnss_n50db_close;
1065 gnss_info.gnss_fw_dl = gnss_n50db_fw_dl;
1066 gnss_info.gnss_dl_read_cb = NULL;
1067 gnss_info.gnss_set = gnss_n50db_set;
1068 gnss_info.gnss_set_cb = gnss_n50db_set_cb;
b.liu8f231a12024-05-31 17:55:06 +08001069 } else {
1070 LOGE("No support : %s", argv[1]);
1071 return -1;
1072 }
1073
1074 LOGD("GNSS : %s, Device: %s", argv[1], gnss_info.dev_name);
1075 // Auto open gnss.
1076 if(gnss_info.auto_open) {
b.liuced8dd02024-06-28 13:28:29 +08001077 int init_mode = atoi(argv[3]);
1078 if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) != init_mode) {
1079 init_mode = 0;
b.liu8f231a12024-05-31 17:55:06 +08001080 }
b.liuced8dd02024-06-28 13:28:29 +08001081 if(gnss_init((uint32)init_mode)) {
1082 LOGE("gnss_init() fail.");
1083 // return -1;
1084 }
1085 } else {
1086 gnss_info.print_port = 0;
b.liu8f231a12024-05-31 17:55:06 +08001087 }
1088
1089 // Init ubus and waitting IPC commands.
b.liud0ba7152024-06-19 14:47:21 +08001090#ifdef MBTK_GNSS_UBUS_ENABLE
b.liu8f231a12024-05-31 17:55:06 +08001091 if(gnss_ubus_init()) {
1092 LOGD("main() run...");
1093 uloop_run();
1094 } else {
1095 LOGE("gnss_ubus_init() fail.");
1096 }
b.liu5f950c52024-06-15 20:13:12 +08001097#else
1098 if(!gnss_ipc_service_start()) {
1099 LOGD("main() run...");
1100 while(1) {
1101 sleep(24 * 60 * 60);
1102 }
1103 } else {
1104 LOGE("gnss_ipc_service_start() fail.");
1105 }
1106#endif
b.liu8f231a12024-05-31 17:55:06 +08001107
1108 LOGD("main() exit.");
1109 return 0;
1110}