blob: 71fd9e32c8c7c3468b085750b228bcb38ee496a3 [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 {
72 printf("Set time to %s.", str_time);
73 return 0;
74 }
75 } else {
76 printf("Set time fail.");
77 return -1;
78 }
79 return 0;
80}
81
82
83
liuyangbfc4d7c2024-05-07 10:44:28 +080084static void* ntp_pthread_run(int* ntp_flag)
luojin071b3852024-01-15 15:58:34 +080085{
liuyang0a15e082024-04-19 17:28:49 +080086 if (mbtk_net_state_get() == MBTK_NET_STATE_OFF)
87 {
88 printf("Network is disconnected. Set time fail.");
liuyangbfc4d7c2024-05-07 10:44:28 +080089 if(NULL != ntp_flag)
90 {
91 *ntp_flag = -1;
92 }
liuyang0a15e082024-04-19 17:28:49 +080093 return NULL;
luojin071b3852024-01-15 15:58:34 +080094 }
95 printf("Network is connected.");
96
97 char time_type[10];
98 while(1){
99 memset(time_type, 0, 10);
100 property_get("persist.mbtk.time_type", time_type, "0");
r.xiao8b074132024-02-22 23:57:50 -0800101 if(atoi(time_type) == LYNQ_TIME_TYPE_NTP) // NTP time
luojin071b3852024-01-15 15:58:34 +0800102 {
103 char time_str[100] = {0};
104 time_t time = 0;
liuyangd0f7fdb2024-04-22 13:53:23 +0800105 if((time = (time_t)mbtk_at_systime()) == 0)
liuyang54582902024-04-19 18:06:25 +0800106 {
107 printf("NTP client fail!\n");
liuyangbfc4d7c2024-05-07 10:44:28 +0800108 if(NULL != ntp_flag)
109 {
110 *ntp_flag = -1;
111 }
liuyang54582902024-04-19 18:06:25 +0800112 return NULL;
luojin071b3852024-01-15 15:58:34 +0800113 }
114 struct tm *tm_t;
115 tm_t = localtime(&time);
116 strftime(time_str,128,"%F %T",tm_t);
117
118 // NTP time
119 metis_strptime(time_str);
120 break;
121 } else {
122 break;
123 }
124
125 sleep(64); // Sleep 64s.
126 }
liuyangbfc4d7c2024-05-07 10:44:28 +0800127 if(NULL != ntp_flag)
128 {
129 *ntp_flag = 0;
130 }
luojin071b3852024-01-15 15:58:34 +0800131 return NULL;
132}
133
134int set_time_user(char* data_time_str)
135{
136
137 int ret = 0;
138 if(strlen(data_time_str) > 0)
139 {
140 ret = metis_strptime(data_time_str);
141 }
142
143 return ret;
144}
145
146
147//MBTK_TIME_TYPE_CELL = 0, //NITZ
148//MBTK_TIME_TYPE_NTP,
149//MBTK_TIME_TYPE_GNSS,
150//MBTK_TIME_TYPE_USER
151void set_time_type(int mbtk_time_type)
152{
153 char type_str[10] = {0};
154 sprintf(type_str, "%d", mbtk_time_type);
155 property_set("persist.mbtk.time_type", type_str);
156
157 return;
158}
159
160
161
b.liu5fa9e772023-11-23 18:00:55 +0800162
163int ntp_sync_time(int enable)
164{
165 UNUSED(enable);
liuyangbfc4d7c2024-05-07 10:44:28 +0800166 int ntp_status = 0;
luojin071b3852024-01-15 15:58:34 +0800167 if(enable)
168 {
liuyangbfc4d7c2024-05-07 10:44:28 +0800169 ntp_pthread_run(&ntp_status);
170 if(ntp_status == 0)
171 {
172 set_time_type(LYNQ_TIME_TYPE_NTP);
173 }
174 else
175 {
176 set_time_type(LYNQ_TIME_TYPE_UNUSE);
177 }
luojin071b3852024-01-15 15:58:34 +0800178 }
r.xiao8b074132024-02-22 23:57:50 -0800179 else
180 {
181 set_time_type(LYNQ_TIME_TYPE_UNUSE);
182 }
183
b.liu5fa9e772023-11-23 18:00:55 +0800184 return 0;
185}
186
luojin071b3852024-01-15 15:58:34 +0800187//enable set time from nitz
b.liu5fa9e772023-11-23 18:00:55 +0800188int modem_time_enable(int enable)
189{
190 UNUSED(enable);
191
luojin071b3852024-01-15 15:58:34 +0800192 if(enable)
193 {
r.xiao8b074132024-02-22 23:57:50 -0800194 set_time_type(LYNQ_TIME_TYPE_CELL);
195 }
196 else
197 {
198 set_time_type(LYNQ_TIME_TYPE_UNUSE);
luojin071b3852024-01-15 15:58:34 +0800199 }
b.liu5fa9e772023-11-23 18:00:55 +0800200 return 0;
201}
202
luojin071b3852024-01-15 15:58:34 +0800203
204//enable set time from gnss
b.liu5fa9e772023-11-23 18:00:55 +0800205int gnss_time_enable(int enable)
206{
207 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800208 if(enable)
209 {
r.xiao8b074132024-02-22 23:57:50 -0800210 set_time_type(LYNQ_TIME_TYPE_GNSS);
luojin071b3852024-01-15 15:58:34 +0800211 }
r.xiao8b074132024-02-22 23:57:50 -0800212 else
213 {
214 set_time_type(LYNQ_TIME_TYPE_UNUSE);
215 }
216
b.liu5fa9e772023-11-23 18:00:55 +0800217 return 0;
218}
219
luojin071b3852024-01-15 15:58:34 +0800220
221//enable set time from user
b.liu5fa9e772023-11-23 18:00:55 +0800222int user_set_time(char* date, char* time)
223{
224 UNUSED(date);
225 UNUSED(time);
luojin071b3852024-01-15 15:58:34 +0800226 if(date == NULL || time == NULL)
227 {
228 return -1;
229 }
b.liu5fa9e772023-11-23 18:00:55 +0800230
luojin071b3852024-01-15 15:58:34 +0800231 int ret = 0;
232 char time_str[128] ={0};
233 memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
234
235 char *p = time;
236 char *p1 = strstr(p, ":");
237 char *p2 = strstr(p1+1, ":");
238 if(p2 == NULL)
239 {
240 sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30
r.xiao8b074132024-02-22 23:57:50 -0800241 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800242 ret = set_time_user(time_str);
243 }else
244 {
245 sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
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 }
249
250 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800251}
252
b.liu5fa9e772023-11-23 18:00:55 +0800253
luojin071b3852024-01-15 15:58:34 +0800254//check sysytem type
b.liu5fa9e772023-11-23 18:00:55 +0800255int lynq_get_time_src_status (time_src_status_s * time_src)
256{
257 UNUSED(time_src);
luojin071b3852024-01-15 15:58:34 +0800258 int type = 0;
259 char time_type[] ={0};
260 property_get("persist.mbtk.time_type", time_type, "0");
b.liu5fa9e772023-11-23 18:00:55 +0800261
luojin071b3852024-01-15 15:58:34 +0800262 type = atoi(time_type);
263 printf("time_type :%d", type);
liuyangbfc4d7c2024-05-07 10:44:28 +0800264 if(type == LYNQ_TIME_TYPE_NTP)
265 {
luojin071b3852024-01-15 15:58:34 +0800266 time_src->ntp = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800267 time_src->nitz = 0;
268 time_src->gnss = 0;
269 }
270 else if(type == LYNQ_TIME_TYPE_CELL)
271 {
272 time_src->ntp = 0;
luojin071b3852024-01-15 15:58:34 +0800273 time_src->nitz = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800274 time_src->gnss = 0;
275 }
276 else if(type == LYNQ_TIME_TYPE_GNSS)
277 {
278 time_src->ntp = 0;
279 time_src->nitz = 0;
luojin071b3852024-01-15 15:58:34 +0800280 time_src->gnss = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800281 }
282 else if(type == LYNQ_TIME_TYPE_UNUSE)
283 {
r.xiao8b074132024-02-22 23:57:50 -0800284 time_src->ntp = 0;
285 time_src->nitz = 0;
286 time_src->gnss = 0;
luojin071b3852024-01-15 15:58:34 +0800287 }
liuyangbfc4d7c2024-05-07 10:44:28 +0800288
b.liu5fa9e772023-11-23 18:00:55 +0800289 return 0;
290}
291
luojin071b3852024-01-15 15:58:34 +0800292// RTC TIME set to system
293int lynq_sync_time_from_rtc(void)
294{
295 system("hwclock --hctosys");
296 return 0;
297}
298
299// system time set to RTC
b.liu5fa9e772023-11-23 18:00:55 +0800300int lynq_set_rtc_time(void)
301{
luojin071b3852024-01-15 15:58:34 +0800302 system("hwclock --systohc");
b.liu5fa9e772023-11-23 18:00:55 +0800303 return 0;
304}
305
306int lynq_get_rtc_time(unsigned long *ulsec)
307{
308 UNUSED(ulsec);
309
310 return 0;
311}
312
luojin071b3852024-01-15 15:58:34 +0800313