Merge "[Feature][ZXW-103] add lynq-systime-demo code"
diff --git a/cap/zx297520v3/sources/meta-zxic-custom/conf/distro/vehicle_dc.conf b/cap/zx297520v3/sources/meta-zxic-custom/conf/distro/vehicle_dc.conf
index ec00506..d5acc77 100755
--- a/cap/zx297520v3/sources/meta-zxic-custom/conf/distro/vehicle_dc.conf
+++ b/cap/zx297520v3/sources/meta-zxic-custom/conf/distro/vehicle_dc.conf
@@ -232,6 +232,7 @@
         lynq-qser-fota-demo \
         lynq-qser-gnss-demo \
         lynq-qser-network-demo \
+        lynq-systime-demo \
 	"
 
 zxic_app_open += "${@bb.utils.contains('CONFIG_TEL_API_SUPPORT', 'RIL', 'rild', '', d)}"
diff --git a/cap/zx297520v3/sources/meta-zxic-custom/conf/distro/vehicle_dc_ref.conf b/cap/zx297520v3/sources/meta-zxic-custom/conf/distro/vehicle_dc_ref.conf
index c12bcad..23bac63 100755
--- a/cap/zx297520v3/sources/meta-zxic-custom/conf/distro/vehicle_dc_ref.conf
+++ b/cap/zx297520v3/sources/meta-zxic-custom/conf/distro/vehicle_dc_ref.conf
@@ -260,7 +260,9 @@
         lynq-qser-fota-demo \
         lynq-qser-gnss-demo \
         lynq-qser-network-demo \
-        poweralarm-demo"
+        poweralarm-demo \
+        lynq-systime-demo \
+        "
 
 zxic_app_open += "${@bb.utils.contains('CONFIG_TEL_API_SUPPORT', 'RIL', 'rild', '', d)}"
 zxic_app_open += "${@bb.utils.contains('CONFIG_TEL_API_SUPPORT', 'BL', 'tel-svr', '', d)}"
diff --git a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-systime-demo/lynq-systime-demo.bb b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-systime-demo/lynq-systime-demo.bb
new file mode 100644
index 0000000..ca07a8a
--- /dev/null
+++ b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/lynq-systime-demo/lynq-systime-demo.bb
@@ -0,0 +1,36 @@
+#inherit externalsrc package
+#inherit externalsrc package systemd
+DESCRIPTION = "lynq-systime-demo"
+LICENSE = "CLOSED"
+LICENSE = "CLOSED"
+LIC_FILES_CHKSUM = "file://LICENSE;md5=b1e07e8d88e26263e71d3a9e2aa9a2ff"
+DEPENDS += "liblynq-systime"
+#inherit workonsrc
+WORKONSRC = "${TOPDIR}/../src/lynq/packages/apps/lynq-systime-demo"
+FILESEXTRAPATHS_prepend :="${TOPDIR}/../src/lynq/packages/apps:"
+SRC_URI = " \
+          file://lynq-systime-demo \
+          "
+
+SRC-DIR = "${S}/../lynq-systime-demo"
+TARGET_CC_ARCH += "${LDFLAGS}"
+SYSTEMD_PACKAGES = "${PN}"
+#Parameters passed to do_compile()
+EXTRA_OEMAKE = "'TARGET_PLATFORM = ${TARGET_PLATFORM}'\"
+EXTRA_OEMAKE += "'MOBILETEK_RIL_CFG = ${MOBILETEK_RIL_CFG}'"
+
+EXTRA_OEMAKE += "'MOBILETEK_FOTA_CFG = ${MOBILETEK_FOTA_CFG}'"
+
+#INHIBIT_PACKAGE_STRIP = "1"
+do_compile () {
+	if test "${PACKAGE_ARCH}" = "cortexa7hf-vfp-vfpv4-neon" || test "${PACKAGE_ARCH}" = "cortexa7hf-neon-vfpv4"; then
+		oe_runmake all -C ${SRC-DIR} ROOT=${STAGING_DIR_HOST} OFLAGS="--sysroot=${STAGING_DIR_HOST} -mhard-float"
+	else
+		oe_runmake all -C ${SRC-DIR} ROOT=${STAGING_DIR_HOST} OFLAGS="--sysroot=${STAGING_DIR_HOST}"
+	fi
+}
+
+do_install() {
+	install -d ${D}${bindir}/
+	install -m 0755 ${SRC-DIR}/lynq-systime-demo ${D}${bindir}/
+}
diff --git a/cap/zx297520v3/src/lynq/packages/apps/lynq-systime-demo/lynq_systime_demo.cpp b/cap/zx297520v3/src/lynq/packages/apps/lynq-systime-demo/lynq_systime_demo.cpp
new file mode 100755
index 0000000..a52a18a
--- /dev/null
+++ b/cap/zx297520v3/src/lynq/packages/apps/lynq-systime-demo/lynq_systime_demo.cpp
@@ -0,0 +1,253 @@
+/**@File lynq-systime-demo.cpp

+ *

+ * @Brief As a example for liblynq-systime.

+ *

+ * @Author sj.zhang

+ *

+ * @Date 2023-08-15

+ *

+ * @Version V1.0

+ *

+ * @copyright Copyright (c) MobileTek

+ */

