From 45dd9c22d253e0ce0c53c27159e0d36a986cb66e Mon Sep 17 00:00:00 2001 From: suvendhu Date: Mon, 2 May 2022 12:01:17 +0530 Subject: [PATCH] T#7935 All devices send inform at same time with unknown periodic inform time --- common.c | 25 +++++++++++++++++++++---- config.c | 23 +++++++---------------- event.c | 9 ++++++++- inc/common.h | 2 ++ 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/common.c b/common.c index d47331c..5d8307b 100755 --- a/common.c +++ b/common.c @@ -635,16 +635,17 @@ char *string_to_hex(const unsigned char *str, size_t size) { size_t i; - if (size == 0) - return ""; - - char* hex = (char*) calloc(size * 2 + 1, sizeof(char)); + char *hex = (char*) calloc(size * 2 + 1, sizeof(char)); if (!hex) { CWMP_LOG(ERROR, "Unable to allocate memory for hex string\n"); return NULL; } + if (size == 0) { + return hex; + } + for (i = 0; i < size; i++) snprintf(hex + (i * 2), 3, "%02X", str[i]); @@ -739,3 +740,19 @@ bool is_obj_excluded(const char *object_name) } return false; } + +time_t convert_datetime_to_timestamp(char *value) +{ + struct tm tm = { 0 }; + int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0; + + sscanf(value, "%4d-%2d-%2dT%2d:%2d:%2d", &year, &month, &day, &hour, &min, &sec); + tm.tm_year = year - 1900; /* years since 1900 */ + tm.tm_mon = month - 1; + tm.tm_mday = day; + tm.tm_hour = hour; + tm.tm_min = min; + tm.tm_sec = sec; + + return mktime(&tm); +} diff --git a/config.c b/config.c index 3f10429..a84611b 100755 --- a/config.c +++ b/config.c @@ -15,6 +15,7 @@ #include "config.h" #include "log.h" #include "reboot.h" +#include "ssl_utils.h" #include "datamodel_interface.h" pthread_mutex_t mutex_config_load = PTHREAD_MUTEX_INITIALIZER; @@ -27,22 +28,6 @@ static int check_global_config(struct config *conf) return CWMP_OK; } -static time_t convert_datetime_to_timestamp(char *value) -{ - struct tm tm = { 0 }; - int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0; - - sscanf(value, "%4d-%2d-%2dT%2d:%2d:%2d", &year, &month, &day, &hour, &min, &sec); - tm.tm_year = year - 1900; /* years since 1900 */ - tm.tm_mon = month - 1; - tm.tm_mday = day; - tm.tm_hour = hour; - tm.tm_min = min; - tm.tm_sec = sec; - - return mktime(&tm); -} - int get_global_config(struct config *conf) { int error, error2, error3; @@ -402,6 +387,12 @@ int get_global_config(struct config *conf) return error; } + char *entropy = generate_random_string(sizeof(unsigned int)); + if (entropy != NULL) { + conf->periodic_entropy = (unsigned int)strtoul(entropy, NULL, 16); + free(entropy); + } + if ((error = uci_get_value(UCI_PERIODIC_INFORM_INTERVAL_PATH, &value)) == CWMP_OK) { int a = 0; diff --git a/event.c b/event.c index 5990f3a..4a50659 100644 --- a/event.c +++ b/event.c @@ -330,17 +330,24 @@ void *thread_event_periodic(void *v) struct timespec periodic_timeout = { 0, 0 }; time_t current_time; long int delta_time; + time_t unknown_time; periodic_interval = cwmp->conf.period; periodic_enable = cwmp->conf.periodic_enable; periodic_time = cwmp->conf.time; + unknown_time = convert_datetime_to_timestamp("0001-01-01T00:00:00Z"); for (;;) { pthread_mutex_lock(&(cwmp->mutex_periodic)); if (cwmp->conf.periodic_enable) { current_time = time(NULL); if (periodic_time != 0) { - delta_time = (current_time - periodic_time) % periodic_interval; + if (periodic_time == unknown_time) { + delta_time = (current_time + cwmp->conf.periodic_entropy) % periodic_interval; + } else { + delta_time = (current_time - periodic_time) % periodic_interval; + } + if (delta_time >= 0) periodic_timeout.tv_sec = current_time + periodic_interval - delta_time; else diff --git a/inc/common.h b/inc/common.h index 57badc5..a47b9aa 100644 --- a/inc/common.h +++ b/inc/common.h @@ -95,6 +95,7 @@ typedef struct config { int delay_reboot; time_t schedule_reboot; time_t time; + unsigned int periodic_entropy; bool periodic_enable; bool periodic_notify_enable; bool insecure_enable; @@ -529,5 +530,6 @@ int copy_file(char *source_file, char *target_file); int get_connection_interface(); char *get_time(time_t t_time); bool is_obj_excluded(const char *object_name); +time_t convert_datetime_to_timestamp(char *value); #endif