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 committed Jul 26, 2023
1 parent 5146594 commit c571f40
Show file tree
Hide file tree
Showing 3 changed files with 14 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((int)lwm2m_seed());
contextP->nextMID = rand();
}

Expand Down
11 changes: 11 additions & 0 deletions examples/shared/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ 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 c571f40

Please sign in to comment.