From c93859c6596ad6e927d3a9823fb4f08c156c90fc Mon Sep 17 00:00:00 2001 From: alperen sener Date: Thu, 30 May 2024 12:52:56 +0200 Subject: [PATCH] bluetooth: mesh: fix lpn receive window timing error APIs for enabling and disabling the scanner may take larger time than expected and scheduling of the lpn state machine is affected by it. Since these type of latencies are factored into timeouts and scheduling times, we need to call scan enable/disable APIs after scheduling the next event time for LPN state machine to be more accurate. Signed-off-by: alperen sener --- subsys/bluetooth/mesh/lpn.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/subsys/bluetooth/mesh/lpn.c b/subsys/bluetooth/mesh/lpn.c index 0d14c9c55e5..a7e48e841d1 100644 --- a/subsys/bluetooth/mesh/lpn.c +++ b/subsys/bluetooth/mesh/lpn.c @@ -296,11 +296,11 @@ static void clear_friendship(bool force, bool disable) if (!disable) { lpn_set_state(BT_MESH_LPN_ENABLED); + k_work_reschedule(&lpn->timer, FRIEND_REQ_RETRY_TIMEOUT); + if (!IS_ENABLED(CONFIG_BT_MESH_LPN_ESTABLISHMENT)) { bt_mesh_scan_enable(); } - - k_work_reschedule(&lpn->timer, FRIEND_REQ_RETRY_TIMEOUT); } if (was_established) { @@ -570,7 +570,6 @@ static void friend_response_received(struct bt_mesh_lpn *lpn) lpn->fsn++; } - bt_mesh_scan_disable(); lpn_set_state(BT_MESH_LPN_ESTABLISHED); lpn->req_attempts = 0U; lpn->sent_req = 0U; @@ -581,6 +580,7 @@ static void friend_response_received(struct bt_mesh_lpn *lpn) int32_t timeout = poll_timeout(lpn); k_work_reschedule(&lpn->timer, K_MSEC(timeout)); + bt_mesh_scan_disable(); } void bt_mesh_lpn_msg_received(struct bt_mesh_net_rx *rx) @@ -857,9 +857,9 @@ static void update_timeout(struct bt_mesh_lpn *lpn) { if (lpn->established) { LOG_WRN("No response from Friend during ReceiveWindow"); - bt_mesh_scan_disable(); lpn_set_state(BT_MESH_LPN_ESTABLISHED); k_work_reschedule(&lpn->timer, K_MSEC(POLL_RETRY_TIMEOUT)); + bt_mesh_scan_disable(); } else { if (IS_ENABLED(CONFIG_BT_MESH_LPN_ESTABLISHMENT)) { bt_mesh_scan_disable(); @@ -903,19 +903,19 @@ static void lpn_timeout(struct k_work *work) send_friend_req(lpn); break; case BT_MESH_LPN_REQ_WAIT: - bt_mesh_scan_enable(); k_work_reschedule(&lpn->timer, K_MSEC(lpn->adv_duration + FRIEND_REQ_SCAN)); lpn_set_state(BT_MESH_LPN_WAIT_OFFER); + bt_mesh_scan_enable(); break; case BT_MESH_LPN_WAIT_OFFER: LOG_WRN("No acceptable Friend Offers received"); - if (IS_ENABLED(CONFIG_BT_MESH_LPN_ESTABLISHMENT)) { - bt_mesh_scan_disable(); - } - lpn_set_state(BT_MESH_LPN_ENABLED); lpn->sent_req = 0U; k_work_reschedule(&lpn->timer, FRIEND_REQ_RETRY_TIMEOUT); + + if (IS_ENABLED(CONFIG_BT_MESH_LPN_ESTABLISHMENT)) { + bt_mesh_scan_disable(); + } break; case BT_MESH_LPN_ESTABLISHED: if (lpn->req_attempts < REQ_ATTEMPTS(lpn)) { @@ -939,8 +939,8 @@ static void lpn_timeout(struct k_work *work) case BT_MESH_LPN_RECV_DELAY: k_work_reschedule(&lpn->timer, K_MSEC(SCAN_LATENCY + lpn->recv_win + RX_DELAY_CORRECTION(lpn))); - bt_mesh_scan_enable(); lpn_set_state(BT_MESH_LPN_WAIT_UPDATE); + bt_mesh_scan_enable(); break; case BT_MESH_LPN_WAIT_UPDATE: update_timeout(lpn);