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