b.liu | 4e243dc | 2023-11-27 11:20:00 +0800 | [diff] [blame] | 1 | #include "lynq_systime.h" |
| 2 | #include "mbtk_type.h" |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 3 | #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 | |
| 29 | typedef enum { |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 30 | 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; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 37 | |
| 38 | //enable set time from ntp |
| 39 | int ntp_sync_time(int enable); |
| 40 | //enable set time from nitz |
| 41 | int modem_time_enable(int enable); |
| 42 | //enable set time from gnss |
| 43 | int gnss_time_enable(int enable); |
| 44 | //enable set time from user |
| 45 | int user_set_time(char* date, char* time); |
| 46 | // RTC TIME set to system |
| 47 | int lynq_sync_time_from_rtc(void); |
| 48 | //check sysytem type |
| 49 | int lynq_get_time_src_status (time_src_status_s * time_src); |
| 50 | // system time set to RTC |
| 51 | int lynq_set_rtc_time(void); |
| 52 | // get RTC time |
| 53 | int lynq_get_rtc_time(unsigned long *ulsec); |
| 54 | |
| 55 | |
| 56 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 57 | //int req_time_set(int type, char *time, int *cme_err); |
| 58 | static 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 | { |
luojian | 88976a3 | 2024-08-14 17:40:01 +0800 | [diff] [blame] | 66 | time_t _t = (long)mktime(&stm); |
| 67 | tzset(); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 68 | tv.tv_sec = _t; |
luojian | 88976a3 | 2024-08-14 17:40:01 +0800 | [diff] [blame] | 69 | tv.tv_usec = 0; |
| 70 | if(_t == -1) |
| 71 | { |
| 72 | printf("Set time :%s", str_time); |
| 73 | printf("timestamp:%ld", _t); |
| 74 | printf("mktime error, reason: %s\n", strerror(errno)); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 75 | return -1; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 76 | } |
luojian | 88976a3 | 2024-08-14 17:40:01 +0800 | [diff] [blame] | 77 | |
| 78 | if(settimeofday(&tv, NULL)) { |
| 79 | printf("Set time :%s", str_time); |
| 80 | printf("timestamp:%ld", _t); |
| 81 | printf("mktime error, reason: %s\n", strerror(errno)); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | printf("Success Set time to %s.\n", str_time); |
| 86 | return 0; |
| 87 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 88 | } else { |
| 89 | printf("Set time fail."); |
| 90 | return -1; |
| 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 96 | static void* ntp_pthread_run(int* ntp_flag) |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 97 | { |
liuyang | 0a15e08 | 2024-04-19 17:28:49 +0800 | [diff] [blame] | 98 | if (mbtk_net_state_get() == MBTK_NET_STATE_OFF) |
| 99 | { |
| 100 | printf("Network is disconnected. Set time fail."); |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 101 | if(NULL != ntp_flag) |
| 102 | { |
| 103 | *ntp_flag = -1; |
| 104 | } |
liuyang | 0a15e08 | 2024-04-19 17:28:49 +0800 | [diff] [blame] | 105 | return NULL; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 106 | } |
| 107 | printf("Network is connected."); |
| 108 | |
| 109 | char time_type[10]; |
| 110 | while(1){ |
| 111 | memset(time_type, 0, 10); |
| 112 | property_get("persist.mbtk.time_type", time_type, "0"); |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 113 | if(atoi(time_type) == LYNQ_TIME_TYPE_NTP) // NTP time |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 114 | { |
| 115 | char time_str[100] = {0}; |
| 116 | time_t time = 0; |
liuyang | d0f7fdb | 2024-04-22 13:53:23 +0800 | [diff] [blame] | 117 | if((time = (time_t)mbtk_at_systime()) == 0) |
liuyang | 5458290 | 2024-04-19 18:06:25 +0800 | [diff] [blame] | 118 | { |
| 119 | printf("NTP client fail!\n"); |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 120 | if(NULL != ntp_flag) |
| 121 | { |
| 122 | *ntp_flag = -1; |
| 123 | } |
liuyang | 5458290 | 2024-04-19 18:06:25 +0800 | [diff] [blame] | 124 | return NULL; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 125 | } |
luojian | 88976a3 | 2024-08-14 17:40:01 +0800 | [diff] [blame] | 126 | #if 1 |
| 127 | struct tm CurlocalTime; |
| 128 | localtime_r(&time, &CurlocalTime); |
| 129 | CurlocalTime.tm_hour += 8; |
| 130 | char dateTime[30]; |
| 131 | strftime(dateTime, 30, "%Y-%m-%d %H:%M:%S %A", &CurlocalTime); |
| 132 | |
| 133 | printf("dateTime:%s, %ld\n", dateTime, time+28800); |
| 134 | |
| 135 | struct timeval tv; |
| 136 | tv.tv_sec = time; |
| 137 | tv.tv_sec += 28800; |
| 138 | tv.tv_usec = 0; |
| 139 | |
| 140 | if(settimeofday(&tv, NULL)) { |
| 141 | printf("Set time :%s", dateTime); |
| 142 | printf("timestamp:%ld, tv.tv_sec:%ld\n", time, tv.tv_sec); |
| 143 | |
| 144 | if(settimeofday(&tv, NULL)) { |
| 145 | *ntp_flag = -1; |
| 146 | printf("mktime error, reason: %s\n", strerror(errno)); |
| 147 | return NULL; |
| 148 | } |
| 149 | } |
| 150 | printf("Set time success\n"); |
| 151 | lynq_set_rtc_time(); |
| 152 | #else |
| 153 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 154 | struct tm *tm_t; |
| 155 | tm_t = localtime(&time); |
luojian | 88976a3 | 2024-08-14 17:40:01 +0800 | [diff] [blame] | 156 | tm_t->tm_hour += 8; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 157 | strftime(time_str,128,"%F %T",tm_t); |
| 158 | |
| 159 | // NTP time |
luojian | 88976a3 | 2024-08-14 17:40:01 +0800 | [diff] [blame] | 160 | if(metis_strptime(time_str)) |
| 161 | { |
| 162 | *ntp_flag = -1; |
| 163 | return NULL; |
| 164 | } |
| 165 | #endif |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 166 | break; |
| 167 | } else { |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | sleep(64); // Sleep 64s. |
| 172 | } |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 173 | if(NULL != ntp_flag) |
| 174 | { |
| 175 | *ntp_flag = 0; |
| 176 | } |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | int set_time_user(char* data_time_str) |
| 181 | { |
| 182 | |
| 183 | int ret = 0; |
| 184 | if(strlen(data_time_str) > 0) |
| 185 | { |
| 186 | ret = metis_strptime(data_time_str); |
| 187 | } |
| 188 | |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | //MBTK_TIME_TYPE_CELL = 0, //NITZ |
| 194 | //MBTK_TIME_TYPE_NTP, |
| 195 | //MBTK_TIME_TYPE_GNSS, |
| 196 | //MBTK_TIME_TYPE_USER |
| 197 | void set_time_type(int mbtk_time_type) |
| 198 | { |
| 199 | char type_str[10] = {0}; |
| 200 | sprintf(type_str, "%d", mbtk_time_type); |
| 201 | property_set("persist.mbtk.time_type", type_str); |
| 202 | |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 208 | |
| 209 | int ntp_sync_time(int enable) |
| 210 | { |
liuyang | 1ce96df | 2024-05-29 19:14:34 +0800 | [diff] [blame] | 211 | if(0 != enable && 1 != enable) |
| 212 | { |
| 213 | return -1; |
| 214 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 215 | UNUSED(enable); |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 216 | int ntp_status = 0; |
luojian | 88976a3 | 2024-08-14 17:40:01 +0800 | [diff] [blame] | 217 | int ret = 0; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 218 | if(enable) |
| 219 | { |
luojian | 88976a3 | 2024-08-14 17:40:01 +0800 | [diff] [blame] | 220 | set_time_type(LYNQ_TIME_TYPE_NTP); |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 221 | ntp_pthread_run(&ntp_status); |
| 222 | if(ntp_status == 0) |
| 223 | { |
luojian | 88976a3 | 2024-08-14 17:40:01 +0800 | [diff] [blame] | 224 | ret = 0; |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 225 | } |
| 226 | else |
| 227 | { |
luojian | 88976a3 | 2024-08-14 17:40:01 +0800 | [diff] [blame] | 228 | ret = -1; |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 229 | set_time_type(LYNQ_TIME_TYPE_UNUSE); |
| 230 | } |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 231 | } |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 232 | else |
| 233 | { |
| 234 | set_time_type(LYNQ_TIME_TYPE_UNUSE); |
| 235 | } |
| 236 | |
luojian | 88976a3 | 2024-08-14 17:40:01 +0800 | [diff] [blame] | 237 | return ret; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 238 | } |
| 239 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 240 | //enable set time from nitz |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 241 | int modem_time_enable(int enable) |
| 242 | { |
| 243 | UNUSED(enable); |
| 244 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 245 | if(enable) |
| 246 | { |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 247 | set_time_type(LYNQ_TIME_TYPE_CELL); |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | set_time_type(LYNQ_TIME_TYPE_UNUSE); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 252 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 256 | |
| 257 | //enable set time from gnss |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 258 | int gnss_time_enable(int enable) |
| 259 | { |
| 260 | UNUSED(enable); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 261 | if(enable) |
| 262 | { |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 263 | set_time_type(LYNQ_TIME_TYPE_GNSS); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 264 | } |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 265 | else |
| 266 | { |
| 267 | set_time_type(LYNQ_TIME_TYPE_UNUSE); |
| 268 | } |
| 269 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 273 | |
| 274 | //enable set time from user |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 275 | int user_set_time(char* date, char* time) |
| 276 | { |
| 277 | UNUSED(date); |
| 278 | UNUSED(time); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 279 | if(date == NULL || time == NULL) |
| 280 | { |
| 281 | return -1; |
| 282 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 283 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 284 | int ret = 0; |
| 285 | char time_str[128] ={0}; |
| 286 | memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX); |
| 287 | |
| 288 | char *p = time; |
| 289 | char *p1 = strstr(p, ":"); |
| 290 | char *p2 = strstr(p1+1, ":"); |
| 291 | if(p2 == NULL) |
| 292 | { |
| 293 | sprintf(time_str, "%s %s:00", date, time); //2023-11-30 11:30 |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 294 | set_time_type(LYNQ_TIME_TYPE_USER); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 295 | ret = set_time_user(time_str); |
| 296 | }else |
| 297 | { |
| 298 | sprintf(time_str, "%s %s", date, time); //2023-11-30 11:30:31 |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 299 | set_time_type(LYNQ_TIME_TYPE_USER); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 300 | ret = set_time_user(time_str); |
| 301 | } |
| 302 | |
| 303 | return ret; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 304 | } |
| 305 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 306 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 307 | //check sysytem type |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 308 | int lynq_get_time_src_status (time_src_status_s * time_src) |
| 309 | { |
| 310 | UNUSED(time_src); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 311 | int type = 0; |
| 312 | char time_type[] ={0}; |
| 313 | property_get("persist.mbtk.time_type", time_type, "0"); |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 314 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 315 | type = atoi(time_type); |
| 316 | printf("time_type :%d", type); |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 317 | if(type == LYNQ_TIME_TYPE_NTP) |
| 318 | { |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 319 | time_src->ntp = 1; |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 320 | time_src->nitz = 0; |
| 321 | time_src->gnss = 0; |
| 322 | } |
| 323 | else if(type == LYNQ_TIME_TYPE_CELL) |
| 324 | { |
| 325 | time_src->ntp = 0; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 326 | time_src->nitz = 1; |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 327 | time_src->gnss = 0; |
| 328 | } |
| 329 | else if(type == LYNQ_TIME_TYPE_GNSS) |
| 330 | { |
| 331 | time_src->ntp = 0; |
| 332 | time_src->nitz = 0; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 333 | time_src->gnss = 1; |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 334 | } |
| 335 | else if(type == LYNQ_TIME_TYPE_UNUSE) |
| 336 | { |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 337 | time_src->ntp = 0; |
| 338 | time_src->nitz = 0; |
| 339 | time_src->gnss = 0; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 340 | } |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 341 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 345 | // RTC TIME set to system |
| 346 | int lynq_sync_time_from_rtc(void) |
| 347 | { |
| 348 | system("hwclock --hctosys"); |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | // system time set to RTC |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 353 | int lynq_set_rtc_time(void) |
| 354 | { |
luojian | 9b9a928 | 2024-07-04 18:56:21 +0800 | [diff] [blame] | 355 | // system("hwclock --systohc"); |
| 356 | system("hwclock -w rtc0"); |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | int lynq_get_rtc_time(unsigned long *ulsec) |
| 361 | { |
| 362 | UNUSED(ulsec); |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 367 | |