blob: c4f2d8df4ab275a87030cb55a03aad6f45fe3adc [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"
21
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.liu99c645d2024-06-20 10:52:15 +080075 LOGD("mbtk_gnssd <6228/8122/5311> <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.
86 if(strcmp(argv[1], GNSS_ID_6228) && strcmp(argv[1], GNSS_ID_8122) && strcmp(argv[1], GNSS_ID_5311)) {
b.liu4ae41182024-06-28 16:30:15 +080087 LOGE("argv[1] = %s", argv[1]);
b.liu8f231a12024-05-31 17:55:06 +080088 goto check_fail;
89 }
90
91 if(access(argv[2], R_OK | W_OK)) {
b.liu4ae41182024-06-28 16:30:15 +080092 LOGE("access(%s) rw fail. ", argv[2]);
b.liu8f231a12024-05-31 17:55:06 +080093 goto check_fail;
94 }
95
b.liuced8dd02024-06-28 13:28:29 +080096#if 0
97 int init_mode = atoi(argv[3]);
98 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 +080099 goto check_fail;
100 }
b.liuced8dd02024-06-28 13:28:29 +0800101#endif
b.liu8f231a12024-05-31 17:55:06 +0800102
103 return 0;
104check_fail:
105 help();
106 return -1;
107}
108
109static int gnss_ports_open(uint32 print_port)
110{
b.liuced8dd02024-06-28 13:28:29 +0800111 // TTY AT change.
112 if((gnss_info.print_port & GNSS_PRINT_PORT_TTY_AT) != (print_port & GNSS_PRINT_PORT_TTY_AT)) {
113 if(print_port & GNSS_PRINT_PORT_TTY_AT) { // Open
114 if(gnss_pty_open(&gnss_pty_master_fd, &gnss_pty_slave_fd, GNSS_PORT_PTY)) {
115 return GNSS_ERR_OPEN_DEV;
116 }
117 LOGD("Open PTY port success.");
118 } else { // Close
119 if(gnss_pty_slave_fd > 0) {
120 close(gnss_pty_slave_fd);
121 gnss_pty_slave_fd = -1;
122 unlink(GNSS_PORT_PTY);
123 }
124 LOGD("Close PTY port success.");
b.liu8f231a12024-05-31 17:55:06 +0800125 }
b.liu8f231a12024-05-31 17:55:06 +0800126 }
127
b.liuced8dd02024-06-28 13:28:29 +0800128 // USB AT change.
129 if((gnss_info.print_port & GNSS_PRINT_PORT_USB_AT) != (print_port & GNSS_PRINT_PORT_USB_AT)) {
130 if(print_port & GNSS_PRINT_PORT_USB_AT) { // Open
b.liu978f5432024-07-01 18:04:18 +0800131 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 +0800132 return GNSS_ERR_OPEN_DEV;
133 }
134 LOGD("Open USB AT port success.");
135 } else { // Close
136 if(gnss_usb_at_port_fd > 0) {
137 close(gnss_usb_at_port_fd);
138 gnss_usb_at_port_fd = -1;
139 }
140 LOGD("Close USB AT port success.");
b.liu8f231a12024-05-31 17:55:06 +0800141 }
b.liu8f231a12024-05-31 17:55:06 +0800142 }
143
b.liuced8dd02024-06-28 13:28:29 +0800144 // USB NMEA change.
145 if((gnss_info.print_port & GNSS_PRINT_PORT_USB_NMEA) != (print_port & GNSS_PRINT_PORT_USB_NMEA)) {
146 if(print_port & GNSS_PRINT_PORT_USB_NMEA) { // Open
b.liu978f5432024-07-01 18:04:18 +0800147 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 +0800148 return GNSS_ERR_OPEN_DEV;
149 }
150 LOGD("Open USB NMEA port success.");
151 } else { // Close
152 if(gnss_usb_nmea_port_fd > 0) {
153 close(gnss_usb_nmea_port_fd);
154 gnss_usb_nmea_port_fd = -1;
155 }
156 LOGD("Close USB NMEA port success.");
b.liu8f231a12024-05-31 17:55:06 +0800157 }
b.liu8f231a12024-05-31 17:55:06 +0800158 }
159
b.liuced8dd02024-06-28 13:28:29 +0800160 // Uart AT change.
161 if((gnss_info.print_port & GNSS_PRINT_PORT_UART1) != (print_port & GNSS_PRINT_PORT_UART1)) {
162 if(print_port & GNSS_PRINT_PORT_UART1) { // Open
163 if((gnss_uart_at_port_fd = gnss_port_open(GNSS_PORT_UART_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) {
164 return GNSS_ERR_OPEN_DEV;
165 }
166 LOGD("Open UART AT port success.");
167 } else { // Close
168 if(gnss_uart_at_port_fd > 0) {
169 close(gnss_uart_at_port_fd);
170 gnss_uart_at_port_fd = -1;
171 }
172 LOGD("Close UART AT port success.");
b.liu8f231a12024-05-31 17:55:06 +0800173 }
b.liu8f231a12024-05-31 17:55:06 +0800174 }
175
b.liuced8dd02024-06-28 13:28:29 +0800176 gnss_info.print_port = print_port;
177
b.liudbc3f4b2024-06-25 18:22:24 +0800178 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800179}
180
181static int gnss_ports_close()
182{
183 if(gnss_usb_at_port_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800184 tcflush(gnss_usb_at_port_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800185 close(gnss_usb_at_port_fd);
186 gnss_usb_at_port_fd = -1;
187 }
188
189 if(gnss_usb_nmea_port_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800190 tcflush(gnss_usb_nmea_port_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800191 close(gnss_usb_nmea_port_fd);
192 gnss_usb_nmea_port_fd = -1;
193 }
194
195 if(gnss_uart_at_port_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800196 tcflush(gnss_uart_at_port_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800197 close(gnss_uart_at_port_fd);
198 gnss_uart_at_port_fd = -1;
199 }
200
201 if(gnss_pty_master_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800202 tcflush(gnss_pty_master_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800203 close(gnss_pty_master_fd);
204 gnss_pty_master_fd = -1;
205 }
206
207 if(gnss_pty_slave_fd > 0) {
b.liu978f5432024-07-01 18:04:18 +0800208 tcflush(gnss_pty_slave_fd, TCIOFLUSH);
b.liu8f231a12024-05-31 17:55:06 +0800209 close(gnss_pty_slave_fd);
210 gnss_pty_slave_fd = -1;
211 unlink(GNSS_PORT_PTY);
212 }
213
b.liuced8dd02024-06-28 13:28:29 +0800214 gnss_info.print_port = 0;
215
b.liu8f231a12024-05-31 17:55:06 +0800216 return 0;
217}
218
b.liu8f231a12024-05-31 17:55:06 +0800219#ifdef GNSS_DEBUG
b.liu99c645d2024-06-20 10:52:15 +0800220static void log_save(int fd, const char *data, int data_len)
221{
b.liu8f231a12024-05-31 17:55:06 +0800222 if(nmea_log_enable){
b.liu99c645d2024-06-20 10:52:15 +0800223 if(0 /* debug_fd_len > GNSS_FILE_LOG_MAX */) {
224 LOGD("Reopen file:%s(len = %d)", GNSS_FILE_LOG, debug_fd_len);
225 close(debug_fd);
226 debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
227 if(debug_fd < 0) {
228 LOGE("Open debug fd fail.");
229 }
230 debug_fd_len = 0;
231
232 LOGD("Reopen file:%s", GNSS_NMEA_FILE_LOG);
b.liu8f231a12024-05-31 17:55:06 +0800233 close(nmea_log_fd);
234 nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
235 if(nmea_log_fd < 0) {
236 LOGE("Open debug fd fail.");
237 }
b.liu8f231a12024-05-31 17:55:06 +0800238 }
239
b.liu99c645d2024-06-20 10:52:15 +0800240 if(fd == nmea_log_fd) {
241 if(nmea_log_fd > 0) {
242 write(nmea_log_fd, data, data_len);
243 debug_fd_len += data_len;
244 }
245 } else if(fd == debug_fd) {
246 if(debug_fd > 0) {
247 write(debug_fd, data, data_len);
248 debug_fd_len += data_len;
249 }
b.liu8f231a12024-05-31 17:55:06 +0800250 }
251 }
b.liu99c645d2024-06-20 10:52:15 +0800252}
b.liu8f231a12024-05-31 17:55:06 +0800253#endif
254
b.liue77ac3a2024-07-17 17:36:57 +0800255static void ind_location_print(const char *data)
256{
257 int index = 0;
258 int buff_len = 0;
259 char buff[GNSS_BUFF_SIZE] = {0};
260 buff[0] = MBTK_IND_START_FLAG;
261 buff_len++;
262 memcpy(buff + 1, MBTK_IND_LOCATION_TAG, strlen(MBTK_IND_LOCATION_TAG));
263 buff_len += strlen(MBTK_IND_LOCATION_TAG);
264 memcpy(buff + strlen(buff), data, strlen(data));
265 buff_len += strlen(data);
266 buff[strlen(buff)] = MBTK_IND_END_FLAG;
267 buff_len++;
268
269 while(index < GNSS_CLI_IND_MAX) {
270 if(ind_info[index].cli_fd > 0 && (ind_info[index].ind_flag & MBTK_GNSS_IND_LOCATION)) {
271 gnss_write(ind_info[index].cli_fd, buff, buff_len);
272 }
273 index++;
274 }
275}
276
277static void ind_nmea_print(const char *data)
278{
279 int index = 0;
280 int buff_len = 0;
281 char buff[GNSS_BUFF_SIZE] = {0};
282 buff[0] = MBTK_IND_START_FLAG;
283 buff_len++;
284 memcpy(buff + 1, MBTK_IND_NMEA_TAG, strlen(MBTK_IND_NMEA_TAG));
285 buff_len += strlen(MBTK_IND_NMEA_TAG);
286 memcpy(buff + strlen(buff), data, strlen(data));
287 buff_len += strlen(data);
288 buff[strlen(buff)] = MBTK_IND_END_FLAG;
289 buff_len++;
290 while(index < GNSS_CLI_IND_MAX) {
291 if(ind_info[index].cli_fd > 0 && (ind_info[index].ind_flag & MBTK_GNSS_IND_NMEA)) {
292 gnss_write(ind_info[index].cli_fd, buff, buff_len);
293 }
294 index++;
295 }
296}
297
b.liu99c645d2024-06-20 10:52:15 +0800298static void nmea_print(const char *nmea, int nmea_len)
299{
b.liu8f231a12024-05-31 17:55:06 +0800300 if(gnss_usb_at_port_fd > 0) {
301 write(gnss_usb_at_port_fd, nmea, nmea_len);
302 }
303
304 if(gnss_usb_nmea_port_fd > 0) {
305 write(gnss_usb_nmea_port_fd, nmea, nmea_len);
306 }
307
308 if(gnss_uart_at_port_fd > 0) {
309 write(gnss_uart_at_port_fd, nmea, nmea_len);
310 }
311
312 if(gnss_pty_master_fd > 0) {
313 write(gnss_pty_master_fd, nmea, nmea_len);
314 }
b.liue77ac3a2024-07-17 17:36:57 +0800315
316 ind_nmea_print(nmea);
b.liu8f231a12024-05-31 17:55:06 +0800317}
318
319static unsigned char nmea_checksum(const char *nmea)
320{
321 const char *p = nmea;
322 unsigned char chs = 0;
323
324 while (*p == '$') // skip '$'
325 p++;
326 while (*p != '*' && *p != 0)
327 chs ^= *p++;
328
329 return chs;
330}
331
332static bool nmea_check(const char *nmea, int nmea_len)
333{
334 char **ptr = gnss_filter_info;
335 while(*ptr) {
336 if(strstr(nmea, *ptr)) {
337 break;
338 }
339 ptr++;
340 }
341
342 if(*ptr == NULL) {
343 LOGD("Unknown NMEA[%d]:%s", nmea_len, nmea);
344 return FALSE;
345 }
346
347 char *checksum_str = strstr(nmea, "*");
348 checksum_str++; // Jump '*'
349 char checksum_buf[3] = {0};
350 snprintf(checksum_buf, 3, "%02x", nmea_checksum(nmea));
351 if(strncasecmp(checksum_buf, checksum_str, 2)) {
352 LOGD("Checksum error[%d](checksum - %s):%s", nmea_len, checksum_buf, nmea);
353 return FALSE;
354 }
355
356 return TRUE;
357}
358
359static void gnss_nmea_process(const char *data, int data_len)
360{
b.liu99c645d2024-06-20 10:52:15 +0800361 // LOGD("gnss_nmea_process() : data_len - %d", data_len);
362#if 0
b.liu8f231a12024-05-31 17:55:06 +0800363 char nmea[GNSS_BUFF_SIZE] = {0};
364 memcpy(nmea, data, data_len);
b.liu99c645d2024-06-20 10:52:15 +0800365#else
366 const char *nmea = data;
367#endif
b.liu8f231a12024-05-31 17:55:06 +0800368
369 if(!nmea_check(nmea, data_len)) {
b.liu99c645d2024-06-20 10:52:15 +0800370 LOGD("NO-NMEA:%s", nmea);
371#if GNSS_DEBUG
372 log_save(nmea_log_fd, "/**/", 4);
373 log_save(nmea_log_fd, nmea, data_len);
374#endif
375 if(gnss_info.gnss_set_cb)
376 gnss_info.gnss_set_cb(nmea, data_len);
b.liu8f231a12024-05-31 17:55:06 +0800377 return;
378 }
379
380#ifdef GNSS_DEBUG
381 if(nmea_log_enable) {
382 LOGD("NMEA[%d]:%s", data_len, nmea);
383 }
b.liu99c645d2024-06-20 10:52:15 +0800384
385 log_save(nmea_log_fd, nmea, data_len);
b.liu8f231a12024-05-31 17:55:06 +0800386#endif
387
388 nmea_print(nmea, data_len);
389}
390
391#if 0
392static void gnss_cmd_rsp_process(const char *data, int data_len)
393{
394 char rsp[GNSS_BUFF_SIZE] = {0};
395 memcpy(rsp, data, data_len);
396 LOGD("RSP[%d]:%s", data_len, rsp);
397}
398#endif
399
400static bool nmea_char_check(char ch)
401{
402 if(isalnum(ch) || ch == '$' || ch == '\r' || ch == '\n' || ch == '.'
b.liu99c645d2024-06-20 10:52:15 +0800403 || ch == ',' || ch == '*' || ch == '\0' || ch == '/' || ch == '_' || ch == '=')
b.liu8f231a12024-05-31 17:55:06 +0800404 return TRUE;
405
406 return FALSE;
407}
408
409static void gnss_data_process(const char *data, int data_len)
410{
411 if(gnss_info.state == GNSS_STATE_OPEN) {
412 LOGD("GNSS_OPEN[%d]:%s", data_len, data);
413 } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) {
414 // LOGD("GNSS_DL[%d]:%s", data_len, data);
b.liu99c645d2024-06-20 10:52:15 +0800415 if(gnss_info.gnss_dl_read_cb) {
416 gnss_info.gnss_dl_read_cb(data, data_len);
417 }
b.liu8f231a12024-05-31 17:55:06 +0800418 } else if(gnss_info.state == GNSS_STATE_READY) {
419 int index = 0;
420 while(index < data_len) {
421 if(nmea_found) {
422 if(!nmea_char_check(data[index])) {
b.liud0ba7152024-06-19 14:47:21 +0800423 // Copy nmea_buff to data_buff
424 // Start with '$', but not nmea data, so copy to data_buff.
425 memcpy(data_buff + data_buff_len, nmea_buff, nmea_buff_len);
426 data_buff_len += nmea_buff_len;
427 data_buff[data_buff_len++] = data[index];
428
429 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800430 nmea_found = FALSE;
b.liu8f231a12024-05-31 17:55:06 +0800431 continue;
432 }
433
434 if(data[index] != '\0') {
b.liud0ba7152024-06-19 14:47:21 +0800435 nmea_buff[nmea_buff_len++] = data[index];
436 if(nmea_buff[nmea_buff_len - 1] == '\n') {
437 if(data_buff_len > 0) {
b.liu99c645d2024-06-20 10:52:15 +0800438#if GNSS_DEBUG
439 log_save(nmea_log_fd, "/**/", 4);
440 log_save(nmea_log_fd, data_buff, data_buff_len);
441#endif
442 if(gnss_info.gnss_set_cb) {
443 gnss_info.gnss_set_cb(data_buff, data_buff_len);
444 }
b.liud0ba7152024-06-19 14:47:21 +0800445 data_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800446 }
447
b.liud0ba7152024-06-19 14:47:21 +0800448 if(nmea_buff_len > 6 && nmea_buff[nmea_buff_len - 5] == '*') { // $XXX*YY\r\n
b.liu99c645d2024-06-20 10:52:15 +0800449 nmea_buff[nmea_buff_len] = '\0';
b.liud0ba7152024-06-19 14:47:21 +0800450 gnss_nmea_process(nmea_buff, nmea_buff_len);
b.liu99c645d2024-06-20 10:52:15 +0800451 } else if(nmea_buff_len > 0) {
452 nmea_buff[nmea_buff_len] = '\0';
453 LOGD("NO-NMEA:%s", nmea_buff);
454#if GNSS_DEBUG
455 log_save(nmea_log_fd, "/**/", 4);
456 log_save(nmea_log_fd, nmea_buff, nmea_buff_len);
457#endif
b.liud0ba7152024-06-19 14:47:21 +0800458 }
459
460 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800461 nmea_found = FALSE;
462 }
463 }
464 } else {
465 if(data[index] == '$') {
b.liud0ba7152024-06-19 14:47:21 +0800466 nmea_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800467 nmea_found = TRUE;
b.liud0ba7152024-06-19 14:47:21 +0800468 nmea_buff[nmea_buff_len++] = data[index];
469 } else {
470 data_buff[data_buff_len++] = data[index];
b.liu8f231a12024-05-31 17:55:06 +0800471 }
472 }
473 index++;
474 }
475 } else {
476 LOGW("Unknown state : %d", gnss_info.state);
477 }
478}
479
480void* gnss_read_pthread(void* arg)
481{
482 LOGD("gnss_read_pthread enter.");
483 char buffer[GNSS_BUFF_SIZE];
484 int len = 0;
485 int ret = 0;
486 fd_set fdr, fdw;
487 int fd_max = 0;
488
b.liu8f231a12024-05-31 17:55:06 +0800489 fd_max = (gnss_info.fd > fd_max) ? gnss_info.fd : fd_max;
b.liu8f231a12024-05-31 17:55:06 +0800490 fd_max = (gnss_info.exit_fd[0] > fd_max) ? gnss_info.exit_fd[0] : fd_max;
b.liud0ba7152024-06-19 14:47:21 +0800491 memset(nmea_buff, 0, sizeof(nmea_buff));
492 memset(data_buff, 0, sizeof(data_buff));
493 nmea_buff_len = 0;
494 data_buff_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800495#if GNSS_DEBUG
b.liu8f231a12024-05-31 17:55:06 +0800496 if(nmea_log_enable) {
497 debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
498 if(debug_fd < 0) {
499 LOGE("Open debug fd fail.");
500 }
501 nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
502 if(nmea_log_fd < 0) {
503 LOGE("Open nmea fd fail.");
504 }
b.liu99c645d2024-06-20 10:52:15 +0800505 debug_fd_len = 0;
b.liu8f231a12024-05-31 17:55:06 +0800506 }
507#endif
508
b.liudbc3f4b2024-06-25 18:22:24 +0800509 LOGD("uart_fd - %d, exit_fd - %d", gnss_info.fd, gnss_info.exit_fd[0]);
510
b.liu8f231a12024-05-31 17:55:06 +0800511 while(gnss_info.state >= GNSS_STATE_OPEN) {
wangyouqiang55d36bf2024-06-27 09:35:52 +0800512 FD_ZERO(&fdw);
513 FD_ZERO(&fdr);
514 FD_SET(gnss_info.fd, &fdr);
515 FD_SET(gnss_info.exit_fd[0], &fdr);
b.liu8f231a12024-05-31 17:55:06 +0800516 ret = select(fd_max + 1, &fdr, &fdw, 0, NULL);
517 //LOGD("select - %d", ret);
b.liuece0db02024-06-25 18:39:09 +0800518 if(gnss_info.state < GNSS_STATE_OPEN) {
519 LOGD("State = %d, ret = %d", gnss_info.state, ret);
520 if(ret > 0) {
521 if (FD_ISSET(gnss_info.fd, &fdr)) {
522 LOGD("gnss_fd can read.");
523 } else if (FD_ISSET(gnss_info.exit_fd[0], &fdr)) {
524 LOGD("exit_fd can read.");
525 } else {
526 LOGW("Unknown select event.");
527 }
528 }
529 break;
530 }
531
b.liu8f231a12024-05-31 17:55:06 +0800532 if (ret < 0)
533 {
534 if (errno == EINTR)
535 {
536 continue;
537 }
538 LOGE("select error, errno = %d (%s)", errno, strerror(errno));
539 break;
540 }
541 else if (ret == 0)
542 {
543 LOGE("select ret == 0");
544 break;
545 }
546
547 if (FD_ISSET(gnss_info.fd, &fdr))
548 {
549 memset(buffer, 0, GNSS_BUFF_SIZE);
b.liu99c645d2024-06-20 10:52:15 +0800550 len = read(gnss_info.fd, buffer, GNSS_BUFF_SIZE - 1);
b.liu8f231a12024-05-31 17:55:06 +0800551 if(len > 0) {
552 //log_hex("READ", buffer, len);
553
554#if GNSS_DEBUG
b.liu99c645d2024-06-20 10:52:15 +0800555 //LOGD("read data_len = %d", len);
556 log_save(debug_fd, buffer, len);
b.liu8f231a12024-05-31 17:55:06 +0800557#endif
558
559 gnss_data_process(buffer, len);
560
561 } else if(len ==0 ){
562 LOGE("Read end : len = 0");
563 break;
564 } else {
565 if(EAGAIN == errno) {
566 usleep(50000);
567 continue;
568 } else {
569 LOGD("Read ret = -1 ,errno = %d", errno);
570 break;
571 }
572 }
573 }
574 else if (FD_ISSET(gnss_info.exit_fd[0], &fdr))
575 {
b.liuece0db02024-06-25 18:39:09 +0800576 LOGD("exit_fd select event.");
b.liu8f231a12024-05-31 17:55:06 +0800577 memset(buffer, 0, GNSS_BUFF_SIZE);
b.liudbc3f4b2024-06-25 18:22:24 +0800578 len = read(gnss_info.exit_fd[0], buffer, GNSS_BUFF_SIZE);
b.liu8f231a12024-05-31 17:55:06 +0800579 if(len > 0) {
580 if(strcmp(buffer, "exit") == 0) {
581 LOGD("Get thread exit message.");
582 break;
583 }
584 }
585 }
586 else
587 {
588 LOGW("Unknown select event.");
589 continue;
590 }
591 }
592
593#if GNSS_DEBUG
594 if(debug_fd > 0) {
595 close(debug_fd);
596 debug_fd = -1;
597 }
598 if(nmea_log_fd > 0) {
599 close(nmea_log_fd);
600 nmea_log_fd = -1;
601 }
602#endif
603
604 gnss_info.state = GNSS_STATE_CLOSE;
605 LOGD("gnss_read_pthread exit.");
606 return NULL;
607}
608
609#if 0
610int gnss_write(int fd, const void *data, int data_len)
611{
612 int count = 0;
613 int len = 0;
614 while(1)
615 {
616 len = write(fd, data + count, data_len - count);
617 if (len > 0)
618 {
619 count += len;
620 }
621 else
622 {
623 LOGE("write() fail,ret = %d,errno = %d", len, errno);
624 break;
625 }
626
627 if (count == data_len)
628 break;
629 }
630
631 return count;
632}
633#else
b.liu778645e2024-06-21 16:47:42 +0800634int gnss_write(int fd, const void* buf, int buf_len)
b.liu8f231a12024-05-31 17:55:06 +0800635{
b.liu778645e2024-06-21 16:47:42 +0800636 ssize_t size;
637 ssize_t size_to_wr;
b.liu8f231a12024-05-31 17:55:06 +0800638 ssize_t size_written;
639 if(GNSS_BUFF_SIZE < buf_len)
640 {
641 return -1;
642 }
643 for(size = 0; size < buf_len;)
644 {
645 size_to_wr = buf_len - size;
646 if( size_to_wr > GNSS_BUFF_SIZE)
647 size_to_wr = GNSS_BUFF_SIZE;
648
b.liu778645e2024-06-21 16:47:42 +0800649 size_written = write(fd, (const uint8*)buf + size, size_to_wr);
b.liu8f231a12024-05-31 17:55:06 +0800650 if (size_written==-1)
651 {
652 return -1;
653 }
654 size += size_written;
655 if(size_written != size_to_wr)
656 {
657 return size;
658 }
659 }
660 // LOGD("SEND %d / %d", size, buf_len);
661 return size;
662}
663#endif
664
665int gnss_init(uint32 print_port)
666{
667 if(gnss_info.state != GNSS_STATE_CLOSE) {
668 LOGW("GNSS not close:%d", gnss_info.state);
b.liuced8dd02024-06-28 13:28:29 +0800669 if(gnss_info.state == GNSS_STATE_READY) {
670 LOGD("Reset print port : %d -> %d", gnss_info.print_port, print_port);
671 if(gnss_info.print_port != print_port) {
672 return gnss_ports_open(print_port);
673 } else {
674 return GNSS_ERR_OK;
675 }
676 } else {
677 return GNSS_ERR_OK;
678 }
b.liu8f231a12024-05-31 17:55:06 +0800679 }
680
681 int ret = 0;
b.liu99c645d2024-06-20 10:52:15 +0800682 if(gnss_info.dl_befor_open) {
683 //if(gnss_info.auto_dl_fw) {
684 gnss_info.state = GNSS_STATE_DOWNLOAD;
b.liudbc3f4b2024-06-25 18:22:24 +0800685 ret = gnss_info.gnss_fw_dl(gnss_info.fd, NULL, gnss_info.dev_name);
b.liu99c645d2024-06-20 10:52:15 +0800686 if(ret) {
687 LOGE("gnss_fw_dl() fail : %d", ret);
688 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800689 return GNSS_ERR_DL_FW;
b.liu99c645d2024-06-20 10:52:15 +0800690 }
b.liu8f231a12024-05-31 17:55:06 +0800691
b.liu99c645d2024-06-20 10:52:15 +0800692 gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name);
693 if(gnss_info.fd <= 0) {
694 LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd);
695 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800696 return GNSS_ERR_OPEN_DEV;
b.liu99c645d2024-06-20 10:52:15 +0800697 }
698 if(pipe(gnss_info.exit_fd)) {
699 LOGE("pipe() fail[%d].", errno);
b.liudbc3f4b2024-06-25 18:22:24 +0800700 return GNSS_ERR_UNKNOWN;
b.liu99c645d2024-06-20 10:52:15 +0800701 }
702 // GNSS is opened.
703 gnss_info.state = GNSS_STATE_OPEN;
b.liu8f231a12024-05-31 17:55:06 +0800704
b.liu99c645d2024-06-20 10:52:15 +0800705#if 0
706 // Start gnss read thread.
707 pthread_attr_t thread_attr;
708 pthread_attr_init(&thread_attr);
709 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
710 {
711 LOGE("pthread_attr_setdetachstate() fail.");
712 goto main_exit;
713 }
b.liu8f231a12024-05-31 17:55:06 +0800714
b.liu99c645d2024-06-20 10:52:15 +0800715 if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL))
b.liu8f231a12024-05-31 17:55:06 +0800716#else
b.liu99c645d2024-06-20 10:52:15 +0800717 if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL))
b.liu8f231a12024-05-31 17:55:06 +0800718#endif
b.liu99c645d2024-06-20 10:52:15 +0800719 {
720 LOGE("pthread_create() fail.");
721 goto exit_with_close;
722 }
b.liu8f231a12024-05-31 17:55:06 +0800723
b.liu99c645d2024-06-20 10:52:15 +0800724 ret = gnss_info.gnss_dev_open();
725 if(ret) {
726 LOGE("gnss_dev_open() fail : %d", ret);
727 goto exit_with_thread_exit;
728 }
729 //}
730 } else {
731 gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name);
732 if(gnss_info.fd <= 0) {
733 LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd);
734 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800735 return GNSS_ERR_OPEN_DEV;
b.liu99c645d2024-06-20 10:52:15 +0800736 }
737 if(pipe(gnss_info.exit_fd)) {
738 LOGE("pipe() fail[%d].", errno);
b.liudbc3f4b2024-06-25 18:22:24 +0800739 return GNSS_ERR_UNKNOWN;
b.liu99c645d2024-06-20 10:52:15 +0800740 }
741 // GNSS is opened.
742 gnss_info.state = GNSS_STATE_OPEN;
b.liu8f231a12024-05-31 17:55:06 +0800743
b.liu99c645d2024-06-20 10:52:15 +0800744#if 0
745 // Start gnss read thread.
746 pthread_attr_t thread_attr;
747 pthread_attr_init(&thread_attr);
748 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
749 {
750 LOGE("pthread_attr_setdetachstate() fail.");
751 goto main_exit;
752 }
753
754 if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL))
755#else
756 if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL))
757#endif
758 {
759 LOGE("pthread_create() fail.");
760 goto exit_with_close;
761 }
762
763 ret = gnss_info.gnss_dev_open();
b.liu8f231a12024-05-31 17:55:06 +0800764 if(ret) {
b.liu99c645d2024-06-20 10:52:15 +0800765 LOGE("gnss_dev_open() fail : %d", ret);
766 goto exit_with_thread_exit;
767 }
b.liu8f231a12024-05-31 17:55:06 +0800768 }
769
770 // GNSS is ready, NMEA can print from uart.
771 gnss_info.state = GNSS_STATE_READY;
b.liu8f231a12024-05-31 17:55:06 +0800772
773 LOGD("GNSS open success.");
774
b.liuced8dd02024-06-28 13:28:29 +0800775 return gnss_ports_open(print_port);
b.liu8f231a12024-05-31 17:55:06 +0800776
b.liu8f231a12024-05-31 17:55:06 +0800777exit_with_thread_exit:
778 gnss_info.state = GNSS_STATE_CLOSING;
779 // Wait for read thread exit.
780 ret = pthread_join(gnss_info.read_pid, NULL);
781 if(ret){
782 LOGE("pthrad_join fail(%d)",ret);
783 }
784exit_with_close:
785 if(gnss_info.gnss_close(gnss_info.fd)) {
786 LOGE("gnss_close() fail.");
787 }
788 if(gnss_info.exit_fd[0] > 0) {
789 close(gnss_info.exit_fd[0]);
790 gnss_info.exit_fd[0] = -1;
791 }
792 if(gnss_info.exit_fd[1] > 0) {
793 close(gnss_info.exit_fd[1]);
794 gnss_info.exit_fd[1] = -1;
795 }
796 gnss_info.state = GNSS_STATE_CLOSE;
b.liudbc3f4b2024-06-25 18:22:24 +0800797 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800798}
799
800int gnss_deinit()
801{
802 if(gnss_info.state == GNSS_STATE_CLOSE) {
803 LOGW("GNSS is closed.");
b.liudbc3f4b2024-06-25 18:22:24 +0800804 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800805 } else if(gnss_info.state == GNSS_STATE_CLOSING) {
806 LOGW("GNSS is closing...");
b.liudbc3f4b2024-06-25 18:22:24 +0800807 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800808 } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) {
809 LOGW("GNSS is downloading...");
b.liudbc3f4b2024-06-25 18:22:24 +0800810 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800811 }
812
b.liu978f5432024-07-01 18:04:18 +0800813 if(gnss_info.gnss_dev_close(gnss_info.fd)) {
814 LOGE("gnss_dev_close() fail.");
815 return GNSS_ERR_UNKNOWN;
816 }
817
b.liu8f231a12024-05-31 17:55:06 +0800818 // Wait for read thread exit.
819 if(gnss_info.exit_fd[1] > 0) {
820 write(gnss_info.exit_fd[1], "exit", 4);
821 }
822
823 gnss_info.state = GNSS_STATE_CLOSING;
824 int ret = pthread_join(gnss_info.read_pid, NULL);
825 if(ret){
826 LOGE("pthrad_join fail(%d)",ret);
b.liudbc3f4b2024-06-25 18:22:24 +0800827 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800828 }
829
830 if(gnss_info.gnss_close(gnss_info.fd)) {
831 LOGE("gnss_close() fail.");
b.liudbc3f4b2024-06-25 18:22:24 +0800832 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800833 }
834
b.liu8f231a12024-05-31 17:55:06 +0800835 if(gnss_ports_close()) {
836 LOGE("gnss_ports_close fail.");
b.liudbc3f4b2024-06-25 18:22:24 +0800837 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800838 }
839
b.liud0ba7152024-06-19 14:47:21 +0800840 LOGD("gnss_ports_close() complete.");
841
b.liu8f231a12024-05-31 17:55:06 +0800842 gnss_info.fd = -1;
843 if(gnss_info.exit_fd[0] > 0) {
844 close(gnss_info.exit_fd[0]);
845 gnss_info.exit_fd[0] = -1;
846 }
847 if(gnss_info.exit_fd[1] > 0) {
848 close(gnss_info.exit_fd[1]);
849 gnss_info.exit_fd[1] = -1;
850 }
851 gnss_info.state = GNSS_STATE_CLOSE;
852 LOGD("GNSS close success.");
b.liudbc3f4b2024-06-25 18:22:24 +0800853 return GNSS_ERR_OK;
b.liu8f231a12024-05-31 17:55:06 +0800854}
855
856int gnss_set(const void* buf, unsigned int buf_len, void *cmd_rsp, int cmd_rsp_len)
857{
b.liud0ba7152024-06-19 14:47:21 +0800858 if(buf && buf_len > 0) {
859 if(cmd_rsp && cmd_rsp_len > 0) {
860 memset(cmd_rsp, 0, cmd_rsp_len);
861 }
b.liu8f231a12024-05-31 17:55:06 +0800862 return gnss_info.gnss_set(gnss_info.fd, buf, cmd_rsp, cmd_rsp_len);
863 } else {
b.liudbc3f4b2024-06-25 18:22:24 +0800864 return GNSS_ERR_UNKNOWN;
865 }
866}
867
868int gnss_dl_fw(const char* fw_name, void *rsp, int rsp_len)
869{
870 if(gnss_info.gnss_id != GNSS_TYPE_8122) {
871 return GNSS_ERR_UNSUPPORT;
872 }
873
874 if(rsp && rsp_len > 0) {
875 memset(rsp, 0, rsp_len);
876 }
877
878 if(gnss_info.gnss_fw_dl) {
879 if(GNSS_ERR_OK != gnss_deinit()) {
880 LOGE("Close gnss fail.");
881 return GNSS_ERR_UNKNOWN;
882 } else {
883 LOGD("Start gnss fw dl.");
884 return gnss_info.gnss_fw_dl(gnss_info.fd, fw_name, gnss_info.dev_name);
885 }
886 } else {
887 return GNSS_ERR_UNKNOWN;
b.liu8f231a12024-05-31 17:55:06 +0800888 }
889}
890
b.liue77ac3a2024-07-17 17:36:57 +0800891int gnss_ind_set(int fd, int ind_type)
892{
893 int index = 0;
894 if(ind_type) { // Add IND flag.
895 while(index < GNSS_CLI_IND_MAX) {
896 if(ind_info[index].cli_fd == fd)
897 break;
898 index++;
899 }
900
901 if(index == GNSS_CLI_IND_MAX) { // Add flag
902 index = 0;
903 while(index < GNSS_CLI_IND_MAX) {
904 if(ind_info[index].cli_fd <= 0)
905 break;
906 index++;
907 }
908 ind_info[index].cli_fd = fd;
909 ind_info[index].ind_flag = (uint32)ind_type;
910 } else { // Change flag
911 ind_info[index].cli_fd = fd;
912 ind_info[index].ind_flag = (uint32)ind_type;
913 }
914 } else { // Clear IND flag.
915 while(index < GNSS_CLI_IND_MAX) {
916 if(ind_info[index].cli_fd == fd)
917 break;
918 index++;
919 }
920
921 if(index == GNSS_CLI_IND_MAX) {
922 return GNSS_ERR_ARG;
923 }
924
925 ind_info[index].cli_fd = 0;
926 ind_info[index].ind_flag = 0;
927 }
928
929 return GNSS_ERR_OK;
930}
931
b.liu8f231a12024-05-31 17:55:06 +0800932static void sig_process(int sig)
933{
934 LOGI("I got signal %d\n", sig);
935 if(gnss_deinit()) {
936 LOGE("gnss_deinit() fail, no exist...");
937 return;
938 }
939
940 switch(sig)
941 {
942 case SIGINT: // Ctrl + C
943 {
944 LOGI("Exit by SIGINT.\n");
945 exit(0);
946 }
947 case SIGQUIT: // Ctrl + \ (ÀàËÆ SIGINT £¬µ«Òª²úÉúcoreÎļþ)
948 {
949 LOGI("Exit by SIGQUIT.\n");
950 exit(0);
951 }
952 case SIGTERM:// ĬÈÏkill (ͬ SIGKILL £¬µ« SIGKILL ²»¿É²¶»ñ)
953 {
954 LOGI("Exit by SIGTERM.\n");
955 exit(0);
956 }
957 case SIGTSTP:// Ctrl + Z (ͬ SIGSTOP £¬µ« SIGSTOP ²»¿É²¶»ñ)
958 {
959 LOGI("Exit by SIGTSTP.\n");
960 exit(0);
961 }
962 case SIGSEGV: // Èç¿ÕÖ¸Õë
963 {
964 LOGI("Exit by SIGSEGV.\n");
965 exit(0);
966 }
967 default:
968 {
969 LOGI("Unknown sig:%d\n",sig);
970 break;
971 }
972 }
973}
974
975
b.liuced8dd02024-06-28 13:28:29 +0800976// mbtk_gnssd 6228 /dev/ttyS2 baud 0/1 <port_type>
b.liu8f231a12024-05-31 17:55:06 +0800977int main(int argc, char *argv[])
978{
979 mbtk_log_init("radio", GNSS_TAG);
980
b.liubb590492024-06-13 16:42:08 +0800981#ifdef MBTK_DUMP_SUPPORT
982 mbtk_debug_open(NULL, TRUE);
983#endif
984
b.liu8f231a12024-05-31 17:55:06 +0800985 signal(SIGINT, sig_process);
986 signal(SIGQUIT, sig_process);
987 signal(SIGTERM, sig_process);
988
989 if(arg_check(argc, argv)) {
990 return -1;
991 }
992
993#ifdef GNSS_DEBUG
994 char buff[10];
995 memset(buff, 0, 10);
996 property_get(MBTK_PROP_GNSS_LOG, buff, "");
997 if(strlen(buff) > 0 && atoi(buff) > 0) {
998 nmea_log_enable = TRUE;
999 }
1000#endif
1001
1002 memset(&gnss_info, 0, sizeof(gnss_info_t));
1003 memcpy(gnss_info.dev_name, argv[2], strlen(argv[2]));
1004 gnss_info.state = GNSS_STATE_CLOSE;
1005 if(!strcmp(argv[1], GNSS_ID_6228)) {
1006 gnss_info.gnss_id = GNSS_TYPE_6228;
1007 gnss_info.auto_open = (bool)atoi(argv[3]);
1008 gnss_info.auto_dl_fw = TRUE;
b.liu99c645d2024-06-20 10:52:15 +08001009 gnss_info.dl_befor_open = FALSE;
b.liu8f231a12024-05-31 17:55:06 +08001010 gnss_info.gnss_dev_open = gnss_6228_dev_open;
1011 gnss_info.gnss_dev_close = gnss_6228_dev_close;
1012 gnss_info.gnss_open = gnss_6228_open;
1013 gnss_info.gnss_close = gnss_6228_close;
1014 gnss_info.gnss_fw_dl = gnss_6228_fw_dl;
1015 gnss_info.gnss_dl_read_cb = gnss_6228_dl_read_cb;
1016 gnss_info.gnss_set = gnss_6228_set;
1017 gnss_info.gnss_set_cb = gnss_6228_set_cb;
b.liuf9fbfa12024-06-14 15:53:59 +08001018 } else if(!strcmp(argv[1], GNSS_ID_8122)) {
1019 gnss_info.gnss_id = GNSS_TYPE_8122;
1020 gnss_info.auto_open = (bool)atoi(argv[3]);
1021 gnss_info.auto_dl_fw = FALSE;
b.liu99c645d2024-06-20 10:52:15 +08001022 gnss_info.dl_befor_open = FALSE;
b.liuf9fbfa12024-06-14 15:53:59 +08001023 gnss_info.gnss_dev_open = gnss_8122_dev_open;
1024 gnss_info.gnss_dev_close = gnss_8122_dev_close;
1025 gnss_info.gnss_open = gnss_8122_open;
1026 gnss_info.gnss_close = gnss_8122_close;
1027 gnss_info.gnss_fw_dl = gnss_8122_fw_dl;
1028 gnss_info.gnss_dl_read_cb = NULL;
1029 gnss_info.gnss_set = gnss_8122_set;
b.liu5f950c52024-06-15 20:13:12 +08001030 gnss_info.gnss_set_cb = gnss_8122_set_cb;
b.liu99c645d2024-06-20 10:52:15 +08001031 } else if(!strcmp(argv[1], GNSS_ID_5311)) {
1032 gnss_info.gnss_id = GNSS_TYPE_5311;
1033 gnss_info.auto_open = (bool)atoi(argv[3]);
1034 gnss_info.auto_dl_fw = TRUE;
1035 gnss_info.dl_befor_open = TRUE;
1036 gnss_info.gnss_dev_open = gnss_5311_dev_open;
1037 gnss_info.gnss_dev_close = gnss_5311_dev_close;
1038 gnss_info.gnss_open = gnss_5311_open;
1039 gnss_info.gnss_close = gnss_5311_close;
1040 gnss_info.gnss_fw_dl = gnss_5311_fw_dl;
1041 gnss_info.gnss_dl_read_cb = NULL;
1042 gnss_info.gnss_set = gnss_5311_set;
1043 gnss_info.gnss_set_cb = gnss_5311_set_cb;
b.liu8f231a12024-05-31 17:55:06 +08001044 } else {
1045 LOGE("No support : %s", argv[1]);
1046 return -1;
1047 }
1048
1049 LOGD("GNSS : %s, Device: %s", argv[1], gnss_info.dev_name);
1050 // Auto open gnss.
1051 if(gnss_info.auto_open) {
b.liuced8dd02024-06-28 13:28:29 +08001052 int init_mode = atoi(argv[3]);
1053 if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) != init_mode) {
1054 init_mode = 0;
b.liu8f231a12024-05-31 17:55:06 +08001055 }
b.liuced8dd02024-06-28 13:28:29 +08001056 if(gnss_init((uint32)init_mode)) {
1057 LOGE("gnss_init() fail.");
1058 // return -1;
1059 }
1060 } else {
1061 gnss_info.print_port = 0;
b.liu8f231a12024-05-31 17:55:06 +08001062 }
1063
1064 // Init ubus and waitting IPC commands.
b.liud0ba7152024-06-19 14:47:21 +08001065#ifdef MBTK_GNSS_UBUS_ENABLE
b.liu8f231a12024-05-31 17:55:06 +08001066 if(gnss_ubus_init()) {
1067 LOGD("main() run...");
1068 uloop_run();
1069 } else {
1070 LOGE("gnss_ubus_init() fail.");
1071 }
b.liu5f950c52024-06-15 20:13:12 +08001072#else
1073 if(!gnss_ipc_service_start()) {
1074 LOGD("main() run...");
1075 while(1) {
1076 sleep(24 * 60 * 60);
1077 }
1078 } else {
1079 LOGE("gnss_ipc_service_start() fail.");
1080 }
1081#endif
b.liu8f231a12024-05-31 17:55:06 +08001082
1083 LOGD("main() exit.");
1084 return 0;
1085}