blob: a288c86a06bade1085cf5b0b5971f3fa802f50a8 [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
luojin071b3852024-01-15 15:58:34 +080057//int req_time_set(int type, char *time, int *cme_err);
58static int metis_strptime(char *str_time)
59{
60 printf("%s(), str_time:%s\n", __FUNCTION__, str_time);
61 struct tm stm;
62 char dateTime[30];
63 struct timeval tv;
64 if(strptime(str_time, "%Y-%m-%d %H:%M:%S",&stm) != NULL)
65 {
luojian88976a32024-08-14 17:40:01 +080066 time_t _t = (long)mktime(&stm);
67 tzset();
luojin071b3852024-01-15 15:58:34 +080068 tv.tv_sec = _t;
luojian88976a32024-08-14 17:40:01 +080069 tv.tv_usec = 0;
70 if(_t == -1)
71 {
72 printf("Set time :%s", str_time);
73 printf("timestamp:%ld", _t);
74 printf("mktime error, reason: %s\n", strerror(errno));
luojin071b3852024-01-15 15:58:34 +080075 return -1;
luojin071b3852024-01-15 15:58:34 +080076 }
luojian88976a32024-08-14 17:40:01 +080077
78 if(settimeofday(&tv, NULL)) {
79 printf("Set time :%s", str_time);
80 printf("timestamp:%ld", _t);
81 printf("mktime error, reason: %s\n", strerror(errno));
82 return -1;
83 }
84
85 printf("Success Set time to %s.\n", str_time);
86 return 0;
87
luojin071b3852024-01-15 15:58:34 +080088 } else {
89 printf("Set time fail.");
90 return -1;
91 }
92 return 0;
93}
94
95
liuyangbfc4d7c2024-05-07 10:44:28 +080096static void* ntp_pthread_run(int* ntp_flag)
luojin071b3852024-01-15 15:58:34 +080097{
liuyang0a15e082024-04-19 17:28:49 +080098 if (mbtk_net_state_get() == MBTK_NET_STATE_OFF)
99 {
100 printf("Network is disconnected. Set time fail.");
liuyangbfc4d7c2024-05-07 10:44:28 +0800101 if(NULL != ntp_flag)
102 {
103 *ntp_flag = -1;
104 }
liuyang0a15e082024-04-19 17:28:49 +0800105 return NULL;
luojin071b3852024-01-15 15:58:34 +0800106 }
107 printf("Network is connected.");
108
109 char time_type[10];
110 while(1){
111 memset(time_type, 0, 10);
112 property_get("persist.mbtk.time_type", time_type, "0");
r.xiao8b074132024-02-22 23:57:50 -0800113 if(atoi(time_type) == LYNQ_TIME_TYPE_NTP) // NTP time
luojin071b3852024-01-15 15:58:34 +0800114 {
115 char time_str[100] = {0};
116 time_t time = 0;
liuyangd0f7fdb2024-04-22 13:53:23 +0800117 if((time = (time_t)mbtk_at_systime()) == 0)
liuyang54582902024-04-19 18:06:25 +0800118 {
119 printf("NTP client fail!\n");
liuyangbfc4d7c2024-05-07 10:44:28 +0800120 if(NULL != ntp_flag)
121 {
122 *ntp_flag = -1;
123 }
liuyang54582902024-04-19 18:06:25 +0800124 return NULL;
luojin071b3852024-01-15 15:58:34 +0800125 }
luojian88976a32024-08-14 17:40:01 +0800126#if 1
127 struct tm CurlocalTime;
128 localtime_r(&time, &CurlocalTime);
luojianf7c8a6a2024-08-15 13:29:56 +0800129 // CurlocalTime.tm_hour += 8; //cst
luojian88976a32024-08-14 17:40:01 +0800130 char dateTime[30];
131 strftime(dateTime, 30, "%Y-%m-%d %H:%M:%S %A", &CurlocalTime);
132
luojianf7c8a6a2024-08-15 13:29:56 +0800133 // printf("dateTime:%s, %ld\n", dateTime, time+28800); //cst
134 printf("dateTime:%s, %ld\n", dateTime, time);
luojian88976a32024-08-14 17:40:01 +0800135
136 struct timeval tv;
137 tv.tv_sec = time;
luojianf7c8a6a2024-08-15 13:29:56 +0800138 // tv.tv_sec += 28800; //cst
luojian88976a32024-08-14 17:40:01 +0800139 tv.tv_usec = 0;
140
141 if(settimeofday(&tv, NULL)) {
142 printf("Set time :%s", dateTime);
143 printf("timestamp:%ld, tv.tv_sec:%ld\n", time, tv.tv_sec);
144
145 if(settimeofday(&tv, NULL)) {
146 *ntp_flag = -1;
147 printf("mktime error, reason: %s\n", strerror(errno));
148 return NULL;
149 }
150 }
151 printf("Set time success\n");
152 lynq_set_rtc_time();
153#else
154
luojin071b3852024-01-15 15:58:34 +0800155 struct tm *tm_t;
156 tm_t = localtime(&time);
luojian88976a32024-08-14 17:40:01 +0800157 tm_t->tm_hour += 8;
luojin071b3852024-01-15 15:58:34 +0800158 strftime(time_str,128,"%F %T",tm_t);
159
160 // NTP time
luojian88976a32024-08-14 17:40:01 +0800161 if(metis_strptime(time_str))
162 {
163 *ntp_flag = -1;
164 return NULL;
165 }
166#endif
luojin071b3852024-01-15 15:58:34 +0800167 break;
168 } else {
169 break;
170 }
171
172 sleep(64); // Sleep 64s.
173 }
liuyangbfc4d7c2024-05-07 10:44:28 +0800174 if(NULL != ntp_flag)
175 {
176 *ntp_flag = 0;
177 }
luojin071b3852024-01-15 15:58:34 +0800178 return NULL;
179}
180
181int set_time_user(char* data_time_str)
182{
183
184 int ret = 0;
185 if(strlen(data_time_str) > 0)
186 {
187 ret = metis_strptime(data_time_str);
188 }
189
190 return ret;
191}
192
193
194//MBTK_TIME_TYPE_CELL = 0, //NITZ
195//MBTK_TIME_TYPE_NTP,
196//MBTK_TIME_TYPE_GNSS,
197//MBTK_TIME_TYPE_USER
198void set_time_type(int mbtk_time_type)
199{
200 char type_str[10] = {0};
201 sprintf(type_str, "%d", mbtk_time_type);
202 property_set("persist.mbtk.time_type", type_str);
203
204 return;
205}
206
207
208
b.liu5fa9e772023-11-23 18:00:55 +0800209
210int ntp_sync_time(int enable)
211{
liuyang1ce96df2024-05-29 19:14:34 +0800212 if(0 != enable && 1 != enable)
213 {
214 return -1;
215 }
b.liu5fa9e772023-11-23 18:00:55 +0800216 UNUSED(enable);
liuyangbfc4d7c2024-05-07 10:44:28 +0800217 int ntp_status = 0;
luojian88976a32024-08-14 17:40:01 +0800218 int ret = 0;
luojin071b3852024-01-15 15:58:34 +0800219 if(enable)
220 {
luojian88976a32024-08-14 17:40:01 +0800221 set_time_type(LYNQ_TIME_TYPE_NTP);
liuyangbfc4d7c2024-05-07 10:44:28 +0800222 ntp_pthread_run(&ntp_status);
223 if(ntp_status == 0)
224 {
luojian88976a32024-08-14 17:40:01 +0800225 ret = 0;
liuyangbfc4d7c2024-05-07 10:44:28 +0800226 }
227 else
228 {
luojian88976a32024-08-14 17:40:01 +0800229 ret = -1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800230 set_time_type(LYNQ_TIME_TYPE_UNUSE);
231 }
luojin071b3852024-01-15 15:58:34 +0800232 }
r.xiao8b074132024-02-22 23:57:50 -0800233 else
234 {
235 set_time_type(LYNQ_TIME_TYPE_UNUSE);
236 }
237
luojian88976a32024-08-14 17:40:01 +0800238 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800239}
240
luojin071b3852024-01-15 15:58:34 +0800241//enable set time from nitz
b.liu5fa9e772023-11-23 18:00:55 +0800242int modem_time_enable(int enable)
243{
244 UNUSED(enable);
245
luojin071b3852024-01-15 15:58:34 +0800246 if(enable)
247 {
r.xiao8b074132024-02-22 23:57:50 -0800248 set_time_type(LYNQ_TIME_TYPE_CELL);
249 }
250 else
251 {
252 set_time_type(LYNQ_TIME_TYPE_UNUSE);
luojin071b3852024-01-15 15:58:34 +0800253 }
b.liu5fa9e772023-11-23 18:00:55 +0800254 return 0;
255}
256
luojin071b3852024-01-15 15:58:34 +0800257
258//enable set time from gnss
b.liu5fa9e772023-11-23 18:00:55 +0800259int gnss_time_enable(int enable)
260{
261 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800262 if(enable)
263 {
r.xiao8b074132024-02-22 23:57:50 -0800264 set_time_type(LYNQ_TIME_TYPE_GNSS);
luojin071b3852024-01-15 15:58:34 +0800265 }
r.xiao8b074132024-02-22 23:57:50 -0800266 else
267 {
268 set_time_type(LYNQ_TIME_TYPE_UNUSE);
269 }
270
b.liu5fa9e772023-11-23 18:00:55 +0800271 return 0;
272}
273
luojin071b3852024-01-15 15:58:34 +0800274
275//enable set time from user
b.liu5fa9e772023-11-23 18:00:55 +0800276int user_set_time(char* date, char* time)
277{
278 UNUSED(date);
279 UNUSED(time);
luojin071b3852024-01-15 15:58:34 +0800280 if(date == NULL || time == NULL)
281 {
282 return -1;
283 }
b.liu5fa9e772023-11-23 18:00:55 +0800284
luojin071b3852024-01-15 15:58:34 +0800285 int ret = 0;
286 char time_str[128] ={0};
287 memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
288
289 char *p = time;
290 char *p1 = strstr(p, ":");
291 char *p2 = strstr(p1+1, ":");
292 if(p2 == NULL)
293 {
294 sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30
r.xiao8b074132024-02-22 23:57:50 -0800295 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800296 ret = set_time_user(time_str);
297 }else
298 {
299 sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
r.xiao8b074132024-02-22 23:57:50 -0800300 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800301 ret = set_time_user(time_str);
302 }
303
304 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800305}
306
b.liu5fa9e772023-11-23 18:00:55 +0800307
luojin071b3852024-01-15 15:58:34 +0800308//check sysytem type
b.liu5fa9e772023-11-23 18:00:55 +0800309int lynq_get_time_src_status (time_src_status_s * time_src)
310{
311 UNUSED(time_src);
luojin071b3852024-01-15 15:58:34 +0800312 int type = 0;
313 char time_type[] ={0};
314 property_get("persist.mbtk.time_type", time_type, "0");
b.liu5fa9e772023-11-23 18:00:55 +0800315
luojin071b3852024-01-15 15:58:34 +0800316 type = atoi(time_type);
317 printf("time_type :%d", type);
liuyangbfc4d7c2024-05-07 10:44:28 +0800318 if(type == LYNQ_TIME_TYPE_NTP)
319 {
luojin071b3852024-01-15 15:58:34 +0800320 time_src->ntp = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800321 time_src->nitz = 0;
322 time_src->gnss = 0;
323 }
324 else if(type == LYNQ_TIME_TYPE_CELL)
325 {
326 time_src->ntp = 0;
luojin071b3852024-01-15 15:58:34 +0800327 time_src->nitz = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800328 time_src->gnss = 0;
329 }
330 else if(type == LYNQ_TIME_TYPE_GNSS)
331 {
332 time_src->ntp = 0;
333 time_src->nitz = 0;
luojin071b3852024-01-15 15:58:34 +0800334 time_src->gnss = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800335 }
336 else if(type == LYNQ_TIME_TYPE_UNUSE)
337 {
r.xiao8b074132024-02-22 23:57:50 -0800338 time_src->ntp = 0;
339 time_src->nitz = 0;
340 time_src->gnss = 0;
luojin071b3852024-01-15 15:58:34 +0800341 }
liuyangbfc4d7c2024-05-07 10:44:28 +0800342
b.liu5fa9e772023-11-23 18:00:55 +0800343 return 0;
344}
345
luojin071b3852024-01-15 15:58:34 +0800346// RTC TIME set to system
347int lynq_sync_time_from_rtc(void)
348{
349 system("hwclock --hctosys");
350 return 0;
351}
352
353// system time set to RTC
b.liu5fa9e772023-11-23 18:00:55 +0800354int lynq_set_rtc_time(void)
355{
luojian9b9a9282024-07-04 18:56:21 +0800356// system("hwclock --systohc");
357 system("hwclock -w rtc0");
b.liu5fa9e772023-11-23 18:00:55 +0800358 return 0;
359}
360
361int lynq_get_rtc_time(unsigned long *ulsec)
362{
363 UNUSED(ulsec);
364
365 return 0;
366}
367
luojin071b3852024-01-15 15:58:34 +0800368