From 187660858fcfda1955fe2c8dbc71e1ba6378b077 Mon Sep 17 00:00:00 2001 From: Parminder Singh <61920513+parmi93@users.noreply.github.com> Date: Thu, 20 Jul 2023 17:36:01 +0000 Subject: [PATCH] lwm2m_seed() is used to get seed The lwm2m_seed() function is used to obtain a seed for random number generation. The implementation of the lwm2m_seed() function is up to the user. --- core/liblwm2m.c | 4 ++-- examples/shared/platform.c | 15 ++++++++------- include/liblwm2m.h | 6 ++---- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/core/liblwm2m.c b/core/liblwm2m.c index 4e705be97..dc924bd8a 100644 --- a/core/liblwm2m.c +++ b/core/liblwm2m.c @@ -68,8 +68,8 @@ lwm2m_context_t * lwm2m_init(void * userData) { memset(contextP, 0, sizeof(lwm2m_context_t)); contextP->userData = userData; - lwm2m_srand((int)lwm2m_gettime()); - contextP->nextMID = lwm2m_rand(); + srand((int)lwm2m_seed()); + contextP->nextMID = rand(); } return contextP; diff --git a/examples/shared/platform.c b/examples/shared/platform.c index eb5a7bdaf..b194499b3 100644 --- a/examples/shared/platform.c +++ b/examples/shared/platform.c @@ -67,14 +67,15 @@ time_t lwm2m_gettime(void) return time(NULL); } -void lwm2m_srand(unsigned int seed) +int lwm2m_seed(void) { - srand(seed); -} - -int lwm2m_rand() -{ - return rand(); + /* + * Return a seed for random number generation, the seed must be a + * different number at every boot and unpredictable, time(NULL) may not be + * a reliable source as a seed. + * See: https://github.com/eclipse/wakaama/pull/711 + */ + return time(NULL); } void lwm2m_printf(const char * format, ...) diff --git a/include/liblwm2m.h b/include/liblwm2m.h index 48c48cf45..72ad3234a 100644 --- a/include/liblwm2m.h +++ b/include/liblwm2m.h @@ -118,10 +118,8 @@ int lwm2m_strcasecmp(const char * str1, const char * str2); // In case of error, this must return a negative value. // Per POSIX specifications, time_t is a signed integer. time_t lwm2m_gettime(void); -// Initialize random number generator -void lwm2m_srand(unsigned int seed); -// Get a random number -int lwm2m_rand(void); +// Get a seed (which must not repeat when the device reboots) for generating a random number +int lwm2m_seed(void); #ifdef LWM2M_WITH_LOGS // Same usage as C89 printf()