+#include <stdio.h>

+#include <dlfcn.h>

+#include <string.h>

+#include <stdlib.h>

+#include <time.h>

+#include "lynq_systime_demo.h"

+

+typedef struct time_source_status

+{

+    int ntp;

+    int nitz;

+    int gnss;

+} time_src_status_s;

+

+#ifdef MOBILETEK_TARGET_PLATFORM_T106

+int (*lynq_sync_time_from_rtc)(void);

+int (*lynq_set_rtc_time)(void);

+int (*lynq_get_rtc_time)(unsigned long *ulsec);

+

+#endif

+void *dlHandle_systime = NULL;

+int (*modem_time_enable)(int enable);

+int (*gnss_time_enable)(int enable);

+int (*ntp_sync_time)(int enable);

+int (*user_set_time)(char *date, char *time);

+int (*lynq_get_time_src_status)(time_src_status_s *time_src);

+

+int lynq_ntp_sync_demo(int argc, char *argv[])

+{

+    int ret = -1;

+    if (argc != 3)

+    {

+        printf("Parameter number wrong !\n");

+        dlclose(dlHandle_systime);

+        return -1;

+    }

+    ntp_sync_time = (int (*)(int enable))dlsym(dlHandle_systime, "ntp_sync_time");

+    if (NULL == ntp_sync_time)

+    {

+        fprintf(stderr, "Failed to dlsym function ntp_sync_time : %s\n", dlerror());

+        dlclose(dlHandle_systime);

+        return -1;

+    }

+    ret = ntp_sync_time(atoi(argv[2]));

+    switch (ret)

+    {

+    case 0:

+        printf("set success!\n");

+        break;

+    case 1:

+        printf("NTP_ALREADY_ENABLE!\n");

+        break;

+    case 2:

+        printf("NTP_ALREADY_DISENABLE!\n");

+        break;

+    default:

+        break;

+    }

+    return ret;

+}

+

+int lynq_modem_sync_demo(int argc, char *argv[])

+{

+    int ret = -1;

+    if (argc != 3)

+    {

+        printf("Parameter number wrong !\n");

+        dlclose(dlHandle_systime);

+        return -1;

+    }

+    modem_time_enable = (int (*)(int enable))dlsym(dlHandle_systime, "modem_time_enable");

+    if (NULL == modem_time_enable)

+    {

+        fprintf(stderr, "Failed to dlsym function modem_time_enable: %s\n", dlerror());

+        dlclose(dlHandle_systime);

+        return -1;

+    }

+    ret = modem_time_enable(atoi(argv[2]));

+    printf("ret = %d\n", ret);

+    if (ret != 0 && ret != 4)

+    {

+        printf("modem_time_enable failed !\n");

+        dlclose(dlHandle_systime);

+        return -1;

+    }

+    return 0;

+}

+

+int lynq_gnss_sync_demo(int argc, char *argv[])

