Skip to content

Commit c433982

Browse files
committed
nimble/mesh: Fix handling of connection handle value
Connection handle is not guaranteed to start from 0.
1 parent d38ca0a commit c433982

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

nimble/host/mesh/src/proxy_msg.c

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static void proxy_sar_timeout(struct ble_npl_event *work)
6161

6262
BT_WARN("Proxy SAR timeout");
6363

64-
if (role->conn_handle) {
64+
if (role->conn_handle != BLE_HS_CONN_HANDLE_NONE) {
6565
rc = ble_gap_terminate(role->conn_handle,
6666
BLE_ERR_REM_USER_CONN_TERM);
6767
assert(rc == 0);
@@ -194,7 +194,7 @@ static void proxy_msg_init(struct bt_mesh_proxy_role *role)
194194

195195
role->buf = NET_BUF_SIMPLE(CONFIG_BT_MESH_PROXY_MSG_LEN);
196196
net_buf_simple_init_with_data(role->buf,
197-
&bufs[role->conn_handle *
197+
&bufs[role->index *
198198
CONFIG_BT_MESH_PROXY_MSG_LEN],
199199
CONFIG_BT_MESH_PROXY_MSG_LEN);
200200

@@ -204,15 +204,42 @@ static void proxy_msg_init(struct bt_mesh_proxy_role *role)
204204
k_work_add_arg_delayable(&role->sar_timer, role);
205205
}
206206

207+
struct bt_mesh_proxy_role *bt_mesh_proxy_role_find_with_buf(const struct os_mbuf *buf)
208+
{
209+
unsigned int i;
210+
211+
for (i = 0; i < CONFIG_BT_MAX_CONN; i++) {
212+
if (roles[i].buf == buf) {
213+
return &roles[i];
214+
}
215+
}
216+
217+
return NULL;
218+
}
219+
220+
struct bt_mesh_proxy_role *get_role(uint16_t conn_handle)
221+
{
222+
unsigned int i;
223+
224+
for (i = 0; i < CONFIG_BT_MAX_CONN; i++) {
225+
if (roles[i].conn_handle == BLE_HS_CONN_HANDLE_NONE) {
226+
roles[i].conn_handle = conn_handle;
227+
return &roles[i];
228+
}
229+
}
230+
231+
return NULL;
232+
}
233+
207234
struct bt_mesh_proxy_role *bt_mesh_proxy_role_setup(uint16_t conn_handle,
208235
proxy_send_cb_t send,
209236
proxy_recv_cb_t recv)
210237
{
211238
struct bt_mesh_proxy_role *role;
212239

213-
role = &roles[conn_handle];
240+
role = get_role(conn_handle);
241+
assert(role);
214242

215-
role->conn_handle = conn_handle;
216243
proxy_msg_init(role);
217244

218245
role->cb.recv = recv;
@@ -234,4 +261,14 @@ void bt_mesh_proxy_role_cleanup(struct bt_mesh_proxy_role *role)
234261
bt_mesh_adv_update();
235262
}
236263

264+
void bt_mesh_proxy_msg_init(void)
265+
{
266+
unsigned int i;
267+
268+
for (i = 0; i < MYNEWT_VAL(BLE_MAX_CONNECTIONS); i++) {
269+
roles[i].index = i;
270+
roles[i].conn_handle = 0xffff;
271+
}
272+
}
273+
237274
#endif /* MYNEWT_VAL(BLE_MESH_PROXY) */

nimble/host/mesh/src/proxy_msg.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef int (*proxy_send_cb_t)(uint16_t conn_handle,
3131
typedef void (*proxy_recv_cb_t)(struct bt_mesh_proxy_role *role);
3232

3333
struct bt_mesh_proxy_role {
34+
unsigned int index;
3435
uint16_t conn_handle;
3536
uint8_t msg_type;
3637

@@ -58,10 +59,13 @@ struct bt_mesh_proxy_client {
5859
int bt_mesh_proxy_msg_recv(struct bt_mesh_proxy_role *role,
5960
const void *buf, uint16_t len);
6061
int bt_mesh_proxy_msg_send(struct bt_mesh_proxy_role *role, uint8_t type, struct os_mbuf *msg);
61-
void bt_mesh_proxy_msg_init(struct bt_mesh_proxy_role *role);
6262
void bt_mesh_proxy_role_cleanup(struct bt_mesh_proxy_role *role);
6363
struct bt_mesh_proxy_role *bt_mesh_proxy_role_setup(uint16_t conn_handle,
6464
proxy_send_cb_t send,
6565
proxy_recv_cb_t recv);
6666
struct bt_mesh_proxy_client *find_client(uint16_t conn_handle);
67+
68+
struct bt_mesh_proxy_role *bt_mesh_proxy_role_find_with_buf(const struct os_mbuf *buf);
69+
70+
void bt_mesh_proxy_msg_init(void);
6771
#endif /* ZEPHYR_SUBSYS_BLUETOOTH_MESH_PROXY_MSG_H_ */

nimble/host/mesh/src/proxy_srv.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,10 +720,13 @@ int bt_mesh_proxy_gatt_disable(void)
720720
void bt_mesh_proxy_addr_add(struct os_mbuf *buf, uint16_t addr)
721721
{
722722
struct bt_mesh_proxy_client *client;
723-
struct bt_mesh_proxy_role *cli =
724-
CONTAINER_OF(buf, struct bt_mesh_proxy_role, buf);
723+
struct bt_mesh_proxy_role *cli;
724+
725+
cli = bt_mesh_proxy_role_find_with_buf(buf);
726+
assert(cli);
725727

726728
client = find_client(cli->conn_handle);
729+
assert(client);
727730

728731
BT_DBG("filter_type %u addr 0x%04x", client->filter_type, addr);
729732

@@ -997,6 +1000,8 @@ int bt_mesh_proxy_init(void)
9971000
clients[i].conn_handle = 0xffff;
9981001
}
9991002

1003+
bt_mesh_proxy_msg_init();
1004+
10001005
resolve_svc_handles();
10011006

10021007
ble_gatts_svc_set_visibility(svc_handles.proxy_h, 0);

0 commit comments

Comments
 (0)