blob: 22e311d4e8e3f8e7d94d49a28a0f4d69a649549e [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 {
66 time_t _t = mktime(&stm);
67 tv.tv_sec = _t;
68 if(settimeofday(&tv, NULL)) {
69 printf("Set time fail:%d");
70 return -1;
71 } else {
luojian51d65f92024-08-08 15:39:42 +080072 lynq_set_rtc_time();
luojin071b3852024-01-15 15:58:34 +080073 printf("Set time to %s.", str_time);
74 return 0;
75 }
76 } else {
77 printf("Set time fail.");
78 return -1;
79 }
80 return 0;
81}
82
83
84
liuyangbfc4d7c2024-05-07 10:44:28 +080085static void* ntp_pthread_run(int* ntp_flag)
luojin071b3852024-01-15 15:58:34 +080086{
liuyang0a15e082024-04-19 17:28:49 +080087 if (mbtk_net_state_get() == MBTK_NET_STATE_OFF)
88 {
89 printf("Network is disconnected. Set time fail.");
liuyangbfc4d7c2024-05-07 10:44:28 +080090 if(NULL != ntp_flag)
91 {
92 *ntp_flag = -1;
93 }
liuyang0a15e082024-04-19 17:28:49 +080094 return NULL;
luojin071b3852024-01-15 15:58:34 +080095 }
96 printf("Network is connected.");
97
98 char time_type[10];
99 while(1){
100 memset(time_type, 0, 10);
101 property_get("persist.mbtk.time_type", time_type, "0");
r.xiao8b074132024-02-22 23:57:50 -0800102 if(atoi(time_type) == LYNQ_TIME_TYPE_NTP) // NTP time
luojin071b3852024-01-15 15:58:34 +0800103 {
104 char time_str[100] = {0};
105 time_t time = 0;
liuyangd0f7fdb2024-04-22 13:53:23 +0800106 if((time = (time_t)mbtk_at_systime()) == 0)
liuyang54582902024-04-19 18:06:25 +0800107 {
108 printf("NTP client fail!\n");
liuyangbfc4d7c2024-05-07 10:44:28 +0800109 if(NULL != ntp_flag)
110 {
111 *ntp_flag = -1;
112 }
liuyang54582902024-04-19 18:06:25 +0800113 return NULL;
luojin071b3852024-01-15 15:58:34 +0800114 }
115 struct tm *tm_t;
116 tm_t = localtime(&time);
117 strftime(time_str,128,"%F %T",tm_t);
118
119 // NTP time
120 metis_strptime(time_str);
121 break;
122 } else {
123 break;
124 }
125
126 sleep(64); // Sleep 64s.
127 }
liuyangbfc4d7c2024-05-07 10:44:28 +0800128 if(NULL != ntp_flag)
129 {
130 *ntp_flag = 0;
131 }
luojin071b3852024-01-15 15:58:34 +0800132 return NULL;
133}
134
135int set_time_user(char* data_time_str)
136{
137
138 int ret = 0;
139 if(strlen(data_time_str) > 0)
140 {
141 ret = metis_strptime(data_time_str);
142 }
143
144 return ret;
145}
146
147
148//MBTK_TIME_TYPE_CELL = 0, //NITZ
149//MBTK_TIME_TYPE_NTP,
150//MBTK_TIME_TYPE_GNSS,
151//MBTK_TIME_TYPE_USER
152void set_time_type(int mbtk_time_type)
153{
154 char type_str[10] = {0};
155 sprintf(type_str, "%d", mbtk_time_type);
156 property_set("persist.mbtk.time_type", type_str);
157
158 return;
159}
160
161
162
b.liu5fa9e772023-11-23 18:00:55 +0800163
164int ntp_sync_time(int enable)
165{
liuyang1ce96df2024-05-29 19:14:34 +0800166 if(0 != enable && 1 != enable)
167 {
168 return -1;
169 }
b.liu5fa9e772023-11-23 18:00:55 +0800170 UNUSED(enable);
liuyangbfc4d7c2024-05-07 10:44:28 +0800171 int ntp_status = 0;
luojin071b3852024-01-15 15:58:34 +0800172 if(enable)
173 {
liuyangbfc4d7c2024-05-07 10:44:28 +0800174 ntp_pthread_run(&ntp_status);
175 if(ntp_status == 0)
176 {
177 set_time_type(LYNQ_TIME_TYPE_NTP);
178 }
179 else
180 {
181 set_time_type(LYNQ_TIME_TYPE_UNUSE);
182 }
luojin071b3852024-01-15 15:58:34 +0800183 }
r.xiao8b074132024-02-22 23:57:50 -0800184 else
185 {
186 set_time_type(LYNQ_TIME_TYPE_UNUSE);
187 }
188
b.liu5fa9e772023-11-23 18:00:55 +0800189 return 0;
190}
191
luojin071b3852024-01-15 15:58:34 +0800192//enable set time from nitz
b.liu5fa9e772023-11-23 18:00:55 +0800193int modem_time_enable(int enable)
194{
195 UNUSED(enable);
196
luojin071b3852024-01-15 15:58:34 +0800197 if(enable)
198 {
r.xiao8b074132024-02-22 23:57:50 -0800199 set_time_type(LYNQ_TIME_TYPE_CELL);
200 }
201 else
202 {
203 set_time_type(LYNQ_TIME_TYPE_UNUSE);
luojin071b3852024-01-15 15:58:34 +0800204 }
b.liu5fa9e772023-11-23 18:00:55 +0800205 return 0;
206}
207
luojin071b3852024-01-15 15:58:34 +0800208
209//enable set time from gnss
b.liu5fa9e772023-11-23 18:00:55 +0800210int gnss_time_enable(int enable)
211{
212 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800213 if(enable)
214 {
r.xiao8b074132024-02-22 23:57:50 -0800215 set_time_type(LYNQ_TIME_TYPE_GNSS);
luojin071b3852024-01-15 15:58:34 +0800216 }
r.xiao8b074132024-02-22 23:57:50 -0800217 else
218 {
219 set_time_type(LYNQ_TIME_TYPE_UNUSE);
220 }
221
b.liu5fa9e772023-11-23 18:00:55 +0800222 return 0;
223}
224
luojin071b3852024-01-15 15:58:34 +0800225
226//enable set time from user
b.liu5fa9e772023-11-23 18:00:55 +0800227int user_set_time(char* date, char* time)
228{
229 UNUSED(date);
230 UNUSED(time);
luojin071b3852024-01-15 15:58:34 +0800231 if(date == NULL || time == NULL)
232 {
233 return -1;
234 }
b.liu5fa9e772023-11-23 18:00:55 +0800235
luojin071b3852024-01-15 15:58:34 +0800236 int ret = 0;
237 char time_str[128] ={0};
238 memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
239
240 char *p = time;
241 char *p1 = strstr(p, ":");
242 char *p2 = strstr(p1+1, ":");
243 if(p2 == NULL)
244 {
245 sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30
r.xiao8b074132024-02-22 23:57:50 -0800246 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800247 ret = set_time_user(time_str);
248 }else
249 {
250 sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
r.xiao8b074132024-02-22 23:57:50 -0800251 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800252 ret = set_time_user(time_str);
253 }
254
255 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800256}
257
b.liu5fa9e772023-11-23 18:00:55 +0800258
luojin071b3852024-01-15 15:58:34 +0800259//check sysytem type
b.liu5fa9e772023-11-23 18:00:55 +0800260int lynq_get_time_src_status (time_src_status_s * time_src)
261{
262 UNUSED(time_src);
luojin071b3852024-01-15 15:58:34 +0800263 int type = 0;
264 char time_type[] ={0};
265 property_get("persist.mbtk.time_type", time_type, "0");
b.liu5fa9e772023-11-23 18:00:55 +0800266
luojin071b3852024-01-15 15:58:34 +0800267 type = atoi(time_type);
268 printf("time_type :%d", type);
liuyangbfc4d7c2024-05-07 10:44:28 +0800269 if(type == LYNQ_TIME_TYPE_NTP)
270 {
luojin071b3852024-01-15 15:58:34 +0800271 time_src->ntp = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800272 time_src->nitz = 0;
273 time_src->gnss = 0;
274 }
275 else if(type == LYNQ_TIME_TYPE_CELL)
276 {
277 time_src->ntp = 0;
luojin071b3852024-01-15 15:58:34 +0800278 time_src->nitz = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800279 time_src->gnss = 0;
280 }
281 else if(type == LYNQ_TIME_TYPE_GNSS)
282 {
283 time_src->ntp = 0;
284 time_src->nitz = 0;
luojin071b3852024-01-15 15:58:34 +0800285 time_src->gnss = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800286 }
287 else if(type == LYNQ_TIME_TYPE_UNUSE)
288 {
r.xiao8b074132024-02-22 23:57:50 -0800289 time_src->ntp = 0;
290 time_src->nitz = 0;
291 time_src->gnss = 0;
luojin071b3852024-01-15 15:58:34 +0800292 }
liuyangbfc4d7c2024-05-07 10:44:28 +0800293
b.liu5fa9e772023-11-23 18:00:55 +0800294 return 0;
295}
296
luojin071b3852024-01-15 15:58:34 +0800297// RTC TIME set to system
298int lynq_sync_time_from_rtc(void)
299{
300 system("hwclock --hctosys");
301 return 0;
302}
303
304// system time set to RTC
b.liu5fa9e772023-11-23 18:00:55 +0800305int lynq_set_rtc_time(void)
306{
luojian9b9a9282024-07-04 18:56:21 +0800307// system("hwclock --systohc");
308 system("hwclock -w rtc0");
b.liu5fa9e772023-11-23 18:00:55 +0800309 return 0;
310}
311
312int lynq_get_rtc_time(unsigned long *ulsec)
313{
314 UNUSED(ulsec);
315
316 return 0;
317}
318
luojin071b3852024-01-15 15:58:34 +0800319