diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index de6d3965b..5f515562e 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -347,6 +347,7 @@ if(CONFIG_BT_ENABLED) "esp_ble_mesh/mesh_common/mesh_atomic.c" "esp_ble_mesh/mesh_common/mesh_buf.c" "esp_ble_mesh/mesh_common/mesh_common.c" + "esp_ble_mesh/mesh_common/mesh_mutex.c" "esp_ble_mesh/mesh_common/mesh_timer.c" "esp_ble_mesh/mesh_common/mesh_util.c" "esp_ble_mesh/mesh_core/storage/settings_nvs.c" diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_common.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_common.h index e4274b0a1..1798148e1 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_common.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_common.h @@ -22,13 +22,9 @@ #include #include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/queue.h" -#include "freertos/semphr.h" - #include "esp_heap_caps.h" +#include "mesh_mutex.h" #include "mesh_access.h" #ifdef __cplusplus @@ -76,21 +72,6 @@ void bt_mesh_free_buf(struct net_buf_simple *buf); */ u8_t bt_mesh_get_device_role(struct bt_mesh_model *model, bool srv_send); -typedef struct { - SemaphoreHandle_t mutex; -#if CONFIG_SPIRAM_USE_MALLOC - StaticQueue_t *buffer; -#endif -} bt_mesh_mutex_t; - -void bt_mesh_mutex_create(bt_mesh_mutex_t *mutex); - -void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex); - -void bt_mesh_mutex_lock(bt_mesh_mutex_t *mutex); - -void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex); - #ifdef __cplusplus } #endif diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_mutex.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_mutex.h new file mode 100644 index 000000000..00287d0ec --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_mutex.h @@ -0,0 +1,63 @@ +// Copyright 2017-2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BLE_MESH_MUTEX_H_ +#define _BLE_MESH_MUTEX_H_ + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/queue.h" +#include "freertos/semphr.h" + +#include "mesh_types.h" +#include "mesh_slist.h" +#include "mesh_atomic.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + SemaphoreHandle_t mutex; +#if CONFIG_SPIRAM_USE_MALLOC + StaticQueue_t *buffer; +#endif +} bt_mesh_mutex_t; + +void bt_mesh_mutex_create(bt_mesh_mutex_t *mutex); +void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex); +void bt_mesh_mutex_lock(bt_mesh_mutex_t *mutex); +void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex); + +void bt_mesh_alarm_lock(void); +void bt_mesh_alarm_unlock(void); + +void bt_mesh_list_lock(void); +void bt_mesh_list_unlock(void); + +void bt_mesh_buf_lock(void); +void bt_mesh_buf_unlock(void); + +void bt_mesh_atomic_lock(void); +void bt_mesh_atomic_unlock(void); + +void bt_mesh_mutex_init(void); +void bt_mesh_mutex_deinit(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_MUTEX_H_ */ + diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_timer.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_timer.h index 94ca1ec67..6a9a2089a 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_timer.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_timer.h @@ -268,17 +268,8 @@ s64_t k_uptime_get(void); */ void k_sleep(s32_t duration); -void bt_mesh_list_lock(void); -void bt_mesh_list_unlock(void); - -void bt_mesh_buf_lock(void); -void bt_mesh_buf_unlock(void); - -void bt_mesh_atomic_lock(void); -void bt_mesh_atomic_unlock(void); - -void bt_mesh_k_init(void); -void bt_mesh_k_deinit(void); +void bt_mesh_timer_init(void); +void bt_mesh_timer_deinit(void); #ifdef __cplusplus } diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_atomic.c b/components/bt/esp_ble_mesh/mesh_common/mesh_atomic.c index 96bb70e8f..595b385ed 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_atomic.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_atomic.c @@ -19,7 +19,7 @@ */ #include "mesh_atomic.h" -#include "mesh_timer.h" +#include "mesh_mutex.h" #ifndef CONFIG_ATOMIC_OPERATIONS_BUILTIN diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c b/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c index b75d315de..3c1c31baa 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c @@ -9,7 +9,7 @@ #include "mesh_buf.h" #include "mesh_trace.h" -#include "mesh_timer.h" +#include "mesh_mutex.h" int net_buf_id(struct net_buf *buf) { diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_common.c b/components/bt/esp_ble_mesh/mesh_common/mesh_common.c index 5aa4a1325..8ab86266e 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_common.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_common.c @@ -65,62 +65,3 @@ u8_t bt_mesh_get_device_role(struct bt_mesh_model *model, bool srv_send) return client->msg_role; } - -void bt_mesh_mutex_create(bt_mesh_mutex_t *mutex) -{ - if (!mutex) { - BT_ERR("%s, Invalid mutex", __func__); - return; - } - -#if CONFIG_SPIRAM_USE_MALLOC - mutex->buffer = heap_caps_calloc(1, sizeof(StaticQueue_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); - __ASSERT(mutex->buffer, "%s, Failed to create queue buffer", __func__); - mutex->mutex = xSemaphoreCreateMutexStatic(mutex->buffer); - __ASSERT(mutex->mutex, "%s, Failed to create static mutex", __func__); -#else - mutex->mutex = xSemaphoreCreateMutex(); - __ASSERT(mutex->mutex, "%s, Failed to create mutex", __func__); -#endif -} - -void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex) -{ - if (!mutex) { - BT_ERR("%s, Invalid mutex", __func__); - return; - } - - if (mutex->mutex) { - vSemaphoreDelete(mutex->mutex); - mutex->mutex = NULL; -#if CONFIG_SPIRAM_USE_MALLOC - heap_caps_free(mutex->buffer); - mutex->buffer = NULL; -#endif - } -} - -void bt_mesh_mutex_lock(bt_mesh_mutex_t *mutex) -{ - if (!mutex) { - BT_ERR("%s, Invalid mutex", __func__); - return; - } - - if (mutex->mutex) { - xSemaphoreTake(mutex->mutex, portMAX_DELAY); - } -} - -void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex) -{ - if (!mutex) { - BT_ERR("%s, Invalid mutex", __func__); - return; - } - - if (mutex->mutex) { - xSemaphoreGive(mutex->mutex); - } -} diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c b/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c new file mode 100644 index 000000000..9d684c065 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c @@ -0,0 +1,183 @@ +// Copyright 2017-2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "mesh_mutex.h" + +static bt_mesh_mutex_t alarm_lock; +static bt_mesh_mutex_t list_lock; +static bt_mesh_mutex_t buf_lock; +static bt_mesh_mutex_t atomic_lock; + +void bt_mesh_mutex_create(bt_mesh_mutex_t *mutex) +{ + if (!mutex) { + BT_ERR("%s, Invalid mutex", __func__); + return; + } + +#if CONFIG_SPIRAM_USE_MALLOC + mutex->buffer = heap_caps_calloc(1, sizeof(StaticQueue_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); + __ASSERT(mutex->buffer, "%s, Failed to create queue buffer", __func__); + mutex->mutex = xSemaphoreCreateMutexStatic(mutex->buffer); + __ASSERT(mutex->mutex, "%s, Failed to create static mutex", __func__); +#else + mutex->mutex = xSemaphoreCreateMutex(); + __ASSERT(mutex->mutex, "%s, Failed to create mutex", __func__); +#endif +} + +void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex) +{ + if (!mutex) { + BT_ERR("%s, Invalid mutex", __func__); + return; + } + + if (mutex->mutex) { + vSemaphoreDelete(mutex->mutex); + mutex->mutex = NULL; +#if CONFIG_SPIRAM_USE_MALLOC + heap_caps_free(mutex->buffer); + mutex->buffer = NULL; +#endif + } +} + +void bt_mesh_mutex_lock(bt_mesh_mutex_t *mutex) +{ + if (!mutex) { + BT_ERR("%s, Invalid mutex", __func__); + return; + } + + if (mutex->mutex) { + xSemaphoreTake(mutex->mutex, portMAX_DELAY); + } +} + +void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex) +{ + if (!mutex) { + BT_ERR("%s, Invalid mutex", __func__); + return; + } + + if (mutex->mutex) { + xSemaphoreGive(mutex->mutex); + } +} + +static void bt_mesh_alarm_mutex_new(void) +{ + if (!alarm_lock.mutex) { + bt_mesh_mutex_create(&alarm_lock); + } +} + +static void bt_mesh_alarm_mutex_free(void) +{ + bt_mesh_mutex_free(&alarm_lock); +} + +void bt_mesh_alarm_lock(void) +{ + bt_mesh_mutex_lock(&alarm_lock); +} + +void bt_mesh_alarm_unlock(void) +{ + bt_mesh_mutex_unlock(&alarm_lock); +} + +static void bt_mesh_list_mutex_new(void) +{ + if (!list_lock.mutex) { + bt_mesh_mutex_create(&list_lock); + } +} + +static void bt_mesh_list_mutex_free(void) +{ + bt_mesh_mutex_free(&list_lock); +} + +void bt_mesh_list_lock(void) +{ + bt_mesh_mutex_lock(&list_lock); +} + +void bt_mesh_list_unlock(void) +{ + bt_mesh_mutex_unlock(&list_lock); +} + +static void bt_mesh_buf_mutex_new(void) +{ + if (!buf_lock.mutex) { + bt_mesh_mutex_create(&buf_lock); + } +} + +static void bt_mesh_buf_mutex_free(void) +{ + bt_mesh_mutex_free(&buf_lock); +} + +void bt_mesh_buf_lock(void) +{ + bt_mesh_mutex_lock(&buf_lock); +} + +void bt_mesh_buf_unlock(void) +{ + bt_mesh_mutex_unlock(&buf_lock); +} + +static void bt_mesh_atomic_mutex_new(void) +{ + if (!atomic_lock.mutex) { + bt_mesh_mutex_create(&atomic_lock); + } +} + +static void bt_mesh_atomic_mutex_free(void) +{ + bt_mesh_mutex_free(&atomic_lock); +} + +void bt_mesh_atomic_lock(void) +{ + bt_mesh_mutex_lock(&atomic_lock); +} + +void bt_mesh_atomic_unlock(void) +{ + bt_mesh_mutex_unlock(&atomic_lock); +} + +void bt_mesh_mutex_init(void) +{ + bt_mesh_alarm_mutex_new(); + bt_mesh_list_mutex_new(); + bt_mesh_buf_mutex_new(); + bt_mesh_atomic_mutex_new(); +} + +void bt_mesh_mutex_deinit(void) +{ + bt_mesh_alarm_mutex_free(); + bt_mesh_list_mutex_free(); + bt_mesh_buf_mutex_free(); + bt_mesh_atomic_mutex_free(); +} diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_timer.c b/components/bt/esp_ble_mesh/mesh_common/mesh_timer.c index 26c052ab4..b9df23563 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_timer.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_timer.c @@ -15,10 +15,6 @@ #include "mesh_common.h" #include "provisioner_prov.h" -static bt_mesh_mutex_t bm_alarm_lock; -static bt_mesh_mutex_t bm_list_lock; -static bt_mesh_mutex_t bm_buf_lock; -static bt_mesh_mutex_t bm_atomic_lock; static hash_map_t *bm_alarm_hash_map; static const size_t BLE_MESH_GENERAL_ALARM_HASH_MAP_SIZE = 20 + CONFIG_BLE_MESH_PBA_SAME_TIME + \ CONFIG_BLE_MESH_PBG_SAME_TIME; @@ -31,94 +27,6 @@ typedef struct alarm_t { int64_t deadline_us; } osi_alarm_t; -static void bt_mesh_alarm_mutex_new(void) -{ - if (!bm_alarm_lock.mutex) { - bt_mesh_mutex_create(&bm_alarm_lock); - } -} - -static void bt_mesh_alarm_mutex_free(void) -{ - bt_mesh_mutex_free(&bm_alarm_lock); -} - -static void bt_mesh_alarm_lock(void) -{ - bt_mesh_mutex_lock(&bm_alarm_lock); -} - -static void bt_mesh_alarm_unlock(void) -{ - bt_mesh_mutex_unlock(&bm_alarm_lock); -} - -static void bt_mesh_list_mutex_new(void) -{ - if (!bm_list_lock.mutex) { - bt_mesh_mutex_create(&bm_list_lock); - } -} - -static void bt_mesh_list_mutex_free(void) -{ - bt_mesh_mutex_free(&bm_list_lock); -} - -void bt_mesh_list_lock(void) -{ - bt_mesh_mutex_lock(&bm_list_lock); -} - -void bt_mesh_list_unlock(void) -{ - bt_mesh_mutex_unlock(&bm_list_lock); -} - -static void bt_mesh_buf_mutex_new(void) -{ - if (!bm_buf_lock.mutex) { - bt_mesh_mutex_create(&bm_buf_lock); - } -} - -static void bt_mesh_buf_mutex_free(void) -{ - bt_mesh_mutex_free(&bm_buf_lock); -} - -void bt_mesh_buf_lock(void) -{ - bt_mesh_mutex_lock(&bm_buf_lock); -} - -void bt_mesh_buf_unlock(void) -{ - bt_mesh_mutex_unlock(&bm_buf_lock); -} - -static void bt_mesh_atomic_mutex_new(void) -{ - if (!bm_atomic_lock.mutex) { - bt_mesh_mutex_create(&bm_atomic_lock); - } -} - -static void bt_mesh_atomic_mutex_free(void) -{ - bt_mesh_mutex_free(&bm_atomic_lock); -} - -void bt_mesh_atomic_lock(void) -{ - bt_mesh_mutex_lock(&bm_atomic_lock); -} - -void bt_mesh_atomic_unlock(void) -{ - bt_mesh_mutex_unlock(&bm_atomic_lock); -} - s64_t k_uptime_get(void) { /** k_uptime_get_32 is in in milliseconds, @@ -141,24 +49,16 @@ void k_sleep(s32_t duration) return; } -void bt_mesh_k_init(void) +void bt_mesh_timer_init(void) { - bt_mesh_alarm_mutex_new(); - bt_mesh_list_mutex_new(); - bt_mesh_buf_mutex_new(); - bt_mesh_atomic_mutex_new(); bm_alarm_hash_map = hash_map_new(BLE_MESH_GENERAL_ALARM_HASH_MAP_SIZE, hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL); __ASSERT(bm_alarm_hash_map, "%s, Failed to create hash map", __func__); } -void bt_mesh_k_deinit(void) +void bt_mesh_timer_deinit(void) { - bt_mesh_alarm_mutex_free(); - bt_mesh_list_mutex_free(); - bt_mesh_buf_mutex_free(); - bt_mesh_atomic_mutex_free(); if (bm_alarm_hash_map) { hash_map_free(bm_alarm_hash_map); bm_alarm_hash_map = NULL; diff --git a/components/bt/esp_ble_mesh/mesh_core/main.c b/components/bt/esp_ble_mesh/mesh_core/main.c index ff2a92f45..601a3c8d3 100644 --- a/components/bt/esp_ble_mesh/mesh_core/main.c +++ b/components/bt/esp_ble_mesh/mesh_core/main.c @@ -319,7 +319,9 @@ int bt_mesh_init(const struct bt_mesh_prov *prov, return -EALREADY; } - bt_mesh_k_init(); + bt_mesh_mutex_init(); + + bt_mesh_timer_init(); bt_mesh_hci_init(); @@ -485,7 +487,9 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param) bt_mesh_settings_deinit(); } - bt_mesh_k_deinit(); + bt_mesh_timer_deinit(); + + bt_mesh_mutex_deinit(); mesh_init = false; return 0; diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h index 05071fe82..d44a2a89e 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h @@ -22,22 +22,13 @@ extern "C" { #endif -#if !CONFIG_BLE_MESH_PROVISIONER - +#ifndef CONFIG_BLE_MESH_PBA_SAME_TIME #define CONFIG_BLE_MESH_PBA_SAME_TIME 0 +#endif + +#ifndef CONFIG_BLE_MESH_PBG_SAME_TIME #define CONFIG_BLE_MESH_PBG_SAME_TIME 0 - -#else - -#if !defined(CONFIG_BLE_MESH_PB_ADV) -#define CONFIG_BLE_MESH_PBA_SAME_TIME 0 -#endif /* !CONFIG_BLE_MESH_PB_ADV */ - -#if !defined(CONFIG_BLE_MESH_PB_GATT) -#define CONFIG_BLE_MESH_PBG_SAME_TIME 0 -#endif /* !CONFIG_BLE_MESH_PB_GATT */ - -#endif /* !CONFIG_BLE_MESH_PROVISIONER */ +#endif #define RM_AFTER_PROV BIT(0) #define START_PROV_NOW BIT(1)