Skip to content

Commit e74b4ea

Browse files
committed
nimble/host: Refactor ble_iso_big_conn_handles_init
This refactors ble_iso_big_conn_handles_init macro into function. Few asserts have been added to catch the potential issues. The nested loop has been removed.
1 parent 12479ba commit e74b4ea

1 file changed

Lines changed: 39 additions & 24 deletions

File tree

nimble/host/src/ble_iso.c

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,6 @@
3434
#define min(a, b) ((a) < (b) ? (a) : (b))
3535
#endif
3636

37-
#define ble_iso_big_conn_handles_init(_big, _handles, _num_handles) \
38-
do { \
39-
struct ble_iso_conn *conn = SLIST_FIRST(&ble_iso_conns); \
40-
\
41-
for (uint8_t i = 0; i < (_num_handles); i++) { \
42-
while (conn != NULL) { \
43-
if (conn->type == BLE_ISO_CONN_BIS) { \
44-
struct ble_iso_bis *bis; \
45-
\
46-
bis = CONTAINER_OF(conn, struct ble_iso_bis, conn); \
47-
if (bis->big == (_big)) { \
48-
conn->handle = le16toh((_handles)[i]); \
49-
conn = SLIST_NEXT(conn, next); \
50-
break; \
51-
} \
52-
} \
53-
\
54-
conn = SLIST_NEXT(conn, next); \
55-
} \
56-
} \
57-
} while (0);
58-
5937
enum ble_iso_conn_type {
6038
BLE_ISO_CONN_BIS,
6139
};
@@ -96,6 +74,43 @@ static struct os_mempool ble_iso_bis_pool;
9674
static os_membuf_t ble_iso_bis_mem[
9775
OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_ISO_MAX_BISES), sizeof (struct ble_iso_bis))];
9876

77+
static int
78+
ble_iso_big_conn_handles_set(struct ble_iso_big *big, const uint8_t *handles,
79+
uint8_t num_handles)
80+
{
81+
struct ble_iso_conn *conn;
82+
uint8_t assigned = 0;
83+
84+
assert(big != NULL);
85+
assert(handles != NULL);
86+
assert(num_handles > 0);
87+
88+
SLIST_FOREACH(conn, &ble_iso_conns, next) {
89+
struct ble_iso_bis *bis;
90+
91+
if (assigned == num_handles) {
92+
break;
93+
}
94+
95+
if (conn->type != BLE_ISO_CONN_BIS) {
96+
continue;
97+
}
98+
99+
bis = CONTAINER_OF(conn, struct ble_iso_bis, conn);
100+
if (bis->big != big) {
101+
continue;
102+
}
103+
104+
assert(conn->handle == BLE_HS_CONN_HANDLE_NONE);
105+
conn->handle = get_le16(&handles[assigned * sizeof(uint16_t)]);
106+
assigned++;
107+
}
108+
109+
assert(assigned == num_handles);
110+
111+
return 0;
112+
}
113+
99114
static void
100115
ble_iso_conn_append(struct ble_iso_conn *conn)
101116
{
@@ -360,7 +375,7 @@ ble_iso_rx_create_big_complete(const struct ble_hci_ev_le_subev_create_big_compl
360375
/* XXX: Should we destroy the group? */
361376
}
362377

363-
ble_iso_big_conn_handles_init(big, ev->conn_handle, ev->num_bis);
378+
ble_iso_big_conn_handles_set(big, (const uint8_t *)ev->conn_handle, ev->num_bis);
364379

365380
big->max_pdu = ev->max_pdu;
366381

@@ -643,7 +658,7 @@ ble_iso_rx_big_sync_established(const struct ble_hci_ev_le_subev_big_sync_establ
643658
/* XXX: Should we destroy the group? */
644659
}
645660

646-
ble_iso_big_conn_handles_init(big, ev->conn_handle, ev->num_bis);
661+
ble_iso_big_conn_handles_set(big, (const uint8_t *)ev->conn_handle, ev->num_bis);
647662

648663
event.big_sync_established.desc.big_handle = ev->big_handle;
649664
event.big_sync_established.desc.transport_latency_big =

0 commit comments

Comments
 (0)