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" |
| 21 | |
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 | |
| 41 | gnss_info_t gnss_info; |
| 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 | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 75 | LOGD("mbtk_gnssd <6228/8122/5311> <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. |
| 86 | if(strcmp(argv[1], GNSS_ID_6228) && strcmp(argv[1], GNSS_ID_8122) && strcmp(argv[1], GNSS_ID_5311)) { |
b.liu | 4ae4118 | 2024-06-28 16:30:15 +0800 | [diff] [blame] | 87 | LOGE("argv[1] = %s", argv[1]); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 88 | goto check_fail; |
| 89 | } |
| 90 | |
| 91 | if(access(argv[2], R_OK | W_OK)) { |
b.liu | 4ae4118 | 2024-06-28 16:30:15 +0800 | [diff] [blame] | 92 | LOGE("access(%s) rw fail. ", argv[2]); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 93 | goto check_fail; |
| 94 | } |
| 95 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 96 | #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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 99 | goto check_fail; |
| 100 | } |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 101 | #endif |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 102 | |
| 103 | return 0; |
| 104 | check_fail: |
| 105 | help(); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | static int gnss_ports_open(uint32 print_port) |
| 110 | { |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 111 | // 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 125 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 126 | } |
| 127 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 128 | // 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.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 131 | 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] | 132 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 141 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 142 | } |
| 143 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 144 | // 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.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 147 | 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] | 148 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 157 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 158 | } |
| 159 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 160 | // 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 173 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 174 | } |
| 175 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 176 | gnss_info.print_port = print_port; |
| 177 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 178 | return GNSS_ERR_OK; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static int gnss_ports_close() |
| 182 | { |
| 183 | if(gnss_usb_at_port_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 184 | tcflush(gnss_usb_at_port_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 185 | close(gnss_usb_at_port_fd); |
| 186 | gnss_usb_at_port_fd = -1; |
| 187 | } |
| 188 | |
| 189 | if(gnss_usb_nmea_port_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 190 | tcflush(gnss_usb_nmea_port_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 191 | close(gnss_usb_nmea_port_fd); |
| 192 | gnss_usb_nmea_port_fd = -1; |
| 193 | } |
| 194 | |
| 195 | if(gnss_uart_at_port_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 196 | tcflush(gnss_uart_at_port_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 197 | close(gnss_uart_at_port_fd); |
| 198 | gnss_uart_at_port_fd = -1; |
| 199 | } |
| 200 | |
| 201 | if(gnss_pty_master_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 202 | tcflush(gnss_pty_master_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 203 | close(gnss_pty_master_fd); |
| 204 | gnss_pty_master_fd = -1; |
| 205 | } |
| 206 | |
| 207 | if(gnss_pty_slave_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 208 | tcflush(gnss_pty_slave_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 209 | close(gnss_pty_slave_fd); |
| 210 | gnss_pty_slave_fd = -1; |
| 211 | unlink(GNSS_PORT_PTY); |
| 212 | } |
| 213 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 214 | gnss_info.print_port = 0; |
| 215 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 219 | #ifdef GNSS_DEBUG |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 220 | static void log_save(int fd, const char *data, int data_len) |
| 221 | { |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 222 | if(nmea_log_enable){ |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 223 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 233 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 238 | } |
| 239 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 240 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 250 | } |
| 251 | } |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 252 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 253 | #endif |
| 254 | |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame^] | 255 | static 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 | |
| 277 | static 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.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 298 | static void nmea_print(const char *nmea, int nmea_len) |
| 299 | { |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 300 | 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.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame^] | 315 | |
| 316 | ind_nmea_print(nmea); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static 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 | |
| 332 | static 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 | |
| 359 | static void gnss_nmea_process(const char *data, int data_len) |
| 360 | { |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 361 | // LOGD("gnss_nmea_process() : data_len - %d", data_len); |
| 362 | #if 0 |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 363 | char nmea[GNSS_BUFF_SIZE] = {0}; |
| 364 | memcpy(nmea, data, data_len); |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 365 | #else |
| 366 | const char *nmea = data; |
| 367 | #endif |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 368 | |
| 369 | if(!nmea_check(nmea, data_len)) { |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 370 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 377 | return; |
| 378 | } |
| 379 | |
| 380 | #ifdef GNSS_DEBUG |
| 381 | if(nmea_log_enable) { |
| 382 | LOGD("NMEA[%d]:%s", data_len, nmea); |
| 383 | } |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 384 | |
| 385 | log_save(nmea_log_fd, nmea, data_len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 386 | #endif |
| 387 | |
| 388 | nmea_print(nmea, data_len); |
| 389 | } |
| 390 | |
| 391 | #if 0 |
| 392 | static 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 | |
| 400 | static bool nmea_char_check(char ch) |
| 401 | { |
| 402 | if(isalnum(ch) || ch == '$' || ch == '\r' || ch == '\n' || ch == '.' |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 403 | || ch == ',' || ch == '*' || ch == '\0' || ch == '/' || ch == '_' || ch == '=') |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 404 | return TRUE; |
| 405 | |
| 406 | return FALSE; |
| 407 | } |
| 408 | |
| 409 | static 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.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 415 | if(gnss_info.gnss_dl_read_cb) { |
| 416 | gnss_info.gnss_dl_read_cb(data, data_len); |
| 417 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 418 | } 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.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 423 | // 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 430 | nmea_found = FALSE; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 431 | continue; |
| 432 | } |
| 433 | |
| 434 | if(data[index] != '\0') { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 435 | nmea_buff[nmea_buff_len++] = data[index]; |
| 436 | if(nmea_buff[nmea_buff_len - 1] == '\n') { |
| 437 | if(data_buff_len > 0) { |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 438 | #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.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 445 | data_buff_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 446 | } |
| 447 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 448 | 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] | 449 | nmea_buff[nmea_buff_len] = '\0'; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 450 | gnss_nmea_process(nmea_buff, nmea_buff_len); |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 451 | } 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.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | nmea_buff_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 461 | nmea_found = FALSE; |
| 462 | } |
| 463 | } |
| 464 | } else { |
| 465 | if(data[index] == '$') { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 466 | nmea_buff_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 467 | nmea_found = TRUE; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 468 | nmea_buff[nmea_buff_len++] = data[index]; |
| 469 | } else { |
| 470 | data_buff[data_buff_len++] = data[index]; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | index++; |
| 474 | } |
| 475 | } else { |
| 476 | LOGW("Unknown state : %d", gnss_info.state); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | void* 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 489 | fd_max = (gnss_info.fd > fd_max) ? gnss_info.fd : fd_max; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 490 | 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] | 491 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 495 | #if GNSS_DEBUG |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 496 | 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.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 505 | debug_fd_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 506 | } |
| 507 | #endif |
| 508 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 509 | LOGD("uart_fd - %d, exit_fd - %d", gnss_info.fd, gnss_info.exit_fd[0]); |
| 510 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 511 | while(gnss_info.state >= GNSS_STATE_OPEN) { |
wangyouqiang | 55d36bf | 2024-06-27 09:35:52 +0800 | [diff] [blame] | 512 | FD_ZERO(&fdw); |
| 513 | FD_ZERO(&fdr); |
| 514 | FD_SET(gnss_info.fd, &fdr); |
| 515 | FD_SET(gnss_info.exit_fd[0], &fdr); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 516 | ret = select(fd_max + 1, &fdr, &fdw, 0, NULL); |
| 517 | //LOGD("select - %d", ret); |
b.liu | ece0db0 | 2024-06-25 18:39:09 +0800 | [diff] [blame] | 518 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 532 | 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.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 550 | len = read(gnss_info.fd, buffer, GNSS_BUFF_SIZE - 1); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 551 | if(len > 0) { |
| 552 | //log_hex("READ", buffer, len); |
| 553 | |
| 554 | #if GNSS_DEBUG |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 555 | //LOGD("read data_len = %d", len); |
| 556 | log_save(debug_fd, buffer, len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 557 | #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.liu | ece0db0 | 2024-06-25 18:39:09 +0800 | [diff] [blame] | 576 | LOGD("exit_fd select event."); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 577 | memset(buffer, 0, GNSS_BUFF_SIZE); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 578 | len = read(gnss_info.exit_fd[0], buffer, GNSS_BUFF_SIZE); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 579 | 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 |
| 610 | int 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.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 634 | int gnss_write(int fd, const void* buf, int buf_len) |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 635 | { |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 636 | ssize_t size; |
| 637 | ssize_t size_to_wr; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 638 | 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.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 649 | size_written = write(fd, (const uint8*)buf + size, size_to_wr); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 650 | 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 | |
| 665 | int gnss_init(uint32 print_port) |
| 666 | { |
| 667 | if(gnss_info.state != GNSS_STATE_CLOSE) { |
| 668 | LOGW("GNSS not close:%d", gnss_info.state); |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 669 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | int ret = 0; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 682 | if(gnss_info.dl_befor_open) { |
| 683 | //if(gnss_info.auto_dl_fw) { |
| 684 | gnss_info.state = GNSS_STATE_DOWNLOAD; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 685 | 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] | 686 | if(ret) { |
| 687 | LOGE("gnss_fw_dl() fail : %d", ret); |
| 688 | gnss_info.state = GNSS_STATE_CLOSE; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 689 | return GNSS_ERR_DL_FW; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 690 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 691 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 692 | 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.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 696 | return GNSS_ERR_OPEN_DEV; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 697 | } |
| 698 | if(pipe(gnss_info.exit_fd)) { |
| 699 | LOGE("pipe() fail[%d].", errno); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 700 | return GNSS_ERR_UNKNOWN; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 701 | } |
| 702 | // GNSS is opened. |
| 703 | gnss_info.state = GNSS_STATE_OPEN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 704 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 705 | #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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 714 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 715 | 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] | 716 | #else |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 717 | if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL)) |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 718 | #endif |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 719 | { |
| 720 | LOGE("pthread_create() fail."); |
| 721 | goto exit_with_close; |
| 722 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 723 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 724 | 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.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 735 | return GNSS_ERR_OPEN_DEV; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 736 | } |
| 737 | if(pipe(gnss_info.exit_fd)) { |
| 738 | LOGE("pipe() fail[%d].", errno); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 739 | return GNSS_ERR_UNKNOWN; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 740 | } |
| 741 | // GNSS is opened. |
| 742 | gnss_info.state = GNSS_STATE_OPEN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 743 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 744 | #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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 764 | if(ret) { |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 765 | LOGE("gnss_dev_open() fail : %d", ret); |
| 766 | goto exit_with_thread_exit; |
| 767 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | // GNSS is ready, NMEA can print from uart. |
| 771 | gnss_info.state = GNSS_STATE_READY; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 772 | |
| 773 | LOGD("GNSS open success."); |
| 774 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 775 | return gnss_ports_open(print_port); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 776 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 777 | exit_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 | } |
| 784 | exit_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.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 797 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | int gnss_deinit() |
| 801 | { |
| 802 | if(gnss_info.state == GNSS_STATE_CLOSE) { |
| 803 | LOGW("GNSS is closed."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 804 | return GNSS_ERR_OK; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 805 | } else if(gnss_info.state == GNSS_STATE_CLOSING) { |
| 806 | LOGW("GNSS is closing..."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 807 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 808 | } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) { |
| 809 | LOGW("GNSS is downloading..."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 810 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 811 | } |
| 812 | |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 813 | if(gnss_info.gnss_dev_close(gnss_info.fd)) { |
| 814 | LOGE("gnss_dev_close() fail."); |
| 815 | return GNSS_ERR_UNKNOWN; |
| 816 | } |
| 817 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 818 | // 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.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 827 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if(gnss_info.gnss_close(gnss_info.fd)) { |
| 831 | LOGE("gnss_close() fail."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 832 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 833 | } |
| 834 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 835 | if(gnss_ports_close()) { |
| 836 | LOGE("gnss_ports_close fail."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 837 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 838 | } |
| 839 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 840 | LOGD("gnss_ports_close() complete."); |
| 841 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 842 | 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.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 853 | return GNSS_ERR_OK; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | int gnss_set(const void* buf, unsigned int buf_len, void *cmd_rsp, int cmd_rsp_len) |
| 857 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 858 | if(buf && buf_len > 0) { |
| 859 | if(cmd_rsp && cmd_rsp_len > 0) { |
| 860 | memset(cmd_rsp, 0, cmd_rsp_len); |
| 861 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 862 | return gnss_info.gnss_set(gnss_info.fd, buf, cmd_rsp, cmd_rsp_len); |
| 863 | } else { |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 864 | return GNSS_ERR_UNKNOWN; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | int 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame^] | 891 | int 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 932 | static 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.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 976 | // mbtk_gnssd 6228 /dev/ttyS2 baud 0/1 <port_type> |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 977 | int main(int argc, char *argv[]) |
| 978 | { |
| 979 | mbtk_log_init("radio", GNSS_TAG); |
| 980 | |
b.liu | bb59049 | 2024-06-13 16:42:08 +0800 | [diff] [blame] | 981 | #ifdef MBTK_DUMP_SUPPORT |
| 982 | mbtk_debug_open(NULL, TRUE); |
| 983 | #endif |
| 984 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 985 | 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.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 1009 | gnss_info.dl_befor_open = FALSE; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1010 | 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.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 1018 | } 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.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 1022 | gnss_info.dl_befor_open = FALSE; |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 1023 | 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.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 1030 | gnss_info.gnss_set_cb = gnss_8122_set_cb; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 1031 | } 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1044 | } 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.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 1052 | 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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1055 | } |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 1056 | if(gnss_init((uint32)init_mode)) { |
| 1057 | LOGE("gnss_init() fail."); |
| 1058 | // return -1; |
| 1059 | } |
| 1060 | } else { |
| 1061 | gnss_info.print_port = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | // Init ubus and waitting IPC commands. |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 1065 | #ifdef MBTK_GNSS_UBUS_ENABLE |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1066 | if(gnss_ubus_init()) { |
| 1067 | LOGD("main() run..."); |
| 1068 | uloop_run(); |
| 1069 | } else { |
| 1070 | LOGE("gnss_ubus_init() fail."); |
| 1071 | } |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 1072 | #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.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1082 | |
| 1083 | LOGD("main() exit."); |
| 1084 | return 0; |
| 1085 | } |