From e24641cc8921a136259e2bf1e62a3220db8df47b Mon Sep 17 00:00:00 2001 From: lly Date: Sun, 19 Jan 2020 18:57:13 +0800 Subject: [PATCH] ble_mesh: Miscellaneous modifications 1. Add an API to set Provisioner static oob value 2. Add an API to deinit BLE Mesh stack 3. Add an API to set Provisioner unicast address 4. Add an API to provision devices with fixed address 5. Add an API to store node composition data 6. Add an API to get node with device uuid 7. Add an API to get node with unicast address 8. Add an API to delete node with device uuid 9. Add an API to delete node with unicast address 10. Add an API for Provisioner to update local AppKey 11. Add an API for Provisioner to update local NetKey 12. Support Provisioner persistent functionality 13. Fix Provisioner entering IV Update procedure 14. Fix an issue which may cause client failing to send msg 15. Use bt_mesh.flags to indicate device role 16. Remove several useless macros 17. Callback RSSI of received mesh provisioning packets 18. Modify the Provisioner disable function 19. Change some log level from debug to info 20. Add parameters to Provisioner bind AppKey completion event 21. Fix node ignoring relay messages issue 22. Support using a specific partition for BLE Mesh 23. Fix compile warning when proxy related macros are disabled 24. Clean up BLE Mesh stack included header files 25. NULL can be input if client message needs no parameters 26. Fix compile warning when BT log is disabled 27. Initilize BLE Mesh stack local variables 28. Support using PSRAM for BLE Mesh mutex, queue and task 29. Add a menuconfig option to enable using memory from PSRAM 30. Clean up sdkconfig.defaults of BLE Mesh examples --- components/bt/CMakeLists.txt | 1 - components/bt/esp_ble_mesh/Kconfig.in | 91 +- .../api/core/esp_ble_mesh_common_api.c | 17 +- .../esp_ble_mesh_local_data_operation_api.c | 2 - .../api/core/esp_ble_mesh_low_power_api.c | 2 +- .../api/core/esp_ble_mesh_networking_api.c | 174 +- .../api/core/esp_ble_mesh_provisioning_api.c | 75 +- .../api/core/esp_ble_mesh_proxy_api.c | 2 +- .../core/include/esp_ble_mesh_common_api.h | 12 + .../include/esp_ble_mesh_networking_api.h | 110 +- .../include/esp_ble_mesh_provisioning_api.h | 56 + .../bt/esp_ble_mesh/api/esp_ble_mesh_defs.h | 897 +++++----- .../models/esp_ble_mesh_config_model_api.c | 26 +- .../models/esp_ble_mesh_generic_model_api.c | 17 +- .../models/esp_ble_mesh_health_model_api.c | 4 +- .../models/esp_ble_mesh_lighting_model_api.c | 4 +- .../models/esp_ble_mesh_sensor_model_api.c | 1 - .../esp_ble_mesh_time_scene_model_api.c | 4 +- .../btc/btc_ble_mesh_config_model.c | 152 +- .../btc/btc_ble_mesh_generic_model.c | 126 +- .../btc/btc_ble_mesh_health_model.c | 113 +- .../btc/btc_ble_mesh_lighting_model.c | 106 +- .../bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c | 402 +++-- .../btc/btc_ble_mesh_sensor_model.c | 135 +- .../btc/btc_ble_mesh_time_scene_model.c | 94 +- .../btc/include/btc_ble_mesh_config_model.h | 3 +- .../btc/include/btc_ble_mesh_generic_model.h | 3 +- .../btc/include/btc_ble_mesh_health_model.h | 3 +- .../btc/include/btc_ble_mesh_lighting_model.h | 3 +- .../btc/include/btc_ble_mesh_prov.h | 67 +- .../btc/include/btc_ble_mesh_sensor_model.h | 3 +- .../include/btc_ble_mesh_time_scene_model.h | 3 +- .../mesh_common/include/mesh_buf.h | 5 - .../mesh_common/include/mesh_common.h | 43 +- .../mesh_common/include/mesh_kernel.h | 5 +- .../mesh_common/include/mesh_trace.h | 7 - .../mesh_common/include/mesh_util.h | 2 +- .../mesh_common/mesh_aes_encrypt.c | 11 +- .../bt/esp_ble_mesh/mesh_common/mesh_atomic.c | 11 +- .../bt/esp_ble_mesh/mesh_common/mesh_buf.c | 33 +- .../bt/esp_ble_mesh/mesh_common/mesh_common.c | 64 +- .../bt/esp_ble_mesh/mesh_common/mesh_kernel.c | 146 +- .../bt/esp_ble_mesh/mesh_common/mesh_util.c | 4 +- components/bt/esp_ble_mesh/mesh_core/access.c | 429 ++--- components/bt/esp_ble_mesh/mesh_core/access.h | 5 +- components/bt/esp_ble_mesh/mesh_core/adv.c | 352 ++-- components/bt/esp_ble_mesh/mesh_core/adv.h | 4 + components/bt/esp_ble_mesh/mesh_core/beacon.c | 136 +- components/bt/esp_ble_mesh/mesh_core/beacon.h | 5 +- .../bluedroid_host/mesh_bearer_adapt.c | 201 ++- .../bt/esp_ble_mesh/mesh_core/cfg_cli.c | 148 +- .../bt/esp_ble_mesh/mesh_core/cfg_srv.c | 376 ++--- components/bt/esp_ble_mesh/mesh_core/crypto.c | 91 +- components/bt/esp_ble_mesh/mesh_core/crypto.h | 2 +- .../bt/esp_ble_mesh/mesh_core/foundation.h | 7 +- components/bt/esp_ble_mesh/mesh_core/friend.c | 134 +- components/bt/esp_ble_mesh/mesh_core/friend.h | 3 + .../bt/esp_ble_mesh/mesh_core/health_cli.c | 112 +- .../bt/esp_ble_mesh/mesh_core/health_srv.c | 82 +- .../esp_ble_mesh/mesh_core/include/cfg_cli.h | 2 - .../esp_ble_mesh/mesh_core/include/cfg_srv.h | 1 - .../mesh_core/include/health_cli.h | 2 - .../mesh_core/include/health_srv.h | 1 - .../mesh_core/include/mesh_access.h | 3 +- .../mesh_core/include/mesh_bearer_adapt.h | 19 +- .../esp_ble_mesh/mesh_core/include/mesh_hci.h | 3 +- .../mesh_core/include/mesh_main.h | 56 +- components/bt/esp_ble_mesh/mesh_core/lpn.c | 47 +- components/bt/esp_ble_mesh/mesh_core/lpn.h | 3 + components/bt/esp_ble_mesh/mesh_core/main.c | 379 +++-- components/bt/esp_ble_mesh/mesh_core/mesh.h | 2 + components/bt/esp_ble_mesh/mesh_core/net.c | 256 +-- components/bt/esp_ble_mesh/mesh_core/net.h | 11 +- .../mesh_core/nimble_host/mesh_bearer_adapt.c | 35 +- components/bt/esp_ble_mesh/mesh_core/prov.c | 154 +- components/bt/esp_ble_mesh/mesh_core/prov.h | 2 +- .../mesh_core/provisioner_beacon.c | 71 - .../mesh_core/provisioner_beacon.h | 20 - .../esp_ble_mesh/mesh_core/provisioner_main.c | 1459 +++++++++++------ .../esp_ble_mesh/mesh_core/provisioner_main.h | 111 +- .../esp_ble_mesh/mesh_core/provisioner_prov.c | 1161 ++++++++----- .../esp_ble_mesh/mesh_core/provisioner_prov.h | 134 +- .../bt/esp_ble_mesh/mesh_core/proxy_client.c | 167 +- .../bt/esp_ble_mesh/mesh_core/proxy_client.h | 17 +- .../bt/esp_ble_mesh/mesh_core/proxy_server.c | 107 +- .../bt/esp_ble_mesh/mesh_core/proxy_server.h | 2 +- .../bt/esp_ble_mesh/mesh_core/settings.c | 1168 +++++++++++-- .../bt/esp_ble_mesh/mesh_core/settings.h | 29 +- .../mesh_core/storage/settings_nvs.c | 72 +- .../mesh_core/storage/settings_nvs.h | 3 +- components/bt/esp_ble_mesh/mesh_core/test.c | 10 +- components/bt/esp_ble_mesh/mesh_core/test.h | 7 +- .../bt/esp_ble_mesh/mesh_core/transport.c | 162 +- .../bt/esp_ble_mesh/mesh_core/transport.h | 3 + .../mesh_models/client/client_common.c | 71 +- .../mesh_models/client/generic_client.c | 175 +- .../client/include/client_common.h | 10 +- .../client/include/generic_client.h | 84 +- .../client/include/lighting_client.h | 53 +- .../client/include/sensor_client.h | 13 +- .../client/include/time_scene_client.h | 33 +- .../mesh_models/client/lighting_client.c | 158 +- .../mesh_models/client/sensor_client.c | 111 +- .../mesh_models/client/time_scene_client.c | 116 +- .../mesh_models/common/include/model_opcode.h | 4 +- .../mesh_models/server/device_property.c | 5 - .../mesh_models/server/generic_server.c | 304 +++- .../server/include/generic_server.h | 15 + .../server/include/lighting_server.h | 14 + .../server/include/sensor_server.h | 3 + .../server/include/server_common.h | 4 +- .../server/include/state_binding.h | 2 + .../server/include/state_transition.h | 1 + .../server/include/time_scene_server.h | 9 +- .../mesh_models/server/lighting_server.c | 374 ++++- .../mesh_models/server/sensor_server.c | 112 +- .../mesh_models/server/server_common.c | 28 +- .../mesh_models/server/state_binding.c | 13 +- .../mesh_models/server/state_transition.c | 22 +- .../mesh_models/server/time_scene_server.c | 174 +- .../esp-ble-mesh/ble-mesh-architecture.rst | 4 +- .../ble_mesh_node/sdkconfig.defaults | 49 +- .../main/ble_mesh_register_provisioner_cmd.c | 6 +- .../ble_mesh_provisioner/sdkconfig.defaults | 35 +- .../sdkconfig.defaults | 40 +- .../sdkconfig.defaults | 38 +- .../onoff_client/sdkconfig.defaults | 44 +- .../onoff_server/sdkconfig.defaults | 41 +- .../ble_mesh_provisioner/sdkconfig.defaults | 37 +- .../components/esp_fast_prov_server_model.c | 4 +- .../fast_prov_vendor_model/sdkconfig.defaults | 30 +- .../ble_mesh_wifi_coexist/sdkconfig.defaults | 43 +- 132 files changed, 8474 insertions(+), 4610 deletions(-) delete mode 100644 components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.c delete mode 100644 components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.h diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index ed65333c9..f69e99684 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -349,7 +349,6 @@ if(CONFIG_BT_ENABLED) "esp_ble_mesh/mesh_core/main.c" "esp_ble_mesh/mesh_core/net.c" "esp_ble_mesh/mesh_core/prov.c" - "esp_ble_mesh/mesh_core/provisioner_beacon.c" "esp_ble_mesh/mesh_core/provisioner_main.c" "esp_ble_mesh/mesh_core/provisioner_prov.c" "esp_ble_mesh/mesh_core/proxy_client.c" diff --git a/components/bt/esp_ble_mesh/Kconfig.in b/components/bt/esp_ble_mesh/Kconfig.in index 5bde20755..3839dffa6 100644 --- a/components/bt/esp_ble_mesh/Kconfig.in +++ b/components/bt/esp_ble_mesh/Kconfig.in @@ -18,6 +18,13 @@ 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 + 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. + config BLE_MESH_FAST_PROV bool "Enable BLE Mesh Fast Provisioning" select BLE_MESH_NODE @@ -116,7 +123,7 @@ if BLE_MESH config BLE_MESH_PROVISIONER_APP_KEY_COUNT int "Maximum number of application keys that can be owned by Provisioner" - default 9 + default 3 range 1 4096 help This option specifies how many application keys the Provisioner can have. @@ -168,15 +175,6 @@ if BLE_MESH ability to act as a proxy between a Mesh GATT Client and a Mesh network. This option should be enabled if a node is going to be a Proxy Server. - config BLE_MESH_GATT_PROXY_CLIENT - bool "BLE Mesh GATT Proxy Client" - select BLE_MESH_PROXY - default n - help - This option enables support for Mesh GATT Proxy Client. The Proxy Client - can use the GATT bearer to send mesh messages to a node that supports the - advertising bearer. - config BLE_MESH_NODE_ID_TIMEOUT int "Node Identity advertising timeout" depends on BLE_MESH_GATT_PROXY_SERVER @@ -191,20 +189,25 @@ if BLE_MESH node, it will start to advertise using Node Identity during the time set by this option. And after that, Network ID will be advertised. - if BLE_MESH_PROXY + config BLE_MESH_PROXY_FILTER_SIZE + int "Maximum number of filter entries per Proxy Client" + depends on BLE_MESH_GATT_PROXY_SERVER + default 4 + range 1 32767 + help + This option specifies how many Proxy Filter entries the local node supports. + The entries of Proxy filter (whitelist or blacklist) are used to store a + list of addresses which can be used to decide which messages will be forwarded + to the Proxy Client by the Proxy Server. - config BLE_MESH_PROXY_FILTER_SIZE - int "Maximum number of filter entries per Proxy Client" - default 1 - default 3 if BLE_MESH_GATT_PROXY_SERVER - range 1 32767 - help - This option specifies how many Proxy Filter entries the local node supports. - The entries of Proxy filter (whitelist or blacklist) are used to store a - list of addresses which can be used to decide which messages will be forwarded - to the Proxy Client by the Proxy Server. - - endif # BLE_MESH_PROXY + config BLE_MESH_GATT_PROXY_CLIENT + bool "BLE Mesh GATT Proxy Client" + select BLE_MESH_PROXY + default n + help + This option enables support for Mesh GATT Proxy Client. The Proxy Client + can use the GATT bearer to send mesh messages to a node that supports the + advertising bearer. config BLE_MESH_NET_BUF_POOL_USAGE bool "BLE Mesh net buffer pool usage tracking" @@ -215,16 +218,42 @@ if BLE_MESH devices. Recommend to enable this option as default. config BLE_MESH_SETTINGS - bool "Store BLE Mesh Node configuration persistently" + bool "Store BLE Mesh configuration persistently" default n help - When selected, the BLE Mesh stack will take care of storing/restoring the - BLE Mesh configuration persistently in flash. Currently this only supports - storing BLE Mesh node configuration. - Currently enabling this option will only store BLE Mesh nodes' information - in the flash. + When selected, the BLE Mesh stack will take care of storing/restoring the BLE + Mesh configuration persistently in flash. + If the device is a BLE Mesh node, when this option is enabled, the configuration + of the device will be stored persistently, including unicast address, NetKey, + AppKey, etc. + And if the device is a BLE Mesh Provisioner, the information of the device will + be stored persistently, including the information of provisioned nodes, NetKey, + AppKey, etc. if BLE_MESH_SETTINGS + + config BLE_MESH_SPECIFIC_PARTITION + bool "Use a specific NVS partition for BLE Mesh" + default n + help + When selected, the mesh stack will use a specified NVS partition instead of + default NVS partition. Note that the specified partition must be registered + with NVS using nvs_flash_init_partition() API, and the partition must exists + in the csv file. + When Provisioner needs to store a large amount of nodes' information in the + flash (e.g. more than 20), this option is recommended to be enabled. + + if BLE_MESH_SPECIFIC_PARTITION + + config BLE_MESH_PARTITION_NAME + string "Name of the NVS partition for BLE Mesh" + default "ble_mesh" + help + This value defines the name of the specified NVS partition used by the + mesh stack. + + endif # BLE_MESH_SPECIFIC_PARTITION + config BLE_MESH_STORE_TIMEOUT int "Delay (in seconds) before storing anything persistently" range 0 1000000 @@ -239,7 +268,7 @@ if BLE_MESH config BLE_MESH_SEQ_STORE_RATE int "How often the sequence number gets updated in storage" range 0 1000000 - default 128 + default 6 help This value defines how often the local sequence number gets updated in persistent storage (i.e. flash). e.g. a value of 100 means that the @@ -403,7 +432,7 @@ if BLE_MESH config BLE_MESH_TX_SEG_MAX int "Maximum number of segments in outgoing messages" - default 20 + default 32 range 2 32 help Maximum number of segments supported for outgoing messages. diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_common_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_common_api.c index b946a16c2..b0b29edfe 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_common_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_common_api.c @@ -17,9 +17,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" -#include "btc/btc_task.h" #include "btc/btc_manage.h" -#include "osi/alarm.h" #include "esp_err.h" @@ -31,7 +29,7 @@ esp_err_t esp_ble_mesh_init(esp_ble_mesh_prov_t *prov, esp_ble_mesh_comp_t *comp btc_ble_mesh_prov_args_t arg = {0}; SemaphoreHandle_t semaphore = NULL; btc_msg_t msg = {0}; - esp_err_t ret; + esp_err_t ret = ESP_OK; if (prov == NULL || comp == NULL) { return ESP_ERR_INVALID_ARG; @@ -46,7 +44,7 @@ esp_err_t esp_ble_mesh_init(esp_ble_mesh_prov_t *prov, esp_ble_mesh_comp_t *comp // Create a semaphore if ((semaphore = xSemaphoreCreateCounting(1, 0)) == NULL) { - LOG_ERROR("%s, Failed to allocate memory for the semaphore", __func__); + BT_ERR("%s, Failed to allocate memory for the semaphore", __func__); return ESP_ERR_NO_MEM; } @@ -61,7 +59,7 @@ esp_err_t esp_ble_mesh_init(esp_ble_mesh_prov_t *prov, esp_ble_mesh_comp_t *comp if (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) != BT_STATUS_SUCCESS) { vSemaphoreDelete(semaphore); - LOG_ERROR("%s, BLE Mesh initialise failed", __func__); + BT_ERR("%s, BLE Mesh initialise failed", __func__); return ESP_FAIL; } @@ -73,3 +71,12 @@ esp_err_t esp_ble_mesh_init(esp_ble_mesh_prov_t *prov, esp_ble_mesh_comp_t *comp return ESP_OK; } +esp_err_t esp_ble_mesh_deinit(esp_ble_mesh_deinit_param_t *param) +{ + if (param == NULL) { + return ESP_ERR_INVALID_ARG; + } + + return btc_ble_mesh_deinit(param); +} + diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_local_data_operation_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_local_data_operation_api.c index f02cb6fb9..656cae4f0 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_local_data_operation_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_local_data_operation_api.c @@ -13,9 +13,7 @@ // limitations under the License. #include -#include -#include "btc/btc_task.h" #include "btc/btc_manage.h" #include "esp_err.h" diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_low_power_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_low_power_api.c index df4053b1c..c4942b4a6 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_low_power_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_low_power_api.c @@ -14,7 +14,7 @@ #include -#include "btc/btc_task.h" +#include "btc/btc_manage.h" #include "esp_err.h" diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c index 2878a35de..7d30b3a64 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c @@ -13,9 +13,8 @@ // limitations under the License. #include -#include +#include -#include "btc/btc_task.h" #include "btc/btc_manage.h" #include "esp_err.h" @@ -37,17 +36,17 @@ static esp_err_t ble_mesh_model_send_msg(esp_ble_mesh_model_t *model, uint8_t op_len = 0, mic_len = 0; uint8_t *msg_data = NULL; btc_msg_t msg = {0}; - esp_err_t status; + esp_err_t status = ESP_OK; ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); if (ctx && ctx->addr == ESP_BLE_MESH_ADDR_UNASSIGNED) { - LOG_ERROR("%s, Invalid destination address 0x0000", __func__); + BT_ERR("%s, Invalid destination address 0x0000", __func__); return ESP_ERR_INVALID_ARG; } if (device_role > ROLE_FAST_PROV) { - LOG_ERROR("%s, Invalid device role 0x%02x", __func__, device_role); + BT_ERR("%s, Invalid device role 0x%02x", __func__, device_role); return ESP_ERR_INVALID_ARG; } @@ -66,7 +65,7 @@ static esp_err_t ble_mesh_model_send_msg(esp_ble_mesh_model_t *model, if (act == BTC_BLE_MESH_ACT_MODEL_PUBLISH) { if (op_len + length > model->pub->msg->size) { - LOG_ERROR("%s, Model publication msg size %d is too small", __func__, model->pub->msg->size); + BT_ERR("%s, Model publication msg size %d is too small", __func__, model->pub->msg->size); return ESP_ERR_INVALID_ARG; } } @@ -78,7 +77,7 @@ static esp_err_t ble_mesh_model_send_msg(esp_ble_mesh_model_t *model, } if (op_len + length + mic_len > MIN(ESP_BLE_MESH_SDU_MAX_LEN, ESP_BLE_MESH_TX_SDU_MAX)) { - LOG_ERROR("%s, Data length %d is too large", __func__, length); + BT_ERR("%s, Data length %d is too large", __func__, length); return ESP_ERR_INVALID_ARG; } @@ -86,7 +85,7 @@ static esp_err_t ble_mesh_model_send_msg(esp_ble_mesh_model_t *model, bt_mesh_model_msg_init(model->pub->msg, opcode); net_buf_simple_add_mem(model->pub->msg, data, length); } else { - msg_data = (uint8_t *)osi_malloc(op_len + length); + msg_data = (uint8_t *)bt_mesh_malloc(op_len + length); if (msg_data == NULL) { return ESP_ERR_NO_MEM; } @@ -115,7 +114,7 @@ static esp_err_t ble_mesh_model_send_msg(esp_ble_mesh_model_t *model, status = (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_model_args_t), btc_ble_mesh_model_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); - osi_free(msg_data); + bt_mesh_free(msg_data); return status; } @@ -129,7 +128,7 @@ esp_err_t esp_ble_mesh_register_custom_model_callback(esp_ble_mesh_model_cb_t ca esp_err_t esp_ble_mesh_model_msg_opcode_init(uint8_t *data, uint32_t opcode) { - uint16_t val; + uint16_t val = 0; if (data == NULL) { return ESP_ERR_INVALID_ARG; @@ -161,9 +160,23 @@ esp_err_t esp_ble_mesh_client_model_init(esp_ble_mesh_model_t *model) if (model == NULL) { return ESP_ERR_INVALID_ARG; } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + return btc_ble_mesh_client_model_init(model); } +esp_err_t esp_ble_mesh_client_model_deinit(esp_ble_mesh_model_t *model) +{ + if (model == NULL) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return btc_ble_mesh_client_model_deinit(model); +} + esp_err_t esp_ble_mesh_server_model_send_msg(esp_ble_mesh_model_t *model, esp_ble_mesh_msg_ctx_t *ctx, uint32_t opcode, uint16_t length, uint8_t *data) @@ -238,7 +251,7 @@ esp_err_t esp_ble_mesh_node_local_reset(void) #if (CONFIG_BLE_MESH_PROVISIONER) -esp_err_t esp_ble_mesh_provisioner_set_node_name(int index, const char *name) +esp_err_t esp_ble_mesh_provisioner_set_node_name(uint16_t index, const char *name) { btc_ble_mesh_prov_args_t arg = {0}; btc_msg_t msg = {0}; @@ -260,20 +273,102 @@ esp_err_t esp_ble_mesh_provisioner_set_node_name(int index, const char *name) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } -const char *esp_ble_mesh_provisioner_get_node_name(int index) +const char *esp_ble_mesh_provisioner_get_node_name(uint16_t index) { return bt_mesh_provisioner_get_node_name(index); } -int esp_ble_mesh_provisioner_get_node_index(const char *name) +uint16_t esp_ble_mesh_provisioner_get_node_index(const char *name) { if (!name || (strlen(name) > ESP_BLE_MESH_NODE_NAME_MAX_LEN)) { - return -EINVAL; + return ESP_BLE_MESH_INVALID_NODE_INDEX; } return bt_mesh_provisioner_get_node_index(name); } +esp_err_t esp_ble_mesh_provisioner_store_node_comp_data(uint16_t unicast_addr, uint8_t *data, uint16_t length) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr) || !data || length <= 14) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA; + + arg.store_node_comp_data.unicast_addr = unicast_addr; + arg.store_node_comp_data.length = length; + arg.store_node_comp_data.data = data; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), btc_ble_mesh_prov_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16]) +{ + if (!uuid) { + return NULL; + } + + return btc_ble_mesh_provisioner_get_node_with_uuid(uuid); +} + +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr) +{ + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) { + return NULL; + } + + return btc_ble_mesh_provisioner_get_node_with_addr(unicast_addr); +} + +esp_err_t esp_ble_mesh_provisioner_delete_node_with_uuid(const uint8_t uuid[16]) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!uuid) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_UUID; + + memcpy(arg.delete_node_with_uuid.uuid, uuid, 16); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_delete_node_with_addr(uint16_t unicast_addr) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_ADDR; + + arg.delete_node_with_addr.unicast_addr = unicast_addr; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + esp_err_t esp_ble_mesh_provisioner_add_local_app_key(const uint8_t app_key[16], uint16_t net_idx, uint16_t app_idx) { @@ -284,7 +379,7 @@ esp_err_t esp_ble_mesh_provisioner_add_local_app_key(const uint8_t app_key[16], msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_PROV; - msg.act = BTC_BLE_MESH_ACT_PROVISIONER_SET_LOCAL_APP_KEY; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_APP_KEY; arg.add_local_app_key.net_idx = net_idx; arg.add_local_app_key.app_idx = app_idx; @@ -297,6 +392,29 @@ esp_err_t esp_ble_mesh_provisioner_add_local_app_key(const uint8_t app_key[16], == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } +esp_err_t esp_ble_mesh_provisioner_update_local_app_key(const uint8_t app_key[16], + uint16_t net_idx, uint16_t app_idx) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (app_key == NULL) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_APP_KEY; + + memcpy(arg.update_local_app_key.app_key, app_key, 16); + arg.update_local_app_key.net_idx = net_idx; + arg.update_local_app_key.app_idx = app_idx; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + const uint8_t *esp_ble_mesh_provisioner_get_local_app_key(uint16_t net_idx, uint16_t app_idx) { return bt_mesh_provisioner_local_app_key_get(net_idx, app_idx); @@ -351,11 +469,37 @@ esp_err_t esp_ble_mesh_provisioner_add_local_net_key(const uint8_t net_key[16], == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } +esp_err_t esp_ble_mesh_provisioner_update_local_net_key(const uint8_t net_key[16], uint16_t net_idx) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (net_key == NULL) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_NET_KEY; + + memcpy(arg.update_local_net_key.net_key, net_key, 16); + arg.update_local_net_key.net_idx = net_idx; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + const uint8_t *esp_ble_mesh_provisioner_get_local_net_key(uint16_t net_idx) { return bt_mesh_provisioner_local_net_key_get(net_idx); } +uint16_t esp_ble_mesh_provisioner_get_prov_node_count(void) +{ + return btc_ble_mesh_provisioner_get_prov_node_count(); +} + #endif /* CONFIG_BLE_MESH_PROVISIONER */ #if (CONFIG_BLE_MESH_FAST_PROV) diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c index 80f96ed05..c3dfdc04a 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c @@ -13,8 +13,8 @@ // limitations under the License. #include +#include -#include "btc/btc_task.h" #include "btc/btc_manage.h" #include "esp_err.h" @@ -285,6 +285,38 @@ esp_err_t esp_ble_mesh_provisioner_add_unprov_dev(esp_ble_mesh_unprov_dev_add_t == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } +esp_err_t esp_ble_mesh_provisioner_prov_device_with_addr(const uint8_t uuid[16], + esp_ble_mesh_bd_addr_t addr, esp_ble_mesh_addr_type_t addr_type, + esp_ble_mesh_prov_bearer_t bearer, uint16_t oob_info, uint16_t unicast_addr) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (uuid == NULL || (bearer == ESP_BLE_MESH_PROV_GATT && (addr == NULL || + addr_type > ESP_BLE_MESH_ADDR_TYPE_RANDOM)) || + (bearer != ESP_BLE_MESH_PROV_ADV && bearer != ESP_BLE_MESH_PROV_GATT) || + !ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_PROV_DEV_WITH_ADDR; + + memcpy(arg.provisioner_prov_dev_with_addr.uuid, uuid, 16); + if (addr) { + memcpy(arg.provisioner_prov_dev_with_addr.addr, addr, BD_ADDR_LEN); + arg.provisioner_prov_dev_with_addr.addr_type = addr_type; + } + arg.provisioner_prov_dev_with_addr.bearer = bearer; + arg.provisioner_prov_dev_with_addr.oob_info = oob_info; + arg.provisioner_prov_dev_with_addr.unicast_addr = unicast_addr; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + esp_err_t esp_ble_mesh_provisioner_delete_dev(esp_ble_mesh_device_delete_t *del_dev) { uint8_t val = DEL_DEV_ADDR_FLAG | DEL_DEV_UUID_FLAG; @@ -362,6 +394,47 @@ esp_err_t esp_ble_mesh_provisioner_set_prov_data_info(esp_ble_mesh_prov_data_inf == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } +esp_err_t esp_ble_mesh_provisioner_set_static_oob_value(const uint8_t *value, uint8_t length) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (value == NULL || length == 0 || length > 16) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_SET_STATIC_OOB_VAL; + + arg.set_static_oob_val.length = length; + memcpy(arg.set_static_oob_val.value, value, length); + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_set_primary_elem_addr(uint16_t addr) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_SET_PRIMARY_ELEM_ADDR; + + arg.set_primary_elem_addr.addr = addr; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + #endif /* CONFIG_BLE_MESH_PROVISIONER */ /* The following APIs are for fast provisioning */ diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_proxy_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_proxy_api.c index b64f789f0..071e0e37f 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_proxy_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_proxy_api.c @@ -13,8 +13,8 @@ // limitations under the License. #include +#include -#include "btc/btc_task.h" #include "btc/btc_manage.h" #include "esp_err.h" diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_common_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_common_api.h index 7f4843668..e218c51b7 100644 --- a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_common_api.h +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_common_api.h @@ -34,4 +34,16 @@ */ esp_err_t esp_ble_mesh_init(esp_ble_mesh_prov_t *prov, esp_ble_mesh_comp_t *comp); +/** + * @brief De-initialize BLE Mesh module. + * + * @note This function shall be invoked after esp_ble_mesh_client_model_deinit(). + * + * @param[in] param: Pointer to the structure of BLE Mesh deinit parameters. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_deinit(esp_ble_mesh_deinit_param_t *param); + #endif /* _ESP_BLE_MESH_COMMON_API_H_ */ diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h index ecf160d5d..0d5bcec30 100644 --- a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h @@ -75,6 +75,18 @@ esp_err_t esp_ble_mesh_model_msg_opcode_init(uint8_t *data, uint32_t opcode); */ esp_err_t esp_ble_mesh_client_model_init(esp_ble_mesh_model_t *model); +/** + * @brief De-initialize the user-defined client model. + * + * @note This function shall be invoked before esp_ble_mesh_deinit() is called. + * + * @param[in] model: Pointer of the Client model. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_client_model_deinit(esp_ble_mesh_model_t *model); + /** * @brief Send server model messages(such as server model status messages). * @@ -176,7 +188,7 @@ esp_err_t esp_ble_mesh_node_local_reset(void); * @return ESP_OK on success or error code otherwise. * */ -esp_err_t esp_ble_mesh_provisioner_set_node_name(int index, const char *name); +esp_err_t esp_ble_mesh_provisioner_set_node_name(uint16_t index, const char *name); /** * @brief This function is called to get the node (provisioned device) name. @@ -188,20 +200,76 @@ esp_err_t esp_ble_mesh_provisioner_set_node_name(int index, const char *name); * @return Node name on success, or NULL on failure. * */ -const char *esp_ble_mesh_provisioner_get_node_name(int index); +const char *esp_ble_mesh_provisioner_get_node_name(uint16_t index); /** * @brief This function is called to get the node (provisioned device) index. * * @param[in] name: Name of the node (end by '\0'). * - * @return Node index on success, or (negative) error code from errno.h on failure. + * @return Node index on success, or an invalid value (0xFFFF) on failure. * */ -int esp_ble_mesh_provisioner_get_node_index(const char *name); +uint16_t esp_ble_mesh_provisioner_get_node_index(const char *name); /** - * @brief This function is called to set the app key for the local BLE Mesh stack. + * @brief This function is called to store the Composition Data of the node. + * + * @param[in] unicast_addr: Element address of the node + * @param[in] data: Pointer of Composition Data + * @param[in] length: Length of Composition Data + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_store_node_comp_data(uint16_t unicast_addr, uint8_t *data, uint16_t length); + +/** + * @brief This function is called to get the provisioned node information + * with the node device uuid. + * + * @param[in] uuid: Device UUID of the node + * + * @return Pointer of the node info struct or NULL on failure. + * + */ +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16]); + +/** + * @brief This function is called to get the provisioned node information + * with the node unicast address. + * + * @param[in] unicast_addr: Unicast address of the node + * + * @return Pointer of the node info struct or NULL on failure. + * + */ +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr); + +/** + * @brief This function is called to delete the provisioned node information + * with the node device uuid. + * + * @param[in] uuid: Device UUID of the node + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_delete_node_with_uuid(const uint8_t uuid[16]); + +/** + * @brief This function is called to delete the provisioned node information + * with the node unicast address. + * + * @param[in] unicast_addr: Unicast address of the node + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_delete_node_with_addr(uint16_t unicast_addr); + +/** + * @brief This function is called to add a local AppKey for Provisioner. * * @param[in] app_key: The app key to be set for the local BLE Mesh stack. * @param[in] net_idx: The network key index. @@ -217,6 +285,19 @@ int esp_ble_mesh_provisioner_get_node_index(const char *name); */ esp_err_t esp_ble_mesh_provisioner_add_local_app_key(const uint8_t app_key[16], uint16_t net_idx, uint16_t app_idx); +/** + * @brief This function is used to update a local AppKey for Provisioner. + * + * @param[in] app_key: Value of the AppKey. + * @param[in] net_idx: Corresponding NetKey Index. + * @param[in] app_idx: The AppKey Index + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_update_local_app_key(const uint8_t app_key[16], + uint16_t net_idx, uint16_t app_idx); + /** * @brief This function is called by Provisioner to get the local app key value. * @@ -260,6 +341,17 @@ esp_err_t esp_ble_mesh_provisioner_bind_app_key_to_local_model(uint16_t element_ */ esp_err_t esp_ble_mesh_provisioner_add_local_net_key(const uint8_t net_key[16], uint16_t net_idx); +/** + * @brief This function is called by Provisioner to update a local network key. + * + * @param[in] net_key: Value of the NetKey. + * @param[in] net_idx: The NetKey Index. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_update_local_net_key(const uint8_t net_key[16], uint16_t net_idx); + /** * @brief This function is called by Provisioner to get the local network key value. * @@ -270,6 +362,14 @@ esp_err_t esp_ble_mesh_provisioner_add_local_net_key(const uint8_t net_key[16], */ const uint8_t *esp_ble_mesh_provisioner_get_local_net_key(uint16_t net_idx); +/** + * @brief This function is called by Provisioner to get provisioned node count. + * + * @return Number of the provisioned nodes. + * + */ +uint16_t esp_ble_mesh_provisioner_get_prov_node_count(void); + /** * @brief This function is called to get fast provisioning application key. * diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h index 34dc38ce7..1caa7447e 100644 --- a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h @@ -230,6 +230,32 @@ esp_err_t esp_ble_mesh_provisioner_prov_disable(esp_ble_mesh_prov_bearer_t beare esp_err_t esp_ble_mesh_provisioner_add_unprov_dev(esp_ble_mesh_unprov_dev_add_t *add_dev, esp_ble_mesh_dev_add_flag_t flags); +/** @brief Provision an unprovisioned device and assign a fixed unicast address for it in advance. + * + * @param[in] uuid: Device UUID of the unprovisioned device + * @param[in] addr: Device address of the unprovisioned device + * @param[in] addr_type: Device address type of the unprovisioned device + * @param[in] bearer: Provisioning bearer going to be used by Provisioner + * @param[in] oob_info: OOB info of the unprovisioned device + * @param[in] unicast_addr: Unicast address going to be allocated for the unprovisioned device + * + * @return Zero on success or (negative) error code otherwise. + * + * @note: 1. Currently address type only supports public address and static random address. + * 2. Bearer must be equal to ESP_BLE_MESH_PROV_ADV or ESP_BLE_MESH_PROV_GATT, since + * Provisioner will start to provision a device immediately once this function is + * invoked. And the input bearer must be identical with the one within the parameters + * of the ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT event. + * 3. If this function is used by a Provisioner to provision devices, the application + * should take care of the assigned unicast address and avoid overlap of the unicast + * addresses of different nodes. + * 4. Recommend to use only one of the functions "esp_ble_mesh_provisioner_add_unprov_dev" + * and "esp_ble_mesh_provisioner_prov_device_with_addr" by a Provisioner. + */ +esp_err_t esp_ble_mesh_provisioner_prov_device_with_addr(const uint8_t uuid[16], + esp_ble_mesh_bd_addr_t addr, esp_ble_mesh_addr_type_t addr_type, + esp_ble_mesh_prov_bearer_t bearer, uint16_t oob_info, uint16_t unicast_addr); + /** * @brief Delete device from queue, reset current provisioning link and reset the node. * @@ -290,6 +316,36 @@ esp_err_t esp_ble_mesh_provisioner_set_dev_uuid_match(const uint8_t *match_val, */ esp_err_t esp_ble_mesh_provisioner_set_prov_data_info(esp_ble_mesh_prov_data_info_t *prov_data_info); +/** + * @brief This function is called by Provisioner to set static oob value used for provisioning. + * + * @param[in] value: Pointer to the static oob value. + * @param[in] length: Length of the static oob value. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_set_static_oob_value(const uint8_t *value, uint8_t length); + +/** + * @brief This function is called by Provisioner to set own Primary element address. + * + * @note This API must be invoked when BLE Mesh initialization is completed successfully, + * and can be invoked before Provisioner functionality is enabled. + * Once this API is invoked successfully, the prov_unicast_addr value in the struct + * esp_ble_mesh_prov_t will be ignored, and Provisioner will use this address as its + * own primary element address. + * And if the unicast address going to assigned for the next unprovisioned device is + * smaller than the input address + element number of Provisioner, then the address + * for the next unprovisioned device will be recalculated internally. + * + * @param[in] addr: Unicast address of the Primary element of Provisioner. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_set_primary_elem_addr(uint16_t addr); + /** * @brief This function is called to set provisioning data information before starting * fast provisioning. diff --git a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h index 733c718aa..aa3d1b999 100644 --- a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h +++ b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h @@ -17,17 +17,9 @@ #include -#include "mesh_proxy.h" -#include "mesh_access.h" -#include "mesh_main.h" - -#include "mesh.h" -#include "proxy_server.h" -#include "foundation.h" -#include "provisioner_main.h" - -#include "model_opcode.h" #include "mesh_common.h" +#include "proxy_server.h" +#include "provisioner_main.h" #ifdef CONFIG_BT_BLUEDROID_ENABLED #include "esp_bt_defs.h" @@ -56,58 +48,61 @@ typedef uint8_t esp_ble_mesh_octet16_t[ESP_BLE_MESH_OCTET16_LEN]; #define ESP_BLE_MESH_OCTET8_LEN 8 typedef uint8_t esp_ble_mesh_octet8_t[ESP_BLE_MESH_OCTET8_LEN]; -#define ESP_BLE_MESH_ADDR_UNASSIGNED BLE_MESH_ADDR_UNASSIGNED -#define ESP_BLE_MESH_ADDR_ALL_NODES BLE_MESH_ADDR_ALL_NODES -#define ESP_BLE_MESH_ADDR_PROXIES BLE_MESH_ADDR_PROXIES -#define ESP_BLE_MESH_ADDR_FRIENDS BLE_MESH_ADDR_FRIENDS -#define ESP_BLE_MESH_ADDR_RELAYS BLE_MESH_ADDR_RELAYS +#define ESP_BLE_MESH_ADDR_UNASSIGNED 0x0000 +#define ESP_BLE_MESH_ADDR_ALL_NODES 0xFFFF +#define ESP_BLE_MESH_ADDR_PROXIES 0xFFFC +#define ESP_BLE_MESH_ADDR_FRIENDS 0xFFFD +#define ESP_BLE_MESH_ADDR_RELAYS 0xFFFE -#define ESP_BLE_MESH_KEY_UNUSED BLE_MESH_KEY_UNUSED -#define ESP_BLE_MESH_KEY_DEV BLE_MESH_KEY_DEV +#define ESP_BLE_MESH_KEY_UNUSED 0xFFFF +#define ESP_BLE_MESH_KEY_DEV 0xFFFE -#define ESP_BLE_MESH_KEY_PRIMARY BLE_MESH_KEY_PRIMARY -#define ESP_BLE_MESH_KEY_ANY BLE_MESH_KEY_ANY +#define ESP_BLE_MESH_KEY_PRIMARY 0x0000 +#define ESP_BLE_MESH_KEY_ANY 0xFFFF /*!< Primary Network Key index */ -#define ESP_BLE_MESH_NET_PRIMARY BLE_MESH_NET_PRIMARY +#define ESP_BLE_MESH_NET_PRIMARY 0x000 /*!< Relay state value */ -#define ESP_BLE_MESH_RELAY_DISABLED BLE_MESH_RELAY_DISABLED -#define ESP_BLE_MESH_RELAY_ENABLED BLE_MESH_RELAY_ENABLED -#define ESP_BLE_MESH_RELAY_NOT_SUPPORTED BLE_MESH_RELAY_NOT_SUPPORTED +#define ESP_BLE_MESH_RELAY_DISABLED 0x00 +#define ESP_BLE_MESH_RELAY_ENABLED 0x01 +#define ESP_BLE_MESH_RELAY_NOT_SUPPORTED 0x02 /*!< Beacon state value */ -#define ESP_BLE_MESH_BEACON_DISABLED BLE_MESH_BEACON_DISABLED -#define ESP_BLE_MESH_BEACON_ENABLED BLE_MESH_BEACON_ENABLED +#define ESP_BLE_MESH_BEACON_DISABLED 0x00 +#define ESP_BLE_MESH_BEACON_ENABLED 0x01 /*!< GATT Proxy state value */ -#define ESP_BLE_MESH_GATT_PROXY_DISABLED BLE_MESH_GATT_PROXY_DISABLED -#define ESP_BLE_MESH_GATT_PROXY_ENABLED BLE_MESH_GATT_PROXY_ENABLED -#define ESP_BLE_MESH_GATT_PROXY_NOT_SUPPORTED BLE_MESH_GATT_PROXY_NOT_SUPPORTED +#define ESP_BLE_MESH_GATT_PROXY_DISABLED 0x00 +#define ESP_BLE_MESH_GATT_PROXY_ENABLED 0x01 +#define ESP_BLE_MESH_GATT_PROXY_NOT_SUPPORTED 0x02 /*!< Friend state value */ -#define ESP_BLE_MESH_FRIEND_DISABLED BLE_MESH_FRIEND_DISABLED -#define ESP_BLE_MESH_FRIEND_ENABLED BLE_MESH_FRIEND_ENABLED -#define ESP_BLE_MESH_FRIEND_NOT_SUPPORTED BLE_MESH_FRIEND_NOT_SUPPORTED +#define ESP_BLE_MESH_FRIEND_DISABLED 0x00 +#define ESP_BLE_MESH_FRIEND_ENABLED 0x01 +#define ESP_BLE_MESH_FRIEND_NOT_SUPPORTED 0x02 /*!< Node identity state value */ -#define ESP_BLE_MESH_NODE_IDENTITY_STOPPED BLE_MESH_NODE_IDENTITY_STOPPED -#define ESP_BLE_MESH_NODE_IDENTITY_RUNNING BLE_MESH_NODE_IDENTITY_RUNNING -#define ESP_BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED +#define ESP_BLE_MESH_NODE_IDENTITY_STOPPED 0x00 +#define ESP_BLE_MESH_NODE_IDENTITY_RUNNING 0x01 +#define ESP_BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED 0x02 /*!< Supported features */ -#define ESP_BLE_MESH_FEATURE_RELAY BLE_MESH_FEAT_RELAY -#define ESP_BLE_MESH_FEATURE_PROXY BLE_MESH_FEAT_PROXY -#define ESP_BLE_MESH_FEATURE_FRIEND BLE_MESH_FEAT_FRIEND -#define ESP_BLE_MESH_FEATURE_LOW_POWER BLE_MESH_FEAT_LOW_POWER -#define ESP_BLE_MESH_FEATURE_ALL_SUPPORTED BLE_MESH_FEAT_SUPPORTED +#define ESP_BLE_MESH_FEATURE_RELAY BIT(0) +#define ESP_BLE_MESH_FEATURE_PROXY BIT(1) +#define ESP_BLE_MESH_FEATURE_FRIEND BIT(2) +#define ESP_BLE_MESH_FEATURE_LOW_POWER BIT(3) +#define ESP_BLE_MESH_FEATURE_ALL_SUPPORTED (ESP_BLE_MESH_FEATURE_RELAY | \ + ESP_BLE_MESH_FEATURE_PROXY | \ + ESP_BLE_MESH_FEATURE_FRIEND | \ + ESP_BLE_MESH_FEATURE_LOW_POWER) -#define ESP_BLE_MESH_ADDR_IS_UNICAST(addr) BLE_MESH_ADDR_IS_UNICAST(addr) -#define ESP_BLE_MESH_ADDR_IS_GROUP(addr) BLE_MESH_ADDR_IS_GROUP(addr) -#define ESP_BLE_MESH_ADDR_IS_VIRTUAL(addr) BLE_MESH_ADDR_IS_VIRTUAL(addr) -#define ESP_BLE_MESH_ADDR_IS_RFU(addr) BLE_MESH_ADDR_IS_RFU(addr) +#define ESP_BLE_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000) +#define ESP_BLE_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xC000 && (addr) <= 0xFF00) +#define ESP_BLE_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xC000) +#define ESP_BLE_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xFF00 && (addr) <= 0xFFFB) -#define ESP_BLE_MESH_INVALID_NODE_INDEX (-1) +#define ESP_BLE_MESH_INVALID_NODE_INDEX 0xFFFF /** @def ESP_BLE_MESH_TRANSMIT * @@ -124,7 +119,7 @@ typedef uint8_t esp_ble_mesh_octet8_t[ESP_BLE_MESH_OCTET8_LEN]; * @return BLE Mesh transmit value that can be used e.g. for the default * values of the Configuration Model data. */ -#define ESP_BLE_MESH_TRANSMIT(count, int_ms) BLE_MESH_TRANSMIT(count, int_ms) +#define ESP_BLE_MESH_TRANSMIT(count, int_ms) ((count) | (((int_ms / 10) - 1) << 3)) /** @def ESP_BLE_MESH_GET_TRANSMIT_COUNT * @@ -134,7 +129,7 @@ typedef uint8_t esp_ble_mesh_octet8_t[ESP_BLE_MESH_OCTET8_LEN]; * * @return Transmission count (actual transmissions equal to N + 1). */ -#define ESP_BLE_MESH_GET_TRANSMIT_COUNT(transmit) BLE_MESH_TRANSMIT_COUNT(transmit) +#define ESP_BLE_MESH_GET_TRANSMIT_COUNT(transmit) (((transmit) & (uint8_t)BIT_MASK(3))) /** @def ESP_BLE_MESH_GET_TRANSMIT_INTERVAL * @@ -144,7 +139,7 @@ typedef uint8_t esp_ble_mesh_octet8_t[ESP_BLE_MESH_OCTET8_LEN]; * * @return Transmission interval in milliseconds. */ -#define ESP_BLE_MESH_GET_TRANSMIT_INTERVAL(transmit) BLE_MESH_TRANSMIT_INT(transmit) +#define ESP_BLE_MESH_GET_TRANSMIT_INTERVAL(transmit) ((((transmit) >> 3) + 1) * 10) /** @def ESP_BLE_MESH_PUBLISH_TRANSMIT * @@ -157,7 +152,7 @@ typedef uint8_t esp_ble_mesh_octet8_t[ESP_BLE_MESH_OCTET8_LEN]; * @return BLE Mesh transmit value that can be used e.g. for the default * values of the Configuration Model data. */ -#define ESP_BLE_MESH_PUBLISH_TRANSMIT(count, int_ms) BLE_MESH_PUB_TRANSMIT(count, int_ms) +#define ESP_BLE_MESH_PUBLISH_TRANSMIT(count, int_ms) ESP_BLE_MESH_TRANSMIT(count, (int_ms) / 5) /** @def ESP_BLE_MESH_GET_PUBLISH_TRANSMIT_COUNT * @@ -167,7 +162,7 @@ typedef uint8_t esp_ble_mesh_octet8_t[ESP_BLE_MESH_OCTET8_LEN]; * * @return Retransmission count (actual transmissions equal to N + 1). */ -#define ESP_BLE_MESH_GET_PUBLISH_TRANSMIT_COUNT(transmit) BLE_MESH_PUB_TRANSMIT_COUNT(transmit) +#define ESP_BLE_MESH_GET_PUBLISH_TRANSMIT_COUNT(transmit) ESP_BLE_MESH_GET_TRANSMIT_COUNT(transmit) /** @def ESP_BLE_MESH_GET_PUBLISH_TRANSMIT_INTERVAL * @@ -177,7 +172,7 @@ typedef uint8_t esp_ble_mesh_octet8_t[ESP_BLE_MESH_OCTET8_LEN]; * * @return Transmission interval in milliseconds. */ -#define ESP_BLE_MESH_GET_PUBLISH_TRANSMIT_INTERVAL(transmit) BLE_MESH_PUB_TRANSMIT_INT(transmit) +#define ESP_BLE_MESH_GET_PUBLISH_TRANSMIT_INTERVAL(transmit) ((((transmit) >> 3) + 1) * 50) /*!< Callbacks which are not needed to be initialized by users (set with 0 and will be initialized internally) */ typedef uint32_t esp_ble_mesh_cb_t; @@ -244,9 +239,9 @@ typedef enum { } esp_ble_mesh_prov_oob_info_t; /*!< Macros used to define message opcode */ -#define ESP_BLE_MESH_MODEL_OP_1(b0) BLE_MESH_MODEL_OP_1(b0) -#define ESP_BLE_MESH_MODEL_OP_2(b0, b1) BLE_MESH_MODEL_OP_2(b0, b1) -#define ESP_BLE_MESH_MODEL_OP_3(b0, cid) BLE_MESH_MODEL_OP_3(b0, cid) +#define ESP_BLE_MESH_MODEL_OP_1(b0) (b0) +#define ESP_BLE_MESH_MODEL_OP_2(b0, b1) (((b0) << 8) | (b1)) +#define ESP_BLE_MESH_MODEL_OP_3(b0, cid) ((((b0) << 16) | 0xC00000) | (cid)) /*!< This macro is associated with BLE_MESH_MODEL in mesh_access.h */ #define ESP_BLE_MESH_SIG_MODEL(_id, _op, _pub, _user_data) \ @@ -321,13 +316,17 @@ typedef uint8_t BD_ADDR[BD_ADDR_LEN]; typedef uint8_t esp_ble_mesh_bd_addr_t[BD_ADDR_LEN]; +#define ESP_BLE_MESH_ADDR_TYPE_PUBLIC 0x00 +#define ESP_BLE_MESH_ADDR_TYPE_RANDOM 0x01 +#define ESP_BLE_MESH_ADDR_TYPE_RPA_PUBLIC 0x02 +#define ESP_BLE_MESH_ADDR_TYPE_RPA_RANDOM 0x03 /// BLE device address type -typedef enum { - ESP_BLE_MESH_ADDR_TYPE_PUBLIC = 0x00, - ESP_BLE_MESH_ADDR_TYPE_RANDOM = 0x01, - ESP_BLE_MESH_ADDR_TYPE_RPA_PUBLIC = 0x02, - ESP_BLE_MESH_ADDR_TYPE_RPA_RANDOM = 0x03, -} esp_ble_mesh_addr_type_t; +typedef uint8_t esp_ble_mesh_addr_type_t; + +/** BLE Mesh deinit parameters */ +typedef struct { + bool erase_flash; /*!< Indicate if erasing flash when deinit mesh stack */ +} esp_ble_mesh_deinit_param_t; typedef struct esp_ble_mesh_model esp_ble_mesh_model_t; @@ -355,12 +354,12 @@ typedef struct { /** Pointer to the model to which the context belongs. Initialized by the stack. */ esp_ble_mesh_model_t *model; - uint16_t publish_addr; /*!< Publish Address. */ - uint16_t app_idx:12, /*!< Publish AppKey Index. */ - cred:1; /*!< Friendship Credentials Flag. */ + uint16_t publish_addr; /*!< Publish Address. */ + uint16_t app_idx:12, /*!< Publish AppKey Index. */ + cred:1; /*!< Friendship Credentials Flag. */ - uint8_t ttl; /*!< Publish Time to Live. */ - uint8_t retransmit; /*!< Retransmit Count & Interval Steps. */ + uint8_t ttl; /*!< Publish Time to Live. */ + uint8_t retransmit; /*!< Retransmit Count & Interval Steps. */ uint8_t period; /*!< Publish Period. */ uint8_t period_div:4, /*!< Divisor for the Period. */ @@ -677,6 +676,28 @@ typedef struct { uint8_t flag; /*!< BIT0: net_idx; BIT1: flags; BIT2: iv_index */ } esp_ble_mesh_prov_data_info_t; +/** Information of the provisioned node */ +typedef struct { + /* Device information */ + esp_ble_mesh_bd_addr_t addr; /*!< Node device address */ + esp_ble_mesh_addr_type_t addr_type; /*!< Node device address type */ + uint8_t dev_uuid[16]; /*!< Device UUID */ + uint16_t oob_info; /*!< Node OOB information */ + + /* Provisioning information */ + uint16_t unicast_addr; /*!< Node unicast address */ + uint8_t element_num; /*!< Node element number */ + uint16_t net_idx; /*!< Node NetKey Index */ + uint8_t flags; /*!< Node key refresh flag and iv update flag */ + uint32_t iv_index; /*!< Node IV Index */ + uint8_t dev_key[16]; /*!< Node device key */ + + /* Additional information */ + char name[ESP_BLE_MESH_NODE_NAME_MAX_LEN]; /*!< Node name */ + uint16_t comp_length; /*!< Length of Composition Data */ + uint8_t *comp_data; /*!< Value of Composition Data */ +} __attribute__((packed)) esp_ble_mesh_node_t; + /** Context of fast provisioning which need to be set. */ typedef struct { uint16_t unicast_min; /*!< Minimum unicast address used for fast provisioning */ @@ -734,16 +755,24 @@ typedef enum { ESP_BLE_MESH_PROVISIONER_PROV_LINK_CLOSE_EVT, /*!< Provisioner close a BLE Mesh link event */ ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT, /*!< Provisioner provisioning done event */ ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT, /*!< Provisioner add a device to the list which contains devices that are waiting/going to be provisioned completion event */ + ESP_BLE_MESH_PROVISIONER_PROV_DEV_WITH_ADDR_COMP_EVT, /*!< Provisioner start to provision an unprovisioned device completion event */ ESP_BLE_MESH_PROVISIONER_DELETE_DEV_COMP_EVT, /*!< Provisioner delete a device from the list, close provisioning link with the device if it exists and remove the device from network completion event */ ESP_BLE_MESH_PROVISIONER_SET_DEV_UUID_MATCH_COMP_EVT, /*!< Provisioner set the value to be compared with part of the unprovisioned device UUID completion event */ ESP_BLE_MESH_PROVISIONER_SET_PROV_DATA_INFO_COMP_EVT, /*!< Provisioner set net_idx/flags/iv_index used for provisioning completion event */ + ESP_BLE_MESH_PROVISIONER_SET_STATIC_OOB_VALUE_COMP_EVT, /*!< Provisioner set static oob value used for provisioning completion event */ + ESP_BLE_MESH_PROVISIONER_SET_PRIMARY_ELEM_ADDR_COMP_EVT, /*!< Provisioner set unicast address of primary element completion event */ ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_COMP_EVT, /*!< Provisioner read unprovisioned device OOB public key completion event */ ESP_BLE_MESH_PROVISIONER_PROV_INPUT_NUMBER_COMP_EVT, /*!< Provisioner input number completion event */ ESP_BLE_MESH_PROVISIONER_PROV_INPUT_STRING_COMP_EVT, /*!< Provisioner input string completion event */ ESP_BLE_MESH_PROVISIONER_SET_NODE_NAME_COMP_EVT, /*!< Provisioner set node name completion event */ ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT, /*!< Provisioner add local app key completion event */ + ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_APP_KEY_COMP_EVT, /*!< Provisioner update local app key completion event */ ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT, /*!< Provisioner bind local model with local app key completion event */ ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_NET_KEY_COMP_EVT, /*!< Provisioner add local network key completion event */ + ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_NET_KEY_COMP_EVT, /*!< Provisioner update local network key completion event */ + ESP_BLE_MESH_PROVISIONER_STORE_NODE_COMP_DATA_COMP_EVT, /*!< Provisioner store node composition data completion event */ + ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT, /*!< Provisioner delete node with uuid completion event */ + ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT, /*!< Provisioner delete node with unicast address completion event */ ESP_BLE_MESH_SET_FAST_PROV_INFO_COMP_EVT, /*!< Set fast provisioning information (e.g. unicast address range, net_idx, etc.) completion event */ ESP_BLE_MESH_SET_FAST_PROV_ACTION_COMP_EVT, /*!< Set fast provisioning action completion event */ ESP_BLE_MESH_HEARTBEAT_MESSAGE_RECV_EVT, /*!< Receive Heartbeat message event */ @@ -888,6 +917,7 @@ typedef union { uint16_t oob_info; /*!< OOB Info of the unprovisoned device */ uint8_t adv_type; /*!< Avertising type of the unprovisoned device */ esp_ble_mesh_prov_bearer_t bearer; /*!< Bearer of the unprovisoned device */ + int8_t rssi; /*!< RSSI of the received advertising packet */ } provisioner_recv_unprov_adv_pkt; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT */ /** * @brief ESP_BLE_MESH_PROVISIONER_PROV_ENABLE_COMP_EVT @@ -946,7 +976,7 @@ typedef union { * @brief ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT */ struct ble_mesh_provisioner_prov_comp_param { - int node_idx; /*!< Index of the provisioned device */ + uint16_t node_idx; /*!< Index of the provisioned device */ esp_ble_mesh_octet16_t device_uuid; /*!< Device UUID of the provisioned device */ uint16_t unicast_addr; /*!< Primary address of the provisioned device */ uint8_t element_num; /*!< Element count of the provisioned device */ @@ -958,6 +988,12 @@ typedef union { struct ble_mesh_provisioner_add_unprov_dev_comp_param { int err_code; /*!< Indicate the result of adding device into queue by the Provisioner */ } provisioner_add_unprov_dev_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_DEV_WITH_ADDR_COMP_EVT + */ + struct ble_mesh_provisioner_prov_dev_with_addr_comp_param { + int err_code; /*!< Indicate the result of Provisioner starting to provision a device */ + } provisioner_prov_dev_with_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_DEV_WITH_ADDR_COMP_EVT */ /** * @brief ESP_BLE_MESH_PROVISIONER_DELETE_DEV_COMP_EVT */ @@ -976,6 +1012,18 @@ typedef union { struct ble_mesh_provisioner_set_prov_data_info_comp_param { int err_code; /*!< Indicate the result of setting provisioning info by the Provisioner */ } provisioner_set_prov_data_info_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SET_PROV_DATA_INFO_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_SET_STATIC_OOB_VALUE_COMP_EVT + */ + struct ble_mesh_provisioner_set_static_oob_val_comp_param { + int err_code; /*!< Indicate the result of setting static oob value by the Provisioner */ + } provisioner_set_static_oob_val_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SET_STATIC_OOB_VALUE_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_SET_PRIMARY_ELEM_ADDR_COMP_EVT + */ + struct ble_mesh_provisioner_set_primary_elem_addr_comp_param { + int err_code; /*!< Indicate the result of setting unicast address of primary element by the Provisioner */ + } provisioner_set_primary_elem_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SET_PRIMARY_ELEM_ADDR_COMP_EVT */ /** * @brief ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_COMP_EVT */ @@ -999,7 +1047,7 @@ typedef union { */ struct ble_mesh_provisioner_set_node_name_comp_param { int err_code; /*!< Indicate the result of setting provisioned device name by the Provisioner */ - int node_index; /*!< Index of the provisioned device */ + uint16_t node_index; /*!< Index of the provisioned device */ } provisioner_set_node_name_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SET_NODE_NAME_COMP_EVT */ /** * @brief ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT @@ -1008,11 +1056,23 @@ typedef union { int err_code; /*!< Indicate the result of adding local AppKey by the Provisioner */ uint16_t app_idx; /*!< AppKey Index */ } provisioner_add_app_key_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_APP_KEY_COMP_EVT + */ + struct ble_mesh_provisioner_update_local_app_key_comp_param { + int err_code; /*!< Indicate the result of updating local AppKey by the Provisioner */ + uint16_t net_idx; /*!< NetKey Index */ + uint16_t app_idx; /*!< AppKey Index */ + } provisioner_update_app_key_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_APP_KEY_COMP_EVT */ /** * @brief ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT */ struct ble_mesh_provisioner_bind_local_mod_app_comp_param { int err_code; /*!< Indicate the result of binding AppKey with model by the Provisioner */ + uint16_t element_addr; /*!< Element address */ + uint16_t app_idx; /*!< AppKey Index */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ } provisioner_bind_app_key_to_model_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT */ /** * @brief ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_NET_KEY_COMP_EVT @@ -1021,6 +1081,34 @@ typedef union { int err_code; /*!< Indicate the result of adding local NetKey by the Provisioner */ uint16_t net_idx; /*!< NetKey Index */ } provisioner_add_net_key_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_NET_KEY_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_NET_KEY_COMP_EVT + */ + struct ble_mesh_provisioner_update_local_net_key_comp_param { + int err_code; /*!< Indicate the result of updating local NetKey by the Provisioner */ + uint16_t net_idx; /*!< NetKey Index */ + } provisioner_update_net_key_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_NET_KEY_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_STORE_NODE_COMP_DATA_COMP_EVT + */ + struct ble_mesh_provisioner_store_node_comp_data_comp_param { + int err_code; /*!< Indicate the result of storing node composition data by the Provisioner */ + uint16_t addr; /*!< Node element address */ + } provisioner_store_node_comp_data_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_STORE_NODE_COMP_DATA_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT + */ + struct ble_mesh_provisioner_delete_node_with_uuid_comp_data_comp_param { + int err_code; /*!< Indicate the result of deleting node with uuid by the Provisioner */ + uint8_t uuid[16]; /*!< Node device uuid */ + } provisioner_delete_node_with_uuid_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT + */ + struct ble_mesh_provisioner_delete_node_with_addr_comp_data_comp_param { + int err_code; /*!< Indicate the result of deleting node with unicast address by the Provisioner */ + uint16_t unicast_addr; /*!< Node unicast address */ + } provisioner_delete_node_with_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT */ /** * @brief ESP_BLE_MESH_SET_FAST_PROV_INFO_COMP_EVT */ @@ -1100,6 +1188,7 @@ typedef union { esp_ble_mesh_addr_type_t addr_type; /*!< Device address type */ uint16_t net_idx; /*!< Network ID related NetKey Index */ uint8_t net_id[8]; /*!< Network ID contained in the advertising packet */ + int8_t rssi; /*!< RSSI of the received advertising packet */ } proxy_client_recv_adv_pkt; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_RECV_ADV_PKT_EVT */ /** * @brief ESP_BLE_MESH_PROXY_CLIENT_CONNECTED_EVT @@ -1177,64 +1266,64 @@ typedef union { */ /*!< Foundation Models */ -#define ESP_BLE_MESH_MODEL_ID_CONFIG_SRV BLE_MESH_MODEL_ID_CFG_SRV -#define ESP_BLE_MESH_MODEL_ID_CONFIG_CLI BLE_MESH_MODEL_ID_CFG_CLI -#define ESP_BLE_MESH_MODEL_ID_HEALTH_SRV BLE_MESH_MODEL_ID_HEALTH_SRV -#define ESP_BLE_MESH_MODEL_ID_HEALTH_CLI BLE_MESH_MODEL_ID_HEALTH_CLI +#define ESP_BLE_MESH_MODEL_ID_CONFIG_SRV 0x0000 +#define ESP_BLE_MESH_MODEL_ID_CONFIG_CLI 0x0001 +#define ESP_BLE_MESH_MODEL_ID_HEALTH_SRV 0x0002 +#define ESP_BLE_MESH_MODEL_ID_HEALTH_CLI 0x0003 /*!< Models from the Mesh Model Specification */ -#define ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV BLE_MESH_MODEL_ID_GEN_ONOFF_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI BLE_MESH_MODEL_ID_GEN_ONOFF_CLI -#define ESP_BLE_MESH_MODEL_ID_GEN_LEVEL_SRV BLE_MESH_MODEL_ID_GEN_LEVEL_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_LEVEL_CLI BLE_MESH_MODEL_ID_GEN_LEVEL_CLI -#define ESP_BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI -#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI -#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI -#define ESP_BLE_MESH_MODEL_ID_GEN_BATTERY_SRV BLE_MESH_MODEL_ID_GEN_BATTERY_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_BATTERY_CLI BLE_MESH_MODEL_ID_GEN_BATTERY_CLI -#define ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_SRV BLE_MESH_MODEL_ID_GEN_LOCATION_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_CLI BLE_MESH_MODEL_ID_GEN_LOCATION_CLI -#define ESP_BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV -#define ESP_BLE_MESH_MODEL_ID_GEN_PROP_CLI BLE_MESH_MODEL_ID_GEN_PROP_CLI -#define ESP_BLE_MESH_MODEL_ID_SENSOR_SRV BLE_MESH_MODEL_ID_SENSOR_SRV -#define ESP_BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV -#define ESP_BLE_MESH_MODEL_ID_SENSOR_CLI BLE_MESH_MODEL_ID_SENSOR_CLI -#define ESP_BLE_MESH_MODEL_ID_TIME_SRV BLE_MESH_MODEL_ID_TIME_SRV -#define ESP_BLE_MESH_MODEL_ID_TIME_SETUP_SRV BLE_MESH_MODEL_ID_TIME_SETUP_SRV -#define ESP_BLE_MESH_MODEL_ID_TIME_CLI BLE_MESH_MODEL_ID_TIME_CLI -#define ESP_BLE_MESH_MODEL_ID_SCENE_SRV BLE_MESH_MODEL_ID_SCENE_SRV -#define ESP_BLE_MESH_MODEL_ID_SCENE_SETUP_SRV BLE_MESH_MODEL_ID_SCENE_SETUP_SRV -#define ESP_BLE_MESH_MODEL_ID_SCENE_CLI BLE_MESH_MODEL_ID_SCENE_CLI -#define ESP_BLE_MESH_MODEL_ID_SCHEDULER_SRV BLE_MESH_MODEL_ID_SCHEDULER_SRV -#define ESP_BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV -#define ESP_BLE_MESH_MODEL_ID_SCHEDULER_CLI BLE_MESH_MODEL_ID_SCHEDULER_CLI -#define ESP_BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI -#define ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_SRV BLE_MESH_MODEL_ID_LIGHT_CTL_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_CLI BLE_MESH_MODEL_ID_LIGHT_CTL_CLI -#define ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_SRV BLE_MESH_MODEL_ID_LIGHT_HSL_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_CLI BLE_MESH_MODEL_ID_LIGHT_HSL_CLI -#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_SRV BLE_MESH_MODEL_ID_LIGHT_XYL_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_CLI BLE_MESH_MODEL_ID_LIGHT_XYL_CLI -#define ESP_BLE_MESH_MODEL_ID_LIGHT_LC_SRV BLE_MESH_MODEL_ID_LIGHT_LC_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV -#define ESP_BLE_MESH_MODEL_ID_LIGHT_LC_CLI BLE_MESH_MODEL_ID_LIGHT_LC_CLI +#define ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV 0x1000 +#define ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI 0x1001 +#define ESP_BLE_MESH_MODEL_ID_GEN_LEVEL_SRV 0x1002 +#define ESP_BLE_MESH_MODEL_ID_GEN_LEVEL_CLI 0x1003 +#define ESP_BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV 0x1004 +#define ESP_BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI 0x1005 +#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV 0x1006 +#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV 0x1007 +#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI 0x1008 +#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV 0x1009 +#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV 0x100a +#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI 0x100b +#define ESP_BLE_MESH_MODEL_ID_GEN_BATTERY_SRV 0x100c +#define ESP_BLE_MESH_MODEL_ID_GEN_BATTERY_CLI 0x100d +#define ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_SRV 0x100e +#define ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV 0x100f +#define ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_CLI 0x1010 +#define ESP_BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV 0x1011 +#define ESP_BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV 0x1012 +#define ESP_BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV 0x1013 +#define ESP_BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV 0x1014 +#define ESP_BLE_MESH_MODEL_ID_GEN_PROP_CLI 0x1015 +#define ESP_BLE_MESH_MODEL_ID_SENSOR_SRV 0x1100 +#define ESP_BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV 0x1101 +#define ESP_BLE_MESH_MODEL_ID_SENSOR_CLI 0x1102 +#define ESP_BLE_MESH_MODEL_ID_TIME_SRV 0x1200 +#define ESP_BLE_MESH_MODEL_ID_TIME_SETUP_SRV 0x1201 +#define ESP_BLE_MESH_MODEL_ID_TIME_CLI 0x1202 +#define ESP_BLE_MESH_MODEL_ID_SCENE_SRV 0x1203 +#define ESP_BLE_MESH_MODEL_ID_SCENE_SETUP_SRV 0x1204 +#define ESP_BLE_MESH_MODEL_ID_SCENE_CLI 0x1205 +#define ESP_BLE_MESH_MODEL_ID_SCHEDULER_SRV 0x1206 +#define ESP_BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV 0x1207 +#define ESP_BLE_MESH_MODEL_ID_SCHEDULER_CLI 0x1208 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV 0x1300 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV 0x1301 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI 0x1302 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_SRV 0x1303 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV 0x1304 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_CLI 0x1305 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV 0x1306 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_SRV 0x1307 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV 0x1308 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_CLI 0x1309 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV 0x130a +#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV 0x130b +#define ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_SRV 0x130c +#define ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV 0x130d +#define ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_CLI 0x130e +#define ESP_BLE_MESH_MODEL_ID_LIGHT_LC_SRV 0x130f +#define ESP_BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV 0x1310 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_LC_CLI 0x1311 /** * esp_ble_mesh_opcode_config_client_get_t belongs to esp_ble_mesh_opcode_t, this typedef is only @@ -1243,25 +1332,25 @@ typedef union { */ typedef uint32_t esp_ble_mesh_opcode_config_client_get_t; -#define ESP_BLE_MESH_MODEL_OP_BEACON_GET OP_BEACON_GET /*!< Config Beacon Get */ -#define ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET OP_DEV_COMP_DATA_GET /*!< Config Composition Data Get */ -#define ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_GET OP_DEFAULT_TTL_GET /*!< Config Default TTL Get */ -#define ESP_BLE_MESH_MODEL_OP_GATT_PROXY_GET OP_GATT_PROXY_GET /*!< Config GATT Proxy Get */ -#define ESP_BLE_MESH_MODEL_OP_RELAY_GET OP_RELAY_GET /*!< Config Relay Get */ -#define ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET OP_MOD_PUB_GET /*!< Config Model Publication Get */ -#define ESP_BLE_MESH_MODEL_OP_FRIEND_GET OP_FRIEND_GET /*!< Config Friend Get */ -#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_GET OP_HEARTBEAT_PUB_GET /*!< Config Heartbeat Publication Get */ -#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_GET OP_HEARTBEAT_SUB_GET /*!< Config Heartbeat Subscription Get */ -#define ESP_BLE_MESH_MODEL_OP_NET_KEY_GET OP_NET_KEY_GET /*!< Config NetKey Get */ -#define ESP_BLE_MESH_MODEL_OP_APP_KEY_GET OP_APP_KEY_GET /*!< Config AppKey Get */ -#define ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_GET OP_NODE_IDENTITY_GET /*!< Config Node Identity Get */ -#define ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_GET OP_MOD_SUB_GET /*!< Config SIG Model Subscription Get */ -#define ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_GET OP_MOD_SUB_GET_VND /*!< Config Vendor Model Subscription Get */ -#define ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_GET OP_SIG_MOD_APP_GET /*!< Config SIG Model App Get */ -#define ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_GET OP_VND_MOD_APP_GET /*!< Config Vendor Model App Get */ -#define ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_GET OP_KRP_GET /*!< Config Key Refresh Phase Get */ -#define ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_GET OP_LPN_TIMEOUT_GET /*!< Config Low Power Node PollTimeout Get */ -#define ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_GET OP_NET_TRANSMIT_GET /*!< Config Network Transmit Get */ +#define ESP_BLE_MESH_MODEL_OP_BEACON_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x09) /*!< Config Beacon Get */ +#define ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x08) /*!< Config Composition Data Get */ +#define ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x0C) /*!< Config Default TTL Get */ +#define ESP_BLE_MESH_MODEL_OP_GATT_PROXY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x12) /*!< Config GATT Proxy Get */ +#define ESP_BLE_MESH_MODEL_OP_RELAY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x26) /*!< Config Relay Get */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x18) /*!< Config Model Publication Get */ +#define ESP_BLE_MESH_MODEL_OP_FRIEND_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x0F) /*!< Config Friend Get */ +#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x38) /*!< Config Heartbeat Publication Get */ +#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x3a) /*!< Config Heartbeat Subscription Get */ +#define ESP_BLE_MESH_MODEL_OP_NET_KEY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x42) /*!< Config NetKey Get */ +#define ESP_BLE_MESH_MODEL_OP_APP_KEY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x01) /*!< Config AppKey Get */ +#define ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x46) /*!< Config Node Identity Get */ +#define ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x29) /*!< Config SIG Model Subscription Get */ +#define ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x2B) /*!< Config Vendor Model Subscription Get */ +#define ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x4B) /*!< Config SIG Model App Get */ +#define ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x4D) /*!< Config Vendor Model App Get */ +#define ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x15) /*!< Config Key Refresh Phase Get */ +#define ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x2D) /*!< Config Low Power Node PollTimeout Get */ +#define ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x23) /*!< Config Network Transmit Get */ /** * esp_ble_mesh_opcode_config_client_set_t belongs to esp_ble_mesh_opcode_t, this typedef is @@ -1270,34 +1359,34 @@ typedef uint32_t esp_ble_mesh_opcode_config_client_get_t; */ typedef uint32_t esp_ble_mesh_opcode_config_client_set_t; -#define ESP_BLE_MESH_MODEL_OP_BEACON_SET OP_BEACON_SET /*!< Config Beacon Set */ -#define ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_SET OP_DEFAULT_TTL_SET /*!< Config Default TTL Set */ -#define ESP_BLE_MESH_MODEL_OP_GATT_PROXY_SET OP_GATT_PROXY_SET /*!< Config GATT Proxy Set */ -#define ESP_BLE_MESH_MODEL_OP_RELAY_SET OP_RELAY_SET /*!< Config Relay Set */ -#define ESP_BLE_MESH_MODEL_OP_MODEL_PUB_SET OP_MOD_PUB_SET /*!< Config Model Publication Set */ -#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD OP_MOD_SUB_ADD /*!< Config Model Subscription Add */ -#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_ADD OP_MOD_SUB_VA_ADD /*!< Config Model Subscription Vritual Address Add */ -#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE OP_MOD_SUB_DEL /*!< Config Model Subscription Delete */ -#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_DELETE OP_MOD_SUB_VA_DEL /*!< Config Model Subscription Virtual Address Delete */ -#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_OVERWRITE OP_MOD_SUB_OVERWRITE /*!< Config Model Subscription Overwrite */ -#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_OVERWRITE OP_MOD_SUB_VA_OVERWRITE /*!< Config Model Subscription Virtual Address Overwrite */ -#define ESP_BLE_MESH_MODEL_OP_NET_KEY_ADD OP_NET_KEY_ADD /*!< Config NetKey Add */ -#define ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD OP_APP_KEY_ADD /*!< Config AppKey Add */ -#define ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND OP_MOD_APP_BIND /*!< Config Model App Bind */ -#define ESP_BLE_MESH_MODEL_OP_NODE_RESET OP_NODE_RESET /*!< Config Node Reset */ -#define ESP_BLE_MESH_MODEL_OP_FRIEND_SET OP_FRIEND_SET /*!< Config Friend Set */ -#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_SET OP_HEARTBEAT_PUB_SET /*!< Config Heartbeat Publication Set */ -#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_SET OP_HEARTBEAT_SUB_SET /*!< Config Heartbeat Subscription Set */ -#define ESP_BLE_MESH_MODEL_OP_NET_KEY_UPDATE OP_NET_KEY_UPDATE /*!< Config NetKey Update */ -#define ESP_BLE_MESH_MODEL_OP_NET_KEY_DELETE OP_NET_KEY_DEL /*!< Config NetKey Delete */ -#define ESP_BLE_MESH_MODEL_OP_APP_KEY_UPDATE OP_APP_KEY_UPDATE /*!< Config AppKey Update */ -#define ESP_BLE_MESH_MODEL_OP_APP_KEY_DELETE OP_APP_KEY_DEL /*!< Config AppKey Delete */ -#define ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_SET OP_NODE_IDENTITY_SET /*!< Config Node Identity Set */ -#define ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_SET OP_KRP_SET /*!< Config Key Refresh Phase Set */ -#define ESP_BLE_MESH_MODEL_OP_MODEL_PUB_VIRTUAL_ADDR_SET OP_MOD_PUB_VA_SET /*!< Config Model Publication Virtual Address Set */ -#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE_ALL OP_MOD_SUB_DEL_ALL /*!< Config Model Subscription Delete All */ -#define ESP_BLE_MESH_MODEL_OP_MODEL_APP_UNBIND OP_MOD_APP_UNBIND /*!< Config Model App Unbind */ -#define ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_SET OP_NET_TRANSMIT_SET /*!< Config Network Transmit Set */ +#define ESP_BLE_MESH_MODEL_OP_BEACON_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x0A) /*!< Config Beacon Set */ +#define ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x0D) /*!< Config Default TTL Set */ +#define ESP_BLE_MESH_MODEL_OP_GATT_PROXY_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x13) /*!< Config GATT Proxy Set */ +#define ESP_BLE_MESH_MODEL_OP_RELAY_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x27) /*!< Config Relay Set */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_PUB_SET ESP_BLE_MESH_MODEL_OP_1(0x03) /*!< Config Model Publication Set */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD ESP_BLE_MESH_MODEL_OP_2(0x80, 0x1B) /*!< Config Model Subscription Add */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_ADD ESP_BLE_MESH_MODEL_OP_2(0x80, 0x20) /*!< Config Model Subscription Vritual Address Add */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x1C) /*!< Config Model Subscription Delete */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_DELETE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x21) /*!< Config Model Subscription Virtual Address Delete */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_OVERWRITE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x1E) /*!< Config Model Subscription Overwrite */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_OVERWRITE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x22) /*!< Config Model Subscription Virtual Address Overwrite */ +#define ESP_BLE_MESH_MODEL_OP_NET_KEY_ADD ESP_BLE_MESH_MODEL_OP_2(0x80, 0x40) /*!< Config NetKey Add */ +#define ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD ESP_BLE_MESH_MODEL_OP_1(0x00) /*!< Config AppKey Add */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND ESP_BLE_MESH_MODEL_OP_2(0x80, 0x3D) /*!< Config Model App Bind */ +#define ESP_BLE_MESH_MODEL_OP_NODE_RESET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x49) /*!< Config Node Reset */ +#define ESP_BLE_MESH_MODEL_OP_FRIEND_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x10) /*!< Config Friend Set */ +#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x39) /*!< Config Heartbeat Publication Set */ +#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x3B) /*!< Config Heartbeat Subscription Set */ +#define ESP_BLE_MESH_MODEL_OP_NET_KEY_UPDATE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x45) /*!< Config NetKey Update */ +#define ESP_BLE_MESH_MODEL_OP_NET_KEY_DELETE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x41) /*!< Config NetKey Delete */ +#define ESP_BLE_MESH_MODEL_OP_APP_KEY_UPDATE ESP_BLE_MESH_MODEL_OP_1(0x01) /*!< Config AppKey Update */ +#define ESP_BLE_MESH_MODEL_OP_APP_KEY_DELETE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x00) /*!< Config AppKey Delete */ +#define ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x47) /*!< Config Node Identity Set */ +#define ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x16) /*!< Config Key Refresh Phase Set */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_PUB_VIRTUAL_ADDR_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x1A) /*!< Config Model Publication Virtual Address Set */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE_ALL ESP_BLE_MESH_MODEL_OP_2(0x80, 0x1D) /*!< Config Model Subscription Delete All */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_APP_UNBIND ESP_BLE_MESH_MODEL_OP_2(0x80, 0x3F) /*!< Config Model App Unbind */ +#define ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x24) /*!< Config Network Transmit Set */ /** * esp_ble_mesh_opcode_config_status_t belongs to esp_ble_mesh_opcode_t, this typedef is only @@ -1307,30 +1396,30 @@ typedef uint32_t esp_ble_mesh_opcode_config_client_set_t; */ typedef uint32_t esp_ble_mesh_opcode_config_status_t; -#define ESP_BLE_MESH_MODEL_OP_BEACON_STATUS OP_BEACON_STATUS -#define ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_STATUS OP_DEV_COMP_DATA_STATUS -#define ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_STATUS OP_DEFAULT_TTL_STATUS -#define ESP_BLE_MESH_MODEL_OP_GATT_PROXY_STATUS OP_GATT_PROXY_STATUS -#define ESP_BLE_MESH_MODEL_OP_RELAY_STATUS OP_RELAY_STATUS -#define ESP_BLE_MESH_MODEL_OP_MODEL_PUB_STATUS OP_MOD_PUB_STATUS -#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_STATUS OP_MOD_SUB_STATUS -#define ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_LIST OP_MOD_SUB_LIST -#define ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_LIST OP_MOD_SUB_LIST_VND -#define ESP_BLE_MESH_MODEL_OP_NET_KEY_STATUS OP_NET_KEY_STATUS -#define ESP_BLE_MESH_MODEL_OP_NET_KEY_LIST OP_NET_KEY_LIST -#define ESP_BLE_MESH_MODEL_OP_APP_KEY_STATUS OP_APP_KEY_STATUS -#define ESP_BLE_MESH_MODEL_OP_APP_KEY_LIST OP_APP_KEY_LIST -#define ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_STATUS OP_NODE_IDENTITY_STATUS -#define ESP_BLE_MESH_MODEL_OP_MODEL_APP_STATUS OP_MOD_APP_STATUS -#define ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_LIST OP_SIG_MOD_APP_LIST -#define ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_LIST OP_VND_MOD_APP_LIST -#define ESP_BLE_MESH_MODEL_OP_NODE_RESET_STATUS OP_NODE_RESET_STATUS -#define ESP_BLE_MESH_MODEL_OP_FRIEND_STATUS OP_FRIEND_STATUS -#define ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_STATUS OP_KRP_STATUS -#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_STATUS OP_HEARTBEAT_PUB_STATUS -#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_STATUS OP_HEARTBEAT_SUB_STATUS -#define ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_STATUS OP_LPN_TIMEOUT_STATUS -#define ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_STATUS OP_NET_TRANSMIT_STATUS +#define ESP_BLE_MESH_MODEL_OP_BEACON_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x0B) +#define ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_STATUS ESP_BLE_MESH_MODEL_OP_1(0x02) +#define ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x0E) +#define ESP_BLE_MESH_MODEL_OP_GATT_PROXY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x14) +#define ESP_BLE_MESH_MODEL_OP_RELAY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x28) +#define ESP_BLE_MESH_MODEL_OP_MODEL_PUB_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x19) +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x1F) +#define ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x2A) +#define ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x2C) +#define ESP_BLE_MESH_MODEL_OP_NET_KEY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x44) +#define ESP_BLE_MESH_MODEL_OP_NET_KEY_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x43) +#define ESP_BLE_MESH_MODEL_OP_APP_KEY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x03) +#define ESP_BLE_MESH_MODEL_OP_APP_KEY_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x02) +#define ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x48) +#define ESP_BLE_MESH_MODEL_OP_MODEL_APP_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x3E) +#define ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x4C) +#define ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x4E) +#define ESP_BLE_MESH_MODEL_OP_NODE_RESET_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x4A) +#define ESP_BLE_MESH_MODEL_OP_FRIEND_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x11) +#define ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x17) +#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_STATUS ESP_BLE_MESH_MODEL_OP_1(0x06) +#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x3C) +#define ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x2E) +#define ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x25) /** * This typedef is only used to indicate the status code contained in some of @@ -1364,9 +1453,9 @@ typedef uint8_t esp_ble_mesh_cfg_status_t; */ typedef uint32_t esp_ble_mesh_opcode_health_client_get_t; -#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET OP_HEALTH_FAULT_GET /*!< Health Fault Get */ -#define ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_GET OP_HEALTH_PERIOD_GET /*!< Health Period Get */ -#define ESP_BLE_MESH_MODEL_OP_ATTENTION_GET OP_ATTENTION_GET /*!< Health Attention Get */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x31) /*!< Health Fault Get */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x34) /*!< Health Period Get */ +#define ESP_BLE_MESH_MODEL_OP_ATTENTION_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x04) /*!< Health Attention Get */ /** * esp_ble_mesh_opcode_health_client_set_t belongs to esp_ble_mesh_opcode_t, this typedef is @@ -1375,14 +1464,14 @@ typedef uint32_t esp_ble_mesh_opcode_health_client_get_t; */ typedef uint32_t esp_ble_mesh_opcode_health_client_set_t; -#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR OP_HEALTH_FAULT_CLEAR /*!< Health Fault Clear */ -#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR_UNACK OP_HEALTH_FAULT_CLEAR_UNREL /*!< Health Fault Clear Unacknowledged */ -#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST OP_HEALTH_FAULT_TEST /*!< Health Fault Test */ -#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST_UNACK OP_HEALTH_FAULT_TEST_UNREL /*!< Health Fault Test Unacknowledged */ -#define ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET OP_HEALTH_PERIOD_SET /*!< Health Period Set */ -#define ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET_UNACK OP_HEALTH_PERIOD_SET_UNREL /*!< Health Period Set Unacknowledged */ -#define ESP_BLE_MESH_MODEL_OP_ATTENTION_SET OP_ATTENTION_SET /*!< Health Attention Set */ -#define ESP_BLE_MESH_MODEL_OP_ATTENTION_SET_UNACK OP_ATTENTION_SET_UNREL /*!< Health Attention Set Unacknowledged */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR ESP_BLE_MESH_MODEL_OP_2(0x80, 0x2F) /*!< Health Fault Clear */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR_UNACK ESP_BLE_MESH_MODEL_OP_2(0x80, 0x30) /*!< Health Fault Clear Unacknowledged */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x32) /*!< Health Fault Test */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST_UNACK ESP_BLE_MESH_MODEL_OP_2(0x80, 0x33) /*!< Health Fault Test Unacknowledged */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x35) /*!< Health Period Set */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x80, 0x36) /*!< Health Period Set Unacknowledged */ +#define ESP_BLE_MESH_MODEL_OP_ATTENTION_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x05) /*!< Health Attention Set */ +#define ESP_BLE_MESH_MODEL_OP_ATTENTION_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x80, 0x06) /*!< Health Attention Set Unacknowledged */ /** * esp_ble_mesh_health_model_status_t belongs to esp_ble_mesh_opcode_t, this typedef is @@ -1392,10 +1481,10 @@ typedef uint32_t esp_ble_mesh_opcode_health_client_set_t; */ typedef uint32_t esp_ble_mesh_health_model_status_t; -#define ESP_BLE_MESH_MODEL_OP_HEALTH_CURRENT_STATUS OP_HEALTH_CURRENT_STATUS -#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_STATUS OP_HEALTH_FAULT_STATUS -#define ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_STATUS OP_HEALTH_PERIOD_STATUS -#define ESP_BLE_MESH_MODEL_OP_ATTENTION_STATUS OP_ATTENTION_STATUS +#define ESP_BLE_MESH_MODEL_OP_HEALTH_CURRENT_STATUS ESP_BLE_MESH_MODEL_OP_1(0x04) +#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_STATUS ESP_BLE_MESH_MODEL_OP_1(0x05) +#define ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x37) +#define ESP_BLE_MESH_MODEL_OP_ATTENTION_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x07) /** * esp_ble_mesh_generic_message_opcode_t belongs to esp_ble_mesh_opcode_t, this typedef is @@ -1405,96 +1494,96 @@ typedef uint32_t esp_ble_mesh_health_model_status_t; typedef uint32_t esp_ble_mesh_generic_message_opcode_t; /*!< Generic OnOff Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET BLE_MESH_MODEL_OP_GEN_ONOFF_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET BLE_MESH_MODEL_OP_GEN_ONOFF_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS +#define ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x01) +#define ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x02) +#define ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x03) +#define ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x04) /*!< Generic Level Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_GET BLE_MESH_MODEL_OP_GEN_LEVEL_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_SET BLE_MESH_MODEL_OP_GEN_LEVEL_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS -#define ESP_BLE_MESH_MODEL_OP_GEN_DELTA_SET BLE_MESH_MODEL_OP_GEN_DELTA_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_GEN_MOVE_SET BLE_MESH_MODEL_OP_GEN_MOVE_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK +#define ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x05) +#define ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x06) +#define ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x07) +#define ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x08) +#define ESP_BLE_MESH_MODEL_OP_GEN_DELTA_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x09) +#define ESP_BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x0A) +#define ESP_BLE_MESH_MODEL_OP_GEN_MOVE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x0B) +#define ESP_BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x0C) /*!< Generic Default Transition Time Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS +#define ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x0D) +#define ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x0E) +#define ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x0F) +#define ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x10) /*!< Generic Power OnOff Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS +#define ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x11) +#define ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x12) /*!< Generic Power OnOff Setup Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK +#define ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x13) +#define ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x14) /*!< Generic Power Level Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x15) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x16) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x17) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x18) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x19) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x1A) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x1B) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x1C) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x1D) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x1E) /*!< Generic Power Level Setup Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x1F) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x20) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x21) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x22) /*!< Generic Battery Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_BATTERY_GET BLE_MESH_MODEL_OP_GEN_BATTERY_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS +#define ESP_BLE_MESH_MODEL_OP_GEN_BATTERY_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x23) +#define ESP_BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x24) /*!< Generic Location Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS -#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x25) +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS ESP_BLE_MESH_MODEL_OP_1(0x40) +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x26) +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x27) /*!< Generic Location Setup Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET ESP_BLE_MESH_MODEL_OP_1(0x41) +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x42) +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x28) +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x29) /*!< Generic Manufacturer Property Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTIES_GET BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTIES_STATUS BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS -#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_GET BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_SET BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_STATUS BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS +#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTIES_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x2A) +#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTIES_STATUS ESP_BLE_MESH_MODEL_OP_1(0x43) +#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x2B) +#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_SET ESP_BLE_MESH_MODEL_OP_1(0x44) +#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x45) +#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_STATUS ESP_BLE_MESH_MODEL_OP_1(0x46) /*!< Generic Admin Property Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS -#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS +#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x2C) +#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS ESP_BLE_MESH_MODEL_OP_1(0x47) +#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x2D) +#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET ESP_BLE_MESH_MODEL_OP_1(0x48) +#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x49) +#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS ESP_BLE_MESH_MODEL_OP_1(0x4A) /*!< Generic User Property Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS -#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET -#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS +#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x2E) +#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS ESP_BLE_MESH_MODEL_OP_1(0x4B) +#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x2F) +#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET ESP_BLE_MESH_MODEL_OP_1(0x4C) +#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x4D) +#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS ESP_BLE_MESH_MODEL_OP_1(0x4E) /*!< Generic Client Property Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET -#define ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS +#define ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET ESP_BLE_MESH_MODEL_OP_1(0x4F) +#define ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS ESP_BLE_MESH_MODEL_OP_1(0x50) /** * esp_ble_mesh_sensor_message_opcode_t belongs to esp_ble_mesh_opcode_t, this typedef is @@ -1504,26 +1593,26 @@ typedef uint32_t esp_ble_mesh_generic_message_opcode_t; typedef uint32_t esp_ble_mesh_sensor_message_opcode_t; /*!< Sensor Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET -#define ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS -#define ESP_BLE_MESH_MODEL_OP_SENSOR_GET BLE_MESH_MODEL_OP_SENSOR_GET -#define ESP_BLE_MESH_MODEL_OP_SENSOR_STATUS BLE_MESH_MODEL_OP_SENSOR_STATUS -#define ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET -#define ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS -#define ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_GET BLE_MESH_MODEL_OP_SENSOR_SERIES_GET -#define ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS +#define ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x30) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS ESP_BLE_MESH_MODEL_OP_1(0x51) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x31) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_STATUS ESP_BLE_MESH_MODEL_OP_1(0x52) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x32) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS ESP_BLE_MESH_MODEL_OP_1(0x53) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x33) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS ESP_BLE_MESH_MODEL_OP_1(0x54) /*!< Sensor Setup Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET -#define ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET -#define ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS -#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET -#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS -#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_GET BLE_MESH_MODEL_OP_SENSOR_SETTING_GET -#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET BLE_MESH_MODEL_OP_SENSOR_SETTING_SET -#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS +#define ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x34) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET ESP_BLE_MESH_MODEL_OP_1(0x55) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x56) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS ESP_BLE_MESH_MODEL_OP_1(0x57) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x35) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS ESP_BLE_MESH_MODEL_OP_1(0x58) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x36) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET ESP_BLE_MESH_MODEL_OP_1(0x59) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x5A) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS ESP_BLE_MESH_MODEL_OP_1(0x5B) /** * esp_ble_mesh_time_scene_message_opcode_t belongs to esp_ble_mesh_opcode_t, this typedef is @@ -1533,42 +1622,42 @@ typedef uint32_t esp_ble_mesh_sensor_message_opcode_t; typedef uint32_t esp_ble_mesh_time_scene_message_opcode_t; /*!< Time Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_TIME_GET BLE_MESH_MODEL_OP_TIME_GET -#define ESP_BLE_MESH_MODEL_OP_TIME_SET BLE_MESH_MODEL_OP_TIME_SET -#define ESP_BLE_MESH_MODEL_OP_TIME_STATUS BLE_MESH_MODEL_OP_TIME_STATUS -#define ESP_BLE_MESH_MODEL_OP_TIME_ROLE_GET BLE_MESH_MODEL_OP_TIME_ROLE_GET -#define ESP_BLE_MESH_MODEL_OP_TIME_ROLE_SET BLE_MESH_MODEL_OP_TIME_ROLE_SET -#define ESP_BLE_MESH_MODEL_OP_TIME_ROLE_STATUS BLE_MESH_MODEL_OP_TIME_ROLE_STATUS -#define ESP_BLE_MESH_MODEL_OP_TIME_ZONE_GET BLE_MESH_MODEL_OP_TIME_ZONE_GET -#define ESP_BLE_MESH_MODEL_OP_TIME_ZONE_SET BLE_MESH_MODEL_OP_TIME_ZONE_SET -#define ESP_BLE_MESH_MODEL_OP_TIME_ZONE_STATUS BLE_MESH_MODEL_OP_TIME_ZONE_STATUS -#define ESP_BLE_MESH_MODEL_OP_TAI_UTC_DELTA_GET BLE_MESH_MODEL_OP_TAI_UTC_DELTA_GET -#define ESP_BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET -#define ESP_BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS +#define ESP_BLE_MESH_MODEL_OP_TIME_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x37) +#define ESP_BLE_MESH_MODEL_OP_TIME_SET ESP_BLE_MESH_MODEL_OP_1(0x5C) +#define ESP_BLE_MESH_MODEL_OP_TIME_STATUS ESP_BLE_MESH_MODEL_OP_1(0x5D) +#define ESP_BLE_MESH_MODEL_OP_TIME_ROLE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x38) +#define ESP_BLE_MESH_MODEL_OP_TIME_ROLE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x39) +#define ESP_BLE_MESH_MODEL_OP_TIME_ROLE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x3A) +#define ESP_BLE_MESH_MODEL_OP_TIME_ZONE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x3B) +#define ESP_BLE_MESH_MODEL_OP_TIME_ZONE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x3C) +#define ESP_BLE_MESH_MODEL_OP_TIME_ZONE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x3D) +#define ESP_BLE_MESH_MODEL_OP_TAI_UTC_DELTA_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x3E) +#define ESP_BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x3F) +#define ESP_BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x40) /*!< Scene Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_SCENE_GET BLE_MESH_MODEL_OP_SCENE_GET -#define ESP_BLE_MESH_MODEL_OP_SCENE_RECALL BLE_MESH_MODEL_OP_SCENE_RECALL -#define ESP_BLE_MESH_MODEL_OP_SCENE_RECALL_UNACK BLE_MESH_MODEL_OP_SCENE_RECALL_UNACK -#define ESP_BLE_MESH_MODEL_OP_SCENE_STATUS BLE_MESH_MODEL_OP_SCENE_STATUS -#define ESP_BLE_MESH_MODEL_OP_SCENE_REGISTER_GET BLE_MESH_MODEL_OP_SCENE_REGISTER_GET -#define ESP_BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS +#define ESP_BLE_MESH_MODEL_OP_SCENE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x41) +#define ESP_BLE_MESH_MODEL_OP_SCENE_RECALL ESP_BLE_MESH_MODEL_OP_2(0x82, 0x42) +#define ESP_BLE_MESH_MODEL_OP_SCENE_RECALL_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x43) +#define ESP_BLE_MESH_MODEL_OP_SCENE_STATUS ESP_BLE_MESH_MODEL_OP_1(0x5E) +#define ESP_BLE_MESH_MODEL_OP_SCENE_REGISTER_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x44) +#define ESP_BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x45) /*!< Scene Setup Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_SCENE_STORE BLE_MESH_MODEL_OP_SCENE_STORE -#define ESP_BLE_MESH_MODEL_OP_SCENE_STORE_UNACK BLE_MESH_MODEL_OP_SCENE_STORE_UNACK -#define ESP_BLE_MESH_MODEL_OP_SCENE_DELETE BLE_MESH_MODEL_OP_SCENE_DELETE -#define ESP_BLE_MESH_MODEL_OP_SCENE_DELETE_UNACK BLE_MESH_MODEL_OP_SCENE_DELETE_UNACK +#define ESP_BLE_MESH_MODEL_OP_SCENE_STORE ESP_BLE_MESH_MODEL_OP_2(0x82, 0x46) +#define ESP_BLE_MESH_MODEL_OP_SCENE_STORE_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x47) +#define ESP_BLE_MESH_MODEL_OP_SCENE_DELETE ESP_BLE_MESH_MODEL_OP_2(0x82, 0x9E) +#define ESP_BLE_MESH_MODEL_OP_SCENE_DELETE_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x9F) /*!< Scheduler Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET -#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS -#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_GET BLE_MESH_MODEL_OP_SCHEDULER_GET -#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_STATUS BLE_MESH_MODEL_OP_SCHEDULER_STATUS +#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x48) +#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS ESP_BLE_MESH_MODEL_OP_1(0x5F) +#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x49) +#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x4A) /*!< Scheduler Setup Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET -#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET_UNACK BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET_UNACK +#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET ESP_BLE_MESH_MODEL_OP_1(0x60) +#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x61) /** * esp_ble_mesh_light_message_opcode_t belongs to esp_ble_mesh_opcode_t, this typedef is @@ -1578,108 +1667,108 @@ typedef uint32_t esp_ble_mesh_time_scene_message_opcode_t; typedef uint32_t esp_ble_mesh_light_message_opcode_t; /*!< Light Lightness Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x4B) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x4C) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x4D) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x4E) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x4F) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x50) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x51) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x52) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x53) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x54) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x55) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x56) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x57) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x58) /*!< Light Lightness Setup Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET_UNACK +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x59) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x5A) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x5B) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x5C) /*!< Light CTL Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_GET BLE_MESH_MODEL_OP_LIGHT_CTL_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_SET BLE_MESH_MODEL_OP_LIGHT_CTL_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_CTL_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x5D) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x5E) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x5F) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x60) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x61) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x62) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x63) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x64) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x65) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x66) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x67) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x68) /*!< Light CTL Setup Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x69) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x6A) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x6B) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x6C) /*!< Light HSL Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_GET BLE_MESH_MODEL_OP_LIGHT_HSL_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SET BLE_MESH_MODEL_OP_LIGHT_HSL_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_HSL_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x6D) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x6E) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x6F) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x70) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x71) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x72) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x73) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x74) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x75) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x76) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x77) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x78) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x79) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x7A) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x7B) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x7C) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x7D) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x7E) /*!< Light HSL Setup Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET_UNACK /* Model spec is wrong */ +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x7F) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x80) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x81) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x82) /*!< Light xyL Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_GET BLE_MESH_MODEL_OP_LIGHT_XYL_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_SET BLE_MESH_MODEL_OP_LIGHT_XYL_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_XYL_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x83) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x84) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x85) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x86) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x87) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x88) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x89) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x8A) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x8B) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x8C) /*!< Light xyL Setup Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET_UNACK +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x8D) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x8E) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x8F) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x90) /*!< Light Control Message Opcode */ -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK -#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x91) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x92) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x93) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x94) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x95) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x96) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x97) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x98) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x99) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x9A) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x9B) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x9C) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x9D) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET ESP_BLE_MESH_MODEL_OP_1(0x62) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x63) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS ESP_BLE_MESH_MODEL_OP_1(0x64) typedef uint32_t esp_ble_mesh_opcode_t; /*!< End of defines of esp_ble_mesh_opcode_t */ diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c index 3b997e113..e6870d086 100644 --- a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c @@ -14,7 +14,6 @@ #include -#include "btc/btc_task.h" #include "btc/btc_manage.h" #include "btc_ble_mesh_config_model.h" @@ -34,13 +33,33 @@ esp_err_t esp_ble_mesh_register_config_server_callback(esp_ble_mesh_cfg_server_c return (btc_profile_cb_set(BTC_PID_CONFIG_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); } +static bool config_client_get_need_param(esp_ble_mesh_opcode_t opcode) +{ + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET: + case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET: + case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_GET: + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_GET: + case ESP_BLE_MESH_MODEL_OP_APP_KEY_GET: + case ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_GET: + case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_GET: + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_GET: + case ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_GET: + case ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_GET: + return true; + default: + return false; + } +} + esp_err_t esp_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_t *params, esp_ble_mesh_cfg_client_get_state_t *get_state) { btc_ble_mesh_config_client_args_t arg = {0}; btc_msg_t msg = {0}; - if (!params || !params->model || !params->ctx.addr || !get_state) { + if (!params || !params->model || !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr) || + (config_client_get_need_param(params->opcode) && !get_state)) { return ESP_ERR_INVALID_ARG; } @@ -62,7 +81,8 @@ esp_err_t esp_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_ btc_ble_mesh_config_client_args_t arg = {0}; btc_msg_t msg = {0}; - if (!params || !params->model || !params->ctx.addr || !set_state) { + if (!params || !params->model || !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr) || + (params->opcode != ESP_BLE_MESH_MODEL_OP_NODE_RESET && !set_state)) { return ESP_ERR_INVALID_ARG; } diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_generic_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_generic_model_api.c index 1bac6aea3..cb2b22a95 100644 --- a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_generic_model_api.c +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_generic_model_api.c @@ -14,7 +14,6 @@ #include -#include "btc/btc_task.h" #include "btc/btc_manage.h" #include "btc_ble_mesh_generic_model.h" @@ -27,13 +26,27 @@ esp_err_t esp_ble_mesh_register_generic_client_callback(esp_ble_mesh_generic_cli return (btc_profile_cb_set(BTC_PID_GENERIC_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); } +static bool generic_client_get_need_param(esp_ble_mesh_opcode_t opcode) +{ + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET: + return true; + default: + return false; + } +} + esp_err_t esp_ble_mesh_generic_client_get_state(esp_ble_mesh_client_common_param_t *params, esp_ble_mesh_generic_client_get_state_t *get_state) { btc_ble_mesh_generic_client_args_t arg = {0}; btc_msg_t msg = {0}; - if (!params || !params->model || !params->ctx.addr || !get_state) { + if (!params || !params->model || !params->ctx.addr || + (generic_client_get_need_param(params->opcode) && !get_state)) { return ESP_ERR_INVALID_ARG; } diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_health_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_health_model_api.c index 59e73c224..506e906c1 100644 --- a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_health_model_api.c +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_health_model_api.c @@ -14,7 +14,6 @@ #include -#include "btc/btc_task.h" #include "btc/btc_manage.h" #include "btc_ble_mesh_health_model.h" @@ -40,7 +39,8 @@ esp_err_t esp_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param_ btc_ble_mesh_health_client_args_t arg = {0}; btc_msg_t msg = {0}; - if (!params || !params->model || !params->ctx.addr || !get_state) { + if (!params || !params->model || !params->ctx.addr || (!get_state && + params->opcode == ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET)) { return ESP_ERR_INVALID_ARG; } diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c index 05317e307..c7847cc26 100644 --- a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c @@ -14,7 +14,6 @@ #include -#include "btc/btc_task.h" #include "btc/btc_manage.h" #include "btc_ble_mesh_lighting_model.h" @@ -33,7 +32,8 @@ esp_err_t esp_ble_mesh_light_client_get_state(esp_ble_mesh_client_common_param_t btc_ble_mesh_lighting_client_args_t arg = {0}; btc_msg_t msg = {0}; - if (!params || !params->model || !params->ctx.addr || !get_state) { + if (!params || !params->model || !params->ctx.addr || (!get_state && + params->opcode == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET)) { return ESP_ERR_INVALID_ARG; } diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_sensor_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_sensor_model_api.c index c4e8743de..2d23d37db 100644 --- a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_sensor_model_api.c +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_sensor_model_api.c @@ -14,7 +14,6 @@ #include -#include "btc/btc_task.h" #include "btc/btc_manage.h" #include "btc_ble_mesh_sensor_model.h" diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_time_scene_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_time_scene_model_api.c index e9afacdea..80347934d 100644 --- a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_time_scene_model_api.c +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_time_scene_model_api.c @@ -14,7 +14,6 @@ #include -#include "btc/btc_task.h" #include "btc/btc_manage.h" #include "btc_ble_mesh_time_scene_model.h" @@ -33,7 +32,8 @@ esp_err_t esp_ble_mesh_time_scene_client_get_state(esp_ble_mesh_client_common_pa btc_ble_mesh_time_scene_client_args_t arg = {0}; btc_msg_t msg = {0}; - if (!params || !params->model || !params->ctx.addr || !get_state) { + if (!params || !params->model || !params->ctx.addr || (!get_state && + params->opcode == ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET)) { return ESP_ERR_INVALID_ARG; } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c index 7cd73b942..20a86c8fa 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c @@ -15,11 +15,10 @@ #include #include -#include "btc/btc_manage.h" -#include "osi/allocator.h" - -#include "cfg_cli.h" +#include "foundation.h" #include "mesh_common.h" +#include "cfg_cli.h" + #include "btc_ble_mesh_config_model.h" #include "esp_ble_mesh_config_model_api.h" @@ -45,39 +44,53 @@ void btc_ble_mesh_config_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void btc_ble_mesh_config_client_args_t *src = (btc_ble_mesh_config_client_args_t *)p_src; if (!msg || !dst || !src) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } switch (msg->act) { case BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE: { - dst->cfg_client_get_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - dst->cfg_client_get_state.get_state = (esp_ble_mesh_cfg_client_get_state_t *)osi_malloc(sizeof(esp_ble_mesh_cfg_client_get_state_t)); - if (dst->cfg_client_get_state.params && dst->cfg_client_get_state.get_state) { + dst->cfg_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->cfg_client_get_state.params) { memcpy(dst->cfg_client_get_state.params, src->cfg_client_get_state.params, sizeof(esp_ble_mesh_client_common_param_t)); - memcpy(dst->cfg_client_get_state.get_state, src->cfg_client_get_state.get_state, - sizeof(esp_ble_mesh_cfg_client_get_state_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + break; + } + if (src->cfg_client_get_state.get_state) { + dst->cfg_client_get_state.get_state = (esp_ble_mesh_cfg_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_cfg_client_get_state_t)); + if (dst->cfg_client_get_state.get_state) { + memcpy(dst->cfg_client_get_state.get_state, src->cfg_client_get_state.get_state, + sizeof(esp_ble_mesh_cfg_client_get_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } } break; } case BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE: { - dst->cfg_client_set_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - dst->cfg_client_set_state.set_state = (esp_ble_mesh_cfg_client_set_state_t *)osi_malloc(sizeof(esp_ble_mesh_cfg_client_set_state_t)); - if (dst->cfg_client_set_state.params && dst->cfg_client_set_state.set_state) { + dst->cfg_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->cfg_client_set_state.params) { memcpy(dst->cfg_client_set_state.params, src->cfg_client_set_state.params, sizeof(esp_ble_mesh_client_common_param_t)); - memcpy(dst->cfg_client_set_state.set_state, src->cfg_client_set_state.set_state, - sizeof(esp_ble_mesh_cfg_client_set_state_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + break; + } + if (src->cfg_client_set_state.set_state) { + dst->cfg_client_set_state.set_state = (esp_ble_mesh_cfg_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_cfg_client_set_state_t)); + if (dst->cfg_client_set_state.set_state) { + memcpy(dst->cfg_client_set_state.set_state, src->cfg_client_set_state.set_state, + sizeof(esp_ble_mesh_cfg_client_set_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } } break; } default: - LOG_DEBUG("%s, Unknown deep copy act %d", __func__, msg->act); + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); break; } } @@ -87,7 +100,7 @@ static void btc_ble_mesh_config_client_arg_deep_free(btc_msg_t *msg) btc_ble_mesh_config_client_args_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -96,18 +109,18 @@ static void btc_ble_mesh_config_client_arg_deep_free(btc_msg_t *msg) switch (msg->act) { case BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE: if (arg->cfg_client_get_state.params) { - osi_free(arg->cfg_client_get_state.params); + bt_mesh_free(arg->cfg_client_get_state.params); } if (arg->cfg_client_get_state.get_state) { - osi_free(arg->cfg_client_get_state.get_state); + bt_mesh_free(arg->cfg_client_get_state.get_state); } break; case BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE: if (arg->cfg_client_set_state.params) { - osi_free(arg->cfg_client_set_state.params); + bt_mesh_free(arg->cfg_client_set_state.params); } if (arg->cfg_client_set_state.set_state) { - osi_free(arg->cfg_client_set_state.set_state); + bt_mesh_free(arg->cfg_client_set_state.set_state); } break; default: @@ -119,17 +132,17 @@ static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_des { esp_ble_mesh_cfg_client_cb_param_t *p_dest_data = (esp_ble_mesh_cfg_client_cb_param_t *)p_dest; esp_ble_mesh_cfg_client_cb_param_t *p_src_data = (esp_ble_mesh_cfg_client_cb_param_t *)p_src; - u16_t length; + u16_t length = 0U; if (!msg || !p_src_data || !p_dest_data) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } if (p_src_data->params) { - p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); if (!p_dest_data->params) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } @@ -148,7 +161,7 @@ static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.comp_data_status.composition_data->len; p_dest_data->status_cb.comp_data_status.composition_data = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.comp_data_status.composition_data) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.comp_data_status.composition_data, @@ -164,7 +177,7 @@ static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.model_sub_list.sub_addr->len; p_dest_data->status_cb.model_sub_list.sub_addr = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.model_sub_list.sub_addr) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.model_sub_list.sub_addr, @@ -178,7 +191,7 @@ static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.netkey_list.net_idx->len; p_dest_data->status_cb.netkey_list.net_idx = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.netkey_list.net_idx) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.netkey_list.net_idx, @@ -192,7 +205,7 @@ static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.appkey_list.app_idx->len; p_dest_data->status_cb.appkey_list.app_idx = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.appkey_list.app_idx) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.appkey_list.app_idx, @@ -208,7 +221,7 @@ static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.model_app_list.app_idx->len; p_dest_data->status_cb.model_app_list.app_idx = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.model_app_list.app_idx) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.model_app_list.app_idx, @@ -232,7 +245,7 @@ static void btc_ble_mesh_config_client_free_req_data(btc_msg_t *msg) esp_ble_mesh_cfg_client_cb_param_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -274,7 +287,7 @@ static void btc_ble_mesh_config_client_free_req_data(btc_msg_t *msg) } case ESP_BLE_MESH_CFG_CLIENT_TIMEOUT_EVT: if (arg->params) { - osi_free(arg->params); + bt_mesh_free(arg->params); } break; default: @@ -286,7 +299,7 @@ static void btc_ble_mesh_config_client_callback(esp_ble_mesh_cfg_client_cb_param { btc_msg_t msg = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_CONFIG_CLIENT)) { @@ -308,11 +321,11 @@ void bt_mesh_config_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, { esp_ble_mesh_cfg_client_cb_param_t cb_params = {0}; esp_ble_mesh_client_common_param_t params = {0}; - size_t length; - uint8_t act; + size_t length = 0U; + uint8_t act = 0U; if (!model || !ctx) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -330,7 +343,7 @@ void bt_mesh_config_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, act = ESP_BLE_MESH_CFG_CLIENT_TIMEOUT_EVT; break; default: - LOG_ERROR("%s, Unknown config client event type %d", __func__, evt_type); + BT_ERR("%s, Unknown config client event type %d", __func__, evt_type); return; } @@ -361,7 +374,7 @@ void btc_ble_mesh_config_client_publish_callback(u32_t opcode, struct net_buf_simple *buf) { if (!model || !ctx || !buf) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -377,10 +390,30 @@ static int btc_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param struct bt_mesh_msg_ctx ctx = {0}; if (!params || !cb) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } + switch (params->opcode) { + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET: + case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET: + case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_GET: + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_GET: + case ESP_BLE_MESH_MODEL_OP_APP_KEY_GET: + case ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_GET: + case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_GET: + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_GET: + case ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_GET: + case ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_GET: + if (get == NULL) { + BT_ERR("%s, Invalid config client get", __func__); + return -EINVAL; + } + break; + default: + break; + } + ctx.net_idx = params->ctx.net_idx; ctx.app_idx = BLE_MESH_KEY_DEV; ctx.addr = params->ctx.addr; @@ -439,7 +472,7 @@ static int btc_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param case ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_GET: return (cb->error_code = bt_mesh_cfg_net_transmit_get(&ctx)); default: - LOG_ERROR("%s, Invalid opcode 0x%x", __func__, params->opcode); + BT_ERR("%s, Invalid opcode 0x%x", __func__, params->opcode); return (cb->error_code = -EINVAL); } @@ -452,8 +485,13 @@ static int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param { struct bt_mesh_msg_ctx ctx = {0}; - if (!params || !set || !cb) { - LOG_ERROR("%s, Invalid parameter", __func__); + if (!params || !cb) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (params->opcode != ESP_BLE_MESH_MODEL_OP_NODE_RESET && set == NULL) { + BT_ERR("%s, Invalid config client set", __func__); return -EINVAL; } @@ -592,7 +630,7 @@ static int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param return (cb->error_code = bt_mesh_cfg_net_transmit_set(&ctx, set->net_transmit_set.net_transmit)); default: - LOG_ERROR("%s, Invalid opcode 0x%x", __func__, params->opcode); + BT_ERR("%s, Invalid opcode 0x%x", __func__, params->opcode); return (cb->error_code = -EINVAL); } @@ -606,7 +644,7 @@ void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg) bt_mesh_role_param_t role_param = {0}; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -618,7 +656,7 @@ void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg) role_param.model = (struct bt_mesh_model *)cb.params->model; role_param.role = cb.params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } btc_ble_mesh_config_client_get_state(arg->cfg_client_get_state.params, @@ -634,7 +672,7 @@ void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg) role_param.model = (struct bt_mesh_model *)cb.params->model; role_param.role = cb.params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } btc_ble_mesh_config_client_set_state(arg->cfg_client_set_state.params, @@ -658,7 +696,7 @@ void btc_ble_mesh_config_client_cb_handler(btc_msg_t *msg) esp_ble_mesh_cfg_client_cb_param_t *param = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -667,7 +705,7 @@ void btc_ble_mesh_config_client_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_CFG_CLIENT_EVT_MAX) { btc_ble_mesh_config_client_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } btc_ble_mesh_config_client_free_req_data(msg); @@ -690,7 +728,7 @@ static void btc_ble_mesh_config_server_callback(esp_ble_mesh_cfg_server_cb_param { btc_msg_t msg = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_CONFIG_SERVER)) { @@ -710,11 +748,11 @@ void bt_mesh_config_server_cb_evt_to_btc(u8_t evt_type, const u8_t *val, size_t len) { esp_ble_mesh_cfg_server_cb_param_t cb_params = {0}; - size_t length; - uint8_t act; + size_t length = 0U; + uint8_t act = 0U; if (!model || !ctx) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -723,7 +761,7 @@ void bt_mesh_config_server_cb_evt_to_btc(u8_t evt_type, act = ESP_BLE_MESH_CFG_SERVER_STATE_CHANGE_EVT; break; default: - LOG_ERROR("%s, Unknown config server event type %d", __func__, evt_type); + BT_ERR("%s, Unknown config server event type %d", __func__, evt_type); return; } @@ -749,7 +787,7 @@ void btc_ble_mesh_config_server_cb_handler(btc_msg_t *msg) esp_ble_mesh_cfg_server_cb_param_t *param = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -758,6 +796,6 @@ void btc_ble_mesh_config_server_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_CFG_SERVER_EVT_MAX) { btc_ble_mesh_config_server_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } } diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c index a761c7722..a9e0d9049 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c @@ -15,10 +15,9 @@ #include #include -#include "btc/btc_manage.h" -#include "osi/allocator.h" - +#include "mesh_common.h" #include "generic_client.h" + #include "btc_ble_mesh_generic_model.h" #include "esp_ble_mesh_generic_model_api.h" @@ -38,30 +37,37 @@ void btc_ble_mesh_generic_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, voi { btc_ble_mesh_generic_client_args_t *dst = (btc_ble_mesh_generic_client_args_t *)p_dest; btc_ble_mesh_generic_client_args_t *src = (btc_ble_mesh_generic_client_args_t *)p_src; - u16_t length; + u16_t length = 0U; if (!msg || !dst || !src) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } switch (msg->act) { case BTC_BLE_MESH_ACT_GENERIC_CLIENT_GET_STATE: { - dst->generic_client_get_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - dst->generic_client_get_state.get_state = (esp_ble_mesh_generic_client_get_state_t *)osi_malloc(sizeof(esp_ble_mesh_generic_client_get_state_t)); - if (dst->generic_client_get_state.params && dst->generic_client_get_state.get_state) { + dst->generic_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->generic_client_get_state.params) { memcpy(dst->generic_client_get_state.params, src->generic_client_get_state.params, sizeof(esp_ble_mesh_client_common_param_t)); - memcpy(dst->generic_client_get_state.get_state, src->generic_client_get_state.get_state, - sizeof(esp_ble_mesh_generic_client_get_state_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + break; + } + if (src->generic_client_get_state.get_state) { + dst->generic_client_get_state.get_state = (esp_ble_mesh_generic_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_generic_client_get_state_t)); + if (dst->generic_client_get_state.get_state) { + memcpy(dst->generic_client_get_state.get_state, src->generic_client_get_state.get_state, + sizeof(esp_ble_mesh_generic_client_get_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } } break; } case BTC_BLE_MESH_ACT_GENERIC_CLIENT_SET_STATE: { - dst->generic_client_set_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - dst->generic_client_set_state.set_state = (esp_ble_mesh_generic_client_set_state_t *)osi_malloc(sizeof(esp_ble_mesh_generic_client_set_state_t)); + dst->generic_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->generic_client_set_state.set_state = (esp_ble_mesh_generic_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_generic_client_set_state_t)); if (dst->generic_client_set_state.params && dst->generic_client_set_state.set_state) { memcpy(dst->generic_client_set_state.params, src->generic_client_set_state.params, sizeof(esp_ble_mesh_client_common_param_t)); @@ -74,7 +80,7 @@ void btc_ble_mesh_generic_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, voi length = src->generic_client_set_state.set_state->user_property_set.property_value->len; dst->generic_client_set_state.set_state->user_property_set.property_value = bt_mesh_alloc_buf(length); if (!dst->generic_client_set_state.set_state->user_property_set.property_value) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(dst->generic_client_set_state.set_state->user_property_set.property_value, @@ -87,7 +93,7 @@ void btc_ble_mesh_generic_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, voi length = src->generic_client_set_state.set_state->admin_property_set.property_value->len; dst->generic_client_set_state.set_state->admin_property_set.property_value = bt_mesh_alloc_buf(length); if (!dst->generic_client_set_state.set_state->admin_property_set.property_value) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(dst->generic_client_set_state.set_state->admin_property_set.property_value, @@ -99,12 +105,12 @@ void btc_ble_mesh_generic_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, voi break; } } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } break; } default: - LOG_DEBUG("%s, Unknown deep copy act %d", __func__, msg->act); + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); break; } } @@ -114,7 +120,7 @@ static void btc_ble_mesh_generic_client_arg_deep_free(btc_msg_t *msg) btc_ble_mesh_generic_client_args_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -123,10 +129,10 @@ static void btc_ble_mesh_generic_client_arg_deep_free(btc_msg_t *msg) switch (msg->act) { case BTC_BLE_MESH_ACT_GENERIC_CLIENT_GET_STATE: if (arg->generic_client_get_state.params) { - osi_free(arg->generic_client_get_state.params); + bt_mesh_free(arg->generic_client_get_state.params); } if (arg->generic_client_get_state.get_state) { - osi_free(arg->generic_client_get_state.get_state); + bt_mesh_free(arg->generic_client_get_state.get_state); } break; case BTC_BLE_MESH_ACT_GENERIC_CLIENT_SET_STATE: @@ -143,10 +149,10 @@ static void btc_ble_mesh_generic_client_arg_deep_free(btc_msg_t *msg) break; } } - osi_free(arg->generic_client_set_state.set_state); + bt_mesh_free(arg->generic_client_set_state.set_state); } if (arg->generic_client_set_state.params) { - osi_free(arg->generic_client_set_state.params); + bt_mesh_free(arg->generic_client_set_state.params); } break; default: @@ -158,17 +164,17 @@ static void btc_ble_mesh_generic_client_copy_req_data(btc_msg_t *msg, void *p_de { esp_ble_mesh_generic_client_cb_param_t *p_dest_data = (esp_ble_mesh_generic_client_cb_param_t *)p_dest; esp_ble_mesh_generic_client_cb_param_t *p_src_data = (esp_ble_mesh_generic_client_cb_param_t *)p_src; - u16_t length; + u16_t length = 0U; if (!msg || !p_src_data || !p_dest_data) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } if (p_src_data->params) { - p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); if (!p_dest_data->params) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } @@ -187,7 +193,7 @@ static void btc_ble_mesh_generic_client_copy_req_data(btc_msg_t *msg, void *p_de length = p_src_data->status_cb.user_properties_status.property_ids->len; p_dest_data->status_cb.user_properties_status.property_ids = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.user_properties_status.property_ids) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.user_properties_status.property_ids, @@ -202,7 +208,7 @@ static void btc_ble_mesh_generic_client_copy_req_data(btc_msg_t *msg, void *p_de length = p_src_data->status_cb.user_property_status.property_value->len; p_dest_data->status_cb.user_property_status.property_value = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.user_property_status.property_value) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.user_property_status.property_value, @@ -216,7 +222,7 @@ static void btc_ble_mesh_generic_client_copy_req_data(btc_msg_t *msg, void *p_de length = p_src_data->status_cb.admin_properties_status.property_ids->len; p_dest_data->status_cb.admin_properties_status.property_ids = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.admin_properties_status.property_ids) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.admin_properties_status.property_ids, @@ -231,7 +237,7 @@ static void btc_ble_mesh_generic_client_copy_req_data(btc_msg_t *msg, void *p_de length = p_src_data->status_cb.admin_property_status.property_value->len; p_dest_data->status_cb.admin_property_status.property_value = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.admin_property_status.property_value) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.admin_property_status.property_value, @@ -245,7 +251,7 @@ static void btc_ble_mesh_generic_client_copy_req_data(btc_msg_t *msg, void *p_de length = p_src_data->status_cb.manufacturer_properties_status.property_ids->len; p_dest_data->status_cb.manufacturer_properties_status.property_ids = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.manufacturer_properties_status.property_ids) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.manufacturer_properties_status.property_ids, @@ -260,7 +266,7 @@ static void btc_ble_mesh_generic_client_copy_req_data(btc_msg_t *msg, void *p_de length = p_src_data->status_cb.manufacturer_property_status.property_value->len; p_dest_data->status_cb.manufacturer_property_status.property_value = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.manufacturer_property_status.property_value) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.manufacturer_property_status.property_value, @@ -274,7 +280,7 @@ static void btc_ble_mesh_generic_client_copy_req_data(btc_msg_t *msg, void *p_de length = p_src_data->status_cb.client_properties_status.property_ids->len; p_dest_data->status_cb.client_properties_status.property_ids = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.client_properties_status.property_ids) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.client_properties_status.property_ids, @@ -298,7 +304,7 @@ static void btc_ble_mesh_generic_client_free_req_data(btc_msg_t *msg) esp_ble_mesh_generic_client_cb_param_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -347,7 +353,7 @@ static void btc_ble_mesh_generic_client_free_req_data(btc_msg_t *msg) } case ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT: if (arg->params) { - osi_free(arg->params); + bt_mesh_free(arg->params); } break; default: @@ -359,7 +365,7 @@ static void btc_ble_mesh_generic_client_callback(esp_ble_mesh_generic_client_cb_ { btc_msg_t msg = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_GENERIC_CLIENT)) { @@ -381,11 +387,11 @@ void bt_mesh_generic_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, { esp_ble_mesh_generic_client_cb_param_t cb_params = {0}; esp_ble_mesh_client_common_param_t params = {0}; - size_t length; - uint8_t act; + size_t length = 0U; + uint8_t act = 0U; if (!model || !ctx) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -403,7 +409,7 @@ void bt_mesh_generic_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, act = ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT; break; default: - LOG_ERROR("%s, Unknown generic client event type %d", __func__, evt_type); + BT_ERR("%s, Unknown generic client event type %d", __func__, evt_type); return; } @@ -434,7 +440,7 @@ void btc_ble_mesh_generic_client_publish_callback(u32_t opcode, struct net_buf_simple *buf) { if (!model || !ctx || !buf) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -452,7 +458,7 @@ void btc_ble_mesh_generic_client_call_handler(btc_msg_t *msg) bt_mesh_role_param_t role_param = {0}; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -464,7 +470,7 @@ void btc_ble_mesh_generic_client_call_handler(btc_msg_t *msg) role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } common.opcode = params->opcode; @@ -490,7 +496,7 @@ void btc_ble_mesh_generic_client_call_handler(btc_msg_t *msg) role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } common.opcode = params->opcode; @@ -524,7 +530,7 @@ void btc_ble_mesh_generic_client_cb_handler(btc_msg_t *msg) esp_ble_mesh_generic_client_cb_param_t *param = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -533,7 +539,7 @@ void btc_ble_mesh_generic_client_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_GENERIC_CLIENT_EVT_MAX) { btc_ble_mesh_generic_client_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } btc_ble_mesh_generic_client_free_req_data(msg); @@ -557,10 +563,10 @@ static void btc_ble_mesh_generic_server_copy_req_data(btc_msg_t *msg, void *p_de { esp_ble_mesh_generic_server_cb_param_t *p_dest_data = (esp_ble_mesh_generic_server_cb_param_t *)p_dest; esp_ble_mesh_generic_server_cb_param_t *p_src_data = (esp_ble_mesh_generic_server_cb_param_t *)p_src; - u16_t length; + u16_t length = 0U; if (!msg || !p_src_data || !p_dest_data) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -573,7 +579,7 @@ static void btc_ble_mesh_generic_server_copy_req_data(btc_msg_t *msg, void *p_de length = p_src_data->value.state_change.user_property_set.value->len; p_dest_data->value.state_change.user_property_set.value = bt_mesh_alloc_buf(length); if (p_dest_data->value.state_change.user_property_set.value == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.state_change.user_property_set.value, @@ -587,7 +593,7 @@ static void btc_ble_mesh_generic_server_copy_req_data(btc_msg_t *msg, void *p_de length = p_src_data->value.state_change.admin_property_set.value->len; p_dest_data->value.state_change.admin_property_set.value = bt_mesh_alloc_buf(length); if (p_dest_data->value.state_change.admin_property_set.value == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.state_change.admin_property_set.value, @@ -607,7 +613,7 @@ static void btc_ble_mesh_generic_server_copy_req_data(btc_msg_t *msg, void *p_de length = p_src_data->value.set.user_property.property_value->len; p_dest_data->value.set.user_property.property_value = bt_mesh_alloc_buf(length); if (p_dest_data->value.set.user_property.property_value == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.set.user_property.property_value, @@ -621,7 +627,7 @@ static void btc_ble_mesh_generic_server_copy_req_data(btc_msg_t *msg, void *p_de length = p_src_data->value.set.admin_property.property_value->len; p_dest_data->value.set.admin_property.property_value = bt_mesh_alloc_buf(length); if (p_dest_data->value.set.admin_property.property_value == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.set.admin_property.property_value, @@ -643,7 +649,7 @@ static void btc_ble_mesh_generic_server_free_req_data(btc_msg_t *msg) esp_ble_mesh_generic_server_cb_param_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -687,7 +693,7 @@ static void btc_ble_mesh_generic_server_callback(esp_ble_mesh_generic_server_cb_ { btc_msg_t msg = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_GENERIC_SERVER)) { @@ -708,11 +714,11 @@ void bt_mesh_generic_server_cb_evt_to_btc(u8_t evt_type, const u8_t *val, size_t len) { esp_ble_mesh_generic_server_cb_param_t cb_params = {0}; - size_t length; - uint8_t act; + size_t length = 0U; + uint8_t act = 0U; if (model == NULL || ctx == NULL) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -727,7 +733,7 @@ void bt_mesh_generic_server_cb_evt_to_btc(u8_t evt_type, act = ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT; break; default: - LOG_ERROR("%s, Unknown Generic Server event type", __func__); + BT_ERR("%s, Unknown Generic Server event type", __func__); return; } @@ -753,7 +759,7 @@ void btc_ble_mesh_generic_server_cb_handler(btc_msg_t *msg) esp_ble_mesh_generic_server_cb_param_t *param = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -762,7 +768,7 @@ void btc_ble_mesh_generic_server_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_GENERIC_SERVER_EVT_MAX) { btc_ble_mesh_generic_server_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } btc_ble_mesh_generic_server_free_req_data(msg); diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c index ab8ef2f12..769d2e3f6 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c @@ -15,13 +15,10 @@ #include #include -#include "btc/btc_manage.h" -#include "btc/btc_task.h" -#include "osi/allocator.h" - +#include "foundation.h" +#include "mesh_common.h" #include "health_srv.h" #include "health_cli.h" -#include "mesh_common.h" #include "btc_ble_mesh_health_model.h" #include "esp_ble_mesh_defs.h" @@ -46,39 +43,46 @@ void btc_ble_mesh_health_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void btc_ble_mesh_health_client_args_t *src = (btc_ble_mesh_health_client_args_t *)p_src; if (!msg || !dst || !src) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } switch (msg->act) { case BTC_BLE_MESH_ACT_HEALTH_CLIENT_GET_STATE: { - dst->health_client_get_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - dst->health_client_get_state.get_state = (esp_ble_mesh_health_client_get_state_t *)osi_malloc(sizeof(esp_ble_mesh_health_client_get_state_t)); - if (dst->health_client_get_state.params && dst->health_client_get_state.get_state) { + dst->health_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->health_client_get_state.params) { memcpy(dst->health_client_get_state.params, src->health_client_get_state.params, sizeof(esp_ble_mesh_client_common_param_t)); - memcpy(dst->health_client_get_state.get_state, src->health_client_get_state.get_state, - sizeof(esp_ble_mesh_health_client_get_state_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + break; + } + if (src->health_client_get_state.get_state) { + dst->health_client_get_state.get_state = (esp_ble_mesh_health_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_health_client_get_state_t)); + if (dst->health_client_get_state.get_state) { + memcpy(dst->health_client_get_state.get_state, src->health_client_get_state.get_state, + sizeof(esp_ble_mesh_health_client_get_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } } break; } case BTC_BLE_MESH_ACT_HEALTH_CLIENT_SET_STATE: { - dst->health_client_set_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - dst->health_client_set_state.set_state = (esp_ble_mesh_health_client_set_state_t *)osi_malloc(sizeof(esp_ble_mesh_health_client_set_state_t)); + dst->health_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->health_client_set_state.set_state = (esp_ble_mesh_health_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_health_client_set_state_t)); if (dst->health_client_set_state.params && dst->health_client_set_state.set_state) { memcpy(dst->health_client_set_state.params, src->health_client_set_state.params, sizeof(esp_ble_mesh_client_common_param_t)); memcpy(dst->health_client_set_state.set_state, src->health_client_set_state.set_state, sizeof(esp_ble_mesh_health_client_set_state_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } break; } default: - LOG_DEBUG("%s, Unknown deep copy act %d", __func__, msg->act); + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); break; } } @@ -88,7 +92,7 @@ static void btc_ble_mesh_health_client_arg_deep_free(btc_msg_t *msg) btc_ble_mesh_health_client_args_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -97,18 +101,18 @@ static void btc_ble_mesh_health_client_arg_deep_free(btc_msg_t *msg) switch (msg->act) { case BTC_BLE_MESH_ACT_HEALTH_CLIENT_GET_STATE: if (arg->health_client_get_state.params) { - osi_free(arg->health_client_get_state.params); + bt_mesh_free(arg->health_client_get_state.params); } if (arg->health_client_get_state.get_state) { - osi_free(arg->health_client_get_state.get_state); + bt_mesh_free(arg->health_client_get_state.get_state); } break; case BTC_BLE_MESH_ACT_HEALTH_CLIENT_SET_STATE: if (arg->health_client_set_state.params) { - osi_free(arg->health_client_set_state.params); + bt_mesh_free(arg->health_client_set_state.params); } if (arg->health_client_set_state.set_state) { - osi_free(arg->health_client_set_state.set_state); + bt_mesh_free(arg->health_client_set_state.set_state); } break; default: @@ -120,17 +124,17 @@ static void btc_ble_mesh_health_client_copy_req_data(btc_msg_t *msg, void *p_des { esp_ble_mesh_health_client_cb_param_t *p_dest_data = (esp_ble_mesh_health_client_cb_param_t *)p_dest; esp_ble_mesh_health_client_cb_param_t *p_src_data = (esp_ble_mesh_health_client_cb_param_t *)p_src; - u16_t length; + u16_t length = 0U; if (!msg || !p_src_data || !p_dest_data) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } if (p_src_data->params) { - p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); if (!p_dest_data->params) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } @@ -148,7 +152,7 @@ static void btc_ble_mesh_health_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.current_status.fault_array->len; p_dest_data->status_cb.current_status.fault_array = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.current_status.fault_array) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.current_status.fault_array, @@ -164,7 +168,7 @@ static void btc_ble_mesh_health_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.fault_status.fault_array->len; p_dest_data->status_cb.fault_status.fault_array = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.fault_status.fault_array) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.fault_status.fault_array, @@ -188,7 +192,7 @@ static void btc_ble_mesh_health_client_free_req_data(btc_msg_t *msg) esp_ble_mesh_health_client_cb_param_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -215,7 +219,7 @@ static void btc_ble_mesh_health_client_free_req_data(btc_msg_t *msg) } case ESP_BLE_MESH_HEALTH_CLIENT_TIMEOUT_EVT: if (arg->params) { - osi_free(arg->params); + bt_mesh_free(arg->params); } break; default: @@ -227,7 +231,7 @@ static void btc_ble_mesh_health_client_callback(esp_ble_mesh_health_client_cb_pa { btc_msg_t msg = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_HEALTH_CLIENT)) { @@ -249,11 +253,11 @@ void bt_mesh_health_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, { esp_ble_mesh_health_client_cb_param_t cb_params = {0}; esp_ble_mesh_client_common_param_t params = {0}; - size_t length; - uint8_t act; + size_t length = 0U; + uint8_t act = 0U; if (!model || !ctx) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -271,7 +275,7 @@ void bt_mesh_health_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, act = ESP_BLE_MESH_HEALTH_CLIENT_TIMEOUT_EVT; break; default: - LOG_ERROR("%s, Unknown health client event type %d", __func__, evt_type); + BT_ERR("%s, Unknown health client event type %d", __func__, evt_type); return; } @@ -302,7 +306,7 @@ void btc_ble_mesh_health_publish_callback(u32_t opcode, struct net_buf_simple *buf) { if (!model || !ctx || !buf) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -318,7 +322,12 @@ static int btc_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param struct bt_mesh_msg_ctx ctx = {0}; if (!params || !cb) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (params->opcode == ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET && get == NULL) { + BT_ERR("%s, Invalid health client get", __func__); return -EINVAL; } @@ -338,7 +347,7 @@ static int btc_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET: return (cb->error_code = bt_mesh_health_fault_get(&ctx, get->fault_get.company_id)); default: - LOG_ERROR("%s, Invalid opcode 0x%x", __func__, params->opcode); + BT_ERR("%s, Invalid opcode 0x%x", __func__, params->opcode); return (cb->error_code = -EINVAL); } @@ -352,7 +361,7 @@ static int btc_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param struct bt_mesh_msg_ctx ctx = {0}; if (!params || !set || !cb) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } @@ -390,7 +399,7 @@ static int btc_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param return (cb->error_code = bt_mesh_health_fault_clear(&ctx, set->fault_clear.company_id, false)); default: - LOG_ERROR("%s, Invalid opcode 0x%x", __func__, params->opcode); + BT_ERR("%s, Invalid opcode 0x%x", __func__, params->opcode); return (cb->error_code = -EINVAL); } @@ -404,7 +413,7 @@ void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg) bt_mesh_role_param_t role_param = {0}; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -416,7 +425,7 @@ void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg) role_param.model = (struct bt_mesh_model *)cb.params->model; role_param.role = cb.params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } btc_ble_mesh_health_client_get_state(arg->health_client_get_state.params, @@ -432,7 +441,7 @@ void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg) role_param.model = (struct bt_mesh_model *)cb.params->model; role_param.role = cb.params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } btc_ble_mesh_health_client_set_state(arg->health_client_set_state.params, @@ -456,7 +465,7 @@ void btc_ble_mesh_health_client_cb_handler(btc_msg_t *msg) esp_ble_mesh_health_client_cb_param_t *param = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -465,7 +474,7 @@ void btc_ble_mesh_health_client_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_HEALTH_CLIENT_EVT_MAX) { btc_ble_mesh_health_client_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } btc_ble_mesh_health_client_free_req_data(msg); @@ -487,7 +496,7 @@ static inline void btc_ble_mesh_health_server_cb_to_app(esp_ble_mesh_health_serv void btc_ble_mesh_health_server_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) { if (!msg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -502,7 +511,7 @@ void btc_ble_mesh_health_server_arg_deep_copy(btc_msg_t *msg, void *p_dest, void static void btc_ble_mesh_health_server_arg_deep_free(btc_msg_t *msg) { if (!msg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -517,7 +526,7 @@ static void btc_ble_mesh_health_server_arg_deep_free(btc_msg_t *msg) static void btc_ble_mesh_health_server_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) { if (!msg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -532,7 +541,7 @@ static void btc_ble_mesh_health_server_copy_req_data(btc_msg_t *msg, void *p_des static void btc_ble_mesh_health_server_free_req_data(btc_msg_t *msg) { if (!msg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -548,7 +557,7 @@ static void btc_ble_mesh_health_server_callback(esp_ble_mesh_health_server_cb_pa { btc_msg_t msg = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_HEALTH_SERVER)) { @@ -569,7 +578,7 @@ void btc_ble_mesh_health_server_call_handler(btc_msg_t *msg) btc_ble_mesh_health_server_args_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -595,7 +604,7 @@ void btc_ble_mesh_health_server_cb_handler(btc_msg_t *msg) esp_ble_mesh_health_server_cb_param_t *param = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -604,7 +613,7 @@ void btc_ble_mesh_health_server_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_HEALTH_SERVER_EVT_MAX) { btc_ble_mesh_health_server_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } btc_ble_mesh_health_server_free_req_data(msg); diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c index b0c4d9f29..7737f2bfd 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c @@ -15,10 +15,9 @@ #include #include -#include "btc/btc_manage.h" -#include "osi/allocator.h" - +#include "mesh_common.h" #include "lighting_client.h" + #include "btc_ble_mesh_lighting_model.h" #include "esp_ble_mesh_lighting_model_api.h" @@ -40,39 +39,46 @@ void btc_ble_mesh_lighting_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, vo btc_ble_mesh_lighting_client_args_t *src = (btc_ble_mesh_lighting_client_args_t *)p_src; if (!msg || !dst || !src) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } switch (msg->act) { case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE: { - dst->light_client_get_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - dst->light_client_get_state.get_state = (esp_ble_mesh_light_client_get_state_t *)osi_malloc(sizeof(esp_ble_mesh_light_client_get_state_t)); - if (dst->light_client_get_state.params && dst->light_client_get_state.get_state) { + dst->light_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->light_client_get_state.params) { memcpy(dst->light_client_get_state.params, src->light_client_get_state.params, sizeof(esp_ble_mesh_client_common_param_t)); - memcpy(dst->light_client_get_state.get_state, src->light_client_get_state.get_state, - sizeof(esp_ble_mesh_light_client_get_state_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + break; + } + if (src->light_client_get_state.get_state) { + dst->light_client_get_state.get_state = (esp_ble_mesh_light_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_light_client_get_state_t)); + if (dst->light_client_get_state.get_state) { + memcpy(dst->light_client_get_state.get_state, src->light_client_get_state.get_state, + sizeof(esp_ble_mesh_light_client_get_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } } break; } case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE: { - dst->light_client_set_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - dst->light_client_set_state.set_state = (esp_ble_mesh_light_client_set_state_t *)osi_malloc(sizeof(esp_ble_mesh_light_client_set_state_t)); + dst->light_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->light_client_set_state.set_state = (esp_ble_mesh_light_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_light_client_set_state_t)); if (dst->light_client_set_state.params && dst->light_client_set_state.set_state) { memcpy(dst->light_client_set_state.params, src->light_client_set_state.params, sizeof(esp_ble_mesh_client_common_param_t)); memcpy(dst->light_client_set_state.set_state, src->light_client_set_state.set_state, sizeof(esp_ble_mesh_light_client_set_state_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } break; } default: - LOG_DEBUG("%s, Unknown deep copy act %d", __func__, msg->act); + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); break; } } @@ -82,7 +88,7 @@ static void btc_ble_mesh_lighting_client_arg_deep_free(btc_msg_t *msg) btc_ble_mesh_lighting_client_args_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -91,18 +97,18 @@ static void btc_ble_mesh_lighting_client_arg_deep_free(btc_msg_t *msg) switch (msg->act) { case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE: if (arg->light_client_get_state.params) { - osi_free(arg->light_client_get_state.params); + bt_mesh_free(arg->light_client_get_state.params); } if (arg->light_client_get_state.get_state) { - osi_free(arg->light_client_get_state.get_state); + bt_mesh_free(arg->light_client_get_state.get_state); } break; case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE: if (arg->light_client_set_state.params) { - osi_free(arg->light_client_set_state.params); + bt_mesh_free(arg->light_client_set_state.params); } if (arg->light_client_set_state.set_state) { - osi_free(arg->light_client_set_state.set_state); + bt_mesh_free(arg->light_client_set_state.set_state); } break; default: @@ -114,17 +120,17 @@ static void btc_ble_mesh_lighting_client_copy_req_data(btc_msg_t *msg, void *p_d { esp_ble_mesh_light_client_cb_param_t *p_dest_data = (esp_ble_mesh_light_client_cb_param_t *)p_dest; esp_ble_mesh_light_client_cb_param_t *p_src_data = (esp_ble_mesh_light_client_cb_param_t *)p_src; - u16_t length; + u16_t length = 0U; if (!msg || !p_src_data || !p_dest_data) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } if (p_src_data->params) { - p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); if (!p_dest_data->params) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } @@ -144,7 +150,7 @@ static void btc_ble_mesh_lighting_client_copy_req_data(btc_msg_t *msg, void *p_d length = p_src_data->status_cb.lc_property_status.property_value->len; p_dest_data->status_cb.lc_property_status.property_value = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.lc_property_status.property_value) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.lc_property_status.property_value, @@ -168,7 +174,7 @@ static void btc_ble_mesh_lighting_client_free_req_data(btc_msg_t *msg) esp_ble_mesh_light_client_cb_param_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -191,7 +197,7 @@ static void btc_ble_mesh_lighting_client_free_req_data(btc_msg_t *msg) } case ESP_BLE_MESH_LIGHT_CLIENT_TIMEOUT_EVT: if (arg->params) { - osi_free(arg->params); + bt_mesh_free(arg->params); } break; default: @@ -203,7 +209,7 @@ static void btc_ble_mesh_lighting_client_callback(esp_ble_mesh_light_client_cb_p { btc_msg_t msg = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_LIGHTING_CLIENT)) { @@ -225,11 +231,11 @@ void bt_mesh_lighting_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, { esp_ble_mesh_light_client_cb_param_t cb_params = {0}; esp_ble_mesh_client_common_param_t params = {0}; - size_t length; - uint8_t act; + size_t length = 0U; + uint8_t act = 0U; if (!model || !ctx) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -247,7 +253,7 @@ void bt_mesh_lighting_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, act = ESP_BLE_MESH_LIGHT_CLIENT_TIMEOUT_EVT; break; default: - LOG_ERROR("%s, Unknown lighting client event type", __func__); + BT_ERR("%s, Unknown lighting client event type", __func__); return; } @@ -278,7 +284,7 @@ void btc_ble_mesh_lighting_client_publish_callback(u32_t opcode, struct net_buf_simple *buf) { if (!model || !ctx || !buf) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -296,7 +302,7 @@ void btc_ble_mesh_lighting_client_call_handler(btc_msg_t *msg) bt_mesh_role_param_t role_param = {0}; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -308,7 +314,7 @@ void btc_ble_mesh_lighting_client_call_handler(btc_msg_t *msg) role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } common.opcode = params->opcode; @@ -334,7 +340,7 @@ void btc_ble_mesh_lighting_client_call_handler(btc_msg_t *msg) role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } common.opcode = params->opcode; @@ -368,7 +374,7 @@ void btc_ble_mesh_lighting_client_cb_handler(btc_msg_t *msg) esp_ble_mesh_light_client_cb_param_t *param = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -377,7 +383,7 @@ void btc_ble_mesh_lighting_client_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_LIGHT_CLIENT_EVT_MAX) { btc_ble_mesh_lighting_client_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } btc_ble_mesh_lighting_client_free_req_data(msg); @@ -401,10 +407,10 @@ static void btc_ble_mesh_lighting_server_copy_req_data(btc_msg_t *msg, void *p_d { esp_ble_mesh_lighting_server_cb_param_t *p_dest_data = (esp_ble_mesh_lighting_server_cb_param_t *)p_dest; esp_ble_mesh_lighting_server_cb_param_t *p_src_data = (esp_ble_mesh_lighting_server_cb_param_t *)p_src; - u16_t length; + u16_t length = 0U; if (!msg || !p_src_data || !p_dest_data) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -416,7 +422,7 @@ static void btc_ble_mesh_lighting_server_copy_req_data(btc_msg_t *msg, void *p_d length = p_src_data->value.state_change.lc_property_set.property_value->len; p_dest_data->value.state_change.lc_property_set.property_value = bt_mesh_alloc_buf(length); if (p_dest_data->value.state_change.lc_property_set.property_value == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.state_change.lc_property_set.property_value, @@ -432,7 +438,7 @@ static void btc_ble_mesh_lighting_server_copy_req_data(btc_msg_t *msg, void *p_d length = p_src_data->value.set.lc_property.property_value->len; p_dest_data->value.set.lc_property.property_value = bt_mesh_alloc_buf(length); if (p_dest_data->value.set.lc_property.property_value == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.set.lc_property.property_value, @@ -447,7 +453,7 @@ static void btc_ble_mesh_lighting_server_copy_req_data(btc_msg_t *msg, void *p_d length = p_src_data->value.status.sensor_status.data->len; p_dest_data->value.status.sensor_status.data = bt_mesh_alloc_buf(length); if (p_dest_data->value.status.sensor_status.data == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.status.sensor_status.data, @@ -466,7 +472,7 @@ static void btc_ble_mesh_lighting_server_free_req_data(btc_msg_t *msg) esp_ble_mesh_lighting_server_cb_param_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -499,7 +505,7 @@ static void btc_ble_mesh_lighting_server_callback(esp_ble_mesh_lighting_server_c { btc_msg_t msg = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_LIGHTING_SERVER)) { @@ -520,11 +526,11 @@ void bt_mesh_lighting_server_cb_evt_to_btc(u8_t evt_type, const u8_t *val, size_t len) { esp_ble_mesh_lighting_server_cb_param_t cb_params = {0}; - size_t length; - uint8_t act; + size_t length = 0U; + uint8_t act = 0U; if (model == NULL || ctx == NULL) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -542,7 +548,7 @@ void bt_mesh_lighting_server_cb_evt_to_btc(u8_t evt_type, act = ESP_BLE_MESH_LIGHTING_SERVER_RECV_STATUS_MSG_EVT; break; default: - LOG_ERROR("%s, Unknown Lighting Server event type", __func__); + BT_ERR("%s, Unknown Lighting Server event type", __func__); return; } @@ -568,7 +574,7 @@ void btc_ble_mesh_lighting_server_cb_handler(btc_msg_t *msg) esp_ble_mesh_lighting_server_cb_param_t *param = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -577,7 +583,7 @@ void btc_ble_mesh_lighting_server_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_LIGHTING_SERVER_EVT_MAX) { btc_ble_mesh_lighting_server_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } btc_ble_mesh_lighting_server_free_req_data(msg); diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c index 7bd500ae2..28e64b767 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c @@ -16,33 +16,21 @@ #include #include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/queue.h" #include "freertos/semphr.h" -#include "btc/btc_manage.h" -#include "osi/allocator.h" - -#include "sdkconfig.h" - -#include "mesh_util.h" -#include "mesh_main.h" -#include "mesh_access.h" #include "mesh_proxy.h" -#include "cfg_cli.h" -#include "health_cli.h" -#include "cfg_srv.h" -#include "health_srv.h" - #include "mesh.h" #include "access.h" -#include "transport.h" -#include "proxy_server.h" #include "prov.h" +#include "proxy_server.h" #include "proxy_client.h" #include "provisioner_prov.h" #include "provisioner_main.h" +#include "cfg_cli.h" +#include "health_cli.h" +#include "cfg_srv.h" +#include "health_srv.h" #include "generic_client.h" #include "lighting_client.h" #include "sensor_client.h" @@ -89,33 +77,42 @@ void btc_ble_mesh_prov_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) btc_ble_mesh_prov_args_t *src = (btc_ble_mesh_prov_args_t *)p_src; if (!msg || !dst || !src) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } switch (msg->act) { case BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR: - LOG_DEBUG("%s, BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR", __func__); - dst->proxy_client_add_filter_addr.addr = (uint16_t *)osi_calloc(src->proxy_client_add_filter_addr.addr_num << 1); + BT_DBG("%s, BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR", __func__); + dst->proxy_client_add_filter_addr.addr = (uint16_t *)bt_mesh_calloc(src->proxy_client_add_filter_addr.addr_num << 1); if (dst->proxy_client_add_filter_addr.addr) { memcpy(dst->proxy_client_add_filter_addr.addr, src->proxy_client_add_filter_addr.addr, src->proxy_client_add_filter_addr.addr_num << 1); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } break; case BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR: - LOG_DEBUG("%s, BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR", __func__); - dst->proxy_client_remove_filter_addr.addr = osi_calloc(src->proxy_client_remove_filter_addr.addr_num << 1); + BT_DBG("%s, BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR", __func__); + dst->proxy_client_remove_filter_addr.addr = bt_mesh_calloc(src->proxy_client_remove_filter_addr.addr_num << 1); if (dst->proxy_client_remove_filter_addr.addr) { memcpy(dst->proxy_client_remove_filter_addr.addr, src->proxy_client_remove_filter_addr.addr, src->proxy_client_remove_filter_addr.addr_num << 1); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + break; + case BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA: + BT_DBG("%s, BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA", __func__); + dst->store_node_comp_data.data = bt_mesh_calloc(src->store_node_comp_data.length); + if (dst->store_node_comp_data.data) { + memcpy(dst->store_node_comp_data.data, src->store_node_comp_data.data, src->store_node_comp_data.length); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } break; default: - LOG_DEBUG("%s, Unknown deep copy act %d", __func__, msg->act); + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); break; } } @@ -125,7 +122,7 @@ static void btc_ble_mesh_prov_arg_deep_free(btc_msg_t *msg) btc_ble_mesh_prov_args_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -134,12 +131,17 @@ static void btc_ble_mesh_prov_arg_deep_free(btc_msg_t *msg) switch (msg->act) { case BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR: if (arg->proxy_client_add_filter_addr.addr) { - osi_free(arg->proxy_client_add_filter_addr.addr); + bt_mesh_free(arg->proxy_client_add_filter_addr.addr); } break; case BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR: if (arg->proxy_client_remove_filter_addr.addr) { - osi_free(arg->proxy_client_remove_filter_addr.addr); + bt_mesh_free(arg->proxy_client_remove_filter_addr.addr); + } + break; + case BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA: + if (arg->store_node_comp_data.data) { + bt_mesh_free(arg->store_node_comp_data.data); } break; default: @@ -153,42 +155,42 @@ void btc_ble_mesh_model_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) btc_ble_mesh_model_args_t *src = (btc_ble_mesh_model_args_t *)p_src; if (!msg || !dst || !src) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } switch (msg->act) { case BTC_BLE_MESH_ACT_SERVER_MODEL_SEND: case BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND: { - LOG_DEBUG("%s, BTC_BLE_MESH_ACT_MODEL_SEND, src->model_send.length = %d", __func__, src->model_send.length); - dst->model_send.data = src->model_send.length ? (uint8_t *)osi_malloc(src->model_send.length) : NULL; - dst->model_send.ctx = osi_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); + BT_DBG("%s, BTC_BLE_MESH_ACT_MODEL_SEND, src->model_send.length = %d", __func__, src->model_send.length); + dst->model_send.data = src->model_send.length ? (uint8_t *)bt_mesh_malloc(src->model_send.length) : NULL; + dst->model_send.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); if (src->model_send.length) { if (dst->model_send.data) { memcpy(dst->model_send.data, src->model_send.data, src->model_send.length); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } } if (dst->model_send.ctx) { memcpy(dst->model_send.ctx, src->model_send.ctx, sizeof(esp_ble_mesh_msg_ctx_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } break; } case BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE: - LOG_DEBUG("%s, BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE", __func__); - dst->model_update_state.value = osi_malloc(sizeof(esp_ble_mesh_server_state_value_t)); + BT_DBG("%s, BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE", __func__); + dst->model_update_state.value = bt_mesh_malloc(sizeof(esp_ble_mesh_server_state_value_t)); if (dst->model_update_state.value) { memcpy(dst->model_update_state.value, src->model_update_state.value, sizeof(esp_ble_mesh_server_state_value_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } break; default: - LOG_DEBUG("%s, Unknown deep copy act %d", __func__, msg->act); + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); break; } } @@ -198,7 +200,7 @@ static void btc_ble_mesh_model_arg_deep_free(btc_msg_t *msg) btc_ble_mesh_model_args_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -208,15 +210,15 @@ static void btc_ble_mesh_model_arg_deep_free(btc_msg_t *msg) case BTC_BLE_MESH_ACT_SERVER_MODEL_SEND: case BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND: if (arg->model_send.data) { - osi_free(arg->model_send.data); + bt_mesh_free(arg->model_send.data); } if (arg->model_send.ctx) { - osi_free(arg->model_send.ctx); + bt_mesh_free(arg->model_send.ctx); } break; case BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE: if (arg->model_update_state.value) { - osi_free(arg->model_update_state.value); + bt_mesh_free(arg->model_update_state.value); } break; default: @@ -232,25 +234,25 @@ static void btc_ble_mesh_model_copy_req_data(btc_msg_t *msg, void *p_dest, void esp_ble_mesh_model_cb_param_t *p_src_data = (esp_ble_mesh_model_cb_param_t *)p_src; if (!msg || !p_src_data || !p_dest_data) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } switch (msg->act) { case ESP_BLE_MESH_MODEL_OPERATION_EVT: { if (p_src_data->model_operation.ctx && p_src_data->model_operation.msg) { - p_dest_data->model_operation.ctx = osi_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); - p_dest_data->model_operation.msg = p_src_data->model_operation.length ? (uint8_t *)osi_malloc(p_src_data->model_operation.length) : NULL; + p_dest_data->model_operation.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); + p_dest_data->model_operation.msg = p_src_data->model_operation.length ? (uint8_t *)bt_mesh_malloc(p_src_data->model_operation.length) : NULL; if (p_dest_data->model_operation.ctx) { memcpy(p_dest_data->model_operation.ctx, p_src_data->model_operation.ctx, sizeof(esp_ble_mesh_msg_ctx_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } if (p_src_data->model_operation.length) { if (p_dest_data->model_operation.msg) { memcpy(p_dest_data->model_operation.msg, p_src_data->model_operation.msg, p_src_data->model_operation.length); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } } } @@ -258,18 +260,18 @@ static void btc_ble_mesh_model_copy_req_data(btc_msg_t *msg, void *p_dest, void } case ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT: { if (p_src_data->client_recv_publish_msg.ctx && p_src_data->client_recv_publish_msg.msg) { - p_dest_data->client_recv_publish_msg.ctx = osi_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); - p_dest_data->client_recv_publish_msg.msg = p_src_data->client_recv_publish_msg.length ? (uint8_t *)osi_malloc(p_src_data->client_recv_publish_msg.length) : NULL; + p_dest_data->client_recv_publish_msg.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); + p_dest_data->client_recv_publish_msg.msg = p_src_data->client_recv_publish_msg.length ? (uint8_t *)bt_mesh_malloc(p_src_data->client_recv_publish_msg.length) : NULL; if (p_dest_data->client_recv_publish_msg.ctx) { memcpy(p_dest_data->client_recv_publish_msg.ctx, p_src_data->client_recv_publish_msg.ctx, sizeof(esp_ble_mesh_msg_ctx_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } if (p_src_data->client_recv_publish_msg.length) { if (p_dest_data->client_recv_publish_msg.msg) { memcpy(p_dest_data->client_recv_publish_msg.msg, p_src_data->client_recv_publish_msg.msg, p_src_data->client_recv_publish_msg.length); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } } } @@ -277,22 +279,22 @@ static void btc_ble_mesh_model_copy_req_data(btc_msg_t *msg, void *p_dest, void } case ESP_BLE_MESH_MODEL_SEND_COMP_EVT: { if (p_src_data->model_send_comp.ctx) { - p_dest_data->model_send_comp.ctx = osi_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); + p_dest_data->model_send_comp.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); if (p_dest_data->model_send_comp.ctx) { memcpy(p_dest_data->model_send_comp.ctx, p_src_data->model_send_comp.ctx, sizeof(esp_ble_mesh_msg_ctx_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } } break; } case ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT: { if (p_src_data->client_send_timeout.ctx) { - p_dest_data->client_send_timeout.ctx = osi_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); + p_dest_data->client_send_timeout.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); if (p_dest_data->client_send_timeout.ctx) { memcpy(p_dest_data->client_send_timeout.ctx, p_src_data->client_send_timeout.ctx, sizeof(esp_ble_mesh_msg_ctx_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } } break; @@ -307,7 +309,7 @@ static void btc_ble_mesh_model_free_req_data(btc_msg_t *msg) esp_ble_mesh_model_cb_param_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -316,31 +318,31 @@ static void btc_ble_mesh_model_free_req_data(btc_msg_t *msg) switch (msg->act) { case ESP_BLE_MESH_MODEL_OPERATION_EVT: { if (arg->model_operation.msg) { - osi_free(arg->model_operation.msg); + bt_mesh_free(arg->model_operation.msg); } if (arg->model_operation.ctx) { - osi_free(arg->model_operation.ctx); + bt_mesh_free(arg->model_operation.ctx); } break; } case ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT: { if (arg->client_recv_publish_msg.msg) { - osi_free(arg->client_recv_publish_msg.msg); + bt_mesh_free(arg->client_recv_publish_msg.msg); } if (arg->client_recv_publish_msg.ctx) { - osi_free(arg->client_recv_publish_msg.ctx); + bt_mesh_free(arg->client_recv_publish_msg.ctx); } break; } case ESP_BLE_MESH_MODEL_SEND_COMP_EVT: { if (arg->model_send_comp.ctx) { - osi_free(arg->model_send_comp.ctx); + bt_mesh_free(arg->model_send_comp.ctx); } break; } case ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT: { if (arg->client_send_timeout.ctx) { - osi_free(arg->client_send_timeout.ctx); + bt_mesh_free(arg->client_send_timeout.ctx); } break; } @@ -352,9 +354,9 @@ static void btc_ble_mesh_model_free_req_data(btc_msg_t *msg) static bt_status_t btc_ble_mesh_model_callback(esp_ble_mesh_model_cb_param_t *param, uint8_t act) { btc_msg_t msg = {0}; - bt_status_t ret; + bt_status_t ret = BT_STATUS_SUCCESS; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_MODEL)) { @@ -368,7 +370,7 @@ static bt_status_t btc_ble_mesh_model_callback(esp_ble_mesh_model_cb_param_t *pa ret = btc_transfer_context(&msg, param, sizeof(esp_ble_mesh_model_cb_param_t), btc_ble_mesh_model_copy_req_data); if (ret != BT_STATUS_SUCCESS) { - LOG_ERROR("%s, btc_transfer_context failed", __func__); + BT_ERR("%s, btc_transfer_context failed", __func__); } return ret; } @@ -397,7 +399,7 @@ static void btc_ble_mesh_client_model_op_cb(struct bt_mesh_model *model, bt_mesh_client_node_t *node = NULL; if (!model || !model->user_data || !ctx || !buf) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -418,9 +420,8 @@ static void btc_ble_mesh_client_model_op_cb(struct bt_mesh_model *model, mesh_param.model_operation.length = buf->len; mesh_param.model_operation.msg = buf->data; if (!k_delayed_work_free(&node->timer)) { - btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_OPERATION_EVT); - // Don't forget to release the node at the end. bt_mesh_client_free_node(node); + btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_OPERATION_EVT); } } @@ -433,6 +434,7 @@ static void btc_ble_mesh_client_model_timeout_cb(struct k_work *work) esp_ble_mesh_model_cb_param_t mesh_param = {0}; struct k_delayed_work *timer = NULL; bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; bt_mesh_client_model_lock(); @@ -441,12 +443,12 @@ static void btc_ble_mesh_client_model_timeout_cb(struct k_work *work) if (timer && !k_delayed_work_free(timer)) { node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); if (node) { + memcpy(&ctx, &node->ctx, sizeof(ctx)); mesh_param.client_send_timeout.opcode = node->opcode; - mesh_param.client_send_timeout.model = (esp_ble_mesh_model_t *)node->ctx.model; - mesh_param.client_send_timeout.ctx = (esp_ble_mesh_msg_ctx_t *)&node->ctx; - btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT); - // Don't forget to release the node at the end. + mesh_param.client_send_timeout.model = (esp_ble_mesh_model_t *)ctx.model; + mesh_param.client_send_timeout.ctx = (esp_ble_mesh_msg_ctx_t *)&ctx; bt_mesh_client_free_node(node); + btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT); } } @@ -481,9 +483,9 @@ static void btc_ble_mesh_model_publish_comp_cb(esp_ble_mesh_model_t *model, int static int btc_ble_mesh_model_publish_update(struct bt_mesh_model *mod) { esp_ble_mesh_model_cb_param_t mesh_param = {0}; - bt_status_t ret; + bt_status_t ret = BT_STATUS_SUCCESS; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.model_publish_update.model = (esp_ble_mesh_model_t *)mod; @@ -507,9 +509,9 @@ static void btc_ble_mesh_server_model_update_state_comp_cb(esp_ble_mesh_model_t static bt_status_t btc_ble_mesh_prov_callback(esp_ble_mesh_prov_cb_param_t *param, uint8_t act) { btc_msg_t msg = {0}; - bt_status_t ret; + bt_status_t ret = BT_STATUS_SUCCESS; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_PROV)) { @@ -522,7 +524,7 @@ static bt_status_t btc_ble_mesh_prov_callback(esp_ble_mesh_prov_cb_param_t *para ret = btc_transfer_context(&msg, param, sizeof(esp_ble_mesh_prov_cb_param_t), NULL); if (ret != BT_STATUS_SUCCESS) { - LOG_ERROR("%s, btc_transfer_context failed", __func__); + BT_ERR("%s, btc_transfer_context failed", __func__); } return ret; } @@ -530,7 +532,7 @@ static bt_status_t btc_ble_mesh_prov_callback(esp_ble_mesh_prov_cb_param_t *para #if CONFIG_BLE_MESH_NODE static void btc_ble_mesh_oob_pub_key_cb(void) { - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); btc_ble_mesh_prov_callback(NULL, ESP_BLE_MESH_NODE_PROV_OOB_PUB_KEY_EVT); return; @@ -539,9 +541,9 @@ static void btc_ble_mesh_oob_pub_key_cb(void) static int btc_ble_mesh_output_number_cb(bt_mesh_output_action_t act, u32_t num) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - bt_status_t ret; + bt_status_t ret = BT_STATUS_SUCCESS; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.node_prov_output_num.action = (esp_ble_mesh_output_action_t)act; mesh_param.node_prov_output_num.number = num; @@ -553,9 +555,9 @@ static int btc_ble_mesh_output_number_cb(bt_mesh_output_action_t act, u32_t num) static int btc_ble_mesh_output_string_cb(const char *str) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - bt_status_t ret; + bt_status_t ret = BT_STATUS_SUCCESS; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); strncpy(mesh_param.node_prov_output_str.string, str, strlen(str)); @@ -566,9 +568,9 @@ static int btc_ble_mesh_output_string_cb(const char *str) static int btc_ble_mesh_input_cb(bt_mesh_input_action_t act, u8_t size) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - bt_status_t ret; + bt_status_t ret = BT_STATUS_SUCCESS; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.node_prov_input.action = (esp_ble_mesh_input_action_t)act; mesh_param.node_prov_input.size = size; @@ -581,7 +583,7 @@ static void btc_ble_mesh_link_open_cb(bt_mesh_prov_bearer_t bearer) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.node_prov_link_open.bearer = (esp_ble_mesh_prov_bearer_t)bearer; @@ -593,7 +595,7 @@ static void btc_ble_mesh_link_close_cb(bt_mesh_prov_bearer_t bearer) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.node_prov_link_close.bearer = (esp_ble_mesh_prov_bearer_t)bearer; @@ -605,7 +607,7 @@ static void btc_ble_mesh_complete_cb(u16_t net_idx, const u8_t net_key[16], u16_ { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.node_prov_complete.net_idx = net_idx; memcpy(mesh_param.node_prov_complete.net_key, net_key, 16); @@ -619,7 +621,7 @@ static void btc_ble_mesh_complete_cb(u16_t net_idx, const u8_t net_key[16], u16_ static void btc_ble_mesh_reset_cb(void) { - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); btc_ble_mesh_prov_callback(NULL, ESP_BLE_MESH_NODE_PROV_RESET_EVT); return; @@ -630,7 +632,7 @@ static void btc_ble_mesh_prov_register_complete_cb(int err_code) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.prov_register_comp.err_code = err_code; @@ -640,7 +642,7 @@ static void btc_ble_mesh_prov_register_complete_cb(int err_code) static void btc_ble_mesh_prov_set_complete_cb(esp_ble_mesh_prov_cb_param_t *param, uint8_t act) { - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); btc_ble_mesh_prov_callback(param, act); return; @@ -650,15 +652,15 @@ static void btc_ble_mesh_prov_set_complete_cb(esp_ble_mesh_prov_cb_param_t *para static void btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb( const u8_t addr[6], const u8_t addr_type, const u8_t adv_type, const u8_t dev_uuid[16], - u16_t oob_info, bt_mesh_prov_bearer_t bearer) + u16_t oob_info, bt_mesh_prov_bearer_t bearer, s8_t rssi) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); if (addr == NULL || dev_uuid == NULL || (bearer != BLE_MESH_PROV_ADV && bearer != BLE_MESH_PROV_GATT)) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -668,6 +670,7 @@ static void btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb( mesh_param.provisioner_recv_unprov_adv_pkt.oob_info = oob_info; mesh_param.provisioner_recv_unprov_adv_pkt.adv_type = adv_type; mesh_param.provisioner_recv_unprov_adv_pkt.bearer = bearer; + mesh_param.provisioner_recv_unprov_adv_pkt.rssi = rssi; btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT); return; @@ -676,9 +679,9 @@ static void btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb( static int btc_ble_mesh_provisioner_prov_read_oob_pub_key_cb(u8_t link_idx) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - bt_status_t ret; + bt_status_t ret = BT_STATUS_SUCCESS; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.provisioner_prov_read_oob_pub_key.link_idx = link_idx; @@ -690,9 +693,9 @@ static int btc_ble_mesh_provisioner_prov_input_cb(u8_t method, bt_mesh_output_action_t act, u8_t size, u8_t link_idx) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - bt_status_t ret; + bt_status_t ret = BT_STATUS_SUCCESS; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.provisioner_prov_input.method = (esp_ble_mesh_oob_method_t)method; mesh_param.provisioner_prov_input.action = (esp_ble_mesh_output_action_t)act; @@ -707,9 +710,9 @@ static int btc_ble_mesh_provisioner_prov_output_cb(u8_t method, bt_mesh_input_action_t act, void *data, u8_t size, u8_t link_idx) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - bt_status_t ret; + bt_status_t ret = BT_STATUS_SUCCESS; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.provisioner_prov_output.method = (esp_ble_mesh_oob_method_t)method; mesh_param.provisioner_prov_output.action = (esp_ble_mesh_input_action_t)act; @@ -729,7 +732,7 @@ static void btc_ble_mesh_provisioner_link_open_cb(bt_mesh_prov_bearer_t bearer) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.provisioner_prov_link_open.bearer = (esp_ble_mesh_prov_bearer_t)bearer; @@ -741,7 +744,7 @@ static void btc_ble_mesh_provisioner_link_close_cb(bt_mesh_prov_bearer_t bearer, { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.provisioner_prov_link_close.bearer = (esp_ble_mesh_prov_bearer_t)bearer; mesh_param.provisioner_prov_link_close.reason = reason; @@ -751,13 +754,13 @@ static void btc_ble_mesh_provisioner_link_close_cb(bt_mesh_prov_bearer_t bearer, } static void btc_ble_mesh_provisioner_prov_complete_cb( - int node_idx, const u8_t device_uuid[16], + u16_t node_idx, const u8_t device_uuid[16], u16_t unicast_addr, u8_t element_num, u16_t netkey_idx) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.provisioner_prov_complete.node_idx = node_idx; mesh_param.provisioner_prov_complete.unicast_addr = unicast_addr; @@ -768,13 +771,24 @@ static void btc_ble_mesh_provisioner_prov_complete_cb( btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT); return; } + +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16]) +{ + return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_uuid(uuid); +} + +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr) +{ + return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_addr(unicast_addr); +} + #endif /* CONFIG_BLE_MESH_PROVISIONER */ static void btc_ble_mesh_heartbeat_msg_recv_cb(u8_t hops, u16_t feature) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.heartbeat_msg_recv.hops = hops; mesh_param.heartbeat_msg_recv.feature = feature; @@ -787,9 +801,9 @@ static void btc_ble_mesh_heartbeat_msg_recv_cb(u8_t hops, u16_t feature) static void btc_ble_mesh_lpn_cb(u16_t friend_addr, bool established) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - u8_t act; + u8_t act = 0U; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); if (established) { mesh_param.lpn_friendship_establish.friend_addr = friend_addr; @@ -808,12 +822,12 @@ static void btc_ble_mesh_lpn_cb(u16_t friend_addr, bool established) void btc_ble_mesh_friend_cb(bool establish, u16_t lpn_addr, u8_t reason) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - u8_t act; + u8_t act = 0U; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); if (!BLE_MESH_ADDR_IS_UNICAST(lpn_addr)) { - LOG_ERROR("%s, Not a unicast address", __func__); + BT_ERR("%s, Not a unicast address", __func__); return; } @@ -833,21 +847,22 @@ void btc_ble_mesh_friend_cb(bool establish, u16_t lpn_addr, u8_t reason) #if CONFIG_BLE_MESH_GATT_PROXY_CLIENT static void btc_ble_mesh_proxy_client_adv_recv_cb(const bt_mesh_addr_t *addr, - u8_t type, bt_mesh_proxy_adv_ctx_t *ctx) + u8_t type, bt_mesh_proxy_adv_ctx_t *ctx, s8_t rssi) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; if (!addr || !ctx || type != BLE_MESH_PROXY_ADV_NET_ID) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.proxy_client_recv_adv_pkt.addr_type = addr->type; memcpy(mesh_param.proxy_client_recv_adv_pkt.addr, addr->val, BD_ADDR_LEN); mesh_param.proxy_client_recv_adv_pkt.net_idx = ctx->net_id.net_idx; memcpy(mesh_param.proxy_client_recv_adv_pkt.net_id, ctx->net_id.net_id, 8); + mesh_param.proxy_client_recv_adv_pkt.rssi = rssi; btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_RECV_ADV_PKT_EVT); return; @@ -859,11 +874,11 @@ static void btc_ble_mesh_proxy_client_connect_cb(const bt_mesh_addr_t *addr, esp_ble_mesh_prov_cb_param_t mesh_param = {0}; if (!addr || conn_handle >= BLE_MESH_MAX_CONN) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.proxy_client_connected.addr_type = addr->type; memcpy(mesh_param.proxy_client_connected.addr, addr->val, BD_ADDR_LEN); @@ -880,11 +895,11 @@ static void btc_ble_mesh_proxy_client_disconnect_cb(const bt_mesh_addr_t *addr, esp_ble_mesh_prov_cb_param_t mesh_param = {0}; if (!addr || conn_handle >= BLE_MESH_MAX_CONN) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.proxy_client_disconnected.addr_type = addr->type; memcpy(mesh_param.proxy_client_disconnected.addr, addr->val, BD_ADDR_LEN); @@ -902,11 +917,11 @@ static void btc_ble_mesh_proxy_client_filter_status_recv_cb(u8_t conn_handle, esp_ble_mesh_prov_cb_param_t mesh_param = {0}; if (conn_handle >= BLE_MESH_MAX_CONN) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); mesh_param.proxy_client_recv_filter_status.conn_handle = conn_handle; mesh_param.proxy_client_recv_filter_status.server_addr = src; @@ -919,6 +934,11 @@ static void btc_ble_mesh_proxy_client_filter_status_recv_cb(u8_t conn_handle, } #endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ +int btc_ble_mesh_deinit(esp_ble_mesh_deinit_param_t *param) +{ + return bt_mesh_deinit((struct bt_mesh_deinit_param *)param); +} + int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model) { __ASSERT(model && model->op, "%s, Invalid parameter", __func__); @@ -930,6 +950,11 @@ int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model) return bt_mesh_client_init((struct bt_mesh_model *)model); } +int btc_ble_mesh_client_model_deinit(esp_ble_mesh_model_t *model) +{ + return bt_mesh_client_deinit((struct bt_mesh_model *)model); +} + int32_t btc_ble_mesh_model_pub_period_get(esp_ble_mesh_model_t *mod) { return bt_mesh_model_pub_period_get((struct bt_mesh_model *)mod); @@ -971,6 +996,11 @@ const esp_ble_mesh_comp_t *btc_ble_mesh_comp_get(void) return (const esp_ble_mesh_comp_t *)bt_mesh_comp_get(); } +u16_t btc_ble_mesh_provisioner_get_prov_node_count(void) +{ + return bt_mesh_provisioner_get_prov_node_count(); +} + /* Configuration Models */ extern const struct bt_mesh_model_op bt_mesh_cfg_srv_op[]; extern const struct bt_mesh_model_op bt_mesh_cfg_cli_op[]; @@ -1043,7 +1073,7 @@ static void btc_ble_mesh_model_op_add(esp_ble_mesh_model_t *model) esp_ble_mesh_model_op_t *op = NULL; if (!model) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -1398,7 +1428,7 @@ static void btc_ble_mesh_model_op_add(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)time_setup_srv_op; if (model->pub) { /* Time Setup Server model does not support subscribing nor publishing. */ - LOG_ERROR("%s, Time Setup Server shall not support publication", __func__); + BT_ERR("%s, Time Setup Server shall not support publication", __func__); return; } break; @@ -1459,10 +1489,10 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) { esp_ble_mesh_prov_cb_param_t param = {0}; btc_ble_mesh_prov_args_t *arg = NULL; - uint8_t act; + uint8_t act = 0U; if (!msg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -1470,7 +1500,7 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) switch (msg->act) { case BTC_BLE_MESH_ACT_MESH_INIT: { - int err_code; + int err_code = 0; for (int i = 0; i < arg->mesh_init.comp->element_count; i++) { esp_ble_mesh_elem_t *elem = &arg->mesh_init.comp->elements[i]; /* For SIG models */ @@ -1513,7 +1543,7 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) arg->mesh_init.prov->provisioner_link_open = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_link_open_cb; arg->mesh_init.prov->provisioner_link_close = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_link_close_cb; arg->mesh_init.prov->provisioner_prov_comp = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_prov_complete_cb; - bt_mesh_prov_adv_pkt_cb_register(btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb); + bt_mesh_provisioner_adv_pkt_cb_register(btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb); #endif /* CONFIG_BLE_MESH_PROVISIONER */ #if CONFIG_BLE_MESH_LOW_POWER bt_mesh_lpn_set_cb(btc_ble_mesh_lpn_cb); @@ -1536,17 +1566,17 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) } #if CONFIG_BLE_MESH_NODE case BTC_BLE_MESH_ACT_PROV_ENABLE: - LOG_DEBUG("%s, BTC_BLE_MESH_ACT_PROV_ENABLE, bearers = %d", __func__, arg->node_prov_enable.bearers); + BT_DBG("%s, BTC_BLE_MESH_ACT_PROV_ENABLE, bearers = %d", __func__, arg->node_prov_enable.bearers); act = ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT; param.node_prov_enable_comp.err_code = bt_mesh_prov_enable(arg->node_prov_enable.bearers); break; case BTC_BLE_MESH_ACT_PROV_DISABLE: - LOG_DEBUG("%s, BTC_BLE_MESH_ACT_PROV_DISABLE, bearers = %d", __func__, arg->node_prov_disable.bearers); + BT_DBG("%s, BTC_BLE_MESH_ACT_PROV_DISABLE, bearers = %d", __func__, arg->node_prov_disable.bearers); act = ESP_BLE_MESH_NODE_PROV_DISABLE_COMP_EVT; param.node_prov_disable_comp.err_code = bt_mesh_prov_disable(arg->node_prov_disable.bearers); break; case BTC_BLE_MESH_ACT_NODE_RESET: - LOG_DEBUG("%s, BTC_BLE_MESH_ACT_NODE_RESET", __func__); + BT_DBG("%s, BTC_BLE_MESH_ACT_NODE_RESET", __func__); bt_mesh_reset(); return; case BTC_BLE_MESH_ACT_SET_OOB_PUB_KEY: @@ -1564,6 +1594,9 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) act = ESP_BLE_MESH_NODE_PROV_INPUT_STRING_COMP_EVT; param.node_prov_input_str_comp.err_code = bt_mesh_input_string(arg->input_string.string); break; +#endif /* CONFIG_BLE_MESH_NODE */ +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER case BTC_BLE_MESH_ACT_SET_DEVICE_NAME: act = ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT; param.node_set_unprov_dev_name_comp.err_code = bt_mesh_set_device_name(arg->set_device_name.name); @@ -1582,25 +1615,25 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) param.node_proxy_gatt_disable_comp.err_code = bt_mesh_proxy_gatt_disable(); break; #endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ -#endif /* CONFIG_BLE_MESH_NODE */ +#endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ #if CONFIG_BLE_MESH_PROVISIONER case BTC_BLE_MESH_ACT_PROVISIONER_READ_OOB_PUB_KEY: act = ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_COMP_EVT; param.provisioner_prov_read_oob_pub_key_comp.err_code = - bt_mesh_prov_read_oob_pub_key(arg->provisioner_read_oob_pub_key.link_idx, - arg->provisioner_read_oob_pub_key.pub_key_x, - arg->provisioner_read_oob_pub_key.pub_key_y); + bt_mesh_provisioner_read_oob_pub_key(arg->provisioner_read_oob_pub_key.link_idx, + arg->provisioner_read_oob_pub_key.pub_key_x, + arg->provisioner_read_oob_pub_key.pub_key_y); break; case BTC_BLE_MESH_ACT_PROVISIONER_INPUT_STR: act = ESP_BLE_MESH_PROVISIONER_PROV_INPUT_STRING_COMP_EVT; param.provisioner_prov_input_str_comp.err_code = - bt_mesh_prov_set_oob_input_data(arg->provisioner_input_str.link_idx, - (const u8_t *)&arg->provisioner_input_str.string, false); + bt_mesh_provisioner_set_oob_input_data(arg->provisioner_input_str.link_idx, + (const u8_t *)&arg->provisioner_input_str.string, false); break; case BTC_BLE_MESH_ACT_PROVISIONER_INPUT_NUM: act = ESP_BLE_MESH_PROVISIONER_PROV_INPUT_NUMBER_COMP_EVT; param.provisioner_prov_input_num_comp.err_code = - bt_mesh_prov_set_oob_input_data(arg->provisioner_input_num.link_idx, + bt_mesh_provisioner_set_oob_input_data(arg->provisioner_input_num.link_idx, (const u8_t *)&arg->provisioner_input_num.number, true); break; case BTC_BLE_MESH_ACT_PROVISIONER_ENABLE: @@ -1625,6 +1658,14 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) bt_mesh_provisioner_add_unprov_dev(&add_dev, arg->provisioner_dev_add.flags); break; } + case BTC_BLE_MESH_ACT_PROVISIONER_PROV_DEV_WITH_ADDR: + act = ESP_BLE_MESH_PROVISIONER_PROV_DEV_WITH_ADDR_COMP_EVT; + param.provisioner_prov_dev_with_addr_comp.err_code = + bt_mesh_provisioner_prov_device_with_addr(arg->provisioner_prov_dev_with_addr.uuid, + arg->provisioner_prov_dev_with_addr.addr, arg->provisioner_prov_dev_with_addr.addr_type, + arg->provisioner_prov_dev_with_addr.bearer, arg->provisioner_prov_dev_with_addr.oob_info, + arg->provisioner_prov_dev_with_addr.unicast_addr); + break; case BTC_BLE_MESH_ACT_PROVISIONER_DEV_DEL: { struct bt_mesh_device_delete del_dev = {0}; if (arg->provisioner_dev_del.del_dev.flag & DEL_DEV_ADDR_FLAG) { @@ -1660,35 +1701,50 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) bt_mesh_provisioner_set_prov_data_info(&info); break; } + case BTC_BLE_MESH_ACT_PROVISIONER_SET_STATIC_OOB_VAL: + act = ESP_BLE_MESH_PROVISIONER_SET_STATIC_OOB_VALUE_COMP_EVT; + param.provisioner_set_static_oob_val_comp.err_code = + bt_mesh_provisioner_set_static_oob_value( + arg->set_static_oob_val.value, arg->set_static_oob_val.length); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_SET_PRIMARY_ELEM_ADDR: + act = ESP_BLE_MESH_PROVISIONER_SET_PRIMARY_ELEM_ADDR_COMP_EVT; + param.provisioner_set_primary_elem_addr_comp.err_code = + bt_mesh_provisioner_set_primary_elem_addr(arg->set_primary_elem_addr.addr); + break; case BTC_BLE_MESH_ACT_PROVISIONER_SET_NODE_NAME: act = ESP_BLE_MESH_PROVISIONER_SET_NODE_NAME_COMP_EVT; + param.provisioner_set_node_name_comp.node_index = arg->set_node_name.index; param.provisioner_set_node_name_comp.err_code = bt_mesh_provisioner_set_node_name(arg->set_node_name.index, arg->set_node_name.name); - if (param.provisioner_set_node_name_comp.err_code) { - param.provisioner_set_node_name_comp.node_index = ESP_BLE_MESH_INVALID_NODE_INDEX; - } else { - param.provisioner_set_node_name_comp.node_index = arg->set_node_name.index; - } break; - case BTC_BLE_MESH_ACT_PROVISIONER_SET_LOCAL_APP_KEY: { + case BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_APP_KEY: { const u8_t *app_key = NULL; const u8_t zero[16] = {0}; if (memcmp(arg->add_local_app_key.app_key, zero, 16)) { app_key = arg->add_local_app_key.app_key; } act = ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT; + param.provisioner_add_app_key_comp.app_idx = arg->add_local_app_key.app_idx; param.provisioner_add_app_key_comp.err_code = bt_mesh_provisioner_local_app_key_add(app_key, arg->add_local_app_key.net_idx, &arg->add_local_app_key.app_idx); - if (param.provisioner_add_app_key_comp.err_code) { - param.provisioner_add_app_key_comp.app_idx = ESP_BLE_MESH_KEY_UNUSED; - } else { - param.provisioner_add_app_key_comp.app_idx = arg->add_local_app_key.app_idx; - } break; } + case BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_APP_KEY: + act = ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_APP_KEY_COMP_EVT; + param.provisioner_update_app_key_comp.net_idx = arg->update_local_app_key.net_idx; + param.provisioner_update_app_key_comp.app_idx = arg->update_local_app_key.app_idx; + param.provisioner_update_app_key_comp.err_code = + bt_mesh_provisioner_local_app_key_update(arg->update_local_app_key.app_key, + arg->update_local_app_key.net_idx, arg->update_local_app_key.app_idx); + break; case BTC_BLE_MESH_ACT_PROVISIONER_BIND_LOCAL_MOD_APP: act = ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT; + param.provisioner_bind_app_key_to_model_comp.element_addr = arg->local_mod_app_bind.elem_addr; + param.provisioner_bind_app_key_to_model_comp.app_idx = arg->local_mod_app_bind.app_idx; + param.provisioner_bind_app_key_to_model_comp.company_id = arg->local_mod_app_bind.cid; + param.provisioner_bind_app_key_to_model_comp.model_id = arg->local_mod_app_bind.model_id; param.provisioner_bind_app_key_to_model_comp.err_code = bt_mesh_provisioner_bind_local_model_app_idx(arg->local_mod_app_bind.elem_addr, arg->local_mod_app_bind.model_id, @@ -1702,15 +1758,37 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) net_key = arg->add_local_net_key.net_key; } act = ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_NET_KEY_COMP_EVT; + param.provisioner_add_net_key_comp.net_idx = arg->add_local_net_key.net_idx; param.provisioner_add_net_key_comp.err_code = bt_mesh_provisioner_local_net_key_add(net_key, &arg->add_local_net_key.net_idx); - if (param.provisioner_add_net_key_comp.err_code) { - param.provisioner_add_net_key_comp.net_idx = ESP_BLE_MESH_KEY_UNUSED; - } else { - param.provisioner_add_net_key_comp.net_idx = arg->add_local_net_key.net_idx; - } break; } + case BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_NET_KEY: + act = ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_NET_KEY_COMP_EVT; + param.provisioner_update_net_key_comp.net_idx = arg->update_local_net_key.net_idx; + param.provisioner_update_net_key_comp.err_code = + bt_mesh_provisioner_local_net_key_update(arg->update_local_net_key.net_key, + arg->update_local_net_key.net_idx); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA: + act = ESP_BLE_MESH_PROVISIONER_STORE_NODE_COMP_DATA_COMP_EVT; + param.provisioner_store_node_comp_data_comp.addr = arg->store_node_comp_data.unicast_addr; + param.provisioner_store_node_comp_data_comp.err_code = + bt_mesh_provisioner_store_node_comp_data(arg->store_node_comp_data.unicast_addr, + arg->store_node_comp_data.data, arg->store_node_comp_data.length); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_UUID: + act = ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT; + memcpy(param.provisioner_delete_node_with_uuid_comp.uuid, arg->delete_node_with_uuid.uuid, 16); + param.provisioner_delete_node_with_uuid_comp.err_code = + bt_mesh_provisioner_delete_node_with_uuid(arg->delete_node_with_uuid.uuid); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_ADDR: + act = ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT; + param.provisioner_delete_node_with_addr_comp.unicast_addr = arg->delete_node_with_addr.unicast_addr; + param.provisioner_delete_node_with_addr_comp.err_code = + bt_mesh_provisioner_delete_node_with_addr(arg->delete_node_with_addr.unicast_addr); + break; #endif /* CONFIG_BLE_MESH_PROVISIONER */ #if CONFIG_BLE_MESH_FAST_PROV case BTC_BLE_MESH_ACT_SET_FAST_PROV_INFO: @@ -1807,7 +1885,7 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) } #endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ default: - LOG_WARN("%s, Invalid msg->act %d", __func__, msg->act); + BT_WARN("%s, Invalid msg->act %d", __func__, msg->act); return; } @@ -1825,7 +1903,7 @@ void btc_ble_mesh_prov_cb_handler(btc_msg_t *msg) esp_ble_mesh_prov_cb_param_t *param = NULL; if (!msg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -1834,17 +1912,17 @@ void btc_ble_mesh_prov_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_PROV_EVT_MAX) { btc_ble_mesh_prov_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } } void btc_ble_mesh_model_call_handler(btc_msg_t *msg) { btc_ble_mesh_model_args_t *arg = NULL; - int err; + int err = 0; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -1857,7 +1935,7 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) common.model = (struct bt_mesh_model *)(arg->model_publish.model); common.role = arg->model_publish.device_role; if (bt_mesh_set_client_model_role(&common)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } } @@ -1869,7 +1947,7 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) /* arg->model_send.length contains opcode & message, 8 is used for TransMIC */ struct net_buf_simple *buf = bt_mesh_alloc_buf(arg->model_send.length + 8); if (!buf) { - LOG_ERROR("%s, Failed to allocate memory", __func__); + BT_ERR("%s, Failed to allocate memory", __func__); break; } net_buf_simple_add_mem(buf, arg->model_send.data, arg->model_send.length); @@ -1887,7 +1965,7 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) /* arg->model_send.length contains opcode & message, 8 is used for TransMIC */ struct net_buf_simple *buf = bt_mesh_alloc_buf(arg->model_send.length + 8); if (!buf) { - LOG_ERROR("%s, Failed to allocate memory", __func__); + BT_ERR("%s, Failed to allocate memory", __func__); break; } net_buf_simple_add_mem(buf, arg->model_send.data, arg->model_send.length); @@ -1895,7 +1973,7 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) common.model = (struct bt_mesh_model *)(arg->model_send.model); common.role = arg->model_send.device_role; if (bt_mesh_set_client_model_role(&common)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } err = bt_mesh_client_send_msg((struct bt_mesh_model *)arg->model_send.model, @@ -1916,7 +1994,7 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) arg->model_update_state.type, err); break; default: - LOG_WARN("%s, Unknown msg->act %d", __func__, msg->act); + BT_WARN("%s, Unknown msg->act %d", __func__, msg->act); break; } @@ -1929,7 +2007,7 @@ void btc_ble_mesh_model_cb_handler(btc_msg_t *msg) esp_ble_mesh_model_cb_param_t *param = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -1938,7 +2016,7 @@ void btc_ble_mesh_model_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_MODEL_EVT_MAX) { btc_ble_mesh_model_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } btc_ble_mesh_model_free_req_data(msg); diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c index 603f90586..cc906d76d 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c @@ -15,10 +15,9 @@ #include #include -#include "btc/btc_manage.h" -#include "osi/allocator.h" - +#include "mesh_common.h" #include "sensor_client.h" + #include "btc_ble_mesh_sensor_model.h" #include "esp_ble_mesh_sensor_model_api.h" @@ -38,17 +37,17 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void { btc_ble_mesh_sensor_client_args_t *dst = (btc_ble_mesh_sensor_client_args_t *)p_dest; btc_ble_mesh_sensor_client_args_t *src = (btc_ble_mesh_sensor_client_args_t *)p_src; - u16_t length; + u16_t length = 0U; if (!msg || !dst || !src) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } switch (msg->act) { case BTC_BLE_MESH_ACT_SENSOR_CLIENT_GET_STATE: { - dst->sensor_client_get_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - dst->sensor_client_get_state.get_state = (esp_ble_mesh_sensor_client_get_state_t *)osi_malloc(sizeof(esp_ble_mesh_sensor_client_get_state_t)); + dst->sensor_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->sensor_client_get_state.get_state = (esp_ble_mesh_sensor_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_sensor_client_get_state_t)); if (dst->sensor_client_get_state.params && dst->sensor_client_get_state.get_state) { memcpy(dst->sensor_client_get_state.params, src->sensor_client_get_state.params, sizeof(esp_ble_mesh_client_common_param_t)); @@ -61,7 +60,7 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void length = src->sensor_client_get_state.get_state->column_get.raw_value_x->len; dst->sensor_client_get_state.get_state->column_get.raw_value_x = bt_mesh_alloc_buf(length); if (!dst->sensor_client_get_state.get_state->column_get.raw_value_x) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(dst->sensor_client_get_state.get_state->column_get.raw_value_x, @@ -74,7 +73,7 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void length = src->sensor_client_get_state.get_state->series_get.raw_value_x1->len; dst->sensor_client_get_state.get_state->series_get.raw_value_x1 = bt_mesh_alloc_buf(length); if (!dst->sensor_client_get_state.get_state->series_get.raw_value_x1) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(dst->sensor_client_get_state.get_state->series_get.raw_value_x1, @@ -85,7 +84,7 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void length = src->sensor_client_get_state.get_state->series_get.raw_value_x2->len; dst->sensor_client_get_state.get_state->series_get.raw_value_x2 = bt_mesh_alloc_buf(length); if (!dst->sensor_client_get_state.get_state->series_get.raw_value_x2) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(dst->sensor_client_get_state.get_state->series_get.raw_value_x2, @@ -97,13 +96,13 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void break; } } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } break; } case BTC_BLE_MESH_ACT_SENSOR_CLIENT_SET_STATE: { - dst->sensor_client_set_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - dst->sensor_client_set_state.set_state = (esp_ble_mesh_sensor_client_set_state_t *)osi_malloc(sizeof(esp_ble_mesh_sensor_client_set_state_t)); + dst->sensor_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->sensor_client_set_state.set_state = (esp_ble_mesh_sensor_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_sensor_client_set_state_t)); if (dst->sensor_client_set_state.params && dst->sensor_client_set_state.set_state) { memcpy(dst->sensor_client_set_state.params, src->sensor_client_set_state.params, sizeof(esp_ble_mesh_client_common_param_t)); @@ -116,7 +115,7 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void length = src->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down->len; dst->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down = bt_mesh_alloc_buf(length); if (!dst->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(dst->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down, @@ -127,7 +126,7 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void length = src->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up->len; dst->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up = bt_mesh_alloc_buf(length); if (!dst->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(dst->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up, @@ -138,7 +137,7 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void length = src->sensor_client_set_state.set_state->cadence_set.fast_cadence_low->len; dst->sensor_client_set_state.set_state->cadence_set.fast_cadence_low = bt_mesh_alloc_buf(length); if (!dst->sensor_client_set_state.set_state->cadence_set.fast_cadence_low) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(dst->sensor_client_set_state.set_state->cadence_set.fast_cadence_low, @@ -149,7 +148,7 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void length = src->sensor_client_set_state.set_state->cadence_set.fast_cadence_high->len; dst->sensor_client_set_state.set_state->cadence_set.fast_cadence_high = bt_mesh_alloc_buf(length); if (!dst->sensor_client_set_state.set_state->cadence_set.fast_cadence_high) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(dst->sensor_client_set_state.set_state->cadence_set.fast_cadence_high, @@ -162,7 +161,7 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void length = src->sensor_client_set_state.set_state->setting_set.sensor_setting_raw->len; dst->sensor_client_set_state.set_state->setting_set.sensor_setting_raw = bt_mesh_alloc_buf(length); if (!dst->sensor_client_set_state.set_state->setting_set.sensor_setting_raw) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(dst->sensor_client_set_state.set_state->setting_set.sensor_setting_raw, @@ -174,12 +173,12 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void break; } } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } break; } default: - LOG_DEBUG("%s, Unknown deep copy act %d", __func__, msg->act); + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); break; } } @@ -189,7 +188,7 @@ void btc_ble_mesh_sensor_client_arg_deep_free(btc_msg_t *msg) btc_ble_mesh_sensor_client_args_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -211,10 +210,10 @@ void btc_ble_mesh_sensor_client_arg_deep_free(btc_msg_t *msg) break; } } - osi_free(arg->sensor_client_get_state.get_state); + bt_mesh_free(arg->sensor_client_get_state.get_state); } if (arg->sensor_client_get_state.params) { - osi_free(arg->sensor_client_get_state.params); + bt_mesh_free(arg->sensor_client_get_state.params); } break; case BTC_BLE_MESH_ACT_SENSOR_CLIENT_SET_STATE: @@ -234,10 +233,10 @@ void btc_ble_mesh_sensor_client_arg_deep_free(btc_msg_t *msg) break; } } - osi_free(arg->sensor_client_set_state.set_state); + bt_mesh_free(arg->sensor_client_set_state.set_state); } if (arg->sensor_client_set_state.params) { - osi_free(arg->sensor_client_set_state.params); + bt_mesh_free(arg->sensor_client_set_state.params); } break; default: @@ -249,17 +248,17 @@ static void btc_ble_mesh_sensor_client_copy_req_data(btc_msg_t *msg, void *p_des { esp_ble_mesh_sensor_client_cb_param_t *p_dest_data = (esp_ble_mesh_sensor_client_cb_param_t *)p_dest; esp_ble_mesh_sensor_client_cb_param_t *p_src_data = (esp_ble_mesh_sensor_client_cb_param_t *)p_src; - u16_t length; + u16_t length = 0U; if (!msg || !p_src_data || !p_dest_data) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } if (p_src_data->params) { - p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); if (!p_dest_data->params) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } @@ -278,7 +277,7 @@ static void btc_ble_mesh_sensor_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.descriptor_status.descriptor->len; p_dest_data->status_cb.descriptor_status.descriptor = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.descriptor_status.descriptor) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.descriptor_status.descriptor, @@ -293,7 +292,7 @@ static void btc_ble_mesh_sensor_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.cadence_status.sensor_cadence_value->len; p_dest_data->status_cb.cadence_status.sensor_cadence_value = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.cadence_status.sensor_cadence_value) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.cadence_status.sensor_cadence_value, @@ -307,7 +306,7 @@ static void btc_ble_mesh_sensor_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.settings_status.sensor_setting_property_ids->len; p_dest_data->status_cb.settings_status.sensor_setting_property_ids = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.settings_status.sensor_setting_property_ids) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.settings_status.sensor_setting_property_ids, @@ -322,7 +321,7 @@ static void btc_ble_mesh_sensor_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.setting_status.sensor_setting_raw->len; p_dest_data->status_cb.setting_status.sensor_setting_raw = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.setting_status.sensor_setting_raw) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.setting_status.sensor_setting_raw, @@ -336,7 +335,7 @@ static void btc_ble_mesh_sensor_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.sensor_status.marshalled_sensor_data->len; p_dest_data->status_cb.sensor_status.marshalled_sensor_data = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.sensor_status.marshalled_sensor_data) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.sensor_status.marshalled_sensor_data, @@ -350,7 +349,7 @@ static void btc_ble_mesh_sensor_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.column_status.sensor_column_value->len; p_dest_data->status_cb.column_status.sensor_column_value = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.column_status.sensor_column_value) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.column_status.sensor_column_value, @@ -364,7 +363,7 @@ static void btc_ble_mesh_sensor_client_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->status_cb.series_status.sensor_series_value->len; p_dest_data->status_cb.series_status.sensor_series_value = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.series_status.sensor_series_value) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.series_status.sensor_series_value, @@ -388,7 +387,7 @@ static void btc_ble_mesh_sensor_client_free_req_data(btc_msg_t *msg) esp_ble_mesh_sensor_client_cb_param_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -436,7 +435,7 @@ static void btc_ble_mesh_sensor_client_free_req_data(btc_msg_t *msg) } case ESP_BLE_MESH_SENSOR_CLIENT_TIMEOUT_EVT: if (arg->params) { - osi_free(arg->params); + bt_mesh_free(arg->params); } break; default: @@ -448,7 +447,7 @@ static void btc_ble_mesh_sensor_client_callback(esp_ble_mesh_sensor_client_cb_pa { btc_msg_t msg = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_SENSOR_CLIENT)) { @@ -470,11 +469,11 @@ void bt_mesh_sensor_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, { esp_ble_mesh_sensor_client_cb_param_t cb_params = {0}; esp_ble_mesh_client_common_param_t params = {0}; - size_t length; - uint8_t act; + size_t length = 0U; + uint8_t act = 0U; if (!model || !ctx) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -492,7 +491,7 @@ void bt_mesh_sensor_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, act = ESP_BLE_MESH_SENSOR_CLIENT_TIMEOUT_EVT; break; default: - LOG_ERROR("%s, Unknown sensor client event type %d", __func__, evt_type); + BT_ERR("%s, Unknown sensor client event type %d", __func__, evt_type); return; } @@ -523,7 +522,7 @@ void btc_ble_mesh_sensor_client_publish_callback(u32_t opcode, struct net_buf_simple *buf) { if (!model || !ctx || !buf) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -541,7 +540,7 @@ void btc_ble_mesh_sensor_client_call_handler(btc_msg_t *msg) bt_mesh_role_param_t role_param = {0}; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -553,7 +552,7 @@ void btc_ble_mesh_sensor_client_call_handler(btc_msg_t *msg) role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } common.opcode = params->opcode; @@ -579,7 +578,7 @@ void btc_ble_mesh_sensor_client_call_handler(btc_msg_t *msg) role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } common.opcode = params->opcode; @@ -613,7 +612,7 @@ void btc_ble_mesh_sensor_client_cb_handler(btc_msg_t *msg) esp_ble_mesh_sensor_client_cb_param_t *param = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -622,7 +621,7 @@ void btc_ble_mesh_sensor_client_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_SENSOR_CLIENT_EVT_MAX) { btc_ble_mesh_sensor_client_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } btc_ble_mesh_sensor_client_free_req_data(msg); @@ -646,10 +645,10 @@ static void btc_ble_mesh_sensor_server_copy_req_data(btc_msg_t *msg, void *p_des { esp_ble_mesh_sensor_server_cb_param_t *p_dest_data = (esp_ble_mesh_sensor_server_cb_param_t *)p_dest; esp_ble_mesh_sensor_server_cb_param_t *p_src_data = (esp_ble_mesh_sensor_server_cb_param_t *)p_src; - u16_t length; + u16_t length = 0U; if (!msg || !p_src_data || !p_dest_data) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -661,7 +660,7 @@ static void btc_ble_mesh_sensor_server_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->value.state_change.sensor_cadence_set.trigger_delta_down->len; p_dest_data->value.state_change.sensor_cadence_set.trigger_delta_down = bt_mesh_alloc_buf(length); if (p_dest_data->value.state_change.sensor_cadence_set.trigger_delta_down == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.state_change.sensor_cadence_set.trigger_delta_down, @@ -672,7 +671,7 @@ static void btc_ble_mesh_sensor_server_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->value.state_change.sensor_cadence_set.trigger_delta_up->len; p_dest_data->value.state_change.sensor_cadence_set.trigger_delta_up = bt_mesh_alloc_buf(length); if (p_dest_data->value.state_change.sensor_cadence_set.trigger_delta_up == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.state_change.sensor_cadence_set.trigger_delta_up, @@ -683,7 +682,7 @@ static void btc_ble_mesh_sensor_server_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->value.state_change.sensor_cadence_set.fast_cadence_low->len; p_dest_data->value.state_change.sensor_cadence_set.fast_cadence_low = bt_mesh_alloc_buf(length); if (p_dest_data->value.state_change.sensor_cadence_set.fast_cadence_low == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.state_change.sensor_cadence_set.fast_cadence_low, @@ -694,7 +693,7 @@ static void btc_ble_mesh_sensor_server_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->value.state_change.sensor_cadence_set.fast_cadence_high->len; p_dest_data->value.state_change.sensor_cadence_set.fast_cadence_high = bt_mesh_alloc_buf(length); if (p_dest_data->value.state_change.sensor_cadence_set.fast_cadence_high == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.state_change.sensor_cadence_set.fast_cadence_high, @@ -707,7 +706,7 @@ static void btc_ble_mesh_sensor_server_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->value.state_change.sensor_setting_set.setting_value->len; p_dest_data->value.state_change.sensor_setting_set.setting_value = bt_mesh_alloc_buf(length); if (p_dest_data->value.state_change.sensor_setting_set.setting_value == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.state_change.sensor_setting_set.setting_value, @@ -722,7 +721,7 @@ static void btc_ble_mesh_sensor_server_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->value.get.sensor_column.raw_value_x->len; p_dest_data->value.get.sensor_column.raw_value_x = bt_mesh_alloc_buf(length); if (p_dest_data->value.get.sensor_column.raw_value_x == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.get.sensor_column.raw_value_x, @@ -734,7 +733,7 @@ static void btc_ble_mesh_sensor_server_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->value.get.sensor_series.raw_value->len; p_dest_data->value.get.sensor_series.raw_value = bt_mesh_alloc_buf(length); if (p_dest_data->value.get.sensor_series.raw_value == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.get.sensor_series.raw_value, @@ -750,7 +749,7 @@ static void btc_ble_mesh_sensor_server_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->value.set.sensor_cadence.cadence->len; p_dest_data->value.set.sensor_cadence.cadence = bt_mesh_alloc_buf(length); if (p_dest_data->value.set.sensor_cadence.cadence == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.set.sensor_cadence.cadence, @@ -763,7 +762,7 @@ static void btc_ble_mesh_sensor_server_copy_req_data(btc_msg_t *msg, void *p_des length = p_src_data->value.set.sensor_setting.setting_raw->len; p_dest_data->value.set.sensor_setting.setting_raw = bt_mesh_alloc_buf(length); if (p_dest_data->value.set.sensor_setting.setting_raw == NULL) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->value.set.sensor_setting.setting_raw, @@ -782,7 +781,7 @@ static void btc_ble_mesh_sensor_server_free_req_data(btc_msg_t *msg) esp_ble_mesh_sensor_server_cb_param_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -826,7 +825,7 @@ static void btc_ble_mesh_sensor_server_callback(esp_ble_mesh_sensor_server_cb_pa { btc_msg_t msg = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_SENSOR_SERVER)) { @@ -847,11 +846,11 @@ void bt_mesh_sensor_server_cb_evt_to_btc(u8_t evt_type, const u8_t *val, size_t len) { esp_ble_mesh_sensor_server_cb_param_t cb_params = {0}; - size_t length; - uint8_t act; + size_t length = 0U; + uint8_t act = 0U; if (model == NULL || ctx == NULL) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -866,7 +865,7 @@ void bt_mesh_sensor_server_cb_evt_to_btc(u8_t evt_type, act = ESP_BLE_MESH_SENSOR_SERVER_RECV_SET_MSG_EVT; break; default: - LOG_ERROR("%s, Unknown Sensor Server event type", __func__); + BT_ERR("%s, Unknown Sensor Server event type", __func__); return; } @@ -892,7 +891,7 @@ void btc_ble_mesh_sensor_server_cb_handler(btc_msg_t *msg) esp_ble_mesh_sensor_server_cb_param_t *param = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -901,7 +900,7 @@ void btc_ble_mesh_sensor_server_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_SENSOR_SERVER_EVT_MAX) { btc_ble_mesh_sensor_server_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } btc_ble_mesh_sensor_server_free_req_data(msg); diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c index aeb65657e..d33702edd 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c @@ -15,10 +15,9 @@ #include #include -#include "btc/btc_manage.h" -#include "osi/allocator.h" - +#include "mesh_common.h" #include "time_scene_client.h" + #include "btc_ble_mesh_time_scene_model.h" #include "esp_ble_mesh_time_scene_model_api.h" @@ -40,39 +39,46 @@ void btc_ble_mesh_time_scene_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, btc_ble_mesh_time_scene_client_args_t *src = (btc_ble_mesh_time_scene_client_args_t *)p_src; if (!msg || !dst || !src) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } switch (msg->act) { case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_GET_STATE: { - dst->time_scene_client_get_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - dst->time_scene_client_get_state.get_state = (esp_ble_mesh_time_scene_client_get_state_t *)osi_malloc(sizeof(esp_ble_mesh_time_scene_client_get_state_t)); - if (dst->time_scene_client_get_state.params && dst->time_scene_client_get_state.get_state) { + dst->time_scene_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->time_scene_client_get_state.params) { memcpy(dst->time_scene_client_get_state.params, src->time_scene_client_get_state.params, sizeof(esp_ble_mesh_client_common_param_t)); - memcpy(dst->time_scene_client_get_state.get_state, src->time_scene_client_get_state.get_state, - sizeof(esp_ble_mesh_time_scene_client_get_state_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + break; + } + if (src->time_scene_client_get_state.get_state) { + dst->time_scene_client_get_state.get_state = (esp_ble_mesh_time_scene_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_time_scene_client_get_state_t)); + if (dst->time_scene_client_get_state.get_state) { + memcpy(dst->time_scene_client_get_state.get_state, src->time_scene_client_get_state.get_state, + sizeof(esp_ble_mesh_time_scene_client_get_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } } break; } case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_SET_STATE: { - dst->time_scene_client_set_state.params = (esp_ble_mesh_client_common_param_t *)osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); - dst->time_scene_client_set_state.set_state = (esp_ble_mesh_time_scene_client_set_state_t *)osi_malloc(sizeof(esp_ble_mesh_time_scene_client_set_state_t)); + dst->time_scene_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->time_scene_client_set_state.set_state = (esp_ble_mesh_time_scene_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_time_scene_client_set_state_t)); if (dst->time_scene_client_set_state.params && dst->time_scene_client_set_state.set_state) { memcpy(dst->time_scene_client_set_state.params, src->time_scene_client_set_state.params, sizeof(esp_ble_mesh_client_common_param_t)); memcpy(dst->time_scene_client_set_state.set_state, src->time_scene_client_set_state.set_state, sizeof(esp_ble_mesh_time_scene_client_set_state_t)); } else { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); } break; } default: - LOG_DEBUG("%s, Unknown deep copy act %d", __func__, msg->act); + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); break; } } @@ -82,7 +88,7 @@ void btc_ble_mesh_time_scene_client_arg_deep_free(btc_msg_t *msg) btc_ble_mesh_time_scene_client_args_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -91,18 +97,18 @@ void btc_ble_mesh_time_scene_client_arg_deep_free(btc_msg_t *msg) switch (msg->act) { case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_GET_STATE: if (arg->time_scene_client_get_state.params) { - osi_free(arg->time_scene_client_get_state.params); + bt_mesh_free(arg->time_scene_client_get_state.params); } if (arg->time_scene_client_get_state.get_state) { - osi_free(arg->time_scene_client_get_state.get_state); + bt_mesh_free(arg->time_scene_client_get_state.get_state); } break; case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_SET_STATE: if (arg->time_scene_client_set_state.params) { - osi_free(arg->time_scene_client_set_state.params); + bt_mesh_free(arg->time_scene_client_set_state.params); } if (arg->time_scene_client_set_state.set_state) { - osi_free(arg->time_scene_client_set_state.set_state); + bt_mesh_free(arg->time_scene_client_set_state.set_state); } break; default: @@ -114,17 +120,17 @@ static void btc_ble_mesh_time_scene_client_copy_req_data(btc_msg_t *msg, void *p { esp_ble_mesh_time_scene_client_cb_param_t *p_dest_data = (esp_ble_mesh_time_scene_client_cb_param_t *)p_dest; esp_ble_mesh_time_scene_client_cb_param_t *p_src_data = (esp_ble_mesh_time_scene_client_cb_param_t *)p_src; - u16_t length; + u16_t length = 0U; if (!msg || !p_src_data || !p_dest_data) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } if (p_src_data->params) { - p_dest_data->params = osi_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); if (!p_dest_data->params) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } @@ -145,7 +151,7 @@ static void btc_ble_mesh_time_scene_client_copy_req_data(btc_msg_t *msg, void *p length = p_src_data->status_cb.scene_register_status.scenes->len; p_dest_data->status_cb.scene_register_status.scenes = bt_mesh_alloc_buf(length); if (!p_dest_data->status_cb.scene_register_status.scenes) { - LOG_ERROR("%s, Failed to allocate memory, act %d", __func__, msg->act); + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); return; } net_buf_simple_add_mem(p_dest_data->status_cb.scene_register_status.scenes, @@ -169,7 +175,7 @@ static void btc_ble_mesh_time_scene_client_free_req_data(btc_msg_t *msg) esp_ble_mesh_time_scene_client_cb_param_t *arg = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -193,7 +199,7 @@ static void btc_ble_mesh_time_scene_client_free_req_data(btc_msg_t *msg) } case ESP_BLE_MESH_TIME_SCENE_CLIENT_TIMEOUT_EVT: if (arg->params) { - osi_free(arg->params); + bt_mesh_free(arg->params); } break; default: @@ -205,7 +211,7 @@ static void btc_ble_mesh_time_scene_client_callback(esp_ble_mesh_time_scene_clie { btc_msg_t msg = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_TIME_SCENE_CLIENT)) { @@ -227,11 +233,11 @@ void bt_mesh_time_scene_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, { esp_ble_mesh_time_scene_client_cb_param_t cb_params = {0}; esp_ble_mesh_client_common_param_t params = {0}; - size_t length; - uint8_t act; + size_t length = 0U; + uint8_t act = 0U; if (!model || !ctx) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -249,7 +255,7 @@ void bt_mesh_time_scene_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, act = ESP_BLE_MESH_TIME_SCENE_CLIENT_TIMEOUT_EVT; break; default: - LOG_ERROR("%s, Unknown time scene client event type %d", __func__, evt_type); + BT_ERR("%s, Unknown time scene client event type %d", __func__, evt_type); return; } @@ -280,7 +286,7 @@ void btc_ble_mesh_time_scene_client_publish_callback(u32_t opcode, struct net_buf_simple *buf) { if (!model || !ctx || !buf) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -298,7 +304,7 @@ void btc_ble_mesh_time_scene_client_call_handler(btc_msg_t *msg) bt_mesh_role_param_t role_param = {0}; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -310,7 +316,7 @@ void btc_ble_mesh_time_scene_client_call_handler(btc_msg_t *msg) role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } common.opcode = params->opcode; @@ -336,7 +342,7 @@ void btc_ble_mesh_time_scene_client_call_handler(btc_msg_t *msg) role_param.model = (struct bt_mesh_model *)params->model; role_param.role = params->msg_role; if (bt_mesh_set_client_model_role(&role_param)) { - LOG_ERROR("%s, Failed to set model role", __func__); + BT_ERR("%s, Failed to set model role", __func__); break; } common.opcode = params->opcode; @@ -370,7 +376,7 @@ void btc_ble_mesh_time_scene_client_cb_handler(btc_msg_t *msg) esp_ble_mesh_time_scene_client_cb_param_t *param = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -379,7 +385,7 @@ void btc_ble_mesh_time_scene_client_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_TIME_SCENE_CLIENT_EVT_MAX) { btc_ble_mesh_time_scene_client_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } btc_ble_mesh_time_scene_client_free_req_data(msg); @@ -403,7 +409,7 @@ static void btc_ble_mesh_time_scene_server_callback(esp_ble_mesh_time_scene_serv { btc_msg_t msg = {0}; - LOG_DEBUG("%s", __func__); + BT_DBG("%s", __func__); /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_TIME_SCENE_SERVER)) { @@ -424,11 +430,11 @@ void bt_mesh_time_scene_server_cb_evt_to_btc(u8_t evt_type, const u8_t *val, size_t len) { esp_ble_mesh_time_scene_server_cb_param_t cb_params = {0}; - size_t length; - uint8_t act; + size_t length = 0U; + uint8_t act = 0U; if (model == NULL || ctx == NULL) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -446,7 +452,7 @@ void bt_mesh_time_scene_server_cb_evt_to_btc(u8_t evt_type, act = ESP_BLE_MESH_TIME_SCENE_SERVER_RECV_STATUS_MSG_EVT; break; default: - LOG_ERROR("%s, Unknown Time Scene Server event type", __func__); + BT_ERR("%s, Unknown Time Scene Server event type", __func__); return; } @@ -472,7 +478,7 @@ void btc_ble_mesh_time_scene_server_cb_handler(btc_msg_t *msg) esp_ble_mesh_time_scene_server_cb_param_t *param = NULL; if (!msg || !msg->arg) { - LOG_ERROR("%s, Invalid parameter", __func__); + BT_ERR("%s, Invalid parameter", __func__); return; } @@ -481,7 +487,7 @@ void btc_ble_mesh_time_scene_server_cb_handler(btc_msg_t *msg) if (msg->act < ESP_BLE_MESH_TIME_SCENE_SERVER_EVT_MAX) { btc_ble_mesh_time_scene_server_cb_to_app(msg->act, param); } else { - LOG_ERROR("%s, Unknown msg->act = %d", __func__, msg->act); + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); } return; diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_config_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_config_model.h index 6070e7636..f9920edbe 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_config_model.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_config_model.h @@ -15,8 +15,7 @@ #ifndef _BTC_BLE_MESH_CONFIG_MODEL_H_ #define _BTC_BLE_MESH_CONFIG_MODEL_H_ -#include -#include "btc/btc_task.h" +#include "btc/btc_manage.h" #include "esp_ble_mesh_config_model_api.h" typedef enum { diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_generic_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_generic_model.h index 20c6bad09..ca615a0fc 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_generic_model.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_generic_model.h @@ -15,8 +15,7 @@ #ifndef _BTC_BLE_MESH_GENERIC_MODEL_H_ #define _BTC_BLE_MESH_GENERIC_MODEL_H_ -#include -#include "btc/btc_task.h" +#include "btc/btc_manage.h" #include "esp_ble_mesh_generic_model_api.h" typedef enum { diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_health_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_health_model.h index 5d0dcd25d..90aba889a 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_health_model.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_health_model.h @@ -15,8 +15,7 @@ #ifndef _BTC_BLE_MESH_HEALTH_MODEL_H_ #define _BTC_BLE_MESH_HEALTH_MODEL_H_ -#include -#include "btc/btc_task.h" +#include "btc/btc_manage.h" #include "esp_ble_mesh_health_model_api.h" typedef enum { diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_lighting_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_lighting_model.h index 55915bc7e..97e67001a 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_lighting_model.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_lighting_model.h @@ -15,8 +15,7 @@ #ifndef _BTC_BLE_MESH_LIGHTING_MODEL_H_ #define _BTC_BLE_MESH_LIGHTING_MODEL_H_ -#include -#include "btc/btc_task.h" +#include "btc/btc_manage.h" #include "esp_ble_mesh_lighting_model_api.h" typedef enum { diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h index fbe6a364d..c55de2135 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h @@ -15,15 +15,7 @@ #ifndef _BTC_BLE_MESH_PROV_H_ #define _BTC_BLE_MESH_PROV_H_ -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/queue.h" -#include "freertos/semphr.h" - -#include "btc/btc_task.h" - -#include "mesh_access.h" -#include "mesh_buf.h" +#include "btc/btc_manage.h" #include "mesh_main.h" #include "provisioner_prov.h" #include "esp_ble_mesh_defs.h" @@ -46,13 +38,21 @@ typedef enum { BTC_BLE_MESH_ACT_PROVISIONER_ENABLE, BTC_BLE_MESH_ACT_PROVISIONER_DISABLE, BTC_BLE_MESH_ACT_PROVISIONER_DEV_ADD, + BTC_BLE_MESH_ACT_PROVISIONER_PROV_DEV_WITH_ADDR, BTC_BLE_MESH_ACT_PROVISIONER_DEV_DEL, BTC_BLE_MESH_ACT_PROVISIONER_SET_DEV_UUID_MATCH, BTC_BLE_MESH_ACT_PROVISIONER_SET_PROV_DATA_INFO, + BTC_BLE_MESH_ACT_PROVISIONER_SET_STATIC_OOB_VAL, + BTC_BLE_MESH_ACT_PROVISIONER_SET_PRIMARY_ELEM_ADDR, BTC_BLE_MESH_ACT_PROVISIONER_SET_NODE_NAME, - BTC_BLE_MESH_ACT_PROVISIONER_SET_LOCAL_APP_KEY, + BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_APP_KEY, + BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_APP_KEY, BTC_BLE_MESH_ACT_PROVISIONER_BIND_LOCAL_MOD_APP, BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_NET_KEY, + BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_NET_KEY, + BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA, + BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_UUID, + BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_ADDR, BTC_BLE_MESH_ACT_SET_FAST_PROV_INFO, BTC_BLE_MESH_ACT_SET_FAST_PROV_ACTION, BTC_BLE_MESH_ACT_LPN_ENABLE, @@ -121,6 +121,14 @@ typedef union { esp_ble_mesh_unprov_dev_add_t add_dev; esp_ble_mesh_dev_add_flag_t flags; } provisioner_dev_add; + struct ble_mesh_provisioner_prov_dev_with_addr_args { + uint8_t uuid[16]; + esp_ble_mesh_bd_addr_t addr; + esp_ble_mesh_addr_type_t addr_type; + esp_ble_mesh_prov_bearer_t bearer; + uint16_t oob_info; + uint16_t unicast_addr; + } provisioner_prov_dev_with_addr; struct ble_mesh_provisioner_dev_del_args { esp_ble_mesh_device_delete_t del_dev; } provisioner_dev_del; @@ -133,8 +141,15 @@ typedef union { struct ble_mesh_provisioner_set_prov_net_idx_args { esp_ble_mesh_prov_data_info_t prov_data; } set_prov_data_info; + struct ble_mesh_provisioner_set_static_oob_val_args { + uint8_t value[16]; + uint8_t length; + } set_static_oob_val; + struct ble_mesh_provisioner_set_primary_elem_addr_args { + uint16_t addr; + } set_primary_elem_addr; struct ble_mesh_provisioner_set_node_name_args { - int index; + uint16_t index; char name[ESP_BLE_MESH_NODE_NAME_MAX_LEN]; } set_node_name; struct ble_mesh_provisioner_add_local_app_key_args { @@ -142,6 +157,11 @@ typedef union { uint16_t net_idx; uint16_t app_idx; } add_local_app_key; + struct ble_mesh_provisioner_update_local_app_key_args { + uint8_t app_key[16]; + uint16_t net_idx; + uint16_t app_idx; + } update_local_app_key; struct ble_mesh_provisioner_bind_local_mod_app_args { uint16_t elem_addr; uint16_t model_id; @@ -152,6 +172,21 @@ typedef union { uint8_t net_key[16]; uint16_t net_idx; } add_local_net_key; + struct ble_mesh_provisioner_update_local_net_key_args { + uint8_t net_key[16]; + uint16_t net_idx; + } update_local_net_key; + struct ble_mesh_provisioner_store_node_comp_data_args { + uint16_t unicast_addr; + uint16_t length; + uint8_t *data; + } store_node_comp_data; + struct ble_mesh_provisioner_delete_node_with_uuid_args { + uint8_t uuid[16]; + } delete_node_with_uuid; + struct ble_mesh_provisioner_delete_node_with_addr_args { + uint16_t unicast_addr; + } delete_node_with_addr; struct ble_mesh_set_fast_prov_info_args { uint16_t unicast_min; uint16_t unicast_max; @@ -227,8 +262,16 @@ void btc_ble_mesh_prov_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); void btc_ble_mesh_model_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16]); + +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr); + +int btc_ble_mesh_deinit(esp_ble_mesh_deinit_param_t *param); + int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model); +int btc_ble_mesh_client_model_deinit(esp_ble_mesh_model_t *model); + int32_t btc_ble_mesh_model_pub_period_get(esp_ble_mesh_model_t *mod); uint16_t btc_ble_mesh_get_primary_addr(void); @@ -247,6 +290,8 @@ esp_ble_mesh_model_t *btc_ble_mesh_model_find(const esp_ble_mesh_elem_t *elem, const esp_ble_mesh_comp_t *btc_ble_mesh_comp_get(void); +u16_t btc_ble_mesh_provisioner_get_prov_node_count(void); + void btc_ble_mesh_model_call_handler(btc_msg_t *msg); void btc_ble_mesh_model_cb_handler(btc_msg_t *msg); diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_sensor_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_sensor_model.h index 8511fbcfa..307cd562d 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_sensor_model.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_sensor_model.h @@ -15,8 +15,7 @@ #ifndef _BTC_BLE_MESH_SENSOR_MODEL_H_ #define _BTC_BLE_MESH_SENSOR_MODEL_H_ -#include -#include "btc/btc_task.h" +#include "btc/btc_manage.h" #include "esp_ble_mesh_sensor_model_api.h" typedef enum { diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_time_scene_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_time_scene_model.h index e1d0d3ba7..a2df94501 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_time_scene_model.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_time_scene_model.h @@ -15,8 +15,7 @@ #ifndef _BTC_BLE_MESH_TIME_SCENE_MODEL_H_ #define _BTC_BLE_MESH_TIME_SCENE_MODEL_H_ -#include -#include "btc/btc_task.h" +#include "btc/btc_manage.h" #include "esp_ble_mesh_time_scene_model_api.h" typedef enum { diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h index e32569589..9609698be 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h @@ -11,12 +11,7 @@ #ifndef _BLE_MESH_BUF_H_ #define _BLE_MESH_BUF_H_ -#include -#include "sys/cdefs.h" -#include "mesh_types.h" #include "mesh_slist.h" -#include "mesh_kernel.h" -#include "mesh_util.h" #ifdef __cplusplus extern "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 dd058c70a..737b8394e 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 @@ -16,16 +16,30 @@ * @brief Bluetooth Mesh Model Common APIs. */ -#ifndef _MESH_COMMON_H_ -#define _MESH_COMMON_H_ +#ifndef _BLE_MESH_COMMON_H_ +#define _BLE_MESH_COMMON_H_ -#include "osi/allocator.h" +#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_types.h" -#include "mesh_buf.h" -#include "mesh_trace.h" #include "mesh_access.h" +#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)) + /** * @brief This function allocates memory to store outgoing message. * @@ -58,4 +72,19 @@ 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); -#endif /* _MESH_COMMON_H_ */ \ No newline at end of file +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); + +#endif /* _BLE_MESH_COMMON_H_ */ \ No newline at end of file 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 5e95e3b1a..e0052e528 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 @@ -8,11 +8,9 @@ #ifndef _BLE_MESH_KERNEL_H_ #define _BLE_MESH_KERNEL_H_ -#include "osi/mutex.h" #include "mesh_types.h" #include "mesh_slist.h" #include "mesh_atomic.h" -#include "mesh_dlist.h" /* number of nsec per usec */ #define NSEC_PER_USEC 1000 @@ -47,8 +45,6 @@ struct k_work; */ typedef void (*k_work_handler_t)(struct k_work *work); -typedef sys_dlist_t _wait_q_t; - struct k_work { void *_reserved; k_work_handler_t handler; @@ -278,6 +274,7 @@ void bt_mesh_atomic_lock(void); void bt_mesh_atomic_unlock(void); void bt_mesh_k_init(void); +void bt_mesh_k_deinit(void); #endif /* _BLE_MESH_KERNEL_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_trace.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_trace.h index dee8bb7cd..821f5716c 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_trace.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_trace.h @@ -10,7 +10,6 @@ #define _BLE_MESH_TRACE_H_ #include "esp_log.h" -#include "sdkconfig.h" /* Define common tracing for all */ #ifndef LOG_LEVEL_ERROR @@ -47,12 +46,6 @@ #define MESH_TRACE_TAG "BLE_MESH" -#define LOG_ERROR(format, ... ) {if (LOG_LOCAL_LEVEL >= ESP_LOG_ERROR) esp_log_write(ESP_LOG_ERROR, "BT_LOG", LOG_FORMAT(E, format), esp_log_timestamp(), "BT_LOG", ##__VA_ARGS__); } -#define LOG_WARN(format, ... ) {if (LOG_LOCAL_LEVEL >= ESP_LOG_WARN) esp_log_write(ESP_LOG_WARN, "BT_LOG", LOG_FORMAT(W, format), esp_log_timestamp(), "BT_LOG", ##__VA_ARGS__); } -#define LOG_INFO(format, ... ) {if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) esp_log_write(ESP_LOG_INFO, "BT_LOG", LOG_FORMAT(I, format), esp_log_timestamp(), "BT_LOG", ##__VA_ARGS__); } -#define LOG_DEBUG(format, ... ) {if (LOG_LOCAL_LEVEL >= ESP_LOG_DEBUG) esp_log_write(ESP_LOG_DEBUG, "BT_LOG", LOG_FORMAT(D, format), esp_log_timestamp(), "BT_LOG", ##__VA_ARGS__); } -#define LOG_VERBOSE(format, ... ) {if (LOG_LOCAL_LEVEL >= ESP_LOG_VERBOSE) esp_log_write(ESP_LOG_VERBOSE, "BT_LOG", LOG_FORMAT(V, format), esp_log_timestamp(), "BT_LOG", ##__VA_ARGS__); } - #if (LOG_LOCAL_LEVEL >= 4) #define BLE_MESH_LOG_LOCAL_LEVEL_MAPPING (LOG_LOCAL_LEVEL + 1) #else diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_util.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_util.h index 623a69e3e..19c0a17ad 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_util.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_util.h @@ -15,9 +15,9 @@ #define _BLE_MESH_UTIL_H_ #include +#include "soc/soc.h" #include "mesh_types.h" #include "mesh_trace.h" -#include "soc/soc.h" #ifdef __cplusplus extern "C" { diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_aes_encrypt.c b/components/bt/esp_ble_mesh/mesh_common/mesh_aes_encrypt.c index 2d1842cc4..544bbea72 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_aes_encrypt.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_aes_encrypt.c @@ -30,9 +30,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "mesh_aes_encrypt.h" #include "mesh_util.h" -#include "sdkconfig.h" +#include "mesh_aes_encrypt.h" /* max number of calls until change the key (2^48).*/ const static uint64_t MAX_CALLS = ((uint64_t)1 << 48); @@ -162,7 +161,7 @@ static inline void mult_row_column(uint8_t *out, const uint8_t *in) static inline void mix_columns(uint8_t *s) { - uint8_t t[Nb * Nk]; + uint8_t t[Nb * Nk] = {0}; mult_row_column(t, s); mult_row_column(&t[Nb], s + Nb); @@ -177,7 +176,7 @@ static inline void mix_columns(uint8_t *s) */ static inline void shift_rows(uint8_t *s) { - uint8_t t[Nb * Nk]; + uint8_t t[Nb * Nk] = {0}; t[0] = s[0]; t[1] = s[5]; t[2] = s[10]; t[3] = s[15]; t[4] = s[4]; t[5] = s[9]; t[6] = s[14]; t[7] = s[3]; @@ -188,7 +187,7 @@ static inline void shift_rows(uint8_t *s) int tc_aes_encrypt(uint8_t *out, const uint8_t *in, const TCAesKeySched_t s) { - uint8_t state[Nk * Nb]; + uint8_t state[Nk * Nb] = {0}; unsigned int i; if (out == (uint8_t *) 0) { @@ -364,7 +363,7 @@ int tc_cmac_update(TCCmacState_t s, const uint8_t *data, size_t data_length) int tc_cmac_final(uint8_t *tag, TCCmacState_t s) { - uint8_t *k; + uint8_t *k = NULL; unsigned int i; /* input sanity check: */ 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 6c2e940a1..5dce0b53b 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_atomic.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_atomic.c @@ -20,7 +20,6 @@ #include "mesh_atomic.h" #include "mesh_kernel.h" -#include "sdkconfig.h" #ifndef CONFIG_ATOMIC_OPERATIONS_BUILTIN @@ -55,7 +54,7 @@ bt_mesh_atomic_val_t bt_mesh_atomic_get(const bt_mesh_atomic_t *target) */ bt_mesh_atomic_val_t bt_mesh_atomic_set(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value) { - bt_mesh_atomic_val_t ret; + bt_mesh_atomic_val_t ret = 0; bt_mesh_atomic_lock(); @@ -82,7 +81,7 @@ bt_mesh_atomic_val_t bt_mesh_atomic_set(bt_mesh_atomic_t *target, bt_mesh_atomic */ bt_mesh_atomic_val_t bt_mesh_atomic_or(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value) { - bt_mesh_atomic_val_t ret; + bt_mesh_atomic_val_t ret = 0; bt_mesh_atomic_lock(); @@ -109,7 +108,7 @@ bt_mesh_atomic_val_t bt_mesh_atomic_or(bt_mesh_atomic_t *target, bt_mesh_atomic_ */ bt_mesh_atomic_val_t bt_mesh_atomic_and(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value) { - bt_mesh_atomic_val_t ret; + bt_mesh_atomic_val_t ret = 0; bt_mesh_atomic_lock(); @@ -134,7 +133,7 @@ bt_mesh_atomic_val_t bt_mesh_atomic_and(bt_mesh_atomic_t *target, bt_mesh_atomic */ bt_mesh_atomic_val_t bt_mesh_atomic_dec(bt_mesh_atomic_t *target) { - bt_mesh_atomic_val_t ret; + bt_mesh_atomic_val_t ret = 0; bt_mesh_atomic_lock(); @@ -159,7 +158,7 @@ bt_mesh_atomic_val_t bt_mesh_atomic_dec(bt_mesh_atomic_t *target) */ bt_mesh_atomic_val_t bt_mesh_atomic_inc(bt_mesh_atomic_t *target) { - bt_mesh_atomic_val_t ret; + bt_mesh_atomic_val_t ret = 0; bt_mesh_atomic_lock(); 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 2cb31d6d5..2fe8da1da 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c @@ -7,10 +7,9 @@ #include -#include "sdkconfig.h" - #include "mesh_buf.h" #include "mesh_trace.h" +#include "mesh_kernel.h" int net_buf_id(struct net_buf *buf) { @@ -22,7 +21,7 @@ int net_buf_id(struct net_buf *buf) static inline struct net_buf *pool_get_uninit(struct net_buf_pool *pool, u16_t uninit_count) { - struct net_buf *buf; + struct net_buf *buf = NULL; buf = &pool->__bufs[pool->buf_count - uninit_count]; @@ -59,7 +58,7 @@ void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem, u8_t *net_buf_simple_add_u8(struct net_buf_simple *buf, u8_t val) { - u8_t *u8; + u8_t *u8 = NULL; NET_BUF_SIMPLE_DBG("buf %p val 0x%02x", buf, val); @@ -161,7 +160,7 @@ void *net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len) u8_t net_buf_simple_pull_u8(struct net_buf_simple *buf) { - u8_t val; + u8_t val = 0U; val = buf->data[0]; net_buf_simple_pull(buf, 1); @@ -171,7 +170,7 @@ u8_t net_buf_simple_pull_u8(struct net_buf_simple *buf) u16_t net_buf_simple_pull_le16(struct net_buf_simple *buf) { - u16_t val; + u16_t val = 0U; val = UNALIGNED_GET((u16_t *)buf->data); net_buf_simple_pull(buf, sizeof(val)); @@ -181,7 +180,7 @@ u16_t net_buf_simple_pull_le16(struct net_buf_simple *buf) u16_t net_buf_simple_pull_be16(struct net_buf_simple *buf) { - u16_t val; + u16_t val = 0U; val = UNALIGNED_GET((u16_t *)buf->data); net_buf_simple_pull(buf, sizeof(val)); @@ -191,7 +190,7 @@ u16_t net_buf_simple_pull_be16(struct net_buf_simple *buf) u32_t net_buf_simple_pull_le32(struct net_buf_simple *buf) { - u32_t val; + u32_t val = 0U; val = UNALIGNED_GET((u32_t *)buf->data); net_buf_simple_pull(buf, sizeof(val)); @@ -201,7 +200,7 @@ u32_t net_buf_simple_pull_le32(struct net_buf_simple *buf) u32_t net_buf_simple_pull_be32(struct net_buf_simple *buf) { - u32_t val; + u32_t val = 0U; val = UNALIGNED_GET((u32_t *)buf->data); net_buf_simple_pull(buf, sizeof(val)); @@ -238,7 +237,7 @@ void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve) void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf) { - struct net_buf *tail; + struct net_buf *tail = NULL; NET_BUF_ASSERT(list); NET_BUF_ASSERT(buf); @@ -254,7 +253,7 @@ void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf) struct net_buf *net_buf_slist_get(sys_slist_t *list) { - struct net_buf *buf, *frag; + struct net_buf *buf = NULL, *frag = NULL; NET_BUF_ASSERT(list); @@ -304,7 +303,7 @@ void net_buf_unref(struct net_buf *buf) while (buf) { struct net_buf *frags = buf->frags; - struct net_buf_pool *pool; + struct net_buf_pool *pool = NULL; #if defined(CONFIG_BLE_MESH_NET_BUF_LOG) if (!buf->ref) { @@ -499,7 +498,7 @@ struct net_buf *net_buf_frag_del_debug(struct net_buf *parent, struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag) #endif { - struct net_buf *next_frag; + struct net_buf *next_frag = NULL; NET_BUF_ASSERT(frag); @@ -525,9 +524,9 @@ struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag) size_t net_buf_linearize(void *dst, size_t dst_len, struct net_buf *src, size_t offset, size_t len) { - struct net_buf *frag; - size_t to_copy; - size_t copied; + struct net_buf *frag = NULL; + size_t to_copy = 0U; + size_t copied = 0U; len = MIN(len, dst_len); @@ -567,7 +566,7 @@ size_t net_buf_append_bytes(struct net_buf *buf, size_t len, net_buf_allocator_cb allocate_cb, void *user_data) { struct net_buf *frag = net_buf_frag_last(buf); - size_t added_len = 0; + size_t added_len = 0U; const u8_t *value8 = value; do { 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 704e540a2..5aa4a1325 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_common.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_common.c @@ -15,6 +15,7 @@ #include #include +#include "mesh_main.h" #include "client_common.h" #include "mesh_common.h" @@ -23,7 +24,7 @@ struct net_buf_simple *bt_mesh_alloc_buf(u16_t size) struct net_buf_simple *buf = NULL; u8_t *data = NULL; - buf = (struct net_buf_simple *)osi_calloc(sizeof(struct net_buf_simple) + size); + buf = (struct net_buf_simple *)bt_mesh_calloc(sizeof(struct net_buf_simple) + size); if (!buf) { BT_ERR("%s, Failed to allocate memory", __func__); return NULL; @@ -42,7 +43,7 @@ struct net_buf_simple *bt_mesh_alloc_buf(u16_t size) void bt_mesh_free_buf(struct net_buf_simple *buf) { if (buf) { - osi_free(buf); + bt_mesh_free(buf); } } @@ -64,3 +65,62 @@ 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_kernel.c b/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c index 2e1803bdf..d4597d818 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c @@ -8,25 +8,17 @@ #include -#include "sdkconfig.h" - #include "osi/hash_map.h" #include "osi/alarm.h" #include "osi/hash_functions.h" -#include "bt_common.h" - -#include "esp_timer.h" - -#include "mesh_kernel.h" -#include "mesh_trace.h" - +#include "mesh_common.h" #include "provisioner_prov.h" -static osi_mutex_t bm_alarm_lock; -static osi_mutex_t bm_list_lock; -static osi_mutex_t bm_buf_lock; -static osi_mutex_t bm_atomic_lock; +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; @@ -39,34 +31,92 @@ 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) { - osi_mutex_lock(&bm_list_lock, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_mutex_lock(&bm_list_lock); } void bt_mesh_list_unlock(void) { - osi_mutex_unlock(&bm_list_lock); + 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) { - osi_mutex_lock(&bm_buf_lock, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_mutex_lock(&bm_buf_lock); } void bt_mesh_buf_unlock(void) { - osi_mutex_unlock(&bm_buf_lock); + 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) { - osi_mutex_lock(&bm_atomic_lock, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_mutex_lock(&bm_atomic_lock); } void bt_mesh_atomic_unlock(void) { - osi_mutex_unlock(&bm_atomic_lock); + bt_mesh_mutex_unlock(&bm_atomic_lock); } s64_t k_uptime_get(void) @@ -93,37 +143,52 @@ void k_sleep(s32_t duration) void bt_mesh_k_init(void) { - osi_mutex_new(&bm_alarm_lock); - osi_mutex_new(&bm_list_lock); - osi_mutex_new(&bm_buf_lock); - osi_mutex_new(&bm_atomic_lock); + 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 != NULL); + __ASSERT(bm_alarm_hash_map, "%s, Failed to create hash map", __func__); +} + +void bt_mesh_k_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; + } } void k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler) { osi_alarm_t *alarm = NULL; - assert(work != NULL && bm_alarm_hash_map != NULL); + if (!work || !bm_alarm_hash_map) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } k_work_init(&work->work, handler); - osi_mutex_lock(&bm_alarm_lock, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_alarm_lock(); if (!hash_map_has_key(bm_alarm_hash_map, (void *)work)) { alarm = osi_alarm_new("bt_mesh", (osi_alarm_callback_t)handler, (void *)&work->work, 0); if (alarm == NULL) { BT_ERR("%s, Unable to create alarm", __func__); - osi_mutex_unlock(&bm_alarm_lock); + bt_mesh_alarm_unlock(); return; } if (!hash_map_set(bm_alarm_hash_map, work, (void *)alarm)) { BT_ERR("%s Unable to add the timer to hash map.", __func__); } } - osi_mutex_unlock(&bm_alarm_lock); + bt_mesh_alarm_unlock(); alarm = hash_map_get(bm_alarm_hash_map, work); if (alarm == NULL) { @@ -138,7 +203,10 @@ void k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler) int k_delayed_work_submit(struct k_delayed_work *work, s32_t delay) { - assert(work != NULL && bm_alarm_hash_map != NULL); + if (!work || !bm_alarm_hash_map) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { @@ -154,7 +222,10 @@ int k_delayed_work_submit(struct k_delayed_work *work, s32_t delay) int k_delayed_work_submit_periodic(struct k_delayed_work *work, s32_t period) { - assert(work != NULL && bm_alarm_hash_map != NULL); + if (!work || !bm_alarm_hash_map) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { @@ -171,7 +242,10 @@ int k_delayed_work_submit_periodic(struct k_delayed_work *work, s32_t period) int k_delayed_work_cancel(struct k_delayed_work *work) { - assert(work != NULL && bm_alarm_hash_map != NULL); + if (!work || !bm_alarm_hash_map) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { @@ -186,7 +260,10 @@ int k_delayed_work_cancel(struct k_delayed_work *work) int k_delayed_work_free(struct k_delayed_work *work) { - assert(work != NULL && bm_alarm_hash_map != NULL); + if (!work || !bm_alarm_hash_map) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, work); if (alarm == NULL) { @@ -201,7 +278,10 @@ int k_delayed_work_free(struct k_delayed_work *work) s32_t k_delayed_work_remaining_get(struct k_delayed_work *work) { - assert(work != NULL && bm_alarm_hash_map != NULL); + if (!work || !bm_alarm_hash_map) { + BT_ERR("%s, Invalid parameter", __func__); + return 0; + } osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_util.c b/components/bt/esp_ble_mesh/mesh_common/mesh_util.c index 232662baa..83949594b 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_util.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_util.c @@ -7,9 +7,9 @@ */ #include -#include "sdkconfig.h" + +#include "mesh_types.h" #include "mesh_util.h" -#include "mesh_kernel.h" #include "mesh_aes_encrypt.h" #define MASK_TWENTY_SEVEN 0x1b diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c index 65b0f9129..8438bc736 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.c +++ b/components/bt/esp_ble_mesh/mesh_core/access.c @@ -10,23 +10,14 @@ #include #include -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_ACCESS) -#include "mesh_util.h" -#include "mesh_buf.h" -#include "mesh_trace.h" -#include "mesh_kernel.h" -#include "mesh_access.h" -#include "mesh_main.h" - #include "mesh.h" #include "adv.h" -#include "net.h" -#include "lpn.h" #include "transport.h" #include "access.h" #include "foundation.h" +#include "mesh_main.h" #include "mesh_common.h" #include "provisioner_main.h" @@ -49,64 +40,64 @@ static const struct { const u16_t id; int (*const init)(struct bt_mesh_model *model, bool primary); } model_init[] = { - { BLE_MESH_MODEL_ID_CFG_SRV, bt_mesh_cfg_srv_init }, - { BLE_MESH_MODEL_ID_HEALTH_SRV, bt_mesh_health_srv_init }, + { BLE_MESH_MODEL_ID_CFG_SRV, bt_mesh_cfg_srv_init }, + { BLE_MESH_MODEL_ID_HEALTH_SRV, bt_mesh_health_srv_init }, #if defined(CONFIG_BLE_MESH_CFG_CLI) - { BLE_MESH_MODEL_ID_CFG_CLI, bt_mesh_cfg_cli_init }, + { BLE_MESH_MODEL_ID_CFG_CLI, bt_mesh_cfg_cli_init }, #endif #if defined(CONFIG_BLE_MESH_HEALTH_CLI) - { BLE_MESH_MODEL_ID_HEALTH_CLI, bt_mesh_health_cli_init }, + { BLE_MESH_MODEL_ID_HEALTH_CLI, bt_mesh_health_cli_init }, #endif #if defined(CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) - { BLE_MESH_MODEL_ID_GEN_ONOFF_CLI, bt_mesh_gen_onoff_cli_init }, + { BLE_MESH_MODEL_ID_GEN_ONOFF_CLI, bt_mesh_gen_onoff_cli_init }, #endif #if defined(CONFIG_BLE_MESH_GENERIC_LEVEL_CLI) - { BLE_MESH_MODEL_ID_GEN_LEVEL_CLI, bt_mesh_gen_level_cli_init }, + { BLE_MESH_MODEL_ID_GEN_LEVEL_CLI, bt_mesh_gen_level_cli_init }, #endif #if defined(CONFIG_BLE_MESH_GENERIC_DEF_TRANS_TIME_CLI) - { BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI, bt_mesh_gen_def_trans_time_cli_init }, + { BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI, bt_mesh_gen_def_trans_time_cli_init }, #endif #if defined(CONFIG_BLE_MESH_GENERIC_POWER_ONOFF_CLI) - { BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI, bt_mesh_gen_pwr_onoff_cli_init }, + { BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI, bt_mesh_gen_pwr_onoff_cli_init }, #endif #if defined(CONFIG_BLE_MESH_GENERIC_POWER_LEVEL_CLI) - { BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI, bt_mesh_gen_pwr_level_cli_init }, + { BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI, bt_mesh_gen_pwr_level_cli_init }, #endif #if defined(CONFIG_BLE_MESH_GENERIC_BATTERY_CLI) - { BLE_MESH_MODEL_ID_GEN_BATTERY_CLI, bt_mesh_gen_battery_cli_init }, + { BLE_MESH_MODEL_ID_GEN_BATTERY_CLI, bt_mesh_gen_battery_cli_init }, #endif #if defined(CONFIG_BLE_MESH_GENERIC_LOCATION_CLI) - { BLE_MESH_MODEL_ID_GEN_LOCATION_CLI, bt_mesh_gen_location_cli_init }, + { BLE_MESH_MODEL_ID_GEN_LOCATION_CLI, bt_mesh_gen_location_cli_init }, #endif #if defined(CONFIG_BLE_MESH_GENERIC_PROPERTY_CLI) - { BLE_MESH_MODEL_ID_GEN_PROP_CLI, bt_mesh_gen_property_cli_init }, + { BLE_MESH_MODEL_ID_GEN_PROP_CLI, bt_mesh_gen_property_cli_init }, #endif #if defined(CONFIG_BLE_MESH_SENSOR_CLI) - { BLE_MESH_MODEL_ID_SENSOR_CLI, bt_mesh_sensor_cli_init }, + { BLE_MESH_MODEL_ID_SENSOR_CLI, bt_mesh_sensor_cli_init }, #endif #if defined(CONFIG_BLE_MESH_TIME_CLI) - { BLE_MESH_MODEL_ID_TIME_CLI, bt_mesh_time_cli_init }, + { BLE_MESH_MODEL_ID_TIME_CLI, bt_mesh_time_cli_init }, #endif #if defined(CONFIG_BLE_MESH_SCENE_CLI) - { BLE_MESH_MODEL_ID_SCENE_CLI, bt_mesh_scene_cli_init }, + { BLE_MESH_MODEL_ID_SCENE_CLI, bt_mesh_scene_cli_init }, #endif #if defined(CONFIG_BLE_MESH_SCHEDULER_CLI) - { BLE_MESH_MODEL_ID_SCHEDULER_CLI, bt_mesh_scheduler_cli_init }, + { BLE_MESH_MODEL_ID_SCHEDULER_CLI, bt_mesh_scheduler_cli_init }, #endif #if defined(CONFIG_BLE_MESH_LIGHT_LIGHTNESS_CLI) - { BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI, bt_mesh_light_lightness_cli_init }, + { BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI, bt_mesh_light_lightness_cli_init }, #endif #if defined(CONFIG_BLE_MESH_LIGHT_CTL_CLI) - { BLE_MESH_MODEL_ID_LIGHT_CTL_CLI, bt_mesh_light_ctl_cli_init }, + { BLE_MESH_MODEL_ID_LIGHT_CTL_CLI, bt_mesh_light_ctl_cli_init }, #endif #if defined(CONFIG_BLE_MESH_LIGHT_HSL_CLI) - { BLE_MESH_MODEL_ID_LIGHT_HSL_CLI, bt_mesh_light_hsl_cli_init }, + { BLE_MESH_MODEL_ID_LIGHT_HSL_CLI, bt_mesh_light_hsl_cli_init }, #endif #if defined(CONFIG_BLE_MESH_LIGHT_XYL_CLI) - { BLE_MESH_MODEL_ID_LIGHT_XYL_CLI, bt_mesh_light_xyl_cli_init }, + { BLE_MESH_MODEL_ID_LIGHT_XYL_CLI, bt_mesh_light_xyl_cli_init }, #endif #if defined(CONFIG_BLE_MESH_LIGHT_LC_CLI) - { BLE_MESH_MODEL_ID_LIGHT_LC_CLI, bt_mesh_light_lc_cli_init }, + { BLE_MESH_MODEL_ID_LIGHT_LC_CLI, bt_mesh_light_lc_cli_init }, #endif { BLE_MESH_MODEL_ID_GEN_ONOFF_SRV, bt_mesh_gen_onoff_srv_init }, { BLE_MESH_MODEL_ID_GEN_LEVEL_SRV, bt_mesh_gen_level_srv_init }, @@ -145,6 +136,106 @@ static const struct { { BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV, bt_mesh_sensor_setup_srv_init }, }; +static const struct { + const u16_t id; + int (*const deinit)(struct bt_mesh_model *model, bool primary); +} model_deinit[] = { + { BLE_MESH_MODEL_ID_CFG_SRV, bt_mesh_cfg_srv_deinit }, + { BLE_MESH_MODEL_ID_HEALTH_SRV, bt_mesh_health_srv_deinit }, +#if defined(CONFIG_BLE_MESH_CFG_CLI) + { BLE_MESH_MODEL_ID_CFG_CLI, bt_mesh_cfg_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_HEALTH_CLI) + { BLE_MESH_MODEL_ID_HEALTH_CLI, bt_mesh_health_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) + { BLE_MESH_MODEL_ID_GEN_ONOFF_CLI, bt_mesh_gen_onoff_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_LEVEL_CLI) + { BLE_MESH_MODEL_ID_GEN_LEVEL_CLI, bt_mesh_gen_level_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_DEF_TRANS_TIME_CLI) + { BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI, bt_mesh_gen_def_trans_time_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_POWER_ONOFF_CLI) + { BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI, bt_mesh_gen_pwr_onoff_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_POWER_LEVEL_CLI) + { BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI, bt_mesh_gen_pwr_level_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_BATTERY_CLI) + { BLE_MESH_MODEL_ID_GEN_BATTERY_CLI, bt_mesh_gen_battery_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_LOCATION_CLI) + { BLE_MESH_MODEL_ID_GEN_LOCATION_CLI, bt_mesh_gen_location_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_PROPERTY_CLI) + { BLE_MESH_MODEL_ID_GEN_PROP_CLI, bt_mesh_gen_property_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_SENSOR_CLI) + { BLE_MESH_MODEL_ID_SENSOR_CLI, bt_mesh_sensor_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_TIME_CLI) + { BLE_MESH_MODEL_ID_TIME_CLI, bt_mesh_time_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_SCENE_CLI) + { BLE_MESH_MODEL_ID_SCENE_CLI, bt_mesh_scene_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_SCHEDULER_CLI) + { BLE_MESH_MODEL_ID_SCHEDULER_CLI, bt_mesh_scheduler_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_LIGHTNESS_CLI) + { BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI, bt_mesh_light_lightness_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_CTL_CLI) + { BLE_MESH_MODEL_ID_LIGHT_CTL_CLI, bt_mesh_light_ctl_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_HSL_CLI) + { BLE_MESH_MODEL_ID_LIGHT_HSL_CLI, bt_mesh_light_hsl_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_XYL_CLI) + { BLE_MESH_MODEL_ID_LIGHT_XYL_CLI, bt_mesh_light_xyl_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_LC_CLI) + { BLE_MESH_MODEL_ID_LIGHT_LC_CLI, bt_mesh_light_lc_cli_deinit }, +#endif + { BLE_MESH_MODEL_ID_GEN_ONOFF_SRV, bt_mesh_gen_onoff_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_LEVEL_SRV, bt_mesh_gen_level_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV, bt_mesh_gen_def_trans_time_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV, bt_mesh_gen_power_onoff_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV, bt_mesh_gen_power_onoff_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV, bt_mesh_gen_power_level_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV, bt_mesh_gen_power_level_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_BATTERY_SRV, bt_mesh_gen_battery_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_LOCATION_SRV, bt_mesh_gen_location_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV, bt_mesh_gen_location_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV, bt_mesh_gen_user_prop_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV, bt_mesh_gen_admin_prop_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV, bt_mesh_gen_manu_prop_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV, bt_mesh_gen_client_prop_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV, bt_mesh_light_lightness_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV, bt_mesh_light_lightness_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_CTL_SRV, bt_mesh_light_ctl_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV, bt_mesh_light_ctl_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV, bt_mesh_light_ctl_temp_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_HSL_SRV, bt_mesh_light_hsl_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV, bt_mesh_light_hsl_hue_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV, bt_mesh_light_hsl_sat_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV, bt_mesh_light_hsl_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_XYL_SRV, bt_mesh_light_xyl_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV, bt_mesh_light_xyl_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_LC_SRV, bt_mesh_light_lc_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV, bt_mesh_light_lc_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_TIME_SRV, bt_mesh_time_srv_deinit }, + { BLE_MESH_MODEL_ID_TIME_SETUP_SRV, bt_mesh_time_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_SCENE_SRV, bt_mesh_scene_srv_deinit }, + { BLE_MESH_MODEL_ID_SCENE_SETUP_SRV, bt_mesh_scene_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_SCHEDULER_SRV, bt_mesh_scheduler_srv_deinit }, + { BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV, bt_mesh_scheduler_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_SENSOR_SRV, bt_mesh_sensor_srv_deinit }, + { BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV, bt_mesh_sensor_setup_srv_deinit }, +}; + void bt_mesh_model_foreach(void (*func)(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, bool vnd, bool primary, @@ -211,7 +302,7 @@ s32_t bt_mesh_model_pub_period_get(struct bt_mesh_model *mod) static s32_t next_period(struct bt_mesh_model *mod) { struct bt_mesh_model_pub *pub = mod->pub; - u32_t elapsed, period; + u32_t elapsed = 0U, period = 0U; if (!pub) { BT_ERR("%s, Model has no publication support", __func__); @@ -225,7 +316,7 @@ static s32_t next_period(struct bt_mesh_model *mod) elapsed = k_uptime_get_32() - pub->period_start; - BT_DBG("Publishing took %ums", elapsed); + BT_INFO("Publishing took %ums", elapsed); if (elapsed >= period) { BT_WARN("Publication sending took longer than the period"); @@ -239,7 +330,7 @@ static s32_t next_period(struct bt_mesh_model *mod) static void publish_sent(int err, void *user_data) { struct bt_mesh_model *mod = user_data; - s32_t delay; + s32_t delay = 0; BT_DBG("err %d", err); @@ -255,7 +346,7 @@ static void publish_sent(int err, void *user_data) } if (delay) { - BT_DBG("Publishing next time in %dms", delay); + BT_INFO("Publishing next time in %dms", delay); k_delayed_work_submit(&mod->pub->timer, delay); } } @@ -303,7 +394,7 @@ static int publish_retransmit(struct bt_mesh_model *mod) .xmit = bt_mesh_net_transmit_get(), .friend_cred = pub->cred, }; - int err; + int err = 0; key = bt_mesh_app_key_find(pub->key); if (!key) { @@ -337,13 +428,13 @@ static void mod_publish(struct k_work *work) struct bt_mesh_model_pub *pub = CONTAINER_OF(work, struct bt_mesh_model_pub, timer.work); - s32_t period_ms; - int err; + s32_t period_ms = 0; + int err = 0; BT_DBG("%s", __func__); period_ms = bt_mesh_model_pub_period_get(pub->mod); - BT_DBG("period %u ms", period_ms); + BT_INFO("period %u ms", period_ms); if (pub->count) { err = publish_retransmit(pub->mod); @@ -390,7 +481,7 @@ struct bt_mesh_elem *bt_mesh_model_elem(struct bt_mesh_model *mod) struct bt_mesh_model *bt_mesh_model_get(bool vnd, u8_t elem_idx, u8_t mod_idx) { - struct bt_mesh_elem *elem; + struct bt_mesh_elem *elem = NULL; if (!dev_comp) { BT_ERR("%s, dev_comp is not initialized", __func__); @@ -456,6 +547,37 @@ static void mod_init(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, } } +static void mod_deinit(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, + bool vnd, bool primary, void *user_data) +{ + int i; + + mod->elem = NULL; + + if (mod->pub) { + mod->pub->mod = NULL; + k_delayed_work_free(&mod->pub->timer); + } + + for (i = 0; i < ARRAY_SIZE(mod->keys); i++) { + mod->keys[i] = BLE_MESH_KEY_UNUSED; + } + + mod->flags = 0U; + mod->elem_idx = 0U; + mod->model_idx = 0U; + + if (vnd) { + return; + } + + for (i = 0; i < ARRAY_SIZE(model_deinit); i++) { + if (model_deinit[i].id == mod->id) { + model_deinit[i].deinit(mod, primary); + } + } +} + int bt_mesh_comp_register(const struct bt_mesh_comp *comp) { /* There must be at least one element */ @@ -470,13 +592,27 @@ int bt_mesh_comp_register(const struct bt_mesh_comp *comp) return 0; } +int bt_mesh_comp_deregister(void) +{ + if (dev_comp == NULL) { + return -EINVAL; + } + + bt_mesh_model_foreach(mod_deinit, NULL); + + dev_primary_addr = BLE_MESH_ADDR_UNASSIGNED; + dev_comp = NULL; + + return 0; +} + void bt_mesh_comp_provision(u16_t addr) { int i; dev_primary_addr = addr; - BT_DBG("addr 0x%04x elem_count %u", addr, dev_comp->elem_count); + BT_INFO("addr 0x%04x elem_count %u", addr, dev_comp->elem_count); for (i = 0; i < dev_comp->elem_count; i++) { struct bt_mesh_elem *elem = &dev_comp->elem[i]; @@ -518,8 +654,8 @@ u16_t *bt_mesh_model_find_group(struct bt_mesh_model *mod, u16_t addr) static struct bt_mesh_model *bt_mesh_elem_find_group(struct bt_mesh_elem *elem, u16_t group_addr) { - struct bt_mesh_model *model; - u16_t *match; + struct bt_mesh_model *model = NULL; + u16_t *match = NULL; int i; for (i = 0; i < elem->model_count; i++) { @@ -545,7 +681,7 @@ static struct bt_mesh_model *bt_mesh_elem_find_group(struct bt_mesh_elem *elem, struct bt_mesh_elem *bt_mesh_elem_find(u16_t addr) { - u16_t index; + u16_t index = 0U; if (BLE_MESH_ADDR_IS_UNICAST(addr)) { index = (addr - dev_comp->elem[0].addr); @@ -590,9 +726,9 @@ static const struct bt_mesh_model_op *find_op(struct bt_mesh_model *models, u16_t app_idx, u32_t opcode, struct bt_mesh_model **model) { - u8_t i; + int i; - for (i = 0U; i < model_count; i++) { + for (i = 0; i < model_count; i++) { const struct bt_mesh_model_op *op; *model = &models[i]; @@ -672,22 +808,22 @@ bool bt_mesh_fixed_group_match(u16_t addr) void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) { - struct bt_mesh_model *models, *model; - const struct bt_mesh_model_op *op; - u32_t opcode; - u8_t count; + struct bt_mesh_model *models = NULL, *model = NULL; + const struct bt_mesh_model_op *op = NULL; + u32_t opcode = 0U; + u8_t count = 0U; int i; - BT_DBG("app_idx 0x%04x src 0x%04x dst 0x%04x", rx->ctx.app_idx, + BT_INFO("app_idx 0x%04x src 0x%04x dst 0x%04x", rx->ctx.app_idx, rx->ctx.addr, rx->ctx.recv_dst); - BT_DBG("len %u: %s", buf->len, bt_hex(buf->data, buf->len)); + BT_INFO("len %u: %s", buf->len, bt_hex(buf->data, buf->len)); if (get_opcode(buf, &opcode) < 0) { BT_WARN("%s, Unable to decode OpCode", __func__); return; } - BT_DBG("OpCode 0x%08x", opcode); + BT_INFO("OpCode 0x%08x", opcode); for (i = 0; i < dev_comp->elem_count; i++) { struct bt_mesh_elem *elem = &dev_comp->elem[i]; @@ -780,48 +916,17 @@ void bt_mesh_model_msg_init(struct net_buf_simple *msg, u32_t opcode) static bool ready_to_send(u8_t role, u16_t dst) { -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (!bt_mesh_is_provisioned()) { - BT_ERR("%s, Local node is not yet provisioned", __func__); - return false; - } - if (!bt_mesh_is_provisioner_en()) { - return true; - } - } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (!provisioner_check_msg_dst_addr(dst)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() && role == NODE) { + return true; + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en() && role == PROVISIONER) { + if (!bt_mesh_provisioner_check_msg_dst(dst)) { BT_ERR("%s, Failed to find DST 0x%04x", __func__, dst); return false; } - if (bt_mesh_is_provisioner_en()) { - return true; - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (!provisioner_check_msg_dst_addr(dst)) { - BT_ERR("%s, Failed to check DST", __func__); - return false; - } - if (bt_mesh_is_provisioner_en()) { - return true; - } - } else { - if (!bt_mesh_is_provisioned()) { - BT_ERR("%s, Local node is not yet provisioned", __func__); - return false; - } - + return true; + } else if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && bt_mesh_is_provisioned() && role == FAST_PROV) { return true; } -#endif return false; } @@ -831,7 +936,7 @@ static int model_send(struct bt_mesh_model *model, struct net_buf_simple *msg, const struct bt_mesh_send_cb *cb, void *cb_data) { - u8_t role; + u8_t role = 0U; role = bt_mesh_get_device_role(model, tx->ctx->srv_send); if (role == ROLE_NVAL) { @@ -839,9 +944,9 @@ static int model_send(struct bt_mesh_model *model, return -EINVAL; } - BT_DBG("net_idx 0x%04x app_idx 0x%04x dst 0x%04x", tx->ctx->net_idx, + BT_INFO("net_idx 0x%04x app_idx 0x%04x dst 0x%04x", tx->ctx->net_idx, tx->ctx->app_idx, tx->ctx->addr); - BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); + BT_INFO("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); if (!ready_to_send(role, tx->ctx->addr)) { BT_ERR("%s, fail", __func__); @@ -872,7 +977,7 @@ int bt_mesh_model_send(struct bt_mesh_model *model, const struct bt_mesh_send_cb *cb, void *cb_data) { struct bt_mesh_subnet *sub = NULL; - u8_t role; + u8_t role = 0U; role = bt_mesh_get_device_role(model, ctx->srv_send); if (role == ROLE_NVAL) { @@ -911,7 +1016,7 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) .src = bt_mesh_model_elem(model)->addr, .xmit = bt_mesh_net_transmit_get(), }; - int err; + int err = 0; BT_DBG("%s", __func__); @@ -925,7 +1030,7 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) return -EADDRNOTAVAIL; } - key = bt_mesh_tx_appkey_get(pub->dev_role, pub->key, BLE_MESH_KEY_ANY); + key = bt_mesh_tx_appkey_get(pub->dev_role, pub->key); if (!key) { BT_ERR("%s, Failed to get AppKey", __func__); return -EADDRNOTAVAIL; @@ -957,7 +1062,7 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) pub->count = BLE_MESH_PUB_TRANSMIT_COUNT(pub->retransmit); - BT_DBG("Publish Retransmit Count %u Interval %ums", pub->count, + BT_INFO("Publish Retransmit Count %u Interval %ums", pub->count, BLE_MESH_PUB_TRANSMIT_INT(pub->retransmit)); sdu = bt_mesh_alloc_buf(pub->msg->len + 4); @@ -983,9 +1088,9 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) struct bt_mesh_model *bt_mesh_model_find_vnd(struct bt_mesh_elem *elem, u16_t company, u16_t id) { - u8_t i; + int i; - for (i = 0U; i < elem->vnd_model_count; i++) { + for (i = 0; i < elem->vnd_model_count; i++) { if (elem->vnd_models[i].vnd.company == company && elem->vnd_models[i].vnd.id == id) { return &elem->vnd_models[i]; @@ -998,9 +1103,9 @@ struct bt_mesh_model *bt_mesh_model_find_vnd(struct bt_mesh_elem *elem, struct bt_mesh_model *bt_mesh_model_find(struct bt_mesh_elem *elem, u16_t id) { - u8_t i; + int i; - for (i = 0U; i < elem->model_count; i++) { + for (i = 0; i < elem->model_count; i++) { if (elem->models[i].id == id) { return &elem->models[i]; } @@ -1019,37 +1124,13 @@ struct bt_mesh_subnet *bt_mesh_tx_netkey_get(u8_t role, u16_t net_idx) { struct bt_mesh_subnet *sub = NULL; -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (bt_mesh_is_provisioned()) { - sub = bt_mesh_subnet_get(net_idx); - } + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() && role == NODE) { + sub = bt_mesh_subnet_get(net_idx); + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en() && role == PROVISIONER) { + sub = bt_mesh_provisioner_subnet_get(net_idx); + } else if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && bt_mesh_is_provisioned() && role == FAST_PROV) { + sub = bt_mesh_fast_prov_subnet_get(net_idx); } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - sub = provisioner_subnet_get(net_idx); - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (bt_mesh_is_provisioned()) { - sub = bt_mesh_subnet_get(net_idx); - } - } else if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - sub = provisioner_subnet_get(net_idx); - } - } else if (role == FAST_PROV) { -#if CONFIG_BLE_MESH_FAST_PROV - sub = fast_prov_subnet_get(net_idx); -#endif - } -#endif return sub; } @@ -1058,76 +1139,28 @@ const u8_t *bt_mesh_tx_devkey_get(u8_t role, u16_t dst) { const u8_t *key = NULL; -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (bt_mesh_is_provisioned()) { - key = bt_mesh.dev_key; - } + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() && role == NODE) { + key = bt_mesh.dev_key; + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en() && role == PROVISIONER) { + key = bt_mesh_provisioner_dev_key_get(dst); + } else if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && bt_mesh_is_provisioned() && role == FAST_PROV) { + key = bt_mesh_fast_prov_dev_key_get(dst); } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - key = provisioner_dev_key_get(dst); - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (bt_mesh_is_provisioned()) { - key = bt_mesh.dev_key; - } - } else if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - key = provisioner_dev_key_get(dst); - } - } else if (role == FAST_PROV) { -#if CONFIG_BLE_MESH_FAST_PROV - key = fast_prov_dev_key_get(dst); -#endif - } -#endif return key; } -struct bt_mesh_app_key *bt_mesh_tx_appkey_get(u8_t role, u16_t app_idx, u16_t net_idx) +struct bt_mesh_app_key *bt_mesh_tx_appkey_get(u8_t role, u16_t app_idx) { struct bt_mesh_app_key *key = NULL; -#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (bt_mesh_is_provisioned()) { - key = bt_mesh_app_key_find(app_idx); - } + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() && role == NODE) { + key = bt_mesh_app_key_find(app_idx); + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en() && role == PROVISIONER) { + key = bt_mesh_provisioner_app_key_find(app_idx); + } else if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && bt_mesh_is_provisioned() && role == FAST_PROV) { + key = bt_mesh_fast_prov_app_key_find(app_idx); } -#endif - -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - key = provisioner_app_key_find(app_idx); - } - } -#endif - -#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (role == NODE) { - if (bt_mesh_is_provisioned()) { - key = bt_mesh_app_key_find(app_idx); - } - } else if (role == PROVISIONER) { - if (bt_mesh_is_provisioner_en()) { - key = provisioner_app_key_find(app_idx); - } - } else if (role == FAST_PROV) { -#if CONFIG_BLE_MESH_FAST_PROV - key = fast_prov_app_key_find(net_idx, app_idx); -#endif - } -#endif return key; } @@ -1135,7 +1168,7 @@ struct bt_mesh_app_key *bt_mesh_tx_appkey_get(u8_t role, u16_t app_idx, u16_t ne /* APIs used by messages decryption in network layer & upper transport layer */ size_t bt_mesh_rx_netkey_size(void) { - size_t size = 0; + size_t size = 0U; #if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER if (bt_mesh_is_provisioned()) { @@ -1188,10 +1221,10 @@ struct bt_mesh_subnet *bt_mesh_rx_netkey_get(size_t index) size_t bt_mesh_rx_devkey_size(void) { - size_t size = 0; + size_t size = 0U; #if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { + if (bt_mesh_is_provisioned()) { size = 1; } #endif @@ -1224,7 +1257,7 @@ const u8_t *bt_mesh_rx_devkey_get(size_t index, u16_t src) #if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER if (bt_mesh_is_provisioner_en()) { - key = provisioner_dev_key_get(src); + key = bt_mesh_provisioner_dev_key_get(src); } #endif @@ -1232,7 +1265,7 @@ const u8_t *bt_mesh_rx_devkey_get(size_t index, u16_t src) if (index < 1) { key = bt_mesh.dev_key; } else { - key = provisioner_dev_key_get(src); + key = bt_mesh_provisioner_dev_key_get(src); } #endif @@ -1241,7 +1274,7 @@ const u8_t *bt_mesh_rx_devkey_get(size_t index, u16_t src) size_t bt_mesh_rx_appkey_size(void) { - size_t size = 0; + size_t size = 0U; #if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER if (bt_mesh_is_provisioned()) { diff --git a/components/bt/esp_ble_mesh/mesh_core/access.h b/components/bt/esp_ble_mesh/mesh_core/access.h index 119ad2362..944548c56 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.h +++ b/components/bt/esp_ble_mesh/mesh_core/access.h @@ -9,8 +9,6 @@ #ifndef _ACCESS_H_ #define _ACCESS_H_ -#include "mesh_access.h" -#include "mesh_buf.h" #include "net.h" /* bt_mesh_model.flags */ @@ -56,12 +54,13 @@ struct bt_mesh_model *bt_mesh_model_get(bool vnd, u8_t elem_idx, u8_t mod_idx); void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf); int bt_mesh_comp_register(const struct bt_mesh_comp *comp); +int bt_mesh_comp_deregister(void); struct bt_mesh_subnet *bt_mesh_tx_netkey_get(u8_t role, u16_t net_idx); const u8_t *bt_mesh_tx_devkey_get(u8_t role, u16_t dst); -struct bt_mesh_app_key *bt_mesh_tx_appkey_get(u8_t role, u16_t app_idx, u16_t net_idx); +struct bt_mesh_app_key *bt_mesh_tx_appkey_get(u8_t role, u16_t app_idx); size_t bt_mesh_rx_netkey_size(void); diff --git a/components/bt/esp_ble_mesh/mesh_core/adv.c b/components/bt/esp_ble_mesh/mesh_core/adv.c index e61eb259b..ea3672e3c 100644 --- a/components/bt/esp_ble_mesh/mesh_core/adv.c +++ b/components/bt/esp_ble_mesh/mesh_core/adv.c @@ -8,6 +8,7 @@ */ #include +#include #include #include "freertos/FreeRTOS.h" @@ -15,26 +16,19 @@ #include "freertos/task.h" #include "osi/thread.h" -#include "sdkconfig.h" + #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_ADV) -#include "mesh_util.h" -#include "mesh_buf.h" -#include "mesh_bearer_adapt.h" -#include "mesh_trace.h" -#include "mesh_hci.h" - #include "mesh.h" +#include "mesh_hci.h" #include "adv.h" -#include "net.h" -#include "foundation.h" #include "beacon.h" #include "prov.h" +#include "foundation.h" #include "proxy_server.h" - -#include "provisioner_prov.h" #include "proxy_client.h" -#include "provisioner_beacon.h" +#include "provisioner_prov.h" +#include "mesh_bearer_adapt.h" /* Convert from ms to 0.625ms units */ #define ADV_SCAN_UNIT(_ms) ((_ms) * 8 / 5) @@ -64,13 +58,22 @@ static const u8_t adv_type[] = { [BLE_MESH_ADV_URI] = BLE_MESH_DATA_URI, }; -NET_BUF_POOL_DEFINE(adv_buf_pool, CONFIG_BLE_MESH_ADV_BUF_COUNT + 3 * CONFIG_BLE_MESH_PBA_SAME_TIME, +NET_BUF_POOL_DEFINE(adv_buf_pool, CONFIG_BLE_MESH_ADV_BUF_COUNT, BLE_MESH_ADV_DATA_SIZE, BLE_MESH_ADV_USER_DATA_SIZE, NULL); -static struct bt_mesh_adv adv_pool[CONFIG_BLE_MESH_ADV_BUF_COUNT + 3 * CONFIG_BLE_MESH_PBA_SAME_TIME]; +static struct bt_mesh_adv adv_pool[CONFIG_BLE_MESH_ADV_BUF_COUNT]; -static QueueHandle_t xBleMeshQueue; -#define BLE_MESH_QUEUE_SIZE 150 +struct bt_mesh_queue { + QueueHandle_t queue; +#if CONFIG_SPIRAM_USE_MALLOC + StaticQueue_t *buffer; + u8_t *storage; +#endif +}; + +static struct bt_mesh_queue xBleMeshQueue; +/* We reserve one queue for bt_mesh_adv_update() */ +#define BLE_MESH_QUEUE_SIZE (CONFIG_BLE_MESH_ADV_BUF_COUNT + 1) #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) NET_BUF_POOL_DEFINE(relay_adv_buf_pool, CONFIG_BLE_MESH_RELAY_ADV_BUF_COUNT, @@ -78,8 +81,8 @@ 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 QueueHandle_t xBleMeshRelayQueue; -#define BLE_MESH_RELAY_QUEUE_SIZE 150 +static struct bt_mesh_queue xBleMeshRelayQueue; +#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) @@ -90,6 +93,16 @@ static QueueSetHandle_t xBleMeshQueueSet; static bool ignore_relay_packet(u32_t timestamp); #endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ +struct bt_mesh_adv_task { + TaskHandle_t handle; +#if CONFIG_SPIRAM_USE_MALLOC + StaticTask_t *task; + StackType_t *stack; +#endif +}; + +static struct bt_mesh_adv_task adv_task; + static struct bt_mesh_adv *adv_alloc(int id) { return &adv_pool[id]; @@ -119,9 +132,9 @@ static inline int adv_send(struct net_buf *buf) const struct bt_mesh_send_cb *cb = BLE_MESH_ADV(buf)->cb; void *cb_data = BLE_MESH_ADV(buf)->cb_data; struct bt_mesh_adv_param param = {0}; - u16_t duration, adv_int; + u16_t duration = 0U, adv_int = 0U; struct bt_mesh_adv_data ad = {0}; - int err; + int err = 0; adv_int = MAX(adv_int_min, BLE_MESH_TRANSMIT_INT(BLE_MESH_ADV(buf)->xmit)); @@ -142,7 +155,7 @@ static inline int adv_send(struct net_buf *buf) param.interval_min = ADV_SCAN_UNIT(adv_int); param.interval_max = param.interval_min; - bt_mesh_adv_buf_ref_debug(__func__, buf, 3U, BLE_MESH_BUF_REF_SMALL); + bt_mesh_adv_buf_ref_debug(__func__, buf, 4U, BLE_MESH_BUF_REF_SMALL); err = bt_le_adv_start(¶m, &ad, 1, NULL, 0); net_buf_unref(buf); @@ -170,10 +183,10 @@ static inline int adv_send(struct net_buf *buf) static void adv_thread(void *p) { #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) - QueueSetMemberHandle_t handle; + QueueSetMemberHandle_t handle = NULL; #endif bt_mesh_msg_t msg = {0}; - struct net_buf **buf; + struct net_buf **buf = NULL; buf = (struct net_buf **)(&msg.arg); @@ -182,72 +195,59 @@ static void adv_thread(void *p) while (1) { *buf = NULL; #if !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) -#if CONFIG_BLE_MESH_NODE - if (IS_ENABLED(CONFIG_BLE_MESH_PROXY)) { - xQueueReceive(xBleMeshQueue, &msg, K_NO_WAIT); +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER + xQueueReceive(xBleMeshQueue.queue, &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); + BT_DBG("Mesh Proxy Advertising stop"); + bt_mesh_proxy_adv_stop(); + } +#else + xQueueReceive(xBleMeshQueue.queue, &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); + 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); + } + } else { while (!(*buf)) { - s32_t timeout; + s32_t timeout = 0; BT_DBG("Mesh Proxy Advertising start"); timeout = bt_mesh_proxy_adv_start(); BT_DBG("Mesh Proxy Advertising up to %d ms", timeout); - xQueueReceive(xBleMeshQueue, &msg, timeout); + handle = xQueueSelectFromSet(xBleMeshQueueSet, timeout); BT_DBG("Mesh Proxy Advertising stop"); bt_mesh_proxy_adv_stop(); - } - } else { - xQueueReceive(xBleMeshQueue, &msg, portMAX_DELAY); - } -#else - xQueueReceive(xBleMeshQueue, &msg, portMAX_DELAY); -#endif -#else /* !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ -#if CONFIG_BLE_MESH_NODE - if (IS_ENABLED(CONFIG_BLE_MESH_PROXY)) { - handle = xQueueSelectFromSet(xBleMeshQueueSet, K_NO_WAIT); - if (handle) { - if (uxQueueMessagesWaiting(xBleMeshQueue)) { - xQueueReceive(xBleMeshQueue, &msg, K_NO_WAIT); - } else if (uxQueueMessagesWaiting(xBleMeshRelayQueue)) { - xQueueReceive(xBleMeshRelayQueue, &msg, K_NO_WAIT); - } - } else { - 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); - handle = xQueueSelectFromSet(xBleMeshQueueSet, timeout); - BT_DBG("Mesh Proxy Advertising stop"); - bt_mesh_proxy_adv_stop(); - if (handle) { - if (uxQueueMessagesWaiting(xBleMeshQueue)) { - xQueueReceive(xBleMeshQueue, &msg, K_NO_WAIT); - } else if (uxQueueMessagesWaiting(xBleMeshRelayQueue)) { - xQueueReceive(xBleMeshRelayQueue, &msg, 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); } } } - } else { - handle = xQueueSelectFromSet(xBleMeshQueueSet, portMAX_DELAY); - if (handle) { - if (uxQueueMessagesWaiting(xBleMeshQueue)) { - xQueueReceive(xBleMeshQueue, &msg, K_NO_WAIT); - } else if (uxQueueMessagesWaiting(xBleMeshRelayQueue)) { - xQueueReceive(xBleMeshRelayQueue, &msg, K_NO_WAIT); - } - } } #else handle = xQueueSelectFromSet(xBleMeshQueueSet, portMAX_DELAY); if (handle) { - if (uxQueueMessagesWaiting(xBleMeshQueue)) { - xQueueReceive(xBleMeshQueue, &msg, K_NO_WAIT); - } else if (uxQueueMessagesWaiting(xBleMeshRelayQueue)) { - xQueueReceive(xBleMeshRelayQueue, &msg, K_NO_WAIT); + if (uxQueueMessagesWaiting(xBleMeshQueue.queue)) { + xQueueReceive(xBleMeshQueue.queue, &msg, K_NO_WAIT); + } else if (uxQueueMessagesWaiting(xBleMeshRelayQueue.queue)) { + xQueueReceive(xBleMeshRelayQueue.queue, &msg, K_NO_WAIT); } } -#endif +#endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ #endif /* !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ if (*buf == NULL) { @@ -266,7 +266,7 @@ static void adv_thread(void *p) /* If the interval between "current time - msg.timestamp" is bigger than * BLE_MESH_RELAY_TIME_INTERVAL, this relay packet will not be sent. */ - BT_DBG("%s, Ignore relay packet", __func__); + BT_INFO("%s, Ignore relay packet", __func__); net_buf_unref(*buf); } else { if (adv_send(*buf)) { @@ -289,8 +289,8 @@ struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool, enum bt_mesh_adv_type type, u8_t xmit, s32_t timeout) { - struct bt_mesh_adv *adv; - struct net_buf *buf; + struct bt_mesh_adv *adv = NULL; + struct net_buf *buf = NULL; if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_SUSPENDED)) { BT_WARN("Refusing to allocate buffer while suspended"); @@ -316,6 +316,24 @@ struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool, return buf; } +void bt_mesh_unref_buf_from_pool(struct net_buf_pool *pool) +{ + int i; + + if (pool == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + for (i = 0; i < pool->buf_count; i++) { + struct net_buf *buf = &pool->__bufs[i]; + if (buf->ref > 1U) { + buf->ref = 1U; + net_buf_unref(buf); + } + } +} + struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type, u8_t xmit, s32_t timeout) { @@ -349,7 +367,7 @@ void bt_mesh_adv_buf_ref_debug(const char *func, struct net_buf *buf, static void bt_mesh_unref_buf(bt_mesh_msg_t *msg) { - struct net_buf *buf; + struct net_buf *buf = NULL; if (msg->arg) { buf = (struct net_buf *)msg->arg; @@ -363,7 +381,13 @@ static void bt_mesh_unref_buf(bt_mesh_msg_t *msg) static void bt_mesh_task_post(bt_mesh_msg_t *msg, uint32_t timeout) { BT_DBG("%s", __func__); - if (xQueueSend(xBleMeshQueue, msg, timeout) != pdTRUE) { + + if (xBleMeshQueue.queue == NULL) { + BT_ERR("%s, Invalid queue", __func__); + return; + } + + if (xQueueSend(xBleMeshQueue.queue, msg, timeout) != pdTRUE) { BT_ERR("%s, Failed to send item to queue", __func__); bt_mesh_unref_buf(msg); } @@ -405,12 +429,10 @@ void bt_mesh_adv_update(void) static bool ignore_relay_packet(u32_t timestamp) { u32_t now = k_uptime_get_32(); - u32_t interval; + u32_t interval = 0U; - if (now > timestamp) { + if (now >= timestamp) { interval = now - timestamp; - } else if (now == timestamp) { - interval = BLE_MESH_MAX_TIME_INTERVAL; } else { interval = BLE_MESH_MAX_TIME_INTERVAL - (timestamp - now) + 1; } @@ -432,12 +454,17 @@ struct net_buf *bt_mesh_relay_adv_create(enum bt_mesh_adv_type type, u8_t xmit, static void ble_mesh_relay_task_post(bt_mesh_msg_t *msg, uint32_t timeout) { - QueueSetMemberHandle_t handle; + QueueSetMemberHandle_t handle = NULL; bt_mesh_msg_t old_msg = {0}; BT_DBG("%s", __func__); - if (xQueueSend(xBleMeshRelayQueue, msg, timeout) == pdTRUE) { + if (xBleMeshRelayQueue.queue == NULL) { + BT_ERR("%s, Invalid relay queue", __func__); + return; + } + + if (xQueueSend(xBleMeshRelayQueue.queue, msg, timeout) == pdTRUE) { return; } @@ -446,10 +473,10 @@ static void ble_mesh_relay_task_post(bt_mesh_msg_t *msg, uint32_t timeout) * remove the oldest packet in the queue and put the new one into it. */ handle = xQueueSelectFromSet(xBleMeshQueueSet, K_NO_WAIT); - if (handle && uxQueueMessagesWaiting(xBleMeshRelayQueue)) { - BT_DBG("%s, Full queue, remove the oldest relay packet", __func__); + if (handle && uxQueueMessagesWaiting(xBleMeshRelayQueue.queue)) { + BT_INFO("%s, Full queue, remove the oldest relay packet", __func__); /* Remove the oldest relay packet from queue */ - if (xQueueReceive(xBleMeshRelayQueue, &old_msg, K_NO_WAIT) != pdTRUE) { + if (xQueueReceive(xBleMeshRelayQueue.queue, &old_msg, K_NO_WAIT) != pdTRUE) { BT_ERR("%s, Failed to remove item from queue", __func__); bt_mesh_unref_buf(msg); return; @@ -457,7 +484,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, msg, K_NO_WAIT) != pdTRUE) { + if (xQueueSend(xBleMeshRelayQueue.queue, msg, K_NO_WAIT) != pdTRUE) { BT_ERR("%s, Failed to send item to relay queue", __func__); bt_mesh_unref_buf(msg); return; @@ -492,7 +519,7 @@ void bt_mesh_relay_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *c u16_t bt_mesh_get_stored_relay_count(void) { - return (u16_t)uxQueueMessagesWaiting(xBleMeshRelayQueue); + return (u16_t)uxQueueMessagesWaiting(xBleMeshRelayQueue.queue); } #endif /* #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ @@ -505,7 +532,7 @@ const bt_mesh_addr_t *bt_mesh_pba_get_addr(void) CONFIG_BLE_MESH_GATT_PROXY_CLIENT static bool bt_mesh_is_adv_flags_valid(struct net_buf_simple *buf) { - u8_t flags; + u8_t flags = 0U; if (buf->len != 1U) { BT_DBG("%s, Unexpected flags length", __func__); @@ -517,6 +544,7 @@ static bool bt_mesh_is_adv_flags_valid(struct net_buf_simple *buf) BT_DBG("Received adv pkt with flags: 0x%02x", flags); /* Flags context will not be checked curently */ + ((void) flags); return true; } @@ -554,9 +582,9 @@ static bool bt_mesh_is_adv_srv_uuid_valid(struct net_buf_simple *buf, u16_t *uui #define BLE_MESH_PROXY_SRV_DATA_LEN1 0x09 #define BLE_MESH_PROXY_SRV_DATA_LEN2 0x11 -static void bt_mesh_adv_srv_data_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr, u16_t uuid) +static void bt_mesh_adv_srv_data_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr, u16_t uuid, s8_t rssi) { - u16_t type; + u16_t type = 0U; if (!buf || !addr) { BT_ERR("%s, Invalid parameter", __func__); @@ -579,7 +607,7 @@ static void bt_mesh_adv_srv_data_recv(struct net_buf_simple *buf, const bt_mesh_ } BT_DBG("Start to handle Mesh Prov Service Data"); - provisioner_prov_adv_ind_recv(buf, addr); + bt_mesh_provisioner_prov_adv_ind_recv(buf, addr, rssi); } break; #endif @@ -592,7 +620,7 @@ static void bt_mesh_adv_srv_data_recv(struct net_buf_simple *buf, const bt_mesh_ } BT_DBG("Start to handle Mesh Proxy Service Data"); - proxy_client_adv_ind_recv(buf, addr); + bt_mesh_proxy_client_adv_ind_recv(buf, addr, rssi); break; #endif default: @@ -606,7 +634,7 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, s8_t rssi, { #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ CONFIG_BLE_MESH_GATT_PROXY_CLIENT - u16_t uuid = 0x0; + u16_t uuid = 0U; #endif if (adv_type != BLE_MESH_ADV_NONCONN_IND && adv_type != BLE_MESH_ADV_IND) { @@ -654,29 +682,16 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, s8_t rssi, break; #if CONFIG_BLE_MESH_PB_ADV case BLE_MESH_DATA_MESH_PROV: -#if CONFIG_BLE_MESH_NODE - if (!bt_mesh_is_provisioner_en()) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node()) { bt_mesh_pb_adv_recv(buf); } -#endif -#if CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - provisioner_pb_adv_recv(buf); + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + bt_mesh_provisioner_pb_adv_recv(buf); } -#endif break; #endif /* CONFIG_BLE_MESH_PB_ADV */ case BLE_MESH_DATA_MESH_BEACON: -#if CONFIG_BLE_MESH_NODE - if (!bt_mesh_is_provisioner_en()) { - bt_mesh_beacon_recv(buf); - } -#endif -#if CONFIG_BLE_MESH_PROVISIONER - if (bt_mesh_is_provisioner_en()) { - provisioner_beacon_recv(buf); - } -#endif + bt_mesh_beacon_recv(buf, rssi); break; #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ CONFIG_BLE_MESH_GATT_PROXY_CLIENT @@ -693,7 +708,7 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, s8_t rssi, } break; case BLE_MESH_DATA_SVC_DATA16: - bt_mesh_adv_srv_data_recv(buf, addr, uuid); + bt_mesh_adv_srv_data_recv(buf, addr, uuid, rssi); break; #endif default: @@ -709,24 +724,109 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, s8_t rssi, void bt_mesh_adv_init(void) { - xBleMeshQueue = xQueueCreate(BLE_MESH_QUEUE_SIZE, sizeof(bt_mesh_msg_t)); - configASSERT(xBleMeshQueue); +#if !CONFIG_SPIRAM_USE_MALLOC + 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__); +#endif + #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) - xBleMeshRelayQueue = xQueueCreate(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t)); - configASSERT(xBleMeshRelayQueue); +#if !CONFIG_SPIRAM_USE_MALLOC + 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__); +#endif + xBleMeshQueueSet = xQueueCreateSet(BLE_MESH_QUEUE_SET_SIZE); - configASSERT(xBleMeshQueueSet); - xQueueAddToSet(xBleMeshQueue, xBleMeshQueueSet); - xQueueAddToSet(xBleMeshRelayQueue, xBleMeshQueueSet); + __ASSERT(xBleMeshQueueSet, "%s, Failed to create queue set", __func__); + xQueueAddToSet(xBleMeshQueue.queue, xBleMeshQueueSet); + xQueueAddToSet(xBleMeshRelayQueue.queue, xBleMeshQueueSet); #endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ - int ret = xTaskCreatePinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", 3072, NULL, - configMAX_PRIORITIES - 7, NULL, ADV_TASK_CORE); - configASSERT(ret == pdTRUE); + +#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 + 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__); + 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.stack, "%s, Failed to create static adv thread stack", __func__); +#endif +} + +void bt_mesh_adv_deinit(void) +{ + if (xBleMeshQueue.queue == NULL) { + return; + } + +#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) + xQueueRemoveFromSet(xBleMeshQueue.queue, xBleMeshQueueSet); + xQueueRemoveFromSet(xBleMeshRelayQueue.queue, xBleMeshQueueSet); + + vQueueDelete(xBleMeshRelayQueue.queue); + xBleMeshRelayQueue.queue = NULL; +#if CONFIG_SPIRAM_USE_MALLOC + heap_caps_free(xBleMeshRelayQueue.buffer); + xBleMeshRelayQueue.buffer = NULL; + heap_caps_free(xBleMeshRelayQueue.storage); + xBleMeshRelayQueue.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; +#endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ + + vQueueDelete(xBleMeshQueue.queue); + xBleMeshQueue.queue = NULL; +#if CONFIG_SPIRAM_USE_MALLOC + heap_caps_free(xBleMeshQueue.buffer); + xBleMeshQueue.buffer = NULL; + heap_caps_free(xBleMeshQueue.storage); + xBleMeshQueue.storage = NULL; +#endif + + bt_mesh_unref_buf_from_pool(&adv_buf_pool); + memset(adv_pool, 0, sizeof(adv_pool)); + + vTaskDelete(adv_task.handle); + adv_task.handle = NULL; +#if CONFIG_SPIRAM_USE_MALLOC + heap_caps_free(adv_task.stack); + adv_task.stack = NULL; + /* Delay certain period for free adv_task.task */ + vTaskDelay(10 / portTICK_PERIOD_MS); + heap_caps_free(adv_task.task); + adv_task.task = NULL; +#endif } int bt_mesh_scan_enable(void) { - int err; + int err = 0; struct bt_mesh_scan_param scan_param = { .type = BLE_MESH_SCAN_PASSIVE, @@ -752,7 +852,7 @@ int bt_mesh_scan_enable(void) int bt_mesh_scan_disable(void) { - int err; + int err = 0; BT_DBG("%s", __func__); diff --git a/components/bt/esp_ble_mesh/mesh_core/adv.h b/components/bt/esp_ble_mesh/mesh_core/adv.h index 7d7cce63f..8d2c05656 100644 --- a/components/bt/esp_ble_mesh/mesh_core/adv.h +++ b/components/bt/esp_ble_mesh/mesh_core/adv.h @@ -10,6 +10,7 @@ #ifndef _ADV_H_ #define _ADV_H_ +#include "mesh_access.h" #include "mesh_bearer_adapt.h" /* Maximum advertising data payload for a single data type */ @@ -72,6 +73,8 @@ struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool, enum bt_mesh_adv_type type, u8_t xmit, s32_t timeout); +void bt_mesh_unref_buf_from_pool(struct net_buf_pool *pool); + void bt_mesh_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, void *cb_data); @@ -88,6 +91,7 @@ u16_t bt_mesh_get_stored_relay_count(void); void bt_mesh_adv_update(void); void bt_mesh_adv_init(void); +void bt_mesh_adv_deinit(void); int bt_mesh_scan_enable(void); diff --git a/components/bt/esp_ble_mesh/mesh_core/beacon.c b/components/bt/esp_ble_mesh/mesh_core/beacon.c index 4ba6c0281..1e720eb08 100644 --- a/components/bt/esp_ble_mesh/mesh_core/beacon.c +++ b/components/bt/esp_ble_mesh/mesh_core/beacon.c @@ -10,24 +10,19 @@ #include #include -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_BEACON) -#include "mesh_util.h" -#include "mesh_buf.h" -#include "mesh_main.h" -#include "mesh_trace.h" - #include "adv.h" #include "mesh.h" -#include "net.h" #include "prov.h" #include "crypto.h" #include "beacon.h" +#include "access.h" #include "foundation.h" #include "proxy_client.h" - -#if CONFIG_BLE_MESH_NODE +#include "mesh_main.h" +#include "provisioner_prov.h" +#include "provisioner_main.h" #if defined(CONFIG_BLE_MESH_FAST_PROV) #define UNPROVISIONED_INTERVAL K_SECONDS(3) @@ -49,12 +44,15 @@ static struct k_delayed_work beacon_timer; static struct bt_mesh_subnet *cache_check(u8_t data[21]) { - int i; + size_t subnet_size = 0U; + int i = 0; - for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { - struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + subnet_size = bt_mesh_rx_netkey_size(); - if (sub->net_idx == BLE_MESH_KEY_UNUSED) { + for (i = 0; i < subnet_size; i++) { + struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i); + + if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) { continue; } @@ -84,7 +82,7 @@ void bt_mesh_beacon_create(struct bt_mesh_subnet *sub, struct net_buf_simple *buf) { u8_t flags = bt_mesh_net_flags(sub); - struct bt_mesh_subnet_keys *keys; + struct bt_mesh_subnet_keys *keys = NULL; net_buf_simple_add_u8(buf, BEACON_TYPE_SECURE); @@ -104,9 +102,9 @@ void bt_mesh_beacon_create(struct bt_mesh_subnet *sub, net_buf_simple_add_mem(buf, sub->auth, 8); - BT_DBG("net_idx 0x%04x flags 0x%02x NetID %s", sub->net_idx, + BT_INFO("net_idx 0x%04x flags 0x%02x NetID %s", sub->net_idx, flags, bt_hex(keys->net_id, 8)); - BT_DBG("IV Index 0x%08x Auth %s", bt_mesh.iv_index, + BT_INFO("IV Index 0x%08x Auth %s", bt_mesh.iv_index, bt_hex(sub->auth, 8)); } @@ -120,16 +118,19 @@ static int secure_beacon_send(void) .end = beacon_complete, }; u32_t now = k_uptime_get_32(); - int i; + size_t subnet_size = 0U; + int i = 0; BT_DBG("%s", __func__); - for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { - struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + subnet_size = bt_mesh_rx_netkey_size(); + + for (i = 0; i < subnet_size; i++) { + struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i); struct net_buf *buf; u32_t time_diff; - if (sub->net_idx == BLE_MESH_KEY_UNUSED) { + if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) { continue; } @@ -167,13 +168,14 @@ static int secure_beacon_send(void) return 0; } +#if defined(CONFIG_BLE_MESH_NODE) static int unprovisioned_beacon_send(void) { #if defined(CONFIG_BLE_MESH_PB_ADV) - const struct bt_mesh_prov *prov; + const struct bt_mesh_prov *prov = NULL; u8_t uri_hash[16] = { 0 }; - struct net_buf *buf; - u16_t oob_info; + struct net_buf *buf = NULL; + u16_t oob_info = 0U; BT_DBG("%s", __func__); @@ -224,11 +226,18 @@ static int unprovisioned_beacon_send(void) #endif /* CONFIG_BLE_MESH_PB_ADV */ return 0; } +#else /* CONFIG_BLE_MESH_NODE */ +static int unprovisioned_beacon_send(void) +{ + return 0; +} +#endif /* CONFIG_BLE_MESH_NODE */ static void update_beacon_observation(void) { static bool first_half; - int i; + size_t subnet_size = 0U; + int i = 0; /* Observation period is 20 seconds, whereas the beacon timer * runs every 10 seconds. We process what's happened during the @@ -239,10 +248,12 @@ static void update_beacon_observation(void) return; } - for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { - struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + subnet_size = bt_mesh_rx_netkey_size(); - if (sub->net_idx == BLE_MESH_KEY_UNUSED) { + for (i = 0; i < subnet_size; i++) { + struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i); + + if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) { continue; } @@ -251,17 +262,33 @@ static void update_beacon_observation(void) } } +static bool ready_to_send(void) +{ + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { + return true; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + if (bt_mesh_provisioner_get_all_node_count()) { + return true; + } + } + + return false; +} + static void beacon_send(struct k_work *work) { /* Don't send anything if we have an active provisioning link */ - if (IS_ENABLED(CONFIG_BLE_MESH_PROV) && bt_prov_active()) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + IS_ENABLED(CONFIG_BLE_MESH_PROV) && bt_prov_active()) { k_delayed_work_submit(&beacon_timer, UNPROVISIONED_INTERVAL); return; } BT_DBG("%s", __func__); - if (bt_mesh_is_provisioned()) { + if (ready_to_send()) { update_beacon_observation(); secure_beacon_send(); @@ -272,19 +299,21 @@ static void beacon_send(struct k_work *work) PROVISIONED_INTERVAL); } } else { - unprovisioned_beacon_send(); - k_delayed_work_submit(&beacon_timer, UNPROVISIONED_INTERVAL); + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node()) { + unprovisioned_beacon_send(); + k_delayed_work_submit(&beacon_timer, UNPROVISIONED_INTERVAL); + } } } static void secure_beacon_recv(struct net_buf_simple *buf) { - u8_t *data, *net_id, *auth; - struct bt_mesh_subnet *sub; - u32_t iv_index; - bool new_key, kr_change, iv_change; - u8_t flags; + u8_t *data = NULL, *net_id = NULL, *auth = NULL; + struct bt_mesh_subnet *sub = NULL; + u32_t iv_index = 0U; + bool new_key = false, kr_change = false, iv_change = false; + u8_t flags = 0U; if (buf->len < 21) { BT_ERR("%s, Too short secure beacon (len %u)", __func__, buf->len); @@ -322,13 +351,13 @@ static void secure_beacon_recv(struct net_buf_simple *buf) cache_add(data, sub); /* If we have NetKey0 accept initiation only from it */ - if (bt_mesh_subnet_get(BLE_MESH_KEY_PRIMARY) && + if (bt_mesh_primary_subnet_exist() && sub->net_idx != BLE_MESH_KEY_PRIMARY) { BT_WARN("Ignoring secure beacon on non-primary subnet"); goto update_stats; } - BT_DBG("net_idx 0x%04x iv_index 0x%08x, current iv_index 0x%08x", + BT_INFO("net_idx 0x%04x iv_index 0x%08x, current iv_index 0x%08x", sub->net_idx, iv_index, bt_mesh.iv_index); if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_INITIATOR) && @@ -359,9 +388,9 @@ update_stats: } } -void bt_mesh_beacon_recv(struct net_buf_simple *buf) +void bt_mesh_beacon_recv(struct net_buf_simple *buf, s8_t rssi) { - u8_t type; + u8_t type = 0U; BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); @@ -373,7 +402,11 @@ void bt_mesh_beacon_recv(struct net_buf_simple *buf) type = net_buf_simple_pull_u8(buf); switch (type) { case BEACON_TYPE_UNPROVISIONED: - BT_DBG("Ignoring unprovisioned device beacon"); + BT_DBG("Unprovisioned device beacon received"); + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && + bt_mesh_is_provisioner_en()) { + bt_mesh_provisioner_unprov_beacon_recv(buf, rssi); + } break; case BEACON_TYPE_SECURE: secure_beacon_recv(buf); @@ -389,6 +422,11 @@ void bt_mesh_beacon_init(void) k_delayed_work_init(&beacon_timer, beacon_send); } +void bt_mesh_beacon_deinit(void) +{ + k_delayed_work_free(&beacon_timer); +} + void bt_mesh_beacon_ivu_initiator(bool enable) { bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_INITIATOR, enable); @@ -402,17 +440,21 @@ void bt_mesh_beacon_ivu_initiator(bool enable) void bt_mesh_beacon_enable(void) { - int i; + size_t subnet_size = 0U; + int i = 0; - if (!bt_mesh_is_provisioned()) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + !bt_mesh_is_provisioned()) { k_work_submit(&beacon_timer.work); return; } - for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { - struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + subnet_size = bt_mesh_rx_netkey_size(); - if (sub->net_idx == BLE_MESH_KEY_UNUSED) { + for (i = 0; i < subnet_size; i++) { + struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i); + + if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) { continue; } @@ -431,5 +473,3 @@ void bt_mesh_beacon_disable(void) k_delayed_work_cancel(&beacon_timer); } } - -#endif /* CONFIG_BLE_MESH_NODE */ diff --git a/components/bt/esp_ble_mesh/mesh_core/beacon.h b/components/bt/esp_ble_mesh/mesh_core/beacon.h index f410fa5d5..4977942e7 100644 --- a/components/bt/esp_ble_mesh/mesh_core/beacon.h +++ b/components/bt/esp_ble_mesh/mesh_core/beacon.h @@ -9,16 +9,19 @@ #ifndef _BEACON_H_ #define _BEACON_H_ +#include "net.h" + void bt_mesh_beacon_enable(void); void bt_mesh_beacon_disable(void); void bt_mesh_beacon_ivu_initiator(bool enable); -void bt_mesh_beacon_recv(struct net_buf_simple *buf); +void bt_mesh_beacon_recv(struct net_buf_simple *buf, s8_t rssi); void bt_mesh_beacon_create(struct bt_mesh_subnet *sub, struct net_buf_simple *buf); void bt_mesh_beacon_init(void); +void bt_mesh_beacon_deinit(void); #endif /* _BEACON_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c index 021ec71a2..3ce7c7dcc 100644 --- a/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c +++ b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c @@ -6,20 +6,16 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include -#include "sdkconfig.h" - #include "bta/bta_api.h" #include "bta/bta_gatt_api.h" #include "bta/bta_gatt_common.h" #include "bta_gattc_int.h" #include "stack/btm_ble_api.h" #include "p_256_ecc_pp.h" -#include "stack/hcimsgs.h" #include "osi/future.h" -#include "osi/allocator.h" -#include "bt_common.h" #include "device/controller.h" #include "mbedtls/aes.h" @@ -27,12 +23,8 @@ #include "mesh_hci.h" #include "mesh_aes_encrypt.h" #include "mesh_bearer_adapt.h" -#include "mesh_trace.h" -#include "mesh_buf.h" -#include "mesh_atomic.h" - -#include "provisioner_prov.h" #include "mesh_common.h" +#include "provisioner_prov.h" struct bt_mesh_dev bt_mesh_dev; @@ -65,7 +57,10 @@ static BT_OCTET32 bt_mesh_private_key = { static bt_mesh_scan_cb_t *bt_mesh_scan_dev_found_cb; static void bt_mesh_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data); -#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER +/* Using UUID with a fixed pattern 0x96 for BLE Mesh GATT Proxy Server */ +#define BLE_MESH_GATTS_APP_UUID_BYTE 0x96 /* the gatt database list to save the attribute table */ static sys_slist_t bt_mesh_gatts_db; @@ -79,10 +74,11 @@ static future_t *future_mesh; /* Static Functions */ static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(u16_t handle); -#endif /* defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE */ +#endif #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ CONFIG_BLE_MESH_GATT_PROXY_CLIENT +/* Using UUID with a fixed pattern 0x97 for BLE Mesh GATT Proxy Client */ #define BLE_MESH_GATTC_APP_UUID_BYTE 0x97 static struct gattc_prov_info { /* Service to be found depends on the type of adv pkt received */ @@ -101,9 +97,14 @@ static struct bt_mesh_prov_conn_cb *bt_mesh_gattc_conn_cb; static tBTA_GATTC_IF bt_mesh_gattc_if; #endif -esp_err_t bt_mesh_host_init(void) +int bt_mesh_host_init(void) { - return ESP_OK; + return 0; +} + +int bt_mesh_host_deinit(void) +{ + return 0; } void bt_mesh_hci_init(void) @@ -138,8 +139,8 @@ void bt_mesh_hci_init(void) static void bt_mesh_scan_results_change_2_bta(tBTM_INQ_RESULTS *p_inq, u8_t *p_eir, tBTA_DM_SEARCH_CBACK *p_scan_cback) { - tBTM_INQ_INFO *p_inq_info; - tBTA_DM_SEARCH result; + tBTM_INQ_INFO *p_inq_info = NULL; + tBTA_DM_SEARCH result = {0}; bdcpy(result.inq_res.bd_addr, p_inq->remote_bd_addr); result.inq_res.rssi = p_inq->rssi; @@ -292,8 +293,8 @@ static int start_le_scan(u8_t scan_type, u16_t interval, u16_t window, u8_t filt static void bt_mesh_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) { bt_mesh_addr_t addr = {0}; - UINT8 adv_type; - UINT8 rssi; + u8_t adv_type = 0U; + s8_t rssi = 0; BT_DBG("%s, event = %d", __func__, event); @@ -315,7 +316,7 @@ static void bt_mesh_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARC if (bt_mesh_scan_dev_found_cb != NULL) { bt_mesh_scan_dev_found_cb(&addr, rssi, adv_type, buf); } - osi_free(buf); + bt_mesh_free(buf); } else if (event == BTA_DM_INQ_CMPL_EVT) { BT_INFO("%s, Scan completed, number of scan response %d", __func__, p_data->inq_cmpl.num_resps); } else { @@ -328,13 +329,13 @@ int bt_le_adv_start(const struct bt_mesh_adv_param *param, const struct bt_mesh_adv_data *ad, size_t ad_len, const struct bt_mesh_adv_data *sd, size_t sd_len) { - tBTA_START_ADV_CMPL_CBACK *p_start_adv_cb; - tBTM_BLE_ADV_CHNL_MAP channel_map; - tBLE_ADDR_TYPE addr_type_own; + tBTA_START_ADV_CMPL_CBACK *p_start_adv_cb = NULL; + tBTM_BLE_ADV_CHNL_MAP channel_map = 0U; + tBLE_ADDR_TYPE addr_type_own = 0U; tBLE_BD_ADDR p_dir_bda = {0}; - tBTM_BLE_AFP adv_fil_pol; - UINT8 adv_type; - int err; + tBTM_BLE_AFP adv_fil_pol = 0U; + u8_t adv_type = 0U; + int err = 0; #if BLE_MESH_DEV if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING)) { @@ -420,7 +421,7 @@ int bt_le_adv_stop(void) int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t cb) { - int err; + int err = 0; #if BLE_MESH_DEV if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { @@ -468,7 +469,8 @@ int bt_le_scan_stop(void) return 0; } -#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER static void bt_mesh_bta_gatts_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) { switch (event) { @@ -480,7 +482,7 @@ static void bt_mesh_bta_gatts_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) case BTA_GATTS_READ_EVT: { struct bt_mesh_gatt_attr *attr = bt_mesh_gatts_find_attr_by_handle(p_data->req_data.p_data->read_req.handle); u8_t index = BLE_MESH_GATT_GET_CONN_ID(p_data->req_data.conn_id); - tBTA_GATTS_RSP rsp; + tBTA_GATTS_RSP rsp = {0}; u8_t buf[100] = {0}; u16_t len = 0; @@ -606,6 +608,11 @@ void bt_mesh_gatts_conn_cb_register(struct bt_mesh_conn_cb *cb) bt_mesh_gatts_conn_cb = cb; } +void bt_mesh_gatts_conn_cb_deregister(void) +{ + bt_mesh_gatts_conn_cb = NULL; +} + static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(u16_t handle) { struct bt_mesh_gatt_service *svc = NULL; @@ -672,7 +679,7 @@ ssize_t bt_mesh_gatts_attr_read(struct bt_mesh_conn *conn, const struct bt_mesh_ void *buf, u16_t buf_len, u16_t offset, const void *value, u16_t value_len) { - u16_t len; + u16_t len = 0U; if (offset > value_len) { return BLE_MESH_GATT_ERR(BLE_MESH_ATT_ERR_INVALID_OFFSET); @@ -700,7 +707,7 @@ ssize_t bt_mesh_gatts_attr_read_included(struct bt_mesh_conn *conn, struct bt_mesh_gatt_attr *incl = attr->user_data; struct bt_mesh_uuid *uuid = incl->user_data; struct gatts_incl pdu = {0}; - u8_t value_len; + u8_t value_len = 0U; /* First attr points to the start handle */ pdu.start_handle = sys_cpu_to_le16(incl->handle); @@ -750,7 +757,7 @@ ssize_t bt_mesh_gatts_attr_read_chrc(struct bt_mesh_conn *conn, struct bt_mesh_gatt_char *chrc = attr->user_data; const struct bt_mesh_gatt_attr *next = NULL; struct gatts_chrc pdu = {0}; - u8_t value_len; + u8_t value_len = 0U; pdu.properties = chrc->properties; /* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] page 534: @@ -802,8 +809,8 @@ static void bta_uuid_to_bt_mesh_uuid(tBT_UUID *bta_uuid, const struct bt_mesh_uu static int gatts_register(struct bt_mesh_gatt_service *svc) { - struct bt_mesh_gatt_service *last; - u16_t handle; + struct bt_mesh_gatt_service *last = NULL; + u16_t handle = 0U; if (sys_slist_is_empty(&bt_mesh_gatts_db)) { handle = 0; @@ -814,11 +821,23 @@ static int gatts_register(struct bt_mesh_gatt_service *svc) handle = last->attrs[last->attr_count - 1].handle; BT_DBG("%s, handle = %d", __func__, handle); + ((void) handle); + populate: sys_slist_append(&bt_mesh_gatts_db, &svc->node); return 0; } +static int gatts_deregister(struct bt_mesh_gatt_service *svc) +{ + if (sys_slist_is_empty(&bt_mesh_gatts_db)) { + return 0; + } + + sys_slist_find_and_remove(&bt_mesh_gatts_db, &svc->node); + return 0; +} + static tBTA_GATT_PERM bt_mesh_perm_to_bta_perm(u8_t perm) { tBTA_GATT_PERM bta_perm = 0; @@ -939,6 +958,17 @@ int bt_mesh_gatts_service_register(struct bt_mesh_gatt_service *svc) return 0; } +int bt_mesh_gatts_service_deregister(struct bt_mesh_gatt_service *svc) +{ + assert(svc != NULL); + + gatts_deregister(svc); + + BTA_GATTS_DeleteService(svc->attrs[0].handle); + + return 0; +} + int bt_mesh_gatts_disconnect(struct bt_mesh_conn *conn, u8_t reason) { UNUSED(reason); @@ -1019,7 +1049,7 @@ int bt_mesh_gatts_set_local_device_name(const char *name) return 0; } -#endif /* defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE */ +#endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ CONFIG_BLE_MESH_GATT_PROXY_CLIENT @@ -1028,12 +1058,17 @@ void bt_mesh_gattc_conn_cb_register(struct bt_mesh_prov_conn_cb *cb) bt_mesh_gattc_conn_cb = cb; } +void bt_mesh_gattc_conn_cb_deregister(void) +{ + bt_mesh_gattc_conn_cb = NULL; +} + u8_t bt_mesh_gattc_get_free_conn_count(void) { - u8_t count = 0; - u8_t i; + u8_t count = 0U; + int i; - for (i = 0U; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { if (bt_mesh_gattc_info[i].conn.handle == 0xFFFF && bt_mesh_gattc_info[i].service_uuid == 0x0000) { ++count; @@ -1045,9 +1080,9 @@ u8_t bt_mesh_gattc_get_free_conn_count(void) u16_t bt_mesh_gattc_get_service_uuid(struct bt_mesh_conn *conn) { - u8_t i; + int i; - for (i = 0U; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { if (conn == &bt_mesh_gattc_info[i].conn) { return bt_mesh_gattc_info[i].service_uuid; } @@ -1139,7 +1174,7 @@ void bt_mesh_gattc_exchange_mtu(u8_t index) * ATT_MTU >= 69 for Mesh GATT Prov Service * ATT_NTU >= 33 for Mesh GATT Proxy Service */ - u16_t conn_id; + u16_t conn_id = 0U; conn_id = BLE_MESH_GATT_CREATE_CONN_ID(bt_mesh_gattc_if, bt_mesh_gattc_info[index].conn.handle); @@ -1162,7 +1197,7 @@ u16_t bt_mesh_gattc_get_mtu_info(struct bt_mesh_conn *conn) int bt_mesh_gattc_write_no_rsp(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, const void *data, u16_t len) { - u16_t conn_id; + u16_t conn_id = 0U; int i; for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { @@ -1189,7 +1224,7 @@ void bt_mesh_gattc_disconnect(struct bt_mesh_conn *conn) * when proxy_disconnected callback comes, the proxy server * information and prov_link information should be cleared. */ - u16_t conn_id; + u16_t conn_id = 0U; int i; for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { @@ -1214,7 +1249,7 @@ void bt_mesh_gattc_disconnect(struct bt_mesh_conn *conn) static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) { struct bt_mesh_conn *conn = NULL; - u16_t handle = 0; + u16_t handle = 0U; ssize_t len = 0; int i = 0; @@ -1299,9 +1334,9 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) u16_t notify_en = BLE_MESH_GATT_CCC_NOTIFY; btgatt_db_element_t *result = NULL; tBT_UUID char_uuid = {0}; - tBTA_GATT_STATUS status; - tBTA_GATT_UNFMT write; - u16_t count = 0; + tBTA_GATT_STATUS status = 0U; + tBTA_GATT_UNFMT write = {0}; + u16_t count = 0U; u16_t num = 0; /* Get the characteristic num within Mesh Provisioning/Proxy Service */ @@ -1334,28 +1369,28 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) } if (num != 1) { - osi_free(result); + bt_mesh_free(result); bt_mesh_gattc_disconnect(conn); return; } if (!j) { if (!(result[0].properties & BLE_MESH_GATT_CHRC_WRITE_WITHOUT_RESP)) { - osi_free(result); + bt_mesh_free(result); bt_mesh_gattc_disconnect(conn); return; } bt_mesh_gattc_info[i].data_in_handle = result[0].attribute_handle; } else { if (!(result[0].properties & BLE_MESH_GATT_CHRC_NOTIFY)) { - osi_free(result); + bt_mesh_free(result); bt_mesh_gattc_disconnect(conn); return; } bt_mesh_gattc_info[i].data_out_handle = result[0].attribute_handle; } - osi_free(result); + bt_mesh_free(result); result = NULL; } @@ -1389,7 +1424,7 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) } if (num != 1) { - osi_free(result); + bt_mesh_free(result); bt_mesh_gattc_disconnect(conn); return; } @@ -1404,7 +1439,7 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) BTA_GATTC_WriteCharDescr(p_data->search_cmpl.conn_id, result[0].attribute_handle, BTA_GATTC_TYPE_WRITE, &write, BTA_GATT_AUTH_REQ_NONE); - osi_free(result); + bt_mesh_free(result); result = NULL; } break; @@ -1587,7 +1622,7 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) */ #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { - provisioner_clear_link_conn_info(bt_mesh_gattc_info[i].addr.val); + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); } #endif } @@ -1598,14 +1633,14 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) */ #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { - provisioner_clear_link_conn_info(bt_mesh_gattc_info[i].addr.val); + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); } #endif } #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { /* Decrease provisioner pbg_count */ - provisioner_pbg_count_dec(); + bt_mesh_provisioner_pbg_count_dec(); } #endif /* Reset corresponding gattc info */ @@ -1647,25 +1682,55 @@ void bt_mesh_conn_unref(struct bt_mesh_conn *conn) void bt_mesh_gatt_init(void) { - tBT_UUID app_uuid = {LEN_UUID_128, {0}}; - BTA_GATT_SetLocalMTU(GATT_DEF_BLE_MTU_SIZE); -#if CONFIG_BLE_MESH_NODE - /* Fill our internal UUID with a fixed pattern 0x96 for the ble mesh */ - memset(&app_uuid.uu.uuid128, 0x96, LEN_UUID_128); - BTA_GATTS_AppRegister(&app_uuid, bt_mesh_bta_gatts_cb); +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER + tBT_UUID gatts_app_uuid = {LEN_UUID_128, {0}}; + memset(&gatts_app_uuid.uu.uuid128, BLE_MESH_GATTS_APP_UUID_BYTE, LEN_UUID_128); + BTA_GATTS_AppRegister(&gatts_app_uuid, bt_mesh_bta_gatts_cb); #endif #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ CONFIG_BLE_MESH_GATT_PROXY_CLIENT + tBT_UUID gattc_app_uuid = {LEN_UUID_128, {0}}; for (int i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { bt_mesh_gattc_info[i].conn.handle = 0xFFFF; bt_mesh_gattc_info[i].mtu = GATT_DEF_BLE_MTU_SIZE; /* Default MTU_SIZE 23 */ bt_mesh_gattc_info[i].wr_desc_done = false; } - memset(&app_uuid.uu.uuid128, BLE_MESH_GATTC_APP_UUID_BYTE, LEN_UUID_128); - BTA_GATTC_AppRegister(&app_uuid, bt_mesh_bta_gattc_cb); + memset(&gattc_app_uuid.uu.uuid128, BLE_MESH_GATTC_APP_UUID_BYTE, LEN_UUID_128); + BTA_GATTC_AppRegister(&gattc_app_uuid, bt_mesh_bta_gattc_cb); +#endif +} + +void bt_mesh_gatt_deinit(void) +{ +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER + BTA_GATTS_AppDeregister(bt_mesh_gatts_if); + memset(bt_mesh_gatts_addr, 0, BLE_MESH_ADDR_LEN); + bt_mesh_gatts_if = 0U; + svc_handle = 0U; + char_handle = 0U; +#endif + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + BTA_GATTC_AppDeregister(bt_mesh_gattc_if); + bt_mesh_gattc_if = 0U; + for (int i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + bt_mesh_gattc_info[i].conn.handle = 0xFFFF; + memset(&bt_mesh_gattc_info[i].addr, 0, sizeof(bt_mesh_addr_t)); + bt_mesh_gattc_info[i].service_uuid = 0U; + bt_mesh_gattc_info[i].mtu = GATT_DEF_BLE_MTU_SIZE; /* Default MTU_SIZE 23 */ + bt_mesh_gattc_info[i].wr_desc_done = false; + bt_mesh_gattc_info[i].start_handle = 0U; + bt_mesh_gattc_info[i].end_handle = 0U; + bt_mesh_gattc_info[i].data_in_handle = 0U; + bt_mesh_gattc_info[i].data_out_handle = 0U; + bt_mesh_gattc_info[i].ccc_handle = 0U; + } #endif } @@ -1778,7 +1843,7 @@ int bt_mesh_dh_key_gen(const u8_t remote_pk[64], bt_mesh_dh_key_cb_t cb, const u static void ecb_encrypt(u8_t const *const key_le, u8_t const *const clear_text_le, u8_t *const cipher_text_le, u8_t *const cipher_text_be) { - struct bt_mesh_ecb_param ecb; + struct bt_mesh_ecb_param ecb = {0}; mbedtls_aes_context aes_ctx = {0}; aes_ctx.key_bytes = 16; @@ -1800,7 +1865,7 @@ static void ecb_encrypt(u8_t const *const key_le, u8_t const *const clear_text_l static void ecb_encrypt_be(u8_t const *const key_be, u8_t const *const clear_text_be, u8_t *const cipher_text_be) { - struct bt_mesh_ecb_param ecb; + struct bt_mesh_ecb_param ecb = {0}; mbedtls_aes_context aes_ctx = {0}; aes_ctx.key_bytes = 16; @@ -1823,8 +1888,8 @@ int bt_mesh_encrypt_le(const u8_t key[16], const u8_t plaintext[16], BT_DBG("enc_data %s", bt_hex(enc_data, 16)); return 0; #else /* CONFIG_MBEDTLS_HARDWARE_AES */ - struct tc_aes_key_sched_struct s; - u8_t tmp[16]; + struct tc_aes_key_sched_struct s = {0}; + u8_t tmp[16] = {0}; BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); @@ -1860,7 +1925,7 @@ int bt_mesh_encrypt_be(const u8_t key[16], const u8_t plaintext[16], return 0; #else /* CONFIG_MBEDTLS_HARDWARE_AES */ - struct tc_aes_key_sched_struct s; + struct tc_aes_key_sched_struct s = {0}; BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); diff --git a/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c b/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c index 044c7c9b1..9ca4c2e32 100644 --- a/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c @@ -11,20 +11,13 @@ #include #include -#include "osi/allocator.h" -#include "osi/mutex.h" -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_MODEL) -#include "mesh_types.h" -#include "mesh_util.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" -#include "cfg_cli.h" - #include "mesh.h" #include "foundation.h" #include "mesh_common.h" +#include "cfg_cli.h" + #include "btc_ble_mesh_config_model.h" #define CID_NVAL 0xffff @@ -86,32 +79,36 @@ static const bt_mesh_client_op_pair_t cfg_op_pair[] = { { OP_NET_TRANSMIT_SET, OP_NET_TRANSMIT_STATUS }, }; -static osi_mutex_t cfg_client_mutex; +static bt_mesh_mutex_t cfg_client_lock; static void bt_mesh_cfg_client_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&cfg_client_mutex); - init = true; + if (!cfg_client_lock.mutex) { + bt_mesh_mutex_create(&cfg_client_lock); } } +static void bt_mesh_cfg_client_mutex_free(void) +{ + bt_mesh_mutex_free(&cfg_client_lock); +} + static void bt_mesh_cfg_client_lock(void) { - osi_mutex_lock(&cfg_client_mutex, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_mutex_lock(&cfg_client_lock); } static void bt_mesh_cfg_client_unlock(void) { - osi_mutex_unlock(&cfg_client_mutex); + bt_mesh_mutex_unlock(&cfg_client_lock); } static void timeout_handler(struct k_work *work) { struct k_delayed_work *timer = NULL; bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; + u32_t opcode = 0U; BT_WARN("Receive configuration status message timeout"); @@ -122,10 +119,11 @@ static void timeout_handler(struct k_work *work) if (timer && !k_delayed_work_free(timer)) { node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); if (node) { - bt_mesh_config_client_cb_evt_to_btc(node->opcode, - BTC_BLE_MESH_EVT_CONFIG_CLIENT_TIMEOUT, node->ctx.model, &node->ctx, NULL, 0); - // Don't forget to release the node at the end. + memcpy(&ctx, &node->ctx, sizeof(ctx)); + opcode = node->opcode; bt_mesh_client_free_node(node); + bt_mesh_config_client_cb_evt_to_btc( + opcode, BTC_BLE_MESH_EVT_CONFIG_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0); } } @@ -214,10 +212,10 @@ static void cfg_client_cancel(struct bt_mesh_model *model, } if (!k_delayed_work_free(&node->timer)) { - bt_mesh_config_client_cb_evt_to_btc( - node->opcode, evt_type, model, ctx, (const u8_t *)status, len); - // Don't forget to release the node at the end. + u32_t opcode = node->opcode; bt_mesh_client_free_node(node); + bt_mesh_config_client_cb_evt_to_btc( + opcode, evt_type, model, ctx, (const u8_t *)status, len); } } @@ -283,7 +281,7 @@ static void state_status_u8(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u8_t status = 0; + u8_t status = 0U; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -343,7 +341,7 @@ static void net_key_status(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_cfg_netkey_status status = {0}; - u16_t app_idx; + u16_t app_idx = 0U; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -689,7 +687,7 @@ const struct bt_mesh_model_op bt_mesh_cfg_cli_op[] = { int bt_mesh_cfg_comp_data_get(struct bt_mesh_msg_ctx *ctx, u8_t page) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_DEV_COMP_DATA_GET, 1); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -711,7 +709,7 @@ int bt_mesh_cfg_comp_data_get(struct bt_mesh_msg_ctx *ctx, u8_t page) static int get_state_u8(struct bt_mesh_msg_ctx *ctx, u32_t op) { BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 0); - int err; + int err = 0; bt_mesh_model_msg_init(&msg, op); @@ -727,7 +725,7 @@ static int get_state_u8(struct bt_mesh_msg_ctx *ctx, u32_t op) static int set_state_u8(struct bt_mesh_msg_ctx *ctx, u32_t op, u8_t new_val) { BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 1); - int err; + int err = 0; bt_mesh_model_msg_init(&msg, op); net_buf_simple_add_u8(&msg, new_val); @@ -808,7 +806,7 @@ int bt_mesh_cfg_gatt_proxy_set(struct bt_mesh_msg_ctx *ctx, u8_t val) int bt_mesh_cfg_relay_get(struct bt_mesh_msg_ctx *ctx) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_GET, 0); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -830,7 +828,7 @@ int bt_mesh_cfg_relay_set(struct bt_mesh_msg_ctx *ctx, u8_t new_relay, u8_t new_transmit) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_SET, 2); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -854,7 +852,7 @@ int bt_mesh_cfg_net_key_add(struct bt_mesh_msg_ctx *ctx, u16_t key_net_idx, const u8_t net_key[16]) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_ADD, 18); - int err; + int err = 0; if (!ctx || !ctx->addr || !net_key) { return -EINVAL; @@ -878,7 +876,7 @@ int bt_mesh_cfg_app_key_add(struct bt_mesh_msg_ctx *ctx, u16_t key_net_idx, u16_t key_app_idx, const u8_t app_key[16]) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_ADD, 19); - int err; + int err = 0; if (!ctx || !ctx->addr || !app_key) { return -EINVAL; @@ -902,7 +900,7 @@ int bt_mesh_cfg_mod_app_bind(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_app_idx, u16_t mod_id, u16_t cid) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_BIND, 8); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -930,7 +928,7 @@ static int mod_sub(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t sub_addr, u16_t mod_id, u16_t cid) { BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 8); - int err; + int err = 0; bt_mesh_model_msg_init(&msg, op); net_buf_simple_add_le16(&msg, elem_addr); @@ -980,7 +978,7 @@ static int mod_sub_va(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, const u8_t label[16], u16_t mod_id, u16_t cid) { BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 22); - int err; + int err = 0; BT_DBG("net_idx 0x%04x addr 0x%04x elem_addr 0x%04x label %s", ctx->net_idx, ctx->addr, elem_addr, label); @@ -1034,7 +1032,7 @@ int bt_mesh_cfg_mod_pub_get(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id, u16_t cid) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_GET, 6); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -1062,7 +1060,7 @@ int bt_mesh_cfg_mod_pub_set(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, struct bt_mesh_cfg_mod_pub *pub) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_SET, 13); - int err; + int err = 0; if (!ctx || !ctx->addr || !pub) { return -EINVAL; @@ -1094,7 +1092,7 @@ int bt_mesh_cfg_hb_sub_set(struct bt_mesh_msg_ctx *ctx, struct bt_mesh_cfg_hb_sub *sub) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_SUB_SET, 5); - int err; + int err = 0; if (!ctx || !ctx->addr || !sub) { return -EINVAL; @@ -1118,7 +1116,7 @@ int bt_mesh_cfg_hb_sub_set(struct bt_mesh_msg_ctx *ctx, int bt_mesh_cfg_hb_sub_get(struct bt_mesh_msg_ctx *ctx) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_SUB_GET, 0); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -1140,7 +1138,7 @@ int bt_mesh_cfg_hb_pub_set(struct bt_mesh_msg_ctx *ctx, const struct bt_mesh_cfg_hb_pub *pub) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_PUB_SET, 9); - int err; + int err = 0; if (!ctx || !ctx->addr || !pub) { return -EINVAL; @@ -1167,7 +1165,7 @@ int bt_mesh_cfg_hb_pub_set(struct bt_mesh_msg_ctx *ctx, int bt_mesh_cfg_hb_pub_get(struct bt_mesh_msg_ctx *ctx) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_PUB_GET, 0); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -1188,7 +1186,7 @@ int bt_mesh_cfg_hb_pub_get(struct bt_mesh_msg_ctx *ctx) int bt_mesh_cfg_node_reset(struct bt_mesh_msg_ctx *ctx) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_RESET, 0); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -1211,7 +1209,7 @@ int bt_mesh_cfg_mod_pub_va_set(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, struct bt_mesh_cfg_mod_pub *pub) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_VA_SET, 27); - int err; + int err = 0; if (!ctx || !ctx->addr || !label || !pub) { return -EINVAL; @@ -1243,7 +1241,7 @@ int bt_mesh_cfg_mod_sub_del_all(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id, u16_t cid) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_DEL_ALL, 6); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -1270,7 +1268,7 @@ static int mod_sub_get(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id, u16_t cid) { BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 6); - int err; + int err = 0; bt_mesh_model_msg_init(&msg, op); net_buf_simple_add_le16(&msg, elem_addr); @@ -1309,7 +1307,7 @@ int bt_mesh_cfg_net_key_update(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, const u8_t net_key[16]) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_UPDATE, 18); - int err; + int err = 0; if (!ctx || !ctx->addr || !net_key) { return -EINVAL; @@ -1332,7 +1330,7 @@ int bt_mesh_cfg_net_key_update(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, int bt_mesh_cfg_net_key_delete(struct bt_mesh_msg_ctx *ctx, u16_t net_idx) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_DEL, 2); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -1354,7 +1352,7 @@ int bt_mesh_cfg_net_key_delete(struct bt_mesh_msg_ctx *ctx, u16_t net_idx) int bt_mesh_cfg_net_key_get(struct bt_mesh_msg_ctx *ctx) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_GET, 0); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -1376,7 +1374,7 @@ int bt_mesh_cfg_app_key_update(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u16_t app_idx, const u8_t app_key[16]) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_UPDATE, 19); - int err; + int err = 0; if (!ctx || !ctx->addr || !app_key) { return -EINVAL; @@ -1399,7 +1397,7 @@ int bt_mesh_cfg_app_key_update(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, int bt_mesh_cfg_app_key_delete(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u16_t app_idx) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_DEL, 3); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -1421,7 +1419,7 @@ int bt_mesh_cfg_app_key_delete(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u16_t int bt_mesh_cfg_app_key_get(struct bt_mesh_msg_ctx *ctx, u16_t net_idx) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_GET, 2); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -1444,7 +1442,7 @@ static int node_identity_op(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u8_t identity) { BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 3); - int err; + int err = 0; bt_mesh_model_msg_init(&msg, op); net_buf_simple_add_le16(&msg, net_idx); @@ -1481,7 +1479,7 @@ int bt_mesh_cfg_mod_app_unbind(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t app_idx, u16_t mod_id, u16_t cid) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_UNBIND, 8); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -1509,7 +1507,7 @@ static int mod_app_get(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id, u16_t cid) { BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 6); - int err; + int err = 0; bt_mesh_model_msg_init(&msg, op); net_buf_simple_add_le16(&msg, elem_addr); @@ -1548,7 +1546,7 @@ static int kr_phase_op(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u8_t transition) { BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 3); - int err; + int err = 0; bt_mesh_model_msg_init(&msg, op); net_buf_simple_add_le16(&msg, net_idx); @@ -1584,7 +1582,7 @@ int bt_mesh_cfg_kr_phase_set(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u8_t tr int bt_mesh_cfg_lpn_timeout_get(struct bt_mesh_msg_ctx *ctx, u16_t lpn_addr) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_LPN_TIMEOUT_GET, 2); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -1653,7 +1651,7 @@ int bt_mesh_cfg_cli_init(struct bt_mesh_model *model, bool primary) } if (!client->internal_data) { - internal = osi_calloc(sizeof(config_internal_data_t)); + internal = bt_mesh_calloc(sizeof(config_internal_data_t)); if (!internal) { BT_ERR("Allocate memory for Configuration Client internal data fail"); return -ENOMEM; @@ -1678,3 +1676,39 @@ int bt_mesh_cfg_cli_init(struct bt_mesh_model *model, bool primary) return 0; } + +int bt_mesh_cfg_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + bt_mesh_config_client_t *client = NULL; + + if (!primary) { + BT_ERR("Configuration Client only allowed in primary element"); + return -EINVAL; + } + + if (!model) { + BT_ERR("Configuration Client model is NULL"); + return -EINVAL; + } + + client = (bt_mesh_config_client_t *)model->user_data; + if (!client) { + BT_ERR("No Configuration Client context provided"); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + cli->internal_data = NULL; + } + + client = NULL; + + bt_mesh_cfg_client_mutex_free(); + + return 0; +} diff --git a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c index b3c2b2609..dbd31dd29 100644 --- a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c @@ -11,30 +11,23 @@ #include #include -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_MODEL) -#include "mesh_types.h" -#include "mesh_util.h" -#include "mesh_main.h" -#include "mesh_trace.h" -#include "cfg_srv.h" -#include "settings.h" - #include "mesh.h" #include "adv.h" -#include "net.h" #include "lpn.h" #include "transport.h" #include "crypto.h" #include "access.h" #include "beacon.h" -#include "proxy_server.h" #include "foundation.h" #include "friend.h" #include "settings.h" - +#include "cfg_srv.h" +#include "proxy_server.h" +#include "mesh_main.h" #include "mesh_common.h" + #include "btc_ble_mesh_config_model.h" #define DEFAULT_TTL 7 @@ -52,7 +45,7 @@ static struct label labels[CONFIG_BLE_MESH_LABEL_COUNT]; static int comp_add_elem(struct net_buf_simple *buf, struct bt_mesh_elem *elem, bool primary) { - struct bt_mesh_model *mod; + struct bt_mesh_model *mod = NULL; int i; if (net_buf_simple_tailroom(buf) < @@ -83,7 +76,7 @@ static int comp_add_elem(struct net_buf_simple *buf, struct bt_mesh_elem *elem, static int comp_get_page_0(struct net_buf_simple *buf) { u16_t feat = 0U; - const struct bt_mesh_comp *comp; + const struct bt_mesh_comp *comp = NULL; int i; comp = bt_mesh_comp_get(); @@ -127,7 +120,7 @@ static void dev_comp_data_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *sdu = NULL; - u8_t page; + u8_t page = 0U; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -166,7 +159,7 @@ static struct bt_mesh_model *get_model(struct bt_mesh_elem *elem, struct net_buf_simple *buf, bool *vnd) { if (buf->len < 4) { - u16_t id; + u16_t id = 0U; id = net_buf_simple_pull_le16(buf); @@ -176,7 +169,7 @@ static struct bt_mesh_model *get_model(struct bt_mesh_elem *elem, return bt_mesh_model_find(elem, id); } else { - u16_t company, id; + u16_t company = 0U, id = 0U; company = net_buf_simple_pull_le16(buf); id = net_buf_simple_pull_le16(buf); @@ -357,9 +350,9 @@ struct bt_mesh_app_key *bt_mesh_app_key_alloc(u16_t app_idx) static u8_t app_key_set(u16_t net_idx, u16_t app_idx, const u8_t val[16], bool update) { - struct bt_mesh_app_keys *keys; - struct bt_mesh_app_key *key; - struct bt_mesh_subnet *sub; + struct bt_mesh_app_keys *keys = NULL; + struct bt_mesh_app_key *key = NULL; + struct bt_mesh_subnet *sub = NULL; BT_DBG("net_idx 0x%04x app_idx %04x update %u val %s", net_idx, app_idx, update, bt_hex(val, 16)); @@ -448,8 +441,8 @@ static void app_key_add(struct bt_mesh_model *model, struct net_buf_simple *buf) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_STATUS, 4); - u16_t key_net_idx, key_app_idx; - u8_t status; + u16_t key_net_idx = 0U, key_app_idx = 0U; + u8_t status = 0U; key_idx_unpack(buf, &key_net_idx, &key_app_idx); @@ -483,8 +476,8 @@ static void app_key_update(struct bt_mesh_model *model, struct net_buf_simple *buf) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_STATUS, 4); - u16_t key_net_idx, key_app_idx; - u8_t status; + u16_t key_net_idx = 0U, key_app_idx = 0U; + u8_t status = 0U; key_idx_unpack(buf, &key_net_idx, &key_app_idx); @@ -546,9 +539,9 @@ static void app_key_del(struct bt_mesh_model *model, struct net_buf_simple *buf) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_STATUS, 4); - u16_t key_net_idx, key_app_idx; - struct bt_mesh_app_key *key; - u8_t status; + u16_t key_net_idx = 0U, key_app_idx = 0U; + struct bt_mesh_app_key *key = NULL; + u8_t status = 0U; key_idx_unpack(buf, &key_net_idx, &key_app_idx); @@ -605,8 +598,8 @@ static void app_key_get(struct bt_mesh_model *model, { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_LIST, 3 + IDX_LEN(CONFIG_BLE_MESH_APP_KEY_COUNT)); - u16_t get_idx, i, prev; - u8_t status; + u16_t get_idx = 0U, i = 0U, prev = 0U; + u8_t status = 0U; get_idx = net_buf_simple_pull_le16(buf); if (get_idx > 0xfff) { @@ -697,13 +690,11 @@ static void beacon_set(struct bt_mesh_model *model, bt_mesh_store_cfg(); } -#if CONFIG_BLE_MESH_NODE if (cfg->beacon) { bt_mesh_beacon_enable(); } else { bt_mesh_beacon_disable(); } -#endif } } else { BT_WARN("Invalid Config Beacon value 0x%02x", buf->data[0]); @@ -831,7 +822,6 @@ static void gatt_proxy_set(struct bt_mesh_model *model, bt_mesh_store_cfg(); } -#if CONFIG_BLE_MESH_NODE if (cfg->gatt_proxy == BLE_MESH_GATT_PROXY_DISABLED) { int i; @@ -855,7 +845,6 @@ static void gatt_proxy_set(struct bt_mesh_model *model, } bt_mesh_adv_update(); -#endif if (cfg->hb_pub.feat & BLE_MESH_FEAT_PROXY) { bt_mesh_heartbeat_send(); @@ -1028,11 +1017,11 @@ static void mod_pub_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u16_t elem_addr, pub_addr = 0U; - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u8_t *mod_id, status; - bool vnd; + u16_t elem_addr = 0U, pub_addr = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL, status = 0U; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -1075,12 +1064,12 @@ static void mod_pub_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u8_t retransmit, status, pub_ttl, pub_period, cred_flag; - u16_t elem_addr, pub_addr, pub_app_idx; - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u8_t *mod_id; - bool vnd; + u8_t retransmit = 0U, status = 0U, pub_ttl = 0U, pub_period = 0U, cred_flag = 0U; + u16_t elem_addr = 0U, pub_addr = 0U, pub_app_idx = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -1194,7 +1183,7 @@ static struct label *va_find(const u8_t *label_uuid, static u8_t va_add(u8_t *label_uuid, u16_t *addr) { - struct label *update, *free_slot = NULL; + struct label *update = NULL, *free_slot = NULL; update = va_find(label_uuid, &free_slot); if (update) { @@ -1221,7 +1210,7 @@ static u8_t va_add(u8_t *label_uuid, u16_t *addr) static u8_t va_del(u8_t *label_uuid, u16_t *addr) { - struct label *update; + struct label *update = NULL; update = va_find(label_uuid, NULL); if (update) { @@ -1244,8 +1233,8 @@ static u8_t va_del(u8_t *label_uuid, u16_t *addr) static size_t mod_sub_list_clear(struct bt_mesh_model *mod) { - u8_t *label_uuid; - size_t clear_count; + u8_t *label_uuid = NULL; + size_t clear_count = 0U; int i; /* Unref stored labels related to this model */ @@ -1278,13 +1267,13 @@ static void mod_pub_va_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u8_t retransmit, status, pub_ttl, pub_period, cred_flag; - u16_t elem_addr, pub_addr, pub_app_idx; - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u8_t *label_uuid; - u8_t *mod_id; - bool vnd; + u8_t retransmit = 0U, status = 0U, pub_ttl = 0U, pub_period = 0U, cred_flag = 0U; + u16_t elem_addr = 0U, pub_addr = 0U, pub_app_idx = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *label_uuid = NULL; + u8_t *mod_id = NULL; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -1342,7 +1331,7 @@ send_status: #else static size_t mod_sub_list_clear(struct bt_mesh_model *mod) { - size_t clear_count; + size_t clear_count = 0U; int i; /* Unref stored labels related to this model */ @@ -1360,11 +1349,11 @@ static void mod_pub_va_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u8_t *mod_id, status; - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u16_t elem_addr, pub_addr = 0U; - bool vnd; + u8_t *mod_id = NULL, status = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u16_t elem_addr = 0U, pub_addr = 0U; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -1436,12 +1425,12 @@ static void mod_sub_add(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u16_t elem_addr, sub_addr; - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u8_t *mod_id; - u8_t status; - bool vnd; + u16_t elem_addr = 0U, sub_addr = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; int i; elem_addr = net_buf_simple_pull_le16(buf); @@ -1522,13 +1511,13 @@ static void mod_sub_del(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u16_t elem_addr, sub_addr; - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u8_t *mod_id; - u16_t *match; - u8_t status; - bool vnd; + u16_t elem_addr = 0U, sub_addr = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL; + u16_t *match = NULL; + u8_t status = 0U; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -1598,12 +1587,12 @@ static void mod_sub_overwrite(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u16_t elem_addr, sub_addr; - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u8_t *mod_id; - u8_t status; - bool vnd; + u16_t elem_addr = 0U, sub_addr = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -1667,12 +1656,12 @@ static void mod_sub_del_all(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u16_t elem_addr; - u8_t *mod_id; - u8_t status; - bool vnd; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u16_t elem_addr = 0U; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -1721,9 +1710,9 @@ static void mod_sub_get(struct bt_mesh_model *model, { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_LIST, 5 + CONFIG_BLE_MESH_MODEL_GROUP_COUNT * 2); - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u16_t addr, id; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u16_t addr = 0U, id = 0U; int i; addr = net_buf_simple_pull_le16(buf); @@ -1777,9 +1766,9 @@ static void mod_sub_get_vnd(struct bt_mesh_model *model, { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_LIST_VND, 7 + CONFIG_BLE_MESH_MODEL_GROUP_COUNT * 2); - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u16_t company, addr, id; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u16_t company = 0U, addr = 0U, id = 0U; int i; addr = net_buf_simple_pull_le16(buf); @@ -1836,13 +1825,13 @@ static void mod_sub_va_add(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u16_t elem_addr, sub_addr; - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u8_t *label_uuid; - u8_t *mod_id; - u8_t status; - bool vnd; + u16_t elem_addr = 0U, sub_addr = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *label_uuid = NULL; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; int i; elem_addr = net_buf_simple_pull_le16(buf); @@ -1913,14 +1902,14 @@ static void mod_sub_va_del(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u16_t elem_addr, sub_addr; - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u8_t *label_uuid; - u8_t *mod_id; - u16_t *match; - u8_t status; - bool vnd; + u16_t elem_addr = 0U, sub_addr = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *label_uuid = NULL; + u8_t *mod_id = NULL; + u16_t *match = NULL; + u8_t status = 0U; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -1981,13 +1970,13 @@ static void mod_sub_va_overwrite(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u16_t elem_addr, sub_addr = BLE_MESH_ADDR_UNASSIGNED; - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u8_t *label_uuid; - u8_t *mod_id; - u8_t status; - bool vnd; + u16_t elem_addr = 0U, sub_addr = BLE_MESH_ADDR_UNASSIGNED; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *label_uuid = NULL; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -2047,12 +2036,12 @@ static void mod_sub_va_add(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u16_t elem_addr; - u8_t *mod_id; - u8_t status; - bool vnd; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u16_t elem_addr = 0U; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -2089,11 +2078,11 @@ static void mod_sub_va_del(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - struct bt_mesh_elem *elem; - u16_t elem_addr; - u8_t *mod_id; - u8_t status; - bool vnd; + struct bt_mesh_elem *elem = NULL; + u16_t elem_addr = 0U; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -2128,11 +2117,11 @@ static void mod_sub_va_overwrite(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - struct bt_mesh_elem *elem; - u16_t elem_addr; - u8_t *mod_id; - u8_t status; - bool vnd; + struct bt_mesh_elem *elem = NULL; + u16_t elem_addr = 0U; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -2184,9 +2173,9 @@ static void net_key_add(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - struct bt_mesh_subnet *sub; - u16_t idx; - int err; + struct bt_mesh_subnet *sub = NULL; + u16_t idx = 0U; + int err = 0; idx = net_buf_simple_pull_le16(buf); if (idx > 0xfff) { @@ -2216,7 +2205,7 @@ static void net_key_add(struct bt_mesh_model *model, /* Check for already existing subnet */ if (sub->net_idx == idx) { - u8_t status; + u8_t status = 0U; if (memcmp(buf->data, sub->keys[0].net, 16)) { status = STATUS_IDX_ALREADY_STORED; @@ -2246,10 +2235,8 @@ static void net_key_add(struct bt_mesh_model *model, if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { sub->node_id = BLE_MESH_NODE_IDENTITY_STOPPED; -#if CONFIG_BLE_MESH_NODE bt_mesh_proxy_beacon_send(sub); bt_mesh_adv_update(); -#endif } else { sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; } @@ -2267,9 +2254,9 @@ static void net_key_update(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - struct bt_mesh_subnet *sub; - u16_t idx; - int err; + struct bt_mesh_subnet *sub = NULL; + u16_t idx = 0U; + int err = 0; idx = net_buf_simple_pull_le16(buf); if (idx > 0xfff) { @@ -2354,9 +2341,9 @@ static void net_key_del(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - struct bt_mesh_subnet *sub; - u16_t del_idx; - u8_t status; + struct bt_mesh_subnet *sub = NULL; + u16_t del_idx = 0U; + u8_t status = 0U; del_idx = net_buf_simple_pull_le16(buf); if (del_idx > 0xfff) { @@ -2403,7 +2390,7 @@ static void net_key_get(struct bt_mesh_model *model, { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_LIST, IDX_LEN(CONFIG_BLE_MESH_SUBNET_COUNT)); - u16_t prev, i; + u16_t prev = 0U, i = 0U; bt_mesh_model_msg_init(&msg, OP_NET_KEY_LIST); @@ -2438,9 +2425,9 @@ static void node_identity_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_IDENTITY_STATUS, 4); - struct bt_mesh_subnet *sub; - u8_t node_id; - u16_t idx; + struct bt_mesh_subnet *sub = NULL; + u8_t node_id = 0U; + u16_t idx = 0U; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -2476,9 +2463,9 @@ static void node_identity_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_IDENTITY_STATUS, 4); - struct bt_mesh_subnet *sub; - u8_t node_id; - u16_t idx; + struct bt_mesh_subnet *sub = NULL; + u8_t node_id = 0U; + u16_t idx = 0U; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -2506,7 +2493,6 @@ static void node_identity_set(struct bt_mesh_model *model, } else { net_buf_simple_add_u8(&msg, STATUS_SUCCESS); net_buf_simple_add_le16(&msg, idx); -#if CONFIG_BLE_MESH_NODE /* Section 4.2.11.1: "When the GATT Proxy state is set to * 0x00, the Node Identity state for all subnets shall be set * to 0x00 and shall not be changed." @@ -2520,7 +2506,6 @@ static void node_identity_set(struct bt_mesh_model *model, } bt_mesh_adv_update(); } -#endif net_buf_simple_add_u8(&msg, sub->node_id); } @@ -2552,11 +2537,11 @@ static void mod_app_bind(struct bt_mesh_model *model, struct net_buf_simple *buf) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_STATUS, 9); - u16_t elem_addr, key_app_idx; - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u8_t *mod_id, status; - bool vnd; + u16_t elem_addr = 0U, key_app_idx = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL, status = 0U; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -2615,11 +2600,11 @@ static void mod_app_unbind(struct bt_mesh_model *model, struct net_buf_simple *buf) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_STATUS, 9); - u16_t elem_addr, key_app_idx; - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u8_t *mod_id, status; - bool vnd; + u16_t elem_addr = 0U, key_app_idx = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL, status = 0U; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -2677,11 +2662,11 @@ static void mod_app_get(struct bt_mesh_model *model, 9 + KEY_LIST_LEN), BLE_MESH_MODEL_BUF_LEN(OP_SIG_MOD_APP_LIST, 9 + KEY_LIST_LEN))); - struct bt_mesh_model *mod; - struct bt_mesh_elem *elem; - u8_t *mod_id, status; - u16_t elem_addr; - bool vnd; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL, status = 0U; + u16_t elem_addr = 0U; + bool vnd = false; elem_addr = net_buf_simple_pull_le16(buf); if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { @@ -2760,9 +2745,9 @@ static void node_reset(struct bt_mesh_model *model, BT_ERR("%s, Unable to send Config Node Reset Status", __func__); } -#if CONFIG_BLE_MESH_NODE - bt_mesh_reset(); -#endif + if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) { + bt_mesh_reset(); + } } static void send_friend_status(struct bt_mesh_model *model, @@ -2841,9 +2826,9 @@ static void lpn_timeout_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_LPN_TIMEOUT_STATUS, 5); - struct bt_mesh_friend *frnd; - u16_t lpn_addr; - s32_t timeout; + struct bt_mesh_friend *frnd = NULL; + u16_t lpn_addr = 0U; + s32_t timeout = 0; lpn_addr = net_buf_simple_pull_le16(buf); @@ -2901,8 +2886,8 @@ static void send_krp_status(struct bt_mesh_model *model, static void krp_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - struct bt_mesh_subnet *sub; - u16_t idx; + struct bt_mesh_subnet *sub = NULL; + u16_t idx = 0U; idx = net_buf_simple_pull_le16(buf); if (idx > 0xfff) { @@ -2924,9 +2909,9 @@ static void krp_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, static void krp_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - struct bt_mesh_subnet *sub; - u8_t phase; - u16_t idx; + struct bt_mesh_subnet *sub = NULL; + u8_t phase = 0U; + u16_t idx = 0U; idx = net_buf_simple_pull_le16(buf); phase = net_buf_simple_pull_u8(buf); @@ -3071,8 +3056,8 @@ static void heartbeat_pub_set(struct bt_mesh_model *model, { struct hb_pub_param *param = (void *)buf->data; struct bt_mesh_cfg_srv *cfg = model->user_data; - u16_t dst, feat, idx; - u8_t status; + u16_t dst = 0U, feat = 0U, idx = 0U; + u8_t status = 0U; BT_DBG("src 0x%04x", ctx->addr); @@ -3168,8 +3153,8 @@ static void hb_sub_send_status(struct bt_mesh_model *model, { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_SUB_STATUS, 9); struct bt_mesh_cfg_srv *cfg = model->user_data; - u16_t period; - s64_t uptime; + u16_t period = 0U; + s64_t uptime = 0; BT_DBG("src 0x%04x status 0x%02x", ctx->addr, status); @@ -3209,9 +3194,9 @@ static void heartbeat_sub_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_cfg_srv *cfg = model->user_data; - u16_t sub_src, sub_dst; - u8_t sub_period; - s32_t period_ms; + u16_t sub_src = 0U, sub_dst = 0U; + u8_t sub_period = 0U; + s32_t period_ms = 0; BT_DBG("src 0x%04x", ctx->addr); @@ -3343,8 +3328,8 @@ static void hb_publish(struct k_work *work) struct bt_mesh_cfg_srv *cfg = CONTAINER_OF(work, struct bt_mesh_cfg_srv, hb_pub.timer.work); - struct bt_mesh_subnet *sub; - u16_t period_ms; + struct bt_mesh_subnet *sub = NULL; + u16_t period_ms = 0U; BT_DBG("hb_pub.count: %u", cfg->hb_pub.count); @@ -3429,10 +3414,29 @@ int bt_mesh_cfg_srv_init(struct bt_mesh_model *model, bool primary) return 0; } +int bt_mesh_cfg_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + struct bt_mesh_cfg_srv *cfg = model->user_data; + + if (!cfg) { + BT_ERR("%s, No Configuration Server context provided", __func__); + return -EINVAL; + } + + bt_mesh_cfg_reset(); + + k_delayed_work_free(&cfg->hb_pub.timer); + cfg->hb_pub.dst = BLE_MESH_ADDR_UNASSIGNED; + + conf = NULL; + + return 0; +} + static void mod_reset(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, bool vnd, bool primary, void *user_data) { - size_t clear_count; + size_t clear_count = 0U; /* Clear model state that isn't otherwise cleared. E.g. AppKey * binding and model publication is cleared as a consequence diff --git a/components/bt/esp_ble_mesh/mesh_core/crypto.c b/components/bt/esp_ble_mesh/mesh_core/crypto.c index 7573e164f..48d3a6098 100644 --- a/components/bt/esp_ble_mesh/mesh_core/crypto.c +++ b/components/bt/esp_ble_mesh/mesh_core/crypto.c @@ -11,18 +11,11 @@ #include #include -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_CRYPTO) -#include "mesh_types.h" -#include "mesh_util.h" -#include "mesh_buf.h" -#include "mesh_trace.h" -#include "mesh_bearer_adapt.h" -#include "mesh_aes_encrypt.h" - -#include "mesh.h" #include "crypto.h" +#include "mesh_aes_encrypt.h" +#include "mesh_bearer_adapt.h" #define NET_MIC_LEN(pdu) (((pdu)[1] & 0x80) ? 8 : 4) #define APP_MIC_LEN(aszmic) ((aszmic) ? 8 : 4) @@ -30,8 +23,8 @@ int bt_mesh_aes_cmac(const u8_t key[16], struct bt_mesh_sg *sg, size_t sg_len, u8_t mac[16]) { - struct tc_aes_key_sched_struct sched; - struct tc_cmac_struct state; + struct tc_aes_key_sched_struct sched = {0}; + struct tc_cmac_struct state = {0}; if (tc_cmac_setup(&state, key, &sched) == TC_CRYPTO_FAIL) { return -EIO; @@ -54,7 +47,7 @@ int bt_mesh_aes_cmac(const u8_t key[16], struct bt_mesh_sg *sg, int bt_mesh_k1(const u8_t *ikm, size_t ikm_len, const u8_t salt[16], const char *info, u8_t okm[16]) { - int err; + int err = 0; err = bt_mesh_aes_cmac_one(salt, ikm, ikm_len, okm); if (err < 0) { @@ -67,12 +60,12 @@ int bt_mesh_k1(const u8_t *ikm, size_t ikm_len, const u8_t salt[16], int bt_mesh_k2(const u8_t n[16], const u8_t *p, size_t p_len, u8_t net_id[1], u8_t enc_key[16], u8_t priv_key[16]) { - struct bt_mesh_sg sg[3]; - u8_t salt[16]; - u8_t out[16]; - u8_t t[16]; - u8_t pad; - int err; + struct bt_mesh_sg sg[3] = {0}; + u8_t salt[16] = {0}; + u8_t out[16] = {0}; + u8_t t[16] = {0}; + u8_t pad = 0U; + int err = 0; BT_DBG("n %s", bt_hex(n, 16)); BT_DBG("p %s", bt_hex(p, p_len)); @@ -132,9 +125,9 @@ int bt_mesh_k2(const u8_t n[16], const u8_t *p, size_t p_len, int bt_mesh_k3(const u8_t n[16], u8_t out[8]) { u8_t id64[] = { 'i', 'd', '6', '4', 0x01 }; - u8_t tmp[16]; - u8_t t[16]; - int err; + u8_t tmp[16] = {0}; + u8_t t[16] = {0}; + int err = 0; err = bt_mesh_s1("smk3", tmp); if (err) { @@ -159,9 +152,9 @@ int bt_mesh_k3(const u8_t n[16], u8_t out[8]) int bt_mesh_k4(const u8_t n[16], u8_t out[1]) { u8_t id6[] = { 'i', 'd', '6', 0x01 }; - u8_t tmp[16]; - u8_t t[16]; - int err; + u8_t tmp[16] = {0}; + u8_t t[16] = {0}; + int err = 0; err = bt_mesh_s1("smk4", tmp); if (err) { @@ -186,8 +179,8 @@ int bt_mesh_k4(const u8_t n[16], u8_t out[1]) int bt_mesh_id128(const u8_t n[16], const char *s, u8_t out[16]) { const char *id128 = "id128\x01"; - u8_t salt[16]; - int err; + u8_t salt[16] = {0}; + int err = 0; err = bt_mesh_s1(s, salt); if (err) { @@ -202,10 +195,11 @@ static int bt_mesh_ccm_decrypt(const u8_t key[16], u8_t nonce[13], const u8_t *aad, size_t aad_len, u8_t *out_msg, size_t mic_size) { - u8_t msg[16], pmsg[16], cmic[16], cmsg[16], Xn[16], mic[16]; - u16_t last_blk, blk_cnt; - size_t i, j; - int err; + u8_t msg[16] = {0}, pmsg[16] = {0}, cmic[16] = {0}, + cmsg[16] = {0}, Xn[16] = {0}, mic[16] = {0}; + u16_t last_blk = 0U, blk_cnt = 0U; + size_t i = 0U, j = 0U; + int err = 0; if (msg_len < 1 || aad_len >= 0xff00) { return -EINVAL; @@ -360,10 +354,11 @@ static int bt_mesh_ccm_encrypt(const u8_t key[16], u8_t nonce[13], const u8_t *aad, size_t aad_len, u8_t *out_msg, size_t mic_size) { - u8_t pmsg[16], cmic[16], cmsg[16], mic[16], Xn[16]; - u16_t blk_cnt, last_blk; - size_t i, j; - int err; + u8_t pmsg[16] = {0}, cmic[16] = {0}, cmsg[16] = {0}, + mic[16] = {0}, Xn[16] = {0}; + u16_t blk_cnt = 0U, last_blk = 0U; + size_t i = 0U, j = 0U; + int err = 0; BT_DBG("key %s", bt_hex(key, 16)); BT_DBG("nonce %s", bt_hex(nonce, 13)); @@ -573,8 +568,8 @@ int bt_mesh_net_obfuscate(u8_t *pdu, u32_t iv_index, const u8_t privacy_key[16]) { u8_t priv_rand[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, }; - u8_t tmp[16]; - int err, i; + u8_t tmp[16] = {0}; + int err = 0, i; BT_DBG("IVIndex %u, PrivacyKey %s", iv_index, bt_hex(privacy_key, 16)); @@ -599,8 +594,8 @@ int bt_mesh_net_encrypt(const u8_t key[16], struct net_buf_simple *buf, u32_t iv_index, bool proxy) { u8_t mic_len = NET_MIC_LEN(buf->data); - u8_t nonce[13]; - int err; + u8_t nonce[13] = {0}; + int err = 0; BT_DBG("IVIndex %u EncKey %s mic_len %u", iv_index, bt_hex(key, 16), mic_len); @@ -631,7 +626,7 @@ int bt_mesh_net_decrypt(const u8_t key[16], struct net_buf_simple *buf, u32_t iv_index, bool proxy) { u8_t mic_len = NET_MIC_LEN(buf->data); - u8_t nonce[13]; + u8_t nonce[13] = {0}; BT_DBG("PDU (%u bytes) %s", buf->len, bt_hex(buf->data, buf->len)); BT_DBG("iv_index %u, key %s mic_len %u", iv_index, bt_hex(key, 16), @@ -677,8 +672,8 @@ int bt_mesh_app_encrypt(const u8_t key[16], bool dev_key, u8_t aszmic, struct net_buf_simple *buf, const u8_t *ad, u16_t src, u16_t dst, u32_t seq_num, u32_t iv_index) { - u8_t nonce[13]; - int err; + u8_t nonce[13] = {0}; + int err = 0; BT_DBG("AppKey %s", bt_hex(key, 16)); BT_DBG("dev_key %u src 0x%04x dst 0x%04x", dev_key, src, dst); @@ -704,8 +699,8 @@ int bt_mesh_app_decrypt(const u8_t key[16], bool dev_key, u8_t aszmic, const u8_t *ad, u16_t src, u16_t dst, u32_t seq_num, u32_t iv_index) { - u8_t nonce[13]; - int err; + u8_t nonce[13] = {0}; + int err = 0; BT_DBG("EncData (len %u) %s", buf->len, bt_hex(buf->data, buf->len)); @@ -794,9 +789,9 @@ bool bt_mesh_fcs_check(struct net_buf_simple *buf, u8_t received_fcs) int bt_mesh_virtual_addr(const u8_t virtual_label[16], u16_t *addr) { - u8_t salt[16]; - u8_t tmp[16]; - int err; + u8_t salt[16] = {0}; + u8_t tmp[16] = {0}; + int err = 0; err = bt_mesh_s1("vtad", salt); if (err) { @@ -856,8 +851,8 @@ int bt_mesh_beacon_auth(const u8_t beacon_key[16], u8_t flags, const u8_t net_id[8], u32_t iv_index, u8_t auth[8]) { - u8_t msg[13], tmp[16]; - int err; + u8_t msg[13] = {0}, tmp[16] = {0}; + int err = 0; BT_DBG("BeaconKey %s", bt_hex(beacon_key, 16)); BT_DBG("NetId %s", bt_hex(net_id, 8)); diff --git a/components/bt/esp_ble_mesh/mesh_core/crypto.h b/components/bt/esp_ble_mesh/mesh_core/crypto.h index d8e9b1452..df98c0e95 100644 --- a/components/bt/esp_ble_mesh/mesh_core/crypto.h +++ b/components/bt/esp_ble_mesh/mesh_core/crypto.h @@ -10,8 +10,8 @@ #ifndef _CRYPTO_H_ #define _CRYPTO_H_ -#include "mesh_types.h" #include +#include "mesh_buf.h" struct bt_mesh_sg { const void *data; diff --git a/components/bt/esp_ble_mesh/mesh_core/foundation.h b/components/bt/esp_ble_mesh/mesh_core/foundation.h index 19bc96dd7..5e1148144 100644 --- a/components/bt/esp_ble_mesh/mesh_core/foundation.h +++ b/components/bt/esp_ble_mesh/mesh_core/foundation.h @@ -8,7 +8,6 @@ #ifndef _FOUNDATION_H_ #define _FOUNDATION_H_ -#include "mesh_access.h" #include "net.h" #define OP_APP_KEY_ADD BLE_MESH_MODEL_OP_1(0x00) @@ -131,9 +130,15 @@ struct label { int bt_mesh_cfg_srv_init(struct bt_mesh_model *model, bool primary); int bt_mesh_health_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_cfg_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_health_srv_deinit(struct bt_mesh_model *model, bool primary); + int bt_mesh_cfg_cli_init(struct bt_mesh_model *model, bool primary); int bt_mesh_health_cli_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_cfg_cli_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_health_cli_deinit(struct bt_mesh_model *model, bool primary); + void bt_mesh_cfg_reset(void); void bt_mesh_heartbeat(u16_t src, u16_t dst, u8_t hops, u16_t feat); diff --git a/components/bt/esp_ble_mesh/mesh_core/friend.c b/components/bt/esp_ble_mesh/mesh_core/friend.c index 894e88cf5..02649b21f 100644 --- a/components/bt/esp_ble_mesh/mesh_core/friend.c +++ b/components/bt/esp_ble_mesh/mesh_core/friend.c @@ -10,23 +10,16 @@ #include #include -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_FRIEND) -#include "mesh_buf.h" -#include "mesh_util.h" -#include "mesh_main.h" -#include "mesh_trace.h" - #include "crypto.h" #include "adv.h" #include "mesh.h" -#include "net.h" #include "transport.h" #include "access.h" -#include "foundation.h" #include "friend.h" -#include "client_common.h" +#include "foundation.h" +#include "mesh_main.h" #include "provisioner_main.h" #ifdef CONFIG_BLE_MESH_FRIEND @@ -81,7 +74,7 @@ static struct bt_mesh_subnet *friend_subnet_get(u16_t net_idx) if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { sub = bt_mesh_subnet_get(net_idx); } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { - sub = provisioner_subnet_get(net_idx); + sub = bt_mesh_provisioner_subnet_get(net_idx); } return sub; @@ -135,7 +128,7 @@ struct bt_mesh_friend *bt_mesh_friend_find(u16_t net_idx, u16_t lpn_addr, static void purge_buffers(sys_slist_t *list) { while (!sys_slist_is_empty(list)) { - struct net_buf *buf; + struct net_buf *buf = NULL; buf = (void *)sys_slist_get_not_empty(list); @@ -248,15 +241,15 @@ void bt_mesh_friend_sec_update(u16_t net_idx) int bt_mesh_friend_clear(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) { struct bt_mesh_ctl_friend_clear *msg = (void *)buf->data; - struct bt_mesh_friend *frnd; - u16_t lpn_addr, lpn_counter; + struct bt_mesh_friend *frnd = NULL; + u16_t lpn_addr = 0U, lpn_counter = 0U; struct bt_mesh_net_tx tx = { .sub = rx->sub, .ctx = &rx->ctx, .src = bt_mesh_primary_addr(), .xmit = bt_mesh_net_transmit_get(), }; - struct bt_mesh_ctl_friend_clear_confirm cfm; + struct bt_mesh_ctl_friend_clear_confirm cfm = {0}; if (buf->len < sizeof(*msg)) { BT_WARN("%s, Too short Friend Clear", __func__); @@ -329,7 +322,7 @@ static struct net_buf *create_friend_pdu(struct bt_mesh_friend *frnd, struct friend_pdu_info *info, struct net_buf_simple *sdu) { - struct net_buf *buf; + struct net_buf *buf = NULL; buf = bt_mesh_adv_create_from_pool(&friend_buf_pool, adv_alloc, BLE_MESH_ADV_DATA, @@ -370,8 +363,8 @@ static int unseg_app_sdu_unpack(struct bt_mesh_friend *frnd, struct unseg_app_sdu_meta *meta) { u16_t app_idx = FRIEND_ADV(buf)->app_idx; - u8_t role; - int err; + u8_t role = 0U; + int err = 0; meta->subnet = friend_subnet_get(frnd->net_idx); if (!meta->subnet) { @@ -408,7 +401,7 @@ static int unseg_app_sdu_decrypt(struct bt_mesh_friend *frnd, struct net_buf *buf, const struct unseg_app_sdu_meta *meta) { - struct net_buf_simple sdu; + struct net_buf_simple sdu = {0}; net_buf_simple_clone(&buf->b, &sdu); net_buf_simple_pull(&sdu, 10); @@ -424,7 +417,7 @@ static int unseg_app_sdu_encrypt(struct bt_mesh_friend *frnd, struct net_buf *buf, const struct unseg_app_sdu_meta *meta) { - struct net_buf_simple sdu; + struct net_buf_simple sdu = {0}; net_buf_simple_clone(&buf->b, &sdu); net_buf_simple_pull(&sdu, 10); @@ -439,8 +432,8 @@ static int unseg_app_sdu_encrypt(struct bt_mesh_friend *frnd, static int unseg_app_sdu_prepare(struct bt_mesh_friend *frnd, struct net_buf *buf) { - struct unseg_app_sdu_meta meta; - int err; + struct unseg_app_sdu_meta meta = {0}; + int err = 0; if (FRIEND_ADV(buf)->app_idx == BLE_MESH_KEY_UNUSED) { return 0; @@ -476,11 +469,11 @@ static int encrypt_friend_pdu(struct bt_mesh_friend *frnd, struct net_buf *buf, bool master_cred) { struct bt_mesh_subnet *sub = friend_subnet_get(frnd->net_idx); - const u8_t *enc, *priv; - u32_t iv_index; - u16_t src; - u8_t nid; - int err; + const u8_t *enc = NULL, *priv = NULL; + u32_t iv_index = 0U; + u16_t src = 0U; + u8_t nid = 0U; + int err = 0; if (!sub) { BT_ERR("Invalid subnet to encrypt friend pdu"); @@ -540,7 +533,7 @@ static struct net_buf *encode_friend_ctl(struct bt_mesh_friend *frnd, u8_t ctl_op, struct net_buf_simple *sdu) { - struct friend_pdu_info info; + struct friend_pdu_info info = {0}; BT_DBG("LPN 0x%04x", frnd->lpn); @@ -561,7 +554,7 @@ static struct net_buf *encode_friend_ctl(struct bt_mesh_friend *frnd, static struct net_buf *encode_update(struct bt_mesh_friend *frnd, u8_t md) { - struct bt_mesh_ctl_friend_update *upd; + struct bt_mesh_ctl_friend_update *upd = NULL; NET_BUF_SIMPLE_DEFINE(sdu, 1 + sizeof(*upd)); struct bt_mesh_subnet *sub = friend_subnet_get(frnd->net_idx); @@ -581,9 +574,9 @@ static struct net_buf *encode_update(struct bt_mesh_friend *frnd, u8_t md) static void enqueue_sub_cfm(struct bt_mesh_friend *frnd, u8_t xact) { - struct bt_mesh_ctl_friend_sub_confirm *cfm; + struct bt_mesh_ctl_friend_sub_confirm *cfm = NULL; NET_BUF_SIMPLE_DEFINE(sdu, 1 + sizeof(*cfm)); - struct net_buf *buf; + struct net_buf *buf = NULL; BT_DBG("lpn 0x%04x xact 0x%02x", frnd->lpn, xact); @@ -615,14 +608,14 @@ static void friend_recv_delay(struct bt_mesh_friend *frnd) { frnd->pending_req = 1U; k_delayed_work_submit(&frnd->timer, recv_delay(frnd)); - BT_DBG("Waiting RecvDelay of %d ms", recv_delay(frnd)); + BT_INFO("Waiting RecvDelay of %d ms", recv_delay(frnd)); } int bt_mesh_friend_sub_add(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) { - struct bt_mesh_friend *frnd; - u8_t xact; + struct bt_mesh_friend *frnd = NULL; + u8_t xact = 0U; if (buf->len < BLE_MESH_FRIEND_SUB_MIN_LEN) { BT_WARN("%s, Too short Friend Subscription Add", __func__); @@ -656,8 +649,8 @@ int bt_mesh_friend_sub_add(struct bt_mesh_net_rx *rx, int bt_mesh_friend_sub_rem(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) { - struct bt_mesh_friend *frnd; - u8_t xact; + struct bt_mesh_friend *frnd = NULL; + u8_t xact = 0U; if (buf->len < BLE_MESH_FRIEND_SUB_MIN_LEN) { BT_WARN("%s, Too short Friend Subscription Remove", __func__); @@ -696,7 +689,7 @@ static void enqueue_buf(struct bt_mesh_friend *frnd, struct net_buf *buf) static void enqueue_update(struct bt_mesh_friend *frnd, u8_t md) { - struct net_buf *buf; + struct net_buf *buf = NULL; buf = encode_update(frnd, md); if (!buf) { @@ -711,7 +704,7 @@ static void enqueue_update(struct bt_mesh_friend *frnd, u8_t md) int bt_mesh_friend_poll(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) { struct bt_mesh_ctl_friend_poll *msg = (void *)buf->data; - struct bt_mesh_friend *frnd; + struct bt_mesh_friend *frnd = NULL; if (buf->len < sizeof(*msg)) { BT_WARN("%s, Too short Friend Poll", __func__); @@ -739,7 +732,7 @@ int bt_mesh_friend_poll(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) friend_recv_delay(frnd); if (!frnd->established) { - BT_DBG("Friendship established with 0x%04x", frnd->lpn); + BT_INFO("Friendship established with 0x%04x", frnd->lpn); frnd->established = 1U; if (friend_cb) { friend_cb(true, frnd->lpn, 0); @@ -828,7 +821,7 @@ static void clear_timeout(struct k_work *work) { struct bt_mesh_friend *frnd = CONTAINER_OF(work, struct bt_mesh_friend, clear.timer.work); - u32_t duration; + u32_t duration = 0U; BT_DBG("LPN 0x%04x (old) Friend 0x%04x", frnd->lpn, frnd->clear.frnd); @@ -856,8 +849,8 @@ int bt_mesh_friend_clear_cfm(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) { struct bt_mesh_ctl_friend_clear_confirm *msg = (void *)buf->data; - struct bt_mesh_friend *frnd; - u16_t lpn_addr, lpn_counter; + struct bt_mesh_friend *frnd = NULL; + u16_t lpn_addr = 0U, lpn_counter = 0U; BT_DBG("%s", __func__); @@ -894,9 +887,9 @@ int bt_mesh_friend_clear_cfm(struct bt_mesh_net_rx *rx, static void enqueue_offer(struct bt_mesh_friend *frnd, s8_t rssi) { - struct bt_mesh_ctl_friend_offer *off; + struct bt_mesh_ctl_friend_offer *off = NULL; NET_BUF_SIMPLE_DEFINE(sdu, 1 + sizeof(*off)); - struct net_buf *buf; + struct net_buf *buf = NULL; BT_DBG("%s", __func__); @@ -942,9 +935,9 @@ static s32_t offer_delay(struct bt_mesh_friend *frnd, s8_t rssi, u8_t crit) * want to avoid floating-point arithmetic. */ static const u8_t fact[] = { 10, 15, 20, 25 }; - s32_t delay; + s32_t delay = 0; - BT_DBG("ReceiveWindowFactor %u ReceiveWindow %u RSSIFactor %u RSSI %d", + BT_INFO("ReceiveWindowFactor %u ReceiveWindow %u RSSIFactor %u RSSI %d", fact[RECV_WIN_FACT(crit)], RECV_WIN, fact[RSSI_FACT(crit)], rssi); @@ -966,7 +959,7 @@ int bt_mesh_friend_req(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) { struct bt_mesh_ctl_friend_req *msg = (void *)buf->data; struct bt_mesh_friend *frnd = NULL; - u32_t poll_to; + u32_t poll_to = 0U; int i; if (buf->len < sizeof(*msg)) { @@ -1039,7 +1032,7 @@ init_friend: frnd->lpn_counter = sys_be16_to_cpu(msg->lpn_counter); frnd->clear.frnd = sys_be16_to_cpu(msg->prev_addr); - BT_DBG("LPN 0x%04x rssi %d recv_delay %u poll_to %ums", + BT_INFO("LPN 0x%04x rssi %d recv_delay %u poll_to %ums", frnd->lpn, rx->ctx.recv_rssi, frnd->recv_delay, frnd->poll_to); /** @@ -1069,9 +1062,9 @@ init_friend: static bool is_seg(struct bt_mesh_friend_seg *seg, u16_t src, u16_t seq_zero) { struct net_buf *buf = (void *)sys_slist_peek_head(&seg->queue); - struct net_buf_simple_state state; - u16_t buf_seq_zero; - u16_t buf_src; + struct net_buf_simple_state state = {0}; + u16_t buf_seq_zero = 0U; + u16_t buf_src = 0U; if (!buf) { return false; @@ -1118,7 +1111,7 @@ static void enqueue_friend_pdu(struct bt_mesh_friend *frnd, u16_t src, u8_t seg_count, struct net_buf *buf) { - struct bt_mesh_friend_seg *seg; + struct bt_mesh_friend_seg *seg = NULL; BT_DBG("type %u", type); @@ -1272,9 +1265,30 @@ int bt_mesh_friend_init(void) return 0; } +int bt_mesh_friend_deinit(void) +{ + int i; + + bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + + frnd->net_idx = BLE_MESH_KEY_UNUSED; + + k_delayed_work_free(&frnd->timer); + k_delayed_work_free(&frnd->clear.timer); + } + + bt_mesh_unref_buf_from_pool(&friend_buf_pool); + memset(adv_pool, 0, sizeof(adv_pool)); + + return 0; +} + static bool is_segack(struct net_buf *buf, u64_t *seqauth, u16_t src) { - struct net_buf_simple_state state; + struct net_buf_simple_state state = {0}; bool found = false; if (buf->len != 16) { @@ -1311,7 +1325,7 @@ end: static void friend_purge_old_ack(struct bt_mesh_friend *frnd, u64_t *seq_auth, u16_t src) { - sys_snode_t *cur, *prev = NULL; + sys_snode_t *cur = NULL, *prev = NULL; BT_DBG("SeqAuth %llx src 0x%04x", *seq_auth, src); @@ -1339,8 +1353,8 @@ static void friend_lpn_enqueue_rx(struct bt_mesh_friend *frnd, u64_t *seq_auth, u8_t seg_count, struct net_buf_simple *sbuf) { - struct friend_pdu_info info; - struct net_buf *buf; + struct friend_pdu_info info = {0}; + struct net_buf *buf = NULL; /* Because of network loopback, tx packets will also be passed into * this rx function. These packets have already been added to the @@ -1391,8 +1405,8 @@ static void friend_lpn_enqueue_tx(struct bt_mesh_friend *frnd, u64_t *seq_auth, u8_t seg_count, struct net_buf_simple *sbuf) { - struct friend_pdu_info info; - struct net_buf *buf; + struct friend_pdu_info info = {0}; + struct net_buf *buf = NULL; BT_DBG("LPN 0x%04x", frnd->lpn); @@ -1545,8 +1559,8 @@ bool bt_mesh_friend_queue_has_space(u16_t net_idx, u16_t src, u16_t dst, static bool friend_queue_prepare_space(struct bt_mesh_friend *frnd, u16_t addr, u64_t *seq_auth, u8_t seg_count) { - bool pending_segments; - u8_t avail_space; + bool pending_segments = false; + u8_t avail_space = 0U; if (!friend_queue_has_space(frnd, addr, seq_auth, seg_count)) { return false; @@ -1683,7 +1697,7 @@ void bt_mesh_friend_clear_incomplete(struct bt_mesh_subnet *sub, u16_t src, void bt_mesh_friend_remove_lpn(u16_t lpn_addr) { - struct bt_mesh_friend *frnd; + struct bt_mesh_friend *frnd = NULL; frnd = bt_mesh_friend_find(BLE_MESH_KEY_ANY, lpn_addr, false, false); if (frnd) { diff --git a/components/bt/esp_ble_mesh/mesh_core/friend.h b/components/bt/esp_ble_mesh/mesh_core/friend.h index 8c1fbe57b..6193e0924 100644 --- a/components/bt/esp_ble_mesh/mesh_core/friend.h +++ b/components/bt/esp_ble_mesh/mesh_core/friend.h @@ -9,6 +9,8 @@ #ifndef _FRIEND_H_ #define _FRIEND_H_ +#include "net.h" + enum bt_mesh_friend_pdu_type { BLE_MESH_FRIEND_PDU_SINGLE, BLE_MESH_FRIEND_PDU_PARTIAL, @@ -50,6 +52,7 @@ int bt_mesh_friend_sub_rem(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf); int bt_mesh_friend_init(void); +int bt_mesh_friend_deinit(void); void bt_mesh_friend_remove_lpn(u16_t lpn_addr); diff --git a/components/bt/esp_ble_mesh/mesh_core/health_cli.c b/components/bt/esp_ble_mesh/mesh_core/health_cli.c index ba4b73575..75a025bfd 100644 --- a/components/bt/esp_ble_mesh/mesh_core/health_cli.c +++ b/components/bt/esp_ble_mesh/mesh_core/health_cli.c @@ -9,20 +9,13 @@ #include #include -#include -#include "osi/allocator.h" -#include "osi/mutex.h" -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_MODEL) -#include "mesh_types.h" -#include "mesh_util.h" -#include "mesh_trace.h" -#include "health_cli.h" - #include "foundation.h" #include "mesh_common.h" +#include "health_cli.h" + #include "btc_ble_mesh_health_model.h" s32_t health_msg_timeout; @@ -39,32 +32,36 @@ static const bt_mesh_client_op_pair_t health_op_pair[] = { { OP_ATTENTION_SET, OP_ATTENTION_STATUS }, }; -static osi_mutex_t health_client_mutex; +static bt_mesh_mutex_t health_client_lock; static void bt_mesh_health_client_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&health_client_mutex); - init = true; + if (!health_client_lock.mutex) { + bt_mesh_mutex_create(&health_client_lock); } } +static void bt_mesh_health_client_mutex_free(void) +{ + bt_mesh_mutex_free(&health_client_lock); +} + static void bt_mesh_health_client_lock(void) { - osi_mutex_lock(&health_client_mutex, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_mutex_lock(&health_client_lock); } static void bt_mesh_health_client_unlock(void) { - osi_mutex_unlock(&health_client_mutex); + bt_mesh_mutex_unlock(&health_client_lock); } static void timeout_handler(struct k_work *work) { struct k_delayed_work *timer = NULL; bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; + u32_t opcode = 0U; BT_WARN("Receive health status message timeout"); @@ -75,10 +72,11 @@ static void timeout_handler(struct k_work *work) if (timer && !k_delayed_work_free(timer)) { node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); if (node) { - bt_mesh_health_client_cb_evt_to_btc(node->opcode, - BTC_BLE_MESH_EVT_HEALTH_CLIENT_TIMEOUT, node->ctx.model, &node->ctx, NULL, 0); - // Don't forget to release the node at the end. + memcpy(&ctx, &node->ctx, sizeof(ctx)); + opcode = node->opcode; bt_mesh_client_free_node(node); + bt_mesh_health_client_cb_evt_to_btc( + opcode, BTC_BLE_MESH_EVT_HEALTH_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0); } } @@ -127,10 +125,10 @@ static void health_client_cancel(struct bt_mesh_model *model, } if (!k_delayed_work_free(&node->timer)) { - bt_mesh_health_client_cb_evt_to_btc( - node->opcode, evt_type, model, ctx, (const u8_t *)status, len); - // Don't forget to release the node at the end. + u32_t opcode = node->opcode; bt_mesh_client_free_node(node); + bt_mesh_health_client_cb_evt_to_btc( + opcode, evt_type, model, ctx, (const u8_t *)status, len); } } @@ -176,8 +174,8 @@ static void health_current_status(struct bt_mesh_model *model, struct net_buf_simple *buf) { bt_mesh_client_node_t *node = NULL; - u8_t test_id; - u16_t cid; + u8_t test_id = 0U; + u16_t cid = 0U; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -193,13 +191,16 @@ static void health_current_status(struct bt_mesh_model *model, BT_DBG("Test ID 0x%02x Company ID 0x%04x Fault Count %u", test_id, cid, buf->len); + + ((void) test_id); + ((void) cid); } static void health_period_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u8_t status = 0; + u8_t status = 0U; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -214,7 +215,7 @@ static void health_attention_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u8_t status = 0; + u8_t status = 0U; BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, @@ -236,7 +237,7 @@ const struct bt_mesh_model_op bt_mesh_health_cli_op[] = { int bt_mesh_health_attention_get(struct bt_mesh_msg_ctx *ctx) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_ATTENTION_GET, 0); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -258,8 +259,8 @@ int bt_mesh_health_attention_set(struct bt_mesh_msg_ctx *ctx, u8_t attention, bool need_ack) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_ATTENTION_SET, 1); - u32_t opcode; - int err; + u32_t opcode = 0U; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -286,7 +287,7 @@ int bt_mesh_health_attention_set(struct bt_mesh_msg_ctx *ctx, int bt_mesh_health_period_get(struct bt_mesh_msg_ctx *ctx) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_PERIOD_GET, 0); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -308,8 +309,8 @@ int bt_mesh_health_period_set(struct bt_mesh_msg_ctx *ctx, u8_t divisor, bool need_ack) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_PERIOD_SET, 1); - u32_t opcode; - int err; + u32_t opcode = 0U; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -337,8 +338,8 @@ int bt_mesh_health_fault_test(struct bt_mesh_msg_ctx *ctx, u16_t cid, u8_t test_id, bool need_ack) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_FAULT_TEST, 3); - u32_t opcode; - int err; + u32_t opcode = 0U; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -367,8 +368,8 @@ int bt_mesh_health_fault_clear(struct bt_mesh_msg_ctx *ctx, u16_t cid, bool need_ack) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_FAULT_CLEAR, 2); - u32_t opcode; - int err; + u32_t opcode = 0U; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -395,7 +396,7 @@ int bt_mesh_health_fault_clear(struct bt_mesh_msg_ctx *ctx, int bt_mesh_health_fault_get(struct bt_mesh_msg_ctx *ctx, u16_t cid) { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_FAULT_GET, 2); - int err; + int err = 0; if (!ctx || !ctx->addr) { return -EINVAL; @@ -455,7 +456,7 @@ int bt_mesh_health_cli_init(struct bt_mesh_model *model, bool primary) } if (!client->internal_data) { - internal = osi_calloc(sizeof(health_internal_data_t)); + internal = bt_mesh_calloc(sizeof(health_internal_data_t)); if (!internal) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; @@ -480,3 +481,36 @@ int bt_mesh_health_cli_init(struct bt_mesh_model *model, bool primary) return 0; } + +int bt_mesh_health_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + bt_mesh_health_client_t *client = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_health_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, No Health Client context provided", __func__); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + client->internal_data = NULL; + } + + bt_mesh_health_client_mutex_free(); + + if (health_cli) { + health_cli = NULL; + } + + return 0; +} diff --git a/components/bt/esp_ble_mesh/mesh_core/health_srv.c b/components/bt/esp_ble_mesh/mesh_core/health_srv.c index 6b039b6f4..fc1d0366c 100644 --- a/components/bt/esp_ble_mesh/mesh_core/health_srv.c +++ b/components/bt/esp_ble_mesh/mesh_core/health_srv.c @@ -9,23 +9,13 @@ #include #include -#include -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_MODEL) -#include "mesh_types.h" -#include "mesh_util.h" -#include "mesh_trace.h" -#include "health_srv.h" - -#include "mesh.h" -#include "adv.h" -#include "net.h" -#include "transport.h" #include "access.h" #include "foundation.h" #include "mesh_common.h" +#include "health_srv.h" #include "btc_ble_mesh_health_model.h" @@ -49,8 +39,8 @@ struct bt_mesh_health_srv *health_srv; static u8_t health_get_curr_fault_count(struct bt_mesh_model *model) { struct bt_mesh_health_srv *srv = model->user_data; - u8_t count = 0; - size_t i; + u8_t count = 0U; + size_t i = 0U; for (i = 0U; i < ARRAY_SIZE(srv->test.curr_faults); i++) { if (srv->test.curr_faults[i] != HEALTH_NO_FAULT) { @@ -66,8 +56,8 @@ static void health_get_fault_value(struct bt_mesh_model *model, bool current) { struct bt_mesh_health_srv *srv = model->user_data; - size_t array_size; - size_t i; + size_t array_size = 0U; + size_t i = 0U; array_size = current ? ARRAY_SIZE(srv->test.curr_faults) : ARRAY_SIZE(srv->test.reg_faults); @@ -86,9 +76,9 @@ static void health_get_fault_value(struct bt_mesh_model *model, static bool health_is_test_id_exist(struct bt_mesh_model *model, u8_t test_id) { struct bt_mesh_health_srv *srv = model->user_data; - u8_t i; + int i; - for (i = 0U; i < srv->test.id_count; i++) { + for (i = 0; i < srv->test.id_count; i++) { if (srv->test.test_ids[i] == test_id) { return true; } @@ -102,7 +92,7 @@ static int health_send_fault_status(struct bt_mesh_model *model, { struct bt_mesh_health_srv *srv = model->user_data; struct net_buf_simple *msg = NULL; - int err; + int err = 0; msg = bt_mesh_alloc_buf(4 + ARRAY_SIZE(srv->test.reg_faults) + 4); if (!msg) { @@ -135,7 +125,7 @@ static void health_fault_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_health_srv *srv = model->user_data; - u16_t company_id; + u16_t company_id = 0U; if (!srv) { BT_ERR("%s, No Health Server context provided", __func__); @@ -158,7 +148,7 @@ static void health_fault_clear(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_health_srv *srv = model->user_data; - u16_t company_id; + u16_t company_id = 0U; if (!srv) { BT_ERR("%s, No Health Server context provided", __func__); @@ -189,8 +179,8 @@ static void health_fault_test(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_health_srv *srv = model->user_data; - u16_t company_id; - u8_t test_id; + u16_t company_id = 0U; + u8_t test_id = 0U; BT_DBG("%s", __func__); @@ -229,7 +219,7 @@ static void send_attention_status(struct bt_mesh_model *model, { BLE_MESH_MODEL_BUF_DEFINE(msg, OP_ATTENTION_STATUS, 1); struct bt_mesh_health_srv *srv = model->user_data; - u8_t time; + u8_t time = 0U; if (!srv) { BT_ERR("%s, No Health Server context provided", __func__); @@ -260,7 +250,7 @@ static void health_set_attention(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u8_t time; + u8_t time = 0U; time = net_buf_simple_pull_u8(buf); @@ -308,7 +298,7 @@ static void health_set_period(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - u8_t period; + u8_t period = 0U; period = net_buf_simple_pull_u8(buf); if (period > 15) { @@ -375,7 +365,7 @@ static size_t health_get_current(struct bt_mesh_model *model, static int health_pub_update(struct bt_mesh_model *model) { struct bt_mesh_model_pub *pub = model->pub; - size_t count; + size_t count = 0U; BT_DBG("%s", __func__); @@ -396,7 +386,7 @@ static int health_pub_update(struct bt_mesh_model *model) int bt_mesh_fault_update(struct bt_mesh_elem *elem) { - struct bt_mesh_model *model; + struct bt_mesh_model *model = NULL; model = bt_mesh_model_find(elem, BLE_MESH_MODEL_ID_HEALTH_SRV); if (!model) { @@ -484,9 +474,45 @@ int bt_mesh_health_srv_init(struct bt_mesh_model *model, bool primary) return 0; } +int bt_mesh_health_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + struct bt_mesh_health_srv *srv = model->user_data; + + if (!srv) { + if (!primary) { + /* If Health Server is in the secondary element with NULL user_data. */ + return 0; + } + + BT_ERR("%s, No Health Server context provided", __func__); + return -EINVAL; + } + + if (srv->test.id_count == 0 || !srv->test.test_ids) { + BT_ERR("%s, No Health Test ID provided", __func__); + return -EINVAL; + } + + if (!model->pub) { + BT_ERR("%s, Health Server has no publication support", __func__); + return -EINVAL; + } + + model->pub->addr = BLE_MESH_ADDR_UNASSIGNED; + model->pub->update = NULL; + + k_delayed_work_free(&srv->attn_timer); + + if (primary) { + health_srv = NULL; + } + + return 0; +} + void bt_mesh_attention(struct bt_mesh_model *model, u8_t time) { - struct bt_mesh_health_srv *srv; + struct bt_mesh_health_srv *srv = NULL; if (!model) { srv = health_srv; diff --git a/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h b/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h index 5bf8b2032..4d4adc6bc 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h @@ -11,8 +11,6 @@ #ifndef _BLE_MESH_CFG_CLI_H_ #define _BLE_MESH_CFG_CLI_H_ -#include "mesh_access.h" -#include "mesh_kernel.h" #include "client_common.h" /** diff --git a/components/bt/esp_ble_mesh/mesh_core/include/cfg_srv.h b/components/bt/esp_ble_mesh/mesh_core/include/cfg_srv.h index 91f07fa4b..69b60c2d1 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/cfg_srv.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/cfg_srv.h @@ -11,7 +11,6 @@ #define _BLE_MESH_CFG_SRV_H_ #include "mesh_access.h" -#include "mesh_kernel.h" /** * @brief Bluetooth Mesh diff --git a/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h b/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h index 4f080766c..b2127c57a 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h @@ -11,8 +11,6 @@ #ifndef _BLE_MESH_HEALTH_CLI_H_ #define _BLE_MESH_HEALTH_CLI_H_ -#include "mesh_access.h" -#include "mesh_kernel.h" #include "client_common.h" /** diff --git a/components/bt/esp_ble_mesh/mesh_core/include/health_srv.h b/components/bt/esp_ble_mesh/mesh_core/include/health_srv.h index 0aa262153..71ccabef7 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/health_srv.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/health_srv.h @@ -11,7 +11,6 @@ #define _BLE_MESH_HEALTH_SRV_H_ #include "mesh_access.h" -#include "mesh_kernel.h" /** * @brief Bluetooth Mesh Health Server Model diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h index 8c387a681..19ba51562 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h @@ -11,11 +11,10 @@ #ifndef _BLE_MESH_ACCESS_H_ #define _BLE_MESH_ACCESS_H_ -#include #include "mesh_types.h" #include "mesh_util.h" #include "mesh_buf.h" -#include "sdkconfig.h" +#include "mesh_kernel.h" /** * @brief Bluetooth Mesh Access Layer diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h index 563d070fe..ddff69ce1 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h @@ -9,31 +9,33 @@ #ifndef _BLE_MESH_BEARER_ADRPT_H_ #define _BLE_MESH_BEARER_ADRPT_H_ -#include +#include #include "mesh_types.h" #include "mesh_util.h" -#include "mesh_buf.h" #include "mesh_uuid.h" +#include "mesh_buf.h" /* BLE Mesh Max Connection Count */ #ifdef CONFIG_BT_BLUEDROID_ENABLED #define BLE_MESH_MAX_CONN \ MIN(CONFIG_BT_ACL_CONNECTIONS, CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN) -#define ADV_TASK_CORE TASK_PINNED_TO_CORE +#define BLE_MESH_ADV_TASK_CORE TASK_PINNED_TO_CORE #endif #ifdef CONFIG_BT_NIMBLE_ENABLED #define BLE_MESH_MAX_CONN CONFIG_BT_NIMBLE_MAX_CONNECTIONS #ifdef CONFIG_BT_NIMBLE_PINNED_TO_CORE -#define ADV_TASK_CORE (CONFIG_BT_NIMBLE_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BT_NIMBLE_PINNED_TO_CORE : tskNO_AFFINITY) +#define BLE_MESH_ADV_TASK_CORE (CONFIG_BT_NIMBLE_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BT_NIMBLE_PINNED_TO_CORE : tskNO_AFFINITY) #else -#define ADV_TASK_CORE (0) +#define BLE_MESH_ADV_TASK_CORE (0) #endif #endif +#define BLE_MESH_ADV_TASK_STACK_SIZE 3072 + #define BLE_MESH_GAP_ADV_MAX_LEN 31 #define BLE_MESH_GATT_DEF_MTU_SIZE 23 @@ -640,7 +642,8 @@ struct bt_mesh_gatt_attr { .attr_count = ARRAY_SIZE(_attrs), \ } -esp_err_t bt_mesh_host_init(void); +int bt_mesh_host_init(void); +int bt_mesh_host_deinit(void); int bt_le_adv_start(const struct bt_mesh_adv_param *param, const struct bt_mesh_adv_data *ad, size_t ad_len, @@ -653,10 +656,12 @@ int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t c int bt_le_scan_stop(void); void bt_mesh_gatts_conn_cb_register(struct bt_mesh_conn_cb *cb); +void bt_mesh_gatts_conn_cb_deregister(void); int bt_mesh_gatts_disconnect(struct bt_mesh_conn *conn, u8_t reason); int bt_mesh_gatts_service_register(struct bt_mesh_gatt_service *svc); +int bt_mesh_gatts_service_deregister(struct bt_mesh_gatt_service *svc); int bt_mesh_gatts_service_unregister(struct bt_mesh_gatt_service *svc); @@ -688,6 +693,7 @@ int bt_mesh_gatts_service_start(struct bt_mesh_gatt_service *svc); int bt_mesh_gatts_set_local_device_name(const char *name); void bt_mesh_gattc_conn_cb_register(struct bt_mesh_prov_conn_cb *cb); +void bt_mesh_gattc_conn_cb_deregister(void); u8_t bt_mesh_gattc_get_free_conn_count(void); @@ -711,6 +717,7 @@ struct bt_mesh_conn *bt_mesh_conn_ref(struct bt_mesh_conn *conn); void bt_mesh_conn_unref(struct bt_mesh_conn *conn); void bt_mesh_gatt_init(void); +void bt_mesh_gatt_deinit(void); void bt_mesh_adapt_init(void); diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_hci.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_hci.h index 1c3e093e0..75ff8ccf3 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/mesh_hci.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_hci.h @@ -7,9 +7,8 @@ #ifndef _BLE_MESH_HCI_H_ #define _BLE_MESH_HCI_H_ -#include "mesh_kernel.h" -#include "mesh_bearer_adapt.h" #include "mesh_atomic.h" +#include "mesh_bearer_adapt.h" #ifdef __cplusplus extern "C" { diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h index 08f7908ef..688c3a8a6 100644 --- a/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h @@ -11,7 +11,6 @@ #ifndef _BLE_MESH_MAIN_H_ #define _BLE_MESH_MAIN_H_ -#include "mesh_util.h" #include "mesh_access.h" /** @@ -295,12 +294,19 @@ struct bt_mesh_prov { * @param element_num Provisioned device element number. * @param netkey_idx Provisioned device assigned netkey index. */ - void (*prov_complete)(int node_idx, const u8_t device_uuid[16], + void (*prov_complete)(u16_t node_idx, const u8_t device_uuid[16], u16_t unicast_addr, u8_t element_num, u16_t netkey_idx); #endif /* CONFIG_BLE_MESH_PROVISIONER */ }; +enum ble_mesh_role { + NODE = 0, + PROVISIONER, + FAST_PROV, + ROLE_NVAL, +}; + /* The following APIs are for BLE Mesh Node */ /** @brief Provide provisioning input OOB string. @@ -345,12 +351,6 @@ int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers); */ int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers); -/** @brief Indicate whether provisioner is enabled - * - * @return true - enabled, false - disabled. - */ -bool bt_mesh_is_provisioner_en(void); - /* The following API is for BLE Mesh Fast Provisioning */ /** @brief Change the device action @@ -386,6 +386,14 @@ int bt_mesh_prov_input_string(const char *str); */ int bt_mesh_prov_input_number(u32_t num); +/** @brief Enable Provisioner corresponding functionalities, e.g. scan, etc. + * + * @param bearers Bit-wise OR of provisioning bearers. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers); + /** @brief Enable specific provisioning bearers * * Enable one or more provisioning bearers. @@ -463,6 +471,19 @@ int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers); int bt_mesh_init(const struct bt_mesh_prov *prov, const struct bt_mesh_comp *comp); +/* BLE Mesh deinit parameters */ +struct bt_mesh_deinit_param { + bool erase; /* Indicate if erasing flash when deinit mesh stack */ +}; + +/** @brief De-initialize Mesh support + * + * @param param BLE Mesh deinit parameters. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_deinit(struct bt_mesh_deinit_param *param); + /** @brief Reset the state of the local Mesh node. * * Resets the state of the node, which means that it needs to be @@ -515,6 +536,13 @@ int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx, u8_t flags, u32_t iv_index, u16_t addr, const u8_t dev_key[16]); +/** @brief Check if the device is an unprovisioned device + * and will act as a node once provisioned. + * + * @return true - yes, false - no. + */ +bool bt_mesh_is_node(void); + /** @brief Check if the local node has been provisioned. * * This API can be used to check if the local node has been provisioned @@ -526,6 +554,18 @@ int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx, */ bool bt_mesh_is_provisioned(void); +/** @brief Check if the device is a Provisioner. + * + * @return true - yes, false - no. + */ +bool bt_mesh_is_provisioner(void); + +/** @brief Check if the Provisioner is enabled + * + * @return true - enabled, false - disabled. + */ +bool bt_mesh_is_provisioner_en(void); + /** @brief Toggle the IV Update test mode * * This API is only available if the IV Update test mode has been enabled diff --git a/components/bt/esp_ble_mesh/mesh_core/lpn.c b/components/bt/esp_ble_mesh/mesh_core/lpn.c index a99b28dc4..69d428ad4 100644 --- a/components/bt/esp_ble_mesh/mesh_core/lpn.c +++ b/components/bt/esp_ble_mesh/mesh_core/lpn.c @@ -9,23 +9,17 @@ #include #include -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_LOW_POWER) -#include "mesh_buf.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" -#include "mesh_main.h" - #include "crypto.h" #include "adv.h" #include "mesh.h" -#include "net.h" #include "transport.h" #include "access.h" #include "beacon.h" -#include "foundation.h" #include "lpn.h" +#include "foundation.h" +#include "mesh_main.h" #include "cfg_srv.h" #ifdef CONFIG_BLE_MESH_LOW_POWER @@ -77,6 +71,7 @@ static void (*lpn_cb)(u16_t friend_addr, bool established); +#if !CONFIG_BLE_MESH_NO_LOG static const char *state2str(int state) { switch (state) { @@ -104,6 +99,7 @@ static const char *state2str(int state) return "(unknown)"; } } +#endif static inline void lpn_set_state(int state) { @@ -394,7 +390,7 @@ static int send_friend_poll(void) }; struct bt_mesh_lpn *lpn = &bt_mesh.lpn; u8_t fsn = lpn->fsn; - int err; + int err = 0; BT_DBG("lpn->sent_req 0x%02x", lpn->sent_req); @@ -513,9 +509,9 @@ int bt_mesh_lpn_friend_offer(struct bt_mesh_net_rx *rx, struct bt_mesh_ctl_friend_offer *msg = (void *)buf->data; struct bt_mesh_lpn *lpn = &bt_mesh.lpn; struct bt_mesh_subnet *sub = rx->sub; - struct friend_cred *cred; - u16_t frnd_counter; - int err; + struct friend_cred *cred = NULL; + u16_t frnd_counter = 0U; + int err = 0; if (buf->len < sizeof(*msg)) { BT_WARN("Too short Friend Offer"); @@ -534,7 +530,7 @@ int bt_mesh_lpn_friend_offer(struct bt_mesh_net_rx *rx, frnd_counter = sys_be16_to_cpu(msg->frnd_counter); - BT_DBG("recv_win %u queue_size %u sub_list_size %u rssi %d counter %u", + BT_INFO("recv_win %u queue_size %u sub_list_size %u rssi %d counter %u", msg->recv_win, msg->queue_size, msg->sub_list_size, msg->rssi, frnd_counter); @@ -576,7 +572,7 @@ int bt_mesh_lpn_friend_clear_cfm(struct bt_mesh_net_rx *rx, { struct bt_mesh_ctl_friend_clear_confirm *msg = (void *)buf->data; struct bt_mesh_lpn *lpn = &bt_mesh.lpn; - u16_t addr, counter; + u16_t addr = 0U, counter = 0U; if (buf->len < sizeof(*msg)) { BT_WARN("Too short Friend Clear Confirm"); @@ -678,8 +674,8 @@ static bool sub_update(u8_t op) .xmit = POLL_XMIT, .friend_cred = true, }; - struct bt_mesh_ctl_friend_sub req; - size_t i, g; + struct bt_mesh_ctl_friend_sub req = {0}; + size_t i = 0U, g = 0U; BT_DBG("op 0x%02x sent_req 0x%02x", op, lpn->sent_req); @@ -687,7 +683,7 @@ static bool sub_update(u8_t op) return false; } - for (i = 0, g = 0; i < ARRAY_SIZE(lpn->groups); i++) { + for (i = 0U, g = 0U; i < ARRAY_SIZE(lpn->groups); i++) { if (lpn->groups[i] == BLE_MESH_ADDR_UNASSIGNED) { continue; } @@ -715,7 +711,7 @@ static bool sub_update(u8_t op) } } - if (g == 0) { + if (g == 0U) { group_zero(lpn->pending); return false; } @@ -963,7 +959,7 @@ int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx, struct bt_mesh_ctl_friend_update *msg = (void *)buf->data; struct bt_mesh_lpn *lpn = &bt_mesh.lpn; struct bt_mesh_subnet *sub = rx->sub; - u32_t iv_index; + u32_t iv_index = 0U; if (buf->len < sizeof(*msg)) { BT_WARN("Too short Friend Update"); @@ -1020,7 +1016,7 @@ int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx, iv_index = sys_be32_to_cpu(msg->iv_index); - BT_DBG("flags 0x%02x iv_index 0x%08x md %u", msg->flags, iv_index, + BT_INFO("flags 0x%02x iv_index 0x%08x md %u", msg->flags, iv_index, msg->md); if (bt_mesh_kr_update(sub, BLE_MESH_KEY_REFRESH(msg->flags), @@ -1094,4 +1090,15 @@ int bt_mesh_lpn_init(void) return 0; } +int bt_mesh_lpn_deinit(void) +{ + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + + bt_mesh_lpn_disable(true); + + k_delayed_work_free(&lpn->timer); + + return 0; +} + #endif /* CONFIG_BLE_MESH_LOW_POWER */ diff --git a/components/bt/esp_ble_mesh/mesh_core/lpn.h b/components/bt/esp_ble_mesh/mesh_core/lpn.h index ad870e99e..45573661a 100644 --- a/components/bt/esp_ble_mesh/mesh_core/lpn.h +++ b/components/bt/esp_ble_mesh/mesh_core/lpn.h @@ -9,6 +9,8 @@ #ifndef _LPN_H_ #define _LPN_H_ +#include "net.h" + int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf); int bt_mesh_lpn_friend_offer(struct bt_mesh_net_rx *rx, @@ -63,5 +65,6 @@ void bt_mesh_lpn_group_del(u16_t *groups, size_t group_count); void bt_mesh_lpn_disable(bool force); int bt_mesh_lpn_init(void); +int bt_mesh_lpn_deinit(void); #endif /* _LPN_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/main.c b/components/bt/esp_ble_mesh/mesh_core/main.c index 90b00e635..dcec933db 100644 --- a/components/bt/esp_ble_mesh/mesh_core/main.c +++ b/components/bt/esp_ble_mesh/mesh_core/main.c @@ -7,52 +7,43 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include -#include #include +#include -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG) -#include "mesh_buf.h" -#include "mesh_trace.h" -#include "mesh_main.h" -#include "mesh_hci.h" - #include "adv.h" #include "prov.h" -#include "net.h" #include "beacon.h" #include "lpn.h" #include "friend.h" #include "transport.h" #include "access.h" #include "foundation.h" -#include "proxy_server.h" #include "settings.h" #include "mesh.h" -#include "provisioner_prov.h" +#include "mesh_hci.h" +#include "mesh_common.h" #include "proxy_client.h" +#include "proxy_server.h" +#include "provisioner_prov.h" #include "provisioner_main.h" -static volatile bool provisioner_en = false; - #define ACTION_ENTER 0x01 #define ACTION_SUSPEND 0x02 #define ACTION_EXIT 0x03 -#if CONFIG_BLE_MESH_NODE - int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx, u8_t flags, u32_t iv_index, u16_t addr, const u8_t dev_key[16]) { - bool pb_gatt_enabled; - int err; + bool pb_gatt_enabled = false; + int err = 0; BT_INFO("Primary Element: 0x%04x", addr); - BT_DBG("net_idx 0x%04x flags 0x%02x iv_index 0x%04x", + BT_INFO("net_idx 0x%04x flags 0x%02x iv_index 0x%04x", net_idx, flags, iv_index); + BT_INFO("dev_key %s", bt_hex(dev_key, 16)); if (bt_mesh_atomic_test_and_set_bit(bt_mesh.flags, BLE_MESH_VALID)) { return -EALREADY; @@ -142,14 +133,41 @@ void bt_mesh_reset(void) bt_mesh_comp_unprovision(); + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_role(); + } + if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) { bt_mesh_prov_reset(); } } +bool bt_mesh_is_node(void) +{ + return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_NODE); +} + bool bt_mesh_is_provisioned(void) { - return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID); + if (bt_mesh_is_node()) { + return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID); + } else { + return false; + } +} + +bool bt_mesh_is_provisioner(void) +{ + return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_PROVISIONER); +} + +bool bt_mesh_is_provisioner_en(void) +{ + if (bt_mesh_is_provisioner()) { + return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID_PROV); + } else { + return false; + } } int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers) @@ -158,6 +176,12 @@ int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers) return -EALREADY; } + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_role(); + } + if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && (bearers & BLE_MESH_PROV_ADV)) { /* Make sure we're scanning for provisioning inviations */ @@ -181,6 +205,12 @@ int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers) return -EALREADY; } + bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_NODE); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_role(); + } + if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && (bearers & BLE_MESH_PROV_ADV)) { bt_mesh_beacon_disable(); @@ -195,8 +225,6 @@ int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers) return 0; } -#endif /* CONFIG_BLE_MESH_NODE */ - static void model_suspend(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, bool vnd, bool primary, void *user_data) { @@ -208,7 +236,7 @@ static void model_suspend(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, int bt_mesh_suspend(void) { - int err; + int err = 0; if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) { return -EINVAL; @@ -250,7 +278,7 @@ static void model_resume(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, int bt_mesh_resume(void) { - int err; + int err = 0; if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) { return -EINVAL; @@ -279,7 +307,7 @@ int bt_mesh_resume(void) int bt_mesh_init(const struct bt_mesh_prov *prov, const struct bt_mesh_comp *comp) { - int err; + int err = 0; bt_mesh_k_init(); @@ -292,101 +320,167 @@ int bt_mesh_init(const struct bt_mesh_prov *prov, return err; } - bt_mesh_gatt_init(); - -#if CONFIG_BLE_MESH_NODE - extern struct bt_mesh_gatt_service proxy_svc; - if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { - bt_mesh_gatts_service_register(&proxy_svc); + if (IS_ENABLED(CONFIG_BLE_MESH_PROXY)) { + bt_mesh_gatt_init(); } - extern struct bt_mesh_gatt_service prov_svc; - if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { - bt_mesh_gatts_service_register(&prov_svc); - } -#endif - if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) { -#if CONFIG_BLE_MESH_NODE - err = bt_mesh_prov_init(prov); - if (err) { - return err; + if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) { + err = bt_mesh_prov_init(prov); + if (err) { + return err; + } } -#endif -#if CONFIG_BLE_MESH_PROVISIONER - err = provisioner_prov_init(prov); - if (err) { - return err; + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) { + err = bt_mesh_provisioner_prov_init(prov); + if (err) { + return err; + } } -#endif } bt_mesh_net_init(); bt_mesh_trans_init(); -#if CONFIG_BLE_MESH_NODE - /* Changed by Espressif, add random delay (0 ~ 3s) */ -#if defined(CONFIG_BLE_MESH_FAST_PROV) - u32_t delay = 0; - bt_mesh_rand(&delay, sizeof(u32_t)); - vTaskDelay((delay % 3000) / portTICK_PERIOD_MS); -#endif + /* Changed by Espressif, add a random delay (0 ~ 3s) */ + if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) { + u32_t delay = 0; + bt_mesh_rand(&delay, sizeof(u32_t)); + vTaskDelay((delay % 3000) / portTICK_PERIOD_MS); + } + bt_mesh_beacon_init(); -#endif bt_mesh_adv_init(); if (IS_ENABLED(CONFIG_BLE_MESH_PROXY)) { -#if CONFIG_BLE_MESH_NODE - bt_mesh_proxy_init(); -#endif -#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT - bt_mesh_proxy_prov_client_init(); -#endif + if ((IS_ENABLED(CONFIG_BLE_MESH_NODE) && + IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) || + IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { + bt_mesh_proxy_init(); + } + if ((IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && + IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) || + IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)) { + bt_mesh_proxy_prov_client_init(); + } } -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - /* If node & provisioner are both enabled and the - * device starts as a node, it must finish provisioning */ - err = provisioner_upper_init(); - if (err) { - return err; + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) { + bt_mesh_provisioner_init(); } -#endif -#if defined(CONFIG_BLE_MESH_SETTINGS) if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { bt_mesh_settings_init(); } -#endif return 0; } -bool bt_mesh_is_provisioner_en(void) +int bt_mesh_deinit(struct bt_mesh_deinit_param *param) { - return provisioner_en; -} + int err = 0; -/* The following APIs are for fast provisioning */ - -#if CONFIG_BLE_MESH_PROVISIONER -int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers) -{ - int err; - - if (bt_mesh_is_provisioner_en()) { - BT_WARN("%s, Provisioner is already enabled", __func__); - return -EALREADY; + if (param == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; } - err = provisioner_upper_init(); + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { + if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV)) { + bt_mesh_beacon_disable(); + bt_mesh_scan_disable(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { + bt_mesh_proxy_prov_disable(true); + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { + bt_mesh_provisioner_pb_gatt_disable(); + } + + bt_mesh_scan_disable(); + bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID_PROV); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) { + err = bt_mesh_prov_deinit(); + if (err) { + return err; + } + } + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) { + err = bt_mesh_provisioner_prov_deinit(param->erase); + if (err) { + return err; + } + } + } + + bt_mesh_trans_deinit(param->erase); + bt_mesh_net_deinit(param->erase); + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) { + bt_mesh_beacon_deinit(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PROXY)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) { + bt_mesh_proxy_deinit(); + } + } + + if ((IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && + IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) || + IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)) { + bt_mesh_proxy_prov_client_deinit(); + } + + bt_mesh_gatt_deinit(); + + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) { + err = bt_mesh_provisioner_deinit(param->erase); + if (err) { + return err; + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + bt_mesh_friend_deinit(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_deinit(); + } + + bt_mesh_adv_deinit(); + + err = bt_mesh_comp_deregister(); if (err) { - BT_ERR("%s, provisioner_upper_init fail", __func__); return err; } + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + if (param->erase) { + bt_mesh_clear_role(); + } + bt_mesh_settings_deinit(); + } + + bt_mesh_k_deinit(); + + return 0; +} + +#if defined(CONFIG_BLE_MESH_PROVISIONER) +int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers) +{ + bt_mesh_provisioner_set_prov_bearer(bearers, false); + #if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && (bearers & BLE_MESH_PROV_ADV)) { @@ -410,42 +504,107 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers) if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && (bearers & BLE_MESH_PROV_GATT)) { - provisioner_pb_gatt_enable(); + bt_mesh_provisioner_pb_gatt_enable(); + } + + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID_PROV); + + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { + bt_mesh_beacon_enable(); } if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { bt_mesh_friend_init(); } - provisioner_en = true; - return 0; } +int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers) +{ + int err = 0; + + if (bt_mesh_is_provisioner_en()) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } + + err = bt_mesh_provisioner_set_prov_info(); + if (err) { + BT_ERR("%s, Failed to set provisioning info", __func__); + return err; + } + + err = bt_mesh_provisioner_net_create(); + if (err) { + BT_ERR("%s, Failed to create network", __func__); + return err; + } + + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_PROVISIONER); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_role(); + } + + return bt_mesh_provisioner_net_start(bearers); +} + int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers) { + bt_mesh_prov_bearer_t enable = 0U; + if (!bt_mesh_is_provisioner_en()) { - BT_WARN("%s, Provisioner is already disabled", __func__); + BT_WARN("%s, Already", __func__); return -EALREADY; } + enable = bt_mesh_provisioner_get_prov_bearer(); + if (!(enable & bearers)) { + BT_ERR("%s, Bearers mismatch", __func__); + return -EINVAL; + } + + bt_mesh_provisioner_set_prov_bearer(bearers, true); + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + (enable & BLE_MESH_PROV_GATT) && (bearers & BLE_MESH_PROV_GATT)) { - provisioner_pb_gatt_disable(); + bt_mesh_provisioner_pb_gatt_disable(); +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_REMOVE, + BLE_MESH_EXCEP_INFO_MESH_PROV_ADV, NULL); +#endif } - if ((IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && - (bearers & BLE_MESH_PROV_ADV)) && - (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && - (bearers & BLE_MESH_PROV_GATT))) { + if (!(enable & (~bearers))) { + /* Provisioner is disabled completely, disable scan here */ bt_mesh_scan_disable(); - } - if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { - bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY); - } +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && + (enable & BLE_MESH_PROV_ADV)) { + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_REMOVE, + BLE_MESH_EXCEP_INFO_MESH_BEACON, NULL); + } +#endif - provisioner_en = false; + /* Clear corresponding flags */ + bt_mesh_atomic_and(bt_mesh.flags, ~(BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV))); + + /* When Provisioner is disabled, the device role indicated by bt_mesh.flags + * will not be cleared, because when Provisioner is restarted after disabled, + * its previous information can be recovered from flash properly. + */ + + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { + bt_mesh_beacon_disable(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY); + } + } return 0; } @@ -460,9 +619,9 @@ u8_t bt_mesh_set_fast_prov_action(u8_t action) return 0x01; } - if ((!provisioner_en && (action == ACTION_SUSPEND || action == ACTION_EXIT)) || - (provisioner_en && (action == ACTION_ENTER))) { - BT_WARN("%s, Action is already done", __func__); + if ((!bt_mesh_is_provisioner_en() && (action == ACTION_SUSPEND || action == ACTION_EXIT)) || + (bt_mesh_is_provisioner_en() && (action == ACTION_ENTER))) { + BT_WARN("%s, Already", __func__); return 0x0; } @@ -481,13 +640,14 @@ u8_t bt_mesh_set_fast_prov_action(u8_t action) bt_mesh_beacon_disable(); } if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { - provisioner_pb_gatt_enable(); + bt_mesh_provisioner_pb_gatt_enable(); } - provisioner_set_fast_prov_flag(true); - provisioner_en = true; + bt_mesh_provisioner_set_prov_bearer(BLE_MESH_PROV_ADV, false); + bt_mesh_provisioner_fast_prov_enable(true); + bt_mesh_atomic_or(bt_mesh.flags, BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV)); } else { if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { - provisioner_pb_gatt_disable(); + bt_mesh_provisioner_pb_gatt_disable(); } if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { bt_mesh_beacon_enable(); @@ -500,11 +660,10 @@ u8_t bt_mesh_set_fast_prov_action(u8_t action) bt_mesh_adv_update(); } #endif - provisioner_set_fast_prov_flag(false); - provisioner_en = false; + bt_mesh_atomic_and(bt_mesh.flags, ~(BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV))); + bt_mesh_provisioner_fast_prov_enable(false); if (action == ACTION_EXIT) { - provisioner_upper_reset_all_nodes(); - provisioner_prov_reset_all_nodes(); + bt_mesh_provisioner_remove_node(NULL); } } diff --git a/components/bt/esp_ble_mesh/mesh_core/mesh.h b/components/bt/esp_ble_mesh/mesh_core/mesh.h index c536184aa..75c89584d 100644 --- a/components/bt/esp_ble_mesh/mesh_core/mesh.h +++ b/components/bt/esp_ble_mesh/mesh_core/mesh.h @@ -9,6 +9,8 @@ #ifndef _MESH_H_ #define _MESH_H_ +#include "net.h" + #define BLE_MESH_KEY_PRIMARY 0x0000 #define BLE_MESH_KEY_ANY 0xffff diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c index d021d333c..a06d62e5a 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.c +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -11,30 +11,22 @@ #include #include -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_NET) -#include "mesh_util.h" -#include "mesh_buf.h" -#include "mesh_main.h" -#include "mesh_trace.h" -#include "mesh.h" - #include "crypto.h" #include "adv.h" #include "mesh.h" -#include "net.h" #include "lpn.h" #include "friend.h" -#include "proxy_server.h" #include "transport.h" #include "access.h" #include "foundation.h" #include "beacon.h" #include "settings.h" #include "prov.h" -#include "provisioner_main.h" #include "proxy_client.h" +#include "proxy_server.h" +#include "provisioner_main.h" /* Minimum valid Mesh Network PDU length. The Network headers * themselves take up 9 bytes. After that there is a minumum of 1 byte @@ -97,7 +89,7 @@ static int dup_cache_next; static bool check_dup(struct net_buf_simple *data) { const u8_t *tail = net_buf_simple_tail(data); - u32_t val; + u32_t val = 0U; int i; val = sys_get_be32(tail - 4) ^ sys_get_be32(tail - 8); @@ -116,7 +108,7 @@ static bool check_dup(struct net_buf_simple *data) static u64_t msg_hash(struct bt_mesh_net_rx *rx, struct net_buf_simple *pdu) { - u32_t hash1, hash2; + u32_t hash1 = 0U, hash2 = 0U; /* Three least significant bytes of IVI + first byte of SEQ */ hash1 = (BLE_MESH_NET_IVI_RX(rx) << 8) | pdu->data[2]; @@ -131,9 +123,9 @@ static bool msg_cache_match(struct bt_mesh_net_rx *rx, struct net_buf_simple *pdu) { u64_t hash = msg_hash(rx, pdu); - u16_t i; + int i; - for (i = 0U; i < ARRAY_SIZE(msg_cache); i++) { + for (i = 0; i < ARRAY_SIZE(msg_cache); i++) { if (msg_cache[i] == hash) { return true; } @@ -147,6 +139,21 @@ static bool msg_cache_match(struct bt_mesh_net_rx *rx, return false; } +#if CONFIG_BLE_MESH_PROVISIONER +void bt_mesh_msg_cache_clear(u16_t unicast_addr, u8_t elem_num) +{ + u16_t src = 0U; + int i; + + for (i = 0; i < ARRAY_SIZE(msg_cache); i++) { + src = (((u8_t)(msg_cache[i] >> 16)) << 8) | (u8_t)(msg_cache[i] >> 24); + if (src >= unicast_addr && src < unicast_addr + elem_num) { + memset(&msg_cache[i], 0x0, sizeof(msg_cache[i])); + } + } +} +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + struct bt_mesh_subnet *bt_mesh_subnet_get(u16_t net_idx) { int i; @@ -168,8 +175,8 @@ int bt_mesh_net_keys_create(struct bt_mesh_subnet_keys *keys, const u8_t key[16]) { u8_t p[] = { 0 }; - u8_t nid; - int err; + u8_t nid = 0U; + int err = 0; err = bt_mesh_k2(key, p, sizeof(p), &nid, keys->enc, keys->privacy); if (err) { @@ -217,9 +224,9 @@ int bt_mesh_net_keys_create(struct bt_mesh_subnet_keys *keys, defined(CONFIG_BLE_MESH_FRIEND)) int friend_cred_set(struct friend_cred *cred, u8_t idx, const u8_t net_key[16]) { - u16_t lpn_addr, frnd_addr; - int err; - u8_t p[9]; + u16_t lpn_addr = 0U, frnd_addr = 0U; + int err = 0; + u8_t p[9] = {0}; #if defined(CONFIG_BLE_MESH_LOW_POWER) if (cred->addr == bt_mesh.lpn.frnd) { @@ -275,7 +282,7 @@ void friend_cred_refresh(u16_t net_idx) int friend_cred_update(struct bt_mesh_subnet *sub) { - int err, i; + int err = 0, i; BT_DBG("net_idx 0x%04x", sub->net_idx); @@ -299,8 +306,8 @@ int friend_cred_update(struct bt_mesh_subnet *sub) struct friend_cred *friend_cred_create(struct bt_mesh_subnet *sub, u16_t addr, u16_t lpn_counter, u16_t frnd_counter) { - struct friend_cred *cred; - int i, err; + struct friend_cred *cred = NULL; + int i, err = 0; BT_DBG("net_idx 0x%04x addr 0x%04x", sub->net_idx, addr); @@ -426,7 +433,7 @@ u8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub) int bt_mesh_net_beacon_update(struct bt_mesh_subnet *sub) { u8_t flags = bt_mesh_net_flags(sub); - struct bt_mesh_subnet_keys *keys; + struct bt_mesh_subnet_keys *keys = NULL; if (sub->kr_flag) { BT_DBG("NetIndex %u Using new key", sub->net_idx); @@ -445,8 +452,8 @@ int bt_mesh_net_beacon_update(struct bt_mesh_subnet *sub) int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16], u32_t iv_index) { - struct bt_mesh_subnet *sub; - int err; + struct bt_mesh_subnet *sub = NULL; + int err = 0; BT_DBG("idx %u flags 0x%02x iv_index %u", idx, flags, iv_index); @@ -527,7 +534,7 @@ bool bt_mesh_kr_update(struct bt_mesh_subnet *sub, u8_t new_kr, bool new_key) if (sub->kr_flag) { if (sub->kr_phase == BLE_MESH_KR_PHASE_1) { - BT_DBG("Phase 1 -> Phase 2"); + BT_INFO("Phase 1 -> Phase 2"); sub->kr_phase = BLE_MESH_KR_PHASE_2; return true; } @@ -546,7 +553,7 @@ bool bt_mesh_kr_update(struct bt_mesh_subnet *sub, u8_t new_kr, bool new_key) * Intentional fall-through. */ case BLE_MESH_KR_PHASE_2: - BT_DBG("KR Phase 0x%02x -> Normal", sub->kr_phase); + BT_INFO("KR Phase 0x%02x -> Normal", sub->kr_phase); bt_mesh_net_revoke_keys(sub); if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) || IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { @@ -616,9 +623,7 @@ void bt_mesh_net_sec_update(struct bt_mesh_subnet *sub) if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED) { -#if CONFIG_BLE_MESH_NODE bt_mesh_proxy_beacon_send(sub); -#endif } } @@ -696,12 +701,12 @@ do_update: if (iv_update) { bt_mesh.iv_index = iv_index; - BT_DBG("IV Update state entered. New index 0x%08x", + BT_INFO("IV Update state entered. New index 0x%08x", bt_mesh.iv_index); bt_mesh_rpl_reset(); } else { - BT_DBG("Normal mode entered"); + BT_INFO("Normal mode entered"); bt_mesh.seq = 0U; } @@ -720,6 +725,21 @@ do_update: return true; } +bool bt_mesh_primary_subnet_exist(void) +{ + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { + if (bt_mesh_subnet_get(BLE_MESH_KEY_PRIMARY)) { + return true; + } + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + if (bt_mesh_provisioner_subnet_get(BLE_MESH_KEY_PRIMARY)) { + return true; + } + } + + return false; +} + u32_t bt_mesh_next_seq(void) { u32_t seq = bt_mesh.seq++; @@ -730,10 +750,8 @@ u32_t bt_mesh_next_seq(void) if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) && bt_mesh.seq > IV_UPDATE_SEQ_LIMIT && - bt_mesh_subnet_get(BLE_MESH_KEY_PRIMARY)) { -#if CONFIG_BLE_MESH_NODE + bt_mesh_primary_subnet_exist()) { bt_mesh_beacon_ivu_initiator(true); -#endif bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true); bt_mesh_net_sec_update(NULL); } @@ -745,10 +763,10 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, bool new_key, const struct bt_mesh_send_cb *cb, void *cb_data) { - const u8_t *enc, *priv; - u32_t seq; - u16_t dst; - int err; + const u8_t *enc = NULL, *priv = NULL; + u32_t seq = 0U; + u16_t dst = 0U; + int err = 0; BT_DBG("net_idx 0x%04x new_key %u len %u", sub->net_idx, new_key, buf->len); @@ -801,7 +819,7 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, static void bt_mesh_net_local(struct k_work *work) { - struct net_buf *buf; + struct net_buf *buf = NULL; while ((buf = net_buf_slist_get(&bt_mesh.local_queue))) { BT_DBG("len %u: %s", buf->len, bt_hex(buf->data, buf->len)); @@ -814,11 +832,11 @@ int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf, bool proxy) { const bool ctl = (tx->ctx->app_idx == BLE_MESH_KEY_UNUSED); - u32_t seq_val; - u8_t nid; - const u8_t *enc, *priv; - u8_t *seq; - int err; + u32_t seq_val = 0U; + u8_t nid = 0U; + const u8_t *enc = NULL, *priv = NULL; + u8_t *seq = NULL; + int err = 0; if (ctl && net_buf_simple_tailroom(buf) < 8) { BT_ERR("%s, Insufficient MIC space for CTL PDU", __func__); @@ -877,7 +895,7 @@ int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf, int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf, const struct bt_mesh_send_cb *cb, void *cb_data) { - int err; + int err = 0; BT_DBG("src 0x%04x dst 0x%04x len %u headroom %u tailroom %u", tx->src, tx->ctx->addr, buf->len, net_buf_headroom(buf), @@ -898,23 +916,19 @@ int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf, * "The output filter of the interface connected to advertising or * GATT bearers shall drop all messages with TTL value set to 1." */ -#if CONFIG_BLE_MESH_NODE - if (bt_mesh_is_provisioned()) { - if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && - tx->ctx->send_ttl != 1U) { - if (bt_mesh_proxy_relay(&buf->b, tx->ctx->addr) && - BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { - /* Notify completion if this only went - * through the Mesh Proxy. - */ - send_cb_finalize(cb, cb_data); + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && + tx->ctx->send_ttl != 1U) { + if (bt_mesh_proxy_relay(&buf->b, tx->ctx->addr) && + BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { + /* Notify completion if this only went + * through the Mesh Proxy. + */ + send_cb_finalize(cb, cb_data); - err = 0; - goto done; - } + err = 0; + goto done; } } -#endif #if CONFIG_BLE_MESH_GATT_PROXY_CLIENT if (tx->ctx->send_ttl != 1U) { @@ -962,7 +976,7 @@ static bool auth_match(struct bt_mesh_subnet_keys *keys, const u8_t net_id[8], u8_t flags, u32_t iv_index, const u8_t auth[8]) { - u8_t net_auth[8]; + u8_t net_auth[8] = {0}; if (memcmp(net_id, keys->net_id, 8)) { return false; @@ -984,12 +998,15 @@ struct bt_mesh_subnet *bt_mesh_subnet_find(const u8_t net_id[8], u8_t flags, u32_t iv_index, const u8_t auth[8], bool *new_key) { + size_t subnet_size = 0U; int i; - for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { - struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + subnet_size = bt_mesh_rx_netkey_size(); - if (sub->net_idx == BLE_MESH_KEY_UNUSED) { + for (i = 0; i < subnet_size; i++) { + struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i); + + if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) { continue; } @@ -1028,11 +1045,6 @@ static int net_decrypt(struct bt_mesh_subnet *sub, const u8_t *enc, return -ENOENT; } - /* TODO: For provisioner, when a device is re-provisioned and start to - * send the same message(e.g. cfg_appkey_add), the status message is easy - * to be filtered here. So when a device is re-provisioned, the related - * msg_cache should be cleared. Will do it later. - */ if (rx->net_if == BLE_MESH_NET_IF_ADV && msg_cache_match(rx, buf)) { BT_WARN("Duplicate found in Network Message Cache"); return -EALREADY; @@ -1046,15 +1058,11 @@ static int net_decrypt(struct bt_mesh_subnet *sub, const u8_t *enc, BT_DBG("src 0x%04x", rx->ctx.addr); -#if CONFIG_BLE_MESH_NODE - if (bt_mesh_is_provisioned()) { - if (IS_ENABLED(CONFIG_BLE_MESH_PROXY) && - rx->net_if == BLE_MESH_NET_IF_PROXY_CFG) { - return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx), - true); - } + if (IS_ENABLED(CONFIG_BLE_MESH_PROXY) && + rx->net_if == BLE_MESH_NET_IF_PROXY_CFG) { + return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx), + true); } -#endif return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx), false); } @@ -1103,7 +1111,7 @@ static bool net_find_and_decrypt(const u8_t *data, size_t data_len, struct net_buf_simple *buf) { struct bt_mesh_subnet *sub = NULL; - size_t array_size = 0; + size_t array_size = 0U; int i; BT_DBG("%s", __func__); @@ -1160,8 +1168,6 @@ static bool net_find_and_decrypt(const u8_t *data, size_t data_len, * get sent to the advertising bearer. If the packet came in through GATT, * then we should only relay it if the GATT Proxy state is enabled. */ -#if CONFIG_BLE_MESH_NODE - static bool relay_to_adv(enum bt_mesh_net_if net_if) { switch (net_if) { @@ -1179,9 +1185,9 @@ static bool relay_to_adv(enum bt_mesh_net_if net_if) static void bt_mesh_net_relay(struct net_buf_simple *sbuf, struct bt_mesh_net_rx *rx) { - const u8_t *enc, *priv; - struct net_buf *buf; - u8_t nid, transmit; + const u8_t *enc = NULL, *priv = NULL; + struct net_buf *buf = NULL; + u8_t nid = 0U, transmit = 0U; if (rx->net_if == BLE_MESH_NET_IF_LOCAL) { /* Locally originated PDUs with TTL=1 will only be delivered @@ -1301,8 +1307,6 @@ done: net_buf_unref(buf); } -#endif /* CONFIG_BLE_MESH_NODE */ - void bt_mesh_net_header_parse(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx) { @@ -1379,25 +1383,15 @@ int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if, static bool ready_to_recv(void) { -#if CONFIG_BLE_MESH_NODE - if (!bt_mesh_is_provisioner_en()) { - if (!bt_mesh_is_provisioned()) { - return false; + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { + return true; + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + if (bt_mesh_provisioner_get_all_node_count()) { + return true; } } -#endif -#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (!bt_mesh_is_provisioner_en()) { - BT_WARN("%s, Provisioner is disabled", __func__); - return false; - } - if (!provisioner_get_prov_node_count()) { - return false; - } -#endif - - return true; + return false; } void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, @@ -1405,7 +1399,7 @@ void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, { NET_BUF_SIMPLE_DEFINE(buf, 29); struct bt_mesh_net_rx rx = { .ctx.recv_rssi = rssi }; - struct net_buf_simple_state state; + struct net_buf_simple_state state = {0}; BT_DBG("rssi %d net_if %u", rssi, net_if); @@ -1420,14 +1414,10 @@ void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, /* Save the state so the buffer can later be relayed */ net_buf_simple_save(&buf, &state); -#if CONFIG_BLE_MESH_NODE - if (bt_mesh_is_provisioned()) { - if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && - net_if == BLE_MESH_NET_IF_PROXY) { - bt_mesh_proxy_addr_add(data, rx.ctx.addr); - } + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && + net_if == BLE_MESH_NET_IF_PROXY) { + bt_mesh_proxy_addr_add(data, rx.ctx.addr); } -#endif rx.local_match = (bt_mesh_fixed_group_match(rx.ctx.recv_dst) || bt_mesh_elem_find(rx.ctx.recv_dst)); @@ -1449,22 +1439,19 @@ void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, /* Relay if this was a group/virtual address, or if the destination * was neither a local element nor an LPN we're Friends for. */ -#if CONFIG_BLE_MESH_NODE - if (bt_mesh_is_provisioned()) { - if (!BLE_MESH_ADDR_IS_UNICAST(rx.ctx.recv_dst) || - (!rx.local_match && !rx.friend_match)) { - net_buf_simple_restore(&buf, &state); - bt_mesh_net_relay(&buf, &rx); - } + if (IS_ENABLED(CONFIG_BLE_MESH_RELAY) && + (!BLE_MESH_ADDR_IS_UNICAST(rx.ctx.recv_dst) || + (!rx.local_match && !rx.friend_match))) { + net_buf_simple_restore(&buf, &state); + bt_mesh_net_relay(&buf, &rx); } -#endif } static void ivu_refresh(struct k_work *work) { bt_mesh.ivu_duration += BLE_MESH_IVU_HOURS; - BT_DBG("%s for %u hour%s", + BT_INFO("%s for %u hour%s", bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) ? "IVU in Progress" : "IVU Normal mode", bt_mesh.ivu_duration, bt_mesh.ivu_duration == 1U ? "" : "s"); @@ -1479,16 +1466,13 @@ static void ivu_refresh(struct k_work *work) } if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) { -#if CONFIG_BLE_MESH_NODE bt_mesh_beacon_ivu_initiator(true); -#endif bt_mesh_net_iv_update(bt_mesh.iv_index, false); } else if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { bt_mesh_store_iv(true); } } -#if defined(CONFIG_BLE_MESH_NODE) void bt_mesh_net_start(void) { if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { @@ -1533,7 +1517,6 @@ void bt_mesh_net_start(void) bt_mesh_prov_complete(net_idx, net_key, addr, flags, iv_index); } } -#endif void bt_mesh_net_init(void) { @@ -1541,3 +1524,34 @@ void bt_mesh_net_init(void) k_work_init(&bt_mesh.local_work, bt_mesh_net_local); } + +void bt_mesh_net_deinit(bool erase) +{ + k_delayed_work_free(&bt_mesh.ivu_timer); + + k_work_init(&bt_mesh.local_work, NULL); + + /* Local queue uses a while loop, currently no need + * to handle this. + */ + +#if FRIEND_CRED_COUNT > 0 + memset(friend_cred, 0, sizeof(friend_cred)); +#endif + + memset(msg_cache, 0, sizeof(msg_cache)); + msg_cache_next = 0U; + + memset(dup_cache, 0, sizeof(dup_cache)); + dup_cache_next = 0U; + + bt_mesh.iv_index = 0U; + bt_mesh.seq = 0U; + + memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags)); + + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_seq(); + bt_mesh_clear_iv(); + } +} diff --git a/components/bt/esp_ble_mesh/mesh_core/net.h b/components/bt/esp_ble_mesh/mesh_core/net.h index 47657f495..abdb57450 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.h +++ b/components/bt/esp_ble_mesh/mesh_core/net.h @@ -10,8 +10,6 @@ #ifndef _NET_H_ #define _NET_H_ -#include "mesh_util.h" -#include "mesh_kernel.h" #include "mesh_access.h" #define BLE_MESH_NET_FLAG_KR BIT(0) @@ -208,7 +206,10 @@ struct bt_mesh_lpn { /* bt_mesh_net.flags */ enum { + BLE_MESH_NODE, /* Device is a node */ + BLE_MESH_PROVISIONER, /* Device is a Provisioner */ BLE_MESH_VALID, /* We have been provisioned */ + BLE_MESH_VALID_PROV, /* Provisioner has been enabled */ BLE_MESH_SUSPENDED, /* Network is temporarily suspended */ BLE_MESH_IVU_IN_PROGRESS, /* IV Update in Progress */ BLE_MESH_IVU_INITIATOR, /* IV Update initiated by us */ @@ -319,6 +320,8 @@ extern struct bt_mesh_net bt_mesh; #define BLE_MESH_NET_HDR_LEN 9 +void bt_mesh_msg_cache_clear(u16_t unicast_addr, u8_t elem_num); + int bt_mesh_net_keys_create(struct bt_mesh_subnet_keys *keys, const u8_t key[16]); @@ -361,11 +364,15 @@ int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if, void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, enum bt_mesh_net_if net_if); +bool bt_mesh_primary_subnet_exist(void); + u32_t bt_mesh_next_seq(void); void bt_mesh_net_start(void); void bt_mesh_net_init(void); +void bt_mesh_net_deinit(bool erase); + void bt_mesh_net_header_parse(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx); diff --git a/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c index 767c2bace..e997be4db 100644 --- a/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c +++ b/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c @@ -8,8 +8,6 @@ #include -#include "sdkconfig.h" - #include "btc/btc_task.h" #include "osi/alarm.h" @@ -24,16 +22,9 @@ #include "services/gatt/ble_svc_gatt.h" #include "mesh_hci.h" -#include "mesh_aes_encrypt.h" -#include "mesh_bearer_adapt.h" -#include "mesh_trace.h" -#include "mesh_buf.h" -#include "mesh_atomic.h" - -#include "esp_ble_mesh_defs.h" - -#include "provisioner_prov.h" #include "mesh_common.h" +#include "mesh_aes_encrypt.h" +#include "provisioner_prov.h" /** @def BT_UUID_MESH_PROV * @brief Mesh Provisioning Service @@ -72,7 +63,7 @@ struct bt_mesh_dev bt_mesh_dev; /* P-256 Variables */ static u8_t bt_mesh_public_key[64]; -static BT_OCTET32 bt_mesh_private_key = { +static u8_t bt_mesh_private_key[32] = { 0x3f, 0x49, 0xf6, 0xd4, 0xa3, 0xc5, 0x5f, 0x38, 0x74, 0xc9, 0xb3, 0xe3, 0xd2, 0x10, 0x3f, 0x50, 0x4a, 0xff, 0x60, 0x7b, 0xeb, 0x40, 0xb7, 0x99, @@ -90,25 +81,25 @@ static sys_slist_t bt_mesh_gatts_db; static struct bt_mesh_conn bt_mesh_gatts_conn[BLE_MESH_MAX_CONN]; static struct bt_mesh_conn_cb *bt_mesh_gatts_conn_cb; -static BD_ADDR bt_mesh_gatts_addr; +static u8_t bt_mesh_gatts_addr[6]; #endif /* defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE */ -esp_err_t bt_mesh_host_init(void) +int bt_mesh_host_init(void) { int rc; rc = btc_init(); if (rc != 0) { - return ESP_FAIL; + return -1; } rc = osi_alarm_create_mux(); if (rc != 0) { - return ESP_FAIL; + return -1; } osi_alarm_init(); - return ESP_OK; + return 0; } uint8_t ble_hs_hci_get_hci_version(void); @@ -401,7 +392,7 @@ static int disc_cb(struct ble_gap_event *event, void *arg) if (bt_mesh_scan_dev_found_cb) { bt_mesh_scan_dev_found_cb((bt_mesh_addr_t *)&desc->addr, desc->rssi, desc->event_type, buf); } - osi_free(buf); + bt_mesh_free(buf); break; #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ CONFIG_BLE_MESH_GATT_PROXY_CLIENT @@ -453,7 +444,7 @@ static int disc_cb(struct ble_gap_event *event, void *arg) */ #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { - provisioner_clear_link_conn_info(bt_mesh_gattc_info[i].addr.val); + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); } #endif } @@ -464,14 +455,14 @@ static int disc_cb(struct ble_gap_event *event, void *arg) */ #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { - provisioner_clear_link_conn_info(bt_mesh_gattc_info[i].addr.val); + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); } #endif } #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { /* Decrease prov pbg_count */ - provisioner_pbg_count_dec(); + bt_mesh_provisioner_pbg_count_dec(); } #endif /* Reset corresponding gattc info */ @@ -1710,7 +1701,7 @@ int bt_mesh_dh_key_gen(const u8_t remote_pk[64], bt_mesh_dh_key_cb_t cb, const u { uint8_t dhkey[32]; - BT_DBG("private key = %s", bt_hex(bt_mesh_private_key, BT_OCTET32_LEN)); + BT_DBG("private key = %s", bt_hex(bt_mesh_private_key, 32)); ble_sm_alg_gen_dhkey((uint8_t *)&remote_pk[0], (uint8_t *)&remote_pk[32], bt_mesh_private_key, dhkey); diff --git a/components/bt/esp_ble_mesh/mesh_core/prov.c b/components/bt/esp_ble_mesh/mesh_core/prov.c index c1156f173..6f95087c0 100644 --- a/components/bt/esp_ble_mesh/mesh_core/prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/prov.c @@ -9,21 +9,15 @@ #include #include -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_PROV) -#include "mesh_util.h" -#include "mesh_main.h" -#include "mesh_uuid.h" -#include "mesh_trace.h" -#include "mesh_proxy.h" - #include "crypto.h" #include "adv.h" #include "mesh.h" -#include "net.h" #include "access.h" #include "foundation.h" +#include "mesh_common.h" +#include "mesh_proxy.h" #include "proxy_server.h" #include "prov.h" @@ -199,6 +193,32 @@ static struct prov_link link; static const struct bt_mesh_prov *prov; +#if defined(CONFIG_BLE_MESH_PB_ADV) +static bt_mesh_mutex_t pb_buf_lock; + +static void bt_mesh_pb_buf_mutex_new(void) +{ + if (!pb_buf_lock.mutex) { + bt_mesh_mutex_create(&pb_buf_lock); + } +} + +static void bt_mesh_pb_buf_mutex_free(void) +{ + bt_mesh_mutex_free(&pb_buf_lock); +} + +static void bt_mesh_pb_buf_lock(void) +{ + bt_mesh_mutex_lock(&pb_buf_lock); +} + +static void bt_mesh_pb_buf_unlock(void) +{ + bt_mesh_mutex_unlock(&pb_buf_lock); +} +#endif /* CONFIG_BLE_MESH_PB_ADV */ + static void reset_state(void) { k_delayed_work_cancel(&link.prot_timer); @@ -252,6 +272,8 @@ static void free_segments(void) { int i; + bt_mesh_pb_buf_lock(); + for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) { struct net_buf *buf = link.tx.buf[i]; @@ -265,6 +287,8 @@ static void free_segments(void) BLE_MESH_ADV(buf)->busy = 0U; net_buf_unref(buf); } + + bt_mesh_pb_buf_unlock(); } static void prov_clear_tx(void) @@ -295,7 +319,7 @@ static void reset_adv_link(void) static struct net_buf *adv_buf_create(void) { - struct net_buf *buf; + struct net_buf *buf = NULL; buf = bt_mesh_adv_create(BLE_MESH_ADV_PROV, PROV_XMIT, BUF_TIMEOUT); if (!buf) { @@ -319,8 +343,8 @@ static void gen_prov_ack_send(u8_t xact_id) static const struct bt_mesh_send_cb cb = { .start = ack_complete, }; - const struct bt_mesh_send_cb *complete; - struct net_buf *buf; + const struct bt_mesh_send_cb *complete = NULL; + struct net_buf *buf = NULL; BT_DBG("xact_id %u", xact_id); @@ -372,7 +396,7 @@ static void send_reliable(void) static int bearer_ctl_send(u8_t op, void *data, u8_t data_len) { - struct net_buf *buf; + struct net_buf *buf = NULL; BT_DBG("op 0x%02x data_len %u", op, data_len); @@ -418,9 +442,9 @@ static inline u8_t next_transaction_id(void) static int prov_send_adv(struct net_buf_simple *msg) { - struct net_buf *start, *buf; - u8_t seg_len, seg_id; - u8_t xact_id; + struct net_buf *start = NULL, *buf = NULL; + u8_t seg_len = 0U, seg_id = 0U; + u8_t xact_id = 0U; s32_t timeout = PROTOCOL_TIMEOUT; BT_DBG("%s, len %u: %s", __func__, msg->len, bt_hex(msg->data, msg->len)); @@ -602,7 +626,7 @@ static void prov_invite(const u8_t *data) static void prov_capabilities(const u8_t *data) { - u16_t algorithms, output_action, input_action; + u16_t algorithms = 0U, output_action = 0U, input_action = 0U; BT_DBG("Elements: %u", data[0]); @@ -620,6 +644,10 @@ static void prov_capabilities(const u8_t *data) input_action = sys_get_be16(&data[9]); BT_DBG("Input OOB Action: 0x%04x", input_action); + + ((void) algorithms); + ((void) output_action); + ((void) input_action); } static bt_mesh_output_action_t output_action(u8_t action) @@ -658,8 +686,8 @@ static bt_mesh_input_action_t input_action(u8_t action) static int prov_auth(u8_t method, u8_t action, u8_t size) { - bt_mesh_output_action_t output; - bt_mesh_input_action_t input; + bt_mesh_output_action_t output = 0U; + bt_mesh_input_action_t input = 0U; switch (method) { case AUTH_METHOD_NO_OOB: @@ -695,8 +723,8 @@ static int prov_auth(u8_t method, u8_t action, u8_t size) } if (output == BLE_MESH_DISPLAY_STRING) { - unsigned char str[9]; - u8_t i; + unsigned char str[9] = {'\0'}; + u8_t i = 0U; bt_mesh_rand(str, size); @@ -720,7 +748,7 @@ static int prov_auth(u8_t method, u8_t action, u8_t size) u32_t div[8] = { 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 }; - u32_t num; + u32_t num = 0U; bt_mesh_rand(&num, sizeof(num)); num %= div[size - 1]; @@ -760,11 +788,11 @@ static int prov_auth(u8_t method, u8_t action, u8_t size) static void prov_start(const u8_t *data) { - BT_DBG("Algorithm: 0x%02x", data[0]); - BT_DBG("Public Key: 0x%02x", data[1]); - BT_DBG("Auth Method: 0x%02x", data[2]); - BT_DBG("Auth Action: 0x%02x", data[3]); - BT_DBG("Auth Size: 0x%02x", data[4]); + BT_INFO("Algorithm: 0x%02x", data[0]); + BT_INFO("Public Key: 0x%02x", data[1]); + BT_INFO("Auth Method: 0x%02x", data[2]); + BT_INFO("Auth Action: 0x%02x", data[3]); + BT_INFO("Auth Size: 0x%02x", data[4]); if (data[0] != PROV_ALG_P256) { BT_ERR("%s, Unknown algorithm 0x%02x", __func__, data[0]); @@ -860,7 +888,7 @@ static void send_input_complete(void) int bt_mesh_input_number(u32_t num) { - BT_DBG("%u", num); + BT_INFO("%u", num); if (!bt_mesh_atomic_test_and_clear_bit(link.flags, WAIT_NUMBER)) { return -EINVAL; @@ -883,7 +911,7 @@ int bt_mesh_input_number(u32_t num) int bt_mesh_input_string(const char *str) { - BT_DBG("%s", str); + BT_INFO("%s", str); if (!bt_mesh_atomic_test_and_clear_bit(link.flags, WAIT_STRING)) { return -EINVAL; @@ -933,7 +961,7 @@ static void prov_dh_key_cb(const u8_t key[32], const u8_t idx) static void send_pub_key(void) { PROV_BUF(buf, 65); - const u8_t *key; + const u8_t *key = NULL; /* Copy remote key in little-endian for bt_mesh_dh_key_gen(). * X and Y halves are swapped independently. Use response @@ -1083,7 +1111,7 @@ static void prov_confirm(const u8_t *data) static void prov_random(const u8_t *data) { PROV_BUF(rnd, 17); - u8_t conf_verify[16]; + u8_t conf_verify[16] = {0}; BT_DBG("Remote Random: %s", bt_hex(data, 16)); @@ -1133,16 +1161,16 @@ static inline bool is_pb_gatt(void) static void prov_data(const u8_t *data) { PROV_BUF(msg, 1); - u8_t session_key[16]; - u8_t nonce[13]; - u8_t dev_key[16]; - u8_t pdu[25]; - u8_t flags; - u32_t iv_index; - u16_t addr; - u16_t net_idx; - int err; - bool identity_enable; + u8_t session_key[16] = {0}; + u8_t nonce[13] = {0}; + u8_t dev_key[16] = {0}; + u8_t pdu[25] = {0}; + u8_t flags = 0U; + u32_t iv_index = 0U; + u16_t addr = 0U; + u16_t net_idx = 0U; + int err = 0; + bool identity_enable = false; BT_DBG("%s", __func__); @@ -1269,6 +1297,8 @@ static void prov_retransmit(struct k_work *work) return; } + bt_mesh_pb_buf_lock(); + for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) { struct net_buf *buf = link.tx.buf[i]; @@ -1289,6 +1319,8 @@ static void prov_retransmit(struct k_work *work) } } + + bt_mesh_pb_buf_unlock(); } static void link_open(struct prov_rx *rx, struct net_buf_simple *buf) @@ -1447,7 +1479,7 @@ static void gen_prov_cont(struct prov_rx *rx, struct net_buf_simple *buf) prov_send_fail_msg(PROV_ERR_NVAL_FMT); return; } else if (seg == link.rx.last_seg) { - u8_t expect_len; + u8_t expect_len = 0U; expect_len = (link.rx.buf->len - 20U - ((link.rx.last_seg - 1) * 23U)); @@ -1563,7 +1595,7 @@ static void gen_prov_recv(struct prov_rx *rx, struct net_buf_simple *buf) void bt_mesh_pb_adv_recv(struct net_buf_simple *buf) { - struct prov_rx rx; + struct prov_rx rx = {0}; if (!bt_prov_active() && bt_mesh_is_provisioned()) { BT_DBG("Ignoring provisioning PDU - already provisioned"); @@ -1593,7 +1625,7 @@ void bt_mesh_pb_adv_recv(struct net_buf_simple *buf) #if defined(CONFIG_BLE_MESH_PB_GATT) int bt_mesh_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf) { - u8_t type; + u8_t type = 0U; BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); @@ -1683,7 +1715,7 @@ bool bt_prov_active(void) static void protocol_timeout(struct k_work *work) { - BT_DBG("Protocol timeout"); + BT_WARN("Protocol timeout"); #if defined(CONFIG_BLE_MESH_PB_GATT) if (link.conn) { @@ -1728,6 +1760,40 @@ int bt_mesh_prov_init(const struct bt_mesh_prov *prov_info) reset_state(); +#if defined(CONFIG_BLE_MESH_PB_ADV) + bt_mesh_pb_buf_mutex_new(); +#endif + + return 0; +} + +int bt_mesh_prov_deinit(void) +{ + if (prov == NULL) { + BT_ERR("%s, No provisioning context provided", __func__); + return -EINVAL; + } + + k_delayed_work_free(&link.prot_timer); + +#if defined(CONFIG_BLE_MESH_PB_ADV) + prov_clear_tx(); + k_delayed_work_free(&link.tx.retransmit); +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + /* Remove the link id from exceptional list */ + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_REMOVE, + BLE_MESH_EXCEP_INFO_MESH_LINK_ID, &link.id); +#endif /* CONFIG_BLE_MESH_USE_DUPLICATE_SCAN */ +#endif /* CONFIG_BLE_MESH_PB_ADV */ + + (void)memset(&link, 0, sizeof(link)); + +#if defined(CONFIG_BLE_MESH_PB_ADV) + bt_mesh_pb_buf_mutex_free(); +#endif + + prov = NULL; + return 0; } diff --git a/components/bt/esp_ble_mesh/mesh_core/prov.h b/components/bt/esp_ble_mesh/mesh_core/prov.h index 756cd011f..782d86921 100644 --- a/components/bt/esp_ble_mesh/mesh_core/prov.h +++ b/components/bt/esp_ble_mesh/mesh_core/prov.h @@ -10,7 +10,6 @@ #define _PROV_H_ #include "mesh_main.h" -#include "mesh_buf.h" #include "mesh_bearer_adapt.h" void bt_mesh_pb_adv_recv(struct net_buf_simple *buf); @@ -27,6 +26,7 @@ int bt_mesh_set_oob_pub_key(const u8_t pub_key_x[32], const u8_t pub_key_y[32], const struct bt_mesh_prov *bt_mesh_prov_get(void); int bt_mesh_prov_init(const struct bt_mesh_prov *prov); +int bt_mesh_prov_deinit(void); void bt_mesh_prov_complete(u16_t net_idx, const u8_t net_key[16], u16_t addr, u8_t flags, u32_t iv_index); void bt_mesh_prov_reset(void); diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.c deleted file mode 100644 index 0790b538d..000000000 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.c +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright 2017-2019 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 -#include - -#include "sdkconfig.h" -#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_BEACON) - -#include "mesh_util.h" -#include "mesh_buf.h" -#include "mesh_main.h" -#include "mesh_trace.h" - -#include "adv.h" -#include "mesh.h" -#include "net.h" -#include "prov.h" -#include "crypto.h" -#include "beacon.h" -#include "foundation.h" -#include "provisioner_prov.h" - -#define BEACON_TYPE_UNPROVISIONED 0x00 -#define BEACON_TYPE_SECURE 0x01 - -#if CONFIG_BLE_MESH_PROVISIONER - -static void provisioner_secure_beacon_recv(struct net_buf_simple *buf) -{ - // TODO: Provisioner receive and handle Secure Network Beacon -} - -void provisioner_beacon_recv(struct net_buf_simple *buf) -{ - u8_t type; - - BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); - - if (buf->len < 1) { - BT_ERR("%s, Too short beacon", __func__); - return; - } - - type = net_buf_simple_pull_u8(buf); - switch (type) { - case BEACON_TYPE_UNPROVISIONED: - BT_DBG("Unprovisioned device beacon received"); - provisioner_unprov_beacon_recv(buf); - break; - case BEACON_TYPE_SECURE: - provisioner_secure_beacon_recv(buf); - break; - default: - BT_DBG("%s, Unknown beacon type 0x%02x", __func__, type); - break; - } -} - -#endif /* CONFIG_BLE_MESH_PROVISIONER */ diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.h b/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.h deleted file mode 100644 index a58de225f..000000000 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_beacon.h +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2017-2019 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 _PROVISIONER_BEACON_H_ -#define _PROVISIONER_BEACON_H_ - -void provisioner_beacon_recv(struct net_buf_simple *buf); - -#endif /* _PROVISIONER_BEACON_H_ */ \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c index 8fc1d4b3c..dcf3421f4 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -15,193 +15,87 @@ #include #include -#include "sdkconfig.h" -#include "osi/allocator.h" - -#include "mesh_util.h" -#include "mesh_main.h" -#include "mesh_trace.h" -#include "mesh_bearer_adapt.h" - #include "mesh.h" #include "crypto.h" #include "adv.h" -#include "net.h" #include "access.h" +#include "settings.h" #include "friend.h" - -#include "provisioner_prov.h" +#include "mesh_common.h" #include "proxy_client.h" +#include "provisioner_prov.h" #include "provisioner_main.h" #if CONFIG_BLE_MESH_PROVISIONER -static const struct bt_mesh_prov *prov; -static const struct bt_mesh_comp *comp; +static struct bt_mesh_node *mesh_nodes[CONFIG_BLE_MESH_MAX_STORED_NODES]; +static bt_mesh_mutex_t provisioner_lock; +static u16_t all_node_count; +static u16_t prov_node_count; -static struct bt_mesh_node_t *mesh_nodes[CONFIG_BLE_MESH_MAX_STORED_NODES]; -static u32_t mesh_node_count; +static int provisioner_remove_node(u16_t index, bool erase); -static bool prov_upper_init = false; - -static int provisioner_index_check(int node_index) +static void bt_mesh_provisioner_mutex_new(void) { - struct bt_mesh_node_t *node = NULL; - - BT_DBG("%s", __func__); - - if (node_index < 0) { - BT_ERR("%s, Invalid node index %d", __func__, node_index); - return -EINVAL; + if (!provisioner_lock.mutex) { + bt_mesh_mutex_create(&provisioner_lock); } +} - if (node_index >= ARRAY_SIZE(mesh_nodes)) { - BT_ERR("%s, Too big node index", __func__); - return -EINVAL; - } +static void bt_mesh_provisioner_mutex_free(void) +{ + bt_mesh_mutex_free(&provisioner_lock); +} - node = mesh_nodes[node_index]; - if (!node) { - BT_ERR("%s, Node is not found", __func__); - return -EINVAL; - } +static void bt_mesh_provisioner_lock(void) +{ + bt_mesh_mutex_lock(&provisioner_lock); +} + +static void bt_mesh_provisioner_unlock(void) +{ + bt_mesh_mutex_unlock(&provisioner_lock); +} + +int bt_mesh_provisioner_init(void) +{ + bt_mesh_provisioner_mutex_new(); return 0; } -int provisioner_node_provision(int node_index, const u8_t uuid[16], u16_t oob_info, - u16_t unicast_addr, u8_t element_num, u16_t net_idx, - u8_t flags, u32_t iv_index, const u8_t dev_key[16]) -{ - struct bt_mesh_node_t *node = NULL; - - BT_DBG("%s", __func__); - - if (mesh_node_count >= ARRAY_SIZE(mesh_nodes)) { - BT_ERR("%s, Node queue is full", __func__); - return -ENOMEM; - } - - if (node_index >= ARRAY_SIZE(mesh_nodes) || !uuid || !dev_key) { - BT_ERR("%s, Invalid parameter", __func__); - return -EINVAL; - } - - node = osi_calloc(sizeof(struct bt_mesh_node_t)); - if (!node) { - BT_ERR("%s, Failed to allocate memory", __func__); - return -ENOMEM; - } - - BT_DBG("node_index: 0x%x, unicast_addr: 0x%x, element_num: 0x%x, net_idx: 0x%x", - node_index, unicast_addr, element_num, net_idx); - BT_DBG("dev_uuid: %s", bt_hex(uuid, 16)); - BT_DBG("dev_key: %s", bt_hex(dev_key, 16)); - - mesh_nodes[node_index] = node; - - memcpy(node->dev_uuid, uuid, 16); - node->oob_info = oob_info; - node->unicast_addr = unicast_addr; - node->element_num = element_num; - node->net_idx = net_idx; - node->flags = flags; - node->iv_index = iv_index; - memcpy(node->dev_key, dev_key, 16); - - mesh_node_count++; - - return 0; -} - -int provisioner_node_reset(int node_index) -{ - struct bt_mesh_node_t *node = NULL; - struct bt_mesh_rpl *rpl = NULL; - int i; - - BT_DBG("%s, reset node %d", __func__, node_index); - - if (!mesh_node_count) { - BT_ERR("%s, Node queue is empty", __func__); - return -ENODEV; - } - - if (provisioner_index_check(node_index)) { - BT_ERR("%s, Failed to check node index", __func__); - return -EINVAL; - } - - node = mesh_nodes[node_index]; - - /* Reset corresponding rpl when reset the node */ - for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { - rpl = &bt_mesh.rpl[i]; - if (rpl->src >= node->unicast_addr && - rpl->src < node->unicast_addr + node->element_num) { - memset(rpl, 0, sizeof(struct bt_mesh_rpl)); - } - } - - if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { - bt_mesh_friend_remove_lpn(node->unicast_addr); - } - - osi_free(mesh_nodes[node_index]); - mesh_nodes[node_index] = NULL; - - mesh_node_count--; - - return 0; -} - -int provisioner_upper_reset_all_nodes(void) -{ - int i, err; - - BT_DBG("%s", __func__); - - for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { - err = provisioner_node_reset(i); - if (err == -ENODEV) { - return 0; - } - } - - return 0; -} - -/** For Provisioner, we use the same data structure - * (like, struct bt_mesh_subnet, etc.) for netkey - * & appkey because if not we need to change a lot - * of APIs. +/** + * When a Provisioner tries to create a network, it will check the + * status of the restored network keys firstly, and try to create + * one if they are not existed. */ -int provisioner_upper_init(void) +int bt_mesh_provisioner_net_create(void) { + const struct bt_mesh_prov *prov = NULL; struct bt_mesh_subnet *sub = NULL; u8_t p_key[16] = {0}; BT_DBG("%s", __func__); - if (prov_upper_init) { - return 0; - } - - comp = bt_mesh_comp_get(); - if (!comp) { - BT_ERR("%s, NULL composition data", __func__); - return -EINVAL; - } - - prov = provisioner_get_prov_info(); + prov = bt_mesh_provisioner_get_prov_info(); if (!prov) { BT_ERR("%s, NULL provisioning context", __func__); return -EINVAL; } /* If the device only acts as a Provisioner, need to initialize - each element's address. */ - bt_mesh_comp_provision(prov->prov_unicast_addr); + * each element's address. + */ + bt_mesh_comp_provision(bt_mesh_provisioner_get_primary_elem_addr()); + + if (bt_mesh.p_sub[0]) { + /* Provisioner is already enabled (enable -> disable -> enable), + * or Provisioner is restored from flash. + */ + BT_INFO("Provisioner already created network"); + sub = bt_mesh.p_sub[0]; + goto done; + } /* Generate the primary netkey */ if (bt_mesh_rand(p_key, 16)) { @@ -209,7 +103,7 @@ int provisioner_upper_init(void) return -EIO; } - sub = osi_calloc(sizeof(struct bt_mesh_subnet)); + sub = bt_mesh_calloc(sizeof(struct bt_mesh_subnet)); if (!sub) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; @@ -219,7 +113,7 @@ int provisioner_upper_init(void) if (sub->kr_flag) { if (bt_mesh_net_keys_create(&sub->keys[1], p_key)) { BT_ERR("%s, Failed to generate net-related keys", __func__); - osi_free(sub); + bt_mesh_free(sub); return -EIO; } sub->kr_phase = BLE_MESH_KR_PHASE_2; @@ -227,7 +121,7 @@ int provisioner_upper_init(void) /* Currently provisioner only use keys[0] */ if (bt_mesh_net_keys_create(&sub->keys[0], p_key)) { BT_ERR("%s, Failed to create net-related keys", __func__); - osi_free(sub); + bt_mesh_free(sub); return -EIO; } sub->kr_phase = BLE_MESH_KR_NORMAL; @@ -241,8 +135,12 @@ int provisioner_upper_init(void) bt_mesh.p_app_idx_next = 0x0000; bt_mesh.p_net_idx_next = 0x0001; - /* In this function, we use the values of struct bt_mesh_prov - which has been initialized in the application layer */ + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_p_net_idx(); + bt_mesh_store_p_app_idx(); + bt_mesh_store_p_subnet(bt_mesh.p_sub[0]); + } + bt_mesh.iv_index = prov->iv_index; bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS, BLE_MESH_IV_UPDATE(prov->flags)); @@ -254,25 +152,636 @@ int provisioner_upper_init(void) */ bt_mesh.ivu_duration = BLE_MESH_IVU_MIN_HOURS; - prov_upper_init = true; + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_iv(true); + } - BT_DBG("kr_flag: %d, kr_phase: %d, net_idx: 0x%02x, node_id %d", - sub->kr_flag, sub->kr_phase, sub->net_idx, sub->node_id); - BT_DBG("netkey: %s, nid: 0x%x", bt_hex(sub->keys[0].net, 16), sub->keys[0].nid); - BT_DBG("enckey: %s", bt_hex(sub->keys[0].enc, 16)); - BT_DBG("network id: %s", bt_hex(sub->keys[0].net_id, 8)); -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) - BT_DBG("identity: %s", bt_hex(sub->keys[0].identity, 16)); -#endif - BT_DBG("privacy: %s", bt_hex(sub->keys[0].privacy, 16)); - BT_DBG("beacon: %s", bt_hex(sub->keys[0].beacon, 16)); +done: + BT_INFO("net_idx 0x%03x, netkey %s, nid 0x%02x", + sub->net_idx, bt_hex(sub->keys[0].net, 16), sub->keys[0].nid); return 0; } -/* The following APIs are for provisioner upper layers internal use */ +int bt_mesh_provisioner_deinit(bool erase) +{ + int i; -const u8_t *provisioner_net_key_get(u16_t net_idx) + for (i = 0; i < CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT; i++) { + if (bt_mesh.p_sub[i]) { + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_p_subnet(bt_mesh.p_sub[i]); + } + bt_mesh_free(bt_mesh.p_sub[i]); + bt_mesh.p_sub[i] = NULL; + } + } + + for (i = 0; i < CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT; i++) { + if (bt_mesh.p_app_keys[i]) { + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_p_app_key(bt_mesh.p_app_keys[i]); + } + bt_mesh_free(bt_mesh.p_app_keys[i]); + bt_mesh.p_app_keys[i] = NULL; + } + } + + bt_mesh.p_net_idx_next = 0U; + bt_mesh.p_app_idx_next = 0U; + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_p_net_idx(); + bt_mesh_clear_p_app_idx(); + } + + for (i = 0; i < CONFIG_BLE_MESH_MAX_STORED_NODES; i++) { + provisioner_remove_node(i, erase); + } + + all_node_count = 0U; + prov_node_count = 0U; + + bt_mesh_provisioner_mutex_free(); + + return 0; +} + +bool bt_mesh_provisioner_check_is_addr_dup(u16_t addr, u8_t elem_num, bool comp_with_own) +{ + const struct bt_mesh_comp *comp = NULL; + struct bt_mesh_node *node = NULL; + u16_t primary_addr = BLE_MESH_ADDR_UNASSIGNED; + u16_t comp_addr = BLE_MESH_ADDR_UNASSIGNED; + int i; + + if (comp_with_own) { + comp = bt_mesh_comp_get(); + if (!comp) { + BT_ERR("NULL composition data"); + return true; + } + + primary_addr = bt_mesh_provisioner_get_primary_elem_addr(); + if (!BLE_MESH_ADDR_IS_UNICAST(primary_addr)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, primary_addr); + return true; + } + } + + for (comp_addr = addr; comp_addr < addr + elem_num; comp_addr++) { + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + node = mesh_nodes[i]; + if (node && comp_addr >= node->unicast_addr && + comp_addr < node->unicast_addr + node->element_num) { + BT_ERR("Duplicate with node address 0x%04x", comp_addr); + return true; + } + + if (comp_with_own && comp_addr >= primary_addr && + comp_addr < primary_addr + comp->elem_count) { + BT_ERR("Duplicate with Provisioner address 0x%04x", comp_addr); + return true; + } + } + } + + return false; +} + +static void provisioner_node_count_inc(bool prov) +{ + all_node_count++; + if (prov) { + prov_node_count++; + } +} + +static void provisioner_node_count_dec(bool prov) +{ + if (all_node_count) { + all_node_count--; + } + if (prov) { + if (prov_node_count) { + prov_node_count--; + } + } +} + +u16_t bt_mesh_provisioner_get_prov_node_count(void) +{ + return prov_node_count; +} + +u16_t bt_mesh_provisioner_get_all_node_count(void) +{ + return all_node_count; +} + +static int provisioner_store_node(struct bt_mesh_node *node, bool prov, bool store, u16_t *index) +{ + u16_t min = 0U, max = 0U; + size_t i = 0U; + + bt_mesh_provisioner_lock(); + + /* Check if the node already exists */ + for (i = 0U; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i] && !memcmp(mesh_nodes[i]->dev_uuid, node->dev_uuid, 16)) { + BT_WARN("Node already exists, uuid %s", bt_hex(node->dev_uuid, 16)); + bt_mesh_provisioner_unlock(); + return -EEXIST; + } + } + + /** + * 0 ~ (CONFIG_BLE_MESH_MAX_PROV_NODES - 1) are used to store + * the information of self-provisioned nodes. + */ + if (prov) { + min = 0U; + max = CONFIG_BLE_MESH_MAX_PROV_NODES; + } else { + min = CONFIG_BLE_MESH_MAX_PROV_NODES; + max = ARRAY_SIZE(mesh_nodes); + } + + for (i = min; i < max; i++) { + if (mesh_nodes[i] == NULL) { + mesh_nodes[i] = bt_mesh_calloc(sizeof(struct bt_mesh_node)); + if (!mesh_nodes[i]) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_provisioner_unlock(); + return -ENOMEM; + } + + memcpy(mesh_nodes[i], node, sizeof(struct bt_mesh_node)); + provisioner_node_count_inc(prov); + if (index) { + *index = i; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { + bt_mesh_store_node_info(mesh_nodes[i], prov); + } + + bt_mesh_provisioner_unlock(); + return 0; + } + } + + BT_ERR("%s, Node queue is full", __func__); + bt_mesh_provisioner_unlock(); + return -ENOMEM; +} + +int bt_mesh_provisioner_restore_node_info(struct bt_mesh_node *node, bool prov) +{ + if (!node) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + return provisioner_store_node(node, prov, false, NULL); +} + +int bt_mesh_provisioner_provision(const bt_mesh_addr_t *addr, const u8_t uuid[16], u16_t oob_info, + u16_t unicast_addr, u8_t element_num, u16_t net_idx, u8_t flags, + u32_t iv_index, const u8_t dev_key[16], u16_t *index) +{ + struct bt_mesh_node node = {0}; + + BT_DBG("%s", __func__); + + if (!addr || !uuid || !dev_key || !index) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + BT_INFO("unicast_addr 0x%04x, elem_num %d, net_idx 0x%04x", + unicast_addr, element_num, net_idx); + BT_INFO("dev_uuid %s", bt_hex(uuid, 16)); + BT_INFO("dev_key %s", bt_hex(dev_key, 16)); + + memcpy(node.addr, addr->val, BLE_MESH_ADDR_LEN); + node.addr_type = addr->type; + memcpy(node.dev_uuid, uuid, 16); + node.oob_info = oob_info; + node.unicast_addr = unicast_addr; + node.element_num = element_num; + node.net_idx = net_idx; + node.flags = flags; + node.iv_index = iv_index; + memcpy(node.dev_key, dev_key, 16); + + return provisioner_store_node(&node, true, true, index); +} + +static int provisioner_remove_node(u16_t index, bool erase) +{ + struct bt_mesh_node *node = NULL; + struct bt_mesh_rpl *rpl = NULL; + bool is_prov = false; + int i; + + BT_DBG("%s, reset node %d", __func__, index); + + bt_mesh_provisioner_lock(); + + if (mesh_nodes[index] == NULL) { + bt_mesh_provisioner_unlock(); + return 0; + } + + node = mesh_nodes[index]; + + /* Reset corresponding network cache when reset the node */ + bt_mesh_msg_cache_clear(node->unicast_addr, node->element_num); + + /* Reset corresponding rpl when removing the node */ + for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { + rpl = &bt_mesh.rpl[i]; + if (rpl->src >= node->unicast_addr && + rpl->src < node->unicast_addr + node->element_num) { + memset(rpl, 0, sizeof(struct bt_mesh_rpl)); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_rpl_single(node->unicast_addr); + } + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + bt_mesh_friend_remove_lpn(node->unicast_addr); + } + + is_prov = index < CONFIG_BLE_MESH_MAX_PROV_NODES ? true : false; + + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_node_info(node->unicast_addr, is_prov); + } + + if (mesh_nodes[index]->comp_data) { + bt_mesh_free(mesh_nodes[index]->comp_data); + } + bt_mesh_free(mesh_nodes[index]); + mesh_nodes[index] = NULL; + + provisioner_node_count_dec(is_prov); + + bt_mesh_provisioner_unlock(); + return 0; +} + +static struct bt_mesh_node *provisioner_find_node_with_uuid(const u8_t uuid[16], u16_t *index) +{ + int i; + + BT_DBG("%s", __func__); + + if (uuid == NULL) { + BT_ERR("%s, Invalid device uuid", __func__); + return NULL; + } + + bt_mesh_provisioner_lock(); + + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i] && !memcmp(mesh_nodes[i]->dev_uuid, uuid, 16)) { + if (index) { + *index = i; + } + bt_mesh_provisioner_unlock(); + return mesh_nodes[i]; + } + } + + bt_mesh_provisioner_unlock(); + return NULL; +} + +bool bt_mesh_provisioner_find_node_with_uuid(const u8_t uuid[16], bool reset) +{ + struct bt_mesh_node *node = NULL; + u16_t index = 0U; + + node = provisioner_find_node_with_uuid(uuid, &index); + if (!node) { + return false; + } + + if (reset) { + provisioner_remove_node(index, true); + } + return true; +} + +bool bt_mesh_provisioner_find_node_with_addr(const bt_mesh_addr_t *addr, bool reset) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i]) { + if (!memcmp(mesh_nodes[i]->addr, addr->val, BLE_MESH_ADDR_LEN) && + mesh_nodes[i]->addr_type == addr->type) { + if (reset) { + provisioner_remove_node(i, true); + } + return true; + } + } + } + + return false; +} + +int bt_mesh_provisioner_remove_node(const u8_t uuid[16]) +{ + struct bt_mesh_node *node = NULL; + u16_t index = 0U; + int i; + + if (uuid == NULL) { + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i]) { + provisioner_remove_node(i, true); + } + } + return 0; + } + + node = provisioner_find_node_with_uuid(uuid, &index); + if (!node) { + BT_WARN("Node %s not exist", bt_hex(uuid, 16)); + return -ENODEV; + } + + provisioner_remove_node(index, true); + return 0; +} + +static struct bt_mesh_node *provisioner_find_node_with_addr(u16_t addr, u16_t *index) +{ + struct bt_mesh_node *node = NULL; + int i; + + BT_DBG("%s", __func__); + + if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, addr); + return NULL; + } + + bt_mesh_provisioner_lock(); + + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + node = mesh_nodes[i]; + if (node && addr >= node->unicast_addr && + addr < (node->unicast_addr + node->element_num)) { + if (index) { + *index = i; + } + bt_mesh_provisioner_unlock(); + return node; + } + } + + bt_mesh_provisioner_unlock(); + return NULL; +} + +int bt_mesh_provisioner_restore_node_name(u16_t addr, const char *name) +{ + struct bt_mesh_node *node = NULL; + + node = provisioner_find_node_with_addr(addr, NULL); + if (node == NULL) { + BT_ERR("%s, Node 0x%04x not exist", __func__, addr); + return -ENODEV; + } + + strncpy(node->name, name, BLE_MESH_NODE_NAME_SIZE); + return 0; +} + +int bt_mesh_provisioner_restore_node_comp_data(u16_t addr, const u8_t *data, u16_t length, bool prov) +{ + struct bt_mesh_node *node = NULL; + + if (!data || length == 0U) { + BT_ERR("%s, Invalid comp data info", __func__); + return -EINVAL; + } + + node = provisioner_find_node_with_addr(addr, NULL); + if (node == NULL) { + BT_ERR("%s, Node 0x%04x not exist", __func__, addr); + return -ENODEV; + } + + node->comp_data = bt_mesh_calloc(length); + if (!node->comp_data) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + node->comp_length = length; + memcpy(node->comp_data, data, length); + + return 0; +} + +int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node) +{ + if (!node) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + return provisioner_store_node(node, false, true, NULL); +} + +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_uuid(const u8_t uuid[16]) +{ + return provisioner_find_node_with_uuid(uuid, NULL); +} + +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_addr(u16_t unicast_addr) +{ + return provisioner_find_node_with_addr(unicast_addr, NULL); +} + +int bt_mesh_provisioner_delete_node_with_uuid(const u8_t uuid[16]) +{ + struct bt_mesh_node *node = NULL; + u16_t index = 0U; + + node = provisioner_find_node_with_uuid(uuid, &index); + if (!node) { + BT_WARN("Node %s not exist", bt_hex(uuid, 16)); + return -ENODEV; + } + + provisioner_remove_node(index, true); + return 0; +} + +int bt_mesh_provisioner_delete_node_with_addr(u16_t unicast_addr) +{ + struct bt_mesh_node *node = NULL; + u16_t index = 0U; + + node = provisioner_find_node_with_addr(unicast_addr, &index); + if (!node) { + BT_WARN("Node 0x%04x not exist", unicast_addr); + return -ENODEV; + } + + provisioner_remove_node(index, true); + return 0; +} + +static int provisioner_check_node_index(u16_t index) +{ + BT_DBG("%s", __func__); + + if (index >= ARRAY_SIZE(mesh_nodes)) { + BT_ERR("%s, Too big node index %d", __func__, index); + return -EINVAL; + } + + if (mesh_nodes[index] == NULL) { + BT_ERR("%s, Node %d is not found", __func__, index); + return -ENODEV; + } + + return 0; +} + +int bt_mesh_provisioner_set_node_name(u16_t index, const char *name) +{ + size_t length = 0U, name_len = 0U; + int i; + + BT_DBG("%s", __func__); + + if (!name) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (provisioner_check_node_index(index)) { + BT_ERR("%s, Failed to check node index", __func__); + return -EINVAL; + } + + BT_DBG("name len is %d, name is %s", strlen(name), name); + + length = (strlen(name) <= BLE_MESH_NODE_NAME_SIZE) ? strlen(name) : BLE_MESH_NODE_NAME_SIZE; + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i]) { + name_len = strlen(mesh_nodes[i]->name); + if (length != name_len) { + continue; + } + if (!strncmp(mesh_nodes[i]->name, name, length)) { + BT_WARN("Node name %s already exists", name); + return -EEXIST; + } + } + } + + memset(mesh_nodes[index]->name, 0, BLE_MESH_NODE_NAME_SIZE); + + strncpy(mesh_nodes[index]->name, name, length); + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_node_name(mesh_nodes[index], + index < CONFIG_BLE_MESH_MAX_PROV_NODES ? true : false); + } + + return 0; +} + +const char *bt_mesh_provisioner_get_node_name(u16_t index) +{ + BT_DBG("%s", __func__); + + if (provisioner_check_node_index(index)) { + BT_ERR("%s, Failed to check node index", __func__); + return NULL; + } + + return mesh_nodes[index]->name; +} + +u16_t bt_mesh_provisioner_get_node_index(const char *name) +{ + size_t length = 0U, name_len = 0U; + int i; + + BT_DBG("%s", __func__); + + if (!name) { + BT_ERR("%s, Invalid parameter", __func__); + return BLE_MESH_INVALID_NODE_INDEX; + } + + length = (strlen(name) <= BLE_MESH_NODE_NAME_SIZE) ? strlen(name) : BLE_MESH_NODE_NAME_SIZE; + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i]) { + name_len = strlen(mesh_nodes[i]->name); + if (length != name_len) { + continue; + } + if (!strncmp(mesh_nodes[i]->name, name, length)) { + return (u16_t)i; + } + } + } + + BT_ERR("%s, Node name %s not exist", __func__, name); + return BLE_MESH_INVALID_NODE_INDEX; +} + +int bt_mesh_provisioner_store_node_comp_data(u16_t addr, const u8_t *data, u16_t length) +{ + struct bt_mesh_node *node = NULL; + u16_t index = 0U; + + if (!BLE_MESH_ADDR_IS_UNICAST(addr) || !data || + (length % 2) || length <= 14) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + node = provisioner_find_node_with_addr(addr, &index); + if (node == NULL) { + BT_ERR("%s, Node 0x%04x not exist", __func__, addr); + return -ENODEV; + } + + node->comp_data = bt_mesh_calloc(length); + if (node->comp_data == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + BT_DBG("%s, index %d", __func__, index); + + memcpy(node->comp_data, data, length); + node->comp_length = length; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_node_comp_data(node, + index < CONFIG_BLE_MESH_MAX_PROV_NODES ? true : false); + } + + return 0; +} + +/* Provisioner DevKey, NetKey and AppKey related functions */ + +const u8_t *bt_mesh_provisioner_net_key_get(u16_t net_idx) { struct bt_mesh_subnet *sub = NULL; int i; @@ -281,19 +790,19 @@ const u8_t *provisioner_net_key_get(u16_t net_idx) for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { sub = bt_mesh.p_sub[i]; - if (!sub || (sub->net_idx != net_idx)) { - continue; + if (sub && sub->net_idx == net_idx) { + if (sub->kr_flag) { + return sub->keys[1].net; + } else { + return sub->keys[0].net; + } } - if (sub->kr_flag) { - return sub->keys[1].net; - } - return sub->keys[0].net; } return NULL; } -struct bt_mesh_subnet *provisioner_subnet_get(u16_t net_idx) +struct bt_mesh_subnet *bt_mesh_provisioner_subnet_get(u16_t net_idx) { struct bt_mesh_subnet *sub = NULL; int i; @@ -306,30 +815,29 @@ struct bt_mesh_subnet *provisioner_subnet_get(u16_t net_idx) for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { sub = bt_mesh.p_sub[i]; - if (!sub || (sub->net_idx != net_idx)) { - continue; + if (sub && sub->net_idx == net_idx) { + return sub; } - return sub; } return NULL; } -bool provisioner_check_msg_dst_addr(u16_t dst_addr) +bool bt_mesh_provisioner_check_msg_dst(u16_t dst) { - struct bt_mesh_node_t *node = NULL; + struct bt_mesh_node *node = NULL; int i; BT_DBG("%s", __func__); - if (!BLE_MESH_ADDR_IS_UNICAST(dst_addr)) { + if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { return true; } for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { node = mesh_nodes[i]; - if (node && dst_addr >= node->unicast_addr && - dst_addr < node->unicast_addr + node->element_num) { + if (node && dst >= node->unicast_addr && + dst < node->unicast_addr + node->element_num) { return true; } } @@ -337,25 +845,25 @@ bool provisioner_check_msg_dst_addr(u16_t dst_addr) return false; } -const u8_t *provisioner_dev_key_get(u16_t dst_addr) +const u8_t *bt_mesh_provisioner_dev_key_get(u16_t dst) { /* Device key is only used to encrypt configuration messages. * Configuration model shall only be supported by the primary * element which uses the primary unicast address. */ - struct bt_mesh_node_t *node = NULL; + struct bt_mesh_node *node = NULL; int i; BT_DBG("%s", __func__); - if (!BLE_MESH_ADDR_IS_UNICAST(dst_addr)) { - BT_ERR("%s, Not a unicast address 0x%04x", __func__, dst_addr); + if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, dst); return NULL; } for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { node = mesh_nodes[i]; - if (node && node->unicast_addr == dst_addr) { + if (node && node->unicast_addr == dst) { return node->dev_key; } } @@ -363,7 +871,7 @@ const u8_t *provisioner_dev_key_get(u16_t dst_addr) return NULL; } -struct bt_mesh_app_key *provisioner_app_key_find(u16_t app_idx) +struct bt_mesh_app_key *bt_mesh_provisioner_app_key_find(u16_t app_idx) { struct bt_mesh_app_key *key = NULL; int i; @@ -372,10 +880,7 @@ struct bt_mesh_app_key *provisioner_app_key_find(u16_t app_idx) for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { key = bt_mesh.p_app_keys[i]; - if (!key) { - continue; - } - if (key->net_idx != BLE_MESH_KEY_UNUSED && + if (key && key->net_idx != BLE_MESH_KEY_UNUSED && key->app_idx == app_idx) { return key; } @@ -384,225 +889,6 @@ struct bt_mesh_app_key *provisioner_app_key_find(u16_t app_idx) return NULL; } -u32_t provisioner_get_prov_node_count(void) -{ - return mesh_node_count; -} - -/* The following APIs are for provisioner application use */ - -#if 0 -static int bt_mesh_provisioner_set_kr_flag(u16_t net_idx, bool kr_flag) -{ - struct bt_mesh_subnet *sub = NULL; - int i; - - for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { - sub = bt_mesh.p_sub[i]; - if (!sub || (sub->net_idx != net_idx)) { - continue; - } - sub->kr_flag = kr_flag; - break; - } - if (i == ARRAY_SIZE(bt_mesh.p_sub)) { - return -ENODEV; - } - - /* TODO: When kr_flag is changed, provisioner may need - * to change the netkey of the subnet and update - * corresponding appkey. - */ - - return 0; -} - -static void bt_mesh_provisioner_set_iv_index(u32_t iv_index) -{ - bt_mesh.iv_index = iv_index; - - /* TODO: When iv_index is changed, provisioner may need to - * start iv update procedure. And the ivu_initiator - * & iv_update flags may also need to be set. - */ -} -#endif - -int bt_mesh_provisioner_store_node_info(struct bt_mesh_node_t *node_info) -{ - struct bt_mesh_node_t *node = NULL; - int i; - - if (!node_info) { - BT_ERR("%s, Invalid parameter", __func__); - return -EINVAL; - } - - /* Check if the device uuid already exists */ - for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { - node = mesh_nodes[i]; - if (node && !memcmp(node->dev_uuid, node_info->dev_uuid, 16)) { - BT_WARN("%s, Node info already exists", __func__); - return -EEXIST; - } - } - - /* 0 ~ (CONFIG_BLE_MESH_MAX_PROV_NODES-1) are left for self-provisioned nodes */ - for (i = CONFIG_BLE_MESH_MAX_PROV_NODES; i < ARRAY_SIZE(mesh_nodes); i++) { - node = mesh_nodes[i]; - if (!node) { - node = osi_calloc(sizeof(struct bt_mesh_node_t)); - if (!node) { - BT_ERR("%s, Failed to allocate memory", __func__); - return -ENOMEM; - } - memcpy(node, node_info, sizeof(struct bt_mesh_node_t)); - mesh_nodes[i] = node; - mesh_node_count++; - return 0; - } - } - - BT_ERR("%s, Node info is full", __func__); - return -ENOMEM; -} - -int bt_mesh_provisioner_get_all_node_unicast_addr(struct net_buf_simple *buf) -{ - struct bt_mesh_node_t *node = NULL; - int i; - - if (!buf) { - BT_ERR("%s, Invalid parameter", __func__); - return -EINVAL; - } - - for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { - node = mesh_nodes[i]; - if (!node || !BLE_MESH_ADDR_IS_UNICAST(node->unicast_addr)) { - continue; - } - net_buf_simple_add_le16(buf, node->unicast_addr); - } - - return 0; -} - -int bt_mesh_provisioner_set_node_name(int node_index, const char *name) -{ - size_t length, name_len; - int i; - - BT_DBG("%s", __func__); - - if (!name) { - BT_ERR("%s, Invalid parameter", __func__); - return -EINVAL; - } - - if (provisioner_index_check(node_index)) { - BT_ERR("%s, Failed to check node index", __func__); - return -EINVAL; - } - - BT_DBG("name len is %d, name is %s", strlen(name), name); - - length = (strlen(name) <= MESH_NAME_SIZE) ? strlen(name) : MESH_NAME_SIZE; - for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { - if (!mesh_nodes[i] || !mesh_nodes[i]->node_name) { - continue; - } - name_len = strlen(mesh_nodes[i]->node_name); - if (length != name_len) { - continue; - } - if (!strncmp(mesh_nodes[i]->node_name, name, length)) { - BT_WARN("%s, Name %s already exists", __func__, name); - return -EEXIST; - } - } - - strncpy(mesh_nodes[node_index]->node_name, name, length); - - return 0; -} - -const char *bt_mesh_provisioner_get_node_name(int node_index) -{ - BT_DBG("%s", __func__); - - if (provisioner_index_check(node_index)) { - BT_ERR("%s, Failed to check node index", __func__); - return NULL; - } - - return mesh_nodes[node_index]->node_name; -} - -int bt_mesh_provisioner_get_node_index(const char *name) -{ - size_t length, name_len; - int i; - - BT_DBG("%s", __func__); - - if (!name) { - return -EINVAL; - } - - length = (strlen(name) <= MESH_NAME_SIZE) ? strlen(name) : MESH_NAME_SIZE; - for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { - if (!mesh_nodes[i] || !mesh_nodes[i]->node_name) { - continue; - } - name_len = strlen(mesh_nodes[i]->node_name); - if (length != name_len) { - continue; - } - if (!strncmp(mesh_nodes[i]->node_name, name, length)) { - return i; - } - } - - return -ENODEV; -} - -struct bt_mesh_node_t *bt_mesh_provisioner_get_node_info(u16_t unicast_addr) -{ - struct bt_mesh_node_t *node = NULL; - int i; - - BT_DBG("%s", __func__); - - if (!BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) { - BT_ERR("%s, Not a unicast address 0x%04x", __func__, unicast_addr); - return NULL; - } - - for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { - node = mesh_nodes[i]; - if (!node) { - continue; - } - if (unicast_addr >= node->unicast_addr && - unicast_addr < (node->unicast_addr + node->element_num)) { - return node; - } - } - - return NULL; -} - -u32_t bt_mesh_provisioner_get_net_key_count(void) -{ - return ARRAY_SIZE(bt_mesh.p_sub); -} - -u32_t bt_mesh_provisioner_get_app_key_count(void) -{ - return ARRAY_SIZE(bt_mesh.p_app_keys); -} - static int provisioner_check_app_key(const u8_t app_key[16], u16_t *app_idx) { struct bt_mesh_app_key *key = NULL; @@ -729,8 +1015,8 @@ static int provisioner_check_net_key_full(void) int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16], u16_t net_idx, u16_t *app_idx) { - struct bt_mesh_app_key *key = NULL; struct bt_mesh_app_keys *keys = NULL; + struct bt_mesh_app_key *key = NULL; u8_t p_key[16] = {0}; int add = -1; @@ -746,7 +1032,7 @@ int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16], u16_t net_idx, /* Check if the same application key already exists */ if (provisioner_check_app_key(app_key, app_idx)) { - BT_WARN("%s, AppKey already exists, AppKey Index updated", __func__); + BT_WARN("AppKey already exists, AppKey Index updated"); return 0; } @@ -777,7 +1063,7 @@ int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16], u16_t net_idx, memcpy(p_key, app_key, 16); } - key = osi_calloc(sizeof(struct bt_mesh_app_key)); + key = bt_mesh_calloc(sizeof(struct bt_mesh_app_key)); if (!key) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; @@ -786,7 +1072,7 @@ int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16], u16_t net_idx, keys = &key->keys[0]; if (bt_mesh_app_id(p_key, &keys->id)) { BT_ERR("%s, Failed to generate AID", __func__); - osi_free(key); + bt_mesh_free(key); return -EIO; } @@ -801,7 +1087,7 @@ int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16], u16_t net_idx, key->app_idx = (++bt_mesh.p_app_idx_next); if (key->app_idx >= 0x1000) { BT_ERR("%s, No AppKey Index available", __func__); - osi_free(key); + bt_mesh_free(key); return -EIO; } } else { @@ -814,6 +1100,54 @@ int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16], u16_t net_idx, bt_mesh.p_app_keys[add] = key; + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_p_app_idx(); + bt_mesh_store_p_app_key(key); + } + + return 0; +} + +int bt_mesh_provisioner_local_app_key_update(const u8_t app_key[16], u16_t net_idx, u16_t app_idx) +{ + struct bt_mesh_app_keys *keys = NULL; + struct bt_mesh_app_key *key = NULL; + + if (app_key == NULL) { + BT_ERR("%s, Invalid AppKey", __func__); + return -EINVAL; + } + + BT_INFO("AppKey %s, net_idx 0x%03x, app_idx 0x%03x", bt_hex(app_key, 16), net_idx, app_idx); + + /* Check if the net_idx exists */ + if (provisioner_check_net_idx(net_idx, false)) { + BT_ERR("%s, NetKey Index 0x%03x not exist", __func__, net_idx); + return -ENODEV; + } + + key = bt_mesh_provisioner_app_key_find(app_idx); + if (key == NULL) { + BT_ERR("%s, AppKey 0x%03x not exist", __func__, app_idx); + return -ENODEV; + } + + keys = &key->keys[0]; + if (bt_mesh_app_id(app_key, &keys->id)) { + BT_ERR("%s, Failed to generate AID", __func__); + return -EIO; + } + + memset(keys->val, 0, 16); + memcpy(keys->val, app_key, 16); + + key->updated = false; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_p_app_idx(); + bt_mesh_store_p_app_key(key); + } + return 0; } @@ -848,6 +1182,64 @@ const u8_t *bt_mesh_provisioner_local_app_key_get(u16_t net_idx, u16_t app_idx) return NULL; } +static void model_pub_clear(struct bt_mesh_model *model) +{ + if (!model->pub) { + return; + } + + if (model->pub->addr == BLE_MESH_ADDR_UNASSIGNED) { + return; + } + + model->pub->addr = BLE_MESH_ADDR_UNASSIGNED; + model->pub->key = 0U; + model->pub->cred = 0U; + model->pub->ttl = 0U; + model->pub->period = 0U; + model->pub->retransmit = 0U; + model->pub->count = 0U; + + if (model->pub->update) { + k_delayed_work_cancel(&model->pub->timer); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_pub(model); + } + + return; +} + +static void model_unbind(struct bt_mesh_model *model, u16_t app_idx) +{ + int i; + + BT_DBG("model %p key_idx 0x%03x", model, app_idx); + + for (i = 0; i < ARRAY_SIZE(model->keys); i++) { + if (model->keys[i] != app_idx) { + continue; + } + + model->keys[i] = BLE_MESH_KEY_UNUSED; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_bind(model); + } + + model_pub_clear(model); + } +} + +static void _model_unbind(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, + bool vnd, bool primary, void *user_data) +{ + u16_t app_idx = *(u16_t *)user_data; + + model_unbind(mod, app_idx); +} + int bt_mesh_provisioner_local_app_key_delete(u16_t net_idx, u16_t app_idx) { struct bt_mesh_app_key *key = NULL; @@ -869,7 +1261,14 @@ int bt_mesh_provisioner_local_app_key_delete(u16_t net_idx, u16_t app_idx) key = bt_mesh.p_app_keys[i]; if (key && key->net_idx == net_idx && key->app_idx == app_idx) { - osi_free(bt_mesh.p_app_keys[i]); + /* Remove the AppKey from the models if they are bound with it */ + bt_mesh_model_foreach(_model_unbind, &app_idx); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_p_app_key(key); + } + + bt_mesh_free(bt_mesh.p_app_keys[i]); bt_mesh.p_app_keys[i] = NULL; return 0; } @@ -897,7 +1296,7 @@ int bt_mesh_provisioner_local_net_key_add(const u8_t net_key[16], u16_t *net_idx /* Check if the same network key already exists */ if (provisioner_check_net_key(net_key, net_idx)) { - BT_WARN("%s, NetKey already exists, NetKey Index updated", __func__); + BT_WARN("NetKey already exists, NetKey Index updated"); return 0; } @@ -922,7 +1321,7 @@ int bt_mesh_provisioner_local_net_key_add(const u8_t net_key[16], u16_t *net_idx memcpy(p_key, net_key, 16); } - sub = osi_calloc(sizeof(struct bt_mesh_subnet)); + sub = bt_mesh_calloc(sizeof(struct bt_mesh_subnet)); if (!sub) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; @@ -930,7 +1329,7 @@ int bt_mesh_provisioner_local_net_key_add(const u8_t net_key[16], u16_t *net_idx if (bt_mesh_net_keys_create(&sub->keys[0], p_key)) { BT_ERR("%s, Failed to generate NID", __func__); - osi_free(sub); + bt_mesh_free(sub); return -EIO; } @@ -943,7 +1342,7 @@ int bt_mesh_provisioner_local_net_key_add(const u8_t net_key[16], u16_t *net_idx sub->net_idx = (++bt_mesh.p_net_idx_next); if (sub->net_idx >= 0x1000) { BT_ERR("%s, No NetKey Index available", __func__); - osi_free(sub); + bt_mesh_free(sub); return -EIO; } } else { @@ -958,6 +1357,55 @@ int bt_mesh_provisioner_local_net_key_add(const u8_t net_key[16], u16_t *net_idx bt_mesh.p_sub[add] = sub; + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_p_net_idx(); + bt_mesh_store_p_subnet(sub); + } + + return 0; +} + +int bt_mesh_provisioner_local_net_key_update(const u8_t net_key[16], u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + int err = 0; + + if (net_key == NULL) { + BT_ERR("%s, Invalid NetKey", __func__); + return -EINVAL; + } + + BT_INFO("NetKey %s, net_idx 0x%03x", bt_hex(net_key, 16), net_idx); + + sub = bt_mesh_provisioner_subnet_get(net_idx); + if (sub == NULL) { + BT_ERR("%s, NetKey 0x%03x not exist", __func__, net_idx); + return -ENODEV; + } + + err = bt_mesh_net_keys_create(&sub->keys[0], net_key); + if (err) { + BT_ERR("%s, Failed to generate NID", __func__); + return -EIO; + } + + memset(sub->keys[0].net, 0, 16); + memcpy(sub->keys[0].net, net_key, 16); + + sub->kr_phase = BLE_MESH_KR_NORMAL; + sub->kr_flag = false; + sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; + + err = bt_mesh_net_beacon_update(sub); + if (err) { + BT_ERR("%s, Failed to update secure beacon", __func__); + return -EIO; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_p_subnet(sub); + } + return 0; } @@ -989,7 +1437,7 @@ const u8_t *bt_mesh_provisioner_local_net_key_get(u16_t net_idx) int bt_mesh_provisioner_local_net_key_delete(u16_t net_idx) { struct bt_mesh_subnet *sub = NULL; - int i; + int i, j; BT_DBG("%s", __func__); @@ -1001,7 +1449,19 @@ int bt_mesh_provisioner_local_net_key_delete(u16_t net_idx) for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { sub = bt_mesh.p_sub[i]; if (sub && sub->net_idx == net_idx) { - osi_free(bt_mesh.p_sub[i]); + /* Delete any app keys bound to this NetKey index */ + for (j = 0; j < ARRAY_SIZE(bt_mesh.p_app_keys); j++) { + struct bt_mesh_app_key *key = bt_mesh.p_app_keys[j]; + if (key->net_idx == sub->net_idx) { + bt_mesh_provisioner_local_app_key_delete(key->net_idx, key->app_idx); + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_p_subnet(sub); + } + + bt_mesh_free(bt_mesh.p_sub[i]); bt_mesh.p_sub[i] = NULL; return 0; } @@ -1011,38 +1471,15 @@ int bt_mesh_provisioner_local_net_key_delete(u16_t net_idx) return -ENODEV; } -int bt_mesh_provisioner_get_own_unicast_addr(u16_t *addr, u8_t *elem_num) -{ - if (!addr || !elem_num || !prov || !comp) { - BT_ERR("%s, Invalid parameter", __func__); - return -EINVAL; - } - - *addr = prov->prov_unicast_addr; - *elem_num = comp->elem_count; - - return 0; -} - int bt_mesh_provisioner_bind_local_model_app_idx(u16_t elem_addr, u16_t mod_id, u16_t cid, u16_t app_idx) { - struct bt_mesh_elem *elem = NULL; struct bt_mesh_model *model = NULL; + struct bt_mesh_elem *elem = NULL; int i; - if (!comp) { - BT_ERR("%s, NULL composition data", __func__); - return -EINVAL; - } - - for (i = 0; i < comp->elem_count; i++) { - elem = &comp->elem[i]; - if (elem->addr == elem_addr) { - break; - } - } - if (i == comp->elem_count) { + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { BT_ERR("%s, No element is found", __func__); return -ENODEV; } @@ -1064,7 +1501,7 @@ int bt_mesh_provisioner_bind_local_model_app_idx(u16_t elem_addr, u16_t mod_id, for (i = 0; i < ARRAY_SIZE(model->keys); i++) { if (model->keys[i] == app_idx) { - BT_WARN("%s, AppKey Index is already binded with model", __func__); + BT_WARN("AppKey 0x%03x is already bound to model", app_idx); return 0; } } @@ -1072,6 +1509,10 @@ int bt_mesh_provisioner_bind_local_model_app_idx(u16_t elem_addr, u16_t mod_id, for (i = 0; i < ARRAY_SIZE(model->keys); i++) { if (model->keys[i] == BLE_MESH_KEY_UNUSED) { model->keys[i] = app_idx; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_bind(model); + } return 0; } } @@ -1080,63 +1521,38 @@ int bt_mesh_provisioner_bind_local_model_app_idx(u16_t elem_addr, u16_t mod_id, return -ENOMEM; } -int bt_mesh_provisioner_bind_local_app_net_idx(u16_t net_idx, u16_t app_idx) +int bt_mesh_print_local_composition_data(void) { - struct bt_mesh_app_key *key = NULL; - int i; - - BT_DBG("%s", __func__); - - if (provisioner_check_net_idx(net_idx, false)) { - BT_ERR("%s, NetKey Index does not exist", __func__); - return -ENODEV; - } - - if (provisioner_check_app_idx(app_idx, false)) { - BT_ERR("%s, AppKey Index does not exist", __func__); - return -ENODEV; - } - - for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { - key = bt_mesh.p_app_keys[i]; - if (!key || (key->app_idx != app_idx)) { - continue; - } - key->net_idx = net_idx; - return 0; - } - - return -ENODEV; -} - -int bt_mesh_provisioner_print_local_element_info(void) -{ - struct bt_mesh_elem *elem = NULL; + const struct bt_mesh_comp *comp = NULL; struct bt_mesh_model *model = NULL; + struct bt_mesh_elem *elem = NULL; int i, j; + comp = bt_mesh_comp_get(); if (!comp) { BT_ERR("%s, NULL composition data", __func__); return -EINVAL; } - BT_WARN("************************************************"); - BT_WARN("* cid: 0x%04x pid: 0x%04x vid: 0x%04x *", comp->cid, comp->pid, comp->vid); - BT_WARN("* Element Number: 0x%02x *", comp->elem_count); + BT_INFO("************************************************"); + BT_INFO("* cid: 0x%04x pid: 0x%04x vid: 0x%04x *", comp->cid, comp->pid, comp->vid); + BT_INFO("* Element Number: 0x%02x *", comp->elem_count); for (i = 0; i < comp->elem_count; i++) { elem = &comp->elem[i]; - BT_WARN("* Element %d: 0x%04x *", i, elem->addr); - BT_WARN("* Loc: 0x%04x NumS: 0x%02x NumV: 0x%02x *", elem->loc, elem->model_count, elem->vnd_model_count); + BT_INFO("* Element %d: 0x%04x *", i, elem->addr); + BT_INFO("* Loc: 0x%04x NumS: 0x%02x NumV: 0x%02x *", elem->loc, elem->model_count, elem->vnd_model_count); for (j = 0; j < elem->model_count; j++) { model = &elem->models[j]; - BT_WARN("* sig_model %d: id - 0x%04x *", j, model->id); + BT_INFO("* sig_model %d: id - 0x%04x *", j, model->id); } for (j = 0; j < elem->vnd_model_count; j++) { model = &elem->vnd_models[j]; - BT_WARN("* vnd_model %d: id - 0x%04x, cid - 0x%04x *", j, model->vnd.id, model->vnd.company); + BT_INFO("* vnd_model %d: id - 0x%04x, cid - 0x%04x *", j, model->vnd.id, model->vnd.company); } } - BT_WARN("************************************************"); + BT_INFO("************************************************"); + + ((void) model); return 0; } @@ -1147,24 +1563,25 @@ int bt_mesh_provisioner_print_local_element_info(void) #if CONFIG_BLE_MESH_FAST_PROV -const u8_t *fast_prov_dev_key_get(u16_t addr) +const u8_t *bt_mesh_fast_prov_dev_key_get(u16_t dst) { - struct bt_mesh_node_t *node = NULL; + struct bt_mesh_node *node = NULL; + int i; BT_DBG("%s", __func__); - if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { - BT_ERR("%s, Not a unicast address 0x%04x", __func__, addr); + if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, dst); return NULL; } - if (addr == bt_mesh_primary_addr()) { + if (dst == bt_mesh_primary_addr()) { return bt_mesh.dev_key; } - for (int i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { node = mesh_nodes[i]; - if (node && node->unicast_addr == addr) { + if (node && node->unicast_addr == dst) { return node->dev_key; } } @@ -1172,20 +1589,21 @@ const u8_t *fast_prov_dev_key_get(u16_t addr) return NULL; } -struct bt_mesh_subnet *fast_prov_subnet_get(u16_t net_idx) +struct bt_mesh_subnet *bt_mesh_fast_prov_subnet_get(u16_t net_idx) { struct bt_mesh_subnet *sub = NULL; + int i; BT_DBG("%s", __func__); - for (int i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { sub = &bt_mesh.sub[i]; if (sub->net_idx == net_idx) { return sub; } } - for (int i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { sub = bt_mesh.p_sub[i]; if (sub && sub->net_idx == net_idx) { return sub; @@ -1195,22 +1613,25 @@ struct bt_mesh_subnet *fast_prov_subnet_get(u16_t net_idx) return NULL; } -struct bt_mesh_app_key *fast_prov_app_key_find(u16_t net_idx, u16_t app_idx) +struct bt_mesh_app_key *bt_mesh_fast_prov_app_key_find(u16_t app_idx) { struct bt_mesh_app_key *key = NULL; + int i; BT_DBG("%s", __func__); - for (int i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { + for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { key = &bt_mesh.app_keys[i]; - if (key->net_idx == net_idx && key->app_idx == app_idx) { + if (key->net_idx != BLE_MESH_KEY_UNUSED && + key->app_idx == app_idx) { return key; } } - for (int i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { key = bt_mesh.p_app_keys[i]; - if (key && key->net_idx == net_idx && key->app_idx == app_idx) { + if (key && key->net_idx != BLE_MESH_KEY_UNUSED && + key->app_idx == app_idx) { return key; } } @@ -1220,27 +1641,27 @@ struct bt_mesh_app_key *fast_prov_app_key_find(u16_t net_idx, u16_t app_idx) u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx) { - struct bt_mesh_subnet *sub = NULL; struct bt_mesh_subnet_keys *key = NULL; + struct bt_mesh_subnet *sub = NULL; - sub = fast_prov_subnet_get(net_idx); + sub = bt_mesh_fast_prov_subnet_get(net_idx); if (sub) { key = BLE_MESH_KEY_REFRESH(sub->kr_flag) ? &sub->keys[1] : &sub->keys[0]; - return provisioner_set_fast_prov_net_idx(key->net, net_idx); + return bt_mesh_provisioner_set_fast_prov_net_idx(key->net, net_idx); } /* If net_idx is not found, set net_idx to fast_prov first, * and wait for primary provisioner to add net_key */ - return provisioner_set_fast_prov_net_idx(NULL, net_idx); + return bt_mesh_provisioner_set_fast_prov_net_idx(NULL, net_idx); } u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]) { const u8_t *keys = NULL; - u16_t net_idx; - int err; + u16_t net_idx = 0U; + int err = 0; - net_idx = provisioner_get_fast_prov_net_idx(); + net_idx = bt_mesh_provisioner_get_fast_prov_net_idx(); bt_mesh.p_net_idx_next = net_idx; err = bt_mesh_provisioner_local_net_key_add(net_key, &net_idx); @@ -1253,14 +1674,14 @@ u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]) return 0x01; /* status: add net_key fail */ } - return provisioner_set_fast_prov_net_idx(keys, net_idx); + return bt_mesh_provisioner_set_fast_prov_net_idx(keys, net_idx); } const u8_t *bt_mesh_get_fast_prov_net_key(u16_t net_idx) { struct bt_mesh_subnet *sub = NULL; - sub = fast_prov_subnet_get(net_idx); + sub = bt_mesh_fast_prov_subnet_get(net_idx); if (!sub) { BT_ERR("%s, Failed to get subnet", __func__); return NULL; @@ -1273,7 +1694,7 @@ const u8_t *bt_mesh_get_fast_prov_app_key(u16_t net_idx, u16_t app_idx) { struct bt_mesh_app_key *key = NULL; - key = fast_prov_app_key_find(net_idx, app_idx); + key = bt_mesh_fast_prov_app_key_find(app_idx); if (!key) { BT_ERR("%s, Failed to get AppKey", __func__); return NULL; diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h index 6fe4150a8..fabe9399e 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h @@ -15,101 +15,120 @@ #ifndef _PROVISIONER_MAIN_H_ #define _PROVISIONER_MAIN_H_ -#include "mesh_util.h" -#include "mesh_kernel.h" -#include "mesh_access.h" #include "net.h" +#include "mesh_bearer_adapt.h" -#define MESH_NAME_SIZE 31 +#define BLE_MESH_INVALID_NODE_INDEX 0xFFFF +#define BLE_MESH_NODE_NAME_SIZE 31 /* Each node information stored by provisioner */ -struct bt_mesh_node_t { - char node_name[MESH_NAME_SIZE]; /* Node name */ - u8_t dev_uuid[16]; /* Device UUID pointer, stored in provisioner_prov.c */ - u16_t oob_info; /* Node OOB information */ - u16_t unicast_addr; /* Node unicast address */ - u8_t element_num; /* Node element number */ - u16_t net_idx; /* Node provision net_idx */ - u8_t flags; /* Node key refresh flag and iv update flag */ - u32_t iv_index; /* Node IV Index */ - u8_t dev_key[16]; /* Node device key */ +struct bt_mesh_node { + /* Device information */ + u8_t addr[6]; /* Node device address */ + u8_t addr_type; /* Node device address type */ + u8_t dev_uuid[16]; /* Node Device UUID */ + u16_t oob_info; /* Node OOB information */ + + /* Provisioning information */ + u16_t unicast_addr; /* Node unicast address */ + u8_t element_num; /* Node element number */ + u16_t net_idx; /* Node NetKey Index */ + u8_t flags; /* Node key refresh flag and iv update flag */ + u32_t iv_index; /* Node IV Index */ + u8_t dev_key[16]; /* Node device key */ + + /* Additional information */ + char name[BLE_MESH_NODE_NAME_SIZE]; /* Node name */ + u16_t comp_length; /* Length of Composition Data */ + u8_t *comp_data; /* Value of Composition Data */ } __packed; -/* The following APIs are for key init, node provision & node reset. */ +int bt_mesh_provisioner_init(void); -int provisioner_node_provision(int node_index, const u8_t uuid[16], u16_t oob_info, - u16_t unicast_addr, u8_t element_num, u16_t net_idx, - u8_t flags, u32_t iv_index, const u8_t dev_key[16]); +int bt_mesh_provisioner_net_create(void); -int provisioner_node_reset(int node_index); +int bt_mesh_provisioner_deinit(bool erase); -int provisioner_upper_reset_all_nodes(void); +bool bt_mesh_provisioner_check_is_addr_dup(u16_t addr, u8_t elem_num, bool comp_with_own); -int provisioner_upper_init(void); +u16_t bt_mesh_provisioner_get_prov_node_count(void); -/* The following APIs are for provisioner upper layers internal usage. */ +u16_t bt_mesh_provisioner_get_all_node_count(void); -const u8_t *provisioner_net_key_get(u16_t net_idx); +int bt_mesh_provisioner_restore_node_info(struct bt_mesh_node *node, bool prov); -struct bt_mesh_subnet *provisioner_subnet_get(u16_t net_idx); +int bt_mesh_provisioner_provision(const bt_mesh_addr_t *addr, const u8_t uuid[16], u16_t oob_info, + u16_t unicast_addr, u8_t element_num, u16_t net_idx, u8_t flags, + u32_t iv_index, const u8_t dev_key[16], u16_t *index); -bool provisioner_check_msg_dst_addr(u16_t dst_addr); +bool bt_mesh_provisioner_find_node_with_uuid(const u8_t uuid[16], bool reset); -const u8_t *provisioner_dev_key_get(u16_t dst_addr); +bool bt_mesh_provisioner_find_node_with_addr(const bt_mesh_addr_t *addr, bool reset); -struct bt_mesh_app_key *provisioner_app_key_find(u16_t app_idx); +int bt_mesh_provisioner_remove_node(const u8_t uuid[16]); -u32_t provisioner_get_prov_node_count(void); +int bt_mesh_provisioner_restore_node_name(u16_t addr, const char *name); -/* The following APIs are for provisioner application use. */ +int bt_mesh_provisioner_restore_node_comp_data(u16_t addr, const u8_t *data, u16_t length, bool prov); -int bt_mesh_provisioner_store_node_info(struct bt_mesh_node_t *node_info); +int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node); -int bt_mesh_provisioner_get_all_node_unicast_addr(struct net_buf_simple *buf); +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_uuid(const u8_t uuid[16]); -int bt_mesh_provisioner_set_node_name(int node_index, const char *name); +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_addr(u16_t unicast_addr); -const char *bt_mesh_provisioner_get_node_name(int node_index); +int bt_mesh_provisioner_delete_node_with_uuid(const u8_t uuid[16]); -int bt_mesh_provisioner_get_node_index(const char *name); +int bt_mesh_provisioner_delete_node_with_addr(u16_t unicast_addr); -struct bt_mesh_node_t *bt_mesh_provisioner_get_node_info(u16_t unicast_addr); +int bt_mesh_provisioner_set_node_name(u16_t index, const char *name); -u32_t bt_mesh_provisioner_get_net_key_count(void); +const char *bt_mesh_provisioner_get_node_name(u16_t index); -u32_t bt_mesh_provisioner_get_app_key_count(void); +u16_t bt_mesh_provisioner_get_node_index(const char *name); + +int bt_mesh_provisioner_store_node_comp_data(u16_t addr, const u8_t *data, u16_t length); + +const u8_t *bt_mesh_provisioner_net_key_get(u16_t net_idx); + +struct bt_mesh_subnet *bt_mesh_provisioner_subnet_get(u16_t net_idx); + +bool bt_mesh_provisioner_check_msg_dst(u16_t dst); + +const u8_t *bt_mesh_provisioner_dev_key_get(u16_t dst); + +struct bt_mesh_app_key *bt_mesh_provisioner_app_key_find(u16_t app_idx); int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16], u16_t net_idx, u16_t *app_idx); +int bt_mesh_provisioner_local_app_key_update(const u8_t app_key[16], u16_t net_idx, u16_t app_idx); + const u8_t *bt_mesh_provisioner_local_app_key_get(u16_t net_idx, u16_t app_idx); int bt_mesh_provisioner_local_app_key_delete(u16_t net_idx, u16_t app_idx); int bt_mesh_provisioner_local_net_key_add(const u8_t net_key[16], u16_t *net_idx); +int bt_mesh_provisioner_local_net_key_update(const u8_t net_key[16], u16_t net_idx); + const u8_t *bt_mesh_provisioner_local_net_key_get(u16_t net_idx); int bt_mesh_provisioner_local_net_key_delete(u16_t net_idx); -int bt_mesh_provisioner_get_own_unicast_addr(u16_t *addr, u8_t *elem_num); - /* Provisioner bind local client model with proper appkey index */ int bt_mesh_provisioner_bind_local_model_app_idx(u16_t elem_addr, u16_t mod_id, u16_t cid, u16_t app_idx); -/* This API can be used to change the net_idx binded with the app_idx. */ -int bt_mesh_provisioner_bind_local_app_net_idx(u16_t net_idx, u16_t app_idx); - /* Provisioner print own element information */ -int bt_mesh_provisioner_print_local_element_info(void); +int bt_mesh_print_local_composition_data(void); /* The following APIs are for fast provisioning */ -const u8_t *fast_prov_dev_key_get(u16_t dst_addr); +const u8_t *bt_mesh_fast_prov_dev_key_get(u16_t dst); -struct bt_mesh_subnet *fast_prov_subnet_get(u16_t net_idx); +struct bt_mesh_subnet *bt_mesh_fast_prov_subnet_get(u16_t net_idx); -struct bt_mesh_app_key *fast_prov_app_key_find(u16_t net_idx, u16_t app_idx); +struct bt_mesh_app_key *bt_mesh_fast_prov_app_key_find(u16_t app_idx); u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx); diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c index dbe593ba9..8df45ed4c 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c @@ -15,19 +15,14 @@ #include #include -#include "sdkconfig.h" -#include "osi/allocator.h" -#include "osi/mutex.h" - -#include "mesh_main.h" -#include "mesh_trace.h" -#include "mesh_bearer_adapt.h" - #include "crypto.h" #include "adv.h" #include "mesh.h" -#include "provisioner_prov.h" +#include "access.h" +#include "settings.h" +#include "mesh_common.h" #include "proxy_client.h" +#include "provisioner_prov.h" #include "provisioner_main.h" #if CONFIG_BLE_MESH_PROVISIONER @@ -140,8 +135,9 @@ struct prov_link { u8_t auth_method; /* choosed authentication method */ u8_t auth_action; /* choosed authentication action */ u8_t auth_size; /* choosed authentication size */ - u16_t unicast_addr; /* unicast address assigned for device */ - bt_mesh_addr_t addr; /* Device address */ + u16_t assign_addr; /* Application assigned address for the device */ + u16_t unicast_addr; /* unicast address allocated for device */ + bt_mesh_addr_t addr; /* Device address */ #if defined(CONFIG_BLE_MESH_PB_GATT) bool connecting; /* start connecting with device */ struct bt_mesh_conn *conn; /* GATT connection */ @@ -204,6 +200,8 @@ struct prov_link { #define BLE_MESH_PROV_SAME_TIME \ (CONFIG_BLE_MESH_PBA_SAME_TIME + CONFIG_BLE_MESH_PBG_SAME_TIME) +#define PROV_MAX_ADDR_TO_ASSIGN 0x7FFF + static struct prov_link link[BLE_MESH_PROV_SAME_TIME]; struct prov_rx { @@ -212,26 +210,27 @@ struct prov_rx { u8_t gpc; }; -#define BLE_MESH_ALREADY_PROV_NUM (CONFIG_BLE_MESH_MAX_PROV_NODES + 10) +struct bt_mesh_prov_ctx { + /* Primary element address of Provisioner */ + u16_t primary_addr; + + /* Provisioning bearers used by Provisioner */ + bt_mesh_prov_bearer_t bearers; -struct prov_ctx_t { /* If provisioning random have been generated, set BIT0 to 1 */ u8_t rand_gen_done; /* Provisioner random */ u8_t random[16]; - /* Number of provisioned devices */ - u16_t node_count; - /* Current number of PB-ADV provisioned devices simultaneously */ u8_t pba_count; /* Current number of PB-GATT provisioned devices simultaneously */ u8_t pbg_count; - /* Current unicast address going to assigned */ - u16_t current_addr; + /* Current unicast address going to allocated */ + u16_t curr_alloc_addr; /* Current net_idx going to be used in provisioning data */ u16_t curr_net_idx; @@ -242,6 +241,12 @@ struct prov_ctx_t { /* Current iv_index going to be used in provisioning data */ u16_t curr_iv_index; + /* Length of Static OOB value */ + u8_t static_oob_len; + + /* Static OOB value */ + u8_t static_oob_val[16]; + /* Offset of the device uuid to be matched, based on zero */ u8_t match_offset; @@ -249,45 +254,39 @@ struct prov_ctx_t { u8_t match_length; /* Value of the device uuid to be matched */ - u8_t *match_value; + u8_t match_value[16]; /* Indicate when received uuid_match adv_pkts, can provision it at once */ bool prov_after_match; +#if defined(CONFIG_BLE_MESH_PB_ADV) /* Mutex used to protect the PB-ADV procedure */ - osi_mutex_t pb_adv_lock; + bt_mesh_mutex_t pb_adv_lock; + /* Mutex used to protect the adv buf during PB-ADV procedure */ + bt_mesh_mutex_t pb_buf_lock; +#endif + +#if defined(CONFIG_BLE_MESH_PB_GATT) /* Mutex used to protect the PB-GATT procedure */ - osi_mutex_t pb_gatt_lock; + bt_mesh_mutex_t pb_gatt_lock; +#endif - /** This structure is used to store the information of the device which - * provisioner has successfully sent provisioning data to. In this - * structure, we don't care if the device is currently in the mesh - * network, or has been removed, or failed to send provisioning - * complete pdu after receiving the provisioning data pdu. - */ - struct already_prov_info { - u8_t uuid[16]; /* device uuid */ - u8_t element_num; /* element number of the deleted node */ - u16_t unicast_addr; /* Primary unicast address of the deleted node */ - } already_prov[BLE_MESH_ALREADY_PROV_NUM]; + /* Fast provisioning related information */ + struct { + bool enable; + u16_t net_idx; + const u8_t *net_key; + u8_t flags; + u32_t iv_index; + u16_t unicast_addr_min; + u16_t unicast_addr_max; + } fast_prov; }; -static struct prov_ctx_t prov_ctx; +static struct bt_mesh_prov_ctx prov_ctx; -struct prov_node_info { - bool provisioned; /* device provisioned flag */ - bt_mesh_addr_t addr; /* device address */ - u8_t uuid[16]; /* node uuid */ - u16_t oob_info; /* oob info contained in adv pkt */ - u8_t element_num; /* element contained in this node */ - u16_t unicast_addr; /* primary unicast address of this node */ - u16_t net_idx; /* Netkey index got during provisioning */ - u8_t flags; /* Key refresh flag and iv update flag */ - u32_t iv_index; /* IV Index */ -}; - -static struct prov_node_info prov_nodes[CONFIG_BLE_MESH_MAX_PROV_NODES]; +#define FAST_PROV_ENABLE() (prov_ctx.fast_prov.enable) struct unprov_dev_queue { bt_mesh_addr_t addr; @@ -303,7 +302,7 @@ struct unprov_dev_queue { }, }; -static unprov_adv_pkt_cb_t notify_unprov_adv_pkt_cb; +static unprov_adv_pkt_cb_t notify_unprov_adv_pkt_cb; #define BUF_TIMEOUT K_MSEC(400) @@ -346,30 +345,87 @@ static struct prov_adv_buf { } adv_buf[CONFIG_BLE_MESH_PBA_SAME_TIME]; static u8_t adv_buf_data[ADV_BUF_SIZE * CONFIG_BLE_MESH_PBA_SAME_TIME]; -#endif +#endif /* CONFIG_BLE_MESH_PB_ADV */ #define PROV_FREE_MEM(_idx, member) \ { \ if (link[_idx].member) { \ - osi_free(link[_idx].member); \ + bt_mesh_free(link[_idx].member); \ + link[_idx].member = NULL; \ } \ } -/* Fast provisioning uses this structure for provisioning data */ -static struct bt_mesh_fast_prov_info { - u16_t net_idx; - const u8_t *net_key; - u8_t flags; - u32_t iv_index; - u16_t unicast_addr_min; - u16_t unicast_addr_max; -} fast_prov_info; +#if defined(CONFIG_BLE_MESH_PB_ADV) +static void bt_mesh_pb_adv_mutex_new(void) +{ + if (!prov_ctx.pb_adv_lock.mutex) { + bt_mesh_mutex_create(&prov_ctx.pb_adv_lock); + } +} -static bool fast_prov_flag; +static void bt_mesh_pb_adv_mutex_free(void) +{ + bt_mesh_mutex_free(&prov_ctx.pb_adv_lock); +} -#define FAST_PROV_FLAG_GET() fast_prov_flag +static void bt_mesh_pb_adv_lock(void) +{ + bt_mesh_mutex_lock(&prov_ctx.pb_adv_lock); +} -void provisioner_pbg_count_dec(void) +static void bt_mesh_pb_adv_unlock(void) +{ + bt_mesh_mutex_unlock(&prov_ctx.pb_adv_lock); +} + +static void bt_mesh_pb_buf_mutex_new(void) +{ + if (!prov_ctx.pb_buf_lock.mutex) { + bt_mesh_mutex_create(&prov_ctx.pb_buf_lock); + } +} + +static void bt_mesh_pb_buf_mutex_free(void) +{ + bt_mesh_mutex_free(&prov_ctx.pb_buf_lock); +} + +static void bt_mesh_pb_buf_lock(void) +{ + bt_mesh_mutex_lock(&prov_ctx.pb_buf_lock); +} + +static void bt_mesh_pb_buf_unlock(void) +{ + bt_mesh_mutex_unlock(&prov_ctx.pb_buf_lock); +} +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +#if defined(CONFIG_BLE_MESH_PB_GATT) +static void bt_mesh_pb_gatt_mutex_new(void) +{ + if (!prov_ctx.pb_gatt_lock.mutex) { + bt_mesh_mutex_create(&prov_ctx.pb_gatt_lock); + } +} + +static void bt_mesh_pb_gatt_mutex_free(void) +{ + bt_mesh_mutex_free(&prov_ctx.pb_gatt_lock); +} + +static void bt_mesh_pb_gatt_lock(void) +{ + bt_mesh_mutex_lock(&prov_ctx.pb_gatt_lock); +} + +static void bt_mesh_pb_gatt_unlock(void) +{ + bt_mesh_mutex_unlock(&prov_ctx.pb_gatt_lock); +} +#endif /* CONFIG_BLE_MESH_PB_GATT */ + +void bt_mesh_provisioner_pbg_count_dec(void) { if (prov_ctx.pbg_count) { prov_ctx.pbg_count--; @@ -382,9 +438,9 @@ static inline void provisioner_pbg_count_inc(void) } #if defined(CONFIG_BLE_MESH_PB_GATT) -void provisioner_clear_link_conn_info(const u8_t addr[6]) +void bt_mesh_provisioner_clear_link_info(const u8_t addr[6]) { - u8_t i; + int i; if (!addr) { BT_ERR("%s, Invalid parameter", __func__); @@ -408,31 +464,20 @@ void provisioner_clear_link_conn_info(const u8_t addr[6]) } } - BT_WARN("%s, Address %s is not found", __func__, bt_hex(addr, BLE_MESH_ADDR_LEN)); + BT_WARN("Device address %s is not found", bt_hex(addr, BLE_MESH_ADDR_LEN)); return; } #endif -const struct bt_mesh_prov *provisioner_get_prov_info(void) +const struct bt_mesh_prov *bt_mesh_provisioner_get_prov_info(void) { return prov; } -int provisioner_prov_reset_all_nodes(void) +void bt_mesh_provisoner_restore_prov_info(u16_t primary_addr, u16_t alloc_addr) { - u16_t i; - - BT_DBG("%s", __func__); - - for (i = 0U; i < ARRAY_SIZE(prov_nodes); i++) { - if (prov_nodes[i].provisioned) { - memset(&prov_nodes[i], 0, sizeof(struct prov_node_info)); - } - } - - prov_ctx.node_count = 0; - - return 0; + prov_ctx.primary_addr = primary_addr; + prov_ctx.curr_alloc_addr = alloc_addr; } static int provisioner_dev_find(const bt_mesh_addr_t *addr, const u8_t uuid[16], u16_t *index) @@ -440,7 +485,7 @@ static int provisioner_dev_find(const bt_mesh_addr_t *addr, const u8_t uuid[16], bool uuid_match = false; bool addr_match = false; u8_t zero[16] = {0}; - u16_t i = 0, j = 0; + u16_t i = 0U, j = 0U; int comp = 0; if (addr) { @@ -497,7 +542,7 @@ static int provisioner_dev_find(const bt_mesh_addr_t *addr, const u8_t uuid[16], static bool is_unprov_dev_being_provision(const u8_t uuid[16]) { - u16_t i; + int i; #if defined(CONFIG_BLE_MESH_FAST_PROV) /** @@ -507,17 +552,13 @@ static bool is_unprov_dev_being_provision(const u8_t uuid[16]) * Unprovisioned Device Beacon when Transaction ACK for Provisioning Complete * is received). So in Fast Provisioning the Provisioner should ignore this. */ - for (i = 0U; i < ARRAY_SIZE(prov_nodes); i++) { - if (prov_nodes[i].provisioned) { - if (!memcmp(prov_nodes[i].uuid, uuid, 16)) { - BT_WARN("Device has already been provisioned"); - return -EALREADY; - } - } + if (bt_mesh_provisioner_find_node_with_uuid(uuid, false)) { + BT_WARN("Device has already been provisioned"); + return true; } #endif - for (i = 0U; i < BLE_MESH_PROV_SAME_TIME; i++) { + for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) { #if defined(CONFIG_BLE_MESH_PB_ADV) && defined(CONFIG_BLE_MESH_PB_GATT) if (link[i].linking || link[i].connecting || bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { @@ -538,7 +579,7 @@ static bool is_unprov_dev_being_provision(const u8_t uuid[16]) static bool is_unprov_dev_uuid_match(const u8_t uuid[16]) { - if (prov_ctx.match_length && prov_ctx.match_value) { + if (prov_ctx.match_length) { if (memcmp(uuid + prov_ctx.match_offset, prov_ctx.match_value, prov_ctx.match_length)) { return false; @@ -548,10 +589,8 @@ static bool is_unprov_dev_uuid_match(const u8_t uuid[16]) return true; } -static int provisioner_check_unprov_dev_info(const u8_t uuid[16]) +static int provisioner_check_unprov_dev_info(const u8_t uuid[16], bt_mesh_prov_bearer_t bearer) { - u16_t i; - if (!uuid) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; @@ -559,7 +598,7 @@ static int provisioner_check_unprov_dev_info(const u8_t uuid[16]) /* Check if the device uuid matches configured value */ if (is_unprov_dev_uuid_match(uuid) == false) { - BT_DBG("%s, Device uuid is not matched", __func__); + BT_DBG("%s, Device uuid mismatch", __func__); return -EIO; } @@ -573,23 +612,26 @@ static int provisioner_check_unprov_dev_info(const u8_t uuid[16]) return -EALREADY; } - /* Check if the device has already been provisioned */ - for (i = 0U; i < ARRAY_SIZE(prov_nodes); i++) { - if (prov_nodes[i].provisioned) { - if (!memcmp(prov_nodes[i].uuid, uuid, 16)) { - BT_WARN("Provisioned before, start to provision again"); - provisioner_node_reset(i); - memset(&prov_nodes[i], 0, sizeof(struct prov_node_info)); - if (prov_ctx.node_count) { - prov_ctx.node_count--; - } - return 0; - } - } + /* Check if the current PB-ADV link is full */ + if (bearer == BLE_MESH_PROV_ADV && prov_ctx.pba_count == CONFIG_BLE_MESH_PBA_SAME_TIME) { + BT_INFO("Current PB-ADV links reach max limit"); + return -ENOMEM; } - /* Check if the prov_nodes queue is full */ - if (prov_ctx.node_count == ARRAY_SIZE(prov_nodes)) { + /* Check if the current PB-GATT link is full */ + if (bearer == BLE_MESH_PROV_GATT && prov_ctx.pbg_count == CONFIG_BLE_MESH_PBG_SAME_TIME) { + BT_INFO("Current PB-GATT links reach max limit"); + return -ENOMEM; + } + + /* Check if the device has already been provisioned */ + if (bt_mesh_provisioner_find_node_with_uuid(uuid, false)) { + BT_WARN("Provisioned before, start to provision again"); + return 0; + } + + /* Check if the provisioned nodes queue is full */ + if (bt_mesh_provisioner_get_prov_node_count() == CONFIG_BLE_MESH_MAX_PROV_NODES) { BT_WARN("Current provisioned devices reach max limit"); return -ENOMEM; } @@ -598,28 +640,37 @@ static int provisioner_check_unprov_dev_info(const u8_t uuid[16]) } #if defined(CONFIG_BLE_MESH_PB_ADV) -static int provisioner_start_prov_pb_adv(const u8_t uuid[16], - const bt_mesh_addr_t *addr, u16_t oob_info) +static int provisioner_start_prov_pb_adv(const u8_t uuid[16], const bt_mesh_addr_t *addr, + u16_t oob_info, u16_t assign_addr) { u8_t zero[6] = {0}; - int addr_cmp; - u8_t i; + int addr_cmp = 0; + int i; if (!uuid || !addr) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } - osi_mutex_lock(&prov_ctx.pb_adv_lock, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_pb_adv_lock(); + + /* If the unicast address of the node is going to be allocated internally, + * then we need to check if there are addresses can be allocated. + */ + if (assign_addr == BLE_MESH_ADDR_UNASSIGNED && + prov_ctx.curr_alloc_addr == BLE_MESH_ADDR_UNASSIGNED) { + BT_ERR("No available unicast address to assign"); + return -EIO; + } if (is_unprov_dev_being_provision(uuid)) { - osi_mutex_unlock(&prov_ctx.pb_adv_lock); + bt_mesh_pb_adv_unlock(); return -EALREADY; } addr_cmp = memcmp(addr->val, zero, BLE_MESH_ADDR_LEN); - for (i = 0U; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { + for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { if (!bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE) && !link[i].linking) { memcpy(link[i].uuid, uuid, 16); link[i].oob_info = oob_info; @@ -628,34 +679,50 @@ static int provisioner_start_prov_pb_adv(const u8_t uuid[16], memcpy(link[i].addr.val, addr->val, BLE_MESH_ADDR_LEN); } send_link_open(i); - osi_mutex_unlock(&prov_ctx.pb_adv_lock); + /* If the application layer assigned a specific unicast address for the device, + * then Provisioner will use this address in the Provisoning Data PDU. + */ + if (BLE_MESH_ADDR_IS_UNICAST(assign_addr)) { + link[i].assign_addr = assign_addr; + } + bt_mesh_pb_adv_unlock(); return 0; } } BT_ERR("%s, No PB-ADV link is available", __func__); - osi_mutex_unlock(&prov_ctx.pb_adv_lock); + bt_mesh_pb_adv_unlock(); + return -ENOMEM; } #endif /* CONFIG_BLE_MESH_PB_ADV */ #if defined(CONFIG_BLE_MESH_PB_GATT) -static int provisioner_start_prov_pb_gatt(const u8_t uuid[16], - const bt_mesh_addr_t *addr, u16_t oob_info) +static int provisioner_start_prov_pb_gatt(const u8_t uuid[16], const bt_mesh_addr_t *addr, + u16_t oob_info, u16_t assign_addr) { u8_t zero[6] = {0}; - int addr_cmp; - u8_t i; + int addr_cmp = 0; + int i; if (!uuid || !addr) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } - osi_mutex_lock(&prov_ctx.pb_gatt_lock, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_pb_gatt_lock(); + + /* If the unicast address of the node is going to be allocated internally, + * then we need to check if there are addresses can be allocated. + */ + if (assign_addr == BLE_MESH_ADDR_UNASSIGNED && + prov_ctx.curr_alloc_addr == BLE_MESH_ADDR_UNASSIGNED) { + BT_ERR("No available unicast address to assign"); + return -EIO; + } if (is_unprov_dev_being_provision(uuid)) { - osi_mutex_unlock(&prov_ctx.pb_gatt_lock); + bt_mesh_pb_gatt_unlock(); return -EALREADY; } @@ -673,19 +740,26 @@ static int provisioner_start_prov_pb_gatt(const u8_t uuid[16], memset(link[i].uuid, 0, 16); link[i].oob_info = 0x0; memset(&link[i].addr, 0, sizeof(bt_mesh_addr_t)); - osi_mutex_unlock(&prov_ctx.pb_gatt_lock); + bt_mesh_pb_gatt_unlock(); return -EIO; } + /* If the application layer assigned a specific unicast address for the device, + * then Provisioner will use this address in the Provisoning Data PDU. + */ + if (BLE_MESH_ADDR_IS_UNICAST(assign_addr)) { + link[i].assign_addr = assign_addr; + } /* If creating connection successfully, set connecting flag to 1 */ link[i].connecting = true; provisioner_pbg_count_inc(); - osi_mutex_unlock(&prov_ctx.pb_gatt_lock); + bt_mesh_pb_gatt_unlock(); return 0; } } BT_ERR("%s, No PB-GATT link is available", __func__); - osi_mutex_unlock(&prov_ctx.pb_gatt_lock); + bt_mesh_pb_gatt_unlock(); + return -ENOMEM; } #endif /* CONFIG_BLE_MESH_PB_GATT */ @@ -696,8 +770,8 @@ int bt_mesh_provisioner_add_unprov_dev(struct bt_mesh_unprov_dev_add *add_dev, u u8_t zero[16] = {0}; int addr_cmp = 0; int uuid_cmp = 0; - u16_t i; - int err; + u16_t i = 0U; + int err = 0; if (!add_dev) { BT_ERR("%s, Invalid parameter", __func__); @@ -773,7 +847,7 @@ int bt_mesh_provisioner_add_unprov_dev(struct bt_mesh_unprov_dev_add *add_dev, u memcpy(unprov_dev[i].uuid, add_dev->uuid, 16); } unprov_dev[i].bearer = add_dev->bearer & BIT_MASK(2); - unprov_dev[i].flags = flags & BIT_MASK(3); + unprov_dev[i].flags = flags & BIT_MASK(3); goto start; } @@ -803,35 +877,106 @@ start: } /* Check if current provisioned node count + active link reach max limit */ - if (prov_ctx.node_count + prov_ctx.pba_count + \ - prov_ctx.pbg_count >= ARRAY_SIZE(prov_nodes)) { - BT_WARN("%s, Node count + active link count reach max limit", __func__); + if (bt_mesh_provisioner_get_prov_node_count() + prov_ctx.pba_count + \ + prov_ctx.pbg_count >= CONFIG_BLE_MESH_MAX_PROV_NODES) { + BT_ERR("Node count + active link count reach max limit"); return -EIO; } - if ((err = provisioner_check_unprov_dev_info(add_dev->uuid))) { + if ((err = provisioner_check_unprov_dev_info(add_dev->uuid, add_dev->bearer))) { return err; } - if (add_dev->bearer & BLE_MESH_PROV_ADV) { + if (add_dev->bearer == BLE_MESH_PROV_ADV) { #if defined(CONFIG_BLE_MESH_PB_ADV) - if (prov_ctx.pba_count == CONFIG_BLE_MESH_PBA_SAME_TIME) { - BT_WARN("%s, Current PB-ADV links reach max limit", __func__); - return -EIO; - } if ((err = provisioner_start_prov_pb_adv( - add_dev->uuid, &add_addr, add_dev->oob_info))) { + add_dev->uuid, &add_addr, add_dev->oob_info, BLE_MESH_ADDR_UNASSIGNED))) { return err; } #endif - } else if (add_dev->bearer & BLE_MESH_PROV_GATT) { + } else if (add_dev->bearer == BLE_MESH_PROV_GATT) { #if defined(CONFIG_BLE_MESH_PB_GATT) - if (prov_ctx.pbg_count == CONFIG_BLE_MESH_PBG_SAME_TIME) { - BT_WARN("%s, Current PB-GATT links reach max limit", __func__); - return -EIO; - } if ((err = provisioner_start_prov_pb_gatt( - add_dev->uuid, &add_addr, add_dev->oob_info))) { + add_dev->uuid, &add_addr, add_dev->oob_info, BLE_MESH_ADDR_UNASSIGNED))) { + return err; + } +#endif + } + + return 0; +} + +int bt_mesh_provisioner_prov_device_with_addr(const u8_t uuid[16], const u8_t addr[6], + u8_t addr_type, bt_mesh_prov_bearer_t bearer, + u16_t oob_info, u16_t unicast_addr) +{ + bt_mesh_addr_t dev_addr = {0}; + int err = 0; + + if (uuid == NULL) { + BT_ERR("%s, NULL device uuid", __func__); + return -EINVAL; + } + + if (bearer != BLE_MESH_PROV_ADV && bearer != BLE_MESH_PROV_GATT) { + BT_ERR("%s, Invalid provisioning bearer 0x%02x", __func__, bearer); + return -EINVAL; + } + + if (!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && bearer == BLE_MESH_PROV_ADV) { + BT_ERR("%s, Not support PB-ADV", __func__); + return -ENOTSUP; + } + + if (!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && bearer == BLE_MESH_PROV_GATT) { + BT_ERR("%s, Not support PB-GATT", __func__); + return -ENOTSUP; + } + + if (bearer == BLE_MESH_PROV_GATT && (addr == NULL || addr_type > BLE_MESH_ADDR_RANDOM)) { + BT_ERR("%s, Invalid device address info", __func__); + return -EINVAL; + } + + if (!BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) { + BT_ERR("%s, Invalid unicast address 0x%04x", __func__, unicast_addr); + return -EINVAL; + } + + /* Here we will not check if the assigned unicast address is overlapped + * with the unicast addresses of other nodes or Provisioner, beacuse: + * 1. At this moment, the element number of the device is unknown + * 2. If the node is a reprovisioned device, then the original allocated + * unicast address will be used. + * 3. Some other devices may be just being provisioning, and currently we + * can not know the exactly allocated addresses of them. + */ + + /* Check if current provisioned node count + active link reach max limit */ + if (bt_mesh_provisioner_get_prov_node_count() + prov_ctx.pba_count + \ + prov_ctx.pbg_count >= CONFIG_BLE_MESH_MAX_PROV_NODES) { + BT_ERR("Node count + active link count reach max limit"); + return -EIO; + } + + if ((err = provisioner_check_unprov_dev_info(uuid, bearer))) { + return err; + } + + if (addr) { + dev_addr.type = addr_type; + memcpy(dev_addr.val, addr, BLE_MESH_ADDR_LEN); + } + + if (bearer == BLE_MESH_PROV_ADV) { +#if defined(CONFIG_BLE_MESH_PB_ADV) + if ((err = provisioner_start_prov_pb_adv(uuid, &dev_addr, oob_info, unicast_addr))) { + return err; + } +#endif + } else if (bearer == BLE_MESH_PROV_GATT) { +#if defined(CONFIG_BLE_MESH_PB_GATT) + if ((err = provisioner_start_prov_pb_gatt(uuid, &dev_addr, oob_info, unicast_addr))) { return err; } #endif @@ -848,7 +993,7 @@ int bt_mesh_provisioner_delete_device(struct bt_mesh_device_delete *del_dev) * 2. device is being provisioned, need to close link & remove from device queue. * 3. device is been provisioned, need to send config_node_reset and may need to * remove from device queue. config _node_reset can be added in function - * provisioner_node_reset() in provisioner_main.c. + * provisioner_reset_node() in provisioner_main.c. */ bt_mesh_addr_t del_addr = {0}; u8_t zero[16] = {0}; @@ -856,8 +1001,8 @@ int bt_mesh_provisioner_delete_device(struct bt_mesh_device_delete *del_dev) bool uuid_match = false; int addr_cmp = 0; int uuid_cmp = 0; - u16_t i; - int err; + u16_t i = 0U; + int err = 0; if (!del_dev) { BT_ERR("%s, Invalid parameter", __func__); @@ -903,25 +1048,15 @@ int bt_mesh_provisioner_delete_device(struct bt_mesh_device_delete *del_dev) } /* Third: find if the device is been provisioned */ - for (i = 0U; i < ARRAY_SIZE(prov_nodes); i++) { - if (addr_cmp && (del_dev->addr_type <= BLE_MESH_ADDR_RANDOM)) { - if (!memcmp(prov_nodes[i].addr.val, del_dev->addr, BLE_MESH_ADDR_LEN) && - prov_nodes[i].addr.type == del_dev->addr_type) { - addr_match = true; - } + if (addr_cmp && (del_dev->addr_type <= BLE_MESH_ADDR_RANDOM)) { + if (bt_mesh_provisioner_find_node_with_addr(&del_addr, true)) { + return 0; } - if (uuid_cmp) { - if (!memcmp(prov_nodes[i].uuid, del_dev->uuid, 16)) { - uuid_match = true; - } - } - if (addr_match || uuid_match) { - memset(&prov_nodes[i], 0, sizeof(struct prov_node_info)); - provisioner_node_reset(i); - if (prov_ctx.node_count) { - prov_ctx.node_count--; - } - break; + } + + if (uuid_cmp) { + if (bt_mesh_provisioner_find_node_with_uuid(del_dev->uuid, true)) { + return 0; } } @@ -936,13 +1071,7 @@ int bt_mesh_provisioner_set_dev_uuid_match(u8_t offset, u8_t length, return -EINVAL; } - if (length && !prov_ctx.match_value) { - prov_ctx.match_value = osi_calloc(16); - if (!prov_ctx.match_value) { - BT_ERR("%s, Failed to allocate memory", __func__); - return -ENOMEM; - } - } + (void)memset(prov_ctx.match_value, 0, 16); prov_ctx.match_offset = offset; prov_ctx.match_length = length; @@ -954,7 +1083,7 @@ int bt_mesh_provisioner_set_dev_uuid_match(u8_t offset, u8_t length, return 0; } -int bt_mesh_prov_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb) +int bt_mesh_provisioner_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb) { if (!cb) { BT_ERR("%s, Invalid parameter", __func__); @@ -974,7 +1103,7 @@ int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info) } if (info->flag & NET_IDX_FLAG) { - key = provisioner_net_key_get(info->net_idx); + key = bt_mesh_provisioner_net_key_get(info->net_idx); if (!key) { BT_ERR("%s, Failed to get NetKey", __func__); return -EINVAL; @@ -989,29 +1118,154 @@ int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info) return 0; } -/* The following APIs are for fast provisioning */ - -void provisioner_set_fast_prov_flag(bool flag) +int bt_mesh_provisioner_set_prov_info(void) { - fast_prov_flag = flag; + const struct bt_mesh_comp *comp = NULL; + + if (prov_ctx.primary_addr == BLE_MESH_ADDR_UNASSIGNED) { + /* If unicast address of primary element of Provisioner has not been set + * before, then the following initilization procedure will be used. + */ + if (!BLE_MESH_ADDR_IS_UNICAST(prov->prov_unicast_addr) || + !BLE_MESH_ADDR_IS_UNICAST(prov->prov_start_address)) { + BT_ERR("%s, Invalid address, own 0x%04x, start 0x%04x", + __func__, prov->prov_unicast_addr, prov->prov_start_address); + return -EINVAL; + } + + comp = bt_mesh_comp_get(); + if (!comp) { + BT_ERR("%s, NULL composition data", __func__); + return -EINVAL; + } + + if (prov->prov_unicast_addr + comp->elem_count > prov->prov_start_address) { + BT_WARN("Too small start address 0x%04x, update to 0x%04x", + prov->prov_start_address, prov->prov_unicast_addr + comp->elem_count); + prov_ctx.curr_alloc_addr = prov->prov_unicast_addr + comp->elem_count; + } else { + prov_ctx.curr_alloc_addr = prov->prov_start_address; + } + prov_ctx.primary_addr = prov->prov_unicast_addr; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.curr_alloc_addr); + } + } + prov_ctx.curr_net_idx = BLE_MESH_KEY_PRIMARY; + prov_ctx.curr_flags = prov->flags; + prov_ctx.curr_iv_index = prov->iv_index; + + return 0; } -u8_t provisioner_set_fast_prov_net_idx(const u8_t *net_key, u16_t net_idx) +void bt_mesh_provisioner_set_prov_bearer(bt_mesh_prov_bearer_t bearers, bool clear) { - fast_prov_info.net_idx = net_idx; - fast_prov_info.net_key = net_key; + if (clear == false) { + prov_ctx.bearers |= bearers; + } else { + prov_ctx.bearers &= ~bearers; + } +} + +bt_mesh_prov_bearer_t bt_mesh_provisioner_get_prov_bearer(void) +{ + return prov_ctx.bearers; +} + +int bt_mesh_provisioner_set_static_oob_value(const u8_t *value, u8_t length) +{ + int i; + + if (value == NULL || length == 0U || length > 16U) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + /* Make sure Static OOB is not being used. */ + for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) { + if (link[i].auth_method == AUTH_METHOD_STATIC) { + BT_ERR("%s, Static OOB is being used", __func__); + return -EINVAL; + } + } + + (void)memset(prov_ctx.static_oob_val, 0, 16); + + prov_ctx.static_oob_len = MIN(16, length); + memcpy(prov_ctx.static_oob_val, value, prov_ctx.static_oob_len); + + return 0; +} + +u16_t bt_mesh_provisioner_get_primary_elem_addr(void) +{ + return prov_ctx.primary_addr; +} + +int bt_mesh_provisioner_set_primary_elem_addr(u16_t addr) +{ + const struct bt_mesh_comp *comp = NULL; + + if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { + BT_ERR("Invalid primary address 0x%04x", addr); + return -EINVAL; + } + + comp = bt_mesh_comp_get(); + if (!comp) { + BT_ERR("NULL composition data"); + return -EINVAL; + } + + /* Make sure Provisioner address is not identical with the addresses of nodes */ + if (bt_mesh_provisioner_check_is_addr_dup(addr, comp->elem_count, false)) { + BT_ERR("Address 0x%04x is duplicated with node address", addr); + return -EINVAL; + } + + /* If the current can-be allocated address is bigger than primary address + + * element number, then the curr_alloc_addr will not be changed, and only + * the Provisioner related addresses will be updated. + */ + if (addr + comp->elem_count > prov_ctx.curr_alloc_addr) { + prov_ctx.curr_alloc_addr = addr + comp->elem_count; + } + BT_INFO("Provisioner primary address updated, old 0x%04x, new 0x%04x", prov_ctx.primary_addr, addr); + prov_ctx.primary_addr = addr; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.curr_alloc_addr); + } + + bt_mesh_comp_provision(addr); + + return 0; +} + +/* The following APIs are for fast provisioning */ + +void bt_mesh_provisioner_fast_prov_enable(bool enable) +{ + prov_ctx.fast_prov.enable = enable; +} + +u8_t bt_mesh_provisioner_set_fast_prov_net_idx(const u8_t *net_key, u16_t net_idx) +{ + prov_ctx.fast_prov.net_idx = net_idx; + prov_ctx.fast_prov.net_key = net_key; if (!net_key) { - BT_WARN("%s, Wait for NetKey for fast provisioning", __func__); + BT_WARN("Wait for NetKey for fast provisioning"); return 0x01; /*status: wait for net_key */ } return 0x0; /* status: success */ } -u16_t provisioner_get_fast_prov_net_idx(void) +u16_t bt_mesh_provisioner_get_fast_prov_net_idx(void) { - return fast_prov_info.net_idx; + return prov_ctx.fast_prov.net_idx; } u8_t bt_mesh_set_fast_prov_unicast_addr_range(u16_t min, u16_t max) @@ -1026,15 +1280,15 @@ u8_t bt_mesh_set_fast_prov_unicast_addr_range(u16_t min, u16_t max) return 0x02; /* status: min is bigger than max */ } - if (min <= fast_prov_info.unicast_addr_max) { + if (min <= prov_ctx.fast_prov.unicast_addr_max) { BT_ERR("%s, Address overlap", __func__); return 0x03; /* status: address overlaps with current value */ } - fast_prov_info.unicast_addr_min = min; - fast_prov_info.unicast_addr_max = max; + prov_ctx.fast_prov.unicast_addr_min = min; + prov_ctx.fast_prov.unicast_addr_max = max; - prov_ctx.current_addr = fast_prov_info.unicast_addr_min; + prov_ctx.curr_alloc_addr = prov_ctx.fast_prov.unicast_addr_min; return 0x0; /* status: success */ } @@ -1042,8 +1296,8 @@ u8_t bt_mesh_set_fast_prov_unicast_addr_range(u16_t min, u16_t max) void bt_mesh_set_fast_prov_flags_iv_index(u8_t flags, u32_t iv_index) { /* BIT0: Key Refreash flag, BIT1: IV Update flag */ - fast_prov_info.flags = flags & BIT_MASK(2); - fast_prov_info.iv_index = iv_index; + prov_ctx.fast_prov.flags = flags & BIT_MASK(2); + prov_ctx.fast_prov.iv_index = iv_index; } #if defined(CONFIG_BLE_MESH_PB_ADV) @@ -1086,9 +1340,11 @@ static struct bt_mesh_send_cb buf_sent_cb = { static void free_segments(const u8_t idx) { - u8_t i; + int i; - for (i = 0U; i < ARRAY_SIZE(link[idx].tx.buf); i++) { + bt_mesh_pb_buf_lock(); + + for (i = 0; i < ARRAY_SIZE(link[idx].tx.buf); i++) { struct net_buf *buf = link[idx].tx.buf[i]; if (!buf) { @@ -1101,6 +1357,8 @@ static void free_segments(const u8_t idx) BLE_MESH_ADV(buf)->busy = 0U; net_buf_unref(buf); } + + bt_mesh_pb_buf_unlock(); } static void prov_clear_tx(const u8_t idx) @@ -1151,7 +1409,7 @@ static void reset_link(const u8_t idx, u8_t reason) static struct net_buf *adv_buf_create(void) { - struct net_buf *buf; + struct net_buf *buf = NULL; buf = bt_mesh_adv_create(BLE_MESH_ADV_PROV, PROV_XMIT, BUF_TIMEOUT); if (!buf) { @@ -1176,8 +1434,8 @@ static void gen_prov_ack_send(const u8_t idx, u8_t xact_id) static const struct bt_mesh_send_cb cb = { .start = ack_complete, }; - const struct bt_mesh_send_cb *complete; - struct net_buf *buf; + const struct bt_mesh_send_cb *complete = NULL; + struct net_buf *buf = NULL; BT_DBG("xact_id %u", xact_id); @@ -1208,11 +1466,11 @@ static void gen_prov_ack_send(const u8_t idx, u8_t xact_id) static void send_reliable(const u8_t idx) { - u8_t i; + int i; link[idx].tx.start = k_uptime_get(); - for (i = 0U; i < ARRAY_SIZE(link[idx].tx.buf); i++) { + for (i = 0; i < ARRAY_SIZE(link[idx].tx.buf); i++) { struct net_buf *buf = link[idx].tx.buf[i]; if (!buf) { @@ -1229,7 +1487,7 @@ static void send_reliable(const u8_t idx) static int bearer_ctl_send(const u8_t idx, u8_t op, void *data, u8_t data_len) { - struct net_buf *buf; + struct net_buf *buf = NULL; BT_DBG("op 0x%02x data_len %u", op, data_len); @@ -1266,14 +1524,14 @@ static int bearer_ctl_send(const u8_t idx, u8_t op, void *data, u8_t data_len) static void send_link_open(const u8_t idx) { - u8_t j; + int j; /** Generate link ID, and may need to check if this id is * currently being used, which may will not happen ever. */ bt_mesh_rand(&link[idx].link_id, sizeof(u32_t)); while (1) { - for (j = 0U; j < CONFIG_BLE_MESH_PBA_SAME_TIME; j++) { + for (j = 0; j < CONFIG_BLE_MESH_PBA_SAME_TIME; j++) { if (bt_mesh_atomic_test_bit(link[j].flags, LINK_ACTIVE) || link[j].linking) { if (link[idx].link_id == link[j].link_id) { bt_mesh_rand(&link[idx].link_id, sizeof(u32_t)); @@ -1335,9 +1593,9 @@ static inline u8_t next_transaction_id(const u8_t idx) static int prov_send_adv(const u8_t idx, struct net_buf_simple *msg) { - struct net_buf *start, *buf; - u8_t seg_len, seg_id; - u8_t xact_id; + struct net_buf *start = NULL, *buf = NULL; + u8_t seg_len = 0U, seg_id = 0U; + u8_t xact_id = 0U; s32_t timeout = PROVISION_TIMEOUT; BT_DBG("%s, len %u: %s", __func__, msg->len, bt_hex(msg->data, msg->len)); @@ -1412,7 +1670,7 @@ static int prov_send_adv(const u8_t idx, struct net_buf_simple *msg) #if defined(CONFIG_BLE_MESH_PB_GATT) static int prov_send_gatt(const u8_t idx, struct net_buf_simple *msg) { - int err; + int err = 0; if (!link[idx].conn) { return -ENOTCONN; @@ -1497,13 +1755,13 @@ static void send_invite(const u8_t idx) static void prov_capabilities(const u8_t idx, const u8_t *data) { PROV_BUF(buf, 6); - u16_t algorithms, output_action, input_action; - u8_t element_num, pub_key_oob, static_oob, - output_size, input_size; - u8_t auth_method, auth_action, auth_size; + u16_t algorithms = 0U, output_action = 0U, input_action = 0U; + u8_t element_num = 0U, pub_key_oob = 0U, static_oob = 0U, + output_size = 0U, input_size = 0U; + u8_t auth_method = 0U, auth_action = 0U, auth_size = 0U; element_num = data[0]; - BT_DBG("Elements: %u", element_num); + BT_INFO("Elements: 0x%02x", element_num); if (!element_num) { BT_ERR("%s, Invalid element number", __func__); goto fail; @@ -1511,14 +1769,14 @@ static void prov_capabilities(const u8_t idx, const u8_t *data) link[idx].element_num = element_num; algorithms = sys_get_be16(&data[1]); - BT_DBG("Algorithms: %u", algorithms); + BT_INFO("Algorithms: 0x%04x", algorithms); if (algorithms != BIT(PROV_ALG_P256)) { BT_ERR("%s, Invalid algorithms", __func__); goto fail; } pub_key_oob = data[3]; - BT_DBG("Public Key Type: 0x%02x", pub_key_oob); + BT_INFO("Public Key Type: 0x%02x", pub_key_oob); if (pub_key_oob > 0x01) { BT_ERR("%s, Invalid public key type", __func__); goto fail; @@ -1527,22 +1785,22 @@ static void prov_capabilities(const u8_t idx, const u8_t *data) prov->prov_pub_key_oob_cb) ? pub_key_oob : 0x00); static_oob = data[4]; - BT_DBG("Static OOB Type: 0x%02x", static_oob); + BT_INFO("Static OOB Type: 0x%02x", static_oob); if (static_oob > 0x01) { BT_ERR("%s, Invalid Static OOB type", __func__); goto fail; } - static_oob = (prov->prov_static_oob_val ? static_oob : 0x00); + static_oob = (prov_ctx.static_oob_len ? static_oob : 0x00); output_size = data[5]; - BT_DBG("Output OOB Size: %u", output_size); + BT_INFO("Output OOB Size: 0x%02x", output_size); if (output_size > 0x08) { BT_ERR("%s, Invalid Output OOB size", __func__); goto fail; } output_action = sys_get_be16(&data[6]); - BT_DBG("Output OOB Action: 0x%04x", output_action); + BT_INFO("Output OOB Action: 0x%04x", output_action); if (output_action > 0x1f) { BT_ERR("%s, Invalid Output OOB action", __func__); goto fail; @@ -1557,14 +1815,14 @@ static void prov_capabilities(const u8_t idx, const u8_t *data) } input_size = data[8]; - BT_DBG("Input OOB Size: %u", input_size); + BT_INFO("Input OOB Size: 0x%02x", input_size); if (input_size > 0x08) { BT_ERR("%s, Invalid Input OOB size", __func__); goto fail; } input_action = sys_get_be16(&data[9]); - BT_DBG("Input OOB Action: 0x%04x", input_action); + BT_INFO("Input OOB Action: 0x%04x", input_action); if (input_action > 0x0f) { BT_ERR("%s, Invalid Input OOB action", __func__); goto fail; @@ -1690,10 +1948,10 @@ static bt_mesh_input_action_t input_action(u8_t action) static int prov_auth(const u8_t idx, u8_t method, u8_t action, u8_t size) { - bt_mesh_output_action_t output; - bt_mesh_input_action_t input; + bt_mesh_output_action_t output = 0U; + bt_mesh_input_action_t input = 0U; - link[idx].auth = (u8_t *)osi_calloc(PROV_AUTH_VAL_SIZE); + link[idx].auth = (u8_t *)bt_mesh_calloc(PROV_AUTH_VAL_SIZE); if (!link[idx].auth) { BT_ERR("%s, Failed to allocate memory", __func__); close_link(idx, CLOSE_REASON_FAILED); @@ -1712,9 +1970,9 @@ static int prov_auth(const u8_t idx, u8_t method, u8_t action, u8_t size) if (action || size) { return -EINVAL; } - memcpy(link[idx].auth + 16 - prov->prov_static_oob_len, - prov->prov_static_oob_val, prov->prov_static_oob_len); - memset(link[idx].auth, 0, 16 - prov->prov_static_oob_len); + memcpy(link[idx].auth + 16 - prov_ctx.static_oob_len, + prov_ctx.static_oob_val, prov_ctx.static_oob_len); + memset(link[idx].auth, 0, 16 - prov_ctx.static_oob_len); return 0; case AUTH_METHOD_OUTPUT: @@ -1736,10 +1994,11 @@ static int prov_auth(const u8_t idx, u8_t method, u8_t action, u8_t size) link[idx].expect = PROV_INPUT_COMPLETE; if (input == BLE_MESH_ENTER_STRING) { - unsigned char str[9]; - u8_t j; + unsigned char str[9] = {'\0'}; + u8_t j = 0U; bt_mesh_rand(str, size); + /* Normalize to '0' .. '9' & 'A' .. 'Z' */ for (j = 0U; j < size; j++) { str[j] %= 36; @@ -1757,7 +2016,7 @@ static int prov_auth(const u8_t idx, u8_t method, u8_t action, u8_t size) return prov->prov_output_num(AUTH_METHOD_INPUT, input, str, size, idx); } else { u32_t div[8] = { 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 }; - u32_t num; + u32_t num = 0U; bt_mesh_rand(&num, sizeof(num)); num %= div[size - 1]; @@ -1781,13 +2040,13 @@ static void send_confirm(const u8_t idx) BT_DBG("ConfInputs[64] %s", bt_hex(link[idx].conf_inputs + 64, 64)); BT_DBG("ConfInputs[128] %s", bt_hex(link[idx].conf_inputs + 128, 17)); - link[idx].conf_salt = (u8_t *)osi_calloc(PROV_CONF_SALT_SIZE); + link[idx].conf_salt = (u8_t *)bt_mesh_calloc(PROV_CONF_SALT_SIZE); if (!link[idx].conf_salt) { BT_ERR("%s, Failed to allocate memory", __func__); goto fail; } - link[idx].conf_key = (u8_t *)osi_calloc(PROV_CONF_KEY_SIZE); + link[idx].conf_key = (u8_t *)bt_mesh_calloc(PROV_CONF_KEY_SIZE); if (!link[idx].conf_key) { BT_ERR("%s, Failed to allocate memory", __func__); goto fail; @@ -1846,7 +2105,7 @@ fail: return; } -int bt_mesh_prov_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_flag) +int bt_mesh_provisioner_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_flag) { /** This function should be called in the prov_input_num * callback, after the data output by device has been @@ -1863,6 +2122,8 @@ int bt_mesh_prov_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_fl return -EINVAL; } + BT_INFO("Link idx %d, type %s", idx, num_flag ? "number" : "string"); + memset(link[idx].auth, 0, 16); if (num_flag) { /* Provisioner inputs number */ @@ -1876,7 +2137,7 @@ int bt_mesh_prov_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_fl return 0; } -int bt_mesh_prov_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, bool num_flag) +int bt_mesh_provisioner_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, bool num_flag) { /** This function should be called in the prov_output_num * callback, after the data has been output by provisioner. @@ -1892,6 +2153,8 @@ int bt_mesh_prov_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, return -EINVAL; } + BT_INFO("Link idx %d, type %s", idx, num_flag ? "number" : "string"); + if (num_flag) { /* Provisioner output number */ memset(link[idx].auth, 0, 16); @@ -1907,7 +2170,7 @@ int bt_mesh_prov_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, return 0; } -int bt_mesh_prov_read_oob_pub_key(const u8_t idx, const u8_t pub_key_x[32], const u8_t pub_key_y[32]) +int bt_mesh_provisioner_read_oob_pub_key(const u8_t idx, const u8_t pub_key_x[32], const u8_t pub_key_y[32]) { if (!link[idx].conf_inputs) { BT_ERR("%s, Link conf_inputs is NULL", __func__); @@ -1936,7 +2199,7 @@ static void prov_dh_key_cb(const u8_t key[32], const u8_t idx) goto fail; } - link[idx].dhkey = (u8_t *)osi_calloc(PROV_DH_KEY_SIZE); + link[idx].dhkey = (u8_t *)bt_mesh_calloc(PROV_DH_KEY_SIZE); if (!link[idx].dhkey) { BT_ERR("%s, Failed to allocate memory", __func__); goto fail; @@ -1976,7 +2239,7 @@ fail: static void prov_gen_dh_key(const u8_t idx) { - u8_t pub_key[64]; + u8_t pub_key[64] = {0}; /* Copy device public key in little-endian for bt_mesh_dh_key_gen(). * X and Y halves are swapped independently. @@ -2065,7 +2328,7 @@ static void prov_pub_key(const u8_t idx, const u8_t *data) prov_clear_tx(idx); #endif bt_mesh_atomic_set_bit(link[idx].flags, REMOTE_PUB_KEY); - BT_WARN("%s, Waiting for local public key", __func__); + BT_WARN("Waiting for local public key"); return; } @@ -2099,7 +2362,7 @@ static void prov_confirm(const u8_t idx, const u8_t *data) k_delayed_work_cancel(&link[idx].timeout); } - link[idx].conf = (u8_t *)osi_calloc(PROV_CONFIRM_SIZE); + link[idx].conf = (u8_t *)bt_mesh_calloc(PROV_CONFIRM_SIZE); if (!link[idx].conf) { BT_ERR("%s, Failed to allocate memory", __func__); close_link(idx, CLOSE_REASON_FAILED); @@ -2131,14 +2394,14 @@ static void prov_confirm(const u8_t idx, const u8_t *data) static void send_prov_data(const u8_t idx) { PROV_BUF(buf, 34); + u16_t prev_addr = BLE_MESH_ADDR_UNASSIGNED; + u16_t max_addr = BLE_MESH_ADDR_UNASSIGNED; + struct bt_mesh_node *node = NULL; const u8_t *netkey = NULL; - bool already_flag = false; - u8_t session_key[16]; - u8_t nonce[13]; - u8_t pdu[25]; - u16_t max_addr; - u16_t j; - int err; + u8_t session_key[16] = {0}; + u8_t nonce[13] = {0}; + u8_t pdu[25] = {0}; + int err = 0; err = bt_mesh_session_key(link[idx].dhkey, link[idx].prov_salt, session_key); if (err) { @@ -2158,18 +2421,18 @@ static void send_prov_data(const u8_t idx) * will be added to the primary subnet, and may add an API to choose to which * subnet will the device be provisioned later. */ - if (FAST_PROV_FLAG_GET()) { - netkey = fast_prov_info.net_key; + if (FAST_PROV_ENABLE()) { + netkey = prov_ctx.fast_prov.net_key; if (!netkey) { BT_ERR("%s, Failed to get NetKey for fast provisioning", __func__); goto fail; } memcpy(pdu, netkey, 16); - sys_put_be16(fast_prov_info.net_idx, &pdu[16]); - pdu[18] = fast_prov_info.flags; - sys_put_be32(fast_prov_info.iv_index, &pdu[19]); + sys_put_be16(prov_ctx.fast_prov.net_idx, &pdu[16]); + pdu[18] = prov_ctx.fast_prov.flags; + sys_put_be32(prov_ctx.fast_prov.iv_index, &pdu[19]); } else { - netkey = provisioner_net_key_get(prov_ctx.curr_net_idx); + netkey = bt_mesh_provisioner_net_key_get(prov_ctx.curr_net_idx); if (!netkey) { BT_ERR("%s, Failed to get NetKey for provisioning data", __func__); goto fail; @@ -2180,56 +2443,65 @@ static void send_prov_data(const u8_t idx) sys_put_be32(prov_ctx.curr_iv_index, &pdu[19]); } - /* 1. The Provisioner must not reuse unicast addresses that have been - * allocated to a device and sent in a Provisioning Data PDU until - * the Provisioner receives an Unprovisioned Device beacon or - * Service Data for the Mesh Provisioning Service from that same - * device, identified using the Device UUID of the device. - * 2. Once the provisioning data for the device has been sent, we will - * add the data sent to this device into the already_prov_info. - * 3. Another situation here is: - * If the device is a re-provisioned one, but the element num has - * changed and is larger than the previous number, here we will - * assign new address for the device. + /** + * The Provisioner must not reuse unicast addresses that have been + * allocated to a device and sent in a Provisioning Data PDU until + * the Provisioner receives an Unprovisioned Device beacon or + * Service Data for the Mesh Provisioning Service from that same + * device, identified using the Device UUID of the device. */ /* Check if this device is a re-provisioned device */ - for (j = 0U; j < ARRAY_SIZE(prov_ctx.already_prov); j++) { - if (!memcmp(link[idx].uuid, prov_ctx.already_prov[j].uuid, 16)) { - if (link[idx].element_num <= prov_ctx.already_prov[j].element_num) { - already_flag = true; - sys_put_be16(prov_ctx.already_prov[j].unicast_addr, &pdu[23]); - link[idx].unicast_addr = prov_ctx.already_prov[j].unicast_addr; - break; - } else { - /* TODO: If the device has a larger element number during the - * second provisioning, then if the device is provisioned the - * third time later, already_prov struct will have two elements - * containing the same device UUID but with different element - * number. So we may add a flag to indicate the unicast address - * in the smaller element can be reused by other devices when - * unicast address is exhausted. - */ - } + node = bt_mesh_provisioner_get_node_with_uuid(link[idx].uuid); + if (node) { + if (link[idx].element_num <= node->element_num) { + /** + * If the device is provisioned before, but the element number of + * the device is bigger now, then we treate it as a new device. + */ + prev_addr = node->unicast_addr; } + bt_mesh_provisioner_remove_node(link[idx].uuid); } - max_addr = FAST_PROV_FLAG_GET() ? fast_prov_info.unicast_addr_max : 0x7FFF; + max_addr = FAST_PROV_ENABLE() ? prov_ctx.fast_prov.unicast_addr_max : PROV_MAX_ADDR_TO_ASSIGN; - if (!already_flag) { - /* If this device to be provisioned is a new device */ - if (!prov_ctx.current_addr) { - BT_ERR("%s, No unicast address can be assigned", __func__); - goto fail; + if (BLE_MESH_ADDR_IS_UNICAST(prev_addr)) { + sys_put_be16(prev_addr, &pdu[23]); + link[idx].unicast_addr = prev_addr; + } else { + if (BLE_MESH_ADDR_IS_UNICAST(link[idx].assign_addr)) { + if (link[idx].assign_addr + link[idx].element_num - 1 > max_addr) { + BT_ERR("%s, Too large assigned address for the device", __func__); + goto fail; + } + + /* Make sure the assigned unicast address is not identical with any unicast address + * of other nodes. Also need to make sure the address is not identical with any + * address of Provisioner. + */ + if (bt_mesh_provisioner_check_is_addr_dup(link[idx].assign_addr, link[idx].element_num, true)) { + BT_ERR("%s, Assigned address 0x%04x is duplicated", __func__, link[idx].assign_addr); + goto fail; + } + + sys_put_be16(link[idx].assign_addr, &pdu[23]); + link[idx].unicast_addr = link[idx].assign_addr; + } else { + /* If this device to be provisioned is a new device */ + if (prov_ctx.curr_alloc_addr == BLE_MESH_ADDR_UNASSIGNED) { + BT_ERR("%s, No unicast address can be allocated", __func__); + goto fail; + } + + if (prov_ctx.curr_alloc_addr + link[idx].element_num - 1 > max_addr) { + BT_ERR("%s, Not enough unicast address for the device", __func__); + goto fail; + } + + sys_put_be16(prov_ctx.curr_alloc_addr, &pdu[23]); + link[idx].unicast_addr = prov_ctx.curr_alloc_addr; } - - if (prov_ctx.current_addr + link[idx].element_num - 1 > max_addr) { - BT_ERR("%s, Not enough unicast address for the device", __func__); - goto fail; - } - - sys_put_be16(prov_ctx.current_addr, &pdu[23]); - link[idx].unicast_addr = prov_ctx.current_addr; } prov_buf_init(&buf, PROV_DATA); @@ -2245,36 +2517,40 @@ static void send_prov_data(const u8_t idx) goto fail; } - /* If provisioning data is sent successfully, add the assigned information - * into the already_prov_info struct if this device is a new one. And if - * sent successfully, update the current_addr in prov_ctx struct. + /** + * We update the next unicast address to be allocated here because if + * Provisioner is provisioning two devices at the same time, we need + * to assign the unicast address for them correctly. Hence we should + * not update the prov_ctx.curr_alloc_addr after the proper provisioning + * complete pdu is received. */ - if (!already_flag) { - for (j = 0U; j < ARRAY_SIZE(prov_ctx.already_prov); j++) { - if (!prov_ctx.already_prov[j].element_num) { - memcpy(prov_ctx.already_prov[j].uuid, link[idx].uuid, 16); - prov_ctx.already_prov[j].element_num = link[idx].element_num; - prov_ctx.already_prov[j].unicast_addr = link[idx].unicast_addr; - break; + if (!BLE_MESH_ADDR_IS_UNICAST(prev_addr)) { + if (BLE_MESH_ADDR_IS_UNICAST(link[idx].assign_addr)) { + /* Even if the unicast address of the node is assigned by the + * application, we will also update the prov_ctx.curr_alloc_addr + * here, in case Users use the two methods together (i.e. allocate + * the unicast address for the node internally and assign the + * unicast address for the node from application). + */ + if (prov_ctx.curr_alloc_addr < link[idx].assign_addr + link[idx].element_num) { + prov_ctx.curr_alloc_addr = link[idx].assign_addr + link[idx].element_num; + } + } else { + prov_ctx.curr_alloc_addr += link[idx].element_num; + if (prov_ctx.curr_alloc_addr > max_addr) { + /* No unicast address will be used for further provisioning */ + prov_ctx.curr_alloc_addr = BLE_MESH_ADDR_UNASSIGNED; } } - - /* We update the next unicast address to be assigned here because - * if provisioner is provisioning two devices at the same time, we - * need to assign the unicast address for them correctly. Hence we - * should not update the prov_ctx.current_addr after the proper - * provisioning complete pdu is received. - */ - prov_ctx.current_addr += link[idx].element_num; - if (prov_ctx.current_addr > max_addr) { - /* No unicast address will be used for further provisioning */ - prov_ctx.current_addr = 0x0000; + /* Store the available unicast address range to flash */ + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.curr_alloc_addr); } } - if (FAST_PROV_FLAG_GET()) { - link[idx].ki_flags = fast_prov_info.flags; - link[idx].iv_index = fast_prov_info.iv_index; + if (FAST_PROV_ENABLE()) { + link[idx].ki_flags = prov_ctx.fast_prov.flags; + link[idx].iv_index = prov_ctx.fast_prov.iv_index; } else { link[idx].ki_flags = prov_ctx.curr_flags; link[idx].iv_index = prov_ctx.curr_iv_index; @@ -2290,7 +2566,7 @@ fail: static void prov_random(const u8_t idx, const u8_t *data) { - u8_t conf_verify[16]; + u8_t conf_verify[16] = {0}; BT_DBG("Remote Random: %s", bt_hex(data, 16)); @@ -2314,11 +2590,11 @@ static void prov_random(const u8_t idx, const u8_t *data) /** After provisioner receives provisioning random from device, * and successfully check the confirmation, the following * should be done: - * 1. osi_calloc memory for prov_salt + * 1. bt_mesh_calloc memory for prov_salt * 2. calculate prov_salt * 3. prepare provisioning data and send */ - link[idx].prov_salt = (u8_t *)osi_calloc(PROV_PROV_SALT_SIZE); + link[idx].prov_salt = (u8_t *)bt_mesh_calloc(PROV_PROV_SALT_SIZE); if (!link[idx].prov_salt) { BT_ERR("%s, Failed to allocate memory", __func__); goto fail; @@ -2342,20 +2618,17 @@ fail: static void prov_complete(const u8_t idx, const u8_t *data) { - u8_t device_key[16]; - u16_t rm = 0; - u16_t j; - int err; + u8_t device_key[16] = {0}; + u16_t net_idx = 0U; + u16_t index = 0U; + u16_t rm = 0U; + int err = 0; /* Make sure received pdu is ok and cancel the timeout timer */ if (bt_mesh_atomic_test_and_clear_bit(link[idx].flags, TIMEOUT_START)) { k_delayed_work_cancel(&link[idx].timeout); } - /* If provisioning complete is received, the provisioning device - * will be stored into the prov_node_info structure and become a - * node within the mesh network - */ err = bt_mesh_dev_key(link[idx].dhkey, link[idx].prov_salt, device_key); if (err) { BT_ERR("%s, Failed to generate device key", __func__); @@ -2363,38 +2636,14 @@ static void prov_complete(const u8_t idx, const u8_t *data) return; } - for (j = 0U; j < ARRAY_SIZE(prov_nodes); j++) { - if (!prov_nodes[j].provisioned) { - prov_nodes[j].provisioned = true; - prov_nodes[j].oob_info = link[idx].oob_info; - prov_nodes[j].element_num = link[idx].element_num; - prov_nodes[j].unicast_addr = link[idx].unicast_addr; - if (FAST_PROV_FLAG_GET()) { - prov_nodes[j].net_idx = fast_prov_info.net_idx; - } else { - prov_nodes[j].net_idx = prov_ctx.curr_net_idx; - } - prov_nodes[j].flags = link[idx].ki_flags; - prov_nodes[j].iv_index = link[idx].iv_index; - prov_nodes[j].addr.type = link[idx].addr.type; - memcpy(prov_nodes[j].addr.val, link[idx].addr.val, BLE_MESH_ADDR_LEN); - memcpy(prov_nodes[j].uuid, link[idx].uuid, 16); - break; - } + if (FAST_PROV_ENABLE()) { + net_idx = prov_ctx.fast_prov.net_idx; + } else { + net_idx = prov_ctx.curr_net_idx; } - - if (j == ARRAY_SIZE(prov_nodes)) { - BT_ERR("%s, Provisioned node queue is full", __func__); - close_link(idx, CLOSE_REASON_FAILED); - return; - } - - prov_ctx.node_count++; - - err = provisioner_node_provision(j, prov_nodes[j].uuid, prov_nodes[j].oob_info, - prov_nodes[j].unicast_addr, prov_nodes[j].element_num, - prov_nodes[j].net_idx, prov_nodes[j].flags, - prov_nodes[j].iv_index, device_key); + err = bt_mesh_provisioner_provision(&link[idx].addr, link[idx].uuid, link[idx].oob_info, + link[idx].unicast_addr, link[idx].element_num, net_idx, + link[idx].ki_flags, link[idx].iv_index, device_key, &index); if (err) { BT_ERR("%s, Failed to store node info", __func__); close_link(idx, CLOSE_REASON_FAILED); @@ -2402,8 +2651,8 @@ static void prov_complete(const u8_t idx, const u8_t *data) } if (prov->prov_complete) { - prov->prov_complete(j, prov_nodes[j].uuid, prov_nodes[j].unicast_addr, - prov_nodes[j].element_num, prov_nodes[j].net_idx); + prov->prov_complete(index, link[idx].uuid, link[idx].unicast_addr, + link[idx].element_num, net_idx); } err = provisioner_dev_find(&link[idx].addr, link[idx].uuid, &rm); @@ -2412,9 +2661,9 @@ static void prov_complete(const u8_t idx, const u8_t *data) memset(&unprov_dev[rm], 0, sizeof(struct unprov_dev_queue)); } } else if (err == -ENODEV) { - BT_DBG("%s, Device is not found in queue", __func__); + BT_DBG("Device is not found in queue"); } else { - BT_WARN("%s, Failed to remove device from queue", __func__); + BT_ERR("Failed to remove device from queue"); } close_link(idx, CLOSE_REASON_SUCCESS); @@ -2473,7 +2722,7 @@ static void prov_timeout(struct k_work *work) { u8_t idx = (u8_t)work->index; - BT_DBG("%s", __func__); + BT_WARN("%s", __func__); close_link(idx, CLOSE_REASON_TIMEOUT); } @@ -2483,12 +2732,12 @@ static void prov_retransmit(struct k_work *work) { s64_t timeout = TRANSACTION_TIMEOUT; u8_t idx = (u8_t)work->index; - u8_t i; + int i; BT_DBG("%s", __func__); if (!bt_mesh_atomic_test_bit(link[idx].flags, LINK_ACTIVE)) { - BT_WARN("%s, Link is not active", __func__); + BT_WARN("Link is not active"); return; } @@ -2513,7 +2762,9 @@ static void prov_retransmit(struct k_work *work) link[idx].send_link_close += BIT(3); } - for (i = 0U; i < ARRAY_SIZE(link[idx].tx.buf); i++) { + bt_mesh_pb_buf_lock(); + + for (i = 0; i < ARRAY_SIZE(link[idx].tx.buf); i++) { struct net_buf *buf = link[idx].tx.buf[i]; if (!buf) { @@ -2532,6 +2783,8 @@ static void prov_retransmit(struct k_work *work) bt_mesh_adv_send(buf, &buf_sent_cb, (void *)(int)idx); } } + + bt_mesh_pb_buf_unlock(); } static void link_ack(const u8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) @@ -2549,7 +2802,7 @@ static void link_ack(const u8_t idx, struct prov_rx *rx, struct net_buf_simple * return; } - link[idx].conf_inputs = (u8_t *)osi_calloc(PROV_CONF_INPUTS_SIZE); + link[idx].conf_inputs = (u8_t *)bt_mesh_calloc(PROV_CONF_INPUTS_SIZE); if (!link[idx].conf_inputs) { BT_ERR("%s, Failed to allocate memory", __func__); close_link(idx, CLOSE_REASON_FAILED); @@ -2561,7 +2814,7 @@ static void link_ack(const u8_t idx, struct prov_rx *rx, struct net_buf_simple * static void link_close(const u8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) { - u8_t reason; + u8_t reason = 0U; BT_DBG("len %u", buf->len); @@ -2653,12 +2906,8 @@ static void gen_prov_cont(const u8_t idx, struct prov_rx *rx, struct net_buf_sim } if (rx->xact_id != link[idx].rx.trans_id) { - BT_WARN("%s, Data for unknown transaction (%u != %u)", - __func__, rx->xact_id, link[idx].rx.trans_id); - /** - * If Provisioner receives a Provisioning PDU with a mismatch - * transaction number, it just ignore it. - */ + BT_WARN("Data for unknown transaction (%u != %u)", + rx->xact_id, link[idx].rx.trans_id); return; } @@ -2666,7 +2915,7 @@ static void gen_prov_cont(const u8_t idx, struct prov_rx *rx, struct net_buf_sim BT_ERR("%s, Invalid segment index %u", __func__, seg); goto fail; } else if (seg == link[idx].rx.last_seg) { - u8_t expect_len; + u8_t expect_len = 0U; expect_len = (link[idx].rx.buf->len - 20 - (23 * (link[idx].rx.last_seg - 1))); @@ -2697,7 +2946,7 @@ fail: static void gen_prov_ack(const u8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) { - u8_t ack_type, pub_key_oob; + u8_t ack_type = 0U, pub_key_oob = 0U; BT_DBG("len %u", buf->len); @@ -2813,10 +3062,10 @@ static void gen_prov_recv(const u8_t idx, struct prov_rx *rx, struct net_buf_sim static int find_link(u32_t link_id, u8_t *idx) { - u8_t i; + int i; /* link for PB-ADV is from 0 to CONFIG_BLE_MESH_PBA_SAME_TIME */ - for (i = 0U; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { + for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { if (bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { if (link[i].link_id == link_id) { if (idx) { @@ -2830,10 +3079,10 @@ static int find_link(u32_t link_id, u8_t *idx) return -1; } -void provisioner_pb_adv_recv(struct net_buf_simple *buf) +void bt_mesh_provisioner_pb_adv_recv(struct net_buf_simple *buf) { struct prov_rx rx = {0}; - u8_t idx; + u8_t idx = 0U; rx.link_id = net_buf_simple_pull_be32(buf); if (find_link(rx.link_id, &idx) < 0) { @@ -2859,7 +3108,7 @@ void provisioner_pb_adv_recv(struct net_buf_simple *buf) #if defined(CONFIG_BLE_MESH_PB_GATT) static struct bt_mesh_conn *find_conn(struct bt_mesh_conn *conn, u8_t *idx) { - u8_t i; + int i; /* link for PB-GATT is from CONFIG_BLE_MESH_PBA_SAME_TIME to BLE_MESH_PROV_SAME_TIME */ for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) { @@ -2876,10 +3125,10 @@ static struct bt_mesh_conn *find_conn(struct bt_mesh_conn *conn, u8_t *idx) return NULL; } -int provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf) +int bt_mesh_provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf) { - u8_t type; - u8_t idx; + u8_t type = 0U; + u8_t idx = 0U; BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); @@ -2919,9 +3168,9 @@ fail: return -EINVAL; } -int provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn) +int bt_mesh_provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn) { - u8_t i; + int i; if (!addr || !conn) { BT_ERR("%s, Invalid parameter", __func__); @@ -2939,9 +3188,10 @@ int provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn) return -ENOMEM; } -int provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr) +int bt_mesh_provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr) { - u8_t idx = 0, i; + u8_t idx = 0U; + int i; BT_DBG("conn %p", conn); @@ -2964,10 +3214,10 @@ int provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr) } #if defined(CONFIG_BLE_MESH_PB_ADV) - for (i = 0U; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { + for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { if (bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { if (!memcmp(link[i].uuid, link[idx].uuid, 16)) { - BT_WARN("%s, Provision using PB-GATT & PB-ADV same time", __func__); + BT_WARN("Provision using PB-GATT & PB-ADV same time"); close_link(idx, CLOSE_REASON_FAILED); return -EALREADY; } @@ -2983,7 +3233,7 @@ int provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr) prov->prov_link_open(BLE_MESH_PROV_GATT); } - link[idx].conf_inputs = (u8_t *)osi_calloc(PROV_CONF_INPUTS_SIZE); + link[idx].conf_inputs = (u8_t *)bt_mesh_calloc(PROV_CONF_INPUTS_SIZE); if (!link[idx].conf_inputs) { /* Disconnect this connection, clear corresponding informations */ BT_ERR("%s, Failed to allocate memory", __func__); @@ -2995,9 +3245,9 @@ int provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr) return 0; } -int provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason) +int bt_mesh_provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason) { - u8_t idx; + u8_t idx = 0U; BT_DBG("conn %p", conn); @@ -3026,21 +3276,16 @@ int provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason) } #endif /* CONFIG_BLE_MESH_PB_GATT */ -int provisioner_prov_init(const struct bt_mesh_prov *prov_info) +int bt_mesh_provisioner_prov_init(const struct bt_mesh_prov *prov_info) { const u8_t *key = NULL; - u8_t i; + int i; if (!prov_info) { BT_ERR("%s, No provisioning context provided", __func__); return -EINVAL; } - if (CONFIG_BLE_MESH_PBG_SAME_TIME > BLE_MESH_MAX_CONN) { - BT_ERR("%s, PB-GATT same time exceeds max connection", __func__); - return -EINVAL; - } - key = bt_mesh_pub_key_get(); if (!key) { BT_ERR("%s, Failed to generate Public Key", __func__); @@ -3049,8 +3294,15 @@ int provisioner_prov_init(const struct bt_mesh_prov *prov_info) prov = prov_info; + prov_ctx.primary_addr = BLE_MESH_ADDR_UNASSIGNED; + + if (prov->prov_static_oob_val && prov->prov_static_oob_len) { + prov_ctx.static_oob_len = MIN(16, prov->prov_static_oob_len); + memcpy(prov_ctx.static_oob_val, prov->prov_static_oob_val, prov_ctx.static_oob_len); + } + #if defined(CONFIG_BLE_MESH_PB_ADV) - for (i = 0U; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { + for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { struct prov_adv_buf *adv = &adv_buf[i]; adv->buf.size = ADV_BUF_SIZE; adv->buf.__buf = adv_buf_data + (i * ADV_BUF_SIZE); @@ -3063,28 +3315,77 @@ int provisioner_prov_init(const struct bt_mesh_prov *prov_info) } #endif - for (i = 0U; i < BLE_MESH_PROV_SAME_TIME; i++) { + for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) { k_delayed_work_init(&link[i].timeout, prov_timeout); link[i].timeout.work.index = (int)i; } - /* for PB-GATT, use servers[] array in proxy_provisioner.c */ +#if defined(CONFIG_BLE_MESH_PB_ADV) + bt_mesh_pb_adv_mutex_new(); + bt_mesh_pb_buf_mutex_new(); +#endif +#if defined(CONFIG_BLE_MESH_PB_GATT) + bt_mesh_pb_gatt_mutex_new(); +#endif - prov_ctx.current_addr = prov->prov_start_address; - prov_ctx.curr_net_idx = BLE_MESH_KEY_PRIMARY; - prov_ctx.curr_flags = prov->flags; - prov_ctx.curr_iv_index = prov->iv_index; + return 0; +} - osi_mutex_new(&prov_ctx.pb_adv_lock); - osi_mutex_new(&prov_ctx.pb_gatt_lock); +int bt_mesh_provisioner_prov_deinit(bool erase) +{ + int i; + + if (prov == NULL) { + BT_ERR("%s, No provisioning context provided", __func__); + return -EINVAL; + } + +#if defined(CONFIG_BLE_MESH_PB_ADV) + for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { + prov_clear_tx(i); + k_delayed_work_free(&link[i].tx.retransmit); +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + /* Remove the link id from exceptional list */ + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_REMOVE, + BLE_MESH_EXCEP_INFO_MESH_LINK_ID, &link[i].link_id); +#endif /* CONFIG_BLE_MESH_USE_DUPLICATE_SCAN */ + } +#endif /* CONFIG_BLE_MESH_PB_ADV */ + + for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) { + prov_memory_free(i); + k_delayed_work_free(&link[i].timeout); + memset(&link[i], 0, sizeof(link[i])); + } + +#if defined(CONFIG_BLE_MESH_PB_ADV) + bt_mesh_pb_adv_mutex_free(); + bt_mesh_pb_buf_mutex_free(); +#endif +#if defined(CONFIG_BLE_MESH_PB_GATT) + bt_mesh_pb_gatt_mutex_free(); +#endif + memset(&prov_ctx, 0, sizeof(prov_ctx)); + +#if defined(CONFIG_BLE_MESH_PB_ADV) + memset(adv_buf, 0, sizeof(adv_buf)); + memset(adv_buf_data, 0, sizeof(adv_buf_data)); +#endif + memset(unprov_dev, 0, sizeof(unprov_dev)); + + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_prov_info(); + } + + prov = NULL; return 0; } static bool is_unprov_dev_info_callback_to_app(bt_mesh_prov_bearer_t bearer, - const u8_t uuid[16], const bt_mesh_addr_t *addr, u16_t oob_info) + const u8_t uuid[16], const bt_mesh_addr_t *addr, u16_t oob_info, s8_t rssi) { - u16_t index; + u16_t index = 0U; if (prov_ctx.prov_after_match == false) { u8_t adv_type = (bearer == BLE_MESH_PROV_ADV) ? @@ -3093,7 +3394,7 @@ static bool is_unprov_dev_info_callback_to_app(bt_mesh_prov_bearer_t bearer, if (provisioner_dev_find(addr, uuid, &index)) { BT_DBG("%s, Device is not in queue, notify to upper layer", __func__); if (notify_unprov_adv_pkt_cb) { - notify_unprov_adv_pkt_cb(addr->val, addr->type, adv_type, uuid, oob_info, bearer); + notify_unprov_adv_pkt_cb(addr->val, addr->type, adv_type, uuid, oob_info, bearer, rssi); } return true; } @@ -3102,7 +3403,7 @@ static bool is_unprov_dev_info_callback_to_app(bt_mesh_prov_bearer_t bearer, BT_WARN("Device in queue not support PB-%s", (bearer == BLE_MESH_PROV_ADV) ? "ADV" : "GATT"); if (notify_unprov_adv_pkt_cb) { - notify_unprov_adv_pkt_cb(addr->val, addr->type, adv_type, uuid, oob_info, bearer); + notify_unprov_adv_pkt_cb(addr->val, addr->type, adv_type, uuid, oob_info, bearer, rssi); } return true; } @@ -3111,20 +3412,20 @@ static bool is_unprov_dev_info_callback_to_app(bt_mesh_prov_bearer_t bearer, return false; } -void provisioner_unprov_beacon_recv(struct net_buf_simple *buf) +void bt_mesh_provisioner_unprov_beacon_recv(struct net_buf_simple *buf, s8_t rssi) { #if defined(CONFIG_BLE_MESH_PB_ADV) const bt_mesh_addr_t *addr = NULL; const u8_t *uuid = NULL; - u16_t oob_info; + u16_t oob_info = 0U; - if (buf->len != 0x12 && buf->len != 0x16) { - BT_ERR("%s, Invalid Unprovisioned Device Beacon length", __func__); + if (!(prov_ctx.bearers & BLE_MESH_PROV_ADV)) { + BT_WARN("Provisioner not support PB-ADV bearer"); return; } - if (prov_ctx.pba_count == CONFIG_BLE_MESH_PBA_SAME_TIME) { - BT_DBG("Current PB-ADV devices reach max limit"); + if (buf->len != 0x12 && buf->len != 0x16) { + BT_ERR("%s, Invalid Unprovisioned Device Beacon length", __func__); return; } @@ -3134,32 +3435,32 @@ void provisioner_unprov_beacon_recv(struct net_buf_simple *buf) /* Mesh beacon uses big-endian to send beacon data */ oob_info = net_buf_simple_pull_be16(buf); - if (provisioner_check_unprov_dev_info(uuid)) { + if (provisioner_check_unprov_dev_info(uuid, BLE_MESH_PROV_ADV)) { return; } if (is_unprov_dev_info_callback_to_app( - BLE_MESH_PROV_ADV, uuid, addr, oob_info)) { + BLE_MESH_PROV_ADV, uuid, addr, oob_info, rssi)) { return; } - provisioner_start_prov_pb_adv(uuid, addr, oob_info); + provisioner_start_prov_pb_adv(uuid, addr, oob_info, BLE_MESH_ADDR_UNASSIGNED); #endif /* CONFIG_BLE_MESH_PB_ADV */ } -void provisioner_prov_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr) +void bt_mesh_provisioner_prov_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr, s8_t rssi) { #if defined(CONFIG_BLE_MESH_PB_GATT) const u8_t *uuid = NULL; - u16_t oob_info; + u16_t oob_info = 0U; - if (prov_ctx.pbg_count == CONFIG_BLE_MESH_PBG_SAME_TIME) { - BT_DBG("Current PB-GATT devices reach max limit"); + if (!(prov_ctx.bearers & BLE_MESH_PROV_GATT)) { + BT_WARN("Provisioner not support PB-GATT bearer"); return; } if (bt_mesh_gattc_get_free_conn_count() == 0) { - BT_WARN("%s, max connections", __func__); + BT_INFO("BLE connections for mesh reach max limit"); return; } @@ -3168,12 +3469,12 @@ void provisioner_prov_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_add /* Mesh beacon uses big-endian to send beacon data */ oob_info = net_buf_simple_pull_be16(buf); - if (provisioner_check_unprov_dev_info(uuid)) { + if (provisioner_check_unprov_dev_info(uuid, BLE_MESH_PROV_GATT)) { return; } if (is_unprov_dev_info_callback_to_app( - BLE_MESH_PROV_GATT, uuid, addr, oob_info)) { + BLE_MESH_PROV_GATT, uuid, addr, oob_info, rssi)) { return; } @@ -3189,7 +3490,7 @@ void provisioner_prov_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_add * Use connecting flag to prevent if two devices's adv pkts are both received, * the previous one info will be replaced by the second one. */ - provisioner_start_prov_pb_gatt(uuid, addr, oob_info); + provisioner_start_prov_pb_gatt(uuid, addr, oob_info, BLE_MESH_ADDR_UNASSIGNED); #endif /* CONFIG_BLE_MESH_PB_GATT */ } 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 6fea1df16..7642e2c8e 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h @@ -15,8 +15,8 @@ #ifndef _PROVISIONER_PROV_H_ #define _PROVISIONER_PROV_H_ -#include "mesh_bearer_adapt.h" #include "mesh_main.h" +#include "mesh_bearer_adapt.h" #if !CONFIG_BLE_MESH_PROVISIONER @@ -73,7 +73,7 @@ struct bt_mesh_prov_data_info { * * @return None */ -void provisioner_pbg_count_dec(void); +void bt_mesh_provisioner_pbg_count_dec(void); /** * @brief This function clears the part of the link info of the proper device. @@ -82,7 +82,7 @@ void provisioner_pbg_count_dec(void); * * @return None */ -void provisioner_clear_link_conn_info(const u8_t addr[6]); +void bt_mesh_provisioner_clear_link_info(const u8_t addr[6]); /** * @brief This function handles the received PB-ADV PDUs. @@ -91,7 +91,7 @@ void provisioner_clear_link_conn_info(const u8_t addr[6]); * * @return Zero - success, otherwise - fail */ -void provisioner_pb_adv_recv(struct net_buf_simple *buf); +void bt_mesh_provisioner_pb_adv_recv(struct net_buf_simple *buf); /** * @brief This function sends provisioning invite to start @@ -102,7 +102,7 @@ void provisioner_pb_adv_recv(struct net_buf_simple *buf); * * @return Zero - success, otherwise - fail */ -int provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn); +int bt_mesh_provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn); /** * @brief This function sends provisioning invite to start @@ -113,7 +113,7 @@ int provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn); * * @return Zero - success, otherwise - fail */ -int provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr); +int bt_mesh_provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr); /** * @brief This function resets the used information when @@ -124,7 +124,7 @@ int provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr); * * @return Zero - success, otherwise - fail */ -int provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason); +int bt_mesh_provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason); /** * @brief This function handles the received PB-GATT provision @@ -135,7 +135,7 @@ int provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason); * * @return Zero - success, otherwise - fail */ -int provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf); +int bt_mesh_provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf); /** * @brief This function initializes provisioner's PB-GATT and PB-ADV @@ -145,34 +145,40 @@ int provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *b * * @return Zero - success, otherwise - fail */ -int provisioner_prov_init(const struct bt_mesh_prov *prov_info); +int bt_mesh_provisioner_prov_init(const struct bt_mesh_prov *prov_info); + +/** + * @brief This function deinitializes provisioner's PB-GATT and PB-ADV + * related information. + * + * @param[in] erase: Indicate if erasing provisioning information from flash. + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_prov_deinit(bool erase); /** * @brief This function parses the received unprovisioned device * beacon advertising packets, and if checked, starts to provision this device * using PB-ADV bearer. * - * @param[in] buf: Pointer to the buffer containing unprovisioned device beacon + * @param[in] buf: Pointer to the buffer containing unprovisioned device beacon + * @param[in] rssi: RSSI of the received unprovisioned device beacon * * @return None */ -void provisioner_unprov_beacon_recv(struct net_buf_simple *buf); +void bt_mesh_provisioner_unprov_beacon_recv(struct net_buf_simple *buf, s8_t rssi); -void provisioner_prov_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr); +void bt_mesh_provisioner_prov_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr, s8_t rssi); /** * @brief This function gets the bt_mesh_prov pointer. * * @return bt_mesh_prov pointer(prov) */ -const struct bt_mesh_prov *provisioner_get_prov_info(void); +const struct bt_mesh_prov *bt_mesh_provisioner_get_prov_info(void); -/** - * @brief This function resets all nodes information in provisioner_prov.c. - * - * @return Zero - */ -int provisioner_prov_reset_all_nodes(void); +void bt_mesh_provisoner_restore_prov_info(u16_t primary_addr, u16_t alloc_addr); /* The following APIs are for primary provisioner application use */ @@ -186,7 +192,7 @@ int provisioner_prov_reset_all_nodes(void); * * @return Zero on success or (negative) error code otherwise. * - * @Note: 1. Currently address type only supports public address and static random address. + * @note 1. Currently address type only supports public address and static random address. * 2. If device UUID and/or device address and address type already exist in the * device queue, but the bearer differs from the existing one, add operation * will also be successful and it will update the provision bearer supported by @@ -194,6 +200,24 @@ int provisioner_prov_reset_all_nodes(void); */ int bt_mesh_provisioner_add_unprov_dev(struct bt_mesh_unprov_dev_add *add_dev, u8_t flags); +/** @brief Provision an unprovisioned device with fixed unicast address. + * + * @param[in] uuid: Device UUID of the unprovisioned device + * @param[in] addr: Device address of the unprovisioned device + * @param[in] addr_type: Device address type of the unprovisioned device + * @param[in] bearer: Provisioning bearer going to be used + * @param[in] oob_info: OOB info of the unprovisioned device + * @param[in] unicast_addr: Unicast address going to be allocated for the unprovisioned device + * + * @return Zero on success or (negative) error code otherwise. + * + * @note 1. Currently address type only supports public address and static random address. + * 2. Bearer must be equal to BLE_MESH_PROV_ADV or BLE_MESH_PROV_GATT + */ +int bt_mesh_provisioner_prov_device_with_addr(const u8_t uuid[16], const u8_t addr[6], + u8_t addr_type, bt_mesh_prov_bearer_t bearer, + u16_t oob_info, u16_t unicast_addr); + /** @brief Delete device from queue, reset current provisioning link and reset the node * * @param[in] del_dev: Pointer to the structure containing the device information @@ -229,11 +253,12 @@ int bt_mesh_provisioner_set_dev_uuid_match(u8_t offset, u8_t length, * @param adv_type Adv packet type, currently this is not used and we can use bearer to device * the adv_type(ADV_IND or ADV_NONCONN_IND). This parameter will be used, when * scan response data will be supported. + * @param rssi RSSI of the received advertising packet * */ typedef void (*unprov_adv_pkt_cb_t)(const u8_t addr[6], const u8_t addr_type, const u8_t adv_type, const u8_t dev_uuid[16], - u16_t oob_info, bt_mesh_prov_bearer_t bearer); + u16_t oob_info, bt_mesh_prov_bearer_t bearer, s8_t rssi); /** * @brief This function registers the callback which notifies the application @@ -244,7 +269,7 @@ typedef void (*unprov_adv_pkt_cb_t)(const u8_t addr[6], const u8_t addr_type, * * @return Zero - success, otherwise - fail */ -int bt_mesh_prov_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb); +int bt_mesh_provisioner_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb); /** * @brief This function changes net_idx or flags or iv_index used in provisioning data. @@ -255,6 +280,57 @@ int bt_mesh_prov_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb); */ int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info); +/** + * @brief This function sets the provisioning information needed by Provisioner, + * including unicast address, IV Index, etc. + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_set_prov_info(void); + +/** + * @brief This function sets the provisioning bearer type used by Provisioner. + * + * @param[in] bearers: Provisioning bearer type + * @param[in] clear: Indicate if the corresponding bearer type will be cleared + * + * @return None + */ +void bt_mesh_provisioner_set_prov_bearer(bt_mesh_prov_bearer_t bearers, bool clear); + +/** + * @brief This function gets the provisioning bearer type used by Provisioner. + * + * @return Currently supported provisioning bearer type + */ +bt_mesh_prov_bearer_t bt_mesh_provisioner_get_prov_bearer(void); + +/** + * @brief This function sets the Static OOB value used by Provisioner. + * + * @param[in] value: Static OOB value + * @param[in] length: Static OOB value length + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_set_static_oob_value(const u8_t *value, u8_t length); + +/** + * @brief This function gets the unicast address of primary element of Provisioner. + * + * @return Unicast address of primary element of Provisioner. + */ +u16_t bt_mesh_provisioner_get_primary_elem_addr(void); + +/** + * @brief This function sets the unicast address of primary element of Provisioner. + * + * @param[in] addr: unicast address of primary element + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_set_primary_elem_addr(u16_t addr); + /** * @brief This function is called to input number/string out-put by unprovisioned device. * @@ -264,7 +340,7 @@ int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info); * * @return Zero - success, otherwise - fail */ -int bt_mesh_prov_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_flag); +int bt_mesh_provisioner_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_flag); /** * @brief This function is called to output number/string which will be input by unprovisioned device. @@ -276,7 +352,7 @@ int bt_mesh_prov_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_fl * * @return Zero - success, otherwise - fail */ -int bt_mesh_prov_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, bool num_flag); +int bt_mesh_provisioner_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, bool num_flag); /** * @brief This function is called to read unprovisioned device's oob public key. @@ -287,18 +363,18 @@ int bt_mesh_prov_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, * * @return Zero - success, otherwise - fail */ -int bt_mesh_prov_read_oob_pub_key(const u8_t idx, const u8_t pub_key_x[32], const u8_t pub_key_y[32]); +int bt_mesh_provisioner_read_oob_pub_key(const u8_t idx, const u8_t pub_key_x[32], const u8_t pub_key_y[32]); /* The following APIs are for fast provisioning */ /** * @brief This function is called to set fast_prov_flag. * - * @param[in] flag: Flag set to fast_prov_flag + * @param[in] enable: Enable or disable fast provisioning * * @return None */ -void provisioner_set_fast_prov_flag(bool flag); +void bt_mesh_provisioner_fast_prov_enable(bool enable); /** * @brief This function is called to set netkey index used for fast provisioning. @@ -308,14 +384,14 @@ void provisioner_set_fast_prov_flag(bool flag); * * @return status for set netkey index msg */ -u8_t provisioner_set_fast_prov_net_idx(const u8_t *net_key, u16_t net_idx); +u8_t bt_mesh_provisioner_set_fast_prov_net_idx(const u8_t *net_key, u16_t net_idx); /** * @brief This function is called to get netkey index used for fast provisioning. * * @return net_idx of fast provisioning */ -u16_t provisioner_get_fast_prov_net_idx(void); +u16_t bt_mesh_provisioner_get_fast_prov_net_idx(void); /** * @brief This function is called to set unicast address range used for fast provisioning. diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy_client.c b/components/bt/esp_ble_mesh/mesh_core/proxy_client.c index ee0961e82..fc3472667 100644 --- a/components/bt/esp_ble_mesh/mesh_core/proxy_client.c +++ b/components/bt/esp_ble_mesh/mesh_core/proxy_client.c @@ -15,21 +15,15 @@ #include #include -#include "sdkconfig.h" - #include "mesh.h" -#include "mesh_bearer_adapt.h" -#include "mesh_trace.h" -#include "mesh_common.h" - -#include "net.h" #include "access.h" #include "beacon.h" +#include "mesh_common.h" #include "foundation.h" -#include "provisioner_prov.h" #include "proxy_client.h" -#include "provisioner_beacon.h" +#include "provisioner_prov.h" #include "provisioner_main.h" +#include "mesh_bearer_adapt.h" #define PDU_TYPE(data) (data[0] & BIT_MASK(6)) #define PDU_SAR(data) (data[0] >> 6) @@ -67,9 +61,9 @@ static u8_t server_buf_data[SERVER_BUF_SIZE * BLE_MESH_MAX_CONN]; static struct bt_mesh_proxy_server *find_server(struct bt_mesh_conn *conn) { - u8_t i; + int i; - for (i = 0U; i < ARRAY_SIZE(servers); i++) { + for (i = 0; i < ARRAY_SIZE(servers); i++) { if (servers[i].conn == conn) { return &servers[i]; } @@ -82,7 +76,7 @@ static void proxy_sar_timeout(struct k_work *work) { struct bt_mesh_proxy_server *server = NULL; - BT_DBG("%s", __func__); + BT_WARN("%s", __func__); server = CONTAINER_OF(work, struct bt_mesh_proxy_server, sar_timer.work); if (!server || !server->conn) { @@ -128,8 +122,8 @@ static void filter_status(struct bt_mesh_proxy_server *server, struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) { - u8_t filter_type; - u16_t list_size; + u8_t filter_type = 0U; + u16_t list_size = 0U; if (buf->len != 3) { BT_ERR("%s, Invalid Proxy Filter Status length %d", __func__, buf->len); @@ -144,7 +138,7 @@ static void filter_status(struct bt_mesh_proxy_server *server, list_size = net_buf_simple_pull_be16(buf); - BT_DBG("%s, filter_type 0x%02x list_size %d", __func__, filter_type, list_size); + BT_INFO("%s, filter_type 0x%02x list_size %d", __func__, filter_type, list_size); if (proxy_client_filter_status_recv_cb) { proxy_client_filter_status_recv_cb(server - servers, rx->ctx.addr, server->net_idx, filter_type, list_size); @@ -157,8 +151,8 @@ static void proxy_cfg(struct bt_mesh_proxy_server *server) { NET_BUF_SIMPLE_DEFINE(buf, 29); struct bt_mesh_net_rx rx = {0}; - u8_t opcode; - int err; + u8_t opcode = 0U; + int err = 0; err = bt_mesh_net_decode(&server->buf, BLE_MESH_NET_IF_PROXY_CFG, &rx, &buf); @@ -205,15 +199,7 @@ static void proxy_complete_pdu(struct bt_mesh_proxy_server *server) break; case BLE_MESH_PROXY_BEACON: BT_DBG("Mesh Beacon PDU"); - if (bt_mesh_is_provisioner_en()) { -#if CONFIG_BLE_MESH_PROVISIONER - provisioner_beacon_recv(&server->buf); -#endif - } else { -#if CONFIG_BLE_MESH_NODE - bt_mesh_beacon_recv(&server->buf); -#endif - } + bt_mesh_beacon_recv(&server->buf, 0); break; case BLE_MESH_PROXY_CONFIG: BT_DBG("Mesh Configuration PDU"); @@ -223,7 +209,7 @@ static void proxy_complete_pdu(struct bt_mesh_proxy_server *server) #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT case BLE_MESH_PROXY_PROV: BT_DBG("Mesh Provisioning PDU"); - provisioner_pb_gatt_recv(server->conn, &server->buf); + bt_mesh_provisioner_pb_gatt_recv(server->conn, &server->buf); break; #endif default: @@ -242,7 +228,7 @@ static ssize_t proxy_recv(struct bt_mesh_conn *conn, { struct bt_mesh_proxy_server *server = find_server(conn); const u8_t *data = buf; - u16_t srvc_uuid; + u16_t srvc_uuid = 0U; if (!server) { BT_ERR("%s, No Proxy Server object found", __func__); @@ -339,8 +325,8 @@ static int proxy_send(struct bt_mesh_conn *conn, const void *data, u16_t len) static int proxy_segment_and_send(struct bt_mesh_conn *conn, u8_t type, struct net_buf_simple *msg) { - u16_t mtu; - int err; + u16_t mtu = 0U; + int err = 0; if (conn == NULL) { BT_ERR("%s, Invalid parameter", __func__); @@ -438,7 +424,7 @@ static void proxy_disconnected(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn, #if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT if (server->conn_type == PROV) { - provisioner_pb_gatt_close(conn, reason); + bt_mesh_provisioner_pb_gatt_close(conn, reason); } #endif @@ -473,12 +459,12 @@ static ssize_t prov_write_ccc(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn) if (server->conn_type == NONE) { server->conn_type = PROV; - if (provisioner_set_prov_conn(addr->val, server->conn)) { - BT_ERR("%s, provisioner_set_prov_conn failed", __func__); + if (bt_mesh_provisioner_set_prov_conn(addr->val, server->conn)) { + BT_ERR("%s, bt_mesh_provisioner_set_prov_conn failed", __func__); bt_mesh_gattc_disconnect(server->conn); return -EIO; } - return provisioner_pb_gatt_open(conn, addr->val); + return bt_mesh_provisioner_pb_gatt_open(conn, addr->val); } return -ENOMEM; @@ -500,13 +486,13 @@ static ssize_t prov_recv_ntf(struct bt_mesh_conn *conn, u8_t *data, u16_t len) return -EINVAL; } -int provisioner_pb_gatt_enable(void) +int bt_mesh_provisioner_pb_gatt_enable(void) { - u8_t i; + int i; BT_DBG("%s", __func__); - for (i = 0U; i < ARRAY_SIZE(servers); i++) { + for (i = 0; i < ARRAY_SIZE(servers); i++) { if (servers[i].conn) { servers[i].conn_type = PROV; } @@ -515,13 +501,13 @@ int provisioner_pb_gatt_enable(void) return 0; } -int provisioner_pb_gatt_disable(void) +int bt_mesh_provisioner_pb_gatt_disable(void) { - u8_t i; + int i; BT_DBG("%s", __func__); - for (i = 0U; i < ARRAY_SIZE(servers); i++) { + for (i = 0; i < ARRAY_SIZE(servers); i++) { struct bt_mesh_proxy_server *server = &servers[i]; if (server->conn && server->conn_type == PROV) { @@ -582,11 +568,11 @@ static ssize_t proxy_recv_ntf(struct bt_mesh_conn *conn, u8_t *data, u16_t len) */ int bt_mesh_proxy_client_enable(void) { - u8_t i; + int i; BT_DBG("%s", __func__); - for (i = 0U; i < ARRAY_SIZE(servers); i++) { + for (i = 0; i < ARRAY_SIZE(servers); i++) { if (servers[i].conn) { servers[i].conn_type = PROXY; } @@ -604,7 +590,7 @@ int bt_mesh_proxy_client_enable(void) int bt_mesh_proxy_client_disable(void) { - u8_t i; + int i; BT_DBG("%s", __func__); @@ -615,7 +601,7 @@ int bt_mesh_proxy_client_disable(void) * it should be disconnected. */ - for (i = 0U; i < ARRAY_SIZE(servers); i++) { + for (i = 0; i < ARRAY_SIZE(servers); i++) { struct bt_mesh_proxy_server *server = &servers[i]; if (server->conn && server->conn_type == PROXY) { @@ -645,7 +631,7 @@ static struct bt_mesh_prov_conn_cb conn_callbacks = { static struct bt_mesh_subnet *bt_mesh_is_net_id_exist(const u8_t net_id[8]) { struct bt_mesh_subnet *sub = NULL; - size_t size, i; + size_t size = 0U, i = 0U; size = bt_mesh_rx_netkey_size(); @@ -659,14 +645,14 @@ static struct bt_mesh_subnet *bt_mesh_is_net_id_exist(const u8_t net_id[8]) return NULL; } -void proxy_client_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr) +void bt_mesh_proxy_client_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr, s8_t rssi) { bt_mesh_proxy_adv_ctx_t ctx = {0}; - u8_t type; + u8_t type = 0U; /* Check if connection reaches the maximum limitation */ if (bt_mesh_gattc_get_free_conn_count() == 0) { - BT_WARN("%s, max connections", __func__); + BT_INFO("BLE connections for mesh reach max limit"); return; } @@ -697,14 +683,14 @@ void proxy_client_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t } if (proxy_client_adv_recv_cb) { - proxy_client_adv_recv_cb(addr, type, &ctx); + proxy_client_adv_recv_cb(addr, type, &ctx, rssi); } } int bt_mesh_proxy_client_connect(const u8_t addr[6], u8_t addr_type, u16_t net_idx) { bt_mesh_addr_t remote_addr = {0}; - int result; + int result = 0; if (!addr || addr_type > BLE_MESH_ADDR_RANDOM) { BT_ERR("%s, Invalid parameter", __func__); @@ -726,7 +712,7 @@ int bt_mesh_proxy_client_connect(const u8_t addr[6], u8_t addr_type, u16_t net_i int bt_mesh_proxy_client_disconnect(u8_t conn_handle) { - struct bt_mesh_conn *conn; + struct bt_mesh_conn *conn = NULL; if (conn_handle >= BLE_MESH_MAX_CONN) { BT_ERR("%s, Invalid parameter", __func__); @@ -748,12 +734,10 @@ int bt_mesh_proxy_client_disconnect(u8_t conn_handle) bool bt_mesh_proxy_client_send(struct net_buf_simple *buf, u16_t dst) { bool send = false; - int err; - u8_t i; + int err = 0; + int i; - BT_DBG("%u bytes to dst 0x%04x", buf->len, dst); - - for (i = 0U; i < ARRAY_SIZE(servers); i++) { + for (i = 0; i < ARRAY_SIZE(servers); i++) { struct bt_mesh_proxy_server *server = &servers[i]; NET_BUF_SIMPLE_DEFINE(msg, 32); @@ -771,6 +755,7 @@ bool bt_mesh_proxy_client_send(struct net_buf_simple *buf, u16_t dst) if (err) { BT_ERR("%s, Failed to send proxy net message (err %d)", __func__, err); } else { + BT_INFO("%u bytes to dst 0x%04x", buf->len, dst); send = true; } } @@ -791,33 +776,35 @@ static int beacon_send(struct bt_mesh_conn *conn, struct bt_mesh_subnet *sub) bool bt_mesh_proxy_client_beacon_send(struct bt_mesh_subnet *sub) { bool send = false; - int err; - u8_t i; + int err = 0; + int i; /* NULL means we send Secure Network Beacon on all subnets */ if (!sub) { - if (bt_mesh_is_provisioner_en()) { -#if CONFIG_BLE_MESH_PROVISIONER - for (i = 0U; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { - if (bt_mesh.p_sub[i] && bt_mesh.p_sub[i]->net_idx != BLE_MESH_KEY_UNUSED) { - send = bt_mesh_proxy_client_beacon_send(bt_mesh.p_sub[i]); - } - } -#endif - } else { #if CONFIG_BLE_MESH_NODE - for (i = 0U; i < ARRAY_SIZE(bt_mesh.sub); i++) { + if (bt_mesh_is_provisioned()) { + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { if (bt_mesh.sub[i].net_idx != BLE_MESH_KEY_UNUSED) { send = bt_mesh_proxy_client_beacon_send(&bt_mesh.sub[i]); } } -#endif + return send; } - +#endif /* CONFIG_BLE_MESH_NODE */ +#if CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + if (bt_mesh.p_sub[i] && bt_mesh.p_sub[i]->net_idx != BLE_MESH_KEY_UNUSED) { + send = bt_mesh_proxy_client_beacon_send(bt_mesh.p_sub[i]); + } + } + return send; + } +#endif /* CONFIG_BLE_MESH_PROVISIONER */ return send; } - for (i = 0U; i < ARRAY_SIZE(servers); i++) { + for (i = 0; i < ARRAY_SIZE(servers); i++) { if (servers[i].conn && servers[i].conn_type == PROXY) { err = beacon_send(servers[i].conn, sub); if (err) { @@ -844,17 +831,13 @@ static int send_proxy_cfg(struct bt_mesh_conn *conn, u16_t net_idx, struct bt_me .src = bt_mesh_primary_addr(), }; struct net_buf_simple *buf = NULL; - u16_t alloc_len; - int err; + u16_t alloc_len = 0U; + int err = 0; - if (bt_mesh_is_provisioner_en()) { -#if CONFIG_BLE_MESH_PROVISIONER - tx.sub = provisioner_subnet_get(net_idx); -#endif - } else { -#if CONFIG_BLE_MESH_NODE + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { tx.sub = bt_mesh_subnet_get(net_idx); -#endif + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + tx.sub = bt_mesh_provisioner_subnet_get(net_idx); } if (!tx.sub) { BT_ERR("%s, Failed to find subnet", __func__); @@ -940,7 +923,7 @@ static int send_proxy_cfg(struct bt_mesh_conn *conn, u16_t net_idx, struct bt_me int bt_mesh_proxy_client_send_cfg(u8_t conn_handle, u16_t net_idx, struct bt_mesh_proxy_cfg_pdu *pdu) { - struct bt_mesh_conn *conn; + struct bt_mesh_conn *conn = NULL; if (conn_handle >= BLE_MESH_MAX_CONN || !pdu || pdu->opcode > BLE_MESH_PROXY_CFG_FILTER_REMOVE) { BT_ERR("%s, Invalid parameter", __func__); @@ -971,10 +954,10 @@ int bt_mesh_proxy_client_send_cfg(u8_t conn_handle, u16_t net_idx, struct bt_mes int bt_mesh_proxy_prov_client_init(void) { - u8_t i; + int i; /* Initialize the server receive buffers */ - for (i = 0U; i < ARRAY_SIZE(servers); i++) { + for (i = 0; i < ARRAY_SIZE(servers); i++) { struct bt_mesh_proxy_server *server = &servers[i]; k_delayed_work_init(&server->sar_timer, proxy_sar_timeout); @@ -995,4 +978,22 @@ int bt_mesh_proxy_prov_client_init(void) return 0; } +int bt_mesh_proxy_prov_client_deinit(void) +{ + int i; + + /* Initialize the server receive buffers */ + for (i = 0; i < ARRAY_SIZE(servers); i++) { + struct bt_mesh_proxy_server *server = &servers[i]; + k_delayed_work_free(&server->sar_timer); + memset(server, 0, sizeof(struct bt_mesh_proxy_server)); + } + + memset(server_buf_data, 0, sizeof(server_buf_data)); + + bt_mesh_gattc_conn_cb_deregister(); + + return 0; +} + #endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy_client.h b/components/bt/esp_ble_mesh/mesh_core/proxy_client.h index 59f974ef2..18943f824 100644 --- a/components/bt/esp_ble_mesh/mesh_core/proxy_client.h +++ b/components/bt/esp_ble_mesh/mesh_core/proxy_client.h @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef _PROVISIONER_PROXY_H_ -#define _PROVISIONER_PROXY_H_ +#ifndef _PROXY_CLIENT_H_ +#define _PROXY_CLIENT_H_ -#include "mesh_buf.h" #include "net.h" +#include "mesh_bearer_adapt.h" #define BLE_MESH_PROXY_ADV_NET_ID 0x00 #define BLE_MESH_PROXY_ADV_NODE_ID 0x01 @@ -72,13 +72,13 @@ typedef struct { int bt_mesh_proxy_prov_client_send(struct bt_mesh_conn *conn, u8_t type, struct net_buf_simple *msg); -int provisioner_pb_gatt_enable(void); -int provisioner_pb_gatt_disable(void); +int bt_mesh_provisioner_pb_gatt_enable(void); +int bt_mesh_provisioner_pb_gatt_disable(void); int bt_mesh_proxy_client_enable(void); int bt_mesh_proxy_client_disable(void); -typedef void (*proxy_client_recv_adv_cb_t)(const bt_mesh_addr_t *addr, u8_t type, bt_mesh_proxy_adv_ctx_t *ctx); +typedef void (*proxy_client_recv_adv_cb_t)(const bt_mesh_addr_t *addr, u8_t type, bt_mesh_proxy_adv_ctx_t *ctx, s8_t rssi); typedef void (*proxy_client_connect_cb_t)(const bt_mesh_addr_t *addr, u8_t conn_handle, u16_t net_idx); typedef void (*proxy_client_disconnect_cb_t)(const bt_mesh_addr_t *addr, u8_t conn_handle, u16_t net_idx, u8_t reason); typedef void (*proxy_client_recv_filter_status_cb_t)(u8_t conn_handle, u16_t src, u16_t net_idx, u8_t filter_type, u16_t list_size); @@ -88,7 +88,7 @@ void bt_mesh_proxy_client_set_conn_cb(proxy_client_connect_cb_t cb); void bt_mesh_proxy_client_set_disconn_cb(proxy_client_disconnect_cb_t cb); void bt_mesh_proxy_client_set_filter_status_cb(proxy_client_recv_filter_status_cb_t cb); -void proxy_client_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr); +void bt_mesh_proxy_client_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr, s8_t rssi); int bt_mesh_proxy_client_connect(const u8_t addr[6], u8_t addr_type, u16_t net_idx); int bt_mesh_proxy_client_disconnect(u8_t conn_handle); @@ -98,5 +98,6 @@ bool bt_mesh_proxy_client_send(struct net_buf_simple *buf, u16_t dst); int bt_mesh_proxy_client_send_cfg(u8_t conn_handle, u16_t net_idx, struct bt_mesh_proxy_cfg_pdu *pdu); int bt_mesh_proxy_prov_client_init(void); +int bt_mesh_proxy_prov_client_deinit(void); -#endif /* _PROVISIONER_PROXY_H_ */ +#endif /* _PROXY_CLIENT_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy_server.c b/components/bt/esp_ble_mesh/mesh_core/proxy_server.c index 319f0ad2f..676b53388 100644 --- a/components/bt/esp_ble_mesh/mesh_core/proxy_server.c +++ b/components/bt/esp_ble_mesh/mesh_core/proxy_server.c @@ -9,24 +9,18 @@ #include #include -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_PROXY) -#include "mesh_buf.h" -#include "mesh_util.h" -#include "mesh_bearer_adapt.h" -#include "mesh_trace.h" - #include "mesh.h" #include "adv.h" -#include "net.h" #include "prov.h" #include "beacon.h" -#include "foundation.h" #include "access.h" +#include "foundation.h" #include "proxy_server.h" -#if CONFIG_BLE_MESH_NODE +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER /* Not support enabling Proxy Client and Proxy Server simultaneously */ _Static_assert(!(IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)), @@ -83,7 +77,9 @@ static bool prov_fast_adv; static struct bt_mesh_proxy_client { struct bt_mesh_conn *conn; +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) u16_t filter[CONFIG_BLE_MESH_PROXY_FILTER_SIZE]; +#endif enum __packed { NONE, WHITELIST, @@ -172,7 +168,7 @@ static int proxy_segment_and_send(struct bt_mesh_conn *conn, u8_t type, static int filter_set(struct bt_mesh_proxy_client *client, struct net_buf_simple *buf) { - u8_t type; + u8_t type = 0U; if (buf->len < 1) { BT_WARN("Too short Filter Set message"); @@ -250,8 +246,8 @@ static void send_filter_status(struct bt_mesh_proxy_client *client, .ctx = &rx->ctx, .src = bt_mesh_primary_addr(), }; - u16_t filter_size; - int i, err; + u16_t filter_size = 0U; + int i, err = 0; /* Configuration messages always have dst unassigned */ tx.ctx->addr = BLE_MESH_ADDR_UNASSIGNED; @@ -292,9 +288,9 @@ static void send_filter_status(struct bt_mesh_proxy_client *client, static void proxy_cfg(struct bt_mesh_proxy_client *client) { NET_BUF_SIMPLE_DEFINE(buf, 29); - struct bt_mesh_net_rx rx; - u8_t opcode; - int err; + struct bt_mesh_net_rx rx = {0}; + u8_t opcode = 0U; + int err = 0; err = bt_mesh_net_decode(&client->buf, BLE_MESH_NET_IF_PROXY_CFG, &rx, &buf); @@ -321,7 +317,7 @@ static void proxy_cfg(struct bt_mesh_proxy_client *client) break; case CFG_FILTER_ADD: while (buf.len >= 2) { - u16_t addr; + u16_t addr = 0U; addr = net_buf_simple_pull_be16(&buf); filter_add(client, addr); @@ -330,7 +326,7 @@ static void proxy_cfg(struct bt_mesh_proxy_client *client) break; case CFG_FILTER_REMOVE: while (buf.len >= 2) { - u16_t addr; + u16_t addr = 0U; addr = net_buf_simple_pull_be16(&buf); filter_remove(client, addr); @@ -355,7 +351,7 @@ static int beacon_send(struct bt_mesh_conn *conn, struct bt_mesh_subnet *sub) static void proxy_send_beacons(struct k_work *work) { - struct bt_mesh_proxy_client *client; + struct bt_mesh_proxy_client *client = NULL; int i; client = CONTAINER_OF(work, struct bt_mesh_proxy_client, send_beacons); @@ -450,7 +446,7 @@ static void proxy_complete_pdu(struct bt_mesh_proxy_client *client) break; case BLE_MESH_PROXY_BEACON: BT_DBG("Mesh Beacon PDU"); - bt_mesh_beacon_recv(&client->buf); + bt_mesh_beacon_recv(&client->buf, 0); break; case BLE_MESH_PROXY_CONFIG: BT_DBG("Mesh Configuration PDU"); @@ -561,7 +557,7 @@ static int conn_count; static void proxy_connected(struct bt_mesh_conn *conn, u8_t err) { - struct bt_mesh_proxy_client *client; + struct bt_mesh_proxy_client *client = NULL; int i; BT_DBG("conn %p err 0x%02x", conn, err); @@ -590,7 +586,9 @@ static void proxy_connected(struct bt_mesh_conn *conn, u8_t err) client->conn = bt_mesh_conn_ref(conn); client->filter_type = NONE; +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) (void)memset(client->filter, 0, sizeof(client->filter)); +#endif net_buf_simple_reset(&client->buf); } @@ -636,7 +634,7 @@ static ssize_t prov_ccc_write(struct bt_mesh_conn *conn, const void *buf, u16_t len, u16_t offset, u8_t flags) { - struct bt_mesh_proxy_client *client; + struct bt_mesh_proxy_client *client = NULL; u16_t *value = attr->user_data; BT_DBG("len %u: %s", len, bt_hex(buf, len)); @@ -771,8 +769,8 @@ static ssize_t proxy_ccc_write(struct bt_mesh_conn *conn, const void *buf, u16_t len, u16_t offset, u8_t flags) { - struct bt_mesh_proxy_client *client; - u16_t value; + struct bt_mesh_proxy_client *client = NULL; + u16_t value = 0U; BT_DBG("len %u: %s", len, bt_hex(buf, len)); @@ -901,7 +899,7 @@ void bt_mesh_proxy_addr_add(struct net_buf_simple *buf, u16_t addr) struct bt_mesh_proxy_client *client = CONTAINER_OF(buf, struct bt_mesh_proxy_client, buf); - BT_DBG("filter_type %u addr 0x%04x", client->filter_type, addr); + BT_INFO("filter_type %u addr 0x%04x", client->filter_type, addr); if (client->filter_type == WHITELIST) { filter_add(client, addr); @@ -998,7 +996,7 @@ static int proxy_send(struct bt_mesh_conn *conn, const void *data, u16_t len) static int proxy_segment_and_send(struct bt_mesh_conn *conn, u8_t type, struct net_buf_simple *msg) { - u16_t mtu; + u16_t mtu = 0U; BT_DBG("conn %p type 0x%02x len %u: %s", conn, type, msg->len, bt_hex(msg->data, msg->len)); @@ -1104,9 +1102,9 @@ static size_t gatt_proxy_adv_create(struct bt_mesh_adv_data *proxy_sd) static int node_id_adv(struct bt_mesh_subnet *sub) { struct bt_mesh_adv_data proxy_sd = {0}; - size_t proxy_sd_len; - u8_t tmp[16]; - int err; + size_t proxy_sd_len = 0U; + u8_t tmp[16] = {0}; + int err = 0; BT_DBG("%s", __func__); @@ -1144,8 +1142,8 @@ static int node_id_adv(struct bt_mesh_subnet *sub) static int net_id_adv(struct bt_mesh_subnet *sub) { struct bt_mesh_adv_data proxy_sd = {0}; - size_t proxy_sd_len; - int err; + size_t proxy_sd_len = 0U; + int err = 0; BT_DBG("%s", __func__); @@ -1225,7 +1223,7 @@ static int sub_count(void) static s32_t gatt_proxy_advertise(struct bt_mesh_subnet *sub) { s32_t remaining = K_FOREVER; - int subnet_count; + int subnet_count = 0; BT_DBG("%s", __func__); @@ -1264,7 +1262,7 @@ static s32_t gatt_proxy_advertise(struct bt_mesh_subnet *sub) subnet_count = sub_count(); BT_DBG("sub_count %u", subnet_count); if (subnet_count > 1) { - s32_t max_timeout; + s32_t max_timeout = 0; /* We use NODE_ID_TIMEOUT as a starting point since it may * be less than 60 seconds. Divide this period into at least @@ -1291,8 +1289,8 @@ static size_t gatt_prov_adv_create(struct bt_mesh_adv_data prov_sd[2]) const struct bt_mesh_prov *prov = bt_mesh_prov_get(); const char *name = device_name; size_t name_len = strlen(name); - size_t prov_sd_len = 0; - size_t sd_space = 31; + size_t prov_sd_len = 0U; + size_t sd_space = 31U; memcpy(prov_svc_data + 2, prov->uuid, 16); sys_put_be16(prov->oob_info, prov_svc_data + 18); @@ -1377,7 +1375,7 @@ s32_t bt_mesh_proxy_adv_start(void) void bt_mesh_proxy_adv_stop(void) { - int err; + int err = 0; BT_DBG("adv_enabled %u", proxy_adv_enabled); @@ -1402,6 +1400,14 @@ int bt_mesh_proxy_init(void) { int i; +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + bt_mesh_gatts_service_register(&proxy_svc); +#endif + +#if defined(CONFIG_BLE_MESH_PB_GATT) + bt_mesh_gatts_service_register(&prov_svc); +#endif + /* Initialize the client receive buffers */ for (i = 0; i < ARRAY_SIZE(clients); i++) { struct bt_mesh_proxy_client *client = &clients[i]; @@ -1422,4 +1428,33 @@ int bt_mesh_proxy_init(void) return bt_mesh_gatts_set_local_device_name(device_name); } -#endif /* CONFIG_BLE_MESH_NODE */ +int bt_mesh_proxy_deinit(void) +{ + int i; + + proxy_adv_enabled = false; + gatt_svc = MESH_GATT_NONE; + +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + bt_mesh_gatts_service_deregister(&proxy_svc); +#endif + +#if defined(CONFIG_BLE_MESH_PB_GATT) + bt_mesh_gatts_service_deregister(&prov_svc); +#endif + + for (i = 0; i < ARRAY_SIZE(clients); i++) { + struct bt_mesh_proxy_client *client = &clients[i]; + k_delayed_work_free(&client->sar_timer); + memset(client, 0, sizeof(struct bt_mesh_proxy_client)); + } + + memset(client_buf_data, 0, sizeof(client_buf_data)); + memset(device_name, 0, sizeof(device_name)); + + bt_mesh_gatts_conn_cb_deregister(); + + return 0; +} + +#endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy_server.h b/components/bt/esp_ble_mesh/mesh_core/proxy_server.h index 41b89c369..df4a73eda 100644 --- a/components/bt/esp_ble_mesh/mesh_core/proxy_server.h +++ b/components/bt/esp_ble_mesh/mesh_core/proxy_server.h @@ -11,7 +11,6 @@ #define _PROXY_H_ #include "net.h" -#include "mesh_buf.h" #include "mesh_bearer_adapt.h" #define BLE_MESH_PROXY_NET_PDU 0x00 @@ -61,5 +60,6 @@ bool bt_mesh_proxy_relay(struct net_buf_simple *buf, u16_t dst); void bt_mesh_proxy_addr_add(struct net_buf_simple *buf, u16_t addr); int bt_mesh_proxy_init(void); +int bt_mesh_proxy_deinit(void); #endif /* _PROXY_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index 57393b903..2792d893d 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -6,31 +6,21 @@ */ #include -#include #include -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_SETTINGS) -#include "mesh_types.h" -#include "mesh_util.h" -#include "mesh_access.h" -#include "mesh_main.h" -#include "mesh_buf.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" -#include "mesh_common.h" - #include "mesh.h" -#include "net.h" #include "crypto.h" #include "transport.h" #include "access.h" #include "foundation.h" #include "proxy_server.h" #include "cfg_srv.h" - +#include "mesh_common.h" #include "settings_nvs.h" +#include "provisioner_main.h" +#include "provisioner_prov.h" /* BLE Mesh NVS Key and corresponding data struct. * Note: The length of nvs key must be <= 15. @@ -159,11 +149,52 @@ static struct { struct cfg_val cfg; } stored_cfg; +struct prov_info { + u16_t primary_addr; + u16_t alloc_addr; +}; + +struct node_info { + u8_t addr[6]; + u8_t addr_type; + u8_t dev_uuid[16]; + u16_t oob_info; + u16_t unicast_addr; + u8_t element_num; + u16_t net_idx; + u8_t flags; + u32_t iv_index; + u8_t dev_key[16]; +} __packed; + +#define DEVICE_ROLE_BITS (BIT(BLE_MESH_NODE) | BIT(BLE_MESH_PROVISIONER)) + +static int role_set(const char *name) +{ + bool exist = false; + int err = 0; + + BT_DBG("%s", __func__); + + err = bt_mesh_load_core_settings(name, (u8_t *)bt_mesh.flags, sizeof(bt_mesh.flags), &exist); + if (err) { + return err; + } + + if (exist == false) { + return 0; + } + + BT_INFO("Restored mesh device role %lu", bt_mesh_atomic_get(bt_mesh.flags) & DEVICE_ROLE_BITS); + + return 0; +} + static int net_set(const char *name) { struct net_val net = {0}; - bool exist; - int err; + bool exist = false; + int err = 0; BT_DBG("%s", __func__); @@ -182,8 +213,8 @@ static int net_set(const char *name) memcpy(bt_mesh.dev_key, net.dev_key, sizeof(bt_mesh.dev_key)); bt_mesh_comp_provision(net.primary_addr); - BT_DBG("Provisioned with primary address 0x%04x", net.primary_addr); - BT_DBG("Recovered DevKey %s", bt_hex(bt_mesh.dev_key, 16)); + BT_INFO("Restored primary address 0x%04x", net.primary_addr); + BT_INFO("Restored DevKey %s", bt_hex(bt_mesh.dev_key, 16)); return 0; } @@ -191,8 +222,8 @@ static int net_set(const char *name) static int iv_set(const char *name) { struct iv_val iv = {0}; - bool exist; - int err; + bool exist = false; + int err = 0; BT_DBG("%s", __func__); @@ -212,7 +243,7 @@ static int iv_set(const char *name) bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS, iv.iv_update); bt_mesh.ivu_duration = iv.iv_duration; - BT_DBG("IV Index 0x%04x (IV Update Flag %u) duration %u hours", + BT_INFO("Restored IV Index 0x%04x (IV Update Flag %u) duration %u hours", iv.iv_index, iv.iv_update, iv.iv_duration); return 0; @@ -221,8 +252,8 @@ static int iv_set(const char *name) static int seq_set(const char *name) { struct seq_val seq = {0}; - bool exist; - int err; + bool exist = false; + int err = 0; BT_DBG("%s", __func__); @@ -240,17 +271,17 @@ static int seq_set(const char *name) bt_mesh.seq = ((u32_t)seq.val[0] | ((u32_t)seq.val[1] << 8) | ((u32_t)seq.val[2] << 16)); - if (CONFIG_BLE_MESH_SEQ_STORE_RATE > 0) { - /* Make sure we have a large enough sequence number. We - * subtract 1 so that the first transmission causes a write - * to the settings storage. - */ - bt_mesh.seq += (CONFIG_BLE_MESH_SEQ_STORE_RATE - - (bt_mesh.seq % CONFIG_BLE_MESH_SEQ_STORE_RATE)); - bt_mesh.seq--; - } +#if CONFIG_BLE_MESH_SEQ_STORE_RATE > 0 + /* Make sure we have a large enough sequence number. We + * subtract 1 so that the first transmission causes a write + * to the settings storage. + */ + bt_mesh.seq += (CONFIG_BLE_MESH_SEQ_STORE_RATE - + (bt_mesh.seq % CONFIG_BLE_MESH_SEQ_STORE_RATE)); + bt_mesh.seq--; +#endif - BT_DBG("Sequence Number 0x%06x", bt_mesh.seq); + BT_INFO("Restored Sequence Number 0x%06x", bt_mesh.seq); return 0; } @@ -288,8 +319,8 @@ static int rpl_set(const char *name) struct bt_mesh_rpl *entry = NULL; struct rpl_val rpl = {0}; char get[16] = {'\0'}; - size_t length; - bool exist; + bool exist = false; + size_t length = 0U; int err = 0; int i; @@ -327,7 +358,7 @@ static int rpl_set(const char *name) } } - BT_DBG("RPL 0x%04x: Seq 0x%06x, old_iv %u", src, rpl.seq, rpl.old_iv); + BT_INFO("Restored RPL 0x%04x: Seq 0x%06x, old_iv %u", src, rpl.seq, rpl.old_iv); entry->src = src; entry->seq = rpl.seq; entry->old_iv = rpl.old_iv; @@ -358,8 +389,8 @@ static int net_key_set(const char *name) struct bt_mesh_subnet *sub = NULL; struct net_key_val key = {0}; char get[16] = {'\0'}; - size_t length; - bool exist; + bool exist = false; + size_t length = 0U; int err = 0; int i; @@ -396,7 +427,7 @@ static int net_key_set(const char *name) } } - BT_DBG("NetKeyIndex 0x%03x recovered from storage", net_idx); + BT_INFO("Restored NetKey Index 0x%03x", net_idx); sub->net_idx = net_idx; sub->kr_flag = key.kr_flag; sub->kr_phase = key.kr_phase; @@ -416,8 +447,8 @@ static int app_key_set(const char *name) struct net_buf_simple *buf = NULL; struct app_key_val key = {0}; char get[16] = {'\0'}; - size_t length; - bool exist; + bool exist = false; + size_t length = 0U; int err = 0; int i; @@ -461,7 +492,7 @@ static int app_key_set(const char *name) } } - BT_DBG("AppKeyIndex 0x%03x recovered from storage", app_idx); + BT_INFO("Restored AppKey Index 0x%03x", app_idx); app->net_idx = key.net_idx; app->app_idx = app_idx; app->updated = key.updated; @@ -480,8 +511,8 @@ static int hb_pub_set(const char *name) { struct bt_mesh_hb_pub *hb_pub = bt_mesh_hb_pub_get(); struct hb_pub_val hb_val = {0}; - bool exist; - int err; + bool exist = false; + int err = 0; BT_DBG("%s", __func__); @@ -516,7 +547,7 @@ static int hb_pub_set(const char *name) hb_pub->count = 0U; } - BT_DBG("Restore Heartbeat Publication"); + BT_INFO("Restored heartbeat publication, dst 0x%04x", hb_pub->dst); return 0; } @@ -525,8 +556,8 @@ static int cfg_set(const char *name) { struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get(); struct cfg_val val = {0}; - bool exist; - int err; + bool exist = false; + int err = 0; BT_DBG("%s", __func__); @@ -549,15 +580,16 @@ static int cfg_set(const char *name) memcpy(&stored_cfg.cfg, &val, sizeof(val)); stored_cfg.valid = true; - BT_DBG("Restore configuration state"); + BT_INFO("Restored configuration state"); return 0; } static int model_set_bind(bool vnd, struct bt_mesh_model *model, u16_t model_key) { char name[16] = {'\0'}; - bool exist; - int i, err; + bool exist = false; + int err = 0; + int i; /* Start with empty array regardless of cleared or set value */ for (i = 0; i < ARRAY_SIZE(model->keys); i++) { @@ -577,8 +609,9 @@ static int model_set_bind(bool vnd, struct bt_mesh_model *model, u16_t model_key static int model_set_sub(bool vnd, struct bt_mesh_model *model, u16_t model_key) { char name[16] = {'\0'}; - bool exist; - int i, err; + bool exist = false; + int err = 0; + int i; /* Start with empty array regardless of cleared or set value */ for (i = 0; i < ARRAY_SIZE(model->groups); i++) { @@ -599,8 +632,8 @@ static int model_set_pub(bool vnd, struct bt_mesh_model *model, u16_t model_key) { struct mod_pub_val pub = {0}; char name[16] = {'\0'}; - bool exist; - int err; + bool exist = false; + int err = 0; if (!model->pub) { BT_WARN("%s, Model has no publication context", __func__); @@ -633,7 +666,7 @@ static int model_set_pub(bool vnd, struct bt_mesh_model *model, u16_t model_key) model->pub->retransmit = pub.retransmit; model->pub->count = 0U; - BT_DBG("Restore model publication, pub_addr 0x%04x app_idx 0x%03x", + BT_INFO("Restored model publication, pub_addr 0x%04x, app_idx 0x%03x", pub.addr, pub.key); return 0; @@ -643,8 +676,8 @@ static int model_set(bool vnd, const char *name) { struct bt_mesh_model *model = NULL; struct net_buf_simple *buf = NULL; - u8_t elem_idx, model_idx; - size_t length; + u8_t elem_idx = 0U, model_idx = 0U; + size_t length = 0U; int err = 0; int i; @@ -710,11 +743,11 @@ static int va_set(const char *name) struct net_buf_simple *buf = NULL; struct va_val va = {0}; char get[16] = {'\0'}; - struct label *lab; - size_t length; - bool exist; + struct label *lab = NULL; + size_t length = 0U; + bool exist = false; int err = 0; - u16_t i; + int i; BT_DBG("%s", __func__); @@ -725,7 +758,7 @@ static int va_set(const char *name) length = buf->len; - for (i = 0U; i < length / SETTINGS_ITEM_SIZE; i++) { + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { u16_t index = net_buf_simple_pull_le16(buf); sprintf(get, "mesh/va/%04x", index); @@ -755,7 +788,7 @@ static int va_set(const char *name) lab->addr = va.addr; lab->ref = va.ref; - BT_DBG("Restore virtual address 0x%04x ref 0x%04x", index, lab->ref); + BT_INFO("Restored virtual address 0x%04x ref 0x%04x", index, lab->ref); } free: @@ -764,25 +797,435 @@ free: } #endif +#if CONFIG_BLE_MESH_PROVISIONER +static int p_prov_set(const char *name) +{ + struct prov_info val = {0}; + bool exist = false; + int err = 0; + + BT_DBG("%s", __func__); + + err = bt_mesh_load_core_settings(name, (u8_t *)&val, sizeof(val), &exist); + if (err) { + BT_ERR("%s, Failed to load Provisioner prov info", __func__); + return 0; + } + + if (exist == false) { + return 0; + } + + bt_mesh_provisoner_restore_prov_info(val.primary_addr, val.alloc_addr); + + BT_INFO("Restored primary_addr 0x%04x, alloc_addr 0x%04x", + val.primary_addr, val.alloc_addr); + + return 0; +} + +static int p_net_idx_set(const char *name) +{ + u16_t net_idx = 0U; + bool exist = false; + int err = 0; + + BT_DBG("%s", __func__); + + err = bt_mesh_load_core_settings(name, (u8_t *)&net_idx, sizeof(net_idx), &exist); + if (err) { + BT_ERR("%s, Failed to load Provisioner keys", __func__); + return 0; + } + + if (exist == false) { + return 0; + } + + bt_mesh.p_net_idx_next = net_idx; + + BT_INFO("Restored p_net_idx_next 0x%04x", bt_mesh.p_net_idx_next); + + return 0; +} + +static int p_app_idx_set(const char *name) +{ + u16_t app_idx = 0U; + bool exist = false; + int err = 0; + + BT_DBG("%s", __func__); + + err = bt_mesh_load_core_settings(name, (u8_t *)&app_idx, sizeof(app_idx), &exist); + if (err) { + return 0; + } + + if (exist == false) { + return 0; + } + + bt_mesh.p_app_idx_next = app_idx; + + BT_INFO("Restored p_app_idx_next 0x%04x", bt_mesh.p_app_idx_next); + + return 0; +} + +static struct bt_mesh_subnet *p_subnet_alloc(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + if (bt_mesh.p_sub[i] == NULL) { + bt_mesh.p_sub[i] = bt_mesh_calloc(sizeof(struct bt_mesh_subnet)); + if (!bt_mesh.p_sub[i]) { + BT_ERR("%s, Failed to allocate memory", __func__); + return NULL; + } + + return bt_mesh.p_sub[i]; + } + } + + return NULL; +} + +static struct bt_mesh_app_key *p_appkey_alloc(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + if (bt_mesh.p_app_keys[i] == NULL) { + bt_mesh.p_app_keys[i] = bt_mesh_calloc(sizeof(struct bt_mesh_app_key)); + if (!bt_mesh.p_app_keys[i]) { + BT_ERR("%s, Failed to allocate memory", __func__); + return NULL; + } + + return bt_mesh.p_app_keys[i]; + } + } + + return NULL; +} + +static int p_net_key_set(const char *name) +{ + struct net_buf_simple *buf = NULL; + struct bt_mesh_subnet *sub = NULL; + struct net_key_val key = {0}; + char get[16] = {'\0'}; + size_t length = 0U; + bool exist = false; + int err = 0; + int i; + + BT_DBG("%s", __func__); + + buf = bt_mesh_get_core_settings_item(name); + if (!buf) { + return 0; + } + + length = buf->len; + + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { + u16_t net_idx = net_buf_simple_pull_le16(buf); + sprintf(get, "mesh/pnk/%04x", net_idx); + + err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist); + if (err) { + BT_ERR("%s, Failed to load Provisioner NetKey %s", __func__, get); + goto free; + } + + if (exist == false) { + continue; + } + + sub = bt_mesh_provisioner_subnet_get(net_idx); + if (!sub) { + sub = p_subnet_alloc(); + if (!sub) { + BT_ERR("%s, No space for a new subnet 0x%03x", __func__, net_idx); + err = -ENOMEM; + goto free; + } + } + + sub->net_idx = net_idx; + sub->kr_flag = key.kr_flag; + sub->kr_phase = key.kr_phase; + memcpy(sub->keys[0].net, &key.val[0], 16); + memcpy(sub->keys[1].net, &key.val[1], 16); + + BT_INFO("Restored NetIdx 0x%03x", sub->net_idx); + BT_INFO("Restored NetKey %s", bt_hex(sub->keys[0].net, 16)); + } + +free: + bt_mesh_free_buf(buf); + return err; +} + +static int p_app_key_set(const char *name) +{ + struct bt_mesh_app_key *app = NULL; + struct bt_mesh_subnet *sub = NULL; + struct net_buf_simple *buf = NULL; + struct app_key_val key = {0}; + char get[16] = {'\0'}; + size_t length = 0U; + bool exist = false; + int err = 0; + int i; + + BT_DBG("%s", __func__); + + buf = bt_mesh_get_core_settings_item(name); + if (!buf) { + return 0; + } + + length = buf->len; + + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { + u16_t app_idx = net_buf_simple_pull_le16(buf); + sprintf(get, "mesh/pak/%04x", app_idx); + + err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist); + if (err) { + BT_ERR("%s, Failed to load Provisioner AppKey %s", __func__, get); + goto free; + } + + if (exist == false) { + continue; + } + + sub = bt_mesh_provisioner_subnet_get(key.net_idx); + if (!sub) { + BT_ERR("%s, Failed to find subnet 0x%03x", __func__, key.net_idx); + err = -ENOENT; + goto free; + } + + app = bt_mesh_provisioner_app_key_find(app_idx); + if (!app) { + app = p_appkey_alloc(); + if (!app) { + BT_ERR("%s, No space for a new app key 0x%03x", __func__, app_idx); + err = -ENOMEM; + goto free; + } + } + + app->net_idx = key.net_idx; + app->app_idx = app_idx; + app->updated = key.updated; + memcpy(app->keys[0].val, key.val[0], 16); + memcpy(app->keys[1].val, key.val[1], 16); + bt_mesh_app_id(app->keys[0].val, &app->keys[0].id); + bt_mesh_app_id(app->keys[1].val, &app->keys[1].id); + + BT_INFO("Restored AppIdx %03x, NetIdx 0x%03x", app->app_idx, app->net_idx); + BT_INFO("Restored AppKey %s", bt_hex(app->keys[0].val, 16)); + } + +free: + bt_mesh_free_buf(buf); + return err; +} + +static int node_info_set(u16_t addr, bool prov, bool *exist) +{ + struct bt_mesh_node node = {0}; + struct node_info info = {0}; + char get[16] = {'\0'}; + int err = 0; + + sprintf(get, prov ? "mesh/pn/%04x/i" : "mesh/sn/%04x/i", addr); + err = bt_mesh_load_core_settings(get, (u8_t *)&info, sizeof(info), exist); + if (err) { + BT_ERR("%s, Failed to load node %s", __func__, get); + return -EIO; + } + + if (*exist == false) { + return 0; + } + + memcpy(node.addr, info.addr, BLE_MESH_ADDR_LEN); + node.addr_type = info.addr_type; + memcpy(node.dev_uuid, info.dev_uuid, 16); + node.oob_info = info.oob_info; + node.unicast_addr = info.unicast_addr; + node.element_num = info.element_num; + node.net_idx = info.net_idx; + node.flags = info.flags; + node.iv_index = info.iv_index; + memcpy(node.dev_key, info.dev_key, 16); + + err = bt_mesh_provisioner_restore_node_info(&node, prov); + if (err) { + BT_ERR("%s, Failed to restore node 0x%04x", __func__, addr); + return -EIO; + } + + BT_INFO("Restored node 0x%04x, uuid %s", addr, bt_hex(node.dev_uuid, 16)); + + return 0; +} + +static int node_name_set(u16_t addr, bool prov) +{ + char name[BLE_MESH_NODE_NAME_SIZE] = {0}; + char get[16] = {'\0'}; + bool exist = false; + int err = 0; + + sprintf(get, prov ? "mesh/pn/%04x/n" : "mesh/sn/%04x/n", addr); + err = bt_mesh_load_core_settings(get, (u8_t *)name, BLE_MESH_NODE_NAME_SIZE, &exist); + if (err) { + BT_ERR("%s, Failed to load node name %s", __func__, get); + return -EIO; + } + + if (exist == false) { + return 0; + } + + err = bt_mesh_provisioner_restore_node_name(addr, name); + if (err) { + BT_ERR("%s, Failed to restore node name 0x%04x", __func__, addr); + return -EIO; + } + + BT_INFO("Restored node 0x%04x, name %s", addr, name); + + return 0; +} + +static int node_comp_data_set(u16_t addr, bool prov) +{ + struct net_buf_simple *buf = NULL; + char get[16] = {'\0'}; + int err = 0; + + sprintf(get, prov ? "mesh/pn/%04x/c" : "mesh/sn/%04x/c", addr); + buf = bt_mesh_get_core_settings_item(get); + if (!buf) { + return 0; + } + + err = bt_mesh_provisioner_restore_node_comp_data(addr, buf->data, buf->len, prov); + if (err) { + BT_ERR("%s, Failed to restore node comp data 0x%04x", __func__, addr); + } + + BT_INFO("Restored node 0x%04x, comp data %s", addr, bt_hex(buf->data, buf->len)); + + bt_mesh_free_buf(buf); + return err; +} + +static int p_node_set(const char *name) +{ + struct net_buf_simple *buf = NULL; + bool exist = false, prov = false; + size_t length = 0U; + int err = 0; + int i; + + buf = bt_mesh_get_core_settings_item(name); + if (!buf) { + return 0; + } + + prov = strcmp(name, "mesh/p_pnode") == 0 ? true : false; + length = buf->len; + + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { + u16_t addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { + BT_ERR("%s, 0x%04x is not a unicast address", __func__, addr); + goto free; + } + + err = node_info_set(addr, prov, &exist); + if (err) { + BT_ERR("%s, Failed to load node 0x%04x info", __func__, addr); + goto free; + } + + if (exist == false) { + continue; + } + + err = node_name_set(addr, prov); + if (err) { + BT_ERR("%s, Failed to load node 0x%04x name", __func__, addr); + goto free; + } + + err = node_comp_data_set(addr, prov); + if (err) { + BT_ERR("%s, Failed to load node 0x%04x comp data", __func__, addr); + goto free; + } + } + +free: + bt_mesh_free_buf(buf); + return err; +} +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + const struct bt_mesh_setting { const char *name; int (*func)(const char *name); } settings[] = { - { "mesh/net", net_set }, - { "mesh/iv", iv_set }, - { "mesh/seq", seq_set }, - { "mesh/rpl", rpl_set }, - { "mesh/netkey", net_key_set }, - { "mesh/appkey", app_key_set }, - { "mesh/hb_pub", hb_pub_set }, - { "mesh/cfg", cfg_set }, - { "mesh/sig", sig_mod_set }, - { "mesh/vnd", vnd_mod_set }, + { "mesh/role", role_set }, + { "mesh/net", net_set }, + { "mesh/iv", iv_set }, + { "mesh/seq", seq_set }, + { "mesh/rpl", rpl_set }, + { "mesh/netkey", net_key_set }, + { "mesh/appkey", app_key_set }, + { "mesh/hb_pub", hb_pub_set }, + { "mesh/cfg", cfg_set }, + { "mesh/sig", sig_mod_set }, + { "mesh/vnd", vnd_mod_set }, #if CONFIG_BLE_MESH_LABEL_COUNT > 0 - { "mesh/vaddr", va_set }, + { "mesh/vaddr", va_set }, +#endif +#if CONFIG_BLE_MESH_PROVISIONER + { "mesh/p_prov", p_prov_set }, + { "mesh/p_netidx", p_net_idx_set }, + { "mesh/p_appidx", p_app_idx_set }, + { "mesh/p_netkey", p_net_key_set }, + { "mesh/p_appkey", p_app_key_set }, + { "mesh/p_pnode", p_node_set }, + { "mesh/p_snode", p_node_set }, #endif }; +/** + * For Provisioner, the load operation needs the following actions: + * net_set: Not needed + * iv_set: Need, although Provisioner will do some initialization of IV Index + * during startup, but we need to restore the last IV Index status + * seq_set: Need, restore the previous sequence number + * rpl_set: Need, restore the previous Replay Protection List + * net_key_set: Need, restore the previous network keys + * app_key_set: Need, restore the previous application keys + * hb_pub_set: Not needed currently + * cfg_set: Not needed currently + * sig_mod_set: Need, restore SIG models related info (app, sub, pub) + * vnd_mod_set: Need, restore vendor models related info (app, sub, pub) + */ int settings_core_load(void) { int i; @@ -790,7 +1233,41 @@ int settings_core_load(void) BT_DBG("%s", __func__); for (i = 0; i < ARRAY_SIZE(settings); i++) { + if ((!strcmp(settings[i].name, "mesh/net") || + !strcmp(settings[i].name, "mesh/netkey") || + !strcmp(settings[i].name, "mesh/appkey") || + !strcmp(settings[i].name, "mesh/hb_pub") || + !strcmp(settings[i].name, "mesh/cfg")) && + (!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioner())) { + BT_DBG("Not restoring %s for Provisioner", settings[i].name); + continue; + } + + if ((!strcmp(settings[i].name, "mesh/p_prov") || + !strcmp(settings[i].name, "mesh/p_netidx") || + !strcmp(settings[i].name, "mesh/p_appidx") || + !strcmp(settings[i].name, "mesh/p_netkey") || + !strcmp(settings[i].name, "mesh/p_appkey") || + !strcmp(settings[i].name, "mesh/p_pnode") || + !strcmp(settings[i].name, "mesh/p_snode")) && + (!IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) || bt_mesh_is_node())) { + BT_DBG("Not restoring %s for node", settings[i].name); + continue; + } + settings[i].func(settings[i].name); + + if (!strcmp(settings[i].name, "mesh/role")) { + u8_t role = bt_mesh_atomic_get(bt_mesh.flags) & DEVICE_ROLE_BITS; + if (role == 0U) { + BT_INFO("Device just starts up, nothing restored"); + return 0; + } + if (role != BIT(BLE_MESH_NODE) && role != BIT(BLE_MESH_PROVISIONER)) { + BT_ERR("Invalid restored device role %d", role); + return 0; + } + } } return 0; @@ -798,7 +1275,7 @@ int settings_core_load(void) static int subnet_init(struct bt_mesh_subnet *sub) { - int err; + int err = 0; err = bt_mesh_net_keys_create(&sub->keys[0], sub->keys[0].net); if (err) { @@ -842,36 +1319,64 @@ static void commit_model(struct bt_mesh_model *model, struct bt_mesh_elem *elem, int settings_core_commit(void) { - struct bt_mesh_hb_pub *hb_pub = NULL; - struct bt_mesh_cfg_srv *cfg = NULL; + struct bt_mesh_subnet *sub = NULL; + int err = 0; int i; - BT_DBG("sub[0].net_idx 0x%03x", bt_mesh.sub[0].net_idx); - - if (bt_mesh.sub[0].net_idx == BLE_MESH_KEY_UNUSED) { - /* Nothing to do since we're not yet provisioned */ - return 0; - } - - if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { #if defined(CONFIG_BLE_MESH_NODE) - bt_mesh_proxy_prov_disable(true); -#endif - } + if (bt_mesh_is_node()) { + BT_INFO("sub[0].net_idx 0x%03x", bt_mesh.sub[0].net_idx); - for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { - struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; - int err; - - if (sub->net_idx == BLE_MESH_KEY_UNUSED) { - continue; + if (bt_mesh.sub[0].net_idx == BLE_MESH_KEY_UNUSED) { + /* Nothing to do since we're not yet provisioned */ + return 0; } - err = subnet_init(sub); - if (err) { - BT_ERR("%s, Failed to init subnet 0x%03x", __func__, sub->net_idx); + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { + bt_mesh_proxy_prov_disable(true); + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + sub = &bt_mesh.sub[i]; + + if (sub->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + err = subnet_init(sub); + if (err) { + BT_ERR("%s, Failed to init subnet 0x%03x", __func__, sub->net_idx); + } } } +#endif /* CONFIG_BLE_MESH_NODE */ + +#if defined(CONFIG_BLE_MESH_PROVISIONER) + if (bt_mesh_is_provisioner()) { + if (bt_mesh.p_sub[0] == NULL || + bt_mesh.p_sub[0]->net_idx == BLE_MESH_KEY_UNUSED) { + return 0; + } + + BT_INFO("p_sub[0]->net_idx 0x%03x", bt_mesh.p_sub[0]->net_idx); + + bt_mesh_comp_provision(bt_mesh_provisioner_get_primary_elem_addr()); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + sub = bt_mesh.p_sub[i]; + + if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + err = subnet_init(sub); + if (err) { + BT_ERR("%s, Failed to init subnet 0x%03x", __func__, sub->net_idx); + } + sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; + } + } +#endif /* CONFIG_BLE_MESH_PROVISIONER */ if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) { k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT); @@ -879,29 +1384,39 @@ int settings_core_commit(void) bt_mesh_model_foreach(commit_model, NULL); - hb_pub = bt_mesh_hb_pub_get(); - if (hb_pub && hb_pub->dst != BLE_MESH_ADDR_UNASSIGNED && - hb_pub->count && hb_pub->period) { - BT_DBG("Starting heartbeat publication"); - k_work_submit(&hb_pub->timer.work); - } - - cfg = bt_mesh_cfg_get(); - if (cfg && stored_cfg.valid) { - cfg->net_transmit = stored_cfg.cfg.net_transmit; - cfg->relay = stored_cfg.cfg.relay; - cfg->relay_retransmit = stored_cfg.cfg.relay_retransmit; - cfg->beacon = stored_cfg.cfg.beacon; - cfg->gatt_proxy = stored_cfg.cfg.gatt_proxy; - cfg->frnd = stored_cfg.cfg.frnd; - cfg->default_ttl = stored_cfg.cfg.default_ttl; - } - - bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID); - #if defined(CONFIG_BLE_MESH_NODE) - bt_mesh_net_start(); -#endif + if (bt_mesh_is_node()) { + struct bt_mesh_hb_pub *hb_pub = NULL; + struct bt_mesh_cfg_srv *cfg = NULL; + + hb_pub = bt_mesh_hb_pub_get(); + if (hb_pub && hb_pub->dst != BLE_MESH_ADDR_UNASSIGNED && + hb_pub->count && hb_pub->period) { + BT_DBG("Starting heartbeat publication"); + k_work_submit(&hb_pub->timer.work); + } + + cfg = bt_mesh_cfg_get(); + if (cfg && stored_cfg.valid) { + cfg->net_transmit = stored_cfg.cfg.net_transmit; + cfg->relay = stored_cfg.cfg.relay; + cfg->relay_retransmit = stored_cfg.cfg.relay_retransmit; + cfg->beacon = stored_cfg.cfg.beacon; + cfg->gatt_proxy = stored_cfg.cfg.gatt_proxy; + cfg->frnd = stored_cfg.cfg.frnd; + cfg->default_ttl = stored_cfg.cfg.default_ttl; + } + + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID); + bt_mesh_net_start(); + } +#endif /* CONFIG_BLE_MESH_NODE */ + +#if defined(CONFIG_BLE_MESH_PROVISIONER) + if (bt_mesh_is_provisioner()) { + bt_mesh_provisioner_net_start(BLE_MESH_PROV_ADV | BLE_MESH_PROV_GATT); + } +#endif /* CONFIG_BLE_MESH_PROVISIONER */ return 0; } @@ -919,7 +1434,7 @@ int settings_core_commit(void) static void schedule_store(int flag) { - s32_t timeout, remaining; + s32_t timeout = 0, remaining = 0; bt_mesh_atomic_set_bit(bt_mesh.flags, flag); @@ -974,6 +1489,13 @@ static void store_pending_net(void) bt_mesh_save_core_settings("mesh/net", (const u8_t *)&net, sizeof(net)); } +void bt_mesh_store_role(void) +{ + BT_DBG("Store, device role %lu", bt_mesh_atomic_get(bt_mesh.flags) & DEVICE_ROLE_BITS); + + bt_mesh_save_core_settings("mesh/role", (const u8_t *)bt_mesh.flags, sizeof(bt_mesh.flags)); +} + void bt_mesh_store_net(void) { schedule_store(BLE_MESH_NET_PENDING); @@ -1000,6 +1522,11 @@ void bt_mesh_store_iv(bool only_duration) } } +void bt_mesh_clear_iv(void) +{ + clear_iv(); +} + static void store_pending_seq(void) { struct seq_val seq = {0}; @@ -1021,11 +1548,16 @@ void bt_mesh_store_seq(void) schedule_store(BLE_MESH_SEQ_PENDING); } +void bt_mesh_clear_seq(void) +{ + bt_mesh_save_core_settings("mesh/seq", NULL, 0); +} + static void store_rpl(struct bt_mesh_rpl *entry) { struct rpl_val rpl = {0}; char name[16] = {'\0'}; - int err; + int err = 0; BT_DBG("src 0x%04x seq 0x%06x old_iv %u", entry->src, entry->seq, entry->old_iv); @@ -1051,8 +1583,8 @@ static void clear_rpl(void) { struct net_buf_simple *buf = NULL; char name[16] = {'\0'}; - size_t length; - u16_t src; + size_t length = 0U; + u16_t src = 0U; int i; BT_DBG("%s", __func__); @@ -1146,7 +1678,7 @@ static void clear_cfg(void) static void clear_app_key(u16_t app_idx) { char name[16] = {'\0'}; - int err; + int err = 0; BT_DBG("AppKeyIndex 0x%03x", app_idx); @@ -1164,7 +1696,7 @@ static void clear_app_key(u16_t app_idx) static void clear_net_key(u16_t net_idx) { char name[16] = {'\0'}; - int err; + int err = 0; BT_DBG("NetKeyIndex 0x%03x", net_idx); @@ -1183,7 +1715,7 @@ static void store_net_key(struct bt_mesh_subnet *sub) { struct net_key_val key = {0}; char name[16] = {'\0'}; - int err; + int err = 0; BT_DBG("NetKeyIndex 0x%03x NetKey %s", sub->net_idx, bt_hex(sub->keys[0].net, 16)); @@ -1212,7 +1744,7 @@ static void store_app_key(struct bt_mesh_app_key *app) { struct app_key_val key = {0}; char name[16] = {'\0'}; - int err; + int err = 0; key.net_idx = app->net_idx; key.updated = app->updated; @@ -1278,14 +1810,15 @@ static void store_pending_keys(void) static void store_pending_mod_bind(struct bt_mesh_model *model, bool vnd) { char name[16] = {'\0'}; - u16_t model_key; - int err; + u16_t model_key = 0U; + int err = 0; model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); sprintf(name, "mesh/%s/%04x/b", vnd ? "v" : "s", model_key); - if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + !bt_mesh_is_provisioned()) { bt_mesh_save_core_settings(name, NULL, 0); return; } @@ -1308,14 +1841,15 @@ static void store_pending_mod_bind(struct bt_mesh_model *model, bool vnd) static void store_pending_mod_sub(struct bt_mesh_model *model, bool vnd) { char name[16] = {'\0'}; - u16_t model_key; - int err; + u16_t model_key = 0U; + int err = 0; model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); sprintf(name, "mesh/%s/%04x/s", vnd ? "v" : "s", model_key); - if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + !bt_mesh_is_provisioned()) { bt_mesh_save_core_settings(name, NULL, 0); return; } @@ -1339,8 +1873,8 @@ static void store_pending_mod_pub(struct bt_mesh_model *model, bool vnd) { struct mod_pub_val pub = {0}; char name[16] = {'\0'}; - u16_t model_key; - int err; + u16_t model_key = 0U; + int err = 0; if (!model->pub) { BT_WARN("%s, No model publication to store", __func__); @@ -1359,7 +1893,8 @@ static void store_pending_mod_pub(struct bt_mesh_model *model, bool vnd) sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key); - if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + !bt_mesh_is_provisioned()) { bt_mesh_save_core_settings(name, NULL, 0); return; } @@ -1408,9 +1943,9 @@ static void store_pending_va(void) { struct va_val va = {0}; char name[16] = {'\0'}; - struct label *lab; - u16_t i; - int err; + struct label *lab = NULL; + u16_t i = 0U; + int err = 0; for (i = 0U; (lab = get_label(i)) != NULL; i++) { if (!bt_mesh_atomic_test_and_clear_bit(lab->flags, @@ -1454,19 +1989,21 @@ static void store_pending(struct k_work *work) BT_DBG("%s", __func__); if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_RPL_PENDING)) { - if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) { + if (!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioned()) { store_pending_rpl(); } else { clear_rpl(); } } - if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_KEYS_PENDING)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_KEYS_PENDING)) { store_pending_keys(); } - if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_NET_PENDING)) { - if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_NET_PENDING)) { + if (bt_mesh_is_provisioned()) { store_pending_net(); } else { clear_net(); @@ -1474,7 +2011,7 @@ static void store_pending(struct k_work *work) } if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_IV_PENDING)) { - if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) { + if (!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioned()) { store_pending_iv(); } else { clear_iv(); @@ -1485,12 +2022,14 @@ static void store_pending(struct k_work *work) store_pending_seq(); } - if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_HB_PUB_PENDING)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_HB_PUB_PENDING)) { store_pending_hb_pub(); } - if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_CFG_PENDING)) { - if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_CFG_PENDING)) { + if (bt_mesh_is_provisioned()) { store_pending_cfg(); } else { clear_cfg(); @@ -1499,7 +2038,8 @@ static void store_pending(struct k_work *work) if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_MOD_PENDING)) { bt_mesh_model_foreach(store_pending_mod, NULL); - if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + !bt_mesh_is_provisioned()) { bt_mesh_save_core_settings("mesh/sig", NULL, 0); bt_mesh_save_core_settings("mesh/vnd", NULL, 0); } @@ -1608,6 +2148,12 @@ void bt_mesh_store_cfg(void) schedule_store(BLE_MESH_CFG_PENDING); } +void bt_mesh_clear_role(void) +{ + BT_DBG("Clear device role"); + bt_mesh_save_core_settings("mesh/role", NULL, 0); +} + void bt_mesh_clear_net(void) { schedule_store(BLE_MESH_NET_PENDING); @@ -1697,6 +2243,320 @@ void bt_mesh_store_label(void) schedule_store(BLE_MESH_VA_PENDING); } +#if CONFIG_BLE_MESH_PROVISIONER +/** + * key: "mesh/p_prov" -> write/read to set/get prov_ctx.curr_addr + * key: "mesh/p_netidx" -> write/read to set/get bt_mesh.p_net_idx_next + * key: "mesh/p_appidx" -> write/read to set/get bt_mesh.p_app_idx_next + * key: "mesh/p_netkey" -> write/read to set/get all Provisioner NetKey Index + * key: "mesh/pnk/xxxx" -> write/read to set/get the "xxxx" NetKey + * key: "mesh/p_appkey" -> write/read to set/get all Provisioner AppKey Index + * key: "mesh/pak/xxxx" -> write/read to set/get the "xxxx" AppKey + * key: "mesh/p_pnode" -> write/read to set/get all self-provisioned nodes info + * key: "mesh/pn/xxxx/i" -> write/read to set/get the "xxxx" provisioned node info + * key: "mesh/pn/xxxx/n" -> write/read to set/get the "xxxx" provisioned node name + * key: "mesh/pn/xxxx/c" -> write/read to set/get the "xxxx" provisioned node composition data + * key: "mesh/p_snode" -> write/read to set/get all locally stored nodes info + * key: "mesh/sn/xxxx/i" -> write/read to set/get the "xxxx" stored node info + * key: "mesh/sn/xxxx/n" -> write/read to set/get the "xxxx" stored node name + * key: "mesh/sn/xxxx/c" -> write/read to set/get the "xxxx" stored node composition data + */ +void bt_mesh_store_prov_info(u16_t primary_addr, u16_t alloc_addr) +{ + struct prov_info val = {0}; + + BT_DBG("primary_addr 0x%04x, alloc_addr 0x%04x", primary_addr, alloc_addr); + + val.primary_addr = primary_addr; + val.alloc_addr = alloc_addr; + + bt_mesh_save_core_settings("mesh/p_prov", (const u8_t *)&val, sizeof(val)); +} + +void bt_mesh_clear_prov_info(void) +{ + bt_mesh_save_core_settings("mesh/p_prov", NULL, 0); +} + +static void clear_p_net_key(u16_t net_idx) +{ + char name[16] = {'\0'}; + int err = 0; + + sprintf(name, "mesh/pnk/%04x", net_idx); + bt_mesh_save_core_settings(name, NULL, 0); + + err = bt_mesh_remove_core_settings_item("mesh/p_netkey", net_idx); + if (err) { + BT_ERR("%s, Failed to remove 0x%04x from mesh/p_netkey", __func__, net_idx); + } +} + +static void clear_p_app_key(u16_t app_idx) +{ + char name[16] = {'\0'}; + int err = 0; + + sprintf(name, "mesh/pak/%04x", app_idx); + bt_mesh_save_core_settings(name, NULL, 0); + + err = bt_mesh_remove_core_settings_item("mesh/p_appkey", app_idx); + if (err) { + BT_ERR("%s, Failed to remove 0x%03x from mesh/p_appkey", __func__, app_idx); + } +} + +static void store_p_net_key(struct bt_mesh_subnet *sub) +{ + struct net_key_val key = {0}; + char name[16] = {'\0'}; + int err = 0; + + memcpy(&key.val[0], sub->keys[0].net, 16); + memcpy(&key.val[1], sub->keys[1].net, 16); + key.kr_flag = sub->kr_flag; + key.kr_phase = sub->kr_phase; + + sprintf(name, "mesh/pnk/%04x", sub->net_idx); + err = bt_mesh_save_core_settings(name, (const u8_t *)&key, sizeof(key)); + if (err) { + BT_ERR("%s, Failed to save NetKey %s", __func__, name); + return; + } + + err = bt_mesh_add_core_settings_item("mesh/p_netkey", sub->net_idx); + if (err) { + BT_ERR("%s, Failed to add 0x%04x to mesh/p_netkey", __func__, sub->net_idx); + } +} + +static void store_p_app_key(struct bt_mesh_app_key *app) +{ + struct app_key_val key = {0}; + char name[16] = {'\0'}; + int err = 0; + + key.net_idx = app->net_idx; + key.updated = app->updated; + memcpy(key.val[0], app->keys[0].val, 16); + memcpy(key.val[1], app->keys[1].val, 16); + + sprintf(name, "mesh/pak/%04x", app->app_idx); + err = bt_mesh_save_core_settings(name, (const u8_t *)&key, sizeof(key)); + if (err) { + BT_ERR("%s, Failed to save AppKey %s", __func__, name); + return; + } + + err = bt_mesh_add_core_settings_item("mesh/p_appkey", app->app_idx); + if (err) { + BT_ERR("%s, Failed to add 0x%04x to mesh/p_appkey", __func__, app->app_idx); + } +} + +void bt_mesh_store_p_net_idx(void) +{ + BT_DBG("p_net_idx_next 0x%03x", bt_mesh.p_net_idx_next); + + bt_mesh_save_core_settings("mesh/p_netidx", + (const u8_t *)&bt_mesh.p_net_idx_next, sizeof(bt_mesh.p_net_idx_next)); +} + +void bt_mesh_clear_p_net_idx(void) +{ + bt_mesh_save_core_settings("mesh/p_netidx", NULL, 0); +} + +void bt_mesh_store_p_app_idx(void) +{ + BT_DBG("p_app_idx_next 0x%03x", bt_mesh.p_app_idx_next); + + bt_mesh_save_core_settings("mesh/p_appidx", + (const u8_t *)&bt_mesh.p_app_idx_next, sizeof(bt_mesh.p_app_idx_next)); +} + +void bt_mesh_clear_p_app_idx(void) +{ + bt_mesh_save_core_settings("mesh/p_appidx", NULL, 0); +} + +void bt_mesh_store_p_subnet(struct bt_mesh_subnet *sub) +{ + if (sub == NULL) { + BT_ERR("%s, Invalid subnet",__func__); + return; + } + + BT_DBG("NetKeyIndex 0x%03x NetKey %s", sub->net_idx, + bt_hex(sub->keys[0].net, 16)); + + store_p_net_key(sub); +} + +void bt_mesh_store_p_app_key(struct bt_mesh_app_key *key) +{ + if (key == NULL) { + BT_ERR("%s, Invalid AppKey",__func__); + return; + } + + BT_DBG("AppKeyIndex 0x%03x AppKey %s", key->app_idx, + bt_hex(key->keys[0].val, 16)); + + store_p_app_key(key); +} + +void bt_mesh_clear_p_subnet(struct bt_mesh_subnet *sub) +{ + if (sub == NULL) { + BT_ERR("%s, Invalid subnet",__func__); + return; + } + + BT_DBG("NetKeyIndex 0x%03x", sub->net_idx); + + clear_p_net_key(sub->net_idx); +} + +void bt_mesh_clear_p_app_key(struct bt_mesh_app_key *key) +{ + if (key == NULL) { + BT_ERR("%s, Invalid AppKey",__func__); + return; + } + + BT_DBG("AppKeyIndex 0x%03x", key->app_idx); + + clear_p_app_key(key->app_idx); +} + +void bt_mesh_clear_rpl_single(u16_t src) +{ + char name[16] = {'\0'}; + int err = 0; + + if (!BLE_MESH_ADDR_IS_UNICAST(src)) { + BT_ERR("%s, Invalid source address 0x%04x", __func__, src); + return; + } + + sprintf(name, "mesh/rpl/%04x", src); + bt_mesh_save_core_settings(name, NULL, 0); + + err = bt_mesh_remove_core_settings_item("mesh/rpl", src); + if (err) { + BT_ERR("%s, Failed to remove 0x%04x from mesh/rpl", __func__, src); + } +} + +void bt_mesh_store_node_info(struct bt_mesh_node *node, bool prov) +{ + struct node_info val = {0}; + char name[16] = {'\0'}; + int err = 0; + + if (node == NULL) { + BT_ERR("%s, Invalid node", __func__); + return; + } + + memcpy(val.addr, node->addr, BLE_MESH_ADDR_LEN); + val.addr_type = node->addr_type; + memcpy(val.dev_uuid, node->dev_uuid, 16); + val.oob_info = node->oob_info; + val.unicast_addr = node->unicast_addr; + val.element_num = node->element_num; + val.net_idx = node->net_idx; + val.flags = node->flags; + val.iv_index = node->iv_index; + memcpy(val.dev_key, node->dev_key, 16); + + sprintf(name, prov ? "mesh/pn/%04x/i" : "mesh/sn/%04x/i", node->unicast_addr); + err = bt_mesh_save_core_settings(name, (const u8_t *)&val, sizeof(val)); + if (err) { + BT_ERR("%s, Failed to save node %s", __func__, name); + return; + } + + err = bt_mesh_add_core_settings_item(prov ? "mesh/p_pnode" : "mesh/p_snode", node->unicast_addr); + if (err) { + BT_ERR("%s, Failed to add node 0x%04x", __func__, node->unicast_addr); + } +} + +static void clear_node(u16_t addr, bool prov) +{ + char name[16] = {'\0'}; + int err = 0; + + /* Clear node information */ + sprintf(name, prov ? "mesh/pn/%04x/i" : "mesh/sn/%04x/i", addr); + bt_mesh_save_core_settings(name, NULL, 0); + + /* Clear node name */ + sprintf(name, prov ? "mesh/pn/%04x/n" : "mesh/sn/%04x/n", addr); + bt_mesh_save_core_settings(name, NULL, 0); + + /* Clear node composition data */ + sprintf(name, prov ? "mesh/pn/%04x/c" : "mesh/sn/%04x/c", addr); + bt_mesh_save_core_settings(name, NULL, 0); + + err = bt_mesh_remove_core_settings_item(prov ? "mesh/p_pnode" : "mesh/p_snode", addr); + if (err) { + BT_ERR("%s, Failed to remove node 0x%04x", __func__, addr); + } +} + +void bt_mesh_clear_node_info(u16_t unicast_addr, bool prov) +{ + if (!BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) { + BT_ERR("%s, Invalid unicast address 0x%04x", __func__, unicast_addr); + return; + } + + BT_DBG("Unicast address 0x%04x", unicast_addr); + + clear_node(unicast_addr, prov); +} + +void bt_mesh_store_node_name(struct bt_mesh_node *node, bool prov) +{ + char node_name[BLE_MESH_NODE_NAME_SIZE] = {0}; + char name[16] = {'\0'}; + int err = 0; + + if (node == NULL) { + BT_ERR("%s, Invalid node", __func__); + return; + } + + strncpy(node_name, node->name, BLE_MESH_NODE_NAME_SIZE); + + sprintf(name, prov ? "mesh/pn/%04x/n" : "mesh/sn/%04x/n", node->unicast_addr); + err = bt_mesh_save_core_settings(name, (const u8_t *)node_name, BLE_MESH_NODE_NAME_SIZE); + if (err) { + BT_ERR("%s, Failed to save node name %s", __func__, name); + } +} + +void bt_mesh_store_node_comp_data(struct bt_mesh_node *node, bool prov) +{ + char name[16] = {'\0'}; + int err = 0; + + if (!node || !node->comp_data || node->comp_length == 0U) { + BT_ERR("%s, Invalid node info", __func__); + return; + } + + sprintf(name, prov ? "mesh/pn/%04x/c" : "mesh/sn/%04x/c", node->unicast_addr); + err = bt_mesh_save_core_settings(name, (const u8_t *)node->comp_data, node->comp_length); + if (err) { + BT_ERR("%s, Failed to save node comp data %s", __func__, name); + } +} +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + int settings_core_init(void) { BT_DBG("%s", __func__); @@ -1715,4 +2575,18 @@ int bt_mesh_settings_init(void) return 0; } +int settings_core_deinit(void) +{ + k_delayed_work_free(&pending_store); + + return 0; +} + +int bt_mesh_settings_deinit(void) +{ + bt_mesh_settings_deforeach(); + + return 0; +} + #endif /* CONFIG_BLE_MESH_SETTINGS */ diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.h b/components/bt/esp_ble_mesh/mesh_core/settings.h index 3b1c4c696..a02db9e55 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.h +++ b/components/bt/esp_ble_mesh/mesh_core/settings.h @@ -7,19 +7,20 @@ #ifndef _SETTINGS_H_ #define _SETTINGS_H_ -#include "sdkconfig.h" - #include "net.h" -#include "mesh_access.h" -#include "mesh_bearer_adapt.h" +#include "provisioner_main.h" int settings_core_init(void); int settings_core_load(void); int settings_core_commit(void); +int settings_core_deinit(void); +void bt_mesh_store_role(void); void bt_mesh_store_net(void); void bt_mesh_store_iv(bool only_duration); +void bt_mesh_clear_iv(void); void bt_mesh_store_seq(void); +void bt_mesh_clear_seq(void); void bt_mesh_store_rpl(struct bt_mesh_rpl *rpl); void bt_mesh_store_subnet(struct bt_mesh_subnet *sub); void bt_mesh_store_app_key(struct bt_mesh_app_key *key); @@ -30,11 +31,31 @@ void bt_mesh_store_mod_sub(struct bt_mesh_model *mod); void bt_mesh_store_mod_pub(struct bt_mesh_model *mod); void bt_mesh_store_label(void); +void bt_mesh_clear_role(void); void bt_mesh_clear_net(void); void bt_mesh_clear_subnet(struct bt_mesh_subnet *sub); void bt_mesh_clear_app_key(struct bt_mesh_app_key *key); void bt_mesh_clear_rpl(void); +#if CONFIG_BLE_MESH_PROVISIONER +void bt_mesh_store_prov_info(u16_t primary_addr, u16_t alloc_addr); +void bt_mesh_clear_prov_info(void); +void bt_mesh_store_p_net_idx(void); +void bt_mesh_clear_p_net_idx(void); +void bt_mesh_store_p_app_idx(void); +void bt_mesh_clear_p_app_idx(void); +void bt_mesh_store_p_subnet(struct bt_mesh_subnet *sub); +void bt_mesh_store_p_app_key(struct bt_mesh_app_key *key); +void bt_mesh_clear_p_subnet(struct bt_mesh_subnet *sub); +void bt_mesh_clear_p_app_key(struct bt_mesh_app_key *key); +void bt_mesh_clear_rpl_single(u16_t src); +void bt_mesh_store_node_info(struct bt_mesh_node *node, bool prov); +void bt_mesh_clear_node_info(u16_t unicast_addr, bool prov); +void bt_mesh_store_node_name(struct bt_mesh_node *node, bool prov); +void bt_mesh_store_node_comp_data(struct bt_mesh_node *node, bool prov); +#endif + int bt_mesh_settings_init(void); +int bt_mesh_settings_deinit(void); #endif /* _SETTINGS_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c index 96ef197ad..400eac9ce 100644 --- a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c +++ b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c @@ -13,17 +13,12 @@ // limitations under the License. #include -#include -#include #include #include "nvs.h" -#include "sdkconfig.h" +#include "nvs_flash.h" -#include "mesh_util.h" -#include "mesh_types.h" #include "mesh_common.h" - #include "settings_nvs.h" #include "settings.h" @@ -41,6 +36,8 @@ struct settings_context { int (*settings_init)(void); int (*settings_load)(void); int (*settings_commit)(void); + int (*settings_deinit)(void); + int (*settings_erase)(void); }; static struct settings_context settings_ctx[] = { @@ -49,6 +46,7 @@ static struct settings_context settings_ctx[] = { .settings_init = settings_core_init, .settings_load = settings_core_load, .settings_commit = settings_core_commit, + .settings_deinit = settings_core_deinit, }, [SETTINGS_SERVER] = { .nvs_name = "mesh_server", @@ -62,12 +60,25 @@ static struct settings_context settings_ctx[] = { void bt_mesh_settings_foreach(void) { - int i, err; + int err = 0; + int i; + +#if CONFIG_BLE_MESH_SPECIFIC_PARTITION + err = nvs_flash_init_partition(CONFIG_BLE_MESH_PARTITION_NAME); + if (err != ESP_OK) { + BT_ERR("Failed to init mesh partition, name %s, err %d", CONFIG_BLE_MESH_PARTITION_NAME, err); + return; + } +#endif for (i = 0; i < ARRAY_SIZE(settings_ctx); i++) { struct settings_context *ctx = &settings_ctx[i]; +#if CONFIG_BLE_MESH_SPECIFIC_PARTITION + err = nvs_open_from_partition(CONFIG_BLE_MESH_PARTITION_NAME, ctx->nvs_name, NVS_READWRITE, &ctx->handle); +#else err = nvs_open(ctx->nvs_name, NVS_READWRITE, &ctx->handle); +#endif if (err != ESP_OK) { BT_ERR("%s, Open nvs failed, name %s, err %d", __func__, ctx->nvs_name, err); continue; @@ -90,6 +101,26 @@ void bt_mesh_settings_foreach(void) } } +void bt_mesh_settings_deforeach(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(settings_ctx); i++) { + struct settings_context *ctx = &settings_ctx[i]; + + if (ctx->settings_deinit && ctx->settings_deinit()) { + BT_ERR("%s, Deinit settings failed, name %s", __func__, ctx->nvs_name); + continue; + } + + nvs_close(ctx->handle); + } + +#if CONFIG_BLE_MESH_SPECIFIC_PARTITION + nvs_flash_deinit_partition(CONFIG_BLE_MESH_PARTITION_NAME); +#endif +} + /* API used to get BLE Mesh related nvs handle */ static inline nvs_handle settings_get_nvs_handle(enum settings_type type) @@ -101,7 +132,7 @@ static inline nvs_handle settings_get_nvs_handle(enum settings_type type) static int settings_save(nvs_handle handle, const char *key, const u8_t *val, size_t len) { - int err; + int err = 0; if (key == NULL) { BT_ERR("%s, Invalid parameter", __func__); @@ -145,7 +176,7 @@ int bt_mesh_save_core_settings(const char *key, const u8_t *val, size_t len) static int settings_load(nvs_handle handle, const char *key, u8_t *buf, size_t buf_len, bool *exist) { - int err; + int err = 0; if (key == NULL || buf == NULL || exist == NULL) { BT_ERR("%s, Invalid parameter", __func__); @@ -178,8 +209,8 @@ int bt_mesh_load_core_settings(const char *key, u8_t *buf, size_t buf_len, bool static size_t settings_get_length(nvs_handle handle, const char *key) { - size_t len = 0; - int err; + size_t len = 0U; + int err = 0; if (key == NULL) { BT_ERR("%s, Invalid parameter", __func__); @@ -205,9 +236,9 @@ static size_t settings_get_length(nvs_handle handle, const char *key) static struct net_buf_simple *settings_get_item(nvs_handle handle, const char *key) { struct net_buf_simple *buf = NULL; - size_t length; - bool exist; - int err; + size_t length = 0U; + bool exist = false; + int err = 0; length = settings_get_length(handle, key); if (!length) { @@ -250,7 +281,7 @@ struct net_buf_simple *bt_mesh_get_core_settings_item(const char *key) static bool is_settings_item_exist(struct net_buf_simple *buf, const u16_t val) { struct net_buf_simple_state state = {0}; - size_t length; + size_t length = 0U; int i; if (!buf) { @@ -278,8 +309,8 @@ static int settings_add_item(nvs_handle handle, const char *key, const u16_t val { struct net_buf_simple *store = NULL; struct net_buf_simple *buf = NULL; - size_t length = 0; - int err; + size_t length = 0U; + int err = 0; buf = settings_get_item(handle, key); @@ -323,9 +354,10 @@ static int settings_remove_item(nvs_handle handle, const char *key, const u16_t { struct net_buf_simple *store = NULL; struct net_buf_simple *buf = NULL; - size_t length = 0; - size_t buf_len; - int i, err; + size_t length = 0U; + size_t buf_len = 0U; + int err = 0; + int i; buf = settings_get_item(handle, key); diff --git a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h index c4d5ad064..099efa54d 100644 --- a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h +++ b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h @@ -15,8 +15,6 @@ #ifndef _BLE_MESH_SETTINGS_NVS_H_ #define _BLE_MESH_SETTINGS_NVS_H_ -#include -#include "mesh_types.h" #include "mesh_buf.h" #ifdef __cplusplus @@ -30,6 +28,7 @@ extern "C" { #define BLE_MESH_GET_MODEL_KEY(a, b) ((u16_t)(((u16_t)((a) << 8)) | b)) void bt_mesh_settings_foreach(void); +void bt_mesh_settings_deforeach(void); int bt_mesh_save_core_settings(const char *key, const u8_t *val, size_t len); diff --git a/components/bt/esp_ble_mesh/mesh_core/test.c b/components/bt/esp_ble_mesh/mesh_core/test.c index f61beb872..384336ebf 100644 --- a/components/bt/esp_ble_mesh/mesh_core/test.c +++ b/components/bt/esp_ble_mesh/mesh_core/test.c @@ -10,16 +10,12 @@ #include #include -#include "mesh_trace.h" -#include "mesh_main.h" -#include "mesh_access.h" - #include "mesh.h" #include "test.h" #include "crypto.h" -#include "net.h" -#include "foundation.h" #include "access.h" +#include "foundation.h" +#include "mesh_main.h" #if defined(CONFIG_BLE_MESH_SELF_TEST) @@ -38,7 +34,7 @@ int bt_mesh_device_auto_enter_network(struct bt_mesh_device_network_info *info) struct bt_mesh_app_key *key = NULL; struct bt_mesh_subnet *sub = NULL; int i, j, k; - int err; + int err = 0; if (info == NULL || !BLE_MESH_ADDR_IS_UNICAST(info->unicast_addr) || !BLE_MESH_ADDR_IS_GROUP(info->group_addr)) { diff --git a/components/bt/esp_ble_mesh/mesh_core/test.h b/components/bt/esp_ble_mesh/mesh_core/test.h index 60e3d0d8f..477ca105f 100644 --- a/components/bt/esp_ble_mesh/mesh_core/test.h +++ b/components/bt/esp_ble_mesh/mesh_core/test.h @@ -10,12 +10,7 @@ #ifndef _BLE_MESH_TEST_H_ #define _BLE_MESH_TEST_H_ -#include -#include -#include - -#include "mesh_buf.h" -#include "sdkconfig.h" +#include "mesh_types.h" #if defined(CONFIG_BLE_MESH_SELF_TEST) int bt_mesh_test(void); diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index 5b0a9a968..3d78e4365 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -7,31 +7,22 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include #include +#include -#include "sdkconfig.h" #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_TRANS) -#include "mesh_types.h" -#include "mesh_util.h" -#include "mesh_buf.h" -#include "mesh_trace.h" -#include "mesh_main.h" -#include "settings.h" - #include "crypto.h" #include "adv.h" #include "mesh.h" -#include "net.h" #include "lpn.h" #include "friend.h" #include "access.h" #include "foundation.h" #include "settings.h" #include "transport.h" +#include "mesh_main.h" #include "mesh_common.h" -#include "client_common.h" #include "cfg_srv.h" /* The transport layer needs at least three buffers for itself to avoid @@ -110,6 +101,30 @@ static u8_t seg_rx_buf_data[(CONFIG_BLE_MESH_RX_SEG_MSG_COUNT * static u16_t hb_sub_dst = BLE_MESH_ADDR_UNASSIGNED; +static bt_mesh_mutex_t tx_seg_lock; + +static void bt_mesh_tx_seg_mutex_new(void) +{ + if (!tx_seg_lock.mutex) { + bt_mesh_mutex_create(&tx_seg_lock); + } +} + +static void bt_mesh_tx_seg_mutex_free(void) +{ + bt_mesh_mutex_free(&tx_seg_lock); +} + +static void bt_mesh_tx_seg_lock(void) +{ + bt_mesh_mutex_lock(&tx_seg_lock); +} + +static void bt_mesh_tx_seg_unlock(void) +{ + bt_mesh_mutex_unlock(&tx_seg_lock); +} + void bt_mesh_set_hb_sub_dst(u16_t addr) { hb_sub_dst = addr; @@ -118,7 +133,7 @@ void bt_mesh_set_hb_sub_dst(u16_t addr) static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, const struct bt_mesh_send_cb *cb, void *cb_data) { - struct net_buf *buf; + struct net_buf *buf = NULL; BT_DBG("src 0x%04x dst 0x%04x app_idx 0x%04x sdu_len %u", tx->src, tx->ctx->addr, tx->ctx->app_idx, sdu->len); @@ -198,6 +213,8 @@ static void seg_tx_reset(struct seg_tx *tx) return; } + bt_mesh_tx_seg_lock(); + for (i = 0; i <= tx->seg_n; i++) { if (!tx->seg[i]) { continue; @@ -211,6 +228,8 @@ static void seg_tx_reset(struct seg_tx *tx) tx->nack_count = 0U; + bt_mesh_tx_seg_unlock(); + if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_IVU_PENDING)) { BT_DBG("Proceding with pending IV Update"); /* bt_mesh_net_iv_update() will re-enable the flag if this @@ -274,7 +293,9 @@ static const struct bt_mesh_send_cb seg_sent_cb = { static void seg_tx_send_unacked(struct seg_tx *tx) { - int i, err; + int i, err = 0; + + bt_mesh_tx_seg_lock(); for (i = 0; i <= tx->seg_n; i++) { struct net_buf *seg = tx->seg[i]; @@ -290,6 +311,7 @@ static void seg_tx_send_unacked(struct seg_tx *tx) if (!(BLE_MESH_ADV(seg)->seg.attempts--)) { BT_WARN("Ran out of retransmit attempts"); + bt_mesh_tx_seg_unlock(); seg_tx_complete(tx, -ETIMEDOUT); return; } @@ -300,10 +322,13 @@ static void seg_tx_send_unacked(struct seg_tx *tx) &seg_sent_cb, tx); if (err) { BT_ERR("%s, Sending segment failed", __func__); + bt_mesh_tx_seg_unlock(); seg_tx_complete(tx, -EIO); return; } } + + bt_mesh_tx_seg_unlock(); } static void seg_retransmit(struct k_work *work) @@ -316,9 +341,9 @@ static void seg_retransmit(struct k_work *work) static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, const struct bt_mesh_send_cb *cb, void *cb_data) { - u8_t seg_hdr, seg_o; - u16_t seq_zero; - struct seg_tx *tx; + u8_t seg_hdr = 0U, seg_o = 0U; + u16_t seq_zero = 0U; + struct seg_tx *tx = NULL; int i; BT_DBG("src 0x%04x dst 0x%04x app_idx 0x%04x aszmic %u sdu_len %u", @@ -385,9 +410,9 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, } for (seg_o = 0U; sdu->len; seg_o++) { - struct net_buf *seg; - u16_t len; - int err; + struct net_buf *seg = NULL; + u16_t len = 0U; + int err = 0; seg = bt_mesh_adv_create(BLE_MESH_ADV_DATA, net_tx->xmit, BUF_TIMEOUT); @@ -412,7 +437,7 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, net_buf_simple_pull(sdu, len); if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { - enum bt_mesh_friend_pdu_type type; + enum bt_mesh_friend_pdu_type type = BLE_MESH_FRIEND_PDU_PARTIAL; if (seg_o == tx->seg_n) { type = BLE_MESH_FRIEND_PDU_COMPLETE; @@ -485,9 +510,9 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, const struct bt_mesh_send_cb *cb, void *cb_data) { const u8_t *key = NULL; - u8_t *ad, role; - u8_t aid; - int err; + u8_t *ad = NULL, role = 0U; + u8_t aid = 0U; + int err = 0; if (net_buf_simple_tailroom(msg) < 4) { BT_ERR("%s, Insufficient tailroom for Transport MIC", __func__); @@ -627,10 +652,10 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, u32_t seq, u8_t hdr, u8_t aszmic, struct net_buf_simple *buf) { struct net_buf_simple *sdu = NULL; - size_t array_size = 0; - u8_t *ad; - u16_t i; - int err; + size_t array_size = 0U; + size_t i = 0U; + u8_t *ad = NULL; + int err = 0; BT_DBG("ASZMIC %u AKF %u AID 0x%02x", aszmic, AKF(&hdr), AID(&hdr)); BT_DBG("len %u: %s", buf->len, bt_hex(buf->data, buf->len)); @@ -667,7 +692,7 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, u32_t seq, u8_t hdr, if (!AKF(&hdr)) { array_size = bt_mesh_rx_devkey_size(); - for (i = 0; i < array_size; i++) { + for (i = 0U; i < array_size; i++) { const u8_t *dev_key = NULL; dev_key = bt_mesh_rx_devkey_get(i, rx->ctx.addr); @@ -699,7 +724,7 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, u32_t seq, u8_t hdr, array_size = bt_mesh_rx_appkey_size(); - for (i = 0; i < array_size; i++) { + for (i = 0U; i < array_size; i++) { struct bt_mesh_app_keys *keys = NULL; struct bt_mesh_app_key *key = NULL; @@ -750,7 +775,7 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, u32_t seq, u8_t hdr, static struct seg_tx *seg_tx_lookup(u16_t seq_zero, u8_t obo, u16_t addr) { - struct seg_tx *tx; + struct seg_tx *tx = NULL; int i; for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { @@ -781,11 +806,11 @@ static struct seg_tx *seg_tx_lookup(u16_t seq_zero, u8_t obo, u16_t addr) static int trans_ack(struct bt_mesh_net_rx *rx, u8_t hdr, struct net_buf_simple *buf, u64_t *seq_auth) { - struct seg_tx *tx; - unsigned int bit; - u32_t ack; - u16_t seq_zero; - u8_t obo; + struct seg_tx *tx = NULL; + unsigned int bit = 0; + u32_t ack = 0U; + u16_t seq_zero = 0U; + u8_t obo = 0U; if (buf->len < 6) { BT_ERR("%s, Too short ack message", __func__); @@ -852,8 +877,8 @@ static int trans_ack(struct bt_mesh_net_rx *rx, u8_t hdr, static int trans_heartbeat(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) { - u8_t init_ttl, hops; - u16_t feat; + u8_t init_ttl = 0U, hops = 0U; + u16_t feat = 0U; if (buf->len < 3) { BT_ERR("%s, Too short heartbeat message", __func__); @@ -870,7 +895,7 @@ static int trans_heartbeat(struct bt_mesh_net_rx *rx, hops = (init_ttl - rx->ctx.recv_ttl + 1); - BT_DBG("src 0x%04x TTL %u InitTTL %u (%u hop%s) feat 0x%04x", + BT_INFO("src 0x%04x TTL %u InitTTL %u (%u hop%s) feat 0x%04x", rx->ctx.addr, rx->ctx.recv_ttl, init_ttl, hops, (hops == 1U) ? "" : "s", feat); @@ -947,7 +972,7 @@ static int ctl_recv(struct bt_mesh_net_rx *rx, u8_t hdr, static int trans_unseg(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx, u64_t *seq_auth) { - u8_t hdr; + u8_t hdr = 0U; BT_DBG("AFK %u AID 0x%02x", AKF(buf->data), AID(buf->data)); @@ -978,8 +1003,8 @@ static int trans_unseg(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx, static inline s32_t ack_timeout(struct seg_rx *rx) { - s32_t to; - u8_t ttl; + s32_t to = 0; + u8_t ttl = 0U; if (rx->ttl == BLE_MESH_TTL_DEFAULT) { ttl = bt_mesh_default_ttl_get(); @@ -1005,7 +1030,7 @@ int bt_mesh_ctl_send(struct bt_mesh_net_tx *tx, u8_t ctl_op, void *data, size_t data_len, u64_t *seq_auth, const struct bt_mesh_send_cb *cb, void *cb_data) { - struct net_buf *buf; + struct net_buf *buf = NULL; BT_DBG("src 0x%04x dst 0x%04x ttl 0x%02x ctl 0x%02x", tx->src, tx->ctx->addr, tx->ctx->send_ttl, ctl_op); @@ -1054,7 +1079,7 @@ static int send_ack(struct bt_mesh_subnet *sub, u16_t src, u16_t dst, .xmit = bt_mesh_net_transmit_get(), }; u16_t seq_zero = *seq_auth & TRANS_SEQ_ZERO_MASK; - u8_t buf[6]; + u8_t buf[6] = {0}; BT_DBG("SeqZero 0x%04x Block 0x%08x OBO %u", seq_zero, block, obo); @@ -1237,12 +1262,12 @@ static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, u8_t *seg_count) { struct bt_mesh_rpl *rpl = NULL; - struct seg_rx *rx; + struct seg_rx *rx = NULL; u8_t *hdr = buf->data; - u16_t seq_zero; - u8_t seg_n; - u8_t seg_o; - int err; + u16_t seq_zero = 0U; + u8_t seg_n = 0U; + u8_t seg_o = 0U; + int err = 0; if (buf->len < 5) { BT_ERR("%s, Too short segmented message (len %u)", __func__, buf->len); @@ -1412,7 +1437,7 @@ found_rx: /* Location in buffer can be calculated based on seg_o & rx->ctl */ memcpy(rx->buf.data + (seg_o * seg_len(rx->ctl)), buf->data, buf->len); - BT_DBG("Received %u/%u", seg_o, seg_n); + BT_INFO("Received %u/%u", seg_o, seg_n); /* Mark segment as received */ rx->block |= BIT(seg_o); @@ -1450,9 +1475,9 @@ int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx) { u64_t seq_auth = TRANS_SEQ_AUTH_NVAL; enum bt_mesh_friend_pdu_type pdu_type = BLE_MESH_FRIEND_PDU_SINGLE; - struct net_buf_simple_state state; + struct net_buf_simple_state state = {0}; u8_t seg_count = 0U; - int err; + int err = 0; if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { rx->friend_match = bt_mesh_friend_match(rx->sub->net_idx, @@ -1573,6 +1598,35 @@ void bt_mesh_trans_init(void) (i * CONFIG_BLE_MESH_RX_SDU_MAX)); seg_rx[i].buf.data = seg_rx[i].buf.__buf; } + + bt_mesh_tx_seg_mutex_new(); +} + +void bt_mesh_trans_deinit(bool erase) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { + seg_rx_reset(&seg_rx[i], true); + } + + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_rpl(); + } else { + bt_mesh_rpl_clear(); + } + + bt_mesh_tx_reset(); + + for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { + k_delayed_work_free(&seg_tx[i].retransmit); + } + + for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { + k_delayed_work_free(&seg_rx[i].ack); + } + + bt_mesh_tx_seg_mutex_free(); } void bt_mesh_rpl_clear(void) @@ -1627,7 +1681,7 @@ void bt_mesh_heartbeat_send(void) hb.feat = sys_cpu_to_be16(feat); - BT_DBG("InitTTL %u feat 0x%04x", cfg->hb_pub.ttl, feat); + BT_INFO("InitTTL %u feat 0x%04x", cfg->hb_pub.ttl, feat); bt_mesh_ctl_send(&tx, TRANS_CTL_OP_HEARTBEAT, &hb, sizeof(hb), NULL, NULL, NULL); @@ -1636,7 +1690,7 @@ void bt_mesh_heartbeat_send(void) int bt_mesh_app_key_get(const struct bt_mesh_subnet *subnet, u16_t app_idx, const u8_t **key, u8_t *aid, u8_t role, u16_t dst) { - struct bt_mesh_app_key *app_key; + struct bt_mesh_app_key *app_key = NULL; if (app_idx == BLE_MESH_KEY_DEV) { *key = bt_mesh_tx_devkey_get(role, dst); @@ -1654,7 +1708,7 @@ int bt_mesh_app_key_get(const struct bt_mesh_subnet *subnet, u16_t app_idx, return -EINVAL; } - app_key = bt_mesh_tx_appkey_get(role, app_idx, subnet->net_idx); + app_key = bt_mesh_tx_appkey_get(role, app_idx); if (!app_key) { BT_ERR("%s, Failed to get AppKey", __func__); return -ENOENT; diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.h b/components/bt/esp_ble_mesh/mesh_core/transport.h index 6fc81fdf2..028b62be8 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.h +++ b/components/bt/esp_ble_mesh/mesh_core/transport.h @@ -10,6 +10,8 @@ #ifndef _TRANSPORT_H_ #define _TRANSPORT_H_ +#include "net.h" + #define TRANS_SEQ_AUTH_NVAL 0xffffffffffffffff #define BLE_MESH_TX_SDU_MAX (CONFIG_BLE_MESH_TX_SEG_MAX * 12) @@ -97,6 +99,7 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx); void bt_mesh_trans_init(void); +void bt_mesh_trans_deinit(bool erase); void bt_mesh_rpl_clear(void); diff --git a/components/bt/esp_ble_mesh/mesh_models/client/client_common.c b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c index c309519ae..13b2896cb 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/client_common.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c @@ -15,16 +15,10 @@ #include #include -#include "osi/allocator.h" -#include "osi/mutex.h" - -#include "mesh_access.h" -#include "mesh_buf.h" -#include "mesh_slist.h" -#include "mesh_main.h" - #include "mesh.h" +#include "mesh_main.h" #include "client_common.h" +#include "mesh_common.h" static bt_mesh_client_node_t *bt_mesh_client_pick_node(sys_slist_t *list, u16_t tx_dst) { @@ -174,7 +168,7 @@ int bt_mesh_client_send_msg(struct bt_mesh_model *model, bt_mesh_client_internal_data_t *internal = NULL; bt_mesh_client_user_data_t *cli = NULL; bt_mesh_client_node_t *node = NULL; - int err; + int err = 0; if (!model || !ctx || !msg) { BT_ERR("%s, Invalid parameter", __func__); @@ -196,7 +190,7 @@ int bt_mesh_client_send_msg(struct bt_mesh_model *model, err = -EBUSY; } else { /* Don't forget to free the node in the timeout (timer_handler) function. */ - node = (bt_mesh_client_node_t *)osi_calloc(sizeof(bt_mesh_client_node_t)); + node = (bt_mesh_client_node_t *)bt_mesh_calloc(sizeof(bt_mesh_client_node_t)); if (!node) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; @@ -206,11 +200,11 @@ int bt_mesh_client_send_msg(struct bt_mesh_model *model, node->opcode = opcode; if ((node->op_pending = bt_mesh_client_get_status_op(cli->op_pair, cli->op_pair_size, opcode)) == 0) { BT_ERR("%s, Not found the status opcode in the op_pair list", __func__); - osi_free(node); + bt_mesh_free(node); return -EINVAL; } if ((err = bt_mesh_model_send(model, ctx, msg, cb, cb_data)) != 0) { - osi_free(node); + bt_mesh_free(node); } else { bt_mesh_list_lock(); sys_slist_append(&internal->queue, &node->client_node); @@ -223,26 +217,28 @@ int bt_mesh_client_send_msg(struct bt_mesh_model *model, return err; } -static osi_mutex_t client_model_mutex; +static bt_mesh_mutex_t client_model_lock; static void bt_mesh_client_model_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&client_model_mutex); - init = true; + if (!client_model_lock.mutex) { + bt_mesh_mutex_create(&client_model_lock); } } +static void bt_mesh_client_model_mutex_free(void) +{ + bt_mesh_mutex_free(&client_model_lock); +} + void bt_mesh_client_model_lock(void) { - osi_mutex_lock(&client_model_mutex, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_mutex_lock(&client_model_lock); } void bt_mesh_client_model_unlock(void) { - osi_mutex_unlock(&client_model_mutex); + bt_mesh_mutex_unlock(&client_model_lock); } int bt_mesh_client_init(struct bt_mesh_model *model) @@ -267,7 +263,7 @@ int bt_mesh_client_init(struct bt_mesh_model *model) } if (!cli->internal_data) { - data = osi_calloc(sizeof(bt_mesh_client_internal_data_t)); + data = bt_mesh_calloc(sizeof(bt_mesh_client_internal_data_t)); if (!data) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; @@ -287,6 +283,35 @@ int bt_mesh_client_init(struct bt_mesh_model *model) return 0; } +int bt_mesh_client_deinit(struct bt_mesh_model *model) +{ + bt_mesh_client_user_data_t *client = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_client_user_data_t *)model->user_data; + if (!client) { + BT_ERR("%s, Client user_data is NULL", __func__); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + client->internal_data = NULL; + } + + bt_mesh_client_model_mutex_free(); + + return 0; +} + int bt_mesh_client_free_node(bt_mesh_client_node_t *node) { bt_mesh_client_internal_data_t *internal = NULL; @@ -314,7 +339,7 @@ int bt_mesh_client_free_node(bt_mesh_client_node_t *node) sys_slist_find_and_remove(&internal->queue, &node->client_node); bt_mesh_list_unlock(); // Free the node - osi_free(node); + bt_mesh_free(node); return 0; } @@ -334,7 +359,7 @@ int bt_mesh_client_clear_list(void *data) bt_mesh_list_lock(); while (!sys_slist_is_empty(&internal->queue)) { node = (void *)sys_slist_get_not_empty(&internal->queue); - osi_free(node); + bt_mesh_free(node); } bt_mesh_list_unlock(); diff --git a/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c b/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c index 8352d1bf4..3ccb43a79 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c @@ -14,19 +14,8 @@ #include #include -#include -#include "osi/allocator.h" -#include "osi/mutex.h" -#include "sdkconfig.h" - -#include "mesh_types.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" - -#include "mesh.h" #include "model_opcode.h" -#include "mesh_common.h" #include "generic_client.h" #include "btc_ble_mesh_generic_model.h" @@ -120,32 +109,36 @@ static const bt_mesh_client_op_pair_t gen_op_pair[] = { { BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET, BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS }, }; -static osi_mutex_t generic_client_mutex; +static bt_mesh_mutex_t generic_client_lock; static void bt_mesh_generic_client_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&generic_client_mutex); - init = true; + if (!generic_client_lock.mutex) { + bt_mesh_mutex_create(&generic_client_lock); } } +static void bt_mesh_generic_client_mutex_free(void) +{ + bt_mesh_mutex_free(&generic_client_lock); +} + static void bt_mesh_generic_client_lock(void) { - osi_mutex_lock(&generic_client_mutex, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_mutex_lock(&generic_client_lock); } static void bt_mesh_generic_client_unlock(void) { - osi_mutex_unlock(&generic_client_mutex); + bt_mesh_mutex_unlock(&generic_client_lock); } static void timeout_handler(struct k_work *work) { struct k_delayed_work *timer = NULL; bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; + u32_t opcode = 0U; BT_WARN("Receive generic status message timeout"); @@ -156,10 +149,11 @@ static void timeout_handler(struct k_work *work) if (timer && !k_delayed_work_free(timer)) { node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); if (node) { - bt_mesh_generic_client_cb_evt_to_btc(node->opcode, - BTC_BLE_MESH_EVT_GENERIC_CLIENT_TIMEOUT, node->ctx.model, &node->ctx, NULL, 0); - // Don't forget to release the node at the end. + memcpy(&ctx, &node->ctx, sizeof(ctx)); + opcode = node->opcode; bt_mesh_client_free_node(node); + bt_mesh_generic_client_cb_evt_to_btc( + opcode, BTC_BLE_MESH_EVT_GENERIC_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0); } } @@ -175,7 +169,7 @@ static void generic_status(struct bt_mesh_model *model, bt_mesh_client_node_t *node = NULL; u8_t *val = NULL; u8_t evt = 0xFF; - size_t len = 0; + size_t len = 0U; BT_DBG("%s, len %d, bytes %s", __func__, buf->len, bt_hex(buf->data, buf->len)); @@ -186,7 +180,7 @@ static void generic_status(struct bt_mesh_model *model, BT_ERR("Invalid Generic OnOff Status length %d", buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_gen_onoff_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_onoff_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -207,7 +201,7 @@ static void generic_status(struct bt_mesh_model *model, BT_ERR("Invalid Generic Level Status length %d", buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_gen_level_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_level_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -228,7 +222,7 @@ static void generic_status(struct bt_mesh_model *model, BT_ERR("Invalid Generic Default Trans Time Status length %d", buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_gen_def_trans_time_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_def_trans_time_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -244,7 +238,7 @@ static void generic_status(struct bt_mesh_model *model, BT_ERR("Invalid Generic OnPowerUp Status length %d", buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_gen_onpowerup_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_onpowerup_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -260,7 +254,7 @@ static void generic_status(struct bt_mesh_model *model, BT_ERR("Invalid Generic Power Level Status length %d", buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_gen_power_level_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_power_level_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -281,7 +275,7 @@ static void generic_status(struct bt_mesh_model *model, BT_ERR("Invalid Generic Power Last Status length %d", buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_gen_power_last_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_power_last_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -297,7 +291,7 @@ static void generic_status(struct bt_mesh_model *model, BT_ERR("Invalid Generic Power Default Status length %d", buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_gen_power_default_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_power_default_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -313,7 +307,7 @@ static void generic_status(struct bt_mesh_model *model, BT_ERR("Invalid Generic Power Range Status length %d", buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_gen_power_range_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_power_range_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -331,7 +325,7 @@ static void generic_status(struct bt_mesh_model *model, BT_ERR("Invalid Generic Battery Status length %d", buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_gen_battery_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_battery_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -353,7 +347,7 @@ static void generic_status(struct bt_mesh_model *model, BT_ERR("Invalid Generic Location Global Status length %d", buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_gen_loc_global_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_loc_global_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -371,7 +365,7 @@ static void generic_status(struct bt_mesh_model *model, BT_ERR("Invalid Generic Location Local Status length %d", buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_gen_loc_local_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_loc_local_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -387,7 +381,7 @@ static void generic_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS: { struct bt_mesh_gen_user_properties_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_gen_user_properties_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_user_properties_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -395,7 +389,7 @@ static void generic_status(struct bt_mesh_model *model, status->user_property_ids = bt_mesh_alloc_buf(buf->len); if (!status->user_property_ids) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->user_property_ids, buf->data, buf->len); @@ -405,7 +399,7 @@ static void generic_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS: { struct bt_mesh_gen_user_property_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_gen_user_property_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_user_property_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -417,7 +411,7 @@ static void generic_status(struct bt_mesh_model *model, status->user_property_value = bt_mesh_alloc_buf(buf->len); if (!status->user_property_value) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->user_property_value, buf->data, buf->len); @@ -428,7 +422,7 @@ static void generic_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS: { struct bt_mesh_gen_admin_properties_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_gen_admin_properties_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_admin_properties_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -436,7 +430,7 @@ static void generic_status(struct bt_mesh_model *model, status->admin_property_ids = bt_mesh_alloc_buf(buf->len); if (!status->admin_property_ids) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->admin_property_ids, buf->data, buf->len); @@ -446,7 +440,7 @@ static void generic_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS: { struct bt_mesh_gen_admin_property_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_gen_admin_property_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_admin_property_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -458,7 +452,7 @@ static void generic_status(struct bt_mesh_model *model, status->admin_property_value = bt_mesh_alloc_buf(buf->len); if (!status->admin_property_value) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->admin_property_value, buf->data, buf->len); @@ -469,7 +463,7 @@ static void generic_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS: { struct bt_mesh_gen_manu_properties_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_gen_manu_properties_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_manu_properties_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -477,7 +471,7 @@ static void generic_status(struct bt_mesh_model *model, status->manu_property_ids = bt_mesh_alloc_buf(buf->len); if (!status->manu_property_ids) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->manu_property_ids, buf->data, buf->len); @@ -487,7 +481,7 @@ static void generic_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS: { struct bt_mesh_gen_manu_property_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_gen_manu_property_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_manu_property_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -499,7 +493,7 @@ static void generic_status(struct bt_mesh_model *model, status->manu_property_value = bt_mesh_alloc_buf(buf->len); if (!status->manu_property_value) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->manu_property_value, buf->data, buf->len); @@ -510,7 +504,7 @@ static void generic_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS: { struct bt_mesh_gen_client_properties_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_gen_client_properties_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_client_properties_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -518,7 +512,7 @@ static void generic_status(struct bt_mesh_model *model, status->client_property_ids = bt_mesh_alloc_buf(buf->len); if (!status->client_property_ids) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->client_property_ids, buf->data, buf->len); @@ -582,9 +576,9 @@ static void generic_status(struct bt_mesh_model *model, } if (!k_delayed_work_free(&node->timer)) { - bt_mesh_generic_client_cb_evt_to_btc(node->opcode, evt, model, ctx, val, len); - // Don't forget to release the node at the end. + u32_t opcode = node->opcode; bt_mesh_client_free_node(node); + bt_mesh_generic_client_cb_evt_to_btc(opcode, evt, model, ctx, val, len); } } @@ -637,7 +631,7 @@ static void generic_status(struct bt_mesh_model *model, break; } - osi_free(val); + bt_mesh_free(val); return; } @@ -695,7 +689,7 @@ const struct bt_mesh_model_op gen_property_cli_op[] = { static int gen_get_state(bt_mesh_client_common_param_t *common, void *value) { NET_BUF_SIMPLE_DEFINE(msg, BLE_MESH_GEN_GET_STATE_MSG_LEN); - int err; + int err = 0; bt_mesh_model_msg_init(&msg, common->opcode); @@ -745,7 +739,7 @@ static int gen_set_state(bt_mesh_client_common_param_t *common, void *value, u16_t value_len, bool need_ack) { struct net_buf_simple *msg = NULL; - int err; + int err = 0; msg = bt_mesh_alloc_buf(value_len); if (!msg) { @@ -989,8 +983,8 @@ int bt_mesh_generic_client_get_state(bt_mesh_client_common_param_t *common, void int bt_mesh_generic_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status) { bt_mesh_generic_client_t *client = NULL; - u16_t length = 0; - bool need_ack = false; + u16_t length = 0U; + bool need_ack = false; if (!common || !common->model || !set) { BT_ERR("%s, Invalid parameter", __func__); @@ -1173,7 +1167,7 @@ static int generic_client_init(struct bt_mesh_model *model, bool primary) } if (!client->internal_data) { - internal = osi_calloc(sizeof(generic_internal_data_t)); + internal = bt_mesh_calloc(sizeof(generic_internal_data_t)); if (!internal) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; @@ -1233,3 +1227,72 @@ int bt_mesh_gen_property_cli_init(struct bt_mesh_model *model, bool primary) { return generic_client_init(model, primary); } + +static int generic_client_deinit(struct bt_mesh_model *model, bool primary) +{ + bt_mesh_generic_client_t *client = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_generic_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, Generic Client user_data is NULL", __func__); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + client->internal_data = NULL; + } + + bt_mesh_generic_client_mutex_free(); + + return 0; +} + +int bt_mesh_gen_onoff_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_level_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_def_trans_time_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_pwr_onoff_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_pwr_level_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_battery_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_location_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_property_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h b/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h index ff4e52609..0e337d34b 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h @@ -84,6 +84,8 @@ void bt_mesh_client_model_unlock(void); int bt_mesh_client_init(struct bt_mesh_model *model); +int bt_mesh_client_deinit(struct bt_mesh_model *model); + /** * @brief Check if the msg received by client model is a publish msg or not * @@ -110,14 +112,6 @@ int bt_mesh_client_free_node(bt_mesh_client_node_t *node); int bt_mesh_client_clear_list(void *data); -enum { - NODE = 0, - PROVISIONER, - FAST_PROV, -}; - -#define ROLE_NVAL 0xFF - typedef struct { struct bt_mesh_model *model; /* The client model structure */ u8_t role; /* Role of the device - Node/Provisioner */ diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h index a5c72b638..ddee425ac 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h @@ -19,9 +19,6 @@ #ifndef _GENERIC_CLIENT_H_ #define _GENERIC_CLIENT_H_ -#include "mesh_access.h" -#include "mesh_kernel.h" - #include "client_common.h" /* Generic client model common structure */ @@ -466,6 +463,87 @@ int bt_mesh_gen_location_cli_init(struct bt_mesh_model *model, bool primary); */ int bt_mesh_gen_property_cli_init(struct bt_mesh_model *model, bool primary); +/** + * @brief This function is called to de-initialize generic onoff client model user_data. + * + * @param[in] model: Pointer to generic onoff client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_onoff_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic level client model user_data. + * + * @param[in] model: Pointer to generic level client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_level_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic default transition time + * client model user_data. + * + * @param[in] model: Pointer to generic default transition time client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_def_trans_time_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic power onoff client model user_data. + * + * @param[in] model: Pointer to generic power onoff client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_pwr_onoff_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic power level client model user_data. + * + * @param[in] model: Pointer to generic power level client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_pwr_level_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic battery client model user_data. + * + * @param[in] model: Pointer to generic battery client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_battery_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic location client model user_data. + * + * @param[in] model: Pointer to generic location client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_location_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic property client model user_data. + * + * @param[in] model: Pointer to generic property client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_property_cli_deinit(struct bt_mesh_model *model, bool primary); + /** * @brief This function is called to get generic states. * diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h index 850205215..309a7c57e 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h @@ -19,9 +19,6 @@ #ifndef _LIGHTING_CLIENT_H_ #define _LIGHTING_CLIENT_H_ -#include "mesh_access.h" -#include "mesh_kernel.h" - #include "client_common.h" /* Light client model common structure */ @@ -467,6 +464,56 @@ int bt_mesh_light_xyl_cli_init(struct bt_mesh_model *model, bool primary); */ int bt_mesh_light_lc_cli_init(struct bt_mesh_model *model, bool primary); +/** + * @brief This function is called to de-initialize light lightness client model user_data. + * + * @param[in] model: Pointer to light lightness client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_lightness_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize light ctl client model user_data. + * + * @param[in] model: Pointer to light ctl client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_ctl_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize light hsl client model user_data. + * + * @param[in] model: Pointer to light hsl client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_hsl_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize light xyl client model user_data. + * + * @param[in] model: Pointer to light xyl client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_xyl_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize light lc client model user_data. + * + * @param[in] model: Pointer to light lc client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_lc_cli_deinit(struct bt_mesh_model *model, bool primary); + /** * @brief This function is called to get light states. * diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h index 39855bd9a..046e09916 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h @@ -19,9 +19,6 @@ #ifndef _SENSOR_CLIENT_H_ #define _SENSOR_CLIENT_H_ -#include "mesh_access.h" -#include "mesh_kernel.h" - #include "client_common.h" /* Sensor Client Model Context */ @@ -142,6 +139,16 @@ struct bt_mesh_sensor_series_get { */ int bt_mesh_sensor_cli_init(struct bt_mesh_model *model, bool primary); +/** + * @brief This function is called to de-initialize sensor client model user_data. + * + * @param[in] model: Pointer to sensor client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_sensor_cli_deinit(struct bt_mesh_model *model, bool primary); + /** * @brief This function is called to get sensor states. * diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h index 182316e05..de978d011 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h @@ -19,9 +19,6 @@ #ifndef _TIME_SCENE_CLIENT_H_ #define _TIME_SCENE_CLIENT_H_ -#include "mesh_access.h" -#include "mesh_kernel.h" - #include "client_common.h" /* Time scene client model common structure */ @@ -232,6 +229,36 @@ int bt_mesh_scene_cli_init(struct bt_mesh_model *model, bool primary); */ int bt_mesh_scheduler_cli_init(struct bt_mesh_model *model, bool primary); +/** + * @brief This function is called to de-initialize time client model user_data. + * + * @param[in] model: Pointer to time client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_time_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize scene client model user_data. + * + * @param[in] model: Pointer to scene client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_scene_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize scheduler client model user_data. + * + * @param[in] model: Pointer to scheduler client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_scheduler_cli_deinit(struct bt_mesh_model *model, bool primary); + /** * @brief This function is called to get scene states. * diff --git a/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c b/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c index 87d2922c1..ff693cf88 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c @@ -14,19 +14,8 @@ #include #include -#include -#include "osi/allocator.h" -#include "osi/mutex.h" -#include "sdkconfig.h" - -#include "mesh_types.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" - -#include "mesh.h" #include "model_opcode.h" -#include "mesh_common.h" #include "lighting_client.h" #include "btc_ble_mesh_lighting_model.h" @@ -129,32 +118,36 @@ static const bt_mesh_client_op_pair_t light_op_pair[] = { { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET, BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS }, }; -static osi_mutex_t light_client_mutex; +static bt_mesh_mutex_t light_client_lock; static void bt_mesh_light_client_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&light_client_mutex); - init = true; + if (!light_client_lock.mutex) { + bt_mesh_mutex_create(&light_client_lock); } } +static void bt_mesh_light_client_mutex_free(void) +{ + bt_mesh_mutex_free(&light_client_lock); +} + static void bt_mesh_light_client_lock(void) { - osi_mutex_lock(&light_client_mutex, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_mutex_lock(&light_client_lock); } static void bt_mesh_light_client_unlock(void) { - osi_mutex_unlock(&light_client_mutex); + bt_mesh_mutex_unlock(&light_client_lock); } static void timeout_handler(struct k_work *work) { struct k_delayed_work *timer = NULL; bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; + u32_t opcode = 0U; BT_WARN("Receive light status message timeout"); @@ -165,10 +158,11 @@ static void timeout_handler(struct k_work *work) if (timer && !k_delayed_work_free(timer)) { node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); if (node) { - bt_mesh_lighting_client_cb_evt_to_btc(node->opcode, - BTC_BLE_MESH_EVT_LIGHTING_CLIENT_TIMEOUT, node->ctx.model, &node->ctx, NULL, 0); - // Don't forget to release the node at the end. + memcpy(&ctx, &node->ctx, sizeof(ctx)); + opcode = node->opcode; bt_mesh_client_free_node(node); + bt_mesh_lighting_client_cb_evt_to_btc( + opcode, BTC_BLE_MESH_EVT_LIGHTING_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0); } } @@ -184,7 +178,7 @@ static void light_status(struct bt_mesh_model *model, bt_mesh_client_node_t *node = NULL; u8_t *val = NULL; u8_t evt = 0xFF; - size_t len = 0; + size_t len = 0U; BT_DBG("%s, len %d, bytes %s", __func__, buf->len, bt_hex(buf->data, buf->len)); @@ -195,7 +189,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light Lightness Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_lightness_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lightness_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -216,7 +210,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light Lightness Linear Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_lightness_linear_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lightness_linear_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -237,7 +231,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light Lightness Last Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_lightness_last_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lightness_last_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -253,7 +247,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light Lightness Default Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_lightness_default_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lightness_default_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -269,7 +263,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light Lightness Range Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_lightness_range_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lightness_range_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -287,7 +281,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light CTL Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_ctl_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_ctl_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -310,7 +304,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light CTL Temperature Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_ctl_temperature_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_ctl_temperature_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -333,7 +327,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light CTL Temperature Range Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_ctl_temperature_range_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_ctl_temperature_range_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -351,7 +345,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light CTL Default Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_ctl_default_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_ctl_default_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -369,7 +363,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light HSL Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_hsl_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_hsl_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -391,7 +385,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light HSL Target Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_hsl_target_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_hsl_target_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -413,7 +407,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light HSL Hue Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_hsl_hue_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_hsl_hue_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -434,7 +428,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light HSL Saturation Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_hsl_saturation_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_hsl_saturation_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -455,7 +449,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light HSL Default Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_hsl_default_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_hsl_default_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -473,7 +467,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light HSL Range Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_hsl_range_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_hsl_range_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -493,7 +487,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light xyL Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_xyl_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_xyl_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -515,7 +509,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light xyL Target Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_xyl_target_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_xyl_target_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -537,7 +531,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light xyL Default Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_xyl_default_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_xyl_default_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -555,7 +549,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light xyL Range Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_xyl_range_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_xyl_range_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -575,7 +569,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light LC Mode Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_lc_mode_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lc_mode_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -591,7 +585,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light LC OM Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_lc_om_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lc_om_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -607,7 +601,7 @@ static void light_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Light LC Light OnOff Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_light_lc_light_onoff_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lc_light_onoff_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -624,7 +618,7 @@ static void light_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS: { struct bt_mesh_light_lc_property_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_light_lc_property_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lc_property_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -633,7 +627,7 @@ static void light_status(struct bt_mesh_model *model, status->light_lc_property_value = bt_mesh_alloc_buf(buf->len); if (!status->light_lc_property_value) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->light_lc_property_value, buf->data, buf->len); @@ -708,9 +702,9 @@ static void light_status(struct bt_mesh_model *model, } if (!k_delayed_work_free(&node->timer)) { - bt_mesh_lighting_client_cb_evt_to_btc(node->opcode, evt, model, ctx, val, len); - // Don't forget to release the node at the end. + u32_t opcode = node->opcode; bt_mesh_client_free_node(node); + bt_mesh_lighting_client_cb_evt_to_btc(opcode, evt, model, ctx, val, len); } } @@ -727,7 +721,7 @@ static void light_status(struct bt_mesh_model *model, break; } - osi_free(val); + bt_mesh_free(val); return; } @@ -778,7 +772,7 @@ const struct bt_mesh_model_op light_lc_cli_op[] = { static int light_get_state(bt_mesh_client_common_param_t *common, void *value) { NET_BUF_SIMPLE_DEFINE(msg, BLE_MESH_LIGHT_GET_STATE_MSG_LEN); - int err; + int err = 0; bt_mesh_model_msg_init(&msg, common->opcode); @@ -810,7 +804,7 @@ static int light_set_state(bt_mesh_client_common_param_t *common, void *value, u16_t value_len, bool need_ack) { struct net_buf_simple *msg = NULL; - int err; + int err = 0; msg = bt_mesh_alloc_buf(value_len); if (!msg) { @@ -1103,8 +1097,8 @@ int bt_mesh_light_client_get_state(bt_mesh_client_common_param_t *common, void * int bt_mesh_light_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status) { bt_mesh_light_client_t *client = NULL; - u16_t length = 0; - bool need_ack = false; + u16_t length = 0U; + bool need_ack = false; if (!common || !common->model || !set) { BT_ERR("%s, Invalid parameter", __func__); @@ -1363,7 +1357,7 @@ static int light_client_init(struct bt_mesh_model *model, bool primary) } if (!client->internal_data) { - internal = osi_calloc(sizeof(light_internal_data_t)); + internal = bt_mesh_calloc(sizeof(light_internal_data_t)); if (!internal) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; @@ -1407,4 +1401,58 @@ int bt_mesh_light_xyl_cli_init(struct bt_mesh_model *model, bool primary) int bt_mesh_light_lc_cli_init(struct bt_mesh_model *model, bool primary) { return light_client_init(model, primary); +} + +static int light_client_deinit(struct bt_mesh_model *model, bool primary) +{ + bt_mesh_light_client_t *client = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_light_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, Lighting Client user_data is NULL", __func__); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + client->internal_data = NULL; + } + + bt_mesh_light_client_mutex_free(); + + return 0; +} + +int bt_mesh_light_lightness_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_client_deinit(model, primary); +} + +int bt_mesh_light_ctl_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_client_deinit(model, primary); +} + +int bt_mesh_light_hsl_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_client_deinit(model, primary); +} + +int bt_mesh_light_xyl_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_client_deinit(model, primary); +} + +int bt_mesh_light_lc_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_client_deinit(model, primary); } \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c b/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c index d8eb7bd2d..9df2e25ae 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c @@ -14,19 +14,8 @@ #include #include -#include -#include "osi/allocator.h" -#include "osi/mutex.h" -#include "sdkconfig.h" - -#include "mesh_types.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" - -#include "mesh.h" #include "model_opcode.h" -#include "mesh_common.h" #include "sensor_client.h" #include "btc_ble_mesh_sensor_model.h" @@ -58,32 +47,36 @@ static const bt_mesh_client_op_pair_t sensor_op_pair[] = { { BLE_MESH_MODEL_OP_SENSOR_SERIES_GET, BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS }, }; -static osi_mutex_t sensor_client_mutex; +static bt_mesh_mutex_t sensor_client_lock; static void bt_mesh_sensor_client_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&sensor_client_mutex); - init = true; + if (!sensor_client_lock.mutex) { + bt_mesh_mutex_create(&sensor_client_lock); } } +static void bt_mesh_sensor_client_mutex_free(void) +{ + bt_mesh_mutex_free(&sensor_client_lock); +} + static void bt_mesh_sensor_client_lock(void) { - osi_mutex_lock(&sensor_client_mutex, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_mutex_lock(&sensor_client_lock); } static void bt_mesh_sensor_client_unlock(void) { - osi_mutex_unlock(&sensor_client_mutex); + bt_mesh_mutex_unlock(&sensor_client_lock); } static void timeout_handler(struct k_work *work) { struct k_delayed_work *timer = NULL; bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; + u32_t opcode = 0U; BT_WARN("Receive sensor status message timeout"); @@ -94,10 +87,11 @@ static void timeout_handler(struct k_work *work) if (timer && !k_delayed_work_free(timer)) { node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); if (node) { - bt_mesh_sensor_client_cb_evt_to_btc(node->opcode, - BTC_BLE_MESH_EVT_SENSOR_CLIENT_TIMEOUT, node->ctx.model, &node->ctx, NULL, 0); - // Don't forget to release the node at the end. + memcpy(&ctx, &node->ctx, sizeof(ctx)); + opcode = node->opcode; bt_mesh_client_free_node(node); + bt_mesh_sensor_client_cb_evt_to_btc( + opcode, BTC_BLE_MESH_EVT_SENSOR_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0); } } @@ -113,14 +107,14 @@ static void sensor_status(struct bt_mesh_model *model, bt_mesh_client_node_t *node = NULL; u8_t *val = NULL; u8_t evt = 0xFF; - size_t len = 0; + size_t len = 0U; BT_DBG("%s, len %d, bytes %s", __func__, buf->len, bt_hex(buf->data, buf->len)); switch (ctx->recv_op) { case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS: { struct bt_mesh_sensor_descriptor_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_sensor_descriptor_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_descriptor_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -128,7 +122,7 @@ static void sensor_status(struct bt_mesh_model *model, status->descriptor = bt_mesh_alloc_buf(buf->len); if (!status->descriptor) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->descriptor, buf->data, buf->len); @@ -138,7 +132,7 @@ static void sensor_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS: { struct bt_mesh_sensor_cadence_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_sensor_cadence_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_cadence_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -147,7 +141,7 @@ static void sensor_status(struct bt_mesh_model *model, status->sensor_cadence_value = bt_mesh_alloc_buf(buf->len); if (!status->sensor_cadence_value) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->sensor_cadence_value, buf->data, buf->len); @@ -157,7 +151,7 @@ static void sensor_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS: { struct bt_mesh_sensor_settings_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_sensor_settings_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_settings_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -166,7 +160,7 @@ static void sensor_status(struct bt_mesh_model *model, status->sensor_setting_property_ids = bt_mesh_alloc_buf(buf->len); if (!status->sensor_setting_property_ids) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->sensor_setting_property_ids, buf->data, buf->len); @@ -176,7 +170,7 @@ static void sensor_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS: { struct bt_mesh_sensor_setting_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_sensor_setting_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_setting_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -189,7 +183,7 @@ static void sensor_status(struct bt_mesh_model *model, status->sensor_setting_raw = bt_mesh_alloc_buf(buf->len); if (!status->sensor_setting_raw) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->sensor_setting_raw, buf->data, buf->len); @@ -200,7 +194,7 @@ static void sensor_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_SENSOR_STATUS: { struct bt_mesh_sensor_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_sensor_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -208,7 +202,7 @@ static void sensor_status(struct bt_mesh_model *model, status->marshalled_sensor_data = bt_mesh_alloc_buf(buf->len); if (!status->marshalled_sensor_data) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->marshalled_sensor_data, buf->data, buf->len); @@ -218,7 +212,7 @@ static void sensor_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS: { struct bt_mesh_sensor_column_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_sensor_column_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_column_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -227,7 +221,7 @@ static void sensor_status(struct bt_mesh_model *model, status->sensor_column_value = bt_mesh_alloc_buf(buf->len); if (!status->sensor_column_value) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->sensor_column_value, buf->data, buf->len); @@ -237,7 +231,7 @@ static void sensor_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS: { struct bt_mesh_sensor_series_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_sensor_series_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_series_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -246,7 +240,7 @@ static void sensor_status(struct bt_mesh_model *model, status->sensor_series_value = bt_mesh_alloc_buf(buf->len); if (!status->sensor_series_value) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->sensor_series_value, buf->data, buf->len); @@ -287,9 +281,9 @@ static void sensor_status(struct bt_mesh_model *model, } if (!k_delayed_work_free(&node->timer)) { - bt_mesh_sensor_client_cb_evt_to_btc(node->opcode, evt, model, ctx, val, len); - // Don't forget to release the node at the end. + u32_t opcode = node->opcode; bt_mesh_client_free_node(node); + bt_mesh_sensor_client_cb_evt_to_btc(opcode, evt, model, ctx, val, len); } } @@ -342,7 +336,7 @@ static void sensor_status(struct bt_mesh_model *model, break; } - osi_free(val); + bt_mesh_free(val); return; } @@ -362,7 +356,7 @@ static int sensor_act_state(bt_mesh_client_common_param_t *common, void *value, u16_t value_len, bool need_ack) { struct net_buf_simple *msg = NULL; - int err; + int err = 0; msg = bt_mesh_alloc_buf(value_len); if (!msg) { @@ -469,7 +463,7 @@ end: int bt_mesh_sensor_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status) { bt_mesh_sensor_client_t *client = NULL; - u16_t length = 0; + u16_t length = 0U; if (!common || !common->model || !get) { BT_ERR("%s, Invalid parameter", __func__); @@ -534,7 +528,7 @@ int bt_mesh_sensor_client_get_state(bt_mesh_client_common_param_t *common, void int bt_mesh_sensor_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status) { bt_mesh_sensor_client_t *client = NULL; - u16_t length = 0; + u16_t length = 0U; bool need_ack = false; if (!common || !common->model || !set) { @@ -605,7 +599,7 @@ int bt_mesh_sensor_cli_init(struct bt_mesh_model *model, bool primary) } if (!client->internal_data) { - internal = osi_calloc(sizeof(sensor_internal_data_t)); + internal = bt_mesh_calloc(sizeof(sensor_internal_data_t)); if (!internal) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; @@ -623,5 +617,34 @@ int bt_mesh_sensor_cli_init(struct bt_mesh_model *model, bool primary) bt_mesh_sensor_client_mutex_new(); + return 0; +} + +int bt_mesh_sensor_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + bt_mesh_sensor_client_t *client = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_sensor_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, Sensor Client user_data is NULL", __func__); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + client->internal_data = NULL; + } + + bt_mesh_sensor_client_mutex_free(); + return 0; } \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c b/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c index b7456d2a4..15961d1eb 100644 --- a/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c +++ b/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c @@ -14,19 +14,8 @@ #include #include -#include -#include "osi/allocator.h" -#include "osi/mutex.h" -#include "sdkconfig.h" - -#include "mesh_types.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" - -#include "mesh.h" #include "model_opcode.h" -#include "mesh_common.h" #include "time_scene_client.h" #include "btc_ble_mesh_time_scene_model.h" @@ -74,32 +63,36 @@ static const bt_mesh_client_op_pair_t time_scene_op_pair[] = { { BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET, BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS }, }; -static osi_mutex_t time_scene_client_mutex; +static bt_mesh_mutex_t time_scene_client_lock; static void bt_mesh_time_scene_client_mutex_new(void) { - static bool init; - - if (!init) { - osi_mutex_new(&time_scene_client_mutex); - init = true; + if (!time_scene_client_lock.mutex) { + bt_mesh_mutex_create(&time_scene_client_lock); } } +static void bt_mesh_time_scene_client_mutex_free(void) +{ + bt_mesh_mutex_free(&time_scene_client_lock); +} + static void bt_mesh_time_scene_client_lock(void) { - osi_mutex_lock(&time_scene_client_mutex, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_mutex_lock(&time_scene_client_lock); } static void bt_mesh_time_scene_client_unlock(void) { - osi_mutex_unlock(&time_scene_client_mutex); + bt_mesh_mutex_unlock(&time_scene_client_lock); } static void timeout_handler(struct k_work *work) { struct k_delayed_work *timer = NULL; bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; + u32_t opcode = 0U; BT_WARN("Receive time scene status message timeout"); @@ -110,10 +103,11 @@ static void timeout_handler(struct k_work *work) if (timer && !k_delayed_work_free(timer)) { node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); if (node) { - bt_mesh_time_scene_client_cb_evt_to_btc(node->opcode, - BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_TIMEOUT, node->ctx.model, &node->ctx, NULL, 0); - // Don't forget to release the node at the end. + memcpy(&ctx, &node->ctx, sizeof(ctx)); + opcode = node->opcode; bt_mesh_client_free_node(node); + bt_mesh_time_scene_client_cb_evt_to_btc( + opcode, BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0); } } @@ -129,7 +123,7 @@ static void time_scene_status(struct bt_mesh_model *model, bt_mesh_client_node_t *node = NULL; u8_t *val = NULL; u8_t evt = 0xFF; - size_t len = 0; + size_t len = 0U; BT_DBG("%s, len %d, bytes %s", __func__, buf->len, bt_hex(buf->data, buf->len)); @@ -140,7 +134,7 @@ static void time_scene_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Time Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_time_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_time_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -163,7 +157,7 @@ static void time_scene_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Time Zone Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_time_zone_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_time_zone_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -182,7 +176,7 @@ static void time_scene_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid TAI UTC Delta Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_tai_utc_delta_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_tai_utc_delta_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -205,7 +199,7 @@ static void time_scene_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Time Role Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_time_role_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_time_role_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -221,7 +215,7 @@ static void time_scene_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Scene Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_scene_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_scene_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -239,7 +233,7 @@ static void time_scene_status(struct bt_mesh_model *model, } case BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS: { struct bt_mesh_scene_register_status *status = NULL; - status = osi_calloc(sizeof(struct bt_mesh_scene_register_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_scene_register_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -249,7 +243,7 @@ static void time_scene_status(struct bt_mesh_model *model, status->scenes = bt_mesh_alloc_buf(buf->len); if (!status->scenes) { BT_ERR("%s, Failed to allocate memory", __func__); - osi_free(status); + bt_mesh_free(status); return; } net_buf_simple_add_mem(status->scenes, buf->data, buf->len); @@ -263,7 +257,7 @@ static void time_scene_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Scheduler Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_scheduler_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_scheduler_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -279,7 +273,7 @@ static void time_scene_status(struct bt_mesh_model *model, BT_ERR("%s, Invalid Scheduler Action Status length %d", __func__, buf->len); return; } - status = osi_calloc(sizeof(struct bt_mesh_scheduler_act_status)); + status = bt_mesh_calloc(sizeof(struct bt_mesh_scheduler_act_status)); if (!status) { BT_ERR("%s, Failed to allocate memory", __func__); return; @@ -331,9 +325,9 @@ static void time_scene_status(struct bt_mesh_model *model, } if (!k_delayed_work_free(&node->timer)) { - bt_mesh_time_scene_client_cb_evt_to_btc(node->opcode, evt, model, ctx, val, len); - // Don't forget to release the node at the end. + u32_t opcode = node->opcode; bt_mesh_client_free_node(node); + bt_mesh_time_scene_client_cb_evt_to_btc(opcode, evt, model, ctx, val, len); } } @@ -350,7 +344,7 @@ static void time_scene_status(struct bt_mesh_model *model, break; } - osi_free(val); + bt_mesh_free(val); return; } @@ -378,7 +372,7 @@ const struct bt_mesh_model_op scheduler_cli_op[] = { static int time_scene_get_state(bt_mesh_client_common_param_t *common, void *value) { NET_BUF_SIMPLE_DEFINE(msg, BLE_MESH_SCENE_GET_STATE_MSG_LEN); - int err; + int err = 0; bt_mesh_model_msg_init(&msg, common->opcode); @@ -410,7 +404,7 @@ static int time_scene_set_state(bt_mesh_client_common_param_t *common, void *value, u16_t value_len, bool need_ack) { struct net_buf_simple *msg = NULL; - int err; + int err = 0; msg = bt_mesh_alloc_buf(value_len); if (!msg) { @@ -544,7 +538,7 @@ int bt_mesh_time_scene_client_get_state(bt_mesh_client_common_param_t *common, v int bt_mesh_time_scene_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status) { bt_mesh_time_scene_client_t *client = NULL; - u16_t length = 0; + u16_t length = 0U; bool need_ack = false; if (!common || !common->model || !set) { @@ -668,7 +662,7 @@ static int time_scene_client_init(struct bt_mesh_model *model, bool primary) } if (!client->internal_data) { - internal = osi_calloc(sizeof(time_scene_internal_data_t)); + internal = bt_mesh_calloc(sizeof(time_scene_internal_data_t)); if (!internal) { BT_ERR("%s, Failed to allocate memory", __func__); return -ENOMEM; @@ -702,4 +696,48 @@ int bt_mesh_scene_cli_init(struct bt_mesh_model *model, bool primary) int bt_mesh_scheduler_cli_init(struct bt_mesh_model *model, bool primary) { return time_scene_client_init(model, primary); +} + +static int time_scene_client_deinit(struct bt_mesh_model *model, bool primary) +{ + bt_mesh_time_scene_client_t *client = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_time_scene_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, Time Scene Client user_data is NULL", __func__); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + client->internal_data = NULL; + } + + bt_mesh_time_scene_client_mutex_free(); + + return 0; +} + +int bt_mesh_time_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return time_scene_client_deinit(model, primary); +} + +int bt_mesh_scene_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return time_scene_client_deinit(model, primary); +} + +int bt_mesh_scheduler_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return time_scene_client_deinit(model, primary); } \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_models/common/include/model_opcode.h b/components/bt/esp_ble_mesh/mesh_models/common/include/model_opcode.h index d9a49e71d..95def6c2c 100644 --- a/components/bt/esp_ble_mesh/mesh_models/common/include/model_opcode.h +++ b/components/bt/esp_ble_mesh/mesh_models/common/include/model_opcode.h @@ -15,7 +15,7 @@ #ifndef _MODEL_OPCODE_H_ #define _MODEL_OPCODE_H_ -#include "mesh_main.h" +#include "mesh_access.h" /* Generic OnOff Message Opcode */ #define BLE_MESH_MODEL_OP_GEN_ONOFF_GET BLE_MESH_MODEL_OP_2(0x82, 0x01) @@ -235,7 +235,7 @@ #define BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x7F) #define BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x80) #define BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x81) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x82) /* Model spec is wrong */ +#define BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x82) /* Light xyL Message Opcode */ #define BLE_MESH_MODEL_OP_LIGHT_XYL_GET BLE_MESH_MODEL_OP_2(0x82, 0x83) diff --git a/components/bt/esp_ble_mesh/mesh_models/server/device_property.c b/components/bt/esp_ble_mesh/mesh_models/server/device_property.c index 523f1bcd4..9e8afffcd 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/device_property.c +++ b/components/bt/esp_ble_mesh/mesh_models/server/device_property.c @@ -12,11 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include - -#include "mesh_types.h" - #include "server_common.h" #include "device_property.h" diff --git a/components/bt/esp_ble_mesh/mesh_models/server/generic_server.c b/components/bt/esp_ble_mesh/mesh_models/server/generic_server.c index 4bc4bbf65..79f7c08eb 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/generic_server.c +++ b/components/bt/esp_ble_mesh/mesh_models/server/generic_server.c @@ -6,47 +6,38 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include -#include -#include #include -#include "osi/mutex.h" - -#include "mesh_types.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" -#include "mesh_common.h" -#include "mesh.h" #include "access.h" -#include "model_opcode.h" #include "transport.h" - -#include "server_common.h" -#include "state_binding.h" +#include "model_opcode.h" #include "state_transition.h" #include "device_property.h" #include "btc_ble_mesh_generic_model.h" -static osi_mutex_t generic_server_mutex; +static bt_mesh_mutex_t generic_server_lock; static void bt_mesh_generic_server_mutex_new(void) { - if (!generic_server_mutex) { - osi_mutex_new(&generic_server_mutex); - __ASSERT(generic_server_mutex, "%s, fail", __func__); + if (!generic_server_lock.mutex) { + bt_mesh_mutex_create(&generic_server_lock); } } +static void bt_mesh_generic_server_mutex_free(void) +{ + bt_mesh_mutex_free(&generic_server_lock); +} + void bt_mesh_generic_server_lock(void) { - osi_mutex_lock(&generic_server_mutex, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_mutex_lock(&generic_server_lock); } void bt_mesh_generic_server_unlock(void) { - osi_mutex_unlock(&generic_server_mutex); + bt_mesh_mutex_unlock(&generic_server_lock); } /* message handlers (Start) */ @@ -133,9 +124,9 @@ static void gen_onoff_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_gen_onoff_srv *srv = model->user_data; - u8_t tid, onoff, trans_time, delay; - bool optional; - s64_t now; + u8_t tid = 0U, onoff = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + s64_t now = 0; if (srv == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -314,10 +305,10 @@ static void gen_level_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_gen_level_srv *srv = model->user_data; - u8_t tid, trans_time, delay; - bool optional; - s16_t level; - s64_t now; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + s16_t level = 0; + s64_t now = 0; if (srv == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -410,10 +401,10 @@ static void gen_delta_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_gen_level_srv *srv = model->user_data; - u8_t tid, trans_time, delay; - s32_t tmp32, delta; - bool optional; - s64_t now; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + s32_t tmp32 = 0, delta = 0; + bool optional = false; + s64_t now = 0; if (srv == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -531,11 +522,11 @@ static void gen_move_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_gen_level_srv *srv = model->user_data; - u8_t tid, trans_time, delay; - bool optional; - s16_t delta; - s32_t tmp32; - s64_t now; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + s16_t delta = 0; + s32_t tmp32 = 0; + s64_t now = 0; if (srv == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -718,7 +709,7 @@ static void gen_def_trans_time_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_gen_def_trans_time_srv *srv = model->user_data; - u8_t trans_time; + u8_t trans_time = 0U; if (srv == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -875,7 +866,7 @@ static void gen_onpowerup_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_gen_power_onoff_setup_srv *srv = model->user_data; - u8_t onpowerup; + u8_t onpowerup = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1003,7 +994,7 @@ static void gen_power_level_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_gen_power_level_srv *srv = model->user_data; - u16_t opcode; + u16_t opcode = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1077,10 +1068,10 @@ static void gen_power_level_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_gen_power_level_srv *srv = model->user_data; - u8_t tid, trans_time, delay; - bool optional; - u16_t power; - s64_t now; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + u16_t power = 0U; + s64_t now = 0; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1188,7 +1179,7 @@ static void gen_power_default_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data; - u16_t power; + u16_t power = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1238,7 +1229,7 @@ static void gen_power_range_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data; - u16_t range_min, range_max; + u16_t range_min = 0U, range_max = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1404,7 +1395,7 @@ static void gen_location_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_gen_location_srv *srv = model->user_data; - u16_t opcode; + u16_t opcode = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1440,7 +1431,7 @@ static void gen_location_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_gen_location_setup_srv *srv = model->user_data; - u16_t opcode; + u16_t opcode = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1553,9 +1544,9 @@ struct bt_mesh_generic_property *gen_get_user_property(struct bt_mesh_model *mod u16_t property_id) { struct bt_mesh_gen_user_prop_srv *srv = model->user_data; - u8_t i; + int i; - for (i = 0U; i < srv->property_count; i++) { + for (i = 0; i < srv->property_count; i++) { if (srv->properties[i].id == property_id) { return &srv->properties[i]; } @@ -1570,7 +1561,7 @@ static void send_gen_user_prop_status(struct bt_mesh_model *model, { struct bt_mesh_generic_property *property = NULL; struct net_buf_simple *msg = NULL; - u16_t length; + u16_t length = 0U; if (property_id == BLE_MESH_INVALID_DEVICE_PROPERTY_ID) { BT_ERR("%s, Invalid User Property ID 0x%04x", __func__, property_id); @@ -1691,8 +1682,8 @@ static void gen_user_prop_set(struct bt_mesh_model *model, { struct bt_mesh_gen_user_prop_srv *srv = model->user_data; struct bt_mesh_generic_property *property = NULL; - u16_t property_id; - u8_t expect_len; + u16_t property_id = 0U; + u8_t expect_len = 0U; if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1757,9 +1748,9 @@ struct bt_mesh_generic_property *gen_get_admin_property(struct bt_mesh_model *mo u16_t property_id) { struct bt_mesh_gen_admin_prop_srv *srv = model->user_data; - u8_t i; + int i; - for (i = 0U; i < srv->property_count; i++) { + for (i = 0; i < srv->property_count; i++) { if (srv->properties[i].id == property_id) { return &srv->properties[i]; } @@ -1774,7 +1765,7 @@ static void send_gen_admin_prop_status(struct bt_mesh_model *model, { struct bt_mesh_generic_property *property = NULL; struct net_buf_simple *msg = NULL; - u16_t length; + u16_t length = 0U; if (property_id == BLE_MESH_INVALID_DEVICE_PROPERTY_ID) { BT_ERR("%s, Invalid User Property ID 0x%04x", __func__, property_id); @@ -1838,7 +1829,7 @@ static void gen_admin_prop_get(struct bt_mesh_model *model, if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { bt_mesh_gen_server_recv_get_msg_t get = {0}; const u8_t *param = NULL; - size_t len = 0; + size_t len = 0U; if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET) { get.admin_property_get.id = net_buf_simple_pull_le16(buf); param = (const u8_t *)&get; @@ -1852,7 +1843,7 @@ static void gen_admin_prop_get(struct bt_mesh_model *model, switch (ctx->recv_op) { case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET: { struct net_buf_simple *msg = NULL; - u8_t i; + u8_t i = 0U; msg = bt_mesh_alloc_buf(1 + srv->property_count * 2 + BLE_MESH_SERVER_TRANS_MIC_SIZE); if (msg == NULL) { BT_ERR("%s, Failed to allocate memory", __func__); @@ -1883,8 +1874,8 @@ static void gen_admin_prop_set(struct bt_mesh_model *model, { struct bt_mesh_gen_admin_prop_srv *srv = model->user_data; struct bt_mesh_generic_property *property = NULL; - u16_t property_id; - u8_t access; + u16_t property_id = 0U; + u8_t access = 0U; if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1945,9 +1936,9 @@ struct bt_mesh_generic_property *gen_get_manu_property(struct bt_mesh_model *mod u16_t property_id) { struct bt_mesh_gen_manu_prop_srv *srv = model->user_data; - u8_t i; + int i; - for (i = 0U; i < srv->property_count; i++) { + for (i = 0; i < srv->property_count; i++) { if (srv->properties[i].id == property_id) { return &srv->properties[i]; } @@ -1962,7 +1953,7 @@ static void send_gen_manu_prop_status(struct bt_mesh_model *model, { struct bt_mesh_generic_property *property = NULL; struct net_buf_simple *msg = NULL; - u16_t length; + u16_t length = 0U; if (property_id == BLE_MESH_INVALID_DEVICE_PROPERTY_ID) { BT_ERR("%s, Invalid User Property ID 0x%04x", __func__, property_id); @@ -2023,7 +2014,7 @@ static void gen_manu_prop_get(struct bt_mesh_model *model, if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { bt_mesh_gen_server_recv_get_msg_t get = {0}; const u8_t *param = NULL; - size_t len = 0; + size_t len = 0U; if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET) { get.manu_property_get.id = net_buf_simple_pull_le16(buf); param = (const u8_t *)&get; @@ -2037,7 +2028,7 @@ static void gen_manu_prop_get(struct bt_mesh_model *model, switch (ctx->recv_op) { case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET: { struct net_buf_simple *msg = NULL; - u8_t i; + u8_t i = 0U; msg = bt_mesh_alloc_buf(1 + srv->property_count * 2 + BLE_MESH_SERVER_TRANS_MIC_SIZE); if (msg == NULL) { BT_ERR("%s, Failed to allocate memory", __func__); @@ -2068,8 +2059,8 @@ static void gen_manu_prop_set(struct bt_mesh_model *model, { struct bt_mesh_gen_manu_prop_srv *srv = model->user_data; struct bt_mesh_generic_property *property = NULL; - u16_t property_id; - u8_t access; + u16_t property_id = 0U; + u8_t access = 0U; if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -2124,8 +2115,8 @@ static void gen_manu_prop_set(struct bt_mesh_model *model, static int search_prop_id_index(const u16_t *array, u8_t array_idx, u16_t id) { static const u16_t *start = NULL; - u8_t index; - u16_t temp; + u8_t index = 0U; + u16_t temp = 0U; if (start == NULL) { start = array; @@ -2157,9 +2148,9 @@ static void gen_client_prop_get(struct bt_mesh_model *model, { struct bt_mesh_gen_client_prop_srv *srv = model->user_data; struct net_buf_simple *sdu = NULL; - u16_t total_len = 5; - u16_t property_id; - int i, index; + u16_t total_len = 5U; + u16_t property_id = 0U; + int i, index = 0; if (srv == NULL || srv->id_count == 0U || srv->property_ids == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -2630,4 +2621,175 @@ int bt_mesh_gen_client_prop_srv_init(struct bt_mesh_model *model, bool primary) } return generic_server_init(model); +} + +static int generic_server_deinit(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, No Generic Server context provided, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_GEN_ONOFF_SRV: { + struct bt_mesh_gen_onoff_srv *srv = model->user_data; + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_GEN_LEVEL_SRV: { + struct bt_mesh_gen_level_srv *srv = model->user_data; + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV: { + struct bt_mesh_gen_power_level_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Generic Power Level State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + default: + BT_WARN("%s, Unknown Generic Server Model, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + bt_mesh_generic_server_mutex_free(); + + return 0; +} + +int bt_mesh_gen_onoff_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic OnOff Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_level_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Level Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_def_trans_time_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Default Trans Time Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_power_onoff_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Power OnOff Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_power_onoff_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_server_deinit(model); +} + +int bt_mesh_gen_power_level_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Power Level Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_power_level_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_server_deinit(model); +} + +int bt_mesh_gen_battery_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Battery Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_location_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Location Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_location_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_server_deinit(model); +} + +int bt_mesh_gen_user_prop_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic User Property has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_admin_prop_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Admin Property has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_manu_prop_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Manufacturer Property has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_client_prop_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Client Property has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); } \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/generic_server.h b/components/bt/esp_ble_mesh/mesh_models/server/include/generic_server.h index a1465aba7..4ab53edda 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/include/generic_server.h +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/generic_server.h @@ -371,4 +371,19 @@ int bt_mesh_gen_admin_prop_srv_init(struct bt_mesh_model *model, bool primary); int bt_mesh_gen_manu_prop_srv_init(struct bt_mesh_model *model, bool primary); int bt_mesh_gen_client_prop_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_onoff_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_level_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_def_trans_time_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_power_onoff_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_power_onoff_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_power_level_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_power_level_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_battery_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_location_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_location_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_user_prop_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_admin_prop_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_manu_prop_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_client_prop_srv_deinit(struct bt_mesh_model *model, bool primary); + #endif /* _GENERIC_SERVER_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/lighting_server.h b/components/bt/esp_ble_mesh/mesh_models/server/include/lighting_server.h index 9ac112466..dd39ce006 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/include/lighting_server.h +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/lighting_server.h @@ -513,4 +513,18 @@ int bt_mesh_light_xyl_setup_srv_init(struct bt_mesh_model *model, bool primary); int bt_mesh_light_lc_srv_init(struct bt_mesh_model *model, bool primary); int bt_mesh_light_lc_setup_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_lightness_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_lightness_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_ctl_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_ctl_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_ctl_temp_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_hsl_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_hsl_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_hsl_hue_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_hsl_sat_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_xyl_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_xyl_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_lc_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_lc_setup_srv_deinit(struct bt_mesh_model *model, bool primary); + #endif /* _LIGHTING_SERVER_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/sensor_server.h b/components/bt/esp_ble_mesh/mesh_models/server/include/sensor_server.h index de92a7ad5..79be44bb7 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/include/sensor_server.h +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/sensor_server.h @@ -246,4 +246,7 @@ typedef union { int bt_mesh_sensor_srv_init(struct bt_mesh_model *model, bool primary); int bt_mesh_sensor_setup_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_sensor_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_sensor_setup_srv_deinit(struct bt_mesh_model *model, bool primary); + #endif /* _SENSOR_SERVER_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/server_common.h b/components/bt/esp_ble_mesh/mesh_models/server/include/server_common.h index d1a313ee7..5647c0ea5 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/include/server_common.h +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/server_common.h @@ -17,10 +17,7 @@ #include #include - -#include "mesh_buf.h" #include "mesh_access.h" -#include "mesh_kernel.h" #define BLE_MESH_SERVER_RSP_MAX_LEN 384 @@ -116,6 +113,7 @@ int bt_mesh_server_get_optional(struct bt_mesh_model *model, bool *optional); void bt_mesh_server_alloc_ctx(struct k_work *work); +void bt_mesh_server_free_ctx(struct k_work *work); bool bt_mesh_is_server_recv_last_msg(struct bt_mesh_last_msg_info *last, u8_t tid, u16_t src, u16_t dst, s64_t *now); diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/state_binding.h b/components/bt/esp_ble_mesh/mesh_models/server/include/state_binding.h index 2d75ff6c9..18ee04ec7 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/include/state_binding.h +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/state_binding.h @@ -9,6 +9,8 @@ #ifndef _STATE_BINDING_H_ #define _STATE_BINDING_H_ +#include "mesh_access.h" + typedef enum { GENERIC_ONOFF_STATE, GENERIC_LEVEL_STATE, diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/state_transition.h b/components/bt/esp_ble_mesh/mesh_models/server/include/state_transition.h index e9a833572..055192d99 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/include/state_transition.h +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/state_transition.h @@ -11,6 +11,7 @@ #include "server_common.h" #include "generic_server.h" +#include "sensor_server.h" #include "lighting_server.h" #include "time_scene_server.h" diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/time_scene_server.h b/components/bt/esp_ble_mesh/mesh_models/server/include/time_scene_server.h index 846677699..c311da65f 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/include/time_scene_server.h +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/time_scene_server.h @@ -15,8 +15,6 @@ #ifndef _TIME_SCENE_SERVER_H_ #define _TIME_SCENE_SERVER_H_ -#include "mesh_slist.h" -#include "mesh_kernel.h" #include "server_common.h" /** @@ -390,4 +388,11 @@ int bt_mesh_scene_setup_srv_init(struct bt_mesh_model *model, bool primary); int bt_mesh_scheduler_srv_init(struct bt_mesh_model *model, bool primary); int bt_mesh_scheduler_setup_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_time_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_time_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_scene_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_scene_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_scheduler_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_scheduler_setup_srv_deinit(struct bt_mesh_model *model, bool primary); + #endif /* _TIME_SCENE_SERVER_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/lighting_server.c b/components/bt/esp_ble_mesh/mesh_models/server/lighting_server.c index 9948d8d24..a63de218c 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/lighting_server.c +++ b/components/bt/esp_ble_mesh/mesh_models/server/lighting_server.c @@ -6,44 +6,38 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include -#include #include -#include "osi/mutex.h" - -#include "mesh_types.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" -#include "mesh.h" #include "access.h" +#include "transport.h" #include "model_opcode.h" - -#include "server_common.h" -#include "state_binding.h" #include "state_transition.h" #include "device_property.h" #include "btc_ble_mesh_lighting_model.h" -static osi_mutex_t light_server_mutex; +static bt_mesh_mutex_t light_server_lock; static void bt_mesh_light_server_mutex_new(void) { - if (!light_server_mutex) { - osi_mutex_new(&light_server_mutex); - __ASSERT(light_server_mutex, "%s, fail", __func__); + if (!light_server_lock.mutex) { + bt_mesh_mutex_create(&light_server_lock); } } +static void bt_mesh_light_server_mutex_free(void) +{ + bt_mesh_mutex_free(&light_server_lock); +} + void bt_mesh_light_server_lock(void) { - osi_mutex_lock(&light_server_mutex, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_mutex_lock(&light_server_lock); } void bt_mesh_light_server_unlock(void) { - osi_mutex_unlock(&light_server_mutex); + bt_mesh_mutex_unlock(&light_server_lock); } /* message handlers (Start) */ @@ -146,7 +140,7 @@ static void light_lightness_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_lightness_srv *srv = model->user_data; - u16_t opcode; + u16_t opcode = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -223,10 +217,10 @@ static void light_lightness_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_lightness_srv *srv = model->user_data; - u8_t tid, trans_time, delay; - bool optional; - u16_t actual; - s64_t now; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + u16_t actual = 0U; + s64_t now = 0; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -335,10 +329,10 @@ static void light_lightness_linear_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_lightness_srv *srv = model->user_data; - u8_t tid, trans_time, delay; - bool optional; - u16_t linear; - s64_t now; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + u16_t linear = 0U; + s64_t now = 0; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -431,7 +425,7 @@ static void light_lightness_default_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_lightness_setup_srv *srv = model->user_data; - u16_t lightness; + u16_t lightness = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -473,7 +467,7 @@ static void light_lightness_range_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_lightness_setup_srv *srv = model->user_data; - u16_t range_min, range_max; + u16_t range_min = 0U, range_max = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -647,7 +641,7 @@ static void light_ctl_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_server_rsp_ctrl *rsp_ctrl = NULL; - u16_t opcode; + u16_t opcode = 0U; if (model->user_data == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -753,11 +747,11 @@ static void light_ctl_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_ctl_srv *srv = model->user_data; - u16_t lightness, temperature; - u8_t tid, trans_time, delay; - s16_t delta_uv; - bool optional; - s64_t now; + u16_t lightness = 0U, temperature = 0U; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + s16_t delta_uv = 0; + bool optional = false; + s64_t now = 0; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -872,8 +866,8 @@ static void light_ctl_default_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_ctl_setup_srv *srv = model->user_data; - u16_t lightness, temperature; - s16_t delta_uv; + u16_t lightness = 0U, temperature = 0U; + s16_t delta_uv = 0; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -936,7 +930,7 @@ static void light_ctl_temp_range_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_ctl_setup_srv *srv = model->user_data; - u16_t min, max; + u16_t min = 0U, max = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1002,11 +996,11 @@ static void light_ctl_temp_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_ctl_temp_srv *srv = model->user_data; - u8_t tid, trans_time, delay; - u16_t temperature; - s16_t delta_uv; - bool optional; - s64_t now; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + u16_t temperature = 0U; + s16_t delta_uv = 0; + bool optional = false; + s64_t now = 0; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1233,7 +1227,7 @@ static void light_hsl_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_server_rsp_ctrl *rsp_ctrl = NULL; - u16_t opcode; + u16_t opcode = 0U; if (model->user_data == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1362,10 +1356,10 @@ static void light_hsl_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_hsl_srv *srv = model->user_data; - u16_t lightness, hue, saturation; - u8_t tid, trans_time, delay; - bool optional; - s64_t now; + u16_t lightness = 0U, hue = 0U, saturation = 0U; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + s64_t now = 0; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1480,7 +1474,7 @@ static void light_hsl_default_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_hsl_setup_srv *srv = model->user_data; - u16_t lightness, hue, saturation; + u16_t lightness = 0U, hue = 0U, saturation = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1540,7 +1534,7 @@ static void light_hsl_range_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_hsl_setup_srv *srv = model->user_data; - u16_t hue_min, hue_max, saturation_min, saturation_max; + u16_t hue_min = 0U, hue_max = 0U, saturation_min = 0U, saturation_max = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1605,10 +1599,10 @@ static void light_hsl_hue_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_hsl_hue_srv *srv = model->user_data; - u8_t tid, trans_time, delay; - bool optional; - u16_t hue; - s64_t now; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + u16_t hue = 0U; + s64_t now = 0; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1706,10 +1700,10 @@ static void light_hsl_sat_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_hsl_sat_srv *srv = model->user_data; - u8_t tid, trans_time, delay; - u16_t saturation; - bool optional; - s64_t now; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + u16_t saturation = 0U; + bool optional = false; + s64_t now = 0; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1905,7 +1899,7 @@ static void light_xyl_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_xyl_srv *srv = model->user_data; - u16_t opcode; + u16_t opcode = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1979,10 +1973,10 @@ static void light_xyl_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_xyl_srv *srv = model->user_data; - u8_t tid, trans_time, delay; - u16_t lightness, x, y; - bool optional; - s64_t now; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + u16_t lightness = 0U, x = 0U, y = 0U; + bool optional = false; + s64_t now = 0; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -2097,7 +2091,7 @@ static void light_xyl_default_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_xyl_setup_srv *srv = model->user_data; - u16_t lightness, x, y; + u16_t lightness = 0U, x = 0U, y = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -2157,7 +2151,7 @@ static void light_xyl_range_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_xyl_setup_srv *srv = model->user_data; - u16_t x_min, x_max, y_min, y_max; + u16_t x_min = 0U, x_max = 0U, y_min = 0U, y_max = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -2282,7 +2276,7 @@ static void light_lc_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_lc_srv *srv = model->user_data; - u16_t opcode; + u16_t opcode = 0U; if (srv == NULL || srv->lc == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -2333,7 +2327,7 @@ static void light_lc_mode_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_lc_srv *srv = model->user_data; - u8_t mode; + u8_t mode = 0U; if (srv == NULL || srv->lc == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -2377,7 +2371,7 @@ static void light_lc_om_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_lc_srv *srv = model->user_data; - u8_t om; + u8_t om = 0U; if (srv == NULL || srv->lc == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -2421,10 +2415,10 @@ static void light_lc_light_onoff_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_lc_srv *srv = model->user_data; - u8_t tid, trans_time, delay; - bool optional; - u8_t onoff; - s64_t now; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + u8_t onoff = 0U; + s64_t now = 0; if (srv == NULL || srv->lc == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -2539,8 +2533,8 @@ static void light_lc_sensor_status(struct bt_mesh_model *model, */ struct bt_mesh_light_lc_srv *srv = model->user_data; bt_mesh_light_server_state_change_t change = {0}; - u16_t mpid, prop_id; - u8_t length; + u16_t mpid = 0U, prop_id = 0U; + u8_t length = 0U; if (srv == NULL || srv->lc == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -2736,7 +2730,7 @@ static void send_light_lc_prop_status(struct bt_mesh_model *model, { struct net_buf_simple *msg = NULL; u8_t length = 1 + 2 + 4; - u8_t *prop_val; + u8_t *prop_val = NULL; prop_val = get_light_lc_prop_val(model, prop_id); if (prop_val == NULL) { @@ -2775,7 +2769,7 @@ static void light_lc_prop_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_lc_setup_srv *srv = model->user_data; - u16_t prop_id; + u16_t prop_id = 0U; if (srv == NULL || srv->lc == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -2807,8 +2801,8 @@ static void light_lc_prop_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct bt_mesh_light_lc_setup_srv *srv = model->user_data; - u8_t *prop_val, expect_len; - u16_t prop_id; + u8_t *prop_val = NULL, expect_len = 0U; + u16_t prop_id = 0U; if (srv == NULL || srv->lc == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -3331,3 +3325,229 @@ int bt_mesh_light_lc_setup_srv_init(struct bt_mesh_model *model, bool primary) } return light_server_init(model); } + +static int light_server_deinit(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, No Light Server context provided, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: { + struct bt_mesh_light_lightness_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light Lightness State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->actual_transition.timer.work); + bt_mesh_server_free_ctx(&srv->linear_transition.timer.work); + k_delayed_work_free(&srv->actual_transition.timer); + k_delayed_work_free(&srv->linear_transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: { + struct bt_mesh_light_ctl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light CTL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: { + struct bt_mesh_light_ctl_temp_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light CTL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: { + struct bt_mesh_light_hsl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light HSL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: { + struct bt_mesh_light_hsl_hue_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light HSL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: { + struct bt_mesh_light_hsl_sat_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light HSL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_XYL_SRV: { + struct bt_mesh_light_xyl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light xyL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_LC_SRV: { + struct bt_mesh_light_lc_srv *srv = model->user_data; + if (srv->lc == NULL) { + BT_ERR("%s, NULL Light LC State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + default: + BT_WARN("%s, Unknown Light Server Model, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + bt_mesh_light_server_mutex_free(); + + return 0; +} + +int bt_mesh_light_lightness_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light Lightness Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_lightness_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_server_deinit(model); +} + +int bt_mesh_light_ctl_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light CTL Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_ctl_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_server_deinit(model); +} + +int bt_mesh_light_ctl_temp_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light CTL Temperature Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_hsl_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light HSL Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_hsl_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_server_deinit(model); +} + +int bt_mesh_light_hsl_hue_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light HSL Hue Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_hsl_sat_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light HSL Saturation Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_xyl_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light xyL Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_xyl_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_server_deinit(model); +} + +int bt_mesh_light_lc_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light LC Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_lc_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light LC Setup Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} diff --git a/components/bt/esp_ble_mesh/mesh_models/server/sensor_server.c b/components/bt/esp_ble_mesh/mesh_models/server/sensor_server.c index a28930f75..d4b0a432a 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/sensor_server.c +++ b/components/bt/esp_ble_mesh/mesh_models/server/sensor_server.c @@ -12,22 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include #include -#include "mesh_types.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" -#include "mesh.h" #include "access.h" -#include "model_opcode.h" #include "transport.h" - -#include "server_common.h" -#include "state_binding.h" +#include "model_opcode.h" #include "state_transition.h" -#include "sensor_server.h" #include "device_property.h" #include "btc_ble_mesh_sensor_model.h" @@ -44,8 +34,8 @@ static void send_sensor_descriptor_status(struct bt_mesh_model *model, struct bt_mesh_sensor_srv *srv = model->user_data; struct bt_mesh_sensor_state *state = NULL; struct net_buf_simple *msg = NULL; - u16_t total_len = 5; - u8_t i; + u16_t total_len = 5U; + int i; msg = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)); if (msg == NULL) { @@ -56,7 +46,7 @@ static void send_sensor_descriptor_status(struct bt_mesh_model *model, bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS); if (get_all == true) { - for (i = 0U; i < srv->state_count; i++) { + for (i = 0; i < srv->state_count; i++) { state = &srv->states[i]; if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID) { total_len += SENSOR_DESCRIPTOR_LEN; @@ -73,7 +63,7 @@ static void send_sensor_descriptor_status(struct bt_mesh_model *model, } } } else { - for (i = 0U; i < srv->state_count; i++) { + for (i = 0; i < srv->state_count; i++) { state = &srv->states[i]; if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && state->sensor_property_id == prop_id) { @@ -104,8 +94,8 @@ static void send_sensor_data_status(struct bt_mesh_model *model, struct bt_mesh_sensor_srv *srv = model->user_data; struct bt_mesh_sensor_state *state = NULL; struct net_buf_simple *msg = NULL; - u16_t total_len = 5; - u8_t i; + u16_t total_len = 5U; + int i; msg = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)); if (msg == NULL) { @@ -116,7 +106,7 @@ static void send_sensor_data_status(struct bt_mesh_model *model, bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_STATUS); if (get_all == true) { - for (i = 0U; i < srv->state_count; i++) { + for (i = 0; i < srv->state_count; i++) { state = &srv->states[i]; if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID) { u8_t mpid_len = (state->sensor_data.format == SENSOR_DATA_FORMAT_A) ? @@ -142,7 +132,7 @@ static void send_sensor_data_status(struct bt_mesh_model *model, } } } else { - for (i = 0U; i < srv->state_count; i++) { + for (i = 0; i < srv->state_count; i++) { state = &srv->states[i]; if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && state->sensor_property_id == prop_id) { @@ -183,10 +173,10 @@ static void send_sensor_cadence_status(struct bt_mesh_model *model, struct bt_mesh_sensor_setup_srv *srv = model->user_data; struct bt_mesh_sensor_state *state = NULL; struct net_buf_simple *msg = NULL; - u16_t length = 0; - u8_t i; + u16_t length = 0U; + int i; - for (i = 0U; i < srv->state_count; i++) { + for (i = 0; i < srv->state_count; i++) { state = &srv->states[i]; if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && state->sensor_property_id == prop_id && state->cadence) { @@ -285,8 +275,8 @@ static void send_sensor_settings_status(struct bt_mesh_model *model, struct bt_mesh_sensor_state *state = NULL; struct sensor_setting *item = NULL; struct net_buf_simple *msg = NULL; - u16_t total_len = 7; - u8_t i, j; + u16_t total_len = 7U; + int i, j; msg = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)); if (msg == NULL) { @@ -297,11 +287,11 @@ static void send_sensor_settings_status(struct bt_mesh_model *model, bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS); net_buf_simple_add_le16(msg, prop_id); - for (i = 0U; i < srv->state_count; i++) { + for (i = 0; i < srv->state_count; i++) { state = &srv->states[i]; if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && state->sensor_property_id == prop_id) { - for (j = 0U; j < state->setting_count; j++) { + for (j = 0; j < state->setting_count; j++) { item = &state->settings[j]; if (item->property_id != INVALID_SENSOR_SETTING_PROPERTY_ID) { total_len += SENSOR_SETTING_PROPERTY_ID_LEN; @@ -330,13 +320,13 @@ static struct sensor_setting *find_sensor_setting(struct bt_mesh_model *model, struct bt_mesh_sensor_setup_srv *srv = model->user_data; struct bt_mesh_sensor_state *state = NULL; struct sensor_setting *item = NULL; - u8_t i, j; + int i, j; - for (i = 0U; i < srv->state_count; i++) { + for (i = 0; i < srv->state_count; i++) { state = &srv->states[i]; if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && state->sensor_property_id == prop_id) { - for (j = 0U; j < state->setting_count; j++) { + for (j = 0; j < state->setting_count; j++) { item = &state->settings[j]; if (item->property_id != INVALID_SENSOR_SETTING_PROPERTY_ID && item->property_id == set_prop_id) { @@ -355,7 +345,7 @@ static void send_sensor_setting_status(struct bt_mesh_model *model, { struct sensor_setting *item = NULL; struct net_buf_simple *msg = NULL; - u16_t length; + u16_t length = 0U; item = find_sensor_setting(model, prop_id, set_prop_id); if (item) { @@ -425,10 +415,10 @@ static void send_sensor_column_status(struct bt_mesh_model *model, struct bt_mesh_sensor_state *state = NULL; struct net_buf_simple *msg = NULL; bool optional = false; - u16_t length = 0; - u8_t i; + u16_t length = 0U; + int i; - for (i = 0U; i < srv->state_count; i++) { + for (i = 0; i < srv->state_count; i++) { state = &srv->states[i]; if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && state->sensor_property_id == prop_id) { @@ -486,10 +476,10 @@ static void send_sensor_series_status(struct bt_mesh_model *model, struct bt_mesh_sensor_state *state = NULL; struct net_buf_simple *msg = NULL; bool optional = false; - u16_t length; - u8_t i; + u16_t length = 0U; + int i; - for (i = 0U; i < srv->state_count; i++) { + for (i = 0; i < srv->state_count; i++) { state = &srv->states[i]; if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && state->sensor_property_id == prop_id) { @@ -704,9 +694,9 @@ static void sensor_cadence_set(struct bt_mesh_model *model, struct bt_mesh_sensor_state *state = NULL; struct bt_mesh_model *sensor_model = NULL; struct bt_mesh_elem *element = NULL; - u16_t prop_id, trigger_len; - u8_t val, divisor; - u8_t i; + u16_t prop_id = 0U, trigger_len = 0U; + u8_t val = 0U, divisor = 0U; + int i; if (srv == NULL || srv->state_count == 0U || srv->states == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -729,7 +719,7 @@ static void sensor_cadence_set(struct bt_mesh_model *model, return; } - for (i = 0U; i < srv->state_count; i++) { + for (i = 0; i < srv->state_count; i++) { state = &srv->states[i]; if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && state->sensor_property_id == prop_id) { @@ -846,7 +836,7 @@ static void update_sensor_periodic_pub(struct bt_mesh_model *model, u16_t prop_i { struct bt_mesh_sensor_state *state = NULL; struct bt_mesh_sensor_srv *srv = NULL; - u8_t i; + int i; if (model->id != BLE_MESH_MODEL_ID_SENSOR_SRV) { BT_ERR("%s, Not a Sensor Server Model", __func__); @@ -859,7 +849,7 @@ static void update_sensor_periodic_pub(struct bt_mesh_model *model, u16_t prop_i return; } - for (i = 0U; i < srv->state_count; i++) { + for (i = 0; i < srv->state_count; i++) { state = &srv->states[i]; if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && state->sensor_property_id == prop_id) { @@ -891,7 +881,7 @@ static void sensor_setting_set(struct bt_mesh_model *model, struct bt_mesh_sensor_setup_srv *srv = model->user_data; bt_mesh_sensor_server_state_change_t change = {0}; struct sensor_setting *item = NULL; - u16_t prop_id, set_prop_id; + u16_t prop_id = 0U, set_prop_id = 0U; if (srv == NULL || srv->state_count == 0U || srv->states == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -974,9 +964,9 @@ static int check_sensor_server_init(struct bt_mesh_sensor_state *state_start, { struct bt_mesh_sensor_state *state = NULL; struct sensor_setting *setting = NULL; - u8_t i, j; + int i, j; - for (i = 0U; i < state_count; i++) { + for (i = 0; i < state_count; i++) { state = &state_start[i]; if (state->sensor_property_id == INVALID_SENSOR_PROPERTY_ID) { BT_ERR("%s, Invalid Sensor Property ID 0x%04x", __func__, state->sensor_property_id); @@ -986,7 +976,7 @@ static int check_sensor_server_init(struct bt_mesh_sensor_state *state_start, BT_ERR("%s, Invalid Sensor Setting state", __func__); return -EINVAL; } - for (j = 0U; j < state->setting_count; j++) { + for (j = 0; j < state->setting_count; j++) { setting = &state->settings[j]; if (setting->property_id == INVALID_SENSOR_SETTING_PROPERTY_ID || setting->raw == NULL) { BT_ERR("%s, Invalid Sensor Setting state internal parameter", __func__); @@ -1086,3 +1076,33 @@ int bt_mesh_sensor_setup_srv_init(struct bt_mesh_model *model, bool primary) return sensor_server_init(model); } + +static int sensor_server_deinit(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, No Sensor Server context provided, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + return 0; +} + +int bt_mesh_sensor_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Sensor Server has no publication support", __func__); + return -EINVAL; + } + + return sensor_server_deinit(model); +} + +int bt_mesh_sensor_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Sensor Setup Server has no publication support", __func__); + return -EINVAL; + } + + return sensor_server_deinit(model); +} diff --git a/components/bt/esp_ble_mesh/mesh_models/server/server_common.c b/components/bt/esp_ble_mesh/mesh_models/server/server_common.c index 041cc3b00..ee698e2d7 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/server_common.c +++ b/components/bt/esp_ble_mesh/mesh_models/server/server_common.c @@ -12,22 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include -#include -#include "osi/allocator.h" - -#include "mesh_types.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" #include "mesh.h" #include "access.h" -#include "model_opcode.h" - -#include "server_common.h" -#include "state_binding.h" -#include "state_transition.h" +#include "mesh_common.h" +#include "generic_server.h" +#include "lighting_server.h" /** * According to Mesh Model Spec: @@ -74,7 +65,7 @@ u8_t bt_mesh_get_default_trans_time(struct bt_mesh_model *model) int bt_mesh_get_light_lc_trans_time(struct bt_mesh_model *model, u8_t *trans_time) { struct bt_mesh_light_lc_srv *srv = NULL; - u32_t value; + u32_t value = 0U; if (model == NULL || trans_time == NULL) { BT_ERR("%s, Invalid parameter", __func__); @@ -194,11 +185,20 @@ void bt_mesh_server_alloc_ctx(struct k_work *work) */ __ASSERT(work, "%s, Invalid parameter", __func__); if (!work->_reserved) { - work->_reserved = osi_calloc(sizeof(struct bt_mesh_msg_ctx)); + work->_reserved = bt_mesh_calloc(sizeof(struct bt_mesh_msg_ctx)); __ASSERT(work->_reserved, "%s, Failed to allocate memory", __func__); } } +void bt_mesh_server_free_ctx(struct k_work *work) +{ + __ASSERT(work, "%s, Invalid parameter", __func__); + if (work->_reserved) { + bt_mesh_free(work->_reserved); + work->_reserved = NULL; + } +} + bool bt_mesh_is_server_recv_last_msg(struct bt_mesh_last_msg_info *last, u8_t tid, u16_t src, u16_t dst, s64_t *now) { diff --git a/components/bt/esp_ble_mesh/mesh_models/server/state_binding.c b/components/bt/esp_ble_mesh/mesh_models/server/state_binding.c index d13623232..45dbadffd 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/state_binding.c +++ b/components/bt/esp_ble_mesh/mesh_models/server/state_binding.c @@ -6,28 +6,17 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include -#include #include -#include - -#include "mesh_types.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" -#include "mesh.h" #include "model_opcode.h" -#include "server_common.h" #include "state_binding.h" #include "state_transition.h" -#include "generic_server.h" -#include "lighting_server.h" #define MINDIFF (2.25e-308) static float bt_mesh_sqrt(float square) { - float root, last, diff; + float root = 0.0, last = 0.0, diff = 0.0; root = square / 3.0; diff = 1; diff --git a/components/bt/esp_ble_mesh/mesh_models/server/state_transition.c b/components/bt/esp_ble_mesh/mesh_models/server/state_transition.c index da05cec19..38c9f94fe 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/state_transition.c +++ b/components/bt/esp_ble_mesh/mesh_models/server/state_transition.c @@ -6,22 +6,8 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include -#include -#include - -#include "mesh_types.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" -#include "mesh.h" #include "model_opcode.h" - -#include "server_common.h" -#include "state_binding.h" #include "state_transition.h" -#include "generic_server.h" -#include "lighting_server.h" -#include "time_scene_server.h" #include "btc_ble_mesh_generic_model.h" #include "btc_ble_mesh_lighting_model.h" @@ -32,9 +18,9 @@ void bt_mesh_server_calc_remain_time(struct bt_mesh_state_transition *transition) { - u8_t steps, resolution; - s32_t duration_remainder; - s64_t now; + u8_t steps = 0U, resolution = 0U; + s32_t duration_remainder = 0; + s64_t now = 0; if (transition->just_started) { transition->remain_time = transition->trans_time; @@ -71,7 +57,7 @@ void bt_mesh_server_calc_remain_time(struct bt_mesh_state_transition *transition static void tt_values_calculator(struct bt_mesh_state_transition *transition) { - u8_t steps_multiplier, resolution; + u8_t steps_multiplier = 0U, resolution = 0U; resolution = (transition->trans_time >> 6); steps_multiplier = (transition->trans_time & 0x3F); diff --git a/components/bt/esp_ble_mesh/mesh_models/server/time_scene_server.c b/components/bt/esp_ble_mesh/mesh_models/server/time_scene_server.c index fec69b166..78caa680f 100644 --- a/components/bt/esp_ble_mesh/mesh_models/server/time_scene_server.c +++ b/components/bt/esp_ble_mesh/mesh_models/server/time_scene_server.c @@ -12,45 +12,37 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include #include -#include "osi/mutex.h" - -#include "mesh_types.h" -#include "mesh_kernel.h" -#include "mesh_trace.h" -#include "mesh.h" #include "access.h" -#include "model_opcode.h" #include "transport.h" - -#include "server_common.h" -#include "state_binding.h" +#include "model_opcode.h" #include "state_transition.h" -#include "time_scene_server.h" #include "btc_ble_mesh_time_scene_model.h" -static osi_mutex_t time_scene_server_mutex; +static bt_mesh_mutex_t time_scene_server_lock; static void bt_mesh_time_scene_server_mutex_new(void) { - if (!time_scene_server_mutex) { - osi_mutex_new(&time_scene_server_mutex); - __ASSERT(time_scene_server_mutex, "%s, fail", __func__); + if (!time_scene_server_lock.mutex) { + bt_mesh_mutex_create(&time_scene_server_lock); } } +static void bt_mesh_time_scene_server_mutex_free(void) +{ + bt_mesh_mutex_free(&time_scene_server_lock); +} + void bt_mesh_time_scene_server_lock(void) { - osi_mutex_lock(&time_scene_server_mutex, OSI_MUTEX_MAX_TIMEOUT); + bt_mesh_mutex_lock(&time_scene_server_lock); } void bt_mesh_time_scene_server_unlock(void) { - osi_mutex_unlock(&time_scene_server_mutex); + bt_mesh_mutex_unlock(&time_scene_server_lock); } /* message handlers (Start) */ @@ -168,8 +160,8 @@ static void time_get(struct bt_mesh_model *model, { struct bt_mesh_server_rsp_ctrl *rsp_ctrl = NULL; u8_t zero[5] = {0}; - u16_t opcode, val; - u8_t prev_ttl; + u16_t opcode = 0U, val = 0U; + u8_t prev_ttl = 0U; if (model->user_data == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -316,8 +308,8 @@ static void time_set(struct bt_mesh_model *model, { struct bt_mesh_time_setup_srv *srv = model->user_data; bt_mesh_time_scene_server_state_change_t change = {0}; - u16_t opcode, val; - u8_t role; + u16_t opcode = 0U, val = 0U; + u8_t role = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -501,8 +493,8 @@ static void send_scene_register_status(struct bt_mesh_model *model, struct bt_mesh_scene_setup_srv *srv = model->user_data; struct scene_register *scene = NULL; struct net_buf_simple *msg = NULL; - u16_t total_len = 9; - u16_t i; + u16_t total_len = 9U; + int i; if (ctx == NULL && publish == false) { BT_ERR("%s, Invalid parameter", __func__); @@ -526,7 +518,7 @@ static void send_scene_register_status(struct bt_mesh_model *model, net_buf_simple_add_u8(msg, status_code); net_buf_simple_add_le16(msg, srv->state->current_scene); - for (i = 0U; i < srv->state->scene_count; i++) { + for (i = 0; i < srv->state->scene_count; i++) { scene = &srv->state->scenes[i]; if (scene->scene_number != INVALID_SCENE_NUMBER) { total_len += SCENE_NUMBER_LEN; @@ -602,11 +594,11 @@ static void scene_recall(struct bt_mesh_model *model, { struct bt_mesh_scene_srv *srv = model->user_data; struct scene_register *scene = NULL; - u8_t tid, trans_time, delay; - u16_t scene_number; - bool optional; - s64_t now; - u16_t i; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + u16_t scene_number = 0U; + bool optional = false; + s64_t now = 0; + int i; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -637,7 +629,7 @@ static void scene_recall(struct bt_mesh_model *model, return; } - for (i = 0U; i < srv->state->scene_count; i++) { + for (i = 0; i < srv->state->scene_count; i++) { scene = &srv->state->scenes[i]; if (scene->scene_number == scene_number) { break; @@ -734,8 +726,8 @@ static void scene_action(struct bt_mesh_model *model, { struct bt_mesh_scene_setup_srv *srv = model->user_data; struct scene_register *scene = NULL; - u16_t scene_number; - u16_t i; + u16_t scene_number = 0U; + int i; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -760,7 +752,7 @@ static void scene_action(struct bt_mesh_model *model, return; } /* Try to find a matching Scene Number */ - for (i = 0U; i < srv->state->scene_count; i++) { + for (i = 0; i < srv->state->scene_count; i++) { scene = &srv->state->scenes[i]; if (scene->scene_number == scene_number) { srv->state->status_code = SCENE_SUCCESS; @@ -771,7 +763,7 @@ static void scene_action(struct bt_mesh_model *model, /* Try to find a unset entry if no matching Scene Number is found */ if (i == srv->state->scene_count) { BT_DBG("%s, No matching Scene Number 0x%04x found", __func__, scene_number); - for (i = 0U; i < srv->state->scene_count; i++) { + for (i = 0; i < srv->state->scene_count; i++) { scene = &srv->state->scenes[i]; if (scene->scene_number == INVALID_SCENE_NUMBER) { scene->scene_number = scene_number; @@ -825,7 +817,7 @@ static void scene_action(struct bt_mesh_model *model, BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); return; } - for (i = 0U; i < srv->state->scene_count; i++) { + for (i = 0; i < srv->state->scene_count; i++) { scene = &srv->state->scenes[i]; if (scene->scene_number == scene_number) { scene->scene_number = INVALID_SCENE_NUMBER; @@ -863,7 +855,7 @@ static void scene_action(struct bt_mesh_model *model, break; } } - if (i == 0U) { + if (i == 0) { /* A value of 0x0000 when no scene is active */ srv->state->current_scene = INVALID_SCENE_NUMBER; } @@ -934,10 +926,10 @@ static void scene_action(struct bt_mesh_model *model, static u16_t get_schedule_reg_bit(struct bt_mesh_scheduler_state *state) { - u16_t val = 0; - u8_t i; + u16_t val = 0U; + int i; - for (i = 0U; i < state->schedule_count; i++) { + for (i = 0; i < state->schedule_count; i++) { if (state->schedules[i].in_use) { val |= (1 << i); } @@ -949,7 +941,7 @@ static u16_t get_schedule_reg_bit(struct bt_mesh_scheduler_state *state) static u64_t get_schedule_reg_state(struct bt_mesh_scheduler_state *state, u8_t index) { struct schedule_register *reg = &state->schedules[index]; - u64_t val; + u64_t val = 0U; val = ((u64_t)(reg->year) << 4) | index; val |= ((u64_t)(reg->day) << 23) | ((u64_t)(reg->month) << 11); @@ -965,7 +957,7 @@ static void send_scheduler_act_status(struct bt_mesh_model *model, u8_t index) { NET_BUF_SIMPLE_DEFINE(msg, 1 + 10 + BLE_MESH_SERVER_TRANS_MIC_SIZE); - u64_t value; + u64_t value = 0U; bt_mesh_model_msg_init(&msg, BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS); switch (model->id) { @@ -1059,9 +1051,10 @@ static void scheduler_act_set(struct bt_mesh_model *model, * queue. */ struct bt_mesh_scheduler_setup_srv *srv = model->user_data; - u8_t index, year, day, hour, minute, second, day_of_week, action, trans_time; - u16_t month, scene_number; - u64_t value; + u8_t index = 0U, year = 0U, day = 0U, hour = 0U, minute = 0U, + second = 0U, day_of_week = 0U, action = 0U, trans_time = 0U; + u16_t month = 0U, scene_number = 0U; + u64_t value = 0U; if (srv == NULL || srv->state == NULL) { BT_ERR("%s, Invalid model user_data", __func__); @@ -1213,14 +1206,14 @@ const struct bt_mesh_model_op scheduler_setup_srv_op[] = { static int check_scene_server_init(struct bt_mesh_scenes_state *state) { - u16_t i; + int i; if (state->scene_count == 0U || state->scenes == NULL) { BT_ERR("%s, Invalid Scene state", __func__); return -EINVAL; } - for (i = 0U; i < state->scene_count; i++) { + for (i = 0; i < state->scene_count; i++) { if (state->scenes[i].scene_value == NULL) { BT_ERR("%s, Invalid Scene value, index %d", __func__, i); return -EINVAL; @@ -1421,4 +1414,87 @@ int bt_mesh_scheduler_setup_srv_init(struct bt_mesh_model *model, bool primary) /* Just give a warning here, continue with the initialization */ } return time_scene_server_init(model); -} \ No newline at end of file +} + +static int time_scene_server_deinit(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, No Time Scene Server context provided, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_SCENE_SRV: { + struct bt_mesh_scene_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Scene State", __func__); + return -EINVAL; + } + if (check_scene_server_init(srv->state)) { + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + default: + BT_WARN("%s, Unknown Time Scene Server Model, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + bt_mesh_time_scene_server_mutex_free(); + + return 0; +} + +int bt_mesh_time_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Time Server has no publication support", __func__); + return -EINVAL; + } + + return time_scene_server_deinit(model); +} + +int bt_mesh_time_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub) { + BT_ERR("%s, Time Setup Server shall not support publication", __func__); + return -EINVAL; + } + + return time_scene_server_deinit(model); +} + +int bt_mesh_scene_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Scene Server has no publication support", __func__); + return -EINVAL; + } + + return time_scene_server_deinit(model); +} + +int bt_mesh_scene_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return time_scene_server_deinit(model); +} + +int bt_mesh_scheduler_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Scheduler Server has no publication support", __func__); + return -EINVAL; + } + + return time_scene_server_deinit(model); +} + +int bt_mesh_scheduler_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return time_scene_server_deinit(model); +} diff --git a/docs/en/api-guides/esp-ble-mesh/ble-mesh-architecture.rst b/docs/en/api-guides/esp-ble-mesh/ble-mesh-architecture.rst index fdeb5f410..da2af2d8a 100644 --- a/docs/en/api-guides/esp-ble-mesh/ble-mesh-architecture.rst +++ b/docs/en/api-guides/esp-ble-mesh/ble-mesh-architecture.rst @@ -276,8 +276,8 @@ Specific files that implement functions of Provisioner are shown in Table 2.3: - ESP-BLE-MESH Provisioner provisioning (PB-ADV & PB-GATT) * - :component_file:`provisioner_proxy.c ` - ESP-BLE-MESH Proxy Client related functionalities - * - :component_file:`provisioner_beacon.c ` - - ESP-BLE-MESH Provisioner receives Unprovisioned Device Beacon + * - :component_file:`provisioner_main.c ` + - ESP-BLE-MESH Provisioner networking related functionalities 2.1.3 Mesh Models Implementation ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/sdkconfig.defaults index c017190ad..b56f278ee 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/sdkconfig.defaults @@ -1,48 +1,23 @@ # Override some defaults so BT stack is enabled # by default in this example -CONFIG_ESP_CONSOLE_UART_BAUDRATE=921600 -CONFIG_ESPTOOLPY_BAUD_921600B=y -CONFIG_ESPTOOLPY_MONITOR_BAUD_921600B=y CONFIG_BT_ENABLED=y CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n CONFIG_BTDM_CTRL_MODE_BTDM=n CONFIG_BTDM_MODEM_SLEEP=n -CONFIG_BTDM_BLE_SCAN_DUPL=y -CONFIG_BTDM_SCAN_DUPL_TYPE=2 CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y -CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE=200 CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y -CONFIG_BTDM_MESH_DUPL_SCAN_CACHE_SIZE=200 -CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED=y -CONFIG_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP=y -CONFIG_BT_GATTS_ENABLE=y CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y -CONFIG_BLE_MESH=y -CONFIG_BLE_MESH_HCI_5_0=y -CONFIG_BLE_MESH_USE_DUPLICATE_SCAN=y -CONFIG_BLE_MESH_NODE=y -CONFIG_BLE_MESH_PROV=y -CONFIG_BLE_MESH_NET_BUF_POOL_USAGE=y -CONFIG_BLE_MESH_PROXY=y -CONFIG_BLE_MESH_PB_GATT=y -CONFIG_BLE_MESH_GATT_PROXY_SERVER=y -CONFIG_BLE_MESH_NODE_ID_TIMEOUT=60 -CONFIG_BLE_MESH_PROXY_FILTER_SIZE=1 -CONFIG_BLE_MESH_SUBNET_COUNT=1 -CONFIG_BLE_MESH_APP_KEY_COUNT=1 -CONFIG_BLE_MESH_MODEL_KEY_COUNT=1 -CONFIG_BLE_MESH_MODEL_GROUP_COUNT=1 -CONFIG_BLE_MESH_LABEL_COUNT=1 -CONFIG_BLE_MESH_CRPL=10 -CONFIG_BLE_MESH_MSG_CACHE_SIZE=10 -CONFIG_BLE_MESH_ADV_BUF_COUNT=60 -CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=6 -CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=1 -CONFIG_BLE_MESH_RX_SDU_MAX=384 -CONFIG_BLE_MESH_TX_SEG_MAX=32 -CONFIG_BLE_MESH_RELAY=y -CONFIG_BLE_MESH_LOW_POWER=n -CONFIG_BLE_MESH_FRIEND=n -CONFIG_BLE_MESH_CFG_CLI=y CONFIG_BT_BTU_TASK_STACK_SIZE=4512 + +# Override some defaults of ESP BLE Mesh +CONFIG_BLE_MESH=y +CONFIG_BLE_MESH_NODE=y +CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=10 +CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=10 +CONFIG_BLE_MESH_CFG_CLI=y + +CONFIG_ESP_CONSOLE_UART_BAUDRATE=921600 +CONFIG_ESPTOOLPY_BAUD_921600B=y +CONFIG_ESPTOOLPY_MONITOR_BAUD_921600B=y \ No newline at end of file diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/main/ble_mesh_register_provisioner_cmd.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/main/ble_mesh_register_provisioner_cmd.c index d36aed92f..740d992a0 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/main/ble_mesh_register_provisioner_cmd.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/main/ble_mesh_register_provisioner_cmd.c @@ -191,7 +191,7 @@ int ble_mesh_provisioner_get_node(int argc, char **argv) { uint16_t unicast_addr = 0; uint16_t i = 0; - struct bt_mesh_node_t *node_info; + esp_ble_mesh_node_t *node_info; ESP_LOGD(TAG, "enter %s\n", __func__); int nerrors = arg_parse(argc, argv, (void **) &provisioner_get_node); @@ -201,7 +201,7 @@ int ble_mesh_provisioner_get_node(int argc, char **argv) } arg_int_to_value(provisioner_get_node.unicast_addr, unicast_addr, "unicast address"); - node_info = bt_mesh_provisioner_get_node_info(unicast_addr); + node_info = esp_ble_mesh_provisioner_get_node_with_addr(unicast_addr); if (node_info == NULL) { return ESP_FAIL; @@ -224,7 +224,7 @@ int ble_mesh_provisioner_get_node(int argc, char **argv) int ble_mesh_provisioner_add_node(int argc, char **argv) { - struct bt_mesh_node_t node_info; + struct bt_mesh_node node_info; esp_err_t result; ESP_LOGD(TAG, " enter %s\n", __func__); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/sdkconfig.defaults index 2cfcfa14a..242c6d62c 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/sdkconfig.defaults @@ -5,38 +5,17 @@ CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n CONFIG_BTDM_CTRL_MODE_BTDM=n CONFIG_BTDM_MODEM_SLEEP=n -CONFIG_BTDM_BLE_SCAN_DUPL=y -CONFIG_BTDM_SCAN_DUPL_TYPE=2 CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y -CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE=200 CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y -CONFIG_BTDM_MESH_DUPL_SCAN_CACHE_SIZE=200 -CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED=y -CONFIG_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 + +# Override some defaults of ESP BLE Mesh CONFIG_BLE_MESH=y -CONFIG_BLE_MESH_HCI_5_0=y -CONFIG_BLE_MESH_USE_DUPLICATE_SCAN=y -CONFIG_BLE_MESH_PROV=y CONFIG_BLE_MESH_PROVISIONER=y -CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM=20 -CONFIG_BLE_MESH_MAX_PROV_NODES=20 +CONFIG_BLE_MESH_PB_GATT=y CONFIG_BLE_MESH_PBA_SAME_TIME=10 CONFIG_BLE_MESH_PBG_SAME_TIME=3 -CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT=3 -CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT=9 -CONFIG_BLE_MESH_PB_ADV=y -CONFIG_BLE_MESH_NET_BUF_POOL_USAGE=y -CONFIG_BLE_MESH_PB_GATT=y -CONFIG_BLE_MESH_GATT_PROXY_SERVER=y -CONFIG_BLE_MESH_RELAY=y -CONFIG_BLE_MESH_LOW_POWER=n -CONFIG_BLE_MESH_FRIEND=n -CONFIG_BLE_MESH_MSG_CACHE_SIZE=10 -CONFIG_BLE_MESH_ADV_BUF_COUNT=60 -CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=6 -CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=1 -CONFIG_BLE_MESH_RX_SDU_MAX=384 -CONFIG_BLE_MESH_TX_SEG_MAX=32 -CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=10 +CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=10 CONFIG_BLE_MESH_CFG_CLI=y -CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y +CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y \ No newline at end of file diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/sdkconfig.defaults index 9412f7d91..0e430712d 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/sdkconfig.defaults @@ -5,41 +5,17 @@ CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n CONFIG_BTDM_CTRL_MODE_BTDM=n CONFIG_BTDM_MODEM_SLEEP=n -CONFIG_BTDM_BLE_SCAN_DUPL=y -CONFIG_BTDM_SCAN_DUPL_TYPE=2 CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y -CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE=200 CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y -CONFIG_BTDM_MESH_DUPL_SCAN_CACHE_SIZE=200 -CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED=y -CONFIG_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP=y -CONFIG_BLE_MESH=y -CONFIG_BLE_MESH_HCI_5_0=y -CONFIG_BLE_MESH_USE_DUPLICATE_SCAN=y -CONFIG_BLE_MESH_PROV=y -CONFIG_BLE_MESH_PROVISIONER=y -CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM=10 -CONFIG_BLE_MESH_MAX_PROV_NODES=10 -CONFIG_BLE_MESH_MAX_STORED_NODES=10 -CONFIG_BLE_MESH_PBA_SAME_TIME=1 -CONFIG_BLE_MESH_PBG_SAME_TIME=1 -CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT=3 -CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT=3 -CONFIG_BLE_MESH_PB_ADV=y -CONFIG_BLE_MESH_NET_BUF_POOL_USAGE=y -CONFIG_BLE_MESH_PB_GATT=y -CONFIG_BLE_MESH_GATT_PROXY_SERVER=n -CONFIG_BLE_MESH_RELAY=y -CONFIG_BLE_MESH_LOW_POWER=n -CONFIG_BLE_MESH_FRIEND=n CONFIG_BT_BTU_TASK_STACK_SIZE=4512 -CONFIG_BLE_MESH_CFG_CLI=y -CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y + +# Override some defaults of ESP BLE Mesh +CONFIG_BLE_MESH=y +CONFIG_BLE_MESH_PROVISIONER=y +CONFIG_BLE_MESH_PBA_SAME_TIME=1 +CONFIG_BLE_MESH_PB_GATT=y CONFIG_BLE_MESH_ADV_BUF_COUNT=100 -CONFIG_BLE_MESH_CRPL=10 CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=10 CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=10 -CONFIG_BLE_MESH_RX_SDU_MAX=384 -CONFIG_BLE_MESH_TX_SEG_MAX=32 -CONFIG_BLE_MESH_NO_LOG=n -CONFIG_BLE_MESH_STACK_TRACE_LEVEL=1 +CONFIG_BLE_MESH_CFG_CLI=y +CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y \ No newline at end of file diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/sdkconfig.defaults index 970540d62..a2a2abb54 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/sdkconfig.defaults @@ -5,49 +5,21 @@ CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n CONFIG_BTDM_CTRL_MODE_BTDM=n CONFIG_BTDM_MODEM_SLEEP=n -CONFIG_BTDM_BLE_SCAN_DUPL=y -CONFIG_BTDM_SCAN_DUPL_TYPE=2 CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y -CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE=200 CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y -CONFIG_BTDM_MESH_DUPL_SCAN_CACHE_SIZE=200 -CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED=y -CONFIG_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP=y -CONFIG_BT_GATTS_ENABLE=y CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 + +# Override some defaults of ESP BLE Mesh CONFIG_BLE_MESH=y -CONFIG_BLE_MESH_HCI_5_0=y -CONFIG_BLE_MESH_USE_DUPLICATE_SCAN=y CONFIG_BLE_MESH_FAST_PROV=y -CONFIG_BLE_MESH_PROV=y -CONFIG_BLE_MESH_NODE=y -CONFIG_BLE_MESH_PROVISIONER=y -CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM=20 -CONFIG_BLE_MESH_MAX_STORED_NODES=10 CONFIG_BLE_MESH_MAX_PROV_NODES=6 CONFIG_BLE_MESH_PBA_SAME_TIME=3 -CONFIG_BLE_MESH_PBG_SAME_TIME=3 -CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT=3 -CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT=9 -CONFIG_BLE_MESH_PB_ADV=y -CONFIG_BLE_MESH_NET_BUF_POOL_USAGE=y -CONFIG_BLE_MESH_SUBNET_COUNT=2 -CONFIG_BLE_MESH_APP_KEY_COUNT=3 -CONFIG_BLE_MESH_MODEL_KEY_COUNT=3 -CONFIG_BLE_MESH_MODEL_GROUP_COUNT=3 CONFIG_BLE_MESH_PB_GATT=y -CONFIG_BLE_MESH_GATT_PROXY_SERVER=y -CONFIG_BLE_MESH_RELAY=y -CONFIG_BLE_MESH_LOW_POWER=n -CONFIG_BLE_MESH_FRIEND=n -CONFIG_BT_BTU_TASK_STACK_SIZE=4512 -CONFIG_BLE_MESH_CFG_CLI=y CONFIG_BLE_MESH_CRPL=60 CONFIG_BLE_MESH_MSG_CACHE_SIZE=60 CONFIG_BLE_MESH_ADV_BUF_COUNT=200 CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=10 CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=10 -CONFIG_BLE_MESH_RX_SDU_MAX=384 -CONFIG_BLE_MESH_TX_SEG_MAX=32 -CONFIG_BLE_MESH_NO_LOG=n -CONFIG_BLE_MESH_STACK_TRACE_LEVEL=0 +CONFIG_BLE_MESH_TRACE_LEVEL_ERROR=y +CONFIG_BLE_MESH_CFG_CLI=y \ No newline at end of file diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/sdkconfig.defaults index 6a5db41ee..805edf332 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/sdkconfig.defaults @@ -5,43 +5,15 @@ CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n CONFIG_BTDM_CTRL_MODE_BTDM=n CONFIG_BTDM_MODEM_SLEEP=n -CONFIG_BTDM_BLE_SCAN_DUPL=y -CONFIG_BTDM_SCAN_DUPL_TYPE=2 CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y -CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE=200 CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y -CONFIG_BTDM_MESH_DUPL_SCAN_CACHE_SIZE=200 -CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED=y -CONFIG_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP=y -CONFIG_BT_GATTS_ENABLE=y CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y -CONFIG_BLE_MESH=y -CONFIG_BLE_MESH_HCI_5_0=y -CONFIG_BLE_MESH_USE_DUPLICATE_SCAN=y -CONFIG_BLE_MESH_NODE=y -CONFIG_BLE_MESH_PROV=y -CONFIG_BLE_MESH_NET_BUF_POOL_USAGE=y -CONFIG_BLE_MESH_PROXY=y -CONFIG_BLE_MESH_PB_GATT=y -CONFIG_BLE_MESH_GATT_PROXY_SERVER=y -CONFIG_BLE_MESH_NODE_ID_TIMEOUT=60 -CONFIG_BLE_MESH_PROXY_FILTER_SIZE=1 -CONFIG_BLE_MESH_IV_UPDATE_TEST=n -CONFIG_BLE_MESH_SUBNET_COUNT=1 -CONFIG_BLE_MESH_APP_KEY_COUNT=1 -CONFIG_BLE_MESH_MODEL_KEY_COUNT=1 -CONFIG_BLE_MESH_MODEL_GROUP_COUNT=1 -CONFIG_BLE_MESH_LABEL_COUNT=1 -CONFIG_BLE_MESH_CRPL=10 -CONFIG_BLE_MESH_MSG_CACHE_SIZE=10 -CONFIG_BLE_MESH_ADV_BUF_COUNT=60 -CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=6 -CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=1 -CONFIG_BLE_MESH_RX_SDU_MAX=384 -CONFIG_BLE_MESH_TX_SEG_MAX=32 -CONFIG_BLE_MESH_RELAY=y -CONFIG_BLE_MESH_LOW_POWER=n -CONFIG_BLE_MESH_FRIEND=n -CONFIG_BLE_MESH_CFG_CLI=n -CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y CONFIG_BT_BTU_TASK_STACK_SIZE=4512 + +# Override some defaults of ESP BLE Mesh +CONFIG_BLE_MESH=y +CONFIG_BLE_MESH_NODE=y +CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=10 +CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=10 +CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.defaults index 0883ed0b6..978559069 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.defaults @@ -5,41 +5,14 @@ CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n CONFIG_BTDM_CTRL_MODE_BTDM=n CONFIG_BTDM_MODEM_SLEEP=n -CONFIG_BTDM_BLE_SCAN_DUPL=y -CONFIG_BTDM_SCAN_DUPL_TYPE=2 CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y -CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE=200 CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y -CONFIG_BTDM_MESH_DUPL_SCAN_CACHE_SIZE=200 -CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED=y -CONFIG_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP=y -CONFIG_BT_GATTS_ENABLE=y CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y -CONFIG_BLE_MESH=y -CONFIG_BLE_MESH_HCI_5_0=y -CONFIG_BLE_MESH_USE_DUPLICATE_SCAN=y -CONFIG_BLE_MESH_NODE=y -CONFIG_BLE_MESH_PROV=y -CONFIG_BLE_MESH_NET_BUF_POOL_USAGE=y -CONFIG_BLE_MESH_PROXY=y -CONFIG_BLE_MESH_PB_GATT=y -CONFIG_BLE_MESH_GATT_PROXY_SERVER=y -CONFIG_BLE_MESH_NODE_ID_TIMEOUT=60 -CONFIG_BLE_MESH_PROXY_FILTER_SIZE=1 -CONFIG_BLE_MESH_SUBNET_COUNT=1 -CONFIG_BLE_MESH_APP_KEY_COUNT=1 -CONFIG_BLE_MESH_MODEL_KEY_COUNT=1 -CONFIG_BLE_MESH_MODEL_GROUP_COUNT=1 -CONFIG_BLE_MESH_LABEL_COUNT=1 -CONFIG_BLE_MESH_CRPL=10 -CONFIG_BLE_MESH_MSG_CACHE_SIZE=10 -CONFIG_BLE_MESH_ADV_BUF_COUNT=60 -CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=6 -CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=1 -CONFIG_BLE_MESH_RX_SDU_MAX=384 -CONFIG_BLE_MESH_TX_SEG_MAX=32 -CONFIG_BLE_MESH_RELAY=y -CONFIG_BLE_MESH_LOW_POWER=n -CONFIG_BLE_MESH_FRIEND=n -CONFIG_BLE_MESH_CFG_CLI=y CONFIG_BT_BTU_TASK_STACK_SIZE=4512 + +# Override some defaults of ESP BLE Mesh +CONFIG_BLE_MESH=y +CONFIG_BLE_MESH_NODE=y +CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=10 +CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=10 \ No newline at end of file diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.defaults index cabb4af85..208ad6086 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.defaults @@ -5,38 +5,15 @@ CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n CONFIG_BTDM_CTRL_MODE_BTDM=n CONFIG_BTDM_MODEM_SLEEP=n -CONFIG_BTDM_BLE_SCAN_DUPL=y -CONFIG_BTDM_SCAN_DUPL_TYPE=2 CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y -CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE=200 CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y -CONFIG_BTDM_MESH_DUPL_SCAN_CACHE_SIZE=200 -CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED=y -CONFIG_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP=y -CONFIG_BLE_MESH=y -CONFIG_BLE_MESH_HCI_5_0=y -CONFIG_BLE_MESH_USE_DUPLICATE_SCAN=y -CONFIG_BLE_MESH_PROV=y -CONFIG_BLE_MESH_PROVISIONER=y -CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM=10 -CONFIG_BLE_MESH_MAX_STORED_NODES=10 -CONFIG_BLE_MESH_MAX_PROV_NODES=10 -CONFIG_BLE_MESH_PBA_SAME_TIME=3 -CONFIG_BLE_MESH_PBG_SAME_TIME=2 -CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT=3 -CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT=3 -CONFIG_BLE_MESH_PB_ADV=y -CONFIG_BLE_MESH_NET_BUF_POOL_USAGE=y -CONFIG_BLE_MESH_PB_GATT=y -CONFIG_BLE_MESH_GATT_PROXY_SERVER=n -CONFIG_BLE_MESH_RELAY=y -CONFIG_BLE_MESH_LOW_POWER=n -CONFIG_BLE_MESH_FRIEND=n -CONFIG_BLE_MESH_ADV_BUF_COUNT=60 -CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=6 -CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=6 -CONFIG_BLE_MESH_RX_SDU_MAX=384 -CONFIG_BLE_MESH_TX_SEG_MAX=32 CONFIG_BT_BTU_TASK_STACK_SIZE=4512 + +# Override some defaults of ESP BLE Mesh +CONFIG_BLE_MESH=y +CONFIG_BLE_MESH_PROVISIONER=y +CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=10 +CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=10 CONFIG_BLE_MESH_CFG_CLI=y CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_models/fast_prov_vendor_model/components/esp_fast_prov_server_model.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_models/fast_prov_vendor_model/components/esp_fast_prov_server_model.c index 198b4bb75..d5dd42c0a 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_models/fast_prov_vendor_model/components/esp_fast_prov_server_model.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_models/fast_prov_vendor_model/components/esp_fast_prov_server_model.c @@ -112,7 +112,7 @@ static void enable_gatt_proxy_cb(struct k_work *work) static void example_free_set_info(example_fast_prov_server_t *srv) { if (srv && srv->set_info) { - osi_free(srv->set_info); + bt_mesh_free(srv->set_info); srv->set_info = NULL; } } @@ -270,7 +270,7 @@ esp_err_t example_fast_prov_server_recv_msg(esp_ble_mesh_model_t *model, } memcpy(&srv->ctx, ctx, sizeof(esp_ble_mesh_msg_ctx_t)); - srv->set_info = osi_calloc(sizeof(struct fast_prov_info_set)); + srv->set_info = bt_mesh_calloc(sizeof(struct fast_prov_info_set)); if (!srv->set_info) { ESP_LOGE(TAG, "%s: Failed to allocate memory", __func__); bt_mesh_free_buf(msg); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_models/fast_prov_vendor_model/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_models/fast_prov_vendor_model/sdkconfig.defaults index 5ebe2b8c9..afce3cf46 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_models/fast_prov_vendor_model/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_models/fast_prov_vendor_model/sdkconfig.defaults @@ -1,33 +1,7 @@ # Override some defaults so BT stack is enabled # by default in this example CONFIG_BT_ENABLED=y + +# Override some defaults of ESP BLE Mesh CONFIG_BLE_MESH=y CONFIG_BLE_MESH_FAST_PROV=y -CONFIG_BLE_MESH_PROV=y -CONFIG_BLE_MESH_NODE=y -CONFIG_BLE_MESH_PROVISIONER=y -CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM=20 -CONFIG_BLE_MESH_MAX_STORED_NODES=10 -CONFIG_BLE_MESH_MAX_PROV_NODES=6 -CONFIG_BLE_MESH_PBA_SAME_TIME=3 -CONFIG_BLE_MESH_PBG_SAME_TIME=3 -CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT=3 -CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT=9 -CONFIG_BLE_MESH_PB_ADV=y -CONFIG_BLE_MESH_NET_BUF_POOL_USAGE=y -CONFIG_BLE_MESH_PB_GATT=y -CONFIG_BLE_MESH_GATT_PROXY_SERVER=y -CONFIG_BLE_MESH_RELAY=y -CONFIG_BLE_MESH_LOW_POWER=n -CONFIG_BLE_MESH_FRIEND=n -CONFIG_BT_BTU_TASK_STACK_SIZE=4512 -CONFIG_BLE_MESH_CFG_CLI=y -CONFIG_BLE_MESH_CRPL=60 -CONFIG_BLE_MESH_MSG_CACHE_SIZE=60 -CONFIG_BLE_MESH_ADV_BUF_COUNT=200 -CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=10 -CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=10 -CONFIG_BLE_MESH_RX_SDU_MAX=384 -CONFIG_BLE_MESH_TX_SEG_MAX=32 -CONFIG_BLE_MESH_NO_LOG=n -CONFIG_BLE_MESH_STACK_TRACE_LEVEL=0 diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/sdkconfig.defaults index 7beba052c..7d214d795 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/sdkconfig.defaults @@ -1,57 +1,32 @@ # Override some defaults so BT stack is enabled # by default in this example - -CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y -CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ=240 -CONFIG_MEMMAP_SMP=y CONFIG_BT_ENABLED=y CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n CONFIG_BTDM_CTRL_MODE_BTDM=n CONFIG_BTDM_MODEM_SLEEP=n -CONFIG_BTDM_BLE_SCAN_DUPL=y -CONFIG_BTDM_SCAN_DUPL_TYPE=2 CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y -CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE=200 CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y -CONFIG_BTDM_MESH_DUPL_SCAN_CACHE_SIZE=200 -CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED=y -CONFIG_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP=y -CONFIG_BT_GATTS_ENABLE=y CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 + +# Override some defaults of ESP BLE Mesh CONFIG_BLE_MESH=y -CONFIG_BLE_MESH_HCI_5_0=y -CONFIG_BLE_MESH_USE_DUPLICATE_SCAN=y CONFIG_BLE_MESH_FAST_PROV=y -CONFIG_BLE_MESH_PROV=y -CONFIG_BLE_MESH_NODE=y -CONFIG_BLE_MESH_PROVISIONER=y -CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM=20 -CONFIG_BLE_MESH_MAX_STORED_NODES=10 CONFIG_BLE_MESH_MAX_PROV_NODES=6 CONFIG_BLE_MESH_PBA_SAME_TIME=3 -CONFIG_BLE_MESH_PBG_SAME_TIME=3 -CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT=3 -CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT=9 -CONFIG_BLE_MESH_PB_ADV=y -CONFIG_BLE_MESH_NET_BUF_POOL_USAGE=y CONFIG_BLE_MESH_PB_GATT=y -CONFIG_BLE_MESH_GATT_PROXY_SERVER=y -CONFIG_BLE_MESH_RELAY=y -CONFIG_BLE_MESH_LOW_POWER=n -CONFIG_BLE_MESH_FRIEND=n -CONFIG_BT_BTU_TASK_STACK_SIZE=4512 -CONFIG_BLE_MESH_CFG_CLI=y -CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y CONFIG_BLE_MESH_CRPL=60 CONFIG_BLE_MESH_MSG_CACHE_SIZE=60 CONFIG_BLE_MESH_ADV_BUF_COUNT=200 CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=10 CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=10 -CONFIG_BLE_MESH_RX_SDU_MAX=384 -CONFIG_BLE_MESH_TX_SEG_MAX=32 -CONFIG_BLE_MESH_NO_LOG=n -CONFIG_BLE_MESH_STACK_TRACE_LEVEL=0 +CONFIG_BLE_MESH_TRACE_LEVEL_ERROR=y +CONFIG_BLE_MESH_CFG_CLI=y + +CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y +CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ=240 +CONFIG_MEMMAP_SMP=y CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=4096