blob: 16933b4d66e1937cc7e1e943aaf0ae6a62728666 [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;
liuyang54582902024-04-19 18:06:25 +0800103 while((time = (time_t)mbtk_at_systime()) == 0)
104 {
105 printf("NTP client fail!\n");
106 LOGI("NTP client fail!\n");
107 return NULL;
luojin071b3852024-01-15 15:58:34 +0800108 }
109 struct tm *tm_t;
110 tm_t = localtime(&time);
111 strftime(time_str,128,"%F %T",tm_t);
112
113 // NTP time
114 metis_strptime(time_str);
115 break;
116 } else {
117 break;
118 }
119
120 sleep(64); // Sleep 64s.
121 }
122 return NULL;
123}
124
125int set_time_user(char* data_time_str)
126{
127
128 int ret = 0;
129 if(strlen(data_time_str) > 0)
130 {
131 ret = metis_strptime(data_time_str);
132 }
133
134 return ret;
135}
136
137
138//MBTK_TIME_TYPE_CELL = 0, //NITZ
139//MBTK_TIME_TYPE_NTP,
140//MBTK_TIME_TYPE_GNSS,
141//MBTK_TIME_TYPE_USER
142void set_time_type(int mbtk_time_type)
143{
144 char type_str[10] = {0};
145 sprintf(type_str, "%d", mbtk_time_type);
146 property_set("persist.mbtk.time_type", type_str);
147
148 return;
149}
150
151
152
b.liu5fa9e772023-11-23 18:00:55 +0800153
154int ntp_sync_time(int enable)
155{
156 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800157 if(enable)
158 {
r.xiao8b074132024-02-22 23:57:50 -0800159 set_time_type(LYNQ_TIME_TYPE_NTP);
luojin071b3852024-01-15 15:58:34 +0800160 ntp_pthread_run();
161 }
r.xiao8b074132024-02-22 23:57:50 -0800162 else
163 {
164 set_time_type(LYNQ_TIME_TYPE_UNUSE);
165 }
166
b.liu5fa9e772023-11-23 18:00:55 +0800167 return 0;
168}
169
luojin071b3852024-01-15 15:58:34 +0800170//enable set time from nitz
b.liu5fa9e772023-11-23 18:00:55 +0800171int modem_time_enable(int enable)
172{
173 UNUSED(enable);
174
luojin071b3852024-01-15 15:58:34 +0800175 if(enable)
176 {
r.xiao8b074132024-02-22 23:57:50 -0800177 set_time_type(LYNQ_TIME_TYPE_CELL);
178 }
179 else
180 {
181 set_time_type(LYNQ_TIME_TYPE_UNUSE);
luojin071b3852024-01-15 15:58:34 +0800182 }
b.liu5fa9e772023-11-23 18:00:55 +0800183 return 0;
184}
185
luojin071b3852024-01-15 15:58:34 +0800186
187//enable set time from gnss
b.liu5fa9e772023-11-23 18:00:55 +0800188int gnss_time_enable(int enable)
189{
190 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800191 if(enable)
192 {
r.xiao8b074132024-02-22 23:57:50 -0800193 set_time_type(LYNQ_TIME_TYPE_GNSS);
luojin071b3852024-01-15 15:58:34 +0800194 }
r.xiao8b074132024-02-22 23:57:50 -0800195 else
196 {
197 set_time_type(LYNQ_TIME_TYPE_UNUSE);
198 }
199
b.liu5fa9e772023-11-23 18:00:55 +0800200 return 0;
201}
202
luojin071b3852024-01-15 15:58:34 +0800203
204//enable set time from user
b.liu5fa9e772023-11-23 18:00:55 +0800205int user_set_time(char* date, char* time)
206{
207 UNUSED(date);
208 UNUSED(time);
luojin071b3852024-01-15 15:58:34 +0800209 if(date == NULL || time == NULL)
210 {
211 return -1;
212 }
b.liu5fa9e772023-11-23 18:00:55 +0800213
luojin071b3852024-01-15 15:58:34 +0800214 int ret = 0;
215 char time_str[128] ={0};
216 memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
217
218 char *p = time;
219 char *p1 = strstr(p, ":");
220 char *p2 = strstr(p1+1, ":");
221 if(p2 == NULL)
222 {
223 sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30
r.xiao8b074132024-02-22 23:57:50 -0800224 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800225 ret = set_time_user(time_str);
226 }else
227 {
228 sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
r.xiao8b074132024-02-22 23:57:50 -0800229 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800230 ret = set_time_user(time_str);
231 }
232
233 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800234}
235
b.liu5fa9e772023-11-23 18:00:55 +0800236
luojin071b3852024-01-15 15:58:34 +0800237//check sysytem type
b.liu5fa9e772023-11-23 18:00:55 +0800238int lynq_get_time_src_status (time_src_status_s * time_src)
239{
240 UNUSED(time_src);
luojin071b3852024-01-15 15:58:34 +0800241 int type = 0;
242 char time_type[] ={0};
243 property_get("persist.mbtk.time_type", time_type, "0");
b.liu5fa9e772023-11-23 18:00:55 +0800244
luojin071b3852024-01-15 15:58:34 +0800245 type = atoi(time_type);
246 printf("time_type :%d", type);
r.xiao8b074132024-02-22 23:57:50 -0800247 if(type == LYNQ_TIME_TYPE_NTP){
luojin071b3852024-01-15 15:58:34 +0800248 time_src->ntp = 1;
r.xiao8b074132024-02-22 23:57:50 -0800249 }else if(type == LYNQ_TIME_TYPE_CELL){
luojin071b3852024-01-15 15:58:34 +0800250 time_src->nitz = 1;
r.xiao8b074132024-02-22 23:57:50 -0800251 }else if(type == LYNQ_TIME_TYPE_GNSS){
luojin071b3852024-01-15 15:58:34 +0800252 time_src->gnss = 1;
r.xiao8b074132024-02-22 23:57:50 -0800253 }else if(type == LYNQ_TIME_TYPE_UNUSE){
254 time_src->ntp = 0;
255 time_src->nitz = 0;
256 time_src->gnss = 0;
luojin071b3852024-01-15 15:58:34 +0800257 }else{
258
259 }
b.liu5fa9e772023-11-23 18:00:55 +0800260 return 0;
261}
262
luojin071b3852024-01-15 15:58:34 +0800263// RTC TIME set to system
264int lynq_sync_time_from_rtc(void)
265{
266 system("hwclock --hctosys");
267 return 0;
268}
269
270// system time set to RTC
b.liu5fa9e772023-11-23 18:00:55 +0800271int lynq_set_rtc_time(void)
272{
luojin071b3852024-01-15 15:58:34 +0800273 system("hwclock --systohc");
b.liu5fa9e772023-11-23 18:00:55 +0800274 return 0;
275}
276
277int lynq_get_rtc_time(unsigned long *ulsec)
278{
279 UNUSED(ulsec);
280
281 return 0;
282}
283
luojin071b3852024-01-15 15:58:34 +0800284