add systime function

Change-Id: Ie6b3886de019a65e86cedecacd9287d1f4ba9fab
diff --git a/mbtk/lynq_lib/Makefile b/mbtk/lynq_lib/Makefile
index b543be5..ddfa753 100755
--- a/mbtk/lynq_lib/Makefile
+++ b/mbtk/lynq_lib/Makefile
@@ -7,7 +7,8 @@
 
 LIB_DIR +=
 
-LIBS += -llog -lmbtk_lib -lprop2uci
+#LIBS += -llog -lmbtk_lib
+LIBS += -lmbtk_lib -lrilutil -lprop2uci -lmtel -laudio-apu -lcutils -ltinyalsa -lacm -llog
 ifeq ($(BUILD_PLATFORM), asr1806)
 LIBS += -lmbtk_audio_lib
 endif
diff --git a/mbtk/lynq_lib/src/lynq_time.c b/mbtk/lynq_lib/src/lynq_time.c
index ce3d3ec..d027b62 100755
--- a/mbtk/lynq_lib/src/lynq_time.c
+++ b/mbtk/lynq_lib/src/lynq_time.c
@@ -1,51 +1,252 @@
 #include "lynq_systime.h"
 #include "mbtk_type.h"
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <errno.h>
+#include <netdb.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+#include <netinet/in.h>
+
+#include <cutils/properties.h>
+#include <sys/time.h>
+
+
+#include "mbtk_ntp.h"
+#include "mbtk_net_control.h"
+#include "lynq_systime.h"
+#include "mbtk_type.h"
+
+
+#define MBTK_AT_NTP_LEN_MAX 128
+
+
+
+
+typedef enum {
+    MBTK_TIME_TYPE_CELL = 0,  //NITZ
+    MBTK_TIME_TYPE_NTP,
+    MBTK_TIME_TYPE_GNSS,
+    MBTK_TIME_TYPE_USER
+} mbtk_time_type_enum;
+
+//enable set time from ntp
+int ntp_sync_time(int enable); 
+//enable set time from nitz
+int modem_time_enable(int enable);
+//enable set time from gnss
+int gnss_time_enable(int enable);
+//enable set time from user
+int user_set_time(char* date, char* time);
+// RTC TIME set to system
+int lynq_sync_time_from_rtc(void);
+//check sysytem type
+int lynq_get_time_src_status (time_src_status_s * time_src);
+// system time set to RTC
+int lynq_set_rtc_time(void);
+// get RTC time
+int lynq_get_rtc_time(unsigned long *ulsec);
+
+
+
+
+
+//int req_time_set(int type, char *time, int *cme_err);
+static int metis_strptime(char *str_time)
+{
+    printf("%s(), str_time:%s\n", __FUNCTION__, str_time);
+    struct tm stm;
+    char dateTime[30];
+    struct timeval tv;
+    if(strptime(str_time, "%Y-%m-%d %H:%M:%S",&stm) != NULL)
+    {
+        time_t _t = mktime(&stm);
+        tv.tv_sec = _t;
+        if(settimeofday(&tv, NULL)) {
+            printf("Set time fail:%d");
+            return -1;
+        } else {
+            printf("Set time to %s.", str_time);
+            return 0;
+        }
+    } else {
+        printf("Set time fail.");
+        return -1;
+    }
+    return 0;
+}
+
+
+
+static void* ntp_pthread_run()
+{
+    // Waitting for network connected.
+    while(mbtk_net_state_get() == MBTK_NET_STATE_OFF) {
+        sleep(1);
+    }
+    printf("Network is connected.");
+
+    char time_type[10];
+    while(1){
+        memset(time_type, 0, 10);
+        property_get("persist.mbtk.time_type", time_type, "0");
+    	if(atoi(time_type) == MBTK_TIME_TYPE_NTP) // NTP time
+    	{
+            char time_str[100] = {0};
+            time_t time = 0;
+            while((time = (time_t)mbtk_at_systime()) == 0) {
+                usleep(100000);
+            }
+            struct tm *tm_t;
+            tm_t = localtime(&time);
+            strftime(time_str,128,"%F %T",tm_t);
+
+            // NTP time
+            metis_strptime(time_str);
+            break;
+    	} else {
+            break;
+    	}
+
+        sleep(64); // Sleep 64s.
+    }
+    return NULL;
+}
+
+int set_time_user(char* data_time_str)
+{
+
+    int ret = 0;
+    if(strlen(data_time_str) > 0)
+    {
+        ret = metis_strptime(data_time_str);
+    }
+
+    return ret;
+}
+
+
+//MBTK_TIME_TYPE_CELL = 0,  //NITZ
+//MBTK_TIME_TYPE_NTP,
+//MBTK_TIME_TYPE_GNSS,
+//MBTK_TIME_TYPE_USER
+void set_time_type(int mbtk_time_type)
+{
+    char type_str[10] = {0};
+    sprintf(type_str, "%d", mbtk_time_type);
+    property_set("persist.mbtk.time_type", type_str);
+
+    return;
+}
+
+
+
 
 int ntp_sync_time(int enable)
 {
     UNUSED(enable);
-
+    if(enable)
+    {
+        set_time_type(MBTK_TIME_TYPE_NTP);
+        ntp_pthread_run();
+    }
     return 0;
 }
 
