blob: 8f432f0332f8c38afdd8ab006afe8aa875631932 [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{
liuyang1ce96df2024-05-29 19:14:34 +0800165 if(0 != enable && 1 != enable)
166 {
167 return -1;
168 }
b.liu5fa9e772023-11-23 18:00:55 +0800169 UNUSED(enable);
liuyangbfc4d7c2024-05-07 10:44:28 +0800170 int ntp_status = 0;
luojin071b3852024-01-15 15:58:34 +0800171 if(enable)
172 {
liuyangbfc4d7c2024-05-07 10:44:28 +0800173 ntp_pthread_run(&ntp_status);
174 if(ntp_status == 0)
175 {
176 set_time_type(LYNQ_TIME_TYPE_NTP);
177 }
178 else
179 {
180 set_time_type(LYNQ_TIME_TYPE_UNUSE);
181 }
luojin071b3852024-01-15 15:58:34 +0800182 }
r.xiao8b074132024-02-22 23:57:50 -0800183 else
184 {
185 set_time_type(LYNQ_TIME_TYPE_UNUSE);
186 }
187
b.liu5fa9e772023-11-23 18:00:55 +0800188 return 0;
189}
190
luojin071b3852024-01-15 15:58:34 +0800191//enable set time from nitz
b.liu5fa9e772023-11-23 18:00:55 +0800192int modem_time_enable(int enable)
193{
194 UNUSED(enable);
195
luojin071b3852024-01-15 15:58:34 +0800196 if(enable)
197 {
r.xiao8b074132024-02-22 23:57:50 -0800198 set_time_type(LYNQ_TIME_TYPE_CELL);
199 }
200 else
201 {
202 set_time_type(LYNQ_TIME_TYPE_UNUSE);
luojin071b3852024-01-15 15:58:34 +0800203 }
b.liu5fa9e772023-11-23 18:00:55 +0800204 return 0;
205}
206
luojin071b3852024-01-15 15:58:34 +0800207
208//enable set time from gnss
b.liu5fa9e772023-11-23 18:00:55 +0800209int gnss_time_enable(int enable)
210{
211 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800212 if(enable)
213 {
r.xiao8b074132024-02-22 23:57:50 -0800214 set_time_type(LYNQ_TIME_TYPE_GNSS);
luojin071b3852024-01-15 15:58:34 +0800215 }
r.xiao8b074132024-02-22 23:57:50 -0800216 else
217 {
218 set_time_type(LYNQ_TIME_TYPE_UNUSE);
219 }
220
b.liu5fa9e772023-11-23 18:00:55 +0800221 return 0;
222}
223
luojin071b3852024-01-15 15:58:34 +0800224
225//enable set time from user
b.liu5fa9e772023-11-23 18:00:55 +0800226int user_set_time(char* date, char* time)
227{
228 UNUSED(date);
229 UNUSED(time);
luojin071b3852024-01-15 15:58:34 +0800230 if(date == NULL || time == NULL)
231 {
232 return -1;
233 }
b.liu5fa9e772023-11-23 18:00:55 +0800234
luojin071b3852024-01-15 15:58:34 +0800235 int ret = 0;
236 char time_str[128] ={0};
237 memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
238
239 char *p = time;
240 char *p1 = strstr(p, ":");
241 char *p2 = strstr(p1+1, ":");
242 if(p2 == NULL)
243 {
244 sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30
r.xiao8b074132024-02-22 23:57:50 -0800245 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800246 ret = set_time_user(time_str);
247 }else
248 {
249 sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
r.xiao8b074132024-02-22 23:57:50 -0800250 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800251 ret = set_time_user(time_str);
252 }
253
254 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800255}
256
b.liu5fa9e772023-11-23 18:00:55 +0800257
luojin071b3852024-01-15 15:58:34 +0800258//check sysytem type
b.liu5fa9e772023-11-23 18:00:55 +0800259int lynq_get_time_src_status (time_src_status_s * time_src)
260{
261 UNUSED(time_src);
luojin071b3852024-01-15 15:58:34 +0800262 int type = 0;
263 char time_type[] ={0};
264 property_get("persist.mbtk.time_type", time_type, "0");
b.liu5fa9e772023-11-23 18:00:55 +0800265
luojin071b3852024-01-15 15:58:34 +0800266 type = atoi(time_type);
267 printf("time_type :%d", type);
liuyangbfc4d7c2024-05-07 10:44:28 +0800268 if(type == LYNQ_TIME_TYPE_NTP)
269 {
luojin071b3852024-01-15 15:58:34 +0800270 time_src->ntp = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800271 time_src->nitz = 0;
272 time_src->gnss = 0;
273 }
274 else if(type == LYNQ_TIME_TYPE_CELL)
275 {
276 time_src->ntp = 0;
luojin071b3852024-01-15 15:58:34 +0800277 time_src->nitz = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800278 time_src->gnss = 0;
279 }
280 else if(type == LYNQ_TIME_TYPE_GNSS)
281 {
282 time_src->ntp = 0;
283 time_src->nitz = 0;
luojin071b3852024-01-15 15:58:34 +0800284 time_src->gnss = 1;
liuyangbfc4d7c2024-05-07 10:44:28 +0800285 }
286 else if(type == LYNQ_TIME_TYPE_UNUSE)
287 {
r.xiao8b074132024-02-22 23:57:50 -0800288 time_src->ntp = 0;
289 time_src->nitz = 0;
290 time_src->gnss = 0;
luojin071b3852024-01-15 15:58:34 +0800291 }
liuyangbfc4d7c2024-05-07 10:44:28 +0800292
b.liu5fa9e772023-11-23 18:00:55 +0800293 return 0;
294}
295
luojin071b3852024-01-15 15:58:34 +0800296// RTC TIME set to system
297int lynq_sync_time_from_rtc(void)
298{
299 system("hwclock --hctosys");
300 return 0;
301}
302
303// system time set to RTC
b.liu5fa9e772023-11-23 18:00:55 +0800304int lynq_set_rtc_time(void)
305{
luojian9b9a9282024-07-04 18:56:21 +0800306// system("hwclock --systohc");
307 system("hwclock -w rtc0");
b.liu5fa9e772023-11-23 18:00:55 +0800308 return 0;
309}
310
311int lynq_get_rtc_time(unsigned long *ulsec)
312{
313 UNUSED(ulsec);
314
315 return 0;
316}
317
luojin071b3852024-01-15 15:58:34 +0800318