+{

+    int ret = -1;

+    if (argc != 3)

+    {

+        printf("Parameter number wrong !\n");

+        dlclose(dlHandle_systime);

+        return -1;

+    }

+    gnss_time_enable = (int (*)(int enable))dlsym(dlHandle_systime, "gnss_time_enable");

+    if (NULL == gnss_time_enable)

+    {

+        fprintf(stderr, "Failed to dlsym function gnss_time_enable: %s\n", dlerror());

+        dlclose(dlHandle_systime);

+        return -1;

+    }

+    ret = gnss_time_enable(atoi(argv[2]));

+    printf("ret = %d\n", ret);

+    if (ret != 0 && ret != 4)

+    {

+        printf("gnss_time_enable failed !\n");

+        dlclose(dlHandle_systime);

+        return -1;

+    }

+    return ret;

+}

+

+int lynq_user_set_time_demo(int argc, char *argv[])

+{

+    int ret = -1;

+    if (argc != 4)

+    {

+        printf("Parameter number wrong !\n");

+        dlclose(dlHandle_systime);

+        return -1;

+    }

+    user_set_time = (int (*)(char *date, char *time))dlsym(dlHandle_systime, "user_set_time");

+    if (NULL == user_set_time)

+    {

+        fprintf(stderr, "Failed to dlsym function user_set_time: %s\n", dlerror());

+        dlclose(dlHandle_systime);

+        return -1;

+    }

+    ret = user_set_time(argv[2], argv[3]);

+    return ret;

+}

+

+int lynq_get_time_src_status_demo(int arg_c, char *arg_v[])

+{

+    lynq_get_time_src_status = (int (*)(time_src_status_s *time_src))dlsym(dlHandle_systime, "lynq_get_time_src_status");

+    if (NULL == lynq_get_time_src_status)

+    {

+        fprintf(stderr, "Failed to dlsym function lynq_get_time_src_status : %s\n", dlerror());

+        dlclose(dlHandle_systime);

+        return -1;

+    }

+    time_src_status_s time_src = {0};

+    lynq_get_time_src_status(&time_src);

+    printf("time source status are :\nntp=%d\nnitz=%d\ngnss=%d\n", time_src.ntp, time_src.nitz, time_src.gnss);

+    return 0;

+}

+

+int main(int argc, char *argv[])

+{

+    int ret = -1;

+    char *cmd = argv[1];

+    printf("enter lynq_systime_demo \n");

+    dlHandle_systime = dlopen("/lib/liblynq-systime.so", RTLD_NOW);

+    if (!dlHandle_systime)

+    {

+        fprintf(stderr, "Failed to load library: %s\n", dlerror());

+        return 1;

+    }

+

+    if (cmd == NULL)

+    {

+        printf("No command received!\n");

+        dlclose(dlHandle_systime);

+        return 1;

+    }

+    else if (strcmp(cmd, "ntp") == 0)

+    {

+        ret = lynq_ntp_sync_demo(argc, argv);

+    }

+    else if (strcmp(cmd, "nitz") == 0)

+    {

+        ret = lynq_modem_sync_demo(argc, argv);

+    }

+    else if (strcmp(cmd, "gnss") == 0)

+    {

+        ret = lynq_gnss_sync_demo(argc, argv);

+    }

+    else if (strcmp(cmd, "user") == 0)

+    {

+        ret = lynq_user_set_time_demo(argc, argv);

+    }

+#ifdef MOBILETEK_TARGET_PLATFORM_T106

+    else if (strcmp(cmd, "sync_rtc") == 0)

+    {

+        lynq_sync_time_from_rtc = (int (*)(void))dlsym(dlHandle_systime, "lynq_sync_time_from_rtc");

+        if (NULL == lynq_sync_time_from_rtc)

+        {

+            fprintf(stderr, "Failed to dlsym function lynq_sync_time_from_rtc : %s\n", dlerror());

+            dlclose(dlHandle_systime);

+            return -1;

+        }

+        ret = lynq_sync_time_from_rtc();

+    }

+    else if (strcmp(cmd, "rtc_set") == 0)

+    {

+        lynq_set_rtc_time = (int (*)(void))dlsym(dlHandle_systime, "lynq_set_rtc_time");

+        if (NULL == lynq_set_rtc_time)

+        {

+            fprintf(stderr, "Failed to dlsym function lynq_set_rtc_time : %s\n", dlerror());

+            dlclose(dlHandle_systime);

+            return -1;

+        }

+        ret = lynq_set_rtc_time();

+    }

+    else if (strcmp(cmd, "rtc_get") == 0)

+    {

+        lynq_get_rtc_time = (int (*)(unsigned long *ulsec))dlsym(dlHandle_systime, "lynq_get_rtc_time");

+        if (NULL == lynq_get_rtc_time)

+        {

+            fprintf(stderr, "Failed to dlsym function lynq_get_rtc_time : %s\n", dlerror());

+            dlclose(dlHandle_systime);

+            return -1;

+        }

+        unsigned long time_ret;

+        ret = lynq_get_rtc_time(&time_ret);

+        time_t tp = (time_t)time_ret;

+        char *rtc_time = ctime(&tp);

+        printf("rtc_time seconds = %lu\n", time_ret);

+        printf("rtc time = %s\n", rtc_time);

+    }

+#endif

+    else if (strcmp(cmd, "src_status") == 0)

+    {

+        ret = lynq_get_time_src_status_demo(argc, argv);

+    }

+    else

+    {

+        printf("command wrong !\n");

+        dlclose(dlHandle_systime);

+        return -1;

+    }

+    if (0 != ret)

+    {

+        printf("ret = %d\n Command execution failure.\n", ret);

+    }

+    dlclose(dlHandle_systime);

+    return ret;

+}

