blob: 64d1fdc7ed7cff86995811ceba8272725d353b8c [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>
b.liu12eaf8d2023-12-19 14:02:37 +080022
wangyouqiang4c2048e2024-01-04 13:39:23 +080023#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"
wangyouqiang7d66fb12024-01-26 19:19:00 +080037#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"
wangyouqiang4c2048e2024-01-04 13:39:23 +080041
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***********************************/
61const struct blobmsg_policy mbtk_gnss_ubus_cb_policy[] = {
62 [0] = {
63 .name = "event",
64 .type = BLOBMSG_TYPE_INT32,
65 },
66};
67
68const struct blobmsg_policy mbtk_gnss_state_resp_cb_policy[] = {
69 [0] = {
70 .name = "gps_state_resp",
71 .type = BLOBMSG_TYPE_STRING,
72 },
73};
74
75static int mbtk_gnss_uloop_init = 0;
76static struct ubus_context *mbtk_gnss_ctx = NULL;
77static int mbtk_gnss_status = MBTK_GNSS_CLOSE;
78static mbtk_gnss_nmea_status nmea_state = {0};
79/**********************************VARIABLE***********************************/
80
81/**********************************FUNC***********************************/
82struct 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
104int 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
136int 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
152static 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
235static 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
269static 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
307static 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
316static 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
335static 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;
wangyouqiang7d66fb12024-01-26 19:19:00 +0800348 int ret = -1;
349 char cmd = 0;
wangyouqiang4c2048e2024-01-04 13:39:23 +0800350 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.wang8bf56562024-01-04 14:07:22 +0800380 return NULL;
wangyouqiang4c2048e2024-01-04 13:39:23 +0800381 }
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);
wangyouqiang7d66fb12024-01-26 19:19:00 +0800392 LOGE("[mbtk_gnss_api] entry nmea thread quit cmd = [%d]!", cmd);
wangyouqiang4c2048e2024-01-04 13:39:23 +0800393 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
455static int mbtk_gnss_nmea_thread_init(void)
456{
457 if(nmea_state.init == 1)
458 {
459 LOGE("[mbtk_gnss_api] nmea thread is open.");
wangyouqiang7d66fb12024-01-26 19:19:00 +0800460 return MBTK_RESULT_SUCCESS;
wangyouqiang4c2048e2024-01-04 13:39:23 +0800461 }
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
496static 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
535int 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***********************************/
603MBTK_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
yq.wang854cec52024-06-08 03:53:54 -0700614#ifndef MBTK_SG_SUPPORT
wangyouqiang4c2048e2024-01-04 13:39:23 +0800615 ret = mbtk_gnss_nmea_thread_init();
616 if(ret != 0)
617 {
618 LOGE("[mbtk_gnss_api] mbtk_gnss_nmea_thread_init fail.");
619 return MBTK_GNSS_RESULT_FAIL;
620 }
yq.wang854cec52024-06-08 03:53:54 -0700621#endif
wangyouqiang4c2048e2024-01-04 13:39:23 +0800622 return MBTK_GNSS_RESULT_SUCCESS;
623}
624
625MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_deinit(void)
626{
627 mbtk_gnss_nmea_thread_deinit();
628 mbtk_gnss_ubus_uloop_deinit();
629 return MBTK_GNSS_RESULT_SUCCESS;
630}
631
632
633MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_open(void)
634{
635 if (mbtk_gnss_uloop_init == 0)
636 {
637 LOGE("[mbtk_gnss_api] gnss not init.");
638 return MBTK_GNSS_RESULT_FAIL;
639 }
640
641 int ret = -1;
642 MBTK_GNSS_5311_RESULT_TYPE ubus_gnss_result = MBTK_GNSS_RESULT_SUCCESS;
643 uint gps_init_val = MBTK_GNSS_OPEN;
644 struct blob_buf outBlob;
645 memset(&outBlob, 0, sizeof(outBlob));
646
647 blob_buf_init(&outBlob, 0);
648 blobmsg_add_u32(&outBlob, MBTK_GNSS_UBUS_INIT_PARAM, gps_init_val);
649
650 //UBUS_STATUS_OK
651 ret = mbtk_invoke_reply_data_cb(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_INIT, outBlob.head,
wangyouqiang7d66fb12024-01-26 19:19:00 +0800652 (ubus_data_handler_t *)mbtk_gnss_ubus_result_callback, &ubus_gnss_result, 25000);
wangyouqiang4c2048e2024-01-04 13:39:23 +0800653 blob_buf_free(&outBlob);
654 if (ret != 0)
655 {
656 LOGE("[mbtk_gnss_api] mbtk_invoke_reply_data_cb fail.");
657 return MBTK_GNSS_RESULT_FAIL;
658 }
659
660 if(ubus_gnss_result != MBTK_GNSS_RESULT_OPEN_SUCCESS)
661 {
662 LOGE("[mbtk_gnss_api] ubus_gnss_result = [%d].", ubus_gnss_result);
663 return ubus_gnss_result;
664 }
665
666
667 mbtk_gnss_status = MBTK_GNSS_OPEN;
668 return MBTK_GNSS_RESULT_SUCCESS;
669}
670
671MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_close(void)
672{
673 if (mbtk_gnss_uloop_init == 0)
674 {
675 LOGE("[mbtk_gnss_api] gnss not init.");
676 return MBTK_GNSS_RESULT_FAIL;
677 }
678
679 int ret = -1;
680 MBTK_GNSS_5311_RESULT_TYPE ubus_gnss_result = MBTK_GNSS_RESULT_SUCCESS;
681 uint gps_init_val = MBTK_GNSS_CLOSE;
682 struct blob_buf outBlob;
683 memset(&outBlob, 0, sizeof(outBlob));
684
685 blob_buf_init(&outBlob, 0);
686 blobmsg_add_u32(&outBlob, MBTK_GNSS_UBUS_INIT_PARAM, gps_init_val);
687
688 //UBUS_STATUS_OK
689 ret = mbtk_invoke_reply_data_cb(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_INIT, outBlob.head,
690 (ubus_data_handler_t *)mbtk_gnss_ubus_result_callback, &ubus_gnss_result, 8000);
691 blob_buf_free(&outBlob);
692 if (ret != 0)
693 {
694 LOGE("[mbtk_gnss_api] mbtk_invoke_reply_data_cb fail.");
695 return MBTK_GNSS_RESULT_FAIL;
696 }
697
698 if(ubus_gnss_result != MBTK_GNSS_RESULT_CLOSE_SUCCESS)
699 {
700 LOGE("[mbtk_gnss_api] ubus_gnss_result = [%d].", ubus_gnss_result);
701 return ubus_gnss_result;
702 }
703
704
705
706 mbtk_gnss_status = MBTK_GNSS_CLOSE;
707 return MBTK_GNSS_RESULT_SUCCESS;
708}
709
710MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_sleep(void)
711{
712 if (mbtk_gnss_uloop_init == 0)
713 {
714 LOGE("[mbtk_gnss_api] gnss not init.");
715 return MBTK_GNSS_RESULT_FAIL;
716 }
717
718 if(mbtk_gnss_status == MBTK_GNSS_CLOSE)
719 {
720 LOGE("[mbtk_gnss_api] gnss not open.");
721 return MBTK_GNSS_RESULT_FAIL;
722 }
723
724 int ret = -1;
725 struct blob_buf outBlob;
726 unsigned int gps_sleep_val = MBTK_GNSS_SLEEP;
727 memset(&outBlob, 0, sizeof(outBlob));
728
729 blob_buf_init(&outBlob, 0);
730 blobmsg_add_u32(&outBlob, MBTK_GNSS_UBUS_SLEEP_PARAM, (unsigned int)gps_sleep_val);
731
732 ret = mbtk_invoke_noreply(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_SLEEP, outBlob.head);
733 blob_buf_free(&outBlob);
734 if (ret != 0)
735 {
736 LOGE("[mbtk_gnss_api] mbtk_invoke_noreply fail.");
737 return MBTK_GNSS_RESULT_FAIL;
738 }
739
740 return MBTK_GNSS_RESULT_SUCCESS;
741}
742
743MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_wakeup(void)
744{
745 if (mbtk_gnss_uloop_init == 0)
746 {
747 LOGE("[mbtk_gnss_api] gnss not init.");
748 return MBTK_GNSS_RESULT_FAIL;
749 }
750
751 if(mbtk_gnss_status == MBTK_GNSS_CLOSE)
752 {
753 LOGE("[mbtk_gnss_api] gnss not open.");
754 return MBTK_GNSS_RESULT_FAIL;
755 }
756
757 int ret = -1;
758 struct blob_buf outBlob;
759 unsigned int gps_sleep_val = MBTK_GNSS_WAKEUP;
760 memset(&outBlob, 0, sizeof(outBlob));
761
762 blob_buf_init(&outBlob, 0);
763 blobmsg_add_u32(&outBlob, MBTK_GNSS_UBUS_SLEEP_PARAM, (unsigned int)gps_sleep_val);
764
765 ret = mbtk_invoke_noreply(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_SLEEP, outBlob.head);
766 blob_buf_free(&outBlob);
767 if (ret != 0)
768 {
769 LOGE("[mbtk_gnss_api] mbtk_invoke_noreply fail.");
770 return MBTK_GNSS_RESULT_FAIL;
771 }
772
773 return MBTK_GNSS_RESULT_SUCCESS;
774}
775
776MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_param_config(const char *param_buf, int param_buf_len)
777{
778 if (mbtk_gnss_uloop_init == 0)
779 {
780 LOGE("[mbtk_gnss_api] gnss not init.");
781 return MBTK_GNSS_RESULT_FAIL;
782 }
783
784 if(mbtk_gnss_status == MBTK_GNSS_CLOSE)
785 {
786 LOGE("[mbtk_gnss_api] gnss not open.");
787 return MBTK_GNSS_RESULT_FAIL;
788 }
789
790 if(param_buf == NULL || param_buf_len <= 0)
791 {
792 LOGE("[mbtk_gnss_api] param is error.");
793 return MBTK_GNSS_RESULT_FAIL;
794 }
795
796 int ret = -1;
797 struct blob_buf outBlob;
798 memset(&outBlob, 0, sizeof(outBlob));
799
800 LOGE("[mbtk_gnss_api] set command [%s].", param_buf);
801 blob_buf_init(&outBlob, 0);
802 blobmsg_add_string(&outBlob, MBTK_GNSS_UBUS_SET_PARAM, param_buf);
803
804 ret = mbtk_invoke_noreply(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_SET, outBlob.head);
805 blob_buf_free(&outBlob);
806 if (ret != 0)
807 {
808 LOGE("[mbtk_gnss_api] mbtk_invoke_noreply fail.");
809 return MBTK_GNSS_RESULT_FAIL;
810 }
811
812 return MBTK_GNSS_RESULT_SUCCESS;
813}
814
815MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_get_status(const char *status_buf, int status_buf_len, int *get_status_len)
816{
817 if (mbtk_gnss_uloop_init == 0)
818 {
819 LOGE("[mbtk_gnss_api] gnss not init.");
820 return MBTK_GNSS_RESULT_FAIL;
821 }
822
823 if(mbtk_gnss_status == MBTK_GNSS_CLOSE)
824 {
825 LOGE("[mbtk_gnss_api] gnss not open.");
826 return MBTK_GNSS_RESULT_FAIL;
827 }
828
829 int ret = -1;
830 char status[128] = {0};
831 int status_len = 0;
832
833 ret = mbtk_invoke_reply_data_cb(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_GET_STATUS, NULL, mbtk_gnss_state_get_callback, status, 4000);
834 if (ret != 0)
835 {
836 LOGE("[mbtk_gnss_api] mbtk_invoke_reply_data_cb fail.");
837 return MBTK_GNSS_RESULT_FAIL;
838 }
839
840 status_len = strlen(status);
841 if(status_len > 0 && status_len < status_buf_len)
842 {
843 memcpy(status_buf, status, status_len);
844 *get_status_len = status_len;
845 }
846 else
847 {
848 LOGE("[mbtk_gnss_api] status_len[%d] error.", status_len);
849 return MBTK_GNSS_RESULT_FAIL;
850 }
851
852 return MBTK_GNSS_RESULT_SUCCESS;
853}
854
855MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_set_nmea_out_type(MBTK_GNSS_MSG_INFO_TYPE type)
856{
857 if(mbtk_gnss_uloop_init == 0)
858 {
859 LOGE("[mbtk_gnss_api] gnss not init.");
860 return MBTK_GNSS_RESULT_FAIL;
861 }
862
863 if(type == MBTK_GNSS_MSG_LOCATION_INFO)
864 {
865 nmea_state.gnss_msg_state = type;
866 }
867 else if(type == MBTK_GNSS_MSG_NMEA_INFO)
868 {
869 nmea_state.gnss_msg_state = type;
870 }
871 else
872 {
873 return MBTK_GNSS_RESULT_FAIL;
874 }
875
876 return MBTK_GNSS_RESULT_SUCCESS;
877}
878
879
880MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_add_nmea_out_func(mbtk_gnss_nmea_func_t cb)
881{
882 if(mbtk_gnss_uloop_init == 0)
883 {
884 LOGE("[mbtk_gnss_api] gnss not init.");
885 return MBTK_GNSS_RESULT_FAIL;
886 }
887
888 if(cb == NULL)
889 {
890 LOGE("[mbtk_gnss_api] cb is NULL.");
891 return MBTK_GNSS_RESULT_FAIL;
892 }
893 nmea_state.callbacks = cb;
894
895 return MBTK_GNSS_RESULT_SUCCESS;
896}
897
wangyouqiang7d66fb12024-01-26 19:19:00 +0800898MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_5311_download_tle(char *host, int alam_flag)
899{
900 if (mbtk_gnss_uloop_init == 0)
901 {
902 LOGE("[mbtk_gnss_api] gnss not init.");
903 return MBTK_GNSS_RESULT_DOWNLOAD_FAIL;
904 }
905
906 if(mbtk_gnss_status == MBTK_GNSS_CLOSE)
907 {
908 LOGE("[mbtk_gnss_api] gnss not open.");
909 return MBTK_GNSS_RESULT_DOWNLOAD_FAIL;
910 }
911
912 int ret = -1;
913 MBTK_GNSS_5311_RESULT_TYPE ubus_gnss_result = MBTK_GNSS_RESULT_DOWNLOAD_SUCCESS;
914 struct blob_buf outBlob;
915 memset(&outBlob, 0, sizeof(outBlob));
916
917 blob_buf_init(&outBlob, 0);
918 blobmsg_add_string(&outBlob, MBTK_GNSS_UBUS_AGPS_SERVER, host);
919 blobmsg_add_u32(&outBlob, MBTK_GNSS_UBUS_ALAM_FLAG, alam_flag);
920
921 //UBUS_STATUS_OK
922 ret = mbtk_invoke_reply_data_cb(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_GET_AGPS, outBlob.head,
923 (ubus_data_handler_t *)mbtk_gnss_ubus_result_callback, &ubus_gnss_result, 8000);
924 blob_buf_free(&outBlob);
925 if (ret != 0)
926 {
927 LOGE("[mbtk_gnss_api] mbtk_invoke_reply_data_cb fail.");
928 return MBTK_GNSS_RESULT_DOWNLOAD_FAIL;
929 }
930
931 if(ubus_gnss_result != MBTK_GNSS_RESULT_DOWNLOAD_SUCCESS)
932 {
933 LOGE("[mbtk_gnss_api] ubus_gnss_result = [%d].", ubus_gnss_result);
934 return MBTK_GNSS_RESULT_DOWNLOAD_FAIL;
935 }
936
937 return MBTK_GNSS_RESULT_DOWNLOAD_SUCCESS;
938}
939
940MBTK_GNSS_5311_RESULT_TYPE mbtk_gnss_injectEphemeris(void)
941{
942 if (mbtk_gnss_uloop_init == 0)
943 {
944 LOGE("[mbtk_gnss_api] gnss not init.");
945 return MBTK_GNSS_RESULT_SEND_FAIL;
946 }
947
948 if(mbtk_gnss_status == MBTK_GNSS_CLOSE)
949 {
950 LOGE("[mbtk_gnss_api] gnss not open.");
951 return MBTK_GNSS_RESULT_SEND_FAIL;
952 }
953
954 int ret = -1;
955 MBTK_GNSS_5311_RESULT_TYPE ubus_gnss_result = MBTK_GNSS_RESULT_SUCCESS;
956
957 //UBUS_STATUS_OK
958 ret = mbtk_invoke_reply_data_cb(MBTK_GNSS_UBUS_SERVER, MBTK_GNSS_UBUS_SET_AGPS, NULL,
959 (ubus_data_handler_t *)mbtk_gnss_ubus_result_callback, &ubus_gnss_result, 8000);
960 if (ret != 0)
961 {
962 LOGE("[mbtk_gnss_api] mbtk_invoke_reply_data_cb fail.");
963 return MBTK_GNSS_RESULT_SEND_FAIL;
964 }
965
966 if(ubus_gnss_result != MBTK_GNSS_RESULT_SEND_SUCCESS)
967 {
968 LOGE("[mbtk_gnss_api] ubus_gnss_result = [%d].", ubus_gnss_result);
969 return MBTK_GNSS_RESULT_SEND_FAIL;
970 }
971
972 return MBTK_GNSS_RESULT_SEND_SUCCESS;
973}
wangyouqiang4c2048e2024-01-04 13:39:23 +0800974
975/**********************************API***********************************/
976
977#endif