Skip to content

Commit 58e9ac6

Browse files
asmk-otaescolar
authored andcommitted
bluetooth: Controller: Refactor node_rx footer to avoid ptr arithmetic
The old footer was appended after PDU using pointer arithmetic. Now the footer fields have been moved to the header struct, the footer fields are now statically located in the data structure, this is type safe and fields can be referred to by their actual names rather than indirectly through reference to other members, thus avoiding pointer arithmetic. Secondly, this change will pave the way for adding other meta data in the future. Signed-off-by: Asger Munk Nielsen <[email protected]>
1 parent 52ab40c commit 58e9ac6

File tree

7 files changed

+83
-45
lines changed

7 files changed

+83
-45
lines changed

subsys/bluetooth/controller/hci/hci.c

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
#include "hci_internal.h"
3636
#include "hci_vendor.h"
3737

38+
#if (!defined(CONFIG_BT_LL_SW_SPLIT))
39+
#include "ll_sw/ctrl.h"
40+
#endif /* CONFIG_BT_LL_SW_SPLIT */
41+
3842
#if defined(CONFIG_BT_HCI_MESH_EXT)
3943
#include "ll_sw/ll_mesh.h"
4044
#endif /* CONFIG_BT_HCI_MESH_EXT */
@@ -1309,7 +1313,7 @@ static void le_read_max_data_len(struct net_buf *buf, struct net_buf **evt)
13091313
#if defined(CONFIG_BT_CTLR_PHY)
13101314
static void le_read_phy(struct net_buf *buf, struct net_buf **evt)
13111315
{
1312-
struct bt_hci_cp_le_read_phy *cmd = (void *) buf->data;
1316+
struct bt_hci_cp_le_read_phy *cmd = (void *)buf->data;
13131317
struct bt_hci_rp_le_read_phy *rp;
13141318
u16_t handle;
13151319
u8_t status;
@@ -1427,6 +1431,7 @@ static void le_rem_dev_from_rl(struct net_buf *buf, struct net_buf **evt)
14271431
static void le_clear_rl(struct net_buf *buf, struct net_buf **evt)
14281432
{
14291433
struct bt_hci_evt_cc_status *ccst;
1434+
14301435
ccst = hci_cmd_complete(evt, sizeof(*ccst));
14311436

14321437
ccst->status = ll_rl_clear();
@@ -2499,8 +2504,15 @@ static void le_advertising_report(struct pdu_data *pdu_data, u8_t *b,
24992504
#endif /* CONFIG_BT_CTLR_EXT_SCAN_FP */
25002505
s8_t *prssi;
25012506

2502-
extra = &b[offsetof(struct node_rx_pdu, pdu) +
2503-
offsetof(struct pdu_adv, payload) + adv->len];
2507+
#if defined(CONFIG_BT_LL_SW_SPLIT)
2508+
struct node_rx_pdu *node_rx;
2509+
2510+
node_rx = (struct node_rx_pdu *)b;
2511+
extra = (u8_t *)&(node_rx->hdr.rx_ftr.param);
2512+
#else
2513+
extra = (u8_t *)&b[offsetof(struct radio_pdu_node_rx, pdu_data) +
2514+
offsetof(struct pdu_adv, payload) + adv->len];
2515+
#endif /* CONFIG_BT_LL_SW_SPLIT */
25042516

25052517
/* The Link Layer currently returns RSSI as an absolute value */
25062518
rssi = -(*extra);
@@ -2592,10 +2604,19 @@ static void le_adv_ext_report(struct pdu_data *pdu_data, u8_t *b,
25922604
{
25932605
struct pdu_adv *adv = (void *)pdu_data;
25942606
s8_t rssi;
2607+
u8_t *extra;
2608+
2609+
#if defined(CONFIG_BT_LL_SW_SPLIT)
2610+
struct node_rx_pdu *node_rx;
25952611

2612+
node_rx = (struct node_rx_pdu *)b;
2613+
extra = (u8_t *)&(node_rx->hdr.rx_ftr.param);
2614+
#else
2615+
extra = (u8_t *)&b[offsetof(struct radio_pdu_node_rx, pdu_data) +
2616+
offsetof(struct pdu_adv, payload) + adv->len];
2617+
#endif /* CONFIG_BT_LL_SW_SPLIT */
25962618
/* The Link Layer currently returns RSSI as an absolute value */
2597-
rssi = -b[offsetof(struct node_rx_pdu, pdu) +
2598-
offsetof(struct pdu_adv, payload) + adv->len];
2619+
rssi = -(*extra);
25992620

26002621
BT_DBG("phy= 0x%x, type= 0x%x, len= %u, tat= %u, rat= %u, rssi=%d dB",
26012622
phy, adv->type, adv->len, adv->tx_addr, adv->rx_addr, rssi);
@@ -2674,15 +2695,28 @@ static void le_scan_req_received(struct pdu_data *pdu_data, u8_t *b,
26742695
char addr_str[BT_ADDR_LE_STR_LEN];
26752696
bt_addr_le_t addr;
26762697
u8_t handle;
2698+
u8_t *extra;
26772699
s8_t rssi;
26782700

26792701
handle = 0U;
26802702
addr.type = adv->tx_addr;
26812703
memcpy(&addr.a.val[0], &adv->scan_req.scan_addr[0],
26822704
sizeof(bt_addr_t));
2705+
2706+
#if defined(CONFIG_BT_LL_SW_SPLIT)
2707+
struct node_rx_pdu *node_rx;
2708+
2709+
node_rx = (struct node_rx_pdu *)b;
2710+
extra = (u8_t *)&(node_rx->hdr.rx_ftr.param);
2711+
#else
2712+
extra = (u8_t *)&b[offsetof(struct radio_pdu_node_rx,
2713+
pdu_data) +
2714+
offsetof(struct pdu_adv, payload) +
2715+
adv->len];
2716+
#endif /* CONFIG_BT_LL_SW_SPLIT */
2717+
26832718
/* The Link Layer currently returns RSSI as an absolute value */
2684-
rssi = -b[offsetof(struct node_rx_pdu, pdu) +
2685-
offsetof(struct pdu_adv, payload) + adv->len];
2719+
rssi = -(*extra);
26862720

26872721
bt_addr_le_to_str(&addr, addr_str, sizeof(addr_str));
26882722

@@ -2754,7 +2788,8 @@ static void le_conn_complete(struct pdu_data *pdu_data, u16_t handle,
27542788

27552789
/* Note: this could be an RPA set as the random address by
27562790
* the Host instead of generated by the controller. That said,
2757-
* this should make no difference. */
2791+
* this should make no difference.
2792+
*/
27582793
if ((node_rx->own_addr_type) &&
27592794
((node_rx->own_addr[5] & 0xc0) == 0x40)) {
27602795
memcpy(&leecc->local_rpa.val[0], &node_rx->own_addr[0],
@@ -3262,9 +3297,16 @@ void hci_acl_encode(struct node_rx_pdu *node_rx, struct net_buf *buf)
32623297
u16_t handle;
32633298
u8_t *data;
32643299

3265-
pdu_data = (void *)node_rx->pdu;
32663300
handle = node_rx->hdr.handle;
32673301

3302+
#if defined(CONFIG_BT_LL_SW_SPLIT)
3303+
pdu_data = (void *)node_rx->pdu;
3304+
#else
3305+
u8_t *b = (u8_t *)node_rx;
3306+
3307+
pdu_data = (void *)&b[offsetof(struct radio_pdu_node_rx, pdu_data)];
3308+
#endif /* CONFIG_BT_LL_SW_SPLIT */
3309+
32683310
switch (pdu_data->ll_id) {
32693311
case PDU_DATA_LLID_DATA_CONTINUE:
32703312
case PDU_DATA_LLID_DATA_START:
@@ -3304,7 +3346,13 @@ void hci_evt_encode(struct node_rx_pdu *node_rx, struct net_buf *buf)
33043346
{
33053347
struct pdu_data *pdu_data;
33063348

3349+
#if defined(CONFIG_BT_LL_SW_SPLIT)
33073350
pdu_data = (void *)node_rx->pdu;
3351+
#else
3352+
u8_t *b = (u8_t *)node_rx;
3353+
3354+
pdu_data = (void *)&b[offsetof(struct radio_pdu_node_rx, pdu_data)];
3355+
#endif /* CONFIG_BT_LL_SW_SPLIT */
33083356

33093357
if (node_rx->hdr.type != NODE_RX_TYPE_DC_PDU) {
33103358
encode_control(node_rx, pdu_data, buf);
@@ -3336,7 +3384,13 @@ s8_t hci_get_class(struct node_rx_pdu *node_rx)
33363384
{
33373385
struct pdu_data *pdu_data;
33383386

3387+
#if defined(CONFIG_BT_LL_SW_SPLIT)
33393388
pdu_data = (void *)node_rx->pdu;
3389+
#else
3390+
u8_t *b = (u8_t *)node_rx;
3391+
3392+
pdu_data = (void *)&b[offsetof(struct radio_pdu_node_rx, pdu_data)];
3393+
#endif /* CONFIG_BT_LL_SW_SPLIT */
33403394

33413395
if (node_rx->hdr.type != NODE_RX_TYPE_DC_PDU) {
33423396

subsys/bluetooth/controller/ll_sw/lll.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ enum node_rx_type {
175175
#endif /* CONFIG_BT_HCI_MESH_EXT */
176176
};
177177

178+
179+
/* Footer of node_rx_hdr */
180+
struct node_rx_ftr {
181+
void *param;
182+
void *extra;
183+
u32_t ticks_anchor;
184+
u32_t us_radio_end;
185+
u32_t us_radio_rdy;
186+
};
187+
188+
178189
/* Header of node_rx_pdu */
179190
struct node_rx_hdr {
180191
union {
@@ -185,26 +196,13 @@ struct node_rx_hdr {
185196

186197
enum node_rx_type type;
187198
u16_t handle;
188-
};
189199

190-
/* Footer of node_rx_pdu.
191-
* TODO: Eliminate footer (move contents to header) to avoid pointer arithmetic
192-
*/
193-
struct node_rx_ftr {
194-
void *param;
195-
void *extra;
196-
u32_t ticks_anchor;
197-
u32_t us_radio_end;
198-
u32_t us_radio_rdy;
200+
struct node_rx_ftr rx_ftr;
199201
};
200202

201203
struct node_rx_pdu {
202204
struct node_rx_hdr hdr;
203205
u8_t pdu[0];
204-
/*
205-
* Footer follows here, but can not be part of this struct due to
206-
* flexible pdu member. Footer obtained by pointer arithmetic
207-
*/
208206
};
209207

210208
enum {

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_adv.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -710,10 +710,7 @@ static inline int isr_rx_pdu(struct lll_adv *lll,
710710
memcpy(rx->pdu, pdu_rx, (offsetof(struct pdu_adv, connect_ind) +
711711
sizeof(struct pdu_adv_connect_ind)));
712712

713-
ftr = (void *)((u8_t *)rx->pdu +
714-
(offsetof(struct pdu_adv, connect_ind) +
715-
sizeof(struct pdu_adv_connect_ind)));
716-
713+
ftr = &(rx->hdr.rx_ftr);
717714
ftr->param = lll;
718715
ftr->ticks_anchor = radio_tmr_start_get();
719716
ftr->us_radio_end = radio_tmr_end_get() -

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_scan.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -768,10 +768,7 @@ static inline u32_t isr_rx_pdu(struct lll_scan *lll, u8_t devmatch_ok,
768768
rx->hdr.type = NODE_RX_TYPE_CONNECTION;
769769
rx->hdr.handle = 0xffff;
770770

771-
ftr = (void *)((u8_t *)rx->pdu +
772-
(offsetof(struct pdu_adv, connect_ind) +
773-
sizeof(struct pdu_adv_connect_ind)));
774-
771+
ftr = &(rx->hdr.rx_ftr);
775772
ftr->param = lll;
776773
ftr->ticks_anchor = radio_tmr_start_get();
777774
ftr->us_radio_end = conn_space_us -
@@ -1035,8 +1032,8 @@ static u32_t isr_rx_scan_report(struct lll_scan *lll, u8_t rssi_ready,
10351032
}
10361033

10371034
pdu_adv_rx = (void *)node_rx->pdu;
1038-
extra = &((u8_t *)pdu_adv_rx)[offsetof(struct pdu_adv, payload) +
1039-
pdu_adv_rx->len];
1035+
extra = (u8_t *)&(node_rx->hdr.rx_ftr.param);
1036+
10401037
/* save the RSSI value */
10411038
*extra = (rssi_ready) ? (radio_rssi_get() & 0x7f) : 0x7f;
10421039
extra += PDU_AC_SIZE_RSSI;

subsys/bluetooth/controller/ll_sw/ull.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ static MFIFO_DEFINE(pdu_rx_free, sizeof(void *), PDU_RX_CNT);
146146
static MFIFO_DEFINE(ll_pdu_rx_free, sizeof(void *), LL_PDU_RX_CNT);
147147

148148
#define NODE_RX_HEADER_SIZE (offsetof(struct node_rx_pdu, pdu))
149-
#define NODE_RX_FOOTER_SIZE (sizeof(struct node_rx_ftr))
150-
#define NODE_RX_STRUCT_OVERHEAD (NODE_RX_HEADER_SIZE + NODE_RX_FOOTER_SIZE)
149+
#define NODE_RX_STRUCT_OVERHEAD (NODE_RX_HEADER_SIZE)
151150

152151
#define PDU_ADVERTIZE_SIZE (PDU_AC_SIZE_MAX + PDU_AC_SIZE_EXTRA)
153152
#define PDU_DATA_SIZE (PDU_DC_LL_HEADER_SIZE + LL_LENGTH_OCTETS_RX_MAX)
@@ -553,9 +552,7 @@ void ll_rx_dequeue(void)
553552
} else if (rx->type == NODE_RX_TYPE_CONNECTION) {
554553
struct node_rx_ftr *ftr;
555554

556-
ftr = (void *)((u8_t *)((struct node_rx_pdu *)rx)->pdu +
557-
(offsetof(struct pdu_adv, connect_ind) +
558-
sizeof(struct pdu_adv_connect_ind)));
555+
ftr = &(rx->rx_ftr);
559556

560557
if (0) {
561558
#if defined(CONFIG_BT_PERIPHERAL)

subsys/bluetooth/controller/ll_sw/ull_adv.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,10 +1140,7 @@ static void disabled_cb(void *param)
11401140
memset(cc, 0x00, sizeof(struct node_rx_cc));
11411141
cc->status = 0x3c;
11421142

1143-
ftr = (void *)((u8_t *)rx->pdu +
1144-
(offsetof(struct pdu_adv, connect_ind) +
1145-
sizeof(struct pdu_adv_connect_ind)));
1146-
1143+
ftr = &(rx->hdr.rx_ftr);
11471144
ftr->param = param;
11481145

11491146
ll_rx_put(link, rx);

subsys/bluetooth/controller/ll_sw/ull_conn.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,7 @@ void ull_conn_setup(memq_link_t *link, struct node_rx_hdr *rx)
629629
struct node_rx_ftr *ftr;
630630
struct lll_conn *lll;
631631

632-
ftr = (void *)((u8_t *)((struct node_rx_pdu *)rx)->pdu +
633-
(offsetof(struct pdu_adv, connect_ind) +
634-
sizeof(struct pdu_adv_connect_ind)));
632+
ftr = &(rx->rx_ftr);
635633

636634
lll = *((struct lll_conn **)((u8_t *)ftr->param +
637635
sizeof(struct lll_hdr)));

0 commit comments

Comments
 (0)