wangyouqiang | 4c2048e | 2024-01-04 13:39:23 +0800 | [diff] [blame] | 1 | /** |
| 2 | * \file mbtk_gnss_5311.c |
| 3 | * \brief gnss module. |
| 4 | * |
| 5 | * Detailed description |
| 6 | * \Author: Sniper <yq.wang@mobiletek.cn> |
| 7 | * \Version: 1.0.0 |
| 8 | * \Date: 2023-12-20 |
| 9 | */ |
| 10 | #if 1 |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | #include <strings.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <errno.h> |
| 16 | #include <pthread.h> |
| 17 | #include <sys/epoll.h> |
| 18 | #include <sys/ioctl.h> |
| 19 | #include <termios.h> |
| 20 | #include <sys/socket.h> |
yq.wang | 8bf5656 | 2024-01-04 14:07:22 +0800 | [diff] [blame] | 21 | #include <fcntl.h> |
b.liu | 12eaf8d | 2023-12-19 14:02:37 +0800 | [diff] [blame] | 22 | |
wangyouqiang | 4c2048e | 2024-01-04 13:39:23 +0800 | [diff] [blame] | 23 | #include <libubox/blobmsg_json.h> |
| 24 | #include <libubus.h> |
| 25 | |
| 26 | #include "mbtk_gnss_5311.h" |
| 27 | |
| 28 | /**********************************DEFINE***********************************/ |
| 29 | #define MBTK_GNSS_UBUS_SERVER "gps" |
| 30 | #define MBTK_GNSS_UBUS_INIT "gnss_init" |
| 31 | #define MBTK_GNSS_UBUS_INIT_PARAM "gnss_init_param" |
| 32 | #define MBTK_GNSS_UBUS_SLEEP "gnss_sleep" |
| 33 | #define MBTK_GNSS_UBUS_SLEEP_PARAM "gnss_sleep_param" |
| 34 | #define MBTK_GNSS_UBUS_SET "gnss_setting" |
| 35 | #define MBTK_GNSS_UBUS_SET_PARAM "gnss_setting_param" |
| 36 | #define MBTK_GNSS_UBUS_GET_STATUS "gnss_get_state" |
wangyouqiang | 7d66fb1 | 2024-01-26 19:19:00 +0800 | [diff] [blame^] | 37 | #define MBTK_GNSS_UBUS_AGPS_SERVER "server_name" |
| 38 | #define MBTK_GNSS_UBUS_ALAM_FLAG "alam_flag" |
| 39 | #define MBTK_GNSS_UBUS_GET_AGPS "gnss_get_agps" |
| 40 | #define MBTK_GNSS_UBUS_SET_AGPS "gnss_set_agps" |
wangyouqiang | 4c2048e | 2024-01-04 13:39:23 +0800 | [diff] [blame] | 41 | |
| 42 | #define MBTK_RESULT_FAIL -1 |
| 43 | #define MBTK_RESULT_SUCCESS 0 |
| 44 | |
| 45 | #define MBTK_GNSS_CLOSE 0 |
| 46 | #define MBTK_GNSS_OPEN 1 |
| 47 | |
| 48 | #define MBTK_GNSS_WAKEUP 0 |
| 49 | #define MBTK_GNSS_SLEEP 1 |
| 50 | |
| 51 | #define DATABITS CS8 |
| 52 | #define BAUD B115200 |
| 53 | #define STOPBITS 0 |
| 54 | #define PARITYON 0 |
| 55 | #define PARITY 0 |
| 56 | |
| 57 | #define MBTK_GNSS_NMEA_PORT "/dev/tty_gnss_nmea" |
| 58 | /**********************************DEFINE***********************************/ |
| 59 | |
| 60 | /**********************************VARIABLE***********************************/ |
| 61 | const struct blobmsg_policy mbtk_gnss_ubus_cb_policy[] = { |
| 62 | [0] = { |
| 63 | .name = "event", |
| 64 | .type = BLOBMSG_TYPE_INT32, |
| 65 | }, |
| 66 | }; |
| 67 | |
| 68 | const struct blobmsg_policy mbtk_gnss_state_resp_cb_policy[] = { |
| 69 | [0] = { |
| 70 | .name = "gps_state_resp", |
| 71 | .type = BLOBMSG_TYPE_STRING, |
| 72 | }, |
| 73 | }; |
| 74 | |
| 75 | static int mbtk_gnss_uloop_init = 0; |
| 76 | static struct ubus_context *mbtk_gnss_ctx = NULL; |
| 77 | static int mbtk_gnss_status = MBTK_GNSS_CLOSE; |
| 78 | static mbtk_gnss_nmea_status nmea_state = {0}; |
| 79 | /**********************************VARIABLE***********************************/ |
| 80 | |
| 81 | /**********************************FUNC***********************************/ |
| 82 | struct ubus_context *mbtk_get_ubus_ctx(void) |
| 83 | { |
| 84 | if (!mbtk_gnss_uloop_init) |
| 85 | { |
| 86 | return NULL; |
| 87 | } |
| 88 | if (mbtk_gnss_ctx != NULL) |
| 89 | { |
| 90 | return mbtk_gnss_ctx; |
| 91 | } |
| 92 | |
| 93 | mbtk_gnss_ctx = ubus_connect(NULL); |
| 94 | if (!mbtk_gnss_ctx) |
| 95 | { |
| 96 | LOGE("[mbtk_gnss_api] ubus_connect connect fail."); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | ubus_add_uloop(mbtk_gnss_ctx); |
| 101 | return mbtk_gnss_ctx; |
| 102 | } |
| 103 | |
| 104 | int mbtk_gnss_ubus_uloop_init(void) |
| 105 | { |
| 106 | int ret = -1; |
| 107 | if(mbtk_gnss_uloop_init == 0) |
| 108 | { |
| 109 | ret = uloop_init(); |
| 110 | if(ret != 0) |
| 111 | { |
| 112 | LOGE("[mbtk_gnss_api] uloop_init fail.ret = [%d]", ret); |
| 113 | return MBTK_RESULT_FAIL; |
| 114 | } |
| 115 | |
| 116 | if(mbtk_gnss_ctx != NULL) |
| 117 | { |
| 118 | LOGE("[mbtk_gnss_api] mbtk_gnss_ctx not NULL."); |
| 119 | return MBTK_RESULT_FAIL; |
| 120 | } |
| 121 | |
| 122 | mbtk_gnss_ctx = ubus_connect(NULL); |
| 123 | if(!mbtk_gnss_ctx) |
| 124 | { |
| 125 | LOGE("[mbtk_gnss_api] ubus_connect fail."); |
| 126 | return MBTK_RESULT_FAIL; |
| 127 | } |
| 128 | |
| 129 | ubus_add_uloop(mbtk_gnss_ctx); |
| 130 | } |
| 131 | mbtk_gnss_uloop_init = 1; |
| 132 | return MBTK_RESULT_SUCCESS; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | int mbtk_gnss_ubus_uloop_deinit(void) |
| 137 | { |
| 138 | if(mbtk_gnss_uloop_init == 1) |
| 139 | { |
| 140 | if(mbtk_gnss_ctx != NULL) |
| 141 | { |
| 142 | ubus_free(mbtk_gnss_ctx); |
| 143 | mbtk_gnss_ctx = NULL; |
| 144 | } |
| 145 | |
| 146 | uloop_done(); |
| 147 | mbtk_gnss_uloop_init = 0; |
| 148 | } |
| 149 | return MBTK_RESULT_SUCCESS; |
| 150 | } |
| 151 | |
| 152 | static void mbtk_gnss_ubus_result_callback(struct ubus_request *req, int type, struct blob_attr *msg) |
| 153 | { |
| 154 | UNUSED(type); |
| 155 | |
| 156 | struct blob_attr *tb[1]; |
| 157 | struct blob_attr *cur = NULL; |
| 158 | unsigned int event; |
| 159 | MBTK_GNSS_5311_RESULT_TYPE* ubus_gnss_result = (MBTK_GNSS_5311_RESULT_TYPE *)req->priv; |
| 160 | int rc; |
| 161 | |
| 162 | /*parsing blob to be accessed easily with tb array - parse "1" argument*/ |
| 163 | rc = blobmsg_parse(mbtk_gnss_ubus_cb_policy, 1, tb, blob_data(msg), blob_len(msg)); |
| 164 | if (rc < 0) |
| 165 | { |
| 166 | LOGE("[mbtk_gnss_api] blobmsg_parse fail.rc = [%d]", rc); |
| 167 | *ubus_gnss_result = MBTK_GNSS_RESULT_FAIL; |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | /*parse first parameter*/ |
| 172 | cur = tb[0]; |
| 173 | if (!cur) |
| 174 | { |
| 175 | LOGE("[mbtk_gnss_api] cur is NULL."); |
| 176 | *ubus_gnss_result = MBTK_GNSS_RESULT_FAIL; |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | event = blobmsg_get_u32(cur); |
| 181 | LOGE("[mbtk_gnss_api] get event = [%d].", event); |
| 182 | |
| 183 | switch(event) |
| 184 | { |
| 185 | case ASR_GPS_INITIAL_SUCCESS: |
| 186 | case ASR_GPS_INITIALED: |
| 187 | { |
| 188 | *ubus_gnss_result = MBTK_GNSS_RESULT_OPEN_SUCCESS; |
| 189 | break; |
| 190 | } |
| 191 | case ASR_GPS_INITIAL_FAILED: |
| 192 | { |
| 193 | *ubus_gnss_result = MBTK_GNSS_RESULT_OPEN_FAIL; |
| 194 | break; |
| 195 | } |
| 196 | case ASR_GPS_DOWNLOAD_SUCCESS: |
| 197 | { |
| 198 | *ubus_gnss_result = MBTK_GNSS_RESULT_DOWNLOAD_SUCCESS; |
| 199 | break; |
| 200 | } |
| 201 | case ASR_GPS_DOWNLOAD_FAIL: |
| 202 | { |
| 203 | *ubus_gnss_result = MBTK_GNSS_RESULT_DOWNLOAD_FAIL; |
| 204 | break; |
| 205 | } |
| 206 | case ASR_GPS_SEND_DATA_SUCCESS: |
| 207 | { |
| 208 | *ubus_gnss_result = MBTK_GNSS_RESULT_SEND_SUCCESS; |
| 209 | break; |
| 210 | } |
| 211 | case ASR_GPS_SEND_DATA_FAIL: |
| 212 | { |
| 213 | *ubus_gnss_result = MBTK_GNSS_RESULT_SEND_FAIL; |
| 214 | break; |
| 215 | } |
| 216 | case ASR_GPS_DEINIT_SUCCESS: |
| 217 | { |
| 218 | *ubus_gnss_result = MBTK_GNSS_RESULT_CLOSE_SUCCESS; |
| 219 | break; |
| 220 | } |
| 221 | case ASR_GPS_DEINIT_FAIL: |
| 222 | { |
| 223 | *ubus_gnss_result = MBTK_GNSS_RESULT_CLOSE_FAIL; |
| 224 | break; |
| 225 | } |
| 226 | default: |
| 227 | { |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | static void mbtk_gnss_state_get_callback(struct ubus_request *req, int type, struct blob_attr *msg) |
| 236 | { |
| 237 | UNUSED(type); |
| 238 | |
| 239 | struct blob_attr *tb[1]; |
| 240 | struct blob_attr *cur; |
| 241 | char *gps_state = NULL; |
| 242 | int rc; |
| 243 | char *outString = (char *)req->priv; |
| 244 | |
| 245 | /*parsing blob to be accessed easily with tb array - parse "1" argument*/ |
| 246 | rc = blobmsg_parse(mbtk_gnss_state_resp_cb_policy, 1, tb, blob_data(msg), blob_len(msg)); |
| 247 | if (rc < 0) |
| 248 | { |
| 249 | LOGE("[mbtk_gnss_api] blobmsg_parse fail.rc = [%d]", rc); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | /*parse first parameter*/ |
| 254 | cur = tb[0]; |
| 255 | if (!cur) |
| 256 | { |
| 257 | LOGE("[mbtk_gnss_api] cur is NULL."); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | gps_state = blobmsg_get_string(cur); |
| 262 | LOGE("[mbtk_gnss_api] get status = [%s].", gps_state); |
| 263 | memset(outString, 0x0, 128); |
| 264 | memcpy(outString, gps_state, strlen(gps_state)); |
| 265 | |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | static void mbtk_gnss_open_port(int *fd_ptr, const char *file_path, int flag, int tty) |
| 270 | { |
| 271 | |
| 272 | int fd = -1; |
| 273 | |
| 274 | if((fd = open(file_path, flag)) < 0) |
| 275 | { |
| 276 | LOGE("[mbtk_gnss_api] Open %s fail errno = [%d].", file_path, errno); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | LOGE("[mbtk_gnss_api] Open %s success.", file_path); |
| 281 | if (tty) |
| 282 | { |
| 283 | /* set newtio */ |
| 284 | struct termios newtio; |
| 285 | memset(&newtio, 0, sizeof(newtio)); |
| 286 | //(void)fcntl(fd, F_SETFL, 0); |
| 287 | |
| 288 | /* no flow control for uart by default */ |
| 289 | newtio.c_cflag = BAUD | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD; |
| 290 | newtio.c_iflag = IGNPAR; |
| 291 | //newtio.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); |
| 292 | newtio.c_oflag = 0; |
| 293 | newtio.c_lflag = 0; /* disable ECHO, ICANON, etc... */ |
| 294 | |
| 295 | newtio.c_cc[VERASE] = 0x8; /* del */ |
| 296 | newtio.c_cc[VEOF] = 4; /* Ctrl-d */ |
| 297 | newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */ |
| 298 | newtio.c_cc[VEOL] = 0xD; /* '\0' */ |
| 299 | |
| 300 | tcflush(fd, TCIOFLUSH); |
| 301 | tcsetattr(fd, TCSANOW, &newtio); |
| 302 | } |
| 303 | |
| 304 | *fd_ptr = fd; |
| 305 | } |
| 306 | |
| 307 | static int epoll_deregister(int epoll_fd,int fd ) |
| 308 | { |
| 309 | int ret; |
| 310 | do { |
| 311 | ret = epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd, NULL ); |
| 312 | } while (ret < 0 && errno == EINTR); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static int epoll_register(int epoll_fd, int fd) |
| 317 | { |
| 318 | struct epoll_event ev; |
| 319 | int ret, flags; |
| 320 | |
| 321 | /* important: make the fd non-blocking */ |
| 322 | flags = fcntl(fd, F_GETFL); |
| 323 | fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
| 324 | |
| 325 | ev.events = EPOLLIN; |
| 326 | ev.data.fd = fd; |
| 327 | do { |
| 328 | ret = epoll_ctl( epoll_fd, EPOLL_CTL_ADD, fd, &ev ); |
| 329 | } while (ret < 0 && errno == EINTR); |
| 330 | |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | static void *gnss_nmea_thread(void* arg) |
| 336 | { |
| 337 | int epoll_fd= epoll_create(2); |
| 338 | int started = 0; |
| 339 | int nmea_fd = nmea_state.fd; |
| 340 | int control_fd = nmea_state.control[1]; |
| 341 | int i = 0; |
| 342 | int fd= -1; |
| 343 | int len = 0; |
| 344 | int offset = 0; |
| 345 | char nmea_buf[1025] = {0}; |
| 346 | struct epoll_event events[2]; |
| 347 | int ne, nevents; |
wangyouqiang | 7d66fb1 | 2024-01-26 19:19:00 +0800 | [diff] [blame^] | 348 | int ret = -1; |
| 349 | char cmd = 0; |
wangyouqiang | 4c2048e | 2024-01-04 13:39:23 +0800 | [diff] [blame] | 350 | char c; |
| 351 | |
| 352 | int pos = 0; |
| 353 | int overflow = 0; |
| 354 | char in[128] = {0}; |
| 355 | |
| 356 | /* register control file descriptors for polling */ |
| 357 | epoll_register( epoll_fd, control_fd ); |
| 358 | epoll_register( epoll_fd, nmea_fd ); |
| 359 | |
| 360 | LOGE("[mbtk_gnss_api] gnss_nmea_thread running"); |
| 361 | |
| 362 | for (;;) |
| 363 | { |
| 364 | nevents = -1; |
| 365 | nevents = epoll_wait( epoll_fd, events, 2, -1 ); |
| 366 | if (nevents < 0) |
| 367 | { |
| 368 | if (errno != EINTR) |
| 369 | { |
| 370 | LOGE("[mbtk_gnss_api] epoll_wait() unexpected error: %s", strerror(errno)); |
| 371 | } |
| 372 | continue; |
| 373 | } |
| 374 | |
| 375 | for (ne = 0; ne < nevents; ne++) |
| 376 | { |
| 377 | if ((events[ne].events & (EPOLLERR|EPOLLHUP)) != 0) |
| 378 | { |
| 379 | LOGE("[mbtk_gnss_api] EPOLLERR or EPOLLHUP after epoll_wait()!"); |
yq.wang | 8bf5656 | 2024-01-04 14:07:22 +0800 | [diff] [blame] | 380 | return NULL; |
wangyouqiang | 4c2048e | 2024-01-04 13:39:23 +0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | if ((events[ne].events & EPOLLIN) != 0) |
| 384 | { |
| 385 | fd = events[ne].data.fd; |
| 386 | |
| 387 | if (fd == control_fd) |
| 388 | { |
| 389 | do { |
| 390 | ret = read( fd, &cmd, 1 ); |
| 391 | } while (ret < 0 && errno == EINTR); |
wangyouqiang | 7d66fb1 | 2024-01-26 19:19:00 +0800 | [diff] [blame^] | 392 | LOGE("[mbtk_gnss_api] entry nmea thread quit cmd = [%d]!", cmd); |
wangyouqiang | 4c2048e | 2024-01-04 13:39:23 +0800 | [diff] [blame] | 393 | if (cmd == 1) |
| 394 | { |
| 395 | epoll_deregister( epoll_fd, control_fd ); |
| 396 | epoll_deregister( epoll_fd, nmea_fd ); |
| 397 | LOGE("[mbtk_gnss_api] gnss thread quitting on demand"); |
| 398 | return NULL; |
| 399 | } |
| 400 | } |
| 401 | else if (fd == nmea_fd) |
| 402 | { |
| 403 | memset(nmea_buf, 0x0, 1024); |
| 404 | len = read(fd, nmea_buf, 1024); |
| 405 | if(len > 0) |
| 406 | { |
| 407 | for(offset = 0; offset < len; offset++) |
| 408 | { |
| 409 | c = nmea_buf[offset]; |
| 410 | if(pos == 0 && c != '$') |
| 411 | { |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | if (overflow) { |
| 416 | overflow = (c != '\n'); |
| 417 | continue; |
| 418 | } |
| 419 | |
| 420 | if (pos >= (int) sizeof(in)-1 ) |
| 421 | { |
| 422 | overflow = 1; |
| 423 | pos = 0; |
| 424 | continue; |
| 425 | } |
| 426 | |
| 427 | in[pos] = c; |
| 428 | pos += 1; |
| 429 | |
| 430 | if (c == '\n') |
| 431 | { |
| 432 | if(nmea_state.callbacks != NULL) |
| 433 | { |
| 434 | nmea_state.callbacks((void *)in, pos); |
| 435 | } |
| 436 | memset(in, 0x0, pos); |
| 437 | pos = 0; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | else |
| 442 | { |
| 443 | LOGE("[mbtk_gnss_api] read() fail:%d, errno = %d\n", len, errno); |
| 444 | } |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | LOGE("[mbtk_gnss_api] epoll_wait() returned unkown fd %d ?", fd); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | static int mbtk_gnss_nmea_thread_init(void) |
| 456 | { |
| 457 | if(nmea_state.init == 1) |
| 458 | { |
| 459 | LOGE("[mbtk_gnss_api] nmea thread is open."); |
wangyouqiang | 7d66fb1 | 2024-01-26 19:19:00 +0800 | [diff] [blame^] | 460 | return MBTK_RESULT_SUCCESS; |
wangyouqiang | 4c2048e | 2024-01-04 13:39:23 +0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | mbtk_gnss_open_port(&nmea_state.fd, MBTK_GNSS_NMEA_PORT, O_RDWR | O_NONBLOCK | O_NOCTTY, 1); |
| 464 | |
| 465 | if (socketpair( AF_LOCAL, SOCK_STREAM, 0, nmea_state.control ) < 0 ) |
| 466 | { |
| 467 | LOGE("[mbtk_gnss_api] could not create thread control socket pair: %s", strerror(errno)); |
| 468 | |
| 469 | /*close the control socket pair && Retry again.*/ |
| 470 | if(nmea_state.control[0] > 0) |
| 471 | { |
| 472 | close( nmea_state.control[0] ); |
| 473 | nmea_state.control[0] = -1; |
| 474 | } |
| 475 | |
| 476 | if(nmea_state.control[1] > 0) |
| 477 | { |
| 478 | close( nmea_state.control[1] ); |
| 479 | nmea_state.control[1] = -1; |
| 480 | } |
| 481 | return MBTK_RESULT_FAIL; |
| 482 | } |
| 483 | |
| 484 | pthread_create(&nmea_state.thread, NULL, gnss_nmea_thread, NULL); |
| 485 | if ( !nmea_state.thread ) |
| 486 | { |
| 487 | LOGE("[mbtk_gnss_api] could not create gps thread: %s", strerror(errno)); |
| 488 | return MBTK_RESULT_FAIL; |
| 489 | } |
| 490 | |
| 491 | nmea_state.gnss_msg_state = MBTK_GNSS_MSG_NMEA_INFO; |
| 492 | nmea_state.init = 1; |
| 493 | return MBTK_RESULT_SUCCESS; |
| 494 | } |
| 495 | |
| 496 | static int mbtk_gnss_nmea_thread_deinit(void) |
| 497 | { |
| 498 | // tell the thread to quit, and wait for it |
| 499 | if(nmea_state.init == 1) |
| 500 | { |
| 501 | char cmd = 1; |
| 502 | void* dummy = NULL; |
| 503 | write( nmea_state.control[0], &cmd, 1 ); |
| 504 | pthread_join(nmea_state.thread, &dummy); |
| 505 | |
| 506 | // close the control socket pair |
| 507 | if(nmea_state.control[0] > 0) |
| 508 | { |
| 509 | close( nmea_state.control[0] ); |
| 510 | nmea_state.control[0] = -1; |
| 511 | } |
| 512 | if(nmea_state.control[1] > 0) |
| 513 | { |
| 514 | close( nmea_state.control[1] ); |
| 515 | nmea_state.control[1] = -1; |
| 516 | } |
| 517 | |
| 518 | LOGE("[mbtk_gnss_api] %s: deinit", __FUNCTION__); |
| 519 | |
| 520 | // close connection to the QEMU GPS daemon |
| 521 | if(nmea_state.fd) |
| 522 | { |
| 523 | close(nmea_state.fd); |
| 524 | nmea_state.fd = -1; |
| 525 | } |
| 526 | |
| 527 | nmea_state.callbacks = NULL; |
| 528 | nmea_state.gnss_msg_state = MBTK_GNSS_MSG_NMEA_INFO; |
| 529 | nmea_state.init = 0; |
| 530 | } |
| 531 | |
| 532 | return MBTK_RESULT_SUCCESS; |
| 533 | } |
| 534 | |
| 535 | int mbtk_invoke_reply_data_cb(const char *service, const char *method, struct blob_attr *msg, |
| 536 | ubus_data_handler_t cb, void *cb_param, int timeout) |
| 537 | { |
| 538 | struct ubus_context *ctx = NULL; |
| 539 | int rc = -1; |
| 540 | uint32_t id; |
| 541 | struct ubus_request req; |
| 542 | |
| 543 | ctx = mbtk_get_ubus_ctx(); |
| 544 | if(ctx == NULL) |
| 545 | { |
| 546 | LOGE("[mbtk_gnss_api] mbtk_get_ubus_ctx fail."); |
| 547 | return MBTK_RESULT_FAIL; |
| 548 | } |
| 549 | |
| 550 | /* Look up the target object id by the object path */ |
| 551 | rc = ubus_lookup_id(ctx, service, &id); |
| 552 | if(rc) |
| 553 | { |
| 554 | LOGE("[mbtk_gnss_api] ubus_lookup_id fail.rc = [%d]", rc); |
| 555 | return MBTK_RESULT_FAIL; |
| 556 | } |
| 557 | |
| 558 | rc = ubus_invoke(ctx, id, method, msg, cb, cb_param, timeout); |
| 559 | if(rc) |
| 560 | { |
| 561 | LOGE("[mbtk_gnss_api] ubus_invoke fail.rc = [%d]", rc); |
| 562 | return MBTK_RESULT_FAIL; |
| 563 | } |
| 564 | return MBTK_RESULT_SUCCESS; |
| 565 | } |
| 566 | |
| 567 | int mbtk_invoke_noreply(const char *service, const char *method, struct blob_attr *msg) |
| 568 | { |
| 569 | struct ubus_context *ctx; |
| 570 | int rc = -1; |
| 571 | uint32_t id; |
| 572 | struct ubus_request req; |
| 573 | |
| 574 | ctx = mbtk_get_ubus_ctx(); |
| 575 | if(ctx == NULL) |
| 576 | { |
| 577 | LOGE("[mbtk_gnss_api] mbtk_get_ubus_ctx fail."); |
| 578 | return MBTK_RESULT_FAIL; |
| 579 | } |
| 580 | /* Look up the target object id by the object path */ |
| 581 | rc = ubus_lookup_id(ctx, service, &id); |
| 582 | if(rc) |
| 583 | { |
| 584 | LOGE("[mbtk_gnss_api] ubus_lookup_id fail.rc = [%d]", rc); |
| 585 | return MBTK_RESULT_FAIL; |
| 586 | } |
| 587 | |
| 588 | rc = ubus_invoke_async(ctx, id, method, msg, &req); |
| 589 | if(rc) |
| 590 | { |
| 591 | LOGE("[mbtk_gnss_api] ubus_invoke_async fail.rc = [%d]", rc); |
| 592 | return MBTK_RESULT_FAIL; |
| 593 | } |
| 594 | |
| 595 | /* cancel req (on client side) because noreply is needed */ |
| 596 | ubus_abort_request(ctx, &req); |
| 597 | return MBTK_RESULT_SUCCESS; |
| 598 | } |
| 599 | |
| 600 | /**********************************FUNC***********************************/ |
| 601 | |
| 602 | /**********************************API***********************************/ |
| 603 | MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_init(void) |
| 604 | { |
| 605 | int ret = MBTK_RESULT_FAIL; |
| 606 | |
| 607 | ret = mbtk_gnss_ubus_uloop_init(); |
| 608 | if(ret < 0) |
| 609 | { |
| 610 | LOGE("[mbtk_gnss_api] mbtk_gnss_uloopinit fail."); |
| 611 | return MBTK_GNSS_RESULT_FAIL; |
| 612 | } |
| 613 | |
| 614 | ret = mbtk_gnss_nmea_thread_init(); |
| 615 | if(ret != 0) |
| 616 | { |
| 617 | LOGE("[mbtk_gnss_api] mbtk_gnss_nmea_thread_init fail."); |
| 618 | return MBTK_GNSS_RESULT_FAIL; |
| 619 | } |
wangyouqiang | 7d66fb1 | 2024-01-26 19:19:00 +0800 | [diff] [blame^] | 620 | |
wangyouqiang | 4c2048e | 2024-01-04 13:39:23 +0800 | [diff] [blame] | 621 | return MBTK_GNSS_RESULT_SUCCESS; |
| 622 | } |
| 623 | |
| 624 | MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_deinit(void) |
| 625 | { |
| 626 | mbtk_gnss_nmea_thread_deinit(); |
| 627 | mbtk_gnss_ubus_uloop_deinit(); |
| 628 | return MBTK_GNSS_RESULT_SUCCESS; |
| 629 | } |
| 630 | |
| 631 | |
| 632 | MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_open(void) |
| 633 | { |
| 634 | if (mbtk_gnss_uloop_init == 0) |
| 635 | { |
| 636 | LOGE("[mbtk_gnss_api] gnss not init."); |
| 637 | return MBTK_GNSS_RESULT_FAIL; |
| 638 | } |
| 639 | |
| 640 | int ret = -1; |
| 641 | MBTK_GNSS_5311_RESULT_TYPE ubus_gnss_result = MBTK_GNSS_RESULT_SUCCESS; |
| 642 | uint gps_init_val = MBTK_GNSS_OPEN; |
| 643 | struct blob_buf outBlob; |
| 644 | memset(&outBlob, 0, sizeof(outBlob)); |
| 645 | |
| 646 | blob_buf_init(&outBlob, 0); |
| 647 | blobmsg_add_u32(&outBlob, MBTK_GNSS_UBUS_INIT_PARAM, gps_init_val); |
| 648 | |
| 649 | //UBUS_STATUS_OK |
| 650 | ret = mbtk_invoke_reply_data_cb(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_INIT, outBlob.head, |
wangyouqiang | 7d66fb1 | 2024-01-26 19:19:00 +0800 | [diff] [blame^] | 651 | (ubus_data_handler_t *)mbtk_gnss_ubus_result_callback, &ubus_gnss_result, 25000); |
wangyouqiang | 4c2048e | 2024-01-04 13:39:23 +0800 | [diff] [blame] | 652 | blob_buf_free(&outBlob); |
| 653 | if (ret != 0) |
| 654 | { |
| 655 | LOGE("[mbtk_gnss_api] mbtk_invoke_reply_data_cb fail."); |
| 656 | return MBTK_GNSS_RESULT_FAIL; |
| 657 | } |
| 658 | |
| 659 | if(ubus_gnss_result != MBTK_GNSS_RESULT_OPEN_SUCCESS) |
| 660 | { |
| 661 | LOGE("[mbtk_gnss_api] ubus_gnss_result = [%d].", ubus_gnss_result); |
| 662 | return ubus_gnss_result; |
| 663 | } |
| 664 | |
| 665 | |
| 666 | mbtk_gnss_status = MBTK_GNSS_OPEN; |
| 667 | return MBTK_GNSS_RESULT_SUCCESS; |
| 668 | } |
| 669 | |
| 670 | MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_close(void) |
| 671 | { |
| 672 | if (mbtk_gnss_uloop_init == 0) |
| 673 | { |
| 674 | LOGE("[mbtk_gnss_api] gnss not init."); |
| 675 | return MBTK_GNSS_RESULT_FAIL; |
| 676 | } |
| 677 | |
| 678 | int ret = -1; |
| 679 | MBTK_GNSS_5311_RESULT_TYPE ubus_gnss_result = MBTK_GNSS_RESULT_SUCCESS; |
| 680 | uint gps_init_val = MBTK_GNSS_CLOSE; |
| 681 | struct blob_buf outBlob; |
| 682 | memset(&outBlob, 0, sizeof(outBlob)); |
| 683 | |
| 684 | blob_buf_init(&outBlob, 0); |
| 685 | blobmsg_add_u32(&outBlob, MBTK_GNSS_UBUS_INIT_PARAM, gps_init_val); |
| 686 | |
| 687 | //UBUS_STATUS_OK |
| 688 | ret = mbtk_invoke_reply_data_cb(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_INIT, outBlob.head, |
| 689 | (ubus_data_handler_t *)mbtk_gnss_ubus_result_callback, &ubus_gnss_result, 8000); |
| 690 | blob_buf_free(&outBlob); |
| 691 | if (ret != 0) |
| 692 | { |
| 693 | LOGE("[mbtk_gnss_api] mbtk_invoke_reply_data_cb fail."); |
| 694 | return MBTK_GNSS_RESULT_FAIL; |
| 695 | } |
| 696 | |
| 697 | if(ubus_gnss_result != MBTK_GNSS_RESULT_CLOSE_SUCCESS) |
| 698 | { |
| 699 | LOGE("[mbtk_gnss_api] ubus_gnss_result = [%d].", ubus_gnss_result); |
| 700 | return ubus_gnss_result; |
| 701 | } |
| 702 | |
| 703 | |
| 704 | |
| 705 | mbtk_gnss_status = MBTK_GNSS_CLOSE; |
| 706 | return MBTK_GNSS_RESULT_SUCCESS; |
| 707 | } |
| 708 | |
| 709 | MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_sleep(void) |
| 710 | { |
| 711 | if (mbtk_gnss_uloop_init == 0) |
| 712 | { |
| 713 | LOGE("[mbtk_gnss_api] gnss not init."); |
| 714 | return MBTK_GNSS_RESULT_FAIL; |
| 715 | } |
| 716 | |
| 717 | if(mbtk_gnss_status == MBTK_GNSS_CLOSE) |
| 718 | { |
| 719 | LOGE("[mbtk_gnss_api] gnss not open."); |
| 720 | return MBTK_GNSS_RESULT_FAIL; |
| 721 | } |
| 722 | |
| 723 | int ret = -1; |
| 724 | struct blob_buf outBlob; |
| 725 | unsigned int gps_sleep_val = MBTK_GNSS_SLEEP; |
| 726 | memset(&outBlob, 0, sizeof(outBlob)); |
| 727 | |
| 728 | blob_buf_init(&outBlob, 0); |
| 729 | blobmsg_add_u32(&outBlob, MBTK_GNSS_UBUS_SLEEP_PARAM, (unsigned int)gps_sleep_val); |
| 730 | |
| 731 | ret = mbtk_invoke_noreply(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_SLEEP, outBlob.head); |
| 732 | blob_buf_free(&outBlob); |
| 733 | if (ret != 0) |
| 734 | { |
| 735 | LOGE("[mbtk_gnss_api] mbtk_invoke_noreply fail."); |
| 736 | return MBTK_GNSS_RESULT_FAIL; |
| 737 | } |
| 738 | |
| 739 | return MBTK_GNSS_RESULT_SUCCESS; |
| 740 | } |
| 741 | |
| 742 | MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_wakeup(void) |
| 743 | { |
| 744 | if (mbtk_gnss_uloop_init == 0) |
| 745 | { |
| 746 | LOGE("[mbtk_gnss_api] gnss not init."); |
| 747 | return MBTK_GNSS_RESULT_FAIL; |
| 748 | } |
| 749 | |
| 750 | if(mbtk_gnss_status == MBTK_GNSS_CLOSE) |
| 751 | { |
| 752 | LOGE("[mbtk_gnss_api] gnss not open."); |
| 753 | return MBTK_GNSS_RESULT_FAIL; |
| 754 | } |
| 755 | |
| 756 | int ret = -1; |
| 757 | struct blob_buf outBlob; |
| 758 | unsigned int gps_sleep_val = MBTK_GNSS_WAKEUP; |
| 759 | memset(&outBlob, 0, sizeof(outBlob)); |
| 760 | |
| 761 | blob_buf_init(&outBlob, 0); |
| 762 | blobmsg_add_u32(&outBlob, MBTK_GNSS_UBUS_SLEEP_PARAM, (unsigned int)gps_sleep_val); |
| 763 | |
| 764 | ret = mbtk_invoke_noreply(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_SLEEP, outBlob.head); |
| 765 | blob_buf_free(&outBlob); |
| 766 | if (ret != 0) |
| 767 | { |
| 768 | LOGE("[mbtk_gnss_api] mbtk_invoke_noreply fail."); |
| 769 | return MBTK_GNSS_RESULT_FAIL; |
| 770 | } |
| 771 | |
| 772 | return MBTK_GNSS_RESULT_SUCCESS; |
| 773 | } |
| 774 | |
| 775 | MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_param_config(const char *param_buf, int param_buf_len) |
| 776 | { |
| 777 | if (mbtk_gnss_uloop_init == 0) |
| 778 | { |
| 779 | LOGE("[mbtk_gnss_api] gnss not init."); |
| 780 | return MBTK_GNSS_RESULT_FAIL; |
| 781 | } |
| 782 | |
| 783 | if(mbtk_gnss_status == MBTK_GNSS_CLOSE) |
| 784 | { |
| 785 | LOGE("[mbtk_gnss_api] gnss not open."); |
| 786 | return MBTK_GNSS_RESULT_FAIL; |
| 787 | } |
| 788 | |
| 789 | if(param_buf == NULL || param_buf_len <= 0) |
| 790 | { |
| 791 | LOGE("[mbtk_gnss_api] param is error."); |
| 792 | return MBTK_GNSS_RESULT_FAIL; |
| 793 | } |
| 794 | |
| 795 | int ret = -1; |
| 796 | struct blob_buf outBlob; |
| 797 | memset(&outBlob, 0, sizeof(outBlob)); |
| 798 | |
| 799 | LOGE("[mbtk_gnss_api] set command [%s].", param_buf); |
| 800 | blob_buf_init(&outBlob, 0); |
| 801 | blobmsg_add_string(&outBlob, MBTK_GNSS_UBUS_SET_PARAM, param_buf); |
| 802 | |
| 803 | ret = mbtk_invoke_noreply(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_SET, outBlob.head); |
| 804 | blob_buf_free(&outBlob); |
| 805 | if (ret != 0) |
| 806 | { |
| 807 | LOGE("[mbtk_gnss_api] mbtk_invoke_noreply fail."); |
| 808 | return MBTK_GNSS_RESULT_FAIL; |
| 809 | } |
| 810 | |
| 811 | return MBTK_GNSS_RESULT_SUCCESS; |
| 812 | } |
| 813 | |
| 814 | MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_get_status(const char *status_buf, int status_buf_len, int *get_status_len) |
| 815 | { |
| 816 | if (mbtk_gnss_uloop_init == 0) |
| 817 | { |
| 818 | LOGE("[mbtk_gnss_api] gnss not init."); |
| 819 | return MBTK_GNSS_RESULT_FAIL; |
| 820 | } |
| 821 | |
| 822 | if(mbtk_gnss_status == MBTK_GNSS_CLOSE) |
| 823 | { |
| 824 | LOGE("[mbtk_gnss_api] gnss not open."); |
| 825 | return MBTK_GNSS_RESULT_FAIL; |
| 826 | } |
| 827 | |
| 828 | int ret = -1; |
| 829 | char status[128] = {0}; |
| 830 | int status_len = 0; |
| 831 | |
| 832 | ret = mbtk_invoke_reply_data_cb(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_GET_STATUS, NULL, mbtk_gnss_state_get_callback, status, 4000); |
| 833 | if (ret != 0) |
| 834 | { |
| 835 | LOGE("[mbtk_gnss_api] mbtk_invoke_reply_data_cb fail."); |
| 836 | return MBTK_GNSS_RESULT_FAIL; |
| 837 | } |
| 838 | |
| 839 | status_len = strlen(status); |
| 840 | if(status_len > 0 && status_len < status_buf_len) |
| 841 | { |
| 842 | memcpy(status_buf, status, status_len); |
| 843 | *get_status_len = status_len; |
| 844 | } |
| 845 | else |
| 846 | { |
| 847 | LOGE("[mbtk_gnss_api] status_len[%d] error.", status_len); |
| 848 | return MBTK_GNSS_RESULT_FAIL; |
| 849 | } |
| 850 | |
| 851 | return MBTK_GNSS_RESULT_SUCCESS; |
| 852 | } |
| 853 | |
| 854 | MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_set_nmea_out_type(MBTK_GNSS_MSG_INFO_TYPE type) |
| 855 | { |
| 856 | if(mbtk_gnss_uloop_init == 0) |
| 857 | { |
| 858 | LOGE("[mbtk_gnss_api] gnss not init."); |
| 859 | return MBTK_GNSS_RESULT_FAIL; |
| 860 | } |
| 861 | |
| 862 | if(type == MBTK_GNSS_MSG_LOCATION_INFO) |
| 863 | { |
| 864 | nmea_state.gnss_msg_state = type; |
| 865 | } |
| 866 | else if(type == MBTK_GNSS_MSG_NMEA_INFO) |
| 867 | { |
| 868 | nmea_state.gnss_msg_state = type; |
| 869 | } |
| 870 | else |
| 871 | { |
| 872 | return MBTK_GNSS_RESULT_FAIL; |
| 873 | } |
| 874 | |
| 875 | return MBTK_GNSS_RESULT_SUCCESS; |
| 876 | } |
| 877 | |
| 878 | |
| 879 | MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_add_nmea_out_func(mbtk_gnss_nmea_func_t cb) |
| 880 | { |
| 881 | if(mbtk_gnss_uloop_init == 0) |
| 882 | { |
| 883 | LOGE("[mbtk_gnss_api] gnss not init."); |
| 884 | return MBTK_GNSS_RESULT_FAIL; |
| 885 | } |
| 886 | |
| 887 | if(cb == NULL) |
| 888 | { |
| 889 | LOGE("[mbtk_gnss_api] cb is NULL."); |
| 890 | return MBTK_GNSS_RESULT_FAIL; |
| 891 | } |
| 892 | nmea_state.callbacks = cb; |
| 893 | |
| 894 | return MBTK_GNSS_RESULT_SUCCESS; |
| 895 | } |
| 896 | |
wangyouqiang | 7d66fb1 | 2024-01-26 19:19:00 +0800 | [diff] [blame^] | 897 | MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_5311_download_tle(char *host, int alam_flag) |
| 898 | { |
| 899 | if (mbtk_gnss_uloop_init == 0) |
| 900 | { |
| 901 | LOGE("[mbtk_gnss_api] gnss not init."); |
| 902 | return MBTK_GNSS_RESULT_DOWNLOAD_FAIL; |
| 903 | } |
| 904 | |
| 905 | if(mbtk_gnss_status == MBTK_GNSS_CLOSE) |
| 906 | { |
| 907 | LOGE("[mbtk_gnss_api] gnss not open."); |
| 908 | return MBTK_GNSS_RESULT_DOWNLOAD_FAIL; |
| 909 | } |
| 910 | |
| 911 | int ret = -1; |
| 912 | MBTK_GNSS_5311_RESULT_TYPE ubus_gnss_result = MBTK_GNSS_RESULT_DOWNLOAD_SUCCESS; |
| 913 | struct blob_buf outBlob; |
| 914 | memset(&outBlob, 0, sizeof(outBlob)); |
| 915 | |
| 916 | blob_buf_init(&outBlob, 0); |
| 917 | blobmsg_add_string(&outBlob, MBTK_GNSS_UBUS_AGPS_SERVER, host); |
| 918 | blobmsg_add_u32(&outBlob, MBTK_GNSS_UBUS_ALAM_FLAG, alam_flag); |
| 919 | |
| 920 | //UBUS_STATUS_OK |
| 921 | ret = mbtk_invoke_reply_data_cb(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_GET_AGPS, outBlob.head, |
| 922 | (ubus_data_handler_t *)mbtk_gnss_ubus_result_callback, &ubus_gnss_result, 8000); |
| 923 | blob_buf_free(&outBlob); |
| 924 | if (ret != 0) |
| 925 | { |
| 926 | LOGE("[mbtk_gnss_api] mbtk_invoke_reply_data_cb fail."); |
| 927 | return MBTK_GNSS_RESULT_DOWNLOAD_FAIL; |
| 928 | } |
| 929 | |
| 930 | if(ubus_gnss_result != MBTK_GNSS_RESULT_DOWNLOAD_SUCCESS) |
| 931 | { |
| 932 | LOGE("[mbtk_gnss_api] ubus_gnss_result = [%d].", ubus_gnss_result); |
| 933 | return MBTK_GNSS_RESULT_DOWNLOAD_FAIL; |
| 934 | } |
| 935 | |
| 936 | return MBTK_GNSS_RESULT_DOWNLOAD_SUCCESS; |
| 937 | } |
| 938 | |
| 939 | MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_injectEphemeris(void) |
| 940 | { |
| 941 | if (mbtk_gnss_uloop_init == 0) |
| 942 | { |
| 943 | LOGE("[mbtk_gnss_api] gnss not init."); |
| 944 | return MBTK_GNSS_RESULT_SEND_FAIL; |
| 945 | } |
| 946 | |
| 947 | if(mbtk_gnss_status == MBTK_GNSS_CLOSE) |
| 948 | { |
| 949 | LOGE("[mbtk_gnss_api] gnss not open."); |
| 950 | return MBTK_GNSS_RESULT_SEND_FAIL; |
| 951 | } |
| 952 | |
| 953 | int ret = -1; |
| 954 | MBTK_GNSS_5311_RESULT_TYPE ubus_gnss_result = MBTK_GNSS_RESULT_SUCCESS; |
| 955 | |
| 956 | //UBUS_STATUS_OK |
| 957 | ret = mbtk_invoke_reply_data_cb(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_SET_AGPS, NULL, |
| 958 | (ubus_data_handler_t *)mbtk_gnss_ubus_result_callback, &ubus_gnss_result, 8000); |
| 959 | if (ret != 0) |
| 960 | { |
| 961 | LOGE("[mbtk_gnss_api] mbtk_invoke_reply_data_cb fail."); |
| 962 | return MBTK_GNSS_RESULT_SEND_FAIL; |
| 963 | } |
| 964 | |
| 965 | if(ubus_gnss_result != MBTK_GNSS_RESULT_SEND_SUCCESS) |
| 966 | { |
| 967 | LOGE("[mbtk_gnss_api] ubus_gnss_result = [%d].", ubus_gnss_result); |
| 968 | return MBTK_GNSS_RESULT_SEND_FAIL; |
| 969 | } |
| 970 | |
| 971 | return MBTK_GNSS_RESULT_SEND_SUCCESS; |
| 972 | } |
wangyouqiang | 4c2048e | 2024-01-04 13:39:23 +0800 | [diff] [blame] | 973 | |
| 974 | /**********************************API***********************************/ |
| 975 | |
| 976 | #endif |