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> |
| 11 | |
| 12 | #include "mbtk_type.h" |
| 13 | #include "mbtk_log.h" |
| 14 | #include "gnss_info.h" |
| 15 | |
| 16 | #include "gnss_6228.h" |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 17 | #include "gnss_hd8122.h" |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 18 | |
| 19 | #define GNSS_DEBUG 1 |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame^] | 20 | #define GNSS_UBUS_ENABLE 0 |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 21 | |
| 22 | #define GNSS_TAG "MBTK_GNSS" |
| 23 | #define GNSS_BUFF_SIZE 2048 |
| 24 | #define MBTK_PROP_GNSS_LOG "persist.mbtk.gnss_log_enable" |
| 25 | #define GNSS_PORT_PTY "/dev/tty_gnss_nmea" |
| 26 | #define GNSS_PORT_USB_AT "/dev/ttyGS0" |
| 27 | #define GNSS_PORT_USB_NMEA "/dev/ttymodem0" |
| 28 | #define GNSS_PORT_UART_AT "/dev/ttyS1" |
| 29 | |
| 30 | #ifdef GNSS_DEBUG |
| 31 | #define GNSS_NMEA_FILE_LOG "/tmp/mbtk_gnss_nmea.log" |
| 32 | #define GNSS_NMEA_FILE_LOG_MAX 10485760 // 10MB |
| 33 | |
| 34 | #define GNSS_FILE_LOG "/tmp/mbtk_gnss.log" |
| 35 | #define GNSS_FILE_LOG_MAX 10485760 // 10MB |
| 36 | #endif |
| 37 | |
| 38 | gnss_info_t gnss_info; |
| 39 | |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame^] | 40 | #if GNSS_UBUS_ENABLE |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 41 | struct ubus_context *gnss_ubus_init(void); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame^] | 42 | #else |
| 43 | int gnss_ipc_service_start(); |
| 44 | #endif |
| 45 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 46 | int gnss_init_config(int fd); |
| 47 | |
| 48 | static char gnss_buff[GNSS_BUFF_SIZE*2] = {0}; |
| 49 | static uint32 gnss_buff_len = 0; |
| 50 | static bool nmea_found = FALSE; |
| 51 | #ifdef GNSS_DEBUG |
| 52 | static bool nmea_log_enable = FALSE; |
| 53 | static int nmea_log_fd = -1; |
| 54 | static int nmea_log_fd_len = 0; |
| 55 | #endif |
| 56 | static int gnss_pty_master_fd = -1; |
| 57 | static int gnss_pty_slave_fd = -1; |
| 58 | static int gnss_usb_at_port_fd = -1; |
| 59 | static int gnss_usb_nmea_port_fd = -1; |
| 60 | static int gnss_uart_at_port_fd = -1; |
| 61 | static char *gnss_filter_info[] = {"RMC", "VTG", "GGA", "GSA", "GSV", "GLL", "ZDA", "GST", "TXT", "DHV", "DTM", NULL}; |
| 62 | |
| 63 | static void help() |
| 64 | { |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 65 | LOGD("mbtk_gnssd <6228/8122...> <gnss_dev> <0/1>"); |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static int arg_check(int argc, char *argv[]) |
| 69 | { |
| 70 | if(argc != 4) { |
| 71 | goto check_fail; |
| 72 | } |
| 73 | |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 74 | // Only support 6228/8122. |
| 75 | if(strcmp(argv[1], GNSS_ID_6228) && strcmp(argv[1], GNSS_ID_8122)) { |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 76 | goto check_fail; |
| 77 | } |
| 78 | |
| 79 | if(access(argv[2], R_OK | W_OK)) { |
| 80 | goto check_fail; |
| 81 | } |
| 82 | |
| 83 | if(strcmp(argv[3], "0") && strcmp(argv[3], "1")) { |
| 84 | goto check_fail; |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | check_fail: |
| 89 | help(); |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | static int gnss_ports_open(uint32 print_port) |
| 94 | { |
| 95 | if(print_port & GNSS_PRINT_PORT_TTY_AT) { |
| 96 | if(gnss_pty_open(&gnss_pty_master_fd, &gnss_pty_slave_fd, GNSS_PORT_PTY)) { |
| 97 | return -1; |
| 98 | } |
| 99 | LOGD("Open PTY port success."); |
| 100 | } |
| 101 | |
| 102 | if(print_port & GNSS_PRINT_PORT_USB_AT) { |
| 103 | if((gnss_usb_at_port_fd = gnss_port_open(GNSS_PORT_USB_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, FALSE)) <= 0) { |
| 104 | return -1; |
| 105 | } |
| 106 | LOGD("Open USB AT port success."); |
| 107 | } |
| 108 | |
| 109 | if(print_port & GNSS_PRINT_PORT_USB_NMEA) { |
| 110 | if((gnss_usb_nmea_port_fd = gnss_port_open(GNSS_PORT_USB_NMEA, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, FALSE)) <= 0) { |
| 111 | return -1; |
| 112 | } |
| 113 | LOGD("Open USB NMEA port success."); |
| 114 | } |
| 115 | |
| 116 | if(print_port & GNSS_PRINT_PORT_UART1) { |
| 117 | if((gnss_uart_at_port_fd = gnss_port_open(GNSS_PORT_UART_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) { |
| 118 | return -1; |
| 119 | } |
| 120 | LOGD("Open UART AT port success."); |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int gnss_ports_close() |
| 127 | { |
| 128 | if(gnss_usb_at_port_fd > 0) { |
| 129 | close(gnss_usb_at_port_fd); |
| 130 | gnss_usb_at_port_fd = -1; |
| 131 | } |
| 132 | |
| 133 | if(gnss_usb_nmea_port_fd > 0) { |
| 134 | close(gnss_usb_nmea_port_fd); |
| 135 | gnss_usb_nmea_port_fd = -1; |
| 136 | } |
| 137 | |
| 138 | if(gnss_uart_at_port_fd > 0) { |
| 139 | close(gnss_uart_at_port_fd); |
| 140 | gnss_uart_at_port_fd = -1; |
| 141 | } |
| 142 | |
| 143 | if(gnss_pty_master_fd > 0) { |
| 144 | close(gnss_pty_master_fd); |
| 145 | gnss_pty_master_fd = -1; |
| 146 | } |
| 147 | |
| 148 | if(gnss_pty_slave_fd > 0) { |
| 149 | close(gnss_pty_slave_fd); |
| 150 | gnss_pty_slave_fd = -1; |
| 151 | unlink(GNSS_PORT_PTY); |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static void nmea_print(const char *nmea, int nmea_len) |
| 158 | { |
| 159 | #ifdef GNSS_DEBUG |
| 160 | if(nmea_log_enable){ |
| 161 | if(nmea_log_fd_len > GNSS_NMEA_FILE_LOG_MAX) { |
| 162 | close(nmea_log_fd); |
| 163 | nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 164 | if(nmea_log_fd < 0) { |
| 165 | LOGE("Open debug fd fail."); |
| 166 | } |
| 167 | nmea_log_fd_len = 0; |
| 168 | } |
| 169 | |
| 170 | if(nmea_log_fd > 0) { |
| 171 | write(nmea_log_fd, nmea, nmea_len); |
| 172 | nmea_log_fd_len += nmea_len; |
| 173 | } |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | if(gnss_usb_at_port_fd > 0) { |
| 178 | write(gnss_usb_at_port_fd, nmea, nmea_len); |
| 179 | } |
| 180 | |
| 181 | if(gnss_usb_nmea_port_fd > 0) { |
| 182 | write(gnss_usb_nmea_port_fd, nmea, nmea_len); |
| 183 | } |
| 184 | |
| 185 | if(gnss_uart_at_port_fd > 0) { |
| 186 | write(gnss_uart_at_port_fd, nmea, nmea_len); |
| 187 | } |
| 188 | |
| 189 | if(gnss_pty_master_fd > 0) { |
| 190 | write(gnss_pty_master_fd, nmea, nmea_len); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | static unsigned char nmea_checksum(const char *nmea) |
| 195 | { |
| 196 | const char *p = nmea; |
| 197 | unsigned char chs = 0; |
| 198 | |
| 199 | while (*p == '$') // skip '$' |
| 200 | p++; |
| 201 | while (*p != '*' && *p != 0) |
| 202 | chs ^= *p++; |
| 203 | |
| 204 | return chs; |
| 205 | } |
| 206 | |
| 207 | static bool nmea_check(const char *nmea, int nmea_len) |
| 208 | { |
| 209 | char **ptr = gnss_filter_info; |
| 210 | while(*ptr) { |
| 211 | if(strstr(nmea, *ptr)) { |
| 212 | break; |
| 213 | } |
| 214 | ptr++; |
| 215 | } |
| 216 | |
| 217 | if(*ptr == NULL) { |
| 218 | LOGD("Unknown NMEA[%d]:%s", nmea_len, nmea); |
| 219 | return FALSE; |
| 220 | } |
| 221 | |
| 222 | char *checksum_str = strstr(nmea, "*"); |
| 223 | checksum_str++; // Jump '*' |
| 224 | char checksum_buf[3] = {0}; |
| 225 | snprintf(checksum_buf, 3, "%02x", nmea_checksum(nmea)); |
| 226 | if(strncasecmp(checksum_buf, checksum_str, 2)) { |
| 227 | LOGD("Checksum error[%d](checksum - %s):%s", nmea_len, checksum_buf, nmea); |
| 228 | return FALSE; |
| 229 | } |
| 230 | |
| 231 | return TRUE; |
| 232 | } |
| 233 | |
| 234 | static void gnss_nmea_process(const char *data, int data_len) |
| 235 | { |
| 236 | char nmea[GNSS_BUFF_SIZE] = {0}; |
| 237 | memcpy(nmea, data, data_len); |
| 238 | |
| 239 | if(!nmea_check(nmea, data_len)) { |
| 240 | gnss_info.gnss_set_cb(nmea, data_len); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | #ifdef GNSS_DEBUG |
| 245 | if(nmea_log_enable) { |
| 246 | LOGD("NMEA[%d]:%s", data_len, nmea); |
| 247 | } |
| 248 | #endif |
| 249 | |
| 250 | nmea_print(nmea, data_len); |
| 251 | } |
| 252 | |
| 253 | #if 0 |
| 254 | static void gnss_cmd_rsp_process(const char *data, int data_len) |
| 255 | { |
| 256 | char rsp[GNSS_BUFF_SIZE] = {0}; |
| 257 | memcpy(rsp, data, data_len); |
| 258 | LOGD("RSP[%d]:%s", data_len, rsp); |
| 259 | } |
| 260 | #endif |
| 261 | |
| 262 | static bool nmea_char_check(char ch) |
| 263 | { |
| 264 | if(isalnum(ch) || ch == '$' || ch == '\r' || ch == '\n' || ch == '.' |
| 265 | || ch == ',' || ch == '*' || ch == '\0' || ch == '/') |
| 266 | return TRUE; |
| 267 | |
| 268 | return FALSE; |
| 269 | } |
| 270 | |
| 271 | static void gnss_data_process(const char *data, int data_len) |
| 272 | { |
| 273 | if(gnss_info.state == GNSS_STATE_OPEN) { |
| 274 | LOGD("GNSS_OPEN[%d]:%s", data_len, data); |
| 275 | } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) { |
| 276 | // LOGD("GNSS_DL[%d]:%s", data_len, data); |
| 277 | gnss_info.gnss_dl_read_cb(data, data_len); |
| 278 | } else if(gnss_info.state == GNSS_STATE_READY) { |
| 279 | int index = 0; |
| 280 | while(index < data_len) { |
| 281 | if(nmea_found) { |
| 282 | if(!nmea_char_check(data[index])) { |
| 283 | gnss_buff_len = 0; |
| 284 | nmea_found = FALSE; |
| 285 | #if 0 |
| 286 | if(isascii(data[index])) { |
| 287 | LOGD("\n\n###%c###%s\n\n", data[index], data); |
| 288 | } |
| 289 | #endif |
| 290 | continue; |
| 291 | } |
| 292 | |
| 293 | if(data[index] != '\0') { |
| 294 | gnss_buff[gnss_buff_len++] = data[index]; |
| 295 | if(gnss_buff[gnss_buff_len - 1] == '\n') { |
| 296 | |
| 297 | if(gnss_buff_len > 6 && gnss_buff[gnss_buff_len - 5] == '*') { // $XXX*YY\r\n |
| 298 | gnss_nmea_process(gnss_buff, gnss_buff_len); |
| 299 | } |
| 300 | |
| 301 | gnss_buff_len = 0; |
| 302 | nmea_found = FALSE; |
| 303 | } |
| 304 | } |
| 305 | } else { |
| 306 | if(data[index] == '$') { |
| 307 | gnss_buff_len = 0; |
| 308 | nmea_found = TRUE; |
| 309 | gnss_buff[gnss_buff_len++] = data[index]; |
| 310 | } |
| 311 | } |
| 312 | index++; |
| 313 | } |
| 314 | } else { |
| 315 | LOGW("Unknown state : %d", gnss_info.state); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void* gnss_read_pthread(void* arg) |
| 320 | { |
| 321 | LOGD("gnss_read_pthread enter."); |
| 322 | char buffer[GNSS_BUFF_SIZE]; |
| 323 | int len = 0; |
| 324 | int ret = 0; |
| 325 | fd_set fdr, fdw; |
| 326 | int fd_max = 0; |
| 327 | |
| 328 | FD_ZERO(&fdw); |
| 329 | FD_ZERO(&fdr); |
| 330 | FD_SET(gnss_info.fd, &fdr); |
| 331 | fd_max = (gnss_info.fd > fd_max) ? gnss_info.fd : fd_max; |
| 332 | FD_SET(gnss_info.exit_fd[0], &fdr); |
| 333 | fd_max = (gnss_info.exit_fd[0] > fd_max) ? gnss_info.exit_fd[0] : fd_max; |
| 334 | memset(gnss_buff, 0, sizeof(gnss_buff)); |
| 335 | gnss_buff_len = 0; |
| 336 | #if GNSS_DEBUG |
| 337 | int debug_fd = -1; |
| 338 | int debug_fd_len = 0; |
| 339 | if(nmea_log_enable) { |
| 340 | debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 341 | if(debug_fd < 0) { |
| 342 | LOGE("Open debug fd fail."); |
| 343 | } |
| 344 | nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 345 | if(nmea_log_fd < 0) { |
| 346 | LOGE("Open nmea fd fail."); |
| 347 | } |
| 348 | nmea_log_fd_len = 0; |
| 349 | } |
| 350 | #endif |
| 351 | |
| 352 | while(gnss_info.state >= GNSS_STATE_OPEN) { |
| 353 | ret = select(fd_max + 1, &fdr, &fdw, 0, NULL); |
| 354 | //LOGD("select - %d", ret); |
| 355 | if (ret < 0) |
| 356 | { |
| 357 | if (errno == EINTR) |
| 358 | { |
| 359 | continue; |
| 360 | } |
| 361 | LOGE("select error, errno = %d (%s)", errno, strerror(errno)); |
| 362 | break; |
| 363 | } |
| 364 | else if (ret == 0) |
| 365 | { |
| 366 | LOGE("select ret == 0"); |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | if (FD_ISSET(gnss_info.fd, &fdr)) |
| 371 | { |
| 372 | memset(buffer, 0, GNSS_BUFF_SIZE); |
| 373 | len = read(gnss_info.fd, buffer, GNSS_BUFF_SIZE); |
| 374 | if(len > 0) { |
| 375 | //log_hex("READ", buffer, len); |
| 376 | |
| 377 | #if GNSS_DEBUG |
| 378 | if(nmea_log_enable){ |
| 379 | if(debug_fd_len > GNSS_FILE_LOG_MAX) { |
| 380 | close(debug_fd); |
| 381 | debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 382 | if(debug_fd < 0) { |
| 383 | LOGE("Open debug fd fail."); |
| 384 | } |
| 385 | debug_fd_len = 0; |
| 386 | } |
| 387 | |
| 388 | if(debug_fd > 0) { |
| 389 | write(debug_fd, buffer, len); |
| 390 | debug_fd_len += len; |
| 391 | } |
| 392 | } |
| 393 | #endif |
| 394 | |
| 395 | gnss_data_process(buffer, len); |
| 396 | |
| 397 | } else if(len ==0 ){ |
| 398 | LOGE("Read end : len = 0"); |
| 399 | break; |
| 400 | } else { |
| 401 | if(EAGAIN == errno) { |
| 402 | usleep(50000); |
| 403 | continue; |
| 404 | } else { |
| 405 | LOGD("Read ret = -1 ,errno = %d", errno); |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | else if (FD_ISSET(gnss_info.exit_fd[0], &fdr)) |
| 411 | { |
| 412 | memset(buffer, 0, GNSS_BUFF_SIZE); |
| 413 | len = read(gnss_info.fd, buffer, GNSS_BUFF_SIZE); |
| 414 | if(len > 0) { |
| 415 | if(strcmp(buffer, "exit") == 0) { |
| 416 | LOGD("Get thread exit message."); |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | else |
| 422 | { |
| 423 | LOGW("Unknown select event."); |
| 424 | continue; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | #if GNSS_DEBUG |
| 429 | if(debug_fd > 0) { |
| 430 | close(debug_fd); |
| 431 | debug_fd = -1; |
| 432 | } |
| 433 | if(nmea_log_fd > 0) { |
| 434 | close(nmea_log_fd); |
| 435 | nmea_log_fd = -1; |
| 436 | } |
| 437 | #endif |
| 438 | |
| 439 | gnss_info.state = GNSS_STATE_CLOSE; |
| 440 | LOGD("gnss_read_pthread exit."); |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | #if 0 |
| 445 | int gnss_write(int fd, const void *data, int data_len) |
| 446 | { |
| 447 | int count = 0; |
| 448 | int len = 0; |
| 449 | while(1) |
| 450 | { |
| 451 | len = write(fd, data + count, data_len - count); |
| 452 | if (len > 0) |
| 453 | { |
| 454 | count += len; |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | LOGE("write() fail,ret = %d,errno = %d", len, errno); |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | if (count == data_len) |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | return count; |
| 467 | } |
| 468 | #else |
| 469 | int gnss_write(int fd, const void* buf, unsigned int buf_len) |
| 470 | { |
| 471 | size_t size; |
| 472 | size_t size_to_wr; |
| 473 | ssize_t size_written; |
| 474 | if(GNSS_BUFF_SIZE < buf_len) |
| 475 | { |
| 476 | return -1; |
| 477 | } |
| 478 | for(size = 0; size < buf_len;) |
| 479 | { |
| 480 | size_to_wr = buf_len - size; |
| 481 | if( size_to_wr > GNSS_BUFF_SIZE) |
| 482 | size_to_wr = GNSS_BUFF_SIZE; |
| 483 | |
| 484 | size_written = write(fd, &buf[size], size_to_wr); |
| 485 | if (size_written==-1) |
| 486 | { |
| 487 | return -1; |
| 488 | } |
| 489 | size += size_written; |
| 490 | if(size_written != size_to_wr) |
| 491 | { |
| 492 | return size; |
| 493 | } |
| 494 | } |
| 495 | // LOGD("SEND %d / %d", size, buf_len); |
| 496 | return size; |
| 497 | } |
| 498 | #endif |
| 499 | |
| 500 | int gnss_init(uint32 print_port) |
| 501 | { |
| 502 | if(gnss_info.state != GNSS_STATE_CLOSE) { |
| 503 | LOGW("GNSS not close:%d", gnss_info.state); |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | int ret = 0; |
| 508 | |
| 509 | gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name); |
| 510 | if(gnss_info.fd <= 0) { |
| 511 | LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd); |
| 512 | gnss_info.state = GNSS_STATE_CLOSE; |
| 513 | return -1; |
| 514 | } |
| 515 | if(pipe(gnss_info.exit_fd)) { |
| 516 | LOGE("pipe() fail[%d].", errno); |
| 517 | return -1; |
| 518 | } |
| 519 | // GNSS is opened. |
| 520 | gnss_info.state = GNSS_STATE_OPEN; |
| 521 | |
| 522 | #if 0 |
| 523 | // Start gnss read thread. |
| 524 | pthread_attr_t thread_attr; |
| 525 | pthread_attr_init(&thread_attr); |
| 526 | if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) |
| 527 | { |
| 528 | LOGE("pthread_attr_setdetachstate() fail."); |
| 529 | goto main_exit; |
| 530 | } |
| 531 | |
| 532 | if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL)) |
| 533 | #else |
| 534 | if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL)) |
| 535 | #endif |
| 536 | { |
| 537 | LOGE("pthread_create() fail."); |
| 538 | goto exit_with_close; |
| 539 | } |
| 540 | |
| 541 | ret = gnss_info.gnss_dev_open(); |
| 542 | if(ret) { |
| 543 | LOGE("gnss_dev_open() fail : %d", ret); |
| 544 | goto exit_with_thread_exit; |
| 545 | } |
| 546 | |
| 547 | if(gnss_info.auto_dl_fw) { |
| 548 | gnss_info.state = GNSS_STATE_DOWNLOAD; |
| 549 | ret = gnss_info.gnss_fw_dl(gnss_info.fd); |
| 550 | if(ret) { |
| 551 | LOGE("gnss_fw_dl() fail : %d", ret); |
| 552 | goto exit_with_dev_close; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | // GNSS is ready, NMEA can print from uart. |
| 557 | gnss_info.state = GNSS_STATE_READY; |
| 558 | gnss_info.print_port = print_port; |
| 559 | |
| 560 | LOGD("GNSS open success."); |
| 561 | |
| 562 | return gnss_ports_open(gnss_info.print_port); |
| 563 | |
| 564 | exit_with_dev_close: |
| 565 | if(gnss_info.gnss_dev_close()) { |
| 566 | LOGE("gnss_dev_close() fail."); |
| 567 | } |
| 568 | exit_with_thread_exit: |
| 569 | gnss_info.state = GNSS_STATE_CLOSING; |
| 570 | // Wait for read thread exit. |
| 571 | ret = pthread_join(gnss_info.read_pid, NULL); |
| 572 | if(ret){ |
| 573 | LOGE("pthrad_join fail(%d)",ret); |
| 574 | } |
| 575 | exit_with_close: |
| 576 | if(gnss_info.gnss_close(gnss_info.fd)) { |
| 577 | LOGE("gnss_close() fail."); |
| 578 | } |
| 579 | if(gnss_info.exit_fd[0] > 0) { |
| 580 | close(gnss_info.exit_fd[0]); |
| 581 | gnss_info.exit_fd[0] = -1; |
| 582 | } |
| 583 | if(gnss_info.exit_fd[1] > 0) { |
| 584 | close(gnss_info.exit_fd[1]); |
| 585 | gnss_info.exit_fd[1] = -1; |
| 586 | } |
| 587 | gnss_info.state = GNSS_STATE_CLOSE; |
| 588 | return -1; |
| 589 | } |
| 590 | |
| 591 | int gnss_deinit() |
| 592 | { |
| 593 | if(gnss_info.state == GNSS_STATE_CLOSE) { |
| 594 | LOGW("GNSS is closed."); |
| 595 | return 0; |
| 596 | } else if(gnss_info.state == GNSS_STATE_CLOSING) { |
| 597 | LOGW("GNSS is closing..."); |
| 598 | return -1; |
| 599 | } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) { |
| 600 | LOGW("GNSS is downloading..."); |
| 601 | return -1; |
| 602 | } |
| 603 | |
| 604 | // Wait for read thread exit. |
| 605 | if(gnss_info.exit_fd[1] > 0) { |
| 606 | write(gnss_info.exit_fd[1], "exit", 4); |
| 607 | } |
| 608 | |
| 609 | gnss_info.state = GNSS_STATE_CLOSING; |
| 610 | int ret = pthread_join(gnss_info.read_pid, NULL); |
| 611 | if(ret){ |
| 612 | LOGE("pthrad_join fail(%d)",ret); |
| 613 | return -1; |
| 614 | } |
| 615 | |
| 616 | if(gnss_info.gnss_close(gnss_info.fd)) { |
| 617 | LOGE("gnss_close() fail."); |
| 618 | return -1; |
| 619 | } |
| 620 | |
| 621 | if(gnss_info.gnss_dev_close()) { |
| 622 | LOGE("gnss_dev_close() fail."); |
| 623 | return -1; |
| 624 | } |
| 625 | |
| 626 | if(gnss_ports_close()) { |
| 627 | LOGE("gnss_ports_close fail."); |
| 628 | return -1; |
| 629 | } |
| 630 | |
| 631 | gnss_info.fd = -1; |
| 632 | if(gnss_info.exit_fd[0] > 0) { |
| 633 | close(gnss_info.exit_fd[0]); |
| 634 | gnss_info.exit_fd[0] = -1; |
| 635 | } |
| 636 | if(gnss_info.exit_fd[1] > 0) { |
| 637 | close(gnss_info.exit_fd[1]); |
| 638 | gnss_info.exit_fd[1] = -1; |
| 639 | } |
| 640 | gnss_info.state = GNSS_STATE_CLOSE; |
| 641 | LOGD("GNSS close success."); |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | int gnss_set(const void* buf, unsigned int buf_len, void *cmd_rsp, int cmd_rsp_len) |
| 646 | { |
| 647 | if(cmd_rsp && buf && buf_len > 0 && cmd_rsp_len > 0) { |
| 648 | memset(cmd_rsp, 0, cmd_rsp_len); |
| 649 | return gnss_info.gnss_set(gnss_info.fd, buf, cmd_rsp, cmd_rsp_len); |
| 650 | } else { |
| 651 | return -1; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | static void sig_process(int sig) |
| 656 | { |
| 657 | LOGI("I got signal %d\n", sig); |
| 658 | if(gnss_deinit()) { |
| 659 | LOGE("gnss_deinit() fail, no exist..."); |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | switch(sig) |
| 664 | { |
| 665 | case SIGINT: // Ctrl + C |
| 666 | { |
| 667 | LOGI("Exit by SIGINT.\n"); |
| 668 | exit(0); |
| 669 | } |
| 670 | case SIGQUIT: // Ctrl + \ (ÀàËÆ SIGINT £¬µ«Òª²úÉúcoreÎļþ) |
| 671 | { |
| 672 | LOGI("Exit by SIGQUIT.\n"); |
| 673 | exit(0); |
| 674 | } |
| 675 | case SIGTERM:// ĬÈÏkill (ͬ SIGKILL £¬µ« SIGKILL ²»¿É²¶»ñ) |
| 676 | { |
| 677 | LOGI("Exit by SIGTERM.\n"); |
| 678 | exit(0); |
| 679 | } |
| 680 | case SIGTSTP:// Ctrl + Z (ͬ SIGSTOP £¬µ« SIGSTOP ²»¿É²¶»ñ) |
| 681 | { |
| 682 | LOGI("Exit by SIGTSTP.\n"); |
| 683 | exit(0); |
| 684 | } |
| 685 | case SIGSEGV: // Èç¿ÕÖ¸Õë |
| 686 | { |
| 687 | LOGI("Exit by SIGSEGV.\n"); |
| 688 | exit(0); |
| 689 | } |
| 690 | default: |
| 691 | { |
| 692 | LOGI("Unknown sig:%d\n",sig); |
| 693 | break; |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | |
| 699 | // mbtk_gnssd 6228 /dev/ttyS2 baud 0/1 |
| 700 | int main(int argc, char *argv[]) |
| 701 | { |
| 702 | mbtk_log_init("radio", GNSS_TAG); |
| 703 | |
b.liu | bb59049 | 2024-06-13 16:42:08 +0800 | [diff] [blame] | 704 | #ifdef MBTK_DUMP_SUPPORT |
| 705 | mbtk_debug_open(NULL, TRUE); |
| 706 | #endif |
| 707 | |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 708 | signal(SIGINT, sig_process); |
| 709 | signal(SIGQUIT, sig_process); |
| 710 | signal(SIGTERM, sig_process); |
| 711 | |
| 712 | if(arg_check(argc, argv)) { |
| 713 | return -1; |
| 714 | } |
| 715 | |
| 716 | #ifdef GNSS_DEBUG |
| 717 | char buff[10]; |
| 718 | memset(buff, 0, 10); |
| 719 | property_get(MBTK_PROP_GNSS_LOG, buff, ""); |
| 720 | if(strlen(buff) > 0 && atoi(buff) > 0) { |
| 721 | nmea_log_enable = TRUE; |
| 722 | } |
| 723 | #endif |
| 724 | |
| 725 | memset(&gnss_info, 0, sizeof(gnss_info_t)); |
| 726 | memcpy(gnss_info.dev_name, argv[2], strlen(argv[2])); |
| 727 | gnss_info.state = GNSS_STATE_CLOSE; |
| 728 | if(!strcmp(argv[1], GNSS_ID_6228)) { |
| 729 | gnss_info.gnss_id = GNSS_TYPE_6228; |
| 730 | gnss_info.auto_open = (bool)atoi(argv[3]); |
| 731 | gnss_info.auto_dl_fw = TRUE; |
| 732 | gnss_info.gnss_dev_open = gnss_6228_dev_open; |
| 733 | gnss_info.gnss_dev_close = gnss_6228_dev_close; |
| 734 | gnss_info.gnss_open = gnss_6228_open; |
| 735 | gnss_info.gnss_close = gnss_6228_close; |
| 736 | gnss_info.gnss_fw_dl = gnss_6228_fw_dl; |
| 737 | gnss_info.gnss_dl_read_cb = gnss_6228_dl_read_cb; |
| 738 | gnss_info.gnss_set = gnss_6228_set; |
| 739 | gnss_info.gnss_set_cb = gnss_6228_set_cb; |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 740 | } else if(!strcmp(argv[1], GNSS_ID_8122)) { |
| 741 | gnss_info.gnss_id = GNSS_TYPE_8122; |
| 742 | gnss_info.auto_open = (bool)atoi(argv[3]); |
| 743 | gnss_info.auto_dl_fw = FALSE; |
| 744 | gnss_info.gnss_dev_open = gnss_8122_dev_open; |
| 745 | gnss_info.gnss_dev_close = gnss_8122_dev_close; |
| 746 | gnss_info.gnss_open = gnss_8122_open; |
| 747 | gnss_info.gnss_close = gnss_8122_close; |
| 748 | gnss_info.gnss_fw_dl = gnss_8122_fw_dl; |
| 749 | gnss_info.gnss_dl_read_cb = NULL; |
| 750 | gnss_info.gnss_set = gnss_8122_set; |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame^] | 751 | gnss_info.gnss_set_cb = gnss_8122_set_cb; |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 752 | } else { |
| 753 | LOGE("No support : %s", argv[1]); |
| 754 | return -1; |
| 755 | } |
| 756 | |
| 757 | LOGD("GNSS : %s, Device: %s", argv[1], gnss_info.dev_name); |
| 758 | // Auto open gnss. |
| 759 | if(gnss_info.auto_open) { |
| 760 | if(gnss_init(0)) { // No print to any port. |
| 761 | LOGE("gnss_init() fail."); |
| 762 | return -1; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | // Init ubus and waitting IPC commands. |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame^] | 767 | #if GNSS_UBUS_ENABLE |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 768 | if(gnss_ubus_init()) { |
| 769 | LOGD("main() run..."); |
| 770 | uloop_run(); |
| 771 | } else { |
| 772 | LOGE("gnss_ubus_init() fail."); |
| 773 | } |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame^] | 774 | #else |
| 775 | if(!gnss_ipc_service_start()) { |
| 776 | LOGD("main() run..."); |
| 777 | while(1) { |
| 778 | sleep(24 * 60 * 60); |
| 779 | } |
| 780 | } else { |
| 781 | LOGE("gnss_ipc_service_start() fail."); |
| 782 | } |
| 783 | #endif |
b.liu | 8f231a1 | 2024-05-31 17:55:06 +0800 | [diff] [blame] | 784 | |
| 785 | LOGD("main() exit."); |
| 786 | return 0; |
| 787 | } |