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