+//enable set time from nitz
 int modem_time_enable(int enable)
 {
     UNUSED(enable);
 
+    if(enable)
+    {
+        set_time_type(MBTK_TIME_TYPE_CELL);
+    }
     return 0;
 }
 
+
+//enable set time from gnss
 int gnss_time_enable(int enable)
 {
     UNUSED(enable);
-
+    if(enable)
+    {
+        set_time_type(MBTK_TIME_TYPE_GNSS);
+    }
     return 0;
 }
 
+
+//enable set time from user
 int user_set_time(char* date, char* time)
 {
     UNUSED(date);
     UNUSED(time);
+    if(date == NULL || time == NULL)
+    {
+        return -1;
+    }
 
-    return 0;
+    int ret = 0;
+    char time_str[128] ={0};
+    memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
+
+    char *p = time;
+    char *p1 = strstr(p, ":");
+    char *p2 = strstr(p1+1, ":");
+    if(p2 == NULL)
+    {
+        sprintf(time_str, "%s %s:00", date, time);  //2023-11-30 11:30
+        set_time_type(MBTK_TIME_TYPE_USER);
+        ret = set_time_user(time_str);
+    }else
+    {
+        sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
+        set_time_type(MBTK_TIME_TYPE_USER);
+        ret = set_time_user(time_str);
+    }
+
+    return ret;
 }
 
-int lynq_sync_time_from_rtc(void)
-{
 
-    return 0;
-}
-
+//check sysytem type
 int lynq_get_time_src_status (time_src_status_s * time_src)
 {
     UNUSED(time_src);
+    int type = 0;
+    char time_type[] ={0};
+    property_get("persist.mbtk.time_type", time_type, "0");
 
+    type = atoi(time_type);
+    printf("time_type :%d", type);
+    if(type == MBTK_TIME_TYPE_NTP){
+        time_src->ntp = 1;
+    }else if(type == MBTK_TIME_TYPE_CELL){
+        time_src->nitz = 1;
+    }else if(type == MBTK_TIME_TYPE_GNSS){
+        time_src->gnss = 1;
+    }else{
+
+    }
     return 0;
 }
 
+// RTC TIME set to system
+int lynq_sync_time_from_rtc(void)
+{
+    system("hwclock --hctosys");
+    return 0;
+}
+
+// system time set to RTC
 int lynq_set_rtc_time(void)
 {
-
+    system("hwclock --systohc");
     return 0;
 }
 
@@ -56,3 +257,4 @@
     return 0;
 }
 
+