26
26
#include "nimble/nimble_opt.h"
27
27
#include "controller/ble_hw.h"
28
28
#include "controller/ble_ll.h"
29
+ #if MYNEWT_VAL (TRNG )
30
+ #include "trng/trng.h"
31
+ #endif
29
32
33
+ #if MYNEWT_VAL (TRNG )
34
+ static struct trng_dev * g_trng ;
35
+ #else
30
36
/* This is a simple circular buffer for holding N samples of random data */
31
37
struct ble_ll_rnum_data
32
38
{
@@ -61,11 +67,21 @@ ble_ll_rand_sample(uint8_t rnum)
61
67
}
62
68
OS_EXIT_CRITICAL (sr );
63
69
}
70
+ #endif
64
71
65
72
/* Get 'len' bytes of random data */
66
73
int
67
74
ble_ll_rand_data_get (uint8_t * buf , uint8_t len )
68
75
{
76
+ #if MYNEWT_VAL (TRNG )
77
+ size_t num ;
78
+
79
+ while (len ) {
80
+ num = trng_read (g_trng , buf , len );
81
+ buf += num ;
82
+ len -= num ;
83
+ }
84
+ #else
69
85
uint8_t rnums ;
70
86
os_sr_t sr ;
71
87
@@ -100,7 +116,7 @@ ble_ll_rand_data_get(uint8_t *buf, uint8_t len)
100
116
}
101
117
}
102
118
}
103
-
119
+ #endif
104
120
return BLE_ERR_SUCCESS ;
105
121
}
106
122
@@ -138,10 +154,14 @@ ble_ll_rand_prand_get(uint8_t *prand)
138
154
int
139
155
ble_ll_rand_start (void )
140
156
{
157
+ #if MYNEWT_VAL (TRNG )
158
+ /* Nothing to do - this is handled by driver */
159
+ #else
141
160
/* Start the generation of numbers if we are not full */
142
161
if (g_ble_ll_rnum_data .rnd_size < MYNEWT_VAL (BLE_LL_RNG_BUFSIZE )) {
143
162
ble_hw_rng_start ();
144
163
}
164
+ #endif
145
165
return 0 ;
146
166
}
147
167
@@ -154,8 +174,12 @@ ble_ll_rand_start(void)
154
174
int
155
175
ble_ll_rand_init (void )
156
176
{
177
+ #if MYNEWT_VAL (TRNG )
178
+ g_trng = (struct trng_dev * ) os_dev_open ("trng" , OS_TIMEOUT_NEVER , NULL );
179
+ #else
157
180
g_ble_ll_rnum_data .rnd_in = g_ble_ll_rnum_buf ;
158
181
g_ble_ll_rnum_data .rnd_out = g_ble_ll_rnum_buf ;
159
182
ble_hw_rng_init (ble_ll_rand_sample , 1 );
183
+ #endif
160
184
return 0 ;
161
185
}
0 commit comments