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]; |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame^] | 56 | static gnss_ind_type_num_t ind_num; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 57 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 58 | static bool nmea_found = FALSE; |
| 59 | #ifdef GNSS_DEBUG |
| 60 | static bool nmea_log_enable = FALSE; |
| 61 | static int nmea_log_fd = -1; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 62 | static int debug_fd = -1; |
| 63 | static int debug_fd_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 64 | #endif |
| 65 | static int gnss_pty_master_fd = -1; |
| 66 | static int gnss_pty_slave_fd = -1; |
| 67 | static int gnss_usb_at_port_fd = -1; |
| 68 | static int gnss_usb_nmea_port_fd = -1; |
| 69 | static int gnss_uart_at_port_fd = -1; |
| 70 | static char *gnss_filter_info[] = {"RMC", "VTG", "GGA", "GSA", "GSV", "GLL", "ZDA", "GST", "TXT", "DHV", "DTM", NULL}; |
| 71 | |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 72 | int gnss_write(int fd, const void* buf, int buf_len); |
| 73 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 74 | static void help() |
| 75 | { |
b.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 76 | LOGD("mbtk_gnssd <6228/8122/5311/N50DB> <gnss_dev> <0/1>"); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static int arg_check(int argc, char *argv[]) |
| 80 | { |
| 81 | if(argc != 4) { |
b.liu | 4ae4118 | 2024-06-28 16:30:15 +0800 | [diff] [blame] | 82 | LOGE("argc = %d", argc); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 83 | goto check_fail; |
| 84 | } |
| 85 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 86 | // Only support 6228/8122/5311. |
b.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 87 | if(strcmp(argv[1], GNSS_ID_6228) && strcmp(argv[1], GNSS_ID_8122) && strcmp(argv[1], GNSS_ID_5311) |
| 88 | && strcmp(argv[1], GNSS_ID_N50DB)) { |
b.liu | 4ae4118 | 2024-06-28 16:30:15 +0800 | [diff] [blame] | 89 | LOGE("argv[1] = %s", argv[1]); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 90 | goto check_fail; |
| 91 | } |
| 92 | |
| 93 | if(access(argv[2], R_OK | W_OK)) { |
b.liu | 4ae4118 | 2024-06-28 16:30:15 +0800 | [diff] [blame] | 94 | LOGE("access(%s) rw fail. ", argv[2]); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 95 | goto check_fail; |
| 96 | } |
| 97 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 98 | #if 0 |
| 99 | int init_mode = atoi(argv[3]); |
| 100 | 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] | 101 | goto check_fail; |
| 102 | } |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 103 | #endif |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 104 | |
| 105 | return 0; |
| 106 | check_fail: |
| 107 | help(); |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | static int gnss_ports_open(uint32 print_port) |
| 112 | { |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 113 | // TTY AT change. |
| 114 | if((gnss_info.print_port & GNSS_PRINT_PORT_TTY_AT) != (print_port & GNSS_PRINT_PORT_TTY_AT)) { |
| 115 | if(print_port & GNSS_PRINT_PORT_TTY_AT) { // Open |
| 116 | if(gnss_pty_open(&gnss_pty_master_fd, &gnss_pty_slave_fd, GNSS_PORT_PTY)) { |
| 117 | return GNSS_ERR_OPEN_DEV; |
| 118 | } |
| 119 | LOGD("Open PTY port success."); |
| 120 | } else { // Close |
| 121 | if(gnss_pty_slave_fd > 0) { |
| 122 | close(gnss_pty_slave_fd); |
| 123 | gnss_pty_slave_fd = -1; |
| 124 | unlink(GNSS_PORT_PTY); |
| 125 | } |
| 126 | LOGD("Close PTY port success."); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 127 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 128 | } |
| 129 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 130 | // USB AT change. |
| 131 | if((gnss_info.print_port & GNSS_PRINT_PORT_USB_AT) != (print_port & GNSS_PRINT_PORT_USB_AT)) { |
| 132 | if(print_port & GNSS_PRINT_PORT_USB_AT) { // Open |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 133 | 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] | 134 | return GNSS_ERR_OPEN_DEV; |
| 135 | } |
| 136 | LOGD("Open USB AT port success."); |
| 137 | } else { // Close |
| 138 | if(gnss_usb_at_port_fd > 0) { |
| 139 | close(gnss_usb_at_port_fd); |
| 140 | gnss_usb_at_port_fd = -1; |
| 141 | } |
| 142 | LOGD("Close USB AT port success."); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 143 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 144 | } |
| 145 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 146 | // USB NMEA change. |
| 147 | if((gnss_info.print_port & GNSS_PRINT_PORT_USB_NMEA) != (print_port & GNSS_PRINT_PORT_USB_NMEA)) { |
| 148 | if(print_port & GNSS_PRINT_PORT_USB_NMEA) { // Open |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 149 | 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] | 150 | return GNSS_ERR_OPEN_DEV; |
| 151 | } |
| 152 | LOGD("Open USB NMEA port success."); |
| 153 | } else { // Close |
| 154 | if(gnss_usb_nmea_port_fd > 0) { |
| 155 | close(gnss_usb_nmea_port_fd); |
| 156 | gnss_usb_nmea_port_fd = -1; |
| 157 | } |
| 158 | LOGD("Close USB NMEA port success."); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 159 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 160 | } |
| 161 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 162 | // Uart AT change. |
| 163 | if((gnss_info.print_port & GNSS_PRINT_PORT_UART1) != (print_port & GNSS_PRINT_PORT_UART1)) { |
| 164 | if(print_port & GNSS_PRINT_PORT_UART1) { // Open |
| 165 | if((gnss_uart_at_port_fd = gnss_port_open(GNSS_PORT_UART_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) { |
| 166 | return GNSS_ERR_OPEN_DEV; |
| 167 | } |
| 168 | LOGD("Open UART AT port success."); |
| 169 | } else { // Close |
| 170 | if(gnss_uart_at_port_fd > 0) { |
| 171 | close(gnss_uart_at_port_fd); |
| 172 | gnss_uart_at_port_fd = -1; |
| 173 | } |
| 174 | LOGD("Close UART AT port success."); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 175 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 176 | } |
| 177 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 178 | gnss_info.print_port = print_port; |
| 179 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 180 | return GNSS_ERR_OK; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static int gnss_ports_close() |
| 184 | { |
| 185 | if(gnss_usb_at_port_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 186 | tcflush(gnss_usb_at_port_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 187 | close(gnss_usb_at_port_fd); |
| 188 | gnss_usb_at_port_fd = -1; |
| 189 | } |
| 190 | |
| 191 | if(gnss_usb_nmea_port_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 192 | tcflush(gnss_usb_nmea_port_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 193 | close(gnss_usb_nmea_port_fd); |
| 194 | gnss_usb_nmea_port_fd = -1; |
| 195 | } |
| 196 | |
| 197 | if(gnss_uart_at_port_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 198 | tcflush(gnss_uart_at_port_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 199 | close(gnss_uart_at_port_fd); |
| 200 | gnss_uart_at_port_fd = -1; |
| 201 | } |
| 202 | |
| 203 | if(gnss_pty_master_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 204 | tcflush(gnss_pty_master_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 205 | close(gnss_pty_master_fd); |
| 206 | gnss_pty_master_fd = -1; |
| 207 | } |
| 208 | |
| 209 | if(gnss_pty_slave_fd > 0) { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 210 | tcflush(gnss_pty_slave_fd, TCIOFLUSH); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 211 | close(gnss_pty_slave_fd); |
| 212 | gnss_pty_slave_fd = -1; |
| 213 | unlink(GNSS_PORT_PTY); |
| 214 | } |
| 215 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 216 | gnss_info.print_port = 0; |
| 217 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 221 | #ifdef GNSS_DEBUG |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 222 | static void log_save(int fd, const char *data, int data_len) |
| 223 | { |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 224 | if(nmea_log_enable){ |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 225 | if(0 /* debug_fd_len > GNSS_FILE_LOG_MAX */) { |
| 226 | LOGD("Reopen file:%s(len = %d)", GNSS_FILE_LOG, debug_fd_len); |
| 227 | close(debug_fd); |
| 228 | debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 229 | if(debug_fd < 0) { |
| 230 | LOGE("Open debug fd fail."); |
| 231 | } |
| 232 | debug_fd_len = 0; |
| 233 | |
| 234 | LOGD("Reopen file:%s", GNSS_NMEA_FILE_LOG); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 235 | close(nmea_log_fd); |
| 236 | nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 237 | if(nmea_log_fd < 0) { |
| 238 | LOGE("Open debug fd fail."); |
| 239 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 240 | } |
| 241 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 242 | if(fd == nmea_log_fd) { |
| 243 | if(nmea_log_fd > 0) { |
| 244 | write(nmea_log_fd, data, data_len); |
| 245 | debug_fd_len += data_len; |
| 246 | } |
| 247 | } else if(fd == debug_fd) { |
| 248 | if(debug_fd > 0) { |
| 249 | write(debug_fd, data, data_len); |
| 250 | debug_fd_len += data_len; |
| 251 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 252 | } |
| 253 | } |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 254 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 255 | #endif |
| 256 | |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 257 | static void ind_location_print(const char *data) |
| 258 | { |
| 259 | int index = 0; |
| 260 | int buff_len = 0; |
| 261 | char buff[GNSS_BUFF_SIZE] = {0}; |
| 262 | buff[0] = MBTK_IND_START_FLAG; |
| 263 | buff_len++; |
| 264 | memcpy(buff + 1, MBTK_IND_LOCATION_TAG, strlen(MBTK_IND_LOCATION_TAG)); |
| 265 | buff_len += strlen(MBTK_IND_LOCATION_TAG); |
| 266 | memcpy(buff + strlen(buff), data, strlen(data)); |
| 267 | buff_len += strlen(data); |
| 268 | buff[strlen(buff)] = MBTK_IND_END_FLAG; |
| 269 | buff_len++; |
| 270 | |
| 271 | while(index < GNSS_CLI_IND_MAX) { |
| 272 | if(ind_info[index].cli_fd > 0 && (ind_info[index].ind_flag & MBTK_GNSS_IND_LOCATION)) { |
| 273 | gnss_write(ind_info[index].cli_fd, buff, buff_len); |
| 274 | } |
| 275 | index++; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | static void ind_nmea_print(const char *data) |
| 280 | { |
| 281 | int index = 0; |
| 282 | int buff_len = 0; |
| 283 | char buff[GNSS_BUFF_SIZE] = {0}; |
| 284 | buff[0] = MBTK_IND_START_FLAG; |
| 285 | buff_len++; |
| 286 | memcpy(buff + 1, MBTK_IND_NMEA_TAG, strlen(MBTK_IND_NMEA_TAG)); |
| 287 | buff_len += strlen(MBTK_IND_NMEA_TAG); |
| 288 | memcpy(buff + strlen(buff), data, strlen(data)); |
| 289 | buff_len += strlen(data); |
| 290 | buff[strlen(buff)] = MBTK_IND_END_FLAG; |
| 291 | buff_len++; |
| 292 | while(index < GNSS_CLI_IND_MAX) { |
| 293 | if(ind_info[index].cli_fd > 0 && (ind_info[index].ind_flag & MBTK_GNSS_IND_NMEA)) { |
| 294 | gnss_write(ind_info[index].cli_fd, buff, buff_len); |
| 295 | } |
| 296 | index++; |
| 297 | } |
| 298 | } |
| 299 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 300 | static void nmea_print(const char *nmea, int nmea_len) |
| 301 | { |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame^] | 302 | int ret = -1; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 303 | if(gnss_usb_at_port_fd > 0) { |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame^] | 304 | ret = write(gnss_usb_at_port_fd, nmea, nmea_len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | if(gnss_usb_nmea_port_fd > 0) { |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame^] | 308 | ret = write(gnss_usb_nmea_port_fd, nmea, nmea_len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | if(gnss_uart_at_port_fd > 0) { |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame^] | 312 | ret = write(gnss_uart_at_port_fd, nmea, nmea_len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | if(gnss_pty_master_fd > 0) { |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame^] | 316 | ret = write(gnss_pty_master_fd, nmea, nmea_len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 317 | } |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 318 | |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame^] | 319 | if(ind_num.nmea_cb_num > 0) |
| 320 | { |
| 321 | ind_nmea_print(nmea); |
| 322 | } |
| 323 | |
| 324 | if(ind_num.locl_cb_num > 0) |
| 325 | { |
| 326 | ind_location_print(nmea); |
| 327 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static unsigned char nmea_checksum(const char *nmea) |
| 331 | { |
| 332 | const char *p = nmea; |
| 333 | unsigned char chs = 0; |
| 334 | |
| 335 | while (*p == '$') // skip '$' |
| 336 | p++; |
| 337 | while (*p != '*' && *p != 0) |
| 338 | chs ^= *p++; |
| 339 | |
| 340 | return chs; |
| 341 | } |
| 342 | |
| 343 | static bool nmea_check(const char *nmea, int nmea_len) |
| 344 | { |
| 345 | char **ptr = gnss_filter_info; |
| 346 | while(*ptr) { |
| 347 | if(strstr(nmea, *ptr)) { |
| 348 | break; |
| 349 | } |
| 350 | ptr++; |
| 351 | } |
| 352 | |
| 353 | if(*ptr == NULL) { |
| 354 | LOGD("Unknown NMEA[%d]:%s", nmea_len, nmea); |
| 355 | return FALSE; |
| 356 | } |
| 357 | |
| 358 | char *checksum_str = strstr(nmea, "*"); |
| 359 | checksum_str++; // Jump '*' |
| 360 | char checksum_buf[3] = {0}; |
| 361 | snprintf(checksum_buf, 3, "%02x", nmea_checksum(nmea)); |
| 362 | if(strncasecmp(checksum_buf, checksum_str, 2)) { |
| 363 | LOGD("Checksum error[%d](checksum - %s):%s", nmea_len, checksum_buf, nmea); |
| 364 | return FALSE; |
| 365 | } |
| 366 | |
| 367 | return TRUE; |
| 368 | } |
| 369 | |
| 370 | static void gnss_nmea_process(const char *data, int data_len) |
| 371 | { |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 372 | // LOGD("gnss_nmea_process() : data_len - %d", data_len); |
| 373 | #if 0 |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 374 | char nmea[GNSS_BUFF_SIZE] = {0}; |
| 375 | memcpy(nmea, data, data_len); |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 376 | #else |
| 377 | const char *nmea = data; |
| 378 | #endif |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 379 | |
| 380 | if(!nmea_check(nmea, data_len)) { |
b.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 381 | // No print "$HOSTSLEEP". |
| 382 | if(memcmp(nmea, "$HOSTSLEEP", strlen("$HOSTSLEEP"))) { |
| 383 | LOGD("NO-NMEA:%s", nmea); |
| 384 | } |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 385 | #if GNSS_DEBUG |
| 386 | log_save(nmea_log_fd, "/**/", 4); |
| 387 | log_save(nmea_log_fd, nmea, data_len); |
| 388 | #endif |
| 389 | if(gnss_info.gnss_set_cb) |
| 390 | gnss_info.gnss_set_cb(nmea, data_len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 391 | return; |
| 392 | } |
| 393 | |
| 394 | #ifdef GNSS_DEBUG |
| 395 | if(nmea_log_enable) { |
| 396 | LOGD("NMEA[%d]:%s", data_len, nmea); |
| 397 | } |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 398 | |
| 399 | log_save(nmea_log_fd, nmea, data_len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 400 | #endif |
| 401 | |
| 402 | nmea_print(nmea, data_len); |
| 403 | } |
| 404 | |
| 405 | #if 0 |
| 406 | static void gnss_cmd_rsp_process(const char *data, int data_len) |
| 407 | { |
| 408 | char rsp[GNSS_BUFF_SIZE] = {0}; |
| 409 | memcpy(rsp, data, data_len); |
| 410 | LOGD("RSP[%d]:%s", data_len, rsp); |
| 411 | } |
| 412 | #endif |
| 413 | |
| 414 | static bool nmea_char_check(char ch) |
| 415 | { |
| 416 | if(isalnum(ch) || ch == '$' || ch == '\r' || ch == '\n' || ch == '.' |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame^] | 417 | || ch == ',' || ch == '*' || ch == '\0' || ch == '/' || ch == '_' || ch == '=' || ch == '-') |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 418 | return TRUE; |
| 419 | |
| 420 | return FALSE; |
| 421 | } |
| 422 | |
| 423 | static void gnss_data_process(const char *data, int data_len) |
| 424 | { |
| 425 | if(gnss_info.state == GNSS_STATE_OPEN) { |
| 426 | LOGD("GNSS_OPEN[%d]:%s", data_len, data); |
| 427 | } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) { |
| 428 | // LOGD("GNSS_DL[%d]:%s", data_len, data); |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 429 | if(gnss_info.gnss_dl_read_cb) { |
| 430 | gnss_info.gnss_dl_read_cb(data, data_len); |
| 431 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 432 | } else if(gnss_info.state == GNSS_STATE_READY) { |
| 433 | int index = 0; |
| 434 | while(index < data_len) { |
| 435 | if(nmea_found) { |
| 436 | if(!nmea_char_check(data[index])) { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 437 | // Copy nmea_buff to data_buff |
| 438 | // Start with '$', but not nmea data, so copy to data_buff. |
| 439 | memcpy(data_buff + data_buff_len, nmea_buff, nmea_buff_len); |
| 440 | data_buff_len += nmea_buff_len; |
| 441 | data_buff[data_buff_len++] = data[index]; |
| 442 | |
| 443 | nmea_buff_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 444 | nmea_found = FALSE; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 445 | continue; |
| 446 | } |
| 447 | |
| 448 | if(data[index] != '\0') { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 449 | nmea_buff[nmea_buff_len++] = data[index]; |
| 450 | if(nmea_buff[nmea_buff_len - 1] == '\n') { |
| 451 | if(data_buff_len > 0) { |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 452 | #if GNSS_DEBUG |
| 453 | log_save(nmea_log_fd, "/**/", 4); |
| 454 | log_save(nmea_log_fd, data_buff, data_buff_len); |
| 455 | #endif |
| 456 | if(gnss_info.gnss_set_cb) { |
| 457 | gnss_info.gnss_set_cb(data_buff, data_buff_len); |
| 458 | } |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 459 | data_buff_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 460 | } |
| 461 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 462 | 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] | 463 | nmea_buff[nmea_buff_len] = '\0'; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 464 | gnss_nmea_process(nmea_buff, nmea_buff_len); |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 465 | } else if(nmea_buff_len > 0) { |
| 466 | nmea_buff[nmea_buff_len] = '\0'; |
b.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 467 | if(memcmp(nmea_buff, "$HOSTSLEEP", strlen("$HOSTSLEEP"))) { |
| 468 | LOGD("NO-NMEA:%s", nmea_buff); |
| 469 | } |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 470 | #if GNSS_DEBUG |
| 471 | log_save(nmea_log_fd, "/**/", 4); |
| 472 | log_save(nmea_log_fd, nmea_buff, nmea_buff_len); |
| 473 | #endif |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | nmea_buff_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 477 | nmea_found = FALSE; |
| 478 | } |
| 479 | } |
| 480 | } else { |
| 481 | if(data[index] == '$') { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 482 | nmea_buff_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 483 | nmea_found = TRUE; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 484 | nmea_buff[nmea_buff_len++] = data[index]; |
| 485 | } else { |
| 486 | data_buff[data_buff_len++] = data[index]; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | index++; |
| 490 | } |
| 491 | } else { |
| 492 | LOGW("Unknown state : %d", gnss_info.state); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | void* gnss_read_pthread(void* arg) |
| 497 | { |
| 498 | LOGD("gnss_read_pthread enter."); |
| 499 | char buffer[GNSS_BUFF_SIZE]; |
| 500 | int len = 0; |
| 501 | int ret = 0; |
| 502 | fd_set fdr, fdw; |
| 503 | int fd_max = 0; |
| 504 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 505 | fd_max = (gnss_info.fd > fd_max) ? gnss_info.fd : fd_max; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 506 | 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] | 507 | memset(nmea_buff, 0, sizeof(nmea_buff)); |
| 508 | memset(data_buff, 0, sizeof(data_buff)); |
| 509 | nmea_buff_len = 0; |
| 510 | data_buff_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 511 | #if GNSS_DEBUG |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 512 | if(nmea_log_enable) { |
| 513 | debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 514 | if(debug_fd < 0) { |
| 515 | LOGE("Open debug fd fail."); |
| 516 | } |
| 517 | nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 518 | if(nmea_log_fd < 0) { |
| 519 | LOGE("Open nmea fd fail."); |
| 520 | } |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 521 | debug_fd_len = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 522 | } |
| 523 | #endif |
| 524 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 525 | LOGD("uart_fd - %d, exit_fd - %d", gnss_info.fd, gnss_info.exit_fd[0]); |
| 526 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 527 | while(gnss_info.state >= GNSS_STATE_OPEN) { |
wangyouqiang | 55d36bf | 2024-06-27 09:35:52 +0800 | [diff] [blame] | 528 | FD_ZERO(&fdw); |
| 529 | FD_ZERO(&fdr); |
| 530 | FD_SET(gnss_info.fd, &fdr); |
| 531 | FD_SET(gnss_info.exit_fd[0], &fdr); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 532 | ret = select(fd_max + 1, &fdr, &fdw, 0, NULL); |
| 533 | //LOGD("select - %d", ret); |
b.liu | ece0db0 | 2024-06-25 18:39:09 +0800 | [diff] [blame] | 534 | if(gnss_info.state < GNSS_STATE_OPEN) { |
| 535 | LOGD("State = %d, ret = %d", gnss_info.state, ret); |
| 536 | if(ret > 0) { |
| 537 | if (FD_ISSET(gnss_info.fd, &fdr)) { |
| 538 | LOGD("gnss_fd can read."); |
| 539 | } else if (FD_ISSET(gnss_info.exit_fd[0], &fdr)) { |
| 540 | LOGD("exit_fd can read."); |
| 541 | } else { |
| 542 | LOGW("Unknown select event."); |
| 543 | } |
| 544 | } |
| 545 | break; |
| 546 | } |
| 547 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 548 | if (ret < 0) |
| 549 | { |
| 550 | if (errno == EINTR) |
| 551 | { |
| 552 | continue; |
| 553 | } |
| 554 | LOGE("select error, errno = %d (%s)", errno, strerror(errno)); |
| 555 | break; |
| 556 | } |
| 557 | else if (ret == 0) |
| 558 | { |
| 559 | LOGE("select ret == 0"); |
| 560 | break; |
| 561 | } |
| 562 | |
| 563 | if (FD_ISSET(gnss_info.fd, &fdr)) |
| 564 | { |
| 565 | memset(buffer, 0, GNSS_BUFF_SIZE); |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 566 | len = read(gnss_info.fd, buffer, GNSS_BUFF_SIZE - 1); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 567 | if(len > 0) { |
| 568 | //log_hex("READ", buffer, len); |
| 569 | |
| 570 | #if GNSS_DEBUG |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 571 | //LOGD("read data_len = %d", len); |
| 572 | log_save(debug_fd, buffer, len); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 573 | #endif |
| 574 | |
| 575 | gnss_data_process(buffer, len); |
| 576 | |
| 577 | } else if(len ==0 ){ |
| 578 | LOGE("Read end : len = 0"); |
| 579 | break; |
| 580 | } else { |
| 581 | if(EAGAIN == errno) { |
| 582 | usleep(50000); |
| 583 | continue; |
| 584 | } else { |
| 585 | LOGD("Read ret = -1 ,errno = %d", errno); |
| 586 | break; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | else if (FD_ISSET(gnss_info.exit_fd[0], &fdr)) |
| 591 | { |
b.liu | ece0db0 | 2024-06-25 18:39:09 +0800 | [diff] [blame] | 592 | LOGD("exit_fd select event."); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 593 | memset(buffer, 0, GNSS_BUFF_SIZE); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 594 | len = read(gnss_info.exit_fd[0], buffer, GNSS_BUFF_SIZE); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 595 | if(len > 0) { |
| 596 | if(strcmp(buffer, "exit") == 0) { |
| 597 | LOGD("Get thread exit message."); |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | else |
| 603 | { |
| 604 | LOGW("Unknown select event."); |
| 605 | continue; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | #if GNSS_DEBUG |
| 610 | if(debug_fd > 0) { |
| 611 | close(debug_fd); |
| 612 | debug_fd = -1; |
| 613 | } |
| 614 | if(nmea_log_fd > 0) { |
| 615 | close(nmea_log_fd); |
| 616 | nmea_log_fd = -1; |
| 617 | } |
| 618 | #endif |
| 619 | |
| 620 | gnss_info.state = GNSS_STATE_CLOSE; |
| 621 | LOGD("gnss_read_pthread exit."); |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | #if 0 |
| 626 | int gnss_write(int fd, const void *data, int data_len) |
| 627 | { |
| 628 | int count = 0; |
| 629 | int len = 0; |
| 630 | while(1) |
| 631 | { |
| 632 | len = write(fd, data + count, data_len - count); |
| 633 | if (len > 0) |
| 634 | { |
| 635 | count += len; |
| 636 | } |
| 637 | else |
| 638 | { |
| 639 | LOGE("write() fail,ret = %d,errno = %d", len, errno); |
| 640 | break; |
| 641 | } |
| 642 | |
| 643 | if (count == data_len) |
| 644 | break; |
| 645 | } |
| 646 | |
| 647 | return count; |
| 648 | } |
| 649 | #else |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 650 | int gnss_write(int fd, const void* buf, int buf_len) |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 651 | { |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 652 | ssize_t size; |
| 653 | ssize_t size_to_wr; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 654 | ssize_t size_written; |
| 655 | if(GNSS_BUFF_SIZE < buf_len) |
| 656 | { |
| 657 | return -1; |
| 658 | } |
| 659 | for(size = 0; size < buf_len;) |
| 660 | { |
| 661 | size_to_wr = buf_len - size; |
| 662 | if( size_to_wr > GNSS_BUFF_SIZE) |
| 663 | size_to_wr = GNSS_BUFF_SIZE; |
| 664 | |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 665 | size_written = write(fd, (const uint8*)buf + size, size_to_wr); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 666 | if (size_written==-1) |
| 667 | { |
| 668 | return -1; |
| 669 | } |
| 670 | size += size_written; |
| 671 | if(size_written != size_to_wr) |
| 672 | { |
| 673 | return size; |
| 674 | } |
| 675 | } |
| 676 | // LOGD("SEND %d / %d", size, buf_len); |
| 677 | return size; |
| 678 | } |
| 679 | #endif |
| 680 | |
| 681 | int gnss_init(uint32 print_port) |
| 682 | { |
| 683 | if(gnss_info.state != GNSS_STATE_CLOSE) { |
| 684 | LOGW("GNSS not close:%d", gnss_info.state); |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 685 | if(gnss_info.state == GNSS_STATE_READY) { |
| 686 | LOGD("Reset print port : %d -> %d", gnss_info.print_port, print_port); |
| 687 | if(gnss_info.print_port != print_port) { |
| 688 | return gnss_ports_open(print_port); |
| 689 | } else { |
| 690 | return GNSS_ERR_OK; |
| 691 | } |
| 692 | } else { |
| 693 | return GNSS_ERR_OK; |
| 694 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | int ret = 0; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 698 | if(gnss_info.dl_befor_open) { |
| 699 | //if(gnss_info.auto_dl_fw) { |
| 700 | gnss_info.state = GNSS_STATE_DOWNLOAD; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 701 | 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] | 702 | if(ret) { |
| 703 | LOGE("gnss_fw_dl() fail : %d", ret); |
| 704 | gnss_info.state = GNSS_STATE_CLOSE; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 705 | return GNSS_ERR_DL_FW; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 706 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 707 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 708 | gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name); |
| 709 | if(gnss_info.fd <= 0) { |
| 710 | LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd); |
| 711 | gnss_info.state = GNSS_STATE_CLOSE; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 712 | return GNSS_ERR_OPEN_DEV; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 713 | } |
| 714 | if(pipe(gnss_info.exit_fd)) { |
| 715 | LOGE("pipe() fail[%d].", errno); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 716 | return GNSS_ERR_UNKNOWN; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 717 | } |
| 718 | // GNSS is opened. |
| 719 | gnss_info.state = GNSS_STATE_OPEN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 720 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 721 | #if 0 |
| 722 | // Start gnss read thread. |
| 723 | pthread_attr_t thread_attr; |
| 724 | pthread_attr_init(&thread_attr); |
| 725 | if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) |
| 726 | { |
| 727 | LOGE("pthread_attr_setdetachstate() fail."); |
| 728 | goto main_exit; |
| 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 | 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] | 732 | #else |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 733 | if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL)) |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 734 | #endif |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 735 | { |
| 736 | LOGE("pthread_create() fail."); |
| 737 | goto exit_with_close; |
| 738 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 739 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 740 | ret = gnss_info.gnss_dev_open(); |
| 741 | if(ret) { |
| 742 | LOGE("gnss_dev_open() fail : %d", ret); |
| 743 | goto exit_with_thread_exit; |
| 744 | } |
| 745 | //} |
| 746 | } else { |
| 747 | gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name); |
| 748 | if(gnss_info.fd <= 0) { |
| 749 | LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd); |
| 750 | gnss_info.state = GNSS_STATE_CLOSE; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 751 | return GNSS_ERR_OPEN_DEV; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 752 | } |
| 753 | if(pipe(gnss_info.exit_fd)) { |
| 754 | LOGE("pipe() fail[%d].", errno); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 755 | return GNSS_ERR_UNKNOWN; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 756 | } |
| 757 | // GNSS is opened. |
| 758 | gnss_info.state = GNSS_STATE_OPEN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 759 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 760 | #if 0 |
| 761 | // Start gnss read thread. |
| 762 | pthread_attr_t thread_attr; |
| 763 | pthread_attr_init(&thread_attr); |
| 764 | if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) |
| 765 | { |
| 766 | LOGE("pthread_attr_setdetachstate() fail."); |
| 767 | goto main_exit; |
| 768 | } |
| 769 | |
| 770 | if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL)) |
| 771 | #else |
| 772 | if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL)) |
| 773 | #endif |
| 774 | { |
| 775 | LOGE("pthread_create() fail."); |
| 776 | goto exit_with_close; |
| 777 | } |
| 778 | |
| 779 | ret = gnss_info.gnss_dev_open(); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 780 | if(ret) { |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 781 | LOGE("gnss_dev_open() fail : %d", ret); |
| 782 | goto exit_with_thread_exit; |
| 783 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | // GNSS is ready, NMEA can print from uart. |
| 787 | gnss_info.state = GNSS_STATE_READY; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 788 | |
| 789 | LOGD("GNSS open success."); |
| 790 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 791 | return gnss_ports_open(print_port); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 792 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 793 | exit_with_thread_exit: |
| 794 | gnss_info.state = GNSS_STATE_CLOSING; |
| 795 | // Wait for read thread exit. |
| 796 | ret = pthread_join(gnss_info.read_pid, NULL); |
| 797 | if(ret){ |
| 798 | LOGE("pthrad_join fail(%d)",ret); |
| 799 | } |
| 800 | exit_with_close: |
| 801 | if(gnss_info.gnss_close(gnss_info.fd)) { |
| 802 | LOGE("gnss_close() fail."); |
| 803 | } |
| 804 | if(gnss_info.exit_fd[0] > 0) { |
| 805 | close(gnss_info.exit_fd[0]); |
| 806 | gnss_info.exit_fd[0] = -1; |
| 807 | } |
| 808 | if(gnss_info.exit_fd[1] > 0) { |
| 809 | close(gnss_info.exit_fd[1]); |
| 810 | gnss_info.exit_fd[1] = -1; |
| 811 | } |
| 812 | gnss_info.state = GNSS_STATE_CLOSE; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 813 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | int gnss_deinit() |
| 817 | { |
| 818 | if(gnss_info.state == GNSS_STATE_CLOSE) { |
| 819 | LOGW("GNSS is closed."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 820 | return GNSS_ERR_OK; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 821 | } else if(gnss_info.state == GNSS_STATE_CLOSING) { |
| 822 | LOGW("GNSS is closing..."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 823 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 824 | } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) { |
| 825 | LOGW("GNSS is downloading..."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 826 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 827 | } |
| 828 | |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 829 | if(gnss_info.gnss_dev_close(gnss_info.fd)) { |
| 830 | LOGE("gnss_dev_close() fail."); |
| 831 | return GNSS_ERR_UNKNOWN; |
| 832 | } |
| 833 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 834 | // Wait for read thread exit. |
| 835 | if(gnss_info.exit_fd[1] > 0) { |
| 836 | write(gnss_info.exit_fd[1], "exit", 4); |
| 837 | } |
| 838 | |
| 839 | gnss_info.state = GNSS_STATE_CLOSING; |
| 840 | int ret = pthread_join(gnss_info.read_pid, NULL); |
| 841 | if(ret){ |
| 842 | LOGE("pthrad_join fail(%d)",ret); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 843 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | if(gnss_info.gnss_close(gnss_info.fd)) { |
| 847 | LOGE("gnss_close() fail."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 848 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 849 | } |
| 850 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 851 | if(gnss_ports_close()) { |
| 852 | LOGE("gnss_ports_close fail."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 853 | return GNSS_ERR_UNKNOWN; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 854 | } |
| 855 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 856 | LOGD("gnss_ports_close() complete."); |
| 857 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 858 | gnss_info.fd = -1; |
| 859 | if(gnss_info.exit_fd[0] > 0) { |
| 860 | close(gnss_info.exit_fd[0]); |
| 861 | gnss_info.exit_fd[0] = -1; |
| 862 | } |
| 863 | if(gnss_info.exit_fd[1] > 0) { |
| 864 | close(gnss_info.exit_fd[1]); |
| 865 | gnss_info.exit_fd[1] = -1; |
| 866 | } |
| 867 | gnss_info.state = GNSS_STATE_CLOSE; |
| 868 | LOGD("GNSS close success."); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 869 | return GNSS_ERR_OK; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | int gnss_set(const void* buf, unsigned int buf_len, void *cmd_rsp, int cmd_rsp_len) |
| 873 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 874 | if(buf && buf_len > 0) { |
| 875 | if(cmd_rsp && cmd_rsp_len > 0) { |
| 876 | memset(cmd_rsp, 0, cmd_rsp_len); |
| 877 | } |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 878 | return gnss_info.gnss_set(gnss_info.fd, buf, cmd_rsp, cmd_rsp_len); |
| 879 | } else { |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 880 | return GNSS_ERR_UNKNOWN; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | int gnss_dl_fw(const char* fw_name, void *rsp, int rsp_len) |
| 885 | { |
b.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 886 | // Only 8122 support download fw. |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 887 | if(gnss_info.gnss_id != GNSS_TYPE_8122) { |
| 888 | return GNSS_ERR_UNSUPPORT; |
| 889 | } |
| 890 | |
| 891 | if(rsp && rsp_len > 0) { |
| 892 | memset(rsp, 0, rsp_len); |
| 893 | } |
| 894 | |
| 895 | if(gnss_info.gnss_fw_dl) { |
| 896 | if(GNSS_ERR_OK != gnss_deinit()) { |
| 897 | LOGE("Close gnss fail."); |
| 898 | return GNSS_ERR_UNKNOWN; |
| 899 | } else { |
| 900 | LOGD("Start gnss fw dl."); |
| 901 | return gnss_info.gnss_fw_dl(gnss_info.fd, fw_name, gnss_info.dev_name); |
| 902 | } |
| 903 | } else { |
b.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 904 | return GNSS_ERR_UNSUPPORT; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 908 | int gnss_ind_set(int fd, int ind_type) |
| 909 | { |
| 910 | int index = 0; |
| 911 | if(ind_type) { // Add IND flag. |
| 912 | while(index < GNSS_CLI_IND_MAX) { |
| 913 | if(ind_info[index].cli_fd == fd) |
| 914 | break; |
| 915 | index++; |
| 916 | } |
| 917 | |
| 918 | if(index == GNSS_CLI_IND_MAX) { // Add flag |
| 919 | index = 0; |
| 920 | while(index < GNSS_CLI_IND_MAX) { |
| 921 | if(ind_info[index].cli_fd <= 0) |
| 922 | break; |
| 923 | index++; |
| 924 | } |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame^] | 925 | if(index == GNSS_CLI_IND_MAX) |
| 926 | { |
| 927 | LOGE("ind flag is full."); |
| 928 | return GNSS_ERR_CLI_FULL; |
| 929 | } |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 930 | ind_info[index].cli_fd = fd; |
| 931 | ind_info[index].ind_flag = (uint32)ind_type; |
| 932 | } else { // Change flag |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame^] | 933 | if(ind_info[index].ind_flag == MBTK_GNSS_IND_LOCATION) |
| 934 | { |
| 935 | ind_num.locl_cb_num--; |
| 936 | } |
| 937 | else if(ind_info[index].ind_flag == MBTK_GNSS_IND_NMEA) |
| 938 | { |
| 939 | ind_num.nmea_cb_num--; |
| 940 | } |
| 941 | |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 942 | ind_info[index].cli_fd = fd; |
| 943 | ind_info[index].ind_flag = (uint32)ind_type; |
| 944 | } |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame^] | 945 | |
| 946 | if(ind_type == MBTK_GNSS_IND_LOCATION) |
| 947 | { |
| 948 | ind_num.locl_cb_num++; |
| 949 | } |
| 950 | else if(ind_type == MBTK_GNSS_IND_NMEA) |
| 951 | { |
| 952 | ind_num.nmea_cb_num++; |
| 953 | } |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 954 | } else { // Clear IND flag. |
| 955 | while(index < GNSS_CLI_IND_MAX) { |
| 956 | if(ind_info[index].cli_fd == fd) |
| 957 | break; |
| 958 | index++; |
| 959 | } |
| 960 | |
| 961 | if(index == GNSS_CLI_IND_MAX) { |
| 962 | return GNSS_ERR_ARG; |
| 963 | } |
| 964 | |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame^] | 965 | if(ind_info[index].ind_flag == MBTK_GNSS_IND_LOCATION) |
| 966 | { |
| 967 | ind_num.locl_cb_num--; |
| 968 | } |
| 969 | else if(ind_info[index].ind_flag == MBTK_GNSS_IND_NMEA) |
| 970 | { |
| 971 | ind_num.nmea_cb_num--; |
| 972 | } |
| 973 | |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame] | 974 | ind_info[index].cli_fd = 0; |
| 975 | ind_info[index].ind_flag = 0; |
| 976 | } |
| 977 | |
| 978 | return GNSS_ERR_OK; |
| 979 | } |
| 980 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 981 | static void sig_process(int sig) |
| 982 | { |
| 983 | LOGI("I got signal %d\n", sig); |
| 984 | if(gnss_deinit()) { |
| 985 | LOGE("gnss_deinit() fail, no exist..."); |
| 986 | return; |
| 987 | } |
| 988 | |
| 989 | switch(sig) |
| 990 | { |
| 991 | case SIGINT: // Ctrl + C |
| 992 | { |
| 993 | LOGI("Exit by SIGINT.\n"); |
| 994 | exit(0); |
| 995 | } |
| 996 | case SIGQUIT: // Ctrl + \ (ÀàËÆ SIGINT £¬µ«Òª²úÉúcoreÎļþ) |
| 997 | { |
| 998 | LOGI("Exit by SIGQUIT.\n"); |
| 999 | exit(0); |
| 1000 | } |
| 1001 | case SIGTERM:// ĬÈÏkill (ͬ SIGKILL £¬µ« SIGKILL ²»¿É²¶»ñ) |
| 1002 | { |
| 1003 | LOGI("Exit by SIGTERM.\n"); |
| 1004 | exit(0); |
| 1005 | } |
| 1006 | case SIGTSTP:// Ctrl + Z (ͬ SIGSTOP £¬µ« SIGSTOP ²»¿É²¶»ñ) |
| 1007 | { |
| 1008 | LOGI("Exit by SIGTSTP.\n"); |
| 1009 | exit(0); |
| 1010 | } |
| 1011 | case SIGSEGV: // Èç¿ÕÖ¸Õë |
| 1012 | { |
| 1013 | LOGI("Exit by SIGSEGV.\n"); |
| 1014 | exit(0); |
| 1015 | } |
| 1016 | default: |
| 1017 | { |
| 1018 | LOGI("Unknown sig:%d\n",sig); |
| 1019 | break; |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 1025 | // mbtk_gnssd 6228 /dev/ttyS2 baud 0/1 <port_type> |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1026 | int main(int argc, char *argv[]) |
| 1027 | { |
| 1028 | mbtk_log_init("radio", GNSS_TAG); |
| 1029 | |
b.liu | bb59049 | 2024-06-13 16:42:08 +0800 | [diff] [blame] | 1030 | #ifdef MBTK_DUMP_SUPPORT |
| 1031 | mbtk_debug_open(NULL, TRUE); |
| 1032 | #endif |
| 1033 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1034 | signal(SIGINT, sig_process); |
| 1035 | signal(SIGQUIT, sig_process); |
| 1036 | signal(SIGTERM, sig_process); |
| 1037 | |
| 1038 | if(arg_check(argc, argv)) { |
| 1039 | return -1; |
| 1040 | } |
| 1041 | |
| 1042 | #ifdef GNSS_DEBUG |
| 1043 | char buff[10]; |
| 1044 | memset(buff, 0, 10); |
| 1045 | property_get(MBTK_PROP_GNSS_LOG, buff, ""); |
| 1046 | if(strlen(buff) > 0 && atoi(buff) > 0) { |
| 1047 | nmea_log_enable = TRUE; |
| 1048 | } |
| 1049 | #endif |
| 1050 | |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame^] | 1051 | memset(&ind_num, 0x0, sizeof(gnss_ind_type_num_t)); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1052 | memset(&gnss_info, 0, sizeof(gnss_info_t)); |
| 1053 | memcpy(gnss_info.dev_name, argv[2], strlen(argv[2])); |
| 1054 | gnss_info.state = GNSS_STATE_CLOSE; |
| 1055 | if(!strcmp(argv[1], GNSS_ID_6228)) { |
| 1056 | gnss_info.gnss_id = GNSS_TYPE_6228; |
| 1057 | gnss_info.auto_open = (bool)atoi(argv[3]); |
| 1058 | gnss_info.auto_dl_fw = TRUE; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 1059 | gnss_info.dl_befor_open = FALSE; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1060 | gnss_info.gnss_dev_open = gnss_6228_dev_open; |
| 1061 | gnss_info.gnss_dev_close = gnss_6228_dev_close; |
| 1062 | gnss_info.gnss_open = gnss_6228_open; |
| 1063 | gnss_info.gnss_close = gnss_6228_close; |
| 1064 | gnss_info.gnss_fw_dl = gnss_6228_fw_dl; |
| 1065 | gnss_info.gnss_dl_read_cb = gnss_6228_dl_read_cb; |
| 1066 | gnss_info.gnss_set = gnss_6228_set; |
| 1067 | gnss_info.gnss_set_cb = gnss_6228_set_cb; |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 1068 | } else if(!strcmp(argv[1], GNSS_ID_8122)) { |
| 1069 | gnss_info.gnss_id = GNSS_TYPE_8122; |
| 1070 | gnss_info.auto_open = (bool)atoi(argv[3]); |
| 1071 | gnss_info.auto_dl_fw = FALSE; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 1072 | gnss_info.dl_befor_open = FALSE; |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 1073 | gnss_info.gnss_dev_open = gnss_8122_dev_open; |
| 1074 | gnss_info.gnss_dev_close = gnss_8122_dev_close; |
| 1075 | gnss_info.gnss_open = gnss_8122_open; |
| 1076 | gnss_info.gnss_close = gnss_8122_close; |
| 1077 | gnss_info.gnss_fw_dl = gnss_8122_fw_dl; |
| 1078 | gnss_info.gnss_dl_read_cb = NULL; |
| 1079 | gnss_info.gnss_set = gnss_8122_set; |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 1080 | gnss_info.gnss_set_cb = gnss_8122_set_cb; |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 1081 | } else if(!strcmp(argv[1], GNSS_ID_5311)) { |
| 1082 | gnss_info.gnss_id = GNSS_TYPE_5311; |
| 1083 | gnss_info.auto_open = (bool)atoi(argv[3]); |
| 1084 | gnss_info.auto_dl_fw = TRUE; |
| 1085 | gnss_info.dl_befor_open = TRUE; |
| 1086 | gnss_info.gnss_dev_open = gnss_5311_dev_open; |
| 1087 | gnss_info.gnss_dev_close = gnss_5311_dev_close; |
| 1088 | gnss_info.gnss_open = gnss_5311_open; |
| 1089 | gnss_info.gnss_close = gnss_5311_close; |
| 1090 | gnss_info.gnss_fw_dl = gnss_5311_fw_dl; |
| 1091 | gnss_info.gnss_dl_read_cb = NULL; |
| 1092 | gnss_info.gnss_set = gnss_5311_set; |
| 1093 | gnss_info.gnss_set_cb = gnss_5311_set_cb; |
b.liu | 42f558e | 2024-07-18 14:06:49 +0800 | [diff] [blame] | 1094 | } else if(!strcmp(argv[1], GNSS_ID_N50DB)) { |
| 1095 | gnss_info.gnss_id = GNSS_TYPE_N50DB; |
| 1096 | gnss_info.auto_open = (bool)atoi(argv[3]); |
| 1097 | gnss_info.auto_dl_fw = FALSE; |
| 1098 | gnss_info.dl_befor_open = FALSE; |
| 1099 | gnss_info.gnss_dev_open = gnss_n50db_dev_open; |
| 1100 | gnss_info.gnss_dev_close = gnss_n50db_dev_close; |
| 1101 | gnss_info.gnss_open = gnss_n50db_open; |
| 1102 | gnss_info.gnss_close = gnss_n50db_close; |
| 1103 | gnss_info.gnss_fw_dl = gnss_n50db_fw_dl; |
| 1104 | gnss_info.gnss_dl_read_cb = NULL; |
| 1105 | gnss_info.gnss_set = gnss_n50db_set; |
| 1106 | gnss_info.gnss_set_cb = gnss_n50db_set_cb; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1107 | } else { |
| 1108 | LOGE("No support : %s", argv[1]); |
| 1109 | return -1; |
| 1110 | } |
| 1111 | |
| 1112 | LOGD("GNSS : %s, Device: %s", argv[1], gnss_info.dev_name); |
| 1113 | // Auto open gnss. |
| 1114 | if(gnss_info.auto_open) { |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 1115 | int init_mode = atoi(argv[3]); |
| 1116 | if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) != init_mode) { |
| 1117 | init_mode = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1118 | } |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 1119 | if(gnss_init((uint32)init_mode)) { |
| 1120 | LOGE("gnss_init() fail."); |
| 1121 | // return -1; |
| 1122 | } |
| 1123 | } else { |
| 1124 | gnss_info.print_port = 0; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | // Init ubus and waitting IPC commands. |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 1128 | #ifdef MBTK_GNSS_UBUS_ENABLE |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1129 | if(gnss_ubus_init()) { |
| 1130 | LOGD("main() run..."); |
| 1131 | uloop_run(); |
| 1132 | } else { |
| 1133 | LOGE("gnss_ubus_init() fail."); |
| 1134 | } |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 1135 | #else |
| 1136 | if(!gnss_ipc_service_start()) { |
| 1137 | LOGD("main() run..."); |
| 1138 | while(1) { |
| 1139 | sleep(24 * 60 * 60); |
| 1140 | } |
| 1141 | } else { |
| 1142 | LOGE("gnss_ipc_service_start() fail."); |
| 1143 | } |
| 1144 | #endif |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 1145 | |
| 1146 | LOGD("main() exit."); |
| 1147 | return 0; |
| 1148 | } |