diff --git a/include/bluetooth/l2cap.h b/include/bluetooth/l2cap.h index c6fb15bd659..d3acbd0c950 100644 --- a/include/bluetooth/l2cap.h +++ b/include/bluetooth/l2cap.h @@ -84,6 +84,17 @@ struct bt_l2cap_le_chan { uint16_t _sdu_len; }; +/** @def BT_L2CAP_LE_CHAN(_ch) + * @brief Helper macro getting container object of type bt_l2cap_le_chan + * address having the same container chan member address as object in question. + * + * @param _ch Address of object of bt_l2cap_chan type + * + * @return Address of in memory bt_l2cap_le_chan object type containing + * the address of in question object. + */ +#define BT_L2CAP_LE_CHAN(_ch) CONTAINER_OF(_ch, struct bt_l2cap_le_chan, chan) + /** @brief BREDR L2CAP Endpoint structure. */ struct bt_l2cap_br_endpoint { /** Endpoint CID */ diff --git a/net/bluetooth/att.c b/net/bluetooth/att.c index 95837ec5c3b..2a8aaa74f29 100644 --- a/net/bluetooth/att.c +++ b/net/bluetooth/att.c @@ -1706,9 +1706,7 @@ static void bt_att_connected(struct bt_l2cap_chan *chan) #if CONFIG_BLUETOOTH_ATT_PREPARE_COUNT > 0 struct bt_att *att = ATT_CHAN(chan); #endif - - struct bt_l2cap_le_chan *ch = - CONTAINER_OF(chan, struct bt_l2cap_le_chan, chan); + struct bt_l2cap_le_chan *ch = BT_L2CAP_LE_CHAN(chan); BT_DBG("chan %p cid 0x%04x", ch, ch->tx.cid); @@ -1740,8 +1738,7 @@ static void att_reset(struct bt_att *att) static void bt_att_disconnected(struct bt_l2cap_chan *chan) { struct bt_att *att = ATT_CHAN(chan); - struct bt_l2cap_le_chan *ch = - CONTAINER_OF(chan, struct bt_l2cap_le_chan, chan); + struct bt_l2cap_le_chan *ch = BT_L2CAP_LE_CHAN(chan); BT_DBG("chan %p cid 0x%04x", ch, ch->tx.cid); @@ -1755,8 +1752,7 @@ static void bt_att_disconnected(struct bt_l2cap_chan *chan) static void bt_att_encrypt_change(struct bt_l2cap_chan *chan) { struct bt_att *att = ATT_CHAN(chan); - struct bt_l2cap_le_chan *ch = - CONTAINER_OF(chan, struct bt_l2cap_le_chan, chan); + struct bt_l2cap_le_chan *ch = BT_L2CAP_LE_CHAN(chan); struct bt_conn *conn = ch->chan.conn; struct bt_att_req *req; diff --git a/net/bluetooth/l2cap.c b/net/bluetooth/l2cap.c index fb1f65902a1..4eed3ce8433 100644 --- a/net/bluetooth/l2cap.c +++ b/net/bluetooth/l2cap.c @@ -40,8 +40,6 @@ #define BT_DBG(fmt, ...) #endif -#define LE_CHAN(_ch) CONTAINER_OF(_ch, struct bt_l2cap_le_chan, chan) - #define L2CAP_LE_MIN_MTU 23 #define L2CAP_LE_MAX_CREDITS (CONFIG_BLUETOOTH_ACL_IN_COUNT - 1) #define L2CAP_LE_CREDITS_THRESHOLD (L2CAP_LE_MAX_CREDITS / 2) @@ -113,7 +111,7 @@ void bt_l2cap_le_fixed_chan_register(struct bt_l2cap_fixed_chan *chan) static struct bt_l2cap_le_chan *l2cap_chan_alloc_cid(struct bt_conn *conn, struct bt_l2cap_chan *chan) { - struct bt_l2cap_le_chan *ch = LE_CHAN(chan); + struct bt_l2cap_le_chan *ch = BT_L2CAP_LE_CHAN(chan); uint16_t cid; /* @@ -174,7 +172,7 @@ void bt_l2cap_connected(struct bt_conn *conn) continue; } - ch = LE_CHAN(chan); + ch = BT_L2CAP_LE_CHAN(chan); /* Fill up remaining fixed channel context attached in * fchan->accept() @@ -488,7 +486,7 @@ static void le_conn_req(struct bt_l2cap *l2cap, uint8_t ident, } if (l2cap_chan_add(conn, chan)) { - struct bt_l2cap_le_chan *ch = LE_CHAN(chan); + struct bt_l2cap_le_chan *ch = BT_L2CAP_LE_CHAN(chan); /* Init TX parameters */ l2cap_chan_tx_init(ch); @@ -526,7 +524,7 @@ static struct bt_l2cap_le_chan *l2cap_remove_tx_cid(struct bt_conn *conn, for (chan = conn->channels, prev = NULL; chan; prev = chan, chan = chan->_next) { /* get the app's l2cap object wherein this chan is contained */ - struct bt_l2cap_le_chan *ch = LE_CHAN(chan); + struct bt_l2cap_le_chan *ch = BT_L2CAP_LE_CHAN(chan); if (ch->tx.cid != cid) { continue; @@ -546,7 +544,7 @@ static struct bt_l2cap_le_chan *l2cap_remove_tx_cid(struct bt_conn *conn, static void l2cap_chan_del(struct bt_l2cap_chan *chan) { - struct bt_l2cap_le_chan *ch = LE_CHAN(chan); + struct bt_l2cap_le_chan *ch = BT_L2CAP_LE_CHAN(chan); BT_DBG("conn %p chan %p cid 0x%04x", chan->conn, ch, ch->rx.cid); @@ -630,7 +628,7 @@ __l2cap_lookup_ident(struct bt_conn *conn, uint16_t ident, bool remove) for (chan = conn->channels, prev = NULL; chan; prev = chan, chan = chan->_next) { /* get the app's l2cap object where this chan is member */ - struct bt_l2cap_le_chan *ch = LE_CHAN(chan); + struct bt_l2cap_le_chan *ch = BT_L2CAP_LE_CHAN(chan); if (ch->ident != ident) { continue; @@ -759,7 +757,7 @@ static void le_credits(struct bt_l2cap *l2cap, uint8_t ident, return; } - ch = LE_CHAN(chan); + ch = BT_L2CAP_LE_CHAN(chan); if (ch->tx.credits.nsig + credits > UINT16_MAX) { BT_ERR("Credits overflow"); @@ -971,7 +969,7 @@ static void l2cap_chan_recv(struct bt_l2cap_chan *chan, struct net_buf *buf) switch (chan->conn->type) { case BT_CONN_TYPE_LE: - ch = LE_CHAN(chan); + ch = BT_L2CAP_LE_CHAN(chan); break; default: ch = NULL; @@ -1061,12 +1059,14 @@ int bt_l2cap_update_conn_param(struct bt_conn *conn, static void l2cap_connected(struct bt_l2cap_chan *chan) { - BT_DBG("ch %p cid 0x%04x", LE_CHAN(chan), LE_CHAN(chan)->rx.cid); + BT_DBG("ch %p cid 0x%04x", BT_L2CAP_LE_CHAN(chan), + BT_L2CAP_LE_CHAN(chan)->rx.cid); } static void l2cap_disconnected(struct bt_l2cap_chan *chan) { - BT_DBG("ch %p cid 0x%04x", LE_CHAN(chan), LE_CHAN(chan)->rx.cid); + BT_DBG("ch %p cid 0x%04x", BT_L2CAP_LE_CHAN(chan), + BT_L2CAP_LE_CHAN(chan)->rx.cid); } static int l2cap_accept(struct bt_conn *conn, struct bt_l2cap_chan **chan) @@ -1123,7 +1123,7 @@ struct bt_l2cap_chan *bt_l2cap_le_lookup_tx_cid(struct bt_conn *conn, struct bt_l2cap_chan *chan; for (chan = conn->channels; chan; chan = chan->_next) { - struct bt_l2cap_le_chan *ch = LE_CHAN(chan); + struct bt_l2cap_le_chan *ch = BT_L2CAP_LE_CHAN(chan); if (ch->tx.cid == cid) return chan; @@ -1138,7 +1138,7 @@ struct bt_l2cap_chan *bt_l2cap_le_lookup_rx_cid(struct bt_conn *conn, struct bt_l2cap_chan *chan; for (chan = conn->channels; chan; chan = chan->_next) { - struct bt_l2cap_le_chan *ch = LE_CHAN(chan); + struct bt_l2cap_le_chan *ch = BT_L2CAP_LE_CHAN(chan); if (ch->rx.cid == cid) { return chan; @@ -1211,7 +1211,7 @@ int bt_l2cap_chan_connect(struct bt_conn *conn, struct bt_l2cap_chan *chan, } #endif /* CONFIG_BLUETOOTH_BREDR */ - return l2cap_le_connect(conn, LE_CHAN(chan), psm); + return l2cap_le_connect(conn, BT_L2CAP_LE_CHAN(chan), psm); } int bt_l2cap_chan_disconnect(struct bt_l2cap_chan *chan) @@ -1232,7 +1232,7 @@ int bt_l2cap_chan_disconnect(struct bt_l2cap_chan *chan) } #endif /* CONFIG_BLUETOOTH_BREDR */ - ch = LE_CHAN(chan); + ch = BT_L2CAP_LE_CHAN(chan); BT_DBG("chan %p scid 0x%04x dcid 0x%04x", chan, ch->rx.cid, ch->tx.cid); @@ -1382,7 +1382,7 @@ int bt_l2cap_chan_send(struct bt_l2cap_chan *chan, struct net_buf *buf) } #endif /* CONFIG_BLUETOOTH_BREDR */ - err = l2cap_chan_le_send_sdu(LE_CHAN(chan), buf); + err = l2cap_chan_le_send_sdu(BT_L2CAP_LE_CHAN(chan), buf); if (err < 0) { BT_ERR("failed to send message %d", err); }