lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <pthread.h> |
| 2 | #include <sched.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <sys/time.h> |
| 6 | #include <time.h> |
| 7 | #include <errno.h> |
| 8 | #include <sys/msg.h> |
| 9 | #include <sys/ipc.h> |
| 10 | #include <sys/sem.h> |
| 11 | #include <errno.h> |
| 12 | #include <sys/socket.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <arpa/inet.h> |
| 15 | #include <sys/select.h> |
| 16 | #include <sys/prctl.h> |
| 17 | #include <netdb.h> |
| 18 | #include <limits.h> |
| 19 | //#include "softap_api.h" |
| 20 | //#include "errorcode.h" |
| 21 | //#include "cfg_api.h" |
| 22 | //#include "at_com.h" |
| 23 | #include "sntp.h" |
| 24 | |
| 25 | |
| 26 | /************************************************************************** |
| 27 | * º¯ÊýÉùÃ÷Çø |
| 28 | **************************************************************************/ |
| 29 | struct list_head sntp_server_head; |
| 30 | struct list_head wait_sync_sntp; |
| 31 | |
| 32 | int sntp_default_server_count = 13; |
| 33 | int sntp_server_count = 3; |
| 34 | |
| 35 | int g_sntp_cmd_from = 0; // 0: webui, 1:not webui |
| 36 | int g_sntp_success_count = 0; |
| 37 | |
| 38 | int g_sntp_sync_interval_time = 1; //ͬ²½³É¹¦ºó£¬ÔÙ´Î×Ô¶¯Í¬²½µÄ¼ä¸ôʱ¼ä£¬µ¥Î»:Ìì |
| 39 | // ¶Ôsem_idÖ´ÐÐp²Ù×÷ |
| 40 | static int sem_p(int sem_id) |
| 41 | { |
| 42 | struct sembuf sem_buf; |
| 43 | sem_buf.sem_num = 0; //ÐźÅÁ¿±àºÅ |
| 44 | sem_buf.sem_op = -1; //P²Ù×÷ |
| 45 | sem_buf.sem_flg = SEM_UNDO; //ϵͳÍ˳öǰδÊÍ·ÅÐźÅÁ¿£¬ÏµÍ³×Ô¶¯ÊÍ·Å |
| 46 | if (semop(sem_id, &sem_buf, 1) == -1) { |
| 47 | return -1; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | // ¶Ôsem_idÖ´ÐÐV²Ù×÷ |
| 53 | static int sem_v(int sem_id) |
| 54 | { |
| 55 | struct sembuf sem_buf; |
| 56 | sem_buf.sem_num = 0; |
| 57 | sem_buf.sem_op = 1; //V²Ù×÷ |
| 58 | sem_buf.sem_flg = SEM_UNDO; |
| 59 | if (semop(sem_id, &sem_buf, 1) == -1) { |
| 60 | return -1; |
| 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int get_sem(key_t sem_key) |
| 66 | { |
| 67 | int sem_id = -1; |
| 68 | |
| 69 | sem_id = semget(sem_key, 1, 0666 | IPC_CREAT); |
| 70 | if (sem_id < 0) { |
| 71 | if (errno == EEXIST) { |
| 72 | return semget(sem_key, 1, 0666 | IPC_CREAT); |
| 73 | } |
| 74 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]semget errno=%d\n", errno); |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | union semun sem_union; |
| 79 | sem_union.val = 1; |
| 80 | if (semctl(sem_id, 0, SETVAL, sem_union) == -1) { |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP]semget init sem_id= %d, get sem success\n", sem_id);; |
| 85 | return sem_id; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | void zte_sntp_set_error_state(sntp_error_type_t error_type) |
| 92 | { |
| 93 | char buf[NV_ITEM_STRING_LEN_64] = {0}; |
| 94 | |
| 95 | if (error_type == SNTP_ERROR_NOT_GET_HOST) { |
| 96 | strncpy(buf, "error getaddrinfo", sizeof(buf) - 1); |
| 97 | } else if (error_type == SNTP_ERROR_CREATE_SOCKET) { |
| 98 | strncpy(buf, "error create socket", sizeof(buf) - 1); |
| 99 | } else if (error_type == SNTP_ERROR_SELECT_OVER_TIME) { |
| 100 | strncpy(buf, "error selete over time", sizeof(buf) - 1); |
| 101 | } else if (error_type == SNTP_ERROR_SELECT_RETURN_ERR) { |
| 102 | strncpy(buf, "error selete value<0", sizeof(buf) - 1); |
| 103 | } else if (error_type == SNTP_ERROR_SEND_TO) { |
| 104 | strncpy(buf, "error send to", sizeof(buf) - 1); |
| 105 | } else if (error_type == SNTP_ERROR_RECV_FROM) { |
| 106 | strncpy(buf, "error recv from", sizeof(buf) - 1); |
| 107 | } else if (error_type == SNTP_ERROR_ALL_SERVER_INVALID) { |
| 108 | strncpy(buf, "all server invalid", sizeof(buf) - 1); |
| 109 | } else { |
| 110 | strncpy(buf, "error other", sizeof(buf) - 1); |
| 111 | } |
| 112 | //sc_cfg_set(SNTP_ERROR_STATE,buf); |
| 113 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] zte_sntp_set_error_state: %s \n", buf); |
| 114 | } |
| 115 | |
| 116 | void zte_sntp_set_process_result(int result) |
| 117 | { |
| 118 | long now_time = 0; |
| 119 | char now_time_str[NV_ITEM_STRING_LEN_20] = {0}; |
| 120 | int count = 0; |
| 121 | char buf[NV_ITEM_STRING_LEN_20] = {0}; |
| 122 | |
| 123 | if (result == SNTP_RESULT_OK) { |
| 124 | sc_cfg_set(SNTP_PROCESS_RESULT, "success"); |
| 125 | //sc_cfg_set(SNTP_SYN_DONE, "1"); |
| 126 | /* |
| 127 | sc_cfg_get(SNTP_SUCCESS_COUNT, buf, sizeof(buf)); |
| 128 | count = atoi(buf); |
| 129 | count++; |
| 130 | memset(buf,0,sizeof(buf)); |
| 131 | sprintf(buf,"%d", count); |
| 132 | sc_cfg_set(SNTP_SUCCESS_COUNT, buf); |
| 133 | */ |
| 134 | |
| 135 | g_sntp_success_count++; |
| 136 | sc_cfg_set("need_clear_traffic_data", "yes");//½â¾öÈç¹ûÉ豸ͨµçʱ¼äûÓдﵽһÌì´¥·¢sntp£¬ÔÚ¿çÔµÄʱºò¾Í²»ÄÜÇå³ýÔÂÁ÷Á¿Í³¼Æ |
| 137 | slog(MISC_PRINT, SLOG_NORMAL,"[SNTP] zte_sntp_set_process_result = success \r\n"); |
| 138 | zte_sntp_success_notify(); |
| 139 | } else { |
| 140 | sc_cfg_set(SNTP_PROCESS_RESULT, "failure"); |
| 141 | //sc_cfg_set(SNTP_SYN_DONE, "0"); |
| 142 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] zte_sntp_set_process_result = fail \r\n"); |
| 143 | } |
| 144 | |
| 145 | time(&now_time);//ÒÔÃëΪµ¥Î» |
| 146 | sprintf(now_time_str, "%ld", now_time); |
| 147 | sc_cfg_set(SNTP_PROCESS_OVER_TIME, now_time_str); |
| 148 | //sc_cfg_set("systime_mode", "auto"); |
| 149 | sc_cfg_set(SNTP_PROCESS_STATE, "over"); |
| 150 | //zte_sntp_set_end_time(now_time); //Õâ¸öº¯ÊýÉèÖÃsntp_end_timeµÄNVÖµ£¬µ«ÊÇûÓеط½Ê¹ÓøÃNV |
| 151 | } |
| 152 | |
| 153 | int zte_sntp_get_select_interval_time() |
| 154 | { |
| 155 | char interval[NV_ITEM_STRING_LEN_10] = {0}; |
| 156 | |
| 157 | /*µ¥Î»ÎªÃë*/ |
| 158 | sc_cfg_get("sntp_sync_select_interval_time", interval, sizeof(interval)); |
| 159 | if (strlen(interval) == 0) { |
| 160 | return SNTP_DEFAULT_SYNC_SELECT_INTERVAL_TIME; |
| 161 | } |
| 162 | |
| 163 | if (atoi(interval) <= 5) { |
| 164 | return 5; |
| 165 | } |
| 166 | |
| 167 | return atoi(interval); |
| 168 | } |
| 169 | |
| 170 | int zte_sntp_get_group_addr_by_name(char *ntpServer, unsigned long *ip_addr, int max_len) |
| 171 | { |
| 172 | int ret = 0; |
| 173 | |
| 174 | int ip_index = 0; |
| 175 | |
| 176 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_get_group_addr_by_name start\n"); |
| 177 | |
| 178 | struct addrinfo hints = { 0 }; |
| 179 | struct addrinfo *result = NULL; |
| 180 | struct addrinfo *result_pointer = NULL; |
| 181 | |
| 182 | hints.ai_family = AF_UNSPEC; |
| 183 | hints.ai_socktype = SOCK_STREAM; |
| 184 | hints.ai_flags = AI_CANONNAME; |
| 185 | hints.ai_protocol = 0; |
| 186 | |
| 187 | pthread_mutex_lock(&g_sntp_getaddrinfo_mutex); |
| 188 | ret = getaddrinfo(ntpServer, NULL, &hints, &result); |
| 189 | pthread_mutex_unlock(&g_sntp_getaddrinfo_mutex); |
| 190 | |
| 191 | if (ret != 0) { |
| 192 | zte_sntp_set_error_state(SNTP_ERROR_NOT_GET_HOST); |
| 193 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] error in getaddrinfo: %s\n", gai_strerror(ret)); |
| 194 | return SNTP_RESULT_FAIL; |
| 195 | } |
| 196 | |
| 197 | for (result_pointer = result; result_pointer != NULL; result_pointer = result_pointer->ai_next) { |
| 198 | char hostname[1025] = {0}; |
| 199 | |
| 200 | if (result_pointer->ai_family == AF_INET6) { |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | pthread_mutex_lock(&g_sntp_getnameinfo_mutex); |
| 205 | ret = getnameinfo(result_pointer->ai_addr, result_pointer->ai_addrlen, hostname, sizeof hostname, NULL, 0, NI_NUMERICHOST); |
| 206 | pthread_mutex_unlock(&g_sntp_getnameinfo_mutex); |
| 207 | if (ret != 0) { |
| 208 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] error in getnameinfo: %s.\n", gai_strerror(ret)); |
| 209 | continue; |
| 210 | } |
| 211 | |
| 212 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] Server ip = %s.\n", hostname); |
| 213 | |
| 214 | ip_addr[ip_index] = (unsigned long)inet_addr(hostname); |
| 215 | ip_index++; |
| 216 | |
| 217 | if (ip_index >= max_len - 1) { |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_get_group_addr_by_name, get ip count = %d \n", ip_index); |
| 223 | |
| 224 | freeaddrinfo(result); |
| 225 | return (ip_index == 0) ? SNTP_RESULT_FAIL : ip_index; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | int zte_sntp_get_ntp_server(int server_index, char *ntp_server, int max_len) |
| 230 | { |
| 231 | char ntp_server_name[NV_ITEM_STRING_LEN_20] = {0}; |
| 232 | |
| 233 | memset(ntp_server, 0, max_len); |
| 234 | if (server_index >= 0 && server_index < sntp_server_count) { |
| 235 | snprintf(ntp_server_name, sizeof(ntp_server_name), "sntp_server%d", server_index); |
| 236 | sc_cfg_get(ntp_server_name, ntp_server, max_len); |
| 237 | if (strcmp(ntp_server, "Other") == 0) { |
| 238 | memset(ntp_server, 0, max_len); |
| 239 | memset(ntp_server_name, 0, NV_ITEM_STRING_LEN_20); |
| 240 | snprintf(ntp_server_name, sizeof(ntp_server_name), "sntp_other_server%d", server_index); |
| 241 | sc_cfg_get(ntp_server_name, ntp_server, max_len); |
| 242 | } |
| 243 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] zte_sntp_get_ntp_server, %s == %s\n", ntp_server_name, ntp_server); |
| 244 | } |
| 245 | |
| 246 | if (ntp_server[0] == '\0') { |
| 247 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] zte_sntp_get_ntp_server, ntpServer == NULL, server_index = %d \n", server_index); |
| 248 | return SNTP_RESULT_INVALID_SERVER; |
| 249 | } |
| 250 | |
| 251 | return SNTP_RESULT_OK; |
| 252 | } |
| 253 | |
| 254 | int zte_sntp_get_one_ntp_server_ip(int server_index, unsigned long *ip_addr, int max_len) |
| 255 | { |
| 256 | char ntpServer[NV_ITEM_STRING_LEN_64] = {0}; |
| 257 | int result = SNTP_RESULT_OK; |
| 258 | |
| 259 | if (ip_addr == NULL) { |
| 260 | return SNTP_RESULT_FAIL; |
| 261 | } |
| 262 | |
| 263 | result = zte_sntp_get_ntp_server(server_index, ntpServer, NV_ITEM_STRING_LEN_64); |
| 264 | if (result != SNTP_RESULT_OK) { |
| 265 | return SNTP_RESULT_FAIL; |
| 266 | } |
| 267 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_get_one_ntp_server_ip, ntp_server: %s; server_index = %d\n", ntpServer, server_index); |
| 268 | |
| 269 | if (inet_addr(ntpServer) == INADDR_NONE) { |
| 270 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] zte_sntp_get_one_ntp_server_ip start getnameinfo\n"); |
| 271 | result = zte_sntp_get_group_addr_by_name(ntpServer, ip_addr, max_len); |
| 272 | #if 0 |
| 273 | if (result == SNTP_RESULT_FAIL) { |
| 274 | return SNTP_RESULT_FAIL; |
| 275 | } |
| 276 | #endif |
| 277 | } else { |
| 278 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] zte_sntp_get_one_ntp_server_ip, single IP: %s \n", ntpServer); |
| 279 | ip_addr[0] = inet_addr(ntpServer); |
| 280 | result = 1; |
| 281 | } |
| 282 | |
| 283 | return result; |
| 284 | } |
| 285 | |
| 286 | void zte_sntp_set_ip_addr(unsigned long *ip_addr, int max_len, char *ntp_ip) |
| 287 | { |
| 288 | int i = 0; |
| 289 | |
| 290 | for (i = 0; i < max_len; i++) { |
| 291 | if (ip_addr[i] == 0) { |
| 292 | ip_addr[i] = inet_addr(ntp_ip); |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | void zte_sntp_set_ip_addr_hostname(unsigned long *ip_addr, int max_len, char *ntp_ip) |
| 298 | { |
| 299 | int i = 0; |
| 300 | struct hostent* host = NULL; |
| 301 | for (i = 0; i < max_len; i++) { |
| 302 | if (ip_addr[i] == 0) { |
| 303 | host = gethostbyname(ntp_ip); |
| 304 | if (host != NULL) { |
| 305 | ip_addr[i] = *((unsigned long *) host->h_addr); |
| 306 | } |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | int zte_sntp_get_default_ip_addr(unsigned long *ip_addr, int max_len) |
| 313 | { |
| 314 | // int i = 0; |
| 315 | char buf[NV_ITEM_STRING_LEN_64 + 1] = {0}; |
| 316 | char sntp_default[NV_ITEM_STRING_LEN_64] = {0}; |
| 317 | //char index[NV_ITEM_STRING_LEN_10] = {0}; |
| 318 | int count = 0; |
| 319 | |
| 320 | struct sntp_server *temp = NULL; |
| 321 | |
| 322 | list_for_each_entry(temp, &sntp_server_head, list) { |
| 323 | memset(buf, 0, sizeof(buf)); |
| 324 | |
| 325 | strcpy(buf, temp->sntp_server_ip); |
| 326 | |
| 327 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_get_default_ip_addr, %s =%s \n", sntp_default, buf); |
| 328 | if (strlen(buf) == 0) { |
| 329 | continue; |
| 330 | } |
| 331 | |
| 332 | if (temp->is_ipaddr == 1) { |
| 333 | zte_sntp_set_ip_addr(ip_addr, max_len, buf); |
| 334 | } else { |
| 335 | zte_sntp_set_ip_addr_hostname(ip_addr, max_len, buf); |
| 336 | } |
| 337 | count++; |
| 338 | } |
| 339 | |
| 340 | return count; |
| 341 | } |
| 342 | |
| 343 | static int parse_string(char *str, char split, char out_str[][NV_ITEM_STRING_LEN_64]) |
| 344 | { |
| 345 | char *curr = NULL; |
| 346 | char *prev = NULL; |
| 347 | char *tail = NULL; |
| 348 | int count = 0; |
| 349 | |
| 350 | prev = str; |
| 351 | tail = str + strlen(str); |
| 352 | |
| 353 | |
| 354 | while ((curr = strchr(prev, split)) != 0) { |
| 355 | strncpy(out_str[count], prev, curr - prev); |
| 356 | prev = curr + 1; |
| 357 | count++; |
| 358 | } |
| 359 | |
| 360 | if (prev < tail) { |
| 361 | strncpy(out_str[count], prev, tail - prev); |
| 362 | count++; |
| 363 | } |
| 364 | |
| 365 | return count; |
| 366 | } |
| 367 | |
| 368 | //Åж¨ÊäÈëµÄ×Ö·û´®ÊÇ·ñΪipµØÖ· |
| 369 | int check_addr_ip(const char *str, int str_len) |
| 370 | { |
| 371 | char *p = NULL; |
| 372 | int i = 0; |
| 373 | |
| 374 | p = str; |
| 375 | while (*p != '\0' && i < str_len) { |
| 376 | if ((*p < '0' || *p > '9') || (*p != '.')) |
| 377 | return 0; |
| 378 | p++; |
| 379 | i++; |
| 380 | } |
| 381 | return 1; |
| 382 | } |
| 383 | |
| 384 | int zte_sntp_default_server_ip_addr() |
| 385 | { |
| 386 | int i = 0; |
| 387 | char sntp_default_ip[NV_ITEM_STRING_LEN_64 * 20 + 20] = {0}; |
| 388 | char sntp_ip[20][NV_ITEM_STRING_LEN_64] = {{0}}; |
| 389 | //int count = 0; |
| 390 | INIT_LIST_HEAD(&sntp_server_head); |
| 391 | |
| 392 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_default_server_ip_addr\n"); |
| 393 | |
| 394 | struct sntp_server *tmp = NULL; |
| 395 | |
| 396 | sc_cfg_get("sntp_default_ip", sntp_default_ip, sizeof(sntp_default_ip)); |
| 397 | //µÈ´ýÐÂ½Ó¿Ú |
| 398 | sntp_default_server_count = parse_string(sntp_default_ip, ';', sntp_ip); |
| 399 | |
| 400 | for (i = 0; i < sntp_default_server_count; i++) { |
| 401 | tmp = malloc(sizeof(struct sntp_server)); |
| 402 | if (tmp == NULL) { |
| 403 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]server_ip malloc failed\n"); |
| 404 | continue; |
| 405 | } |
| 406 | INIT_LIST_HEAD(&tmp->list); |
| 407 | memset(tmp->sntp_server_ip, 0, sizeof(tmp->sntp_server_ip)); |
| 408 | strncpy(tmp->sntp_server_ip, sntp_ip[i], sizeof(tmp->sntp_server_ip)-1); |
| 409 | tmp->is_ipaddr = check_addr_ip(tmp->sntp_server_ip, strlen(tmp->sntp_server_ip) + 1); //Åж¨¸Ã×Ö·û´®ÊÇipµØÖ·£¬»¹ÊÇÓòÃûµØÖ· |
| 410 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] server_ip sntp_ip[%d] = %s\n", i, sntp_ip[i]); |
| 411 | |
| 412 | list_add_tail(&tmp->list, &sntp_server_head); |
| 413 | } |
| 414 | return sntp_default_server_count; |
| 415 | } |
| 416 | |
| 417 | float zte_sntp_get_timezone() |
| 418 | { |
| 419 | float tz = 0; |
| 420 | char time_zone[NV_ITEM_STRING_LEN_10] = {0}; |
| 421 | |
| 422 | sc_cfg_get("sntp_timezone", time_zone, sizeof(time_zone)); |
| 423 | sscanf(time_zone, "%f", &tz); |
| 424 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] sntp_timezone timezone = %f\n", tz); |
| 425 | return tz; |
| 426 | } |
| 427 | |
| 428 | void zte_sntp_login_timemark_set(time_t synBeforeTime, time_t synAfterTime) |
| 429 | { |
| 430 | long user_login_timemark = 0; |
| 431 | char login_timemark[NV_ITEM_STRING_LEN_20] = {0}; |
| 432 | long now_timemark = 0; |
| 433 | long connect_time = 0; |
| 434 | |
| 435 | sc_cfg_get("user_login_timemark", login_timemark, sizeof(login_timemark)); |
| 436 | user_login_timemark = atol(login_timemark); |
| 437 | if (user_login_timemark == 0 || user_login_timemark > LONG_MAX - 1) { |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | memset(&login_timemark, 0, sizeof(login_timemark)); |
| 442 | connect_time = synBeforeTime - user_login_timemark; |
| 443 | now_timemark = synAfterTime - connect_time; |
| 444 | sprintf(login_timemark, "%ld", now_timemark); |
| 445 | sc_cfg_set("user_login_timemark", login_timemark); |
| 446 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] timemark, connect_time = %ld, synAfterTime = %ld, user_login_timemark=%ld\n", connect_time, synAfterTime, now_timemark); |
| 447 | } |
| 448 | |
| 449 | int zte_sntp_set_clear_traffic_data_nv_flag() |
| 450 | { |
| 451 | char temp[10] = {0}; |
| 452 | //sc_cfg_get(SNTP_SUCCESS_COUNT,temp,sizeof(temp)); |
| 453 | int count = atoi(temp); |
| 454 | |
| 455 | //if(count == 0) |
| 456 | if (g_sntp_success_count == 0) { |
| 457 | slog(MISC_PRINT, SLOG_DEBUG,"[zyl]set nv need_clear = no\n"); |
| 458 | sc_cfg_set("need_clear_traffic_data", "no"); |
| 459 | } else { |
| 460 | slog(MISC_PRINT, SLOG_DEBUG,"[zyl]set nv need_clear = yes\n"); |
| 461 | sc_cfg_set("need_clear_traffic_data", "yes"); |
| 462 | } |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | int zte_sntp_handle_time(struct timeval tp) |
| 467 | { |
| 468 | time_t nowtime; |
| 469 | time_t SynBeforeTime = 0; |
| 470 | int ltime = 0, mtime = 0, ntime = 0; |
| 471 | char SynSystemTotal[NV_ITEM_STRING_LEN_20] = {0}; |
| 472 | char SynPppTotal[NV_ITEM_STRING_LEN_20] = {0}; |
| 473 | char SynSecond[NV_ITEM_STRING_LEN_20] = {0}; |
| 474 | char default_wan_rel[NV_ITEM_STRING_LEN_50] = {0}; |
| 475 | char default_wan6_rel[NV_ITEM_STRING_LEN_50] = {0}; |
| 476 | // int count; |
| 477 | // int flag = 0; |
| 478 | // int i; |
| 479 | // int ret = 0; |
| 480 | int sem_id = -1; |
| 481 | int result = 0; |
| 482 | time(&SynBeforeTime); |
| 483 | |
| 484 | slog(MISC_PRINT, SLOG_DEBUG, "[SYSTEM] auto set sntp time!"); |
| 485 | |
| 486 | sc_cfg_get("syn_after_time", SynSecond, sizeof(SynSecond)); |
| 487 | sscanf(SynSecond, "%d", <ime); |
| 488 | |
| 489 | sc_cfg_get("syn_system_total", SynSystemTotal, sizeof(SynSystemTotal)); |
| 490 | sscanf(SynSystemTotal, "%d", &mtime); |
| 491 | |
| 492 | if(ltime > INT_MAX-1) // kw 3 |
| 493 | { |
| 494 | ltime = INT_MAX; |
| 495 | } |
| 496 | |
| 497 | if(mtime > INT_MAX-1) // kw 3 |
| 498 | { |
| 499 | mtime = INT_MAX; |
| 500 | } |
| 501 | |
| 502 | if (0 != mtime) { |
| 503 | mtime += (SynBeforeTime - ltime); |
| 504 | } else { |
| 505 | mtime = (SynBeforeTime - SNTP_JAN_2000); |
| 506 | } |
| 507 | sprintf(SynSystemTotal, "%d", mtime); |
| 508 | sc_cfg_set("syn_system_total", SynSystemTotal); |
| 509 | |
| 510 | sc_cfg_get("default_wan_rel", default_wan_rel, sizeof(default_wan_rel)); |
| 511 | sc_cfg_get("default_wan6_rel", default_wan6_rel, sizeof(default_wan6_rel)); |
| 512 | if ((0 != strcmp(default_wan_rel, "")) || (0 != strcmp(default_wan6_rel, ""))) { |
| 513 | sc_cfg_get("syn_ppp_total", SynPppTotal, sizeof(SynPppTotal)); |
| 514 | sscanf(SynPppTotal, "%d", &mtime); |
| 515 | |
| 516 | if(mtime > INT_MAX-1) // kw 3 |
| 517 | { |
| 518 | mtime = INT_MAX; |
| 519 | } |
| 520 | |
| 521 | if (0 != mtime) { |
| 522 | mtime += (SynBeforeTime - ltime); |
| 523 | } else { |
| 524 | memset(&SynSecond, 0, sizeof(SynSecond)); |
| 525 | sc_cfg_get("ppp_start_time", SynSecond, sizeof(SynSecond)); |
| 526 | sscanf(SynSecond, "%d", &ntime); |
| 527 | if(ntime > INT_MAX-1) // kw 3 |
| 528 | { |
| 529 | ntime = INT_MAX; |
| 530 | } |
| 531 | mtime = SynBeforeTime - ntime; |
| 532 | } |
| 533 | sprintf(SynPppTotal, "%d", mtime); |
| 534 | sc_cfg_set("syn_ppp_total", SynPppTotal); |
| 535 | sc_cfg_set("syn_order_flag", "ppp_on"); |
| 536 | } else { |
| 537 | sc_cfg_set("syn_order_flag", "ppp_off"); |
| 538 | sc_cfg_set("syn_ppp_total", "0"); |
| 539 | } |
| 540 | |
| 541 | sem_id = get_sem(TIME_SEM_KEY_2); |
| 542 | |
| 543 | if (sem_id != -1) { |
| 544 | result = sem_p(sem_id); |
| 545 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] zte_sntp_handle_time, sem_p, sem_id = %d, result = %d\r\n", sem_id, result); |
| 546 | } |
| 547 | |
| 548 | if (0 == settimeofday(&tp, NULL)) { |
| 549 | system("zte-rtc-clock"); |
| 550 | slog(MISC_PRINT, SLOG_DEBUG, "[zyl]system zte-rtc-clock \n"); |
| 551 | } |
| 552 | |
| 553 | #if 1 |
| 554 | time_t t_synSecond = 0; |
| 555 | t_synSecond = time((time_t *)NULL); |
| 556 | |
| 557 | sprintf(SynSecond, "%d", (int)(t_synSecond)); |
| 558 | sc_cfg_set("syn_after_time", SynSecond); |
| 559 | zte_sntp_login_timemark_set(SynBeforeTime, t_synSecond); |
| 560 | #else |
| 561 | sprintf(SynSecond, "%d", (int)(time((time_t *)NULL))); |
| 562 | sc_cfg_set("syn_after_time", SynSecond); |
| 563 | zte_sntp_login_timemark_set(SynBeforeTime, atol(SynSecond)); |
| 564 | |
| 565 | #endif |
| 566 | |
| 567 | if (sem_id != -1) { |
| 568 | result = sem_v(sem_id); |
| 569 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] zte_sntp_parse_time, sem_v, sem_id = %d, result = %d\r\n", sem_id, result); |
| 570 | } |
| 571 | |
| 572 | return SNTP_RESULT_OK; |
| 573 | } |
| 574 | |
| 575 | |
| 576 | int zte_sntp_parse_time(SNTP_HEADER *sntp_header) |
| 577 | { |
| 578 | NTP_TIME_FMT ct; |
| 579 | struct tm ptm = {0}; |
| 580 | // struct tm time_tm = {0}; |
| 581 | struct timeval time_tv; |
| 582 | float tz = 0; |
| 583 | int trip; |
| 584 | int sem_id = 0; |
| 585 | int result = 0; |
| 586 | char tmp_buf[NV_ITEM_STRING_LEN_10] = {0}; |
| 587 | char temp_nv[NV_ITEM_STRING_LEN_10] = {0}; |
| 588 | //tz = zte_sntp_get_timezone(); |
| 589 | ct.seconds = time(NULL); |
| 590 | ct.fractional = 0; |
| 591 | |
| 592 | struct tm tm_utc = {0}; |
| 593 | struct tm tm_os = {0}; |
| 594 | time_t org_time = 0; |
| 595 | time_t rcv_time = 0; |
| 596 | time_t trn_time = 0; |
| 597 | time_t timezone = 0; |
| 598 | sntp_header->rootDelay = ntohl(sntp_header->rootDelay); |
| 599 | sntp_header->rootDispersion = ntohl(sntp_header->rootDispersion); |
| 600 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] RootDelay = %lu\r\n", sntp_header->rootDelay); |
| 601 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] RootDispersion = %lu\r\n", sntp_header->rootDispersion); |
| 602 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] RefrenceID = %02X %02X %02X %02X\r\n", sntp_header->referenceID[0], sntp_header->referenceID[1], sntp_header->referenceID[2], sntp_header->referenceID[3]); |
| 603 | |
| 604 | org_time = ntohl(sntp_header->originateTimestamp.seconds) - SNTP_SECONDS_FROM_1900_TO_1970; |
| 605 | rcv_time = ntohl(sntp_header->receiveTimestamp.seconds) - SNTP_SECONDS_FROM_1900_TO_1970; |
| 606 | trn_time = ntohl(sntp_header->transmitTimestamp.seconds) - SNTP_SECONDS_FROM_1900_TO_1970; |
| 607 | /* |
| 608 | sntp_header->referenceTimestamp.seconds = ntohl(sntp_header->referenceTimestamp.seconds) - SNTP_SECONDS_FROM_1900_TO_1970 + (long)(tz * 3600); |
| 609 | sntp_header->referenceTimestamp.fractional = ntohl(sntp_header->referenceTimestamp.fractional); |
| 610 | |
| 611 | //ptm = gmtime(&sntp_header->referenceTimestamp.seconds); |
| 612 | gmtime_r(&sntp_header->referenceTimestamp.seconds, &ptm); |
| 613 | //if(ptm) |
| 614 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] ReferenceTime = %d-%02d-%02d %02d:%02d:%02d\r\n", ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec); |
| 615 | |
| 616 | |
| 617 | sntp_header->originateTimestamp.seconds = ntohl(sntp_header->originateTimestamp.seconds) - SNTP_SECONDS_FROM_1900_TO_1970 + (long)(tz * 3600); |
| 618 | sntp_header->originateTimestamp.fractional = ntohl(sntp_header->originateTimestamp.fractional); |
| 619 | |
| 620 | //ptm = gmtime(&sntp_header->originateTimestamp.seconds); |
| 621 | gmtime_r(&sntp_header->originateTimestamp.seconds, &ptm); |
| 622 | //if(ptm) |
| 623 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] OriginateTime = %d-%02d-%02d %02d:%02d:%02d\r\n", ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec); |
| 624 | |
| 625 | |
| 626 | sntp_header->receiveTimestamp.seconds = ntohl(sntp_header->receiveTimestamp.seconds) - SNTP_SECONDS_FROM_1900_TO_1970 + (long)(tz * 3600); |
| 627 | sntp_header->receiveTimestamp.fractional = ntohl(sntp_header->receiveTimestamp.fractional); |
| 628 | |
| 629 | //ptm = gmtime(&sntp_header->receiveTimestamp.seconds); |
| 630 | gmtime_r(&sntp_header->receiveTimestamp.seconds, &ptm); |
| 631 | //if(ptm) |
| 632 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] ReceiveTime = %d-%02d-%02d %02d:%02d:%02d\r\n", ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec); |
| 633 | |
| 634 | |
| 635 | sntp_header->transmitTimestamp.seconds = ntohl(sntp_header->transmitTimestamp.seconds) - SNTP_SECONDS_FROM_1900_TO_1970 + (long)(tz * 3600); |
| 636 | sntp_header->transmitTimestamp.fractional = ntohl(sntp_header->transmitTimestamp.fractional); |
| 637 | |
| 638 | //ptm = gmtime(&sntp_header->transmitTimestamp.seconds); |
| 639 | gmtime_r(&sntp_header->transmitTimestamp.seconds, &ptm); |
| 640 | //if(ptm) |
| 641 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] transmitTime = %d-%02d-%02d %02d:%02d:%02d (%d s)\r\n", ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec, (int)sntp_header->transmitTimestamp.seconds); |
| 642 | */ |
| 643 | gmtime_r(&trn_time, &tm_utc); |
| 644 | localtime_r(&trn_time, &tm_os); |
| 645 | timezone = mktime(&tm_os) - mktime(&tm_utc); |
| 646 | |
| 647 | trip = (ct.seconds - org_time) - (trn_time- rcv_time); |
| 648 | //slog(MISC_PRINT, SLOG_DEBUG,"[zyl] ct.seconds = %ld\r\n", ct.seconds); |
| 649 | //slog(MISC_PRINT, SLOG_DEBUG,"[zyl] sntp_header->originateTimestamp.seconds = %ld\r\n", sntp_header->originateTimestamp.seconds); |
| 650 | //slog(MISC_PRINT, SLOG_DEBUG,"[zyl] sntp_header->transmitTimestamp.seconds = %ld\r\n", sntp_header->transmitTimestamp.seconds); |
| 651 | //slog(MISC_PRINT, SLOG_DEBUG,"[zyl] sntp_header->receiveTimestamp.seconds = %ld\r\n", sntp_header->receiveTimestamp.seconds); |
| 652 | |
| 653 | ct.seconds = trn_time + trip / 2; |
| 654 | ct.fractional = ntohl(sntp_header->transmitTimestamp.fractional); |
| 655 | /* |
| 656 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] RoundTripDelay = %d\r\n", trip); |
| 657 | //ptm = gmtime(&ct.seconds); |
| 658 | gmtime_r(&ct.seconds, &ptm); |
| 659 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] Synchronize system time to %d-%02d-%02d %02d:%02d:%02d\r\n", ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec); |
| 660 | */ |
| 661 | //snprintf(temp_nv,sizeof(temp_nv),"%d",ptm.tm_year+1900); |
| 662 | //sc_cfg_set("sntp_year_fluxstat", temp_nv); |
| 663 | //slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] sntp_year_fluxstat = %s\n", temp_nv); |
| 664 | //snprintf(temp_nv,sizeof(temp_nv),"%d",ptm.tm_mon+1); |
| 665 | //sc_cfg_set("sntp_month_fluxstat", temp_nv); |
| 666 | //slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] sntp_month_fluxstat = %s\n", temp_nv); |
| 667 | //sc_cfg_save(); |
| 668 | time_tv.tv_sec = ct.seconds; |
| 669 | time_tv.tv_usec = 0; |
| 670 | |
| 671 | //slog(MISC_PRINT, SLOG_DEBUG,"[zyl] time_tv.tv_sec = %ld\r\n", time_tv.tv_sec); |
| 672 | |
| 673 | sc_cfg_get("sntp_dst_enable", tmp_buf, sizeof(tmp_buf)); |
| 674 | if (0 == strcmp(tmp_buf, "1")) { |
| 675 | time_tv.tv_sec = time_tv.tv_sec + 60 * 60; |
| 676 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] use dst time!!!\n"); |
| 677 | } |
| 678 | |
| 679 | sem_id = get_sem(TIME_SEM_KEY_1); |
| 680 | if (sem_id != -1) { |
| 681 | result = sem_p(sem_id); |
| 682 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] zte_sntp_parse_time, sem_p, sem_id = %d, result = %d\r\n", sem_id, result); |
| 683 | } |
| 684 | |
| 685 | zte_sntp_handle_time(time_tv); |
| 686 | zte_sntp_set_clear_traffic_data_nv_flag(); |
| 687 | if (sem_id != -1) { |
| 688 | result = sem_v(sem_id); |
| 689 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] zte_sntp_parse_time, sem_v, sem_id = %d, result = %d\r\n", sem_id, result); |
| 690 | } |
| 691 | |
| 692 | return SNTP_RESULT_OK; |
| 693 | } |
| 694 | |
| 695 | int zte_sntp_socket_create() |
| 696 | { |
| 697 | int sock_fd = -1; |
| 698 | sock_fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 699 | if (sock_fd < 0) { |
| 700 | zte_sntp_set_error_state(SNTP_ERROR_CREATE_SOCKET); |
| 701 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] sendSocket Creation Fail!\n"); |
| 702 | return SNTP_RESULT_FAIL; |
| 703 | } |
| 704 | return sock_fd; |
| 705 | } |
| 706 | |
| 707 | void zte_sntp_socket_close(int sock_fd) |
| 708 | { |
| 709 | if (sock_fd != -1) { |
| 710 | close(sock_fd); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | |
| 715 | int zte_sntp_socket_sendto(int sock_fd, unsigned long *ip_addr, int ip_count) |
| 716 | { |
| 717 | struct sockaddr_in toAddr; |
| 718 | NTP_TIME_FMT ct = {0}; |
| 719 | int addrLen; |
| 720 | SNTP_HEADER sntp_header = {0}; |
| 721 | int retValue; |
| 722 | float tz = 0; |
| 723 | int i = 0; |
| 724 | int count = 0; |
| 725 | char IPdotdec[20] = {0}; |
| 726 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_socket_sendto, need send ip count = %d\n", ip_count); |
| 727 | |
| 728 | //tz = zte_sntp_get_timezone(); |
| 729 | toAddr.sin_family = AF_INET; |
| 730 | toAddr.sin_port = htons(SNTP_PORT); |
| 731 | addrLen = sizeof(struct sockaddr); |
| 732 | #ifdef _OS_TOS |
| 733 | ct.seconds = zOss_Time(NULL); |
| 734 | #else |
| 735 | ct.seconds = time(NULL); |
| 736 | #endif |
| 737 | memset(&sntp_header, 0, sizeof(sntp_header)); |
| 738 | sntp_header.liVnMode = 0x1b; |
| 739 | sntp_header.transmitTimestamp.seconds = htonl((long)ct.seconds + (long)SNTP_SECONDS_FROM_1900_TO_1970 - (long)(tz * 3600)); |
| 740 | sntp_header.transmitTimestamp.fractional = 0; |
| 741 | |
| 742 | for (i = 0; i < ip_count; i++) { |
| 743 | if (ip_addr[i] == 0) continue; |
| 744 | |
| 745 | count++; |
| 746 | toAddr.sin_addr.s_addr = ip_addr[i]; |
| 747 | inet_ntop(AF_INET, &toAddr.sin_addr, IPdotdec, 16); |
| 748 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_socket_sendto Send, i = %d, IP: %s \n", i, IPdotdec); |
| 749 | |
| 750 | retValue = sendto(sock_fd, &sntp_header, sizeof(SNTP_HEADER), 0, |
| 751 | (struct sockaddr *)&toAddr, addrLen); |
| 752 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_socket_sendto Send addrLen = %d . \n", addrLen); |
| 753 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_socket_sendto Send success %d chars, need send %d chars\n", retValue, sizeof(SNTP_HEADER)); |
| 754 | //sleep(50); |
| 755 | } |
| 756 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] zte_sntp_socket_sendto Send success %d ip!!!!!!!\n", count); |
| 757 | |
| 758 | return SNTP_RESULT_OK; |
| 759 | } |
| 760 | |
| 761 | int zte_sntp_socket_select(int sock_fd) |
| 762 | { |
| 763 | fd_set readfds; |
| 764 | struct timeval tv; |
| 765 | int retValue; |
| 766 | long before_time = 0; |
| 767 | long after_time = 0; |
| 768 | |
| 769 | time(&before_time); |
| 770 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_socket_select start to select ======before_time = %ld\n", before_time); |
| 771 | |
| 772 | FD_ZERO(&readfds); |
| 773 | FD_SET(sock_fd, &readfds); |
| 774 | tv.tv_sec = zte_sntp_get_select_interval_time(); |
| 775 | tv.tv_usec = 0; |
| 776 | do { |
| 777 | retValue = select(sock_fd + 1, &readfds, NULL, NULL, &tv); |
| 778 | } while (retValue < 0 && errno == EINTR); |
| 779 | |
| 780 | if (retValue < 0) { |
| 781 | zte_sntp_set_error_state(SNTP_ERROR_SELECT_RETURN_ERR); |
| 782 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] 1...select data fail...break \n"); |
| 783 | return SNTP_RESULT_FAIL; |
| 784 | } |
| 785 | |
| 786 | time(&after_time); |
| 787 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] 2...select data, after_time = %ld, use time = %ld \n", after_time, after_time - before_time); |
| 788 | |
| 789 | if (retValue == 0) { |
| 790 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] 3...select data, retValue==0, after_time = %ld, use time = %ld \n", after_time, after_time - before_time); |
| 791 | return SNTP_RESULT_CONTINUE; |
| 792 | } |
| 793 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] 4...select data, retValue==1, after_time = %ld, use time = %ld \n", after_time, after_time - before_time); |
| 794 | return SNTP_RESULT_OK; |
| 795 | } |
| 796 | |
| 797 | int zte_sntp_socket_recvfrom(int sock_fd) |
| 798 | { |
| 799 | int retValue = 0; |
| 800 | int addrLen = 0; |
| 801 | //NTP_TIME_FMT ct; |
| 802 | struct sockaddr_in recvAddr; |
| 803 | SNTP_HEADER sntp_header = {0}; |
| 804 | |
| 805 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_socket_recvfrom...Received msg ... \n"); |
| 806 | addrLen = sizeof(struct sockaddr); |
| 807 | memset(&sntp_header, 0, sizeof(sntp_header)); |
| 808 | retValue = recvfrom(sock_fd, (char*)&sntp_header, sizeof(SNTP_HEADER), 0, (struct sockaddr*)&recvAddr, &addrLen); |
| 809 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] recvfrom retValue = %d\n", retValue); |
| 810 | if (retValue <= 0) { |
| 811 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] ...recvfrom data fail... \n"); |
| 812 | zte_sntp_set_error_state(SNTP_ERROR_RECV_FROM); |
| 813 | return SNTP_RESULT_FAIL; |
| 814 | } |
| 815 | |
| 816 | return zte_sntp_parse_time(&sntp_header); |
| 817 | } |
| 818 | |
| 819 | void zte_sntp_init_param() |
| 820 | { |
| 821 | sc_cfg_set(SNTP_PROCESS_STATE, "processing"); |
| 822 | sc_cfg_set(SNTP_PROCESS_RESULT, "processing"); |
| 823 | //sc_cfg_set(SNTP_SYN_DONE, "processing"); |
| 824 | } |
| 825 | |
| 826 | void zte_sntp_process_for_webui() |
| 827 | { |
| 828 | int result = 0; |
| 829 | int sock_fd = -1; |
| 830 | int curr_server_index = 0; |
| 831 | int ip_count = 0; |
| 832 | unsigned long ip_addr[20] = {0}; |
| 833 | //BOOL first_use_config_ip = FALSE; |
| 834 | |
| 835 | zte_sntp_init_param(); |
| 836 | sock_fd = zte_sntp_socket_create(); |
| 837 | if (sock_fd == SNTP_RESULT_FAIL) { |
| 838 | zte_sntp_set_process_result(SNTP_RESULT_FAIL); |
| 839 | return; |
| 840 | } |
| 841 | |
| 842 | /*result = zte_sntp_get_config_ip_addr(&ip_addr, sizeof(ip_addr)); |
| 843 | if(result == SNTP_RESULT_OK) |
| 844 | { |
| 845 | first_use_config_ip = TRUE; |
| 846 | }*/ |
| 847 | |
| 848 | for (curr_server_index = 0; curr_server_index < sntp_server_count; curr_server_index++) { |
| 849 | /*if(!first_use_config_ip) |
| 850 | { |
| 851 | memset(ip_addr, 0, sizeof(ip_addr)); |
| 852 | if(curr_server_index >= SNTP_MAX_SERVER_NUM) |
| 853 | { |
| 854 | zte_sntp_set_error_state(SNTP_ERROR_ALL_SERVER_INVALID); |
| 855 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_get_ntp_server_ip: all server invalide, return \n"); |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | result = zte_sntp_get_one_ntp_server_ip(curr_server_index, &ip_addr, SNTP_MAX_SUPPORT_IP_COUNT); |
| 860 | if(result != SNTP_RESULT_OK) |
| 861 | { |
| 862 | curr_server_index++; |
| 863 | continue; |
| 864 | } |
| 865 | }*/ |
| 866 | memset(ip_addr, 0, sizeof(ip_addr)); |
| 867 | |
| 868 | ip_count = zte_sntp_get_one_ntp_server_ip(curr_server_index, ip_addr, 2 * sntp_server_count); |
| 869 | if (ip_count == SNTP_RESULT_FAIL) { |
| 870 | continue; |
| 871 | } |
| 872 | |
| 873 | result = zte_sntp_socket_sendto(sock_fd, ip_addr, ip_count); |
| 874 | if (result == SNTP_RESULT_FAIL) { |
| 875 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] zte_sntp_socket_sendto failed!\n"); |
| 876 | break; |
| 877 | } |
| 878 | |
| 879 | result = zte_sntp_socket_select(sock_fd); |
| 880 | if (result == SNTP_RESULT_CONTINUE) { |
| 881 | continue; |
| 882 | } else if (result == SNTP_RESULT_FAIL) { |
| 883 | break; |
| 884 | } |
| 885 | |
| 886 | result = zte_sntp_socket_recvfrom(sock_fd); |
| 887 | if (result == SNTP_RESULT_FAIL) { |
| 888 | break; |
| 889 | } |
| 890 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_process_for_webui=========success!\n"); |
| 891 | break; |
| 892 | } |
| 893 | |
| 894 | if (curr_server_index >= sntp_server_count) { |
| 895 | zte_sntp_set_error_state(SNTP_ERROR_ALL_SERVER_INVALID); |
| 896 | result = SNTP_RESULT_FAIL; |
| 897 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] zte_sntp_get_ntp_server_ip: all server invalide, return \n"); |
| 898 | } |
| 899 | zte_sntp_socket_close(sock_fd); |
| 900 | zte_sntp_set_process_result(result); |
| 901 | return; |
| 902 | } |
| 903 | |
| 904 | void zte_sntp_process_for_background() |
| 905 | { |
| 906 | int result = 0; |
| 907 | int sock_fd = -1; |
| 908 | int curr_server_index = 0; |
| 909 | int ip_count = 0; |
| 910 | unsigned long ip_addr[20] = {0}; |
| 911 | BOOL first_use_default_ip = TRUE; |
| 912 | |
| 913 | zte_sntp_init_param(); |
| 914 | sock_fd = zte_sntp_socket_create(); |
| 915 | if (sock_fd == SNTP_RESULT_FAIL) { |
| 916 | zte_sntp_set_process_result(SNTP_RESULT_FAIL); |
| 917 | return; |
| 918 | } |
| 919 | |
| 920 | for (curr_server_index = 0; curr_server_index < sntp_server_count; curr_server_index++) { |
| 921 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_process_for_background first_use_default_ip= %d!\n", first_use_default_ip); |
| 922 | memset(ip_addr, 0, sizeof(ip_addr)); |
| 923 | if (first_use_default_ip) { |
| 924 | ip_count = zte_sntp_get_default_ip_addr(ip_addr, sntp_default_server_count); |
| 925 | curr_server_index--; |
| 926 | } else { |
| 927 | ip_count = zte_sntp_get_one_ntp_server_ip(curr_server_index, ip_addr, 2 * sntp_server_count); |
| 928 | if (ip_count == SNTP_RESULT_FAIL) { |
| 929 | continue; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | if (ip_count == 0) { |
| 934 | first_use_default_ip = FALSE; |
| 935 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_process_for_background ip_count == 0!\n"); |
| 936 | continue; |
| 937 | } |
| 938 | |
| 939 | result = zte_sntp_socket_sendto(sock_fd, ip_addr, ip_count); |
| 940 | if (result == SNTP_RESULT_FAIL) { |
| 941 | break; |
| 942 | } |
| 943 | |
| 944 | result = zte_sntp_socket_select(sock_fd); |
| 945 | if (result == SNTP_RESULT_CONTINUE) { |
| 946 | first_use_default_ip = FALSE; |
| 947 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_socket_select,SNTP_RESULT_CONTINUE curr_server_index = %d!\n", curr_server_index); |
| 948 | continue; |
| 949 | } else if (result == SNTP_RESULT_FAIL) { |
| 950 | break; |
| 951 | } |
| 952 | |
| 953 | result = zte_sntp_socket_recvfrom(sock_fd); |
| 954 | if (result == SNTP_RESULT_FAIL) { |
| 955 | break; |
| 956 | } |
| 957 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] zte_sntp_process_for_background=========success!\n"); |
| 958 | break; |
| 959 | } |
| 960 | if (curr_server_index >= sntp_server_count) { |
| 961 | zte_sntp_set_error_state(SNTP_ERROR_ALL_SERVER_INVALID); |
| 962 | result = SNTP_RESULT_FAIL; |
| 963 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] zte_sntp_get_ntp_server_ip: all server invalide, return \n"); |
| 964 | } |
| 965 | zte_sntp_socket_close(sock_fd); |
| 966 | zte_sntp_set_process_result(result); |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | int zte_sntp_check_ip() |
| 971 | { |
| 972 | char sntp_server[NV_ITEM_STRING_LEN_20] = {0}; |
| 973 | // char sntp_default[NV_ITEM_STRING_LEN_20] = {0}; |
| 974 | char sntp_server_nv[NV_ITEM_STRING_LEN_20] = {0}; |
| 975 | // char sntp_default_nv[NV_ITEM_STRING_LEN_20] = {0}; |
| 976 | struct sntp_server *temp = NULL; |
| 977 | int p = 0; |
| 978 | // int q = 0; |
| 979 | for (p = 0; p < sntp_server_count; p++) { |
| 980 | snprintf(sntp_server, sizeof(sntp_server), "sntp_server%d", p); |
| 981 | sc_cfg_get(sntp_server, sntp_server_nv, sizeof(sntp_server_nv)); |
| 982 | |
| 983 | list_for_each_entry(temp, &sntp_server_head, list) { |
| 984 | if (strcmp(temp->sntp_server_ip, sntp_server_nv) == 0) { |
| 985 | slog(MISC_PRINT, SLOG_DEBUG,"[sntp]sntp server have one record same with default\n"); |
| 986 | slog(MISC_PRINT, SLOG_DEBUG,"[sntp]sntp_server_nv:%s,sntp_server_nv:%s\n", sntp_server_nv, sntp_server_nv); |
| 987 | return 1; |
| 988 | } |
| 989 | } |
| 990 | slog(MISC_PRINT, SLOG_DEBUG,"[sntp]sntp_server%d:%s have no record\n", p, sntp_server_nv); |
| 991 | } |
| 992 | return 0; |
| 993 | |
| 994 | } |
| 995 | void zte_sntp_thread_process() |
| 996 | { |
| 997 | char buf[NV_ITEM_STRING_LEN_20] = {0}; |
| 998 | |
| 999 | //sc_cfg_get("sntp_cmd_from", buf, sizeof(buf)); |
| 1000 | //if(0 == strcmp(buf, "WEBUI")) |
| 1001 | if (g_sntp_cmd_from == 0) { |
| 1002 | if (zte_sntp_check_ip() == 0) { |
| 1003 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] zte_sntp_thread_process, cmd form WEBUI!\n"); |
| 1004 | zte_sntp_process_for_webui(); |
| 1005 | } else { |
| 1006 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] zte_sntp_thread_process, cmd form background!\n"); |
| 1007 | zte_sntp_process_for_background(); |
| 1008 | } |
| 1009 | } else { |
| 1010 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] zte_sntp_thread_process, cmd form background!\n"); |
| 1011 | zte_sntp_process_for_background(); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | void sntp_set_mode(MSG_BUF *msg) |
| 1016 | { |
| 1017 | int mode = -1; |
| 1018 | |
| 1019 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]sntp_set_mode enter \n"); |
| 1020 | |
| 1021 | if (msg->usDataLen <= 0) { |
| 1022 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]sntp_set_mode param error len=0\n"); |
| 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | mode = atoi(msg->aucDataBuf); |
| 1027 | if (mode == 1) { |
| 1028 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] sntp_set_mode: auto\n"); |
| 1029 | sc_cfg_set("sntp_process_state", "idle"); |
| 1030 | sc_cfg_set("sntp_time_set_mode", "auto"); |
| 1031 | sc_cfg_set("sntp_process_result", ""); |
| 1032 | ipc_send_message(MODULE_ID_SNTP, MODULE_ID_SNTP, MSG_CMD_SNTP_START, 0, NULL, 0); |
| 1033 | } else if (mode == 0) { |
| 1034 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] sntp_set_mode: manual\n"); |
| 1035 | sc_cfg_set("sntp_time_set_mode", "manual"); |
| 1036 | sc_cfg_set("sntp_process_result", "success"); |
| 1037 | } else |
| 1038 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] sntp_set_mode: mode unexpected\n"); |
| 1039 | |
| 1040 | //sc_cfg_save(); |
| 1041 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]sntp_set_mode leave \n"); |
| 1042 | } |
| 1043 | |
| 1044 | void sntp_set_time(MSG_BUF *msg) |
| 1045 | { |
| 1046 | struct tm set_tm = {0}; |
| 1047 | struct timeval time_tv; |
| 1048 | long timeSec = 0; |
| 1049 | int ret = 0; |
| 1050 | |
| 1051 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]sntp_set_time, %d, %d \n", sizeof(struct tm), msg->usDataLen); |
| 1052 | |
| 1053 | //memcpy(&rtc_tm, msg->aucDataBuf, sizeof(struct rtc_time)); |
| 1054 | memcpy(&set_tm, msg->aucDataBuf, msg->usDataLen); |
| 1055 | |
| 1056 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]sntp_set_time, year:%d, month:%d, day:%d, hour:%d, min:%d, sec:%d, wday:%d \n", |
| 1057 | set_tm.tm_year, set_tm.tm_mon, set_tm.tm_mday, set_tm.tm_hour, set_tm.tm_min, set_tm.tm_sec, set_tm.tm_wday); |
| 1058 | |
| 1059 | time_tv.tv_sec = mktime(&set_tm); |
| 1060 | time_tv.tv_usec = 0; |
| 1061 | |
| 1062 | if(time_tv.tv_sec < 0) // cov M NEGATIVE_RETURNS |
| 1063 | { |
| 1064 | slog(MISC_PRINT, SLOG_ERR, "mktime return -1, set time_tv.tv_Sec to 0"); |
| 1065 | time_tv.tv_sec = 0; |
| 1066 | } |
| 1067 | |
| 1068 | if (0 != settimeofday(&time_tv, NULL)) { |
| 1069 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP]sntp_set_time failed \n"); |
| 1070 | return ; |
| 1071 | } |
| 1072 | |
| 1073 | timeSec = time(0); |
| 1074 | |
| 1075 | ret = ipc_send_message2(MODULE_ID_SNTP, MODULE_ID_RTC_SERVICE, RTC_MSG_SET_TIME, |
| 1076 | sizeof(timeSec), &timeSec, 0); |
| 1077 | if(ret != 0) |
| 1078 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]send msg RTC_MSG_SET_TIME to rtc-service failed. \n"); |
| 1079 | } |
| 1080 | |
| 1081 | void zte_sntp_start() |
| 1082 | { |
| 1083 | pthread_t sntp_thread_id; |
| 1084 | pthread_attr_t attr; |
| 1085 | int result = 0; |
| 1086 | |
| 1087 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] start sntp\n"); |
| 1088 | |
| 1089 | sc_cfg_set(SNTP_PROCESS_STATE, "idle"); |
| 1090 | pthread_attr_init(&attr); |
| 1091 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 1092 | result = pthread_create(&sntp_thread_id, &attr, zte_sntp_thread_process, NULL); |
| 1093 | pthread_attr_destroy(&attr); |
| 1094 | if (result != 0) { |
| 1095 | sc_cfg_set(SNTP_PROCESS_STATE, "over"); |
| 1096 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] pthread_create faild!\n"); |
| 1097 | } else { |
| 1098 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] pthread_create SUCCESS!\n"); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | void zte_sntp_add_list(MSG_BUF *stMsg) |
| 1103 | { |
| 1104 | char result_buf[NV_ITEM_STRING_LEN_20] = {0}; |
| 1105 | struct sntp_sync_queue *tmp = NULL; |
| 1106 | |
| 1107 | tmp = malloc(sizeof(struct sntp_sync_queue)); |
| 1108 | if (tmp == NULL) { |
| 1109 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]sntp_sync_queue malloc failed\n"); |
| 1110 | return; |
| 1111 | } |
| 1112 | INIT_LIST_HEAD(&tmp->list); |
| 1113 | tmp->src_id = stMsg->src_id; |
| 1114 | list_add_tail(&tmp->list, &wait_sync_sntp); |
| 1115 | |
| 1116 | slog(MISC_PRINT, SLOG_ERR, "[SNTP] zte_sntp_register_id = %d\n", stMsg->src_id); |
| 1117 | |
| 1118 | sc_cfg_get(SNTP_PROCESS_RESULT, result_buf, sizeof(result_buf)); |
| 1119 | if (strcmp(result_buf, "success") == 0) { |
| 1120 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP]get sync register and sync now!\n"); |
| 1121 | ipc_send_message(MODULE_ID_SNTP, stMsg->src_id, MSG_CMD_SNTP_SUCCESS, 0, NULL, 0); |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | void zte_sntp_success_notify() |
| 1126 | { |
| 1127 | struct sntp_sync_queue *temp = NULL; |
| 1128 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] enter zte_sntp_success_notify!\n"); |
| 1129 | |
| 1130 | //¿Ï¶¨´æÔÚÐèҪ֪ͨµÄÄ£¿é |
| 1131 | ipc_send_message(MODULE_ID_SNTP, MODULE_ID_RTC_SERVICE, MSG_CMD_SNTP_SUCCESS, 0, NULL, 0); |
| 1132 | |
| 1133 | //²»Ò»¶¨´æÔÚͨ¹ý×¢²áÐèҪ֪ͨµÄÄ£¿é |
| 1134 | list_for_each_entry(temp, &wait_sync_sntp, list) { |
| 1135 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] zte_sntp_notify_process_id = %d\n", temp->src_id); |
| 1136 | ipc_send_message(MODULE_ID_SNTP, temp->src_id, MSG_CMD_SNTP_SUCCESS, 0, NULL, 0); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | void zte_sntp_remove_list(MSG_BUF *stMsg) |
| 1141 | { |
| 1142 | struct sntp_sync_queue *temp = NULL; |
| 1143 | struct sntp_sync_queue *temp1 = NULL; |
| 1144 | list_for_each_entry_safe(temp, temp1, &wait_sync_sntp, list) { |
| 1145 | if (temp->src_id == stMsg->src_id) { |
| 1146 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP] zte_sntp_unregister_id = %d\n", temp->src_id); |
| 1147 | list_del(&temp->list); |
| 1148 | free(temp); |
| 1149 | break; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | unsigned long net_state_check()//0,error; |
| 1155 | { |
| 1156 | // int i = -1; |
| 1157 | // char sntp_index[32] = {0}; |
| 1158 | struct hostent* host = NULL; |
| 1159 | unsigned long ip = 0; |
| 1160 | struct sntp_server *temp = NULL; |
| 1161 | |
| 1162 | list_for_each_entry(temp, &sntp_server_head, list) { |
| 1163 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP][ZYL]value->%s\n", temp->sntp_server_ip); |
| 1164 | |
| 1165 | if (0 == strcmp("", temp->sntp_server_ip)) { |
| 1166 | continue; |
| 1167 | } |
| 1168 | if (temp->is_ipaddr == 1) { |
| 1169 | ip = inet_addr(temp->sntp_server_ip); |
| 1170 | } else { |
| 1171 | host = gethostbyname(temp->sntp_server_ip); |
| 1172 | if (host != NULL) { |
| 1173 | ip = *((unsigned long *) host->h_addr); |
| 1174 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP][ZYL]ip->%s\n", inet_ntoa(*(struct in_addr *)*host->h_addr_list)); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | if (ip != 0) { |
| 1179 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP][ZYL]get host\n"); |
| 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP]net_state_check error£¬name->%s\n", temp->sntp_server_ip); |
| 1184 | } |
| 1185 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP][ZYL]ip->%lu\n", ip); |
| 1186 | return ip; |
| 1187 | } |
| 1188 | |
| 1189 | int zte_sntp_check()//0:error; |
| 1190 | { |
| 1191 | char state[NV_ITEM_STRING_LEN_20] = {0}; |
| 1192 | char mode[NV_ITEM_STRING_LEN_10] = {0}; |
| 1193 | char default_wan_rel[NV_ITEM_STRING_LEN_50] = {0}; |
| 1194 | |
| 1195 | sc_cfg_get("default_wan_rel", default_wan_rel, sizeof(default_wan_rel)); |
| 1196 | |
| 1197 | if (0 == strcmp(default_wan_rel, "")) { |
| 1198 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] Network disconnected, can not sntp!\n"); |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | sc_cfg_get(SNTP_PROCESS_STATE, state, sizeof(state)); |
| 1203 | sc_cfg_get("sntp_time_set_mode", mode, sizeof(mode)); |
| 1204 | |
| 1205 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] state->%s\n", state); |
| 1206 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP] mode->%s\n", mode); |
| 1207 | |
| 1208 | if ((strcmp(state, "over") == 0) && (strcmp(mode, "auto") == 0)) { |
| 1209 | return net_state_check();//0:error; |
| 1210 | } |
| 1211 | |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | void zte_sntp_cfg_init() |
| 1216 | { |
| 1217 | char server_count[20] = {0}; |
| 1218 | char mode[NV_ITEM_STRING_LEN_10] = {0}; |
| 1219 | char interval[NV_ITEM_STRING_LEN_10] = {0}; |
| 1220 | sc_cfg_set("sntp_process_state", "over"); |
| 1221 | //sc_cfg_set("poweron_sntp","1"); |
| 1222 | sc_cfg_get("sntp_time_set_mode", mode, sizeof(mode)); |
| 1223 | sc_cfg_get("sntp_sync_time", interval, sizeof(interval)); |
| 1224 | g_sntp_sync_interval_time = atoi(interval); |
| 1225 | if(g_sntp_sync_interval_time <= 0) |
| 1226 | g_sntp_sync_interval_time = 1; |
| 1227 | if (strcmp(mode, "manual") == 0) { |
| 1228 | sc_cfg_set("sntp_process_result", "success"); |
| 1229 | } else { |
| 1230 | sc_cfg_set("sntp_process_result", ""); |
| 1231 | } |
| 1232 | |
| 1233 | // sc_cfg_save(); |
| 1234 | pthread_mutex_init(&g_sntp_getaddrinfo_mutex, NULL); |
| 1235 | pthread_mutex_init(&g_sntp_getnameinfo_mutex, NULL); |
| 1236 | zte_sntp_default_server_ip_addr(); |
| 1237 | |
| 1238 | sc_cfg_get(SNTP_SERVER_NUM, server_count, sizeof(server_count)); |
| 1239 | sntp_server_count = atoi(server_count); |
| 1240 | } |
| 1241 | int zte_sntp_action() |
| 1242 | { |
| 1243 | long now_time = 0; |
| 1244 | long last_time = 0; |
| 1245 | char last_time_str[NV_ITEM_STRING_LEN_20] = {0}; |
| 1246 | char result[NV_ITEM_STRING_LEN_20] = {0}; |
| 1247 | |
| 1248 | sc_cfg_get(SNTP_PROCESS_RESULT, result, sizeof(result)); |
| 1249 | time(&now_time); |
| 1250 | sc_cfg_get(SNTP_PROCESS_OVER_TIME, last_time_str, sizeof(last_time_str)); |
| 1251 | last_time = atol(last_time_str); |
| 1252 | if(last_time > LONG_MAX-1) // kw 3 |
| 1253 | { |
| 1254 | last_time = LONG_MAX; |
| 1255 | } |
| 1256 | |
| 1257 | if (0 == zte_sntp_check()) { |
| 1258 | slog(MISC_PRINT, SLOG_DEBUG, "[SNTP][ZYL]Plz check net state or sntp mode\n"); |
| 1259 | return -1; |
| 1260 | } |
| 1261 | |
| 1262 | if (strcmp(result, "success") != 0 || (now_time - last_time) >(g_sntp_sync_interval_time*SNTP_WAIT_TIME_DAY)) { |
| 1263 | //sc_cfg_set("sntp_cmd_from", "NO_WEBUI"); |
| 1264 | g_sntp_cmd_from = 1; |
| 1265 | zte_sntp_start(); |
| 1266 | //ÕýÔÚ½øÐÐʱ¼äͬ²½£¬µÈ´ýÏÂÒ»´ÎÑ»· |
| 1267 | return SNTP_DEFAULT_NEXT_SYNC_INTERVAL_TIME_WHEN_FAILURE; |
| 1268 | } |
| 1269 | |
| 1270 | //ÒѾͬ²½³É¹¦£¬Ë¯ÃßÖ±µ½³¬Ê± |
| 1271 | return SNTP_DEFAULT_NEXT_SYNC_INTERVAL_TIME_WHEN_SUCCESS; |
| 1272 | |
| 1273 | } |
| 1274 | |
| 1275 | int sntp_proc() |
| 1276 | { |
| 1277 | prctl(PR_SET_NAME, "sntp-proc", 0, 0, 0); |
| 1278 | |
| 1279 | while (1) { |
| 1280 | int time = 0; |
| 1281 | /* |
| 1282 | char result[NV_ITEM_STRING_LEN_20] = {0}; |
| 1283 | zte_sntp_handle(&time); |
| 1284 | sc_cfg_get(SNTP_PROCESS_RESULT, result, sizeof(result)); |
| 1285 | */ |
| 1286 | time = zte_sntp_action(); |
| 1287 | if (time == SNTP_DEFAULT_NEXT_SYNC_INTERVAL_TIME_WHEN_FAILURE) |
| 1288 | sleep(time); |
| 1289 | else if (time == SNTP_DEFAULT_NEXT_SYNC_INTERVAL_TIME_WHEN_SUCCESS) |
| 1290 | sleep(SNTP_WAIT_TIME_HOUR); |
| 1291 | else |
| 1292 | sleep(10);//ÍøÂç»·¾³Î´¾ÍÐ÷ |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | int sntp_main(int argc, char * argv[]) |
| 1298 | { |
| 1299 | int interruptSIG = 0; |
| 1300 | int Create_Data_Thread_result = 0; //´´½¨×ÓÏ߳̽á¹û |
| 1301 | int iMsgHandle; |
| 1302 | int iRet = 0; |
| 1303 | MSG_BUF stMsg; |
| 1304 | LONG msgSize; |
| 1305 | pthread_attr_t attr; |
| 1306 | pthread_t Create_Data_Thread_id; |
| 1307 | prctl(PR_SET_NAME, "sntp", 0, 0, 0); |
| 1308 | //¸ù¾ÝNV³õʼ»¯´òÓ¡¼¶±ð£¬²¢×¢²á¶¯Ì¬µ÷Õû´òÓ¡¼¶±ðÐźÅÁ¿ |
| 1309 | loglevel_init(); |
| 1310 | |
| 1311 | INIT_LIST_HEAD(&wait_sync_sntp); |
| 1312 | zte_sntp_cfg_init(); |
| 1313 | |
| 1314 | pthread_attr_init(&attr); |
| 1315 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 1316 | Create_Data_Thread_result = pthread_create(&Create_Data_Thread_id, &attr, sntp_proc, NULL); |
| 1317 | pthread_attr_destroy(&attr); |
| 1318 | if (Create_Data_Thread_result != 0) { |
| 1319 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]Create_Data_Thread_result faild!\n"); |
| 1320 | exit(-1); |
| 1321 | } |
| 1322 | |
| 1323 | iMsgHandle = msgget(MODULE_ID_SNTP, IPC_CREAT | 0600); |
| 1324 | msgSize = sizeof(MSG_BUF) - sizeof(LONG); |
| 1325 | if (iMsgHandle == -1) { |
| 1326 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]Create_msg_result faild!\n"); |
| 1327 | exit(-1); |
| 1328 | } |
| 1329 | |
| 1330 | //FPÐèÒª»ñÈ¡CPµÄʱ¼ä£¬ÀýÈçÊÖ¶¯ÉèÖÃʱ¼ä£¬²»»áÓÐSNTPÀ´ÉèÖÃAPʱ¼ä |
| 1331 | #if (PRODUCT_TYPE == PRODUCT_PHONE) |
| 1332 | iRet = ipc_send_message(MODULE_ID_SNTP, MODULE_ID_RTC_SERVICE, RTC_MSG_GET_TIME, 0, NULL, 0); |
| 1333 | if (iRet != 0) |
| 1334 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]send msg RTC_MSG_GET_TIME failed. \n"); |
| 1335 | #endif |
| 1336 | |
| 1337 | for (; ;) { |
| 1338 | if (0 == interruptSIG) { |
| 1339 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]data begin to rcv msg from queue %d. \n", iMsgHandle); |
| 1340 | } |
| 1341 | memset(&stMsg, '\0', sizeof(stMsg)); |
| 1342 | |
| 1343 | iRet = 0; |
| 1344 | |
| 1345 | //×èÈû½ÓÊÕÏûÏ¢ |
| 1346 | iRet = msgrcv(iMsgHandle, &stMsg, msgSize, 0, 0); |
| 1347 | if (-1 == iRet) { |
| 1348 | if (EINTR == errno) { |
| 1349 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]received msg EINTR\n"); |
| 1350 | interruptSIG = 1; |
| 1351 | continue; |
| 1352 | } else { |
| 1353 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]recv msg from the MODULE_ID_DATA fail errno=%d!", errno); |
| 1354 | return -1; |
| 1355 | } |
| 1356 | } else { |
| 1357 | interruptSIG = 1; |
| 1358 | } |
| 1359 | |
| 1360 | #if 0 |
| 1361 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP]iMsgHandle->%d\n", iMsgHandle); |
| 1362 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP]usSourceModuleID->%d\n", stMsg.usSourceModuleID); |
| 1363 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP]usMsgCmd->%d\n", stMsg.usMsgCmd); |
| 1364 | #endif |
| 1365 | |
| 1366 | if (stMsg.dst_id == MODULE_ID_SNTP) { |
| 1367 | if (stMsg.usMsgCmd == MSG_CMD_SNTP_SET_MODE) { |
| 1368 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]MSG_CMD_SNTP_SET \n"); |
| 1369 | sntp_set_mode(&stMsg); |
| 1370 | } else if (stMsg.usMsgCmd == MSG_CMD_SNTP_SET_TIME) { |
| 1371 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP]MSG_CMD_SNTP_SET_TIME\n"); |
| 1372 | sntp_set_time(&stMsg); |
| 1373 | } else if (stMsg.usMsgCmd == MSG_CMD_SNTP_START) { |
| 1374 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP]zte_sntp_start\n"); |
| 1375 | g_sntp_cmd_from = 0; |
| 1376 | zte_sntp_start(); |
| 1377 | } else if (stMsg.usMsgCmd == MSG_CMD_SNTP_REGISTER) { |
| 1378 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP]get sntp register msg\n"); |
| 1379 | zte_sntp_add_list(&stMsg); |
| 1380 | } else if (stMsg.usMsgCmd == MSG_CMD_SNTP_UNREGISTER) { |
| 1381 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP]get sync unregister msg\n"); |
| 1382 | zte_sntp_remove_list(&stMsg); |
| 1383 | } else { |
| 1384 | slog(MISC_PRINT, SLOG_DEBUG,"[SNTP]usMsgCmd error!\n"); |
| 1385 | } |
| 1386 | } else { |
| 1387 | slog(MISC_PRINT, SLOG_ERR, "[SNTP]src_id error!\n"); |
| 1388 | } |
| 1389 | } |
| 1390 | } |