blob: 99e7d2ce1e29d9d5e381d6d3939581ea0ef49beb [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);
129 CurlocalTime.tm_hour += 8;
130 char dateTime[30];
131 strftime(dateTime, 30, "%Y-%m-%d %H:%M:%S %A", &CurlocalTime);
132
133 printf("dateTime:%s, %ld\n", dateTime, time+28800);
134
135 struct timeval tv;
136 tv.tv_sec = time;
137 tv.tv_sec += 28800;
138 tv.tv_usec = 0;
139
140 if(settimeofday(&tv, NULL)) {
141 printf("Set time :%s", dateTime);
142 printf("timestamp:%ld, tv.tv_sec:%ld\n", time, tv.tv_sec);
143
144 if(settimeofday(&tv, NULL)) {
145 *ntp_flag = -1;
146 printf("mktime error, reason: %s\n", strerror(errno));
147 return NULL;
148 }
149 }
150 printf("Set time success\n");
151 lynq_set_rtc_time();
152#else
153
luojin071b3852024-01-15 15:58:34 +0800154 struct tm *tm_t;
155 tm_t = localtime(&time);
luojian88976a32024-08-14 17:40:01 +0800156 tm_t->tm_hour += 8;
luojin071b3852024-01-15 15:58:34 +0800157 strftime(time_str,128,"%F %T",tm_t);
158
159 // NTP time
luojian88976a32024-08-14 17:40:01 +0800160 if(metis_strptime(time_str))
161 {
162 *ntp_flag = -1;
163 return NULL;
164 }
165#endif
luojin071b3852024-01-15 15:58:34 +0800166 break;
167 } else {
168 break;
169 }
170
171 sleep(64); // Sleep 64s.
172 }
liuyangbfc4d7c2024-05-07 10:44:28 +0800173 if(NULL != ntp_flag)
174 {
175 *ntp_flag = 0;
176 }
luojin071b3852024-01-15 15:58:34 +0800177 return NULL;
178}
179
180int set_time_user(char* data_time_str)
181{
182
183 int ret = 0;
184 if(strlen(data_time_str) > 0)
185 {
186 ret = metis_strptime(data_time_str);
187 }
188
189 return ret;
190}
191
192
193//MBTK_TIME_TYPE_CELL = 0, //NITZ
194//MBTK_TIME_TYPE_NTP,
195//MBTK_TIME_TYPE_GNSS,
196//MBTK_TIME_TYPE_USER
197void set_time_type(int mbtk_time_type)
198{
199 char type_str[10] = {0};
200 sprintf(type_str, "%d", mbtk_time_type);
201 property_set("persist.mbtk.time_type", type_str);
202
203 return;
204}
205
206
207
b.liu5fa9e772023-11-23 18:00:55 +0800208
209int ntp_sync_time(int enable)
210{
liuyang1ce96df2024-05-29 19:14:34 +0800211 if(0 != enable && 1 != enable)
212 {
213 return -1;
214 }
b.liu5fa9e772023-11-23 18:00:55 +0800215 UNUSED(enable);
liuyangbfc4d7c2024-05-07 10:44:28 +0800216 int ntp_status = 0;
luojian88976a32024-08-14 17:40:01 +0800217 int ret = 0;
luojin071b3852024-01-15 15:58:34 +0800218 if(enable)
219 {
luojian88976a32024-08-14 17:40:01 +0800220 set_time_type(LYNQ_TIME_TYPE_NTP);
liuyangbfc4d7c2024-05-07 10:44:28 +0800221 ntp_pthread_run(&ntp_status);
222 if(ntp_status == 0)
223 {
luojian88976a32024-08-14 17:40:01 +0800224 ret = 0;
liuyangbfc4d7c2024-05-07 10:44:28 +0800225 }
226 else
227 {
luojian88976a32024-08-14 17:40:01 +0800228 ret = -1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800229 set_time_type(LYNQ_TIME_TYPE_UNUSE);
230 }
luojin071b3852024-01-15 15:58:34 +0800231 }
r.xiao8b074132024-02-22 23:57:50 -0800232 else
233 {
234 set_time_type(LYNQ_TIME_TYPE_UNUSE);
235 }
236
luojian88976a32024-08-14 17:40:01 +0800237 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800238}
239
luojin071b3852024-01-15 15:58:34 +0800240//enable set time from nitz
b.liu5fa9e772023-11-23 18:00:55 +0800241int modem_time_enable(int enable)
242{
243 UNUSED(enable);
244
luojin071b3852024-01-15 15:58:34 +0800245 if(enable)
246 {
r.xiao8b074132024-02-22 23:57:50 -0800247 set_time_type(LYNQ_TIME_TYPE_CELL);
248 }
249 else
250 {
251 set_time_type(LYNQ_TIME_TYPE_UNUSE);
luojin071b3852024-01-15 15:58:34 +0800252 }
b.liu5fa9e772023-11-23 18:00:55 +0800253 return 0;
254}
255
luojin071b3852024-01-15 15:58:34 +0800256
257//enable set time from gnss
b.liu5fa9e772023-11-23 18:00:55 +0800258int gnss_time_enable(int enable)
259{
260 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800261 if(enable)
262 {
r.xiao8b074132024-02-22 23:57:50 -0800263 set_time_type(LYNQ_TIME_TYPE_GNSS);
luojin071b3852024-01-15 15:58:34 +0800264 }
r.xiao8b074132024-02-22 23:57:50 -0800265 else
266 {
267 set_time_type(LYNQ_TIME_TYPE_UNUSE);
268 }
269
b.liu5fa9e772023-11-23 18:00:55 +0800270 return 0;
271}
272
luojin071b3852024-01-15 15:58:34 +0800273
274//enable set time from user
b.liu5fa9e772023-11-23 18:00:55 +0800275int user_set_time(char* date, char* time)
276{
277 UNUSED(date);
278 UNUSED(time);
luojin071b3852024-01-15 15:58:34 +0800279 if(date == NULL || time == NULL)
280 {
281 return -1;
282 }
b.liu5fa9e772023-11-23 18:00:55 +0800283
luojin071b3852024-01-15 15:58:34 +0800284 int ret = 0;
285 char time_str[128] ={0};
286 memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
287
288 char *p = time;
289 char *p1 = strstr(p, ":");
290 char *p2 = strstr(p1+1, ":");
291 if(p2 == NULL)
292 {
293 sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30
r.xiao8b074132024-02-22 23:57:50 -0800294 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800295 ret = set_time_user(time_str);
296 }else
297 {
298 sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
r.xiao8b074132024-02-22 23:57:50 -0800299 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800300 ret = set_time_user(time_str);
301 }
302
303 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800304}
305
b.liu5fa9e772023-11-23 18:00:55 +0800306
luojin071b3852024-01-15 15:58:34 +0800307//check sysytem type
b.liu5fa9e772023-11-23 18:00:55 +0800308int lynq_get_time_src_status (time_src_status_s * time_src)
309{
310 UNUSED(time_src);
luojin071b3852024-01-15 15:58:34 +0800311 int type = 0;
312 char time_type[] ={0};
313 property_get("persist.mbtk.time_type", time_type, "0");
b.liu5fa9e772023-11-23 18:00:55 +0800314
luojin071b3852024-01-15 15:58:34 +0800315 type = atoi(time_type);
316 printf("time_type :%d", type);
liuyangbfc4d7c2024-05-07 10:44:28 +0800317 if(type == LYNQ_TIME_TYPE_NTP)
318 {
luojin071b3852024-01-15 15:58:34 +0800319 time_src->ntp = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800320 time_src->nitz = 0;
321 time_src->gnss = 0;
322 }
323 else if(type == LYNQ_TIME_TYPE_CELL)
324 {
325 time_src->ntp = 0;
luojin071b3852024-01-15 15:58:34 +0800326 time_src->nitz = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800327 time_src->gnss = 0;
328 }
329 else if(type == LYNQ_TIME_TYPE_GNSS)
330 {
331 time_src->ntp = 0;
332 time_src->nitz = 0;
luojin071b3852024-01-15 15:58:34 +0800333 time_src->gnss = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800334 }
335 else if(type == LYNQ_TIME_TYPE_UNUSE)
336 {
r.xiao8b074132024-02-22 23:57:50 -0800337 time_src->ntp = 0;
338 time_src->nitz = 0;
339 time_src->gnss = 0;
luojin071b3852024-01-15 15:58:34 +0800340 }
liuyangbfc4d7c2024-05-07 10:44:28 +0800341
b.liu5fa9e772023-11-23 18:00:55 +0800342 return 0;
343}
344
luojin071b3852024-01-15 15:58:34 +0800345// RTC TIME set to system
346int lynq_sync_time_from_rtc(void)
347{
348 system("hwclock --hctosys");
349 return 0;
350}
351
352// system time set to RTC
b.liu5fa9e772023-11-23 18:00:55 +0800353int lynq_set_rtc_time(void)
354{
luojian9b9a9282024-07-04 18:56:21 +0800355// system("hwclock --systohc");
356 system("hwclock -w rtc0");
b.liu5fa9e772023-11-23 18:00:55 +0800357 return 0;
358}
359
360int lynq_get_rtc_time(unsigned long *ulsec)
361{
362 UNUSED(ulsec);
363
364 return 0;
365}
366
luojin071b3852024-01-15 15:58:34 +0800367