21
21
#if MYNEWT_VAL (BLE_LL_CHANNEL_SOUNDING )
22
22
#include <stdint.h>
23
23
#include "controller/ble_ll.h"
24
+ #include "controller/ble_ll_conn.h"
24
25
#include "controller/ble_ll_sched.h"
25
26
#include "controller/ble_ll_tmr.h"
27
+ #include "controller/ble_hw.h"
28
+ #include "controller/ble_ll_hci.h"
29
+ #include "mbedtls/aes.h"
30
+ #include "ble_ll_priv.h"
26
31
#include "ble_ll_cs_priv.h"
27
32
33
+ extern struct ble_ll_cs_sm g_ble_ll_cs_sm [MYNEWT_VAL (BLE_MAX_CONNECTIONS )];
28
34
extern struct ble_ll_cs_sm * g_ble_ll_cs_sm_current ;
35
+ extern int8_t g_ble_ll_tx_power ;
36
+
37
+ static uint8_t
38
+ ble_ll_cs_sync_make (struct ble_ll_cs_sm * cssm , uint8_t * buf )
39
+ {
40
+ int i ;
41
+ int rc ;
42
+ uint32_t aa ;
43
+ struct ble_ll_cs_config * conf = cssm -> active_config ;
44
+ uint8_t sequence_len ;
45
+ uint8_t bits ;
46
+
47
+ if (conf -> rtt_type != BLE_LL_CS_RTT_AA_ONLY ) {
48
+ rc = ble_ll_cs_drbg_generate_sync_sequence (
49
+ & cssm -> drbg_ctx , cssm -> steps_in_procedure_count , conf -> rtt_type ,
50
+ buf , & sequence_len );
51
+
52
+ if (rc ) {
53
+ return 0 ;
54
+ }
55
+ }
56
+
57
+ /* Shift by 4 bits to make space for trailer bits */
58
+ if (sequence_len > 0 ) {
59
+ assert (sequence_len >= BLE_PHY_MAX_PDU_LEN );
60
+ buf [sequence_len ] = 0 ;
61
+ bits = 0 ;
62
+ for (i = sequence_len - 1 ; i >= 0 ; -- i ) {
63
+ bits = buf [i ] >> 4 ;
64
+ buf [i ] = (buf [i ] << 4 ) & 0xF0 ;
65
+ buf [i + 1 ] = (buf [i + 1 ] & 0xF0 ) | bits ;
66
+ }
67
+ }
68
+
69
+ if (conf -> role == BLE_LL_CS_ROLE_INITIATOR ) {
70
+ aa = cssm -> initiator_aa ;
71
+ } else {
72
+ aa = cssm -> reflector_aa ;
73
+ }
74
+
75
+ /* Set the trailer bits */
76
+ if (aa & 0x80000000 ) {
77
+ buf [0 ] |= 0x0A ;
78
+ } else {
79
+ buf [0 ] |= 0x05 ;
80
+ }
81
+ ++ sequence_len ;
82
+
83
+ return sequence_len ;
84
+ }
29
85
30
86
/**
31
87
* Called when a CS pkt has been transmitted.
@@ -43,22 +99,114 @@ ble_ll_cs_sync_tx_end_cb(void *arg)
43
99
ble_ll_cs_proc_schedule_next_tx_or_rx (cssm );
44
100
}
45
101
102
+ static uint8_t
103
+ ble_ll_cs_sync_tx_make (uint8_t * dptr , void * arg , uint8_t * hdr_byte )
104
+ {
105
+ uint8_t pdu_len ;
106
+ struct ble_ll_cs_sm * cssm = arg ;
107
+
108
+ /* TODO: Unused fields in CS Sync packet */
109
+ pdu_len = ble_ll_cs_sync_make (cssm , dptr );
110
+ * hdr_byte = 0 ;
111
+
112
+ return pdu_len ;
113
+ }
114
+
46
115
int
47
116
ble_ll_cs_sync_tx_sched_cb (struct ble_ll_sched_item * sch )
48
117
{
49
- /* TODO: Start TX of CS SYNC */
118
+ int rc ;
119
+ struct ble_ll_cs_sm * cssm = sch -> cb_arg ;
120
+
121
+ BLE_LL_ASSERT (ble_ll_state_get () == BLE_LL_STATE_STANDBY );
122
+
123
+ /* TODO: Add to PHY a support CS mode.
124
+ * Adjustments for nrf:
125
+ * - Turn off CRC
126
+ * - set 4 bits in S1
127
+ *
128
+ * ble_ll_phy_to_cs_sync_mode(cssm->active_config->cs_sync_phy, 0);
129
+ */
130
+
131
+ ble_ll_tx_power_set (g_ble_ll_tx_power );
132
+
133
+ rc = ble_phy_tx_set_start_time (sch -> start_time + g_ble_ll_sched_offset_ticks ,
134
+ sch -> remainder );
135
+ if (rc ) {
136
+ return BLE_LL_SCHED_STATE_DONE ;
137
+ }
138
+
139
+ ble_phy_set_txend_cb (ble_ll_cs_sync_tx_end_cb , cssm );
140
+
141
+ /* TODO: Add to PHY a support for 72 RF channels for CS exchanges.
142
+ * ble_phy_cs_setchan(cssm->channel, cssm->tx_aa, 0);
143
+ */
144
+
145
+ /* TODO: Add to PHY a support for CS_SYNC transmission.
146
+ * rc = ble_phy_tx_cs_sync(ble_ll_cs_sync_tx_make, cssm,
147
+ * BLE_PHY_TRANSITION_TX_RX);
148
+ */
149
+ (void ) ble_ll_cs_sync_tx_make ;
150
+ if (rc ) {
151
+ ble_phy_disable ();
152
+ return BLE_LL_SCHED_STATE_DONE ;
153
+ }
154
+
155
+ ble_ll_state_set (BLE_LL_STATE_CS );
50
156
51
157
return BLE_LL_SCHED_STATE_RUNNING ;
52
158
}
53
159
54
160
int
55
161
ble_ll_cs_sync_rx_sched_cb (struct ble_ll_sched_item * sch )
56
162
{
57
- /* TODO: Start RX of CS SYNC */
163
+ int rc ;
164
+ uint32_t wfr_usecs ;
165
+ struct ble_ll_cs_sm * cssm = sch -> cb_arg ;
166
+
167
+ BLE_LL_ASSERT (ble_ll_state_get () == BLE_LL_STATE_STANDBY );
168
+
169
+ /* TODO: Add to PHY a support CS mode.
170
+ * Adjustments for nrf:
171
+ * - Turn off CRC
172
+ * - set 4 bits in S1
173
+ *
174
+ * ble_ll_phy_to_cs_sync_mode(cssm->active_config->cs_sync_phy, 0);
175
+ */
176
+
177
+ /* TODO: Add to PHY a support for 72 RF channels for CS exchanges.
178
+ * ble_phy_cs_setchan(cssm->channel, cssm->tx_aa, 0);
179
+ */
180
+
181
+ rc = ble_phy_rx_set_start_time (sch -> start_time + g_ble_ll_sched_offset_ticks ,
182
+ sch -> remainder );
183
+ if (rc ) {
184
+ return BLE_LL_SCHED_STATE_DONE ;
185
+ }
186
+
187
+ wfr_usecs = cssm -> duration_usecs ;
188
+ ble_phy_wfr_enable (BLE_PHY_WFR_ENABLE_RX , 0 , wfr_usecs );
58
189
59
190
return BLE_LL_SCHED_STATE_RUNNING ;
60
191
}
61
192
193
+ static struct ble_ll_cs_sm *
194
+ ble_ll_cs_sync_find_sm_match_aa (uint32_t aa )
195
+ {
196
+ int i ;
197
+ struct ble_ll_cs_sm * cssm ;
198
+
199
+ for (i = 0 ; i < ARRAY_SIZE (g_ble_ll_cs_sm ); ++ i ) {
200
+ cssm = & g_ble_ll_cs_sm [i ];
201
+
202
+ if (cssm -> rx_aa == aa ) {
203
+ return cssm ;
204
+ }
205
+ }
206
+
207
+ return NULL ;
208
+ }
209
+
62
210
/**
63
211
* Called when a receive PDU has started.
64
212
* Check if the frame is the next expected CS_SYNC packet.
@@ -72,7 +220,14 @@ ble_ll_cs_sync_rx_sched_cb(struct ble_ll_sched_item *sch)
72
220
int
73
221
ble_ll_cs_sync_rx_isr_start (struct ble_mbuf_hdr * rxhdr , uint32_t aa )
74
222
{
75
- /* TODO: Verify CS Access Address */
223
+ struct ble_ll_cs_sm * cssm ;
224
+
225
+ cssm = ble_ll_cs_sync_find_sm_match_aa (aa );
226
+
227
+ if (cssm == NULL ) {
228
+ /* This is not the expected packet. Skip the frame. */
229
+ return -1 ;
230
+ }
76
231
77
232
return 0 ;
78
233
}
@@ -96,16 +251,16 @@ ble_ll_cs_sync_rx_isr_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr)
96
251
struct os_mbuf * rxpdu ;
97
252
struct ble_ll_cs_sm * cssm = g_ble_ll_cs_sm_current ;
98
253
99
- assert (cssm != NULL );
100
- ble_ll_cs_proc_set_now_as_anchor_point (cssm );
101
- ble_ll_cs_proc_schedule_next_tx_or_rx (cssm );
102
-
103
254
/* Packet type was verified in isr_start */
104
255
105
256
rxpdu = ble_ll_rxpdu_alloc (rxbuf [1 ] + BLE_LL_PDU_HDR_LEN );
106
257
if (rxpdu ) {
107
258
ble_phy_rxpdu_copy (rxbuf , rxpdu );
108
259
260
+ assert (cssm != NULL );
261
+ ble_ll_cs_proc_set_now_as_anchor_point (cssm );
262
+ ble_ll_cs_proc_schedule_next_tx_or_rx (cssm );
263
+
109
264
/* Send the packet to Link Layer context */
110
265
ble_ll_rx_pdu_in (rxpdu );
111
266
}
0 commit comments