blob: 5b59a1893a8941a84517c691ae430a4dcf0d352e [file] [log] [blame]
b.liu4e243dc2023-11-27 11:20:00 +08001#include "lynq_systime.h"
2#include "mbtk_type.h"
luojin071b3852024-01-15 15:58:34 +08003#include <stdlib.h>
4#include <sys/types.h>
5#include <sys/socket.h>
6#include <stdio.h>
7#include <errno.h>
8#include <netdb.h>
9#include <string.h>
10#include <unistd.h>
11#include <time.h>
12#include <netinet/in.h>
13
14#include <cutils/properties.h>
15#include <sys/time.h>
16
17
18#include "mbtk_ntp.h"
19#include "mbtk_net_control.h"
20#include "lynq_systime.h"
21#include "mbtk_type.h"
22
23
24#define MBTK_AT_NTP_LEN_MAX 128
25
26
27
28
29typedef enum {
r.xiao8b074132024-02-22 23:57:50 -080030 LYNQ_TIME_TYPE_CELL = 0, //NITZ
31 LYNQ_TIME_TYPE_NTP,
32 LYNQ_TIME_TYPE_GNSS,
33 LYNQ_TIME_TYPE_USER,
34
35 LYNQ_TIME_TYPE_UNUSE
36} lynq_time_type_enum;
luojin071b3852024-01-15 15:58:34 +080037
38//enable set time from ntp
39int ntp_sync_time(int enable);
40//enable set time from nitz
41int modem_time_enable(int enable);
42//enable set time from gnss
43int gnss_time_enable(int enable);
44//enable set time from user
45int user_set_time(char* date, char* time);
46// RTC TIME set to system
47int lynq_sync_time_from_rtc(void);
48//check sysytem type
49int lynq_get_time_src_status (time_src_status_s * time_src);
50// system time set to RTC
51int lynq_set_rtc_time(void);
52// get RTC time
53int lynq_get_rtc_time(unsigned long *ulsec);
54
55
56
57
58
59//int req_time_set(int type, char *time, int *cme_err);
60static int metis_strptime(char *str_time)
61{
62 printf("%s(), str_time:%s\n", __FUNCTION__, str_time);
63 struct tm stm;
64 char dateTime[30];
65 struct timeval tv;
66 if(strptime(str_time, "%Y-%m-%d %H:%M:%S",&stm) != NULL)
67 {
68 time_t _t = mktime(&stm);
69 tv.tv_sec = _t;
70 if(settimeofday(&tv, NULL)) {
71 printf("Set time fail:%d");
72 return -1;
73 } else {
74 printf("Set time to %s.", str_time);
75 return 0;
76 }
77 } else {
78 printf("Set time fail.");
79 return -1;
80 }
81 return 0;
82}
83
84
85
86static void* ntp_pthread_run()
87{
88 // Waitting for network connected.
89 while(mbtk_net_state_get() == MBTK_NET_STATE_OFF) {
90 sleep(1);
91 }
92 printf("Network is connected.");
93
94 char time_type[10];
95 while(1){
96 memset(time_type, 0, 10);
97 property_get("persist.mbtk.time_type", time_type, "0");
r.xiao8b074132024-02-22 23:57:50 -080098 if(atoi(time_type) == LYNQ_TIME_TYPE_NTP) // NTP time
luojin071b3852024-01-15 15:58:34 +080099 {
100 char time_str[100] = {0};
101 time_t time = 0;
102 while((time = (time_t)mbtk_at_systime()) == 0) {
103 usleep(100000);
104 }
105 struct tm *tm_t;
106 tm_t = localtime(&time);
107 strftime(time_str,128,"%F %T",tm_t);
108
109 // NTP time
110 metis_strptime(time_str);
111 break;
112 } else {
113 break;
114 }
115
116 sleep(64); // Sleep 64s.
117 }
118 return NULL;
119}
120
121int set_time_user(char* data_time_str)
122{
123
124 int ret = 0;
125 if(strlen(data_time_str) > 0)
126 {
127 ret = metis_strptime(data_time_str);
128 }
129
130 return ret;
131}
132
133
134//MBTK_TIME_TYPE_CELL = 0, //NITZ
135//MBTK_TIME_TYPE_NTP,
136//MBTK_TIME_TYPE_GNSS,
137//MBTK_TIME_TYPE_USER
138void set_time_type(int mbtk_time_type)
139{
140 char type_str[10] = {0};
141 sprintf(type_str, "%d", mbtk_time_type);
142 property_set("persist.mbtk.time_type", type_str);
143
144 return;
145}
146
147
148
b.liu5fa9e772023-11-23 18:00:55 +0800149
150int ntp_sync_time(int enable)
151{
152 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800153 if(enable)
154 {
r.xiao8b074132024-02-22 23:57:50 -0800155 set_time_type(LYNQ_TIME_TYPE_NTP);
luojin071b3852024-01-15 15:58:34 +0800156 ntp_pthread_run();
157 }
r.xiao8b074132024-02-22 23:57:50 -0800158 else
159 {
160 set_time_type(LYNQ_TIME_TYPE_UNUSE);
161 }
162
b.liu5fa9e772023-11-23 18:00:55 +0800163 return 0;
164}
165
luojin071b3852024-01-15 15:58:34 +0800166//enable set time from nitz
b.liu5fa9e772023-11-23 18:00:55 +0800167int modem_time_enable(int enable)
168{
169 UNUSED(enable);
170
luojin071b3852024-01-15 15:58:34 +0800171 if(enable)
172 {
r.xiao8b074132024-02-22 23:57:50 -0800173 set_time_type(LYNQ_TIME_TYPE_CELL);
174 }
175 else
176 {
177 set_time_type(LYNQ_TIME_TYPE_UNUSE);
luojin071b3852024-01-15 15:58:34 +0800178 }
b.liu5fa9e772023-11-23 18:00:55 +0800179 return 0;
180}
181
luojin071b3852024-01-15 15:58:34 +0800182
183//enable set time from gnss
b.liu5fa9e772023-11-23 18:00:55 +0800184int gnss_time_enable(int enable)
185{
186 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800187 if(enable)
188 {
r.xiao8b074132024-02-22 23:57:50 -0800189 set_time_type(LYNQ_TIME_TYPE_GNSS);
luojin071b3852024-01-15 15:58:34 +0800190 }
r.xiao8b074132024-02-22 23:57:50 -0800191 else
192 {
193 set_time_type(LYNQ_TIME_TYPE_UNUSE);
194 }
195
b.liu5fa9e772023-11-23 18:00:55 +0800196 return 0;
197}
198
luojin071b3852024-01-15 15:58:34 +0800199
200//enable set time from user
b.liu5fa9e772023-11-23 18:00:55 +0800201int user_set_time(char* date, char* time)
202{
203 UNUSED(date);
204 UNUSED(time);
luojin071b3852024-01-15 15:58:34 +0800205 if(date == NULL || time == NULL)
206 {
207 return -1;
208 }
b.liu5fa9e772023-11-23 18:00:55 +0800209
luojin071b3852024-01-15 15:58:34 +0800210 int ret = 0;
211 char time_str[128] ={0};
212 memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
213
214 char *p = time;
215 char *p1 = strstr(p, ":");
216 char *p2 = strstr(p1+1, ":");
217 if(p2 == NULL)
218 {
219 sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30
r.xiao8b074132024-02-22 23:57:50 -0800220 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800221 ret = set_time_user(time_str);
222 }else
223 {
224 sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
r.xiao8b074132024-02-22 23:57:50 -0800225 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800226 ret = set_time_user(time_str);
227 }
228
229 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800230}
231
b.liu5fa9e772023-11-23 18:00:55 +0800232
luojin071b3852024-01-15 15:58:34 +0800233//check sysytem type
b.liu5fa9e772023-11-23 18:00:55 +0800234int lynq_get_time_src_status (time_src_status_s * time_src)
235{
236 UNUSED(time_src);
luojin071b3852024-01-15 15:58:34 +0800237 int type = 0;
238 char time_type[] ={0};
239 property_get("persist.mbtk.time_type", time_type, "0");
b.liu5fa9e772023-11-23 18:00:55 +0800240
luojin071b3852024-01-15 15:58:34 +0800241 type = atoi(time_type);
242 printf("time_type :%d", type);
r.xiao8b074132024-02-22 23:57:50 -0800243 if(type == LYNQ_TIME_TYPE_NTP){
luojin071b3852024-01-15 15:58:34 +0800244 time_src->ntp = 1;
r.xiao8b074132024-02-22 23:57:50 -0800245 }else if(type == LYNQ_TIME_TYPE_CELL){
luojin071b3852024-01-15 15:58:34 +0800246 time_src->nitz = 1;
r.xiao8b074132024-02-22 23:57:50 -0800247 }else if(type == LYNQ_TIME_TYPE_GNSS){
luojin071b3852024-01-15 15:58:34 +0800248 time_src->gnss = 1;
r.xiao8b074132024-02-22 23:57:50 -0800249 }else if(type == LYNQ_TIME_TYPE_UNUSE){
250 time_src->ntp = 0;
251 time_src->nitz = 0;
252 time_src->gnss = 0;
luojin071b3852024-01-15 15:58:34 +0800253 }else{
254
255 }
b.liu5fa9e772023-11-23 18:00:55 +0800256 return 0;
257}
258
luojin071b3852024-01-15 15:58:34 +0800259// RTC TIME set to system
260int lynq_sync_time_from_rtc(void)
261{
262 system("hwclock --hctosys");
263 return 0;
264}
265
266// system time set to RTC
b.liu5fa9e772023-11-23 18:00:55 +0800267int lynq_set_rtc_time(void)
268{
luojin071b3852024-01-15 15:58:34 +0800269 system("hwclock --systohc");
b.liu5fa9e772023-11-23 18:00:55 +0800270 return 0;
271}
272
273int lynq_get_rtc_time(unsigned long *ulsec)
274{
275 UNUSED(ulsec);
276
277 return 0;
278}
279
luojin071b3852024-01-15 15:58:34 +0800280