blob: 29937bd14aaa4a92ae0e4530dde365279ce27823 [file] [log] [blame]
b.liu68a94c92025-05-24 12:53:41 +08001#include "lynq_systime.h"
2#include "mbtk_type.h"
3#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#include "mbtk_log.h"
23#include "mbtk_utils.h"
24
25
26#define MBTK_AT_NTP_LEN_MAX 128
27
28
29
30
31typedef enum {
32 LYNQ_TIME_TYPE_CELL = 0, //NITZ
33 LYNQ_TIME_TYPE_NTP,
34 LYNQ_TIME_TYPE_GNSS,
35 LYNQ_TIME_TYPE_USER,
36
37 LYNQ_TIME_TYPE_UNUSE
38} lynq_time_type_enum;
39
40//enable set time from ntp
41int ntp_sync_time(int enable);
42//enable set time from nitz
43int modem_time_enable(int enable);
44//enable set time from gnss
45int gnss_time_enable(int enable);
46//enable set time from user
47int user_set_time(char* date, char* time);
48// RTC TIME set to system
49int lynq_sync_time_from_rtc(void);
50//check sysytem type
51int lynq_get_time_src_status (time_src_status_s * time_src);
52// system time set to RTC
53int lynq_set_rtc_time(void);
54// get RTC time
55int lynq_get_rtc_time(unsigned long *ulsec);
56
57int sync_time_flag = 0;
58
59//int req_time_set(int type, char *time, int *cme_err);
60static int metis_strptime(char *str_time)
61{
62 LOGD("%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 = (long)mktime(&stm);
69 tzset();
70 tv.tv_sec = _t;
71 tv.tv_usec = 0;
72 if(_t == -1)
73 {
74 LOGD("Set time :%s", str_time);
75 LOGD("timestamp:%ld", _t);
76 LOGD("mktime error, reason: %s\n", strerror(errno));
77 return -1;
78 }
79
80 if(settimeofday(&tv, NULL)) {
81 LOGD("Set time :%s", str_time);
82 LOGD("timestamp:%ld", _t);
83 LOGD("mktime error, reason: %s\n", strerror(errno));
84 return -1;
85 }
86
87 LOGD("Success Set time to %s.\n", str_time);
88 return 0;
89
90 } else {
91 LOGD("Set time fail.");
92 return -1;
93 }
94 return 0;
95}
96
97
98static void* ntp_pthread_run(int* ntp_flag)
99{
100 if (mbtk_net_state_get() == MBTK_NET_STATE_OFF)
101 {
102 LOGD("Network is disconnected. Set time fail.");
103 if(NULL != ntp_flag)
104 {
105 *ntp_flag = -1;
106 }
107 return NULL;
108 }
109 LOGD("Network is connected.");
110
111 char time_type[10];
112 while(1){
113 memset(time_type, 0, 10);
114 property_get("persist.mbtk.time_type", time_type, "0");
115 if(atoi(time_type) == LYNQ_TIME_TYPE_NTP) // NTP time
116 {
117// char time_str[100] = {0};
118 time_t time = 0;
119 if((time = (time_t)mbtk_at_systime()) == 0)
120 {
121 LOGD("NTP client fail!\n");
122 if(NULL != ntp_flag)
123 {
124 *ntp_flag = -1;
125 }
126 return NULL;
127 }
128#if 1
129 struct tm CurlocalTime;
130 localtime_r(&time, &CurlocalTime);
131 // CurlocalTime.tm_hour += 8; //cst
132 char dateTime[30];
133 strftime(dateTime, 30, "%Y-%m-%d %H:%M:%S %A", &CurlocalTime);
134
135 // printf("dateTime:%s, %ld\n", dateTime, time+28800); //cst
136 LOGD("dateTime:%s, %ld\n", dateTime, time);
137
138 struct timeval tv;
139 tv.tv_sec = time;
140 // tv.tv_sec += 28800; //cst
141 tv.tv_usec = 0;
142
143 if(settimeofday(&tv, NULL)) {
144 LOGD("Set time :%s", dateTime);
145 LOGD("timestamp:%ld, tv.tv_sec:%ld\n", time, tv.tv_sec);
146
147 if(settimeofday(&tv, NULL)) {
148 *ntp_flag = -1;
149 LOGD("mktime error, reason: %s\n", strerror(errno));
150 return NULL;
151 }
152 }
153 LOGD("Set time success\n");
154 lynq_set_rtc_time();
155#else
156
157 struct tm *tm_t;
158 tm_t = localtime(&time);
159 tm_t->tm_hour += 8;
160 strftime(time_str,128,"%F %T",tm_t);
161
162 // NTP time
163 if(metis_strptime(time_str))
164 {
165 *ntp_flag = -1;
166 return NULL;
167 }
168#endif
169 break;
170 } else {
171 break;
172 }
173
174 sleep(64); // Sleep 64s.
175 }
176 if(NULL != ntp_flag)
177 {
178 *ntp_flag = 0;
179 }
180 return NULL;
181}
182
183int set_time_user(char* data_time_str)
184{
185
186 int ret = 0;
187 if(strlen(data_time_str) > 0)
188 {
189 ret = metis_strptime(data_time_str);
190 }
191
192 return ret;
193}
194
195
196//MBTK_TIME_TYPE_CELL = 0, //NITZ
197//MBTK_TIME_TYPE_NTP,
198//MBTK_TIME_TYPE_GNSS,
199//MBTK_TIME_TYPE_USER
200void set_time_type(int mbtk_time_type)
201{
202 char type_str[10] = {0};
203 sprintf(type_str, "%d", mbtk_time_type);
204 property_set("persist.mbtk.time_type", type_str);
205
206 return;
207}
208
209static int mbtk_get_gnss_time_set_flag() {
210 int type = 0;
211 char time_type[] ={0};
212 property_get("persist.mbtk.gnss_time_type", time_type, "0");
213
214 type = atoi(time_type);
215 LOGD("persist.mbtk.gnss_time_type :%d\n", type);
216 return type;
217}
218
219
220int ntp_sync_time(int enable)
221{
222 if(0 != enable && 1 != enable)
223 {
224 return -1;
225 }
226 UNUSED(enable);
227 int ntp_status = 0;
228 int ret = 0;
229 sync_time_flag = 0;
230 if(enable)
231 {
232 set_time_type(LYNQ_TIME_TYPE_NTP);
233 ntp_pthread_run(&ntp_status);
234 if(ntp_status == 0)
235 {
236 ret = 0;
237 sync_time_flag = 0;
238 }
239 else
240 {
241 ret = -1;
242 set_time_type(LYNQ_TIME_TYPE_UNUSE);
243 sync_time_flag = -1;
244 }
245 }
246 else
247 {
248 set_time_type(LYNQ_TIME_TYPE_UNUSE);
249 }
250
251 return ret;
252}
253
254//enable set time from nitz
255int modem_time_enable(int enable)
256{
257 if(0 != enable && 1 != enable)
258 {
259 return -1;
260 }
261 UNUSED(enable);
262 sync_time_flag = 0;
263
264 if(enable)
265 {
266 set_time_type(LYNQ_TIME_TYPE_CELL);
267 }
268 else
269 {
270 set_time_type(LYNQ_TIME_TYPE_UNUSE);
271 }
272 return 0;
273}
274
275
276//enable set time from gnss
277int gnss_time_enable(int enable)
278{
279 if(0 != enable && 1 != enable)
280 {
281 return -1;
282 }
283 UNUSED(enable);
284 sync_time_flag = 0;
285 if(enable)
286 {
287 set_time_type(LYNQ_TIME_TYPE_GNSS);
288 }
289 else
290 {
291 set_time_type(LYNQ_TIME_TYPE_UNUSE);
292 }
293
294 return 0;
295}
296
297
298//enable set time from user
299int user_set_time(char* date, char* time)
300{
301 UNUSED(date);
302 UNUSED(time);
303 if(date == NULL || time == NULL)
304 {
305 return -1;
306 }
307
308 int ret = 0;
309 char time_str[128] ={0};
310 memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
311
312 char *p = time;
313 char *p1 = strstr(p, ":");
314 char *p2 = strstr(p1+1, ":");
315 if(p2 == NULL)
316 {
317 sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30
318 set_time_type(LYNQ_TIME_TYPE_USER);
319 ret = set_time_user(time_str);
320 }else
321 {
322 sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
323 set_time_type(LYNQ_TIME_TYPE_USER);
324 ret = set_time_user(time_str);
325 }
326
327 return ret;
328}
329
330
331//check sysytem type
332int lynq_get_time_src_status (time_src_status_s * time_src)
333{
334 UNUSED(time_src);
335 int type = 0;
336 char time_type[] ={0};
337 property_get("persist.mbtk.time_type", time_type, "0");
338
339 type = atoi(time_type);
340 printf("time_type :%d", type);
341 if(type == LYNQ_TIME_TYPE_NTP)
342 {
343 time_src->ntp = 1;
344 time_src->nitz = 0;
345 time_src->gnss = 0;
346 }
347 else if(type == LYNQ_TIME_TYPE_CELL)
348 {
349 time_src->ntp = 0;
350 time_src->nitz = 1;
351 time_src->gnss = 0;
352 }
353 else if(type == LYNQ_TIME_TYPE_GNSS)
354 {
355 time_src->ntp = 0;
356 time_src->nitz = 0;
357 time_src->gnss = 1;
358 }
359 else if(type == LYNQ_TIME_TYPE_UNUSE)
360 {
361 time_src->ntp = 0;
362 time_src->nitz = 0;
363 time_src->gnss = 0;
364 }
365
366 return 0;
367}
368
369// RTC TIME set to system
370int lynq_sync_time_from_rtc(void)
371{
372 mbtk_system("hwclock --hctosys");
373 return 0;
374}
375
376// system time set to RTC
377int lynq_set_rtc_time(void)
378{
379// system("hwclock --systohc");
380 mbtk_system("hwclock -w rtc0");
381 return 0;
382}
383
384int lynq_get_rtc_time(unsigned long *ulsec)
385{
386 UNUSED(ulsec);
387
388 return 0;
389}
390
391
392int get_sync_time_result(void )
393{
394 int type = 0;
395 int gnss_status = 0;
396 char time_type[] ={0};
397 property_get("persist.mbtk.time_type", time_type, "0");
398 type = atoi(time_type);
399 printf("time_type :%d", type);
400 if(type == LYNQ_TIME_TYPE_GNSS)
401 {
402 gnss_status = mbtk_get_gnss_time_set_flag();
403 if(gnss_status) //success
404 {
405 sync_time_flag = 0;
406 }
407 else{
408 sync_time_flag = -1;
409 }
410 }
411 LOGD("mbtk_gnss_time_set_flag :%d", mbtk_get_gnss_time_set_flag());
412 return sync_time_flag;
413}
414
415
416
417
418