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