From 13ab2f4015e3c3181a75dbba1c491e4a0d37170b Mon Sep 17 00:00:00 2001 From: lly Date: Tue, 3 Mar 2020 19:08:32 +0800 Subject: [PATCH] ble_mesh: Use lock for mesh timer operations --- .../bt/esp_ble_mesh/mesh_common/mesh_kernel.c | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c b/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c index d4597d818..26c052ab4 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c @@ -186,18 +186,21 @@ void k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler) } if (!hash_map_set(bm_alarm_hash_map, work, (void *)alarm)) { BT_ERR("%s Unable to add the timer to hash map.", __func__); + bt_mesh_alarm_unlock(); + return; } } - bt_mesh_alarm_unlock(); alarm = hash_map_get(bm_alarm_hash_map, work); if (alarm == NULL) { BT_WARN("%s, Unable to find expected alarm in hash map", __func__); + bt_mesh_alarm_unlock(); return; } // Just init the work timer only, don't start it. osi_alarm_cancel(alarm); + bt_mesh_alarm_unlock(); return; } @@ -208,15 +211,18 @@ int k_delayed_work_submit(struct k_delayed_work *work, s32_t delay) return -EINVAL; } + bt_mesh_alarm_lock(); osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { BT_WARN("%s, Unable to find expected alarm in hash map", __func__); + bt_mesh_alarm_unlock(); return -EINVAL; } // Cancel the alarm first, before start the alarm. osi_alarm_cancel(alarm); osi_alarm_set(alarm, delay); + bt_mesh_alarm_unlock(); return 0; } @@ -227,16 +233,18 @@ int k_delayed_work_submit_periodic(struct k_delayed_work *work, s32_t period) return -EINVAL; } + bt_mesh_alarm_lock(); osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { BT_WARN("%s, Unable to find expected alarm in hash map", __func__); + bt_mesh_alarm_unlock(); return -EINVAL; } /* Cancel the alarm first before starting it. */ osi_alarm_cancel(alarm); osi_alarm_set_periodic(alarm, period); - + bt_mesh_alarm_unlock(); return 0; } @@ -247,14 +255,17 @@ int k_delayed_work_cancel(struct k_delayed_work *work) return -EINVAL; } + bt_mesh_alarm_lock(); osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { BT_WARN("%s, Unable to find expected alarm in hash map", __func__); + bt_mesh_alarm_unlock(); return -EINVAL; } osi_alarm_cancel(alarm); alarm->deadline_us = 0; + bt_mesh_alarm_unlock(); return 0; } @@ -265,29 +276,38 @@ int k_delayed_work_free(struct k_delayed_work *work) return -EINVAL; } + bt_mesh_alarm_lock(); osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, work); if (alarm == NULL) { BT_WARN("%s Unable to find expected alarm in hash map", __func__); + bt_mesh_alarm_unlock(); return -EINVAL; } osi_alarm_cancel(alarm); hash_map_erase(bm_alarm_hash_map, work); + bt_mesh_alarm_unlock(); return 0; } s32_t k_delayed_work_remaining_get(struct k_delayed_work *work) { + s32_t time = 0; + if (!work || !bm_alarm_hash_map) { BT_ERR("%s, Invalid parameter", __func__); return 0; } + bt_mesh_alarm_lock(); osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { BT_WARN("%s Unable to find expected alarm in hash map", __func__); + bt_mesh_alarm_unlock(); return 0; } - return osi_alarm_get_remaining_ms(alarm); + time = osi_alarm_get_remaining_ms(alarm); + bt_mesh_alarm_unlock(); + return time; }