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 | |
| 57 | |
| 58 | |
| 59 | //int req_time_set(int type, char *time, int *cme_err); |
| 60 | static 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 | |
| 86 | static void* ntp_pthread_run() |
| 87 | { |
liuyang | 0a15e08 | 2024-04-19 17:28:49 +0800 | [diff] [blame] | 88 | if (mbtk_net_state_get() == MBTK_NET_STATE_OFF) |
| 89 | { |
| 90 | printf("Network is disconnected. Set time fail."); |
| 91 | return NULL; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 92 | } |
| 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.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 99 | if(atoi(time_type) == LYNQ_TIME_TYPE_NTP) // NTP time |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 100 | { |
| 101 | char time_str[100] = {0}; |
| 102 | time_t time = 0; |
liuyang | 5458290 | 2024-04-19 18:06:25 +0800 | [diff] [blame^] | 103 | while((time = (time_t)mbtk_at_systime()) == 0) |
| 104 | { |
| 105 | printf("NTP client fail!\n"); |
| 106 | LOGI("NTP client fail!\n"); |
| 107 | return NULL; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 108 | } |
| 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 | |
| 125 | int 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 |
| 142 | void 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.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 153 | |
| 154 | int ntp_sync_time(int enable) |
| 155 | { |
| 156 | UNUSED(enable); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 157 | if(enable) |
| 158 | { |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 159 | set_time_type(LYNQ_TIME_TYPE_NTP); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 160 | ntp_pthread_run(); |
| 161 | } |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 162 | else |
| 163 | { |
| 164 | set_time_type(LYNQ_TIME_TYPE_UNUSE); |
| 165 | } |
| 166 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 170 | //enable set time from nitz |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 171 | int modem_time_enable(int enable) |
| 172 | { |
| 173 | UNUSED(enable); |
| 174 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 175 | if(enable) |
| 176 | { |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 177 | set_time_type(LYNQ_TIME_TYPE_CELL); |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | set_time_type(LYNQ_TIME_TYPE_UNUSE); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 182 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 186 | |
| 187 | //enable set time from gnss |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 188 | int gnss_time_enable(int enable) |
| 189 | { |
| 190 | UNUSED(enable); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 191 | if(enable) |
| 192 | { |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 193 | set_time_type(LYNQ_TIME_TYPE_GNSS); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 194 | } |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 195 | else |
| 196 | { |
| 197 | set_time_type(LYNQ_TIME_TYPE_UNUSE); |
| 198 | } |
| 199 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 203 | |
| 204 | //enable set time from user |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 205 | int user_set_time(char* date, char* time) |
| 206 | { |
| 207 | UNUSED(date); |
| 208 | UNUSED(time); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 209 | if(date == NULL || time == NULL) |
| 210 | { |
| 211 | return -1; |
| 212 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 213 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 214 | 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.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 224 | set_time_type(LYNQ_TIME_TYPE_USER); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 225 | ret = set_time_user(time_str); |
| 226 | }else |
| 227 | { |
| 228 | 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] | 229 | set_time_type(LYNQ_TIME_TYPE_USER); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 230 | ret = set_time_user(time_str); |
| 231 | } |
| 232 | |
| 233 | return ret; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 234 | } |
| 235 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 236 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 237 | //check sysytem type |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 238 | int lynq_get_time_src_status (time_src_status_s * time_src) |
| 239 | { |
| 240 | UNUSED(time_src); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 241 | int type = 0; |
| 242 | char time_type[] ={0}; |
| 243 | property_get("persist.mbtk.time_type", time_type, "0"); |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 244 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 245 | type = atoi(time_type); |
| 246 | printf("time_type :%d", type); |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 247 | if(type == LYNQ_TIME_TYPE_NTP){ |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 248 | time_src->ntp = 1; |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 249 | }else if(type == LYNQ_TIME_TYPE_CELL){ |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 250 | time_src->nitz = 1; |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 251 | }else if(type == LYNQ_TIME_TYPE_GNSS){ |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 252 | time_src->gnss = 1; |
r.xiao | 8b07413 | 2024-02-22 23:57:50 -0800 | [diff] [blame] | 253 | }else if(type == LYNQ_TIME_TYPE_UNUSE){ |
| 254 | time_src->ntp = 0; |
| 255 | time_src->nitz = 0; |
| 256 | time_src->gnss = 0; |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 257 | }else{ |
| 258 | |
| 259 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 263 | // RTC TIME set to system |
| 264 | int lynq_sync_time_from_rtc(void) |
| 265 | { |
| 266 | system("hwclock --hctosys"); |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | // system time set to RTC |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 271 | int lynq_set_rtc_time(void) |
| 272 | { |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 273 | system("hwclock --systohc"); |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | int lynq_get_rtc_time(unsigned long *ulsec) |
| 278 | { |
| 279 | UNUSED(ulsec); |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame] | 284 | |