Skip to content

Commit 0fd8af9

Browse files
committed
Bluetooth: Host: Make use of common array helper macros
There's a bunch of convenience macros in sys/util.h that are intended to help out with translating array indices and determining if an element is part of an array or not. Use those instead of custom code. Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
1 parent 45fc6fe commit 0fd8af9

File tree

3 files changed

+11
-17
lines changed

3 files changed

+11
-17
lines changed

‎subsys/bluetooth/host/adv.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,9 @@ static struct bt_le_ext_adv adv_pool[CONFIG_BT_EXT_ADV_MAX_ADV_SET];
187187
#if defined(CONFIG_BT_EXT_ADV)
188188
uint8_t bt_le_ext_adv_get_index(struct bt_le_ext_adv *adv)
189189
{
190-
ptrdiff_t index = adv - adv_pool;
190+
__ASSERT(IS_ARRAY_ELEMENT(adv_pool, adv), "Invalid bt_adv pointer");
191191

192-
__ASSERT(index >= 0 && index < ARRAY_SIZE(adv_pool),
193-
"Invalid bt_adv pointer");
194-
return (uint8_t)index;
192+
return (uint8_t)ARRAY_INDEX(adv_pool, adv);
195193
}
196194

197195
static struct bt_le_ext_adv *adv_new(void)

‎subsys/bluetooth/host/conn.c‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,23 +1546,20 @@ uint8_t bt_conn_index(const struct bt_conn *conn)
15461546
switch (conn->type) {
15471547
#if defined(CONFIG_BT_ISO)
15481548
case BT_CONN_TYPE_ISO:
1549-
index = conn - iso_conns;
1550-
__ASSERT(index >= 0 && index < ARRAY_SIZE(iso_conns),
1551-
"Invalid bt_conn pointer");
1549+
__ASSERT(IS_ARRAY_ELEMENT(iso_conns, conn), "Invalid bt_conn pointer");
1550+
index = ARRAY_INDEX(iso_conns, conn);
15521551
break;
15531552
#endif
15541553
#if defined(CONFIG_BT_CLASSIC)
15551554
case BT_CONN_TYPE_SCO:
1556-
index = conn - sco_conns;
1557-
__ASSERT(index >= 0 && index < ARRAY_SIZE(sco_conns),
1558-
"Invalid bt_conn pointer");
1555+
__ASSERT(IS_ARRAY_ELEMENT(sco_conns, conn), "Invalid bt_conn pointer");
1556+
index = ARRAY_INDEX(sco_conns, conn);
15591557
break;
15601558
#endif
15611559
default:
15621560
#if defined(CONFIG_BT_CONN)
1563-
index = conn - acl_conns;
1564-
__ASSERT(index >= 0 && index < ARRAY_SIZE(acl_conns),
1565-
"Invalid bt_conn pointer");
1561+
__ASSERT(IS_ARRAY_ELEMENT(acl_conns, conn), "Invalid bt_conn pointer");
1562+
index = ARRAY_INDEX(acl_conns, conn);
15661563
#else
15671564
__ASSERT(false, "Invalid connection type %u", conn->type);
15681565
#endif /* CONFIG_BT_CONN */

‎subsys/bluetooth/host/scan.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,11 +1841,10 @@ void bt_le_scan_cb_unregister(struct bt_le_scan_cb *cb)
18411841
#if defined(CONFIG_BT_PER_ADV_SYNC)
18421842
uint8_t bt_le_per_adv_sync_get_index(struct bt_le_per_adv_sync *per_adv_sync)
18431843
{
1844-
ptrdiff_t index = per_adv_sync - per_adv_sync_pool;
1845-
1846-
__ASSERT(index >= 0 && ARRAY_SIZE(per_adv_sync_pool) > index,
1844+
__ASSERT(IS_ARRAY_ELEMENT(per_adv_sync_pool, per_adv_sync),
18471845
"Invalid per_adv_sync pointer");
1848-
return (uint8_t)index;
1846+
1847+
return (uint8_t)ARRAY_INDEX(per_adv_sync_pool, per_adv_sync);
18491848
}
18501849

18511850
struct bt_le_per_adv_sync *bt_le_per_adv_sync_lookup_index(uint8_t index)

0 commit comments

Comments
 (0)