diff --git a/cap/zx297520v3/src/lynq/packages/apps/lynq-systime-demo/makefile b/cap/zx297520v3/src/lynq/packages/apps/lynq-systime-demo/makefile
new file mode 100755
index 0000000..890f85a
--- /dev/null
+++ b/cap/zx297520v3/src/lynq/packages/apps/lynq-systime-demo/makefile
@@ -0,0 +1,58 @@
+SHELL = /bin/sh

+RM = rm -f

+

+LOCAL_CFLAGS := -Wall \

+                -std=gnu++14 \

+                -g -Os \

+                -flto \

+                -fpermissive \

+

+ifeq ($(strip $(TARGET_PLATFORM)), T106)

+LOCAL_CFLAGS += -DBINDER_IPC_32BIT=1 -DHAVE_ENDIAN_H -DHAVE_PTHREADS -DHAVE_SYS_UIO_H -DHAVE_POSIX_FILEMAP -DHAVE_STRLCPY -DHAVE_PRCTL -DHAVE_MEMSET16 -DHAVE_MEMSET32 -DANDROID_SMP=0

+endif

+

+ifeq ($(strip $(TARGET_PLATFORM)), T106)

+LOCAL_CFLAGS += -DMOBILETEK_TARGET_PLATFORM_T106

+endif

+

+LOCAL_CFLAGS += -Wno-error=format-security

+

+LOCAL_PATH   = .

+

+LOCAL_C_INCLUDES = \

+  -I. \

+  -I$(LOCAL_PATH)/include/ \

+  -I$(ROOT)$(includedir)/ \

+  -I$(ROOT)$(includedir)/include/ \

+

+

+

+LOCAL_LIBS := \

+    -L. \

+    -ldl \

+    -lstdc++ \

+    -lpthread \

+    -llynq-systime \

+

+

+

+SOURCES = lynq_systime_demo.cpp

+

+EXECUTABLE = lynq-systime-demo

+

+OBJECTS=$(SOURCES:.cpp=.o)

+

+OBJECTS_TOOL=$(SOURCES_TOOL:.cpp=.o)

+all: $(EXECUTABLE) 

+

+$(EXECUTABLE): $(OBJECTS)

+	$(CXX) $(OBJECTS) $(LOCAL_LIBS) $(LOCAL_CFLAGS) $(LOCAL_C_INCLUDES) -o $@

+

+%.o : %.cpp

+	$(CXX) $(LOCAL_C_INCLUDES) $(LOCAL_CFLAGS) $(LOCAL_LIBS) -o $@ -c $<

+

+.PHONY: clean

+clean:

+	$(RM) $(OBJECTS) $(EXECUTABLE)

+	$(RM) $(OBJECTS_TOOL) $(EXECUTABLE)

+