Skip to content

Commit

Permalink
lwm2m_seed() is used to get 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 20, 2023
1 parent e5dbff6 commit 1876608
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
4 changes: 2 additions & 2 deletions core/liblwm2m.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 8 additions & 7 deletions examples/shared/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...)
Expand Down
6 changes: 2 additions & 4 deletions include/liblwm2m.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1876608

Please sign in to comment.