Skip to content

Commit

Permalink
Use lwm2m_seed() to get the seed
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
parmi93 authored and mlasch committed Aug 16, 2023
1 parent bf81d08 commit 9433484
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/liblwm2m.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
10 changes: 10 additions & 0 deletions examples/shared/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions include/liblwm2m.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9433484

Please sign in to comment.