Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion nimble/controller/include/controller/ble_ll.h
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,6 @@ int ble_ll_rand_init(void);
void ble_ll_rand_sample(uint8_t rnum);
int ble_ll_rand_data_get(uint8_t *buf, uint8_t len);
void ble_ll_rand_prand_get(uint8_t *prand);
int ble_ll_rand_start(void);
uint32_t ble_ll_rand(void);

static inline int
Expand Down
2 changes: 0 additions & 2 deletions nimble/controller/src/ble_ll.c
Original file line number Diff line number Diff line change
Expand Up @@ -1687,8 +1687,6 @@ ble_ll_init(void)

/* Initialize random number generation */
ble_ll_rand_init();
/* Start the random number generator */
ble_ll_rand_start();

rc = stats_init_and_reg(STATS_HDR(ble_ll_stats),
STATS_SIZE_INIT_PARMS(ble_ll_stats, STATS_SIZE_32),
Expand Down
42 changes: 15 additions & 27 deletions nimble/controller/src/ble_ll_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ struct ble_ll_rnum_data
struct ble_ll_rnum_data g_ble_ll_rnum_data;
uint8_t g_ble_ll_rnum_buf[MYNEWT_VAL(BLE_LL_RNG_BUFSIZE)];

static unsigned short xsubi[3];

#define IS_RNUM_BUF_END(x) \
(x == &g_ble_ll_rnum_buf[MYNEWT_VAL(BLE_LL_RNG_BUFSIZE) - 1])

Expand Down Expand Up @@ -127,14 +129,6 @@ ble_ll_rand_data_get(uint8_t *buf, uint8_t len)
uint32_t
ble_ll_rand(void)
{
static unsigned short xsubi[3];
static bool init = true;

if (init) {
init = false;
ble_ll_rand_data_get((uint8_t *)xsubi, sizeof(xsubi));
}

return (uint32_t) jrand48(xsubi);
}

Expand Down Expand Up @@ -164,25 +158,6 @@ ble_ll_rand_prand_get(uint8_t *prand)
prand[2] |= 0x40;
}

/**
* Start the generation of random numbers
*
* @return int
*/
int
ble_ll_rand_start(void)
{
#if MYNEWT_VAL(TRNG)
/* Nothing to do - this is handled by driver */
#else
/* Start the generation of numbers if we are not full */
if (g_ble_ll_rnum_data.rnd_size < MYNEWT_VAL(BLE_LL_RNG_BUFSIZE)) {
ble_hw_rng_start();
}
#endif
return 0;
}

/**
* Initialize LL random number generation. Should be called only once on
* initialization.
Expand All @@ -198,6 +173,19 @@ ble_ll_rand_init(void)
g_ble_ll_rnum_data.rnd_in = g_ble_ll_rnum_buf;
g_ble_ll_rnum_data.rnd_out = g_ble_ll_rnum_buf;
ble_hw_rng_init(ble_ll_rand_sample, 1);

/* Start the generation of numbers if we are not full */
if (g_ble_ll_rnum_data.rnd_size < MYNEWT_VAL(BLE_LL_RNG_BUFSIZE)) {
ble_hw_rng_start();
}
#endif

/* Seed the pseudo random number generator (jrand48) */
ble_ll_rand_data_get((uint8_t *)xsubi, sizeof(xsubi));
/* We need to trigger jrand48 once discarding its output, as the initial
* call to jrand48 causes dynamic memory allocation by the libc. We do not
* want this to happen in interrupt context, which can happen if we wait for
* the first actual call to ble_ll_rand(). */
ble_ll_rand();
return 0;
}