Skip to content

Commit ba6be70

Browse files
cvinayakcarlescufi
authored andcommitted
Bluetooth: Controller: Added comments for CSA #2 implementation
Added Bluetooth Specification references to the implementation of Channel Selection algorithm #2 in the Controller. Signed-off-by: Vinayak Kariappa Chettimada <[email protected]>
1 parent 7b12723 commit ba6be70

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

subsys/bluetooth/controller/ll_sw/lll_chan.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ static uint8_t chan_d(uint8_t n);
2929
#endif /* CONFIG_BT_CTLR_CHAN_SEL_2 */
3030

3131
#if defined(CONFIG_BT_CONN)
32+
/* Refer to Bluetooth Specification v5.2 Vol 6, Part B, Section 4.5.8.2
33+
* Channel Selection algorithm #1
34+
*/
3235
uint8_t lll_chan_sel_1(uint8_t *chan_use, uint8_t hop, uint16_t latency, uint8_t *chan_map,
3336
uint8_t chan_count)
3437
{
@@ -52,6 +55,9 @@ uint8_t lll_chan_sel_1(uint8_t *chan_use, uint8_t hop, uint16_t latency, uint8_t
5255
#endif /* CONFIG_BT_CONN */
5356

5457
#if defined(CONFIG_BT_CTLR_CHAN_SEL_2)
58+
/* Refer to Bluetooth Specification v5.2 Vol 6, Part B, Section 4.5.8.3.2
59+
* Inputs and basic components
60+
*/
5561
uint16_t lll_chan_id(uint8_t *access_addr)
5662
{
5763
uint16_t aa_ls = ((uint16_t)access_addr[1] << 8) | access_addr[0];
@@ -60,6 +66,10 @@ uint16_t lll_chan_id(uint8_t *access_addr)
6066
return aa_ms ^ aa_ls;
6167
}
6268

69+
/* Refer to Bluetooth Specification v5.2 Vol 6, Part B, Section 4.5.8.3
70+
* Channel Selection algorithm #2, and Section 4.5.8.3.1 Overview
71+
* Below interface is used for ACL connections.
72+
*/
6373
uint8_t lll_chan_sel_2(uint16_t counter, uint16_t chan_id, uint8_t *chan_map,
6474
uint8_t chan_count)
6575
{
@@ -83,6 +93,11 @@ uint8_t lll_chan_sel_2(uint16_t counter, uint16_t chan_id, uint8_t *chan_map,
8393
}
8494

8595
#if defined(CONFIG_BT_CTLR_ADV_ISO) || defined(CONFIG_BT_CTLR_SYNC_ISO)
96+
/* Refer to Bluetooth Specification v5.2 Vol 6, Part B, Section 4.5.8.3
97+
* Channel Selection algorithm #2, and Section 4.5.8.3.1 Overview
98+
*
99+
* Below interface is used for ISO first subevent.
100+
*/
86101
uint8_t lll_chan_iso_event(uint16_t counter, uint16_t chan_id,
87102
uint8_t *chan_map, uint8_t chan_count,
88103
uint16_t *prn_s, uint16_t *remap_idx)
@@ -105,6 +120,11 @@ uint8_t lll_chan_iso_event(uint16_t counter, uint16_t chan_id,
105120
return chan_idx;
106121
}
107122

123+
/* Refer to Bluetooth Specification v5.2 Vol 6, Part B, Section 4.5.8.3
124+
* Channel Selection algorithm #2, and Section 4.5.8.3.1 Overview
125+
*
126+
* Below interface is used for ISO next subevent.
127+
*/
108128
uint8_t lll_chan_iso_subevent(uint16_t chan_id, uint8_t *chan_map,
109129
uint8_t chan_count, uint16_t *prn_subevent_lu,
110130
uint16_t *remap_idx)
@@ -117,6 +137,10 @@ uint8_t lll_chan_iso_subevent(uint16_t chan_id, uint8_t *chan_map,
117137
prn_subevent_se = chan_prn_subevent_se(chan_id, prn_subevent_lu);
118138

119139
d = chan_d(chan_count);
140+
141+
/* Sub-expression to get natural number (N - 2d + 1) to be used in the
142+
* calculation of d.
143+
*/
120144
if ((chan_count + 1) > (d << 1)) {
121145
x = (chan_count + 1) - (d << 1);
122146
} else {
@@ -138,6 +162,10 @@ uint8_t lll_chan_iso_subevent(uint16_t chan_id, uint8_t *chan_map,
138162
#endif /* CONFIG_BT_CTLR_ADV_ISO || CONFIG_BT_CTLR_SYNC_ISO */
139163
#endif /* CONFIG_BT_CTLR_CHAN_SEL_2 */
140164

165+
/* Refer to Bluetooth Specification v5.2 Vol 6, Part B, Section 4.5.8.3
166+
* Channel Selection algorithm #2, and Section 4.5.8.3.4 Event mapping to used
167+
* channel index
168+
*/
141169
static uint8_t chan_sel_remap(uint8_t *chan_map, uint8_t chan_index)
142170
{
143171
uint8_t chan_next;
@@ -176,6 +204,9 @@ static uint8_t chan_sel_remap(uint8_t *chan_map, uint8_t chan_index)
176204
/* Attribution:
177205
* http://graphics.stanford.edu/%7Eseander/bithacks.html#ReverseByteWith32Bits
178206
*/
207+
/* Refer to Bluetooth Specification v5.2 Vol 6, Part B, Section 4.5.8.3.2
208+
* Inputs and basic components, for below operations
209+
*/
179210
static uint8_t chan_rev_8(uint8_t b)
180211
{
181212
b = (((uint32_t)b * 0x0802LU & 0x22110LU) |
@@ -220,6 +251,13 @@ static uint16_t chan_prn_e(uint16_t counter, uint16_t chan_id)
220251
}
221252

222253
#if defined(CONFIG_BT_CTLR_ADV_ISO) || defined(CONFIG_BT_CTLR_SYNC_ISO)
254+
/* Refer to Bluetooth Specification v5.2 Vol 6, Part B, Section 4.5.8.3
255+
* Channel Selection algorithm #2, and Section 4.5.8.3.4 Event mapping to used
256+
* channel index
257+
*
258+
* Below function is used in the context of next subevent, return remapping
259+
* index.
260+
*/
223261
static uint8_t chan_sel_remap_index(uint8_t *chan_map, uint8_t chan_index)
224262
{
225263
uint8_t octet_count;
@@ -251,6 +289,10 @@ static uint8_t chan_sel_remap_index(uint8_t *chan_map, uint8_t chan_index)
251289
return 0;
252290
}
253291

292+
/* Refer to Bluetooth Specification v5.2 Vol 6, Part B, Section 4.5.8.3
293+
* Channel Selection algorithm #2, and Section 4.5.8.3.5 Subevent pseudo-random
294+
* number generation
295+
*/
254296
static uint16_t chan_prn_subevent_se(uint16_t chan_id,
255297
uint16_t *prn_subevent_lu)
256298
{
@@ -268,27 +310,41 @@ static uint16_t chan_prn_subevent_se(uint16_t chan_id,
268310
return prn_subevent_se;
269311
}
270312

313+
/* Refer to Bluetooth Specification v5.2 Vol 6, Part B, Section 4.5.8.3
314+
* Channel Selection algorithm #2, and Section 4.5.8.3.6 Subevent mapping to
315+
* used channel index
316+
*/
271317
static uint8_t chan_d(uint8_t n)
272318
{
273319
uint8_t x, y;
274320

321+
/* Sub-expression to get natural number (N - 5) to be used in the
322+
* calculation of d.
323+
*/
275324
if (n > 5) {
276325
x = n - 5;
277326
} else {
278327
x = 0;
279328
}
280329

330+
/* Sub-expression to get natural number ((N - 10) / 2) to be used in the
331+
* calculation of d.
332+
*/
281333
if (n > 10) {
282334
y = (n - 10) >> 1;
283335
} else {
284336
y = 0;
285337
}
286338

339+
/* Calculate d using the above sub expressions */
287340
return MAX(1, MAX(MIN(3, x), MIN(11, y)));
288341
}
289342
#endif /* CONFIG_BT_CTLR_ADV_ISO || CONFIG_BT_CTLR_SYNC_ISO */
290343

291344
#if defined(CONFIG_BT_CTLR_TEST)
345+
/* Refer to Bluetooth Specification v5.2 Vol 6, Part C, Section 3 LE Channel
346+
* Selection algorithm #2 sample data
347+
*/
292348
void lll_chan_sel_2_ut(void)
293349
{
294350
uint8_t chan_map_1[] = {0xFF, 0xFF, 0xFF, 0xFF, 0x1F};
@@ -298,6 +354,8 @@ void lll_chan_sel_2_ut(void)
298354
uint16_t const chan_id = 0x305F;
299355
uint8_t m;
300356

357+
/* Tests when ISO not supported */
358+
/* Section 3.1 Sample Data 1 (37 used channels) */
301359
m = lll_chan_sel_2(0, chan_id, chan_map_1, chan_map_1_37_used);
302360
LL_ASSERT(m == 25U);
303361

@@ -310,6 +368,7 @@ void lll_chan_sel_2_ut(void)
310368
m = lll_chan_sel_2(3, chan_id, chan_map_1, chan_map_1_37_used);
311369
LL_ASSERT(m == 21U);
312370

371+
/* Section 3.1 Sample Data 2 (9 used channels) */
313372
m = lll_chan_sel_2(6, chan_id, chan_map_2, chan_map_2_9_used);
314373
LL_ASSERT(m == 23U);
315374

@@ -340,6 +399,7 @@ void lll_chan_sel_2_ut(void)
340399
prn_subevent_se = chan_prn_subevent_se(chan_id, &prn_subevent_lu);
341400
LL_ASSERT(prn_subevent_se == 38198);
342401

402+
/* Section 3.1 Sample Data 1 (37 used channels) */
343403
/* BIS subevent 1, event counter 0 */
344404
m = lll_chan_iso_event(0, chan_id, chan_map_1, chan_map_1_37_used, &prn_s, &remap_idx);
345405
LL_ASSERT((prn_s ^ chan_id) == 56857);
@@ -424,6 +484,7 @@ void lll_chan_sel_2_ut(void)
424484
LL_ASSERT(remap_idx == 8U);
425485
LL_ASSERT(m == 8U);
426486

487+
/* Section 3.1 Sample Data 2 (9 used channels) */
427488
/* BIS subevent 1, event counter 6 */
428489
m = lll_chan_iso_event(6, chan_id, chan_map_2, chan_map_2_9_used, &prn_s, &remap_idx);
429490
LL_ASSERT((prn_s ^ chan_id) == 10975);

0 commit comments

Comments
 (0)