Skip to content

Commit 6bec265

Browse files
committed
nimble/ll: Add CS_SYNC transmission/reception
- CS SYNC sequence generation - CS Access Address matching
1 parent 14cc98e commit 6bec265

File tree

2 files changed

+163
-8
lines changed

2 files changed

+163
-8
lines changed

nimble/controller/src/ble_ll_cs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "ble_ll_cs_priv.h"
3232

3333
struct ble_ll_cs_supp_cap g_ble_ll_cs_local_cap;
34-
static struct ble_ll_cs_sm g_ble_ll_cs_sm[MYNEWT_VAL(BLE_MAX_CONNECTIONS)];
34+
struct ble_ll_cs_sm g_ble_ll_cs_sm[MYNEWT_VAL(BLE_MAX_CONNECTIONS)];
3535
static const uint8_t t_ip1[] = {10, 20, 30, 40, 50, 60, 80, 145};
3636
static const uint8_t t_ip2[] = {10, 20, 30, 40, 50, 60, 80, 145};
3737
static const uint8_t t_fcs[] = {15, 20, 30, 40, 50, 60, 80, 100, 120, 150};

nimble/controller/src/ble_ll_cs_sync.c

Lines changed: 162 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,67 @@
2121
#if MYNEWT_VAL(BLE_LL_CHANNEL_SOUNDING)
2222
#include <stdint.h>
2323
#include "controller/ble_ll.h"
24+
#include "controller/ble_ll_conn.h"
2425
#include "controller/ble_ll_sched.h"
2526
#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"
2631
#include "ble_ll_cs_priv.h"
2732

33+
extern struct ble_ll_cs_sm g_ble_ll_cs_sm[MYNEWT_VAL(BLE_MAX_CONNECTIONS)];
2834
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+
}
2985

3086
/**
3187
* Called when a CS pkt has been transmitted.
@@ -43,22 +99,114 @@ ble_ll_cs_sync_tx_end_cb(void *arg)
4399
ble_ll_cs_proc_schedule_next_tx_or_rx(cssm);
44100
}
45101

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+
46115
int
47116
ble_ll_cs_sync_tx_sched_cb(struct ble_ll_sched_item *sch)
48117
{
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);
50156

51157
return BLE_LL_SCHED_STATE_RUNNING;
52158
}
53159

54160
int
55161
ble_ll_cs_sync_rx_sched_cb(struct ble_ll_sched_item *sch)
56162
{
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);
58189

59190
return BLE_LL_SCHED_STATE_RUNNING;
60191
}
61192

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+
62210
/**
63211
* Called when a receive PDU has started.
64212
* 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)
72220
int
73221
ble_ll_cs_sync_rx_isr_start(struct ble_mbuf_hdr *rxhdr, uint32_t aa)
74222
{
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+
}
76231

77232
return 0;
78233
}
@@ -96,16 +251,16 @@ ble_ll_cs_sync_rx_isr_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr)
96251
struct os_mbuf *rxpdu;
97252
struct ble_ll_cs_sm *cssm = g_ble_ll_cs_sm_current;
98253

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-
103254
/* Packet type was verified in isr_start */
104255

105256
rxpdu = ble_ll_rxpdu_alloc(rxbuf[1] + BLE_LL_PDU_HDR_LEN);
106257
if (rxpdu) {
107258
ble_phy_rxpdu_copy(rxbuf, rxpdu);
108259

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+
109264
/* Send the packet to Link Layer context */
110265
ble_ll_rx_pdu_in(rxpdu);
111266
}

0 commit comments

Comments
 (0)