Skip to content

Commit 70fa832

Browse files
Add soft device rng portion to MicroBitDevice::seedRandom().
Otherwise, calling this function when BLE is running results in 0xBBC5EED always being used as the seed.
1 parent bd446c4 commit 70fa832

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

source/MicroBitDevice.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,21 @@ void MicroBitDevice::seedRandom()
6868
{
6969
uint32_t r = 0xBBC5EED;
7070

71-
if(!ble_running())
71+
if (ble_running())
72+
{
73+
#ifdef SOFTDEVICE_PRESENT
74+
// If Bluetooth is enabled, we need to go through the Nordic software to safely do this.
75+
uint8_t available_bytes = 0;
76+
while (available_bytes < 4)
77+
sd_rand_application_bytes_available_get(&available_bytes);
78+
79+
uint32_t random_value = 0;
80+
uint32_t result = sd_rand_application_vector_get((uint8_t*)&random_value, sizeof(random_value));
81+
if (result == NRF_SUCCESS)
82+
r = random_value;
83+
#endif
84+
}
85+
else
7286
{
7387
// Start the Random number generator. No need to leave it running... I hope. :-)
7488
NRF_RNG->TASKS_START = 1;

0 commit comments

Comments
 (0)