blob: 842717477c9d5a65a8b7fb174667efa4fdc02fc4 [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
57
58
59//int req_time_set(int type, char *time, int *cme_err);
60static int metis_strptime(char *str_time)
61{
62 printf("%s(), str_time:%s\n", __FUNCTION__, str_time);
63 struct tm stm;
64 char dateTime[30];
65 struct timeval tv;
66 if(strptime(str_time, "%Y-%m-%d %H:%M:%S",&stm) != NULL)
67 {
68 time_t _t = mktime(&stm);
69 tv.tv_sec = _t;
70 if(settimeofday(&tv, NULL)) {
71 printf("Set time fail:%d");
72 return -1;
73 } else {
74 printf("Set time to %s.", str_time);
75 return 0;
76 }
77 } else {
78 printf("Set time fail.");
79 return -1;
80 }
81 return 0;
82}
83
84
85
86static void* ntp_pthread_run()
87{
liuyang0a15e082024-04-19 17:28:49 +080088 if (mbtk_net_state_get() == MBTK_NET_STATE_OFF)
89 {
90 printf("Network is disconnected. Set time fail.");
91 return NULL;
luojin071b3852024-01-15 15:58:34 +080092 }
93 printf("Network is connected.");
94
95 char time_type[10];
96 while(1){
97 memset(time_type, 0, 10);
98 property_get("persist.mbtk.time_type", time_type, "0");
r.xiao8b074132024-02-22 23:57:50 -080099 if(atoi(time_type) == LYNQ_TIME_TYPE_NTP) // NTP time
luojin071b3852024-01-15 15:58:34 +0800100 {
101 char time_str[100] = {0};
102 time_t time = 0;
liuyangd0f7fdb2024-04-22 13:53:23 +0800103 if((time = (time_t)mbtk_at_systime()) == 0)
liuyang54582902024-04-19 18:06:25 +0800104 {
105 printf("NTP client fail!\n");
liuyang54582902024-04-19 18:06:25 +0800106 return NULL;
luojin071b3852024-01-15 15:58:34 +0800107 }
108 struct tm *tm_t;
109 tm_t = localtime(&time);
110 strftime(time_str,128,"%F %T",tm_t);
111
112 // NTP time
113 metis_strptime(time_str);
114 break;
115 } else {
116 break;
117 }
118
119 sleep(64); // Sleep 64s.
120 }
121 return NULL;
122}
123
124int set_time_user(char* data_time_str)
125{
126
127 int ret = 0;
128 if(strlen(data_time_str) > 0)
129 {
130 ret = metis_strptime(data_time_str);
131 }
132
133 return ret;
134}
135
136
137//MBTK_TIME_TYPE_CELL = 0, //NITZ
138//MBTK_TIME_TYPE_NTP,
139//MBTK_TIME_TYPE_GNSS,
140//MBTK_TIME_TYPE_USER
141void set_time_type(int mbtk_time_type)
142{
143 char type_str[10] = {0};
144 sprintf(type_str, "%d", mbtk_time_type);
145 property_set("persist.mbtk.time_type", type_str);
146
147 return;
148}
149
150
151
b.liu5fa9e772023-11-23 18:00:55 +0800152
153int ntp_sync_time(int enable)
154{
155 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800156 if(enable)
157 {
r.xiao8b074132024-02-22 23:57:50 -0800158 set_time_type(LYNQ_TIME_TYPE_NTP);
luojin071b3852024-01-15 15:58:34 +0800159 ntp_pthread_run();
160 }
r.xiao8b074132024-02-22 23:57:50 -0800161 else
162 {
163 set_time_type(LYNQ_TIME_TYPE_UNUSE);
164 }
165
b.liu5fa9e772023-11-23 18:00:55 +0800166 return 0;
167}
168
luojin071b3852024-01-15 15:58:34 +0800169//enable set time from nitz
b.liu5fa9e772023-11-23 18:00:55 +0800170int modem_time_enable(int enable)
171{
172 UNUSED(enable);
173
luojin071b3852024-01-15 15:58:34 +0800174 if(enable)
175 {
r.xiao8b074132024-02-22 23:57:50 -0800176 set_time_type(LYNQ_TIME_TYPE_CELL);
177 }
178 else
179 {
180 set_time_type(LYNQ_TIME_TYPE_UNUSE);
luojin071b3852024-01-15 15:58:34 +0800181 }
b.liu5fa9e772023-11-23 18:00:55 +0800182 return 0;
183}
184
luojin071b3852024-01-15 15:58:34 +0800185
186//enable set time from gnss
b.liu5fa9e772023-11-23 18:00:55 +0800187int gnss_time_enable(int enable)
188{
189 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800190 if(enable)
191 {
r.xiao8b074132024-02-22 23:57:50 -0800192 set_time_type(LYNQ_TIME_TYPE_GNSS);
luojin071b3852024-01-15 15:58:34 +0800193 }
r.xiao8b074132024-02-22 23:57:50 -0800194 else
195 {
196 set_time_type(LYNQ_TIME_TYPE_UNUSE);
197 }
198
b.liu5fa9e772023-11-23 18:00:55 +0800199 return 0;
200}
201
luojin071b3852024-01-15 15:58:34 +0800202
203//enable set time from user
b.liu5fa9e772023-11-23 18:00:55 +0800204int user_set_time(char* date, char* time)
205{
206 UNUSED(date);
207 UNUSED(time);
luojin071b3852024-01-15 15:58:34 +0800208 if(date == NULL || time == NULL)
209 {
210 return -1;
211 }
b.liu5fa9e772023-11-23 18:00:55 +0800212
luojin071b3852024-01-15 15:58:34 +0800213 int ret = 0;
214 char time_str[128] ={0};
215 memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
216
217 char *p = time;
218 char *p1 = strstr(p, ":");
219 char *p2 = strstr(p1+1, ":");
220 if(p2 == NULL)
221 {
222 sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30
r.xiao8b074132024-02-22 23:57:50 -0800223 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800224 ret = set_time_user(time_str);
225 }else
226 {
227 sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
r.xiao8b074132024-02-22 23:57:50 -0800228 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800229 ret = set_time_user(time_str);
230 }
231
232 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800233}
234
b.liu5fa9e772023-11-23 18:00:55 +0800235
luojin071b3852024-01-15 15:58:34 +0800236//check sysytem type
b.liu5fa9e772023-11-23 18:00:55 +0800237int lynq_get_time_src_status (time_src_status_s * time_src)
238{
239 UNUSED(time_src);
luojin071b3852024-01-15 15:58:34 +0800240 int type = 0;
241 char time_type[] ={0};
242 property_get("persist.mbtk.time_type", time_type, "0");
b.liu5fa9e772023-11-23 18:00:55 +0800243
luojin071b3852024-01-15 15:58:34 +0800244 type = atoi(time_type);
245 printf("time_type :%d", type);
r.xiao8b074132024-02-22 23:57:50 -0800246 if(type == LYNQ_TIME_TYPE_NTP){
luojin071b3852024-01-15 15:58:34 +0800247 time_src->ntp = 1;
r.xiao8b074132024-02-22 23:57:50 -0800248 }else if(type == LYNQ_TIME_TYPE_CELL){
luojin071b3852024-01-15 15:58:34 +0800249 time_src->nitz = 1;
r.xiao8b074132024-02-22 23:57:50 -0800250 }else if(type == LYNQ_TIME_TYPE_GNSS){
luojin071b3852024-01-15 15:58:34 +0800251 time_src->gnss = 1;
r.xiao8b074132024-02-22 23:57:50 -0800252 }else if(type == LYNQ_TIME_TYPE_UNUSE){
253 time_src->ntp = 0;
254 time_src->nitz = 0;
255 time_src->gnss = 0;
luojin071b3852024-01-15 15:58:34 +0800256 }else{
257
258 }
b.liu5fa9e772023-11-23 18:00:55 +0800259 return 0;
260}
261
luojin071b3852024-01-15 15:58:34 +0800262// RTC TIME set to system
263int lynq_sync_time_from_rtc(void)
264{
265 system("hwclock --hctosys");
266 return 0;
267}
268
269// system time set to RTC
b.liu5fa9e772023-11-23 18:00:55 +0800270int lynq_set_rtc_time(void)
271{
luojin071b3852024-01-15 15:58:34 +0800272 system("hwclock --systohc");
b.liu5fa9e772023-11-23 18:00:55 +0800273 return 0;
274}
275
276int lynq_get_rtc_time(unsigned long *ulsec)
277{
278 UNUSED(ulsec);
279
280 return 0;
281}
282
luojin071b3852024-01-15 15:58:34 +0800283