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 | { |
| 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 | |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 84 | static void* ntp_pthread_run(int* ntp_flag) |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 85 | { |
liuyang | 0a15e08 | 2024-04-19 17:28:49 +0800 | [diff] [blame] | 86 | if (mbtk_net_state_get() == MBTK_NET_STATE_OFF) |
| 87 | { |
| 88 | printf("Network is disconnected. Set time fail."); |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 89 | if(NULL != ntp_flag) |
| 90 | { |
| 91 | *ntp_flag = -1; |
| 92 | } |
liuyang | 0a15e08 | 2024-04-19 17:28:49 +0800 | [diff] [blame] | 93 | return NULL; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 94 | } |
| 95 | printf("Network is connected."); |
| 96 | |
| 97 | char time_type[10]; |
| 98 | while(1){ |
| 99 | memset(time_type, 0, 10); |
| 100 | property_get("persist.mbtk.time_type", time_type, "0"); |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 101 | if(atoi(time_type) == LYNQ_TIME_TYPE_NTP) // NTP time |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 102 | { |
| 103 | char time_str[100] = {0}; |
| 104 | time_t time = 0; |
liuyang | d0f7fdb | 2024-04-22 13:53:23 +0800 | [diff] [blame] | 105 | if((time = (time_t)mbtk_at_systime()) == 0) |
liuyang | 5458290 | 2024-04-19 18:06:25 +0800 | [diff] [blame] | 106 | { |
| 107 | printf("NTP client fail!\n"); |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 108 | if(NULL != ntp_flag) |
| 109 | { |
| 110 | *ntp_flag = -1; |
| 111 | } |
liuyang | 5458290 | 2024-04-19 18:06:25 +0800 | [diff] [blame] | 112 | return NULL; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 113 | } |
| 114 | struct tm *tm_t; |
| 115 | tm_t = localtime(&time); |
| 116 | strftime(time_str,128,"%F %T",tm_t); |
| 117 | |
| 118 | // NTP time |
| 119 | metis_strptime(time_str); |
| 120 | break; |
| 121 | } else { |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | sleep(64); // Sleep 64s. |
| 126 | } |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 127 | if(NULL != ntp_flag) |
| 128 | { |
| 129 | *ntp_flag = 0; |
| 130 | } |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | int set_time_user(char* data_time_str) |
| 135 | { |
| 136 | |
| 137 | int ret = 0; |
| 138 | if(strlen(data_time_str) > 0) |
| 139 | { |
| 140 | ret = metis_strptime(data_time_str); |
| 141 | } |
| 142 | |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | //MBTK_TIME_TYPE_CELL = 0, //NITZ |
| 148 | //MBTK_TIME_TYPE_NTP, |
| 149 | //MBTK_TIME_TYPE_GNSS, |
| 150 | //MBTK_TIME_TYPE_USER |
| 151 | void set_time_type(int mbtk_time_type) |
| 152 | { |
| 153 | char type_str[10] = {0}; |
| 154 | sprintf(type_str, "%d", mbtk_time_type); |
| 155 | property_set("persist.mbtk.time_type", type_str); |
| 156 | |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 162 | |
| 163 | int ntp_sync_time(int enable) |
| 164 | { |
liuyang | 1ce96df | 2024-05-29 19:14:34 +0800 | [diff] [blame] | 165 | if(0 != enable && 1 != enable) |
| 166 | { |
| 167 | return -1; |
| 168 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 169 | UNUSED(enable); |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 170 | int ntp_status = 0; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 171 | if(enable) |
| 172 | { |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 173 | ntp_pthread_run(&ntp_status); |
| 174 | if(ntp_status == 0) |
| 175 | { |
| 176 | set_time_type(LYNQ_TIME_TYPE_NTP); |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | set_time_type(LYNQ_TIME_TYPE_UNUSE); |
| 181 | } |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 182 | } |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 183 | else |
| 184 | { |
| 185 | set_time_type(LYNQ_TIME_TYPE_UNUSE); |
| 186 | } |
| 187 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 191 | //enable set time from nitz |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 192 | int modem_time_enable(int enable) |
| 193 | { |
| 194 | UNUSED(enable); |
| 195 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 196 | if(enable) |
| 197 | { |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 198 | set_time_type(LYNQ_TIME_TYPE_CELL); |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | set_time_type(LYNQ_TIME_TYPE_UNUSE); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 203 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 207 | |
| 208 | //enable set time from gnss |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 209 | int gnss_time_enable(int enable) |
| 210 | { |
| 211 | UNUSED(enable); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 212 | if(enable) |
| 213 | { |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 214 | set_time_type(LYNQ_TIME_TYPE_GNSS); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 215 | } |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 216 | else |
| 217 | { |
| 218 | set_time_type(LYNQ_TIME_TYPE_UNUSE); |
| 219 | } |
| 220 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 224 | |
| 225 | //enable set time from user |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 226 | int user_set_time(char* date, char* time) |
| 227 | { |
| 228 | UNUSED(date); |
| 229 | UNUSED(time); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 230 | if(date == NULL || time == NULL) |
| 231 | { |
| 232 | return -1; |
| 233 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 234 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 235 | int ret = 0; |
| 236 | char time_str[128] ={0}; |
| 237 | memset(time_str, 0x0, MBTK_AT_NTP_LEN_MAX); |
| 238 | |
| 239 | char *p = time; |
| 240 | char *p1 = strstr(p, ":"); |
| 241 | char *p2 = strstr(p1+1, ":"); |
| 242 | if(p2 == NULL) |
| 243 | { |
| 244 | 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] | 245 | set_time_type(LYNQ_TIME_TYPE_USER); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 246 | ret = set_time_user(time_str); |
| 247 | }else |
| 248 | { |
| 249 | 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] | 250 | set_time_type(LYNQ_TIME_TYPE_USER); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 251 | ret = set_time_user(time_str); |
| 252 | } |
| 253 | |
| 254 | return ret; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 255 | } |
| 256 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 257 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 258 | //check sysytem type |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 259 | int lynq_get_time_src_status (time_src_status_s * time_src) |
| 260 | { |
| 261 | UNUSED(time_src); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 262 | int type = 0; |
| 263 | char time_type[] ={0}; |
| 264 | property_get("persist.mbtk.time_type", time_type, "0"); |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 265 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 266 | type = atoi(time_type); |
| 267 | printf("time_type :%d", type); |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 268 | if(type == LYNQ_TIME_TYPE_NTP) |
| 269 | { |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 270 | time_src->ntp = 1; |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 271 | time_src->nitz = 0; |
| 272 | time_src->gnss = 0; |
| 273 | } |
| 274 | else if(type == LYNQ_TIME_TYPE_CELL) |
| 275 | { |
| 276 | time_src->ntp = 0; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 277 | time_src->nitz = 1; |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 278 | time_src->gnss = 0; |
| 279 | } |
| 280 | else if(type == LYNQ_TIME_TYPE_GNSS) |
| 281 | { |
| 282 | time_src->ntp = 0; |
| 283 | time_src->nitz = 0; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 284 | time_src->gnss = 1; |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 285 | } |
| 286 | else if(type == LYNQ_TIME_TYPE_UNUSE) |
| 287 | { |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 288 | time_src->ntp = 0; |
| 289 | time_src->nitz = 0; |
| 290 | time_src->gnss = 0; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 291 | } |
liuyang | bfc4d7c | 2024-05-07 10:44:28 +0800 | [diff] [blame] | 292 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 296 | // RTC TIME set to system |
| 297 | int lynq_sync_time_from_rtc(void) |
| 298 | { |
| 299 | system("hwclock --hctosys"); |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | // system time set to RTC |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 304 | int lynq_set_rtc_time(void) |
| 305 | { |
luojian | 9b9a928 | 2024-07-04 18:56:21 +0800 | [diff] [blame^] | 306 | // system("hwclock --systohc"); |
| 307 | system("hwclock -w rtc0"); |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | int lynq_get_rtc_time(unsigned long *ulsec) |
| 312 | { |
| 313 | UNUSED(ulsec); |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 318 | |