blob: b7a5d41e35f9198d8c4a48c8dc9df30c3e2f9961 [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;
103 while((time = (time_t)mbtk_at_systime()) == 0) {
104 usleep(100000);
105 }
106 struct tm *tm_t;
107 tm_t = localtime(&time);
108 strftime(time_str,128,"%F %T",tm_t);
109
110 // NTP time
111 metis_strptime(time_str);
112 break;
113 } else {
114 break;
115 }
116
117 sleep(64); // Sleep 64s.
118 }
119 return NULL;
120}
121
122int set_time_user(char* data_time_str)
123{
124
125 int ret = 0;
126 if(strlen(data_time_str) > 0)
127 {
128 ret = metis_strptime(data_time_str);
129 }
130
131 return ret;
132}
133
134
135//MBTK_TIME_TYPE_CELL = 0, //NITZ
136//MBTK_TIME_TYPE_NTP,
137//MBTK_TIME_TYPE_GNSS,
138//MBTK_TIME_TYPE_USER
139void set_time_type(int mbtk_time_type)
140{
141 char type_str[10] = {0};
142 sprintf(type_str, "%d", mbtk_time_type);
143 property_set("persist.mbtk.time_type", type_str);
144
145 return;
146}
147
148
149
b.liu5fa9e772023-11-23 18:00:55 +0800150
151int ntp_sync_time(int enable)
152{
153 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800154 if(enable)
155 {
r.xiao8b074132024-02-22 23:57:50 -0800156 set_time_type(LYNQ_TIME_TYPE_NTP);
luojin071b3852024-01-15 15:58:34 +0800157 ntp_pthread_run();
158 }
r.xiao8b074132024-02-22 23:57:50 -0800159 else
160 {
161 set_time_type(LYNQ_TIME_TYPE_UNUSE);
162 }
163
b.liu5fa9e772023-11-23 18:00:55 +0800164 return 0;
165}
166
luojin071b3852024-01-15 15:58:34 +0800167//enable set time from nitz
b.liu5fa9e772023-11-23 18:00:55 +0800168int modem_time_enable(int enable)
169{
170 UNUSED(enable);
171
luojin071b3852024-01-15 15:58:34 +0800172 if(enable)
173 {
r.xiao8b074132024-02-22 23:57:50 -0800174 set_time_type(LYNQ_TIME_TYPE_CELL);
175 }
176 else
177 {
178 set_time_type(LYNQ_TIME_TYPE_UNUSE);
luojin071b3852024-01-15 15:58:34 +0800179 }
b.liu5fa9e772023-11-23 18:00:55 +0800180 return 0;
181}
182
luojin071b3852024-01-15 15:58:34 +0800183
184//enable set time from gnss
b.liu5fa9e772023-11-23 18:00:55 +0800185int gnss_time_enable(int enable)
186{
187 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800188 if(enable)
189 {
r.xiao8b074132024-02-22 23:57:50 -0800190 set_time_type(LYNQ_TIME_TYPE_GNSS);
luojin071b3852024-01-15 15:58:34 +0800191 }
r.xiao8b074132024-02-22 23:57:50 -0800192 else
193 {
194 set_time_type(LYNQ_TIME_TYPE_UNUSE);
195 }
196
b.liu5fa9e772023-11-23 18:00:55 +0800197 return 0;
198}
199
luojin071b3852024-01-15 15:58:34 +0800200
201//enable set time from user
b.liu5fa9e772023-11-23 18:00:55 +0800202int user_set_time(char* date, char* time)
203{
204 UNUSED(date);
205 UNUSED(time);
luojin071b3852024-01-15 15:58:34 +0800206 if(date == NULL || time == NULL)
207 {
208 return -1;
209 }
b.liu5fa9e772023-11-23 18:00:55 +0800210
luojin071b3852024-01-15 15:58:34 +0800211 int ret = 0;
212 char time_str[128] ={0};
213 memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
214
215 char *p = time;
216 char *p1 = strstr(p, ":");
217 char *p2 = strstr(p1+1, ":");
218 if(p2 == NULL)
219 {
220 sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30
r.xiao8b074132024-02-22 23:57:50 -0800221 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800222 ret = set_time_user(time_str);
223 }else
224 {
225 sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
r.xiao8b074132024-02-22 23:57:50 -0800226 set_time_type(LYNQ_TIME_TYPE_USER);
luojin071b3852024-01-15 15:58:34 +0800227 ret = set_time_user(time_str);
228 }
229
230 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800231}
232
b.liu5fa9e772023-11-23 18:00:55 +0800233
luojin071b3852024-01-15 15:58:34 +0800234//check sysytem type
b.liu5fa9e772023-11-23 18:00:55 +0800235int lynq_get_time_src_status (time_src_status_s * time_src)
236{
237 UNUSED(time_src);
luojin071b3852024-01-15 15:58:34 +0800238 int type = 0;
239 char time_type[] ={0};
240 property_get("persist.mbtk.time_type", time_type, "0");
b.liu5fa9e772023-11-23 18:00:55 +0800241
luojin071b3852024-01-15 15:58:34 +0800242 type = atoi(time_type);
243 printf("time_type :%d", type);
r.xiao8b074132024-02-22 23:57:50 -0800244 if(type == LYNQ_TIME_TYPE_NTP){
luojin071b3852024-01-15 15:58:34 +0800245 time_src->ntp = 1;
r.xiao8b074132024-02-22 23:57:50 -0800246 }else if(type == LYNQ_TIME_TYPE_CELL){
luojin071b3852024-01-15 15:58:34 +0800247 time_src->nitz = 1;
r.xiao8b074132024-02-22 23:57:50 -0800248 }else if(type == LYNQ_TIME_TYPE_GNSS){
luojin071b3852024-01-15 15:58:34 +0800249 time_src->gnss = 1;
r.xiao8b074132024-02-22 23:57:50 -0800250 }else if(type == LYNQ_TIME_TYPE_UNUSE){
251 time_src->ntp = 0;
252 time_src->nitz = 0;
253 time_src->gnss = 0;
luojin071b3852024-01-15 15:58:34 +0800254 }else{
255
256 }
b.liu5fa9e772023-11-23 18:00:55 +0800257 return 0;
258}
259
luojin071b3852024-01-15 15:58:34 +0800260// RTC TIME set to system
261int lynq_sync_time_from_rtc(void)
262{
263 system("hwclock --hctosys");
264 return 0;
265}
266
267// system time set to RTC
b.liu5fa9e772023-11-23 18:00:55 +0800268int lynq_set_rtc_time(void)
269{
luojin071b3852024-01-15 15:58:34 +0800270 system("hwclock --systohc");
b.liu5fa9e772023-11-23 18:00:55 +0800271 return 0;
272}
273
274int lynq_get_rtc_time(unsigned long *ulsec)
275{
276 UNUSED(ulsec);
277
278 return 0;
279}
280
luojin071b3852024-01-15 15:58:34 +0800281