From a42abba4f60dff37c77f5a5172ee9e1d7ec125d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rymanowski?= Date: Mon, 19 Aug 2019 11:38:47 +0200 Subject: [PATCH] nimble/mesh: Improve proxy handling when using BLE_EXT_ADV When using multiadvertising feature, mesh can more precisly say when remote connects to the proxy server. It is valuable when Node is using additional advertising instances for other purposes than mesh. --- nimble/host/mesh/include/mesh/glue.h | 14 +++++++++ nimble/host/mesh/src/glue.c | 14 --------- nimble/host/mesh/src/proxy.c | 43 +++++++++++++++++++++++----- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/nimble/host/mesh/include/mesh/glue.h b/nimble/host/mesh/include/mesh/glue.h index 09c64e187..59a8e5e94 100644 --- a/nimble/host/mesh/include/mesh/glue.h +++ b/nimble/host/mesh/include/mesh/glue.h @@ -206,6 +206,20 @@ static inline struct os_mbuf * NET_BUF_SIMPLE(uint16_t size) #define K_NO_WAIT (0) #define K_FOREVER (-1) +#if MYNEWT_VAL(BLE_EXT_ADV) +#define BT_MESH_ADV_INST (MYNEWT_VAL(BLE_MULTI_ADV_INSTANCES)) + +#if MYNEWT_VAL(BLE_MESH_PROXY) +/* Note that BLE_MULTI_ADV_INSTANCES contains number of additional instances. + * Instance 0 is always there + */ +#if MYNEWT_VAL(BLE_MULTI_ADV_INSTANCES) < 1 +#error "Mesh needs at least BLE_MULTI_ADV_INSTANCES set to 1" +#endif +#define BT_MESH_ADV_GATT_INST (MYNEWT_VAL(BLE_MULTI_ADV_INSTANCES) - 1) +#endif /* BLE_MESH_PROXY */ +#endif /* BLE_EXT_ADV */ + /* This is by purpose */ static inline void net_buf_simple_init(struct os_mbuf *buf, size_t reserve_head) diff --git a/nimble/host/mesh/src/glue.c b/nimble/host/mesh/src/glue.c index 96df0f8f0..2b6289806 100644 --- a/nimble/host/mesh/src/glue.c +++ b/nimble/host/mesh/src/glue.c @@ -29,20 +29,6 @@ #define BT_DBG_ENABLED (MYNEWT_VAL(BLE_MESH_DEBUG)) -#if MYNEWT_VAL(BLE_EXT_ADV) -#define BT_MESH_ADV_INST (MYNEWT_VAL(BLE_MULTI_ADV_INSTANCES)) - -#if MYNEWT_VAL(BLE_MESH_PROXY) -/* Note that BLE_MULTI_ADV_INSTANCES contains number of additional instances. - * Instance 0 is always there - */ -#if MYNEWT_VAL(BLE_MULTI_ADV_INSTANCES) < 1 -#error "Mesh needs at least BLE_MULTI_ADV_INSTANCES set to 1" -#endif -#define BT_MESH_ADV_GATT_INST (MYNEWT_VAL(BLE_MULTI_ADV_INSTANCES) - 1) -#endif /* BLE_MESH_PROXY */ -#endif /* BLE_EXT_ADV */ - extern u8_t g_mesh_addr_type; #if MYNEWT_VAL(BLE_EXT_ADV) diff --git a/nimble/host/mesh/src/proxy.c b/nimble/host/mesh/src/proxy.c index 609da54d2..f6ba1d892 100644 --- a/nimble/host/mesh/src/proxy.c +++ b/nimble/host/mesh/src/proxy.c @@ -654,10 +654,7 @@ static void proxy_connected(uint16_t conn_handle) static void proxy_disconnected(uint16_t conn_handle, int reason) { int i; - - BT_INFO("conn_handle %d reason %d", conn_handle, reason); - - conn_count--; + bool disconnected = false; for (i = 0; i < ARRAY_SIZE(clients); i++) { struct bt_mesh_proxy_client *client = &clients[i]; @@ -670,11 +667,16 @@ static void proxy_disconnected(uint16_t conn_handle, int reason) k_delayed_work_cancel(&client->sar_timer); client->conn_handle = BLE_HS_CONN_HANDLE_NONE; + conn_count--; + disconnected = true; break; } } - bt_mesh_adv_update(); + if (disconnected) { + BT_INFO("conn_handle %d reason %d", conn_handle, reason); + bt_mesh_adv_update(); + } } struct os_mbuf *bt_mesh_proxy_get_buf(void) @@ -1353,12 +1355,39 @@ void bt_mesh_proxy_adv_stop(void) } } -int -ble_mesh_proxy_gap_event(struct ble_gap_event *event, void *arg) +static void ble_mesh_handle_connect(struct ble_gap_event *event, void *arg) { +#if MYNEWT_VAL(BLE_EXT_ADV) + /* When EXT ADV is enabled then mesh proxy is connected + * when proxy advertising instance is completed. + * Therefore no need to handle BLE_GAP_EVENT_CONNECT + */ + if (event->type == BLE_GAP_EVENT_ADV_COMPLETE) { + /* Reason 0 means advertising has been completed because + * connection has been established + */ + if (event->adv_complete.reason != 0) { + return; + } + if (event->adv_complete.instance != BT_MESH_ADV_GATT_INST) { + return; + } + + proxy_connected(event->adv_complete.conn_handle); + } +#else if (event->type == BLE_GAP_EVENT_CONNECT) { proxy_connected(event->connect.conn_handle); + } +#endif +} + +int ble_mesh_proxy_gap_event(struct ble_gap_event *event, void *arg) +{ + if ((event->type == BLE_GAP_EVENT_CONNECT) || + (event->type == BLE_GAP_EVENT_ADV_COMPLETE)) { + ble_mesh_handle_connect(event, arg); } else if (event->type == BLE_GAP_EVENT_DISCONNECT) { proxy_disconnected(event->disconnect.conn.conn_handle, event->disconnect.reason);