Skip to content

Commit ef767b8

Browse files
anhmoltlemrey
authored andcommitted
nrf_sdh: add function for getting index from conn_handle
Add functionality for assigning indices between 0 and (CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT - 1) to connection handles on connect and un-assign on disconnect. Add function for getting the index from a connection handle. This will enable other modules/libraries to get an index that can be used for assigning and looking up connection specific data. Signed-off-by: Andreas Moltumyr <[email protected]>
1 parent b37a52c commit ef767b8

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

include/nrf_sdh_ble.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ int nrf_sdh_ble_app_ram_start_get(uint32_t *app_ram_start);
8282
*/
8383
int nrf_sdh_ble_enable(uint8_t conn_cfg_tag);
8484

85+
/**
86+
* @brief Get the assigned index for a connection handle.
87+
*
88+
* The returned value can be used for indexing into arrays where each element is associated
89+
* with a specific connection. Connection handles should never directly be used for indexing arrays.
90+
*
91+
* @param[in] conn_handle Connection handle.
92+
*
93+
* @returns An integer in the range from 0 to (CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT - 1) if the
94+
* connection handle has been assigned to an index, otherwise -1.
95+
*/
96+
int nrf_sdh_ble_idx_get(uint16_t conn_handle);
97+
8598
#ifdef __cplusplus
8699
}
87100
#endif

subsys/softdevice_handler/nrf_sdh_ble.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,56 @@ int nrf_sdh_ble_enable(uint8_t conn_cfg_tag)
151151
return 0;
152152
}
153153

154+
static uint16_t conn_handles[CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT] = {
155+
[0 ... CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT - 1] = BLE_CONN_HANDLE_INVALID,
156+
};
157+
158+
int nrf_sdh_ble_idx_get(uint16_t conn_handle)
159+
{
160+
for (int idx = 0; idx < ARRAY_SIZE(conn_handles); idx++) {
161+
if (conn_handles[idx] == conn_handle) {
162+
return idx;
163+
}
164+
}
165+
166+
return -1;
167+
}
168+
169+
static void idx_assign(uint16_t conn_handle)
170+
{
171+
__ASSERT(conn_handle != BLE_CONN_HANDLE_INVALID, "Got invalid conn_handle from SoftDevice");
172+
173+
for (int idx = 0; idx < ARRAY_SIZE(conn_handles); idx++) {
174+
__ASSERT(conn_handles[idx] != conn_handle,
175+
"conn_handle %#x is already assigned to idx %d", conn_handle, idx);
176+
}
177+
178+
for (int idx = 0; idx < ARRAY_SIZE(conn_handles); idx++) {
179+
if (conn_handles[idx] == BLE_CONN_HANDLE_INVALID) {
180+
conn_handles[idx] = conn_handle;
181+
LOG_DBG("Assigned idx %d to conn_handle %#x", idx, conn_handle);
182+
return;
183+
}
184+
}
185+
186+
__ASSERT(false, "Failed to assign idx to conn_handle %#x, all are in use");
187+
188+
LOG_ERR("Failed to assign idx to conn_handle %#x", conn_handle);
189+
}
190+
191+
static void idx_unassign(uint16_t conn_handle)
192+
{
193+
for (int idx = 0; idx < ARRAY_SIZE(conn_handles); idx++) {
194+
if (conn_handles[idx] == conn_handle) {
195+
conn_handles[idx] = BLE_CONN_HANDLE_INVALID;
196+
LOG_DBG("Unassigned idx %d from conn_handle %#x", idx, conn_handle);
197+
return;
198+
}
199+
}
200+
201+
__ASSERT(false, "Could not find any idx assigned to conn_handle %#x");
202+
}
203+
154204
static void ble_evt_poll(void *context)
155205
{
156206
int err;
@@ -172,11 +222,19 @@ static void ble_evt_poll(void *context)
172222
LOG_DBG("BLE event: %#x", ble_evt->header.evt_id);
173223
}
174224

225+
if (ble_evt->header.evt_id == BLE_GAP_EVT_CONNECTED) {
226+
idx_assign(ble_evt->evt.gap_evt.conn_handle);
227+
}
228+
175229
/* Forward the event to BLE observers. */
176230
TYPE_SECTION_FOREACH(
177231
struct nrf_sdh_ble_evt_observer, nrf_sdh_ble_evt_observers, obs) {
178232
obs->handler(ble_evt, obs->context);
179233
}
234+
235+
if (ble_evt->header.evt_id == BLE_GAP_EVT_DISCONNECTED) {
236+
idx_unassign(ble_evt->evt.gap_evt.conn_handle);
237+
}
180238
}
181239

182240
__ASSERT(err == NRF_ERROR_NOT_FOUND,

0 commit comments

Comments
 (0)