From a194c415a8fc3e6eab80b0dc3419d27f1bd94562 Mon Sep 17 00:00:00 2001 From: lly Date: Thu, 2 Jul 2020 10:53:07 +0800 Subject: [PATCH 1/4] ble_mesh: stack: Update option of using DRAM/SPIRAM for memory allocation --- components/bt/Kconfig | 35 ++++++++++++++++--- .../mesh_common/include/mesh_common.h | 14 ++++---- .../bt/esp_ble_mesh/mesh_common/mesh_common.c | 31 ++++++++++++++++ 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/components/bt/Kconfig b/components/bt/Kconfig index 8c9d53657..647c23e9e 100644 --- a/components/bt/Kconfig +++ b/components/bt/Kconfig @@ -1738,12 +1738,37 @@ if BLE_MESH option in the Bluetooth Controller section in menuconfig, which is "Scan Duplicate By Device Address and Advertising Data". - config BLE_MESH_ALLOC_FROM_PSRAM_FIRST - bool "BLE Mesh will first allocate memory from PSRAM" - default n + choice BLE_MESH_MEM_ALLOC_MODE + prompt "Memory allocation strategy" + default BLE_MESH_MEM_ALLOC_MODE_INTERNAL help - When this option is enabled, BLE Mesh stack will try to allocate memory - from PSRAM firstly. This will save the internal RAM if PSRAM exists. + Allocation strategy for BLE Mesh stack, essentially provides ability to + allocate all required dynamic allocations from, + + - Internal DRAM memory only + - External SPIRAM memory only + - Either internal or external memory based on default malloc() + behavior in ESP-IDF + + Recommended mode here is always internal, since that is most preferred + from security perspective. But if application requirement does not allow + sufficient free internal memory then alternate mode can be selected. + + config BLE_MESH_MEM_ALLOC_MODE_INTERNAL + bool "Internal DRAM" + + config BLE_MESH_ALLOC_FROM_PSRAM_FIRST + bool "External SPIRAM" + depends on SPIRAM_SUPPORT + + config BLE_MESH_MEM_ALLOC_MODE_DEFAULT + bool "Default alloc mode" + depends on SPIRAM_SUPPORT + help + Enable this option to use the default memory allocation strategy when + external SPIRAM is enabled. See the SPIRAM options for more details. + + endchoice # BLE_MESH_MEM_ALLOC_MODE config BLE_MESH_FAST_PROV bool "Enable BLE Mesh Fast Provisioning" 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 ad968dd0e..f78a55776 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,6 +22,7 @@ #include #include +#include "esp_attr.h" #include "esp_heap_caps.h" #include "mesh_byteorder.h" @@ -34,14 +35,11 @@ extern "C" { #endif -#if CONFIG_BLE_MESH_ALLOC_FROM_PSRAM_FIRST -#define bt_mesh_malloc(size) heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL) -#define bt_mesh_calloc(size) heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL) -#else -#define bt_mesh_malloc(size) malloc((size)) -#define bt_mesh_calloc(size) calloc(1, (size)) -#endif /* CONFIG_BLE_MESH_ALLOC_FROM_PSRAM_FIRST */ -#define bt_mesh_free(p) free((p)) +IRAM_ATTR void *bt_mesh_malloc(size_t size); + +IRAM_ATTR void *bt_mesh_calloc(size_t size); + +IRAM_ATTR void bt_mesh_free(void *ptr); /** * @brief This function allocates memory to store outgoing message. 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 8ab86266e..73a84e4a1 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_common.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_common.c @@ -19,6 +19,37 @@ #include "client_common.h" #include "mesh_common.h" +IRAM_ATTR void *bt_mesh_malloc(size_t size) +{ +#ifdef CONFIG_BLE_MESH_MEM_ALLOC_MODE_INTERNAL + return heap_caps_malloc(size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_BLE_MESH_ALLOC_FROM_PSRAM_FIRST + return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_IRAM_8BIT + return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#else + return malloc(size); +#endif +} + +IRAM_ATTR void *bt_mesh_calloc(size_t size) +{ +#ifdef CONFIG_BLE_MESH_MEM_ALLOC_MODE_INTERNAL + return heap_caps_calloc(1, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_BLE_MESH_ALLOC_FROM_PSRAM_FIRST + return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_IRAM_8BIT + return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#else + return calloc(1, size); +#endif +} + +IRAM_ATTR void bt_mesh_free(void *ptr) +{ + heap_caps_free(ptr); +} + struct net_buf_simple *bt_mesh_alloc_buf(u16_t size) { struct net_buf_simple *buf = NULL; From df8b2c5f3aa36e3f276147cfc0ed2f606f33ec16 Mon Sep 17 00:00:00 2001 From: lly Date: Fri, 3 Jul 2020 16:33:36 +0800 Subject: [PATCH 2/4] ble_mesh: stack: Make freertos static allocation visible --- components/bt/Kconfig | 28 ++++++ .../mesh_common/include/mesh_mutex.h | 2 +- .../bt/esp_ble_mesh/mesh_common/mesh_mutex.c | 22 +++-- components/bt/esp_ble_mesh/mesh_core/adv.c | 94 +++++++++++-------- 4 files changed, 98 insertions(+), 48 deletions(-) diff --git a/components/bt/Kconfig b/components/bt/Kconfig index 647c23e9e..882efb98e 100644 --- a/components/bt/Kconfig +++ b/components/bt/Kconfig @@ -1770,6 +1770,34 @@ if BLE_MESH endchoice # BLE_MESH_MEM_ALLOC_MODE + config BLE_MESH_FREERTOS_STATIC_ALLOC + bool "Enable FreeRTOS static allocation" + depends on SUPPORT_STATIC_ALLOCATION && SPIRAM_SUPPORT + default n + help + Enable this option to use FreeRTOS static allocation APIs for BLE Mesh, + which provides the ability to use different dynamic memory (i.e. SPIRAM) + for FreeRTOS objects. + If this option is disabled, the FreeRTOS static allocation APIs will not + be used, and internal DRAM will be allocated for FreeRTOS objects. + + choice BLE_MESH_FREERTOS_STATIC_ALLOC_MODE + prompt "Memory allocation for FreeRTOS objects" + depends on BLE_MESH_FREERTOS_STATIC_ALLOC + help + Choose the memory to be used for FreeRTOS objects. + + config BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL + bool "External SPIRAM" + depends on SPIRAM_SUPPORT + help + If enabled, BLE Mesh allocates dynamic memory from external SPIRAM for + FreeRTOS objects, i.e. mutex, queue, and task stack. External SPIRAM + can only be used for task stack when SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY + is enabled. See the SPIRAM options for more details. + + endchoice # BLE_MESH_FREERTOS_STATIC_ALLOC_MODE + config BLE_MESH_FAST_PROV bool "Enable BLE Mesh Fast Provisioning" select BLE_MESH_NODE 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 index 488e010fa..faded20d3 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_mutex.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_mutex.h @@ -25,7 +25,7 @@ extern "C" { typedef struct { SemaphoreHandle_t mutex; -#if CONFIG_SPIRAM_USE_MALLOC +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC StaticQueue_t *buffer; #endif } bt_mesh_mutex_t; diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c b/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c index 1bd6a2358..3f673209d 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c @@ -26,15 +26,19 @@ void bt_mesh_mutex_create(bt_mesh_mutex_t *mutex) 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__); +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL + mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT + mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); #endif + __ASSERT(mutex->buffer, "Failed to create mutex buffer"); + mutex->mutex = xSemaphoreCreateMutexStatic(mutex->buffer); + __ASSERT(mutex->mutex, "Failed to create static mutex"); +#else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ + mutex->mutex = xSemaphoreCreateMutex(); + __ASSERT(mutex->mutex, "Failed to create mutex"); +#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ } void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex) @@ -47,7 +51,7 @@ void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex) if (mutex->mutex) { vSemaphoreDelete(mutex->mutex); mutex->mutex = NULL; -#if CONFIG_SPIRAM_USE_MALLOC +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC heap_caps_free(mutex->buffer); mutex->buffer = NULL; #endif diff --git a/components/bt/esp_ble_mesh/mesh_core/adv.c b/components/bt/esp_ble_mesh/mesh_core/adv.c index 4e1d94849..7b10f9012 100644 --- a/components/bt/esp_ble_mesh/mesh_core/adv.c +++ b/components/bt/esp_ble_mesh/mesh_core/adv.c @@ -63,7 +63,7 @@ static struct bt_mesh_adv adv_pool[CONFIG_BLE_MESH_ADV_BUF_COUNT]; struct bt_mesh_queue { QueueHandle_t queue; -#if CONFIG_SPIRAM_USE_MALLOC +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC StaticQueue_t *buffer; u8_t *storage; #endif @@ -121,7 +121,9 @@ static void bt_mesh_ble_adv_deinit(void); struct bt_mesh_adv_task { TaskHandle_t handle; -#if CONFIG_SPIRAM_USE_MALLOC +#if (CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && \ + CONFIG_SPIRAM_CACHE_WORKAROUND && \ + CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY) StaticTask_t *task; StackType_t *stack; #endif @@ -795,54 +797,68 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, s8_t rssi, void bt_mesh_adv_init(void) { -#if !CONFIG_SPIRAM_USE_MALLOC +#if !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC xBleMeshQueue.queue = xQueueCreate(BLE_MESH_QUEUE_SIZE, sizeof(bt_mesh_msg_t)); - __ASSERT(xBleMeshQueue.queue, "%s, Failed to create queue", __func__); -#else - xBleMeshQueue.buffer = heap_caps_calloc(1, sizeof(StaticQueue_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); - __ASSERT(xBleMeshQueue.buffer, "%s, Failed to create queue buffer", __func__); - xBleMeshQueue.storage = heap_caps_calloc(1, (BLE_MESH_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); - __ASSERT(xBleMeshQueue.storage, "%s, Failed to create queue storage", __func__); - xBleMeshQueue.queue = xQueueCreateStatic(BLE_MESH_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)xBleMeshQueue.storage, xBleMeshQueue.buffer); - __ASSERT(xBleMeshQueue.queue, "%s, Failed to create static queue", __func__); + __ASSERT(xBleMeshQueue.queue, "Failed to create queue"); +#else /* !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL + xBleMeshQueue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT + xBleMeshQueue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); #endif + __ASSERT(xBleMeshQueue.buffer, "Failed to create queue buffer"); +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL + xBleMeshQueue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT + xBleMeshQueue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#endif + __ASSERT(xBleMeshQueue.storage, "Failed to create queue storage"); + xBleMeshQueue.queue = xQueueCreateStatic(BLE_MESH_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)xBleMeshQueue.storage, xBleMeshQueue.buffer); + __ASSERT(xBleMeshQueue.queue, "Failed to create static queue"); +#endif /* !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) -#if !CONFIG_SPIRAM_USE_MALLOC +#if !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC xBleMeshRelayQueue.queue = xQueueCreate(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t)); - __ASSERT(xBleMeshRelayQueue.queue, "%s, Failed to create relay queue", __func__); -#else - xBleMeshRelayQueue.buffer = heap_caps_calloc(1, sizeof(StaticQueue_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); - __ASSERT(xBleMeshRelayQueue.buffer, "%s, Failed to create relay queue buffer", __func__); - xBleMeshRelayQueue.storage = heap_caps_calloc(1, (BLE_MESH_RELAY_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); - __ASSERT(xBleMeshRelayQueue.storage, "%s, Failed to create relay queue storage", __func__); - xBleMeshRelayQueue.queue = xQueueCreateStatic(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)xBleMeshRelayQueue.storage, xBleMeshRelayQueue.buffer); - __ASSERT(xBleMeshRelayQueue.queue, "%s, Failed to create static relay queue", __func__); + __ASSERT(xBleMeshRelayQueue.queue, "Failed to create relay queue"); +#else /* !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL + xBleMeshRelayQueue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT + xBleMeshRelayQueue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); #endif + __ASSERT(xBleMeshRelayQueue.buffer, "Failed to create relay queue buffer"); +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL + xBleMeshRelayQueue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_RELAY_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT + xBleMeshRelayQueue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_RELAY_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#endif + __ASSERT(xBleMeshRelayQueue.storage, "Failed to create relay queue storage"); + xBleMeshRelayQueue.queue = xQueueCreateStatic(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)xBleMeshRelayQueue.storage, xBleMeshRelayQueue.buffer); + __ASSERT(xBleMeshRelayQueue.queue, "Failed to create static relay queue"); +#endif /* !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ xBleMeshQueueSet = xQueueCreateSet(BLE_MESH_QUEUE_SET_SIZE); - __ASSERT(xBleMeshQueueSet, "%s, Failed to create queue set", __func__); + __ASSERT(xBleMeshQueueSet, "Failed to create queue set"); xQueueAddToSet(xBleMeshQueue.queue, xBleMeshQueueSet); xQueueAddToSet(xBleMeshRelayQueue.queue, xBleMeshQueueSet); #endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ -#if !CONFIG_SPIRAM_USE_MALLOC - int ret = xTaskCreatePinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", BLE_MESH_ADV_TASK_STACK_SIZE, NULL, - configMAX_PRIORITIES - 5, &adv_task.handle, BLE_MESH_ADV_TASK_CORE); - __ASSERT(ret == pdTRUE, "%s, Failed to create adv thread", __func__); -#else +#if (CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && \ + CONFIG_SPIRAM_CACHE_WORKAROUND && \ + CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY) adv_task.task = heap_caps_calloc(1, sizeof(StaticTask_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); - __ASSERT(adv_task.task, "%s, Failed to create adv thread task", __func__); -#if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY - adv_task.stack = heap_caps_calloc(1, BLE_MESH_ADV_TASK_STACK_SIZE * sizeof(StackType_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); -#else - adv_task.stack = heap_caps_calloc(1, BLE_MESH_ADV_TASK_STACK_SIZE * sizeof(StackType_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); -#endif - __ASSERT(adv_task.stack, "%s, Failed to create adv thread stack", __func__); + __ASSERT(adv_task.task, "Failed to create adv thread task"); + adv_task.stack = heap_caps_calloc_prefer(1, BLE_MESH_ADV_TASK_STACK_SIZE * sizeof(StackType_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + __ASSERT(adv_task.stack, "Failed to create adv thread stack"); adv_task.handle = xTaskCreateStaticPinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", BLE_MESH_ADV_TASK_STACK_SIZE, NULL, configMAX_PRIORITIES - 5, adv_task.stack, adv_task.task, BLE_MESH_ADV_TASK_CORE); - __ASSERT(adv_task.handle, "%s, Failed to create static adv thread", __func__); -#endif + __ASSERT(adv_task.handle, "Failed to create static adv thread"); +#else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && CONFIG_SPIRAM_CACHE_WORKAROUND && CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY */ + int ret = xTaskCreatePinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", BLE_MESH_ADV_TASK_STACK_SIZE, NULL, + configMAX_PRIORITIES - 5, &adv_task.handle, BLE_MESH_ADV_TASK_CORE); + __ASSERT(ret == pdTRUE, "Failed to create adv thread"); +#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && CONFIG_SPIRAM_CACHE_WORKAROUND && CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY */ } void bt_mesh_adv_deinit(void) @@ -853,7 +869,9 @@ void bt_mesh_adv_deinit(void) vTaskDelete(adv_task.handle); adv_task.handle = NULL; -#if CONFIG_SPIRAM_USE_MALLOC +#if (CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && \ + CONFIG_SPIRAM_CACHE_WORKAROUND && \ + CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY) heap_caps_free(adv_task.stack); adv_task.stack = NULL; heap_caps_free(adv_task.task); @@ -866,7 +884,7 @@ void bt_mesh_adv_deinit(void) vQueueDelete(xBleMeshRelayQueue.queue); xBleMeshRelayQueue.queue = NULL; -#if CONFIG_SPIRAM_USE_MALLOC +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC heap_caps_free(xBleMeshRelayQueue.buffer); xBleMeshRelayQueue.buffer = NULL; heap_caps_free(xBleMeshRelayQueue.storage); @@ -882,7 +900,7 @@ void bt_mesh_adv_deinit(void) vQueueDelete(xBleMeshQueue.queue); xBleMeshQueue.queue = NULL; -#if CONFIG_SPIRAM_USE_MALLOC +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC heap_caps_free(xBleMeshQueue.buffer); xBleMeshQueue.buffer = NULL; heap_caps_free(xBleMeshQueue.storage); From da068a639fa32f57a4a1d3454d058b6baa778bfb Mon Sep 17 00:00:00 2001 From: lly Date: Fri, 3 Jul 2020 16:56:46 +0800 Subject: [PATCH 3/4] ble_mesh: stack: Use macros for adv task name & prio --- .../bt/esp_ble_mesh/mesh_common/include/mesh_kernel.h | 3 +++ components/bt/esp_ble_mesh/mesh_core/adv.c | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_kernel.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_kernel.h index 1a6baee71..eef953eb3 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_kernel.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_kernel.h @@ -13,6 +13,7 @@ #include "freertos/queue.h" #include "freertos/semphr.h" +#include "sdkconfig.h" #include "mesh_types.h" #ifdef __cplusplus @@ -36,6 +37,8 @@ extern "C" { #endif #define BLE_MESH_ADV_TASK_STACK_SIZE 3072 +#define BLE_MESH_ADV_TASK_NAME "mesh_adv_task" +#define BLE_MESH_ADV_TASK_PRIO (configMAX_PRIORITIES - 5) /** * @brief Put the current thread to sleep. diff --git a/components/bt/esp_ble_mesh/mesh_core/adv.c b/components/bt/esp_ble_mesh/mesh_core/adv.c index 7b10f9012..e08494990 100644 --- a/components/bt/esp_ble_mesh/mesh_core/adv.c +++ b/components/bt/esp_ble_mesh/mesh_core/adv.c @@ -851,12 +851,12 @@ void bt_mesh_adv_init(void) __ASSERT(adv_task.task, "Failed to create adv thread task"); adv_task.stack = heap_caps_calloc_prefer(1, BLE_MESH_ADV_TASK_STACK_SIZE * sizeof(StackType_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); __ASSERT(adv_task.stack, "Failed to create adv thread stack"); - adv_task.handle = xTaskCreateStaticPinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", BLE_MESH_ADV_TASK_STACK_SIZE, NULL, - configMAX_PRIORITIES - 5, adv_task.stack, adv_task.task, BLE_MESH_ADV_TASK_CORE); + adv_task.handle = xTaskCreateStaticPinnedToCore(adv_thread, BLE_MESH_ADV_TASK_NAME, BLE_MESH_ADV_TASK_STACK_SIZE, NULL, + BLE_MESH_ADV_TASK_PRIO, adv_task.stack, adv_task.task, BLE_MESH_ADV_TASK_CORE); __ASSERT(adv_task.handle, "Failed to create static adv thread"); #else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && CONFIG_SPIRAM_CACHE_WORKAROUND && CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY */ - int ret = xTaskCreatePinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", BLE_MESH_ADV_TASK_STACK_SIZE, NULL, - configMAX_PRIORITIES - 5, &adv_task.handle, BLE_MESH_ADV_TASK_CORE); + int ret = xTaskCreatePinnedToCore(adv_thread, BLE_MESH_ADV_TASK_NAME, BLE_MESH_ADV_TASK_STACK_SIZE, NULL, + BLE_MESH_ADV_TASK_PRIO, &adv_task.handle, BLE_MESH_ADV_TASK_CORE); __ASSERT(ret == pdTRUE, "Failed to create adv thread"); #endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && CONFIG_SPIRAM_CACHE_WORKAROUND && CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY */ } From 2a6b0d68ce94158d3143b40fb846451a4aef4006 Mon Sep 17 00:00:00 2001 From: lly Date: Fri, 3 Jul 2020 18:23:46 +0800 Subject: [PATCH 4/4] ble_mesh: stack: Rename mesh adv queue and relay queue --- components/bt/esp_ble_mesh/mesh_core/adv.c | 174 ++++++++++----------- 1 file changed, 84 insertions(+), 90 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/adv.c b/components/bt/esp_ble_mesh/mesh_core/adv.c index e08494990..c43bbc2d9 100644 --- a/components/bt/esp_ble_mesh/mesh_core/adv.c +++ b/components/bt/esp_ble_mesh/mesh_core/adv.c @@ -27,25 +27,19 @@ #include "mesh_bearer_adapt.h" /* Convert from ms to 0.625ms units */ -#define ADV_SCAN_UNIT(_ms) ((_ms) * 8 / 5) +#define ADV_SCAN_UNIT(_ms) ((_ms) * 8 / 5) /* Convert from 0.625ms units to interval(ms) */ -#define ADV_SCAN_INT(val) ((val) * 5 / 8) +#define ADV_SCAN_INT(val) ((val) * 5 / 8) /* Window and Interval are equal for continuous scanning */ -#define MESH_SCAN_INTERVAL 0x20 -#define MESH_SCAN_WINDOW 0x20 +#define MESH_SCAN_INTERVAL 0x20 +#define MESH_SCAN_WINDOW 0x20 /* Pre-5.0 controllers enforce a minimum interval of 100ms * whereas 5.0+ controllers can go down to 20ms. */ -#define ADV_INT_DEFAULT_MS 100 -#define ADV_INT_FAST_MS 20 - -#if defined(CONFIG_BT_HOST_CRYPTO) -#define ADV_STACK_SIZE 1024 -#else -#define ADV_STACK_SIZE 768 -#endif +#define ADV_INT_DEFAULT_MS 100 +#define ADV_INT_FAST_MS 20 static const bt_mesh_addr_t *dev_addr; @@ -62,19 +56,19 @@ NET_BUF_POOL_DEFINE(adv_buf_pool, CONFIG_BLE_MESH_ADV_BUF_COUNT, static struct bt_mesh_adv adv_pool[CONFIG_BLE_MESH_ADV_BUF_COUNT]; struct bt_mesh_queue { - QueueHandle_t queue; + QueueHandle_t handle; #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC StaticQueue_t *buffer; u8_t *storage; #endif }; -static struct bt_mesh_queue xBleMeshQueue; -/* We reserve one queue for bt_mesh_adv_update() */ +static struct bt_mesh_queue adv_queue; +/* We reserve one queue item for bt_mesh_adv_update() */ #if CONFIG_BLE_MESH_SUPPORT_BLE_ADV -#define BLE_MESH_QUEUE_SIZE (CONFIG_BLE_MESH_ADV_BUF_COUNT + CONFIG_BLE_MESH_BLE_ADV_BUF_COUNT + 1) +#define BLE_MESH_ADV_QUEUE_SIZE (CONFIG_BLE_MESH_ADV_BUF_COUNT + CONFIG_BLE_MESH_BLE_ADV_BUF_COUNT + 1) #else -#define BLE_MESH_QUEUE_SIZE (CONFIG_BLE_MESH_ADV_BUF_COUNT + 1) +#define BLE_MESH_ADV_QUEUE_SIZE (CONFIG_BLE_MESH_ADV_BUF_COUNT + 1) #endif #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) @@ -83,11 +77,11 @@ NET_BUF_POOL_DEFINE(relay_adv_buf_pool, CONFIG_BLE_MESH_RELAY_ADV_BUF_COUNT, static struct bt_mesh_adv relay_adv_pool[CONFIG_BLE_MESH_RELAY_ADV_BUF_COUNT]; -static struct bt_mesh_queue xBleMeshRelayQueue; +static struct bt_mesh_queue relay_queue; #define BLE_MESH_RELAY_QUEUE_SIZE CONFIG_BLE_MESH_RELAY_ADV_BUF_COUNT -static QueueSetHandle_t xBleMeshQueueSet; -#define BLE_MESH_QUEUE_SET_SIZE (BLE_MESH_QUEUE_SIZE + BLE_MESH_RELAY_QUEUE_SIZE) +static QueueSetHandle_t mesh_queue_set; +#define BLE_MESH_QUEUE_SET_SIZE (BLE_MESH_ADV_QUEUE_SIZE + BLE_MESH_RELAY_QUEUE_SIZE) #define BLE_MESH_RELAY_TIME_INTERVAL K_SECONDS(6) #define BLE_MESH_MAX_TIME_INTERVAL 0xFFFFFFFF @@ -260,28 +254,28 @@ static void adv_thread(void *p) #if !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) #if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ CONFIG_BLE_MESH_GATT_PROXY_SERVER - xQueueReceive(xBleMeshQueue.queue, &msg, K_NO_WAIT); + xQueueReceive(adv_queue.handle, &msg, K_NO_WAIT); while (!(*buf)) { s32_t timeout; BT_DBG("Mesh Proxy Advertising start"); timeout = bt_mesh_proxy_adv_start(); BT_DBG("Mesh Proxy Advertising up to %d ms", timeout); - xQueueReceive(xBleMeshQueue.queue, &msg, timeout); + xQueueReceive(adv_queue.handle, &msg, timeout); BT_DBG("Mesh Proxy Advertising stop"); bt_mesh_proxy_adv_stop(); } #else - xQueueReceive(xBleMeshQueue.queue, &msg, portMAX_DELAY); + xQueueReceive(adv_queue.handle, &msg, portMAX_DELAY); #endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ #else /* !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ #if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ CONFIG_BLE_MESH_GATT_PROXY_SERVER - handle = xQueueSelectFromSet(xBleMeshQueueSet, K_NO_WAIT); + handle = xQueueSelectFromSet(mesh_queue_set, K_NO_WAIT); if (handle) { - if (uxQueueMessagesWaiting(xBleMeshQueue.queue)) { - xQueueReceive(xBleMeshQueue.queue, &msg, K_NO_WAIT); - } else if (uxQueueMessagesWaiting(xBleMeshRelayQueue.queue)) { - xQueueReceive(xBleMeshRelayQueue.queue, &msg, K_NO_WAIT); + if (uxQueueMessagesWaiting(adv_queue.handle)) { + xQueueReceive(adv_queue.handle, &msg, K_NO_WAIT); + } else if (uxQueueMessagesWaiting(relay_queue.handle)) { + xQueueReceive(relay_queue.handle, &msg, K_NO_WAIT); } } else { while (!(*buf)) { @@ -289,25 +283,25 @@ static void adv_thread(void *p) BT_DBG("Mesh Proxy Advertising start"); timeout = bt_mesh_proxy_adv_start(); BT_DBG("Mesh Proxy Advertising up to %d ms", timeout); - handle = xQueueSelectFromSet(xBleMeshQueueSet, timeout); + handle = xQueueSelectFromSet(mesh_queue_set, timeout); BT_DBG("Mesh Proxy Advertising stop"); bt_mesh_proxy_adv_stop(); if (handle) { - if (uxQueueMessagesWaiting(xBleMeshQueue.queue)) { - xQueueReceive(xBleMeshQueue.queue, &msg, K_NO_WAIT); - } else if (uxQueueMessagesWaiting(xBleMeshRelayQueue.queue)) { - xQueueReceive(xBleMeshRelayQueue.queue, &msg, K_NO_WAIT); + if (uxQueueMessagesWaiting(adv_queue.handle)) { + xQueueReceive(adv_queue.handle, &msg, K_NO_WAIT); + } else if (uxQueueMessagesWaiting(relay_queue.handle)) { + xQueueReceive(relay_queue.handle, &msg, K_NO_WAIT); } } } } #else - handle = xQueueSelectFromSet(xBleMeshQueueSet, portMAX_DELAY); + handle = xQueueSelectFromSet(mesh_queue_set, portMAX_DELAY); if (handle) { - if (uxQueueMessagesWaiting(xBleMeshQueue.queue)) { - xQueueReceive(xBleMeshQueue.queue, &msg, K_NO_WAIT); - } else if (uxQueueMessagesWaiting(xBleMeshRelayQueue.queue)) { - xQueueReceive(xBleMeshRelayQueue.queue, &msg, K_NO_WAIT); + if (uxQueueMessagesWaiting(adv_queue.handle)) { + xQueueReceive(adv_queue.handle, &msg, K_NO_WAIT); + } else if (uxQueueMessagesWaiting(relay_queue.handle)) { + xQueueReceive(relay_queue.handle, &msg, K_NO_WAIT); } } #endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ @@ -448,18 +442,18 @@ static void bt_mesh_task_post(bt_mesh_msg_t *msg, uint32_t timeout, bool front) { BT_DBG("%s", __func__); - if (xBleMeshQueue.queue == NULL) { + if (adv_queue.handle == NULL) { BT_ERR("%s, Invalid queue", __func__); return; } if (front) { - if (xQueueSendToFront(xBleMeshQueue.queue, msg, timeout) != pdTRUE) { + if (xQueueSendToFront(adv_queue.handle, msg, timeout) != pdTRUE) { BT_ERR("%s, Failed to send item to queue front", __func__); bt_mesh_unref_buf(msg); } } else { - if (xQueueSend(xBleMeshQueue.queue, msg, timeout) != pdTRUE) { + if (xQueueSend(adv_queue.handle, msg, timeout) != pdTRUE) { BT_ERR("%s, Failed to send item to queue back", __func__); bt_mesh_unref_buf(msg); } @@ -532,12 +526,12 @@ static void ble_mesh_relay_task_post(bt_mesh_msg_t *msg, uint32_t timeout) BT_DBG("%s", __func__); - if (xBleMeshRelayQueue.queue == NULL) { + if (relay_queue.handle == NULL) { BT_ERR("%s, Invalid relay queue", __func__); return; } - if (xQueueSend(xBleMeshRelayQueue.queue, msg, timeout) == pdTRUE) { + if (xQueueSend(relay_queue.handle, msg, timeout) == pdTRUE) { return; } @@ -545,11 +539,11 @@ static void ble_mesh_relay_task_post(bt_mesh_msg_t *msg, uint32_t timeout) * If failed to send packet to the relay queue(queue is full), we will * remove the oldest packet in the queue and put the new one into it. */ - handle = xQueueSelectFromSet(xBleMeshQueueSet, K_NO_WAIT); - if (handle && uxQueueMessagesWaiting(xBleMeshRelayQueue.queue)) { + handle = xQueueSelectFromSet(mesh_queue_set, K_NO_WAIT); + if (handle && uxQueueMessagesWaiting(relay_queue.handle)) { BT_INFO("%s, Full queue, remove the oldest relay packet", __func__); /* Remove the oldest relay packet from queue */ - if (xQueueReceive(xBleMeshRelayQueue.queue, &old_msg, K_NO_WAIT) != pdTRUE) { + if (xQueueReceive(relay_queue.handle, &old_msg, K_NO_WAIT) != pdTRUE) { BT_ERR("%s, Failed to remove item from queue", __func__); bt_mesh_unref_buf(msg); return; @@ -557,7 +551,7 @@ static void ble_mesh_relay_task_post(bt_mesh_msg_t *msg, uint32_t timeout) /* Unref buf used for the oldest relay packet */ bt_mesh_unref_buf(&old_msg); /* Send the latest relay packet to queue */ - if (xQueueSend(xBleMeshRelayQueue.queue, msg, K_NO_WAIT) != pdTRUE) { + if (xQueueSend(relay_queue.handle, msg, K_NO_WAIT) != pdTRUE) { BT_ERR("%s, Failed to send item to relay queue", __func__); bt_mesh_unref_buf(msg); return; @@ -586,13 +580,13 @@ void bt_mesh_relay_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *c msg.src = src; msg.dst = dst; msg.timestamp = k_uptime_get_32(); - /* Use K_NO_WAIT here, if xBleMeshRelayQueue is full return immediately */ + /* Use K_NO_WAIT here, if relay_queue is full return immediately */ ble_mesh_relay_task_post(&msg, K_NO_WAIT); } u16_t bt_mesh_get_stored_relay_count(void) { - return (u16_t)uxQueueMessagesWaiting(xBleMeshRelayQueue.queue); + return (u16_t)uxQueueMessagesWaiting(relay_queue.handle); } #endif /* #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ @@ -798,50 +792,50 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, s8_t rssi, void bt_mesh_adv_init(void) { #if !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC - xBleMeshQueue.queue = xQueueCreate(BLE_MESH_QUEUE_SIZE, sizeof(bt_mesh_msg_t)); - __ASSERT(xBleMeshQueue.queue, "Failed to create queue"); + adv_queue.handle = xQueueCreate(BLE_MESH_ADV_QUEUE_SIZE, sizeof(bt_mesh_msg_t)); + __ASSERT(adv_queue.handle, "Failed to create queue"); #else /* !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL - xBleMeshQueue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + adv_queue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); #elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT - xBleMeshQueue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + adv_queue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); #endif - __ASSERT(xBleMeshQueue.buffer, "Failed to create queue buffer"); + __ASSERT(adv_queue.buffer, "Failed to create queue buffer"); #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL - xBleMeshQueue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + adv_queue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_ADV_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); #elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT - xBleMeshQueue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + adv_queue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_ADV_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); #endif - __ASSERT(xBleMeshQueue.storage, "Failed to create queue storage"); - xBleMeshQueue.queue = xQueueCreateStatic(BLE_MESH_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)xBleMeshQueue.storage, xBleMeshQueue.buffer); - __ASSERT(xBleMeshQueue.queue, "Failed to create static queue"); + __ASSERT(adv_queue.storage, "Failed to create queue storage"); + adv_queue.handle = xQueueCreateStatic(BLE_MESH_ADV_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)adv_queue.storage, adv_queue.buffer); + __ASSERT(adv_queue.handle, "Failed to create static queue"); #endif /* !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) #if !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC - xBleMeshRelayQueue.queue = xQueueCreate(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t)); - __ASSERT(xBleMeshRelayQueue.queue, "Failed to create relay queue"); + relay_queue.handle = xQueueCreate(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t)); + __ASSERT(relay_queue.handle, "Failed to create relay queue"); #else /* !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL - xBleMeshRelayQueue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + relay_queue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); #elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT - xBleMeshRelayQueue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + relay_queue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); #endif - __ASSERT(xBleMeshRelayQueue.buffer, "Failed to create relay queue buffer"); + __ASSERT(relay_queue.buffer, "Failed to create relay queue buffer"); #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL - xBleMeshRelayQueue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_RELAY_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + relay_queue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_RELAY_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); #elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT - xBleMeshRelayQueue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_RELAY_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + relay_queue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_RELAY_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); #endif - __ASSERT(xBleMeshRelayQueue.storage, "Failed to create relay queue storage"); - xBleMeshRelayQueue.queue = xQueueCreateStatic(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)xBleMeshRelayQueue.storage, xBleMeshRelayQueue.buffer); - __ASSERT(xBleMeshRelayQueue.queue, "Failed to create static relay queue"); + __ASSERT(relay_queue.storage, "Failed to create relay queue storage"); + relay_queue.handle = xQueueCreateStatic(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)relay_queue.storage, relay_queue.buffer); + __ASSERT(relay_queue.handle, "Failed to create static relay queue"); #endif /* !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ - xBleMeshQueueSet = xQueueCreateSet(BLE_MESH_QUEUE_SET_SIZE); - __ASSERT(xBleMeshQueueSet, "Failed to create queue set"); - xQueueAddToSet(xBleMeshQueue.queue, xBleMeshQueueSet); - xQueueAddToSet(xBleMeshRelayQueue.queue, xBleMeshQueueSet); + mesh_queue_set = xQueueCreateSet(BLE_MESH_QUEUE_SET_SIZE); + __ASSERT(mesh_queue_set, "Failed to create queue set"); + xQueueAddToSet(adv_queue.handle, mesh_queue_set); + xQueueAddToSet(relay_queue.handle, mesh_queue_set); #endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ #if (CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && \ @@ -863,7 +857,7 @@ void bt_mesh_adv_init(void) void bt_mesh_adv_deinit(void) { - if (xBleMeshQueue.queue == NULL) { + if (adv_queue.handle == NULL) { return; } @@ -879,32 +873,32 @@ void bt_mesh_adv_deinit(void) #endif #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) - xQueueRemoveFromSet(xBleMeshQueue.queue, xBleMeshQueueSet); - xQueueRemoveFromSet(xBleMeshRelayQueue.queue, xBleMeshQueueSet); + xQueueRemoveFromSet(adv_queue.handle, mesh_queue_set); + xQueueRemoveFromSet(relay_queue.handle, mesh_queue_set); - vQueueDelete(xBleMeshRelayQueue.queue); - xBleMeshRelayQueue.queue = NULL; + vQueueDelete(relay_queue.handle); + relay_queue.handle = NULL; #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC - heap_caps_free(xBleMeshRelayQueue.buffer); - xBleMeshRelayQueue.buffer = NULL; - heap_caps_free(xBleMeshRelayQueue.storage); - xBleMeshRelayQueue.storage = NULL; + heap_caps_free(relay_queue.buffer); + relay_queue.buffer = NULL; + heap_caps_free(relay_queue.storage); + relay_queue.storage = NULL; #endif bt_mesh_unref_buf_from_pool(&relay_adv_buf_pool); memset(relay_adv_pool, 0, sizeof(relay_adv_pool)); - vQueueDelete(xBleMeshQueueSet); - xBleMeshQueueSet = NULL; + vQueueDelete(mesh_queue_set); + mesh_queue_set = NULL; #endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ - vQueueDelete(xBleMeshQueue.queue); - xBleMeshQueue.queue = NULL; + vQueueDelete(adv_queue.handle); + adv_queue.handle = NULL; #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC - heap_caps_free(xBleMeshQueue.buffer); - xBleMeshQueue.buffer = NULL; - heap_caps_free(xBleMeshQueue.storage); - xBleMeshQueue.storage = NULL; + heap_caps_free(adv_queue.buffer); + adv_queue.buffer = NULL; + heap_caps_free(adv_queue.storage); + adv_queue.storage = NULL; #endif bt_mesh_unref_buf_from_pool(&adv_buf_pool);