b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1 | #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.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 11 | #include <ctype.h> |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 12 | #include <termios.h> |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 13 | |
| 14 | #include "mbtk_type.h" |
| 15 | #include "mbtk_log.h" |
| 16 | #include "gnss_info.h" |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 17 | #include "gnss_utils.h" |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 18 | #include "gnss_6228.h" |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 19 | #include "gnss_hd8122.h" |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 20 | #include "gnss_asr5311.h" |
b.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 21 | #include "gnss_n50db.h" |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 22 | |
| 23 | #define GNSS_DEBUG 1 |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 24 | #define GNSS_UBUS_ENABLE 1 |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 25 | |
| 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.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 33 | #define GNSS_CLI_IND_MAX 10 |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 34 | |
| 35 | #ifdef GNSS_DEBUG |
| 36 | #define GNSS_NMEA_FILE_LOG "/tmp/mbtk_gnss_nmea.log" |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 37 | #define GNSS_FILE_LOG "/tmp/mbtk_gnss.log" |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 38 | #define GNSS_FILE_LOG_MAX 104857600 // 100MB |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 39 | #endif |
| 40 | |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame] | 41 | gnss_info_t gnss_info; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 42 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 43 | #ifdef MBTK_GNSS_UBUS_ENABLE |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 44 | struct ubus_context *gnss_ubus_init(void); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 45 | #else |
| 46 | int gnss_ipc_service_start(); |
| 47 | #endif |
| 48 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 49 | int gnss_init_config(int fd); |
| 50 | |
wangyouqiang | fa897f8 | 2024-06-27 09:44:55 +0800 | [diff] [blame] | 51 | static char nmea_buff[GNSS_BUFF_SIZE*4] = {0}; |
| 52 | static char data_buff[GNSS_BUFF_SIZE*4] = {0}; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 53 | static uint32 nmea_buff_len = 0; |
| 54 | static uint32 data_buff_len = 0; |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 55 | static gnss_ind_info_t ind_info[GNSS_CLI_IND_MAX]; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 56 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 57 | static bool nmea_found = FALSE; |
| 58 | #ifdef GNSS_DEBUG |
| 59 | static bool nmea_log_enable = FALSE; |
| 60 | static int nmea_log_fd = -1; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 61 | static int debug_fd = -1; |
| 62 | static int debug_fd_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 63 | #endif |
| 64 | static int gnss_pty_master_fd = -1; |
| 65 | static int gnss_pty_slave_fd = -1; |
| 66 | static int gnss_usb_at_port_fd = -1; |
| 67 | static int gnss_usb_nmea_port_fd = -1; |
| 68 | static int gnss_uart_at_port_fd = -1; |
| 69 | static char *gnss_filter_info[] = {"RMC", "VTG", "GGA", "GSA", "GSV", "GLL", "ZDA", "GST", "TXT", "DHV", "DTM", NULL}; |
| 70 | |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 71 | int gnss_write(int fd, const void* buf, int buf_len); |
| 72 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 73 | static void help() |
| 74 | { |
b.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 75 | LOGD("mbtk_gnssd <6228/8122/5311/N50DB> <gnss_dev> <0/1>"); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static int arg_check(int argc, char *argv[]) |
| 79 | { |
| 80 | if(argc != 4) { |
b.liu | 4ae4118 | 2024-06-28 16:30:15 +0800 | [diff] [blame] | 81 | LOGE("argc = %d", argc); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 82 | goto check_fail; |
| 83 | } |
| 84 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 85 | // Only support 6228/8122/5311. |
b.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 86 | 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.liu | 4ae4118 | 2024-06-28 16:30:15 +0800 | [diff] [blame] | 88 | LOGE("argv[1] = %s", argv[1]); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 89 | goto check_fail; |
| 90 | } |
| 91 | |
| 92 | if(access(argv[2], R_OK | W_OK)) { |
b.liu | 4ae4118 | 2024-06-28 16:30:15 +0800 | [diff] [blame] | 93 | LOGE("access(%s) rw fail. ", argv[2]); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 94 | goto check_fail; |
| 95 | } |
| 96 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 97 | #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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 100 | goto check_fail; |
| 101 | } |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 102 | #endif |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | check_fail: |
| 106 | help(); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | static int gnss_ports_open(uint32 print_port) |
| 111 | { |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 112 | // 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 126 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 127 | } |
| 128 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 129 | // 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.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 132 | if((gnss_usb_at_port_fd = gnss_port_open(GNSS_PORT_USB_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) { |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 133 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 142 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 143 | } |
| 144 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 145 | // 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.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 148 | if((gnss_usb_nmea_port_fd = gnss_port_open(GNSS_PORT_USB_NMEA, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) { |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 149 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 158 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 159 | } |
| 160 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 161 | // 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 174 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 175 | } |
| 176 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 177 | gnss_info.print_port = print_port; |
| 178 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 179 | return GNSS_ERR_OK; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static int gnss_ports_close() |
| 183 | { |
| 184 | if(gnss_usb_at_port_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 185 | tcflush(gnss_usb_at_port_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 186 | close(gnss_usb_at_port_fd); |
| 187 | gnss_usb_at_port_fd = -1; |
| 188 | } |
| 189 | |
| 190 | if(gnss_usb_nmea_port_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 191 | tcflush(gnss_usb_nmea_port_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 192 | close(gnss_usb_nmea_port_fd); |
| 193 | gnss_usb_nmea_port_fd = -1; |
| 194 | } |
| 195 | |
| 196 | if(gnss_uart_at_port_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 197 | tcflush(gnss_uart_at_port_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 198 | close(gnss_uart_at_port_fd); |
| 199 | gnss_uart_at_port_fd = -1; |
| 200 | } |
| 201 | |
| 202 | if(gnss_pty_master_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 203 | tcflush(gnss_pty_master_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 204 | close(gnss_pty_master_fd); |
| 205 | gnss_pty_master_fd = -1; |
| 206 | } |
| 207 | |
| 208 | if(gnss_pty_slave_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 209 | tcflush(gnss_pty_slave_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 210 | close(gnss_pty_slave_fd); |
| 211 | gnss_pty_slave_fd = -1; |
| 212 | unlink(GNSS_PORT_PTY); |
| 213 | } |
| 214 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 215 | gnss_info.print_port = 0; |
| 216 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 220 | #ifdef GNSS_DEBUG |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 221 | static void log_save(int fd, const char *data, int data_len) |
| 222 | { |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 223 | if(nmea_log_enable){ |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 224 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 234 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 239 | } |
| 240 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 241 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 251 | } |
| 252 | } |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 253 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 254 | #endif |
| 255 | |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 256 | static 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 | |
| 278 | static 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.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 299 | static void nmea_print(const char *nmea, int nmea_len) |
| 300 | { |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame] | 301 | int ret = -1; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 302 | if(gnss_usb_at_port_fd > 0) { |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame] | 303 | ret = write(gnss_usb_at_port_fd, nmea, nmea_len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | if(gnss_usb_nmea_port_fd > 0) { |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame] | 307 | ret = write(gnss_usb_nmea_port_fd, nmea, nmea_len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | if(gnss_uart_at_port_fd > 0) { |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame] | 311 | ret = write(gnss_uart_at_port_fd, nmea, nmea_len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if(gnss_pty_master_fd > 0) { |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame] | 315 | ret = write(gnss_pty_master_fd, nmea, nmea_len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 316 | } |
yq.wang | 937355d | 2024-07-26 01:24:42 -0700 | [diff] [blame^] | 317 | ind_nmea_print(nmea); |
| 318 | ind_location_print(nmea); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static 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 | |
| 334 | static 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 | |
| 361 | static void gnss_nmea_process(const char *data, int data_len) |
| 362 | { |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 363 | // LOGD("gnss_nmea_process() : data_len - %d", data_len); |
| 364 | #if 0 |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 365 | char nmea[GNSS_BUFF_SIZE] = {0}; |
| 366 | memcpy(nmea, data, data_len); |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 367 | #else |
| 368 | const char *nmea = data; |
| 369 | #endif |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 370 | |
| 371 | if(!nmea_check(nmea, data_len)) { |
b.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 372 | // No print "$HOSTSLEEP". |
| 373 | if(memcmp(nmea, "$HOSTSLEEP", strlen("$HOSTSLEEP"))) { |
| 374 | LOGD("NO-NMEA:%s", nmea); |
| 375 | } |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 376 | #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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 382 | return; |
| 383 | } |
| 384 | |
| 385 | #ifdef GNSS_DEBUG |
| 386 | if(nmea_log_enable) { |
| 387 | LOGD("NMEA[%d]:%s", data_len, nmea); |
| 388 | } |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 389 | |
| 390 | log_save(nmea_log_fd, nmea, data_len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 391 | #endif |
| 392 | |
| 393 | nmea_print(nmea, data_len); |
| 394 | } |
| 395 | |
| 396 | #if 0 |
| 397 | static 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 | |
| 405 | static bool nmea_char_check(char ch) |
| 406 | { |
| 407 | if(isalnum(ch) || ch == '$' || ch == '\r' || ch == '\n' || ch == '.' |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame] | 408 | || ch == ',' || ch == '*' || ch == '\0' || ch == '/' || ch == '_' || ch == '=' || ch == '-') |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 409 | return TRUE; |
| 410 | |
| 411 | return FALSE; |
| 412 | } |
| 413 | |
| 414 | static 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.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 420 | if(gnss_info.gnss_dl_read_cb) { |
| 421 | gnss_info.gnss_dl_read_cb(data, data_len); |
| 422 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 423 | } 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.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 428 | // 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 435 | nmea_found = FALSE; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 436 | continue; |
| 437 | } |
| 438 | |
| 439 | if(data[index] != '\0') { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 440 | nmea_buff[nmea_buff_len++] = data[index]; |
| 441 | if(nmea_buff[nmea_buff_len - 1] == '\n') { |
| 442 | if(data_buff_len > 0) { |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 443 | #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.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 450 | data_buff_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 451 | } |
| 452 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 453 | if(nmea_buff_len > 6 && nmea_buff[nmea_buff_len - 5] == '*') { // $XXX*YY\r\n |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 454 | nmea_buff[nmea_buff_len] = '\0'; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 455 | gnss_nmea_process(nmea_buff, nmea_buff_len); |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 456 | } else if(nmea_buff_len > 0) { |
| 457 | nmea_buff[nmea_buff_len] = '\0'; |
b.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 458 | if(memcmp(nmea_buff, "$HOSTSLEEP", strlen("$HOSTSLEEP"))) { |
| 459 | LOGD("NO-NMEA:%s", nmea_buff); |
| 460 | } |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 461 | #if GNSS_DEBUG |
| 462 | log_save(nmea_log_fd, "/**/", 4); |
| 463 | log_save(nmea_log_fd, nmea_buff, nmea_buff_len); |
| 464 | #endif |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | nmea_buff_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 468 | nmea_found = FALSE; |
| 469 | } |
| 470 | } |
| 471 | } else { |
| 472 | if(data[index] == '$') { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 473 | nmea_buff_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 474 | nmea_found = TRUE; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 475 | nmea_buff[nmea_buff_len++] = data[index]; |
| 476 | } else { |
| 477 | data_buff[data_buff_len++] = data[index]; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | index++; |
| 481 | } |
| 482 | } else { |
| 483 | LOGW("Unknown state : %d", gnss_info.state); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | void* 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 496 | fd_max = (gnss_info.fd > fd_max) ? gnss_info.fd : fd_max; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 497 | fd_max = (gnss_info.exit_fd[0] > fd_max) ? gnss_info.exit_fd[0] : fd_max; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 498 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 502 | #if GNSS_DEBUG |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 503 | 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.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 512 | debug_fd_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 513 | } |
| 514 | #endif |
| 515 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 516 | LOGD("uart_fd - %d, exit_fd - %d", gnss_info.fd, gnss_info.exit_fd[0]); |
| 517 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 518 | while(gnss_info.state >= GNSS_STATE_OPEN) { |
wangyouqiang | 55d36bf | 2024-06-27 09:35:52 +0800 | [diff] [blame] | 519 | FD_ZERO(&fdw); |
| 520 | FD_ZERO(&fdr); |
| 521 | FD_SET(gnss_info.fd, &fdr); |
| 522 | FD_SET(gnss_info.exit_fd[0], &fdr); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 523 | ret = select(fd_max + 1, &fdr, &fdw, 0, NULL); |
| 524 | //LOGD("select - %d", ret); |
b.liu | ece0db0 | 2024-06-25 18:39:09 +0800 | [diff] [blame] | 525 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 539 | 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.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 557 | len = read(gnss_info.fd, buffer, GNSS_BUFF_SIZE - 1); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 558 | if(len > 0) { |
| 559 | //log_hex("READ", buffer, len); |
| 560 | |
| 561 | #if GNSS_DEBUG |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 562 | //LOGD("read data_len = %d", len); |
| 563 | log_save(debug_fd, buffer, len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 564 | #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.liu | ece0db0 | 2024-06-25 18:39:09 +0800 | [diff] [blame] | 583 | LOGD("exit_fd select event."); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 584 | memset(buffer, 0, GNSS_BUFF_SIZE); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 585 | len = read(gnss_info.exit_fd[0], buffer, GNSS_BUFF_SIZE); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 586 | 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 |
| 617 | int 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.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 641 | int gnss_write(int fd, const void* buf, int buf_len) |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 642 | { |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 643 | ssize_t size; |
| 644 | ssize_t size_to_wr; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 645 | 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.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 656 | size_written = write(fd, (const uint8*)buf + size, size_to_wr); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 657 | 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 | |
| 672 | int gnss_init(uint32 print_port) |
| 673 | { |
| 674 | if(gnss_info.state != GNSS_STATE_CLOSE) { |
| 675 | LOGW("GNSS not close:%d", gnss_info.state); |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 676 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | int ret = 0; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 689 | if(gnss_info.dl_befor_open) { |
| 690 | //if(gnss_info.auto_dl_fw) { |
| 691 | gnss_info.state = GNSS_STATE_DOWNLOAD; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 692 | ret = gnss_info.gnss_fw_dl(gnss_info.fd, NULL, gnss_info.dev_name); |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 693 | if(ret) { |
| 694 | LOGE("gnss_fw_dl() fail : %d", ret); |
| 695 | gnss_info.state = GNSS_STATE_CLOSE; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 696 | return GNSS_ERR_DL_FW; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 697 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 698 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 699 | 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.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 703 | return GNSS_ERR_OPEN_DEV; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 704 | } |
| 705 | if(pipe(gnss_info.exit_fd)) { |
| 706 | LOGE("pipe() fail[%d].", errno); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 707 | return GNSS_ERR_UNKNOWN; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 708 | } |
| 709 | // GNSS is opened. |
| 710 | gnss_info.state = GNSS_STATE_OPEN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 711 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 712 | #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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 721 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 722 | if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL)) |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 723 | #else |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 724 | if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL)) |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 725 | #endif |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 726 | { |
| 727 | LOGE("pthread_create() fail."); |
| 728 | goto exit_with_close; |
| 729 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 730 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 731 | 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.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 742 | return GNSS_ERR_OPEN_DEV; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 743 | } |
| 744 | if(pipe(gnss_info.exit_fd)) { |
| 745 | LOGE("pipe() fail[%d].", errno); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 746 | return GNSS_ERR_UNKNOWN; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 747 | } |
| 748 | // GNSS is opened. |
| 749 | gnss_info.state = GNSS_STATE_OPEN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 750 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 751 | #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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 771 | if(ret) { |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 772 | LOGE("gnss_dev_open() fail : %d", ret); |
| 773 | goto exit_with_thread_exit; |
| 774 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | // GNSS is ready, NMEA can print from uart. |
| 778 | gnss_info.state = GNSS_STATE_READY; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 779 | |
| 780 | LOGD("GNSS open success."); |
| 781 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 782 | return gnss_ports_open(print_port); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 783 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 784 | exit_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 | } |
| 791 | exit_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.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 804 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | int gnss_deinit() |
| 808 | { |
| 809 | if(gnss_info.state == GNSS_STATE_CLOSE) { |
| 810 | LOGW("GNSS is closed."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 811 | return GNSS_ERR_OK; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 812 | } else if(gnss_info.state == GNSS_STATE_CLOSING) { |
| 813 | LOGW("GNSS is closing..."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 814 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 815 | } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) { |
| 816 | LOGW("GNSS is downloading..."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 817 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 818 | } |
| 819 | |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 820 | if(gnss_info.gnss_dev_close(gnss_info.fd)) { |
| 821 | LOGE("gnss_dev_close() fail."); |
| 822 | return GNSS_ERR_UNKNOWN; |
| 823 | } |
| 824 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 825 | // 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.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 834 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | if(gnss_info.gnss_close(gnss_info.fd)) { |
| 838 | LOGE("gnss_close() fail."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 839 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 840 | } |
| 841 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 842 | if(gnss_ports_close()) { |
| 843 | LOGE("gnss_ports_close fail."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 844 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 845 | } |
| 846 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 847 | LOGD("gnss_ports_close() complete."); |
| 848 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 849 | 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.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 860 | return GNSS_ERR_OK; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | int gnss_set(const void* buf, unsigned int buf_len, void *cmd_rsp, int cmd_rsp_len) |
| 864 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 865 | if(buf && buf_len > 0) { |
| 866 | if(cmd_rsp && cmd_rsp_len > 0) { |
| 867 | memset(cmd_rsp, 0, cmd_rsp_len); |
| 868 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 869 | return gnss_info.gnss_set(gnss_info.fd, buf, cmd_rsp, cmd_rsp_len); |
| 870 | } else { |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 871 | return GNSS_ERR_UNKNOWN; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | int gnss_dl_fw(const char* fw_name, void *rsp, int rsp_len) |
| 876 | { |
b.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 877 | // Only 8122 support download fw. |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 878 | 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.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 895 | return GNSS_ERR_UNSUPPORT; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 899 | int 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.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame] | 916 | if(index == GNSS_CLI_IND_MAX) |
| 917 | { |
| 918 | LOGE("ind flag is full."); |
| 919 | return GNSS_ERR_CLI_FULL; |
| 920 | } |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 921 | ind_info[index].cli_fd = fd; |
| 922 | ind_info[index].ind_flag = (uint32)ind_type; |
yq.wang | 937355d | 2024-07-26 01:24:42 -0700 | [diff] [blame^] | 923 | } else { // Change flag |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 924 | 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.wang | 937355d | 2024-07-26 01:24:42 -0700 | [diff] [blame^] | 936 | } |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 937 | ind_info[index].cli_fd = 0; |
| 938 | ind_info[index].ind_flag = 0; |
| 939 | } |
| 940 | |
| 941 | return GNSS_ERR_OK; |
| 942 | } |
| 943 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 944 | static 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.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 988 | // mbtk_gnssd 6228 /dev/ttyS2 baud 0/1 <port_type> |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 989 | int main(int argc, char *argv[]) |
| 990 | { |
| 991 | mbtk_log_init("radio", GNSS_TAG); |
| 992 | |
b.liu | bb59049 | 2024-06-13 16:42:08 +0800 | [diff] [blame] | 993 | #ifdef MBTK_DUMP_SUPPORT |
| 994 | mbtk_debug_open(NULL, TRUE); |
| 995 | #endif |
| 996 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 997 | 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.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 1021 | gnss_info.dl_befor_open = FALSE; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1022 | 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.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 1030 | } 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.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 1034 | gnss_info.dl_befor_open = FALSE; |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 1035 | 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.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 1042 | gnss_info.gnss_set_cb = gnss_8122_set_cb; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 1043 | } 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.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 1056 | } 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1069 | } 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.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 1077 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1080 | } |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 1081 | if(gnss_init((uint32)init_mode)) { |
| 1082 | LOGE("gnss_init() fail."); |
| 1083 | // return -1; |
| 1084 | } |
| 1085 | } else { |
| 1086 | gnss_info.print_port = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | // Init ubus and waitting IPC commands. |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 1090 | #ifdef MBTK_GNSS_UBUS_ENABLE |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1091 | if(gnss_ubus_init()) { |
| 1092 | LOGD("main() run..."); |
| 1093 | uloop_run(); |
| 1094 | } else { |
| 1095 | LOGE("gnss_ubus_init() fail."); |
| 1096 | } |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 1097 | #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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1107 | |
| 1108 | LOGD("main() exit."); |
| 1109 | return 0; |
| 1110 | } |