blob: 57383a1c72f94bab6180d6e817b523a03d455771 [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"
luojian252d7bc2024-09-24 16:32:38 +080022#include "mbtk_log.h"
luojin071b3852024-01-15 15:58:34 +080023
24
25#define MBTK_AT_NTP_LEN_MAX 128
26
27
28
29
30typedef enum {
r.xiao8b074132024-02-22 23:57:50 -080031 LYNQ_TIME_TYPE_CELL = 0, //NITZ
32 LYNQ_TIME_TYPE_NTP,
33 LYNQ_TIME_TYPE_GNSS,
34 LYNQ_TIME_TYPE_USER,
35
36 LYNQ_TIME_TYPE_UNUSE
37} lynq_time_type_enum;
luojin071b3852024-01-15 15:58:34 +080038
39//enable set time from ntp
40int ntp_sync_time(int enable);
41//enable set time from nitz
42int modem_time_enable(int enable);
43//enable set time from gnss
44int gnss_time_enable(int enable);
45//enable set time from user
46int user_set_time(char* date, char* time);
47// RTC TIME set to system
48int lynq_sync_time_from_rtc(void);
49//check sysytem type
50int lynq_get_time_src_status (time_src_status_s * time_src);
51// system time set to RTC
52int lynq_set_rtc_time(void);
53// get RTC time
54int lynq_get_rtc_time(unsigned long *ulsec);
55
luojian799341e2024-09-25 17:58:07 +080056int sync_time_flag = 0;
luojin071b3852024-01-15 15:58:34 +080057
luojin071b3852024-01-15 15:58:34 +080058//int req_time_set(int type, char *time, int *cme_err);
59static int metis_strptime(char *str_time)
60{
luojian252d7bc2024-09-24 16:32:38 +080061 LOGD("%s(), str_time:%s\n", __FUNCTION__, str_time);
luojin071b3852024-01-15 15:58:34 +080062 struct tm stm;
63 char dateTime[30];
64 struct timeval tv;
65 if(strptime(str_time, "%Y-%m-%d %H:%M:%S",&stm) != NULL)
66 {
luojian88976a32024-08-14 17:40:01 +080067 time_t _t = (long)mktime(&stm);
68 tzset();
luojin071b3852024-01-15 15:58:34 +080069 tv.tv_sec = _t;
luojian88976a32024-08-14 17:40:01 +080070 tv.tv_usec = 0;
71 if(_t == -1)
72 {
luojian252d7bc2024-09-24 16:32:38 +080073 LOGD("Set time :%s", str_time);
74 LOGD("timestamp:%ld", _t);
75 LOGD("mktime error, reason: %s\n", strerror(errno));
luojin071b3852024-01-15 15:58:34 +080076 return -1;
luojin071b3852024-01-15 15:58:34 +080077 }
luojian88976a32024-08-14 17:40:01 +080078
79 if(settimeofday(&tv, NULL)) {
luojian252d7bc2024-09-24 16:32:38 +080080 LOGD("Set time :%s", str_time);
81 LOGD("timestamp:%ld", _t);
82 LOGD("mktime error, reason: %s\n", strerror(errno));
luojian88976a32024-08-14 17:40:01 +080083 return -1;
84 }
85
luojian252d7bc2024-09-24 16:32:38 +080086 LOGD("Success Set time to %s.\n", str_time);
luojian88976a32024-08-14 17:40:01 +080087 return 0;
88
luojin071b3852024-01-15 15:58:34 +080089 } else {
luojian252d7bc2024-09-24 16:32:38 +080090 LOGD("Set time fail.");
luojin071b3852024-01-15 15:58:34 +080091 return -1;
92 }
93 return 0;
94}
95
96
liuyangbfc4d7c2024-05-07 10:44:28 +080097static void* ntp_pthread_run(int* ntp_flag)
luojin071b3852024-01-15 15:58:34 +080098{
liuyang0a15e082024-04-19 17:28:49 +080099 if (mbtk_net_state_get() == MBTK_NET_STATE_OFF)
100 {
luojian252d7bc2024-09-24 16:32:38 +0800101 LOGD("Network is disconnected. Set time fail.");
liuyangbfc4d7c2024-05-07 10:44:28 +0800102 if(NULL != ntp_flag)
103 {
104 *ntp_flag = -1;
105 }
liuyang0a15e082024-04-19 17:28:49 +0800106 return NULL;
luojin071b3852024-01-15 15:58:34 +0800107 }
luojian252d7bc2024-09-24 16:32:38 +0800108 LOGD("Network is connected.");
luojin071b3852024-01-15 15:58:34 +0800109
110 char time_type[10];
111 while(1){
112 memset(time_type, 0, 10);
113 property_get("persist.mbtk.time_type", time_type, "0");
r.xiao8b074132024-02-22 23:57:50 -0800114 if(atoi(time_type) == LYNQ_TIME_TYPE_NTP) // NTP time
luojin071b3852024-01-15 15:58:34 +0800115 {
116 char time_str[100] = {0};
117 time_t time = 0;
liuyangd0f7fdb2024-04-22 13:53:23 +0800118 if((time = (time_t)mbtk_at_systime()) == 0)
liuyang54582902024-04-19 18:06:25 +0800119 {
luojian252d7bc2024-09-24 16:32:38 +0800120 LOGD("NTP client fail!\n");
liuyangbfc4d7c2024-05-07 10:44:28 +0800121 if(NULL != ntp_flag)
122 {
123 *ntp_flag = -1;
124 }
liuyang54582902024-04-19 18:06:25 +0800125 return NULL;
luojin071b3852024-01-15 15:58:34 +0800126 }
luojian88976a32024-08-14 17:40:01 +0800127#if 1
128 struct tm CurlocalTime;
129 localtime_r(&time, &CurlocalTime);
luojianf7c8a6a2024-08-15 13:29:56 +0800130 // CurlocalTime.tm_hour += 8; //cst
luojian88976a32024-08-14 17:40:01 +0800131 char dateTime[30];
132 strftime(dateTime, 30, "%Y-%m-%d %H:%M:%S %A", &CurlocalTime);
133
luojianf7c8a6a2024-08-15 13:29:56 +0800134 // printf("dateTime:%s, %ld\n", dateTime, time+28800); //cst
luojian252d7bc2024-09-24 16:32:38 +0800135 LOGD("dateTime:%s, %ld\n", dateTime, time);
luojian88976a32024-08-14 17:40:01 +0800136
137 struct timeval tv;
138 tv.tv_sec = time;
luojianf7c8a6a2024-08-15 13:29:56 +0800139 // tv.tv_sec += 28800; //cst
luojian88976a32024-08-14 17:40:01 +0800140 tv.tv_usec = 0;
141
142 if(settimeofday(&tv, NULL)) {
luojian252d7bc2024-09-24 16:32:38 +0800143 LOGD("Set time :%s", dateTime);
144 LOGD("timestamp:%ld, tv.tv_sec:%ld\n", time, tv.tv_sec);
luojian88976a32024-08-14 17:40:01 +0800145
146 if(settimeofday(&tv, NULL)) {
147 *ntp_flag = -1;
luojian252d7bc2024-09-24 16:32:38 +0800148 LOGD("mktime error, reason: %s\n", strerror(errno));
luojian88976a32024-08-14 17:40:01 +0800149 return NULL;
150 }
151 }
luojian252d7bc2024-09-24 16:32:38 +0800152 LOGD("Set time success\n");
luojian88976a32024-08-14 17:40:01 +0800153 lynq_set_rtc_time();
154#else
155
luojin071b3852024-01-15 15:58:34 +0800156 struct tm *tm_t;
157 tm_t = localtime(&time);
luojian88976a32024-08-14 17:40:01 +0800158 tm_t->tm_hour += 8;
luojin071b3852024-01-15 15:58:34 +0800159 strftime(time_str,128,"%F %T",tm_t);
160
161 // NTP time
luojian88976a32024-08-14 17:40:01 +0800162 if(metis_strptime(time_str))
163 {
164 *ntp_flag = -1;
165 return NULL;
166 }
167#endif
luojin071b3852024-01-15 15:58:34 +0800168 break;
169 } else {
170 break;
171 }
172
173 sleep(64); // Sleep 64s.
174 }
liuyangbfc4d7c2024-05-07 10:44:28 +0800175 if(NULL != ntp_flag)
176 {
177 *ntp_flag = 0;
178 }
luojin071b3852024-01-15 15:58:34 +0800179 return NULL;
180}
181
182int set_time_user(char* data_time_str)
183{
184
185 int ret = 0;
186 if(strlen(data_time_str) > 0)
187 {
188 ret = metis_strptime(data_time_str);
189 }
190
191 return ret;
192}
193
194
195//MBTK_TIME_TYPE_CELL = 0, //NITZ
196//MBTK_TIME_TYPE_NTP,
197//MBTK_TIME_TYPE_GNSS,
198//MBTK_TIME_TYPE_USER
199void set_time_type(int mbtk_time_type)
200{
201 char type_str[10] = {0};
202 sprintf(type_str, "%d", mbtk_time_type);
203 property_set("persist.mbtk.time_type", type_str);
204
205 return;
206}
207
208
209
b.liu5fa9e772023-11-23 18:00:55 +0800210
211int ntp_sync_time(int enable)
212{
liuyang1ce96df2024-05-29 19:14:34 +0800213 if(0 != enable && 1 != enable)
214 {
215 return -1;
216 }
b.liu5fa9e772023-11-23 18:00:55 +0800217 UNUSED(enable);
liuyangbfc4d7c2024-05-07 10:44:28 +0800218 int ntp_status = 0;
luojian88976a32024-08-14 17:40:01 +0800219 int ret = 0;
luojian799341e2024-09-25 17:58:07 +0800220 sync_time_flag = 0;
luojin071b3852024-01-15 15:58:34 +0800221 if(enable)
222 {
luojian88976a32024-08-14 17:40:01 +0800223 set_time_type(LYNQ_TIME_TYPE_NTP);
liuyangbfc4d7c2024-05-07 10:44:28 +0800224 ntp_pthread_run(&ntp_status);
225 if(ntp_status == 0)
226 {
luojian88976a32024-08-14 17:40:01 +0800227 ret = 0;
luojian799341e2024-09-25 17:58:07 +0800228 sync_time_flag = 0;
liuyangbfc4d7c2024-05-07 10:44:28 +0800229 }
230 else
231 {
luojian88976a32024-08-14 17:40:01 +0800232 ret = -1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800233 set_time_type(LYNQ_TIME_TYPE_UNUSE);
luojian799341e2024-09-25 17:58:07 +0800234 sync_time_flag = -1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800235 }
luojin071b3852024-01-15 15:58:34 +0800236 }
r.xiao8b074132024-02-22 23:57:50 -0800237 else
238 {
239 set_time_type(LYNQ_TIME_TYPE_UNUSE);
240 }
241
luojian88976a32024-08-14 17:40:01 +0800242 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800243}
244
luojin071b3852024-01-15 15:58:34 +0800245//enable set time from nitz
b.liu5fa9e772023-11-23 18:00:55 +0800246int modem_time_enable(int enable)
247{
248 UNUSED(enable);
luojian799341e2024-09-25 17:58:07 +0800249 sync_time_flag = 0;
b.liu5fa9e772023-11-23 18:00:55 +0800250
luojin071b3852024-01-15 15:58:34 +0800251 if(enable)
252 {
r.xiao8b074132024-02-22 23:57:50 -0800253 set_time_type(LYNQ_TIME_TYPE_CELL);
254 }
255 else
256 {
257 set_time_type(LYNQ_TIME_TYPE_UNUSE);
luojin071b3852024-01-15 15:58:34 +0800258 }
b.liu5fa9e772023-11-23 18:00:55 +0800259 return 0;
260}
261
luojin071b3852024-01-15 15:58:34 +0800262
263//enable set time from gnss
b.liu5fa9e772023-11-23 18:00:55 +0800264int gnss_time_enable(int enable)
265{
266 UNUSED(enable);
luojian799341e2024-09-25 17:58:07 +0800267 sync_time_flag = 0;
luojin071b3852024-01-15 15:58:34 +0800268 if(enable)
269 {
r.xiao8b074132024-02-22 23:57:50 -0800270 set_time_type(LYNQ_TIME_TYPE_GNSS);
luojin071b3852024-01-15 15:58:34 +0800271 }
r.xiao8b074132024-02-22 23:57:50 -0800272 else
273 {
274 set_time_type(LYNQ_TIME_TYPE_UNUSE);
275 }
276
b.liu5fa9e772023-11-23 18:00:55 +0800277 return 0;
278}
279
luojin071b3852024-01-15 15:58:34 +0800280
281//enable set time from user
b.liu5fa9e772023-11-23 18:00:55 +0800282int user_set_time(char* date, char* time)
283{
284 UNUSED(date);
285 UNUSED(time);
luojin071b3852024-01-15 15:58:34 +0800286 if(date == NULL || time == NULL)
287 {
288 return -1;
289 }
b.liu5fa9e772023-11-23 18:00:55 +0800290
luojin071b3852024-01-15 15:58:34 +0800291 int ret = 0;
292 char time_str[128] ={0};
293 memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
294
295 char *p = time;
296 char *p1 = strstr(p, ":");
297 char *p2 = strstr(p1+1, ":");
298 if(p2 == NULL)
299 {
300 sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30
r.xiao8b074132024-02-22 23:57:50 -0800301 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800302 ret = set_time_user(time_str);
303 }else
304 {
305 sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
r.xiao8b074132024-02-22 23:57:50 -0800306 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800307 ret = set_time_user(time_str);
308 }
309
310 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800311}
312
b.liu5fa9e772023-11-23 18:00:55 +0800313
luojin071b3852024-01-15 15:58:34 +0800314//check sysytem type
b.liu5fa9e772023-11-23 18:00:55 +0800315int lynq_get_time_src_status (time_src_status_s * time_src)
316{
317 UNUSED(time_src);
luojin071b3852024-01-15 15:58:34 +0800318 int type = 0;
319 char time_type[] ={0};
320 property_get("persist.mbtk.time_type", time_type, "0");
b.liu5fa9e772023-11-23 18:00:55 +0800321
luojin071b3852024-01-15 15:58:34 +0800322 type = atoi(time_type);
323 printf("time_type :%d", type);
liuyangbfc4d7c2024-05-07 10:44:28 +0800324 if(type == LYNQ_TIME_TYPE_NTP)
325 {
luojin071b3852024-01-15 15:58:34 +0800326 time_src->ntp = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800327 time_src->nitz = 0;
328 time_src->gnss = 0;
329 }
330 else if(type == LYNQ_TIME_TYPE_CELL)
331 {
332 time_src->ntp = 0;
luojin071b3852024-01-15 15:58:34 +0800333 time_src->nitz = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800334 time_src->gnss = 0;
335 }
336 else if(type == LYNQ_TIME_TYPE_GNSS)
337 {
338 time_src->ntp = 0;
339 time_src->nitz = 0;
luojin071b3852024-01-15 15:58:34 +0800340 time_src->gnss = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800341 }
342 else if(type == LYNQ_TIME_TYPE_UNUSE)
343 {
r.xiao8b074132024-02-22 23:57:50 -0800344 time_src->ntp = 0;
345 time_src->nitz = 0;
346 time_src->gnss = 0;
luojin071b3852024-01-15 15:58:34 +0800347 }
liuyangbfc4d7c2024-05-07 10:44:28 +0800348
b.liu5fa9e772023-11-23 18:00:55 +0800349 return 0;
350}
351
luojin071b3852024-01-15 15:58:34 +0800352// RTC TIME set to system
353int lynq_sync_time_from_rtc(void)
354{
355 system("hwclock --hctosys");
356 return 0;
357}
358
359// system time set to RTC
b.liu5fa9e772023-11-23 18:00:55 +0800360int lynq_set_rtc_time(void)
361{
luojian9b9a9282024-07-04 18:56:21 +0800362// system("hwclock --systohc");
363 system("hwclock -w rtc0");
b.liu5fa9e772023-11-23 18:00:55 +0800364 return 0;
365}
366
367int lynq_get_rtc_time(unsigned long *ulsec)
368{
369 UNUSED(ulsec);
370
371 return 0;
372}
373
luojian799341e2024-09-25 17:58:07 +0800374int get_sync_time_result(void )
375{
376
377 return sync_time_flag;
378}
379
380
381
luojin071b3852024-01-15 15:58:34 +0800382
luojian252d7bc2024-09-24 16:32:38 +0800383