[Bugfix][T106BUG-182] fix the issue of no gnss time source.

Only Configure: No
Affected branch: master
Affected module: systime gnss
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: Yes
Doc Update: Yes

Change-Id: Ie19972665a5028ccd00a2efa2fd68b08ba00fb21
diff --git a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/liblynq-qser-gnss/liblynq-qser-gnss.bb b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/liblynq-qser-gnss/liblynq-qser-gnss.bb
index bebad96..687a272 100755
--- a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/liblynq-qser-gnss/liblynq-qser-gnss.bb
+++ b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/liblynq-qser-gnss/liblynq-qser-gnss.bb
@@ -3,7 +3,7 @@
 DESCRIPTION = "liblynq-qser-gnss"

 LICENSE = "CLOSED"

 LIC_FILES_CHKSUM = "file://LICENSE;md5=3f86c7f456a6d0bbeca155e65084eee1"

-DEPENDS += "liblynq-log libxml2"

+DEPENDS += "liblynq-log libxml2 liblynq-uci"

 #inherit workonsrc

 WORKONSRC = "${TOPDIR}/../src/lynq/lib/liblynq-qser-gnss/"

 FILESEXTRAPATHS_prepend :="${TOPDIR}/../src/lynq/lib/:"

diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/makefile b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/makefile
index b18390b..c59a62e 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/makefile
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/makefile
@@ -32,6 +32,7 @@
     -lpthread \

     -llynq-log \

     -lxml2 \

+    -llynq-uci \

 

 

 SOURCES = $(wildcard *.c wildcard *.h src/*.cpp)

diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/lynq_qser_gnss.cpp b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/lynq_qser_gnss.cpp
index a7f7191..326f697 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/lynq_qser_gnss.cpp
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/lynq_qser_gnss.cpp
@@ -34,6 +34,7 @@
 #include "mbtk_gnss_internal.h"
 #include "ringbuffer.h"
 #include "lynq_qser_gnss.h"
+#include "include/lynq_uci.h"
 DEFINE_LYNQ_LIB_LOG(LYNQ_QSER_GNSS)
 #ifdef __cplusplus
 extern "C" {
@@ -41,6 +42,8 @@
 
 int ret;
 LYNQ_INJECT_TIME_INTO_T time_test;
+int g_gnss_sync_enable_flag = 0;
+int g_gnss_sync_done = 0;
 
 int qser_Gnss_Init(uint32_t *h_gnss)
 {
@@ -133,6 +136,12 @@
 int qser_Gnss_Start(uint32_t h_gnss)
 {
     struct mbtk_gnss_handle_t *gnss_handle = (struct mbtk_gnss_handle_t *)h_gnss;
+
+    char gnss_sync_enable[24] = "";
+    lynq_get_value("lynq_uci", "lynq_sync_time", "lynq_gnss_sync_time_enable" , gnss_sync_enable);
+    g_gnss_sync_enable_flag = atoi(gnss_sync_enable);
+    g_gnss_sync_done = 0;
+
     lynq_open_gps(1);
     ret = set_baudrate(gnss_handle->dev_fd, B9600);
     if(-1 == ret)
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/mbtk_gnss.cpp b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/mbtk_gnss.cpp
index 5721c83..7afec28 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/mbtk_gnss.cpp
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/mbtk_gnss.cpp
@@ -25,6 +25,35 @@
 extern "C" {
 #endif
 
+#define _XOPEN_SOURCE
+#define NMEA_RMC "RMC"
+extern long timezone;
+extern int g_gnss_sync_enable_flag;
+extern int g_gnss_sync_done;
+static inline int update_system_time(time_t timestamp)
+{
+    struct timeval tv;
+    int ret = gettimeofday(&tv, NULL);
+    if (ret == -1) {
+        perror("gettimeofday");
+        return -1;
+    }
+    if (tv.tv_sec == timestamp)
+    {
+        return 0;
+    }
+    printf("Now: %ld\n", tv.tv_sec);
+    tv.tv_sec = timestamp;
+    tv.tv_usec = 0;
+
+    ret = settimeofday(&tv, NULL);
+    if (ret == -1) {
+        perror("settimeofday");
+        return -1;
+    }
+
+    return 0;
+}
 
 struct mbtk_gnss_handle_t
 {
@@ -180,14 +209,17 @@
     memcpy(tmp_char, &time[6], 2);
     tmp_time->tm_mday = atoi(tmp_char);
     memcpy(tmp_char, &time[8], 2);
-    tmp_time->tm_mon = atoi(tmp_char);
+    tmp_time->tm_mon = atoi(tmp_char) - 1;
     memcpy(tmp_char, &time[10], 2);
     tmp_time->tm_year = 100 + atoi(tmp_char);
 
     time_t _t = mktime(tmp_time);//按当地时区解析tmp_time
-    // gnss_log("timestamp: %ld\n",_t);
-    free(tmp_time);
+    //gnss_log("timestamp: %ld\n",_t);
+    tzset();
+    _t = _t - timezone;
+    //gnss_log("timestamp: %ld\n",_t);
 
+    free(tmp_time);
     return _t;
 }
 /**
@@ -451,7 +483,17 @@
         handle->gnss_handler_func((uint32)handle, E_MT_LOC_MSG_ID_LOCATION_INFO, &mopen_location_info, NULL);
         memset(&mopen_location_info, 0, sizeof(mopen_location_info));
     }
-
+    //set system time.
+    if (g_gnss_sync_enable_flag == 1 && g_gnss_sync_done == 0)
+    {
+        if( strncmp(data+3,NMEA_RMC,strlen(NMEA_RMC)) == 0 && data[17] == 'A')
+        {
+            if (update_system_time(nmea_info.timestamp) == 0)
+            {
+                g_gnss_sync_done = 1;
+            }
+        }
+    }
     return;
 }