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 { |
| 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 |
| 37 | int ntp_sync_time(int enable); |
| 38 | //enable set time from nitz |
| 39 | int modem_time_enable(int enable); |
| 40 | //enable set time from gnss |
| 41 | int gnss_time_enable(int enable); |
| 42 | //enable set time from user |
| 43 | int user_set_time(char* date, char* time); |
| 44 | // RTC TIME set to system |
| 45 | int lynq_sync_time_from_rtc(void); |
| 46 | //check sysytem type |
| 47 | int lynq_get_time_src_status (time_src_status_s * time_src); |
| 48 | // system time set to RTC |
| 49 | int lynq_set_rtc_time(void); |
| 50 | // get RTC time |
| 51 | int lynq_get_rtc_time(unsigned long *ulsec); |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 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 | |
| 84 | static 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 | |
| 119 | int 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 |
| 136 | void 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.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 147 | |
| 148 | int ntp_sync_time(int enable) |
| 149 | { |
| 150 | UNUSED(enable); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 151 | if(enable) |
| 152 | { |
| 153 | set_time_type(MBTK_TIME_TYPE_NTP); |
| 154 | ntp_pthread_run(); |
| 155 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 159 | //enable set time from nitz |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 160 | int modem_time_enable(int enable) |
| 161 | { |
| 162 | UNUSED(enable); |
| 163 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 164 | if(enable) |
| 165 | { |
| 166 | set_time_type(MBTK_TIME_TYPE_CELL); |
| 167 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 171 | |
| 172 | //enable set time from gnss |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 173 | int gnss_time_enable(int enable) |
| 174 | { |
| 175 | UNUSED(enable); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 176 | if(enable) |
| 177 | { |
| 178 | set_time_type(MBTK_TIME_TYPE_GNSS); |
| 179 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 183 | |
| 184 | //enable set time from user |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 185 | int user_set_time(char* date, char* time) |
| 186 | { |
| 187 | UNUSED(date); |
| 188 | UNUSED(time); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 189 | if(date == NULL || time == NULL) |
| 190 | { |
| 191 | return -1; |
| 192 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 193 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 194 | 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.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 214 | } |
| 215 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 216 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 217 | //check sysytem type |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 218 | int lynq_get_time_src_status (time_src_status_s * time_src) |
| 219 | { |
| 220 | UNUSED(time_src); |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 221 | int type = 0; |
| 222 | char time_type[] ={0}; |
| 223 | property_get("persist.mbtk.time_type", time_type, "0"); |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 224 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 225 | 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.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 239 | // RTC TIME set to system |
| 240 | int lynq_sync_time_from_rtc(void) |
| 241 | { |
| 242 | system("hwclock --hctosys"); |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | // system time set to RTC |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 247 | int lynq_set_rtc_time(void) |
| 248 | { |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 249 | system("hwclock --systohc"); |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | int lynq_get_rtc_time(unsigned long *ulsec) |
| 254 | { |
| 255 | UNUSED(ulsec); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
luojin | 071b385 | 2024-01-15 15:58:34 +0800 | [diff] [blame^] | 260 | |