blob: d027b62a67f567284e6d3050557cbcaf45a2b63c [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 {
30 MBTK_TIME_TYPE_CELL = 0, //NITZ
31 MBTK_TIME_TYPE_NTP,
32 MBTK_TIME_TYPE_GNSS,
33 MBTK_TIME_TYPE_USER
34} mbtk_time_type_enum;
35
36//enable set time from ntp
37int ntp_sync_time(int enable);
38//enable set time from nitz
39int modem_time_enable(int enable);
40//enable set time from gnss
41int gnss_time_enable(int enable);
42//enable set time from user
43int user_set_time(char* date, char* time);
44// RTC TIME set to system
45int lynq_sync_time_from_rtc(void);
46//check sysytem type
47int lynq_get_time_src_status (time_src_status_s * time_src);
48// system time set to RTC
49int lynq_set_rtc_time(void);
50// get RTC time
51int lynq_get_rtc_time(unsigned long *ulsec);
52
53
54
55
56
57//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
84static void* ntp_pthread_run()
85{
86 // Waitting for network connected.
87 while(mbtk_net_state_get() == MBTK_NET_STATE_OFF) {
88 sleep(1);
89 }
90 printf("Network is connected.");
91
92 char time_type[10];
93 while(1){
94 memset(time_type, 0, 10);
95 property_get("persist.mbtk.time_type", time_type, "0");
96 if(atoi(time_type) == MBTK_TIME_TYPE_NTP) // NTP time
97 {
98 char time_str[100] = {0};
99 time_t time = 0;
100 while((time = (time_t)mbtk_at_systime()) == 0) {
101 usleep(100000);
102 }
103 struct tm *tm_t;
104 tm_t = localtime(&time);
105 strftime(time_str,128,"%F %T",tm_t);
106
107 // NTP time
108 metis_strptime(time_str);
109 break;
110 } else {
111 break;
112 }
113
114 sleep(64); // Sleep 64s.
115 }
116 return NULL;
117}
118
119int set_time_user(char* data_time_str)
120{
121
122 int ret = 0;
123 if(strlen(data_time_str) > 0)
124 {
125 ret = metis_strptime(data_time_str);
126 }
127
128 return ret;
129}
130
131
132//MBTK_TIME_TYPE_CELL = 0, //NITZ
133//MBTK_TIME_TYPE_NTP,
134//MBTK_TIME_TYPE_GNSS,
135//MBTK_TIME_TYPE_USER
136void set_time_type(int mbtk_time_type)
137{
138 char type_str[10] = {0};
139 sprintf(type_str, "%d", mbtk_time_type);
140 property_set("persist.mbtk.time_type", type_str);
141
142 return;
143}
144
145
146
b.liu5fa9e772023-11-23 18:00:55 +0800147
148int ntp_sync_time(int enable)
149{
150 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800151 if(enable)
152 {
153 set_time_type(MBTK_TIME_TYPE_NTP);
154 ntp_pthread_run();
155 }
b.liu5fa9e772023-11-23 18:00:55 +0800156 return 0;
157}
158
luojin071b3852024-01-15 15:58:34 +0800159//enable set time from nitz
b.liu5fa9e772023-11-23 18:00:55 +0800160int modem_time_enable(int enable)
161{
162 UNUSED(enable);
163
luojin071b3852024-01-15 15:58:34 +0800164 if(enable)
165 {
166 set_time_type(MBTK_TIME_TYPE_CELL);
167 }
b.liu5fa9e772023-11-23 18:00:55 +0800168 return 0;
169}
170
luojin071b3852024-01-15 15:58:34 +0800171
172//enable set time from gnss
b.liu5fa9e772023-11-23 18:00:55 +0800173int gnss_time_enable(int enable)
174{
175 UNUSED(enable);
luojin071b3852024-01-15 15:58:34 +0800176 if(enable)
177 {
178 set_time_type(MBTK_TIME_TYPE_GNSS);
179 }
b.liu5fa9e772023-11-23 18:00:55 +0800180 return 0;
181}
182
luojin071b3852024-01-15 15:58:34 +0800183
184//enable set time from user
b.liu5fa9e772023-11-23 18:00:55 +0800185int user_set_time(char* date, char* time)
186{
187 UNUSED(date);
188 UNUSED(time);
luojin071b3852024-01-15 15:58:34 +0800189 if(date == NULL || time == NULL)
190 {
191 return -1;
192 }
b.liu5fa9e772023-11-23 18:00:55 +0800193
luojin071b3852024-01-15 15:58:34 +0800194 int ret = 0;
195 char time_str[128] ={0};
196 memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX);
197
198 char *p = time;
199 char *p1 = strstr(p, ":");
200 char *p2 = strstr(p1+1, ":");
201 if(p2 == NULL)
202 {
203 sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30
204 set_time_type(MBTK_TIME_TYPE_USER);
205 ret = set_time_user(time_str);
206 }else
207 {
208 sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31
209 set_time_type(MBTK_TIME_TYPE_USER);
210 ret = set_time_user(time_str);
211 }
212
213 return ret;
b.liu5fa9e772023-11-23 18:00:55 +0800214}
215
b.liu5fa9e772023-11-23 18:00:55 +0800216
luojin071b3852024-01-15 15:58:34 +0800217//check sysytem type
b.liu5fa9e772023-11-23 18:00:55 +0800218int lynq_get_time_src_status (time_src_status_s * time_src)
219{
220 UNUSED(time_src);
luojin071b3852024-01-15 15:58:34 +0800221 int type = 0;
222 char time_type[] ={0};
223 property_get("persist.mbtk.time_type", time_type, "0");
b.liu5fa9e772023-11-23 18:00:55 +0800224
luojin071b3852024-01-15 15:58:34 +0800225 type = atoi(time_type);
226 printf("time_type :%d", type);
227 if(type == MBTK_TIME_TYPE_NTP){
228 time_src->ntp = 1;
229 }else if(type == MBTK_TIME_TYPE_CELL){
230 time_src->nitz = 1;
231 }else if(type == MBTK_TIME_TYPE_GNSS){
232 time_src->gnss = 1;
233 }else{
234
235 }
b.liu5fa9e772023-11-23 18:00:55 +0800236 return 0;
237}
238
luojin071b3852024-01-15 15:58:34 +0800239// RTC TIME set to system
240int lynq_sync_time_from_rtc(void)
241{
242 system("hwclock --hctosys");
243 return 0;
244}
245
246// system time set to RTC
b.liu5fa9e772023-11-23 18:00:55 +0800247int lynq_set_rtc_time(void)
248{
luojin071b3852024-01-15 15:58:34 +0800249 system("hwclock --systohc");
b.liu5fa9e772023-11-23 18:00:55 +0800250 return 0;
251}
252
253int lynq_get_rtc_time(unsigned long *ulsec)
254{
255 UNUSED(ulsec);
256
257 return 0;
258}
259
luojin071b3852024-01-15 15:58:34 +0800260