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