From 9433484c244bd1fef70fbdae3ddb60267e69f37d Mon Sep 17 00:00:00 2001 From: Parminder Singh <61920513+parmi93@users.noreply.github.com> Date: Fri, 16 Jun 2023 10:19:50 +0000 Subject: [PATCH] Use lwm2m_seed() to get the 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 | 2 +- examples/shared/platform.c | 10 ++++++++++ include/liblwm2m.h | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/core/liblwm2m.c b/core/liblwm2m.c index 8a2d69ec1..205e5428b 100644 --- a/core/liblwm2m.c +++ b/core/liblwm2m.c @@ -68,7 +68,7 @@ lwm2m_context_t * lwm2m_init(void * userData) { memset(contextP, 0, sizeof(lwm2m_context_t)); contextP->userData = userData; - srand((int)lwm2m_gettime()); + srand(lwm2m_seed()); contextP->nextMID = rand(); } diff --git a/examples/shared/platform.c b/examples/shared/platform.c index ab8090bb1..608869157 100644 --- a/examples/shared/platform.c +++ b/examples/shared/platform.c @@ -67,6 +67,16 @@ time_t lwm2m_gettime(void) return time(NULL); } +int lwm2m_seed(void) { + /* + * 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, ...) { va_list ap; diff --git a/include/liblwm2m.h b/include/liblwm2m.h index 85da067d9..84fc5790a 100644 --- a/include/liblwm2m.h +++ b/include/liblwm2m.h @@ -118,6 +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); +// 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()