blob: 269527da11b8f6ce52383e113ef071c9c9fe2630 [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
56
57
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;
luojin071b3852024-01-15 15:58:34 +0800220 if(enable)
221 {
luojian88976a32024-08-14 17:40:01 +0800222 set_time_type(LYNQ_TIME_TYPE_NTP);
liuyangbfc4d7c2024-05-07 10:44:28 +0800223 ntp_pthread_run(&ntp_status);
224 if(ntp_status == 0)
225 {
luojian88976a32024-08-14 17:40:01 +0800226 ret = 0;
liuyangbfc4d7c2024-05-07 10:44:28 +0800227 }
228 else
229 {
luojian88976a32024-08-14 17:40:01 +0800230 ret = -1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800231 set_time_type(LYNQ_TIME_TYPE_UNUSE);
232 }
luojin071b3852024-01-15 15:58:34 +0800233 }
r.xiao8b074132024-02-22 23:57:50 -0800234 else
235 {
236 set_time_type(LYNQ_TIME_TYPE_UNUSE);
237 }
238
luojian88976a32024-08-14 17:40:01 +0800239 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800240}
241
luojin071b3852024-01-15 15:58:34 +0800242//enable set time from nitz
b.liu5fa9e772023-11-23 18:00:55 +0800243int modem_time_enable(int enable)
244{
245 UNUSED(enable);
246
luojin071b3852024-01-15 15:58:34 +0800247 if(enable)
248 {
r.xiao8b074132024-02-22 23:57:50 -0800249 set_time_type(LYNQ_TIME_TYPE_CELL);
250 }
251 else
252 {
253 set_time_type(LYNQ_TIME_TYPE_UNUSE);
luojin071b3852024-01-15 15:58:34 +0800254 }
b.liu5fa9e772023-11-23 18:00:55 +0800255 return 0;
256}
257
luojin071b3852024-01-15 15:58:34 +0800258
259//enable set time from gnss
b.liu5fa9e772023-11-23 18:00:55 +0800260int gnss_time_enable(int enable)
261{
262 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800263 if(enable)
264 {
r.xiao8b074132024-02-22 23:57:50 -0800265 set_time_type(LYNQ_TIME_TYPE_GNSS);
luojin071b3852024-01-15 15:58:34 +0800266 }
r.xiao8b074132024-02-22 23:57:50 -0800267 else
268 {
269 set_time_type(LYNQ_TIME_TYPE_UNUSE);
270 }
271
b.liu5fa9e772023-11-23 18:00:55 +0800272 return 0;
273}
274
luojin071b3852024-01-15 15:58:34 +0800275
276//enable set time from user
b.liu5fa9e772023-11-23 18:00:55 +0800277int user_set_time(char* date, char* time)
278{
279 UNUSED(date);
280 UNUSED(time);
luojin071b3852024-01-15 15:58:34 +0800281 if(date == NULL || time == NULL)
282 {
283 return -1;
284 }
b.liu5fa9e772023-11-23 18:00:55 +0800285
luojin071b3852024-01-15 15:58:34 +0800286 int ret = 0;
287 char time_str[128] ={0};
288 memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
289
290 char *p = time;
291 char *p1 = strstr(p, ":");
292 char *p2 = strstr(p1+1, ":");
293 if(p2 == NULL)
294 {
295 sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30
r.xiao8b074132024-02-22 23:57:50 -0800296 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800297 ret = set_time_user(time_str);
298 }else
299 {
300 sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
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 }
304
305 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800306}
307
b.liu5fa9e772023-11-23 18:00:55 +0800308
luojin071b3852024-01-15 15:58:34 +0800309//check sysytem type
b.liu5fa9e772023-11-23 18:00:55 +0800310int lynq_get_time_src_status (time_src_status_s * time_src)
311{
312 UNUSED(time_src);
luojin071b3852024-01-15 15:58:34 +0800313 int type = 0;
314 char time_type[] ={0};
315 property_get("persist.mbtk.time_type", time_type, "0");
b.liu5fa9e772023-11-23 18:00:55 +0800316
luojin071b3852024-01-15 15:58:34 +0800317 type = atoi(time_type);
318 printf("time_type :%d", type);
liuyangbfc4d7c2024-05-07 10:44:28 +0800319 if(type == LYNQ_TIME_TYPE_NTP)
320 {
luojin071b3852024-01-15 15:58:34 +0800321 time_src->ntp = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800322 time_src->nitz = 0;
323 time_src->gnss = 0;
324 }
325 else if(type == LYNQ_TIME_TYPE_CELL)
326 {
327 time_src->ntp = 0;
luojin071b3852024-01-15 15:58:34 +0800328 time_src->nitz = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800329 time_src->gnss = 0;
330 }
331 else if(type == LYNQ_TIME_TYPE_GNSS)
332 {
333 time_src->ntp = 0;
334 time_src->nitz = 0;
luojin071b3852024-01-15 15:58:34 +0800335 time_src->gnss = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800336 }
337 else if(type == LYNQ_TIME_TYPE_UNUSE)
338 {
r.xiao8b074132024-02-22 23:57:50 -0800339 time_src->ntp = 0;
340 time_src->nitz = 0;
341 time_src->gnss = 0;
luojin071b3852024-01-15 15:58:34 +0800342 }
liuyangbfc4d7c2024-05-07 10:44:28 +0800343
b.liu5fa9e772023-11-23 18:00:55 +0800344 return 0;
345}
346
luojin071b3852024-01-15 15:58:34 +0800347// RTC TIME set to system
348int lynq_sync_time_from_rtc(void)
349{
350 system("hwclock --hctosys");
351 return 0;
352}
353
354// system time set to RTC
b.liu5fa9e772023-11-23 18:00:55 +0800355int lynq_set_rtc_time(void)
356{
luojian9b9a9282024-07-04 18:56:21 +0800357// system("hwclock --systohc");
358 system("hwclock -w rtc0");
b.liu5fa9e772023-11-23 18:00:55 +0800359 return 0;
360}
361
362int lynq_get_rtc_time(unsigned long *ulsec)
363{
364 UNUSED(ulsec);
365
366 return 0;
367}
368
luojin071b3852024-01-15 15:58:34 +0800369
luojian252d7bc2024-09-24 16:32:38 +0800370