blob: aaf794b142f364fdfd24eb98c55f2e0936874c62 [file] [log] [blame]
b.liue77ac3a2024-07-17 17:36:57 +08001/*
2* mbtk_gnss.c
3*
4* MBTK GNSS API source.
5*
6*/
7/******************************************************************************
8
9 EDIT HISTORY FOR FILE
10
11 WHEN WHO WHAT,WHERE,WHY
12-------- -------- -------------------------------------------------------
132024/7/11 LiuBin Initial version
14
15******************************************************************************/
16#include <stdio.h>
17#include <stdlib.h>
18#include <unistd.h>
19#include <errno.h>
20#include <pthread.h>
21#include <sys/socket.h>
22#include <sys/un.h>
23#include <netinet/in.h>
24#include <fcntl.h>
25#include <sys/epoll.h>
yq.wang1ddd1fd2024-07-25 23:00:14 -070026#include <time.h>
b.liue77ac3a2024-07-17 17:36:57 +080027
28#include "mbtk_gnss_inter.h"
29#include "mbtk_log.h"
30#include "mbtk_utils.h"
31
32#define GNSS_BUFF_SIZE 2048
33
34static int gnss_cli_fd = -1;
35static pthread_t read_thread_id;
36static int exit_fd[2] = {-1};
37static mbtk_gnss_callback_func gnss_cb = NULL;
38static bool gnss_busy = FALSE;
39static pthread_cond_t gnss_cond;
40static pthread_mutex_t gnss_mutex;
41static gnss_err_enum gnss_result;
42
yq.wang1ddd1fd2024-07-25 23:00:14 -070043#if 1//MBTK_GNSS_LOCATION_INFO
44static mbtk_gnss_location_info_t locl_info;
45
46extern long timezone;
47#endif
48
b.liu9e8584b2024-11-06 19:21:28 +080049static int sock_read(int fd, void *msg, int data_len)
b.liue77ac3a2024-07-17 17:36:57 +080050{
51 memset(msg, 0, data_len);
52 int len = 0;
53
54 int read_len = 0;
55 while(1)
56 {
57 len = read(fd, msg + read_len, data_len - read_len);
58 if(len > 0)
59 {
60 read_len += len;
61 }
62 else if(len == 0)
63 {
64 LOGE("read() end.");
65 break;
66 }
67 else
68 {
69 if(EAGAIN == errno)
70 {
b.liu42f558e2024-07-18 14:06:49 +080071 LOGV("Read end, lenght = %d", read_len);
b.liue77ac3a2024-07-17 17:36:57 +080072 }
73 else
74 {
75 LOGE("read() error[%d].", errno);
76 }
77 break;
78 }
79 }
80
81 if(read_len > 0)
82 {
83 // log_hex("DATA_RECV", msg, read_len);
84 return read_len;
85 }
86 else
87 {
88 return -1;
89 }
90}
91
b.liu9e8584b2024-11-06 19:21:28 +080092static int sock_write(int fd, void *msg, int data_len)
b.liue77ac3a2024-07-17 17:36:57 +080093{
94 int len = 0;
95 int write_len = 0;
96 while(write_len < data_len)
97 {
98 len = write(fd, msg + write_len, data_len - write_len);
99 if(len > 0)
100 {
101 write_len += len;
102 }
103 else if(len == 0)
104 {
105 LOGE("write() end.");
106 break;
107 }
108 else
109 {
110 LOGE("write() error[%d].", errno);
111 break;
112 }
113 }
114
115 if(write_len > 0)
116 {
yq.wang9dd771b2024-09-13 23:38:40 -0700117 //log_hex("DATA_SEND", msg, write_len);
b.liue77ac3a2024-07-17 17:36:57 +0800118 return write_len;
119 }
120 else
121 {
122 return -1;
123 }
124}
125
126static void gnss_timer_cb(int signo)
127{
128 if(gnss_busy) {
129 pthread_mutex_lock(&gnss_mutex);
130 pthread_cond_signal(&gnss_cond);
131 pthread_mutex_unlock(&gnss_mutex);
132 gnss_result = GNSS_ERR_TIMEOUT;
133 }
134 return;
135}
136
yq.wang1ddd1fd2024-07-25 23:00:14 -0700137#if 1//MBTK_GNSS_LOCATION_INFO
138static int strstr_n(const char *s1, const char *s2)
139{
140 int n;
141 int strlen = 0;
142
143 if(*s2)
144 {
145 while(*s1)
146 {
147 for(n = 0; *(s1+n) == *(s2 + n); n++)
148 {
149 if(!*(s2 + n + 1))
150 {
151 strlen++;
152 return strlen;
153 }
154 }
155 s1++;
156 strlen++;
157 }
158 return 0;
159 }
160
161 return 0;
162}
163
b.liu9e8584b2024-11-06 19:21:28 +0800164#if 0
yq.wang1ddd1fd2024-07-25 23:00:14 -0700165static int str2int( const char* head, const char* end )
166{
167 int result = 0;
168 int len = end - head;
169
170 for(; len > 0; len--, head++)
171 {
172 int c;
173 if (head >= end)
174 {
175 goto Fail;
176 }
177
178 c = *head - '0';
179 if ((unsigned)c >= 10)
180 {
181 goto Fail;
182 }
183 result = result * 10 + c;
184 }
185 return result;
186Fail:
187 return -1;
188}
b.liu9e8584b2024-11-06 19:21:28 +0800189#endif
yq.wang1ddd1fd2024-07-25 23:00:14 -0700190
191static double str2float( const char* head, const char* end )
192{
193 int len = end - head;
194 char temp[16];
195
196 if(len >= (int)sizeof(temp))
197 {
198 return 0;
199 }
200
201 memcpy( temp, head, len );
202 temp[len] = 0;
203 return strtod(temp, NULL);
204}
205
206static mbtk_token nmea_tokenizer_get(mbtk_nmeatokenizer* t, int index)
207{
208 mbtk_token tok;
209 static const char* dummy = "";
210
211 if (index < 0 || index >= t->count)
212 {
213 tok.head = tok.end = dummy;
214 }
215 else
216 {
217 tok = t->tokens[index];
218 }
219 return tok;
220}
221
222static int nmea_tokenizer_init(mbtk_nmeatokenizer* t, const char* head, const char* end, int param_num)
223{
224 int count = 0;
225 const char* p = head;
226 const char* q = end;
227 const char* tmp = NULL;
228 // the initial '$' is optional
229 if (p < q && p[0] == '$')
230 {
231 p += 1;
232 }
233 else
234 {
235 return -1;
236 }
237
238 //find '*',del '*25\r\n'
239 // get rid of checksum at the end of the sentecne
240 if (q >= p + 5 && q[-5] == '*')
241 {
242 q -= 5;
243 }
244 else
245 {
246 return -1;
247 }
248
b.liubcf86c92024-08-19 19:48:28 +0800249 while (p <= q)
yq.wang1ddd1fd2024-07-25 23:00:14 -0700250 {
251 tmp = memchr(p, ',', q-p);
252 if (tmp == NULL)
253 {
254 tmp = q;
255 }
256 // if (q > p) {
257 // q >= p include empty token: ,,
258 if (tmp >= p)
259 {
260 if (count < MAX_NMEA_TOKENS)
261 {
262 t->tokens[count].head = p;
263 t->tokens[count].end = tmp;
264 count += 1;
265 }
266 }
b.liubcf86c92024-08-19 19:48:28 +0800267
yq.wang1ddd1fd2024-07-25 23:00:14 -0700268 if (tmp <= q)
269 {
270 tmp += 1;
271 }
272
273 p = tmp;
274 }
275
276 if(count != param_num)
277 {
278 LOGD("count [%d], param_num [%d]", count, param_num);
279 return -1;
280 }
281
282 t->count = count;
283 return count;
284}
285
286static int nmea_update_date_time(mbtk_gnss_location_info_t* locl_info, mbtk_token date, mbtk_token time)
287{
288 char tmp_char[4] = {0};
289 struct tm tmp_time;
290
291 memset(&tmp_time, 0x0, sizeof(struct tm));
292 if (date.head + 6 > date.end)
293 {
294 LOGD("date get fail");
295 return -1;
296 }
297
298 memcpy(tmp_char, date.head, 2);
299 tmp_time.tm_mday = atoi(tmp_char);
300 memcpy(tmp_char, date.head + 2, 2);
301 tmp_time.tm_mon = atoi(tmp_char) - 1;
302 memcpy(tmp_char, date.head + 4, 2);
303 tmp_time.tm_year = 100 + atoi(tmp_char);
304
305 if (time.head + 6 > time.end)
306 {
307 LOGD("time get fail");
308 return -1;
309 }
310
311 memcpy(tmp_char, time.head, 2);
312 tmp_time.tm_hour = atoi(tmp_char);
313 memcpy(tmp_char, time.head + 2, 2);
314 tmp_time.tm_min = atoi(tmp_char);
315 memcpy(tmp_char, time.head + 4, 2);
316 tmp_time.tm_sec = atoi(tmp_char);
317 tmp_time.tm_isdst = -1;
b.liubcf86c92024-08-19 19:48:28 +0800318
yq.wang1ddd1fd2024-07-25 23:00:14 -0700319#if MBTK_GNSS_LOG_ENABLED
b.liubcf86c92024-08-19 19:48:28 +0800320 LOGD("data:%d-%d-%d %d:%d:%d", tmp_time.tm_year + 1900,
yq.wang1ddd1fd2024-07-25 23:00:14 -0700321 tmp_time.tm_mon,
322 tmp_time.tm_mday,
323 tmp_time.tm_hour,
324 tmp_time.tm_min,
325 tmp_time.tm_sec);
326#endif
327
328 time_t _t = mktime(&tmp_time);//parse location tmp_time
329#if MBTK_GNSS_LOG_ENABLED
330 LOGD("time: %ld", _t);
331#endif
332 tzset(); // auto set tz
333 _t = _t - timezone;
334 locl_info->timestamp = (int64_t)_t;
335#if MBTK_GNSS_LOG_ENABLED
336 LOGD("timestamp: %ld, %ld", locl_info->timestamp, timezone);
337#endif
338 return 0;
339}
340
341static int nmea_update_bearing(mbtk_gnss_location_info_t* locl_info, mbtk_token bearing)
342{
343 mbtk_token tok = bearing;
344
345 if (tok.head >= tok.end)
346 {
347 LOGD("bearing get fail");
348 return -1;
349 }
350
351 locl_info->bearing = str2float(tok.head, tok.end);
352 return 0;
353}
354
355static int nmea_update_speed(mbtk_gnss_location_info_t* locl_info, mbtk_token speed)
356{
357 mbtk_token tok = speed;
358
359 if (tok.head >= tok.end)
360 {
361 LOGD("speed get fail");
362 return -1;
363 }
364
365 locl_info->speed = str2float(tok.head, tok.end);
366 return 0;
367}
368
369static int nmea_update_latlong(mbtk_gnss_location_info_t* locl_info, mbtk_token latitude, mbtk_token longitude)
370{
yq.wang1ddd1fd2024-07-25 23:00:14 -0700371 mbtk_token tok;
372 tok = latitude;
373 if (tok.head + 6 > tok.end)
374 {
375 LOGD("latitude get fail");
376 return -1;
377 }
378 locl_info->latitude = str2float(tok.head, tok.end);
379
380 tok = longitude;
b.liubcf86c92024-08-19 19:48:28 +0800381 if (tok.head + 6 > tok.end)
yq.wang1ddd1fd2024-07-25 23:00:14 -0700382 {
383 LOGD("longitude get fail");
384 return -1;
385 }
386 locl_info->longitude = str2float(tok.head, tok.end);
387 return 0;
388}
389
390static int nmea_update_altitude(mbtk_gnss_location_info_t* locl_info, mbtk_token altitude)
391{
392 mbtk_token tok = altitude;
393
394 if (tok.head >= tok.end)
395 {
396 LOGD("altitude get fail");
397 return -1;
398 }
399
400 locl_info->altitude = str2float(tok.head, tok.end);
401 return 0;
402}
403
404static int ind_nmea_parse(const char *data, int data_len, mbtk_gnss_location_info_t* locl_info)
405{
406 int ret;
407 mbtk_nmeatokenizer tzer = {0};
408 if(strstr_n(data + 3, "RMC"))
409 {
410#if MBTK_GNSS_LOG_ENABLED
411 LOGD("ind data_len: [%d] data: %s", data_len, data);
412#endif
413 ret = nmea_tokenizer_init(&tzer, data, data + data_len, NMEA_RMC_PARAM_NUM);
414 if(ret < 0)
415 {
416 LOGD("nmea_tokenizer_init fail");
417 return -1;
418 }
419
420 mbtk_token tok_time = nmea_tokenizer_get(&tzer,1);
421 mbtk_token tok_fixStatus = nmea_tokenizer_get(&tzer,2);
422 mbtk_token tok_speed = nmea_tokenizer_get(&tzer,7);
423 mbtk_token tok_bearing = nmea_tokenizer_get(&tzer,8);
424 mbtk_token tok_date = nmea_tokenizer_get(&tzer,9);
425
426 if(tok_fixStatus.head[0] == 'A')
427 {
428 ret = nmea_update_date_time(locl_info, tok_date, tok_time);
429 if(ret < 0)
430 {
431 LOGD("nmea_update_date_time fail");
432 return -1;
433 }
434 locl_info->flags |= GNSS_LOCATION_HAS_TIMESTAMP;
435
436 ret = nmea_update_bearing(locl_info, tok_bearing);
437 if(ret < 0)
438 {
439 LOGD("nmea_update_bearing fail");
440 return -1;
441 }
442 locl_info->flags |= GNSS_LOCATION_HAS_BEARING;
443
444 ret = nmea_update_speed(locl_info, tok_speed);
445 if(ret < 0)
446 {
447 LOGD("nmea_update_speed fail");
448 return -1;
449 }
450 locl_info->flags |= GNSS_LOCATION_HAS_SPEED;
451 }
452 }
453 else if(strstr_n(data + 3, "GGA"))
454 {
455#if MBTK_GNSS_LOG_ENABLED
456 LOGD("ind data_len: [%d] data: %s", data_len, data);
457#endif
458 ret = nmea_tokenizer_init(&tzer, data, data + data_len, NMEA_GGA_PARAM_NUM);
459 if(ret < 0)
460 {
461 LOGD("nmea_tokenizer_init fail");
462 return -1;
463 }
464
465 mbtk_token tok_latitude = nmea_tokenizer_get(&tzer,2);
466 mbtk_token tok_longitude = nmea_tokenizer_get(&tzer,4);
467 mbtk_token tok_isPix = nmea_tokenizer_get(&tzer,6);
468 mbtk_token tok_altitude = nmea_tokenizer_get(&tzer,9);
469
470 if(tok_isPix.head[0] > '0' && tok_isPix.head[0] < '9')
471 {
472 ret = nmea_update_latlong(locl_info, tok_latitude, tok_longitude);
473 if(ret < 0)
474 {
475 LOGD("nmea_update_latlong fail");
476 return -1;
477 }
478 locl_info->flags |= GNSS_LOCATION_HAS_LAT_LONG;
b.liubcf86c92024-08-19 19:48:28 +0800479
yq.wang1ddd1fd2024-07-25 23:00:14 -0700480 ret = nmea_update_altitude(locl_info, tok_altitude);
481 if(ret < 0)
482 {
483 LOGD("nmea_update_altitude fail");
484 return -1;
485 }
486 locl_info->flags |= GNSS_LOCATION_HAS_ALTITUDE;
487 }
488 }
489 else
490 {
491#if MBTK_GNSS_LOG_ENABLED
492 LOGD("ind data_len: [%d] data: %s", data_len, data);
493#endif
494 }
495 return 0;
496}
497#endif
498
499
b.liue77ac3a2024-07-17 17:36:57 +0800500static void gnss_rsp_process(const char *data, int data_len)
501{
502 int index = 0;
503 char buff[GNSS_BUFF_SIZE];
504 int buff_len = 0;
yq.wang1ddd1fd2024-07-25 23:00:14 -0700505 int ret = 0;
506 int tag_len = 0;
b.liue77ac3a2024-07-17 17:36:57 +0800507 while(index < data_len) {
508 if(data[index] == MBTK_IND_START_FLAG) {
509 memset(buff, 0, sizeof(buff));
510 buff_len = 0;
511 } else if(data[index] == MBTK_IND_END_FLAG) {
512 buff[buff_len] = '\0';
b.liue77ac3a2024-07-17 17:36:57 +0800513 if(memcmp(MBTK_IND_LOCATION_TAG, buff, strlen(MBTK_IND_LOCATION_TAG)) == 0) {
514 if(gnss_cb) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700515#if 1//MBTK_GNSS_LOCATION_INFO
516 tag_len = strlen(MBTK_IND_LOCATION_TAG);
517 ret = ind_nmea_parse(buff + tag_len, buff_len - tag_len, &locl_info);
518#if MBTK_GNSS_LOG_ENABLED
519 LOGD("gnss_cb: [%p], locl_info.flags [%d]", gnss_cb, locl_info.flags);
520#endif
b.liu9e8584b2024-11-06 19:21:28 +0800521 if(ret == 0 && locl_info.flags == GNSS_LOCATION_HAS_ALL) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700522 gnss_cb(MBTK_GNSS_IND_LOCATION, &locl_info, sizeof(mbtk_gnss_location_info_t));
523 locl_info.flags = 0;
524 }
525#endif
b.liue77ac3a2024-07-17 17:36:57 +0800526 }
527 } else if(memcmp(MBTK_IND_NMEA_TAG, buff, strlen(MBTK_IND_NMEA_TAG)) == 0) {
528 if(gnss_cb) {
yq.wang1ddd1fd2024-07-25 23:00:14 -0700529 tag_len = strlen(MBTK_IND_NMEA_TAG);
530 gnss_cb(MBTK_GNSS_IND_NMEA, buff + tag_len, buff_len - tag_len);
b.liue77ac3a2024-07-17 17:36:57 +0800531 }
532 } else {
b.liu42f558e2024-07-18 14:06:49 +0800533 LOGD("RSP[len - %d] : %s", buff_len, buff);
b.liue77ac3a2024-07-17 17:36:57 +0800534 if(gnss_busy) {
b.liu42f558e2024-07-18 14:06:49 +0800535 // XXXXXX:<result>
536 char *ptr = strstr(buff, ":");
537 if(ptr) {
538 gnss_result = atoi(ptr + 1);
539 }
b.liue77ac3a2024-07-17 17:36:57 +0800540 pthread_mutex_lock(&gnss_mutex);
541 pthread_cond_signal(&gnss_cond);
542 pthread_mutex_unlock(&gnss_mutex);
543 }
544 }
545 } else {
546 buff[buff_len++] = data[index];
547 }
548 index++;
549 }
550}
551
552static void* gnss_read_run(void* arg)
553{
554 int epoll_fd = epoll_create(5);
555 if(epoll_fd < 0)
556 {
557 LOGE("epoll_create() fail[%d].", errno);
558 return NULL;
559 }
560
561 uint32 event = EPOLLIN | EPOLLET;
562 struct epoll_event ev_cli, ev_exit;
563 ev_cli.data.fd = gnss_cli_fd;
564 ev_cli.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
565 epoll_ctl(epoll_fd,EPOLL_CTL_ADD,gnss_cli_fd,&ev_cli);
566
567 ev_exit.data.fd = exit_fd[0];
568 ev_exit.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
569 epoll_ctl(epoll_fd,EPOLL_CTL_ADD,exit_fd[0],&ev_exit);
570
571 int nready = -1;
572 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
573 while(1)
574 {
575 nready = epoll_wait(epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
576 if(nready > 0)
577 {
578 int i;
579 for(i = 0; i < nready; i++)
580 {
b.liu42f558e2024-07-18 14:06:49 +0800581 LOGV("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
b.liue77ac3a2024-07-17 17:36:57 +0800582 if(epoll_events[i].events & EPOLLHUP) // Closed by server.
583 {
584
585 }
586 else if(epoll_events[i].events & EPOLLIN)
587 {
588 if(gnss_cli_fd == epoll_events[i].data.fd) // Server data arrive.
589 {
590 char buff[GNSS_BUFF_SIZE + 1];
591 int len = sock_read(gnss_cli_fd, buff, GNSS_BUFF_SIZE);
592 if(len > 0) {
593 gnss_rsp_process(buff, len);
594 }
595 }
596 else if(exit_fd[0] == epoll_events[i].data.fd) //
597 {
598 char buff[100] = {0};
599 int len = read(exit_fd[0], buff, 100);
600 if(len > 0) {
601 LOGI("CMD : %s", buff);
602 if(strcmp(buff, "EXIT") == 0) {
603 goto read_thread_exit;
604 } else {
605 LOGD("Unkonw cmd : %s", buff);
606 }
607 } else {
608 LOGE("sock_read() fail.");
609 }
610 }
611 else
612 {
613 LOGW("Unknown socket : %d", epoll_events[i].data.fd);
614 }
615 }
616 else
617 {
618 LOGW("Unknown event : %x", epoll_events[i].events);
619 }
620 }
621 }
622 else
623 {
624 LOGW("epoll_wait() fail[%d].", errno);
625 }
626 }
627
628read_thread_exit:
629 LOGD("info_read thread exit.");
630 return NULL;
631}
632
633gnss_err_enum mbtk_gnss_init(mbtk_gnss_callback_func cb)
634{
635 if(gnss_cli_fd > 0) {
636 LOGW("GNSS client has inited.");
637 return GNSS_ERR_OK;
638 }
639
640 gnss_cli_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
641 if(gnss_cli_fd < 0)
642 {
643 LOGE("socket() fail[%d].", errno);
644 goto error;
645 }
646
647 // Set O_NONBLOCK
648 int flags = fcntl(gnss_cli_fd, F_GETFL, 0);
649 if (flags < 0)
650 {
651 LOGE("Get flags error:%d", errno);
652 goto error;
653 }
654 flags |= O_NONBLOCK;
655 if (fcntl(gnss_cli_fd, F_SETFL, flags) < 0)
656 {
657 LOGE("Set flags error:%d", errno);
658 goto error;
659 }
660
661 struct sockaddr_un cli_addr;
662 memset(&cli_addr, 0, sizeof(cli_addr));
663 cli_addr.sun_family = AF_LOCAL;
664 strcpy(cli_addr.sun_path, SOCK_GNSS_PATH);
665 if(connect(gnss_cli_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)))
666 {
667 LOGE("connect() fail[%d].", errno);
668 goto error;
669 }
670
671 if(pipe(exit_fd)) {
672 LOGE("pipe() fail[%d].", errno);
673 goto error;
674 }
675
676 if(pthread_create(&read_thread_id, NULL, gnss_read_run, NULL))
677 {
678 LOGE("pthread_create() fail.");
679 goto error;
680 }
681 pthread_mutex_init(&gnss_mutex, NULL);
682 pthread_cond_init(&gnss_cond, NULL);
683 gnss_cb = cb;
684 return GNSS_ERR_OK;
685
686error:
687 if(gnss_cli_fd > 0) {
688 close(gnss_cli_fd);
689 gnss_cli_fd = -1;
690 }
691 if(exit_fd[0] > 0) {
692 close(exit_fd[0]);
693 exit_fd[0] = -1;
694 }
695 if(exit_fd[1] > 0) {
696 close(exit_fd[1]);
697 exit_fd[1] = -1;
698 }
699
700 return GNSS_ERR_UNKNOWN;
701}
702
703gnss_err_enum mbtk_gnss_deinit()
704{
705 if(gnss_cli_fd < 0) {
706 LOGW("GNSS client not inited.");
707 return GNSS_ERR_UNKNOWN;
708 }
709
710 if(gnss_busy) {
711 LOGE("BUSY");
712 return GNSS_ERR_BUSY;
713 }
714
715 if(exit_fd[1] > 0) {
b.liu9e8584b2024-11-06 19:21:28 +0800716 if(4 != write(exit_fd[1], "EXIT", 4)) {
717 LOGE("write EXIT fail.");
718 }
b.liue77ac3a2024-07-17 17:36:57 +0800719 }
720
721 int ret = pthread_join(read_thread_id, NULL);
722 if(ret){
723 LOGE("pthrad_join fail(%d)",ret);
724 return GNSS_ERR_UNKNOWN;
725 }
726
727 close(gnss_cli_fd);
728 gnss_cli_fd = -1;
729 close(exit_fd[0]);
730 exit_fd[0] = -1;
731 close(exit_fd[1]);
732 exit_fd[1] = -1;
733 gnss_cb = NULL;
734 gnss_busy = FALSE;
735 pthread_mutex_destroy(&gnss_mutex);
736 pthread_cond_destroy(&gnss_cond);
737
738 return GNSS_ERR_OK;
739}
740
b.liuc223a942024-07-18 15:44:12 +0800741/*===========================================================================
742FUNCTION mbtk_gnss_open
743
744DESCRIPTION:
745 Open/Close GNSS.
746
747PARAMETERS:
748 type [IN]: How to turn on or off GNSS,The available values are as follows:
749 0 : Close GNSS;
750 1-15 : Open GNSS with NMEA print;
751 (GNSS_PRINT_PORT_UART1 / GNSS_PRINT_PORT_USB_NMEA / GNSS_PRINT_PORT_USB_AT / GNSS_PRINT_PORT_TTY_AT)
752 Other : Open GNSS without print NMEA
753 timeout [IN]: Timeout with second.
754
755RETURN VALUE:
756 gnss_err_enum
757
758===========================================================================*/
b.liue77ac3a2024-07-17 17:36:57 +0800759gnss_err_enum mbtk_gnss_open(int type, int timeout)
760{
761 if(gnss_cli_fd < 0) {
762 LOGW("GNSS client not inited.");
763 return GNSS_ERR_UNKNOWN;
764 }
765
766 if(gnss_busy) {
767 LOGE("BUSY");
768 return GNSS_ERR_BUSY;
769 } else {
770 gnss_result = GNSS_ERR_OK;
771 gnss_busy = TRUE;
772 if(timeout > 0) {
773 mbtk_timer_set(gnss_timer_cb, timeout * 1000);
774 }
775
776 // gnss_init:x
777 char cmd[32] = {0};
778 snprintf(cmd, sizeof(cmd), "gnss_init:%d", type);
779 sock_write(gnss_cli_fd, cmd, strlen(cmd));
780
781 pthread_mutex_lock(&gnss_mutex);
782 pthread_cond_wait(&gnss_cond, &gnss_mutex);
783 pthread_mutex_unlock(&gnss_mutex);
784
785 gnss_busy = FALSE;
786
787 return gnss_result;
788 }
789}
790
791gnss_err_enum mbtk_gnss_close(int timeout)
792{
793 if(gnss_cli_fd < 0) {
794 LOGW("GNSS client not inited.");
795 return GNSS_ERR_UNKNOWN;
796 }
797
798 if(gnss_busy) {
799 LOGE("BUSY");
800 return GNSS_ERR_BUSY;
801 } else {
802 gnss_result = GNSS_ERR_OK;
803 gnss_busy = TRUE;
804 if(timeout > 0) {
805 mbtk_timer_set(gnss_timer_cb, timeout * 1000);
806 }
807
808 // gnss_deinit
809 char *cmd = "gnss_deinit";
810 sock_write(gnss_cli_fd, cmd, strlen(cmd));
811
812 pthread_mutex_lock(&gnss_mutex);
813 pthread_cond_wait(&gnss_cond, &gnss_mutex);
814 pthread_mutex_unlock(&gnss_mutex);
815
816 gnss_busy = FALSE;
817
818 return gnss_result;
819 }
820}
821
822
823gnss_err_enum mbtk_gnss_setting(const char *setting_cmd, int timeout)
824{
825 if(gnss_cli_fd < 0) {
826 LOGW("GNSS client not inited.");
827 return GNSS_ERR_UNKNOWN;
828 }
829
830 if(gnss_busy) {
831 LOGE("BUSY");
832 return GNSS_ERR_BUSY;
833 } else {
834 gnss_result = GNSS_ERR_OK;
835 gnss_busy = TRUE;
836 if(timeout > 0) {
837 mbtk_timer_set(gnss_timer_cb, timeout * 1000);
838 }
839
840 // gnss_setting:cmd
yq.wang9dd771b2024-09-13 23:38:40 -0700841 char cmd[1024] = {0};
b.liue77ac3a2024-07-17 17:36:57 +0800842 snprintf(cmd, sizeof(cmd), "gnss_setting:%s", setting_cmd);
843 sock_write(gnss_cli_fd, cmd, strlen(cmd));
844
845 pthread_mutex_lock(&gnss_mutex);
846 pthread_cond_wait(&gnss_cond, &gnss_mutex);
847 pthread_mutex_unlock(&gnss_mutex);
848
849 gnss_busy = FALSE;
850
851 return gnss_result;
852 }
853}
854
855gnss_err_enum mbtk_gnss_dl(const char *fw_path, int timeout)
856{
857 if(gnss_cli_fd < 0) {
858 LOGW("GNSS client not inited.");
859 return GNSS_ERR_UNKNOWN;
860 }
861
862 if(gnss_busy) {
863 LOGE("BUSY");
864 return GNSS_ERR_BUSY;
865 } else {
866 gnss_result = GNSS_ERR_OK;
867 gnss_busy = TRUE;
868 if(timeout > 0) {
869 mbtk_timer_set(gnss_timer_cb, timeout * 1000);
870 }
rx.xiecf3a3782025-08-08 05:07:12 -0700871
b.liue77ac3a2024-07-17 17:36:57 +0800872
873 // gnss_dl:fw_name
rx.xiecf3a3782025-08-08 05:07:12 -0700874 char cmd[64] = {0};
b.liue77ac3a2024-07-17 17:36:57 +0800875 snprintf(cmd, sizeof(cmd), "gnss_dl:%s", fw_path);
876 sock_write(gnss_cli_fd, cmd, strlen(cmd));
877
878 pthread_mutex_lock(&gnss_mutex);
879 pthread_cond_wait(&gnss_cond, &gnss_mutex);
880 pthread_mutex_unlock(&gnss_mutex);
881
882 gnss_busy = FALSE;
883
884 return gnss_result;
885 }
886}
887
b.liuc223a942024-07-18 15:44:12 +0800888/**
889* Set GNSS data callback.
890*
891* gnss_ind : MBTK_GNSS_IND_LOCATION / MBTK_GNSS_IND_NMEA
892* timeout : Timeout with second.
893*
894*/
b.liue77ac3a2024-07-17 17:36:57 +0800895gnss_err_enum mbtk_gnss_ind_set(uint32 gnss_ind, int timeout)
896{
897 if(gnss_cli_fd < 0) {
898 LOGW("GNSS client not inited.");
899 return GNSS_ERR_UNKNOWN;
900 }
901
902 if(gnss_busy) {
903 LOGE("BUSY");
904 return GNSS_ERR_BUSY;
905 } else {
906 gnss_result = GNSS_ERR_OK;
907 gnss_busy = TRUE;
908 if(timeout > 0) {
909 mbtk_timer_set(gnss_timer_cb, timeout * 1000);
910 }
911
912 // gnss_ind:ind_type
913 char cmd[32] = {0};
914 snprintf(cmd, sizeof(cmd), "gnss_ind:%d", gnss_ind);
915 sock_write(gnss_cli_fd, cmd, strlen(cmd));
916
917 pthread_mutex_lock(&gnss_mutex);
918 pthread_cond_wait(&gnss_cond, &gnss_mutex);
919 pthread_mutex_unlock(&gnss_mutex);
920
921 gnss_busy = FALSE;
922
923 return gnss_result;
924 }
925}
926
yq.wang9dd771b2024-09-13 23:38:40 -0700927gnss_err_enum mbtk_gnss_eph_download(int timeout)
928{
929 if(gnss_cli_fd < 0) {
930 LOGW("GNSS client not inited.");
931 return GNSS_ERR_UNKNOWN;
932 }
933
934 if(gnss_busy) {
935 LOGE("BUSY");
936 return GNSS_ERR_BUSY;
937 } else {
938 gnss_result = GNSS_ERR_OK;
939 gnss_busy = TRUE;
940 if(timeout > 0) {
941 mbtk_timer_set(gnss_timer_cb, timeout * 1000);
942 }
943
944 // gnss_agnss_get:<eph_data>,<alam_flag>
945 char cmd[32] = {0};
946 snprintf(cmd, sizeof(cmd), "gnss_agnss_get:%d,0", (int)GNSS_EPH_CFG);
947 sock_write(gnss_cli_fd, cmd, strlen(cmd));
948
949 pthread_mutex_lock(&gnss_mutex);
950 pthread_cond_wait(&gnss_cond, &gnss_mutex);
951 pthread_mutex_unlock(&gnss_mutex);
952
953 gnss_busy = FALSE;
954
955 return gnss_result;
956 }
957}
958
959gnss_err_enum mbtk_gnss_eph_inject(int timeout)
960{
961 if(gnss_cli_fd < 0) {
962 LOGW("GNSS client not inited.");
963 return GNSS_ERR_UNKNOWN;
964 }
965
966 if(gnss_busy) {
967 LOGE("BUSY");
968 return GNSS_ERR_BUSY;
969 } else {
970 gnss_result = GNSS_ERR_OK;
971 gnss_busy = TRUE;
972 if(timeout > 0) {
973 mbtk_timer_set(gnss_timer_cb, timeout * 1000);
974 }
975
976 // gnss_agnss_set
977 char cmd[32] = {0};
978 snprintf(cmd, sizeof(cmd), "gnss_agnss_set");
979 sock_write(gnss_cli_fd, cmd, strlen(cmd));
980
981 pthread_mutex_lock(&gnss_mutex);
982 pthread_cond_wait(&gnss_cond, &gnss_mutex);
983 pthread_mutex_unlock(&gnss_mutex);
984
985 gnss_busy = FALSE;
986
987 return gnss_result;
988 }
989}
990
b.liubcf86c92024-08-19 19:48:28 +0800991void mbtk_gnss_lib_info_print()
992{
993 MBTK_SOURCE_INFO_PRINT("mbtk_gnss_lib");
994}
995