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