diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index 1ec32112da96..9f39dcf57bf6 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -390,7 +390,11 @@ if(CONFIG_BT_ENABLED) "esp_ble_mesh/models/server/include" "esp_ble_mesh/api/core/include" "esp_ble_mesh/api/models/include" - "esp_ble_mesh/api") + "esp_ble_mesh/api" + "esp_ble_mesh/v1.1/api/core/include" + "esp_ble_mesh/v1.1/api/models/include" + "esp_ble_mesh/v1.1/btc/include" + "esp_ble_mesh/v1.1/include") list(APPEND srcs "esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c" "esp_ble_mesh/api/core/esp_ble_mesh_common_api.c" @@ -452,15 +456,18 @@ if(CONFIG_BT_ENABLED) "esp_ble_mesh/core/lpn.c" "esp_ble_mesh/core/main.c" "esp_ble_mesh/core/net.c" + "esp_ble_mesh/core/prov_common.c" "esp_ble_mesh/core/prov_node.c" "esp_ble_mesh/core/prov_pvnr.c" "esp_ble_mesh/core/proxy_client.c" "esp_ble_mesh/core/proxy_server.c" "esp_ble_mesh/core/pvnr_mgmt.c" + "esp_ble_mesh/core/rpl.c" "esp_ble_mesh/core/scan.c" "esp_ble_mesh/core/test.c" "esp_ble_mesh/core/transport.c" "esp_ble_mesh/models/common/device_property.c" + "esp_ble_mesh/models/common/model_common.c" "esp_ble_mesh/models/client/client_common.c" "esp_ble_mesh/models/client/generic_client.c" "esp_ble_mesh/models/client/lighting_client.c" @@ -472,7 +479,29 @@ if(CONFIG_BT_ENABLED) "esp_ble_mesh/models/server/server_common.c" "esp_ble_mesh/models/server/state_binding.c" "esp_ble_mesh/models/server/state_transition.c" - "esp_ble_mesh/models/server/time_scene_server.c") + "esp_ble_mesh/models/server/time_scene_server.c" + "esp_ble_mesh/v1.1/api/core/esp_ble_mesh_agg_model_api.c" + "esp_ble_mesh/v1.1/api/core/esp_ble_mesh_brc_model_api.c" + "esp_ble_mesh/v1.1/api/core/esp_ble_mesh_cm_data_api.c" + "esp_ble_mesh/v1.1/api/core/esp_ble_mesh_df_model_api.c" + "esp_ble_mesh/v1.1/api/core/esp_ble_mesh_lcd_model_api.c" + "esp_ble_mesh/v1.1/api/core/esp_ble_mesh_odp_model_api.c" + "esp_ble_mesh/v1.1/api/core/esp_ble_mesh_prb_model_api.c" + "esp_ble_mesh/v1.1/api/core/esp_ble_mesh_rpr_model_api.c" + "esp_ble_mesh/v1.1/api/core/esp_ble_mesh_sar_model_api.c" + "esp_ble_mesh/v1.1/api/core/esp_ble_mesh_srpl_model_api.c" + "esp_ble_mesh/v1.1/api/models/esp_ble_mesh_mbt_model_api.c" + "esp_ble_mesh/v1.1/btc/btc_ble_mesh_agg_model.c" + "esp_ble_mesh/v1.1/btc/btc_ble_mesh_brc_model.c" + "esp_ble_mesh/v1.1/btc/btc_ble_mesh_df_model.c" + "esp_ble_mesh/v1.1/btc/btc_ble_mesh_lcd_model.c" + "esp_ble_mesh/v1.1/btc/btc_ble_mesh_mbt_model.c" + "esp_ble_mesh/v1.1/btc/btc_ble_mesh_odp_model.c" + "esp_ble_mesh/v1.1/btc/btc_ble_mesh_prb_model.c" + "esp_ble_mesh/v1.1/btc/btc_ble_mesh_rpr_model.c" + "esp_ble_mesh/v1.1/btc/btc_ble_mesh_sar_model.c" + "esp_ble_mesh/v1.1/btc/btc_ble_mesh_srpl_model.c" + "esp_ble_mesh/v1.1/ext.c") endif() @@ -741,6 +770,25 @@ if(CONFIG_BT_ENABLED) target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") endif() +if(CONFIG_BLE_MESH) + if(CONFIG_IDF_TARGET_ESP32) + add_prebuilt_library(mesh_v11 "esp_ble_mesh/v1.1/lib/esp32/libmesh_v1.1.a") + target_link_libraries(${COMPONENT_LIB} PRIVATE mesh_v11) + elseif(CONFIG_IDF_TARGET_ESP32S3) + add_prebuilt_library(mesh_v11 "esp_ble_mesh/v1.1/lib/esp32s3/libmesh_v1.1.a") + target_link_libraries(${COMPONENT_LIB} PRIVATE mesh_v11) + elseif(CONFIG_IDF_TARGET_ESP32C3) + add_prebuilt_library(mesh_v11 "esp_ble_mesh/v1.1/lib/esp32c3/libmesh_v1.1.a") + target_link_libraries(${COMPONENT_LIB} PRIVATE mesh_v11) + elseif(CONFIG_IDF_TARGET_ESP32C6) + add_prebuilt_library(mesh_v11 "esp_ble_mesh/v1.1/lib/esp32c6/libmesh_v1.1.a") + target_link_libraries(${COMPONENT_LIB} PRIVATE mesh_v11) + elseif(CONFIG_IDF_TARGET_ESP32H2) + add_prebuilt_library(mesh_v11 "esp_ble_mesh/v1.1/lib/esp32h2/libmesh_v1.1.a") + target_link_libraries(${COMPONENT_LIB} PRIVATE mesh_v11) + endif() +endif() + if(CONFIG_BT_NIMBLE_MESH) set_source_files_properties("host/nimble/nimble/nimble/host/mesh/src/net.c" PROPERTIES COMPILE_FLAGS -Wno-type-limits) diff --git a/components/bt/common/btc/core/btc_task.c b/components/bt/common/btc/core/btc_task.c index 93f36b5e2332..179d501e722d 100644 --- a/components/bt/common/btc/core/btc_task.c +++ b/components/bt/common/btc/core/btc_task.c @@ -69,10 +69,20 @@ #include "btc_ble_mesh_prov.h" #include "btc_ble_mesh_health_model.h" #include "btc_ble_mesh_config_model.h" +#include "btc_ble_mesh_agg_model.h" +#include "btc_ble_mesh_brc_model.h" +#include "btc_ble_mesh_df_model.h" +#include "btc_ble_mesh_lcd_model.h" +#include "btc_ble_mesh_odp_model.h" +#include "btc_ble_mesh_prb_model.h" +#include "btc_ble_mesh_rpr_model.h" +#include "btc_ble_mesh_sar_model.h" +#include "btc_ble_mesh_srpl_model.h" #include "btc_ble_mesh_generic_model.h" #include "btc_ble_mesh_lighting_model.h" #include "btc_ble_mesh_sensor_model.h" #include "btc_ble_mesh_time_scene_model.h" +#include "btc_ble_mesh_mbt_model.h" #endif /* #if CONFIG_BLE_MESH */ #define BTC_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) @@ -158,6 +168,60 @@ static const btc_func_t profile_tab[BTC_PID_NUM] = { [BTC_PID_CONFIG_CLIENT] = {btc_ble_mesh_config_client_call_handler, btc_ble_mesh_config_client_cb_handler }, #endif /* CONFIG_BLE_MESH_CFG_CLI */ [BTC_PID_CONFIG_SERVER] = {NULL, btc_ble_mesh_config_server_cb_handler }, +#if CONFIG_BLE_MESH_AGG_CLI + [BTC_PID_AGG_CLIENT] = {btc_ble_mesh_agg_client_call_handler, btc_ble_mesh_agg_client_cb_handler }, +#endif /* CONFIG_BLE_MESH_AGG_CLI */ +#if CONFIG_BLE_MESH_AGG_SRV + [BTC_PID_AGG_SERVER] = {NULL, btc_ble_mesh_agg_server_cb_handler }, +#endif /* CONFIG_BLE_MESH_AGG_SRV */ +#if CONFIG_BLE_MESH_BRC_CLI + [BTC_PID_BRC_CLIENT] = {btc_ble_mesh_brc_client_call_handler, btc_ble_mesh_brc_client_cb_handler }, +#endif /* CONFIG_BLE_MESH_BRC_CLI */ +#if CONFIG_BLE_MESH_BRC_SRV + [BTC_PID_BRC_SERVER] = {NULL, btc_ble_mesh_brc_server_cb_handler }, +#endif /* CONFIG_BLE_MESH_BRC_SRV */ +#if CONFIG_BLE_MESH_DF_CLI + [BTC_PID_DF_CLIENT] = {btc_ble_mesh_df_client_call_handler, btc_ble_mesh_df_client_cb_handler }, +#endif /* CONFIG_BLE_MESH_DF_CLI */ +#if CONFIG_BLE_MESH_DF_SRV + [BTC_PID_DF_SERVER] = {NULL, btc_ble_mesh_df_server_cb_handler }, +#endif /* CONFIG_BLE_MESH_DF_SRV */ +#if CONFIG_BLE_MESH_LCD_CLI + [BTC_PID_LCD_CLIENT] = {btc_ble_mesh_lcd_client_call_handler, btc_ble_mesh_lcd_client_cb_handler }, +#endif /* CONFIG_BLE_MESH_LCD_CLI */ +#if CONFIG_BLE_MESH_LCD_SRV + [BTC_PID_LCD_SERVER] = {NULL, btc_ble_mesh_lcd_server_cb_handler }, +#endif /* CONFIG_BLE_MESH_LCD_SRV */ +#if CONFIG_BLE_MESH_ODP_CLI + [BTC_PID_ODP_CLIENT] = {btc_ble_mesh_odp_client_call_handler, btc_ble_mesh_odp_client_cb_handler }, +#endif /* CONFIG_BLE_MESH_ODP_CLI */ +#if CONFIG_BLE_MESH_ODP_SRV + [BTC_PID_ODP_SERVER] = {NULL, btc_ble_mesh_odp_server_cb_handler }, +#endif /* CONFIG_BLE_MESH_ODP_SRV */ +#if CONFIG_BLE_MESH_PRB_CLI + [BTC_PID_PRB_CLIENT] = {btc_ble_mesh_prb_client_call_handler, btc_ble_mesh_prb_client_cb_handler }, +#endif /* CONFIG_BLE_MESH_PRB_CLI */ +#if CONFIG_BLE_MESH_PRB_SRV + [BTC_PID_PRB_SERVER] = {NULL, btc_ble_mesh_prb_server_cb_handler }, +#endif /*CONFIG_BLE_MESH_PRB_SRV*/ +#if CONFIG_BLE_MESH_RPR_CLI + [BTC_PID_RPR_CLIENT] = {btc_ble_mesh_rpr_client_call_handler, btc_ble_mesh_rpr_client_cb_handler }, +#endif /* CONFIG_BLE_MESH_RPR_CLI */ +#if CONFIG_BLE_MESH_RPR_SRV + [BTC_PID_RPR_SERVER] = {NULL, btc_ble_mesh_rpr_server_cb_handler }, +#endif /* CONFIG_BLE_MESH_RPR_SRV */ +#if CONFIG_BLE_MESH_SAR_CLI + [BTC_PID_SAR_CLIENT] = {btc_ble_mesh_sar_client_call_handler, btc_ble_mesh_sar_client_cb_handler }, +#endif /* CONFIG_BLE_MESH_SAR_CLI */ +#if CONFIG_BLE_MESH_SAR_SRV + [BTC_PID_SAR_SERVER] = {NULL, btc_ble_mesh_sar_server_cb_handler }, +#endif /* CONFIG_BLE_MESH_SAR_SRV */ +#if CONFIG_BLE_MESH_SRPL_CLI + [BTC_PID_SRPL_CLIENT] = {btc_ble_mesh_srpl_client_call_handler, btc_ble_mesh_srpl_client_cb_handler }, +#endif /* CONFIG_BLE_MESH_SRPL_CLI */ +#if CONFIG_BLE_MESH_SRPL_SRV + [BTC_PID_SRPL_SERVER] = {NULL, btc_ble_mesh_srpl_server_cb_handler }, +#endif /* CONFIG_BLE_MESH_SRPL_SRV */ #if CONFIG_BLE_MESH_GENERIC_CLIENT [BTC_PID_GENERIC_CLIENT] = {btc_ble_mesh_generic_client_call_handler, btc_ble_mesh_generic_client_cb_handler }, #endif /* CONFIG_BLE_MESH_GENERIC_CLIENT */ @@ -182,6 +246,12 @@ static const btc_func_t profile_tab[BTC_PID_NUM] = { #if CONFIG_BLE_MESH_TIME_SCENE_SERVER [BTC_PID_TIME_SCENE_SERVER] = {NULL, btc_ble_mesh_time_scene_server_cb_handler}, #endif /* CONFIG_BLE_MESH_TIME_SCENE_SERVER */ +#if CONFIG_BLE_MESH_MBT_CLI + [BTC_PID_MBT_CLIENT] = {btc_ble_mesh_mbt_client_call_handler, btc_ble_mesh_mbt_client_cb_handler }, +#endif /* CONFIG_BLE_MESH_MBT_CLI */ +#if CONFIG_BLE_MESH_MBT_SRV + [BTC_PID_MBT_SERVER] = {btc_ble_mesh_mbt_server_call_handler, btc_ble_mesh_mbt_server_cb_handler }, +#endif /* CONFIG_BLE_MESH_MBT_SRV */ #if CONFIG_BLE_MESH_BLE_COEX_SUPPORT [BTC_PID_BLE_MESH_BLE_COEX] = {btc_ble_mesh_ble_call_handler, btc_ble_mesh_ble_cb_handler }, #endif /* CONFIG_BLE_MESH_BLE_COEX_SUPPORT */ diff --git a/components/bt/common/btc/include/btc/btc_task.h b/components/bt/common/btc/include/btc/btc_task.h index ef286c323636..232186b51c5b 100644 --- a/components/bt/common/btc/include/btc/btc_task.h +++ b/components/bt/common/btc/include/btc/btc_task.h @@ -75,6 +75,24 @@ typedef enum { BTC_PID_HEALTH_SERVER, BTC_PID_CONFIG_CLIENT, BTC_PID_CONFIG_SERVER, + BTC_PID_AGG_CLIENT, + BTC_PID_AGG_SERVER, + BTC_PID_BRC_CLIENT, + BTC_PID_BRC_SERVER, + BTC_PID_DF_CLIENT, + BTC_PID_DF_SERVER, + BTC_PID_LCD_CLIENT, + BTC_PID_LCD_SERVER, + BTC_PID_ODP_CLIENT, + BTC_PID_ODP_SERVER, + BTC_PID_PRB_CLIENT, + BTC_PID_PRB_SERVER, + BTC_PID_RPR_CLIENT, + BTC_PID_RPR_SERVER, + BTC_PID_SAR_CLIENT, + BTC_PID_SAR_SERVER, + BTC_PID_SRPL_CLIENT, + BTC_PID_SRPL_SERVER, BTC_PID_GENERIC_CLIENT, BTC_PID_LIGHTING_CLIENT, BTC_PID_SENSOR_CLIENT, @@ -83,6 +101,8 @@ typedef enum { BTC_PID_LIGHTING_SERVER, BTC_PID_SENSOR_SERVER, BTC_PID_TIME_SCENE_SERVER, + BTC_PID_MBT_CLIENT, + BTC_PID_MBT_SERVER, BTC_PID_BLE_MESH_BLE_COEX, #endif /* CONFIG_BLE_MESH */ BTC_PID_NUM, diff --git a/components/bt/esp_ble_mesh/Kconfig.in b/components/bt/esp_ble_mesh/Kconfig.in index 807f1a4fe3d7..ac5548d140f2 100644 --- a/components/bt/esp_ble_mesh/Kconfig.in +++ b/components/bt/esp_ble_mesh/Kconfig.in @@ -21,6 +21,11 @@ if BLE_MESH option in the Bluetooth Controller section in menuconfig, which is "Scan Duplicate By Device Address and Advertising Data". + config BLE_MESH_ACTIVE_SCAN + bool "Support Active Scan in BLE Mesh" + help + Enable this option to allow using BLE Active Scan for BLE Mesh. + choice BLE_MESH_MEM_ALLOC_MODE prompt "Memory allocation strategy" default BLE_MESH_MEM_ALLOC_MODE_INTERNAL @@ -264,6 +269,31 @@ if BLE_MESH Enable this option to support BLE Mesh Provisioning functionality. For BLE Mesh, this option should be always enabled. + config BLE_MESH_PROV_EPA + bool "BLE Mesh enhanced provisioning authentication" + depends on BLE_MESH_PROV + default y + help + Enable this option to support BLE Mesh enhanced provisioning authentication + functionality. This option can increase the security level of provisioning. + It is recommended to enable this option. + + config BLE_MESH_CERT_BASED_PROV + bool "Support Certificate-based provisioning" + depends on BLE_MESH_PROV + default n + help + Enable this option to support BLE Mesh Certificate-Based Provisioning. + + config BLE_MESH_RECORD_FRAG_MAX_SIZE + int "Maximum size of the provisioning record fragment that Provisioner can receive" + depends on BLE_MESH_CERT_BASED_PROV + default 56 + range 1 57 + help + This option sets the maximum size of the provisioning record fragment that the + Provisioner can receive. The range depends on provisioning bearer. + config BLE_MESH_PB_ADV bool "Provisioning support using the advertising bearer (PB-ADV)" select BLE_MESH_PROV @@ -338,6 +368,34 @@ if BLE_MESH 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_PRIVACY + bool "Support Proxy Privacy" + depends on BLE_MESH_PRB_SRV && BLE_MESH_GATT_PROXY_SERVER + default y + help + The Proxy Privacy parameter controls the privacy of the Proxy Server + over the connection. The value of the Proxy Privacy parameter is + controlled by the type of proxy connection, which is dependent on the + bearer used by the proxy connection. + + config BLE_MESH_PROXY_SOLIC_PDU_RX + bool "Support receiving Proxy Solicitation PDU" + depends on BLE_MESH_GATT_PROXY_SERVER + help + Enable this option to support receiving Proxy Solicitation PDU. + + config BLE_MESH_PROXY_SOLIC_RX_CRPL + int "Maximum capacity of solicitation replay protection list" + depends on BLE_MESH_PROXY_SOLIC_PDU_RX + default 2 + range 1 255 + help + This option specifies the maximum capacity of the solicitation replay + protection list. The solicitation replay protection list is used to + reject Solicitation PDUs that were already processed by a node, which + will store the solicitation src and solicitation sequence number of + the received Solicitation PDU message. + config BLE_MESH_GATT_PROXY_CLIENT bool "BLE Mesh GATT Proxy Client" select BLE_MESH_PROXY @@ -347,6 +405,25 @@ if BLE_MESH can use the GATT bearer to send mesh messages to a node that supports the advertising bearer. + config BLE_MESH_PROXY_SOLIC_PDU_TX + bool "Support sending Proxy Solicitation PDU" + depends on BLE_MESH_GATT_PROXY_CLIENT + help + Enable this option to support sending Proxy Solicitation PDU. + + config BLE_MESH_PROXY_SOLIC_TX_SRC_COUNT + int "Maximum number of SSRC that can be used by Proxy Client" + depends on BLE_MESH_PROXY_SOLIC_PDU_TX + default 2 + range 1 16 + help + This option specifies the maximum number of Solicitation Source (SSRC) + that can be used by Proxy Client for sending a Solicitation PDU. + A Proxy Client may use the primary address or any of the secondary + addresses as the SSRC for a Solicitation PDU. + So for a Proxy Client, it's better to choose the value based on its + own element count. + config BLE_MESH_NET_BUF_POOL_USAGE bool default y @@ -994,6 +1071,244 @@ if BLE_MESH help Enable support for Health Server model. + config BLE_MESH_BRC_CLI + bool "Bridge Configuration Client model" + help + Enable support for Bridge Configuration Client model. + + config BLE_MESH_BRC_SRV + bool "Bridge Configuration Server model" + default n + help + Enable support for Bridge Configuration Server model. + + if BLE_MESH_BRC_SRV + + config BLE_MESH_MAX_BRIDGING_TABLE_ENTRY_COUNT + int "Maximum number of Bridging Table entries" + range 16 65535 + default 16 + help + Maximum number of Bridging Table entries that the Bridge Configuration Server can support. + + endif #BLE_MESH_BRC_SRV + + config BLE_MESH_PRB_CLI + bool "Mesh Private Beacon Client model" + help + Enable support for Mesh Private Beacon Client model. + + config BLE_MESH_PRB_SRV + bool "Mesh Private Beacon Server model" + help + Enable support for Mesh Private Beacon Server model. + + config BLE_MESH_ODP_CLI + bool "On-Demand Private Proxy Client model" + help + Enable support for On-Demand Private Proxy Client model. + + config BLE_MESH_ODP_SRV + bool "On-Demand Private Proxy Server model" + depends on BLE_MESH_PROXY_SOLIC_PDU_RX + select BLE_MESH_SRPL_SRV + help + Enable support for On-Demand Private Proxy Server model. + + config BLE_MESH_SRPL_CLI + bool "Solicitation PDU RPL Configuration Client model" + help + Enable support for Solicitation PDU RPL Configuration Client model. + + config BLE_MESH_SRPL_SRV + bool "Solicitation PDU RPL Configuration Server model" + depends on BLE_MESH_PROXY_SOLIC_PDU_RX + help + Enable support for Solicitation PDU RPL Configuration Server model. + Note: + This option depends on the functionality of receiving Solicitation + PDU. If the device doesn't support receiving Solicitation PDU, then + there is no need to enable this server model. + + config BLE_MESH_AGG_CLI + bool "Opcodes Aggregator Client model" + help + Enable support for Opcodes Aggregator Client model. + + config BLE_MESH_AGG_SRV + bool "Opcodes Aggregator Server model" + help + Enable support for Opcodes Aggregator Server model. + + config BLE_MESH_SAR_CLI + bool "SAR Configuration Client model" + help + Enable support for SAR Configuration Client model. + + config BLE_MESH_SAR_SRV + bool "SAR Configuration Server model" + help + Enable support for SAR Configuration Server model. + + config BLE_MESH_COMP_DATA_1 + bool "Support Composition Data Page 1" + help + Composition Data Page 1 contains information about the relationships + among models. + Each model either can be a root model or can extend other models. + + config BLE_MESH_COMP_DATA_128 + bool "Support Composition Data Page 128" + help + Composition Data Page 128 is used to indicate the structure of + elements, features, and models of a node after the successful + execution of the Node Address Refresh procedure or the Node + Composition Refresh procedure, or after the execution of the + Node Removal procedure followed by the provisioning process. + Composition Data Page 128 shall be present if the node supports + the Remote Provisioning Server model; otherwise it is optional. + + config BLE_MESH_MODELS_METADATA_0 + bool "Support Models Metadata Page 0" + help + The Models Metadata state contains metadata of a node’s models. + The Models Metadata state is composed of a number of pages of + information. + Models Metadata Page 0 shall be present if the node supports + the Large Composition Data Server model. + + config BLE_MESH_MODELS_METADATA_128 + bool "Support Models Metadata Page 128" + depends on BLE_MESH_MODELS_METADATA_0 + help + The Models Metadata state contains metadata of a node’s models. + The Models Metadata state is composed of a number of pages of + information. + Models Metadata Page 128 contains metadata for the node’s models + after the successful execution of the Node Address Refresh + procedure or the Node Composition Refresh procedure, or after + the execution of the Node Removal procedure followed by the + provisioning process. + Models Metadata Page 128 shall be present if the node supports + the Remote Provisioning Server model and the node supports the + Large Composition Data Server model. + + config BLE_MESH_LCD_CLI + bool "Large Composition Data Client model" + help + Enable support for Large Composition Data Client model. + + config BLE_MESH_LCD_SRV + bool "Large Composition Data Server model" + select BLE_MESH_MODELS_METADATA_0 + help + Enable support for Large Composition Data Server model. + + config BLE_MESH_RPR_CLI + bool "Remote Provisioning Client model" + depends on BLE_MESH_PROVISIONER + select BLE_MESH_PROV + help + Enable support for Remote Provisioning Client model + + if BLE_MESH_RPR_CLI + + config BLE_MESH_RPR_CLI_PROV_SAME_TIME + int "Maximum number of PB-Remote running at the same time by Provisioner" + range 1 5 + default 2 + help + This option specifies how many devices can be provisioned at the same time + using PB-REMOTE. For example, if the value is 2, it means a Provisioner can + provision two unprovisioned devices with PB-REMOTE at the same time. + + endif # BLE_MESH_RPR_CLI + + config BLE_MESH_RPR_SRV + bool "Remote Provisioning Server model" + depends on BLE_MESH_NODE + select BLE_MESH_PB_ADV + help + Enable support for Remote Provisioning Server model + + if BLE_MESH_RPR_SRV + + config BLE_MESH_RPR_SRV_MAX_SCANNED_ITEMS + int "Maximum number of device information can be scanned" + range 4 255 + default 10 + help + This option specifies how many device information can a Remote + Provisioning Server store each time while scanning. + + config BLE_MESH_RPR_SRV_ACTIVE_SCAN + bool "Support Active Scan for remote provisioning" + select BLE_MESH_ACTIVE_SCAN + help + Enable this option to support Active Scan for remote provisioning. + + config BLE_MESH_RPR_SRV_MAX_EXT_SCAN + int "Maximum number of extended scan procedures" + range 1 10 + default 1 + help + This option specifies how many extended scan procedures can be + started by the Remote Provisioning Server. + + endif # BLE_MESH_RPR_SRV + + config BLE_MESH_DF_CLI + bool "Directed Forwarding Configuration Client model" + help + Enable support for Directed Forwarding Configuration Client model. + + config BLE_MESH_DF_SRV + bool "Directed Forwarding Configuration Server model" + help + Enable support for Directed Forwarding Configuration Server model. + + if BLE_MESH_DF_SRV + + config BLE_MESH_MAX_DISC_TABLE_ENTRY_COUNT + int "Maximum number of discovery table entries in a given subnet" + range 2 255 + default 2 + help + Maximum number of Discovery Table entries supported by the node in a given subnet. + + config BLE_MESH_MAX_FORWARD_TABLE_ENTRY_COUNT + int "Maximum number of forward table entries in a given subnet" + range 2 64 + default 2 + help + Maximum number of Forward Table entries supported by the node in a given subnet. + + config BLE_MESH_MAX_DEPS_NODES_PER_PATH + int "Maximum number of dependent nodes per path" + range 2 64 + default 2 + help + Maximum size of dependent nodes list supported by each forward table entry. + + config BLE_MESH_PATH_MONITOR_TEST + bool "Enable Path Monitoring test mode" + default n + help + The option only removes the Path Use timer; all other behavior of the + device is not changed. + If Path Monitoring test mode is going to be used, this option should + be enabled. + + if BLE_MESH_GATT_PROXY_SERVER + config BLE_MESH_SUPPORT_DIRECTED_PROXY + bool "Enable Directed Proxy functionality" + default y + help + Support Directed Proxy functionality. + endif + + endif # BLE_MESH_DF_SRV + endmenu #Support for BLE Mesh Foundation models menu "Support for BLE Mesh Client/Server models" @@ -1107,6 +1422,30 @@ if BLE_MESH help Enable support for Lighting server models. + config BLE_MESH_MBT_CLI + bool "BLOB Transfer Client model" + default y + help + Enable support for BLOB Transfer Client model. + + if BLE_MESH_MBT_CLI + + config BLE_MESH_MAX_BLOB_RECEIVERS + int "Maximum number of simultaneous blob receivers" + default 2 + range 1 255 + help + Maximum number of BLOB Transfer Server models that can participating + in the BLOB transfer with a BLOB Transfer Client model. + + endif # BLE_MESH_MBT_CLI + + config BLE_MESH_MBT_SRV + bool "BLOB Transfer Server model" + default y + help + Enable support for BLOB Transfer Server model. + endmenu #Support for BLE Mesh Client/Server models config BLE_MESH_IV_UPDATE_TEST 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 13ce4f534219..e4f2e67b678c 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 @@ -17,6 +17,7 @@ int32_t esp_ble_mesh_get_model_publish_period(esp_ble_mesh_model_t *model) if (model == NULL) { return 0; } + return btc_ble_mesh_model_pub_period_get(model); } @@ -31,6 +32,7 @@ uint16_t *esp_ble_mesh_is_model_subscribed_to_group(esp_ble_mesh_model_t *model, if (model == NULL) { return NULL; } + return btc_ble_mesh_model_find_group(model, group_addr); } @@ -39,6 +41,7 @@ esp_ble_mesh_elem_t *esp_ble_mesh_find_element(uint16_t element_addr) if (!ESP_BLE_MESH_ADDR_IS_UNICAST(element_addr)) { return NULL; } + return btc_ble_mesh_elem_find(element_addr); } @@ -53,6 +56,7 @@ esp_ble_mesh_model_t *esp_ble_mesh_find_vendor_model(const esp_ble_mesh_elem_t * if (element == NULL) { return NULL; } + return btc_ble_mesh_model_find_vnd(element, company_id, model_id); } @@ -62,6 +66,7 @@ esp_ble_mesh_model_t *esp_ble_mesh_find_sig_model(const esp_ble_mesh_elem_t *ele if (element == NULL) { return NULL; } + return btc_ble_mesh_model_find(element, model_id); } @@ -122,6 +127,15 @@ esp_err_t esp_ble_mesh_model_unsubscribe_group_addr(uint16_t element_addr, uint1 == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } +#if CONFIG_BLE_MESH_DF_SRV +esp_err_t esp_ble_mesh_enable_directed_forwarding(uint16_t net_idx, bool directed_forwarding, + bool directed_forwarding_relay) +{ + return btc_ble_mesh_enable_directed_forwarding(net_idx, directed_forwarding, + directed_forwarding_relay); +} +#endif /* CONFIG_BLE_MESH_DF_SRV */ + #if CONFIG_BLE_MESH_NODE const uint8_t *esp_ble_mesh_node_get_local_net_key(uint16_t net_idx) 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 3bc1a60eaeb8..16b0805dea49 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 @@ -35,11 +35,6 @@ static esp_err_t ble_mesh_model_send_msg(esp_ble_mesh_model_t *model, return ESP_ERR_INVALID_ARG; } - if (device_role > ROLE_FAST_PROV) { - BT_ERR("Invalid device role 0x%02x", device_role); - return ESP_ERR_INVALID_ARG; - } - /* When data is NULL, it is mandatory to set length to 0 to prevent users from misinterpreting parameters. */ if (data == NULL) { length = 0; @@ -61,9 +56,18 @@ static esp_err_t ble_mesh_model_send_msg(esp_ble_mesh_model_t *model, } if (act == BTC_BLE_MESH_ACT_MODEL_PUBLISH) { - mic_len = ESP_BLE_MESH_MIC_SHORT; + /* When "send_rel" is true and "send_szmic" is 1, 8-octets TransMIC will + * be used, otherwise 4-octets TransMIC will be used. + */ + mic_len = (model->pub->send_rel && model->pub->send_szmic) ? + ESP_BLE_MESH_MIC_LONG : ESP_BLE_MESH_MIC_SHORT; } else { - mic_len = ctx->send_rel ? ESP_BLE_MESH_MIC_LONG : ESP_BLE_MESH_MIC_SHORT; + /* When the message is tagged with the send-segmented tag and "send_szmic" + * is 1, 8-octets TransMIC will be used, otherwise 4-octets TransMIC will + * be used. + */ + mic_len = ((ctx->send_tag & ESP_BLE_MESH_TAG_SEND_SEGMENTED) && ctx->send_szmic) ? + ESP_BLE_MESH_MIC_LONG : ESP_BLE_MESH_MIC_SHORT; } if (op_len + length + mic_len > MIN(ESP_BLE_MESH_SDU_MAX_LEN, ESP_BLE_MESH_TX_SDU_MAX)) { @@ -89,7 +93,6 @@ static esp_err_t ble_mesh_model_send_msg(esp_ble_mesh_model_t *model, if (act == BTC_BLE_MESH_ACT_MODEL_PUBLISH) { arg.model_publish.model = model; - arg.model_publish.device_role = device_role; } else { arg.model_send.model = model; arg.model_send.ctx = ctx; @@ -97,7 +100,6 @@ static esp_err_t ble_mesh_model_send_msg(esp_ble_mesh_model_t *model, arg.model_send.opcode = opcode; arg.model_send.length = op_len + length; arg.model_send.data = msg_data; - arg.model_send.device_role = device_role; arg.model_send.msg_timeout = msg_timeout; } @@ -254,8 +256,7 @@ esp_err_t esp_ble_mesh_node_local_reset(void) return (btc_transfer_context(&msg, NULL, 0, NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } -#if (CONFIG_BLE_MESH_PROVISIONER) - +#if CONFIG_BLE_MESH_PROVISIONER esp_err_t esp_ble_mesh_provisioner_set_node_name(uint16_t index, const char *name) { btc_ble_mesh_prov_args_t arg = {0}; @@ -312,6 +313,7 @@ esp_err_t esp_ble_mesh_provisioner_store_node_comp_data(uint16_t unicast_addr, 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, btc_ble_mesh_prov_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -414,6 +416,7 @@ esp_err_t esp_ble_mesh_provisioner_add_local_app_key(const uint8_t app_key[16], } else { bzero(arg.add_local_app_key.app_key, 16); } + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -437,6 +440,7 @@ esp_err_t esp_ble_mesh_provisioner_update_local_app_key(const uint8_t app_key[16 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, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -466,6 +470,7 @@ esp_err_t esp_ble_mesh_provisioner_bind_app_key_to_local_model(uint16_t element_ arg.local_mod_app_bind.app_idx = app_idx; arg.local_mod_app_bind.model_id = model_id; arg.local_mod_app_bind.cid = company_id; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -491,6 +496,7 @@ esp_err_t esp_ble_mesh_provisioner_add_local_net_key(const uint8_t net_key[16], } else { bzero(arg.add_local_net_key.net_key, 16); } + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -512,13 +518,14 @@ esp_err_t esp_ble_mesh_provisioner_update_local_net_key(const uint8_t net_key[16 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, 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); + return bt_mesh_provisioner_net_key_get(net_idx); } #if CONFIG_BLE_MESH_PROVISIONER_RECV_HB @@ -760,9 +767,85 @@ uint8_t esp_ble_mesh_provisioner_get_free_settings_count(void) } #endif /* CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE */ +#if CONFIG_BLE_MESH_CERT_BASED_PROV +esp_err_t esp_ble_mesh_provisioner_send_prov_records_get(uint16_t link_idx) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + 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_SEND_PROV_RECORDS_GET; + + arg.send_prov_records_get.link_idx = link_idx; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_send_prov_record_req(uint16_t link_idx, uint16_t record_id, + uint16_t frag_offset, uint16_t max_size) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (record_id >= ESP_BLE_MESH_PROV_RECORD_MAX_ID) { + 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_SEND_PROV_RECORD_REQUEST; + + arg.send_prov_record_req.link_idx = link_idx; + arg.send_prov_record_req.record_id = record_id; + arg.send_prov_record_req.frag_offset = frag_offset; + arg.send_prov_record_req.max_size = max_size; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_send_prov_invite(uint16_t link_idx) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + 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_SEND_PROV_INVITE; + + arg.send_prov_invite.link_idx = link_idx; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_send_link_close(uint16_t link_idx) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + 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_SEND_LINK_CLOSE; + + arg.send_link_close.link_idx = link_idx; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_CERT_BASED_PROV */ #endif /* CONFIG_BLE_MESH_PROVISIONER */ -#if (CONFIG_BLE_MESH_FAST_PROV) +#if CONFIG_BLE_MESH_FAST_PROV const uint8_t *esp_ble_mesh_get_fast_prov_app_key(uint16_t net_idx, uint16_t app_idx) { return bt_mesh_get_fast_prov_app_key(net_idx, app_idx); 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 017164a7c2a3..3f407b3a4fe1 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 @@ -31,13 +31,14 @@ static bool prov_bearers_valid(esp_ble_mesh_prov_bearer_t bearers) { if ((!(bearers & (ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT))) || (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && - !IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && - !(bearers & ESP_BLE_MESH_PROV_ADV)) || + !IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + !(bearers & ESP_BLE_MESH_PROV_ADV)) || (!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && - IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && - !(bearers & ESP_BLE_MESH_PROV_GATT))) { + IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + !(bearers & ESP_BLE_MESH_PROV_GATT))) { return false; } + return true; } @@ -141,7 +142,7 @@ esp_err_t esp_ble_mesh_node_input_string(const char *string) msg.act = BTC_BLE_MESH_ACT_INPUT_STRING; memset(arg.input_string.string, 0, sizeof(arg.input_string.string)); strncpy(arg.input_string.string, string, - MIN(strlen(string), sizeof(arg.input_string.string))); + MIN(strlen(string), sizeof(arg.input_string.string))); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); @@ -169,7 +170,7 @@ esp_err_t esp_ble_mesh_set_unprovisioned_device_name(const char *name) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } -#if (CONFIG_BLE_MESH_PROVISIONER) +#if CONFIG_BLE_MESH_PROVISIONER esp_err_t esp_ble_mesh_provisioner_read_oob_pub_key(uint8_t link_idx, uint8_t pub_key_x[32], uint8_t pub_key_y[32]) { @@ -212,7 +213,7 @@ esp_err_t esp_ble_mesh_provisioner_input_string(const char *string, uint8_t link memset(arg.provisioner_input_str.string, 0, sizeof(arg.provisioner_input_str.string)); strncpy(arg.provisioner_input_str.string, string, - MIN(strlen(string), sizeof(arg.provisioner_input_str.string))); + MIN(strlen(string), sizeof(arg.provisioner_input_str.string))); arg.provisioner_input_str.link_idx = link_idx; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) @@ -305,6 +306,7 @@ esp_err_t esp_ble_mesh_provisioner_add_unprov_dev(esp_ble_mesh_unprov_dev_add_t memcpy(arg.provisioner_dev_add.add_dev.addr, add_dev->addr, sizeof(esp_ble_mesh_bd_addr_t)); memcpy(arg.provisioner_dev_add.add_dev.uuid, add_dev->uuid, 16); arg.provisioner_dev_add.flags = flags; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -339,6 +341,7 @@ esp_err_t esp_ble_mesh_provisioner_prov_device_with_addr(const uint8_t uuid[16], 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, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -366,6 +369,7 @@ esp_err_t esp_ble_mesh_provisioner_delete_dev(esp_ble_mesh_device_delete_t *del_ } else if (del_dev->flag & DEL_DEV_UUID_FLAG) { memcpy(arg.provisioner_dev_del.del_dev.uuid, del_dev->uuid, 16); } + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -392,6 +396,7 @@ esp_err_t esp_ble_mesh_provisioner_set_dev_uuid_match(const uint8_t *match_val, arg.set_dev_uuid_match.match_len = match_len; arg.set_dev_uuid_match.offset = offset; arg.set_dev_uuid_match.prov_after_match = prov_after_match; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -420,6 +425,7 @@ esp_err_t esp_ble_mesh_provisioner_set_prov_data_info(esp_ble_mesh_prov_data_inf } else if (prov_data_info->flag & PROV_DATA_IV_INDEX_FLAG) { arg.set_prov_data_info.prov_data.iv_index = prov_data_info->iv_index; } + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -441,6 +447,7 @@ esp_err_t esp_ble_mesh_provisioner_set_static_oob_value(const uint8_t *value, ui 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, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -461,23 +468,22 @@ esp_err_t esp_ble_mesh_provisioner_set_primary_elem_addr(uint16_t addr) 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, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } - #endif /* CONFIG_BLE_MESH_PROVISIONER */ /* The following APIs are for fast provisioning */ -#if (CONFIG_BLE_MESH_FAST_PROV) - +#if CONFIG_BLE_MESH_FAST_PROV esp_err_t esp_ble_mesh_set_fast_prov_info(esp_ble_mesh_fast_prov_info_t *fast_prov_info) { btc_ble_mesh_prov_args_t arg = {0}; btc_msg_t msg = {0}; if (fast_prov_info == NULL || (fast_prov_info->offset + - fast_prov_info->match_len > ESP_BLE_MESH_OCTET16_LEN)) { + fast_prov_info->match_len > ESP_BLE_MESH_OCTET16_LEN)) { return ESP_ERR_INVALID_ARG; } @@ -497,6 +503,7 @@ esp_err_t esp_ble_mesh_set_fast_prov_info(esp_ble_mesh_fast_prov_info_t *fast_pr if (fast_prov_info->match_len) { memcpy(arg.set_fast_prov_info.match_val, fast_prov_info->match_val, fast_prov_info->match_len); } + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } @@ -517,8 +524,8 @@ esp_err_t esp_ble_mesh_set_fast_prov_action(esp_ble_mesh_fast_prov_action_t acti msg.act = BTC_BLE_MESH_ACT_SET_FAST_PROV_ACTION; arg.set_fast_prov_action.action = action; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } - #endif /* CONFIG_BLE_MESH_FAST_PROV */ 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 cce62d63f616..582ea775571f 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 @@ -11,6 +11,8 @@ #include "btc_ble_mesh_prov.h" #include "esp_ble_mesh_defs.h" +#include "esp_ble_mesh_local_data_operation_api.h" +#include "esp_ble_mesh_proxy_api.h" esp_err_t esp_ble_mesh_proxy_identity_enable(void) { @@ -166,3 +168,54 @@ esp_err_t esp_ble_mesh_proxy_client_remove_filter_addr(uint8_t conn_handle, uint return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), btc_ble_mesh_prov_arg_deep_copy, btc_ble_mesh_prov_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } + +#if CONFIG_BLE_MESH_DF_CLI +esp_err_t esp_ble_mesh_proxy_client_directed_proxy_set(uint8_t conn_handle, uint16_t net_idx, + uint8_t use_directed) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (use_directed > ESP_BLE_MESH_PROXY_CLI_DIRECTED_FORWARDING_ENABLE) { + 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_PROXY_CLIENT_DIRECTED_PROXY_SET; + + arg.proxy_client_directed_proxy_set.conn_handle = conn_handle; + arg.proxy_client_directed_proxy_set.net_idx = net_idx; + arg.proxy_client_directed_proxy_set.use_directed = use_directed; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), btc_ble_mesh_prov_arg_deep_copy, btc_ble_mesh_prov_arg_deep_free) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_DF_CLI */ + +#if CONFIG_BLE_MESH_PROXY_SOLIC_PDU_TX +esp_err_t esp_ble_mesh_proxy_client_send_solic_pdu(uint8_t net_idx, uint16_t ssrc, uint16_t dst) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (esp_ble_mesh_find_element(ssrc) == 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_PROXY_CLIENT_SEND_SOLIC_PDU; + + arg.proxy_client_send_solic_pdu.net_idx = net_idx; + arg.proxy_client_send_solic_pdu.ssrc = ssrc; + arg.proxy_client_send_solic_pdu.dst = dst; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_PROXY_SOLIC_PDU_TX */ diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h index 80ab5707dc07..6818ba18aec6 100644 --- a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h @@ -76,7 +76,7 @@ typedef void (* esp_ble_mesh_ble_cb_t)(esp_ble_mesh_ble_cb_event_t event, /** * @brief Register BLE scanning callback. * - * @param[in] callback: Pointer to the BLE scaning callback function. + * @param[in] callback: Pointer to the BLE scanning callback function. * * @return ESP_OK on success or error code otherwise. * diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_local_data_operation_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_local_data_operation_api.h index 800786812675..e5e7c721edf4 100644 --- a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_local_data_operation_api.h +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_local_data_operation_api.h @@ -200,6 +200,24 @@ esp_err_t esp_ble_mesh_node_add_local_app_key(const uint8_t app_key[16], uint16_ esp_err_t esp_ble_mesh_node_bind_app_key_to_local_model(uint16_t element_addr, uint16_t company_id, uint16_t model_id, uint16_t app_idx); +/** + * @brief This function used to enable directed forwarding and directed forwarding relay on self. + * + * @param[in] net_idx: NetKey Index. + * @param[in] directed_forwarding: Enable or Disable directed forwarding. + * @param[in] directed_forwarding_relay: Enable or Disable directed forwarding relay. + * + * @note If the directed forwarding was set to disable, the directed forwarding relay + * must also be set to disable. + * + * @return ESP_OK on success or error code otherwise. + * +*/ +#if CONFIG_BLE_MESH_DF_SRV +esp_err_t esp_ble_mesh_enable_directed_forwarding(uint16_t net_idx, bool directed_forwarding, + bool directed_forwarding_relay); +#endif + #ifdef __cplusplus } #endif 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 01af79513825..2b512f1cd81a 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 @@ -60,7 +60,7 @@ esp_err_t esp_ble_mesh_model_msg_opcode_init(uint8_t *data, uint32_t opcode); /** * @brief Initialize the user-defined client model. All user-defined client models * shall call this function to initialize the client model internal data. - * Node: Before calling this API, the op_pair_size and op_pair variabled within + * Node: Before calling this API, the op_pair_size and op_pair variables within * the user_data(defined using esp_ble_mesh_client_t_) of the client model * need to be initialized. * @@ -168,7 +168,7 @@ esp_err_t esp_ble_mesh_server_model_update_state(esp_ble_mesh_model_t *model, * @brief Reset the provisioning procedure of the local BLE Mesh node. * * @note All provisioning information in this node will be deleted and the node - * needs to be reprovisioned. The API function esp_ble_mesh_node_prov_enable() + * needs to be re-provisioned. The API function esp_ble_mesh_node_prov_enable() * needs to be called to start a new provisioning procedure. * * @return ESP_OK on success or error code otherwise. @@ -641,6 +641,55 @@ uint8_t esp_ble_mesh_provisioner_get_free_settings_count(void); */ const uint8_t *esp_ble_mesh_get_fast_prov_app_key(uint16_t net_idx, uint16_t app_idx); +#if CONFIG_BLE_MESH_CERT_BASED_PROV +/** + * @brief This function is called by provisioner to send provisioning records + * get message. + * + * @param[in] link_idx: The provisioning link index. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_send_prov_records_get(uint16_t link_idx); + +/** + * @brief This function is called by provisioner to send provisioning record + * request message. + * + * @param[in] link_idx: The provisioning link index. + * @param[in] record_id: The record identity. + * @param[in] frag_offset: The starting offset of the fragment. + * @param[in] max_size: The max record fragment size. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_send_prov_record_req(uint16_t link_idx, uint16_t record_id, + uint16_t frag_offset, uint16_t max_size); + +/** + * @brief This function is called by provisioner to send provisioning invite + * message. + * + * @param[in] link_idx: The provisioning link index. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_send_prov_invite(uint16_t link_idx); + +/** + * @brief This function is called by provisioner to send link close + * + * @param[in] link_idx: The provisioning link index. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_send_link_close(uint16_t link_idx); +#endif /* #if CONFIG_BLE_MESH_CERT_BASED_PROV */ + #ifdef __cplusplus } #endif diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_proxy_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_proxy_api.h index c29e284481ee..b0f9c821045e 100644 --- a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_proxy_api.h +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_proxy_api.h @@ -13,6 +13,9 @@ extern "C" { #endif +#define ESP_BLE_MESH_PROXY_CLI_DIRECTED_FORWARDING_ENABLE 0x01 +#define ESP_BLE_MESH_PROXY_CLI_DIRECTED_FORWARDING_DISABLE 0x00 + /** * @brief Enable advertising with Node Identity. * @@ -111,6 +114,31 @@ esp_err_t esp_ble_mesh_proxy_client_add_filter_addr(uint8_t conn_handle, uint16_ esp_err_t esp_ble_mesh_proxy_client_remove_filter_addr(uint8_t conn_handle, uint16_t net_idx, uint16_t *addr, uint16_t addr_num); +/** + * @brief Proxy Client sets whether or not the Directed Proxy Server uses directed forwarding + * for Directed Proxy Client messages. + * + * @param[in] conn_handle: Proxy connection handle. + * @param[in] net_idx: Corresponding NetKey Index. + * @param[in] use_directed: Whether or not to send message by directed forwarding. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_proxy_client_directed_proxy_set(uint8_t conn_handle, uint16_t net_idx, + uint8_t use_directed); +/** + * @brief Proxy Client sends Solicitation PDU. + * + * @param[in] net_idx: Corresponding NetKey Index. + * @param[in] ssrc: Solicitation SRC, shall be one of its element address. + * @param[in] dst: Solicitation DST (TBD). + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_proxy_client_send_solic_pdu(uint8_t net_idx, uint16_t ssrc, uint16_t dst); + #ifdef __cplusplus } #endif 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 98677395a249..37f4bf05dc8d 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 @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -19,25 +19,28 @@ extern "C" { #endif /*!< The maximum length of a BLE Mesh message, including Opcode, Payload and TransMIC */ -#define ESP_BLE_MESH_SDU_MAX_LEN 384 +#define ESP_BLE_MESH_SDU_MAX_LEN 384 /*!< Length of a short Mesh MIC. */ -#define ESP_BLE_MESH_MIC_SHORT 4 +#define ESP_BLE_MESH_MIC_SHORT 4 /*!< Length of a long Mesh MIC. */ -#define ESP_BLE_MESH_MIC_LONG 8 +#define ESP_BLE_MESH_MIC_LONG 8 /*!< The maximum length of a BLE Mesh provisioned node name */ -#define ESP_BLE_MESH_NODE_NAME_MAX_LEN 31 +#define ESP_BLE_MESH_NODE_NAME_MAX_LEN 31 /*!< The maximum length of a BLE Mesh unprovisioned device name */ -#define ESP_BLE_MESH_DEVICE_NAME_MAX_LEN DEVICE_NAME_SIZE +#define ESP_BLE_MESH_DEVICE_NAME_MAX_LEN DEVICE_NAME_SIZE /*!< The maximum length of settings user id */ -#define ESP_BLE_MESH_SETTINGS_UID_SIZE 20 +#define ESP_BLE_MESH_SETTINGS_UID_SIZE 20 + +/*!< The default value of Random Update Interval Steps */ +#define ESP_BLE_MESH_RAND_UPDATE_INTERVAL_DEFAULT 0x3C /*!< Invalid settings index */ -#define ESP_BLE_MESH_INVALID_SETTINGS_IDX 0xFF +#define ESP_BLE_MESH_INVALID_SETTINGS_IDX 0xFF /*!< Define the BLE Mesh octet 16 bytes size */ #define ESP_BLE_MESH_OCTET16_LEN 16 @@ -48,69 +51,86 @@ typedef uint8_t esp_ble_mesh_octet16_t[ESP_BLE_MESH_OCTET16_LEN]; typedef uint8_t esp_ble_mesh_octet8_t[ESP_BLE_MESH_OCTET8_LEN]; /*!< Invalid Company ID */ -#define ESP_BLE_MESH_CID_NVAL 0xFFFF +#define ESP_BLE_MESH_CID_NVAL 0xFFFF /*!< Special TTL value to request using configured default TTL */ -#define ESP_BLE_MESH_TTL_DEFAULT 0xFF +#define ESP_BLE_MESH_TTL_DEFAULT 0xFF /*!< Maximum allowed TTL value */ -#define ESP_BLE_MESH_TTL_MAX 0x7F +#define ESP_BLE_MESH_TTL_MAX 0x7F -#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_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 0xFFFF -#define ESP_BLE_MESH_KEY_DEV 0xFFFE +#define ESP_BLE_MESH_KEY_UNUSED 0xFFFF +#define ESP_BLE_MESH_KEY_DEV 0xFFFE -#define ESP_BLE_MESH_KEY_PRIMARY 0x0000 -#define ESP_BLE_MESH_KEY_ANY 0xFFFF +#define ESP_BLE_MESH_KEY_PRIMARY 0x0000 +#define ESP_BLE_MESH_KEY_ANY 0xFFFF /*!< Primary Network Key index */ -#define ESP_BLE_MESH_NET_PRIMARY 0x000 +#define ESP_BLE_MESH_NET_PRIMARY 0x000 /*!< Relay state value */ -#define ESP_BLE_MESH_RELAY_DISABLED 0x00 -#define ESP_BLE_MESH_RELAY_ENABLED 0x01 -#define ESP_BLE_MESH_RELAY_NOT_SUPPORTED 0x02 +#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 0x00 -#define ESP_BLE_MESH_BEACON_ENABLED 0x01 +#define ESP_BLE_MESH_BEACON_DISABLED 0x00 +#define ESP_BLE_MESH_BEACON_ENABLED 0x01 + +#define ESP_BLE_MESH_PRIVATE_BEACON_DISABLE 0x00 +#define ESP_BLE_MESH_PRIVATE_BEACON_ENABLE 0x01 /*!< GATT Proxy state value */ -#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 +#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 + +#define ESP_BLE_MESH_PRIVATE_GATT_PROXY_DISABLED 0x00 +#define ESP_BLE_MESH_PRIVATE_GATT_PROXY_ENABLED 0x01 +#define ESP_BLE_MESH_PRIVATE_GATT_PROXY_NOT_SUPPORTED 0x02 + +#define ESP_BLE_MESH_PRIVATE_NODE_IDENTITY_DISABLED 0x00 +#define ESP_BLE_MESH_PRIVATE_NODE_IDENTITY_ENABLED 0x01 +#define ESP_BLE_MESH_PRIVATE_NODE_IDENTITY_NOT_SUPPORTED 0x02 /*!< Friend state value */ -#define ESP_BLE_MESH_FRIEND_DISABLED 0x00 -#define ESP_BLE_MESH_FRIEND_ENABLED 0x01 -#define ESP_BLE_MESH_FRIEND_NOT_SUPPORTED 0x02 +#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 0x00 -#define ESP_BLE_MESH_NODE_IDENTITY_RUNNING 0x01 -#define ESP_BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED 0x02 +#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 + +/*!< Subnet Bridge state value */ +#define ESP_BLE_MESH_SUBNET_BRIDGE_DISABLED 0x00 +#define ESP_BLE_MESH_SUBNET_BRIDGE_ENABLED 0x01 /*!< Supported features */ -#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) ((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 0xFFFF +#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) ((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 0xFFFF + +#define ESP_BLE_MESH_PROV_RECORD_MAX_ID 0x0013 /** @def ESP_BLE_MESH_TRANSMIT * @@ -238,7 +258,9 @@ typedef enum { ESP_BLE_MESH_PROV_OOB_NFC = BIT(4), ESP_BLE_MESH_PROV_OOB_NUMBER = BIT(5), ESP_BLE_MESH_PROV_OOB_STRING = BIT(6), - /* 7 - 10 are reserved */ + ESP_BLE_MESH_PROV_CERT_BASED = BIT(7), + ESP_BLE_MESH_PROV_RECORDS = BIT(8), + /* 9 - 10 are reserved */ ESP_BLE_MESH_PROV_OOB_ON_BOX = BIT(11), ESP_BLE_MESH_PROV_OOB_IN_BOX = BIT(12), ESP_BLE_MESH_PROV_OOB_ON_PAPER = BIT(13), @@ -317,34 +339,83 @@ typedef enum { .input_action = in_act, \ } -typedef uint8_t UINT8; -typedef uint16_t UINT16; -typedef uint32_t UINT32; -typedef uint64_t UINT64; - -#define BT_OCTET32_LEN 32 -typedef UINT8 BT_OCTET32[BT_OCTET32_LEN]; /* octet array: size 32 */ +typedef uint8_t UINT8 __attribute__((deprecated)); +typedef uint16_t UINT16 __attribute__((deprecated)); +typedef uint32_t UINT32 __attribute__((deprecated)); +typedef uint64_t UINT64 __attribute__((deprecated)); +#ifndef BT_OCTET32_LEN +#define BT_OCTET32_LEN 32 +typedef UINT8 BT_OCTET32[BT_OCTET32_LEN] __attribute__((deprecated)); +#endif #ifndef BD_ADDR_LEN #define BD_ADDR_LEN 6 -typedef uint8_t BD_ADDR[BD_ADDR_LEN]; +typedef uint8_t BD_ADDR[BD_ADDR_LEN] __attribute__((deprecated)); #endif 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 +#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 uint8_t esp_ble_mesh_addr_type_t; +#define ESP_BLE_MESH_DIRECTED_FORWARDING_DISABLED 0x00 +#define ESP_BLE_MESH_DIRECTED_FORWARDING_ENABLED 0x01 + +#define ESP_BLE_MESH_DIRECTED_RELAY_DISABLED 0x00 +#define ESP_BLE_MESH_DIRECTED_RELAY_ENABLED 0x01 + +#define ESP_BLE_MESH_DIRECTED_PROXY_IGNORE 0xFF +#define ESP_BLE_MESH_DIRECTED_PROXY_USE_DEFAULT_IGNORE 0xFF +#define ESP_BLE_MESH_DIRECTED_FRIEND_IGNORE 0xFF + +#define ESP_BLE_MESH_DIRECTED_PROXY_DISABLED 0x00 +#define ESP_BLE_MESH_DIRECTED_PROXY_ENABLED 0x01 +#define ESP_BLE_MESH_DIRECTED_PROXY_NOT_SUPPORTED 0x02 + +#define ESP_BLE_MESH_DIRECTED_PROXY_USE_DEF_DISABLED 0x00 +#define ESP_BLE_MESH_DIRECTED_PROXY_USE_DEF_ENABLED 0x01 +#define ESP_BLE_MESH_DIRECTED_PROXY_USE_DEF_NOT_SUPPORTED 0x02 + +#define ESP_BLE_MESH_DIRECTED_FRIEND_DISABLED 0x00 +#define ESP_BLE_MESH_DIRECTED_FRIEND_ENABLED 0x01 +#define ESP_BLE_MESH_DIRECTED_FRIEND_NOT_SUPPORTED 0x02 + +#define ESP_BLE_MESH_DIRECTED_PUB_POLICY_FLOODING 0x00 +#define ESP_BLE_MESH_DIRECTED_PUB_POLICY_FORWARD 0x01 + +#define ESP_BLE_MESH_PROXY_USE_DIRECTED_DISABLED 0x00 +#define ESP_BLE_MESH_PROXY_USE_DIRECTED_ENABLED 0x01 + +#define ESP_BLE_MESH_FLOODING_CRED 0x00 /* Managed flooding security credentials */ +#define ESP_BLE_MESH_FRIENDSHIP_CRED 0x01 /* Friendship security credentials */ +#define ESP_BLE_MESH_DIRECTED_CRED 0x02 /* Directed security credentials */ + +#define ESP_BLE_MESH_TAG_SEND_SEGMENTED BIT(0) /* Tagged with send-segmented */ +#define ESP_BLE_MESH_TAG_IMMUTABLE_CRED BIT(1) /* Tagged with immutable-credentials */ +#define ESP_BLE_MESH_TAG_USE_DIRECTED BIT(2) /* Tagged with use-directed */ +#define ESP_BLE_MESH_TAG_RELAY BIT(3) /* Tagged as relay */ +#define ESP_BLE_MESH_TAG_FRIENDSHIP BIT(4) /* Tagged as a friendship PDU */ + +#define ESP_BLE_MESH_SEG_SZMIC_SHORT 0 /* Using 4-octets TransMIC for a segmented message */ +#define ESP_BLE_MESH_SEG_SZMIC_LONG 1 /* Using 8-octets TransMIC for a segmented message */ + /** BLE Mesh deinit parameters */ typedef struct { bool erase_flash; /*!< Indicate if erasing flash when deinit mesh stack */ } esp_ble_mesh_deinit_param_t; +/** Format of Unicast Address Range */ +typedef struct { + uint16_t len_present:1, /*!< Indicate the presence or absence of the RangeLength field */ + range_start:15; /*!< 15 least significant bits of the starting unicast address */ + uint8_t range_length; /*!< Number of addresses in the range (0x02 - 0xFF) */ +} esp_ble_mesh_uar_t; + typedef struct esp_ble_mesh_model esp_ble_mesh_model_t; /** Abstraction that describes a BLE Mesh Element. @@ -374,7 +445,8 @@ typedef struct { uint16_t publish_addr; /*!< Publish Address. */ uint16_t app_idx:12, /*!< Publish AppKey Index. */ cred:1, /*!< Friendship Credentials Flag. */ - send_rel:1; /*!< Force reliable sending (segment acks) */ + send_rel:1, /*!< Force reliable sending (segment acks) */ + send_szmic:1; /*!< Size of TransMIC when publishing a Segmented Access message */ uint8_t ttl; /*!< Publish Time to Live. */ uint8_t retransmit; /*!< Retransmit Count & Interval Steps. */ @@ -384,7 +456,11 @@ typedef struct { fast_period:1, /*!< Use FastPeriodDivisor */ count:3; /*!< Retransmissions left. */ - uint32_t period_start; /*!< Start of the current period. */ + uint32_t period_start; /*!< Start of the current period. */ + +#if CONFIG_BLE_MESH_DF_SRV + uint8_t directed_pub_policy; /*!< Directed publish policy */ +#endif /** @brief Publication buffer, containing the publication message. * @@ -402,7 +478,7 @@ typedef struct { struct k_delayed_work timer; /** Role of the device that is going to publish messages */ - uint8_t dev_role; + uint8_t dev_role __attribute__((deprecated)); } esp_ble_mesh_model_pub_t; /** @def ESP_BLE_MESH_MODEL_PUB_DEFINE @@ -418,7 +494,6 @@ typedef struct { static esp_ble_mesh_model_pub_t _name = { \ .update = (uint32_t)NULL, \ .msg = &bt_mesh_pub_msg_##_name, \ - .dev_role = _role, \ } /** @def ESP_BLE_MESH_MODEL_OP @@ -472,7 +547,7 @@ struct esp_ble_mesh_model { const uint16_t model_id; /*!< 16-bit model identifier */ struct { uint16_t company_id; /*!< 16-bit company identifier */ - uint16_t model_id; /*!< 16-bit model identifier */ + uint16_t model_id; /*!< 16-bit model identifier */ } vnd; /*!< Structure encapsulating a model ID with a company ID */ }; @@ -524,26 +599,41 @@ typedef struct { /** Destination address of a received message. Not used for sending. */ uint16_t recv_dst; - /** RSSI of received packet. Not used for sending. */ + /** RSSI of a received message. Not used for sending. */ int8_t recv_rssi; + /** Opcode of a received message. Not used for sending. */ + uint32_t recv_op; + /** Received TTL value. Not used for sending. */ - uint8_t recv_ttl: 7; + uint8_t recv_ttl; + + /** Security credentials of a received message. Not used for sending. */ + uint8_t recv_cred; + + /** Tag of a received message. Not used for sending. */ + uint8_t recv_tag; - /** Force sending reliably by using segment acknowledgement */ - uint8_t send_rel: 1; + /** Force sending reliably by using segment acknowledgement. */ + uint8_t send_rel:1 __attribute__((deprecated)); + + /** Size of TransMIC when sending a Segmented Access message. */ + uint8_t send_szmic:1; /** TTL, or ESP_BLE_MESH_TTL_DEFAULT for default TTL. */ uint8_t send_ttl; - /** Opcode of a received message. Not used for sending message. */ - uint32_t recv_op; + /** Security credentials used for sending the message */ + uint8_t send_cred; + + /** Tag used for sending the message. */ + uint8_t send_tag; /** Model corresponding to the message, no need to be initialized before sending message */ - esp_ble_mesh_model_t *model; + esp_ble_mesh_model_t *model __attribute__((deprecated)); /** Indicate if the message is sent by a node server model, no need to be initialized before sending message */ - bool srv_send; + bool srv_send __attribute__((deprecated)); } esp_ble_mesh_msg_ctx_t; /** Provisioning properties & capabilities. @@ -576,6 +666,9 @@ typedef struct { /** Callback used to notify to set OOB Public Key. Initialized by the stack. */ esp_ble_mesh_cb_t oob_pub_key_cb; + /** OOB type */ + uint8_t oob_type; + /** Static OOB value */ const uint8_t *static_val; /** Static OOB value length */ @@ -668,6 +761,12 @@ typedef struct { esp_ble_mesh_cb_t provisioner_link_close; /** Callback used to indicate that a device is provisioned. Initialized by the stack. */ esp_ble_mesh_cb_t provisioner_prov_comp; + /** Callback used to indicate that certificate based provisioning needs to start. Initialized by the stack. */ + esp_ble_mesh_cb_t cert_based_prov_start; + /** Callback used to indicate that provisioner has received provisioning records list successfully. Initialized by the stack. */ + esp_ble_mesh_cb_t records_list_get; + /** Callback used to indicate that provisioner has received a complete provisioning record successfully. Initialized by the stack. */ + esp_ble_mesh_cb_t prov_record_recv_comp; #endif /* CONFIG_BLE_MESH_PROVISIONER */ } esp_ble_mesh_prov_t; @@ -698,8 +797,8 @@ typedef uint8_t esp_ble_mesh_dev_add_flag_t; /** Information of the device which is going to be added for provisioning. */ typedef struct { - esp_ble_mesh_bd_addr_t addr; /*!< Device address */ - esp_ble_mesh_addr_type_t addr_type; /*!< Device address type */ + esp_ble_mesh_bd_addr_t addr; /*!< Device address */ + esp_ble_mesh_addr_type_t addr_type; /*!< Device address type */ uint8_t uuid[16]; /*!< Device UUID */ uint16_t oob_info; /*!< Device OOB Info */ /*!< ADD_DEV_START_PROV_NOW_FLAG shall not be set if the bearer has both PB-ADV and PB-GATT enabled */ @@ -710,14 +809,16 @@ typedef struct { #define DEL_DEV_UUID_FLAG BIT(1) /** Information of the device which is going to be deleted. */ typedef struct { + /** Union of Device information */ union { + /** Device address */ struct { - esp_ble_mesh_bd_addr_t addr; /*!< Device address */ - esp_ble_mesh_addr_type_t addr_type; /*!< Device address type */ + esp_ble_mesh_bd_addr_t addr; /*!< Device address */ + esp_ble_mesh_addr_type_t addr_type; /*!< Device address type */ }; - uint8_t uuid[16]; /*!< Device UUID */ + uint8_t uuid[16]; /*!< Device UUID */ }; - uint8_t flag; /*!< BIT0: device address; BIT1: device UUID */ + uint8_t flag; /*!< BIT0: device address; BIT1: device UUID */ } esp_ble_mesh_device_delete_t; #define PROV_DATA_NET_IDX_FLAG BIT(0) @@ -725,6 +826,7 @@ typedef struct { #define PROV_DATA_IV_INDEX_FLAG BIT(2) /** Information of the provisioner which is going to be updated. */ typedef struct { + /** Provisioning data */ union { uint16_t net_idx; /*!< NetKey Index */ uint8_t flags; /*!< Flags */ @@ -828,6 +930,13 @@ typedef enum { ESP_BLE_MESH_PROVISIONER_PROV_LINK_OPEN_EVT, /*!< Provisioner establish a BLE Mesh link event */ 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_CERT_BASED_PROV_START_EVT, /*!< Provisioner initiate a certificate based provisioning */ + ESP_BLE_MESH_PROVISIONER_RECV_PROV_RECORDS_LIST_EVT, /*!< Provisioner receive provisioning records list event */ + ESP_BLE_MESH_PROVISIONER_PROV_RECORD_RECV_COMP_EVT, /*!< Provisioner receive provisioning record complete event */ + ESP_BLE_MESH_PROVISIONER_SEND_PROV_RECORDS_GET_EVT, /*!< Provisioner send provisioning records get to device event */ + ESP_BLE_MESH_PROVISIONER_SEND_PROV_RECORD_REQUEST_EVT, /*!< Provisioner send provisioning record request to device event */ + ESP_BLE_MESH_PROVISIONER_SEND_PROV_INVITE_EVT, /*!< Provisioner send provisioning invite to device event */ + ESP_BLE_MESH_PROVISIONER_SEND_LINK_CLOSE_EVT, /*!< Provisioner send link close to device 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 completion event */ @@ -877,8 +986,10 @@ typedef enum { ESP_BLE_MESH_PROXY_CLIENT_SET_FILTER_TYPE_COMP_EVT, /*!< Proxy Client set filter type completion event */ ESP_BLE_MESH_PROXY_CLIENT_ADD_FILTER_ADDR_COMP_EVT, /*!< Proxy Client add filter address completion event */ ESP_BLE_MESH_PROXY_CLIENT_REMOVE_FILTER_ADDR_COMP_EVT, /*!< Proxy Client remove filter address completion event */ + ESP_BLE_MESH_PROXY_CLIENT_DIRECTED_PROXY_SET_COMP_EVT, /*!< Proxy Client directed proxy set completion event */ ESP_BLE_MESH_PROXY_SERVER_CONNECTED_EVT, /*!< Proxy Server establishes connection successfully event */ ESP_BLE_MESH_PROXY_SERVER_DISCONNECTED_EVT, /*!< Proxy Server terminates connection successfully event */ + ESP_BLE_MESH_PROXY_CLIENT_SEND_SOLIC_PDU_COMP_EVT, /*!< Proxy Client send Solicitation PDU completion event */ ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT, /*!< Local model subscribes group address completion event */ ESP_BLE_MESH_MODEL_UNSUBSCRIBE_GROUP_ADDR_COMP_EVT, /*!< Local model unsubscribes group address completion event */ ESP_BLE_MESH_DEINIT_MESH_COMP_EVT, /*!< De-initialize BLE Mesh stack completion event */ @@ -924,6 +1035,7 @@ typedef union { */ struct ble_mesh_link_close_evt_param { esp_ble_mesh_prov_bearer_t bearer; /*!< Type of the bearer used when device link is closed */ + uint8_t reason; /*!< Reason of the closed provisioning link */ } node_prov_link_close; /*!< Event parameter of ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT */ /** * @brief ESP_BLE_MESH_NODE_PROV_OUTPUT_NUMBER_EVT @@ -1030,7 +1142,7 @@ typedef union { esp_ble_mesh_bd_addr_t addr; /*!< Device address of the unprovisioned device */ esp_ble_mesh_addr_type_t addr_type; /*!< Device address type */ uint16_t oob_info; /*!< OOB Info of the unprovisioned device */ - uint8_t adv_type; /*!< Avertising type of the unprovisioned device */ + uint8_t adv_type; /*!< Advertising type of the unprovisioned device */ esp_ble_mesh_prov_bearer_t bearer; /*!< Bearer of the unprovisioned 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 */ @@ -1075,6 +1187,7 @@ typedef union { esp_ble_mesh_input_action_t action; /*!< Action of device Input OOB Authentication */ uint8_t size; /*!< Size of device Input OOB Authentication */ uint8_t link_idx; /*!< Index of the provisioning link */ + /** Union of output OOB */ union { char string[8]; /*!< String output by the Provisioner */ uint32_t number; /*!< Number output by the Provisioner */ @@ -1097,6 +1210,62 @@ typedef union { uint8_t element_num; /*!< Element count of the provisioned device */ uint16_t netkey_idx; /*!< NetKey Index of the provisioned device */ } provisioner_prov_complete; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_CERT_BASED_PROV_START_EVT + */ + struct ble_mesh_provisioner_cert_based_prov_start_evt_param { + uint16_t link_idx; /*!< Index of the provisioning link */ + } provisioner_cert_based_prov_start; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_CERT_BASED_PROV_START_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_RECV_PROV_RECORDS_LIST_EVT + */ + struct ble_mesh_provisioner_recv_prov_records_list_evt_param { + uint16_t link_idx; /*!< Index of the provisioning link */ + uint16_t len; /*!< Length of message */ + uint8_t *msg; /*!< Lists the Record IDs of the provisioning records stored on the Provisionee */ + } recv_provisioner_records_list; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_RECV_PROV_RECORDS_LIST_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_RECORD_RECV_COMP_EVT + */ + struct ble_mesh_provisioner_prov_record_recv_comp_evt_param { + uint8_t status; /*!< Indicates whether or not the request was handled successfully */ + uint16_t link_idx; /*!< Index of the provisioning link */ + uint16_t record_id; /*!< Identifies the provisioning record for which the request is made */ + uint16_t frag_offset; /*!< The starting offset of the requested fragment in the provisioning record data */ + uint16_t total_len; /*!< Total length of the provisioning record data stored on the Provisionee */ + uint8_t *record; /*!< Provisioning record data fragment */ + } provisioner_prov_record_recv_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_RECORD_RECV_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_SEND_PROV_RECORDS_GET_EVT + */ + struct ble_mesh_provisioner_send_prov_records_get_evt_param { + int err_code; /*!< Indicate the result of send Provisioning Records List Get message */ + uint16_t link_idx; /*!< Index of the provisioning link */ + } provisioner_send_records_get; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SEND_PROV_RECORDS_GET_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_SEND_PROV_RECORD_REQUEST_EVT + */ + struct ble_mesh_provisioner_send_prov_record_req_evt_param { + int err_code; /*!< Indicate the result of send Provisioning Record Request message */ + uint16_t link_idx; /*!< Index of the provisioning link */ + uint16_t record_id; /*!< Identifies the provisioning record for which the request is made */ + uint16_t frag_offset; /*!< The starting offset of the requested fragment in the provisioning record data */ + uint16_t max_size; /*!< The maximum size of the provisioning record fragment that the Provisioner can receive */ + } provisioner_send_record_req; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SEND_PROV_RECORD_REQUEST_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_SEND_PROV_INVITE_EVT + */ + struct ble_mesh_provisioner_send_prov_invite_evt_param { + uint16_t link_idx; /*!< Index of the provisioning link */ + int err_code; /*!< Indicate the result of send Provisioning Invite message */ + } provisioner_send_prov_invite; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SEND_PROV_INVITE_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_SEND_LINK_CLOSE_EVT + */ + struct ble_mesh_provisioner_send_link_close_evt_param { + uint16_t link_idx; /*!< Index of the provisioning link */ + int err_code; /*!< Indicate the result of send Link Close message */ + } provisioner_send_link_close; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SEND_LINK_CLOSE_EVT */ /** * @brief ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT */ @@ -1461,6 +1630,14 @@ typedef union { uint8_t conn_handle; /*!< Proxy connection handle */ uint16_t net_idx; /*!< Corresponding NetKey Index */ } proxy_client_remove_filter_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_REMOVE_FILTER_ADDR_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROXY_CLIENT_DIRECTED_PROXY_CONTROL_COMP_EVT + */ + struct ble_mesh_proxy_client_directed_proxy_set_param { + int err_code; /*!< Indicate the result of Proxy Client directed proxy control address */ + uint8_t conn_handle; /*!< Proxy connection handle */ + uint16_t net_idx; /*!< Corresponding NetKey Index */ + } proxy_client_directed_proxy_set_comp; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_DIRECTED_PROXY_SET_COMP_EVT */ /** * @brief ESP_BLE_MESH_PROXY_SERVER_CONNECTED_EVT */ @@ -1474,6 +1651,15 @@ typedef union { uint8_t conn_handle; /*!< Proxy connection handle */ uint8_t reason; /*!< Proxy disconnect reason */ } proxy_server_disconnected; /*!< Event parameter of ESP_BLE_MESH_PROXY_SERVER_DISCONNECTED_EVT */ + /** + * @brief ESP_BLE_MESH_PROXY_CLIENT_SEND_SOLIC_PDU_COMP_EVT + */ + struct { + int err_code; /*!< Indicate the result of Proxy Client send Solicitation PDU */ + uint16_t net_idx; /*!< Corresponding NetKey Index */ + uint16_t ssrc; /*!< Solicitation SRC */ + uint16_t dst; /*!< Solicitation DST */ + } proxy_client_send_solic_pdu_comp; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_SEND_SOLIC_PDU_COMP_EVT */ /** * @brief ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT */ @@ -1500,7 +1686,7 @@ typedef union { struct ble_mesh_deinit_mesh_comp_param { int err_code; /*!< Indicate the result of BLE Mesh deinitialization */ } deinit_mesh_comp; /*!< Event parameter of ESP_BLE_MESH_DEINIT_MESH_COMP_EVT */ -} esp_ble_mesh_prov_cb_param_t; +} esp_ble_mesh_prov_cb_param_t; /*!< Event parameters of ESP_BLE_MESH_DEINIT_MESH_COMP_EVT */ /** * @brief BLE Mesh models related Model ID and Opcode definitions @@ -1511,6 +1697,24 @@ typedef union { #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 +#define ESP_BLE_MESH_MODEL_ID_RPR_SRV 0x0004 +#define ESP_BLE_MESH_MODEL_ID_RPR_CLI 0x0005 +#define ESP_BLE_MESH_MODEL_ID_DF_SRV 0x0006 +#define ESP_BLE_MESH_MODEL_ID_DF_CLI 0x0007 +#define ESP_BLE_MESH_MODEL_ID_BRC_SRV 0x0008 +#define ESP_BLE_MESH_MODEL_ID_BRC_CLI 0x0009 +#define ESP_BLE_MESH_MODEL_ID_PRB_SRV 0x000A +#define ESP_BLE_MESH_MODEL_ID_PRB_CLI 0x000B +#define ESP_BLE_MESH_MODEL_ID_ODP_SRV 0x000C +#define ESP_BLE_MESH_MODEL_ID_ODP_CLI 0x000D +#define ESP_BLE_MESH_MODEL_ID_SAR_SRV 0x000E +#define ESP_BLE_MESH_MODEL_ID_SAR_CLI 0x000F +#define ESP_BLE_MESH_MODEL_ID_AGG_SRV 0x0010 +#define ESP_BLE_MESH_MODEL_ID_AGG_CLI 0x0011 +#define ESP_BLE_MESH_MODEL_ID_LCD_SRV 0x0012 +#define ESP_BLE_MESH_MODEL_ID_LCD_CLI 0x0013 +#define ESP_BLE_MESH_MODEL_ID_SRPL_SRV 0x0014 +#define ESP_BLE_MESH_MODEL_ID_SRPL_CLI 0x0015 /*!< Models from the Mesh Model Specification */ #define ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV 0x1000 @@ -1565,6 +1769,8 @@ typedef union { #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 +#define ESP_BLE_MESH_MODEL_ID_MBT_SRV 0x1400 +#define ESP_BLE_MESH_MODEL_ID_MBT_CLI 0x1401 /** * esp_ble_mesh_opcode_config_client_get_t belongs to esp_ble_mesh_opcode_t, this typedef is only @@ -1686,6 +1892,10 @@ typedef uint8_t esp_ble_mesh_cfg_status_t; #define ESP_BLE_MESH_CFG_STATUS_CANNOT_SET 0x0F #define ESP_BLE_MESH_CFG_STATUS_UNSPECIFIED_ERROR 0x10 #define ESP_BLE_MESH_CFG_STATUS_INVALID_BINDING 0x11 +#define ESP_BLE_MESH_CFG_STATUS_INVALID_PATH_ENTRY 0x12 +#define ESP_BLE_MESH_CFG_STATUS_CANNOT_GET 0x13 +#define ESP_BLE_MESH_CFG_STATUS_OBSOLETE_INFO 0x14 +#define ESP_BLE_MESH_CFG_STATUS_INVALID_BEARER 0x15 /** * esp_ble_mesh_opcode_health_client_get_t belongs to esp_ble_mesh_opcode_t, this typedef is @@ -2037,11 +2247,12 @@ typedef struct { /** Client Model user data context. */ typedef struct { esp_ble_mesh_model_t *model; /*!< Pointer to the client model. Initialized by the stack. */ - int op_pair_size; /*!< Size of the op_pair */ + uint32_t op_pair_size; /*!< Size of the op_pair */ const esp_ble_mesh_client_op_pair_t *op_pair; /*!< Table containing get/set message opcode and corresponding status message opcode */ uint32_t publish_status; /*!< Callback used to handle the received unsolicited message. Initialized by the stack. */ void *internal_data; /*!< Pointer to the internal data of client model */ - uint8_t msg_role; /*!< Role of the device (Node/Provisioner) that is going to send messages */ + void *vendor_data; /*!< Pointer to the vendor data of client model */ + uint8_t msg_role __attribute__((deprecated)); /*!< Role of the device (Node/Provisioner) that is going to send messages */ } esp_ble_mesh_client_t; /** Common parameters of the messages sent by Client Model. */ @@ -2051,7 +2262,7 @@ typedef struct { esp_ble_mesh_msg_ctx_t ctx; /*!< The context used to send message */ int32_t msg_timeout; /*!< Timeout value (ms) to get response to the sent message */ /*!< Note: if using default timeout value in menuconfig, make sure to set this value to 0 */ - uint8_t msg_role; /*!< Role of the device - Node/Provisioner */ + uint8_t msg_role __attribute__((deprecated)); /*!< Role of the device - Node/Provisioner */ } esp_ble_mesh_client_common_param_t; /** diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_config_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_config_model_api.h index 31369d895672..b444465e3e7c 100644 --- a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_config_model_api.h +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_config_model_api.h @@ -42,7 +42,7 @@ extern "C" { NULL, NULL, cli_data) /** Configuration Server Model context */ -typedef struct esp_ble_mesh_cfg_srv { +typedef struct { esp_ble_mesh_model_t *model; /*!< Pointer to Configuration Server Model */ uint8_t net_transmit; /*!< Network Transmit state */ @@ -316,7 +316,7 @@ typedef struct { } esp_ble_mesh_cfg_net_transmit_set_t; /** Parameters of Config Model Heartbeat Publication Set. */ -typedef struct { +typedef struct { uint16_t dst; /*!< Destination address for Heartbeat messages */ uint8_t count; /*!< Number of Heartbeat messages to be sent */ uint8_t period; /*!< Period for sending Heartbeat messages */ @@ -610,6 +610,7 @@ typedef enum { * @brief Configuration Server model related context. */ +/** Parameters of Config Model Publication Set */ typedef struct { uint16_t element_addr; /*!< Element Address */ uint16_t pub_addr; /*!< Publish Address */ @@ -622,6 +623,19 @@ typedef struct { uint16_t model_id; /*!< Model ID */ } esp_ble_mesh_state_change_cfg_mod_pub_set_t; +/** Parameters of Config Model Publication Virtual Address Set */ +typedef struct { + uint16_t element_addr; /*!< Element Address */ + uint8_t label_uuid[16]; /*!< Label UUID */ + uint16_t app_idx; /*!< AppKey Index */ + bool cred_flag; /*!< Friendship Credential Flag */ + uint8_t pub_ttl; /*!< Publish TTL */ + uint8_t pub_period; /*!< Publish Period */ + uint8_t pub_retransmit; /*!< Publish Retransmit */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ +} esp_ble_mesh_state_change_cfg_mod_pub_va_set_t; + /** Parameters of Config Model Subscription Add */ typedef struct { uint16_t element_addr; /*!< Element Address */ @@ -705,6 +719,7 @@ typedef union { * The recv_op in ctx can be used to decide which state is changed. */ esp_ble_mesh_state_change_cfg_mod_pub_set_t mod_pub_set; /*!< Config Model Publication Set */ + esp_ble_mesh_state_change_cfg_mod_pub_va_set_t mod_pub_va_set; /*!< Config Model Publication Virtual Address Set */ esp_ble_mesh_state_change_cfg_model_sub_add_t mod_sub_add; /*!< Config Model Subscription Add */ esp_ble_mesh_state_change_cfg_model_sub_delete_t mod_sub_delete; /*!< Config Model Subscription Delete */ esp_ble_mesh_state_change_cfg_netkey_add_t netkey_add; /*!< Config NetKey Add */ diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_lighting_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_lighting_model_api.h index 48f45f5e4f20..c610c77a96ed 100644 --- a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_lighting_model_api.h +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_lighting_model_api.h @@ -134,7 +134,7 @@ typedef struct { typedef struct { bool op_en; /*!< Indicate if optional parameters are included */ uint16_t ctl_lightness; /*!< Target value of light ctl lightness state */ - uint16_t ctl_temperatrue; /*!< Target value of light ctl temperature state */ + uint16_t ctl_temperature; /*!< Target value of light ctl temperature state */ int16_t ctl_delta_uv; /*!< Target value of light ctl delta UV state */ uint8_t tid; /*!< Transaction ID */ uint8_t trans_time; /*!< Time to complete state transition (optional) */ @@ -144,7 +144,7 @@ typedef struct { /** Parameters of Light CTL Temperature Set */ typedef struct { bool op_en; /*!< Indicate if optional parameters are included */ - uint16_t ctl_temperatrue; /*!< Target value of light ctl temperature state */ + uint16_t ctl_temperature; /*!< Target value of light ctl temperature state */ int16_t ctl_delta_uv; /*!< Target value of light ctl delta UV state */ uint8_t tid; /*!< Transaction ID */ uint8_t trans_time; /*!< Time to complete state transition (optional) */ diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_sensor_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_sensor_model_api.h index 4f051a0b2658..ba72c2238dde 100644 --- a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_sensor_model_api.h +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_sensor_model_api.h @@ -141,7 +141,7 @@ typedef struct { } esp_ble_mesh_sensor_settings_status_cb_t; /** Parameters of Sensor Setting Status */ -typedef struct { +typedef struct { bool op_en; /*!< Indicate id optional parameters are included */ uint16_t sensor_property_id; /*!< Property ID identifying a sensor */ uint16_t sensor_setting_property_id; /*!< Setting ID identifying a setting within a sensor */ diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_time_scene_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_time_scene_model_api.h index 07e5a5ba0175..34700f8b7266 100644 --- a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_time_scene_model_api.h +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_time_scene_model_api.h @@ -468,7 +468,7 @@ esp_err_t esp_ble_mesh_time_scene_client_set_state(esp_ble_mesh_client_common_pa #define ESP_BLE_MESH_TIME_NONE 0x00 /*!< Time Role - None */ #define ESP_BLE_MESH_TIME_AUTHORITY 0x01 /*!< Time Role - Mesh Time Authority */ #define ESP_BLE_MESH_TIME_RELAY 0x02 /*!< Time Role - Mesh Time Relay */ -#define ESP_BLE_MESH_TIME_CLINET 0x03 /*!< Time Role - Mesh Time Client */ +#define ESP_BLE_MESH_TIME_CLIENT 0x03 /*!< Time Role - Mesh Time Client */ #define ESP_BLE_MESH_SCENE_SUCCESS 0x00 /*!< Scene operation - Success */ #define ESP_BLE_MESH_SCENE_REG_FULL 0x01 /*!< Scene operation - Scene Register Full */ @@ -482,7 +482,7 @@ typedef struct { uint8_t uncertainty; /*!< The value of the Uncertainty field */ uint8_t time_zone_offset_curr; /*!< The value of the Time Zone Offset Current field */ uint8_t time_zone_offset_new; /*!< The value of the Time Zone Offset New state */ - uint8_t tai_zone_change[5]; /*!< The value of the TAI of Zone Chaneg field */ + uint8_t tai_zone_change[5]; /*!< The value of the TAI of Zone Change field */ uint16_t time_authority : 1, /*!< The value of the Time Authority bit */ tai_utc_delta_curr : 15; /*!< The value of the TAI-UTC Delta Current state */ uint16_t tai_utc_delta_new : 15; /*!< The value of the TAI-UTC Delta New state */ 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 2f6115f6e448..c1802e1137e3 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 @@ -7,8 +7,8 @@ #include #include +#include "btc_ble_mesh_model_common.h" #include "btc_ble_mesh_config_model.h" -#include "foundation.h" #include "esp_ble_mesh_config_model_api.h" #if CONFIG_BLE_MESH_CFG_CLI @@ -50,7 +50,7 @@ void btc_ble_mesh_config_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void 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)); + sizeof(esp_ble_mesh_cfg_client_get_state_t)); } else { BT_ERR("%s, Out of memory, act %d", __func__, msg->act); } @@ -70,7 +70,7 @@ void btc_ble_mesh_config_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void 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)); + sizeof(esp_ble_mesh_cfg_client_set_state_t)); } else { BT_ERR("%s, Out of memory, act %d", __func__, msg->act); } @@ -143,8 +143,8 @@ static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_des case ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT: if (p_src_data->params) { switch (p_src_data->params->opcode) { - case OP_DEV_COMP_DATA_GET: - case OP_DEV_COMP_DATA_STATUS: + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET: + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_STATUS: if (p_src_data->status_cb.comp_data_status.composition_data) { 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); @@ -157,10 +157,10 @@ static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_des p_src_data->status_cb.comp_data_status.composition_data->len); } break; - case OP_MOD_SUB_GET: - case OP_MOD_SUB_GET_VND: - case OP_MOD_SUB_LIST: - case OP_MOD_SUB_LIST_VND: + 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_SIG_MODEL_SUB_LIST: + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_LIST: if (p_src_data->status_cb.model_sub_list.sub_addr) { 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); @@ -173,8 +173,8 @@ static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_des p_src_data->status_cb.model_sub_list.sub_addr->len); } break; - case OP_NET_KEY_GET: - case OP_NET_KEY_LIST: + case ESP_BLE_MESH_MODEL_OP_NET_KEY_GET: + case ESP_BLE_MESH_MODEL_OP_NET_KEY_LIST: if (p_src_data->status_cb.netkey_list.net_idx) { 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); @@ -187,8 +187,8 @@ static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_des p_src_data->status_cb.netkey_list.net_idx->len); } break; - case OP_APP_KEY_GET: - case OP_APP_KEY_LIST: + case ESP_BLE_MESH_MODEL_OP_APP_KEY_GET: + case ESP_BLE_MESH_MODEL_OP_APP_KEY_LIST: if (p_src_data->status_cb.appkey_list.app_idx) { 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); @@ -201,10 +201,10 @@ static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_des p_src_data->status_cb.appkey_list.app_idx->len); } break; - case OP_SIG_MOD_APP_GET: - case OP_VND_MOD_APP_GET: - case OP_SIG_MOD_APP_LIST: - case OP_VND_MOD_APP_LIST: + 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_SIG_MODEL_APP_LIST: + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_LIST: if (p_src_data->status_cb.model_app_list.app_idx) { 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); @@ -245,28 +245,28 @@ static void btc_ble_mesh_config_client_free_req_data(btc_msg_t *msg) case ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT: if (arg->params) { switch (arg->params->opcode) { - case OP_DEV_COMP_DATA_GET: - case OP_DEV_COMP_DATA_STATUS: + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET: + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_STATUS: bt_mesh_free_buf(arg->status_cb.comp_data_status.composition_data); break; - case OP_MOD_SUB_GET: - case OP_MOD_SUB_GET_VND: - case OP_MOD_SUB_LIST: - case OP_MOD_SUB_LIST_VND: + 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_SIG_MODEL_SUB_LIST: + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_LIST: bt_mesh_free_buf(arg->status_cb.model_sub_list.sub_addr); break; - case OP_NET_KEY_GET: - case OP_NET_KEY_LIST: + case ESP_BLE_MESH_MODEL_OP_NET_KEY_GET: + case ESP_BLE_MESH_MODEL_OP_NET_KEY_LIST: bt_mesh_free_buf(arg->status_cb.netkey_list.net_idx); break; - case OP_APP_KEY_GET: - case OP_APP_KEY_LIST: + case ESP_BLE_MESH_MODEL_OP_APP_KEY_GET: + case ESP_BLE_MESH_MODEL_OP_APP_KEY_LIST: bt_mesh_free_buf(arg->status_cb.appkey_list.app_idx); break; - case OP_SIG_MOD_APP_GET: - case OP_VND_MOD_APP_GET: - case OP_SIG_MOD_APP_LIST: - case OP_VND_MOD_APP_LIST: + 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_SIG_MODEL_APP_LIST: + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_LIST: bt_mesh_free_buf(arg->status_cb.model_app_list.app_idx); break; default: @@ -287,8 +287,6 @@ static void btc_ble_mesh_config_client_callback(esp_ble_mesh_cfg_client_cb_param { btc_msg_t msg = {0}; - BT_DBG("%s", __func__); - /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_CONFIG_CLIENT)) { return; @@ -311,7 +309,7 @@ void bt_mesh_config_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, esp_ble_mesh_client_common_param_t params = {0}; uint8_t act = 0U; - if (!model || !ctx) { + if (!model || !ctx || len > sizeof(cb_params.status_cb)) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -349,14 +347,14 @@ void bt_mesh_config_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, cb_params.params = ¶ms; if (val && len) { - memcpy(&cb_params.status_cb, val, MIN(len, sizeof(cb_params.status_cb))); + memcpy(&cb_params.status_cb, val, len); } btc_ble_mesh_config_client_callback(&cb_params, act); - return; } -void btc_ble_mesh_config_client_publish_callback(uint32_t opcode, struct bt_mesh_model *model, +void btc_ble_mesh_config_client_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { @@ -367,7 +365,6 @@ void btc_ble_mesh_config_client_publish_callback(uint32_t opcode, struct bt_mesh bt_mesh_config_client_cb_evt_to_btc(opcode, BTC_BLE_MESH_EVT_CONFIG_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); - return; } static int btc_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_t *params, @@ -400,15 +397,7 @@ static int btc_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param break; } - param.opcode = params->opcode; - param.model = (struct bt_mesh_model *)params->model; - param.ctx.net_idx = params->ctx.net_idx; - param.ctx.app_idx = BLE_MESH_KEY_DEV; - param.ctx.addr = params->ctx.addr; - param.ctx.send_rel = params->ctx.send_rel; - param.ctx.send_ttl = params->ctx.send_ttl; - param.msg_timeout = params->msg_timeout; - param.msg_role = params->msg_role; + btc_ble_mesh_set_client_common_param(params, ¶m, true); switch (param.opcode) { case ESP_BLE_MESH_MODEL_OP_BEACON_GET: @@ -461,8 +450,6 @@ static int btc_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param BT_ERR("Invalid Configuration Get opcode 0x%04x", param.opcode); return -EINVAL; } - - return 0; } static int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_t *params, @@ -480,15 +467,7 @@ static int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param return -EINVAL; } - param.opcode = params->opcode; - param.model = (struct bt_mesh_model *)params->model; - param.ctx.net_idx = params->ctx.net_idx; - param.ctx.app_idx = BLE_MESH_KEY_DEV; - param.ctx.addr = params->ctx.addr; - param.ctx.send_rel = params->ctx.send_rel; - param.ctx.send_ttl = params->ctx.send_ttl; - param.msg_timeout = params->msg_timeout; - param.msg_role = params->msg_role; + btc_ble_mesh_set_client_common_param(params, ¶m, true); switch (param.opcode) { case ESP_BLE_MESH_MODEL_OP_BEACON_SET: @@ -609,8 +588,6 @@ static int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param BT_ERR("Invalid Configuration Set opcode 0x%04x", param.opcode); return -EINVAL; } - - return 0; } void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg) @@ -649,7 +626,6 @@ void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg) } btc_ble_mesh_config_client_arg_deep_free(msg); - return; } void btc_ble_mesh_config_client_cb_handler(btc_msg_t *msg) @@ -670,7 +646,6 @@ void btc_ble_mesh_config_client_cb_handler(btc_msg_t *msg) } btc_ble_mesh_config_client_free_req_data(msg); - return; } #endif /* CONFIG_BLE_MESH_CFG_CLI */ @@ -691,8 +666,6 @@ static void btc_ble_mesh_config_server_callback(esp_ble_mesh_cfg_server_cb_param { btc_msg_t msg = {0}; - BT_DBG("%s", __func__); - /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_CONFIG_SERVER)) { return; @@ -705,14 +678,15 @@ static void btc_ble_mesh_config_server_callback(esp_ble_mesh_cfg_server_cb_param btc_transfer_context(&msg, cb_params, cb_params == NULL ? 0 : sizeof(esp_ble_mesh_cfg_server_cb_param_t), NULL, NULL); } -void bt_mesh_config_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model *model, +void bt_mesh_config_server_cb_evt_to_btc(uint8_t evt_type, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const uint8_t *val, size_t len) { esp_ble_mesh_cfg_server_cb_param_t cb_params = {0}; uint8_t act = 0U; - if (!model || !ctx) { + if (!model || !ctx || len > sizeof(cb_params.value)) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -737,11 +711,10 @@ void bt_mesh_config_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model cb_params.ctx.send_ttl = ctx->send_ttl; if (val && len) { - memcpy(&cb_params.value, val, MIN(len, sizeof(cb_params.value))); + memcpy(&cb_params.value, val, len); } btc_ble_mesh_config_server_callback(&cb_params, act); - return; } void btc_ble_mesh_config_server_cb_handler(btc_msg_t *msg) 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 f36074a6e777..b6680f997876 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 @@ -7,6 +7,7 @@ #include #include +#include "btc_ble_mesh_model_common.h" #include "btc_ble_mesh_generic_model.h" #include "esp_ble_mesh_generic_model_api.h" @@ -357,8 +358,6 @@ static void btc_ble_mesh_generic_client_callback(esp_ble_mesh_generic_client_cb_ { btc_msg_t msg = {0}; - BT_DBG("%s", __func__); - /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_GENERIC_CLIENT)) { return; @@ -381,7 +380,7 @@ void bt_mesh_generic_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, esp_ble_mesh_client_common_param_t params = {0}; uint8_t act = 0U; - if (!model || !ctx) { + if (!model || !ctx || len > sizeof(cb_params.status_cb)) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -419,14 +418,14 @@ void bt_mesh_generic_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, cb_params.params = ¶ms; if (val && len) { - memcpy(&cb_params.status_cb, val, MIN(len, sizeof(cb_params.status_cb))); + memcpy(&cb_params.status_cb, val, len); } btc_ble_mesh_generic_client_callback(&cb_params, act); - return; } -void btc_ble_mesh_generic_client_publish_callback(uint32_t opcode, struct bt_mesh_model *model, +void btc_ble_mesh_generic_client_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { @@ -437,15 +436,13 @@ void btc_ble_mesh_generic_client_publish_callback(uint32_t opcode, struct bt_mes bt_mesh_generic_client_cb_evt_to_btc(opcode, BTC_BLE_MESH_EVT_GENERIC_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); - return; } void btc_ble_mesh_generic_client_call_handler(btc_msg_t *msg) { - esp_ble_mesh_client_common_param_t *params = NULL; - btc_ble_mesh_generic_client_args_t *arg = NULL; esp_ble_mesh_generic_client_cb_param_t cb = {0}; - bt_mesh_client_common_param_t common = {0}; + btc_ble_mesh_generic_client_args_t *arg = NULL; + bt_mesh_client_common_param_t param = {0}; if (!msg) { BT_ERR("%s, Invalid parameter", __func__); @@ -455,52 +452,31 @@ void btc_ble_mesh_generic_client_call_handler(btc_msg_t *msg) arg = (btc_ble_mesh_generic_client_args_t *)(msg->arg); switch (msg->act) { - case BTC_BLE_MESH_ACT_GENERIC_CLIENT_GET_STATE: { - params = arg->generic_client_get_state.params; - common.opcode = params->opcode; - common.model = (struct bt_mesh_model *)params->model; - common.ctx.net_idx = params->ctx.net_idx; - common.ctx.app_idx = params->ctx.app_idx; - common.ctx.addr = params->ctx.addr; - common.ctx.send_rel = params->ctx.send_rel; - common.ctx.send_ttl = params->ctx.send_ttl; - common.msg_timeout = params->msg_timeout; - common.msg_role = params->msg_role; + case BTC_BLE_MESH_ACT_GENERIC_CLIENT_GET_STATE: + btc_ble_mesh_set_client_common_param(arg->generic_client_get_state.params, ¶m, false); cb.params = arg->generic_client_get_state.params; - cb.error_code = bt_mesh_generic_client_get_state(&common, arg->generic_client_get_state.get_state); + cb.error_code = bt_mesh_generic_client_get_state(¶m, arg->generic_client_get_state.get_state); if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ btc_ble_mesh_generic_client_callback(&cb, ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT); } break; - } - case BTC_BLE_MESH_ACT_GENERIC_CLIENT_SET_STATE: { - params = arg->generic_client_set_state.params; - common.opcode = params->opcode; - common.model = (struct bt_mesh_model *)params->model; - common.ctx.net_idx = params->ctx.net_idx; - common.ctx.app_idx = params->ctx.app_idx; - common.ctx.addr = params->ctx.addr; - common.ctx.send_rel = params->ctx.send_rel; - common.ctx.send_ttl = params->ctx.send_ttl; - common.msg_timeout = params->msg_timeout; - common.msg_role = params->msg_role; + case BTC_BLE_MESH_ACT_GENERIC_CLIENT_SET_STATE: + btc_ble_mesh_set_client_common_param(arg->generic_client_set_state.params, ¶m, false); cb.params = arg->generic_client_set_state.params; - cb.error_code = bt_mesh_generic_client_set_state(&common, arg->generic_client_set_state.set_state); + cb.error_code = bt_mesh_generic_client_set_state(¶m, arg->generic_client_set_state.set_state); if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ btc_ble_mesh_generic_client_callback(&cb, ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT); } break; - } default: break; } btc_ble_mesh_generic_client_arg_deep_free(msg); - return; } void btc_ble_mesh_generic_client_cb_handler(btc_msg_t *msg) @@ -521,7 +497,6 @@ void btc_ble_mesh_generic_client_cb_handler(btc_msg_t *msg) } btc_ble_mesh_generic_client_free_req_data(msg); - return; } #endif /* CONFIG_BLE_MESH_GENERIC_CLIENT */ @@ -674,8 +649,6 @@ static void btc_ble_mesh_generic_server_callback(esp_ble_mesh_generic_server_cb_ { btc_msg_t msg = {0}; - BT_DBG("%s", __func__); - /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_GENERIC_SERVER)) { return; @@ -689,14 +662,15 @@ static void btc_ble_mesh_generic_server_callback(esp_ble_mesh_generic_server_cb_ btc_ble_mesh_generic_server_copy_req_data, btc_ble_mesh_generic_server_free_req_data); } -void bt_mesh_generic_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model *model, +void bt_mesh_generic_server_cb_evt_to_btc(uint8_t evt_type, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const uint8_t *val, size_t len) { esp_ble_mesh_generic_server_cb_param_t cb_params = {0}; uint8_t act = 0U; - if (model == NULL || ctx == NULL) { + if (model == NULL || ctx == NULL || len > sizeof(cb_params.value)) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -725,13 +699,13 @@ void bt_mesh_generic_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model cb_params.ctx.recv_dst = ctx->recv_dst; cb_params.ctx.recv_rssi = ctx->recv_rssi; cb_params.ctx.send_ttl = ctx->send_ttl; + cb_params.ctx.recv_cred = ctx->recv_cred; if (val && len) { - memcpy(&cb_params.value, val, MIN(len, sizeof(cb_params.value))); + memcpy(&cb_params.value, val, len); } btc_ble_mesh_generic_server_callback(&cb_params, act); - return; } void btc_ble_mesh_generic_server_cb_handler(btc_msg_t *msg) @@ -752,7 +726,6 @@ void btc_ble_mesh_generic_server_cb_handler(btc_msg_t *msg) } btc_ble_mesh_generic_server_free_req_data(msg); - return; } #endif /* CONFIG_BLE_MESH_GENERIC_SERVER */ 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 49f90d930b21..75ae22c20839 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 @@ -7,8 +7,8 @@ #include #include +#include "btc_ble_mesh_model_common.h" #include "btc_ble_mesh_health_model.h" -#include "foundation.h" #include "esp_ble_mesh_health_model_api.h" #if CONFIG_BLE_MESH_HEALTH_CLI @@ -136,7 +136,7 @@ static void btc_ble_mesh_health_client_copy_req_data(btc_msg_t *msg, void *p_des case ESP_BLE_MESH_HEALTH_CLIENT_PUBLISH_EVT: if (p_src_data->params) { switch (p_src_data->params->opcode) { - case OP_HEALTH_CURRENT_STATUS: + case ESP_BLE_MESH_MODEL_OP_HEALTH_CURRENT_STATUS: if (p_src_data->status_cb.current_status.fault_array) { 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); @@ -149,10 +149,10 @@ static void btc_ble_mesh_health_client_copy_req_data(btc_msg_t *msg, void *p_des p_src_data->status_cb.current_status.fault_array->len); } break; - case OP_HEALTH_FAULT_GET: - case OP_HEALTH_FAULT_CLEAR: - case OP_HEALTH_FAULT_TEST: - case OP_HEALTH_FAULT_STATUS: + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET: + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR: + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST: + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_STATUS: if (p_src_data->status_cb.fault_status.fault_array) { 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); @@ -193,13 +193,13 @@ static void btc_ble_mesh_health_client_free_req_data(btc_msg_t *msg) case ESP_BLE_MESH_HEALTH_CLIENT_PUBLISH_EVT: if (arg->params) { switch (arg->params->opcode) { - case OP_HEALTH_CURRENT_STATUS: + case ESP_BLE_MESH_MODEL_OP_HEALTH_CURRENT_STATUS: bt_mesh_free_buf(arg->status_cb.current_status.fault_array); break; - case OP_HEALTH_FAULT_GET: - case OP_HEALTH_FAULT_CLEAR: - case OP_HEALTH_FAULT_TEST: - case OP_HEALTH_FAULT_STATUS: + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET: + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR: + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST: + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_STATUS: bt_mesh_free_buf(arg->status_cb.fault_status.fault_array); break; default: @@ -220,8 +220,6 @@ static void btc_ble_mesh_health_client_callback(esp_ble_mesh_health_client_cb_pa { btc_msg_t msg = {0}; - BT_DBG("%s", __func__); - /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_HEALTH_CLIENT)) { return; @@ -244,7 +242,7 @@ void bt_mesh_health_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, esp_ble_mesh_client_common_param_t params = {0}; uint8_t act = 0U; - if (!model || !ctx) { + if (!model || !ctx || len > sizeof(cb_params.status_cb)) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -282,14 +280,14 @@ void bt_mesh_health_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, cb_params.params = ¶ms; if (val && len) { - memcpy(&cb_params.status_cb, val, MIN(len, sizeof(cb_params.status_cb))); + memcpy(&cb_params.status_cb, val, len); } btc_ble_mesh_health_client_callback(&cb_params, act); - return; } -void btc_ble_mesh_health_publish_callback(uint32_t opcode, struct bt_mesh_model *model, +void btc_ble_mesh_health_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { @@ -300,7 +298,6 @@ void btc_ble_mesh_health_publish_callback(uint32_t opcode, struct bt_mesh_model bt_mesh_health_client_cb_evt_to_btc(opcode, BTC_BLE_MESH_EVT_HEALTH_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); - return; } static int btc_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param_t *params, @@ -318,15 +315,7 @@ static int btc_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param return -EINVAL; } - param.opcode = params->opcode; - param.model = (struct bt_mesh_model *)params->model; - param.ctx.net_idx = params->ctx.net_idx; - param.ctx.app_idx = params->ctx.app_idx; - param.ctx.addr = params->ctx.addr; - param.ctx.send_rel = params->ctx.send_rel; - param.ctx.send_ttl = params->ctx.send_ttl; - param.msg_timeout = params->msg_timeout; - param.msg_role = params->msg_role; + btc_ble_mesh_set_client_common_param(params, ¶m, false); switch (param.opcode) { case ESP_BLE_MESH_MODEL_OP_ATTENTION_GET: @@ -339,8 +328,6 @@ static int btc_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param BT_ERR("Invalid Health Get opcode 0x%04x", param.opcode); return -EINVAL; } - - return 0; } static int btc_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param_t *params, @@ -353,15 +340,7 @@ static int btc_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param return -EINVAL; } - param.opcode = params->opcode; - param.model = (struct bt_mesh_model *)params->model; - param.ctx.net_idx = params->ctx.net_idx; - param.ctx.app_idx = params->ctx.app_idx; - param.ctx.addr = params->ctx.addr; - param.ctx.send_rel = params->ctx.send_rel; - param.ctx.send_ttl = params->ctx.send_ttl; - param.msg_timeout = params->msg_timeout; - param.msg_role = params->msg_role; + btc_ble_mesh_set_client_common_param(params, ¶m, false); switch (param.opcode) { case ESP_BLE_MESH_MODEL_OP_ATTENTION_SET: @@ -384,8 +363,6 @@ static int btc_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param BT_ERR("Invalid Health Set opcode 0x%04x", param.opcode); return -EINVAL; } - - return 0; } void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg) @@ -426,7 +403,6 @@ void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg) } btc_ble_mesh_health_client_arg_deep_free(msg); - return; } void btc_ble_mesh_health_client_cb_handler(btc_msg_t *msg) @@ -447,7 +423,6 @@ void btc_ble_mesh_health_client_cb_handler(btc_msg_t *msg) } btc_ble_mesh_health_client_free_req_data(msg); - return; } #endif /* CONFIG_BLE_MESH_HEALTH_CLI */ @@ -531,8 +506,6 @@ static void btc_ble_mesh_health_server_callback(esp_ble_mesh_health_server_cb_pa { btc_msg_t msg = {0}; - BT_DBG("%s", __func__); - /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_HEALTH_SERVER)) { return; @@ -570,7 +543,6 @@ void btc_ble_mesh_health_server_call_handler(btc_msg_t *msg) } btc_ble_mesh_health_server_arg_deep_free(msg); - return; } void btc_ble_mesh_health_server_cb_handler(btc_msg_t *msg) @@ -591,7 +563,6 @@ void btc_ble_mesh_health_server_cb_handler(btc_msg_t *msg) } btc_ble_mesh_health_server_free_req_data(msg); - return; } void btc_ble_mesh_health_server_fault_clear(struct bt_mesh_model *model, uint16_t company_id) 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 ea3d5917e726..3195c513f543 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 @@ -7,6 +7,7 @@ #include #include +#include "btc_ble_mesh_model_common.h" #include "btc_ble_mesh_lighting_model.h" #include "esp_ble_mesh_lighting_model_api.h" @@ -79,7 +80,7 @@ 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) { + if (!msg) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -165,7 +166,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) { + if (!msg) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -201,8 +202,6 @@ static void btc_ble_mesh_lighting_client_callback(esp_ble_mesh_light_client_cb_p { btc_msg_t msg = {0}; - BT_DBG("%s", __func__); - /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_LIGHTING_CLIENT)) { return; @@ -225,7 +224,7 @@ void bt_mesh_lighting_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, esp_ble_mesh_client_common_param_t params = {0}; uint8_t act = 0U; - if (!model || !ctx) { + if (!model || !ctx || len > sizeof(cb_params.status_cb)) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -263,14 +262,14 @@ void bt_mesh_lighting_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, cb_params.params = ¶ms; if (val && len) { - memcpy(&cb_params.status_cb, val, MIN(len, sizeof(cb_params.status_cb))); + memcpy(&cb_params.status_cb, val, len); } btc_ble_mesh_lighting_client_callback(&cb_params, act); - return; } -void btc_ble_mesh_lighting_client_publish_callback(uint32_t opcode, struct bt_mesh_model *model, +void btc_ble_mesh_lighting_client_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { @@ -281,17 +280,15 @@ void btc_ble_mesh_lighting_client_publish_callback(uint32_t opcode, struct bt_me bt_mesh_lighting_client_cb_evt_to_btc(opcode, BTC_BLE_MESH_EVT_LIGHTING_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); - return; } void btc_ble_mesh_lighting_client_call_handler(btc_msg_t *msg) { - esp_ble_mesh_client_common_param_t *params = NULL; btc_ble_mesh_lighting_client_args_t *arg = NULL; esp_ble_mesh_light_client_cb_param_t cb = {0}; - bt_mesh_client_common_param_t common = {0}; + bt_mesh_client_common_param_t param = {0}; - if (!msg || !msg->arg) { + if (!msg) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -299,59 +296,38 @@ void btc_ble_mesh_lighting_client_call_handler(btc_msg_t *msg) arg = (btc_ble_mesh_lighting_client_args_t *)(msg->arg); switch (msg->act) { - case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE: { - params = arg->light_client_get_state.params; - common.opcode = params->opcode; - common.model = (struct bt_mesh_model *)params->model; - common.ctx.net_idx = params->ctx.net_idx; - common.ctx.app_idx = params->ctx.app_idx; - common.ctx.addr = params->ctx.addr; - common.ctx.send_rel = params->ctx.send_rel; - common.ctx.send_ttl = params->ctx.send_ttl; - common.msg_timeout = params->msg_timeout; - common.msg_role = params->msg_role; + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE: + btc_ble_mesh_set_client_common_param(arg->light_client_get_state.params, ¶m, false); cb.params = arg->light_client_get_state.params; - cb.error_code = bt_mesh_light_client_get_state(&common, arg->light_client_get_state.get_state); + cb.error_code = bt_mesh_light_client_get_state(¶m, arg->light_client_get_state.get_state); if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ btc_ble_mesh_lighting_client_callback(&cb, ESP_BLE_MESH_LIGHT_CLIENT_GET_STATE_EVT); } break; - } - case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE: { - params = arg->light_client_set_state.params; - common.opcode = params->opcode; - common.model = (struct bt_mesh_model *)params->model; - common.ctx.net_idx = params->ctx.net_idx; - common.ctx.app_idx = params->ctx.app_idx; - common.ctx.addr = params->ctx.addr; - common.ctx.send_rel = params->ctx.send_rel; - common.ctx.send_ttl = params->ctx.send_ttl; - common.msg_timeout = params->msg_timeout; - common.msg_role = params->msg_role; + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE: + btc_ble_mesh_set_client_common_param(arg->light_client_set_state.params, ¶m, false); cb.params = arg->light_client_set_state.params; - cb.error_code = bt_mesh_light_client_set_state(&common, arg->light_client_set_state.set_state); + cb.error_code = bt_mesh_light_client_set_state(¶m, arg->light_client_set_state.set_state); if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ btc_ble_mesh_lighting_client_callback(&cb, ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT); } break; - } default: break; } btc_ble_mesh_lighting_client_arg_deep_free(msg); - return; } 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) { + if (!msg) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -365,7 +341,6 @@ void btc_ble_mesh_lighting_client_cb_handler(btc_msg_t *msg) } btc_ble_mesh_lighting_client_free_req_data(msg); - return; } #endif /* CONFIG_BLE_MESH_LIGHTING_CLIENT */ @@ -462,13 +437,13 @@ static void btc_ble_mesh_lighting_server_free_req_data(btc_msg_t *msg) switch (msg->act) { case ESP_BLE_MESH_LIGHTING_SERVER_STATE_CHANGE_EVT: if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET || - arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK) { + arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK) { bt_mesh_free_buf(arg->value.state_change.lc_property_set.property_value); } break; case ESP_BLE_MESH_LIGHTING_SERVER_RECV_SET_MSG_EVT: if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET || - arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK) { + arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK) { bt_mesh_free_buf(arg->value.set.lc_property.property_value); } break; @@ -486,8 +461,6 @@ static void btc_ble_mesh_lighting_server_callback(esp_ble_mesh_lighting_server_c { btc_msg_t msg = {0}; - BT_DBG("%s", __func__); - /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_LIGHTING_SERVER)) { return; @@ -501,14 +474,15 @@ static void btc_ble_mesh_lighting_server_callback(esp_ble_mesh_lighting_server_c btc_ble_mesh_lighting_server_copy_req_data, btc_ble_mesh_lighting_server_free_req_data); } -void bt_mesh_lighting_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model *model, +void bt_mesh_lighting_server_cb_evt_to_btc(uint8_t evt_type, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const uint8_t *val, size_t len) { esp_ble_mesh_lighting_server_cb_param_t cb_params = {0}; uint8_t act = 0U; - if (model == NULL || ctx == NULL) { + if (model == NULL || ctx == NULL || len > sizeof(cb_params.value)) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -542,11 +516,10 @@ void bt_mesh_lighting_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_mode cb_params.ctx.send_ttl = ctx->send_ttl; if (val && len) { - memcpy(&cb_params.value, val, MIN(len, sizeof(cb_params.value))); + memcpy(&cb_params.value, val, len); } btc_ble_mesh_lighting_server_callback(&cb_params, act); - return; } void btc_ble_mesh_lighting_server_cb_handler(btc_msg_t *msg) @@ -567,7 +540,6 @@ void btc_ble_mesh_lighting_server_cb_handler(btc_msg_t *msg) } btc_ble_mesh_lighting_server_free_req_data(msg); - return; } #endif /* CONFIG_BLE_MESH_LIGHTING_SERVER */ 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 b29820bec5a0..927e923ec5b7 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 @@ -10,10 +10,20 @@ #include "btc_ble_mesh_prov.h" #include "btc_ble_mesh_config_model.h" #include "btc_ble_mesh_health_model.h" +#include "btc_ble_mesh_prb_model.h" #include "btc_ble_mesh_generic_model.h" #include "btc_ble_mesh_time_scene_model.h" #include "btc_ble_mesh_sensor_model.h" #include "btc_ble_mesh_lighting_model.h" +#include "btc_ble_mesh_brc_model.h" +#include "btc_ble_mesh_odp_model.h" +#include "btc_ble_mesh_srpl_model.h" +#include "btc_ble_mesh_agg_model.h" +#include "btc_ble_mesh_sar_model.h" +#include "btc_ble_mesh_lcd_model.h" +#include "btc_ble_mesh_rpr_model.h" +#include "btc_ble_mesh_df_model.h" +#include "btc_ble_mesh_mbt_model.h" #include "adv.h" #include "mesh/kernel.h" @@ -22,6 +32,7 @@ #include "access.h" #include "prov_node.h" #include "settings_uid.h" +#include "proxy_common.h" #include "proxy_server.h" #include "proxy_client.h" #include "prov_pvnr.h" @@ -53,6 +64,8 @@ #include "mesh/state_binding.h" #include "local.h" +#include "mesh_v1.1/utils.h" + #include "esp_ble_mesh_common_api.h" #include "esp_ble_mesh_provisioning_api.h" #include "esp_ble_mesh_networking_api.h" @@ -152,6 +165,65 @@ void btc_ble_mesh_prov_arg_deep_free(btc_msg_t *msg) } } +static void btc_ble_mesh_prov_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_prov_cb_param_t *p_dest_data = (esp_ble_mesh_prov_cb_param_t *)p_dest; + esp_ble_mesh_prov_cb_param_t *p_src_data = (esp_ble_mesh_prov_cb_param_t *)p_src; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { +#if CONFIG_BLE_MESH_CERT_BASED_PROV + case ESP_BLE_MESH_PROVISIONER_RECV_PROV_RECORDS_LIST_EVT: { + if (p_src_data->recv_provisioner_records_list.msg) { + p_dest_data->recv_provisioner_records_list.msg = (uint8_t *)bt_mesh_alloc_buf(p_src_data->recv_provisioner_records_list.len); + if (!p_dest_data->recv_provisioner_records_list.msg) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->recv_provisioner_records_list.msg, + p_src_data->recv_provisioner_records_list.msg, + p_src_data->recv_provisioner_records_list.len); + } + break; + } +#endif /* CONFIG_BLE_MESH_CERT_BASED_PROV */ + default: + break; + } +} + +static void btc_ble_mesh_prov_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_prov_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_prov_cb_param_t *)(msg->arg); + + switch (msg->act) { +#if CONFIG_BLE_MESH_CERT_BASED_PROV + case ESP_BLE_MESH_PROVISIONER_RECV_PROV_RECORDS_LIST_EVT: { + if (arg->recv_provisioner_records_list.msg) { + bt_mesh_free(arg->recv_provisioner_records_list.msg); + } + break; + } +#else + ARG_UNUSED(arg); +#endif /* CONFIG_BLE_MESH_CERT_BASED_PROV */ + default: + break; + } +} + void btc_ble_mesh_model_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) { btc_ble_mesh_model_args_t *dst = (btc_ble_mesh_model_args_t *)p_dest; @@ -225,8 +297,6 @@ void btc_ble_mesh_model_arg_deep_free(btc_msg_t *msg) default: break; } - - return; } static void btc_ble_mesh_model_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) @@ -357,8 +427,6 @@ static bt_status_t btc_ble_mesh_model_callback(esp_ble_mesh_model_cb_param_t *pa btc_msg_t msg = {0}; bt_status_t ret = BT_STATUS_SUCCESS; - BT_DBG("%s", __func__); - /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_MODEL)) { return BT_STATUS_SUCCESS; @@ -389,7 +457,6 @@ static void btc_ble_mesh_server_model_op_cb(struct bt_mesh_model *model, mesh_param.model_operation.msg = buf->data; btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_OPERATION_EVT); - return; } static void btc_ble_mesh_client_model_op_cb(struct bt_mesh_model *model, @@ -427,7 +494,6 @@ static void btc_ble_mesh_client_model_op_cb(struct bt_mesh_model *model, } bt_mesh_client_model_unlock(); - return; } static void btc_ble_mesh_client_model_timeout_cb(struct k_work *work) @@ -446,7 +512,7 @@ static void btc_ble_mesh_client_model_timeout_cb(struct k_work *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 *)ctx.model; + mesh_param.client_send_timeout.model = (esp_ble_mesh_model_t *)node->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); @@ -454,7 +520,6 @@ static void btc_ble_mesh_client_model_timeout_cb(struct k_work *work) } bt_mesh_client_model_unlock(); - return; } static void btc_ble_mesh_model_send_comp_cb(esp_ble_mesh_model_t *model, @@ -469,7 +534,6 @@ static void btc_ble_mesh_model_send_comp_cb(esp_ble_mesh_model_t *model, mesh_param.model_send_comp.ctx = ctx; btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_SEND_COMP_EVT); - return; } static void btc_ble_mesh_model_publish_comp_cb(esp_ble_mesh_model_t *model, int err) @@ -480,7 +544,6 @@ static void btc_ble_mesh_model_publish_comp_cb(esp_ble_mesh_model_t *model, int mesh_param.model_publish_comp.model = model; btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_PUBLISH_COMP_EVT); - return; } static int btc_ble_mesh_model_publish_update(struct bt_mesh_model *mod) @@ -488,8 +551,6 @@ 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_SUCCESS; - BT_DBG("%s", __func__); - mesh_param.model_publish_update.model = (esp_ble_mesh_model_t *)mod; ret = btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_PUBLISH_UPDATE_EVT); @@ -508,7 +569,6 @@ static void btc_ble_mesh_server_model_update_state_comp_cb(esp_ble_mesh_model_t mesh_param.server_model_update_state.type = type; btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_SERVER_MODEL_UPDATE_STATE_COMP_EVT); - return; } #endif /* CONFIG_BLE_MESH_SERVER_MODEL */ @@ -517,8 +577,6 @@ static bt_status_t btc_ble_mesh_prov_callback(esp_ble_mesh_prov_cb_param_t *para btc_msg_t msg = {0}; bt_status_t ret = BT_STATUS_SUCCESS; - BT_DBG("%s", __func__); - /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_PROV)) { return BT_STATUS_SUCCESS; @@ -528,20 +586,26 @@ static bt_status_t btc_ble_mesh_prov_callback(esp_ble_mesh_prov_cb_param_t *para msg.pid = BTC_PID_PROV; msg.act = act; - ret = btc_transfer_context(&msg, param, param == NULL ? 0 : sizeof(esp_ble_mesh_prov_cb_param_t), NULL, NULL); + ret = btc_transfer_context(&msg, param, param == NULL ? 0 : sizeof(esp_ble_mesh_prov_cb_param_t), + btc_ble_mesh_prov_copy_req_data, btc_ble_mesh_prov_free_req_data); if (ret != BT_STATUS_SUCCESS) { BT_ERR("btc_transfer_context failed"); } return ret; } +#if CONFIG_BLE_MESH_DF_SRV +int btc_ble_mesh_enable_directed_forwarding(uint16_t net_idx, bool directed_forwarding, + bool directed_forwarding_relay) +{ + return bt_mesh_enable_directed_forwarding(net_idx, directed_forwarding, directed_forwarding_relay); +} +#endif /* CONFIG_BLE_MESH_DF_SRV */ + #if CONFIG_BLE_MESH_NODE static void btc_ble_mesh_oob_pub_key_cb(void) { - BT_DBG("%s", __func__); - btc_ble_mesh_prov_callback(NULL, ESP_BLE_MESH_NODE_PROV_OOB_PUB_KEY_EVT); - return; } static int btc_ble_mesh_output_number_cb(bt_mesh_output_action_t act, uint32_t num) @@ -549,8 +613,6 @@ static int btc_ble_mesh_output_number_cb(bt_mesh_output_action_t act, uint32_t n esp_ble_mesh_prov_cb_param_t mesh_param = {0}; bt_status_t ret = BT_STATUS_SUCCESS; - 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; @@ -563,10 +625,8 @@ 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_SUCCESS; - BT_DBG("%s", __func__); - strncpy(mesh_param.node_prov_output_str.string, str, - MIN(strlen(str), sizeof(mesh_param.node_prov_output_str.string))); + MIN(strlen(str), sizeof(mesh_param.node_prov_output_str.string))); ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_OUTPUT_STRING_EVT); return (ret == BT_STATUS_SUCCESS) ? 0 : -1; @@ -577,8 +637,6 @@ static int btc_ble_mesh_input_cb(bt_mesh_input_action_t act, uint8_t size) esp_ble_mesh_prov_cb_param_t mesh_param = {0}; bt_status_t ret = BT_STATUS_SUCCESS; - BT_DBG("%s", __func__); - mesh_param.node_prov_input.action = (esp_ble_mesh_input_action_t)act; mesh_param.node_prov_input.size = size; @@ -590,24 +648,19 @@ static void btc_ble_mesh_link_open_cb(bt_mesh_prov_bearer_t bearer) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - BT_DBG("%s", __func__); - mesh_param.node_prov_link_open.bearer = (esp_ble_mesh_prov_bearer_t)bearer; btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT); - return; } -static void btc_ble_mesh_link_close_cb(bt_mesh_prov_bearer_t bearer) +static void btc_ble_mesh_link_close_cb(bt_mesh_prov_bearer_t bearer, uint8_t reason) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - BT_DBG("%s", __func__); - mesh_param.node_prov_link_close.bearer = (esp_ble_mesh_prov_bearer_t)bearer; + mesh_param.node_prov_link_close.reason = reason; btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT); - return; } static void btc_ble_mesh_complete_cb(uint16_t net_idx, const uint8_t net_key[16], @@ -615,8 +668,6 @@ static void btc_ble_mesh_complete_cb(uint16_t net_idx, const uint8_t net_key[16] { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - BT_DBG("%s", __func__); - mesh_param.node_prov_complete.net_idx = net_idx; memcpy(mesh_param.node_prov_complete.net_key, net_key, 16); mesh_param.node_prov_complete.addr = addr; @@ -624,15 +675,11 @@ static void btc_ble_mesh_complete_cb(uint16_t net_idx, const uint8_t net_key[16] mesh_param.node_prov_complete.iv_index = iv_index; btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT); - return; } static void btc_ble_mesh_reset_cb(void) { - BT_DBG("%s", __func__); - btc_ble_mesh_prov_callback(NULL, ESP_BLE_MESH_NODE_PROV_RESET_EVT); - return; } const uint8_t *btc_ble_mesh_node_get_local_net_key(uint16_t net_idx) @@ -650,20 +697,14 @@ static void btc_ble_mesh_prov_register_complete_cb(int err_code) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - BT_DBG("%s", __func__); - mesh_param.prov_register_comp.err_code = err_code; btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROV_REGISTER_COMP_EVT); - return; } static void btc_ble_mesh_prov_set_complete_cb(esp_ble_mesh_prov_cb_param_t *param, uint8_t act) { - BT_DBG("%s", __func__); - btc_ble_mesh_prov_callback(param, act); - return; } #if CONFIG_BLE_MESH_PROVISIONER @@ -674,10 +715,8 @@ static void btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb(const uint8_t addr[6 { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - BT_DBG("%s", __func__); - if (addr == NULL || dev_uuid == NULL || - (bearer != BLE_MESH_PROV_ADV && bearer != BLE_MESH_PROV_GATT)) { + (bearer != BLE_MESH_PROV_ADV && bearer != BLE_MESH_PROV_GATT)) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -691,7 +730,6 @@ static void btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb(const uint8_t addr[6 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; } static int btc_ble_mesh_provisioner_prov_read_oob_pub_key_cb(uint8_t link_idx) @@ -699,8 +737,6 @@ static int btc_ble_mesh_provisioner_prov_read_oob_pub_key_cb(uint8_t link_idx) esp_ble_mesh_prov_cb_param_t mesh_param = {0}; bt_status_t ret = BT_STATUS_SUCCESS; - BT_DBG("%s", __func__); - mesh_param.provisioner_prov_read_oob_pub_key.link_idx = link_idx; ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_EVT); @@ -713,8 +749,6 @@ static int btc_ble_mesh_provisioner_prov_input_cb(uint8_t method, bt_mesh_output esp_ble_mesh_prov_cb_param_t mesh_param = {0}; bt_status_t ret = BT_STATUS_SUCCESS; - 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; mesh_param.provisioner_prov_input.size = size; @@ -730,8 +764,6 @@ static int btc_ble_mesh_provisioner_prov_output_cb(uint8_t method, bt_mesh_input esp_ble_mesh_prov_cb_param_t mesh_param = {0}; bt_status_t ret = BT_STATUS_SUCCESS; - 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; mesh_param.provisioner_prov_output.size = size; @@ -750,25 +782,19 @@ 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}; - BT_DBG("%s", __func__); - mesh_param.provisioner_prov_link_open.bearer = (esp_ble_mesh_prov_bearer_t)bearer; btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_LINK_OPEN_EVT); - return; } static void btc_ble_mesh_provisioner_link_close_cb(bt_mesh_prov_bearer_t bearer, uint8_t reason) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - 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; btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_LINK_CLOSE_EVT); - return; } static void btc_ble_mesh_provisioner_prov_complete_cb(uint16_t node_idx, const uint8_t device_uuid[16], @@ -777,8 +803,6 @@ static void btc_ble_mesh_provisioner_prov_complete_cb(uint16_t node_idx, const u { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - BT_DBG("%s", __func__); - mesh_param.provisioner_prov_complete.node_idx = node_idx; mesh_param.provisioner_prov_complete.unicast_addr = unicast_addr; mesh_param.provisioner_prov_complete.element_num = element_num; @@ -786,9 +810,45 @@ static void btc_ble_mesh_provisioner_prov_complete_cb(uint16_t node_idx, const u memcpy(mesh_param.provisioner_prov_complete.device_uuid, device_uuid, sizeof(esp_ble_mesh_octet16_t)); btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT); - return; } +#if CONFIG_BLE_MESH_CERT_BASED_PROV +static void btc_ble_mesh_provisioner_cert_based_prov_start_cb(uint16_t link_idx) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + mesh_param.provisioner_cert_based_prov_start.link_idx = link_idx; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_CERT_BASED_PROV_START_EVT); +} + +static void btc_ble_mesh_provisioner_records_list_get_cb(uint16_t link_idx, struct net_buf_simple *buf) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + mesh_param.recv_provisioner_records_list.link_idx = link_idx; + mesh_param.recv_provisioner_records_list.len = buf->len; + mesh_param.recv_provisioner_records_list.msg = buf->data; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_RECV_PROV_RECORDS_LIST_EVT); +} + +static void btc_ble_mesh_provisioner_record_recv_comp_cb(uint8_t status, uint16_t link_idx, uint16_t record_id, + uint16_t frag_offset, uint16_t total_len, uint8_t *record) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + mesh_param.provisioner_prov_record_recv_comp.status = status; + mesh_param.provisioner_prov_record_recv_comp.link_idx = link_idx; + mesh_param.provisioner_prov_record_recv_comp.record_id = record_id; + mesh_param.provisioner_prov_record_recv_comp.frag_offset = frag_offset; + mesh_param.provisioner_prov_record_recv_comp.total_len = total_len; + mesh_param.provisioner_prov_record_recv_comp.record = record; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_RECORD_RECV_COMP_EVT); +} +#endif /* CONFIG_BLE_MESH_CERT_BASED_PROV */ + 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); @@ -856,13 +916,10 @@ static void btc_ble_mesh_node_recv_heartbeat_cb(uint8_t hops, uint16_t feature) { esp_ble_mesh_prov_cb_param_t mesh_param = {0}; - BT_DBG("%s", __func__); - mesh_param.heartbeat_msg_recv.hops = hops; mesh_param.heartbeat_msg_recv.feature = feature; btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_HEARTBEAT_MESSAGE_RECV_EVT); - return; } #if CONFIG_BLE_MESH_LOW_POWER @@ -871,8 +928,6 @@ static void btc_ble_mesh_lpn_cb(uint16_t friend_addr, bool established) esp_ble_mesh_prov_cb_param_t mesh_param = {0}; uint8_t act = 0U; - BT_DBG("%s", __func__); - if (established) { mesh_param.lpn_friendship_establish.friend_addr = friend_addr; act = ESP_BLE_MESH_LPN_FRIENDSHIP_ESTABLISH_EVT; @@ -882,7 +937,6 @@ static void btc_ble_mesh_lpn_cb(uint16_t friend_addr, bool established) } btc_ble_mesh_prov_callback(&mesh_param, act); - return; } #endif /* CONFIG_BLE_MESH_LOW_POWER */ @@ -892,8 +946,6 @@ void btc_ble_mesh_friend_cb(bool establish, uint16_t lpn_addr, uint8_t reason) esp_ble_mesh_prov_cb_param_t mesh_param = {0}; uint8_t act = 0U; - BT_DBG("%s", __func__); - if (!BLE_MESH_ADDR_IS_UNICAST(lpn_addr)) { BT_ERR("Not a unicast lpn address 0x%04x", lpn_addr); return; @@ -909,7 +961,6 @@ void btc_ble_mesh_friend_cb(bool establish, uint16_t lpn_addr, uint8_t reason) } btc_ble_mesh_prov_callback(&mesh_param, act); - return; } #endif /* CONFIG_BLE_MESH_FRIEND */ @@ -924,8 +975,6 @@ static void btc_ble_mesh_proxy_client_adv_recv_cb(const bt_mesh_addr_t *addr, ui return; } - 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; @@ -933,7 +982,6 @@ static void btc_ble_mesh_proxy_client_adv_recv_cb(const bt_mesh_addr_t *addr, ui 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; } static void btc_ble_mesh_proxy_client_connect_cb(const bt_mesh_addr_t *addr, @@ -946,15 +994,12 @@ static void btc_ble_mesh_proxy_client_connect_cb(const bt_mesh_addr_t *addr, return; } - 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); mesh_param.proxy_client_connected.conn_handle = conn_handle; mesh_param.proxy_client_connected.net_idx = net_idx; btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_CONNECTED_EVT); - return; } static void btc_ble_mesh_proxy_client_disconnect_cb(const bt_mesh_addr_t *addr, uint8_t conn_handle, @@ -967,8 +1012,6 @@ static void btc_ble_mesh_proxy_client_disconnect_cb(const bt_mesh_addr_t *addr, return; } - 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); mesh_param.proxy_client_disconnected.conn_handle = conn_handle; @@ -976,7 +1019,6 @@ static void btc_ble_mesh_proxy_client_disconnect_cb(const bt_mesh_addr_t *addr, mesh_param.proxy_client_disconnected.reason = reason; btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_DISCONNECTED_EVT); - return; } static void btc_ble_mesh_proxy_client_filter_status_recv_cb(uint8_t conn_handle, uint16_t src, uint16_t net_idx, @@ -989,8 +1031,6 @@ static void btc_ble_mesh_proxy_client_filter_status_recv_cb(uint8_t conn_handle, return; } - 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; mesh_param.proxy_client_recv_filter_status.net_idx = net_idx; @@ -998,7 +1038,6 @@ static void btc_ble_mesh_proxy_client_filter_status_recv_cb(uint8_t conn_handle, mesh_param.proxy_client_recv_filter_status.list_size = list_size; btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_RECV_FILTER_STATUS_EVT); - return; } #endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ @@ -1012,8 +1051,6 @@ static void btc_ble_mesh_proxy_server_connect_cb(uint8_t conn_handle) return; } - BT_DBG("%s", __func__); - mesh_param.proxy_server_connected.conn_handle = conn_handle; btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_SERVER_CONNECTED_EVT); @@ -1028,8 +1065,6 @@ static void btc_ble_mesh_proxy_server_disconnect_cb(uint8_t conn_handle, uint8_t return; } - BT_DBG("%s", __func__); - mesh_param.proxy_server_disconnected.conn_handle = conn_handle; mesh_param.proxy_server_disconnected.reason = reason; @@ -1046,7 +1081,7 @@ int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model) __ASSERT(model && model->op, "Invalid parameter"); esp_ble_mesh_model_op_t *op = model->op; - while (op != NULL && op->opcode != 0) { + while (op && op->opcode != 0) { op->param_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_client_model_op_cb; op++; } @@ -1119,6 +1154,100 @@ extern const struct bt_mesh_model_op bt_mesh_health_cli_op[]; extern const struct bt_mesh_model_cb bt_mesh_health_cli_cb; #endif /* CONFIG_BLE_MESH_HEALTH_CLI */ +/* Bridge Configuration Models */ +#if CONFIG_BLE_MESH_BRC_SRV +extern const struct bt_mesh_model_op bt_mesh_brc_srv_op[]; +extern const struct bt_mesh_model_cb bt_mesh_brc_srv_cb; +#endif /* CONFIG_BLE_MESH_BRC_SRV */ +#if CONFIG_BLE_MESH_BRC_CLI +typedef bt_mesh_client_user_data_t bt_mesh_brc_client_t; +extern const struct bt_mesh_model_op bt_mesh_brc_cli_op[]; +extern const struct bt_mesh_model_cb bt_mesh_brc_cli_cb; +#endif /* CONFIG_BLE_MESH_BRC_CLI */ + +/* Mesh Private Beacon Models */ +#if CONFIG_BLE_MESH_PRB_SRV +extern const struct bt_mesh_model_op bt_mesh_prb_srv_op[]; +extern const struct bt_mesh_model_cb bt_mesh_prb_srv_cb; +#endif /* CONFIG_BLE_MESH_PRB_SRV */ +#if CONFIG_BLE_MESH_PRB_CLI +typedef bt_mesh_client_user_data_t bt_mesh_prb_client_t; +extern const struct bt_mesh_model_op bt_mesh_prb_cli_op[]; +extern const struct bt_mesh_model_cb bt_mesh_prb_cli_cb; +#endif /* CONFIG_BLE_MESH_PRB_CLI */ + +#if CONFIG_BLE_MESH_ODP_SRV +extern const struct bt_mesh_model_op bt_mesh_odp_srv_op[]; +extern const struct bt_mesh_model_cb bt_mesh_odp_srv_cb; +#endif /* CONFIG_BLE_MESH_ODP_SRV */ +#if CONFIG_BLE_MESH_ODP_CLI +typedef bt_mesh_client_user_data_t bt_mesh_odp_client_t; +extern const struct bt_mesh_model_op bt_mesh_odp_cli_op[]; +extern const struct bt_mesh_model_cb bt_mesh_odp_cli_cb; +#endif /* CONFIG_BLE_MESH_ODP_CLI */ + +#if CONFIG_BLE_MESH_SRPL_SRV +extern const struct bt_mesh_model_op bt_mesh_srpl_srv_op[]; +extern const struct bt_mesh_model_cb bt_mesh_srpl_srv_cb; +#endif /* CONFIG_BLE_MESH_SRPL_SRV */ +#if CONFIG_BLE_MESH_SRPL_CLI +typedef bt_mesh_client_user_data_t bt_mesh_srpl_client_t; +extern const struct bt_mesh_model_op bt_mesh_srpl_cli_op[]; +extern const struct bt_mesh_model_cb bt_mesh_srpl_cli_cb; +#endif /* CONFIG_BLE_MESH_SRPL_CLI */ + +#if CONFIG_BLE_MESH_AGG_SRV +extern const struct bt_mesh_model_op bt_mesh_agg_srv_op[]; +extern const struct bt_mesh_model_cb bt_mesh_agg_srv_cb; +#endif /* CONFIG_BLE_MESH_AGG_SRV */ +#if CONFIG_BLE_MESH_AGG_CLI +typedef bt_mesh_client_user_data_t bt_mesh_agg_client_t; +extern const struct bt_mesh_model_op bt_mesh_agg_cli_op[]; +extern const struct bt_mesh_model_cb bt_mesh_agg_cli_cb; +#endif /* CONFIG_BLE_MESH_AGG_CLI */ + +#if CONFIG_BLE_MESH_SAR_SRV +extern const struct bt_mesh_model_op bt_mesh_sar_srv_op[]; +extern const struct bt_mesh_model_cb bt_mesh_sar_srv_cb; +#endif /* CONFIG_BLE_MESH_SAR_SRV */ +#if CONFIG_BLE_MESH_SAR_CLI +typedef bt_mesh_client_user_data_t bt_mesh_sar_client_t; +extern const struct bt_mesh_model_op bt_mesh_sar_cli_op[]; +extern const struct bt_mesh_model_cb bt_mesh_sar_cli_cb; +#endif /* CONFIG_BLE_MESH_SAR_CLI */ + +#if CONFIG_BLE_MESH_LCD_SRV +extern const struct bt_mesh_model_op bt_mesh_lcd_srv_op[]; +extern const struct bt_mesh_model_cb bt_mesh_lcd_srv_cb; +#endif /* CONFIG_BLE_MESH_LCD_SRV */ +#if CONFIG_BLE_MESH_LCD_CLI +typedef bt_mesh_client_user_data_t bt_mesh_lcd_client_t; +extern const struct bt_mesh_model_op bt_mesh_lcd_cli_op[]; +extern const struct bt_mesh_model_cb bt_mesh_lcd_cli_cb; +#endif /* CONFIG_BLE_MESH_LCD_CLI */ + +/* Remote Provisioning models */ +#if CONFIG_BLE_MESH_RPR_SRV +extern const struct bt_mesh_model_op bt_mesh_rpr_srv_op[]; +extern const struct bt_mesh_model_cb bt_mesh_rpr_srv_cb; +#endif /* CONFIG_BLE_MESH_RPR_SRV */ +#if CONFIG_BLE_MESH_RPR_CLI +typedef bt_mesh_client_user_data_t bt_mesh_rpr_client_t; +extern const struct bt_mesh_model_op bt_mesh_rpr_cli_op[]; +extern const struct bt_mesh_model_cb bt_mesh_rpr_cli_cb; +#endif /* CONFIG_BLE_MESH_RPR_CLI */ + +/* Directed Forwarding Configuration Models */ +#if CONFIG_BLE_MESH_DF_SRV +extern const struct bt_mesh_model_op bt_mesh_df_srv_op[]; +extern const struct bt_mesh_model_cb bt_mesh_df_srv_cb; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +#if CONFIG_BLE_MESH_DF_CLI +typedef bt_mesh_client_user_data_t bt_mesh_df_client_t; +extern const struct bt_mesh_model_op bt_mesh_df_cli_op[]; +extern const struct bt_mesh_model_cb bt_mesh_df_cli_cb; +#endif /* CONFIG_BLE_MESH_DF_CLI */ + /* Generic Client Models */ #if CONFIG_BLE_MESH_GENERIC_CLIENT extern const struct bt_mesh_model_op bt_mesh_gen_onoff_cli_op[]; @@ -1242,6 +1371,17 @@ extern const struct bt_mesh_model_cb bt_mesh_sensor_srv_cb; extern const struct bt_mesh_model_cb bt_mesh_sensor_setup_srv_cb; #endif /* CONFIG_BLE_MESH_SENSOR_SERVER */ +/* Blob Transfer Models */ +#if CONFIG_BLE_MESH_MBT_SRV +extern const struct bt_mesh_model_op bt_mesh_mbt_srv_op[]; +extern const struct bt_mesh_model_cb bt_mesh_mbt_srv_cb; +#endif /* CONFIG_BLE_MESH_MBT_SRV */ +#if CONFIG_BLE_MESH_MBT_CLI +typedef bt_mesh_client_user_data_t bt_mesh_mbt_client_t; +extern const struct bt_mesh_model_op bt_mesh_mbt_cli_op[]; +extern const struct bt_mesh_model_cb bt_mesh_mbt_cli_cb; +#endif /* CONFIG_BLE_MESH_MBT_CLI */ + static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) { if (!model) { @@ -1271,11 +1411,15 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_cfg_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_cfg_cli_cb; bt_mesh_config_client_t *cli = (bt_mesh_config_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_config_client_publish_callback; } break; } +#else + case BLE_MESH_MODEL_ID_CFG_CLI: + __ASSERT(0, "Configuration Client model is not enabled"); + break; #endif /* CONFIG_BLE_MESH_CFG_CLI */ #if CONFIG_BLE_MESH_HEALTH_SRV case BLE_MESH_MODEL_ID_HEALTH_SRV: { @@ -1290,24 +1434,258 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) } break; } +#else + case BLE_MESH_MODEL_ID_HEALTH_SRV: + __ASSERT(0, "Health Server model is not enabled"); + break; #endif /* CONFIG_BLE_MESH_HEALTH_SRV */ #if CONFIG_BLE_MESH_HEALTH_CLI case BLE_MESH_MODEL_ID_HEALTH_CLI: { model->op = (esp_ble_mesh_model_op_t *)bt_mesh_health_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_health_cli_cb; bt_mesh_health_client_t *cli = (bt_mesh_health_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_health_publish_callback; } break; } +#else + case BLE_MESH_MODEL_ID_HEALTH_CLI: + __ASSERT(0, "Health Client model is not enabled"); + break; #endif /* CONFIG_BLE_MESH_HEALTH_CLI */ +#if CONFIG_BLE_MESH_BRC_SRV + case BLE_MESH_MODEL_ID_BRC_SRV: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_brc_srv_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_brc_srv_cb; + break; + } +#else + case BLE_MESH_MODEL_ID_BRC_SRV: + __ASSERT(0, "Bridge Configuration Server model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_BRC_SRV */ +#if CONFIG_BLE_MESH_BRC_CLI + case BLE_MESH_MODEL_ID_BRC_CLI: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_brc_cli_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_brc_cli_cb; + bt_mesh_brc_client_t *cli = (bt_mesh_brc_client_t *)model->user_data; + if (cli) { + cli->publish_status = btc_ble_mesh_brc_client_recv_pub_cb; + } + break; + } +#else + case BLE_MESH_MODEL_ID_BRC_CLI: + __ASSERT(0, "Bridge Configuration Client model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_BRC_CLI */ +#if CONFIG_BLE_MESH_PRB_SRV + case BLE_MESH_MODEL_ID_PRB_SRV: + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_prb_srv_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_prb_srv_cb; + break; +#else + case BLE_MESH_MODEL_ID_PRB_SRV: + __ASSERT(0, "Mesh Private Beacon Server model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_PRB_SRV */ +#if CONFIG_BLE_MESH_PRB_CLI + case BLE_MESH_MODEL_ID_PRB_CLI: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_prb_cli_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_prb_cli_cb; + bt_mesh_prb_client_t *cli = (bt_mesh_prb_client_t *)model->user_data; + if (cli) { + cli->publish_status = btc_ble_mesh_prb_client_recv_pub_cb; + } + break; + } +#else + case BLE_MESH_MODEL_ID_PRB_CLI: + __ASSERT(0, "Mesh Private Beacon Client model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_PRB_CLI */ +#if CONFIG_BLE_MESH_ODP_CLI + case BLE_MESH_MODEL_ID_ODP_CLI: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_odp_cli_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_odp_cli_cb; + bt_mesh_odp_client_t *cli = model->user_data; + if (cli) { + cli->publish_status = btc_ble_mesh_odp_client_recv_pub_cb; + } + break; + } +#else + case BLE_MESH_MODEL_ID_ODP_CLI: + __ASSERT(0, "On-Demand Private Proxy Client model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_ODP_CLI */ +#if CONFIG_BLE_MESH_ODP_SRV + case BLE_MESH_MODEL_ID_ODP_SRV: + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_odp_srv_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_odp_srv_cb; + break; +#else + case BLE_MESH_MODEL_ID_ODP_SRV: + __ASSERT(0, "On-Demand Private Proxy Server model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_ODP_SRV */ +#if CONFIG_BLE_MESH_SRPL_CLI + case BLE_MESH_MODEL_ID_SRPL_CLI: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_srpl_cli_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_srpl_cli_cb; + bt_mesh_srpl_client_t *cli = model->user_data; + if (cli) { + cli->publish_status = btc_ble_mesh_srpl_client_recv_pub_cb; + } + break; + } +#else + case BLE_MESH_MODEL_ID_SRPL_CLI: + __ASSERT(0, "Solicitation PDU RPL Client model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_SRPL_CLI */ +#if CONFIG_BLE_MESH_SRPL_SRV + case BLE_MESH_MODEL_ID_SRPL_SRV: + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_srpl_srv_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_srpl_srv_cb; + break; +#else + case BLE_MESH_MODEL_ID_SRPL_SRV: + __ASSERT(0, "Solicitation PDU RPL Server model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_SRPL_SRV */ +#if CONFIG_BLE_MESH_AGG_CLI + case BLE_MESH_MODEL_ID_AGG_CLI: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_agg_cli_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_agg_cli_cb; + bt_mesh_agg_client_t *cli = model->user_data; + if (cli) { + cli->publish_status = btc_ble_mesh_agg_client_recv_pub_cb; + } + break; + } +#else + case BLE_MESH_MODEL_ID_AGG_CLI: + __ASSERT(0, "Opcodes Aggregator Client model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_AGG_CLI */ +#if CONFIG_BLE_MESH_AGG_SRV + case BLE_MESH_MODEL_ID_AGG_SRV: + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_agg_srv_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_agg_srv_cb; + break; +#else + case BLE_MESH_MODEL_ID_AGG_SRV: + __ASSERT(0, "Opcodes Aggregator Server model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_AGG_SRV */ +#if CONFIG_BLE_MESH_SAR_CLI + case BLE_MESH_MODEL_ID_SAR_CLI: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_sar_cli_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_sar_cli_cb; + bt_mesh_sar_client_t *cli = model->user_data; + if (cli) { + cli->publish_status = btc_ble_mesh_sar_client_recv_pub_cb; + } + break; + } +#else + case BLE_MESH_MODEL_ID_SAR_CLI: + __ASSERT(0, "SAR Configuration Client model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_SAR_CLI */ +#if CONFIG_BLE_MESH_SAR_SRV + case BLE_MESH_MODEL_ID_SAR_SRV: + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_sar_srv_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_sar_srv_cb; + break; +#else + case BLE_MESH_MODEL_ID_SAR_SRV: + __ASSERT(0, "SAR Configuration Server model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_SAR_SRV */ +#if CONFIG_BLE_MESH_LCD_CLI + case BLE_MESH_MODEL_ID_LCD_CLI: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_lcd_cli_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_lcd_cli_cb; + bt_mesh_lcd_client_t *cli = model->user_data; + if (cli) { + cli->publish_status = btc_ble_mesh_lcd_client_recv_pub_cb; + } + break; + } +#else + case BLE_MESH_MODEL_ID_LCD_CLI: + __ASSERT(0, "Large Comp Data Client model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_LCD_CLI */ +#if CONFIG_BLE_MESH_LCD_SRV + case BLE_MESH_MODEL_ID_LCD_SRV: + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_lcd_srv_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_lcd_srv_cb; + break; +#else + case BLE_MESH_MODEL_ID_LCD_SRV: + __ASSERT(0, "Large Comp Data Server model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_LCD_SRV */ +#if CONFIG_BLE_MESH_RPR_CLI + case BLE_MESH_MODEL_ID_RPR_CLI: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_rpr_cli_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_rpr_cli_cb; + bt_mesh_rpr_client_t *cli = model->user_data; + if (cli) { + cli->publish_status = btc_ble_mesh_rpr_client_recv_pub_cb; + } + break; + } +#else + case BLE_MESH_MODEL_ID_RPR_CLI: + __ASSERT(0, "Remote Provisioning Client model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_RPR_CLI */ +#if CONFIG_BLE_MESH_RPR_SRV + case BLE_MESH_MODEL_ID_RPR_SRV: + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_rpr_srv_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_rpr_srv_cb; + break; +#else + case BLE_MESH_MODEL_ID_RPR_SRV: + __ASSERT(0, "Remote Provisioning Server model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_RPR_SRV */ +#if CONFIG_BLE_MESH_DF_CLI + case BLE_MESH_MODEL_ID_DF_CLI: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_df_cli_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_df_cli_cb; + bt_mesh_df_client_t *cli = (bt_mesh_df_client_t *)model->user_data; + if (cli) { + cli->publish_status = btc_ble_mesh_df_client_recv_pub_cb; + } + break; + } +#else + case BLE_MESH_MODEL_ID_DF_CLI: + __ASSERT(0, "Directed Forwarding Client model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_DF_CLI */ +#if CONFIG_BLE_MESH_DF_SRV + case BLE_MESH_MODEL_ID_DF_SRV: + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_df_srv_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_df_srv_cb; + break; +#else + case BLE_MESH_MODEL_ID_DF_SRV: + __ASSERT(0, "Directed Forwarding Server model is not enabled"); + break; +#endif /* CONFIG_BLE_MESH_DF_SRV */ #if CONFIG_BLE_MESH_GENERIC_CLIENT case BLE_MESH_MODEL_ID_GEN_ONOFF_CLI: { model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_onoff_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb; bt_mesh_gen_onoff_client_t *cli = (bt_mesh_gen_onoff_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; @@ -1316,7 +1694,7 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_level_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb; bt_mesh_gen_level_client_t *cli = (bt_mesh_gen_level_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; @@ -1325,7 +1703,7 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_def_trans_time_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb; bt_mesh_gen_def_trans_time_client_t *cli = (bt_mesh_gen_def_trans_time_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; @@ -1334,7 +1712,7 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_power_onoff_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb; bt_mesh_gen_power_onoff_client_t *cli = (bt_mesh_gen_power_onoff_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; @@ -1343,7 +1721,7 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_power_level_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb; bt_mesh_gen_power_level_client_t *cli = (bt_mesh_gen_power_level_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; @@ -1352,7 +1730,7 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_battery_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb; bt_mesh_gen_battery_client_t *cli = (bt_mesh_gen_battery_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; @@ -1361,7 +1739,7 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_location_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb; bt_mesh_gen_location_client_t *cli = (bt_mesh_gen_location_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; @@ -1370,18 +1748,29 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_property_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb; bt_mesh_gen_property_client_t *cli = (bt_mesh_gen_property_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_generic_client_publish_callback; } break; } +#else + case BLE_MESH_MODEL_ID_GEN_ONOFF_CLI: + case BLE_MESH_MODEL_ID_GEN_LEVEL_CLI: + case BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI: + case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI: + case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI: + case BLE_MESH_MODEL_ID_GEN_BATTERY_CLI: + case BLE_MESH_MODEL_ID_GEN_LOCATION_CLI: + case BLE_MESH_MODEL_ID_GEN_PROP_CLI: + __ASSERT(0, "Generic Client model is not enabled"); + break; #endif /* CONFIG_BLE_MESH_GENERIC_CLIENT */ #if CONFIG_BLE_MESH_LIGHTING_CLIENT case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI: { model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_lightness_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_lighting_client_cb; bt_mesh_light_lightness_client_t *cli = (bt_mesh_light_lightness_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; } break; @@ -1390,7 +1779,7 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_ctl_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_lighting_client_cb; bt_mesh_light_ctl_client_t *cli = (bt_mesh_light_ctl_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; } break; @@ -1399,7 +1788,7 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_hsl_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_lighting_client_cb; bt_mesh_light_hsl_client_t *cli = (bt_mesh_light_hsl_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; } break; @@ -1408,7 +1797,7 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_xyl_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_lighting_client_cb; bt_mesh_light_xyl_client_t *cli = (bt_mesh_light_xyl_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; } break; @@ -1417,29 +1806,41 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_lc_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_lighting_client_cb; bt_mesh_light_lc_client_t *cli = (bt_mesh_light_lc_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; } break; } +#else + case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI: + case BLE_MESH_MODEL_ID_LIGHT_CTL_CLI: + case BLE_MESH_MODEL_ID_LIGHT_HSL_CLI: + case BLE_MESH_MODEL_ID_LIGHT_XYL_CLI: + case BLE_MESH_MODEL_ID_LIGHT_LC_CLI: + __ASSERT(0, "Lighting Client model is not enabled"); + break; #endif /* CONFIG_BLE_MESH_LIGHTING_CLIENT */ #if CONFIG_BLE_MESH_SENSOR_CLI case BLE_MESH_MODEL_ID_SENSOR_CLI: { model->op = (esp_ble_mesh_model_op_t *)bt_mesh_sensor_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_sensor_client_cb; bt_mesh_sensor_client_t *cli = (bt_mesh_sensor_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_sensor_client_publish_callback; } break; } +#else + case BLE_MESH_MODEL_ID_SENSOR_CLI: + __ASSERT(0, "Sensor Client model is not enabled"); + break; #endif /* CONFIG_BLE_MESH_SENSOR_CLI */ #if CONFIG_BLE_MESH_TIME_SCENE_CLIENT case BLE_MESH_MODEL_ID_TIME_CLI: { model->op = (esp_ble_mesh_model_op_t *)bt_mesh_time_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_time_scene_client_cb; bt_mesh_time_client_t *cli = (bt_mesh_time_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_time_scene_client_publish_callback; } break; @@ -1448,7 +1849,7 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_scene_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_time_scene_client_cb; bt_mesh_scene_client_t *cli = (bt_mesh_scene_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_time_scene_client_publish_callback; } break; @@ -1457,11 +1858,17 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->op = (esp_ble_mesh_model_op_t *)bt_mesh_scheduler_cli_op; model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_time_scene_client_cb; bt_mesh_scheduler_client_t *cli = (bt_mesh_scheduler_client_t *)model->user_data; - if (cli != NULL) { + if (cli) { cli->publish_status = btc_ble_mesh_time_scene_client_publish_callback; } break; } +#else + case BLE_MESH_MODEL_ID_TIME_CLI: + case BLE_MESH_MODEL_ID_SCENE_CLI: + case BLE_MESH_MODEL_ID_SCHEDULER_CLI: + __ASSERT(0, "Time Scene Client model is not enabled"); + break; #endif /* CONFIG_BLE_MESH_TIME_SCENE_CLIENT */ #if CONFIG_BLE_MESH_GENERIC_SERVER case BLE_MESH_MODEL_ID_GEN_ONOFF_SRV: @@ -1562,6 +1969,23 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; } break; +#else + case BLE_MESH_MODEL_ID_GEN_ONOFF_SRV: + case BLE_MESH_MODEL_ID_GEN_LEVEL_SRV: + case BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV: + case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV: + case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV: + case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV: + case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV: + case BLE_MESH_MODEL_ID_GEN_BATTERY_SRV: + case BLE_MESH_MODEL_ID_GEN_LOCATION_SRV: + case BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV: + case BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV: + case BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV: + case BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV: + case BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV: + __ASSERT(0, "Generic Server model is not enabled"); + break; #endif /* CONFIG_BLE_MESH_GENERIC_SERVER */ #if CONFIG_BLE_MESH_LIGHTING_SERVER case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: @@ -1655,6 +2079,22 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; } break; +#else + case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: + case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV: + case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: + case BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV: + case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: + case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: + case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: + case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: + case BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV: + case BLE_MESH_MODEL_ID_LIGHT_XYL_SRV: + case BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV: + case BLE_MESH_MODEL_ID_LIGHT_LC_SRV: + case BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV: + __ASSERT(0, "Lighting Server model is not enabled"); + break; #endif /* CONFIG_BLE_MESH_LIGHTING_SERVER */ #if CONFIG_BLE_MESH_TIME_SCENE_SERVER case BLE_MESH_MODEL_ID_TIME_SRV: @@ -1701,6 +2141,15 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; } break; +#else + case BLE_MESH_MODEL_ID_TIME_SRV: + case BLE_MESH_MODEL_ID_TIME_SETUP_SRV: + case BLE_MESH_MODEL_ID_SCENE_SRV: + case BLE_MESH_MODEL_ID_SCENE_SETUP_SRV: + case BLE_MESH_MODEL_ID_SCHEDULER_SRV: + case BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV: + __ASSERT(0, "Time Scene Server model is not enabled"); + break; #endif /* CONFIG_BLE_MESH_TIME_SCENE_SERVER */ #if CONFIG_BLE_MESH_SENSOR_SERVER case BLE_MESH_MODEL_ID_SENSOR_SRV: @@ -1717,7 +2166,31 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; } break; +#else + case BLE_MESH_MODEL_ID_SENSOR_SRV: + case BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV: + __ASSERT(0, "Sensor Server model is not enabled"); + break; #endif /* CONFIG_BLE_MESH_SENSOR_SERVER */ +#if CONFIG_BLE_MESH_MBT_CLI + case BLE_MESH_MODEL_ID_MBT_CLI: + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_mbt_cli_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_mbt_cli_cb; + bt_mesh_mbt_client_t *cli = (bt_mesh_mbt_client_t *)model->user_data; + if (cli) { + cli->publish_status = btc_ble_mesh_mbt_client_publish_callback; + } + break; +#endif /* CONFIG_BLE_MESH_MBT_CLI */ +#if CONFIG_BLE_MESH_MBT_SRV + case BLE_MESH_MODEL_ID_MBT_SRV: + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_mbt_srv_op; + model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_mbt_srv_cb; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; +#endif /* CONFIG_BLE_MESH_MBT_SRV */ default: goto set_vnd_op; } @@ -1728,11 +2201,10 @@ static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model) model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; } esp_ble_mesh_model_op_t *op = model->op; - while (op != NULL && op->opcode != 0) { + while (op && op->opcode != 0) { op->param_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_server_model_op_cb; op++; } - return; } void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) @@ -1791,6 +2263,11 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) 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_provisioner_adv_pkt_cb_register(btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb); +#if CONFIG_BLE_MESH_CERT_BASED_PROV + arg->mesh_init.prov->cert_based_prov_start = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_cert_based_prov_start_cb; + arg->mesh_init.prov->records_list_get = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_records_list_get_cb; + arg->mesh_init.prov->prov_record_recv_comp = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_record_recv_comp_cb; +#endif /* CONFIG_BLE_MESH_CERT_BASED_PROV */ #endif /* CONFIG_BLE_MESH_PROVISIONER */ #if CONFIG_BLE_MESH_LOW_POWER bt_mesh_lpn_set_cb(btc_ble_mesh_lpn_cb); @@ -1872,12 +2349,12 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) break; #endif /* CONFIG_BLE_MESH_NODE */ #if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_SERVER + 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); break; -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER case BTC_BLE_MESH_ACT_PROXY_IDENTITY_ENABLE: act = ESP_BLE_MESH_NODE_PROXY_IDENTITY_ENABLE_COMP_EVT; param.node_proxy_identity_enable_comp.err_code = bt_mesh_proxy_identity_enable(); @@ -2145,6 +2622,44 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) ¶m.provisioner_delete_settings_with_uid_comp.index); break; #endif /* CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE */ +#if CONFIG_BLE_MESH_CERT_BASED_PROV + extern int bt_mesh_provisioner_send_prov_records_get(uint16_t link_idx); + extern int bt_mesh_provisioner_send_prov_record_req(uint16_t link_idx, uint16_t record_id, + uint16_t frag_offset, uint16_t max_size); + extern int bt_mesh_provisioner_send_prov_invite(uint16_t link_idx); + extern int bt_mesh_provisioner_send_link_close(uint16_t link_idx); + + case BTC_BLE_MESH_ACT_PROVISIONER_SEND_PROV_RECORDS_GET: + act = ESP_BLE_MESH_PROVISIONER_SEND_PROV_RECORDS_GET_EVT; + param.provisioner_send_records_get.link_idx = arg->send_prov_records_get.link_idx; + param.provisioner_send_records_get.err_code = + bt_mesh_provisioner_send_prov_records_get(arg->send_prov_records_get.link_idx); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_SEND_PROV_RECORD_REQUEST: + act = ESP_BLE_MESH_PROVISIONER_SEND_PROV_RECORD_REQUEST_EVT; + param.provisioner_send_record_req.link_idx = arg->send_prov_record_req.link_idx; + param.provisioner_send_record_req.record_id = arg->send_prov_record_req.record_id; + param.provisioner_send_record_req.frag_offset = arg->send_prov_record_req.frag_offset; + param.provisioner_send_record_req.max_size = arg->send_prov_record_req.max_size; + param.provisioner_send_record_req.err_code = + bt_mesh_provisioner_send_prov_record_req(arg->send_prov_record_req.link_idx, + arg->send_prov_record_req.record_id, + arg->send_prov_record_req.frag_offset, + arg->send_prov_record_req.max_size); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_SEND_PROV_INVITE: + act = ESP_BLE_MESH_PROVISIONER_SEND_PROV_INVITE_EVT; + param.provisioner_send_prov_invite.link_idx = arg->send_prov_invite.link_idx; + param.provisioner_send_prov_invite.err_code = + bt_mesh_provisioner_send_prov_invite(arg->send_prov_invite.link_idx); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_SEND_LINK_CLOSE: + act = ESP_BLE_MESH_PROVISIONER_SEND_LINK_CLOSE_EVT; + param.provisioner_send_link_close.link_idx = arg->send_link_close.link_idx; + param.provisioner_send_link_close.err_code = + bt_mesh_provisioner_send_link_close(arg->send_link_close.link_idx); + break; +#endif /* CONFIG_BLE_MESH_CERT_BASED_PROV */ #endif /* CONFIG_BLE_MESH_PROVISIONER */ #if CONFIG_BLE_MESH_FAST_PROV case BTC_BLE_MESH_ACT_SET_FAST_PROV_INFO: @@ -2154,8 +2669,6 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) arg->set_fast_prov_info.unicast_max); param.set_fast_prov_info_comp.status_net_idx = bt_mesh_set_fast_prov_net_idx(arg->set_fast_prov_info.net_idx); - bt_mesh_set_fast_prov_flags_iv_index(arg->set_fast_prov_info.flags, - arg->set_fast_prov_info.iv_index); param.set_fast_prov_info_comp.status_match = bt_mesh_provisioner_set_dev_uuid_match(arg->set_fast_prov_info.offset, arg->set_fast_prov_info.match_len, @@ -2239,6 +2752,39 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) arg->proxy_client_remove_filter_addr.net_idx, &pdu); break; } +#if CONFIG_BLE_MESH_DF_CLI + case BTC_BLE_MESH_ACT_PROXY_CLIENT_DIRECTED_PROXY_SET: { + uint8_t elem_cnt = bt_mesh_elem_count(); + struct bt_mesh_proxy_cfg_pdu pdu = { + .opcode = BLE_MESH_PROXY_CFG_DIRECTED_PROXY_CONTROL, + .direct_proxy_ctrl.use_directed = arg->proxy_client_directed_proxy_set.use_directed, + }; + if (pdu.direct_proxy_ctrl.use_directed) { + pdu.direct_proxy_ctrl.proxy_client_uar.len_present = (elem_cnt >= 2); + pdu.direct_proxy_ctrl.proxy_client_uar.range_start = bt_mesh_primary_addr(); + pdu.direct_proxy_ctrl.proxy_client_uar.range_length = (elem_cnt >= 2 ? elem_cnt : 0); + } + act = ESP_BLE_MESH_PROXY_CLIENT_DIRECTED_PROXY_SET_COMP_EVT; + param.proxy_client_directed_proxy_set_comp.conn_handle = arg->proxy_client_directed_proxy_set.conn_handle; + param.proxy_client_directed_proxy_set_comp.net_idx = arg->proxy_client_directed_proxy_set.net_idx; + param.proxy_client_directed_proxy_set_comp.err_code = + bt_mesh_proxy_client_cfg_send(arg->proxy_client_directed_proxy_set.conn_handle, + arg->proxy_client_directed_proxy_set.net_idx, &pdu); + break; + } +#endif +#if CONFIG_BLE_MESH_PROXY_SOLIC_PDU_TX + case BTC_BLE_MESH_ACT_PROXY_CLIENT_SEND_SOLIC_PDU: + act = ESP_BLE_MESH_PROXY_CLIENT_SEND_SOLIC_PDU_COMP_EVT; + param.proxy_client_send_solic_pdu_comp.net_idx = arg->proxy_client_send_solic_pdu.net_idx; + param.proxy_client_send_solic_pdu_comp.ssrc = arg->proxy_client_send_solic_pdu.ssrc; + param.proxy_client_send_solic_pdu_comp.dst = arg->proxy_client_send_solic_pdu.dst; + param.proxy_client_send_solic_pdu_comp.err_code = + bt_mesh_proxy_client_solic_send(arg->proxy_client_send_solic_pdu.net_idx, + arg->proxy_client_send_solic_pdu.ssrc, + arg->proxy_client_send_solic_pdu.dst); + break; +#endif /* CONFIG_BLE_MESH_PROXY_SOLIC_PDU_TX */ #endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ case BTC_BLE_MESH_ACT_MODEL_SUBSCRIBE_GROUP_ADDR: act = ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT; @@ -2279,7 +2825,6 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) btc_ble_mesh_prov_set_complete_cb(¶m, act); btc_ble_mesh_prov_arg_deep_free(msg); - return; } void btc_ble_mesh_prov_cb_handler(btc_msg_t *msg) @@ -2298,6 +2843,8 @@ void btc_ble_mesh_prov_cb_handler(btc_msg_t *msg) } else { BT_ERR("%s, Unknown act %d", __func__, msg->act); } + + btc_ble_mesh_prov_free_req_data(msg); } void btc_ble_mesh_model_call_handler(btc_msg_t *msg) @@ -2314,16 +2861,6 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) switch (msg->act) { case BTC_BLE_MESH_ACT_MODEL_PUBLISH: { - if (arg->model_publish.device_role == PROVISIONER) { - /* Currently Provisioner only supports client model */ - err = bt_mesh_set_client_model_role((struct bt_mesh_model *)arg->model_publish.model, - arg->model_publish.device_role); - if (err) { - BT_ERR("Failed to set client role"); - btc_ble_mesh_model_publish_comp_cb(arg->model_publish.model, err); - break; - } - } err = bt_mesh_model_publish((struct bt_mesh_model *)arg->model_publish.model); btc_ble_mesh_model_publish_comp_cb(arg->model_publish.model, err); break; @@ -2339,7 +2876,6 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) } net_buf_simple_add_mem(buf, arg->model_send.data, arg->model_send.length); - arg->model_send.ctx->srv_send = true; err = bt_mesh_model_send((struct bt_mesh_model *)arg->model_send.model, (struct bt_mesh_msg_ctx *)arg->model_send.ctx, @@ -2361,16 +2897,16 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) net_buf_simple_add_mem(buf, arg->model_send.data, arg->model_send.length); bt_mesh_client_common_param_t param = { - .opcode = arg->model_send.opcode, - .model = (struct bt_mesh_model *)arg->model_send.model, - .ctx.net_idx = arg->model_send.ctx->net_idx, - .ctx.app_idx = arg->model_send.ctx->app_idx, - .ctx.addr = arg->model_send.ctx->addr, - .ctx.send_rel = arg->model_send.ctx->send_rel, - .ctx.send_ttl = arg->model_send.ctx->send_ttl, - .ctx.srv_send = false, - .msg_timeout = arg->model_send.msg_timeout, - .msg_role = arg->model_send.device_role, + .opcode = arg->model_send.opcode, + .model = (struct bt_mesh_model *)arg->model_send.model, + .ctx.net_idx = arg->model_send.ctx->net_idx, + .ctx.app_idx = arg->model_send.ctx->app_idx, + .ctx.addr = arg->model_send.ctx->addr, + .ctx.send_szmic = arg->model_send.ctx->send_szmic, + .ctx.send_ttl = arg->model_send.ctx->send_ttl, + .ctx.send_cred = arg->model_send.ctx->send_cred, + .ctx.send_tag = arg->model_send.ctx->send_tag, + .msg_timeout = arg->model_send.msg_timeout, }; err = bt_mesh_client_send_msg(¶m, buf, arg->model_send.need_rsp, btc_ble_mesh_client_model_timeout_cb); @@ -2394,7 +2930,6 @@ void btc_ble_mesh_model_call_handler(btc_msg_t *msg) } btc_ble_mesh_model_arg_deep_free(msg); - return; } void btc_ble_mesh_model_cb_handler(btc_msg_t *msg) @@ -2415,5 +2950,4 @@ void btc_ble_mesh_model_cb_handler(btc_msg_t *msg) } btc_ble_mesh_model_free_req_data(msg); - return; } 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 ae32c6586a54..0a29fe904921 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 @@ -7,6 +7,7 @@ #include #include +#include "btc_ble_mesh_model_common.h" #include "btc_ble_mesh_sensor_model.h" #include "esp_ble_mesh_sensor_model_api.h" @@ -439,8 +440,6 @@ static void btc_ble_mesh_sensor_client_callback(esp_ble_mesh_sensor_client_cb_pa { btc_msg_t msg = {0}; - BT_DBG("%s", __func__); - /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_SENSOR_CLIENT)) { return; @@ -463,7 +462,7 @@ void bt_mesh_sensor_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, esp_ble_mesh_client_common_param_t params = {0}; uint8_t act = 0U; - if (!model || !ctx) { + if (!model || !ctx || len > sizeof(cb_params.status_cb)) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -501,14 +500,14 @@ void bt_mesh_sensor_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, cb_params.params = ¶ms; if (val && len) { - memcpy(&cb_params.status_cb, val, MIN(len, sizeof(cb_params.status_cb))); + memcpy(&cb_params.status_cb, val, len); } btc_ble_mesh_sensor_client_callback(&cb_params, act); - return; } -void btc_ble_mesh_sensor_client_publish_callback(uint32_t opcode, struct bt_mesh_model *model, +void btc_ble_mesh_sensor_client_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { @@ -519,15 +518,13 @@ void btc_ble_mesh_sensor_client_publish_callback(uint32_t opcode, struct bt_mesh bt_mesh_sensor_client_cb_evt_to_btc(opcode, BTC_BLE_MESH_EVT_SENSOR_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); - return; } void btc_ble_mesh_sensor_client_call_handler(btc_msg_t *msg) { - esp_ble_mesh_client_common_param_t *params = NULL; - btc_ble_mesh_sensor_client_args_t *arg = NULL; esp_ble_mesh_sensor_client_cb_param_t cb = {0}; - bt_mesh_client_common_param_t common = {0}; + btc_ble_mesh_sensor_client_args_t *arg = NULL; + bt_mesh_client_common_param_t param = {0}; if (!msg) { BT_ERR("%s, Invalid parameter", __func__); @@ -537,52 +534,31 @@ void btc_ble_mesh_sensor_client_call_handler(btc_msg_t *msg) arg = (btc_ble_mesh_sensor_client_args_t *)(msg->arg); switch (msg->act) { - case BTC_BLE_MESH_ACT_SENSOR_CLIENT_GET_STATE: { - params = arg->sensor_client_get_state.params; - common.opcode = params->opcode; - common.model = (struct bt_mesh_model *)params->model; - common.ctx.net_idx = params->ctx.net_idx; - common.ctx.app_idx = params->ctx.app_idx; - common.ctx.addr = params->ctx.addr; - common.ctx.send_rel = params->ctx.send_rel; - common.ctx.send_ttl = params->ctx.send_ttl; - common.msg_timeout = params->msg_timeout; - common.msg_role = params->msg_role; + case BTC_BLE_MESH_ACT_SENSOR_CLIENT_GET_STATE: + btc_ble_mesh_set_client_common_param(arg->sensor_client_get_state.params, ¶m, false); cb.params = arg->sensor_client_get_state.params; - cb.error_code = bt_mesh_sensor_client_get_state(&common, arg->sensor_client_get_state.get_state); + cb.error_code = bt_mesh_sensor_client_get_state(¶m, arg->sensor_client_get_state.get_state); if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ btc_ble_mesh_sensor_client_callback(&cb, ESP_BLE_MESH_SENSOR_CLIENT_GET_STATE_EVT); } break; - } - case BTC_BLE_MESH_ACT_SENSOR_CLIENT_SET_STATE: { - params = arg->sensor_client_set_state.params; - common.opcode = params->opcode; - common.model = (struct bt_mesh_model *)params->model; - common.ctx.net_idx = params->ctx.net_idx; - common.ctx.app_idx = params->ctx.app_idx; - common.ctx.addr = params->ctx.addr; - common.ctx.send_rel = params->ctx.send_rel; - common.ctx.send_ttl = params->ctx.send_ttl; - common.msg_timeout = params->msg_timeout; - common.msg_role = params->msg_role; + case BTC_BLE_MESH_ACT_SENSOR_CLIENT_SET_STATE: + btc_ble_mesh_set_client_common_param(arg->sensor_client_set_state.params, ¶m, false); cb.params = arg->sensor_client_set_state.params; - cb.error_code = bt_mesh_sensor_client_set_state(&common, arg->sensor_client_set_state.set_state); + cb.error_code = bt_mesh_sensor_client_set_state(¶m, arg->sensor_client_set_state.set_state); if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ btc_ble_mesh_sensor_client_callback(&cb, ESP_BLE_MESH_SENSOR_CLIENT_SET_STATE_EVT); } break; - } default: break; } btc_ble_mesh_sensor_client_arg_deep_free(msg); - return; } void btc_ble_mesh_sensor_client_cb_handler(btc_msg_t *msg) @@ -603,7 +579,6 @@ void btc_ble_mesh_sensor_client_cb_handler(btc_msg_t *msg) } btc_ble_mesh_sensor_client_free_req_data(msg); - return; } #endif /* CONFIG_BLE_MESH_SENSOR_CLI */ @@ -771,7 +746,7 @@ static void btc_ble_mesh_sensor_server_free_req_data(btc_msg_t *msg) switch (msg->act) { case ESP_BLE_MESH_SENSOR_SERVER_STATE_CHANGE_EVT: if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET || - arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK) { + arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK) { bt_mesh_free_buf(arg->value.state_change.sensor_cadence_set.trigger_delta_down); bt_mesh_free_buf(arg->value.state_change.sensor_cadence_set.trigger_delta_up); bt_mesh_free_buf(arg->value.state_change.sensor_cadence_set.fast_cadence_low); @@ -790,7 +765,7 @@ static void btc_ble_mesh_sensor_server_free_req_data(btc_msg_t *msg) break; case ESP_BLE_MESH_SENSOR_SERVER_RECV_SET_MSG_EVT: if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET || - arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK) { + arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK) { bt_mesh_free_buf(arg->value.set.sensor_cadence.cadence); } else if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET || arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK) { @@ -806,8 +781,6 @@ static void btc_ble_mesh_sensor_server_callback(esp_ble_mesh_sensor_server_cb_pa { btc_msg_t msg = {0}; - BT_DBG("%s", __func__); - /* If corresponding callback is not registered, event will not be posted. */ if (!btc_profile_cb_get(BTC_PID_SENSOR_SERVER)) { return; @@ -821,14 +794,15 @@ static void btc_ble_mesh_sensor_server_callback(esp_ble_mesh_sensor_server_cb_pa btc_ble_mesh_sensor_server_copy_req_data, btc_ble_mesh_sensor_server_free_req_data); } -void bt_mesh_sensor_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model *model, +void bt_mesh_sensor_server_cb_evt_to_btc(uint8_t evt_type, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const uint8_t *val, size_t len) { esp_ble_mesh_sensor_server_cb_param_t cb_params = {0}; uint8_t act = 0U; - if (model == NULL || ctx == NULL) { + if (model == NULL || ctx == NULL || len > sizeof(cb_params.value)) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -859,11 +833,10 @@ void bt_mesh_sensor_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model cb_params.ctx.send_ttl = ctx->send_ttl; if (val && len) { - memcpy(&cb_params.value, val, MIN(len, sizeof(cb_params.value))); + memcpy(&cb_params.value, val, len); } btc_ble_mesh_sensor_server_callback(&cb_params, act); - return; } void btc_ble_mesh_sensor_server_cb_handler(btc_msg_t *msg) @@ -884,7 +857,6 @@ void btc_ble_mesh_sensor_server_cb_handler(btc_msg_t *msg) } btc_ble_mesh_sensor_server_free_req_data(msg); - return; } #endif /* CONFIG_BLE_MESH_SENSOR_SERVER */ 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 977a42836f1e..a8556a9023f5 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 @@ -7,6 +7,7 @@ #include #include +#include "btc_ble_mesh_model_common.h" #include "btc_ble_mesh_time_scene_model.h" #include "esp_ble_mesh_time_scene_model_api.h" @@ -79,7 +80,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) { + if (!msg) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -166,7 +167,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) { + if (!msg) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -203,8 +204,6 @@ static void btc_ble_mesh_time_scene_client_callback(esp_ble_mesh_time_scene_clie { btc_msg_t msg = {0}; - 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)) { return; @@ -227,7 +226,7 @@ void bt_mesh_time_scene_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, esp_ble_mesh_client_common_param_t params = {0}; uint8_t act = 0U; - if (!model || !ctx) { + if (!model || !ctx || len > sizeof(cb_params.status_cb)) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -265,14 +264,14 @@ void bt_mesh_time_scene_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, cb_params.params = ¶ms; if (val && len) { - memcpy(&cb_params.status_cb, val, MIN(len, sizeof(cb_params.status_cb))); + memcpy(&cb_params.status_cb, val, len); } btc_ble_mesh_time_scene_client_callback(&cb_params, act); - return; } -void btc_ble_mesh_time_scene_client_publish_callback(uint32_t opcode, struct bt_mesh_model *model, +void btc_ble_mesh_time_scene_client_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { @@ -283,17 +282,15 @@ void btc_ble_mesh_time_scene_client_publish_callback(uint32_t opcode, struct bt_ bt_mesh_time_scene_client_cb_evt_to_btc(opcode, BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); - return; } void btc_ble_mesh_time_scene_client_call_handler(btc_msg_t *msg) { - btc_ble_mesh_time_scene_client_args_t *arg = NULL; - esp_ble_mesh_client_common_param_t *params = NULL; esp_ble_mesh_time_scene_client_cb_param_t cb = {0}; - bt_mesh_client_common_param_t common = {0}; + btc_ble_mesh_time_scene_client_args_t *arg = NULL; + bt_mesh_client_common_param_t param = {0}; - if (!msg || !msg->arg) { + if (!msg) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -301,59 +298,38 @@ void btc_ble_mesh_time_scene_client_call_handler(btc_msg_t *msg) arg = (btc_ble_mesh_time_scene_client_args_t *)(msg->arg); switch (msg->act) { - case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_GET_STATE: { - params = arg->time_scene_client_get_state.params; - common.opcode = params->opcode; - common.model = (struct bt_mesh_model *)params->model; - common.ctx.net_idx = params->ctx.net_idx; - common.ctx.app_idx = params->ctx.app_idx; - common.ctx.addr = params->ctx.addr; - common.ctx.send_rel = params->ctx.send_rel; - common.ctx.send_ttl = params->ctx.send_ttl; - common.msg_timeout = params->msg_timeout; - common.msg_role = params->msg_role; + case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_GET_STATE: + btc_ble_mesh_set_client_common_param(arg->time_scene_client_get_state.params, ¶m, false); cb.params = arg->time_scene_client_get_state.params; - cb.error_code = bt_mesh_time_scene_client_get_state(&common, arg->time_scene_client_get_state.get_state); + cb.error_code = bt_mesh_time_scene_client_get_state(¶m, arg->time_scene_client_get_state.get_state); if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ btc_ble_mesh_time_scene_client_callback(&cb, ESP_BLE_MESH_TIME_SCENE_CLIENT_GET_STATE_EVT); } break; - } - case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_SET_STATE: { - params = arg->time_scene_client_set_state.params; - common.opcode = params->opcode; - common.model = (struct bt_mesh_model *)params->model; - common.ctx.net_idx = params->ctx.net_idx; - common.ctx.app_idx = params->ctx.app_idx; - common.ctx.addr = params->ctx.addr; - common.ctx.send_rel = params->ctx.send_rel; - common.ctx.send_ttl = params->ctx.send_ttl; - common.msg_timeout = params->msg_timeout; - common.msg_role = params->msg_role; + case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_SET_STATE: + btc_ble_mesh_set_client_common_param(arg->time_scene_client_set_state.params, ¶m, false); cb.params = arg->time_scene_client_set_state.params; - cb.error_code = bt_mesh_time_scene_client_set_state(&common, arg->time_scene_client_set_state.set_state); + cb.error_code = bt_mesh_time_scene_client_set_state(¶m, arg->time_scene_client_set_state.set_state); if (cb.error_code) { /* If send failed, callback error_code to app layer immediately */ btc_ble_mesh_time_scene_client_callback(&cb, ESP_BLE_MESH_TIME_SCENE_CLIENT_SET_STATE_EVT); } break; - } default: break; } btc_ble_mesh_time_scene_client_arg_deep_free(msg); - return; } 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) { + if (!msg) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -367,7 +343,6 @@ void btc_ble_mesh_time_scene_client_cb_handler(btc_msg_t *msg) } btc_ble_mesh_time_scene_client_free_req_data(msg); - return; } #endif /* CONFIG_BLE_MESH_TIME_SCENE_CLIENT */ @@ -390,8 +365,6 @@ static void btc_ble_mesh_time_scene_server_callback(esp_ble_mesh_time_scene_serv { btc_msg_t msg = {0}; - 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)) { return; @@ -404,14 +377,15 @@ static void btc_ble_mesh_time_scene_server_callback(esp_ble_mesh_time_scene_serv btc_transfer_context(&msg, cb_params, cb_params == NULL ? 0 : sizeof(esp_ble_mesh_time_scene_server_cb_param_t), NULL, NULL); } -void bt_mesh_time_scene_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model *model, +void bt_mesh_time_scene_server_cb_evt_to_btc(uint8_t evt_type, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const uint8_t *val, size_t len) { esp_ble_mesh_time_scene_server_cb_param_t cb_params = {0}; uint8_t act = 0U; - if (model == NULL || ctx == NULL) { + if (model == NULL || ctx == NULL || len > sizeof(cb_params.value)) { BT_ERR("%s, Invalid parameter", __func__); return; } @@ -445,11 +419,10 @@ void bt_mesh_time_scene_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_mo cb_params.ctx.send_ttl = ctx->send_ttl; if (val && len) { - memcpy(&cb_params.value, val, MIN(len, sizeof(cb_params.value))); + memcpy(&cb_params.value, val, len); } btc_ble_mesh_time_scene_server_callback(&cb_params, act); - return; } void btc_ble_mesh_time_scene_server_cb_handler(btc_msg_t *msg) @@ -468,8 +441,6 @@ void btc_ble_mesh_time_scene_server_cb_handler(btc_msg_t *msg) } else { BT_ERR("%s, Unknown act %d", __func__, msg->act); } - - return; } #endif /* CONFIG_BLE_MESH_TIME_SCENE_SERVER */ 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 ce81b6c261f8..9b8e6fd3b80d 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 @@ -47,7 +47,8 @@ void btc_ble_mesh_config_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void void btc_ble_mesh_config_client_arg_deep_free(btc_msg_t *msg); -void btc_ble_mesh_config_client_publish_callback(uint32_t opcode, struct bt_mesh_model *model, +void btc_ble_mesh_config_client_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); @@ -63,7 +64,8 @@ typedef enum { BTC_BLE_MESH_EVT_CONFIG_SERVER_MAX, } btc_ble_mesh_config_server_evt_t; -void bt_mesh_config_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model *model, +void bt_mesh_config_server_cb_evt_to_btc(uint8_t evt_type, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const uint8_t *val, size_t len); 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 d06785094df9..e7c98a23ad76 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 @@ -47,7 +47,8 @@ void btc_ble_mesh_generic_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, voi void btc_ble_mesh_generic_client_arg_deep_free(btc_msg_t *msg); -void btc_ble_mesh_generic_client_publish_callback(uint32_t opcode, struct bt_mesh_model *model, +void btc_ble_mesh_generic_client_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); @@ -63,7 +64,8 @@ typedef enum { BTC_BLE_MESH_EVT_GENERIC_SERVER_MAX, } btc_ble_mesh_generic_server_evt_t; -void bt_mesh_generic_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model *model, +void bt_mesh_generic_server_cb_evt_to_btc(uint8_t evt_type, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const uint8_t *val, size_t len); 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 a04745485bf6..46da45682779 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 @@ -47,7 +47,8 @@ void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg); void btc_ble_mesh_health_client_cb_handler(btc_msg_t *msg); -void btc_ble_mesh_health_publish_callback(uint32_t opcode, struct bt_mesh_model *model, +void btc_ble_mesh_health_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); 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 45ca7ee14be4..3ec2cd7dedba 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 @@ -47,7 +47,8 @@ void btc_ble_mesh_lighting_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, vo void btc_ble_mesh_lighting_client_arg_deep_free(btc_msg_t *msg); -void btc_ble_mesh_lighting_client_publish_callback(uint32_t opcode, struct bt_mesh_model *model, +void btc_ble_mesh_lighting_client_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); @@ -64,7 +65,8 @@ typedef enum { BTC_BLE_MESH_EVT_LIGHTING_SERVER_MAX, } btc_ble_mesh_lighting_server_evt_t; -void bt_mesh_lighting_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model *model, +void bt_mesh_lighting_server_cb_evt_to_btc(uint8_t evt_type, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const uint8_t *val, size_t len); diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_model_common.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_model_common.h new file mode 100644 index 000000000000..8c1f1d37b9b5 --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_model_common.h @@ -0,0 +1,42 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BTC_BLE_MESH_MODEL_COMMON_H_ +#define _BTC_BLE_MESH_MODEL_COMMON_H_ + +#include "btc/btc_manage.h" + +#include "mesh/access.h" +#include "mesh/client_common.h" +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +static inline void btc_ble_mesh_set_client_common_param(esp_ble_mesh_client_common_param_t *input, + bt_mesh_client_common_param_t *output, + bool use_dev_key) +{ + if (input && output) { + output->opcode = input->opcode; + output->model = (struct bt_mesh_model *)input->model; + output->ctx.net_idx = input->ctx.net_idx; + output->ctx.app_idx = use_dev_key ? BLE_MESH_KEY_DEV : input->ctx.app_idx; + output->ctx.addr = input->ctx.addr; + output->ctx.send_szmic = input->ctx.send_szmic; + output->ctx.send_ttl = input->ctx.send_ttl; + output->ctx.send_cred = input->ctx.send_cred; + output->ctx.send_tag = input->ctx.send_tag; + output->msg_timeout = input->msg_timeout; + } +} + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_MODEL_COMMON_H_ */ 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 e09a454a1ff4..efdae7f337f9 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 @@ -12,7 +12,6 @@ #include "mesh/config.h" #include "mesh/main.h" #include "fast_prov.h" -#include "prov_pvnr.h" #include "esp_ble_mesh_defs.h" #ifdef __cplusplus @@ -65,6 +64,10 @@ typedef enum { BTC_BLE_MESH_ACT_PROVISIONER_CLOSE_SETTINGS_WITH_UID, BTC_BLE_MESH_ACT_PROVISIONER_DELETE_SETTINGS_WITH_INDEX, BTC_BLE_MESH_ACT_PROVISIONER_DELETE_SETTINGS_WITH_UID, + BTC_BLE_MESH_ACT_PROVISIONER_SEND_PROV_RECORDS_GET, + BTC_BLE_MESH_ACT_PROVISIONER_SEND_PROV_RECORD_REQUEST, + BTC_BLE_MESH_ACT_PROVISIONER_SEND_PROV_INVITE, + BTC_BLE_MESH_ACT_PROVISIONER_SEND_LINK_CLOSE, BTC_BLE_MESH_ACT_SET_FAST_PROV_INFO, BTC_BLE_MESH_ACT_SET_FAST_PROV_ACTION, BTC_BLE_MESH_ACT_LPN_ENABLE, @@ -75,6 +78,8 @@ typedef enum { BTC_BLE_MESH_ACT_PROXY_CLIENT_SET_FILTER_TYPE, BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR, BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR, + BTC_BLE_MESH_ACT_PROXY_CLIENT_DIRECTED_PROXY_SET, + BTC_BLE_MESH_ACT_PROXY_CLIENT_SEND_SOLIC_PDU, BTC_BLE_MESH_ACT_MODEL_SUBSCRIBE_GROUP_ADDR, BTC_BLE_MESH_ACT_MODEL_UNSUBSCRIBE_GROUP_ADDR, BTC_BLE_MESH_ACT_DEINIT_MESH, @@ -248,6 +253,21 @@ typedef union { struct { char uid[ESP_BLE_MESH_SETTINGS_UID_SIZE + 1]; } delete_settings_with_uid; + struct { + uint16_t link_idx; + } send_prov_records_get; + struct { + uint16_t link_idx; + uint16_t record_id; + uint16_t frag_offset; + uint16_t max_size; + } send_prov_record_req; + struct { + uint16_t link_idx; + } send_prov_invite; + struct { + uint16_t link_idx; + } send_link_close; struct ble_mesh_set_fast_prov_info_args { uint16_t unicast_min; uint16_t unicast_max; @@ -295,6 +315,16 @@ typedef union { uint16_t addr_num; uint16_t *addr; } proxy_client_remove_filter_addr; + struct ble_mesh_proxy_client_directed_proxy_set_args { + uint8_t conn_handle; + uint16_t net_idx; + uint8_t use_directed; + } proxy_client_directed_proxy_set; + struct { + uint16_t net_idx; + uint16_t ssrc; + uint16_t dst; + } proxy_client_send_solic_pdu; struct ble_mesh_model_sub_group_addr_args { uint16_t element_addr; uint16_t company_id; @@ -315,7 +345,7 @@ typedef union { typedef union { struct ble_mesh_model_publish_args { esp_ble_mesh_model_t *model; - uint8_t device_role; + uint8_t device_role __attribute__((deprecated)); } model_publish; struct ble_mesh_model_send_args { esp_ble_mesh_model_t *model; @@ -324,7 +354,7 @@ typedef union { bool need_rsp; uint16_t length; uint8_t *data; - uint8_t device_role; + uint8_t device_role __attribute__((deprecated)); int32_t msg_timeout; } model_send; struct ble_mesh_server_model_update_state_args { @@ -389,6 +419,11 @@ void btc_ble_mesh_model_cb_handler(btc_msg_t *msg); void btc_ble_mesh_prov_call_handler(btc_msg_t *msg); void btc_ble_mesh_prov_cb_handler(btc_msg_t *msg); +#if CONFIG_BLE_MESH_DF_SRV +int btc_ble_mesh_enable_directed_forwarding(uint16_t net_idx, bool directed_forwarding, + bool directed_forwarding_relay); +#endif + #ifdef __cplusplus } #endif 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 3d216c7fa95d..bc4a3ca427bf 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 @@ -47,7 +47,8 @@ void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void void btc_ble_mesh_sensor_client_arg_deep_free(btc_msg_t *msg); -void btc_ble_mesh_sensor_client_publish_callback(uint32_t opcode, struct bt_mesh_model *model, +void btc_ble_mesh_sensor_client_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); @@ -63,7 +64,8 @@ typedef enum { BTC_BLE_MESH_EVT_SENSOR_SERVER_MAX, } btc_ble_mesh_sensor_server_evt_t; -void bt_mesh_sensor_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model *model, +void bt_mesh_sensor_server_cb_evt_to_btc(uint8_t evt_type, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const uint8_t *val, size_t len); 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 6c878dc7769a..e18c60df3e08 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 @@ -45,7 +45,10 @@ void btc_ble_mesh_time_scene_client_cb_handler(btc_msg_t *msg); void btc_ble_mesh_time_scene_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); -void btc_ble_mesh_time_scene_client_publish_callback(uint32_t opcode, struct bt_mesh_model *model, +void btc_ble_mesh_time_scene_client_arg_deep_free(btc_msg_t *msg); + +void btc_ble_mesh_time_scene_client_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); @@ -62,7 +65,8 @@ typedef enum { BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_MAX, } btc_ble_mesh_time_scene_server_evt_t; -void bt_mesh_time_scene_server_cb_evt_to_btc(uint8_t evt_type, struct bt_mesh_model *model, +void bt_mesh_time_scene_server_cb_evt_to_btc(uint8_t evt_type, + struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, const uint8_t *val, size_t len); diff --git a/components/bt/esp_ble_mesh/common/buf.c b/components/bt/esp_ble_mesh/common/buf.c index cc240e07c510..f1c9559dd0cd 100644 --- a/components/bt/esp_ble_mesh/common/buf.c +++ b/components/bt/esp_ble_mesh/common/buf.c @@ -281,7 +281,7 @@ uint32_t net_buf_simple_pull_le24(struct net_buf_simple *buf) { struct uint24 { uint32_t u24:24; - } __packed val; + } __attribute__((packed)) val; val = UNALIGNED_GET((struct uint24 *)buf->data); net_buf_simple_pull(buf, sizeof(val)); @@ -293,7 +293,7 @@ uint32_t net_buf_simple_pull_be24(struct net_buf_simple *buf) { struct uint24 { uint32_t u24:24; - } __packed val; + } __attribute__((packed)) val; val = UNALIGNED_GET((struct uint24 *)buf->data); net_buf_simple_pull(buf, sizeof(val)); @@ -325,7 +325,7 @@ uint64_t net_buf_simple_pull_le48(struct net_buf_simple *buf) { struct uint48 { uint64_t u48:48; - } __packed val; + } __attribute__((packed)) val; val = UNALIGNED_GET((struct uint48 *)buf->data); net_buf_simple_pull(buf, sizeof(val)); @@ -337,7 +337,7 @@ uint64_t net_buf_simple_pull_be48(struct net_buf_simple *buf) { struct uint48 { uint64_t u48:48; - } __packed val; + } __attribute__((packed)) val; val = UNALIGNED_GET((struct uint48 *)buf->data); net_buf_simple_pull(buf, sizeof(val)); @@ -459,7 +459,7 @@ struct net_buf *net_buf_ref(struct net_buf *buf) return buf; } -#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +#if CONFIG_BLE_MESH_NET_BUF_LOG void net_buf_unref_debug(struct net_buf *buf, const char *func, int line) #else void net_buf_unref(struct net_buf *buf) @@ -471,7 +471,7 @@ void net_buf_unref(struct net_buf *buf) struct net_buf *frags = buf->frags; struct net_buf_pool *pool = NULL; -#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +#if CONFIG_BLE_MESH_NET_BUF_LOG if (!buf->ref) { NET_BUF_ERR("%s():%d: buf %p double free", func, line, buf); @@ -491,7 +491,7 @@ void net_buf_unref(struct net_buf *buf) pool = buf->pool; pool->uninit_count++; -#if defined(CONFIG_BLE_MESH_NET_BUF_POOL_USAGE) +#if CONFIG_BLE_MESH_NET_BUF_POOL_USAGE pool->avail_count++; NET_BUF_DBG("Unref, pool %p, avail_count %d, uninit_count %d", pool, pool->avail_count, pool->uninit_count); @@ -533,7 +533,7 @@ static uint8_t *data_alloc(struct net_buf *buf, size_t *size, int32_t timeout) return pool->alloc->cb->alloc(buf, size, timeout); } -#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +#if CONFIG_BLE_MESH_NET_BUF_LOG struct net_buf *net_buf_alloc_len_debug(struct net_buf_pool *pool, size_t size, int32_t timeout, const char *func, int line) #else @@ -594,7 +594,7 @@ struct net_buf *net_buf_alloc_len(struct net_buf_pool *pool, size_t size, net_buf_reset(buf); pool->uninit_count--; -#if defined(CONFIG_BLE_MESH_NET_BUF_POOL_USAGE) +#if CONFIG_BLE_MESH_NET_BUF_POOL_USAGE pool->avail_count--; NET_BUF_ASSERT(pool->avail_count >= 0); #endif @@ -602,7 +602,7 @@ struct net_buf *net_buf_alloc_len(struct net_buf_pool *pool, size_t size, return buf; } -#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +#if CONFIG_BLE_MESH_NET_BUF_LOG struct net_buf *net_buf_alloc_fixed_debug(struct net_buf_pool *pool, int32_t timeout, const char *func, int line) @@ -656,7 +656,7 @@ struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag) return head; } -#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +#if CONFIG_BLE_MESH_NET_BUF_LOG struct net_buf *net_buf_frag_del_debug(struct net_buf *parent, struct net_buf *frag, const char *func, int line) @@ -678,7 +678,7 @@ struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag) frag->frags = NULL; -#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +#if CONFIG_BLE_MESH_NET_BUF_LOG net_buf_unref_debug(frag, func, line); #else net_buf_unref(frag); diff --git a/components/bt/esp_ble_mesh/common/common.c b/components/bt/esp_ble_mesh/common/common.c index 60696aa56514..87f0c6a7bc36 100644 --- a/components/bt/esp_ble_mesh/common/common.c +++ b/components/bt/esp_ble_mesh/common/common.c @@ -72,25 +72,6 @@ void bt_mesh_free_buf(struct net_buf_simple *buf) } } -uint8_t bt_mesh_get_device_role(struct bt_mesh_model *model, bool srv_send) -{ - bt_mesh_client_user_data_t *client = NULL; - - if (srv_send) { - BT_DBG("Message is sent by a server model"); - return NODE; - } - - if (!model || !model->user_data) { - BT_ERR("%s, Invalid parameter", __func__); - return ROLE_NVAL; - } - - client = (bt_mesh_client_user_data_t *)model->user_data; - - return client->msg_role; -} - int bt_mesh_rand(void *buf, size_t len) { if (buf == NULL || len == 0) { diff --git a/components/bt/esp_ble_mesh/common/include/mesh/buf.h b/components/bt/esp_ble_mesh/common/include/mesh/buf.h index 4664626a6211..5e5538b1d04c 100644 --- a/components/bt/esp_ble_mesh/common/include/mesh/buf.h +++ b/components/bt/esp_ble_mesh/common/include/mesh/buf.h @@ -816,7 +816,7 @@ struct net_buf_pool { /** Number of uninitialized buffers */ uint16_t uninit_count; -#if defined(CONFIG_BLE_MESH_NET_BUF_POOL_USAGE) +#if CONFIG_BLE_MESH_NET_BUF_POOL_USAGE /** Amount of available buffers in the pool. */ int16_t avail_count; @@ -837,7 +837,7 @@ struct net_buf_pool { struct net_buf *const __bufs; }; -#if defined(CONFIG_BLE_MESH_NET_BUF_POOL_USAGE) +#if CONFIG_BLE_MESH_NET_BUF_POOL_USAGE #define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _destroy) \ { \ .alloc = _alloc, \ @@ -962,11 +962,11 @@ int net_buf_id(struct net_buf *buf); * * @return New buffer or NULL if out of buffers. */ -#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +#if CONFIG_BLE_MESH_NET_BUF_LOG struct net_buf *net_buf_alloc_fixed_debug(struct net_buf_pool *pool, int32_t timeout, const char *func, int line); #define net_buf_alloc_fixed(_pool, _timeout) \ - net_buf_alloc_fixed_debug(_pool, _timeout, __func__, __LINE__) + net_buf_alloc_fixed_debug(_pool, _timeout, __func__, __LINE__) #else struct net_buf *net_buf_alloc_fixed(struct net_buf_pool *pool, int32_t timeout); #endif @@ -1031,7 +1031,7 @@ struct net_buf *net_buf_slist_get(sys_slist_t *list); * * @param buf A valid pointer on a buffer */ -#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +#if CONFIG_BLE_MESH_NET_BUF_LOG void net_buf_unref_debug(struct net_buf *buf, const char *func, int line); #define net_buf_unref(_buf) \ net_buf_unref_debug(_buf, __func__, __LINE__) @@ -1630,12 +1630,12 @@ struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag); * @return Pointer to the buffer following the fragment, or NULL if it * had no further fragments. */ -#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +#if CONFIG_BLE_MESH_NET_BUF_LOG struct net_buf *net_buf_frag_del_debug(struct net_buf *parent, struct net_buf *frag, const char *func, int line); #define net_buf_frag_del(_parent, _frag) \ - net_buf_frag_del_debug(_parent, _frag, __func__, __LINE__) + net_buf_frag_del_debug(_parent, _frag, __func__, __LINE__) #else struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag); #endif diff --git a/components/bt/esp_ble_mesh/common/include/mesh/byteorder.h b/components/bt/esp_ble_mesh/common/include/mesh/byteorder.h index 85fe44a47c4f..6926b80475e8 100644 --- a/components/bt/esp_ble_mesh/common/include/mesh/byteorder.h +++ b/components/bt/esp_ble_mesh/common/include/mesh/byteorder.h @@ -335,7 +335,7 @@ static inline void sys_put_le16(uint16_t val, uint8_t dst[2]) * @brief Put a 24-bit integer as little-endian to arbitrary location. * * Put a 24-bit integer, originally in host endianness, to a - * potentially unaligned memory location in littel-endian format. + * potentially unaligned memory location in little-endian format. * * @param val 24-bit integer in host endianness. * @param dst Destination memory address to store the result. diff --git a/components/bt/esp_ble_mesh/common/include/mesh/common.h b/components/bt/esp_ble_mesh/common/include/mesh/common.h index 14f8f78e909f..db6c1d372a7a 100644 --- a/components/bt/esp_ble_mesh/common/include/mesh/common.h +++ b/components/bt/esp_ble_mesh/common/include/mesh/common.h @@ -51,20 +51,6 @@ struct net_buf_simple *bt_mesh_alloc_buf(uint16_t size); */ void bt_mesh_free_buf(struct net_buf_simple *buf); -/** - * @brief This function gets device role for stack internal use. - * - * @Note Currently Provisioner only support client models, Node supports - * client models and server models. Hence if srv_send is set to be - * TRUE, then role NODE will be returned. - * - * @param[in] model: Pointer to the model structure - * @param[in] srv_send: Indicate if the message is sent by a server model - * - * @return 0 - Node, 1 - Provisioner - */ -uint8_t bt_mesh_get_device_role(struct bt_mesh_model *model, bool srv_send); - int bt_mesh_rand(void *buf, size_t len); #ifdef __cplusplus diff --git a/components/bt/esp_ble_mesh/common/include/mesh/config.h b/components/bt/esp_ble_mesh/common/include/mesh/config.h index d75a4588c31b..ab06eda8f06b 100644 --- a/components/bt/esp_ble_mesh/common/include/mesh/config.h +++ b/components/bt/esp_ble_mesh/common/include/mesh/config.h @@ -13,6 +13,17 @@ extern "C" { #endif +#ifndef CONFIG_BLE_MESH_PBA_SAME_TIME +#define CONFIG_BLE_MESH_PBA_SAME_TIME 0 +#endif + +#ifndef CONFIG_BLE_MESH_PBG_SAME_TIME +#define CONFIG_BLE_MESH_PBG_SAME_TIME 0 +#endif + +#define CONFIG_BLE_MESH_PRIVATE_BEACON (CONFIG_BLE_MESH_PRB_SRV || \ + CONFIG_BLE_MESH_PRB_CLI) + #define CONFIG_BLE_MESH_GENERIC_CLIENT (CONFIG_BLE_MESH_GENERIC_ONOFF_CLI || \ CONFIG_BLE_MESH_GENERIC_LEVEL_CLI || \ CONFIG_BLE_MESH_GENERIC_DEF_TRANS_TIME_CLI || \ @@ -40,6 +51,9 @@ extern "C" { #define CONFIG_BLE_MESH_BLE_COEX_SUPPORT (CONFIG_BLE_MESH_SUPPORT_BLE_ADV || \ CONFIG_BLE_MESH_SUPPORT_BLE_SCAN) +#define CONFIG_BLE_MESH_PROXY_SOLIC (CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX || \ + CONFIG_BLE_MESH_PROXY_SOLIC_PDU_TX) + #ifdef __cplusplus } #endif diff --git a/components/bt/esp_ble_mesh/common/include/mesh/mutex.h b/components/bt/esp_ble_mesh/common/include/mesh/mutex.h index 0b364a65f749..ee89750029f1 100644 --- a/components/bt/esp_ble_mesh/common/include/mesh/mutex.h +++ b/components/bt/esp_ble_mesh/common/include/mesh/mutex.h @@ -27,6 +27,11 @@ void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex); void bt_mesh_mutex_lock(bt_mesh_mutex_t *mutex); void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex); +void bt_mesh_r_mutex_create(bt_mesh_mutex_t *mutex); +void bt_mesh_r_mutex_free(bt_mesh_mutex_t *mutex); +void bt_mesh_r_mutex_lock(bt_mesh_mutex_t *mutex); +void bt_mesh_r_mutex_unlock(bt_mesh_mutex_t *mutex); + void bt_mesh_alarm_lock(void); void bt_mesh_alarm_unlock(void); diff --git a/components/bt/esp_ble_mesh/common/include/mesh/timer.h b/components/bt/esp_ble_mesh/common/include/mesh/timer.h index f29c06fcc526..e1296aa82062 100644 --- a/components/bt/esp_ble_mesh/common/include/mesh/timer.h +++ b/components/bt/esp_ble_mesh/common/include/mesh/timer.h @@ -50,15 +50,15 @@ struct k_work; typedef void (*k_work_handler_t)(struct k_work *work); struct k_work { - void *_reserved; k_work_handler_t handler; int index; + void *user_data; }; #define _K_WORK_INITIALIZER(work_handler) \ { \ - ._reserved = NULL, \ .handler = work_handler, \ + .user_data = NULL, \ } /** @@ -119,6 +119,18 @@ struct k_work { */ #define K_HOURS(h) K_MINUTES((h) * 60) +/** + * @brief Generate timeout delay from days. + * + * This macro generates a timeout delay that that instructs a kernel API + * to wait up to @a d days to perform the requested operation. + * + * @param d Duration in days. + * + * @return Timeout delay value. + */ +#define K_DAYS(d) K_HOURS((d) * 24) + /** * @brief Generate infinite timeout delay. * diff --git a/components/bt/esp_ble_mesh/common/include/mesh/trace.h b/components/bt/esp_ble_mesh/common/include/mesh/trace.h index 1ff7be41b207..48f604f1c2e2 100644 --- a/components/bt/esp_ble_mesh/common/include/mesh/trace.h +++ b/components/bt/esp_ble_mesh/common/include/mesh/trace.h @@ -92,7 +92,7 @@ extern "C" { #define BT_DBG(fmt, args...) #endif -#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) && (!CONFIG_BLE_MESH_NO_LOG) +#if (CONFIG_BLE_MESH_NET_BUF_LOG && !CONFIG_BLE_MESH_NO_LOG) #define NET_BUF_ERR(fmt, args...) do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_ERROR) && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, ERROR)) BLE_MESH_PRINT_E(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0) #define NET_BUF_WARN(fmt, args...) do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_WARN) && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, WARN)) BLE_MESH_PRINT_W(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0) #define NET_BUF_INFO(fmt, args...) do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_INFO) && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, INFO)) BLE_MESH_PRINT_I(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0) @@ -106,7 +106,7 @@ extern "C" { #define NET_BUF_ASSERT(cond) #endif -#if defined(CONFIG_BLE_MESH_NET_BUF_SIMPLE_LOG) && (!CONFIG_BLE_MESH_NO_LOG) +#if (CONFIG_BLE_MESH_NET_BUF_SIMPLE_LOG && !CONFIG_BLE_MESH_NO_LOG) #define NET_BUF_SIMPLE_ERR(fmt, args...) do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_ERROR) && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, ERROR)) BLE_MESH_PRINT_E(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0) #define NET_BUF_SIMPLE_WARN(fmt, args...) do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_WARN) && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, WARN)) BLE_MESH_PRINT_W(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0) #define NET_BUF_SIMPLE_INFO(fmt, args...) do {if ((BLE_MESH_NET_BUF_LOG_LEVEL >= BLE_MESH_LOG_LEVEL_INFO) && BLE_MESH_LOG_LEVEL_CHECK(BLE_MESH_NET_BUF, INFO)) BLE_MESH_PRINT_I(BLE_MESH_TRACE_TAG, fmt, ## args);} while(0) diff --git a/components/bt/esp_ble_mesh/common/include/mesh/utils.h b/components/bt/esp_ble_mesh/common/include/mesh/utils.h index f815e5278158..e5572afc3e06 100644 --- a/components/bt/esp_ble_mesh/common/include/mesh/utils.h +++ b/components/bt/esp_ble_mesh/common/include/mesh/utils.h @@ -26,13 +26,13 @@ extern "C" { * Those are available for 32 bits architectures: */ #ifndef POINTER_TO_UINT -#define POINTER_TO_UINT(x) ((uint32_t) (x)) +#define POINTER_TO_UINT(x) ((uint32_t) (x)) #endif #ifndef UINT_TO_POINTER #define UINT_TO_POINTER(x) ((void *) (x)) #endif #ifndef POINTER_TO_INT -#define POINTER_TO_INT(x) ((int32_t) (x)) +#define POINTER_TO_INT(x) ((int32_t) (x)) #endif #ifndef INT_TO_POINTER #define INT_TO_POINTER(x) ((void *) (x)) @@ -108,7 +108,7 @@ extern "C" { * evaluation version. */ #ifndef MAX -#define MAX(a, b) (((a) > (b)) ? (a) : (b)) +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) #endif /** @brief Return smaller value of two provided expressions. @@ -117,15 +117,15 @@ extern "C" { * evaluation version. */ #ifndef MIN -#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) #endif #ifndef BIT -#define BIT(n) (1UL << (n)) +#define BIT(n) (1UL << (n)) #endif #ifndef BIT_MASK -#define BIT_MASK(n) (BIT(n) - 1) +#define BIT_MASK(n) (BIT(n) - 1) #endif /** diff --git a/components/bt/esp_ble_mesh/common/mutex.c b/components/bt/esp_ble_mesh/common/mutex.c index 18015a99d5ad..6c3b2bf6f72a 100644 --- a/components/bt/esp_ble_mesh/common/mutex.c +++ b/components/bt/esp_ble_mesh/common/mutex.c @@ -74,30 +74,67 @@ void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex) } } -static inline void bt_mesh_alarm_mutex_new(void) +void bt_mesh_r_mutex_create(bt_mesh_mutex_t *mutex) { - if (!alarm_lock.mutex) { - bt_mesh_mutex_create(&alarm_lock); + if (!mutex) { + BT_ERR("Create, invalid recursive mutex"); + return; } + +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL + mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT + mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#endif + __ASSERT(mutex->buffer, "Failed to create recursive mutex buffer"); + mutex->mutex = xSemaphoreCreateRecursiveMutexStatic(mutex->buffer); + __ASSERT(mutex->mutex, "Failed to create static recursive mutex"); +#else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ + mutex->mutex = xSemaphoreCreateRecursiveMutex(); + __ASSERT(mutex->mutex, "Failed to create recursive mutex"); +#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ } -void bt_mesh_alarm_lock(void) +void bt_mesh_r_mutex_free(bt_mesh_mutex_t *mutex) { - bt_mesh_mutex_lock(&alarm_lock); + bt_mesh_mutex_free(mutex); } -void bt_mesh_alarm_unlock(void) +void bt_mesh_r_mutex_lock(bt_mesh_mutex_t *mutex) { - bt_mesh_mutex_unlock(&alarm_lock); + if (!mutex) { + BT_ERR("Lock, invalid recursive mutex"); + return; + } + + if (mutex->mutex) { + xSemaphoreTakeRecursive(mutex->mutex, portMAX_DELAY); + } } -static inline void bt_mesh_list_mutex_new(void) +void bt_mesh_r_mutex_unlock(bt_mesh_mutex_t *mutex) { - if (!list_lock.mutex) { - bt_mesh_mutex_create(&list_lock); + if (!mutex) { + BT_ERR("Unlock, invalid recursive mutex"); + return; + } + + if (mutex->mutex) { + xSemaphoreGiveRecursive(mutex->mutex); } } +void bt_mesh_alarm_lock(void) +{ + bt_mesh_mutex_lock(&alarm_lock); +} + +void bt_mesh_alarm_unlock(void) +{ + bt_mesh_mutex_unlock(&alarm_lock); +} + void bt_mesh_list_lock(void) { bt_mesh_mutex_lock(&list_lock); @@ -108,13 +145,6 @@ void bt_mesh_list_unlock(void) bt_mesh_mutex_unlock(&list_lock); } -static inline void bt_mesh_buf_mutex_new(void) -{ - if (!buf_lock.mutex) { - bt_mesh_mutex_create(&buf_lock); - } -} - void bt_mesh_buf_lock(void) { bt_mesh_mutex_lock(&buf_lock); @@ -125,13 +155,6 @@ void bt_mesh_buf_unlock(void) bt_mesh_mutex_unlock(&buf_lock); } -static inline void bt_mesh_atomic_mutex_new(void) -{ - if (!atomic_lock.mutex) { - bt_mesh_mutex_create(&atomic_lock); - } -} - void bt_mesh_atomic_lock(void) { bt_mesh_mutex_lock(&atomic_lock); @@ -144,38 +167,18 @@ void bt_mesh_atomic_unlock(void) void bt_mesh_mutex_init(void) { - bt_mesh_alarm_mutex_new(); - bt_mesh_list_mutex_new(); - bt_mesh_buf_mutex_new(); - bt_mesh_atomic_mutex_new(); + bt_mesh_mutex_create(&alarm_lock); + bt_mesh_mutex_create(&list_lock); + bt_mesh_mutex_create(&buf_lock); + bt_mesh_mutex_create(&atomic_lock); } #if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_alarm_mutex_free(void) +void bt_mesh_mutex_deinit(void) { bt_mesh_mutex_free(&alarm_lock); -} - -static inline void bt_mesh_list_mutex_free(void) -{ bt_mesh_mutex_free(&list_lock); -} - -static inline void bt_mesh_buf_mutex_free(void) -{ bt_mesh_mutex_free(&buf_lock); -} - -static inline void bt_mesh_atomic_mutex_free(void) -{ bt_mesh_mutex_free(&atomic_lock); } - -void bt_mesh_mutex_deinit(void) -{ - bt_mesh_alarm_mutex_free(); - bt_mesh_list_mutex_free(); - bt_mesh_buf_mutex_free(); - bt_mesh_atomic_mutex_free(); -} #endif /* CONFIG_BLE_MESH_DEINIT */ diff --git a/components/bt/esp_ble_mesh/common/timer.c b/components/bt/esp_ble_mesh/common/timer.c index 540849e6d42c..74fe3b492e5b 100644 --- a/components/bt/esp_ble_mesh/common/timer.c +++ b/components/bt/esp_ble_mesh/common/timer.c @@ -12,12 +12,12 @@ #include "osi/alarm.h" #include "osi/hash_functions.h" +#include "mesh/config.h" #include "mesh/common.h" -#include "prov_pvnr.h" 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; +static const size_t BLE_MESH_ALARM_HASH_MAP_SIZE = 20 + CONFIG_BLE_MESH_PBA_SAME_TIME + + CONFIG_BLE_MESH_PBG_SAME_TIME; typedef struct alarm_t { /* timer id point to here */ @@ -45,7 +45,7 @@ uint32_t k_uptime_get_32(void) void bt_mesh_timer_init(void) { - bm_alarm_hash_map = hash_map_new(BLE_MESH_GENERAL_ALARM_HASH_MAP_SIZE, + bm_alarm_hash_map = hash_map_new(BLE_MESH_ALARM_HASH_MAP_SIZE, hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL); __ASSERT(bm_alarm_hash_map, "Failed to create hash map"); @@ -73,42 +73,53 @@ int k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler) k_work_init(&work->work, handler); 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("Alarm not created"); - bt_mesh_alarm_unlock(); - return -EIO; - } - if (!hash_map_set(bm_alarm_hash_map, work, (void *)alarm)) { - BT_ERR("Alarm not set"); - bt_mesh_alarm_unlock(); - return -EIO; - } - } alarm = hash_map_get(bm_alarm_hash_map, work); + if (alarm) { + BT_ERR("Init, alarm already exists"); + bt_mesh_alarm_unlock(); + return -EEXIST; + } + + alarm = osi_alarm_new("bt_mesh", (osi_alarm_callback_t)handler, (void *)&work->work, 0); if (alarm == NULL) { - BT_ERR("Init, alarm not found"); + BT_ERR("Init, alarm not created"); + bt_mesh_alarm_unlock(); + return -EIO; + } + + if (!hash_map_set(bm_alarm_hash_map, work, (void *)alarm)) { + BT_ERR("Init, alarm not set"); bt_mesh_alarm_unlock(); - return -ENODEV; + return -EIO; } // Just init the work timer only, don't start it. osi_alarm_cancel(alarm); + bt_mesh_alarm_unlock(); + return 0; } int k_delayed_work_submit(struct k_delayed_work *work, int32_t delay) { + osi_alarm_t *alarm = NULL; + if (!work || !bm_alarm_hash_map) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } + /* If delay is 0, call the corresponding timeout handler. */ + if (delay == 0) { + k_work_submit(&work->work); + return 0; + } + bt_mesh_alarm_lock(); - osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); + + alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { BT_WARN("Submit, alarm not found"); bt_mesh_alarm_unlock(); @@ -118,19 +129,30 @@ int k_delayed_work_submit(struct k_delayed_work *work, int32_t delay) // Cancel the alarm first, before start the alarm. osi_alarm_cancel(alarm); osi_alarm_set(alarm, delay); + bt_mesh_alarm_unlock(); + return 0; } int k_delayed_work_submit_periodic(struct k_delayed_work *work, int32_t period) { + osi_alarm_t *alarm = NULL; + if (!work || !bm_alarm_hash_map) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } + /* If period is 0, call the corresponding timeout handler. */ + if (period == 0) { + k_work_submit(&work->work); + return 0; + } + bt_mesh_alarm_lock(); - osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); + + alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { BT_WARN("Submit, alarm not found"); bt_mesh_alarm_unlock(); @@ -140,19 +162,24 @@ int k_delayed_work_submit_periodic(struct k_delayed_work *work, int32_t period) /* Cancel the alarm first before starting it. */ osi_alarm_cancel(alarm); osi_alarm_set_periodic(alarm, period); + bt_mesh_alarm_unlock(); + return 0; } int k_delayed_work_cancel(struct k_delayed_work *work) { + osi_alarm_t *alarm = NULL; + if (!work || !bm_alarm_hash_map) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } bt_mesh_alarm_lock(); - osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); + + alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { BT_WARN("Cancel, alarm not found"); bt_mesh_alarm_unlock(); @@ -161,19 +188,24 @@ int k_delayed_work_cancel(struct k_delayed_work *work) osi_alarm_cancel(alarm); alarm->deadline_us = 0; + bt_mesh_alarm_unlock(); + return 0; } int k_delayed_work_free(struct k_delayed_work *work) { + osi_alarm_t *alarm = NULL; + if (!work || !bm_alarm_hash_map) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } bt_mesh_alarm_lock(); - osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, work); + + alarm = hash_map_get(bm_alarm_hash_map, work); if (alarm == NULL) { BT_WARN("Free, alarm not found"); bt_mesh_alarm_unlock(); @@ -182,12 +214,15 @@ int k_delayed_work_free(struct k_delayed_work *work) osi_alarm_cancel(alarm); hash_map_erase(bm_alarm_hash_map, work); + bt_mesh_alarm_unlock(); + return 0; } int32_t k_delayed_work_remaining_get(struct k_delayed_work *work) { + osi_alarm_t *alarm = NULL; int32_t time = 0; if (!work || !bm_alarm_hash_map) { @@ -196,7 +231,8 @@ int32_t k_delayed_work_remaining_get(struct k_delayed_work *work) } bt_mesh_alarm_lock(); - osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); + + alarm = hash_map_get(bm_alarm_hash_map, (void *)work); if (alarm == NULL) { BT_WARN("Get time, alarm not found"); bt_mesh_alarm_unlock(); @@ -204,6 +240,8 @@ int32_t k_delayed_work_remaining_get(struct k_delayed_work *work) } time = osi_alarm_get_remaining_ms(alarm); + bt_mesh_alarm_unlock(); + return time; } diff --git a/components/bt/esp_ble_mesh/core/access.c b/components/bt/esp_ble_mesh/core/access.c index 446cb13e8a53..51bf870bda51 100644 --- a/components/bt/esp_ble_mesh/core/access.c +++ b/components/bt/esp_ble_mesh/core/access.c @@ -12,6 +12,8 @@ #include "mesh.h" #include "adv.h" +#include "lpn.h" +#include "friend.h" #include "transport.h" #include "access.h" #include "foundation.h" @@ -20,11 +22,18 @@ #include "fast_prov.h" #include "pvnr_mgmt.h" +#include "mesh_v1.1/utils.h" + #define BLE_MESH_SDU_MAX_LEN 384 -static const struct bt_mesh_comp *dev_comp; +extern const struct bt_mesh_comp *comp_0; static uint16_t dev_primary_addr; +static int model_send(struct bt_mesh_model *model, + struct bt_mesh_net_tx *tx, bool implicit_bind, + struct net_buf_simple *msg, + const struct bt_mesh_send_cb *cb, void *cb_data); + void bt_mesh_model_foreach(void (*func)(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, bool vnd, bool primary, @@ -33,13 +42,13 @@ void bt_mesh_model_foreach(void (*func)(struct bt_mesh_model *mod, { int i, j; - if (dev_comp == NULL) { + if (comp_0 == NULL) { BT_ERR("Invalid device composition"); return; } - for (i = 0; i < dev_comp->elem_count; i++) { - struct bt_mesh_elem *elem = &dev_comp->elem[i]; + for (i = 0; i < comp_0->elem_count; i++) { + struct bt_mesh_elem *elem = &comp_0->elem[i]; for (j = 0; j < elem->model_count; j++) { struct bt_mesh_model *model = &elem->models[j]; @@ -88,9 +97,9 @@ int32_t bt_mesh_model_pub_period_get(struct bt_mesh_model *mod) if (mod->pub->fast_period) { return period >> mod->pub->period_div; - } else { - return period; } + + return period; } static int32_t next_period(struct bt_mesh_model *mod) @@ -169,43 +178,49 @@ static const struct bt_mesh_send_cb pub_sent_cb = { static int publish_retransmit(struct bt_mesh_model *mod) { struct bt_mesh_model_pub *pub = mod->pub; - if (!pub) { - BT_ERR("Model has no publication support"); - return -ENOTSUP; - } - struct bt_mesh_app_key *key = NULL; struct net_buf_simple *sdu = NULL; - struct bt_mesh_msg_ctx ctx = { - .addr = pub->addr, - .send_ttl = pub->ttl, - .model = mod, - .srv_send = (pub->dev_role == NODE ? true : false), - }; + struct bt_mesh_msg_ctx ctx = {0}; struct bt_mesh_net_tx tx = { - .ctx = &ctx, - .src = bt_mesh_model_elem(mod)->addr, + .ctx = &ctx, + .src = bt_mesh_model_elem(mod)->addr, .xmit = bt_mesh_net_transmit_get(), - .friend_cred = pub->cred, }; int err = 0; - key = bt_mesh_tx_appkey_get(pub->dev_role, pub->key); + if (!pub || !pub->msg) { + BT_ERR("Model has no publication support"); + return -ENOTSUP; + } + + key = bt_mesh_app_key_get(pub->key); if (!key) { - BT_ERR("AppKey 0x%03x not exists", pub->key); + BT_ERR("Publish, AppKey 0x%03x not found", pub->key); return -EADDRNOTAVAIL; } - tx.sub = bt_mesh_tx_netkey_get(pub->dev_role, key->net_idx); + tx.sub = bt_mesh_subnet_get(key->net_idx); if (!tx.sub) { - BT_ERR("Subnet 0x%04x not exists", key->net_idx); + BT_ERR("Publish, NetKey 0x%04x not found", key->net_idx); return -EADDRNOTAVAIL; } + ctx.addr = pub->addr; ctx.net_idx = key->net_idx; ctx.app_idx = key->app_idx; + ctx.send_ttl = pub->ttl; + ctx.send_cred = pub->cred ? BLE_MESH_FRIENDSHIP_CRED : BLE_MESH_FLOODING_CRED; + ctx.send_szmic = pub->send_szmic; + if (pub->send_rel) { + /* Tag with send-segmented */ + ctx.send_tag |= BLE_MESH_TAG_SEND_SEGMENTED; + } + +#if CONFIG_BLE_MESH_DF_SRV + bt_mesh_model_pub_use_directed(&tx, pub->directed_pub_policy); +#endif - sdu = bt_mesh_alloc_buf(pub->msg->len + BLE_MESH_MIC_SHORT); + sdu = bt_mesh_alloc_buf(pub->msg->len + BLE_MESH_MIC_LONG); if (!sdu) { BT_ERR("%s, Out of memory", __func__); return -ENOMEM; @@ -215,7 +230,7 @@ static int publish_retransmit(struct bt_mesh_model *mod) pub->count--; - err = bt_mesh_trans_send(&tx, sdu, &pub_sent_cb, mod); + err = model_send(mod, &tx, true, sdu, &pub_sent_cb, mod); bt_mesh_free_buf(sdu); return err; @@ -237,8 +252,6 @@ static void mod_publish(struct k_work *work) int32_t period_ms = 0; int err = 0; - BT_DBG("%s", __func__); - period_ms = bt_mesh_model_pub_period_get(pub->mod); BT_INFO("Publish period %u ms", period_ms); @@ -254,7 +267,6 @@ static void mod_publish(struct k_work *work) k_delayed_work_submit(&pub->timer, period_ms); } } - return; } @@ -282,24 +294,24 @@ static void mod_publish(struct k_work *work) struct bt_mesh_elem *bt_mesh_model_elem(struct bt_mesh_model *mod) { - return &dev_comp->elem[mod->elem_idx]; + return &comp_0->elem[mod->elem_idx]; } struct bt_mesh_model *bt_mesh_model_get(bool vnd, uint8_t elem_idx, uint8_t mod_idx) { struct bt_mesh_elem *elem = NULL; - if (!dev_comp) { - BT_ERR("dev_comp not initialized"); + if (!comp_0) { + BT_ERR("comp_0 not initialized"); return NULL; } - if (elem_idx >= dev_comp->elem_count) { + if (elem_idx >= comp_0->elem_count) { BT_ERR("Invalid element index %u", elem_idx); return NULL; } - elem = &dev_comp->elem[elem_idx]; + elem = &comp_0->elem[elem_idx]; if (vnd) { if (mod_idx >= elem->vnd_model_count) { @@ -346,7 +358,7 @@ static void mod_init(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, } mod->flags = 0; - mod->elem_idx = elem - dev_comp->elem; + mod->elem_idx = elem - comp_0->elem; if (vnd) { mod->model_idx = mod - elem->vnd_models; } else { @@ -371,7 +383,7 @@ int bt_mesh_comp_register(const struct bt_mesh_comp *comp) return -EINVAL; } - dev_comp = comp; + comp_0 = comp; bt_mesh_model_foreach(mod_init, &err); @@ -423,13 +435,13 @@ int bt_mesh_comp_deregister(void) { int err = 0; - if (dev_comp == NULL) { + if (comp_0 == NULL) { return -EINVAL; } bt_mesh_model_foreach(mod_deinit, &err); - dev_comp = NULL; + comp_0 = NULL; return err; } @@ -441,10 +453,10 @@ void bt_mesh_comp_provision(uint16_t addr) dev_primary_addr = addr; - BT_INFO("Primary address 0x%04x, element count %u", addr, dev_comp->elem_count); + BT_INFO("Primary address 0x%04x, element count %u", addr, comp_0->elem_count); - for (i = 0; i < dev_comp->elem_count; i++) { - struct bt_mesh_elem *elem = &dev_comp->elem[i]; + for (i = 0; i < comp_0->elem_count; i++) { + struct bt_mesh_elem *elem = &comp_0->elem[i]; elem->addr = addr++; @@ -455,8 +467,6 @@ void bt_mesh_comp_provision(uint16_t addr) void bt_mesh_comp_unprovision(void) { - BT_DBG("%s", __func__); - dev_primary_addr = BLE_MESH_ADDR_UNASSIGNED; } @@ -511,16 +521,16 @@ struct bt_mesh_elem *bt_mesh_elem_find(uint16_t addr) uint16_t index = 0U; if (BLE_MESH_ADDR_IS_UNICAST(addr)) { - index = (addr - dev_comp->elem[0].addr); - if (index < dev_comp->elem_count) { - return &dev_comp->elem[index]; - } else { - return NULL; + index = (addr - comp_0->elem[0].addr); + if (index < comp_0->elem_count) { + return &comp_0->elem[index]; } + + return NULL; } - for (index = 0; index < dev_comp->elem_count; index++) { - struct bt_mesh_elem *elem = &dev_comp->elem[index]; + for (index = 0; index < comp_0->elem_count; index++) { + struct bt_mesh_elem *elem = &comp_0->elem[index]; if (bt_mesh_elem_find_group(elem, addr)) { return elem; @@ -532,7 +542,7 @@ struct bt_mesh_elem *bt_mesh_elem_find(uint16_t addr) uint8_t bt_mesh_elem_count(void) { - return dev_comp->elem_count; + return comp_0->elem_count; } static bool model_has_key(struct bt_mesh_model *mod, uint16_t key) @@ -548,15 +558,20 @@ static bool model_has_key(struct bt_mesh_model *mod, uint16_t key) return false; } -static bool model_has_dst(struct bt_mesh_model *model, uint16_t dst) +static bool model_has_dst(struct bt_mesh_model *model, + struct bt_mesh_subnet *sub, + uint16_t dst) { if (BLE_MESH_ADDR_IS_UNICAST(dst)) { - return (dev_comp->elem[model->elem_idx].addr == dst); - } else if (BLE_MESH_ADDR_IS_GROUP(dst) || BLE_MESH_ADDR_IS_VIRTUAL(dst)) { + return (comp_0->elem[model->elem_idx].addr == dst); + } + + if (BLE_MESH_ADDR_IS_GROUP(dst) || BLE_MESH_ADDR_IS_VIRTUAL(dst)) { return !!bt_mesh_model_find_group(model, dst); } - return (model->elem_idx == 0 && bt_mesh_fixed_group_match(dst)); + return (model->elem_idx == 0 && (bt_mesh_fixed_group_match(dst) || + bt_mesh_fixed_direct_match(sub, dst))); } static const struct bt_mesh_model_op *find_op(struct bt_mesh_model *models, @@ -581,7 +596,7 @@ static const struct bt_mesh_model_op *find_op(struct bt_mesh_model *models, return NULL; } -static int get_opcode(struct net_buf_simple *buf, uint32_t *opcode) +static int get_opcode(struct net_buf_simple *buf, uint32_t *opcode, bool pull_buf) { switch (buf->data[0] >> 6) { case 0x00: @@ -591,34 +606,51 @@ static int get_opcode(struct net_buf_simple *buf, uint32_t *opcode) return -EINVAL; } - *opcode = net_buf_simple_pull_u8(buf); + *opcode = pull_buf ? net_buf_simple_pull_u8(buf) : buf->data[0]; return 0; + case 0x02: if (buf->len < 2) { BT_ERR("Too short payload for 2-octet OpCode"); return -EINVAL; } - *opcode = net_buf_simple_pull_be16(buf); + *opcode = pull_buf ? net_buf_simple_pull_be16(buf) : sys_get_be16(buf->data); return 0; + case 0x03: if (buf->len < 3) { BT_ERR("Too short payload for 3-octet OpCode"); return -EINVAL; } - *opcode = net_buf_simple_pull_u8(buf) << 16; - /* Using LE for the CID since the model layer is defined as - * little-endian in the mesh spec and using BT_MESH_MODEL_OP_3 - * will declare the opcode in this way. - */ - *opcode |= net_buf_simple_pull_le16(buf); + if (pull_buf) { + *opcode = net_buf_simple_pull_u8(buf) << 16; + /* Using LE for the CID since the model layer is defined as + * little-endian in the mesh spec and using BT_MESH_MODEL_OP_3 + * will declare the opcode in this way. + */ + *opcode |= net_buf_simple_pull_le16(buf); + } else { + *opcode = buf->data[0] << 16 | sys_get_le16(&buf->data[1]); + } return 0; } return -EINVAL; } +int bt_mesh_get_opcode(struct net_buf_simple *buf, + uint32_t *opcode, bool pull_buf) +{ + if (buf == NULL || buf->len == 0 || opcode == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + return get_opcode(buf, opcode, pull_buf); +} + bool bt_mesh_fixed_group_match(uint16_t addr) { /* Check for fixed group addresses */ @@ -636,6 +668,22 @@ bool bt_mesh_fixed_group_match(uint16_t addr) } } +bool bt_mesh_fixed_direct_match(struct bt_mesh_subnet *sub, uint16_t addr) +{ + /* A message sent to the all-directed-forwarding-nodes address + * shall be processed by the primary element of all nodes that + * have directed forwarding functionality enabled. + */ +#if CONFIG_BLE_MESH_DF_SRV + if (addr == BLE_MESH_ADDR_DIRECTS && sub && + sub->directed_forwarding == BLE_MESH_DIRECTED_FORWARDING_ENABLED) { + return true; + } +#endif + + return false; +} + void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) { struct bt_mesh_model *models = NULL, *model = NULL; @@ -648,15 +696,15 @@ void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) rx->ctx.addr, rx->ctx.recv_dst); BT_INFO("recv, len %u: %s", buf->len, bt_hex(buf->data, buf->len)); - if (get_opcode(buf, &opcode) < 0) { + if (get_opcode(buf, &opcode, true) < 0) { BT_WARN("Unable to decode OpCode"); return; } BT_DBG("OpCode 0x%08x", opcode); - for (i = 0; i < dev_comp->elem_count; i++) { - struct bt_mesh_elem *elem = &dev_comp->elem[i]; + for (i = 0; i < comp_0->elem_count; i++) { + struct bt_mesh_elem *elem = &comp_0->elem[i]; struct net_buf_simple_state state = {0}; /* SIG models cannot contain 3-byte (vendor) OpCodes, and @@ -681,7 +729,7 @@ void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) continue; } - if (!model_has_dst(model, rx->ctx.recv_dst)) { + if (!model_has_dst(model, rx->sub, rx->ctx.recv_dst)) { continue; } @@ -690,17 +738,28 @@ void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) continue; } - /* The following three operations are added by Espressif. - * 1. Update the "recv_op" with the opcode got from the buf; - * 2. Update the model pointer with the found model; - * 3. Update the "srv_send" to be true when received a message. - * This flag will be used when a server model sends a status - * message, and has no impact on the client messages. - * Most of these info will be used by the application layer. + /* The following operation is added by Espressif. + * Update the "recv_op" with the opcode got from the buf; */ rx->ctx.recv_op = opcode; - rx->ctx.model = model; - rx->ctx.srv_send = true; + + /* The message is transmitted by a model in response to a message that + * it has received. + * + * The TTL field shall be set to the value of the Default TTL state. + * + * The response message shall use master security credentials. However, + * the security credentials may be changed by a lower layer unless the + * received message uses the master security credentials. If the received + * message uses master security credentials, then the response message + * shall be tagged with the immutable-credentials tag, and the security + * credentials will not be changed by any lower layer. + */ + rx->ctx.send_ttl = BLE_MESH_TTL_DEFAULT; + if (rx->ctx.recv_cred == BLE_MESH_FLOODING_CRED) { + rx->ctx.send_tag |= BLE_MESH_TAG_IMMUTABLE_CRED; + rx->ctx.send_cred = BLE_MESH_FLOODING_CRED; + } /* The callback will likely parse the buffer, so store * the parsing state in case multiple models receive @@ -737,18 +796,19 @@ void bt_mesh_model_msg_init(struct net_buf_simple *msg, uint32_t opcode) } } -static bool ready_to_send(uint8_t role, uint16_t dst) +static bool ready_to_send(uint16_t dst) { - if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() && role == NODE) { + 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() && role == PROVISIONER) { - if (!bt_mesh_provisioner_check_msg_dst(dst)) { + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + if (bt_mesh_provisioner_check_msg_dst(dst) == false && + bt_mesh_elem_find(dst) == false) { BT_ERR("Failed to find DST 0x%04x", dst); return false; } return true; - } else if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && bt_mesh_is_provisioned() && role == FAST_PROV) { - return true; } return false; @@ -759,23 +819,23 @@ static int model_send(struct bt_mesh_model *model, struct net_buf_simple *msg, const struct bt_mesh_send_cb *cb, void *cb_data) { - uint8_t role = 0U; - - role = bt_mesh_get_device_role(model, tx->ctx->srv_send); - if (role == ROLE_NVAL) { - BT_ERR("Failed to get model role"); - return -EINVAL; - } + int err = 0; BT_INFO("send, app_idx 0x%04x src 0x%04x dst 0x%04x", tx->ctx->app_idx, tx->src, tx->ctx->addr); BT_INFO("send, len %u: %s", msg->len, bt_hex(msg->data, msg->len)); - if (!ready_to_send(role, tx->ctx->addr)) { + if (ready_to_send(tx->ctx->addr) == false) { BT_ERR("Not ready to send"); return -EINVAL; } + if (tx->ctx->send_ttl != BLE_MESH_TTL_DEFAULT && + tx->ctx->send_ttl > BLE_MESH_TTL_MAX) { + BT_ERR("Too big send TTL 0x%02x", tx->ctx->send_ttl); + return -EINVAL; + } + if (net_buf_simple_tailroom(msg) < BLE_MESH_MIC_SHORT) { BT_ERR("Not enough tailroom for TransMIC"); return -EINVAL; @@ -791,7 +851,43 @@ static int model_send(struct bt_mesh_model *model, return -EINVAL; } - return bt_mesh_trans_send(tx, msg, cb, cb_data); + if (bt_mesh_valid_security_cred(tx) == false) { + BT_ERR("Can not use security cred 0x%02x", tx->ctx->send_cred); + return -EIO; + } + + bt_mesh_choose_better_security_cred(tx); + + err = bt_mesh_trans_send(tx, msg, cb, cb_data); + +#if CONFIG_BLE_MESH_DF_SRV + bt_mesh_is_directed_path_needed(tx); +#endif + + return err; +} + +int bt_mesh_model_send_implicit(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx,bool implicit_bind, + struct net_buf_simple *msg, + const struct bt_mesh_send_cb *cb, void *cb_data) +{ + struct bt_mesh_subnet *sub = NULL; + + sub = bt_mesh_subnet_get(ctx->net_idx); + if (!sub) { + BT_ERR("Send, NetKey 0x%04x not found", ctx->net_idx); + return -EADDRNOTAVAIL; + } + + struct bt_mesh_net_tx tx = { + .sub = sub, + .ctx = ctx, + .src = bt_mesh_model_elem(model)->addr, + .xmit = bt_mesh_net_transmit_get(), + }; + + return model_send(model, &tx, implicit_bind, msg, cb, cb_data); } int bt_mesh_model_send(struct bt_mesh_model *model, @@ -800,28 +896,18 @@ 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; - uint8_t role = 0U; - role = bt_mesh_get_device_role(model, ctx->srv_send); - if (role == ROLE_NVAL) { - BT_ERR("Failed to get model role"); - return -EINVAL; - } - - sub = bt_mesh_tx_netkey_get(role, ctx->net_idx); + sub = bt_mesh_subnet_get(ctx->net_idx); if (!sub) { - BT_ERR("Invalid NetKeyIndex 0x%04x", ctx->net_idx); - return -EINVAL; + BT_ERR("Send, NetKey 0x%04x not found", ctx->net_idx); + return -EADDRNOTAVAIL; } - ctx->model = model; - struct bt_mesh_net_tx tx = { - .sub = sub, - .ctx = ctx, - .src = bt_mesh_model_elem(model)->addr, + .sub = sub, + .ctx = ctx, + .src = bt_mesh_model_elem(model)->addr, .xmit = bt_mesh_net_transmit_get(), - .friend_cred = 0, }; return model_send(model, &tx, false, msg, cb, cb_data); @@ -832,19 +918,14 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) struct bt_mesh_model_pub *pub = model->pub; struct bt_mesh_app_key *key = NULL; struct net_buf_simple *sdu = NULL; - struct bt_mesh_msg_ctx ctx = { - .model = model, - }; + struct bt_mesh_msg_ctx ctx = {0}; struct bt_mesh_net_tx tx = { - .sub = NULL, - .ctx = &ctx, - .src = bt_mesh_model_elem(model)->addr, + .ctx = &ctx, + .src = bt_mesh_model_elem(model)->addr, .xmit = bt_mesh_net_transmit_get(), }; int err = 0; - BT_DBG("%s", __func__); - if (!pub || !pub->msg) { BT_ERR("Model has no publication support"); return -ENOTSUP; @@ -855,12 +936,6 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) return -EADDRNOTAVAIL; } - key = bt_mesh_tx_appkey_get(pub->dev_role, pub->key); - if (!key) { - BT_ERR("Invalid AppKeyIndex 0x%03x", pub->key); - return -EADDRNOTAVAIL; - } - if (pub->msg->len + BLE_MESH_MIC_SHORT > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SDU_MAX_LEN)) { BT_ERR("Message does not fit maximum SDU size"); return -EMSGSIZE; @@ -871,27 +946,39 @@ int bt_mesh_model_publish(struct bt_mesh_model *model) k_delayed_work_cancel(&pub->timer); } - ctx.addr = pub->addr; - ctx.send_rel = pub->send_rel; - ctx.send_ttl = pub->ttl; - ctx.net_idx = key->net_idx; - ctx.app_idx = key->app_idx; - ctx.srv_send = pub->dev_role == NODE ? true : false; - - tx.friend_cred = pub->cred; + key = bt_mesh_app_key_get(pub->key); + if (!key) { + BT_ERR("Publish, AppKey 0x%03x not found", pub->key); + return -EADDRNOTAVAIL; + } - tx.sub = bt_mesh_tx_netkey_get(pub->dev_role, ctx.net_idx); + tx.sub = bt_mesh_subnet_get(ctx.net_idx); if (!tx.sub) { - BT_ERR("Invalid NetKeyIndex 0x%04x", ctx.net_idx); + BT_ERR("Publish, NetKey 0x%04x not found", ctx.net_idx); return -EADDRNOTAVAIL; } + ctx.addr = pub->addr; + ctx.net_idx = key->net_idx; + ctx.app_idx = key->app_idx; + ctx.send_ttl = pub->ttl; + ctx.send_cred = pub->cred ? BLE_MESH_FRIENDSHIP_CRED : BLE_MESH_FLOODING_CRED; + ctx.send_szmic = pub->send_szmic; + if (pub->send_rel) { + /* Tag with send-segmented */ + ctx.send_tag |= BLE_MESH_TAG_SEND_SEGMENTED; + } + +#if CONFIG_BLE_MESH_DF_SRV + bt_mesh_model_pub_use_directed(&tx, pub->directed_pub_policy); +#endif + pub->count = BLE_MESH_PUB_TRANSMIT_COUNT(pub->retransmit); 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 + BLE_MESH_MIC_SHORT); + sdu = bt_mesh_alloc_buf(pub->msg->len + BLE_MESH_MIC_LONG); if (!sdu) { BT_ERR("%s, Out of memory", __func__); return -ENOMEM; @@ -915,7 +1002,7 @@ struct bt_mesh_model *bt_mesh_model_find_vnd(struct bt_mesh_elem *elem, for (i = 0; i < elem->vnd_model_count; i++) { if (elem->vnd_models[i].vnd.company == company && - elem->vnd_models[i].vnd.id == id) { + elem->vnd_models[i].vnd.id == id) { return &elem->vnd_models[i]; } } @@ -938,56 +1025,26 @@ struct bt_mesh_model *bt_mesh_model_find(struct bt_mesh_elem *elem, uint16_t id) const struct bt_mesh_comp *bt_mesh_comp_get(void) { - return dev_comp; -} - -/* APIs used by messages encryption in upper transport layer & network layer */ -struct bt_mesh_subnet *bt_mesh_tx_netkey_get(uint8_t role, uint16_t net_idx) -{ - struct bt_mesh_subnet *sub = NULL; - - 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); - } - - return sub; + return comp_0; } -const uint8_t *bt_mesh_tx_devkey_get(uint8_t role, uint16_t dst) +const uint8_t *bt_mesh_dev_key_get(uint16_t dst) { const uint8_t *key = NULL; - 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) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { + if (!IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) { + key = bt_mesh.dev_key; + } else { + key = bt_mesh_fast_prov_dev_key_get(dst); + } + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { 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); } return key; } -struct bt_mesh_app_key *bt_mesh_tx_appkey_get(uint8_t role, uint16_t app_idx) -{ - struct bt_mesh_app_key *key = NULL; - - 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); - } - - return key; -} - -/* APIs used by messages decryption in network layer & upper transport layer */ size_t bt_mesh_rx_netkey_size(void) { size_t size = 0U; @@ -1048,6 +1105,9 @@ size_t bt_mesh_rx_devkey_size(void) #if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER if (bt_mesh_is_provisioned()) { size = 1; + if (bt_mesh_dev_key_ca_valid()) { + size += 1; + } } #endif @@ -1059,6 +1119,9 @@ size_t bt_mesh_rx_devkey_size(void) #if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER size = 1; + if (bt_mesh_dev_key_ca_valid()) { + size += 1; + } if (bt_mesh_is_provisioner_en()) { size += 1; } @@ -1073,7 +1136,11 @@ const uint8_t *bt_mesh_rx_devkey_get(size_t index, uint16_t src) #if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER if (bt_mesh_is_provisioned()) { - key = bt_mesh.dev_key; + if (index == 0) { + key = bt_mesh.dev_key; + } else if (index == 1) { + key = bt_mesh.dev_key_ca; + } } #endif @@ -1084,8 +1151,18 @@ const uint8_t *bt_mesh_rx_devkey_get(size_t index, uint16_t src) #endif #if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER - if (index < 1) { + if (index == 0) { key = bt_mesh.dev_key; + } else if (index == 1 && bt_mesh_dev_key_ca_valid()) { + /* If index == 1, there are two cases. + * 1. bt_mesh_dev_key_ca_valid() is true, it should be return bt_mesh.dev_key_ca. + * 2. bt_mesh_is_provisioner_en() is true, it should be return bt_mesh_provisioner_dev_key_get(src). + * + * If index == 2, that means bt_mesh_dev_key_ca_valid() and bt_mesh_is_provisioner_en() are true. + * So the previous round of function bt_mesh_rx_devkey_get(1, src) will return bt_mesh.dev_key_ca. + * Then this round of function bt_mesh_rx_devkey_get(2, src) will return bt_mesh_provisioner_dev_key_get(src). + */ + key = bt_mesh.dev_key_ca; } else { key = bt_mesh_provisioner_dev_key_get(src); } diff --git a/components/bt/esp_ble_mesh/core/access.h b/components/bt/esp_ble_mesh/core/access.h index a74e0563920d..672a9a2c7bf3 100644 --- a/components/bt/esp_ble_mesh/core/access.h +++ b/components/bt/esp_ble_mesh/core/access.h @@ -31,8 +31,13 @@ struct bt_mesh_elem *bt_mesh_elem_find(uint16_t addr); uint16_t *bt_mesh_model_find_group(struct bt_mesh_model *mod, uint16_t addr); +int bt_mesh_get_opcode(struct net_buf_simple *buf, + uint32_t *opcode, bool pull_buf); + bool bt_mesh_fixed_group_match(uint16_t addr); +bool bt_mesh_fixed_direct_match(struct bt_mesh_subnet *sub, uint16_t addr); + void bt_mesh_model_foreach(void (*func)(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, bool vnd, bool primary, @@ -55,11 +60,7 @@ 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(uint8_t role, uint16_t net_idx); - -const uint8_t *bt_mesh_tx_devkey_get(uint8_t role, uint16_t dst); - -struct bt_mesh_app_key *bt_mesh_tx_appkey_get(uint8_t role, uint16_t app_idx); +const uint8_t *bt_mesh_dev_key_get(uint16_t dst); size_t bt_mesh_rx_netkey_size(void); diff --git a/components/bt/esp_ble_mesh/core/adv.c b/components/bt/esp_ble_mesh/core/adv.c index 2a84e5a12225..b99d22f2a1d4 100644 --- a/components/bt/esp_ble_mesh/core/adv.c +++ b/components/bt/esp_ble_mesh/core/adv.c @@ -11,11 +11,11 @@ #include #include -#include "mesh/kernel.h" +#include "adv.h" #include "mesh.h" #include "mesh/hci.h" +#include "mesh/kernel.h" #include "mesh/common.h" -#include "adv.h" #include "beacon.h" #include "prov_node.h" #include "foundation.h" @@ -63,7 +63,7 @@ static struct bt_mesh_queue adv_queue; #define BLE_MESH_ADV_QUEUE_SIZE (CONFIG_BLE_MESH_ADV_BUF_COUNT + 1) #endif -#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) +#if CONFIG_BLE_MESH_RELAY_ADV_BUF NET_BUF_POOL_DEFINE(relay_adv_buf_pool, CONFIG_BLE_MESH_RELAY_ADV_BUF_COUNT, BLE_MESH_ADV_DATA_SIZE, BLE_MESH_ADV_USER_DATA_SIZE, NULL); @@ -79,7 +79,7 @@ static QueueSetHandle_t mesh_queue_set; #define BLE_MESH_MAX_TIME_INTERVAL 0xFFFFFFFF static bool ignore_relay_packet(uint32_t timestamp); -#endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ +#endif /* CONFIG_BLE_MESH_RELAY_ADV_BUF */ #if CONFIG_BLE_MESH_SUPPORT_BLE_ADV /* length + advertising data + length + scan response data */ @@ -175,9 +175,21 @@ 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, 4U, BLE_MESH_BUF_REF_SMALL); - - err = bt_le_adv_start(¶m, &ad, 1, NULL, 0); +#if CONFIG_BLE_MESH_PROXY_SOLIC_PDU_TX + if (BLE_MESH_ADV(buf)->type == BLE_MESH_ADV_PROXY_SOLIC) { + bt_mesh_adv_buf_ref_debug(__func__, buf, 3U, BLE_MESH_BUF_REF_SMALL); + struct bt_mesh_adv_data solic_ad[3] = { + BLE_MESH_ADV_DATA_BYTES(BLE_MESH_DATA_FLAGS, (BLE_MESH_AD_GENERAL | BLE_MESH_AD_NO_BREDR)), + BLE_MESH_ADV_DATA_BYTES(BLE_MESH_DATA_UUID16_ALL, 0x29, 0x18), + BLE_MESH_ADV_DATA(BLE_MESH_DATA_SVC_DATA16, buf->data, buf->len), + }; + err = bt_le_adv_start(¶m, solic_ad, 3, NULL, 0); + } else +#endif + { + bt_mesh_adv_buf_ref_debug(__func__, buf, 4U, BLE_MESH_BUF_REF_SMALL); + err = bt_le_adv_start(¶m, &ad, 1, NULL, 0); + } #if CONFIG_BLE_MESH_SUPPORT_BLE_ADV } else { struct bt_mesh_ble_adv_data data = {0}; @@ -238,7 +250,7 @@ static inline TickType_t K_WAIT(int32_t val) static void adv_thread(void *p) { -#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) +#if CONFIG_BLE_MESH_RELAY_ADV_BUF QueueSetMemberHandle_t handle = NULL; #endif bt_mesh_msg_t msg = {0}; @@ -250,9 +262,9 @@ static void adv_thread(void *p) while (1) { *buf = NULL; -#if !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) +#if !CONFIG_BLE_MESH_RELAY_ADV_BUF #if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_SERVER + CONFIG_BLE_MESH_GATT_PROXY_SERVER xQueueReceive(adv_queue.handle, &msg, K_NO_WAIT); while (!(*buf)) { int32_t timeout = 0; @@ -266,9 +278,9 @@ static void adv_thread(void *p) #else xQueueReceive(adv_queue.handle, &msg, portMAX_DELAY); #endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ -#else /* !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ +#else /* !CONFIG_BLE_MESH_RELAY_ADV_BUF */ #if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_SERVER + CONFIG_BLE_MESH_GATT_PROXY_SERVER handle = xQueueSelectFromSet(mesh_queue_set, K_NO_WAIT); if (handle) { if (uxQueueMessagesWaiting(adv_queue.handle)) { @@ -304,7 +316,7 @@ static void adv_thread(void *p) } } #endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ -#endif /* !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ +#endif /* !CONFIG_BLE_MESH_RELAY_ADV_BUF */ if (*buf == NULL) { continue; @@ -313,11 +325,11 @@ static void adv_thread(void *p) /* busy == 0 means this was canceled */ if (BLE_MESH_ADV(*buf)->busy) { BLE_MESH_ADV(*buf)->busy = 0U; -#if !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) +#if !CONFIG_BLE_MESH_RELAY_ADV_BUF if (adv_send(*buf)) { BT_WARN("Failed to send adv packet"); } -#else /* !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ +#else /* !CONFIG_BLE_MESH_RELAY_ADV_BUF */ if (msg.relay && ignore_relay_packet(msg.timestamp)) { /* If the interval between "current time - msg.timestamp" is bigger than * BLE_MESH_RELAY_TIME_INTERVAL, this relay packet will not be sent. @@ -329,7 +341,7 @@ static void adv_thread(void *p) BT_WARN("Failed to send adv packet"); } } -#endif +#endif /* !CONFIG_BLE_MESH_RELAY_ADV_BUF */ } else { bt_mesh_adv_buf_ref_debug(__func__, *buf, 1U, BLE_MESH_BUF_REF_EQUAL); net_buf_unref(*buf); @@ -343,7 +355,7 @@ static void adv_thread(void *p) struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool, bt_mesh_adv_alloc_t get_id, enum bt_mesh_adv_type type, - uint8_t xmit, int32_t timeout) + int32_t timeout) { struct bt_mesh_adv *adv = NULL; struct net_buf *buf = NULL; @@ -367,7 +379,6 @@ struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool, (void)memset(adv, 0, sizeof(*adv)); adv->type = type; - adv->xmit = xmit; return buf; } @@ -390,11 +401,10 @@ void bt_mesh_unref_buf_from_pool(struct net_buf_pool *pool) } } -struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type, uint8_t xmit, - int32_t timeout) +struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type, int32_t timeout) { - return bt_mesh_adv_create_from_pool(&adv_buf_pool, adv_alloc, type, - xmit, timeout); + return bt_mesh_adv_create_from_pool(&adv_buf_pool, adv_alloc, + type, timeout); } void bt_mesh_adv_buf_ref_debug(const char *func, struct net_buf *buf, @@ -433,14 +443,10 @@ static void bt_mesh_unref_buf(bt_mesh_msg_t *msg) } net_buf_unref(buf); } - - return; } static void bt_mesh_task_post(bt_mesh_msg_t *msg, uint32_t timeout, bool front) { - BT_DBG("%s", __func__); - if (adv_queue.handle == NULL) { BT_ERR("Invalid adv queue"); return; @@ -459,7 +465,8 @@ static void bt_mesh_task_post(bt_mesh_msg_t *msg, uint32_t timeout, bool front) } } -void bt_mesh_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, +void bt_mesh_adv_send(struct net_buf *buf, uint8_t xmit, + const struct bt_mesh_send_cb *cb, void *cb_data) { bt_mesh_msg_t msg = { @@ -472,6 +479,7 @@ void bt_mesh_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, BLE_MESH_ADV(buf)->cb = cb; BLE_MESH_ADV(buf)->cb_data = cb_data; BLE_MESH_ADV(buf)->busy = 1U; + BLE_MESH_ADV(buf)->xmit = xmit; bt_mesh_adv_buf_ref_debug(__func__, buf, 3U, BLE_MESH_BUF_REF_SMALL); @@ -486,12 +494,10 @@ void bt_mesh_adv_update(void) .arg = NULL, }; - BT_DBG("%s", __func__); - bt_mesh_task_post(&msg, K_NO_WAIT, false); } -#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) +#if CONFIG_BLE_MESH_RELAY_ADV_BUF static bool ignore_relay_packet(uint32_t timestamp) { uint32_t now = k_uptime_get_32(); @@ -511,11 +517,10 @@ static struct bt_mesh_adv *relay_adv_alloc(int id) return &relay_adv_pool[id]; } -struct net_buf *bt_mesh_relay_adv_create(enum bt_mesh_adv_type type, uint8_t xmit, - int32_t timeout) +struct net_buf *bt_mesh_relay_adv_create(enum bt_mesh_adv_type type, int32_t timeout) { - return bt_mesh_adv_create_from_pool(&relay_adv_buf_pool, relay_adv_alloc, type, - xmit, timeout); + return bt_mesh_adv_create_from_pool(&relay_adv_buf_pool, relay_adv_alloc, + type, timeout); } static void ble_mesh_relay_task_post(bt_mesh_msg_t *msg, uint32_t timeout) @@ -523,8 +528,6 @@ static void ble_mesh_relay_task_post(bt_mesh_msg_t *msg, uint32_t timeout) QueueSetMemberHandle_t handle = NULL; bt_mesh_msg_t old_msg = {0}; - BT_DBG("%s", __func__); - if (relay_queue.handle == NULL) { BT_ERR("Invalid relay queue"); return; @@ -534,8 +537,7 @@ static void ble_mesh_relay_task_post(bt_mesh_msg_t *msg, uint32_t timeout) return; } - /** - * If failed to send packet to the relay queue(queue is full), we will + /* If failed to send packet to the relay queue(queue is full), we will * remove the oldest packet in the queue and put the new one into it. */ handle = xQueueSelectFromSet(mesh_queue_set, K_NO_WAIT); @@ -561,8 +563,10 @@ static void ble_mesh_relay_task_post(bt_mesh_msg_t *msg, uint32_t timeout) } } -void bt_mesh_relay_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, - void *cb_data, uint16_t src, uint16_t dst) +void bt_mesh_relay_adv_send(struct net_buf *buf, uint8_t xmit, + uint16_t src, uint16_t dst, + const struct bt_mesh_send_cb *cb, + void *cb_data) { bt_mesh_msg_t msg = { .relay = true, @@ -574,6 +578,7 @@ void bt_mesh_relay_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *c BLE_MESH_ADV(buf)->cb = cb; BLE_MESH_ADV(buf)->cb_data = cb_data; BLE_MESH_ADV(buf)->busy = 1U; + BLE_MESH_ADV(buf)->xmit = xmit; msg.arg = (void *)net_buf_ref(buf); msg.src = src; @@ -587,7 +592,7 @@ uint16_t bt_mesh_get_stored_relay_count(void) { return (uint16_t)uxQueueMessagesWaiting(relay_queue.handle); } -#endif /* #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ +#endif /* #if CONFIG_BLE_MESH_RELAY_ADV_BUF */ void bt_mesh_adv_init(void) { @@ -611,7 +616,7 @@ void bt_mesh_adv_init(void) __ASSERT(adv_queue.handle, "Failed to create static queue"); #endif /* !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ -#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) +#if CONFIG_BLE_MESH_RELAY_ADV_BUF #if !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC relay_queue.handle = xQueueCreate(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t)); __ASSERT(relay_queue.handle, "Failed to create relay queue"); @@ -636,7 +641,7 @@ void bt_mesh_adv_init(void) __ASSERT(mesh_queue_set, "Failed to create queue set"); xQueueAddToSet(adv_queue.handle, mesh_queue_set); xQueueAddToSet(relay_queue.handle, mesh_queue_set); -#endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ +#endif /* CONFIG_BLE_MESH_RELAY_ADV_BUF */ #if (CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && \ (CONFIG_SPIRAM_CACHE_WORKAROUND || !CONFIG_IDF_TARGET_ESP32) && \ @@ -674,7 +679,7 @@ void bt_mesh_adv_deinit(void) adv_task.task = NULL; #endif -#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) +#if CONFIG_BLE_MESH_RELAY_ADV_BUF xQueueRemoveFromSet(adv_queue.handle, mesh_queue_set); xQueueRemoveFromSet(relay_queue.handle, mesh_queue_set); @@ -685,14 +690,14 @@ void bt_mesh_adv_deinit(void) relay_queue.buffer = NULL; heap_caps_free(relay_queue.storage); relay_queue.storage = NULL; -#endif +#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ bt_mesh_unref_buf_from_pool(&relay_adv_buf_pool); memset(relay_adv_pool, 0, sizeof(relay_adv_pool)); vQueueDelete(mesh_queue_set); mesh_queue_set = NULL; -#endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ +#endif /* CONFIG_BLE_MESH_RELAY_ADV_BUF */ vQueueDelete(adv_queue.handle); adv_queue.handle = NULL; @@ -701,14 +706,14 @@ void bt_mesh_adv_deinit(void) adv_queue.buffer = NULL; heap_caps_free(adv_queue.storage); adv_queue.storage = NULL; -#endif +#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ bt_mesh_unref_buf_from_pool(&adv_buf_pool); memset(adv_pool, 0, sizeof(adv_pool)); #if CONFIG_BLE_MESH_SUPPORT_BLE_ADV bt_mesh_ble_adv_deinit(); -#endif +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ } #endif /* CONFIG_BLE_MESH_DEINIT */ @@ -718,10 +723,10 @@ static struct bt_mesh_adv *ble_adv_alloc(int id) return &ble_adv_pool[id]; } -static struct net_buf *bt_mesh_ble_adv_create(enum bt_mesh_adv_type type, uint8_t xmit, int32_t timeout) +static struct net_buf *bt_mesh_ble_adv_create(enum bt_mesh_adv_type type, int32_t timeout) { - return bt_mesh_adv_create_from_pool(&ble_adv_buf_pool, ble_adv_alloc, type, - xmit, timeout); + return bt_mesh_adv_create_from_pool(&ble_adv_buf_pool, ble_adv_alloc, + type, timeout); } static void bt_mesh_ble_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, @@ -789,11 +794,7 @@ static void ble_adv_send_end(int err, void *cb_data) } if (tx->param.count) { - if (tx->param.period) { - k_delayed_work_submit(&tx->resend, tx->param.period); - } else { - k_work_submit(&tx->resend.work); - } + k_delayed_work_submit(&tx->resend, tx->param.period); } else { ble_adv_tx_reset(tx, true); } @@ -806,7 +807,9 @@ static struct bt_mesh_send_cb ble_adv_send_cb = { static void ble_adv_resend(struct k_work *work) { - struct ble_adv_tx *tx = CONTAINER_OF(work, struct ble_adv_tx, resend.work); + struct ble_adv_tx *tx = CONTAINER_OF(work, + struct ble_adv_tx, + resend.work); bool front = false; if (tx->buf == NULL) { @@ -880,7 +883,7 @@ int bt_mesh_start_ble_advertising(const struct bt_mesh_ble_adv_param *param, return -EINVAL; } - buf = bt_mesh_ble_adv_create(BLE_MESH_ADV_BLE, 0U, K_NO_WAIT); + buf = bt_mesh_ble_adv_create(BLE_MESH_ADV_BLE, K_NO_WAIT); if (!buf) { BT_ERR("No empty ble adv buffer"); return -ENOBUFS; diff --git a/components/bt/esp_ble_mesh/core/adv.h b/components/bt/esp_ble_mesh/core/adv.h index 16c3f15918db..9367f962fd9a 100644 --- a/components/bt/esp_ble_mesh/core/adv.h +++ b/components/bt/esp_ble_mesh/core/adv.h @@ -18,16 +18,16 @@ extern "C" { #endif /* Maximum advertising data payload for a single data type */ -#define BLE_MESH_ADV_DATA_SIZE 29 +#define BLE_MESH_ADV_DATA_SIZE 29 /* The user data is a pointer (4 bytes) to struct bt_mesh_adv */ -#define BLE_MESH_ADV_USER_DATA_SIZE 4 +#define BLE_MESH_ADV_USER_DATA_SIZE 4 -#define BLE_MESH_ADV(buf) (*(struct bt_mesh_adv **)net_buf_user_data(buf)) +#define BLE_MESH_ADV(buf) (*(struct bt_mesh_adv **)net_buf_user_data(buf)) typedef struct bt_mesh_msg { - bool relay; /* Flag indicates if the packet is a relayed one */ - void *arg; /* Pointer to the struct net_buf */ + bool relay; /* Flag indicates if the packet is a relayed one */ + void *arg; /* Pointer to the struct net_buf */ uint16_t src; /* Source address for relay packets */ uint16_t dst; /* Destination address for relay packets */ uint32_t timestamp; /* Timestamp recorded when the relay packet is posted to queue */ @@ -39,6 +39,7 @@ enum bt_mesh_adv_type { BLE_MESH_ADV_BEACON, BLE_MESH_ADV_URI, BLE_MESH_ADV_BLE, + BLE_MESH_ADV_PROXY_SOLIC, }; struct bt_mesh_adv { @@ -52,9 +53,7 @@ struct bt_mesh_adv { typedef struct bt_mesh_adv *(*bt_mesh_adv_alloc_t)(int id); -/* xmit_count: Number of retransmissions, i.e. 0 == 1 transmission */ -struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type, uint8_t xmit, - int32_t timeout); +struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type, int32_t timeout); typedef enum { BLE_MESH_BUF_REF_EQUAL, @@ -68,18 +67,20 @@ void bt_mesh_adv_buf_ref_debug(const char *func, struct net_buf *buf, struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool, bt_mesh_adv_alloc_t get_id, enum bt_mesh_adv_type type, - uint8_t xmit, int32_t timeout); + int32_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 bt_mesh_adv_send(struct net_buf *buf, uint8_t xmit, + const struct bt_mesh_send_cb *cb, void *cb_data); -struct net_buf *bt_mesh_relay_adv_create(enum bt_mesh_adv_type type, uint8_t xmit, - int32_t timeout); +struct net_buf *bt_mesh_relay_adv_create(enum bt_mesh_adv_type type, int32_t timeout); -void bt_mesh_relay_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, - void *cb_data, uint16_t src, uint16_t dst); +void bt_mesh_relay_adv_send(struct net_buf *buf, uint8_t xmit, + uint16_t src, uint16_t dst, + const struct bt_mesh_send_cb *cb, + void *cb_data); uint16_t bt_mesh_get_stored_relay_count(void); diff --git a/components/bt/esp_ble_mesh/core/beacon.c b/components/bt/esp_ble_mesh/core/beacon.c index ac0a55930857..488a3c5f6d08 100644 --- a/components/bt/esp_ble_mesh/core/beacon.c +++ b/components/bt/esp_ble_mesh/core/beacon.c @@ -11,41 +11,45 @@ #include #include "adv.h" +#include "scan.h" #include "mesh.h" -#include "prov_node.h" #include "crypto.h" #include "beacon.h" #include "access.h" #include "foundation.h" #include "proxy_client.h" #include "mesh/main.h" +#include "prov_common.h" +#include "prov_node.h" #include "prov_pvnr.h" #include "pvnr_mgmt.h" +#include "mesh/common.h" + +#include "mesh_v1.1/utils.h" #if defined(CONFIG_BLE_MESH_UNPROVISIONED_BEACON_INTERVAL) -#define UNPROVISIONED_INTERVAL K_SECONDS(CONFIG_BLE_MESH_UNPROVISIONED_BEACON_INTERVAL) +#define UNPROV_BEACON_INTERVAL K_SECONDS(CONFIG_BLE_MESH_UNPROVISIONED_BEACON_INTERVAL) #else -#define UNPROVISIONED_INTERVAL K_SECONDS(5) +#define UNPROV_BEACON_INTERVAL K_SECONDS(5) #endif -#define PROVISIONED_INTERVAL K_SECONDS(10) -#define BEACON_TYPE_UNPROVISIONED 0x00 -#define BEACON_TYPE_SECURE 0x01 +#define SECURE_BEACON_INTERVAL K_SECONDS(10) /* 3 transmissions, 20ms interval */ #define UNPROV_XMIT BLE_MESH_TRANSMIT(2, 20) /* 1 transmission, 20ms interval */ -#define PROV_XMIT BLE_MESH_TRANSMIT(0, 20) +#define SNB_XMIT BLE_MESH_TRANSMIT(0, 20) -#define SNB_NET_IDX_SET(_val) ((void *)((uint32_t)(_val))) -#define SNB_NET_IDX_GET(_ptr) ((uint32_t)(_ptr)) - -static struct k_delayed_work beacon_timer; +/* For a device, using the snb_timer when sending Unprovisioned Device Beacon; + * For a node, using the snb_timer when sending Secure Network Beacon. + */ +static struct k_delayed_work snb_timer; -static struct bt_mesh_subnet *cache_check(uint8_t data[21]) +struct bt_mesh_subnet *cache_check(uint8_t data[21], bool private_beacon) { size_t subnet_size = 0U; + uint8_t *cache = NULL; int i = 0; subnet_size = bt_mesh_rx_netkey_size(); @@ -57,7 +61,13 @@ static struct bt_mesh_subnet *cache_check(uint8_t data[21]) continue; } - if (!memcmp(sub->beacon_cache, data, 21)) { +#if CONFIG_BLE_MESH_PRIVATE_BEACON + cache = private_beacon ? sub->mpb_cache : sub->snb_cache; +#else + cache = sub->snb_cache; +#endif + + if (!memcmp(cache, data, 21)) { return sub; } } @@ -65,19 +75,26 @@ static struct bt_mesh_subnet *cache_check(uint8_t data[21]) return NULL; } -static void cache_add(uint8_t data[21], struct bt_mesh_subnet *sub) +void cache_add(uint8_t data[21], struct bt_mesh_subnet *sub, bool private_beacon) { - memcpy(sub->beacon_cache, data, 21); +#if CONFIG_BLE_MESH_PRIVATE_BEACON + if (private_beacon) { + memcpy(sub->mpb_cache, data, 21); + } else +#endif + { + memcpy(sub->snb_cache, data, 21); + } } -static void beacon_complete(int err, void *user_data) +static void secure_beacon_complete(int err, void *user_data) { struct bt_mesh_subnet *sub = NULL; uint16_t net_idx = BLE_MESH_KEY_UNUSED; BT_DBG("err %d", err); - net_idx = (uint16_t)SNB_NET_IDX_GET(user_data); + net_idx = (uint16_t)NET_IDX_GET(user_data); /* For node, directly updating the "beacon_sent" timestamp is fine, * since the subnet is pre-allocated. @@ -86,20 +103,14 @@ static void beacon_complete(int err, void *user_data) * a chance that the subnet is removed just before the completion of * sending the Secure Network Beacon. */ - 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 = bt_mesh_provisioner_subnet_get(net_idx); - } - + sub = bt_mesh_subnet_get(net_idx); if (sub) { - sub->beacon_sent = k_uptime_get_32(); + sub->snb_sent = k_uptime_get_32(); } } -void bt_mesh_beacon_create(struct bt_mesh_subnet *sub, - struct net_buf_simple *buf) +void bt_mesh_secure_beacon_create(struct bt_mesh_subnet *sub, + struct net_buf_simple *buf) { uint8_t flags = bt_mesh_net_flags(sub); struct bt_mesh_subnet_keys *keys = NULL; @@ -122,26 +133,21 @@ void bt_mesh_beacon_create(struct bt_mesh_subnet *sub, net_buf_simple_add_mem(buf, sub->auth, 8); - BT_INFO("net_idx 0x%03x iv_index 0x%08x flags 0x%02x", + BT_DBG("SNB: net_idx 0x%03x iv_index 0x%08x flags 0x%02x", sub->net_idx, bt_mesh.iv_index, flags); - BT_DBG("NetID %s Auth %s", bt_hex(keys->net_id, 8), + BT_DBG("SNB: NetID %s Auth %s", bt_hex(keys->net_id, 8), bt_hex(sub->auth, 8)); } -/* If the interval has passed or is within 5 seconds from now send a beacon */ -#define BEACON_THRESHOLD(sub) (K_SECONDS(10 * ((sub)->beacons_last + 1)) - K_SECONDS(5)) - static int secure_beacon_send(void) { static const struct bt_mesh_send_cb send_cb = { - .end = beacon_complete, + .end = secure_beacon_complete, }; uint32_t now = k_uptime_get_32(); size_t subnet_size = 0U; int i = 0; - BT_DBG("%s", __func__); - subnet_size = bt_mesh_rx_netkey_size(); for (i = 0; i < subnet_size; i++) { @@ -153,128 +159,137 @@ static int secure_beacon_send(void) continue; } - time_diff = now - sub->beacon_sent; + time_diff = now - sub->snb_sent; if (time_diff < K_SECONDS(600) && - time_diff < BEACON_THRESHOLD(sub)) { + time_diff < BEACON_THRESHOLD(sub->snb_last)) { continue; } - /** - * If a node enables the Proxy Client functionality, and it + /* If a node enables the Proxy Client functionality, and it * succeeds to send Secure Network Beacon with GATT bearer, * here we will continue to send Secure Network Beacon of * other subnets. */ -#if defined(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) - if (bt_mesh_proxy_client_beacon_send(sub)) { +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + if (bt_mesh_proxy_client_beacon_send(sub, false)) { continue; } #endif - buf = bt_mesh_adv_create(BLE_MESH_ADV_BEACON, PROV_XMIT, - K_NO_WAIT); + buf = bt_mesh_adv_create(BLE_MESH_ADV_BEACON, K_NO_WAIT); if (!buf) { - BT_ERR("Out of beacon buffer"); + BT_ERR("Out of secure beacon buffer"); return -ENOBUFS; } - bt_mesh_beacon_create(sub, &buf->b); + bt_mesh_secure_beacon_create(sub, &buf->b); /* Care should be taken here. Previously the user_data is the * pointer of a subnet. When the device is a Provisioner, its * subnet is created dynamically. If the corresponding subnet * is removed right after the Secure Network Beacon is sent, - * update its "beacon_sent" timestamp in beacon_complete() will - * cause exception. + * update its "snb_sent" timestamp in secure_beacon_complete() + * will cause exception. * Here we use the "net_idx" of the subnet instead. And in the - * beacon_complete(), we will try to get the subnet before - * updating its "beacon_sent" timestamp. + * secure_beacon_complete(), we will try to get the subnet before + * updating its "snb_sent" timestamp. */ - bt_mesh_adv_send(buf, &send_cb, SNB_NET_IDX_SET(sub->net_idx)); + bt_mesh_adv_send(buf, SNB_XMIT, &send_cb, NET_IDX_SET(sub->net_idx)); net_buf_unref(buf); } return 0; } -#if defined(CONFIG_BLE_MESH_NODE) +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_ADV) static int unprovisioned_beacon_send(void) { -#if defined(CONFIG_BLE_MESH_PB_ADV) - const struct bt_mesh_prov *prov = NULL; - uint8_t uri_hash[16] = { 0 }; + uint8_t uri_hash[16] = {0}; struct net_buf *buf = NULL; uint16_t oob_info = 0U; - BT_DBG("%s", __func__); + if (bt_mesh_prov_get() == NULL) { + BT_ERR("No provisioning context provided"); + return -EINVAL; + } - buf = bt_mesh_adv_create(BLE_MESH_ADV_BEACON, UNPROV_XMIT, K_NO_WAIT); + buf = bt_mesh_adv_create(BLE_MESH_ADV_BEACON, K_NO_WAIT); if (!buf) { - BT_ERR("Out of beacon buffer"); + BT_ERR("Out of unprov beacon buffer"); return -ENOBUFS; } - prov = bt_mesh_prov_get(); - net_buf_add_u8(buf, BEACON_TYPE_UNPROVISIONED); - net_buf_add_mem(buf, prov->uuid, 16); + net_buf_add_mem(buf, bt_mesh_prov_get()->uuid, 16); - if (prov->uri && bt_mesh_s1(prov->uri, uri_hash) == 0) { - oob_info = prov->oob_info | BLE_MESH_PROV_OOB_URI; + if (bt_mesh_prov_get()->uri && + bt_mesh_s1(bt_mesh_prov_get()->uri, uri_hash) == 0) { + oob_info = bt_mesh_prov_get()->oob_info | BLE_MESH_PROV_OOB_URI; } else { - oob_info = prov->oob_info; + oob_info = bt_mesh_prov_get()->oob_info; } net_buf_add_be16(buf, oob_info); net_buf_add_mem(buf, uri_hash, 4); - bt_mesh_adv_send(buf, NULL, NULL); + bt_mesh_adv_send(buf, UNPROV_XMIT, NULL, NULL); net_buf_unref(buf); - if (prov->uri) { - size_t len; + if (bt_mesh_prov_get()->uri) { + size_t len = 0; - buf = bt_mesh_adv_create(BLE_MESH_ADV_URI, UNPROV_XMIT, - K_NO_WAIT); + buf = bt_mesh_adv_create(BLE_MESH_ADV_URI, K_NO_WAIT); if (!buf) { BT_ERR("Unable to allocate URI buffer"); return -ENOBUFS; } - len = strlen(prov->uri); + len = strlen(bt_mesh_prov_get()->uri); + if (net_buf_tailroom(buf) < len) { BT_WARN("Too long URI to fit advertising data"); } else { - net_buf_add_mem(buf, prov->uri, len); - bt_mesh_adv_send(buf, NULL, NULL); + net_buf_add_mem(buf, bt_mesh_prov_get()->uri, len); + bt_mesh_adv_send(buf, UNPROV_XMIT, NULL, NULL); } net_buf_unref(buf); } -#endif /* CONFIG_BLE_MESH_PB_ADV */ return 0; } -#else /* CONFIG_BLE_MESH_NODE */ +#else /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_ADV) */ static int unprovisioned_beacon_send(void) { return 0; } -#endif /* CONFIG_BLE_MESH_NODE */ +#endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_ADV) */ -static void update_beacon_observation(void) +void update_beacon_observation(bool private_beacon) { - static bool first_half; + static bool snb_first_half; 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 - * window only after the second half. + * runs every 10 seconds. We process what's happened during + * the window only after the second half. */ - first_half = !first_half; - if (first_half) { - return; +#if CONFIG_BLE_MESH_PRB_SRV + static bool mpb_first_half; + + if (private_beacon) { + mpb_first_half = !mpb_first_half; + if (mpb_first_half) { + return; + } + } else +#endif + { + snb_first_half = !snb_first_half; + if (snb_first_half) { + return; + } } subnet_size = bt_mesh_rx_netkey_size(); @@ -286,8 +301,16 @@ static void update_beacon_observation(void) continue; } - sub->beacons_last = sub->beacons_cur; - sub->beacons_cur = 0U; +#if CONFIG_BLE_MESH_PRB_SRV + if (private_beacon) { + sub->mpb_last = sub->mpb_cur; + sub->mpb_cur = 0U; + } else +#endif + { + sub->snb_last = sub->snb_cur; + sub->snb_cur = 0U; + } } } @@ -299,34 +322,31 @@ static bool ready_to_send(void) return false; } -static void beacon_send(struct k_work *work) +static void secure_beacon_send_timeout(struct k_work *work) { /* Don't send anything if we have an active provisioning link */ 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); + IS_ENABLED(CONFIG_BLE_MESH_PROV) && bt_mesh_prov_active()) { + k_delayed_work_submit(&snb_timer, UNPROV_BEACON_INTERVAL); return; } - BT_DBG("%s", __func__); - if (ready_to_send()) { - update_beacon_observation(); + update_beacon_observation(false); + secure_beacon_send(); /* Only resubmit if beaconing is still enabled */ - if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED || - bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_INITIATOR)) { - k_delayed_work_submit(&beacon_timer, - PROVISIONED_INTERVAL); + if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED || + bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_INITIATOR)) { + k_delayed_work_submit(&snb_timer, SECURE_BEACON_INTERVAL); } } else { if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node()) { unprovisioned_beacon_send(); - k_delayed_work_submit(&beacon_timer, UNPROVISIONED_INTERVAL); + k_delayed_work_submit(&snb_timer, UNPROV_BEACON_INTERVAL); } } - } static void secure_beacon_recv(struct net_buf_simple *buf) @@ -339,12 +359,12 @@ static void secure_beacon_recv(struct net_buf_simple *buf) bool new_key = false; uint8_t flags = 0U; - if (buf->len < 21) { - BT_ERR("Too short secure beacon (len %u)", buf->len); + if (buf->len != 21) { + BT_ERR("Malformed secure beacon (len %u)", buf->len); return; } - sub = cache_check(buf->data); + sub = cache_check(buf->data, false); if (sub) { /* We've seen this beacon before - just update the stats */ goto update_stats; @@ -361,9 +381,9 @@ static void secure_beacon_recv(struct net_buf_simple *buf) BT_DBG("flags 0x%02x id %s iv_index 0x%08x", flags, bt_hex(net_id, 8), iv_index); - sub = bt_mesh_subnet_find(net_id, flags, iv_index, auth, &new_key); + sub = bt_mesh_subnet_find_with_snb(net_id, flags, iv_index, auth, &new_key); if (!sub) { - BT_DBG("No subnet that matched beacon"); + BT_DBG("No subnet that matched secure beacon"); return; } @@ -372,16 +392,16 @@ static void secure_beacon_recv(struct net_buf_simple *buf) return; } - cache_add(data, sub); + cache_add(data, sub, false); - /* Spec v1.0.1, Section 3.8.4: - * If a node on a primary subnet receives an update on + /* If we have NetKey0 accept initiation only from it. + * + * Spec v1.1, Section 3.8.4: + * If a device on a primary subnet receives an update on * the primary subnet, it shall propagate the IV update - * to all other subnets. If a node on a primary subnet + * to all other subnets. If a device on a primary subnet * receives an IV update on any other subnet, the update * shall be ignored. - * If a node on a primary subnet receives an key update - * on any other subnet, the update shall not be ignored. */ if (bt_mesh_primary_subnet_exist() && sub->net_idx != BLE_MESH_KEY_PRIMARY && @@ -391,12 +411,12 @@ static void secure_beacon_recv(struct net_buf_simple *buf) goto update_stats; } - BT_INFO("net_idx 0x%03x iv_index 0x%08x current iv_index 0x%08x", + BT_DBG("SNB: net_idx 0x%03x 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) && - (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) == - BLE_MESH_IV_UPDATE(flags))) { + (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) == + BLE_MESH_IV_UPDATE(flags))) { bt_mesh_beacon_ivu_initiator(false); } @@ -412,7 +432,7 @@ static void secure_beacon_recv(struct net_buf_simple *buf) kr_change = bt_mesh_kr_update(sub, BLE_MESH_KEY_REFRESH(flags), new_key); if (kr_change) { - bt_mesh_net_beacon_update(sub); + bt_mesh_net_secure_beacon_update(sub); } if (iv_change) { @@ -424,9 +444,9 @@ static void secure_beacon_recv(struct net_buf_simple *buf) } update_stats: - if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED && - sub->beacons_cur < 0xff) { - sub->beacons_cur++; + if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED && + sub->snb_cur < 0xff) { + sub->snb_cur++; } } @@ -445,14 +465,28 @@ void bt_mesh_beacon_recv(struct net_buf_simple *buf, int8_t rssi) switch (type) { case BEACON_TYPE_UNPROVISIONED: BT_DBG("Unprovisioned device beacon received"); + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && - bt_mesh_is_provisioner_en()) { + IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && + bt_mesh_is_provisioner_en()) { bt_mesh_provisioner_unprov_beacon_recv(buf, rssi); } + + if (IS_ENABLED(CONFIG_BLE_MESH_RPR_SRV) && + bt_mesh_is_provisioned()) { + const bt_mesh_addr_t *addr = bt_mesh_get_unprov_dev_addr(); + bt_mesh_unprov_dev_fifo_enqueue(buf->data, addr->val, bt_mesh_get_adv_type()); + bt_mesh_rpr_srv_unprov_beacon_recv(buf, bt_mesh_get_adv_type(), addr, rssi); + } break; case BEACON_TYPE_SECURE: secure_beacon_recv(buf); break; +#if CONFIG_BLE_MESH_PRIVATE_BEACON + case BEACON_TYPE_PRIVATE: + bt_mesh_private_beacon_recv(buf); + break; +#endif default: BT_DBG("Unknown beacon type 0x%02x", type); break; @@ -461,13 +495,31 @@ void bt_mesh_beacon_recv(struct net_buf_simple *buf, int8_t rssi) void bt_mesh_beacon_init(void) { - k_delayed_work_init(&beacon_timer, beacon_send); + /* secure beacon init */ + if (k_delayed_work_init(&snb_timer, secure_beacon_send_timeout)) { + BT_ERR("Failed to create a snb_timer"); + return; + } + +#if CONFIG_BLE_MESH_PRB_SRV + /* private beacon init */ + if (bt_mesh_private_beacon_timer_init()) { + BT_ERR("Failed to create a mpb_timer"); + return; + } +#endif } #if CONFIG_BLE_MESH_DEINIT void bt_mesh_beacon_deinit(void) { - k_delayed_work_free(&beacon_timer); + /* secure beacon deinit */ + k_delayed_work_free(&snb_timer); + +#if CONFIG_BLE_MESH_PRB_SRV + /* private beacon deinit */ + bt_mesh_private_beacon_timer_free(); +#endif } #endif /* CONFIG_BLE_MESH_DEINIT */ @@ -476,20 +528,32 @@ void bt_mesh_beacon_ivu_initiator(bool enable) bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_INITIATOR, enable); if (enable) { - k_work_submit(&beacon_timer.work); - } else if (bt_mesh_beacon_get() == BLE_MESH_BEACON_DISABLED) { - k_delayed_work_cancel(&beacon_timer); + k_delayed_work_submit(&snb_timer, K_NO_WAIT); +#if CONFIG_BLE_MESH_PRB_SRV + if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) { + bt_mesh_private_beacon_timer_submit(K_NO_WAIT); + } +#endif + } else { + if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_DISABLED) { + k_delayed_work_cancel(&snb_timer); + } +#if CONFIG_BLE_MESH_PRB_SRV + if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_DISABLED) { + bt_mesh_private_beacon_timer_cancel(); + } +#endif } } -void bt_mesh_beacon_enable(void) +void bt_mesh_secure_beacon_enable(void) { size_t subnet_size = 0U; int i = 0; - if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && - !bt_mesh_is_provisioned()) { - k_work_submit(&beacon_timer.work); + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && + bt_mesh_is_node() && !bt_mesh_is_provisioned()) { + k_delayed_work_submit(&snb_timer, K_NO_WAIT); return; } @@ -502,18 +566,18 @@ void bt_mesh_beacon_enable(void) continue; } - sub->beacons_last = 0U; - sub->beacons_cur = 0U; + sub->snb_last = 0U; + sub->snb_cur = 0U; - bt_mesh_net_beacon_update(sub); + bt_mesh_net_secure_beacon_update(sub); } - k_work_submit(&beacon_timer.work); + k_delayed_work_submit(&snb_timer, K_NO_WAIT); } -void bt_mesh_beacon_disable(void) +void bt_mesh_secure_beacon_disable(void) { if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_INITIATOR)) { - k_delayed_work_cancel(&beacon_timer); + k_delayed_work_cancel(&snb_timer); } } diff --git a/components/bt/esp_ble_mesh/core/beacon.h b/components/bt/esp_ble_mesh/core/beacon.h index 5e97b4cfe001..764d86f1cbdb 100644 --- a/components/bt/esp_ble_mesh/core/beacon.h +++ b/components/bt/esp_ble_mesh/core/beacon.h @@ -15,17 +15,28 @@ extern "C" { #endif -void bt_mesh_beacon_enable(void); -void bt_mesh_beacon_disable(void); +#define BEACON_TYPE_UNPROVISIONED 0x00 +#define BEACON_TYPE_SECURE 0x01 +#define BEACON_TYPE_PRIVATE 0x02 + +#define NET_IDX_SET(_val) ((void *)((uint32_t)(_val))) +#define NET_IDX_GET(_ptr) ((uint32_t)(_ptr)) + +/* If the interval has passed or is within 5 seconds from now send a beacon */ +#define BEACON_THRESHOLD(last) (K_SECONDS(10 * ((last) + 1)) - K_SECONDS(5)) + +void bt_mesh_secure_beacon_enable(void); +void bt_mesh_secure_beacon_disable(void); void bt_mesh_beacon_ivu_initiator(bool enable); void bt_mesh_beacon_recv(struct net_buf_simple *buf, int8_t rssi); -void bt_mesh_beacon_create(struct bt_mesh_subnet *sub, - struct net_buf_simple *buf); +void bt_mesh_secure_beacon_create(struct bt_mesh_subnet *sub, + struct net_buf_simple *buf); void bt_mesh_beacon_init(void); + void bt_mesh_beacon_deinit(void); #ifdef __cplusplus diff --git a/components/bt/esp_ble_mesh/core/bluedroid_host/adapter.c b/components/bt/esp_ble_mesh/core/bluedroid_host/adapter.c index c9abf821ad37..a9908b72503f 100644 --- a/components/bt/esp_ble_mesh/core/bluedroid_host/adapter.c +++ b/components/bt/esp_ble_mesh/core/bluedroid_host/adapter.c @@ -18,7 +18,9 @@ #include "osi/future.h" #include "device/controller.h" +#if CONFIG_MBEDTLS_HARDWARE_AES #include "mbedtls/aes.h" +#endif #include #include @@ -27,6 +29,9 @@ #include "mesh/adapter.h" #include "mesh/common.h" #include "prov_pvnr.h" +#include "net.h" + +#include "mesh_v1.1/utils.h" struct bt_mesh_dev bt_mesh_dev; @@ -48,14 +53,15 @@ struct bt_mesh_dev bt_mesh_dev; /* P-256 Variables */ static uint8_t bt_mesh_public_key[64]; -static BT_OCTET32 bt_mesh_private_key; +static uint8_t bt_mesh_private_key[32]; /* Scan related functions */ 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 (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_SERVER + CONFIG_BLE_MESH_GATT_PROXY_SERVER || \ + (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) /* 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 */ @@ -65,7 +71,7 @@ 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 tBTA_GATTS_IF bt_mesh_gatts_if; -static BD_ADDR bt_mesh_gatts_addr; +static uint8_t bt_mesh_gatts_addr[BLE_MESH_ADDR_LEN]; static uint16_t svc_handle, char_handle; static future_t *future_mesh; @@ -74,7 +80,8 @@ static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(uint16_t hand #endif #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) /* 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 { @@ -176,14 +183,14 @@ static bool valid_adv_param(const struct bt_mesh_adv_param *param) if (!(param->options & BLE_MESH_ADV_OPT_CONNECTABLE)) { #if BLE_MESH_DEV if (bt_mesh_dev.hci_version < BLE_MESH_HCI_VERSION_5_0 && - param->interval_min < 0x00a0) { + param->interval_min < 0x00a0) { return false; } #endif } if (param->interval_min > param->interval_max || - param->interval_min < 0x0020 || param->interval_max > 0x4000) { + param->interval_min < 0x0020 || param->interval_max > 0x4000) { return false; } @@ -234,12 +241,12 @@ static void start_adv_completed_cb(uint8_t status) static bool valid_scan_param(const struct bt_mesh_scan_param *param) { if (param->type != BLE_MESH_SCAN_PASSIVE && - param->type != BLE_MESH_SCAN_ACTIVE) { + param->type != BLE_MESH_SCAN_ACTIVE) { return false; } if (param->filter_dup != BLE_MESH_SCAN_FILTER_DUP_DISABLE && - param->filter_dup != BLE_MESH_SCAN_FILTER_DUP_ENABLE) { + param->filter_dup != BLE_MESH_SCAN_FILTER_DUP_ENABLE) { return false; } @@ -261,7 +268,7 @@ static bool valid_scan_param(const struct bt_mesh_scan_param *param) static int start_le_scan(uint8_t scan_type, uint16_t interval, uint16_t window, uint8_t filter_dup, uint8_t scan_fil_policy) { - UINT8 addr_type_own = BLE_MESH_ADDR_PUBLIC; /* Currently only support Public Address */ + uint8_t addr_type_own = BLE_MESH_ADDR_PUBLIC; /* Currently only support Public Address */ tGATT_IF client_if = 0xFF; /* Default GATT interface id */ BLE_MESH_BTM_CHECK_STATUS( @@ -297,7 +304,7 @@ static void bt_mesh_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARC net_buf_simple_init_with_data(&buf, p_data->inq_res.p_eir, p_data->inq_res.adv_data_len); if (bt_mesh_scan_dev_found_cb) { - bt_mesh_scan_dev_found_cb(&addr, p_data->inq_res.rssi, p_data->inq_res.ble_evt_type, &buf); + bt_mesh_scan_dev_found_cb(&addr, p_data->inq_res.rssi, p_data->inq_res.ble_evt_type, &buf, p_data->inq_res.scan_rsp_len); } } else if (event == BTA_DM_INQ_CMPL_EVT) { BT_INFO("Scan completed, number of scan response %d", p_data->inq_cmpl.num_resps); @@ -360,7 +367,13 @@ int bt_le_adv_start(const struct bt_mesh_adv_param *param, } else { adv_type = BLE_MESH_ADV_NONCONN_IND; } - addr_type_own = BLE_MESH_ADDR_PUBLIC; /* Currently only support Public Address */ + +#if CONFIG_BLE_MESH_PRB_SRV + addr_type_own = bt_mesh_private_beacon_update_addr_type(ad); +#else + addr_type_own = BLE_MESH_ADDR_PUBLIC; +#endif + channel_map = BLE_MESH_ADV_CHNL_37 | BLE_MESH_ADV_CHNL_38 | BLE_MESH_ADV_CHNL_39; adv_fil_pol = BLE_MESH_AP_SCAN_CONN_ALL; p_start_adv_cb = start_adv_completed_cb; @@ -502,7 +515,7 @@ int bt_le_update_white_list(struct bt_mesh_white_list *wl) } if (BTM_BleUpdateAdvWhitelist(wl->add_remove, wl->remote_bda, - wl->addr_type, (tBTM_UPDATE_WHITELIST_CBACK *)wl->update_wl_comp_cb) == false) { + wl->addr_type, (tBTM_UPDATE_WHITELIST_CBACK *)wl->update_wl_comp_cb) == false) { return -EIO; } @@ -511,7 +524,7 @@ int bt_le_update_white_list(struct bt_mesh_white_list *wl) #endif #if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_SERVER + CONFIG_BLE_MESH_GATT_PROXY_SERVER static void bt_mesh_bta_gatts_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) { switch (event) { @@ -740,7 +753,7 @@ struct gatts_incl { uint16_t start_handle; uint16_t end_handle; uint16_t uuid16; -} __packed; +} __attribute__((packed)); ssize_t bt_mesh_gatts_attr_read_included(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, @@ -790,7 +803,7 @@ struct gatts_chrc { uint16_t uuid16; uint8_t uuid[16]; }; -} __packed; +} __attribute__((packed)); ssize_t bt_mesh_gatts_attr_read_chrc(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, @@ -845,8 +858,6 @@ static void bta_uuid_to_bt_mesh_uuid(tBT_UUID *bta_uuid, const struct bt_mesh_uu } else { BT_ERR("Invalid mesh uuid type %d", uuid->type); } - - return; } static int gatts_register(struct bt_mesh_gatt_service *svc) @@ -1099,7 +1110,8 @@ int bt_mesh_gatts_set_local_device_name(const char *name) #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 + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) void bt_mesh_gattc_conn_cb_register(struct bt_mesh_prov_conn_cb *cb) { bt_mesh_gattc_conn_cb = cb; @@ -1117,7 +1129,7 @@ uint8_t bt_mesh_gattc_get_free_conn_count(void) 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) { + bt_mesh_gattc_info[i].service_uuid == 0x0000) { ++count; } } @@ -1155,13 +1167,13 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, uint16_t service_uuid) int i; if (!addr || !memcmp(addr->val, zero, BLE_MESH_ADDR_LEN) || - (addr->type > BLE_ADDR_RANDOM)) { + (addr->type > BLE_ADDR_RANDOM)) { BT_ERR("Invalid remote address"); return -EINVAL; } if (service_uuid != BLE_MESH_UUID_MESH_PROV_VAL && - service_uuid != BLE_MESH_UUID_MESH_PROXY_VAL) { + service_uuid != BLE_MESH_UUID_MESH_PROXY_VAL) { BT_ERR("Invalid service uuid 0x%04x", service_uuid); return -EINVAL; } @@ -1178,7 +1190,7 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, uint16_t service_uuid) /* Find empty element in queue to store device info */ 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)) { + (bt_mesh_gattc_info[i].service_uuid == 0x0000)) { memcpy(bt_mesh_gattc_info[i].addr.val, addr->val, BLE_MESH_ADDR_LEN); bt_mesh_gattc_info[i].addr.type = addr->type; /* Service to be found after exchanging mtu size */ @@ -1281,7 +1293,6 @@ void bt_mesh_gattc_disconnect(struct bt_mesh_conn *conn) } BT_ERR("Conn %p not found", conn); - return; } /** Mesh Provisioning Service: 0x1827 @@ -1306,7 +1317,7 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) BT_DBG("BTA_GATTC_REG_EVT"); if (p_data->reg_oper.app_uuid.len == LEN_UUID_128 && - !memcmp(p_data->reg_oper.app_uuid.uu.uuid128, uuid, 16)) { + !memcmp(p_data->reg_oper.app_uuid.uu.uuid128, uuid, 16)) { bt_mesh_gattc_if = p_data->reg_oper.client_if; BT_DBG("bt_mesh_gattc_if is %d", bt_mesh_gattc_if); } @@ -1388,7 +1399,8 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) BTA_GATTC_GetDBSizeByType(p_data->search_cmpl.conn_id, BTGATT_DB_CHARACTERISTIC, bt_mesh_gattc_info[i].start_handle, bt_mesh_gattc_info[i].end_handle, BTA_GATTC_INVALID_HANDLE, &count); - if (count != 2) { + if (count != 3 && count != 2) { + BT_ERR("Invalid characteristic num(%d) within Mesh Provisioning/Proxy Service", count); bt_mesh_gattc_disconnect(conn); return; } @@ -1562,8 +1574,8 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) } if (memcmp(bt_mesh_gattc_info[i].addr.val, p_data->notify.bda, BLE_MESH_ADDR_LEN) || - bt_mesh_gattc_info[i].data_out_handle != p_data->notify.handle || - p_data->notify.is_notify == false) { + bt_mesh_gattc_info[i].data_out_handle != p_data->notify.handle || + p_data->notify.is_notify == false) { BT_ERR("Notification error"); bt_mesh_gattc_disconnect(conn); return; @@ -1572,7 +1584,8 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->prov_notify != NULL) { len = bt_mesh_gattc_conn_cb->prov_notify(&bt_mesh_gattc_info[i].conn, - p_data->notify.value, p_data->notify.len); + p_data->notify.value, + p_data->notify.len); if (len < 0) { BT_ERR("prov_notify failed"); bt_mesh_gattc_disconnect(conn); @@ -1582,7 +1595,8 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) } else if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROXY_VAL) { if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->proxy_notify != NULL) { len = bt_mesh_gattc_conn_cb->proxy_notify(&bt_mesh_gattc_info[i].conn, - p_data->notify.value, p_data->notify.len); + p_data->notify.value, + p_data->notify.len); if (len < 0) { BT_ERR("proxy_notify failed"); bt_mesh_gattc_disconnect(conn); @@ -1721,14 +1735,15 @@ void bt_mesh_gatt_init(void) BTA_GATT_SetLocalMTU(GATT_DEF_BLE_MTU_SIZE); #if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_SERVER + 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 + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) 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; @@ -1744,7 +1759,7 @@ void bt_mesh_gatt_init(void) void bt_mesh_gatt_deinit(void) { #if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_SERVER + 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; @@ -1753,7 +1768,8 @@ void bt_mesh_gatt_deinit(void) #endif #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) BTA_GATTC_AppDeregister(bt_mesh_gattc_if); bt_mesh_gattc_if = 0U; for (int i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { @@ -1774,8 +1790,6 @@ void bt_mesh_gatt_deinit(void) void bt_mesh_adapt_init(void) { - BT_DBG("%s", __func__); - /* initialization of P-256 parameters */ p_256_init_curve(KEY_LENGTH_DWORDS_P256); @@ -1795,7 +1809,7 @@ void bt_mesh_set_private_key(const uint8_t pri_key[32]) const uint8_t *bt_mesh_pub_key_get(void) { - BT_OCTET32 private_key = {0}; + uint8_t private_key[32] = {0}; Point public_key = {0}; if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_HAS_PUB_KEY)) { @@ -1842,17 +1856,17 @@ bool bt_mesh_check_public_key(const uint8_t key[64]) return ECC_CheckPointIsInElliCur_P256((Point *)&check); } -int bt_mesh_dh_key_gen(const uint8_t remote_pk[64], bt_mesh_dh_key_cb_t cb, const uint8_t idx) +int bt_mesh_dh_key_gen(const uint8_t remote_pub_key[64], uint8_t dhkey[32]) { - BT_OCTET32 private_key = {0}; + uint8_t private_key[32] = {0}; Point peer_pub_key = {0}; Point new_pub_key = {0}; BT_DBG("private key = %s", bt_hex(bt_mesh_private_key, BT_OCTET32_LEN)); memcpy(private_key, bt_mesh_private_key, BT_OCTET32_LEN); - memcpy(peer_pub_key.x, remote_pk, BT_OCTET32_LEN); - memcpy(peer_pub_key.y, &remote_pk[BT_OCTET32_LEN], BT_OCTET32_LEN); + memcpy(peer_pub_key.x, remote_pub_key, BT_OCTET32_LEN); + memcpy(peer_pub_key.y, &remote_pub_key[BT_OCTET32_LEN], BT_OCTET32_LEN); BT_DBG("remote public key x = %s", bt_hex(peer_pub_key.x, BT_OCTET32_LEN)); BT_DBG("remote public key y = %s", bt_hex(peer_pub_key.y, BT_OCTET32_LEN)); @@ -1862,9 +1876,7 @@ int bt_mesh_dh_key_gen(const uint8_t remote_pk[64], bt_mesh_dh_key_cb_t cb, cons BT_DBG("new public key x = %s", bt_hex(new_pub_key.x, 32)); BT_DBG("new public key y = %s", bt_hex(new_pub_key.y, 32)); - if (cb != NULL) { - cb((const uint8_t *)new_pub_key.x, idx); - } + memcpy(dhkey, new_pub_key.x, 32); return 0; } @@ -1959,10 +1971,10 @@ int bt_mesh_encrypt_be(const uint8_t key[16], const uint8_t plaintext[16], return 0; } -#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN int bt_mesh_update_exceptional_list(uint8_t sub_code, uint32_t type, void *info) { - BD_ADDR value = {0}; + uint8_t value[BLE_MESH_ADDR_LEN] = {0}; if ((sub_code > BLE_MESH_EXCEP_LIST_SUB_CODE_CLEAN) || (sub_code < BLE_MESH_EXCEP_LIST_SUB_CODE_CLEAN && diff --git a/components/bt/esp_ble_mesh/core/cfg_cli.c b/components/bt/esp_ble_mesh/core/cfg_cli.c index 023fe43ea65e..3aedd3644b8a 100644 --- a/components/bt/esp_ble_mesh/core/cfg_cli.c +++ b/components/bt/esp_ble_mesh/core/cfg_cli.c @@ -24,7 +24,7 @@ static const bt_mesh_client_op_pair_t cfg_op_pair[] = { { OP_BEACON_GET, OP_BEACON_STATUS }, { OP_BEACON_SET, OP_BEACON_STATUS }, - { OP_DEV_COMP_DATA_GET, OP_DEV_COMP_DATA_STATUS }, + { OP_COMP_DATA_GET, OP_COMP_DATA_STATUS }, { OP_DEFAULT_TTL_GET, OP_DEFAULT_TTL_STATUS }, { OP_DEFAULT_TTL_SET, OP_DEFAULT_TTL_STATUS }, { OP_GATT_PROXY_GET, OP_GATT_PROXY_STATUS }, @@ -73,40 +73,17 @@ static const bt_mesh_client_op_pair_t cfg_op_pair[] = { static bt_mesh_mutex_t cfg_client_lock; -static inline void bt_mesh_cfg_client_mutex_new(void) -{ - if (!cfg_client_lock.mutex) { - bt_mesh_mutex_create(&cfg_client_lock); - } -} - -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_cfg_client_mutex_free(void) -{ - bt_mesh_mutex_free(&cfg_client_lock); -} -#endif /* CONFIG_BLE_MESH_DEINIT */ - -static inline void bt_mesh_cfg_client_lock(void) -{ - bt_mesh_mutex_lock(&cfg_client_lock); -} - -static inline void bt_mesh_cfg_client_unlock(void) -{ - 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_model *model = NULL; struct bt_mesh_msg_ctx ctx = {0}; uint32_t opcode = 0U; BT_WARN("Receive configuration status message timeout"); - bt_mesh_cfg_client_lock(); + bt_mesh_mutex_lock(&cfg_client_lock); timer = CONTAINER_OF(work, struct k_delayed_work, work); @@ -115,15 +92,14 @@ static void timeout_handler(struct k_work *work) if (node) { memcpy(&ctx, &node->ctx, sizeof(ctx)); opcode = node->opcode; + model = node->model; 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); + opcode, BTC_BLE_MESH_EVT_CONFIG_CLIENT_TIMEOUT, model, &ctx, NULL, 0); } } - bt_mesh_cfg_client_unlock(); - - return; + bt_mesh_mutex_unlock(&cfg_client_lock); } static void cfg_client_recv_status(struct bt_mesh_model *model, @@ -143,7 +119,7 @@ static void cfg_client_recv_status(struct bt_mesh_model *model, buf.data = (uint8_t *)status; buf.len = (uint16_t)len; - bt_mesh_cfg_client_lock(); + bt_mesh_mutex_lock(&cfg_client_lock); node = bt_mesh_is_client_recv_publish_msg(model, ctx, &buf, true); if (!node) { @@ -151,7 +127,7 @@ static void cfg_client_recv_status(struct bt_mesh_model *model, } else { switch (node->opcode) { case OP_BEACON_GET: - case OP_DEV_COMP_DATA_GET: + case OP_COMP_DATA_GET: case OP_DEFAULT_TTL_GET: case OP_GATT_PROXY_GET: case OP_RELAY_GET: @@ -213,10 +189,10 @@ static void cfg_client_recv_status(struct bt_mesh_model *model, } } - bt_mesh_cfg_client_unlock(); + bt_mesh_mutex_unlock(&cfg_client_lock); switch (ctx->recv_op) { - case OP_DEV_COMP_DATA_STATUS: { + case OP_COMP_DATA_STATUS: { struct bt_mesh_cfg_comp_data_status *val = status; bt_mesh_free_buf(val->comp_data); break; @@ -649,7 +625,7 @@ static void net_trans_status(struct bt_mesh_model *model, } const struct bt_mesh_model_op bt_mesh_cfg_cli_op[] = { - { OP_DEV_COMP_DATA_STATUS, 15, comp_data_status }, + { OP_COMP_DATA_STATUS, 15, comp_data_status }, { OP_BEACON_STATUS, 1, beacon_status }, { OP_DEFAULT_TTL_STATUS, 1, ttl_status }, { OP_FRIEND_STATUS, 1, friend_status }, @@ -707,7 +683,7 @@ static int send_msg_with_le16(bt_mesh_client_common_param_t *param, uint32_t op, int bt_mesh_cfg_comp_data_get(bt_mesh_client_common_param_t *param, uint8_t page) { - return send_msg_with_u8(param, OP_DEV_COMP_DATA_GET, page); + return send_msg_with_u8(param, OP_COMP_DATA_GET, page); } int bt_mesh_cfg_beacon_get(bt_mesh_client_common_param_t *param) @@ -1264,27 +1240,28 @@ static int cfg_cli_init(struct bt_mesh_model *model) return -EINVAL; } - if (!client->internal_data) { - internal = bt_mesh_calloc(sizeof(config_internal_data_t)); - if (!internal) { - BT_ERR("Allocate memory for Configuration Client internal data fail"); - return -ENOMEM; - } - - sys_slist_init(&internal->queue); + if (client->internal_data) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } - client->model = model; - client->op_pair_size = ARRAY_SIZE(cfg_op_pair); - client->op_pair = cfg_op_pair; - client->internal_data = internal; - } else { - bt_mesh_client_clear_list(client->internal_data); + internal = bt_mesh_calloc(sizeof(config_internal_data_t)); + if (!internal) { + BT_ERR("%s, Out of memory", __func__); + return -ENOMEM; } + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(cfg_op_pair); + client->op_pair = cfg_op_pair; + client->internal_data = internal; + /* Configuration Model security is device-key based */ model->keys[0] = BLE_MESH_KEY_DEV; - bt_mesh_cfg_client_mutex_new(); + bt_mesh_mutex_create(&cfg_client_lock); return 0; } @@ -1319,7 +1296,7 @@ static int cfg_cli_deinit(struct bt_mesh_model *model) client->internal_data = NULL; } - bt_mesh_cfg_client_mutex_free(); + bt_mesh_mutex_free(&cfg_client_lock); return 0; } diff --git a/components/bt/esp_ble_mesh/core/cfg_srv.c b/components/bt/esp_ble_mesh/core/cfg_srv.c index 33d0c8f395ee..5e77a7a58903 100644 --- a/components/bt/esp_ble_mesh/core/cfg_srv.c +++ b/components/bt/esp_ble_mesh/core/cfg_srv.c @@ -18,6 +18,7 @@ #include "lpn.h" #include "transport.h" #include "crypto.h" +#include "net.h" #include "access.h" #include "beacon.h" #include "foundation.h" @@ -28,94 +29,17 @@ #include "mesh/main.h" #include "mesh/common.h" -#define DEFAULT_TTL 7 +#include "mesh_v1.1/utils.h" -/* Maximum message length is 384 in BLE Mesh. Here for composition data, - * due to 1 octet opcode and 4 octets TransMIC, 379 octets can be used to - * store device composition data. - */ -#define COMP_DATA_MAX_LEN 379 +#define DEFAULT_TTL 7 static struct bt_mesh_cfg_srv *conf; 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 = NULL; - int i; - - if (net_buf_simple_tailroom(buf) < - 4 + (elem->model_count * 2U) + (elem->vnd_model_count * 4U)) { - BT_ERR("Too large device composition"); - return -E2BIG; - } - - net_buf_simple_add_le16(buf, elem->loc); - - net_buf_simple_add_u8(buf, elem->model_count); - net_buf_simple_add_u8(buf, elem->vnd_model_count); - - for (i = 0; i < elem->model_count; i++) { - mod = &elem->models[i]; - net_buf_simple_add_le16(buf, mod->id); - } - - for (i = 0; i < elem->vnd_model_count; i++) { - mod = &elem->vnd_models[i]; - net_buf_simple_add_le16(buf, mod->vnd.company); - net_buf_simple_add_le16(buf, mod->vnd.id); - } - - return 0; -} - -static int comp_get_page_0(struct net_buf_simple *buf) -{ - const struct bt_mesh_comp *comp = NULL; - uint16_t feat = 0U; - int i; - - comp = bt_mesh_comp_get(); - - if (IS_ENABLED(CONFIG_BLE_MESH_RELAY)) { - feat |= BLE_MESH_FEAT_RELAY; - } - - if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { - feat |= BLE_MESH_FEAT_PROXY; - } - - if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { - feat |= BLE_MESH_FEAT_FRIEND; - } - - if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { - feat |= BLE_MESH_FEAT_LOW_POWER; - } - - net_buf_simple_add_le16(buf, comp->cid); - net_buf_simple_add_le16(buf, comp->pid); - net_buf_simple_add_le16(buf, comp->vid); - net_buf_simple_add_le16(buf, CONFIG_BLE_MESH_CRPL); - net_buf_simple_add_le16(buf, feat); - - for (i = 0; i < comp->elem_count; i++) { - int err; - - err = comp_add_elem(buf, &comp->elem[i], i == 0); - if (err) { - return err; - } - } - - return 0; -} - -static void dev_comp_data_get(struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf) +static void comp_data_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) { struct net_buf_simple *sdu = NULL; uint8_t page = 0U; @@ -124,23 +48,59 @@ static void dev_comp_data_get(struct bt_mesh_model *model, ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, bt_hex(buf->data, buf->len)); + /* TODO: + * + * When an element receives a Config Composition Data Get message with + * the Page field of the message containing a value of a Composition + * Data Page that the node contains, it shall respond with a Config + * Composition Data Status message with the Page field set to the page + * number of the Composition Data and the Data field set to the value + * of the largest portion of the Composition Data Page that fits in the + * Data field. If an element is reported in the Config Composition Data + * Status message, the complete list of models supported by the element + * shall be included in the elements description. If the complete list + * of models does not fit in the Data field, the element shall not be + * reported. + * + * When an element receives a Config Composition Data Get message with + * the Page field of the message containing a reserved page number or a + * page number the node does not support, it shall respond with a Config + * Composition Data Status message with the Page field set to the largest + * page number of the Composition Data that the node supports and that is + * less than the Page field value of the received Config Composition Data + * Get message and with the Data field set to the value of the largest + * portion of the Composition Data Page for that page number that fits in + * the Data field. If an element is reported in a Config Composition Data + * Status message, the complete list of models supported by the element + * shall be included in the elements description. If the complete list of + * models does not fit in the Data field, the element shall not be reported. + */ + page = net_buf_simple_pull_u8(buf); - if (page != 0U) { - BT_WARN("Composition page %u not available", page); - page = 0U; - } - sdu = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, COMP_DATA_MAX_LEN)); + /* Check if the page exists, and if not, get the largest one + * which is smaller than this page. + */ + page = bt_mesh_comp_page_check(page, false); + + sdu = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_MAX_PDU_LEN_WITH_SMIC)); if (!sdu) { BT_ERR("%s, Out of memory", __func__); return; } - bt_mesh_model_msg_init(sdu, OP_DEV_COMP_DATA_STATUS); - + bt_mesh_model_msg_init(sdu, OP_COMP_DATA_STATUS); net_buf_simple_add_u8(sdu, page); - if (comp_get_page_0(sdu) < 0) { - BT_ERR("Unable to get composition page 0"); + + /* Mesh v1.1 updates: + * If an element is reported in the Config Composition Data + * Status message, the complete list of models supported by + * the element shall be included in the elements description. + * If the complete list of models does not fit in the Data + * field, the element shall not be reported. + */ + if (bt_mesh_get_comp_data(sdu, page, 0, true)) { + BT_ERR("Unable to get composition page 0x%02x", page); bt_mesh_free_buf(sdu); return; } @@ -150,15 +110,14 @@ static void dev_comp_data_get(struct bt_mesh_model *model, } bt_mesh_free_buf(sdu); - return; } static struct bt_mesh_model *get_model(struct bt_mesh_elem *elem, struct net_buf_simple *buf, bool *vnd) { - if (buf->len < 4) { - uint16_t id = 0U; + uint16_t company = 0U, id = 0U; + if (buf->len < 4) { id = net_buf_simple_pull_le16(buf); BT_DBG("ID 0x%04x addr 0x%04x", id, elem->addr); @@ -166,35 +125,17 @@ static struct bt_mesh_model *get_model(struct bt_mesh_elem *elem, *vnd = false; return bt_mesh_model_find(elem, id); - } else { - uint16_t company = 0U, id = 0U; - - company = net_buf_simple_pull_le16(buf); - id = net_buf_simple_pull_le16(buf); - - BT_DBG("Company 0x%04x ID 0x%04x addr 0x%04x", company, id, - elem->addr); - - *vnd = true; - - return bt_mesh_model_find_vnd(elem, company, id); } -} -static bool app_key_is_valid(uint16_t app_idx) -{ - int i; + company = net_buf_simple_pull_le16(buf); + id = net_buf_simple_pull_le16(buf); - for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { - struct bt_mesh_app_key *key = &bt_mesh.app_keys[i]; + BT_DBG("Company 0x%04x ID 0x%04x addr 0x%04x", company, id, + elem->addr); - if (key->net_idx != BLE_MESH_KEY_UNUSED && - key->app_idx == app_idx) { - return true; - } - } + *vnd = true; - return false; + return bt_mesh_model_find_vnd(elem, company, id); } static bool mod_pub_app_key_bound(struct bt_mesh_model *model, @@ -208,7 +149,7 @@ static bool mod_pub_app_key_bound(struct bt_mesh_model *model, } } - BT_ERR("Appkey(0x%02x) not bound to this model.", app_idx); + BT_ERR("AppKey(0x%02x) not bound to this model", app_idx); return false; } @@ -255,7 +196,7 @@ static uint8_t _mod_pub_set(struct bt_mesh_model *model, uint16_t pub_addr, /* For case MESH/NODE/CFG/MP/BI-03-C, need to check if appkey * is bound to model identified by the ModelIdentifier. */ - if (!bt_mesh_app_key_find(app_idx) || + if (!bt_mesh_app_key_get(app_idx) || !mod_pub_app_key_bound(model, app_idx)) { return STATUS_INVALID_APPKEY; } @@ -293,7 +234,7 @@ static uint8_t mod_bind(struct bt_mesh_model *model, uint16_t key_idx) BT_DBG("model %p key_idx 0x%03x", model, key_idx); - if (!app_key_is_valid(key_idx)) { + if (!bt_mesh_app_key_get(key_idx)) { return STATUS_INVALID_APPKEY; } @@ -325,7 +266,7 @@ static uint8_t mod_unbind(struct bt_mesh_model *model, uint16_t key_idx, bool st BT_DBG("model %p key_idx 0x%03x store %u", model, key_idx, store); - if (!app_key_is_valid(key_idx)) { + if (!bt_mesh_app_key_get(key_idx)) { return STATUS_INVALID_APPKEY; } @@ -379,7 +320,7 @@ static uint8_t app_key_set(uint16_t net_idx, uint16_t app_idx, const uint8_t val return STATUS_INVALID_NETKEY; } - key = bt_mesh_app_key_find(app_idx); + key = bt_mesh_app_key_get(app_idx); if (update) { if (!key) { return STATUS_INVALID_APPKEY; @@ -403,24 +344,24 @@ static uint8_t app_key_set(uint16_t net_idx, uint16_t app_idx, const uint8_t val if (key->updated) { if (memcmp(keys->val, val, 16)) { return STATUS_CANNOT_UPDATE; - } else { - return STATUS_SUCCESS; } + + return STATUS_SUCCESS; } key->updated = true; } else { if (key) { if (key->net_idx == net_idx && - !memcmp(key->keys[0].val, val, 16)) { + !memcmp(key->keys[0].val, val, 16)) { return STATUS_SUCCESS; } if (key->net_idx == net_idx) { return STATUS_IDX_ALREADY_STORED; - } else { - return STATUS_INVALID_NETKEY; } + + return STATUS_INVALID_NETKEY; } key = bt_mesh_app_key_alloc(app_idx); @@ -569,7 +510,7 @@ static void app_key_del(struct bt_mesh_model *model, goto send_status; } - key = bt_mesh_app_key_find(key_app_idx); + key = bt_mesh_app_key_get(key_app_idx); if (!key) { /* Treat as success since the client might have missed a * previous response and is resending the request. @@ -679,7 +620,7 @@ static void beacon_get(struct bt_mesh_model *model, bt_hex(buf->data, buf->len)); bt_mesh_model_msg_init(&msg, OP_BEACON_STATUS); - net_buf_simple_add_u8(&msg, bt_mesh_beacon_get()); + net_buf_simple_add_u8(&msg, bt_mesh_secure_beacon_get()); if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { BT_ERR("Unable to send Config Beacon Status"); @@ -708,9 +649,9 @@ static void beacon_set(struct bt_mesh_model *model, } if (cfg->beacon) { - bt_mesh_beacon_enable(); + bt_mesh_secure_beacon_enable(); } else { - bt_mesh_beacon_disable(); + bt_mesh_secure_beacon_disable(); } } } else { @@ -719,7 +660,7 @@ static void beacon_set(struct bt_mesh_model *model, } bt_mesh_model_msg_init(&msg, OP_BEACON_STATUS); - net_buf_simple_add_u8(&msg, bt_mesh_beacon_get()); + net_buf_simple_add_u8(&msg, bt_mesh_secure_beacon_get()); if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { BT_ERR("Unable to send Config Beacon Status"); @@ -818,7 +759,7 @@ static void gatt_proxy_set(struct bt_mesh_model *model, } if (!IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) || - bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_NOT_SUPPORTED) { + bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_NOT_SUPPORTED) { goto send_status; } @@ -835,6 +776,23 @@ static void gatt_proxy_set(struct bt_mesh_model *model, cfg->gatt_proxy = buf->data[0]; +#if CONFIG_BLE_MESH_PRB_SRV + /* If the value of the GATT Proxy state of the node is 0x01 (see Table 4.21), + * then the value of the Private GATT Proxy state shall be Disable (0x00). + */ + if (buf->data[0] == BLE_MESH_GATT_PROXY_ENABLED) { + bt_mesh_disable_private_gatt_proxy(); + } +#endif + +#if CONFIG_BLE_MESH_DF_SRV + /* If the value of the GATT Proxy state of the node is 0x00, + * then the value of the directed proxy state shall be 0x00, + * directed proxy use directed default shall be 0x02. + */ + bt_mesh_disable_directed_proxy_state(ctx->net_idx); +#endif + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { bt_mesh_store_cfg(); } @@ -1464,7 +1422,8 @@ static void mod_sub_add(struct bt_mesh_model *model, goto send_status; } - BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT, + BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | \ + BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT, "SubGroupAddr: 0x%x", sub_addr); for (i = 0; i < ARRAY_SIZE(mod->groups); i++) { @@ -1492,6 +1451,10 @@ static void mod_sub_add(struct bt_mesh_model *model, send_mod_sub_status(model, ctx, status, elem_addr, sub_addr, mod_id, vnd); +#if CONFIG_BLE_MESH_DF_SRV + bt_mesh_directed_forwarding_node_solicitation(mod, bt_mesh_subnet_get(ctx->net_idx)); +#endif + if (status == STATUS_SUCCESS) { bt_mesh_cfg_server_state_change_t change = {0}; change.cfg_mod_sub_add.elem_addr = elem_addr; @@ -1868,7 +1831,8 @@ static void mod_sub_va_add(struct bt_mesh_model *model, goto send_status; } - BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT, + BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | \ + BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT, "SubVirtualAddr: 0x%x", sub_addr); for (i = 0; i < ARRAY_SIZE(mod->groups); i++) { @@ -2230,11 +2194,24 @@ static void net_key_add(struct bt_mesh_model *model, } /* Make sure we have valid beacon data to be sent */ - bt_mesh_net_beacon_update(sub); + bt_mesh_net_secure_beacon_update(sub); if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { sub->node_id = BLE_MESH_NODE_IDENTITY_STOPPED; bt_mesh_proxy_server_beacon_send(sub); + +#if CONFIG_BLE_MESH_DF_SRV && CONFIG_BLE_MESH_SUPPORT_DIRECTED_PROXY + /* When the Directed Proxy Server is added to a new subnet, and the + * Proxy_Client_Type parameter for the connection is either Unset or + * Directed_Proxy_Client, then the Directed Proxy Server shall send a + * DIRECTED_PROXY_CAPABILITIES_STATUS message for that subnet to the + * Proxy Client. + */ + if (sub->directed_proxy != BLE_MESH_DIRECTED_PROXY_NOT_SUPPORTED) { + bt_mesh_directed_proxy_server_directed_proxy_caps_send(sub, false); + } +#endif + bt_mesh_adv_update(); } else { sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; @@ -2242,6 +2219,12 @@ static void net_key_add(struct bt_mesh_model *model, send_net_key_status(model, ctx, idx, STATUS_SUCCESS); +#if CONFIG_BLE_MESH_DF_SRV + if (bt_mesh_directed_forwarding_sub_init(sub)) { + BT_ERR("Failed to init subnet for directed forward"); + } +#endif + bt_mesh_cfg_server_state_change_t change = {0}; change.cfg_netkey_add.net_idx = sub->net_idx; memcpy(change.cfg_netkey_add.net_key, sub->keys[0].net, 16); @@ -2313,7 +2296,7 @@ static void net_key_update(struct bt_mesh_model *model, bt_mesh_store_subnet(sub); } - bt_mesh_net_beacon_update(sub); + bt_mesh_net_secure_beacon_update(sub); send_net_key_status(model, ctx, idx, STATUS_SUCCESS); @@ -2326,8 +2309,6 @@ static void net_key_update(struct bt_mesh_model *model, static void hb_pub_disable(struct bt_mesh_cfg_srv *cfg) { - BT_DBG("%s", __func__); - cfg->hb_pub.dst = BLE_MESH_ADDR_UNASSIGNED; cfg->hb_pub.count = 0U; cfg->hb_pub.ttl = 0U; @@ -2369,9 +2350,44 @@ static void net_key_del(struct bt_mesh_model *model, goto send_status; } +#if CONFIG_BLE_MESH_DF_SRV && CONFIG_BLE_MESH_SUPPORT_DIRECTED_PROXY + /* When the Directed Proxy Server is deleted from a subnet, and the + * Proxy_Client_Type parameter for the connection is either Unset or + * Directed_Proxy_Client, then the Directed Proxy Server shall set + * the Use_Directed parameter of the connection for the deleted subnet + * to 0x00, shall set the Proxy_Client_Address_Range parameter of the + * connection for the deleted subnet to the Unassigned value, and shall + * send a DIRECTED_PROXY_CAPABILITIES_STATUS message for the deleted + * subnet to the Proxy Client. + */ + if (sub->directed_proxy != BLE_MESH_DIRECTED_PROXY_NOT_SUPPORTED) { + /* Directed Proxy Caps Status must be sent before the subnet is deleted */ + bt_mesh_directed_proxy_server_directed_proxy_caps_send(sub, true); + } + +#if CONFIG_BLE_MESH_SETTINGS + bt_mesh_clear_directed_forwarding_table_data(del_idx); +#endif +#endif + bt_mesh_subnet_del(sub, true); status = STATUS_SUCCESS; +#if CONFIG_BLE_MESH_BRC_SRV + /** + * TODO: When a NetKey is deleted from the NetKey List state, + * and subnet bridge functionality is supported, then all the + * Bridging Table state entries with one of the values of the + * NetKeyIndex1 and NetKeyIndex2 fields that matches the NetKey + * Index of the deleted NetKey are removed. + */ + bt_mesh_delete_netkey_in_bridge_table(del_idx); +#endif /* CONFIG_BLE_MESH_BRC_SRV */ + +#if CONFIG_BLE_MESH_RPR_SRV + bt_mesh_rpr_srv_netkey_del(del_idx); +#endif + send_status: send_net_key_status(model, ctx, del_idx, status); @@ -2489,18 +2505,25 @@ static void node_identity_set(struct bt_mesh_model *model, net_buf_simple_add_u8(&msg, STATUS_INVALID_NETKEY); net_buf_simple_add_le16(&msg, idx); net_buf_simple_add_u8(&msg, node_id); - } else { - net_buf_simple_add_u8(&msg, STATUS_SUCCESS); - net_buf_simple_add_le16(&msg, idx); - + } else { if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { - if (node_id) { + if (node_id == BLE_MESH_NODE_IDENTITY_RUNNING) { +#if CONFIG_BLE_MESH_PRB_SRV + /* If the value of the Node Identity state of the node for + * any subnet is 0x01, then the value of the Private Node + * Identity state shall be Disable (0x00). + */ + disable_all_private_node_identity(); +#endif bt_mesh_proxy_server_identity_start(sub); } else { bt_mesh_proxy_server_identity_stop(sub); } bt_mesh_adv_update(); } + + net_buf_simple_add_u8(&msg, STATUS_SUCCESS); + net_buf_simple_add_le16(&msg, idx); net_buf_simple_add_u8(&msg, sub->node_id); } @@ -2570,6 +2593,7 @@ static void mod_app_bind(struct bt_mesh_model *model, status = mod_bind(mod, key_app_idx); + BT_INFO("bind app key %#x on mode %#x", key_app_idx, mod->id); send_status: BT_DBG("status 0x%02x", status); create_mod_app_status(&msg, mod, vnd, elem_addr, key_app_idx, status, @@ -2805,6 +2829,13 @@ static void friend_set(struct bt_mesh_model *model, if (cfg->frnd == BLE_MESH_FRIEND_DISABLED) { bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY); + +#if CONFIG_BLE_MESH_DF_SRV && CONFIG_BLE_MESH_FRIEND + /* If the value of the friend state of the node is 0x00, + * then the value of the directed friend state shall be 0x00. + */ + bt_mesh_disable_directed_friend_state(ctx->net_idx); +#endif } } @@ -2925,8 +2956,8 @@ static void krp_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, BT_DBG("%u -> %u", sub->kr_phase, phase); if (phase < BLE_MESH_KR_PHASE_2 || phase > BLE_MESH_KR_PHASE_3 || - (sub->kr_phase == BLE_MESH_KR_NORMAL && - phase == BLE_MESH_KR_PHASE_2)) { + (sub->kr_phase == BLE_MESH_KR_NORMAL && + phase == BLE_MESH_KR_PHASE_2)) { BT_WARN("Prohibited transition %u -> %u", sub->kr_phase, phase); return; } @@ -2941,7 +2972,7 @@ static void krp_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, bt_mesh_store_subnet(sub); } - bt_mesh_net_beacon_update(sub); + bt_mesh_net_secure_beacon_update(sub); } else if ((sub->kr_phase == BLE_MESH_KR_PHASE_1 || sub->kr_phase == BLE_MESH_KR_PHASE_2) && phase == BLE_MESH_KR_PHASE_3) { @@ -2950,11 +2981,11 @@ static void krp_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, bt_mesh_net_revoke_keys(sub); if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) || - IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { - friend_cred_refresh(ctx->net_idx); + IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + friend_cred_refresh(sub->net_idx); } - bt_mesh_net_beacon_update(sub); + bt_mesh_net_secure_beacon_update(sub); } send_krp_status(model, ctx, idx, sub->kr_phase, STATUS_SUCCESS); @@ -2968,35 +2999,39 @@ static void krp_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, static uint8_t hb_log(uint16_t val) { - if (!val) { + switch (val) { + case 0x0000: return 0x00; - } else if (val == 0xffff) { - return 0xff; - } else { + case 0xFFFF: + return 0xFF; + default: return 32 - __builtin_clz(val); } } static uint8_t hb_pub_count_log(uint16_t val) { - if (!val) { + switch (val) { + case 0x0000: return 0x00; - } else if (val == 0x01) { + case 0x0001: return 0x01; - } else if (val == 0xffff) { - return 0xff; - } else { + case 0xFFFF: + return 0xFF; + default: return 32 - __builtin_clz(val - 1) + 1; } } static uint16_t hb_pwr2(uint8_t val, uint8_t sub) { - if (!val) { + switch (val) { + case 0x00: return 0x0000; - } else if (val == 0xff || val == 0x11) { - return 0xffff; - } else { + case 0x11: + case 0xFF: + return 0xFFFF; + default: return (1 << (val - sub)); } } @@ -3008,7 +3043,7 @@ struct hb_pub_param { uint8_t ttl; uint16_t feat; uint16_t net_idx; -} __packed; +} __attribute__((packed)); static void hb_pub_send_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, uint8_t status, @@ -3118,7 +3153,7 @@ static void heartbeat_pub_set(struct bt_mesh_model *model, * has been configured for periodic publishing. */ if (param->period_log && param->count_log) { - k_work_submit(&cfg->hb_pub.timer.work); + k_delayed_work_submit(&cfg->hb_pub.timer, K_NO_WAIT); } else { k_delayed_work_cancel(&cfg->hb_pub.timer); } @@ -3137,7 +3172,7 @@ static void heartbeat_pub_set(struct bt_mesh_model *model, */ if (dst != BLE_MESH_ADDR_UNASSIGNED) { if (param->period_log && param->count_log) { - k_work_submit(&cfg->hb_pub.timer.work); + k_delayed_work_submit(&cfg->hb_pub.timer, K_NO_WAIT); } else { k_delayed_work_cancel(&cfg->hb_pub.timer); } @@ -3209,14 +3244,14 @@ static void heartbeat_sub_set(struct bt_mesh_model *model, sub_src, sub_dst, sub_period); if (sub_src != BLE_MESH_ADDR_UNASSIGNED && - !BLE_MESH_ADDR_IS_UNICAST(sub_src)) { + !BLE_MESH_ADDR_IS_UNICAST(sub_src)) { BT_WARN("Prohibited source address"); return; } if (BLE_MESH_ADDR_IS_VIRTUAL(sub_dst) || BLE_MESH_ADDR_IS_RFU(sub_dst) || - (BLE_MESH_ADDR_IS_UNICAST(sub_dst) && - sub_dst != bt_mesh_primary_addr())) { + (BLE_MESH_ADDR_IS_UNICAST(sub_dst) && + sub_dst != bt_mesh_primary_addr())) { BT_WARN("Prohibited destination address"); return; } @@ -3278,53 +3313,53 @@ static void heartbeat_sub_set(struct bt_mesh_model *model, } const struct bt_mesh_model_op bt_mesh_cfg_srv_op[] = { - { OP_DEV_COMP_DATA_GET, 1, dev_comp_data_get }, - { OP_APP_KEY_ADD, 19, app_key_add }, - { OP_APP_KEY_UPDATE, 19, app_key_update }, - { OP_APP_KEY_DEL, 3, app_key_del }, - { OP_APP_KEY_GET, 2, app_key_get }, - { OP_BEACON_GET, 0, beacon_get }, - { OP_BEACON_SET, 1, beacon_set }, - { OP_DEFAULT_TTL_GET, 0, default_ttl_get }, - { OP_DEFAULT_TTL_SET, 1, default_ttl_set }, - { OP_GATT_PROXY_GET, 0, gatt_proxy_get }, - { OP_GATT_PROXY_SET, 1, gatt_proxy_set }, - { OP_NET_TRANSMIT_GET, 0, net_transmit_get }, - { OP_NET_TRANSMIT_SET, 1, net_transmit_set }, - { OP_RELAY_GET, 0, relay_get }, - { OP_RELAY_SET, 2, relay_set }, - { OP_MOD_PUB_GET, 4, mod_pub_get }, - { OP_MOD_PUB_SET, 11, mod_pub_set }, - { OP_MOD_PUB_VA_SET, 24, mod_pub_va_set }, - { OP_MOD_SUB_ADD, 6, mod_sub_add }, - { OP_MOD_SUB_VA_ADD, 20, mod_sub_va_add }, - { OP_MOD_SUB_DEL, 6, mod_sub_del }, - { OP_MOD_SUB_VA_DEL, 20, mod_sub_va_del }, - { OP_MOD_SUB_OVERWRITE, 6, mod_sub_overwrite }, - { OP_MOD_SUB_VA_OVERWRITE, 20, mod_sub_va_overwrite }, - { OP_MOD_SUB_DEL_ALL, 4, mod_sub_del_all }, - { OP_MOD_SUB_GET, 4, mod_sub_get }, - { OP_MOD_SUB_GET_VND, 6, mod_sub_get_vnd }, - { OP_NET_KEY_ADD, 18, net_key_add }, - { OP_NET_KEY_UPDATE, 18, net_key_update }, - { OP_NET_KEY_DEL, 2, net_key_del }, - { OP_NET_KEY_GET, 0, net_key_get }, - { OP_NODE_IDENTITY_GET, 2, node_identity_get }, - { OP_NODE_IDENTITY_SET, 3, node_identity_set }, - { OP_MOD_APP_BIND, 6, mod_app_bind }, - { OP_MOD_APP_UNBIND, 6, mod_app_unbind }, - { OP_SIG_MOD_APP_GET, 4, mod_app_get }, - { OP_VND_MOD_APP_GET, 6, mod_app_get }, - { OP_NODE_RESET, 0, node_reset }, - { OP_FRIEND_GET, 0, friend_get }, - { OP_FRIEND_SET, 1, friend_set }, - { OP_LPN_TIMEOUT_GET, 2, lpn_timeout_get }, - { OP_KRP_GET, 2, krp_get }, - { OP_KRP_SET, 3, krp_set }, - { OP_HEARTBEAT_PUB_GET, 0, heartbeat_pub_get }, - { OP_HEARTBEAT_PUB_SET, 9, heartbeat_pub_set }, - { OP_HEARTBEAT_SUB_GET, 0, heartbeat_sub_get }, - { OP_HEARTBEAT_SUB_SET, 5, heartbeat_sub_set }, + { OP_COMP_DATA_GET, 1, comp_data_get }, + { OP_APP_KEY_ADD, 19, app_key_add }, + { OP_APP_KEY_UPDATE, 19, app_key_update }, + { OP_APP_KEY_DEL, 3, app_key_del }, + { OP_APP_KEY_GET, 2, app_key_get }, + { OP_BEACON_GET, 0, beacon_get }, + { OP_BEACON_SET, 1, beacon_set }, + { OP_DEFAULT_TTL_GET, 0, default_ttl_get }, + { OP_DEFAULT_TTL_SET, 1, default_ttl_set }, + { OP_GATT_PROXY_GET, 0, gatt_proxy_get }, + { OP_GATT_PROXY_SET, 1, gatt_proxy_set }, + { OP_NET_TRANSMIT_GET, 0, net_transmit_get }, + { OP_NET_TRANSMIT_SET, 1, net_transmit_set }, + { OP_RELAY_GET, 0, relay_get }, + { OP_RELAY_SET, 2, relay_set }, + { OP_MOD_PUB_GET, 4, mod_pub_get }, + { OP_MOD_PUB_SET, 11, mod_pub_set }, + { OP_MOD_PUB_VA_SET, 24, mod_pub_va_set }, + { OP_MOD_SUB_ADD, 6, mod_sub_add }, + { OP_MOD_SUB_VA_ADD, 20, mod_sub_va_add }, + { OP_MOD_SUB_DEL, 6, mod_sub_del }, + { OP_MOD_SUB_VA_DEL, 20, mod_sub_va_del }, + { OP_MOD_SUB_OVERWRITE, 6, mod_sub_overwrite }, + { OP_MOD_SUB_VA_OVERWRITE, 20, mod_sub_va_overwrite }, + { OP_MOD_SUB_DEL_ALL, 4, mod_sub_del_all }, + { OP_MOD_SUB_GET, 4, mod_sub_get }, + { OP_MOD_SUB_GET_VND, 6, mod_sub_get_vnd }, + { OP_NET_KEY_ADD, 18, net_key_add }, + { OP_NET_KEY_UPDATE, 18, net_key_update }, + { OP_NET_KEY_DEL, 2, net_key_del }, + { OP_NET_KEY_GET, 0, net_key_get }, + { OP_NODE_IDENTITY_GET, 2, node_identity_get }, + { OP_NODE_IDENTITY_SET, 3, node_identity_set }, + { OP_MOD_APP_BIND, 6, mod_app_bind }, + { OP_MOD_APP_UNBIND, 6, mod_app_unbind }, + { OP_SIG_MOD_APP_GET, 4, mod_app_get }, + { OP_VND_MOD_APP_GET, 6, mod_app_get }, + { OP_NODE_RESET, 0, node_reset }, + { OP_FRIEND_GET, 0, friend_get }, + { OP_FRIEND_SET, 1, friend_set }, + { OP_LPN_TIMEOUT_GET, 2, lpn_timeout_get }, + { OP_KRP_GET, 2, krp_get }, + { OP_KRP_SET, 3, krp_set }, + { OP_HEARTBEAT_PUB_GET, 0, heartbeat_pub_get }, + { OP_HEARTBEAT_PUB_SET, 9, heartbeat_pub_set }, + { OP_HEARTBEAT_SUB_GET, 0, heartbeat_sub_get }, + { OP_HEARTBEAT_SUB_SET, 5, heartbeat_sub_set }, BLE_MESH_MODEL_OP_END, }; @@ -3376,6 +3411,21 @@ static bool conf_is_valid(struct bt_mesh_cfg_srv *cfg) return false; } + if (!IS_ENABLED(CONFIG_BLE_MESH_RELAY)) { + BT_INFO("Relay not supported"); + cfg->relay = BLE_MESH_RELAY_NOT_SUPPORTED; + } + + if (!IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { + BT_INFO("GATT Proxy not supported"); + cfg->gatt_proxy = BLE_MESH_GATT_PROXY_NOT_SUPPORTED; + } + + if (!IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + BT_INFO("Friend not supported"); + cfg->frnd = BLE_MESH_FRIEND_NOT_SUPPORTED; + } + return true; } @@ -3489,8 +3539,6 @@ void bt_mesh_cfg_reset(bool store) struct bt_mesh_cfg_srv *cfg = conf; int i; - BT_DBG("%s", __func__); - if (!cfg) { return; } @@ -3589,13 +3637,13 @@ uint8_t bt_mesh_relay_retransmit_get(void) return 0; } -uint8_t bt_mesh_beacon_get(void) +uint8_t bt_mesh_secure_beacon_get(void) { if (conf) { return conf->beacon; } - return BLE_MESH_BEACON_DISABLED; + return BLE_MESH_SECURE_BEACON_DISABLED; } uint8_t bt_mesh_gatt_proxy_get(void) diff --git a/components/bt/esp_ble_mesh/core/crypto.c b/components/bt/esp_ble_mesh/core/crypto.c index 3f1d18b703a0..9b762fb5ed3c 100644 --- a/components/bt/esp_ble_mesh/core/crypto.c +++ b/components/bt/esp_ble_mesh/core/crypto.c @@ -14,11 +14,16 @@ #include #include #include +#include +#include #include "crypto.h" +#include "mesh/config.h" #include "mesh/common.h" #include "mesh/adapter.h" +#include "mesh_v1.1/utils.h" + #define NET_MIC_LEN(pdu) (((pdu)[1] & 0x80) ? 8 : 4) #define APP_MIC_LEN(aszmic) ((aszmic) ? 8 : 4) @@ -512,7 +517,7 @@ static int bt_mesh_ccm_encrypt(const uint8_t key[16], uint8_t nonce[13], return 0; } -#if defined(CONFIG_BLE_MESH_PROXY) +#if CONFIG_BLE_MESH_PROXY static void create_proxy_nonce(uint8_t nonce[13], const uint8_t *pdu, uint32_t iv_index) { @@ -538,7 +543,7 @@ static void create_proxy_nonce(uint8_t nonce[13], const uint8_t *pdu, /* IV Index */ sys_put_be32(iv_index, &nonce[9]); } -#endif /* PROXY */ +#endif /* CONFIG_BLE_MESH_PROXY */ static void create_net_nonce(uint8_t nonce[13], const uint8_t *pdu, uint32_t iv_index) @@ -593,7 +598,7 @@ int bt_mesh_net_obfuscate(uint8_t *pdu, uint32_t iv_index, } int bt_mesh_net_encrypt(const uint8_t key[16], struct net_buf_simple *buf, - uint32_t iv_index, bool proxy) + uint32_t iv_index, bool proxy, bool proxy_solic) { uint8_t mic_len = NET_MIC_LEN(buf->data); uint8_t nonce[13] = {0}; @@ -603,15 +608,22 @@ int bt_mesh_net_encrypt(const uint8_t key[16], struct net_buf_simple *buf, mic_len); BT_DBG("PDU (len %u) %s", buf->len, bt_hex(buf->data, buf->len)); -#if defined(CONFIG_BLE_MESH_PROXY) +#if CONFIG_BLE_MESH_PROXY if (proxy) { - create_proxy_nonce(nonce, buf->data, iv_index); +#if CONFIG_BLE_MESH_PROXY_SOLIC + if (proxy_solic) { + bt_mesh_create_proxy_solic_nonce(nonce, buf->data, iv_index); + } else +#endif /* CONFIG_BLE_MESH_PROXY_SOLIC */ + { + create_proxy_nonce(nonce, buf->data, iv_index); + } } else { create_net_nonce(nonce, buf->data, iv_index); } -#else +#else /* CONFIG_BLE_MESH_PROXY */ create_net_nonce(nonce, buf->data, iv_index); -#endif +#endif /* CONFIG_BLE_MESH_PROXY */ BT_DBG("Nonce %s", bt_hex(nonce, 13)); @@ -625,7 +637,7 @@ int bt_mesh_net_encrypt(const uint8_t key[16], struct net_buf_simple *buf, } int bt_mesh_net_decrypt(const uint8_t key[16], struct net_buf_simple *buf, - uint32_t iv_index, bool proxy) + uint32_t iv_index, bool proxy, bool proxy_solic) { uint8_t mic_len = NET_MIC_LEN(buf->data); uint8_t nonce[13] = {0}; @@ -634,15 +646,22 @@ int bt_mesh_net_decrypt(const uint8_t key[16], struct net_buf_simple *buf, BT_DBG("iv_index %u, key %s mic_len %u", iv_index, bt_hex(key, 16), mic_len); -#if defined(CONFIG_BLE_MESH_PROXY) +#if CONFIG_BLE_MESH_PROXY if (proxy) { - create_proxy_nonce(nonce, buf->data, iv_index); +#if CONFIG_BLE_MESH_PROXY_SOLIC + if (proxy_solic) { + bt_mesh_create_proxy_solic_nonce(nonce, buf->data, iv_index); + } else +#endif /* CONFIG_BLE_MESH_PROXY_SOLIC */ + { + create_proxy_nonce(nonce, buf->data, iv_index); + } } else { create_net_nonce(nonce, buf->data, iv_index); } -#else +#else /* CONFIG_BLE_MESH_PROXY */ create_net_nonce(nonce, buf->data, iv_index); -#endif +#endif /* CONFIG_BLE_MESH_PROXY */ BT_DBG("Nonce %s", bt_hex(nonce, 13)); @@ -849,9 +868,9 @@ int bt_mesh_prov_encrypt(const uint8_t key[16], uint8_t nonce[13], } #endif -int bt_mesh_beacon_auth(const uint8_t beacon_key[16], uint8_t flags, - const uint8_t net_id[8], uint32_t iv_index, - uint8_t auth[8]) +int bt_mesh_secure_beacon_auth(const uint8_t beacon_key[16], uint8_t flags, + const uint8_t net_id[8], uint32_t iv_index, + uint8_t auth[8]) { uint8_t msg[13] = {0}, tmp[16] = {0}; int err = 0; diff --git a/components/bt/esp_ble_mesh/core/crypto.h b/components/bt/esp_ble_mesh/core/crypto.h index 2039a3e31251..69627c956bb1 100644 --- a/components/bt/esp_ble_mesh/core/crypto.h +++ b/components/bt/esp_ble_mesh/core/crypto.h @@ -70,8 +70,8 @@ static inline int bt_mesh_identity_key(const uint8_t net_key[16], return bt_mesh_id128(net_key, "nkik", identity_key); } -static inline int bt_mesh_beacon_key(const uint8_t net_key[16], - uint8_t beacon_key[16]) +static inline int bt_mesh_secure_beacon_key(const uint8_t net_key[16], + uint8_t beacon_key[16]) { return bt_mesh_id128(net_key, "nkbk", beacon_key); } @@ -80,6 +80,10 @@ int bt_mesh_beacon_auth(const uint8_t beacon_key[16], uint8_t flags, const uint8_t net_id[8], uint32_t iv_index, uint8_t auth[8]); +int bt_mesh_secure_beacon_auth(const uint8_t beacon_key[16], uint8_t flags, + const uint8_t net_id[8], uint32_t iv_index, + uint8_t auth[8]); + static inline int bt_mesh_app_id(const uint8_t app_key[16], uint8_t app_id[1]) { return bt_mesh_k4(app_key, app_id); @@ -133,10 +137,10 @@ int bt_mesh_net_obfuscate(uint8_t *pdu, uint32_t iv_index, const uint8_t privacy_key[16]); int bt_mesh_net_encrypt(const uint8_t key[16], struct net_buf_simple *buf, - uint32_t iv_index, bool proxy); + uint32_t iv_index, bool proxy, bool proxy_solic); int bt_mesh_net_decrypt(const uint8_t key[16], struct net_buf_simple *buf, - uint32_t iv_index, bool proxy); + uint32_t iv_index, bool proxy, bool proxy_solic); int bt_mesh_app_encrypt(const uint8_t key[16], bool dev_key, uint8_t aszmic, struct net_buf_simple *buf, const uint8_t *ad, diff --git a/components/bt/esp_ble_mesh/core/fast_prov.c b/components/bt/esp_ble_mesh/core/fast_prov.c index d00cd2aa1f20..049a12bb438c 100644 --- a/components/bt/esp_ble_mesh/core/fast_prov.c +++ b/components/bt/esp_ble_mesh/core/fast_prov.c @@ -9,6 +9,7 @@ #include "mesh.h" #include "mesh/common.h" +#include "mesh/main.h" #include "access.h" #include "beacon.h" #include "foundation.h" @@ -24,16 +25,27 @@ const uint8_t *bt_mesh_fast_prov_dev_key_get(uint16_t dst) { + const uint8_t *key = NULL; + if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { BT_ERR("Invalid unicast address 0x%04x", dst); return NULL; } - if (dst == bt_mesh_primary_addr()) { + if (bt_mesh_is_provisioner_en() == false) { return bt_mesh.dev_key; } - return bt_mesh_provisioner_dev_key_get(dst); + /* For fast provisioning, try to find the DevKey from + * the database firstly. If the dst is not in the DB, + * then we will directly use the DevKey. + */ + key = bt_mesh_provisioner_dev_key_get(dst); + if (key) { + return key; + } + + return bt_mesh.dev_key; } struct bt_mesh_subnet *bt_mesh_fast_prov_subnet_get(uint16_t net_idx) @@ -153,8 +165,8 @@ uint8_t bt_mesh_set_fast_prov_action(uint8_t action) } if (action == ACTION_ENTER) { - if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { - bt_mesh_beacon_disable(); + if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) { + bt_mesh_secure_beacon_disable(); } if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { bt_mesh_proxy_client_prov_enable(); @@ -167,8 +179,8 @@ uint8_t bt_mesh_set_fast_prov_action(uint8_t action) if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { bt_mesh_proxy_client_prov_disable(); } - if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { - bt_mesh_beacon_enable(); + if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) { + bt_mesh_secure_beacon_enable(); } bt_mesh_atomic_and(bt_mesh.flags, ~(BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV))); bt_mesh_provisioner_fast_prov_enable(false); diff --git a/components/bt/esp_ble_mesh/core/foundation.h b/components/bt/esp_ble_mesh/core/foundation.h index 1d764aa9596f..53b70f698025 100644 --- a/components/bt/esp_ble_mesh/core/foundation.h +++ b/components/bt/esp_ble_mesh/core/foundation.h @@ -15,111 +15,241 @@ extern "C" { #endif -#define OP_APP_KEY_ADD BLE_MESH_MODEL_OP_1(0x00) -#define OP_APP_KEY_UPDATE BLE_MESH_MODEL_OP_1(0x01) -#define OP_DEV_COMP_DATA_STATUS BLE_MESH_MODEL_OP_1(0x02) -#define OP_MOD_PUB_SET BLE_MESH_MODEL_OP_1(0x03) -#define OP_HEALTH_CURRENT_STATUS BLE_MESH_MODEL_OP_1(0x04) -#define OP_HEALTH_FAULT_STATUS BLE_MESH_MODEL_OP_1(0x05) -#define OP_HEARTBEAT_PUB_STATUS BLE_MESH_MODEL_OP_1(0x06) -#define OP_APP_KEY_DEL BLE_MESH_MODEL_OP_2(0x80, 0x00) -#define OP_APP_KEY_GET BLE_MESH_MODEL_OP_2(0x80, 0x01) -#define OP_APP_KEY_LIST BLE_MESH_MODEL_OP_2(0x80, 0x02) -#define OP_APP_KEY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x03) -#define OP_ATTENTION_GET BLE_MESH_MODEL_OP_2(0x80, 0x04) -#define OP_ATTENTION_SET BLE_MESH_MODEL_OP_2(0x80, 0x05) -#define OP_ATTENTION_SET_UNREL BLE_MESH_MODEL_OP_2(0x80, 0x06) -#define OP_ATTENTION_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x07) -#define OP_DEV_COMP_DATA_GET BLE_MESH_MODEL_OP_2(0x80, 0x08) -#define OP_BEACON_GET BLE_MESH_MODEL_OP_2(0x80, 0x09) -#define OP_BEACON_SET BLE_MESH_MODEL_OP_2(0x80, 0x0a) -#define OP_BEACON_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x0b) -#define OP_DEFAULT_TTL_GET BLE_MESH_MODEL_OP_2(0x80, 0x0c) -#define OP_DEFAULT_TTL_SET BLE_MESH_MODEL_OP_2(0x80, 0x0d) -#define OP_DEFAULT_TTL_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x0e) -#define OP_FRIEND_GET BLE_MESH_MODEL_OP_2(0x80, 0x0f) -#define OP_FRIEND_SET BLE_MESH_MODEL_OP_2(0x80, 0x10) -#define OP_FRIEND_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x11) -#define OP_GATT_PROXY_GET BLE_MESH_MODEL_OP_2(0x80, 0x12) -#define OP_GATT_PROXY_SET BLE_MESH_MODEL_OP_2(0x80, 0x13) -#define OP_GATT_PROXY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x14) -#define OP_KRP_GET BLE_MESH_MODEL_OP_2(0x80, 0x15) -#define OP_KRP_SET BLE_MESH_MODEL_OP_2(0x80, 0x16) -#define OP_KRP_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x17) -#define OP_MOD_PUB_GET BLE_MESH_MODEL_OP_2(0x80, 0x18) -#define OP_MOD_PUB_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x19) -#define OP_MOD_PUB_VA_SET BLE_MESH_MODEL_OP_2(0x80, 0x1a) -#define OP_MOD_SUB_ADD BLE_MESH_MODEL_OP_2(0x80, 0x1b) -#define OP_MOD_SUB_DEL BLE_MESH_MODEL_OP_2(0x80, 0x1c) -#define OP_MOD_SUB_DEL_ALL BLE_MESH_MODEL_OP_2(0x80, 0x1d) -#define OP_MOD_SUB_OVERWRITE BLE_MESH_MODEL_OP_2(0x80, 0x1e) -#define OP_MOD_SUB_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x1f) -#define OP_MOD_SUB_VA_ADD BLE_MESH_MODEL_OP_2(0x80, 0x20) -#define OP_MOD_SUB_VA_DEL BLE_MESH_MODEL_OP_2(0x80, 0x21) -#define OP_MOD_SUB_VA_OVERWRITE BLE_MESH_MODEL_OP_2(0x80, 0x22) -#define OP_NET_TRANSMIT_GET BLE_MESH_MODEL_OP_2(0x80, 0x23) -#define OP_NET_TRANSMIT_SET BLE_MESH_MODEL_OP_2(0x80, 0x24) -#define OP_NET_TRANSMIT_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x25) -#define OP_RELAY_GET BLE_MESH_MODEL_OP_2(0x80, 0x26) -#define OP_RELAY_SET BLE_MESH_MODEL_OP_2(0x80, 0x27) -#define OP_RELAY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x28) -#define OP_MOD_SUB_GET BLE_MESH_MODEL_OP_2(0x80, 0x29) -#define OP_MOD_SUB_LIST BLE_MESH_MODEL_OP_2(0x80, 0x2a) -#define OP_MOD_SUB_GET_VND BLE_MESH_MODEL_OP_2(0x80, 0x2b) -#define OP_MOD_SUB_LIST_VND BLE_MESH_MODEL_OP_2(0x80, 0x2c) -#define OP_LPN_TIMEOUT_GET BLE_MESH_MODEL_OP_2(0x80, 0x2d) -#define OP_LPN_TIMEOUT_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x2e) -#define OP_HEALTH_FAULT_CLEAR BLE_MESH_MODEL_OP_2(0x80, 0x2f) -#define OP_HEALTH_FAULT_CLEAR_UNREL BLE_MESH_MODEL_OP_2(0x80, 0x30) -#define OP_HEALTH_FAULT_GET BLE_MESH_MODEL_OP_2(0x80, 0x31) -#define OP_HEALTH_FAULT_TEST BLE_MESH_MODEL_OP_2(0x80, 0x32) -#define OP_HEALTH_FAULT_TEST_UNREL BLE_MESH_MODEL_OP_2(0x80, 0x33) -#define OP_HEALTH_PERIOD_GET BLE_MESH_MODEL_OP_2(0x80, 0x34) -#define OP_HEALTH_PERIOD_SET BLE_MESH_MODEL_OP_2(0x80, 0x35) -#define OP_HEALTH_PERIOD_SET_UNREL BLE_MESH_MODEL_OP_2(0x80, 0x36) -#define OP_HEALTH_PERIOD_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x37) -#define OP_HEARTBEAT_PUB_GET BLE_MESH_MODEL_OP_2(0x80, 0x38) -#define OP_HEARTBEAT_PUB_SET BLE_MESH_MODEL_OP_2(0x80, 0x39) -#define OP_HEARTBEAT_SUB_GET BLE_MESH_MODEL_OP_2(0x80, 0x3a) -#define OP_HEARTBEAT_SUB_SET BLE_MESH_MODEL_OP_2(0x80, 0x3b) -#define OP_HEARTBEAT_SUB_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x3c) -#define OP_MOD_APP_BIND BLE_MESH_MODEL_OP_2(0x80, 0x3d) -#define OP_MOD_APP_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x3e) -#define OP_MOD_APP_UNBIND BLE_MESH_MODEL_OP_2(0x80, 0x3f) -#define OP_NET_KEY_ADD BLE_MESH_MODEL_OP_2(0x80, 0x40) -#define OP_NET_KEY_DEL BLE_MESH_MODEL_OP_2(0x80, 0x41) -#define OP_NET_KEY_GET BLE_MESH_MODEL_OP_2(0x80, 0x42) -#define OP_NET_KEY_LIST BLE_MESH_MODEL_OP_2(0x80, 0x43) -#define OP_NET_KEY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x44) -#define OP_NET_KEY_UPDATE BLE_MESH_MODEL_OP_2(0x80, 0x45) -#define OP_NODE_IDENTITY_GET BLE_MESH_MODEL_OP_2(0x80, 0x46) -#define OP_NODE_IDENTITY_SET BLE_MESH_MODEL_OP_2(0x80, 0x47) -#define OP_NODE_IDENTITY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x48) -#define OP_NODE_RESET BLE_MESH_MODEL_OP_2(0x80, 0x49) -#define OP_NODE_RESET_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x4a) -#define OP_SIG_MOD_APP_GET BLE_MESH_MODEL_OP_2(0x80, 0x4b) -#define OP_SIG_MOD_APP_LIST BLE_MESH_MODEL_OP_2(0x80, 0x4c) -#define OP_VND_MOD_APP_GET BLE_MESH_MODEL_OP_2(0x80, 0x4d) -#define OP_VND_MOD_APP_LIST BLE_MESH_MODEL_OP_2(0x80, 0x4e) - -#define STATUS_SUCCESS 0x00 -#define STATUS_INVALID_ADDRESS 0x01 -#define STATUS_INVALID_MODEL 0x02 -#define STATUS_INVALID_APPKEY 0x03 -#define STATUS_INVALID_NETKEY 0x04 -#define STATUS_INSUFF_RESOURCES 0x05 -#define STATUS_IDX_ALREADY_STORED 0x06 -#define STATUS_NVAL_PUB_PARAM 0x07 -#define STATUS_NOT_SUB_MOD 0x08 -#define STATUS_STORAGE_FAIL 0x09 -#define STATUS_FEAT_NOT_SUPP 0x0a -#define STATUS_CANNOT_UPDATE 0x0b -#define STATUS_CANNOT_REMOVE 0x0c -#define STATUS_CANNOT_BIND 0x0d -#define STATUS_TEMP_STATE_CHG_FAIL 0x0e -#define STATUS_CANNOT_SET 0x0f -#define STATUS_UNSPECIFIED 0x10 -#define STATUS_INVALID_BINDING 0x11 +#define OP_APP_KEY_ADD BLE_MESH_MODEL_OP_1(0x00) +#define OP_APP_KEY_UPDATE BLE_MESH_MODEL_OP_1(0x01) +#define OP_COMP_DATA_STATUS BLE_MESH_MODEL_OP_1(0x02) +#define OP_MOD_PUB_SET BLE_MESH_MODEL_OP_1(0x03) +#define OP_HEALTH_CURRENT_STATUS BLE_MESH_MODEL_OP_1(0x04) +#define OP_HEALTH_FAULT_STATUS BLE_MESH_MODEL_OP_1(0x05) +#define OP_HEARTBEAT_PUB_STATUS BLE_MESH_MODEL_OP_1(0x06) +#define OP_APP_KEY_DEL BLE_MESH_MODEL_OP_2(0x80, 0x00) +#define OP_APP_KEY_GET BLE_MESH_MODEL_OP_2(0x80, 0x01) +#define OP_APP_KEY_LIST BLE_MESH_MODEL_OP_2(0x80, 0x02) +#define OP_APP_KEY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x03) +#define OP_ATTENTION_GET BLE_MESH_MODEL_OP_2(0x80, 0x04) +#define OP_ATTENTION_SET BLE_MESH_MODEL_OP_2(0x80, 0x05) +#define OP_ATTENTION_SET_UNREL BLE_MESH_MODEL_OP_2(0x80, 0x06) +#define OP_ATTENTION_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x07) +#define OP_COMP_DATA_GET BLE_MESH_MODEL_OP_2(0x80, 0x08) +#define OP_BEACON_GET BLE_MESH_MODEL_OP_2(0x80, 0x09) +#define OP_BEACON_SET BLE_MESH_MODEL_OP_2(0x80, 0x0A) +#define OP_BEACON_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x0B) +#define OP_DEFAULT_TTL_GET BLE_MESH_MODEL_OP_2(0x80, 0x0C) +#define OP_DEFAULT_TTL_SET BLE_MESH_MODEL_OP_2(0x80, 0x0D) +#define OP_DEFAULT_TTL_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x0E) +#define OP_FRIEND_GET BLE_MESH_MODEL_OP_2(0x80, 0x0F) +#define OP_FRIEND_SET BLE_MESH_MODEL_OP_2(0x80, 0x10) +#define OP_FRIEND_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x11) +#define OP_GATT_PROXY_GET BLE_MESH_MODEL_OP_2(0x80, 0x12) +#define OP_GATT_PROXY_SET BLE_MESH_MODEL_OP_2(0x80, 0x13) +#define OP_GATT_PROXY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x14) +#define OP_KRP_GET BLE_MESH_MODEL_OP_2(0x80, 0x15) +#define OP_KRP_SET BLE_MESH_MODEL_OP_2(0x80, 0x16) +#define OP_KRP_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x17) +#define OP_MOD_PUB_GET BLE_MESH_MODEL_OP_2(0x80, 0x18) +#define OP_MOD_PUB_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x19) +#define OP_MOD_PUB_VA_SET BLE_MESH_MODEL_OP_2(0x80, 0x1A) +#define OP_MOD_SUB_ADD BLE_MESH_MODEL_OP_2(0x80, 0x1B) +#define OP_MOD_SUB_DEL BLE_MESH_MODEL_OP_2(0x80, 0x1C) +#define OP_MOD_SUB_DEL_ALL BLE_MESH_MODEL_OP_2(0x80, 0x1D) +#define OP_MOD_SUB_OVERWRITE BLE_MESH_MODEL_OP_2(0x80, 0x1E) +#define OP_MOD_SUB_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x1F) +#define OP_MOD_SUB_VA_ADD BLE_MESH_MODEL_OP_2(0x80, 0x20) +#define OP_MOD_SUB_VA_DEL BLE_MESH_MODEL_OP_2(0x80, 0x21) +#define OP_MOD_SUB_VA_OVERWRITE BLE_MESH_MODEL_OP_2(0x80, 0x22) +#define OP_NET_TRANSMIT_GET BLE_MESH_MODEL_OP_2(0x80, 0x23) +#define OP_NET_TRANSMIT_SET BLE_MESH_MODEL_OP_2(0x80, 0x24) +#define OP_NET_TRANSMIT_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x25) +#define OP_RELAY_GET BLE_MESH_MODEL_OP_2(0x80, 0x26) +#define OP_RELAY_SET BLE_MESH_MODEL_OP_2(0x80, 0x27) +#define OP_RELAY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x28) +#define OP_MOD_SUB_GET BLE_MESH_MODEL_OP_2(0x80, 0x29) +#define OP_MOD_SUB_LIST BLE_MESH_MODEL_OP_2(0x80, 0x2A) +#define OP_MOD_SUB_GET_VND BLE_MESH_MODEL_OP_2(0x80, 0x2B) +#define OP_MOD_SUB_LIST_VND BLE_MESH_MODEL_OP_2(0x80, 0x2C) +#define OP_LPN_TIMEOUT_GET BLE_MESH_MODEL_OP_2(0x80, 0x2D) +#define OP_LPN_TIMEOUT_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x2E) +#define OP_HEALTH_FAULT_CLEAR BLE_MESH_MODEL_OP_2(0x80, 0x2F) +#define OP_HEALTH_FAULT_CLEAR_UNREL BLE_MESH_MODEL_OP_2(0x80, 0x30) +#define OP_HEALTH_FAULT_GET BLE_MESH_MODEL_OP_2(0x80, 0x31) +#define OP_HEALTH_FAULT_TEST BLE_MESH_MODEL_OP_2(0x80, 0x32) +#define OP_HEALTH_FAULT_TEST_UNREL BLE_MESH_MODEL_OP_2(0x80, 0x33) +#define OP_HEALTH_PERIOD_GET BLE_MESH_MODEL_OP_2(0x80, 0x34) +#define OP_HEALTH_PERIOD_SET BLE_MESH_MODEL_OP_2(0x80, 0x35) +#define OP_HEALTH_PERIOD_SET_UNREL BLE_MESH_MODEL_OP_2(0x80, 0x36) +#define OP_HEALTH_PERIOD_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x37) +#define OP_HEARTBEAT_PUB_GET BLE_MESH_MODEL_OP_2(0x80, 0x38) +#define OP_HEARTBEAT_PUB_SET BLE_MESH_MODEL_OP_2(0x80, 0x39) +#define OP_HEARTBEAT_SUB_GET BLE_MESH_MODEL_OP_2(0x80, 0x3A) +#define OP_HEARTBEAT_SUB_SET BLE_MESH_MODEL_OP_2(0x80, 0x3B) +#define OP_HEARTBEAT_SUB_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x3C) +#define OP_MOD_APP_BIND BLE_MESH_MODEL_OP_2(0x80, 0x3D) +#define OP_MOD_APP_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x3E) +#define OP_MOD_APP_UNBIND BLE_MESH_MODEL_OP_2(0x80, 0x3F) +#define OP_NET_KEY_ADD BLE_MESH_MODEL_OP_2(0x80, 0x40) +#define OP_NET_KEY_DEL BLE_MESH_MODEL_OP_2(0x80, 0x41) +#define OP_NET_KEY_GET BLE_MESH_MODEL_OP_2(0x80, 0x42) +#define OP_NET_KEY_LIST BLE_MESH_MODEL_OP_2(0x80, 0x43) +#define OP_NET_KEY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x44) +#define OP_NET_KEY_UPDATE BLE_MESH_MODEL_OP_2(0x80, 0x45) +#define OP_NODE_IDENTITY_GET BLE_MESH_MODEL_OP_2(0x80, 0x46) +#define OP_NODE_IDENTITY_SET BLE_MESH_MODEL_OP_2(0x80, 0x47) +#define OP_NODE_IDENTITY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x48) +#define OP_NODE_RESET BLE_MESH_MODEL_OP_2(0x80, 0x49) +#define OP_NODE_RESET_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x4A) +#define OP_SIG_MOD_APP_GET BLE_MESH_MODEL_OP_2(0x80, 0x4B) +#define OP_SIG_MOD_APP_LIST BLE_MESH_MODEL_OP_2(0x80, 0x4C) +#define OP_VND_MOD_APP_GET BLE_MESH_MODEL_OP_2(0x80, 0x4D) +#define OP_VND_MOD_APP_LIST BLE_MESH_MODEL_OP_2(0x80, 0x4E) + +#define OP_REMOTE_PROV_SCAN_CAPS_GET BLE_MESH_MODEL_OP_2(0x80, 0x4F) +#define OP_REMOTE_PROV_SCAN_CAPS_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x50) +#define OP_REMOTE_PROV_SCAN_GET BLE_MESH_MODEL_OP_2(0x80, 0x51) +#define OP_REMOTE_PROV_SCAN_START BLE_MESH_MODEL_OP_2(0x80, 0x52) +#define OP_REMOTE_PROV_SCAN_STOP BLE_MESH_MODEL_OP_2(0x80, 0x53) +#define OP_REMOTE_PROV_SCAN_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x54) +#define OP_REMOTE_PROV_SCAN_REPORT BLE_MESH_MODEL_OP_2(0x80, 0x55) +#define OP_REMOTE_PROV_EXTENDED_SCAN_START BLE_MESH_MODEL_OP_2(0x80, 0x56) +#define OP_REMOTE_PROV_EXTENDED_SCAN_REPORT BLE_MESH_MODEL_OP_2(0x80, 0x57) +#define OP_REMOTE_PROV_LINK_GET BLE_MESH_MODEL_OP_2(0x80, 0x58) +#define OP_REMOTE_PROV_LINK_OPEN BLE_MESH_MODEL_OP_2(0x80, 0x59) +#define OP_REMOTE_PROV_LINK_CLOSE BLE_MESH_MODEL_OP_2(0x80, 0x5A) +#define OP_REMOTE_PROV_LINK_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x5B) +#define OP_REMOTE_PROV_LINK_REPORT BLE_MESH_MODEL_OP_2(0x80, 0x5C) +#define OP_REMOTE_PROV_PDU_SEND BLE_MESH_MODEL_OP_2(0x80, 0x5D) +#define OP_REMOTE_PROV_PDU_OUTBOUND_REPORT BLE_MESH_MODEL_OP_2(0x80, 0x5E) +#define OP_REMOTE_PROV_PDU_REPORT BLE_MESH_MODEL_OP_2(0x80, 0x5F) + +#define OP_DIRECTED_CONTROL_GET BLE_MESH_MODEL_OP_2(0x80, 0x7B) +#define OP_DIRECTED_CONTROL_SET BLE_MESH_MODEL_OP_2(0x80, 0x7C) +#define OP_DIRECTED_CONTROL_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x7D) +#define OP_PATH_METRIC_GET BLE_MESH_MODEL_OP_2(0x80, 0x7E) +#define OP_PATH_METRIC_SET BLE_MESH_MODEL_OP_2(0x80, 0x7F) +#define OP_PATH_METRIC_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x80) +#define OP_DISCOVERY_TABLE_CAPS_GET BLE_MESH_MODEL_OP_2(0x80, 0x81) +#define OP_DISCOVERY_TABLE_CAPS_SET BLE_MESH_MODEL_OP_2(0x80, 0x82) +#define OP_DISCOVERY_TABLE_CAPS_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x83) +#define OP_FORWARDING_TABLE_ADD BLE_MESH_MODEL_OP_2(0x80, 0x84) +#define OP_FORWARDING_TABLE_DEL BLE_MESH_MODEL_OP_2(0x80, 0x85) +#define OP_FORWARDING_TABLE_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x86) +#define OP_FORWARDING_TABLE_DEPS_ADD BLE_MESH_MODEL_OP_2(0x80, 0x87) +#define OP_FORWARDING_TABLE_DEPS_DEL BLE_MESH_MODEL_OP_2(0x80, 0x88) +#define OP_FORWARDING_TABLE_DEPS_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x89) +#define OP_FORWARDING_TABLE_DEPS_GET BLE_MESH_MODEL_OP_2(0x80, 0x8A) +#define OP_FORWARDING_TABLE_DEPS_GET_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x8B) +#define OP_FORWARDING_TABLE_ENTRIES_CNT_GET BLE_MESH_MODEL_OP_2(0x80, 0x8C) +#define OP_FORWARDING_TABLE_ENTRIES_CNT_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x8D) +#define OP_FORWARDING_TABLE_ENTRIES_GET BLE_MESH_MODEL_OP_2(0x80, 0x8E) +#define OP_FORWARDING_TABLE_ENTRIES_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x8F) +#define OP_WANTED_LANES_GET BLE_MESH_MODEL_OP_2(0x80, 0x90) +#define OP_WANTED_LANES_SET BLE_MESH_MODEL_OP_2(0x80, 0x91) +#define OP_WANTED_LANES_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x92) +#define OP_TWO_WAY_PATH_GET BLE_MESH_MODEL_OP_2(0x80, 0x93) +#define OP_TWO_WAY_PATH_SET BLE_MESH_MODEL_OP_2(0x80, 0x94) +#define OP_TWO_WAY_PATH_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x95) +#define OP_PATH_ECHO_INTERVAL_GET BLE_MESH_MODEL_OP_2(0x80, 0x96) +#define OP_PATH_ECHO_INTERVAL_SET BLE_MESH_MODEL_OP_2(0x80, 0x97) +#define OP_PATH_ECHO_INTERVAL_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x98) +#define OP_DIRECTED_NET_TRANSMIT_GET BLE_MESH_MODEL_OP_2(0x80, 0x99) +#define OP_DIRECTED_NET_TRANSMIT_SET BLE_MESH_MODEL_OP_2(0x80, 0x9A) +#define OP_DIRECTED_NET_TRANSMIT_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x9B) +#define OP_DIRECTED_RELAY_RETRANSMIT_GET BLE_MESH_MODEL_OP_2(0x80, 0x9C) +#define OP_DIRECTED_RELAY_RETRANSMIT_SET BLE_MESH_MODEL_OP_2(0x80, 0x9D) +#define OP_DIRECTED_RELAY_RETRANSMIT_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x9E) +#define OP_RSSI_THRESHOLD_GET BLE_MESH_MODEL_OP_2(0x80, 0x9F) +#define OP_RSSI_THRESHOLD_SET BLE_MESH_MODEL_OP_2(0x80, 0xA0) +#define OP_RSSI_THRESHOLD_STATUS BLE_MESH_MODEL_OP_2(0x80, 0xA1) +#define OP_DIRECTED_PATHS_GET BLE_MESH_MODEL_OP_2(0x80, 0xA2) +#define OP_DIRECTED_PATHS_STATUS BLE_MESH_MODEL_OP_2(0x80, 0xA3) +#define OP_DIRECTED_PUB_POLICY_GET BLE_MESH_MODEL_OP_2(0x80, 0xA4) +#define OP_DIRECTED_PUB_POLICY_SET BLE_MESH_MODEL_OP_2(0x80, 0xA5) +#define OP_DIRECTED_PUB_POLICY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0xA6) +#define OP_PATH_DISCOVERY_TIMING_CTL_GET BLE_MESH_MODEL_OP_2(0x80, 0xA7) +#define OP_PATH_DISCOVERY_TIMING_CTL_SET BLE_MESH_MODEL_OP_2(0x80, 0xA8) +#define OP_PATH_DISCOVERY_TIMING_CTL_STATUS BLE_MESH_MODEL_OP_2(0x80, 0xA9) +#define OP_DIRECTED_CTL_NET_TRANSMIT_GET BLE_MESH_MODEL_OP_2(0x80, 0xAB) +#define OP_DIRECTED_CTL_NET_TRANSMIT_SET BLE_MESH_MODEL_OP_2(0x80, 0xAC) +#define OP_DIRECTED_CTL_NET_TRANSMIT_STATUS BLE_MESH_MODEL_OP_2(0x80, 0xAD) +#define OP_DIRECTED_CTL_RELAY_RETRANSMIT_GET BLE_MESH_MODEL_OP_2(0x80, 0xAE) +#define OP_DIRECTED_CTL_RELAY_RETRANSMIT_SET BLE_MESH_MODEL_OP_2(0x80, 0xAF) +#define OP_DIRECTED_CTL_RELAY_RETRANSMIT_STATUS BLE_MESH_MODEL_OP_2(0x80, 0xB0) + +#define OP_SUBNET_BRIDGE_GET BLE_MESH_MODEL_OP_2(0x80, 0xB1) +#define OP_SUBNET_BRIDGE_SET BLE_MESH_MODEL_OP_2(0x80, 0xB2) +#define OP_SUBNET_BRIDGE_STATUS BLE_MESH_MODEL_OP_2(0x80, 0xB3) +#define OP_BRIDGING_TABLE_ADD BLE_MESH_MODEL_OP_2(0x80, 0xB4) +#define OP_BRIDGING_TABLE_REMOVE BLE_MESH_MODEL_OP_2(0x80, 0xB5) +#define OP_BRIDGING_TABLE_STATUS BLE_MESH_MODEL_OP_2(0x80, 0xB6) +#define OP_BRIDGED_SUBNETS_GET BLE_MESH_MODEL_OP_2(0x80, 0xB7) +#define OP_BRIDGED_SUBNETS_LIST BLE_MESH_MODEL_OP_2(0x80, 0xB8) +#define OP_BRIDGING_TABLE_GET BLE_MESH_MODEL_OP_2(0x80, 0xB9) +#define OP_BRIDGING_TABLE_LIST BLE_MESH_MODEL_OP_2(0x80, 0xBA) +#define OP_BRIDGING_TABLE_SIZE_GET BLE_MESH_MODEL_OP_2(0x80, 0xBB) +#define OP_BRIDGING_TABLE_SIZE_STATUS BLE_MESH_MODEL_OP_2(0x80, 0xBC) + +#define OP_PRIVATE_BEACON_GET BLE_MESH_MODEL_OP_2(0x80, 0x60) +#define OP_PRIVATE_BEACON_SET BLE_MESH_MODEL_OP_2(0x80, 0x61) +#define OP_PRIVATE_BEACON_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x62) +#define OP_PRIVATE_GATT_PROXY_GET BLE_MESH_MODEL_OP_2(0x80, 0x63) +#define OP_PRIVATE_GATT_PROXY_SET BLE_MESH_MODEL_OP_2(0x80, 0x64) +#define OP_PRIVATE_GATT_PROXY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x65) +#define OP_PRIVATE_NODE_IDENTITY_GET BLE_MESH_MODEL_OP_2(0x80, 0x66) +#define OP_PRIVATE_NODE_IDENTITY_SET BLE_MESH_MODEL_OP_2(0x80, 0x67) +#define OP_PRIVATE_NODE_IDENTITY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x68) + +#define OP_ON_DEMAND_PRIVATE_PROXY_GET BLE_MESH_MODEL_OP_2(0x80, 0x69) +#define OP_ON_DEMAND_PRIVATE_PROXY_SET BLE_MESH_MODEL_OP_2(0x80, 0x6A) +#define OP_ON_DEMAND_PRIVATE_PROXY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x6B) + +#define OP_SAR_TRANSMITTER_GET BLE_MESH_MODEL_OP_2(0x80, 0x6C) +#define OP_SAR_TRANSMITTER_SET BLE_MESH_MODEL_OP_2(0x80, 0x6D) +#define OP_SAR_TRANSMITTER_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x6E) +#define OP_SAR_RECEIVER_GET BLE_MESH_MODEL_OP_2(0x80, 0x6F) +#define OP_SAR_RECEIVER_SET BLE_MESH_MODEL_OP_2(0x80, 0x70) +#define OP_SAR_RECEIVER_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x71) + +#define OP_OPCODES_AGG_SEQUENCE BLE_MESH_MODEL_OP_2(0x80, 0x72) +#define OP_OPCODES_AGG_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x73) + +#define OP_LARGE_COMP_DATA_GET BLE_MESH_MODEL_OP_2(0x80, 0x74) +#define OP_LARGE_COMP_DATA_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x75) +#define OP_MODELS_METADATA_GET BLE_MESH_MODEL_OP_2(0x80, 0x76) +#define OP_MODELS_METADATA_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x77) + +#define OP_SOLIC_PDU_RPL_ITEMS_CLEAR BLE_MESH_MODEL_OP_2(0x80, 0x78) +#define OP_SOLIC_PDU_RPL_ITEMS_CLEAR_UNACK BLE_MESH_MODEL_OP_2(0x80, 0x79) +#define OP_SOLIC_PDU_RPL_ITEMS_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x7A) + +#define STATUS_SUCCESS 0x00 +#define STATUS_INVALID_ADDRESS 0x01 +#define STATUS_INVALID_MODEL 0x02 +#define STATUS_INVALID_APPKEY 0x03 +#define STATUS_INVALID_NETKEY 0x04 +#define STATUS_INSUFF_RESOURCES 0x05 +#define STATUS_IDX_ALREADY_STORED 0x06 +#define STATUS_NVAL_PUB_PARAM 0x07 +#define STATUS_NOT_SUB_MOD 0x08 +#define STATUS_STORAGE_FAIL 0x09 +#define STATUS_FEAT_NOT_SUPP 0x0A +#define STATUS_CANNOT_UPDATE 0x0B +#define STATUS_CANNOT_REMOVE 0x0C +#define STATUS_CANNOT_BIND 0x0D +#define STATUS_TEMP_STATE_CHG_FAIL 0x0E +#define STATUS_CANNOT_SET 0x0F +#define STATUS_UNSPECIFIED 0x10 +#define STATUS_INVALID_BINDING 0x11 +#define STATUS_INVALID_PATH_ENTRY 0x12 +#define STATUS_CANNOT_GET 0x13 +#define STATUS_OBSOLETE_INFO 0x14 +#define STATUS_INVALID_BEARER 0x15 + +/* Defines the status codes for Opcodes Aggregator messages. */ +#define AGG_STATUS_SUCCESS 0x00 +#define AGG_STATUS_INVALID_ADDRESS 0x01 +#define AGG_STATUS_INVALID_MODEL 0x02 +#define AGG_STATUS_WRONG_ACCESS_KEY 0x03 +#define AGG_STATUS_WRONG_OPCODE 0x04 +#define AGG_STATUS_MSG_NOT_UNDERSTOOD 0x05 enum { BLE_MESH_VA_CHANGED, /* Label information changed */ @@ -152,7 +282,7 @@ uint8_t bt_mesh_net_transmit_get(void); uint8_t bt_mesh_relay_get(void); uint8_t bt_mesh_friend_get(void); uint8_t bt_mesh_relay_retransmit_get(void); -uint8_t bt_mesh_beacon_get(void); +uint8_t bt_mesh_secure_beacon_get(void); uint8_t bt_mesh_gatt_proxy_get(void); uint8_t bt_mesh_default_ttl_get(void); diff --git a/components/bt/esp_ble_mesh/core/friend.c b/components/bt/esp_ble_mesh/core/friend.c index 0637b17d1f31..fca3cf1358a4 100644 --- a/components/bt/esp_ble_mesh/core/friend.c +++ b/components/bt/esp_ble_mesh/core/friend.c @@ -18,20 +18,28 @@ #include "friend.h" #include "foundation.h" #include "mesh/main.h" +#include "mesh/common.h" #include "pvnr_mgmt.h" +#include "mesh_v1.1/utils.h" + #ifdef CONFIG_BLE_MESH_FRIEND /* We reserve one extra buffer for each friendship, since we need to be able * to resend the last sent PDU, which sits separately outside of the queue. */ #define FRIEND_BUF_COUNT ((CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE + 1) * \ - CONFIG_BLE_MESH_FRIEND_LPN_COUNT) + CONFIG_BLE_MESH_FRIEND_LPN_COUNT) -#define FRIEND_ADV(buf) CONTAINER_OF(BLE_MESH_ADV(buf), struct friend_adv, adv) +#define FRIEND_ADV(buf) CONTAINER_OF(BLE_MESH_ADV(buf), struct friend_adv, adv) /* PDUs from Friend to the LPN should only be transmitted once with the * smallest possible interval (20ms). + * + * Spec v1.1, Section 3.4.6: + * When transmitting a Network PDU that is tagged as friendship, the + * Advertising Bearer Network Interface shall transmit the Network PDU + * over the advertising bearer only once. */ #define FRIEND_XMIT BLE_MESH_TRANSMIT(0, 20) @@ -70,15 +78,7 @@ static bool friend_init = false; static struct bt_mesh_subnet *friend_subnet_get(uint16_t net_idx) { - struct bt_mesh_subnet *sub = NULL; - - 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 = bt_mesh_provisioner_subnet_get(net_idx); - } - - return sub; + return bt_mesh_subnet_get(net_idx); } static struct bt_mesh_adv *adv_alloc(int id) @@ -166,6 +166,15 @@ static void friend_clear(struct bt_mesh_friend *frnd, uint8_t reason) if (friend_cb) { friend_cb(false, frnd->lpn, reason); } + +#if CONFIG_BLE_MESH_DF_SRV + struct bt_mesh_subnet *sub = friend_subnet_get(frnd->net_idx); + if (sub && sub->directed_friend == BLE_MESH_DIRECTED_FRIEND_ENABLED) { + if (bt_mesh_directed_update_dependent_node(sub, 0, frnd->lpn, frnd->num_elem)) { + BT_ERR("Friend failed to update dependent node"); + } + } +#endif /* CONFIG_BLE_MESH_DF_SRV */ } friend_cred_del(frnd->net_idx, frnd->lpn); @@ -296,7 +305,10 @@ int bt_mesh_friend_clear(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) return 0; } - tx.ctx->send_ttl = BLE_MESH_TTL_MAX; + tx.ctx->send_ttl = BLE_MESH_TTL_MAX; + tx.ctx->send_cred = BLE_MESH_FLOODING_CRED; + /* Tag with immutable-credentials */ + tx.ctx->send_tag = BLE_MESH_TAG_IMMUTABLE_CRED; cfm.lpn_addr = msg->lpn_addr; cfm.lpn_counter = msg->lpn_counter; @@ -309,6 +321,19 @@ int bt_mesh_friend_clear(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) return 0; } +static bool friend_sub_exist(struct bt_mesh_friend *frnd, uint16_t addr) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(frnd->sub_list); i++) { + if (frnd->sub_list[i] == addr) { + return true; + } + } + + return false; +} + static void friend_sub_add(struct bt_mesh_friend *frnd, uint16_t addr) { int i; @@ -342,8 +367,7 @@ static struct net_buf *create_friend_pdu(struct bt_mesh_friend *frnd, struct net_buf *buf = NULL; buf = bt_mesh_adv_create_from_pool(&friend_buf_pool, adv_alloc, - BLE_MESH_ADV_DATA, - FRIEND_XMIT, K_NO_WAIT); + BLE_MESH_ADV_DATA, K_NO_WAIT); if (!buf) { return NULL; } @@ -380,7 +404,6 @@ static int unseg_app_sdu_unpack(struct bt_mesh_friend *frnd, struct unseg_app_sdu_meta *meta) { uint16_t app_idx = FRIEND_ADV(buf)->app_idx; - uint8_t role = 0U; int err = 0; meta->subnet = friend_subnet_get(frnd->net_idx); @@ -389,13 +412,10 @@ static int unseg_app_sdu_unpack(struct bt_mesh_friend *frnd, return -EINVAL; } - role = (IS_ENABLED(CONFIG_BLE_MESH_NODE) && - bt_mesh_is_provisioned()) ? NODE : PROVISIONER; - meta->is_dev_key = (app_idx == BLE_MESH_KEY_DEV); bt_mesh_net_header_parse(&buf->b, &meta->net); - err = bt_mesh_app_key_get(meta->subnet, app_idx, &meta->key, - &meta->aid, role, meta->net.ctx.addr); + err = bt_mesh_upper_key_get(meta->subnet, app_idx, &meta->key, + &meta->aid, meta->net.ctx.addr); if (err) { BT_ERR("Failed to get AppKey"); return err; @@ -532,7 +552,7 @@ static int encrypt_friend_pdu(struct bt_mesh_friend *frnd, struct net_buf *buf, buf->data[0] = (nid | (iv_index & 1) << 7); - if (bt_mesh_net_encrypt(enc, &buf->b, iv_index, false)) { + if (bt_mesh_net_encrypt(enc, &buf->b, iv_index, false, false)) { BT_ERR("Encrypting failed"); return -EINVAL; } @@ -657,12 +677,29 @@ int bt_mesh_friend_sub_add(struct bt_mesh_net_rx *rx, xact = net_buf_simple_pull_u8(buf); while (buf->len >= 2U) { - friend_sub_add(frnd, net_buf_simple_pull_be16(buf)); + uint16_t addr = net_buf_simple_pull_be16(buf); + + if (!BLE_MESH_ADDR_IS_GROUP(addr) && + !BLE_MESH_ADDR_IS_VIRTUAL(addr) && + !BLE_MESH_ADDR_IS_FIXED_GROUP(addr)) { + BT_WARN("Invalid friend sub addr 0x%04x to add", addr); + continue; + } + + if (friend_sub_exist(frnd, addr)) { + continue; + } + + friend_sub_add(frnd, addr); } enqueue_sub_cfm(frnd, xact); +#if CONFIG_BLE_MESH_DF_SRV + return bt_mesh_directed_friend_solicitation(frnd, rx->sub); +#else return 0; +#endif } int bt_mesh_friend_sub_rem(struct bt_mesh_net_rx *rx, @@ -692,7 +729,16 @@ int bt_mesh_friend_sub_rem(struct bt_mesh_net_rx *rx, xact = net_buf_simple_pull_u8(buf); while (buf->len >= 2U) { - friend_sub_rem(frnd, net_buf_simple_pull_be16(buf)); + uint16_t addr = net_buf_simple_pull_be16(buf); + + if (!BLE_MESH_ADDR_IS_GROUP(addr) && + !BLE_MESH_ADDR_IS_VIRTUAL(addr) && + !BLE_MESH_ADDR_IS_FIXED_GROUP(addr)) { + BT_WARN("Invalid friend sub addr 0x%04x to remove", addr); + continue; + } + + friend_sub_rem(frnd, addr); } enqueue_sub_cfm(frnd, xact); @@ -808,10 +854,13 @@ static const struct bt_mesh_send_cb clear_sent_cb = { static void send_friend_clear(struct bt_mesh_friend *frnd) { struct bt_mesh_msg_ctx ctx = { - .net_idx = frnd->net_idx, - .app_idx = BLE_MESH_KEY_UNUSED, - .addr = frnd->clear.frnd, - .send_ttl = BLE_MESH_TTL_MAX, + .net_idx = frnd->net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = frnd->clear.frnd, + .send_ttl = BLE_MESH_TTL_MAX, + .send_cred = BLE_MESH_FLOODING_CRED, + /* Tag with immutable-credentials */ + .send_tag = BLE_MESH_TAG_IMMUTABLE_CRED, }; struct bt_mesh_net_tx tx = { .sub = friend_subnet_get(frnd->net_idx), @@ -824,8 +873,6 @@ static void send_friend_clear(struct bt_mesh_friend *frnd) .lpn_counter = sys_cpu_to_be16(frnd->lpn_counter), }; - BT_DBG("%s", __func__); - if (!tx.sub) { BT_ERR("Invalid subnet for Friend Clear"); return; @@ -870,8 +917,6 @@ int bt_mesh_friend_clear_cfm(struct bt_mesh_net_rx *rx, struct bt_mesh_friend *frnd = NULL; uint16_t lpn_addr = 0U, lpn_counter = 0U; - BT_DBG("%s", __func__); - if (buf->len < sizeof(*msg)) { BT_WARN("Too short Friend Clear Confirm (len %d)", buf->len); return -EINVAL; @@ -909,17 +954,15 @@ static void enqueue_offer(struct bt_mesh_friend *frnd, int8_t rssi) NET_BUF_SIMPLE_DEFINE(sdu, 1 + sizeof(*off)); struct net_buf *buf = NULL; - BT_DBG("%s", __func__); - net_buf_simple_reserve(&sdu, 1); off = net_buf_simple_add(&sdu, sizeof(*off)); - off->recv_win = CONFIG_BLE_MESH_FRIEND_RECV_WIN, - off->queue_size = CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE, - off->sub_list_size = ARRAY_SIZE(frnd->sub_list), - off->rssi = rssi, - off->frnd_counter = sys_cpu_to_be16(frnd->counter); + off->recv_win = CONFIG_BLE_MESH_FRIEND_RECV_WIN; + off->queue_size = CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE; + off->sub_list_size = ARRAY_SIZE(frnd->sub_list); + off->rssi = rssi; + off->frnd_counter = sys_cpu_to_be16(frnd->counter); buf = encode_friend_ctl(frnd, TRANS_CTL_OP_FRIEND_OFFER, &sdu); if (!buf) { @@ -956,8 +999,8 @@ static int32_t offer_delay(struct bt_mesh_friend *frnd, int8_t rssi, uint8_t cri int32_t delay = 0; BT_INFO("ReceiveWindowFactor %u ReceiveWindow %u RSSIFactor %u RSSI %d", - fact[RECV_WIN_FACT(crit)], RECV_WIN, - fact[RSSI_FACT(crit)], rssi); + fact[RECV_WIN_FACT(crit)], RECV_WIN, + fact[RSSI_FACT(crit)], rssi); /* Delay = ReceiveWindowFactor * ReceiveWindow - RSSIFactor * RSSI */ delay = (int32_t)fact[RECV_WIN_FACT(crit)] * RECV_WIN; @@ -1059,7 +1102,7 @@ int bt_mesh_friend_req(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) * begin sending Friend Clear messages to that unicast address. */ if (BLE_MESH_ADDR_IS_UNICAST(frnd->clear.frnd) && - !bt_mesh_elem_find(frnd->clear.frnd)) { + !bt_mesh_elem_find(frnd->clear.frnd)) { clear_procedure_start(frnd); } @@ -1245,7 +1288,7 @@ static void friend_timeout(struct k_work *work) send_last: frnd->pending_req = 0U; frnd->pending_buf = 1U; - bt_mesh_adv_send(frnd->last, &buf_sent_cb, frnd); + bt_mesh_adv_send(frnd->last, FRIEND_XMIT, &buf_sent_cb, frnd); } void bt_mesh_friend_set_cb(void (*cb)(bool establish, uint16_t lpn_addr, uint8_t reason)) @@ -1513,6 +1556,29 @@ bool bt_mesh_friend_match(uint16_t net_idx, uint16_t addr) return false; } +bool bt_mesh_friend_unicast_match(uint16_t net_idx, uint16_t addr, uint8_t *selem) +{ + int i; + + if (!BLE_MESH_ADDR_IS_UNICAST(addr) || selem == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return false; + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + + if (frnd->established && net_idx == frnd->net_idx && + BLE_MESH_ADDR_IS_UNICAST(frnd->lpn) && + (addr >= frnd->lpn && addr < (frnd->lpn + frnd->num_elem))) { + *selem = frnd->num_elem - 1; + return true; + } + } + + return false; +} + static bool friend_queue_has_space(struct bt_mesh_friend *frnd, uint16_t addr, const uint64_t *seq_auth, uint8_t seg_count) { @@ -1623,8 +1689,8 @@ void bt_mesh_friend_enqueue_rx(struct bt_mesh_net_rx *rx, int i; if (!rx->friend_match || - (rx->ctx.recv_ttl <= 1U && rx->net_if != BLE_MESH_NET_IF_LOCAL) || - bt_mesh_friend_get() != BLE_MESH_FRIEND_ENABLED) { + (rx->ctx.recv_ttl <= 1U && rx->net_if != BLE_MESH_NET_IF_LOCAL) || + bt_mesh_friend_get() != BLE_MESH_FRIEND_ENABLED) { return; } @@ -1659,7 +1725,7 @@ bool bt_mesh_friend_enqueue_tx(struct bt_mesh_net_tx *tx, int i; if (!bt_mesh_friend_match(tx->sub->net_idx, tx->ctx->addr) || - bt_mesh_friend_get() != BLE_MESH_FRIEND_ENABLED) { + bt_mesh_friend_get() != BLE_MESH_FRIEND_ENABLED) { return matched; } @@ -1692,8 +1758,6 @@ void bt_mesh_friend_clear_incomplete(struct bt_mesh_subnet *sub, uint16_t src, { int i; - BT_DBG("%s", __func__); - for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; int j; diff --git a/components/bt/esp_ble_mesh/core/friend.h b/components/bt/esp_ble_mesh/core/friend.h index 2855081bf57e..2e583f357d22 100644 --- a/components/bt/esp_ble_mesh/core/friend.h +++ b/components/bt/esp_ble_mesh/core/friend.h @@ -22,6 +22,7 @@ enum bt_mesh_friend_pdu_type { }; bool bt_mesh_friend_match(uint16_t net_idx, uint16_t addr); +bool bt_mesh_friend_unicast_match(uint16_t net_idx, uint16_t addr, uint8_t *selem); struct bt_mesh_friend *bt_mesh_friend_find(uint16_t net_idx, uint16_t lpn_addr, bool valid, bool established); diff --git a/components/bt/esp_ble_mesh/core/health_cli.c b/components/bt/esp_ble_mesh/core/health_cli.c index 6ddedef55f51..1e03d93513c4 100644 --- a/components/bt/esp_ble_mesh/core/health_cli.c +++ b/components/bt/esp_ble_mesh/core/health_cli.c @@ -31,40 +31,17 @@ static const bt_mesh_client_op_pair_t health_op_pair[] = { static bt_mesh_mutex_t health_client_lock; -static inline void bt_mesh_health_client_mutex_new(void) -{ - if (!health_client_lock.mutex) { - bt_mesh_mutex_create(&health_client_lock); - } -} - -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_health_client_mutex_free(void) -{ - bt_mesh_mutex_free(&health_client_lock); -} -#endif /* CONFIG_BLE_MESH_DEINIT */ - -static inline void bt_mesh_health_client_lock(void) -{ - bt_mesh_mutex_lock(&health_client_lock); -} - -static inline void bt_mesh_health_client_unlock(void) -{ - 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_model *model = NULL; struct bt_mesh_msg_ctx ctx = {0}; uint32_t opcode = 0U; BT_WARN("Receive health status message timeout"); - bt_mesh_health_client_lock(); + bt_mesh_mutex_lock(&health_client_lock); timer = CONTAINER_OF(work, struct k_delayed_work, work); @@ -73,15 +50,14 @@ static void timeout_handler(struct k_work *work) if (node) { memcpy(&ctx, &node->ctx, sizeof(ctx)); opcode = node->opcode; + model = node->model; 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); + opcode, BTC_BLE_MESH_EVT_HEALTH_CLIENT_TIMEOUT, model, &ctx, NULL, 0); } } - bt_mesh_health_client_unlock(); - - return; + bt_mesh_mutex_unlock(&health_client_lock); } static void health_client_recv_status(struct bt_mesh_model *model, @@ -101,7 +77,7 @@ static void health_client_recv_status(struct bt_mesh_model *model, buf.data = (uint8_t *)status; buf.len = (uint16_t)len; - bt_mesh_health_client_lock(); + bt_mesh_mutex_lock(&health_client_lock); node = bt_mesh_is_client_recv_publish_msg(model, ctx, &buf, true); if (!node) { @@ -131,7 +107,7 @@ static void health_client_recv_status(struct bt_mesh_model *model, } } - bt_mesh_health_client_unlock(); + bt_mesh_mutex_unlock(&health_client_lock); switch (ctx->recv_op) { case OP_HEALTH_FAULT_STATUS: { @@ -324,24 +300,25 @@ static int health_cli_init(struct bt_mesh_model *model) return -EINVAL; } - if (!client->internal_data) { - internal = bt_mesh_calloc(sizeof(health_internal_data_t)); - if (!internal) { - BT_ERR("%s, Out of memory", __func__); - return -ENOMEM; - } - - sys_slist_init(&internal->queue); + if (client->internal_data) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } - client->model = model; - client->op_pair_size = ARRAY_SIZE(health_op_pair); - client->op_pair = health_op_pair; - client->internal_data = internal; - } else { - bt_mesh_client_clear_list(client->internal_data); + internal = bt_mesh_calloc(sizeof(health_internal_data_t)); + if (!internal) { + BT_ERR("%s, Out of memory", __func__); + return -ENOMEM; } - bt_mesh_health_client_mutex_new(); + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(health_op_pair); + client->op_pair = health_op_pair; + client->internal_data = internal; + + bt_mesh_mutex_create(&health_client_lock); return 0; } @@ -373,7 +350,7 @@ static int health_cli_deinit(struct bt_mesh_model *model) client->internal_data = NULL; } - bt_mesh_health_client_mutex_free(); + bt_mesh_mutex_free(&health_client_lock); return 0; } diff --git a/components/bt/esp_ble_mesh/core/health_srv.c b/components/bt/esp_ble_mesh/core/health_srv.c index 70a3e3fbfc88..a27e4547b211 100644 --- a/components/bt/esp_ble_mesh/core/health_srv.c +++ b/components/bt/esp_ble_mesh/core/health_srv.c @@ -182,8 +182,6 @@ static void health_fault_test(struct bt_mesh_model *model, uint16_t company_id = 0U; uint8_t test_id = 0U; - BT_DBG("%s", __func__); - if (!srv) { BT_ERR("No Health Server context provided"); return; @@ -241,8 +239,6 @@ static void attention_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - BT_DBG("%s", __func__); - send_attention_status(model, ctx); } @@ -263,8 +259,6 @@ static void attention_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - BT_DBG("%s", __func__); - health_set_attention(model, ctx, buf); if (ctx->recv_op == OP_ATTENTION_SET) { @@ -289,8 +283,6 @@ static void health_period_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - BT_DBG("%s", __func__); - send_health_period_status(model, ctx); } @@ -315,8 +307,6 @@ static void health_period_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - BT_DBG("%s", __func__); - health_set_period(model, ctx, buf); if (ctx->recv_op == OP_HEALTH_PERIOD_SET) { @@ -367,8 +357,6 @@ static int health_pub_update(struct bt_mesh_model *model) struct bt_mesh_model_pub *pub = model->pub; size_t count = 0U; - BT_DBG("%s", __func__); - if (!pub || !pub->msg) { BT_ERR("Invalid health publication context"); return -EINVAL; @@ -416,8 +404,6 @@ static void attention_off(struct k_work *work) struct bt_mesh_health_srv *srv = CONTAINER_OF(work, struct bt_mesh_health_srv, attn_timer.work); - BT_DBG("%s", __func__); - if (!srv) { BT_ERR("No Health Server context provided"); return; diff --git a/components/bt/esp_ble_mesh/core/include/mesh/access.h b/components/bt/esp_ble_mesh/core/include/mesh/access.h index 3e826cea2632..1b78cbd416ba 100644 --- a/components/bt/esp_ble_mesh/core/include/mesh/access.h +++ b/components/bt/esp_ble_mesh/core/include/mesh/access.h @@ -4,7 +4,7 @@ /* * SPDX-FileCopyrightText: 2017 Intel Corporation - * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileContributor: 2018-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -26,16 +26,20 @@ extern "C" { #endif -#define BLE_MESH_CID_NVAL 0xFFFF +#define BLE_MESH_CID_NVAL 0xFFFF -#define BLE_MESH_ADDR_UNASSIGNED 0x0000 -#define BLE_MESH_ADDR_ALL_NODES 0xffff -#define BLE_MESH_ADDR_PROXIES 0xfffc -#define BLE_MESH_ADDR_FRIENDS 0xfffd -#define BLE_MESH_ADDR_RELAYS 0xfffe +#define BLE_MESH_ADDR_UNASSIGNED 0x0000 -#define BLE_MESH_KEY_UNUSED 0xffff -#define BLE_MESH_KEY_DEV 0xfffe +#define BLE_MESH_ADDR_IPT_ROUTERS 0xFFF9 +#define BLE_MESH_ADDR_IPT_NODES 0xFFFA +#define BLE_MESH_ADDR_DIRECTS 0xFFFB +#define BLE_MESH_ADDR_PROXIES 0xFFFC +#define BLE_MESH_ADDR_FRIENDS 0xFFFD +#define BLE_MESH_ADDR_RELAYS 0xFFFE +#define BLE_MESH_ADDR_ALL_NODES 0xFFFF + +#define BLE_MESH_KEY_UNUSED 0xFFFF +#define BLE_MESH_KEY_DEV 0xFFFE /** Helper to define a mesh element within an array. * @@ -75,6 +79,24 @@ struct bt_mesh_elem { #define BLE_MESH_MODEL_ID_CFG_CLI 0x0001 #define BLE_MESH_MODEL_ID_HEALTH_SRV 0x0002 #define BLE_MESH_MODEL_ID_HEALTH_CLI 0x0003 +#define BLE_MESH_MODEL_ID_RPR_SRV 0x0004 +#define BLE_MESH_MODEL_ID_RPR_CLI 0x0005 +#define BLE_MESH_MODEL_ID_DF_SRV 0x0006 +#define BLE_MESH_MODEL_ID_DF_CLI 0x0007 +#define BLE_MESH_MODEL_ID_BRC_SRV 0x0008 +#define BLE_MESH_MODEL_ID_BRC_CLI 0x0009 +#define BLE_MESH_MODEL_ID_PRB_SRV 0x000A +#define BLE_MESH_MODEL_ID_PRB_CLI 0x000B +#define BLE_MESH_MODEL_ID_ODP_SRV 0x000C +#define BLE_MESH_MODEL_ID_ODP_CLI 0x000D +#define BLE_MESH_MODEL_ID_SAR_SRV 0x000E +#define BLE_MESH_MODEL_ID_SAR_CLI 0x000F +#define BLE_MESH_MODEL_ID_AGG_SRV 0x0010 +#define BLE_MESH_MODEL_ID_AGG_CLI 0x0011 +#define BLE_MESH_MODEL_ID_LCD_SRV 0x0012 +#define BLE_MESH_MODEL_ID_LCD_CLI 0x0013 +#define BLE_MESH_MODEL_ID_SRPL_SRV 0x0014 +#define BLE_MESH_MODEL_ID_SRPL_CLI 0x0015 /* Models from the Mesh Model Specification */ #define BLE_MESH_MODEL_ID_GEN_ONOFF_SRV 0x1000 @@ -129,6 +151,8 @@ struct bt_mesh_elem { #define BLE_MESH_MODEL_ID_LIGHT_LC_SRV 0x130f #define BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV 0x1310 #define BLE_MESH_MODEL_ID_LIGHT_LC_CLI 0x1311 +#define BLE_MESH_MODEL_ID_MBT_SRV 0x1400 +#define BLE_MESH_MODEL_ID_MBT_CLI 0x1401 /** Message sending context. */ struct bt_mesh_msg_ctx { @@ -144,28 +168,42 @@ struct bt_mesh_msg_ctx { /** Destination address of a received message. Not used for sending. */ uint16_t recv_dst; - /** RSSI of received packet. Not used for sending. */ - int8_t recv_rssi; + /** RSSI of a received message. Not used for sending. */ + int8_t recv_rssi; + + /** Opcode of a received message. Not used for sending. */ + uint32_t recv_op; /** Received TTL value. Not used for sending. */ - uint8_t recv_ttl: 7; + uint8_t recv_ttl; + + /** Security credentials of a received message. Not used for sending. */ + uint8_t recv_cred; - /** Force sending reliably by using segment acknowledgement */ - uint8_t send_rel: 1; + /** Tag of a received message. Not used for sending. */ + uint8_t recv_tag; + + /** Force sending reliably by using segment acknowledgement. */ + uint8_t send_rel:1 __attribute__((deprecated)); + + /** Size of TransMIC when sending a Segmented Access message. */ + uint8_t send_szmic:1; /** TTL, or BLE_MESH_TTL_DEFAULT for default TTL. */ uint8_t send_ttl; - /** Change by Espressif, opcode of a received message. - * Not used for sending message. */ - uint32_t recv_op; + /** Security credentials used for sending the message */ + uint8_t send_cred; + + /** Tag used for sending the message. */ + uint8_t send_tag; /** Change by Espressif, model corresponds to the message */ - struct bt_mesh_model *model; + struct bt_mesh_model *model __attribute__((deprecated)); /** Change by Espressif, if the message is sent by a server * model. Not used for receiving message. */ - bool srv_send; + bool srv_send __attribute__((deprecated)); }; struct bt_mesh_model_op { @@ -194,16 +232,29 @@ struct bt_mesh_model_op { /** Length of a short Mesh MIC. */ #define BLE_MESH_MIC_SHORT 4 + /** Length of a long Mesh MIC. */ #define BLE_MESH_MIC_LONG 8 +/* Using 4-octets TransMIC for a segmented message */ +#define BLE_MESH_SEG_SZMIC_SHORT 0 + +/* Using 8-octets TransMIC for a segmented message */ +#define BLE_MESH_SEG_SZMIC_LONG 1 + +/** Maximum length of payload with short MIC */ +#define BLE_MESH_MAX_PDU_LEN_WITH_SMIC 380 + +/** Maximum length of payload with long MIC */ +#define BLE_MESH_MAX_PDU_LEN_WITH_LMIC 376 + /** @def BLE_MESH_MODEL_OP_LEN * * @brief Helper to determine the length of an opcode. * * @param _op Opcode. */ -#define BLE_MESH_MODEL_OP_LEN(_op) ((_op) <= 0xff ? 1 : (_op) <= 0xffff ? 2 : 3) +#define BLE_MESH_MODEL_OP_LEN(_op) ((_op) <= 0xff ? 1 : (_op) <= 0xffff ? 2 : 3) /** @def BLE_MESH_MODEL_BUF_LEN * @@ -364,7 +415,8 @@ struct bt_mesh_model_pub { uint16_t addr; /**< Publish Address. */ uint16_t key:12, /**< Publish AppKey Index. */ cred:1, /**< Friendship Credentials Flag. */ - send_rel:1; /**< Force reliable sending (segment acks) */ + send_rel:1, /**< Force reliable sending (segment acks) */ + send_szmic:1; /**< Size of TransMIC when sending Segmented Access message */ uint8_t ttl; /**< Publish Time to Live. */ uint8_t retransmit; /**< Retransmit Count & Interval Steps. */ @@ -375,6 +427,10 @@ struct bt_mesh_model_pub { uint32_t period_start; /**< Start of the current period. */ +#if CONFIG_BLE_MESH_DF_SRV + uint8_t directed_pub_policy; /**< Directed publish policy */ +#endif + /** @brief Publication buffer, containing the publication message. * * This will get correctly created when the publication context @@ -405,7 +461,7 @@ struct bt_mesh_model_pub { struct k_delayed_work timer; /* Change by Espressif, role of the device going to publish messages */ - uint8_t dev_role; + uint8_t dev_role __attribute__((deprecated)); }; /** @def BLE_MESH_MODEL_PUB_DEFINE diff --git a/components/bt/esp_ble_mesh/core/include/mesh/adapter.h b/components/bt/esp_ble_mesh/core/include/mesh/adapter.h index 47d4b550d095..431fc72cfecd 100644 --- a/components/bt/esp_ble_mesh/core/include/mesh/adapter.h +++ b/components/bt/esp_ble_mesh/core/include/mesh/adapter.h @@ -113,20 +113,41 @@ extern "C" { #define BLE_MESH_DATA_UUID128_ALL 0x07 /* 128-bit UUID, all listed */ #define BLE_MESH_DATA_NAME_SHORTENED 0x08 /* Shortened name */ #define BLE_MESH_DATA_NAME_COMPLETE 0x09 /* Complete name */ -#define BLE_MESH_DATA_TX_POWER 0x0a /* Tx Power */ +#define BLE_MESH_DATA_TX_POWER 0x0A /* Tx Power */ +#define BLE_MESH_DATA_CLASS_OF_DEVICE 0x0D /* Class of Device */ +#define BLE_MESH_DATA_SIMP_PAIR_HASH_C 0x0E /* Simple Pairing Hash C */ +#define BLE_MESH_DATA_SIMP_PAIR_RAND_R 0x0F /* Simple Pairing Randomizer R */ +#define BLE_MESH_DATA_SEC_MANAGER_TK_VAL 0x10 /* Security Manager TK Value */ +#define BLE_MESH_DATA_SEC_MANAGER_OOB_FLAG 0x11 /* Security Manager Out of Band Flags */ +#define BLE_MESH_DATA_SLAVE_CONN_INT_RANGE 0x12 /* Slave Connection Interval Range */ #define BLE_MESH_DATA_SOLICIT16 0x14 /* Solicit UUIDs, 16-bit */ #define BLE_MESH_DATA_SOLICIT128 0x15 /* Solicit UUIDs, 128-bit */ #define BLE_MESH_DATA_SVC_DATA16 0x16 /* Service data, 16-bit UUID */ +#define BLE_MESH_DATA_PUBLIC_TARGET_ADDR 0x17 /* Public Target Address */ +#define BLE_MESH_DATA_RANDOM_TARGET_ADDR 0x18 /* Random Target Address */ #define BLE_MESH_DATA_GAP_APPEARANCE 0x19 /* GAP appearance */ -#define BLE_MESH_DATA_SOLICIT32 0x1f /* Solicit UUIDs, 32-bit */ +#define BLE_MESH_DATA_ADV_INTERVAL 0x1A /* Advertising Interval */ +#define BLE_MESH_DATA_LE_DEVICE_ADDR 0x1B /* LE Bluetooth Device Address */ +#define BLE_MESH_DATA_LE_ROLE 0x1C /* LE Role */ +#define BLE_MESH_DATA_SIMP_PAIR_HASH_C_256 0x1D /* Simple Pairing Hash C-256 */ +#define BLE_MESH_DATA_SIMP_PAIR_RAND_R_256 0x1E /* Simple Pairing Randomizer R-256 */ +#define BLE_MESH_DATA_SOLICIT32 0x1F /* Solicit UUIDs, 32-bit */ #define BLE_MESH_DATA_SVC_DATA32 0x20 /* Service data, 32-bit UUID */ #define BLE_MESH_DATA_SVC_DATA128 0x21 /* Service data, 128-bit UUID */ +#define BLE_MESH_DATA_LE_SEC_CONN_CFM_VAL 0x22 /* LE Secure Connections Confirmation Value */ +#define BLE_MESH_DATA_LE_SEC_CONN_RAND_VAL 0x23 /* LE Secure Connections Random Value */ #define BLE_MESH_DATA_URI 0x24 /* URI */ +#define BLE_MESH_DATA_INDOOR_POSITION 0x25 /* Indoor Positioning */ +#define BLE_MESH_DATA_TRANS_DISC_DATA 0x26 /* Transport Discovery Data */ +#define BLE_MESH_DATA_LE_SUPPORT_FEAT 0x27 /* LE Supported Features */ +#define BLE_MESH_DATA_CHAN_MAP_UPDATE_IND 0x28 /* Channel Map Update Indication */ #define BLE_MESH_DATA_MESH_PROV 0x29 /* Mesh Provisioning PDU */ -#define BLE_MESH_DATA_MESH_MESSAGE 0x2a /* Mesh Networking PDU */ -#define BLE_MESH_DATA_MESH_BEACON 0x2b /* Mesh Beacon */ - -#define BLE_MESH_DATA_MANUFACTURER_DATA 0xff /* Manufacturer Specific Data */ +#define BLE_MESH_DATA_MESH_MESSAGE 0x2A /* Mesh Networking PDU */ +#define BLE_MESH_DATA_MESH_BEACON 0x2B /* Mesh Beacon */ +#define BLE_MESH_DATA_BIGINFO 0x2C /* BIGInf */ +#define BLE_MESH_DATA_BROADCAST_COD 0x2D /* Broadcast_Cod */ +#define BLE_MESH_DATA_3D_INFO_DATA 0x3D /* 3D Information Data */ +#define BLE_MESH_DATA_MANUFACTURER_DATA 0xFF /* Manufacturer Specific Data */ #define BLE_MESH_AD_LIMITED 0x01 /* Limited Discoverable */ #define BLE_MESH_AD_GENERAL 0x02 /* General Discoverable */ @@ -442,9 +463,11 @@ struct bt_mesh_conn { * @param rssi Strength of advertiser signal. * @param adv_type Type of advertising response from advertiser. * @param data Buffer containing advertiser data. + * @param scan_rsp_len Scan Response data length. */ typedef void bt_mesh_scan_cb_t(const bt_mesh_addr_t *addr, int8_t rssi, - uint8_t adv_type, struct net_buf_simple *buf); + uint8_t adv_type, struct net_buf_simple *buf, + uint8_t scan_rsp_len); /* @typedef bt_mesh_dh_key_cb_t * @brief Callback type for DH Key calculation. @@ -452,11 +475,11 @@ typedef void bt_mesh_scan_cb_t(const bt_mesh_addr_t *addr, int8_t rssi, * Used to notify of the calculated DH Key. * * @param key Public key. - * @param idx Provisioning link index, only used by Provisioner. + * @param user_data User data. * * @return The DH Key, or NULL in case of failure. */ -typedef void (*bt_mesh_dh_key_cb_t)(const uint8_t key[32], const uint8_t idx); +typedef void (*bt_mesh_dh_key_cb_t)(const uint8_t key[32], void *user_data); /** @typedef bt_mesh_gatt_attr_func_t * @brief Attribute iterator callback. @@ -767,7 +790,7 @@ const uint8_t *bt_mesh_pub_key_get(void); bool bt_mesh_check_public_key(const uint8_t key[64]); -int bt_mesh_dh_key_gen(const uint8_t remote_pk[64], bt_mesh_dh_key_cb_t cb, const uint8_t idx); +int bt_mesh_dh_key_gen(const uint8_t remote_pub_key[64], uint8_t dhkey[32]); int bt_mesh_encrypt_le(const uint8_t key[16], const uint8_t plaintext[16], uint8_t enc_data[16]); diff --git a/components/bt/esp_ble_mesh/core/include/mesh/hci.h b/components/bt/esp_ble_mesh/core/include/mesh/hci.h index c48ffe4db50c..9be507d95e49 100644 --- a/components/bt/esp_ble_mesh/core/include/mesh/hci.h +++ b/components/bt/esp_ble_mesh/core/include/mesh/hci.h @@ -107,19 +107,19 @@ struct bt_mesh_hci_cp_set_adv_param { bt_mesh_addr_t direct_addr; uint8_t channel_map; uint8_t filter_policy; -} __packed; +} __attribute__((packed)); #define BLE_MESH_HCI_OP_SET_ADV_DATA BLE_MESH_OP(BLE_MESH_OGF_LE, 0x0008) struct bt_mesh_hci_cp_set_adv_data { uint8_t len; uint8_t data[31]; -} __packed; +} __attribute__((packed)); #define BLE_MESH_HCI_OP_SET_SCAN_RSP_DATA BLE_MESH_OP(BLE_MESH_OGF_LE, 0x0009) struct bt_mesh_hci_cp_set_scan_rsp_data { uint8_t len; uint8_t data[31]; -} __packed; +} __attribute__((packed)); /* Added by Espressif */ extern struct bt_mesh_dev bt_mesh_dev; diff --git a/components/bt/esp_ble_mesh/core/include/mesh/main.h b/components/bt/esp_ble_mesh/core/include/mesh/main.h index bc04033732ea..35e554458ae2 100644 --- a/components/bt/esp_ble_mesh/core/include/mesh/main.h +++ b/components/bt/esp_ble_mesh/core/include/mesh/main.h @@ -54,7 +54,9 @@ typedef enum { BLE_MESH_PROV_OOB_NFC = BIT(4), BLE_MESH_PROV_OOB_NUMBER = BIT(5), BLE_MESH_PROV_OOB_STRING = BIT(6), - /* 7 - 10 are reserved */ + BLE_MESH_PROV_CERT_BASED = BIT(7), + BLE_MESH_PROV_RECORDS = BIT(8), + /* 9 - 10 are reserved */ BLE_MESH_PROV_OOB_ON_BOX = BIT(11), BLE_MESH_PROV_OOB_IN_BOX = BIT(12), BLE_MESH_PROV_OOB_ON_PAPER = BIT(13), @@ -62,7 +64,12 @@ typedef enum { BLE_MESH_PROV_OOB_ON_DEV = BIT(15), } bt_mesh_prov_oob_info_t; +#if CONFIG_BLE_MESH_PROV_EPA +#define BLE_MESH_PROV_STATIC_OOB_MAX_LEN 32 +#else /* CONFIG_BLE_MESH_PROV_EPA */ #define BLE_MESH_PROV_STATIC_OOB_MAX_LEN 16 +#endif /* CONFIG_BLE_MESH_PROV_EPA */ + #define BLE_MESH_PROV_OUTPUT_OOB_MAX_LEN 8 #define BLE_MESH_PROV_INPUT_OOB_MAX_LEN 8 @@ -92,6 +99,9 @@ struct bt_mesh_prov { */ void (*oob_pub_key_cb)(void); + /** OOB type */ + uint8_t oob_type; + /** Static OOB value */ const uint8_t *static_val; /** Static OOB value length */ @@ -161,8 +171,9 @@ struct bt_mesh_prov { * link has been closed on the given provisioning bearer. * * @param bearer Provisioning bearer. + * @param reason Provisioning link close reason(disconnect reason) */ - void (*link_close)(bt_mesh_prov_bearer_t bearer); + void (*link_close)(bt_mesh_prov_bearer_t bearer, uint8_t reason); /** @brief Provisioning is complete. * @@ -181,7 +192,7 @@ struct bt_mesh_prov { /** @brief Node has been reset. * * This callback notifies the application that the local node - * has been reset and needs to be reprovisioned. The node will + * has been reset and needs to be re-provisioned. The node will * not automatically advertise as unprovisioned, rather the * bt_mesh_prov_enable() API needs to be called to enable * unprovisioned advertising on one or more provisioning bearers. @@ -305,6 +316,41 @@ struct bt_mesh_prov { void (*prov_complete)(uint16_t node_idx, const uint8_t device_uuid[16], uint16_t unicast_addr, uint8_t element_num, uint16_t netkey_idx); + + /** @brief Provisioner start certificate based provisioning. + * + * This callback notifies the application that certificate based provisioning + * has started. Provisioner need to send prov_records_get, prov_record_request + * or prov_invite message. + * + * @param link_idx: The provisioning link index + */ + void (*cert_based_prov_start)(uint16_t link_idx); + + /** @brief Provisioner receive provisioning records list from device. + * + * This callback notifies the application that provisioner has received + * provisioning records list, and will send the list to application. + * + * @param link_idx: The provisioning link index + * @param data: The provisioning records list. + */ + void (*records_list_get)(uint16_t link_idx, struct net_buf_simple *data); + + /** @brief Provisioner receive complete provisioning record from device. + * + * This callback notifies the application that provisioner has received + * complete provisioning record, and will send the record to application. + * + * @param status: Status + * @param link_idx: The provisioning link index. + * @param record_id: The provisioning record index. + * @param frag_offset: The starting of the fragment. + * @param total_len: The length of the record. + * @param record: The data of the record. + */ + void (*prov_record_recv_comp)(uint8_t status, uint16_t link_idx, uint16_t record_id, + uint16_t frag_offset, uint16_t total_len, uint8_t *record); #endif /* CONFIG_BLE_MESH_PROVISIONER */ }; @@ -415,36 +461,82 @@ int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers); */ /* Primary Network Key index */ -#define BLE_MESH_NET_PRIMARY 0x000 +#define BLE_MESH_NET_PRIMARY 0x000 + +#define BLE_MESH_RELAY_DISABLED 0x00 +#define BLE_MESH_RELAY_ENABLED 0x01 +#define BLE_MESH_RELAY_NOT_SUPPORTED 0x02 + +#define BLE_MESH_SECURE_BEACON_DISABLED 0x00 +#define BLE_MESH_SECURE_BEACON_ENABLED 0x01 + +#define BLE_MESH_GATT_PROXY_DISABLED 0x00 +#define BLE_MESH_GATT_PROXY_ENABLED 0x01 +#define BLE_MESH_GATT_PROXY_NOT_SUPPORTED 0x02 + +#define BLE_MESH_FRIEND_DISABLED 0x00 +#define BLE_MESH_FRIEND_ENABLED 0x01 +#define BLE_MESH_FRIEND_NOT_SUPPORTED 0x02 + +#define BLE_MESH_NODE_IDENTITY_STOPPED 0x00 +#define BLE_MESH_NODE_IDENTITY_RUNNING 0x01 +#define BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED 0x02 + +#define BLE_MESH_PRIVATE_BEACON_DISABLED 0x00 +#define BLE_MESH_PRIVATE_BEACON_ENABLED 0x01 + +#define BLE_MESH_RANDOM_UPDATE_EVERY_PRIVATE_BEACON 0x00 + +#define BLE_MESH_PRIVATE_GATT_PROXY_DISABLED 0x00 +#define BLE_MESH_PRIVATE_GATT_PROXY_ENABLED 0x01 +#define BLE_MESH_PRIVATE_GATT_PROXY_NOT_SUPPORTED 0x02 + +#define BLE_MESH_PRIVATE_NODE_IDENTITY_STOPPED 0x00 +#define BLE_MESH_PRIVATE_NODE_IDENTITY_RUNNING 0x01 +#define BLE_MESH_PRIVATE_NODE_IDENTITY_NOT_SUPPORTED 0x02 + +#define BLE_MESH_SUBNET_BRIDGE_DISABLED 0x00 +#define BLE_MESH_SUBNET_BRIDGE_ENABLED 0x01 + +#define BLE_MESH_PRIVATE_NET_ID_CANNOT_EN_ON_DEMAND 0x00 + +#define BLE_MESH_DIRECTED_FORWARDING_DISABLED 0x00 +#define BLE_MESH_DIRECTED_FORWARDING_ENABLED 0x01 + +#define BLE_MESH_DIRECTED_RELAY_DISABLED 0x00 +#define BLE_MESH_DIRECTED_RELAY_ENABLED 0x01 + +#define BLE_MESH_DIRECTED_PROXY_DISABLED 0x00 +#define BLE_MESH_DIRECTED_PROXY_ENABLED 0x01 +#define BLE_MESH_DIRECTED_PROXY_NOT_SUPPORTED 0x02 -#define BLE_MESH_RELAY_DISABLED 0x00 -#define BLE_MESH_RELAY_ENABLED 0x01 -#define BLE_MESH_RELAY_NOT_SUPPORTED 0x02 +#define BLE_MESH_DIRECTED_PROXY_USE_DEF_DISABLED 0x00 +#define BLE_MESH_DIRECTED_PROXY_USE_DEF_ENABLED 0x01 +#define BLE_MESH_DIRECTED_PROXY_USE_DEF_NOT_SUPPORTED 0x02 -#define BLE_MESH_BEACON_DISABLED 0x00 -#define BLE_MESH_BEACON_ENABLED 0x01 +#define BLE_MESH_DIRECTED_FRIEND_DISABLED 0x00 +#define BLE_MESH_DIRECTED_FRIEND_ENABLED 0x01 +#define BLE_MESH_DIRECTED_FRIEND_NOT_SUPPORTED 0x02 -#define BLE_MESH_GATT_PROXY_DISABLED 0x00 -#define BLE_MESH_GATT_PROXY_ENABLED 0x01 -#define BLE_MESH_GATT_PROXY_NOT_SUPPORTED 0x02 +#define BLE_MESH_DIRECTED_PUB_POLICY_FLOODING 0x00 +#define BLE_MESH_DIRECTED_PUB_POLICY_FORWARD 0x01 -#define BLE_MESH_FRIEND_DISABLED 0x00 -#define BLE_MESH_FRIEND_ENABLED 0x01 -#define BLE_MESH_FRIEND_NOT_SUPPORTED 0x02 +#define BLE_MESH_PROXY_USE_DIRECTED_DISABLED 0x00 +#define BLE_MESH_PROXY_USE_DIRECTED_ENABLED 0x01 -#define BLE_MESH_NODE_IDENTITY_STOPPED 0x00 -#define BLE_MESH_NODE_IDENTITY_RUNNING 0x01 -#define BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED 0x02 +#define BLE_MESH_FLOODING_CRED 0x00 +#define BLE_MESH_FRIENDSHIP_CRED 0x01 +#define BLE_MESH_DIRECTED_CRED 0x02 /* Features */ -#define BLE_MESH_FEAT_RELAY BIT(0) -#define BLE_MESH_FEAT_PROXY BIT(1) -#define BLE_MESH_FEAT_FRIEND BIT(2) -#define BLE_MESH_FEAT_LOW_POWER BIT(3) -#define BLE_MESH_FEAT_SUPPORTED (BLE_MESH_FEAT_RELAY | \ - BLE_MESH_FEAT_PROXY | \ - BLE_MESH_FEAT_FRIEND | \ - BLE_MESH_FEAT_LOW_POWER) +#define BLE_MESH_FEAT_RELAY BIT(0) +#define BLE_MESH_FEAT_PROXY BIT(1) +#define BLE_MESH_FEAT_FRIEND BIT(2) +#define BLE_MESH_FEAT_LOW_POWER BIT(3) +#define BLE_MESH_FEAT_SUPPORTED (BLE_MESH_FEAT_RELAY | \ + BLE_MESH_FEAT_PROXY | \ + BLE_MESH_FEAT_FRIEND | \ + BLE_MESH_FEAT_LOW_POWER) /** @brief Check if the mesh stack is initialized. * @@ -482,7 +574,7 @@ 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 - * reprovisioned to become an active node in a Mesh network again. + * re-provisioned to become an active node in a Mesh network again. * * After calling this API, the node will not automatically advertise as * unprovisioned, rather the bt_mesh_prov_enable() API needs to be called diff --git a/components/bt/esp_ble_mesh/core/include/mesh/uuid.h b/components/bt/esp_ble_mesh/core/include/mesh/uuid.h index 986d7f1dde1b..bd63b84d86cd 100644 --- a/components/bt/esp_ble_mesh/core/include/mesh/uuid.h +++ b/components/bt/esp_ble_mesh/core/include/mesh/uuid.h @@ -466,6 +466,10 @@ struct bt_mesh_uuid_128 { */ #define BLE_MESH_UUID_MESH_PROXY_DATA_OUT BLE_MESH_UUID_DECLARE_16(0x2ade) #define BLE_MESH_UUID_MESH_PROXY_DATA_OUT_VAL 0x2ade +/** @def BLE_MESH_UUID_MESH_PROXY_SOLIC_VAL + * @brief Mesh Proxy Solicitation UUID + */ +#define BLE_MESH_UUID_MESH_PROXY_SOLIC_VAL 0x7fcb /* * Protocol UUIDs diff --git a/components/bt/esp_ble_mesh/core/local.c b/components/bt/esp_ble_mesh/core/local.c index e3367e93eb39..b8bcfffa5572 100644 --- a/components/bt/esp_ble_mesh/core/local.c +++ b/components/bt/esp_ble_mesh/core/local.c @@ -35,9 +35,9 @@ static struct bt_mesh_model *find_model(uint16_t elem_addr, uint16_t cid, uint16 if (cid == BLE_MESH_CID_NVAL) { return bt_mesh_model_find(elem, mod_id); - } else { - return bt_mesh_model_find_vnd(elem, cid, mod_id); } + + return bt_mesh_model_find_vnd(elem, cid, mod_id); } int bt_mesh_model_subscribe_group_addr(uint16_t elem_addr, uint16_t cid, @@ -120,6 +120,36 @@ int bt_mesh_model_unsubscribe_group_addr(uint16_t elem_addr, uint16_t cid, return 0; } +#if CONFIG_BLE_MESH_DF_SRV +int bt_mesh_enable_directed_forwarding(uint16_t net_idx, bool directed_forwarding, + bool directed_forwarding_relay) +{ + struct bt_mesh_subnet *sub = NULL; + + if (net_idx > 0xFFF) { + BT_ERR("Invalid NetKeyIndex 0x%04x", net_idx); + return -EINVAL; + } + + sub = bt_mesh_subnet_get(net_idx); + if (!sub) { + BT_ERR("NetKey 0x%04x not exists", net_idx); + return -EINVAL; + } + + if (directed_forwarding == BLE_MESH_DIRECTED_FORWARDING_DISABLED && + directed_forwarding_relay == BLE_MESH_DIRECTED_RELAY_ENABLED) { + BT_ERR("Invalid Config directed forwarding: %d, directed forwarding relay: %d", directed_forwarding, directed_forwarding_relay); + return -EINVAL; + } + + sub->directed_forwarding = directed_forwarding; + sub->directed_relay = directed_forwarding_relay; + + return 0; +} + +#endif #if CONFIG_BLE_MESH_NODE const uint8_t *bt_mesh_node_get_local_net_key(uint16_t net_idx) @@ -149,7 +179,7 @@ const uint8_t *bt_mesh_node_get_local_app_key(uint16_t app_idx) return NULL; } - key = bt_mesh_app_key_find(app_idx); + key = bt_mesh_app_key_get(app_idx); if (!key) { BT_ERR("AppKey 0x%04x not exists", app_idx); return NULL; @@ -225,7 +255,7 @@ int bt_mesh_node_local_net_key_add(uint16_t net_idx, const uint8_t net_key[16]) } /* Make sure we have valid beacon data to be sent */ - bt_mesh_net_beacon_update(sub); + bt_mesh_net_secure_beacon_update(sub); return 0; } @@ -250,7 +280,7 @@ int bt_mesh_node_local_app_key_add(uint16_t net_idx, uint16_t app_idx, return -EIO; } - key = bt_mesh_app_key_find(app_idx); + key = bt_mesh_app_key_get(app_idx); if (key) { BT_WARN("AppKey 0x%04x already exists", app_idx); return -EEXIST; @@ -312,7 +342,7 @@ int bt_mesh_node_bind_app_key_to_model(uint16_t elem_addr, uint16_t mod_id, return -ENODEV; } - if (bt_mesh_app_key_find(app_idx) == NULL) { + if (bt_mesh_app_key_get(app_idx) == NULL) { BT_ERR("Bind, AppKey 0x%03x not exists", app_idx); return -ENODEV; } diff --git a/components/bt/esp_ble_mesh/core/local.h b/components/bt/esp_ble_mesh/core/local.h index 85b37e64bd66..3172d58e1709 100644 --- a/components/bt/esp_ble_mesh/core/local.h +++ b/components/bt/esp_ble_mesh/core/local.h @@ -34,6 +34,11 @@ int bt_mesh_node_local_app_key_add(uint16_t net_idx, uint16_t app_idx, int bt_mesh_node_bind_app_key_to_model(uint16_t elem_addr, uint16_t mod_id, uint16_t cid, uint16_t app_idx); +#if CONFIG_BLE_MESH_DF_SRV +int bt_mesh_enable_directed_forwarding(uint16_t net_idx, bool directed_forwarding, + bool directed_forwarding_relay); +#endif + #ifdef __cplusplus } #endif diff --git a/components/bt/esp_ble_mesh/core/lpn.c b/components/bt/esp_ble_mesh/core/lpn.c index 4de5bebe6b1f..52b01a9c5754 100644 --- a/components/bt/esp_ble_mesh/core/lpn.c +++ b/components/bt/esp_ble_mesh/core/lpn.c @@ -17,13 +17,16 @@ #include "access.h" #include "beacon.h" #include "lpn.h" +#include "rpl.h" #include "foundation.h" #include "mesh/main.h" #include "mesh/cfg_srv.h" +#include "mesh_v1.1/utils.h" + #ifdef CONFIG_BLE_MESH_LOW_POWER -#if defined(CONFIG_BLE_MESH_LPN_AUTO) +#if CONFIG_BLE_MESH_LPN_AUTO #define LPN_AUTO_TIMEOUT K_SECONDS(CONFIG_BLE_MESH_LPN_AUTO_TIMEOUT) #else #define LPN_AUTO_TIMEOUT 0 @@ -180,15 +183,18 @@ static const struct bt_mesh_send_cb clear_sent_cb = { static int send_friend_clear(void) { struct bt_mesh_msg_ctx ctx = { - .net_idx = bt_mesh.sub[0].net_idx, - .app_idx = BLE_MESH_KEY_UNUSED, - .addr = bt_mesh.lpn.frnd, - .send_ttl = 0, + .net_idx = bt_mesh.sub[0].net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = bt_mesh.lpn.frnd, + .send_ttl = 0, + .send_cred = BLE_MESH_FLOODING_CRED, + /* Tag with immutable-credentials */ + .send_tag = BLE_MESH_TAG_IMMUTABLE_CRED, }; struct bt_mesh_net_tx tx = { - .sub = &bt_mesh.sub[0], - .ctx = &ctx, - .src = bt_mesh_primary_addr(), + .sub = &bt_mesh.sub[0], + .ctx = &ctx, + .src = bt_mesh_primary_addr(), .xmit = bt_mesh_net_transmit_get(), }; struct bt_mesh_ctl_friend_clear req = { @@ -196,8 +202,6 @@ static int send_friend_clear(void) .lpn_counter = sys_cpu_to_be16(bt_mesh.lpn.counter), }; - BT_DBG("%s", __func__); - return bt_mesh_ctl_send(&tx, TRANS_CTL_OP_FRIEND_CLEAR, &req, sizeof(req), &clear_sent_cb, NULL); } @@ -210,13 +214,14 @@ static void clear_friendship(bool force, bool disable) BT_DBG("force %u disable %u", force, disable); if (!force && lpn->established && !lpn->clear_success && - lpn->req_attempts < CLEAR_ATTEMPTS) { + lpn->req_attempts < CLEAR_ATTEMPTS) { send_friend_clear(); lpn->disable = disable; return; } - bt_mesh_rx_reset(true); + bt_mesh_rx_reset(); + bt_mesh_rpl_reset(true); k_delayed_work_cancel(&lpn->timer); @@ -303,15 +308,18 @@ static int send_friend_req(struct bt_mesh_lpn *lpn) { const struct bt_mesh_comp *comp = bt_mesh_comp_get(); struct bt_mesh_msg_ctx ctx = { - .net_idx = bt_mesh.sub[0].net_idx, - .app_idx = BLE_MESH_KEY_UNUSED, - .addr = BLE_MESH_ADDR_FRIENDS, - .send_ttl = 0, + .net_idx = bt_mesh.sub[0].net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = BLE_MESH_ADDR_FRIENDS, + .send_ttl = 0, + .send_cred = BLE_MESH_FLOODING_CRED, + /* Tag with immutable-credentials and as a friendship PDU */ + .send_tag = BLE_MESH_TAG_IMMUTABLE_CRED | BLE_MESH_TAG_FRIENDSHIP, }; struct bt_mesh_net_tx tx = { - .sub = &bt_mesh.sub[0], - .ctx = &ctx, - .src = bt_mesh_primary_addr(), + .sub = &bt_mesh.sub[0], + .ctx = &ctx, + .src = bt_mesh_primary_addr(), .xmit = POLL_XMIT, }; struct bt_mesh_ctl_friend_req req = { @@ -323,8 +331,6 @@ static int send_friend_req(struct bt_mesh_lpn *lpn) .lpn_counter = sys_cpu_to_be16(lpn->counter), }; - BT_DBG("%s", __func__); - return bt_mesh_ctl_send(&tx, TRANS_CTL_OP_FRIEND_REQ, &req, sizeof(req), &friend_req_sent_cb, NULL); } @@ -348,7 +354,7 @@ static void req_sent(uint16_t duration, int err, void *user_data) if (lpn->established || IS_ENABLED(CONFIG_BLE_MESH_LPN_ESTABLISHMENT)) { lpn_set_state(BLE_MESH_LPN_RECV_DELAY); - /* We start scanning a bit early to elimitate risk of missing + /* We start scanning a bit early to eliminate risk of missing * response data due to HCI and other latencies. */ k_delayed_work_submit(&lpn->timer, @@ -374,17 +380,19 @@ static const struct bt_mesh_send_cb req_sent_cb = { static int send_friend_poll(void) { struct bt_mesh_msg_ctx ctx = { - .net_idx = bt_mesh.sub[0].net_idx, - .app_idx = BLE_MESH_KEY_UNUSED, - .addr = bt_mesh.lpn.frnd, - .send_ttl = 0, + .net_idx = bt_mesh.sub[0].net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = bt_mesh.lpn.frnd, + .send_ttl = 0, + .send_cred = BLE_MESH_FRIENDSHIP_CRED, + /* Tag with immutable-credentials and as a friendship PDU */ + .send_tag = BLE_MESH_TAG_IMMUTABLE_CRED | BLE_MESH_TAG_FRIENDSHIP, }; struct bt_mesh_net_tx tx = { - .sub = &bt_mesh.sub[0], - .ctx = &ctx, - .src = bt_mesh_primary_addr(), + .sub = &bt_mesh.sub[0], + .ctx = &ctx, + .src = bt_mesh_primary_addr(), .xmit = POLL_XMIT, - .friend_cred = true, }; struct bt_mesh_lpn *lpn = &bt_mesh.lpn; uint8_t fsn = lpn->fsn; @@ -632,7 +640,7 @@ static void lpn_group_del(uint16_t group) for (i = 0; i < ARRAY_SIZE(lpn->groups); i++) { if (lpn->groups[i] == group) { if (bt_mesh_atomic_test_bit(lpn->added, i) || - bt_mesh_atomic_test_bit(lpn->pending, i)) { + bt_mesh_atomic_test_bit(lpn->pending, i)) { bt_mesh_atomic_set_bit(lpn->to_remove, i); lpn->groups_changed = 1U; } else { @@ -660,17 +668,19 @@ static bool sub_update(uint8_t op) struct bt_mesh_lpn *lpn = &bt_mesh.lpn; int added_count = group_popcount(lpn->added); struct bt_mesh_msg_ctx ctx = { - .net_idx = bt_mesh.sub[0].net_idx, - .app_idx = BLE_MESH_KEY_UNUSED, - .addr = lpn->frnd, - .send_ttl = 0, + .net_idx = bt_mesh.sub[0].net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = lpn->frnd, + .send_ttl = 0, + .send_cred = BLE_MESH_FRIENDSHIP_CRED, + /* Tag with immutable-credentials and as a friendship PDU */ + .send_tag = BLE_MESH_TAG_IMMUTABLE_CRED | BLE_MESH_TAG_FRIENDSHIP, }; struct bt_mesh_net_tx tx = { - .sub = &bt_mesh.sub[0], - .ctx = &ctx, - .src = bt_mesh_primary_addr(), + .sub = &bt_mesh.sub[0], + .ctx = &ctx, + .src = bt_mesh_primary_addr(), .xmit = POLL_XMIT, - .friend_cred = true, }; struct bt_mesh_ctl_friend_sub req = {0}; size_t i = 0U, g = 0U; @@ -920,7 +930,7 @@ int bt_mesh_lpn_friend_sub_cfm(struct bt_mesh_net_rx *rx, for (i = 0; i < ARRAY_SIZE(lpn->groups); i++) { if (bt_mesh_atomic_test_and_clear_bit(lpn->pending, i) && - bt_mesh_atomic_test_and_clear_bit(lpn->to_remove, i)) { + bt_mesh_atomic_test_and_clear_bit(lpn->to_remove, i)) { lpn->groups[i] = BLE_MESH_ADDR_UNASSIGNED; } } @@ -975,8 +985,8 @@ int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx, } if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_INITIATOR) && - (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) == - BLE_MESH_IV_UPDATE(msg->flags))) { + (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) == + BLE_MESH_IV_UPDATE(msg->flags))) { bt_mesh_beacon_ivu_initiator(false); } @@ -988,7 +998,7 @@ int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx, * credentials so we need to ensure the right ones (Friend * Credentials) were used for this message. */ - if (!rx->friend_cred) { + if (rx->ctx.recv_cred != BLE_MESH_FRIENDSHIP_CRED) { BT_WARN("Friend Update with wrong credentials"); return -EINVAL; } @@ -1008,6 +1018,17 @@ int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx, /* Set initial poll timeout */ lpn->poll_timeout = MIN(POLL_TIMEOUT_MAX(lpn), POLL_TIMEOUT_INIT); + + /* If the Low Power node supports directed forwarding functionality when + * the friendship is established in a subnet, the Low Power node shall + * store the current value of the Directed Forwarding state and shall set + * the state to 0x00 for that subnet. When that friendship is terminated, + * the Low Power node shall set the Directed Forwarding state to the stored + * value. + */ + /* TODO: + * Store - clear - restore directed forwarding state value of the subnet. + */ } friend_response_received(lpn); @@ -1019,7 +1040,7 @@ int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx, if (bt_mesh_kr_update(sub, BLE_MESH_KEY_REFRESH(msg->flags), rx->new_key)) { - bt_mesh_net_beacon_update(sub); + bt_mesh_net_secure_beacon_update(sub); } bt_mesh_net_iv_update(iv_index, BLE_MESH_IV_UPDATE(msg->flags)); @@ -1065,8 +1086,6 @@ int bt_mesh_lpn_init(void) { struct bt_mesh_lpn *lpn = &bt_mesh.lpn; - BT_DBG("%s", __func__); - k_delayed_work_init(&lpn->timer, lpn_timeout); if (lpn->state == BLE_MESH_LPN_ENABLED) { diff --git a/components/bt/esp_ble_mesh/core/lpn.h b/components/bt/esp_ble_mesh/core/lpn.h index dbff49d624d4..7c1edd02d307 100644 --- a/components/bt/esp_ble_mesh/core/lpn.h +++ b/components/bt/esp_ble_mesh/core/lpn.h @@ -26,7 +26,7 @@ int bt_mesh_lpn_friend_sub_cfm(struct bt_mesh_net_rx *rx, static inline bool bt_mesh_lpn_established(void) { -#if defined(CONFIG_BLE_MESH_LOW_POWER) +#if CONFIG_BLE_MESH_LOW_POWER return bt_mesh.lpn.established; #else return false; @@ -35,7 +35,7 @@ static inline bool bt_mesh_lpn_established(void) static inline bool bt_mesh_lpn_match(uint16_t addr) { -#if defined(CONFIG_BLE_MESH_LOW_POWER) +#if CONFIG_BLE_MESH_LOW_POWER if (bt_mesh_lpn_established()) { return (addr == bt_mesh.lpn.frnd); } @@ -45,7 +45,7 @@ static inline bool bt_mesh_lpn_match(uint16_t addr) static inline bool bt_mesh_lpn_waiting_update(void) { -#if defined(CONFIG_BLE_MESH_LOW_POWER) +#if CONFIG_BLE_MESH_LOW_POWER return (bt_mesh.lpn.state == BLE_MESH_LPN_WAIT_UPDATE); #else return false; @@ -54,7 +54,7 @@ static inline bool bt_mesh_lpn_waiting_update(void) static inline bool bt_mesh_lpn_timer(void) { -#if defined(CONFIG_BLE_MESH_LPN_AUTO) +#if CONFIG_BLE_MESH_LPN_AUTO return (bt_mesh.lpn.state == BLE_MESH_LPN_TIMER); #else return false; diff --git a/components/bt/esp_ble_mesh/core/main.c b/components/bt/esp_ble_mesh/core/main.c index 7b5a3c715acd..deea8d74ee56 100644 --- a/components/bt/esp_ble_mesh/core/main.c +++ b/components/bt/esp_ble_mesh/core/main.c @@ -12,9 +12,9 @@ #include "adv.h" #include "scan.h" -#include "prov_node.h" #include "beacon.h" #include "lpn.h" +#include "rpl.h" #include "friend.h" #include "transport.h" #include "access.h" @@ -25,9 +25,13 @@ #include "mesh/common.h" #include "proxy_client.h" #include "proxy_server.h" +#include "prov_common.h" +#include "prov_node.h" #include "prov_pvnr.h" #include "pvnr_mgmt.h" +#include "mesh_v1.1/utils.h" + static bool mesh_init = false; bool bt_mesh_is_initialized(void) @@ -45,9 +49,11 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx, BT_INFO("Primary Element: 0x%04x", addr); 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)); + BT_INFO("DevKey %s", bt_hex(dev_key, 16)); + BT_INFO("NetKey %s", bt_hex(net_key, 16)); if (bt_mesh_atomic_test_and_set_bit(bt_mesh.flags, BLE_MESH_VALID)) { + BT_ERR("Already enter network!"); return -EALREADY; } @@ -63,6 +69,7 @@ int bt_mesh_provision(const uint8_t net_key[16], uint16_t net_idx, err = bt_mesh_net_create(net_idx, flags, net_key, iv_index); if (err) { + BT_ERR("Create network for node failed"); bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID); if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && pb_gatt_enabled) { @@ -111,8 +118,9 @@ void bt_mesh_node_reset(void) bt_mesh_cfg_reset(true); - bt_mesh_rx_reset(true); + bt_mesh_rx_reset(); bt_mesh_tx_reset(); + bt_mesh_rpl_reset(true); if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { if (IS_ENABLED(CONFIG_BLE_MESH_LPN_SUB_ALL_NODES_ADDR)) { @@ -135,14 +143,22 @@ void bt_mesh_node_reset(void) (void)memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key)); bt_mesh_scan_disable(); - bt_mesh_beacon_disable(); + bt_mesh_secure_beacon_disable(); + +#if CONFIG_BLE_MESH_PRB_SRV + bt_mesh_private_beacon_disable(); +#endif bt_mesh_comp_unprovision(); if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { bt_mesh_clear_net(); bt_mesh_clear_seq(); + bt_mesh_clear_dkca(); bt_mesh_clear_role(); + if (IS_ENABLED(CONFIG_BLE_MESH_DF_SRV)) { + bt_mesh_clear_all_directed_forwarding_table_data(); + } } memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags)); @@ -175,20 +191,20 @@ 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; } + + return false; } static bool prov_bearers_valid(bt_mesh_prov_bearer_t bearers) { if ((!(bearers & (BLE_MESH_PROV_ADV | BLE_MESH_PROV_GATT))) || (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && - !IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && - !(bearers & BLE_MESH_PROV_ADV)) || + !IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + !(bearers & BLE_MESH_PROV_ADV)) || (!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && - IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && - !(bearers & BLE_MESH_PROV_GATT))) { + IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + !(bearers & BLE_MESH_PROV_GATT))) { BT_ERR("Invalid bearers 0x%02x", bearers); return false; } @@ -232,7 +248,7 @@ int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers) } if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && - (bearers & BLE_MESH_PROV_ADV)) { + (bearers & BLE_MESH_PROV_ADV)) { /* Make sure we're scanning for provisioning invitations */ err = bt_mesh_scan_enable(); if (err) { @@ -240,11 +256,11 @@ int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers) } /* Enable unprovisioned beacon sending */ - bt_mesh_beacon_enable(); + bt_mesh_secure_beacon_enable(); } if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && - (bearers & BLE_MESH_PROV_GATT)) { + (bearers & BLE_MESH_PROV_GATT)) { bt_mesh_proxy_server_prov_enable(); bt_mesh_adv_update(); } @@ -270,13 +286,13 @@ int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers) } if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && - (bearers & BLE_MESH_PROV_ADV)) { - bt_mesh_beacon_disable(); + (bearers & BLE_MESH_PROV_ADV)) { + bt_mesh_secure_beacon_disable(); bt_mesh_scan_disable(); } if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && - (bearers & BLE_MESH_PROV_GATT)) { + (bearers & BLE_MESH_PROV_GATT)) { bt_mesh_proxy_server_prov_disable(true); } @@ -313,10 +329,16 @@ int bt_mesh_suspend(void) bt_mesh_hb_pub_disable(); - if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { - bt_mesh_beacon_disable(); + if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) { + bt_mesh_secure_beacon_disable(); } +#if CONFIG_BLE_MESH_PRB_SRV + if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) { + bt_mesh_private_beacon_disable(); + } +#endif + bt_mesh_model_foreach(model_suspend, NULL); return 0; @@ -353,10 +375,16 @@ int bt_mesh_resume(void) return err; } - if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { - bt_mesh_beacon_enable(); + if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) { + bt_mesh_secure_beacon_enable(); } +#if CONFIG_BLE_MESH_PRB_SRV + if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) { + bt_mesh_private_beacon_enable(); + } +#endif + bt_mesh_model_foreach(model_resume, NULL); return err; @@ -372,6 +400,13 @@ int bt_mesh_init(const struct bt_mesh_prov *prov, return -EALREADY; } + extern int bt_mesh_v11_ext_init(void); + err = bt_mesh_v11_ext_init(); + if (err) { + BT_ERR("Bluetooth Mesh v1.1 init failed"); + return err; + } + bt_mesh_mutex_init(); bt_mesh_timer_init(); @@ -401,15 +436,28 @@ int bt_mesh_init(const struct bt_mesh_prov *prov, bt_mesh_proxy_client_init(); } +#if CONFIG_BLE_MESH_PROXY_SOLIC + err = bt_mesh_proxy_solic_init(); + if (err) { + return err; + } +#endif + if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) { + err = bt_mesh_prov_set(prov); + if (err) { + return err; + } + if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) { - err = bt_mesh_prov_init(prov); + err = bt_mesh_prov_init(); if (err) { return err; } } + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) { - err = bt_mesh_provisioner_prov_init(prov); + err = bt_mesh_provisioner_prov_init(); if (err) { return err; } @@ -417,6 +465,7 @@ int bt_mesh_init(const struct bt_mesh_prov *prov, } bt_mesh_net_init(); + bt_mesh_trans_init(); /* Changed by Espressif, add a random delay (0 ~ 3s) */ @@ -458,7 +507,11 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param) } bt_mesh_scan_disable(); - bt_mesh_beacon_disable(); + bt_mesh_secure_beacon_disable(); + +#if CONFIG_BLE_MESH_PRB_SRV + bt_mesh_private_beacon_disable(); +#endif if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node()) { if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && @@ -507,21 +560,28 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param) return err; } } + + bt_mesh_prov_set(NULL); } bt_mesh_trans_deinit(param->erase); + bt_mesh_net_deinit(); bt_mesh_beacon_deinit(); +#if CONFIG_BLE_MESH_PROXY_SOLIC + bt_mesh_proxy_solic_deinit(); +#endif + if ((IS_ENABLED(CONFIG_BLE_MESH_NODE) && - IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) || + IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) || IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { bt_mesh_proxy_server_deinit(); } if ((IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && - IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) || + IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) || IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)) { bt_mesh_proxy_client_deinit(); } @@ -560,7 +620,7 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param) } #endif /* CONFIG_BLE_MESH_DEINIT */ -#if defined(CONFIG_BLE_MESH_PROVISIONER) +#if CONFIG_BLE_MESH_PROVISIONER int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers) { int err = 0; @@ -616,22 +676,24 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers) bt_mesh_comp_provision(bt_mesh_provisioner_get_primary_elem_addr()); -#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && - (bearers & BLE_MESH_PROV_ADV)) { + (bearers & BLE_MESH_PROV_ADV)) { bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD, - BLE_MESH_EXCEP_LIST_TYPE_MESH_BEACON, NULL); + BLE_MESH_EXCEP_LIST_TYPE_MESH_BEACON, + NULL); } if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && - (bearers & BLE_MESH_PROV_GATT)) { + (bearers & BLE_MESH_PROV_GATT)) { bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD, - BLE_MESH_EXCEP_LIST_TYPE_MESH_PROV_ADV, NULL); + BLE_MESH_EXCEP_LIST_TYPE_MESH_PROV_ADV, + NULL); } #endif if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && - (bearers & BLE_MESH_PROV_GATT)) { + (bearers & BLE_MESH_PROV_GATT)) { bt_mesh_proxy_client_prov_enable(); } @@ -639,10 +701,16 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers) bt_mesh_friend_init(); } - if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { - bt_mesh_beacon_enable(); + if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) { + bt_mesh_secure_beacon_enable(); } +#if CONFIG_BLE_MESH_PRB_SRV + if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) { + bt_mesh_private_beacon_enable(); + } +#endif + err = bt_mesh_scan_enable(); if (err) { return err; @@ -673,12 +741,14 @@ int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers) 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)) { + (enable & BLE_MESH_PROV_GATT) && + (bearers & BLE_MESH_PROV_GATT)) { bt_mesh_proxy_client_prov_disable(); -#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE, - BLE_MESH_EXCEP_LIST_TYPE_MESH_PROV_ADV, NULL); + BLE_MESH_EXCEP_LIST_TYPE_MESH_PROV_ADV, + NULL); #endif } @@ -686,11 +756,12 @@ int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers) /* Provisioner is disabled completely, disable scan here */ bt_mesh_scan_disable(); -#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && - (enable & BLE_MESH_PROV_ADV)) { + (enable & BLE_MESH_PROV_ADV)) { bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE, - BLE_MESH_EXCEP_LIST_TYPE_MESH_BEACON, NULL); + BLE_MESH_EXCEP_LIST_TYPE_MESH_BEACON, + NULL); } #endif @@ -702,10 +773,16 @@ int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers) * its previous information can be recovered from flash properly. */ - if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { - bt_mesh_beacon_disable(); + if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) { + bt_mesh_secure_beacon_disable(); } +#if CONFIG_BLE_MESH_PRB_SRV + if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) { + bt_mesh_private_beacon_disable(); + } +#endif + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY); } diff --git a/components/bt/esp_ble_mesh/core/mesh.h b/components/bt/esp_ble_mesh/core/mesh.h index cb6efd0dc033..c9392240696e 100644 --- a/components/bt/esp_ble_mesh/core/mesh.h +++ b/components/bt/esp_ble_mesh/core/mesh.h @@ -16,13 +16,19 @@ extern "C" { #endif -#define BLE_MESH_KEY_PRIMARY 0x0000 -#define BLE_MESH_KEY_ANY 0xffff - -#define BLE_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000) -#define BLE_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xc000 && (addr) < 0xff00) -#define BLE_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xc000) -#define BLE_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xff00 && (addr) <= 0xfffb) +#define BLE_MESH_KEY_PRIMARY 0x0000 +#define BLE_MESH_KEY_ANY 0xFFFF + +#define BLE_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000) +#define BLE_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xC000) +#define BLE_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xC000 && (addr) < 0xFF00) +/* Currently not including all-ipt-nodes & all-ipt-border-routers in RFU & fixed group */ +#define BLE_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xFF00 && (addr) <= 0xFFFA) +#define BLE_MESH_ADDR_IS_FIXED_GROUP(addr) ((addr) >= 0xFFFB && ((addr) < 0xFFFF || (addr) == 0xFFFF)) + +#define BLE_MESH_SUPPORTING_TYPE_FRIEND 1 +#define BLE_MESH_SUPPORTING_TYPE_PROXY 2 +#define BLE_MESH_SUPPORTING_TYPE_SBR 3 struct bt_mesh_net; diff --git a/components/bt/esp_ble_mesh/core/net.c b/components/bt/esp_ble_mesh/core/net.c index 89274121ad79..fef500e66393 100644 --- a/components/bt/esp_ble_mesh/core/net.c +++ b/components/bt/esp_ble_mesh/core/net.c @@ -2,7 +2,7 @@ /* * SPDX-FileCopyrightText: 2017 Intel Corporation - * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileContributor: 2018-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -16,17 +16,23 @@ #include "scan.h" #include "mesh.h" #include "lpn.h" +#include "rpl.h" #include "friend.h" #include "transport.h" #include "access.h" #include "foundation.h" #include "beacon.h" #include "settings.h" +#include "fast_prov.h" #include "prov_node.h" +#include "test.h" +#include "fast_prov.h" #include "proxy_client.h" #include "proxy_server.h" #include "pvnr_mgmt.h" +#include "mesh_v1.1/utils.h" + /* Minimum valid Mesh Network PDU length. The Network headers * themselves take up 9 bytes. After that there is a minumum of 1 byte * payload for both CTL=1 and CTL=0 PDUs (smallest OpCode is 1 byte). CTL=1 @@ -38,18 +44,10 @@ /* Seq limit after IV Update is triggered */ #define IV_UPDATE_SEQ_LIMIT 8000000 -#define IVI(pdu) ((pdu)[0] >> 7) -#define NID(pdu) ((pdu)[0] & 0x7f) -#define CTL(pdu) ((pdu)[1] >> 7) -#define TTL(pdu) ((pdu)[1] & 0x7f) -#define SEQ(pdu) (sys_get_be24(&(pdu)[2])) -#define SRC(pdu) (sys_get_be16(&(pdu)[5])) -#define DST(pdu) (sys_get_be16(&(pdu)[7])) - /* Determine how many friendship credentials we need */ -#if defined(CONFIG_BLE_MESH_FRIEND) +#if CONFIG_BLE_MESH_FRIEND #define FRIEND_CRED_COUNT CONFIG_BLE_MESH_FRIEND_LPN_COUNT -#elif defined(CONFIG_BLE_MESH_LOW_POWER) +#elif CONFIG_BLE_MESH_LOW_POWER #define FRIEND_CRED_COUNT CONFIG_BLE_MESH_SUBNET_COUNT #else #define FRIEND_CRED_COUNT 0 @@ -83,7 +81,7 @@ struct bt_mesh_net bt_mesh = { static uint32_t dup_cache[4]; static int dup_cache_next; -#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) +#if CONFIG_BLE_MESH_RELAY_ADV_BUF #define BLE_MESH_MAX_STORED_RELAY_COUNT (CONFIG_BLE_MESH_RELAY_ADV_BUF_COUNT / 2) #endif @@ -113,8 +111,8 @@ static bool msg_cache_match(struct bt_mesh_net_rx *rx, int i; for (i = 0; i < ARRAY_SIZE(msg_cache); i++) { - if (msg_cache[i].src == SRC(pdu->data) && - msg_cache[i].seq == (SEQ(pdu->data) & BIT_MASK(17))) { + if (msg_cache[i].src == BLE_MESH_NET_HDR_SRC(pdu->data) && + msg_cache[i].seq == (BLE_MESH_NET_HDR_SEQ(pdu->data) & BIT_MASK(17))) { return true; } } @@ -146,16 +144,35 @@ void bt_mesh_msg_cache_clear(uint16_t unicast_addr, uint8_t elem_num) struct bt_mesh_subnet *bt_mesh_subnet_get(uint16_t net_idx) { - int i; + if (bt_mesh_is_provisioned()) { +#if CONFIG_BLE_MESH_NODE + if (!IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) { + if (net_idx == BLE_MESH_KEY_ANY) { + return &bt_mesh.sub[0]; + } - if (net_idx == BLE_MESH_KEY_ANY) { - return &bt_mesh.sub[0]; - } + for (int i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + if (bt_mesh.sub[i].net_idx == net_idx) { + return &bt_mesh.sub[i]; + } + } + } else { + return bt_mesh_fast_prov_subnet_get(net_idx); + } +#endif + } else if (bt_mesh_is_provisioner_en()) { +#if CONFIG_BLE_MESH_PROVISIONER + if (net_idx == BLE_MESH_KEY_ANY) { + return bt_mesh.p_sub[0]; + } - for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { - if (bt_mesh.sub[i].net_idx == net_idx) { - return &bt_mesh.sub[i]; + for (int i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + if (bt_mesh.p_sub[i] && + bt_mesh.p_sub[i]->net_idx == net_idx) { + return bt_mesh.p_sub[i]; + } } +#endif } return NULL; @@ -189,7 +206,7 @@ int bt_mesh_net_keys_create(struct bt_mesh_subnet_keys *keys, BT_DBG("NetID %s", bt_hex(keys->net_id, 8)); -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER err = bt_mesh_identity_key(key, keys->identity); if (err) { BT_ERR("Unable to generate IdentityKey"); @@ -199,7 +216,7 @@ int bt_mesh_net_keys_create(struct bt_mesh_subnet_keys *keys, BT_DBG("IdentityKey %s", bt_hex(keys->identity, 16)); #endif /* GATT_PROXY */ - err = bt_mesh_beacon_key(key, keys->beacon); + err = bt_mesh_secure_beacon_key(key, keys->beacon); if (err) { BT_ERR("Unable to generate beacon key"); return err; @@ -207,18 +224,39 @@ int bt_mesh_net_keys_create(struct bt_mesh_subnet_keys *keys, BT_DBG("BeaconKey %s", bt_hex(keys->beacon, 16)); +#if CONFIG_BLE_MESH_PRIVATE_BEACON + err = bt_mesh_private_beacon_key(key, keys->private_beacon); + if (err) { + BT_ERR("Unable to generate private beacon key"); + return err; + } + + BT_DBG("PrivateBeaconKey %s", bt_hex(keys->private_beacon, 16)); +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ + +#if CONFIG_BLE_MESH_DF_SRV + p[0] = 0x02; + + err = bt_mesh_k2(key, p, sizeof(p), &nid, keys->direct_enc, keys->direct_privacy); + if (err) { + BT_ERR("Unable to generate directed NID, EncKey & PrivacyKey"); + return err; + } + + keys->direct_nid = nid; +#endif /* CONFIG_BLE_MESH_DF_SRV */ + return 0; } -#if (defined(CONFIG_BLE_MESH_LOW_POWER) || \ - defined(CONFIG_BLE_MESH_FRIEND)) +#if (CONFIG_BLE_MESH_LOW_POWER || CONFIG_BLE_MESH_FRIEND) int friend_cred_set(struct friend_cred *cred, uint8_t idx, const uint8_t net_key[16]) { uint16_t lpn_addr = 0U, frnd_addr = 0U; uint8_t p[9] = {0}; int err = 0; -#if defined(CONFIG_BLE_MESH_LOW_POWER) +#if CONFIG_BLE_MESH_LOW_POWER if (cred->addr == bt_mesh.lpn.frnd) { lpn_addr = bt_mesh_primary_addr(); frnd_addr = cred->addr; @@ -263,7 +301,7 @@ void friend_cred_refresh(uint16_t net_idx) struct friend_cred *cred = &friend_cred[i]; if (cred->addr != BLE_MESH_ADDR_UNASSIGNED && - cred->net_idx == net_idx) { + cred->net_idx == net_idx) { memcpy(&cred->cred[0], &cred->cred[1], sizeof(cred->cred[0])); } @@ -280,7 +318,7 @@ int friend_cred_update(struct bt_mesh_subnet *sub) struct friend_cred *cred = &friend_cred[i]; if (cred->addr == BLE_MESH_ADDR_UNASSIGNED || - cred->net_idx != sub->net_idx) { + cred->net_idx != sub->net_idx) { continue; } @@ -303,8 +341,8 @@ struct friend_cred *friend_cred_create(struct bt_mesh_subnet *sub, uint16_t addr for (cred = NULL, i = 0; i < ARRAY_SIZE(friend_cred); i++) { if ((friend_cred[i].addr == BLE_MESH_ADDR_UNASSIGNED) || - (friend_cred[i].addr == addr && - friend_cred[i].net_idx == sub->net_idx)) { + (friend_cred[i].addr == addr && + friend_cred[i].net_idx == sub->net_idx)) { cred = &friend_cred[i]; break; } @@ -420,7 +458,7 @@ uint8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub) return flags; } -int bt_mesh_net_beacon_update(struct bt_mesh_subnet *sub) +int bt_mesh_net_secure_beacon_update(struct bt_mesh_subnet *sub) { uint8_t flags = bt_mesh_net_flags(sub); struct bt_mesh_subnet_keys *keys = NULL; @@ -435,8 +473,8 @@ int bt_mesh_net_beacon_update(struct bt_mesh_subnet *sub) BT_DBG("flags 0x%02x, IVI 0x%08x", flags, bt_mesh.iv_index); - return bt_mesh_beacon_auth(keys->beacon, flags, keys->net_id, - bt_mesh.iv_index, sub->auth); + return bt_mesh_secure_beacon_auth(keys->beacon, flags, keys->net_id, + bt_mesh.iv_index, sub->auth); } int bt_mesh_net_create(uint16_t idx, uint8_t flags, const uint8_t key[16], @@ -475,6 +513,9 @@ int bt_mesh_net_create(uint16_t idx, uint8_t flags, const uint8_t key[16], sub->node_id = BLE_MESH_NODE_IDENTITY_STOPPED; } else { sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; +#if CONFIG_BLE_MESH_PRB_SRV + sub->private_node_id = BLE_MESH_PRIVATE_NODE_IDENTITY_NOT_SUPPORTED; +#endif } bt_mesh.iv_index = iv_index; @@ -488,7 +529,11 @@ int bt_mesh_net_create(uint16_t idx, uint8_t flags, const uint8_t key[16], bt_mesh.ivu_duration = BLE_MESH_IVU_MIN_HOURS; /* Make sure we have valid beacon data to be sent */ - bt_mesh_net_beacon_update(sub); + bt_mesh_net_secure_beacon_update(sub); + +#if CONFIG_BLE_MESH_DF_SRV + return bt_mesh_directed_forwarding_sub_init(sub); +#endif return 0; } @@ -565,7 +610,7 @@ bool bt_mesh_kr_update(struct bt_mesh_subnet *sub, uint8_t new_kr, bool new_key) bt_mesh_net_revoke_keys(sub); if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) || - IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { friend_cred_refresh(sub->net_idx); } @@ -576,31 +621,7 @@ bool bt_mesh_kr_update(struct bt_mesh_subnet *sub, uint8_t new_kr, bool new_key) return false; } -void bt_mesh_rpl_reset(void) -{ - int i; - - /* Discard "old old" IV Index entries from RPL and flag - * any other ones (which are valid) as old. - */ - for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { - struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i]; - - if (rpl->src) { - if (rpl->old_iv) { - (void)memset(rpl, 0, sizeof(*rpl)); - } else { - rpl->old_iv = true; - } - - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_store_rpl(rpl); - } - } - } -} - -#if defined(CONFIG_BLE_MESH_IV_UPDATE_TEST) +#if CONFIG_BLE_MESH_IV_UPDATE_TEST void bt_mesh_iv_update_test(bool enable) { bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_TEST, enable); @@ -635,7 +656,11 @@ 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) { + (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED +#if CONFIG_BLE_MESH_PRB_SRV + || bt_mesh_private_gatt_proxy_state_get() == BLE_MESH_PRIVATE_GATT_PROXY_ENABLED +#endif + )) { bt_mesh_proxy_server_beacon_send(sub); } } @@ -666,13 +691,23 @@ bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update) return false; } + /* If a node in Normal Operation receives a Secure Network beacon or + * a Mesh Private beacon with an IV index less than the last known + * IV Index or greater than the last known IV Index + 42, the Secure + * Network beacon or the Mesh Private beacon shall be ignored. + */ if (iv_index < bt_mesh.iv_index || - iv_index > bt_mesh.iv_index + 42) { + iv_index > bt_mesh.iv_index + 42) { BT_ERR("IV Index out of sync: 0x%08x != 0x%08x", iv_index, bt_mesh.iv_index); return false; } + /* If a node in Normal Operation receives a Secure Network beacon + * or a Mesh Private beacon with an IV index greater than the + * last known IV Index + 1, it may initiate an IV Index Recovery + * procedure see Section 3.10.6 + */ if ((iv_index > bt_mesh.iv_index + 1) #if CONFIG_BLE_MESH_IVU_RECOVERY_IVI || (iv_index == bt_mesh.iv_index + 1 && !iv_update) @@ -685,6 +720,13 @@ bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update) goto do_update; } + /* If a node in Normal Operation receives a Secure Network beacon + * or a Mesh Private beacon with an IV index equal to the last known + * IV index+1 and the IV Update Flag set to 0, the node may update + * its IV without going to the IV Update in Progress state, or it may + * initiate an IV Index Recovery procedure(Section 3.10.6), or it may + * ignore the Secure Network beacon or the Mesh Private beacon. + */ #if !CONFIG_BLE_MESH_IVU_RECOVERY_IVI if (iv_index == bt_mesh.iv_index + 1 && !iv_update) { BT_WARN("Ignoring new index in normal mode"); @@ -700,7 +742,7 @@ bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update) } if (!(IS_ENABLED(CONFIG_BLE_MESH_IV_UPDATE_TEST) && - bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_TEST))) { + bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_TEST))) { if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) { BT_WARN("IV Update before minimum duration"); return false; @@ -721,9 +763,9 @@ bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update) if (iv_update) { bt_mesh.iv_index = iv_index; BT_INFO("IV Update state entered. New index 0x%08x", - bt_mesh.iv_index); + bt_mesh.iv_index); - bt_mesh_rpl_reset(); + bt_mesh_rpl_update(); } else { BT_INFO("Normal mode entered"); bt_mesh.seq = 0U; @@ -736,7 +778,7 @@ bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update) for (i = 0; i < subnet_size; i++) { struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i); if (sub && sub->net_idx != BLE_MESH_KEY_UNUSED) { - bt_mesh_net_beacon_update(sub); + bt_mesh_net_secure_beacon_update(sub); } } @@ -749,14 +791,8 @@ bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update) 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; - } + if (bt_mesh_subnet_get(BLE_MESH_KEY_PRIMARY)) { + return true; } return false; @@ -771,8 +807,8 @@ uint32_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_primary_subnet_exist()) { + bt_mesh.seq > IV_UPDATE_SEQ_LIMIT && + bt_mesh_primary_subnet_exist()) { bt_mesh_beacon_ivu_initiator(true); bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true); bt_mesh_net_sec_update(NULL); @@ -782,8 +818,8 @@ uint32_t bt_mesh_next_seq(void) } 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) + bool new_key, uint8_t *tx_cred, uint8_t tx_tag, + const struct bt_mesh_send_cb *cb, void *cb_data) { const uint8_t *enc = NULL, *priv = NULL; uint32_t seq = 0U; @@ -793,8 +829,48 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, BT_DBG("net_idx 0x%04x new_key %u len %u", sub->net_idx, new_key, buf->len); - enc = sub->keys[new_key].enc; - priv = sub->keys[new_key].privacy; + /* Previously when resending the segments, only managed flooding + * security credentials will be used. + * Now for the segments retransmission, try to get the security + * credentials based on the parameters "tx_cred" and "tag". + * + * Note: + * Use "tag" here in case the friendship is terminated, which will + * cause the friendship security credentials are deleted. + * + * TODO: + * Potential issue here, if the subnet is deleted, exception (on + * Provisioner) will happen. + */ + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && + *tx_cred == BLE_MESH_FRIENDSHIP_CRED) { + err = friend_cred_get(sub, BLE_MESH_ADDR_UNASSIGNED, NULL, &enc, &priv); + if (err) { + /* If tagged with immutable-credentials, then no + * falling back should happen. + */ + if (bt_mesh_tag_immutable_cred(tx_tag)) { + BT_ERR("Resend, no friendship credentials found"); + return err; + } + + BT_WARN("Falling back to managed flooding credentials"); + + *tx_cred = BLE_MESH_FLOODING_CRED; + enc = sub->keys[new_key].enc; + priv = sub->keys[new_key].privacy; + } + } +#if CONFIG_BLE_MESH_DF_SRV + else if (*tx_cred == BLE_MESH_DIRECTED_CRED) { + enc = sub->keys[new_key].direct_enc; + priv = sub->keys[new_key].direct_privacy; + } +#endif /* CONFIG_BLE_MESH_DF_SRV */ + else { + enc = sub->keys[new_key].enc; + priv = sub->keys[new_key].privacy; + } err = bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_TX, priv); if (err) { @@ -802,7 +878,7 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, return err; } - err = bt_mesh_net_decrypt(enc, &buf->b, BLE_MESH_NET_IVI_TX, false); + err = bt_mesh_net_decrypt(enc, &buf->b, BLE_MESH_NET_IVI_TX, false, false); if (err) { BT_ERR("Decrypt failed (err %d)", err); return err; @@ -813,9 +889,9 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, sys_put_be24(seq, &buf->data[2]); /* Get destination, in case it's a proxy client */ - dst = DST(buf->data); + dst = BLE_MESH_NET_HDR_DST(buf->data); - err = bt_mesh_net_encrypt(enc, &buf->b, BLE_MESH_NET_IVI_TX, false); + err = bt_mesh_net_encrypt(enc, &buf->b, BLE_MESH_NET_IVI_TX, false, false); if (err) { BT_ERR("Encrypt failed (err %d)", err); return err; @@ -840,11 +916,11 @@ int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, return 0; } - bt_mesh_adv_send(buf, cb, cb_data); + bt_mesh_adv_send(buf, BLE_MESH_ADV(buf)->xmit, cb, cb_data); return 0; } -static void bt_mesh_net_local(struct k_work *work) +static void bt_mesh_net_local(void) { struct net_buf *buf = NULL; @@ -866,7 +942,9 @@ int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf, if (ctl && net_buf_simple_tailroom(buf) < BLE_MESH_MIC_LONG) { BT_ERR("Insufficient MIC space for CTL PDU"); return -EINVAL; - } else if (net_buf_simple_tailroom(buf) < BLE_MESH_MIC_SHORT) { + } + + if (net_buf_simple_tailroom(buf) < BLE_MESH_MIC_SHORT) { BT_ERR("Insufficient MIC space for PDU"); return -EINVAL; } @@ -885,19 +963,39 @@ int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf, net_buf_simple_push_u8(buf, tx->ctx->send_ttl); } - if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && tx->friend_cred) { - if (friend_cred_get(tx->sub, BLE_MESH_ADDR_UNASSIGNED, - &nid, &enc, &priv)) { - BT_WARN("Falling back to master credentials"); + BT_INFO("Use security credentials 0x%02x, proxy %d", tx->ctx->send_cred, proxy); + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && + tx->ctx->send_cred == BLE_MESH_FRIENDSHIP_CRED) { + err = friend_cred_get(tx->sub, BLE_MESH_ADDR_UNASSIGNED, + &nid, &enc, &priv); + if (err) { + /* If tagged with immutable-credentials, then no + * falling back should happen. + */ + if (bt_mesh_tag_immutable_cred(tx->ctx->send_tag)) { + BT_ERR("No friendship credentials found"); + return err; + } + + BT_WARN("Falling back to managed flooding credentials"); - tx->friend_cred = 0U; + tx->ctx->send_cred = BLE_MESH_FLOODING_CRED; nid = tx->sub->keys[tx->sub->kr_flag].nid; enc = tx->sub->keys[tx->sub->kr_flag].enc; priv = tx->sub->keys[tx->sub->kr_flag].privacy; } - } else { - tx->friend_cred = 0U; + } +#if CONFIG_BLE_MESH_DF_SRV + else if (tx->ctx->send_cred == BLE_MESH_DIRECTED_CRED) { + nid = tx->sub->keys[tx->sub->kr_flag].direct_nid; + enc = tx->sub->keys[tx->sub->kr_flag].direct_enc; + priv = tx->sub->keys[tx->sub->kr_flag].direct_privacy; + } +#endif /* CONFIG_BLE_MESH_DF_SRV */ + else { + tx->ctx->send_cred = BLE_MESH_FLOODING_CRED; nid = tx->sub->keys[tx->sub->kr_flag].nid; enc = tx->sub->keys[tx->sub->kr_flag].enc; priv = tx->sub->keys[tx->sub->kr_flag].privacy; @@ -905,7 +1003,7 @@ int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf, net_buf_simple_push_u8(buf, (nid | (BLE_MESH_NET_IVI_TX & 1) << 7)); - err = bt_mesh_net_encrypt(enc, buf, BLE_MESH_NET_IVI_TX, proxy); + err = bt_mesh_net_encrypt(enc, buf, BLE_MESH_NET_IVI_TX, proxy, false); if (err) { return err; } @@ -916,6 +1014,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) { + uint8_t bearer = BLE_MESH_ALL_BEARERS; int err = 0; BT_DBG("src 0x%04x dst 0x%04x len %u headroom %u tailroom %u", @@ -928,31 +1027,81 @@ int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf, tx->ctx->send_ttl = bt_mesh_default_ttl_get(); } + /* The output filter of the interface connected to advertising + * or GATT bearers shall drop all messages with the TTL value + * set to 1 unless they contain a network PDU that is tagged + * as relay. + */ + if (tx->ctx->send_ttl == 1U && + !bt_mesh_tag_relay(tx->ctx->send_tag)) { + BT_WARN("Ignore PDU with TTL=1 but not tagged as relay"); + err = -EIO; + goto done; + } + + /* Spec: + * If the message security material is not set by the network + * layer or any higher layer, the message shall be secured + * using the managed flooding security credentials. + * + * Note: + * Currently no need to add any code for this here, because if + * the security material is not set, the "tx->ctx->send_cred" + * will be initialized to 0x00, which will be used as managed + * flooding security credentials. + */ + +#if CONFIG_BLE_MESH_DF_SRV + bt_mesh_update_net_send_cred(tx, &bearer); +#endif /* CONFIG_BLE_MESH_DF_SRV */ + err = bt_mesh_net_encode(tx, &buf->b, false); if (err) { goto done; } +#if CONFIG_BLE_MESH_SELF_TEST + if (net_pdu_test_cb) { + net_pdu_test_cb(buf->data, buf->len); + goto done; + } +#endif /* CONFIG_BLE_MESH_SELF_TEST */ + /* Deliver to GATT Proxy Clients if necessary. Mesh spec 3.4.5.2: * "The output filter of the interface connected to advertising or - * GATT bearers shall drop all messages with TTL value set to 1." + * GATT bearers shall drop all messages with the TTL value set to + * 1 unless they contain a network PDU that is tagged as relay." + * + * "The output filter of the interface connected to the GATT bearer + * shall drop all messages secured using the friendship security + * credentials." */ + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && - tx->ctx->send_ttl != 1U) { + (bearer & BLE_MESH_GATT_BEARER) && + (tx->ctx->send_ttl != 1U || + bt_mesh_tag_relay(tx->ctx->send_tag)) && + tx->ctx->send_cred != BLE_MESH_FRIENDSHIP_CRED) { if (bt_mesh_proxy_server_relay(&buf->b, tx->ctx->addr) && - BLE_MESH_ADDR_IS_UNICAST(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 ((bearer & (~BLE_MESH_GATT_BEARER)) == 0) { + send_cb_finalize(cb, cb_data); + + err = 0; + goto done; + } - err = 0; - goto done; } } if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) && - tx->ctx->send_ttl != 1U) { + (bearer & BLE_MESH_GATT_BEARER) && + (tx->ctx->send_ttl != 1U || + bt_mesh_tag_relay(tx->ctx->send_tag)) && + tx->ctx->send_cred != BLE_MESH_FRIENDSHIP_CRED) { if (bt_mesh_proxy_client_relay(&buf->b, tx->ctx->addr)) { /* If Proxy Client succeeds to send messages with GATT bearer, * we can directly finish here. And if not, which means no @@ -967,26 +1116,53 @@ int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf, } /* Deliver to local network interface if necessary */ - if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() && - (bt_mesh_fixed_group_match(tx->ctx->addr) || - bt_mesh_elem_find(tx->ctx->addr))) { + if (((IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) || + (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en())) && + (bt_mesh_fixed_group_match(tx->ctx->addr) || bt_mesh_elem_find(tx->ctx->addr))) { if (cb && cb->start) { cb->start(0, 0, cb_data); } + net_buf_slist_put(&bt_mesh.local_queue, net_buf_ref(buf)); + if (cb && cb->end) { cb->end(0, cb_data); } - k_work_submit(&bt_mesh.local_work); - } else if (tx->ctx->send_ttl != 1U) { + + bt_mesh_net_local(); + + err = 0; + goto done; + } + + if ((bearer & BLE_MESH_ADV_BEARER) && + (tx->ctx->send_ttl != 1U || bt_mesh_tag_relay(tx->ctx->send_tag))) { /* Deliver to the advertising network interface. Mesh spec * 3.4.5.2: "The output filter of the interface connected to * advertising or GATT bearers shall drop all messages with * TTL value set to 1." + * Spec v1.1 adds additional condition: + * "unless they contain a network PDU that is tagged as relay" + * + * "tx->xmit" has already been initialized in the access.c + * (i.e. bt_mesh_model_send() & bt_mesh_model_publish()). + * Here xmit may needs to be updated according Mesh spec 3.4.6. */ - bt_mesh_adv_send(buf, cb, cb_data); + bt_mesh_net_adv_xmit_update(tx); + + BT_INFO("Network PDU, count %d, interval %d", + BLE_MESH_TRANSMIT_COUNT(tx->xmit), BLE_MESH_TRANSMIT_INT(tx->xmit)); + bt_mesh_adv_send(buf, tx->xmit, cb, cb_data); + + err = 0; + goto done; } + BT_WARN("Not sent, src 0x%04x, dst 0x%04x, ttl %d, cred 0x%02x, tag 0x%02x", + tx->src, tx->ctx->addr, tx->ctx->send_ttl, tx->ctx->send_cred, + tx->ctx->send_tag); + err = -EIO; + done: net_buf_unref(buf); return err; @@ -1002,8 +1178,8 @@ static bool auth_match(struct bt_mesh_subnet_keys *keys, return false; } - bt_mesh_beacon_auth(keys->beacon, flags, keys->net_id, iv_index, - net_auth); + bt_mesh_secure_beacon_auth(keys->beacon, flags, keys->net_id, iv_index, + net_auth); if (memcmp(auth, net_auth, 8)) { BT_WARN("Authentication Value %s != %s", @@ -1014,9 +1190,9 @@ static bool auth_match(struct bt_mesh_subnet_keys *keys, return true; } -struct bt_mesh_subnet *bt_mesh_subnet_find(const uint8_t net_id[8], uint8_t flags, - uint32_t iv_index, const uint8_t auth[8], - bool *new_key) +struct bt_mesh_subnet *bt_mesh_subnet_find_with_snb(const uint8_t net_id[8], uint8_t flags, + uint32_t iv_index, const uint8_t auth[8], + bool *new_key) { size_t subnet_size = 0U; int i; @@ -1048,15 +1224,15 @@ struct bt_mesh_subnet *bt_mesh_subnet_find(const uint8_t net_id[8], uint8_t flag return NULL; } -static int net_decrypt(struct bt_mesh_subnet *sub, const uint8_t *enc, - const uint8_t *priv, const uint8_t *data, - size_t data_len, struct bt_mesh_net_rx *rx, - struct net_buf_simple *buf) +int net_decrypt(struct bt_mesh_subnet *sub, const uint8_t *enc, + const uint8_t *priv, const uint8_t *data, + size_t data_len, struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) { - BT_DBG("NID 0x%02x net_idx 0x%04x", NID(data), sub->net_idx); - BT_DBG("IVI %u net->iv_index 0x%08x", IVI(data), bt_mesh.iv_index); + BT_DBG("NID 0x%02x net_idx 0x%04x", BLE_MESH_NET_HDR_NID(data), sub->net_idx); + BT_DBG("IVI %u net->iv_index 0x%08x", BLE_MESH_NET_HDR_IVI(data), bt_mesh.iv_index); - rx->old_iv = (IVI(data) != (bt_mesh.iv_index & 0x01)); + rx->old_iv = (BLE_MESH_NET_HDR_IVI(data) != (bt_mesh.iv_index & 0x01)); net_buf_simple_reset(buf); memcpy(net_buf_simple_add(buf, data_len), data, data_len); @@ -1065,7 +1241,7 @@ static int net_decrypt(struct bt_mesh_subnet *sub, const uint8_t *enc, return -ENOENT; } - rx->ctx.addr = SRC(buf->data); + rx->ctx.addr = BLE_MESH_NET_HDR_SRC(buf->data); if (!BLE_MESH_ADDR_IS_UNICAST(rx->ctx.addr)) { BT_INFO("Ignoring non-unicast src addr 0x%04x", rx->ctx.addr); return -EINVAL; @@ -1079,23 +1255,22 @@ static int net_decrypt(struct bt_mesh_subnet *sub, const uint8_t *enc, BT_DBG("src 0x%04x", rx->ctx.addr); if (IS_ENABLED(CONFIG_BLE_MESH_PROXY) && - rx->net_if == BLE_MESH_NET_IF_PROXY_CFG) { + rx->net_if == BLE_MESH_NET_IF_PROXY_CFG) { return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx), - true); + true, false); } - return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx), false); + return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx), false, false); } -#if (defined(CONFIG_BLE_MESH_LOW_POWER) || \ - defined(CONFIG_BLE_MESH_FRIEND)) +#if (CONFIG_BLE_MESH_LOW_POWER || CONFIG_BLE_MESH_FRIEND) static int friend_decrypt(struct bt_mesh_subnet *sub, const uint8_t *data, size_t data_len, struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) { int i; - BT_DBG("NID 0x%02x net_idx 0x%04x", NID(data), sub->net_idx); + BT_DBG("NID 0x%02x net_idx 0x%04x", BLE_MESH_NET_HDR_NID(data), sub->net_idx); for (i = 0; i < ARRAY_SIZE(friend_cred); i++) { struct friend_cred *cred = &friend_cred[i]; @@ -1104,9 +1279,9 @@ static int friend_decrypt(struct bt_mesh_subnet *sub, const uint8_t *data, continue; } - if (NID(data) == cred->cred[0].nid && - !net_decrypt(sub, cred->cred[0].enc, cred->cred[0].privacy, - data, data_len, rx, buf)) { + if (BLE_MESH_NET_HDR_NID(data) == cred->cred[0].nid && + !net_decrypt(sub, cred->cred[0].enc, cred->cred[0].privacy, + data, data_len, rx, buf)) { return 0; } @@ -1114,9 +1289,9 @@ static int friend_decrypt(struct bt_mesh_subnet *sub, const uint8_t *data, continue; } - if (NID(data) == cred->cred[1].nid && - !net_decrypt(sub, cred->cred[1].enc, cred->cred[1].privacy, - data, data_len, rx, buf)) { + if (BLE_MESH_NET_HDR_NID(data) == cred->cred[1].nid && + !net_decrypt(sub, cred->cred[1].enc, cred->cred[1].privacy, + data, data_len, rx, buf)) { rx->new_key = 1U; return 0; } @@ -1124,7 +1299,33 @@ static int friend_decrypt(struct bt_mesh_subnet *sub, const uint8_t *data, return -ENOENT; } -#endif +#endif /* (CONFIG_BLE_MESH_LOW_POWER || CONFIG_BLE_MESH_FRIEND) */ + +static int flooding_decrypt(struct bt_mesh_subnet *sub, const uint8_t *data, + size_t data_len, struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + BT_DBG("NID 0x%02x net_idx 0x%04x", BLE_MESH_NET_HDR_NID(data), sub->net_idx); + + if (BLE_MESH_NET_HDR_NID(data) == sub->keys[0].nid && + !net_decrypt(sub, sub->keys[0].enc, sub->keys[0].privacy, + data, data_len, rx, buf)) { + return 0; + } + + if (sub->kr_phase == BLE_MESH_KR_NORMAL) { + return -ENOENT; + } + + if (BLE_MESH_NET_HDR_NID(data) == sub->keys[1].nid && + !net_decrypt(sub, sub->keys[1].enc, sub->keys[1].privacy, + data, data_len, rx, buf)) { + rx->new_key = 1U; + return 0; + } + + return -ENOENT; +} static bool net_find_and_decrypt(const uint8_t *data, size_t data_len, struct bt_mesh_net_rx *rx, @@ -1134,8 +1335,6 @@ static bool net_find_and_decrypt(const uint8_t *data, size_t data_len, size_t array_size = 0U; int i; - BT_DBG("%s", __func__); - array_size = bt_mesh_rx_netkey_size(); for (i = 0; i < array_size; i++) { @@ -1149,31 +1348,30 @@ static bool net_find_and_decrypt(const uint8_t *data, size_t data_len, continue; } -#if (defined(CONFIG_BLE_MESH_LOW_POWER) || defined(CONFIG_BLE_MESH_FRIEND)) +#if CONFIG_BLE_MESH_BRC_SRV + sub->sbr_net_idx = BLE_MESH_KEY_UNUSED; +#endif + +#if (CONFIG_BLE_MESH_LOW_POWER || CONFIG_BLE_MESH_FRIEND) if (!friend_decrypt(sub, data, data_len, rx, buf)) { - rx->friend_cred = 1; + rx->ctx.recv_cred = BLE_MESH_FRIENDSHIP_CRED; rx->ctx.net_idx = sub->net_idx; rx->sub = sub; return true; } #endif - if (NID(data) == sub->keys[0].nid && - !net_decrypt(sub, sub->keys[0].enc, sub->keys[0].privacy, - data, data_len, rx, buf)) { +#if CONFIG_BLE_MESH_DF_SRV + if (!bt_mesh_directed_decrypt(sub, data, data_len, rx, buf)) { + rx->ctx.recv_cred = BLE_MESH_DIRECTED_CRED; rx->ctx.net_idx = sub->net_idx; rx->sub = sub; return true; } +#endif /* CONFIG_BLE_MESH_DF_SRV */ - if (sub->kr_phase == BLE_MESH_KR_NORMAL) { - continue; - } - - if (NID(data) == sub->keys[1].nid && - !net_decrypt(sub, sub->keys[1].enc, sub->keys[1].privacy, - data, data_len, rx, buf)) { - rx->new_key = 1U; + if (!flooding_decrypt(sub, data, data_len, rx, buf)) { + rx->ctx.recv_cred = BLE_MESH_FLOODING_CRED; rx->ctx.net_idx = sub->net_idx; rx->sub = sub; return true; @@ -1207,7 +1405,12 @@ static void bt_mesh_net_relay(struct net_buf_simple *sbuf, { const uint8_t *enc = NULL, *priv = NULL; struct net_buf *buf = NULL; - uint8_t nid = 0U, transmit = 0U; + uint8_t bearer = 0; + uint8_t xmit = 0U; + uint8_t cred = 0; + uint8_t nid = 0U; + bool netkey_changed = false; + uint8_t tag = 0; if (rx->net_if == BLE_MESH_NET_IF_LOCAL) { /* Locally originated PDUs with TTL=1 will only be delivered @@ -1216,52 +1419,100 @@ static void bt_mesh_net_relay(struct net_buf_simple *sbuf, * advertising or GATT bearers shall drop all messages with * TTL value set to 1." */ + /* For local network interface, no outbound bearer will be found. + * So currently if a PDU is received from local network interface + * with TTL=1, it will be ignored here. + */ if (rx->ctx.recv_ttl == 1U) { return; } } else { - if (rx->ctx.recv_ttl <= 1U) { + /* Only PDU with TTL=0 will be ignored here. PDU with TTL=1 will + * be checked later to see if it could be tagged with RELAY. + */ + if (rx->ctx.recv_ttl < 1U) { return; } } +#if 0 + /* This could be moved, since in mesh v1.1, two more conditions could + * cause a message to be relayed, i.e. directed forwarding and subnet + * bridge. + * The two conditions will be checked with retransmission requirements. + */ if (rx->net_if == BLE_MESH_NET_IF_ADV && - !rx->friend_cred && - bt_mesh_relay_get() != BLE_MESH_RELAY_ENABLED && - bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_ENABLED) { + bt_mesh_relay_get() != BLE_MESH_RELAY_ENABLED && + bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_ENABLED && + rx->ctx.recv_cred != BLE_MESH_FRIENDSHIP_CRED) { + return; + } +#endif + + /* Mesh spec 3.4.6.3: + * "The Network PDU shall be evaluated against retransmission conditions + * for incoming Network PDUs as defined in Table 3.10. For a Network PDU, + * there may be more than one matching row in Table 3.10. If there is no + * row that matches the retransmission conditions, then the Network PDU + * shall not be retransmitted." + */ + bearer = bt_mesh_net_retrans_match(rx, &cred, &tag); + if (bearer == BLE_MESH_NONE_BEARER) { + BT_WARN("No outbound bearer found, inbound bearer %d", rx->net_if); return; } - BT_DBG("TTL %u CTL %u dst 0x%04x", rx->ctx.recv_ttl, rx->ctl, - rx->ctx.recv_dst); + if (cred != BLE_MESH_FLOODING_CRED +#if CONFIG_BLE_MESH_DF_SRV + && cred != BLE_MESH_DIRECTED_CRED +#endif + ) { + BT_WARN("No outbound security cred found, inbound cred %d", rx->ctx.recv_cred); + return; + } + + if (rx->ctx.recv_ttl == 0x01 && bt_mesh_tag_relay(tag) == false) { + BT_DBG("Ignore PDU with TTL=1 but not tagged as relay"); + return; + } + + BT_DBG("TTL %u CTL %u dst 0x%04x", rx->ctx.recv_ttl, rx->ctl, rx->ctx.recv_dst); /* The Relay Retransmit state is only applied to adv-adv relaying. * Anything else (like GATT to adv, or locally originated packets) * use the Network Transmit state. */ - if (rx->net_if == BLE_MESH_NET_IF_ADV && !rx->friend_cred) { - transmit = bt_mesh_relay_retransmit_get(); + if (rx->net_if == BLE_MESH_NET_IF_ADV && + rx->ctx.recv_cred != BLE_MESH_FRIENDSHIP_CRED) { + xmit = bt_mesh_relay_retransmit_get(); } else { - transmit = bt_mesh_net_transmit_get(); + xmit = bt_mesh_net_transmit_get(); } - /** +#if 0 + /* TODO: Needs further thinking. */ + xmit = bt_mesh_net_adv_xmit_update(cred, tag, rx->ctl); + if (xmit == 0) { + return; + } +#endif + + /* TODO: * When the node tries to relay a Segment ACK message, in this case * the corresponding segment packets (if exist) can be removed from * the relay queue. */ -#if !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) - buf = bt_mesh_adv_create(BLE_MESH_ADV_DATA, transmit, K_NO_WAIT); +#if !CONFIG_BLE_MESH_RELAY_ADV_BUF + buf = bt_mesh_adv_create(BLE_MESH_ADV_DATA, K_NO_WAIT); #else - /** - * Check if the number of relay packets in queue is too large, if so + /* Check if the number of relay packets in queue is too large, if so * use minimum relay retransmit value for later relay packets. */ if (bt_mesh_get_stored_relay_count() >= BLE_MESH_MAX_STORED_RELAY_COUNT) { - transmit = BLE_MESH_TRANSMIT(0, 20); + xmit = BLE_MESH_TRANSMIT(0, 20); } - buf = bt_mesh_relay_adv_create(BLE_MESH_ADV_DATA, transmit, K_NO_WAIT); + buf = bt_mesh_relay_adv_create(BLE_MESH_ADV_DATA, K_NO_WAIT); #endif if (!buf) { @@ -1278,14 +1529,31 @@ static void bt_mesh_net_relay(struct net_buf_simple *sbuf, net_buf_add_mem(buf, sbuf->data, sbuf->len); - enc = rx->sub->keys[rx->sub->kr_flag].enc; - priv = rx->sub->keys[rx->sub->kr_flag].privacy; - nid = rx->sub->keys[rx->sub->kr_flag].nid; + if (cred == BLE_MESH_FLOODING_CRED) { + enc = rx->sub->keys[rx->sub->kr_flag].enc; + priv = rx->sub->keys[rx->sub->kr_flag].privacy; + nid = rx->sub->keys[rx->sub->kr_flag].nid; +#if CONFIG_BLE_MESH_DF_SRV + } else { + enc = rx->sub->keys[rx->sub->kr_flag].direct_enc; + priv = rx->sub->keys[rx->sub->kr_flag].direct_privacy; + nid = rx->sub->keys[rx->sub->kr_flag].direct_nid; +#endif /* CONFIG_BLE_MESH_DF_SRV */ + } + +#if CONFIG_BLE_MESH_BRC_SRV + /* Check if subnet bridge is supported & decide which NetKey is used */ + if (bt_mesh_subnet_bridge_state_get() == BLE_MESH_SUBNET_BRIDGE_ENABLED) { + netkey_changed = bt_mesh_bridge_change_net_key(rx, &enc, &priv, &nid, cred); + } +#endif - BT_DBG("Relaying packet. TTL is now %u", TTL(buf->data)); + BT_DBG("Relaying packet. TTL is now %u", BLE_MESH_NET_HDR_TTL(buf->data)); - /* Update NID if RX or RX was with friend credentials */ - if (rx->friend_cred) { + /* 1. Update NID if RX or RX was with friend credentials. + * 2. Update NID if the net_key has changed. + */ + if (rx->ctx.recv_cred == BLE_MESH_FRIENDSHIP_CRED || netkey_changed) { buf->data[0] &= 0x80; /* Clear everything except IVI */ buf->data[0] |= nid; } @@ -1294,7 +1562,7 @@ static void bt_mesh_net_relay(struct net_buf_simple *sbuf, * the normal TX IVI (which may be different) since the transport * layer nonce includes the IVI. */ - if (bt_mesh_net_encrypt(enc, &buf->b, BLE_MESH_NET_IVI_RX(rx), false)) { + if (bt_mesh_net_encrypt(enc, &buf->b, BLE_MESH_NET_IVI_RX(rx), false, false)) { BT_ERR("Re-encrypting failed"); goto done; } @@ -1306,22 +1574,46 @@ static void bt_mesh_net_relay(struct net_buf_simple *sbuf, /* Sending to the GATT bearer should only happen if GATT Proxy * is enabled or the message originates from the local node. + * + * The output filter of the interface connected to the GATT bearer + * shall drop all messages secured using the friendship security + * credentials. */ if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && - (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED || - rx->friend_cred || - rx->net_if == BLE_MESH_NET_IF_LOCAL)) { + (bearer & BLE_MESH_GATT_BEARER) && + ((bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED && + cred != BLE_MESH_FRIENDSHIP_CRED) || + bt_mesh_private_gatt_proxy_state_get() == BLE_MESH_PRIVATE_GATT_PROXY_ENABLED || + rx->net_if == BLE_MESH_NET_IF_LOCAL || + rx->ctx.recv_cred == BLE_MESH_FRIENDSHIP_CRED)) { if (bt_mesh_proxy_server_relay(&buf->b, rx->ctx.recv_dst) && - BLE_MESH_ADDR_IS_UNICAST(rx->ctx.recv_dst)) { - goto done; + BLE_MESH_ADDR_IS_UNICAST(rx->ctx.recv_dst)) { + if ((bearer & (~BLE_MESH_GATT_BEARER)) == 0) { + goto done; + } } } - if (relay_to_adv(rx->net_if) || rx->friend_cred) { -#if !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) - bt_mesh_adv_send(buf, NULL, NULL); + if (((bearer & BLE_MESH_ADV_BEARER) && relay_to_adv(rx->net_if)) || + netkey_changed || + rx->ctx.recv_cred == BLE_MESH_FRIENDSHIP_CRED) { + /* NOTE: temporary add for case MESH/NODE/SBR/NET/BV-01-C */ +#if CONFIG_BLE_MESH_BRC_SRV + if (bt_mesh_bridge_rpl_check(rx, NULL) && netkey_changed) { + BT_ERR("It is RPL attack, not bridge"); + /** + * @todo:/NODE/DF/INIT/BV-TBD-C, + * The message from LT2 was double-checked, + * so the message was mistaken for an RPL attack. + */ + goto done; + } +#endif + +#if !CONFIG_BLE_MESH_RELAY_ADV_BUF + bt_mesh_adv_send(buf, xmit, NULL, NULL); #else - bt_mesh_relay_adv_send(buf, NULL, NULL, rx->ctx.addr, rx->ctx.recv_dst); + bt_mesh_relay_adv_send(buf, xmit, rx->ctx.addr, rx->ctx.recv_dst, NULL, NULL); #endif } @@ -1332,12 +1624,12 @@ static void bt_mesh_net_relay(struct net_buf_simple *sbuf, void bt_mesh_net_header_parse(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx) { - rx->old_iv = (IVI(buf->data) != (bt_mesh.iv_index & 0x01)); - rx->ctl = CTL(buf->data); - rx->ctx.recv_ttl = TTL(buf->data); - rx->seq = SEQ(buf->data); - rx->ctx.addr = SRC(buf->data); - rx->ctx.recv_dst = DST(buf->data); + rx->old_iv = (BLE_MESH_NET_HDR_IVI(buf->data) != (bt_mesh.iv_index & 0x01)); + rx->ctl = BLE_MESH_NET_HDR_CTL(buf->data); + rx->ctx.recv_ttl = BLE_MESH_NET_HDR_TTL(buf->data); + rx->seq = BLE_MESH_NET_HDR_SEQ(buf->data); + rx->ctx.addr = BLE_MESH_NET_HDR_SRC(buf->data); + rx->ctx.recv_dst = BLE_MESH_NET_HDR_DST(buf->data); } int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if, @@ -1362,10 +1654,21 @@ int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if, return -ENOENT; } + /* Spec v1.1, Section 3.4.5.1: + * The input filter of the interface connected to the GATT + * bearer shall drop all messages that have been secured + * using the friendship security credentials. + */ + if (net_if == BLE_MESH_NET_IF_PROXY && + rx->ctx.recv_cred == BLE_MESH_FRIENDSHIP_CRED) { + BT_WARN("Dropping packet from GATT bearer using friendship creds"); + return -EBADMSG; + } + /* Initialize AppIdx to a sane value */ rx->ctx.app_idx = BLE_MESH_KEY_UNUSED; - rx->ctx.recv_ttl = TTL(buf->data); + rx->ctx.recv_ttl = BLE_MESH_NET_HDR_TTL(buf->data); /* Default to responding with TTL 0 for non-routed messages */ if (rx->ctx.recv_ttl == 0U) { @@ -1374,14 +1677,14 @@ int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if, rx->ctx.send_ttl = BLE_MESH_TTL_DEFAULT; } - rx->ctl = CTL(buf->data); - rx->seq = SEQ(buf->data); - rx->ctx.recv_dst = DST(buf->data); + rx->ctl = BLE_MESH_NET_HDR_CTL(buf->data); + rx->seq = BLE_MESH_NET_HDR_SEQ(buf->data); + rx->ctx.recv_dst = BLE_MESH_NET_HDR_DST(buf->data); BT_DBG("Decryption successful. Payload len %u", buf->len); if (net_if != BLE_MESH_NET_IF_PROXY_CFG && - rx->ctx.recv_dst == BLE_MESH_ADDR_UNASSIGNED) { + rx->ctx.recv_dst == BLE_MESH_ADDR_UNASSIGNED) { BT_ERR("Destination address is unassigned; dropping packet"); return -EBADMSG; } @@ -1412,7 +1715,9 @@ static bool ready_to_recv(void) { 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 (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { if (bt_mesh_provisioner_get_node_count()) { return true; } @@ -1439,12 +1744,16 @@ static bool ignore_net_msg(uint16_t src, uint16_t dst) * address of Provisioner, but Provisioner fails to find the * node in its provisioning database, then this message will * be ignored. + * Note: extra check src here to support Provisioner sending + * messages to itself. */ - if (!bt_mesh_provisioner_get_node_with_addr(src)) { + if (!bt_mesh_provisioner_get_node_with_addr(src) && + !bt_mesh_elem_find(src)) { BT_INFO("Not found node address 0x%04x", src); return true; } } + return false; } @@ -1472,16 +1781,23 @@ void bt_mesh_net_recv(struct net_buf_simple *data, int8_t rssi, /* Save the state so the buffer can later be relayed */ net_buf_simple_save(&buf, &state); - BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_NET, + BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | \ + BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_NET, "\nNetRecv: ctl: %d, src: %d, dst: %d, ttl: %d, data: 0x%s", - CTL(buf.data), SRC(buf.data), DST(buf.data), TTL(buf.data), + rx.ctl, rx.ctx.addr, rx.ctx.recv_dst, rx.ctx.recv_ttl, bt_hex(buf.data + BLE_MESH_NET_HDR_LEN, buf.len - BLE_MESH_NET_HDR_LEN)); + /* If trying to handle a message with DST set to all-directed-forwarding-nodes, + * we need to make sure the directed forwarding functionality is enabled in the + * corresponding subnet. + */ rx.local_match = (bt_mesh_fixed_group_match(rx.ctx.recv_dst) || + bt_mesh_fixed_direct_match(rx.sub, rx.ctx.recv_dst) || bt_mesh_elem_find(rx.ctx.recv_dst)); if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && - net_if == BLE_MESH_NET_IF_PROXY) { + bt_mesh_private_gatt_proxy_state_get() == BLE_MESH_PRIVATE_GATT_PROXY_DISABLED && + net_if == BLE_MESH_NET_IF_PROXY) { bt_mesh_proxy_server_addr_add(data, rx.ctx.addr); if (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_DISABLED && @@ -1489,6 +1805,22 @@ void bt_mesh_net_recv(struct net_buf_simple *data, int8_t rssi, BT_INFO("Proxy is disabled; ignoring message"); return; } + + /* If the Directed Proxy Server receives a valid Network PDU from the Directed + * Proxy Client, and the value of the Use_Directed parameter of the connection + * for the subnet identified by the Network PDU is Enabled, and the source + * address of the Network PDU is outside the unicast address range defined by + * the Proxy_Client_Address_Range parameter, then the Directed Proxy Server shall + * tag the Network PDU with the immutable-credentials tag. + */ +#if CONFIG_BLE_MESH_DF_SRV + if (rx.sub->directed_proxy == BLE_MESH_DIRECTED_PROXY_ENABLED && + rx.sub->use_directed == BLE_MESH_PROXY_USE_DIRECTED_ENABLED && + !bt_mesh_addr_in_uar(&rx.sub->proxy_client_uar, rx.ctx.addr) && + !bt_mesh_proxy_server_find_client_by_addr(rx.ctx.addr)) { + rx.ctx.recv_tag |= BLE_MESH_TAG_IMMUTABLE_CRED; + } +#endif /* CONFIG_BLE_MESH_DF_SRV */ } /* The transport layer has indicated that it has rejected the message, @@ -1509,7 +1841,7 @@ void bt_mesh_net_recv(struct net_buf_simple *data, int8_t rssi, * was neither a local element nor an LPN we're Friends for. */ if (!BLE_MESH_ADDR_IS_UNICAST(rx.ctx.recv_dst) || - (!rx.local_match && !rx.friend_match)) { + (!rx.local_match && !rx.friend_match)) { net_buf_simple_restore(&buf, &state); bt_mesh_net_relay(&buf, &rx); } @@ -1520,9 +1852,9 @@ static void ivu_refresh(struct k_work *work) bt_mesh.ivu_duration += BLE_MESH_IVU_HOURS; 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"); + 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"); if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) { if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { @@ -1543,22 +1875,31 @@ static void ivu_refresh(struct k_work *work) void bt_mesh_net_start(void) { - if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { - bt_mesh_beacon_enable(); + if (bt_mesh_secure_beacon_get() == BLE_MESH_SECURE_BEACON_ENABLED) { + bt_mesh_secure_beacon_enable(); + } else { + bt_mesh_secure_beacon_disable(); + } + +#if CONFIG_BLE_MESH_PRB_SRV + if (bt_mesh_private_beacon_state_get() == BLE_MESH_PRIVATE_BEACON_ENABLED) { + bt_mesh_private_beacon_enable(); } else { - bt_mesh_beacon_disable(); + bt_mesh_private_beacon_disable(); } +#endif if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && - bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_NOT_SUPPORTED) { + bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_NOT_SUPPORTED) { bt_mesh_proxy_server_gatt_enable(); bt_mesh_adv_update(); } -#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN /* Add Mesh beacon type (Secure Network Beacon) to the exceptional list */ bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD, - BLE_MESH_EXCEP_LIST_TYPE_MESH_BEACON, NULL); + BLE_MESH_EXCEP_LIST_TYPE_MESH_BEACON, + NULL); #endif if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { @@ -1589,8 +1930,6 @@ void bt_mesh_net_start(void) void bt_mesh_net_init(void) { k_delayed_work_init(&bt_mesh.ivu_timer, ivu_refresh); - - k_work_init(&bt_mesh.local_work, bt_mesh_net_local); } void bt_mesh_net_reset(void) @@ -1618,8 +1957,6 @@ void bt_mesh_net_deinit(void) 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. */ diff --git a/components/bt/esp_ble_mesh/core/net.h b/components/bt/esp_ble_mesh/core/net.h index b0e34885244d..b9dfb42dda16 100644 --- a/components/bt/esp_ble_mesh/core/net.h +++ b/components/bt/esp_ble_mesh/core/net.h @@ -11,18 +11,19 @@ #define _NET_H_ #include "mesh/access.h" +#include "mesh/mutex.h" #ifdef __cplusplus extern "C" { #endif -#define BLE_MESH_NET_FLAG_KR BIT(0) -#define BLE_MESH_NET_FLAG_IVU BIT(1) +#define BLE_MESH_NET_FLAG_KR BIT(0) +#define BLE_MESH_NET_FLAG_IVU BIT(1) -#define BLE_MESH_KR_NORMAL 0x00 -#define BLE_MESH_KR_PHASE_1 0x01 -#define BLE_MESH_KR_PHASE_2 0x02 -#define BLE_MESH_KR_PHASE_3 0x03 +#define BLE_MESH_KR_NORMAL 0x00 +#define BLE_MESH_KR_PHASE_1 0x01 +#define BLE_MESH_KR_PHASE_2 0x02 +#define BLE_MESH_KR_PHASE_3 0x03 #define BLE_MESH_IV_UPDATE(flags) ((flags >> 1) & 0x01) #define BLE_MESH_KEY_REFRESH(flags) (flags & 0x01) @@ -43,45 +44,131 @@ struct bt_mesh_app_key { }; struct bt_mesh_subnet { - uint32_t beacon_sent; /* Timestamp of last sent beacon */ - uint8_t beacons_last; /* Number of beacons during last observation window. */ - uint8_t beacons_cur; /* Number of beacons observed during currently ongoing window. */ + uint32_t snb_sent; /* Timestamp of last sent secure network beacon */ + uint8_t snb_last; /* Number of secure network beacons during last observation window */ + uint8_t snb_cur; /* Number of secure network beacons observed during currently ongoing window.*/ + uint8_t snb_cache[21]; /* Cached last receive authenticated secure beacon */ - uint8_t beacon_cache[21]; /* Cached last authenticated beacon */ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + uint32_t mpb_sent; /* Timestamp of last sent private beacon */ + uint8_t mpb_last; /* Number of private beacons during last observation window */ + uint8_t mpb_cur; /* Number of private beacons observed during currently ongoing window. */ + uint8_t mpb_cache[21]; /* Cached last receive private beacon (Random + Authentication Tag) */ - uint16_t net_idx; /* NetKeyIndex */ + uint8_t mpb_flags_last; /* Flags of last sent private beacon */ + uint8_t mpb_ivi_last: 1; /* IV Index of last sent private beacon */ + uint8_t mpb_random[13]; /* Random of current private beacon */ + uint8_t mpb_random_last[13]; /* Random of last sent private beacon */ - bool kr_flag; /* Key Refresh Flag */ - uint8_t kr_phase; /* Key Refresh Phase */ + uint8_t private_node_id; /* Private Node Identity State */ +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ - uint8_t node_id; /* Node Identity State */ - uint32_t node_id_start; /* Node Identity started timestamp */ + uint16_t net_idx; /* NetKeyIndex */ - uint8_t auth[8]; /* Beacon Authentication Value */ +#if CONFIG_BLE_MESH_BRC_SRV + uint16_t sbr_net_idx; /* NetKeyIndex of bridged subnet */ +#endif + + bool kr_flag; /* Key Refresh Flag */ + uint8_t kr_phase; /* Key Refresh Phase */ + + uint8_t node_id; /* Node Identity State */ + uint32_t node_id_start; /* Node Identity started timestamp */ + + uint8_t auth[8]; /* Beacon Authentication Value */ struct bt_mesh_subnet_keys { - uint8_t net[16]; /* NetKey */ - uint8_t nid; /* NID */ - uint8_t enc[16]; /* EncKey */ - uint8_t net_id[8]; /* Network ID */ -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) - uint8_t identity[16]; /* IdentityKey */ -#endif - uint8_t privacy[16]; /* PrivacyKey */ - uint8_t beacon[16]; /* BeaconKey */ + uint8_t net[16]; /* NetKey */ + uint8_t nid; /* NID */ + uint8_t enc[16]; /* EncKey */ + uint8_t net_id[8]; /* Network ID */ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + uint8_t identity[16]; /* IdentityKey */ +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ + uint8_t privacy[16]; /* PrivacyKey */ + uint8_t beacon[16]; /* BeaconKey */ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + uint8_t private_beacon[16]; /* Private BeaconKey */ +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +#if CONFIG_BLE_MESH_DF_SRV + uint8_t direct_nid; /* Directed NID */ + uint8_t direct_enc[16]; /* Directed EncKey */ + uint8_t direct_privacy[16]; /* Directed PrivacyKey */ +#endif /* CONFIG_BLE_MESH_DF_SRV */ } keys[2]; + + /* Indicate if proxy privacy is enabled (i.e. sending Mesh Private Beacons + * or Secure Network Beacons) to proxy client. + * + * Note: in Mesh Spec, it describes as "the Proxy Privacy parameter for the + * connection". Here we put the parameter in the subnet, since when sending + * mesh beacon, and for the existing subnets, proxy server should send mesh + * beacon for each of the subnets. + */ + uint8_t proxy_privacy; + +#if CONFIG_BLE_MESH_DF_SRV + uint8_t directed_forwarding; + uint8_t directed_relay; /* Binding with Directed Forwarding state */ + uint8_t directed_proxy; /* Binding with Directed Forwarding state & GATT Proxy state */ + uint8_t directed_proxy_use_default; /* Binding with Directed Proxy state */ + uint8_t directed_friend; + + uint8_t use_directed; + struct { + uint16_t len_present:1, + range_start:15; + uint8_t range_length; + } proxy_client_uar; + + uint8_t path_metric_type:3, + path_lifetime_type:2, + two_way_path:1; + + uint8_t forward_number; + + /* The Discovery Table initially is empty. A Path Origin updates + * its Discovery Table when a Directed Forwarding Initialization + * procedure is executed. A Path Target or a Directed Relay node + * updates its Discovery Table when a PATH_REQUEST message is + * received and processed. + */ + struct bt_mesh_discovery_table { + uint8_t max_disc_entries; + uint8_t max_concurr_init; /* default is 0x02 */ + uint8_t concurr_init; + + bt_mesh_mutex_t mutex; + sys_slist_t entries; + } discovery_table; + + struct bt_mesh_forward_table { + uint8_t max_ford_entries; + uint8_t max_deps_nodes; + uint16_t update_id; + + bt_mesh_mutex_t mutex; + sys_slist_t entries; + } forward_table; + + uint8_t wanted_lanes; + + uint8_t unicast_echo_interval; + + uint8_t multicast_echo_interval; +#endif /* CONFIG_BLE_MESH_DF_SRV */ }; struct bt_mesh_rpl { uint16_t src; bool old_iv; -#if defined(CONFIG_BLE_MESH_SETTINGS) +#if CONFIG_BLE_MESH_SETTINGS bool store; #endif uint32_t seq; }; -#if defined(CONFIG_BLE_MESH_FRIEND) +#if CONFIG_BLE_MESH_FRIEND #define FRIEND_SEG_RX CONFIG_BLE_MESH_FRIEND_SEG_RX #define FRIEND_SUB_LIST_SIZE CONFIG_BLE_MESH_FRIEND_SUB_LIST_SIZE #else @@ -133,7 +220,7 @@ struct bt_mesh_friend { } clear; }; -#if defined(CONFIG_BLE_MESH_LOW_POWER) +#if CONFIG_BLE_MESH_LOW_POWER #define LPN_GROUPS CONFIG_BLE_MESH_LPN_GROUPS #else #define LPN_GROUPS 0 @@ -141,7 +228,7 @@ struct bt_mesh_friend { /* Low Power Node state */ struct bt_mesh_lpn { - enum __packed { + enum __attribute__((packed)) { BLE_MESH_LPN_DISABLED, /* LPN feature is disabled */ BLE_MESH_LPN_CLEAR, /* Clear in progress */ BLE_MESH_LPN_TIMER, /* Waiting for auto timer expiry */ @@ -236,15 +323,14 @@ struct bt_mesh_net { BLE_MESH_ATOMIC_DEFINE(flags, BLE_MESH_FLAG_COUNT); /* Local network interface */ - struct k_work local_work; sys_slist_t local_queue; -#if defined(CONFIG_BLE_MESH_FRIEND) +#if CONFIG_BLE_MESH_FRIEND /* Friend state, unique for each LPN that we're Friends for */ struct bt_mesh_friend frnd[CONFIG_BLE_MESH_FRIEND_LPN_COUNT]; #endif -#if defined(CONFIG_BLE_MESH_LOW_POWER) +#if CONFIG_BLE_MESH_LOW_POWER struct bt_mesh_lpn lpn; /* Low Power Node state */ #endif @@ -254,7 +340,8 @@ struct bt_mesh_net { /* Timer to track duration in current IV Update state */ struct k_delayed_work ivu_timer; - uint8_t dev_key[16]; + uint8_t dev_key[16]; /* Device Key */ + uint8_t dev_key_ca[16]; /* Device Key Candidate */ struct bt_mesh_app_key app_keys[CONFIG_BLE_MESH_APP_KEY_COUNT]; @@ -262,7 +349,7 @@ struct bt_mesh_net { struct bt_mesh_rpl rpl[CONFIG_BLE_MESH_CRPL]; -#if defined(CONFIG_BLE_MESH_PROVISIONER) +#if CONFIG_BLE_MESH_PROVISIONER /* Application keys stored by provisioner */ struct bt_mesh_app_key *p_app_keys[CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT]; /* Next app_idx can be assigned */ @@ -283,6 +370,12 @@ enum bt_mesh_net_if { BLE_MESH_NET_IF_PROXY_CFG, }; +#define BLE_MESH_NONE_BEARER 0 +#define BLE_MESH_ADV_BEARER BIT(0) +#define BLE_MESH_GATT_BEARER BIT(1) +#define BLE_MESH_LOCAL_BEARER BIT(2) +#define BLE_MESH_ALL_BEARERS (BLE_MESH_ADV_BEARER | BLE_MESH_GATT_BEARER) + /* Decoding context for Network/Transport data */ struct bt_mesh_net_rx { struct bt_mesh_subnet *sub; @@ -290,7 +383,7 @@ struct bt_mesh_net_rx { uint32_t seq; /* Sequence Number */ uint8_t old_iv:1, /* iv_index - 1 was used */ new_key:1, /* Data was encrypted with updated key */ - friend_cred:1, /* Data was encrypted with friend cred */ + friend_cred:1 __attribute__((deprecated)), /* Data was encrypted with friend cred */ ctl:1, /* Network Control */ net_if:2, /* Network interface */ local_match:1, /* Matched a local element */ @@ -304,19 +397,27 @@ struct bt_mesh_net_tx { struct bt_mesh_msg_ctx *ctx; uint16_t src; uint8_t xmit; - uint8_t friend_cred:1, + uint8_t friend_cred:1 __attribute__((deprecated)), aszmic:1, - aid: 6; + aid:6; }; extern struct bt_mesh_net bt_mesh; -#define BLE_MESH_NET_IVI_TX (bt_mesh.iv_index - \ - bt_mesh_atomic_test_bit(bt_mesh.flags, \ - BLE_MESH_IVU_IN_PROGRESS)) -#define BLE_MESH_NET_IVI_RX(rx) (bt_mesh.iv_index - (rx)->old_iv) +#define BLE_MESH_NET_IVI_TX (bt_mesh.iv_index - \ + bt_mesh_atomic_test_bit(bt_mesh.flags, \ + BLE_MESH_IVU_IN_PROGRESS)) +#define BLE_MESH_NET_IVI_RX(rx) (bt_mesh.iv_index - (rx)->old_iv) -#define BLE_MESH_NET_HDR_LEN 9 +#define BLE_MESH_NET_HDR_LEN 9 + +#define BLE_MESH_NET_HDR_IVI(pdu) ((pdu)[0] >> 7) +#define BLE_MESH_NET_HDR_NID(pdu) ((pdu)[0] & 0x7F) +#define BLE_MESH_NET_HDR_CTL(pdu) ((pdu)[1] >> 7) +#define BLE_MESH_NET_HDR_TTL(pdu) ((pdu)[1] & 0x7F) +#define BLE_MESH_NET_HDR_SEQ(pdu) (sys_get_be24(&(pdu)[2])) +#define BLE_MESH_NET_HDR_SRC(pdu) (sys_get_be16(&(pdu)[5])) +#define BLE_MESH_NET_HDR_DST(pdu) (sys_get_be16(&(pdu)[7])) void bt_mesh_msg_cache_clear(uint16_t unicast_addr, uint8_t elem_num); @@ -332,9 +433,7 @@ bool bt_mesh_kr_update(struct bt_mesh_subnet *sub, uint8_t new_kr, bool new_key) void bt_mesh_net_revoke_keys(struct bt_mesh_subnet *sub); -int bt_mesh_net_beacon_update(struct bt_mesh_subnet *sub); - -void bt_mesh_rpl_reset(void); +int bt_mesh_net_secure_beacon_update(struct bt_mesh_subnet *sub); bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update); @@ -342,9 +441,9 @@ void bt_mesh_net_sec_update(struct bt_mesh_subnet *sub); struct bt_mesh_subnet *bt_mesh_subnet_get(uint16_t net_idx); -struct bt_mesh_subnet *bt_mesh_subnet_find(const uint8_t net_id[8], uint8_t flags, - uint32_t iv_index, const uint8_t auth[8], - bool *new_key); +struct bt_mesh_subnet *bt_mesh_subnet_find_with_snb(const uint8_t net_id[8], uint8_t flags, + uint32_t iv_index, const uint8_t auth[8], + bool *new_key); int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf, bool proxy); @@ -353,8 +452,8 @@ 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 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); + bool new_key, uint8_t *tx_cred, uint8_t tx_tag, + const struct bt_mesh_send_cb *cb, void *cb_data); int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if, struct bt_mesh_net_rx *rx, struct net_buf_simple *buf); diff --git a/components/bt/esp_ble_mesh/core/nimble_host/adapter.c b/components/bt/esp_ble_mesh/core/nimble_host/adapter.c index 47f0a7ca56e5..398c1526a9a9 100644 --- a/components/bt/esp_ble_mesh/core/nimble_host/adapter.c +++ b/components/bt/esp_ble_mesh/core/nimble_host/adapter.c @@ -70,7 +70,7 @@ static uint8_t bt_mesh_private_key[32]; /* Scan related functions */ static bt_mesh_scan_cb_t *bt_mesh_scan_dev_found_cb; -#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE +#if CONFIG_BLE_MESH_NODE /* the gatt database list to save the attribute table */ static sys_slist_t bt_mesh_gatts_db; @@ -79,8 +79,7 @@ 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 uint8_t bt_mesh_gatts_addr[6]; - -#endif /* defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE */ +#endif /* CONFIG_BLE_MESH_NODE */ int bt_mesh_host_init(void) { @@ -119,12 +118,11 @@ void bt_mesh_hci_init(void) #else bt_mesh_dev.hci_version = ble_hs_hci_get_hci_version(); #endif - return; } static struct ble_gap_disc_params scan_param; #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT + CONFIG_BLE_MESH_GATT_PROXY_CLIENT static struct gattc_prov_info { /* Service to be found depends on the type of adv pkt received */ struct bt_mesh_conn conn; @@ -149,6 +147,7 @@ static int ble_on_subscribe(uint16_t conn_handle, struct bt_mesh_conn *conn = NULL; uint8_t value[2] = {0x01, 0x00}; int i = (int)arg, j, len; + MODLOG_DFLT(INFO, "Subscribe complete; status=%d conn_handle=%d " "attr_handle=%d\n", error->status, conn_handle, attr->handle); @@ -158,8 +157,8 @@ static int ble_on_subscribe(uint16_t conn_handle, break; } } - if (j == ARRAY_SIZE(bt_mesh_gattc_info)) { + if (j == ARRAY_SIZE(bt_mesh_gattc_info)) { conn = &bt_mesh_gattc_info[i].conn; if (bt_mesh_gattc_info[i].ccc_handle != attr->handle) { @@ -190,7 +189,6 @@ static int ble_on_subscribe(uint16_t conn_handle, } } - return 0; } @@ -248,7 +246,6 @@ static int dsc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, return rc; } - static int chr_disced(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_chr *chr, void *arg) { @@ -257,9 +254,11 @@ static int chr_disced(uint16_t conn_handle, const struct ble_gatt_error *error, int i = (int)arg; /* service index */ struct bt_mesh_conn *conn = &bt_mesh_gattc_info[i].conn; const ble_uuid_any_t *uuid = &chr->uuid; + if (chr) { uuid16 = (uint16_t) BLE_UUID16(uuid)->value; } + switch (error->status) { case 0: /* Get Mesh Provisioning/Proxy Data In/Out Characteristic */ @@ -279,6 +278,7 @@ static int chr_disced(uint16_t conn_handle, const struct ble_gatt_error *error, bt_mesh_gattc_info[i].data_out_handle = chr->val_handle; } break; + case BLE_HS_EDONE: /* All characteristics in this service discovered; start discovering * characteristics in the next service. @@ -310,7 +310,6 @@ static int chr_disced(uint16_t conn_handle, const struct ble_gatt_error *error, return rc; } - static int svc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *service, void *arg) { @@ -318,16 +317,19 @@ static int svc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, int rc = 0, i; const ble_uuid_any_t *uuid; uint8_t uuid_length; + switch (error->status) { case 0: if (!service) { return 0; } + uuid = &service->uuid; uuid_length = (uint8_t) (uuid->u.type == BLE_UUID_TYPE_16 ? 2 : 16); if (uuid_length != 2) { return 0; } + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { if (bt_mesh_gattc_info[i].service_uuid == (uint16_t)BLE_UUID16(uuid)->value) { bt_mesh_gattc_info[i].start_handle = service->start_handle; @@ -335,8 +337,8 @@ static int svc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, break; } } - break; + case BLE_HS_EDONE: for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { if (bt_mesh_gattc_info[i].conn.handle == conn_handle) { @@ -348,10 +350,11 @@ static int svc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, BT_ERR("Conn handle 0x%04x not found", conn_handle); return 0; } + conn = &bt_mesh_gattc_info[i].conn; if (bt_mesh_gattc_info[i].start_handle == 0x00 || - bt_mesh_gattc_info[i].end_handle == 0x00 || - (bt_mesh_gattc_info[i].start_handle > bt_mesh_gattc_info[i].end_handle)) { + bt_mesh_gattc_info[i].end_handle == 0x00 || + (bt_mesh_gattc_info[i].start_handle > bt_mesh_gattc_info[i].end_handle)) { bt_mesh_gattc_disconnect(conn); return 0; } @@ -368,16 +371,13 @@ static int svc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, return rc; } - - #endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ static int disc_cb(struct ble_gap_event *event, void *arg) { struct ble_gap_disc_desc *desc; - #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT + CONFIG_BLE_MESH_GATT_PROXY_CLIENT int rc, i; uint8_t notif_data[100]; uint16_t notif_len; @@ -394,12 +394,13 @@ static int disc_cb(struct ble_gap_event *event, void *arg) net_buf_simple_init_with_data(&buf, (void *)desc->data, desc->length_data); 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); + /* TODO: Support Scan Response data length for NimBLE host */ + bt_mesh_scan_dev_found_cb((bt_mesh_addr_t *)&desc->addr, desc->rssi, desc->event_type, &buf, 0); } break; } #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT + CONFIG_BLE_MESH_GATT_PROXY_CLIENT case BLE_GAP_EVENT_CONNECT: if (event->connect.status == 0) { /* Connection successfully established. */ @@ -561,7 +562,6 @@ static int disc_cb(struct ble_gap_event *event, void *arg) static int start_le_scan(uint8_t scan_type, uint16_t interval, uint16_t window, uint8_t filter_dup) { - scan_param.filter_duplicates = filter_dup; scan_param.itvl = interval; scan_param.window = window; @@ -600,7 +600,7 @@ static int set_ad(const struct bt_mesh_adv_data *ad, size_t ad_len, uint8_t *buf return 0; } -#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE +#if CONFIG_BLE_MESH_NODE static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(uint16_t handle); static int gap_event_cb(struct ble_gap_event *event, void *arg) @@ -713,7 +713,6 @@ static int gap_event_cb(struct ble_gap_event *event, void *arg) 0 /* offset */, 0)) > 0) { } } - return 0; case BLE_GAP_EVENT_MTU: @@ -747,12 +746,11 @@ static int gap_event_cb(struct ble_gap_event *event, void *arg) return 0; } #else - static int gap_event_cb(struct ble_gap_event *event, void *arg) { return 0; } -#endif +#endif /* CONFIG_BLE_MESH_NODE */ /* APIs functions */ int bt_le_adv_start(const struct bt_mesh_adv_param *param, @@ -986,8 +984,7 @@ int bt_le_update_white_list(struct bt_mesh_white_list *wl) } #endif -#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE - +#if CONFIG_BLE_MESH_NODE void bt_mesh_gatts_conn_cb_register(struct bt_mesh_conn_cb *cb) { bt_mesh_gatts_conn_cb = cb; @@ -1084,7 +1081,7 @@ struct gatts_incl { uint16_t start_handle; uint16_t end_handle; uint16_t uuid16; -} __packed; +} __attribute__((packed)); ssize_t bt_mesh_gatts_attr_read_included(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, @@ -1134,7 +1131,7 @@ struct gatts_chrc { uint16_t uuid16; uint8_t uuid[16]; }; -} __packed; +} __attribute__((packed)); ssize_t bt_mesh_gatts_attr_read_chrc(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, @@ -1268,11 +1265,13 @@ int bt_mesh_gatts_service_stop(struct bt_mesh_gatt_service *svc) { int rc; uint16_t handle; + const ble_uuid_t *uuid; + if (!svc) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } - const ble_uuid_t *uuid; + if (BLE_MESH_UUID_16(svc->attrs[0].user_data)->val == BT_UUID_MESH_PROXY_VAL) { uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL); } else { @@ -1294,6 +1293,7 @@ int bt_mesh_gatts_service_start(struct bt_mesh_gatt_service *svc) int rc; uint16_t handle; const ble_uuid_t *uuid; + if (BLE_MESH_UUID_16(svc->attrs[0].user_data)->val == BT_UUID_MESH_PROXY_VAL) { uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL); } else { @@ -1314,10 +1314,10 @@ int bt_mesh_gatts_set_local_device_name(const char *name) { return ble_svc_gap_device_name_set(name); } -#endif /* defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE */ +#endif /* CONFIG_BLE_MESH_NODE */ #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT + CONFIG_BLE_MESH_GATT_PROXY_CLIENT void bt_mesh_gattc_conn_cb_register(struct bt_mesh_prov_conn_cb *cb) { bt_mesh_gattc_conn_cb = cb; @@ -1335,7 +1335,7 @@ uint8_t bt_mesh_gattc_get_free_conn_count(void) for (i = 0U; 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) { + bt_mesh_gattc_info[i].service_uuid == 0x0000) { ++count; } } @@ -1376,13 +1376,13 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, uint16_t service_uuid) int i, rc; if (!addr || !memcmp(addr->val, zero, BLE_MESH_ADDR_LEN) || - (addr->type > BLE_ADDR_RANDOM)) { + (addr->type > BLE_ADDR_RANDOM)) { BT_ERR("Invalid remote address"); return -EINVAL; } if (service_uuid != BLE_MESH_UUID_MESH_PROV_VAL && - service_uuid != BLE_MESH_UUID_MESH_PROXY_VAL) { + service_uuid != BLE_MESH_UUID_MESH_PROXY_VAL) { BT_ERR("Invalid service uuid 0x%04x", service_uuid); return -EINVAL; } @@ -1399,7 +1399,7 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, uint16_t service_uuid) /* Find empty element in queue to store device info */ 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)) { + (bt_mesh_gattc_info[i].service_uuid == 0x0000)) { memcpy(bt_mesh_gattc_info[i].addr.val, addr->val, BLE_MESH_ADDR_LEN); bt_mesh_gattc_info[i].addr.type = addr->type; /* Service to be found after exchanging mtu size */ @@ -1438,7 +1438,6 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, uint16_t service_uuid) conn_params.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN; conn_params.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN; - ble_addr_t peer_addr; memcpy(peer_addr.val, addr->val, 6); peer_addr.type = addr->type; @@ -1452,8 +1451,8 @@ static int mtu_cb(uint16_t conn_handle, uint16_t mtu, void *arg) { int i; - if (error->status == 0) { + if (error->status == 0) { for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { if (bt_mesh_gattc_info[i].conn.handle == conn_handle) { bt_mesh_gattc_info[i].mtu = mtu; @@ -1461,11 +1460,10 @@ static int mtu_cb(uint16_t conn_handle, } } } + return 0; } - - void bt_mesh_gattc_exchange_mtu(uint8_t index) { /** Set local MTU and exchange with GATT server. @@ -1549,6 +1547,7 @@ void bt_mesh_gattc_disconnect(struct bt_mesh_conn *conn) BT_ERR("Conn %p not found", conn); return; } + ble_gap_terminate(bt_mesh_gattc_info[i].conn.handle, BLE_ERR_REM_USER_CONN_TERM); } @@ -1577,7 +1576,7 @@ void bt_mesh_conn_unref(struct bt_mesh_conn *conn) BT_DBG("handle %u ref %u", conn->handle, bt_mesh_atomic_get(&conn->ref)); } -#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE +#if CONFIG_BLE_MESH_NODE static int proxy_char_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { @@ -1660,10 +1659,10 @@ static const struct ble_gatt_svc_def svc_defs [] = { 0, /* No more services. */ }, }; -#endif +#endif /* CONFIG_BLE_MESH_NODE */ void gatt_register_cb(struct ble_gatt_register_ctxt *ctxt, - void *arg ) + void *arg) { if (ctxt->op == BLE_GATT_REGISTER_OP_SVC) { if (ble_uuid_cmp(ctxt->svc.svc_def->uuid, BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL)) == 0) { @@ -1680,7 +1679,7 @@ void bt_mesh_gatt_init(void) ble_hs_cfg.gatts_register_cb = gatt_register_cb; -#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE +#if CONFIG_BLE_MESH_NODE static bool init = false; int rc; @@ -1701,10 +1700,10 @@ void bt_mesh_gatt_init(void) init = true; } -#endif +#endif /* CONFIG_BLE_MESH_NODE */ #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT + CONFIG_BLE_MESH_GATT_PROXY_CLIENT 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 = BLE_ATT_MTU_DFLT; @@ -1717,12 +1716,12 @@ void bt_mesh_gatt_init(void) void bt_mesh_gatt_deinit(void) { #if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_SERVER + CONFIG_BLE_MESH_GATT_PROXY_SERVER memset(bt_mesh_gatts_addr, 0, BLE_MESH_ADDR_LEN); #endif #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT + CONFIG_BLE_MESH_GATT_PROXY_CLIENT 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)); @@ -1743,8 +1742,6 @@ void ble_sm_alg_ecc_init(void); void bt_mesh_adapt_init(void) { - BT_DBG("%s", __func__); - /* initialization of P-256 parameters */ ble_sm_alg_ecc_init(); @@ -1838,18 +1835,12 @@ bool bt_mesh_check_public_key(const uint8_t key[64]) int ble_sm_alg_gen_dhkey(uint8_t *peer_pub_key_x, uint8_t *peer_pub_key_y, uint8_t *our_priv_key, uint8_t *out_dhkey); -int bt_mesh_dh_key_gen(const uint8_t remote_pk[64], bt_mesh_dh_key_cb_t cb, const uint8_t idx) +int bt_mesh_dh_key_gen(const uint8_t remote_pub_key[64], uint8_t dhkey[32]) { - uint8_t dhkey[32]; - 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); - - if (cb != NULL) { - cb((const uint8_t *)dhkey, idx); - } - return 0; + return ble_sm_alg_gen_dhkey((uint8_t *)&remote_pub_key[0], (uint8_t *)&remote_pub_key[32], + bt_mesh_private_key, dhkey); } int bt_mesh_encrypt_le(const uint8_t key[16], const uint8_t plaintext[16], @@ -1942,7 +1933,7 @@ int bt_mesh_encrypt_be(const uint8_t key[16], const uint8_t plaintext[16], return 0; } -#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN int bt_mesh_update_exceptional_list(uint8_t sub_code, uint32_t type, void *info) { uint8_t value[6] = {0}; diff --git a/components/bt/esp_ble_mesh/core/prov_common.c b/components/bt/esp_ble_mesh/core/prov_common.c new file mode 100644 index 000000000000..db6109c41bcd --- /dev/null +++ b/components/bt/esp_ble_mesh/core/prov_common.c @@ -0,0 +1,684 @@ +/* Bluetooth Mesh */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "adv.h" +#include "crypto.h" +#include "mesh/mutex.h" +#include "mesh/common.h" +#include "mesh/access.h" +#include "prov_common.h" + +static const struct bt_mesh_prov *prov; + +const struct bt_mesh_prov *bt_mesh_prov_get(void) +{ + return prov; +} + +int bt_mesh_prov_set(const struct bt_mesh_prov *val) +{ + prov = val; + return 0; +} + +void bt_mesh_prov_buf_init(struct net_buf_simple *buf, uint8_t type) +{ + net_buf_simple_reserve(buf, PROV_BUF_HEADROOM); + net_buf_simple_add_u8(buf, type); +} + +#define OUTPUT_OOB_BLINK 0x00 +#define OUTPUT_OOB_BEEP 0x01 +#define OUTPUT_OOB_VIBRATE 0x02 +#define OUTPUT_OOB_NUMBER 0x03 +#define OUTPUT_OOB_STRING 0x04 + +#define INPUT_OOB_PUSH 0x00 +#define INPUT_OOB_TWIST 0x01 +#define INPUT_OOB_NUMBER 0x02 +#define INPUT_OOB_STRING 0x03 + +bt_mesh_output_action_t bt_mesh_prov_output_action(uint8_t action) +{ + switch (action) { + case OUTPUT_OOB_BLINK: + return BLE_MESH_BLINK; + case OUTPUT_OOB_BEEP: + return BLE_MESH_BEEP; + case OUTPUT_OOB_VIBRATE: + return BLE_MESH_VIBRATE; + case OUTPUT_OOB_NUMBER: + return BLE_MESH_DISPLAY_NUMBER; + case OUTPUT_OOB_STRING: + return BLE_MESH_DISPLAY_STRING; + default: + return BLE_MESH_NO_OUTPUT; + } +} + +bt_mesh_input_action_t bt_mesh_prov_input_action(uint8_t action) +{ + switch (action) { + case INPUT_OOB_PUSH: + return BLE_MESH_PUSH; + case INPUT_OOB_TWIST: + return BLE_MESH_TWIST; + case INPUT_OOB_NUMBER: + return BLE_MESH_ENTER_NUMBER; + case INPUT_OOB_STRING: + return BLE_MESH_ENTER_STRING; + default: + return BLE_MESH_NO_INPUT; + } +} + +static const struct { + uint16_t length; +} prov_pdu[] = { + { 1 }, /* Provisioning Invite */ + { 11 }, /* Provisioning Capabilities */ + { 5 }, /* Provisioning Start */ + { 64 }, /* Provisioning Public Key */ + { 0 }, /* Provisioning Input Complete */ + { 16 }, /* Provisioning Confirmation */ + { 16 }, /* Provisioning Random */ + { 33 }, /* Provisioning Data */ + { 0 }, /* Provisioning Complete */ + { 1 }, /* Provisioning Failed */ + { 6 }, /* Provisioning Record Request */ + { 7 }, /* Provisioning Record Response */ + { 0 }, /* Provisioning Records Get */ + { 2 }, /* Provisioning Records List */ +}; + +bool bt_mesh_prov_pdu_check(uint8_t type, uint16_t length, uint8_t *reason) +{ + if (prov_pdu[type].length != length) { +#if CONFIG_BLE_MESH_CERT_BASED_PROV + if ((type == PROV_REC_LIST || type == PROV_REC_RSP) && + length >= prov_pdu[type].length) { + return true; + } +#endif + +#if CONFIG_BLE_MESH_PROV_EPA + /* NOTE: PROV_CONFIRM and PROV_RANDOM PDU may have length 16 or 32 */ + if ((type == PROV_CONFIRM || type == PROV_RANDOM) && length == 32) { + return true; + } +#endif + + BT_ERR("Invalid length %u for type 0x%02x", length, type); + if (reason) { + *reason = PROV_ERR_NVAL_FMT; + } + return false; + } + + return true; +} + +#if CONFIG_BLE_MESH_PB_ADV + +/* 3 transmissions, 20ms interval */ +#define PROV_XMIT BLE_MESH_TRANSMIT(2, 20) + +/* 3 transmissions, 20ms interval */ +#define CLOSE_XMIT BLE_MESH_TRANSMIT(2, 20) + +#define CLOSE_TIMEOUT K_MSEC(100) + +#define BUF_TIMEOUT K_MSEC(400) + +#define XACT_SEG_DATA(link, _seg) (&link->rx.buf->data[20 + (((_seg) - 1) * 23)]) +#define XACT_SEG_RECV(link, _seg) (link->rx.seg &= ~(1 << (_seg))) + +uint8_t node_next_xact_id(struct bt_mesh_prov_link *link) +{ + if (link->tx.id != 0 && link->tx.id != 0xFF) { + return ++link->tx.id; + } + + link->tx.id = 0x80; + return link->tx.id; +} + +uint8_t pvnr_next_xact_id(struct bt_mesh_prov_link *link) +{ + if (link->tx.id > 0x7F) { + link->tx.id = 0; + } + return link->tx.id++; +} + +bool bt_mesh_gen_prov_start(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf, + struct prov_rx *rx, bool *close) +{ + if (link->rx.seg) { + BT_INFO("Get Start while there are unreceived segments"); + return false; + } + + if (link->rx.prev_id == rx->xact_id) { + BT_INFO("Resending ack"); + bt_mesh_gen_prov_ack_send(link, rx->xact_id); + return false; + } + + link->rx.buf->len = net_buf_simple_pull_be16(buf); + link->rx.id = rx->xact_id; + link->rx.fcs = net_buf_simple_pull_u8(buf); + + BT_DBG("len %u last_seg %u total_len %u fcs 0x%02x", buf->len, + START_LAST_SEG(rx->gpc), link->rx.buf->len, link->rx.fcs); + + /* At least one-octet pdu type is needed */ + if (link->rx.buf->len < 1) { + BT_ERR("Ignoring zero-length provisioning PDU"); + if (close) { + *close = true; + } + return false; + } + + if (START_LAST_SEG(rx->gpc) > START_LAST_SEG_MAX) { + BT_ERR("Invalid SegN 0x%02x", START_LAST_SEG(rx->gpc)); + if (close) { + *close = true; + } + return false; + } + + if (link->rx.buf->len > link->rx.buf->size) { + BT_ERR("Too large provisioning PDU (%u bytes)", + link->rx.buf->len); + if (close) { + *close = true; + } + return false; + } + + if (START_LAST_SEG(rx->gpc) > 0 && link->rx.buf->len <= 20) { + BT_ERR("Too small total length for multi-segment PDU"); + if (close) { + *close = true; + } + return false; + } + + link->rx.seg = (1 << (START_LAST_SEG(rx->gpc) + 1)) - 1; + link->rx.last_seg = START_LAST_SEG(rx->gpc); + memcpy(link->rx.buf->data, buf->data, buf->len); + XACT_SEG_RECV(link, 0); + + /* Still have some segments to receive */ + if (link->rx.seg) { + return false; + } + + return true; +} + +bool bt_mesh_gen_prov_cont(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf, + struct prov_rx *rx, bool *close) +{ + uint8_t seg = CONT_SEG_INDEX(rx->gpc); + + BT_DBG("len %u, seg_index %u", buf->len, seg); + + if (link->rx.seg == 0 && link->rx.prev_id == rx->xact_id) { + BT_INFO("Resending ack"); + bt_mesh_gen_prov_ack_send(link, rx->xact_id); + return false; + } + + if (rx->xact_id != link->rx.id) { + BT_WARN("Data for unknown transaction (%u != %u)", + rx->xact_id, link->rx.id); + return false; + } + + if (seg > link->rx.last_seg) { + BT_ERR("Invalid segment index %u", seg); + if (close) { + *close = true; + } + return false; + } + + if (seg == link->rx.last_seg) { + uint8_t expect_len = (link->rx.buf->len - 20 - + (23 * (link->rx.last_seg - 1))); + if (expect_len != buf->len) { + BT_ERR("Incorrect last seg len: %u != %u", + expect_len, buf->len); + if (close) { + *close = true; + } + return false; + } + } + + if ((link->rx.seg & BIT(seg)) == 0) { + BT_INFO("Ignore already received segment"); + return false; + } + + memcpy(XACT_SEG_DATA(link, seg), buf->data, buf->len); + XACT_SEG_RECV(link, seg); + + /* Still have some segments to receive */ + if (link->rx.seg) { + return false; + } + + return true; +} + +static struct net_buf *adv_buf_create(void) +{ + struct net_buf *buf = NULL; + + buf = bt_mesh_adv_create(BLE_MESH_ADV_PROV, BUF_TIMEOUT); + if (!buf) { + BT_ERR("Out of provisioning buffers"); + return NULL; + } + + return buf; +} + +static void ack_complete(uint16_t duration, int err, void *user_data) +{ + struct bt_mesh_prov_link *link = user_data; + + BT_DBG("xact %u complete", link->pending_ack); + + link->pending_ack = PROV_XACT_NVAL; +} + +void bt_mesh_gen_prov_ack_send(struct bt_mesh_prov_link *link, uint8_t xact_id) +{ + static const struct bt_mesh_send_cb cb = { + .start = ack_complete, + }; + const struct bt_mesh_send_cb *complete = NULL; + struct net_buf *buf = NULL; + + BT_DBG("xact_id %u", xact_id); + + if (link->pending_ack == xact_id) { + BT_DBG("Not sending duplicate ack"); + return; + } + + buf = adv_buf_create(); + if (!buf) { + return; + } + + if (link->pending_ack == PROV_XACT_NVAL) { + link->pending_ack = xact_id; + complete = &cb; + } else { + complete = NULL; + } + + net_buf_add_be32(buf, link->link_id); + net_buf_add_u8(buf, xact_id); + net_buf_add_u8(buf, GPC_ACK); + + bt_mesh_adv_send(buf, PROV_XMIT, complete, link); + net_buf_unref(buf); +} + +static void free_segments(struct bt_mesh_prov_link *link) +{ + for (size_t i = 0; i < ARRAY_SIZE(link->tx.buf); i++) { + struct net_buf *buf = link->tx.buf[i]; + + if (!buf) { + break; + } + + link->tx.buf[i] = NULL; + bt_mesh_adv_buf_ref_debug(__func__, buf, 3U, BLE_MESH_BUF_REF_SMALL); + /* Mark as canceled */ + BLE_MESH_ADV(buf)->busy = 0U; + net_buf_unref(buf); + } +} + +void bt_mesh_prov_clear_tx(struct bt_mesh_prov_link *link, bool cancel) +{ + bt_mesh_mutex_lock(&link->buf_lock); + + if (cancel) { + k_delayed_work_cancel(&link->tx.retransmit); + } + + free_segments(link); + + bt_mesh_mutex_unlock(&link->buf_lock); +} + +static void buf_sent(int err, void *user_data) +{ + struct bt_mesh_prov_link *link = user_data; + int32_t timeout = RETRANSMIT_TIMEOUT; + + if (!link->tx.buf[0]) { + return; + } + + /* The following may happen in TWO situations: + * 1. LINK_CLOSING flag is set, and Link Close is sent; + * 2. LINK_CLOSING flag is set, and any other provisioning + * PDU within the adv queue is sent. + * Regarding the second situation, since LINK_CLOSING flag + * is set, so once a pdu is sent, the link could be closed. + */ + if (bt_mesh_atomic_test_bit(link->flags, LINK_CLOSING)) { + timeout = CLOSE_TIMEOUT; + } + + k_delayed_work_submit(&link->tx.retransmit, timeout); +} + +static struct bt_mesh_send_cb buf_sent_cb = { + .end = buf_sent, +}; + +static void prov_retransmit(struct k_work *work) +{ + struct bt_mesh_prov_link *link = work->user_data; + int64_t timeout = TRANSACTION_TIMEOUT; + + if (!bt_mesh_atomic_test_bit(link->flags, LINK_ACTIVE)) { + BT_WARN("Link not active"); + return; + } + +#if CONFIG_BLE_MESH_FAST_PROV + if (link->tx_pdu_type >= link->last_tx_pdu) { + timeout = K_SECONDS(30); + } +#endif /* CONFIG_BLE_MESH_FAST_PROV */ + + /* Use a timeout of 0 ~ 60s for PB-Remote Link Open Procedure. */ + if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE) && + bt_mesh_atomic_test_bit(link->flags, PBR_OPENING)) { + timeout = K_SECONDS(link->pb_remote_timeout); + } + + if (k_uptime_get() - link->tx.start > timeout) { + BT_WARN("Timeout, giving up transaction"); + if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) { + bt_mesh_prov_clear_tx(link, true); + link->pb_remote_cbd = false; + link->pb_remote_reset = false; + link->pb_remote_csp = true; + if (link->pb_remote_close) { + link->pb_remote_close(link, CLOSE_REASON_TIMEOUT); + } + } else { + /* Send Link Close if the device is Provisioner, or + * directly reset adv link if the device is Node. + */ + if (link->retrans_timeout) { + link->retrans_timeout(link, CLOSE_REASON_TIMEOUT); + } + } + return; + } + + if (bt_mesh_atomic_test_bit(link->flags, LINK_CLOSING)) { + if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) { + bt_mesh_prov_clear_tx(link, true); + link->pb_remote_cbd = false; + /* In this case, no need to send Link Close */ + link->pb_remote_reset = true; + if (link->pb_remote_close) { + link->pb_remote_close(link, link->reason); + } + } else { + if (link->reset_adv_link) { + link->reset_adv_link(link, link->reason); + } + } + return; + } + + bt_mesh_mutex_lock(&link->buf_lock); + + for (size_t i = 0; i < ARRAY_SIZE(link->tx.buf); i++) { + struct net_buf *buf = link->tx.buf[i]; + + if (!buf) { + break; + } + + if (BLE_MESH_ADV(buf)->busy) { + continue; + } + + BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); + + if (i + 1 < ARRAY_SIZE(link->tx.buf) && link->tx.buf[i + 1]) { + bt_mesh_adv_send(buf, PROV_XMIT, NULL, NULL); + } else { + bt_mesh_adv_send(buf, PROV_XMIT, &buf_sent_cb, link); + } + } + + bt_mesh_mutex_unlock(&link->buf_lock); +} + +int bt_mesh_prov_retransmit_init(struct bt_mesh_prov_link *link) +{ + link->tx.retransmit.work.user_data = link; + + return k_delayed_work_init(&link->tx.retransmit, prov_retransmit); +} + +static void send_reliable(struct bt_mesh_prov_link *link, uint8_t xmit) +{ + link->tx.start = k_uptime_get(); + + for (size_t i = 0; i < ARRAY_SIZE(link->tx.buf); i++) { + struct net_buf *buf = link->tx.buf[i]; + + if (!buf) { + break; + } + + if (i + 1 < ARRAY_SIZE(link->tx.buf) && link->tx.buf[i + 1]) { + bt_mesh_adv_send(buf, xmit, NULL, NULL); + } else { + bt_mesh_adv_send(buf, xmit, &buf_sent_cb, link); + } + } +} + +int bt_mesh_prov_bearer_ctl_send(struct bt_mesh_prov_link *link, uint8_t op, + void *data, uint8_t data_len) +{ + struct net_buf *buf = NULL; + uint8_t xmit = 0; + + BT_DBG("op 0x%02x data_len %u", op, data_len); + + bt_mesh_prov_clear_tx(link, true); + + buf = adv_buf_create(); + if (!buf) { + return -ENOBUFS; + } + + net_buf_add_be32(buf, link->link_id); + /* Transaction ID, always 0 for Bearer messages */ + net_buf_add_u8(buf, 0x00); + net_buf_add_u8(buf, GPC_CTL(op)); + net_buf_add_mem(buf, data, data_len); + + link->tx.buf[0] = buf; + link->tx.id = 0; /* Set Transaction ID to 0 */ + + xmit = (op == LINK_CLOSE) ? CLOSE_XMIT : PROV_XMIT; + + send_reliable(link, xmit); + + if (op == LINK_CLOSE) { + bt_mesh_atomic_clear_bit(link->flags, LINK_ACTIVE); + bt_mesh_atomic_set_bit(link->flags, LINK_CLOSING); + link->reason = *((uint8_t *)data); + } + + return 0; +} + +static uint8_t last_seg(uint8_t len) +{ + if (len <= START_PAYLOAD_MAX) { + return 0; + } + + len -= START_PAYLOAD_MAX; + + return 1 + (len / CONT_PAYLOAD_MAX); +} + +int bt_mesh_prov_send_adv(struct bt_mesh_prov_link *link, struct net_buf_simple *msg) +{ + struct net_buf *start = NULL, *buf = NULL; + int32_t timeout = PROTOCOL_TIMEOUT; + uint8_t seg_len = 0U, seg_id = 0U; + uint8_t xact_id = 0U; + + BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); + + if (link->next_xact_id == NULL) { + BT_ERR("Empty transaction id cb"); + return -EIO; + } + + bt_mesh_prov_clear_tx(link, true); + + start = adv_buf_create(); + if (!start) { + return -ENOBUFS; + } + + xact_id = link->next_xact_id(link); + net_buf_add_be32(start, link->link_id); + net_buf_add_u8(start, xact_id); + + net_buf_add_u8(start, GPC_START(last_seg(msg->len))); + net_buf_add_be16(start, msg->len); + net_buf_add_u8(start, bt_mesh_fcs_calc(msg->data, msg->len)); + + link->tx.buf[0] = start; + +#if CONFIG_BLE_MESH_FAST_PROV + link->tx_pdu_type = msg->data[0]; +#endif /* CONFIG_BLE_MESH_FAST_PROV */ + + seg_len = MIN(msg->len, START_PAYLOAD_MAX); + BT_DBG("seg 0 len %u: %s", seg_len, bt_hex(msg->data, seg_len)); + net_buf_add_mem(start, msg->data, seg_len); + net_buf_simple_pull(msg, seg_len); + + for (seg_id = 1; msg->len > 0; seg_id++) { + if (seg_id >= ARRAY_SIZE(link->tx.buf)) { + BT_ERR("Too big message (seg_id %d)", seg_id); + bt_mesh_prov_clear_tx(link, false); + return -E2BIG; + } + + buf = adv_buf_create(); + if (!buf) { + bt_mesh_prov_clear_tx(link, false); + return -ENOBUFS; + } + + link->tx.buf[seg_id] = buf; + + seg_len = MIN(msg->len, CONT_PAYLOAD_MAX); + + BT_DBG("seg_id %u len %u: %s", seg_id, seg_len, + bt_hex(msg->data, seg_len)); + + net_buf_add_be32(buf, link->link_id); + net_buf_add_u8(buf, xact_id); + net_buf_add_u8(buf, GPC_CONT(seg_id)); + net_buf_add_mem(buf, msg->data, seg_len); + net_buf_simple_pull(msg, seg_len); + } + + send_reliable(link, PROV_XMIT); + +#if CONFIG_BLE_MESH_FAST_PROV + if (link->tx_pdu_type >= link->last_tx_pdu) { + timeout = K_SECONDS(60); + } +#endif /* CONFIG_BLE_MESH_FAST_PROV */ + + k_delayed_work_submit(&link->prot_timer, timeout); + + return 0; +} +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +int bt_mesh_prov_send(struct bt_mesh_prov_link *link, struct net_buf_simple *buf) +{ +#if CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_atomic_test_bit(link->flags, PB_NPPI)) { + if (link->pb_remote_send) { + BT_INFO("NPPI, send prov pdu 0x%02x", buf->data[0]); + return link->pb_remote_send(link, buf); + } + + BT_ERR("No NPPI send callback provided"); + return -EIO; + } +#endif /* CONFIG_BLE_MESH_RPR_SRV */ + +#if CONFIG_BLE_MESH_RPR_CLI || CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) { + if (link->pb_remote_send) { + BT_INFO("PB-Remote, send prov pdu 0x%02x", buf->data[0]); + return link->pb_remote_send(link, buf); + } + BT_ERR("No PB-Remote send callback provided"); + return -EIO; + } +#endif /* CONFIG_BLE_MESH_RPR_CLI || CONFIG_BLE_MESH_RPR_SRV */ + +#if CONFIG_BLE_MESH_PB_GATT + if (link->conn) { + if (link->pb_gatt_send) { + return link->pb_gatt_send(link, buf); + } + + BT_ERR("No PB-GATT send callback provided"); + return -EIO; + } +#endif /* CONFIG_BLE_MESH_PB_GATT */ + +#if CONFIG_BLE_MESH_PB_ADV + return bt_mesh_prov_send_adv(link, buf); +#endif /* CONFIG_BLE_MESH_PB_ADV */ + + /* Shall not reach here. */ + return 0; +} diff --git a/components/bt/esp_ble_mesh/core/prov_common.h b/components/bt/esp_ble_mesh/core/prov_common.h new file mode 100644 index 000000000000..4671b07f21c1 --- /dev/null +++ b/components/bt/esp_ble_mesh/core/prov_common.h @@ -0,0 +1,333 @@ +/* Bluetooth Mesh */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _PROV_COMMON_H_ +#define _PROV_COMMON_H_ + +#include "mesh/config.h" +#include "mesh/types.h" +#include "mesh/main.h" +#include "mesh/mutex.h" +#include "mesh/timer.h" +#include "mesh/adapter.h" + +#include "mesh_v1.1/utils.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define AUTH_METHOD_NO_OOB 0x00 +#define AUTH_METHOD_STATIC 0x01 +#define AUTH_METHOD_OUTPUT 0x02 +#define AUTH_METHOD_INPUT 0x03 + +#define PROV_ERR_NONE 0x00 +#define PROV_ERR_NVAL_PDU 0x01 +#define PROV_ERR_NVAL_FMT 0x02 +#define PROV_ERR_UNEXP_PDU 0x03 +#define PROV_ERR_CFM_FAILED 0x04 +#define PROV_ERR_RESOURCES 0x05 +#define PROV_ERR_DECRYPT 0x06 +#define PROV_ERR_UNEXP_ERR 0x07 +#define PROV_ERR_ADDR 0x08 +#define PROV_ERR_NVAL_DATA 0x09 + +#define PROV_INVITE 0x00 +#define PROV_CAPABILITIES 0x01 +#define PROV_START 0x02 +#define PROV_PUB_KEY 0x03 +#define PROV_INPUT_COMPLETE 0x04 +#define PROV_CONFIRM 0x05 +#define PROV_RANDOM 0x06 +#define PROV_DATA 0x07 +#define PROV_COMPLETE 0x08 +#define PROV_FAILED 0x09 +#define PROV_REC_REQ 0x0A +#define PROV_REC_RSP 0x0B +#define PROV_REC_GET 0x0C +#define PROV_REC_LIST 0x0D +/* NOTE: PROV_REC_EXP is specifically defined by Espressif. + * It indicates that the expected PDU may be PROV_INVITE, + * PROV_REC_REQ or PROV_REC_GET. */ +#define PROV_REC_EXP 0xFF + +#define REC_RSP_SUCCESS 0x00 +#define REC_RSP_REC_NOT_PRESENT 0x01 +#define REC_RSP_OFFSET_OUT_OF_BOUND 0x02 + +#define CERT_BASED_PROV_SUPPORT(oob) ((oob) & BIT_MASK(7)) +#define PROV_REC_SUPPORT(oob) ((oob) & BIT_MASK(8)) + +#if CONFIG_BLE_MESH_PROV_EPA +#define PROV_ENC_SIZE(link) ((link)->algorithm == PROV_ALG_P256_HMAC_SHA256 ? 32 : 16) +#define PROV_AUTH_SIZE(link) ((link)->algorithm == PROV_ALG_P256_HMAC_SHA256 ? 32 : 16) +#define PROV_CONF_SIZE(link) ((link)->algorithm == PROV_ALG_P256_HMAC_SHA256 ? 32 : 16) +#define PROV_RAND_SIZE(link) ((link)->algorithm == PROV_ALG_P256_HMAC_SHA256 ? 32 : 16) +#define PROV_CONF_SALT_SIZE(link) ((link)->algorithm == PROV_ALG_P256_HMAC_SHA256 ? 32 : 16) +#define PROV_CONF_KEY_SIZE(link) ((link)->algorithm == PROV_ALG_P256_HMAC_SHA256 ? 32 : 16) +#define PROV_AUTH_MAX_SIZE 32 +#define PROV_CONF_MAX_SIZE 32 +#define PROV_RAND_MAX_SIZE 32 +#else /* CONFIG_BLE_MESH_PROV_EPA */ +#define PROV_ENC_SIZE(link) 16 +#define PROV_AUTH_SIZE(link) 16 +#define PROV_CONF_SIZE(link) 16 +#define PROV_RAND_SIZE(link) 16 +#define PROV_CONF_SALT_SIZE(link) 16 +#define PROV_CONF_KEY_SIZE(link) 16 +#define PROV_AUTH_MAX_SIZE 16 +#define PROV_CONF_MAX_SIZE 16 +#define PROV_RAND_MAX_SIZE 16 +#endif /* CONFIG_BLE_MESH_PROV_EPA */ + +#define PROV_STATIC_OOB_AVAILABLE 0x00 +#define PROV_ONLY_OOB_AUTH_SUPPORT 0x01 + +#define PROV_NO_OOB_PUB_KEY 0x00 +#define PROV_OOB_PUB_KEY 0x01 + +#define GPCF(gpc) ((gpc) & 0x03) +#define GPC_START(last_seg) (((last_seg) << 2) | 0x00) +#define GPC_ACK 0x01 +#define GPC_CONT(seg_id) (((seg_id) << 2) | 0x02) +#define GPC_CTL(op) (((op) << 2) | 0x03) + +#define START_PAYLOAD_MAX 20 +#define CONT_PAYLOAD_MAX 23 +#define START_LAST_SEG_MAX 2 + +#define START_LAST_SEG(gpc) ((gpc) >> 2) +#define CONT_SEG_INDEX(gpc) ((gpc) >> 2) + +#define BEARER_CTL(gpc) ((gpc) >> 2) +#define LINK_OPEN 0x00 +#define LINK_ACK 0x01 +#define LINK_CLOSE 0x02 + +#define CLOSE_REASON_SUCCESS 0x00 +#define CLOSE_REASON_TIMEOUT 0x01 +#define CLOSE_REASON_FAILED 0x02 + +#define PROV_DH_KEY_SIZE 32 + +#define PROV_CONF_INPUTS_SIZE 145 + +#define PROV_XACT_NVAL 0xFF + +#define NPPI_DEV_KEY_REFRESH 0x00 +#define NPPI_NODE_ADDR_REFRESH 0x01 +#define NPPI_NODE_COMP_REFRESH 0x02 +#define NPPI_UNKNOWN 0x03 + +#if CONFIG_BLE_MESH_FAST_PROV +#define RETRANSMIT_TIMEOUT K_MSEC(360) +#define TRANSACTION_TIMEOUT K_SECONDS(3) +#define PROTOCOL_TIMEOUT K_SECONDS(6) +#else +#define RETRANSMIT_TIMEOUT K_MSEC(500) +#define TRANSACTION_TIMEOUT K_SECONDS(30) +#define PROTOCOL_TIMEOUT K_SECONDS(60) +#endif /* CONFIG_BLE_MESH_FAST_PROV */ + +#if CONFIG_BLE_MESH_PB_GATT +#define PROV_BUF_HEADROOM 5 +#else +#define PROV_BUF_HEADROOM 0 +#endif + +#define PROV_BUF(name, len) \ + NET_BUF_SIMPLE_DEFINE(name, PROV_BUF_HEADROOM + len) + +#define PROV_RX_BUF_SIZE 65 + +#define BLE_MESH_REC_MAX_ID 0x0013 + +enum { + PROV_ALG_P256_CMAC_AES128, +#if CONFIG_BLE_MESH_PROV_EPA + PROV_ALG_P256_HMAC_SHA256, +#endif + PROV_ALG_METHOD_MAX_NUM, +}; + +struct prov_rx { + uint32_t link_id; + uint8_t xact_id; + uint8_t gpc; +}; + +enum { + CONNECTING, /* Indicate if PB-GATT connection is in progress (Provisioner) */ + REMOTE_PUB_KEY, /* Remote key has been received (Node & Provisioner) */ + OOB_PUB_KEY, /* OOB public key is available (Node) */ + LINK_ACTIVE, /* Link has been opened (Node & Provisioner) */ + WAIT_GEN_DHKEY, /* Waiting for remote public key to generate DHKey (Provisioner) */ + HAVE_DHKEY, /* DHKey has been calculated (Node & Provisioner) */ + SEND_CONFIRM, /* Waiting to send Confirm value (Node & Provisioner) */ + WAIT_NUMBER, /* Waiting for number input from user (Node & Provisioner) */ + WAIT_STRING, /* Waiting for string input from user (Node & Provisioner) */ + LINK_INVALID, /* Error occurred during provisioning (Node) */ + LINK_CLOSING, /* Indicate Link Close is being sent (Provisioner) */ + PB_REMOTE, /* Indicate if the link is used by PB-Remote */ + PB_NPPI, /* Indicate if the link is used by NPPI */ + PBR_OPENING, /* Indicate if the PB-Remote Open Link Procedure is ongoing*/ + WAIT_PK_OBR, /* Waiting for Remote Provisioning Outbound Report for Public Key */ + PROV_NUM_FLAGS, +}; + +struct bt_mesh_prov_link { + BLE_MESH_ATOMIC_DEFINE(flags, PROV_NUM_FLAGS); + + uint8_t expect; /* Next expected PDU */ + + uint8_t public_key; /* Public Key type */ + + uint8_t auth_method; /* Authentication method */ + uint8_t auth_action; /* Authentication action */ + uint8_t auth_size; /* Authentication size */ + + uint8_t auth[PROV_AUTH_MAX_SIZE]; /* Authentication Value */ + uint8_t rand[PROV_RAND_MAX_SIZE]; /* Local Random */ + uint8_t conf[PROV_CONF_MAX_SIZE]; /* Remote Confirmation */ + uint8_t local_conf[PROV_CONF_MAX_SIZE]; /* Local Confirmation */ + + uint8_t dhkey[32]; /* Calculated DHKey */ + + uint8_t algorithm; /* Provisioning Algorithm */ + + uint8_t conf_salt[PROV_CONF_MAX_SIZE]; /* ConfirmationSalt */ + uint8_t conf_key[PROV_CONF_MAX_SIZE]; /* ConfirmationKey */ + uint8_t conf_inputs[145]; /* ConfirmationInputs */ + uint8_t prov_salt[16]; /* Provisioning Salt */ + + bt_mesh_addr_t addr; /* Device address */ + +#if CONFIG_BLE_MESH_NODE + bool invite_recv; /* Indicate if Provisioning Invite is received or not */ +#endif /* CONFIG_BLE_MESH_NODE */ + +#if CONFIG_BLE_MESH_PROVISIONER + uint8_t uuid[16]; /* Check if device is being provisioned */ + uint16_t oob_info; /* OOB info of this device */ + uint8_t element_num; /* Element num of device */ + uint8_t kri_flags; /* Key refresh flag and iv update flag */ + uint16_t assign_addr; /* Application assigned address for the device */ + uint16_t unicast_addr; /* Unicast address allocated for device */ + +#if CONFIG_BLE_MESH_CERT_BASED_PROV + uint16_t record_id_expect; /* The record id field of expected record response PDU */ + uint16_t offset_expect; /* The offset field of expected record response PDU */ + uint16_t max_size; /* The maximum size of provisioning record fragment can receive*/ + + uint8_t *records[BLE_MESH_REC_MAX_ID]; /* Used to store provisioning records */ +#endif /* CONFIG_BLE_MESH_CERT_BASED_PROV */ + +#if CONFIG_BLE_MESH_PB_ADV + uint8_t expect_ack_for; /* Transaction ACK expected for provisioning pdu */ +#endif /* CONFIG_BLE_MESH_PB_ADV */ +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + +#if CONFIG_BLE_MESH_FAST_PROV + uint8_t tx_pdu_type; /* Current transmitted Provisioning PDU type */ + uint8_t last_tx_pdu; /* Type of last sent Provisioning PDU */ +#endif /* CONFIG_BLE_MESH_FAST_PROV */ + +#if CONFIG_BLE_MESH_PB_GATT + struct bt_mesh_conn *conn; /* GATT connection */ + int (*pb_gatt_send)(struct bt_mesh_prov_link *link, struct net_buf_simple *msg); +#endif /* CONFIG_BLE_MESH_PB_GATT */ + +#if CONFIG_BLE_MESH_PB_ADV + uint32_t link_id; /* Link ID */ + uint8_t pending_ack; /* Decide which transaction id ack is pending */ + uint8_t reason; /* Reason of closing provisioning link */ + + /* Callback used to get next transaction id */ + uint8_t (*next_xact_id)(struct bt_mesh_prov_link *link); + + /* Callback used to reset PB-ADV link */ + void (*reset_adv_link)(struct bt_mesh_prov_link *link, uint8_t reason); + + /* Callback used to handle PB-ADV 30s timeout */ + void (*retrans_timeout)(struct bt_mesh_prov_link *link, uint8_t reason); + + struct { + uint8_t id; /* Transaction ID */ + uint8_t prev_id; /* Previous Transaction ID */ + uint8_t seg; /* Bit-field of unreceived segments */ + uint8_t last_seg; /* Last segment (to check length) */ + uint8_t fcs; /* Expected FCS value */ + struct net_buf_simple *buf; /* Incoming buffer */ + } rx; + + struct { + int64_t start; /* Start timestamp of the transaction */ + uint8_t id; /* Transaction id*/ + struct net_buf *buf[3]; /* Pending outgoing buffer(s) */ + struct k_delayed_work retransmit; /* Retransmit timer */ + } tx; + + bt_mesh_mutex_t buf_lock; /* Mutex used to protect PB-ADV buffer */ +#endif /* CONFIG_BLE_MESH_PB_ADV */ + + uint8_t pb_remote_uuid[16]; /* Device UUID used by PB-Remote */ + uint8_t pb_remote_timeout; /* Timeout value used by PB-Remote Open Link procedure */ + uint8_t pb_remote_nppi; /* NPPI Procedure */ + uint8_t pb_remote_pub_key; /* Public Key used by PB-Remote */ + uint8_t pb_remote_cbd:1, /* Indicate if the link is closed by the unprovisioned device */ + pb_remote_csp:1, /* Indicate if the link is close as server can not send pdu */ + pb_remote_reset:1; /* Indicate if the link is reset */ + void *pb_remote_data; /* Remote Provisioning Server/Client PB-Remote information */ + /* Callback used to send Remote Provisioning messages */ + int (*pb_remote_send)(struct bt_mesh_prov_link *link, struct net_buf_simple *buf); + /* Callback used to notify Remote Provisioning Server that link is closed */ + void (*pb_remote_close)(struct bt_mesh_prov_link *link, uint8_t reason); + + struct k_delayed_work prot_timer; /* Protocol timer */ +}; + +const struct bt_mesh_prov *bt_mesh_prov_get(void); + +int bt_mesh_prov_set(const struct bt_mesh_prov *val); + +void bt_mesh_prov_buf_init(struct net_buf_simple *buf, uint8_t type); + +bt_mesh_output_action_t bt_mesh_prov_output_action(uint8_t action); + +bt_mesh_input_action_t bt_mesh_prov_input_action(uint8_t action); + +bool bt_mesh_prov_pdu_check(uint8_t type, uint16_t length, uint8_t *reason); + +int bt_mesh_prov_send_adv(struct bt_mesh_prov_link *link, struct net_buf_simple *msg); + +bool bt_mesh_gen_prov_start(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf, + struct prov_rx *rx, bool *close); + +bool bt_mesh_gen_prov_cont(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf, + struct prov_rx *rx, bool *close); + +void bt_mesh_gen_prov_ack_send(struct bt_mesh_prov_link *link, uint8_t xact_id); + +void bt_mesh_prov_clear_tx(struct bt_mesh_prov_link *link, bool cancel); + +int bt_mesh_prov_retransmit_init(struct bt_mesh_prov_link *link); + +int bt_mesh_prov_bearer_ctl_send(struct bt_mesh_prov_link *link, uint8_t op, + void *data, uint8_t data_len); + +int bt_mesh_prov_send(struct bt_mesh_prov_link *link, struct net_buf_simple *buf); + +#ifdef __cplusplus +} +#endif + +#endif /* _PROV_COMMON_H_ */ diff --git a/components/bt/esp_ble_mesh/core/prov_node.c b/components/bt/esp_ble_mesh/core/prov_node.c index f5458008fdd5..d32e4f2af3c7 100644 --- a/components/bt/esp_ble_mesh/core/prov_node.c +++ b/components/bt/esp_ble_mesh/core/prov_node.c @@ -16,565 +16,183 @@ #include "foundation.h" #include "mesh/common.h" #include "mesh/proxy.h" +#include "proxy_common.h" #include "proxy_server.h" +#include "proxy_client.h" +#include "prov_common.h" #include "prov_node.h" -#if CONFIG_BLE_MESH_NODE +#include "mesh_v1.1/utils.h" -/* 3 transmissions, 20ms interval */ -#define PROV_XMIT BLE_MESH_TRANSMIT(2, 20) - -#define AUTH_METHOD_NO_OOB 0x00 -#define AUTH_METHOD_STATIC 0x01 -#define AUTH_METHOD_OUTPUT 0x02 -#define AUTH_METHOD_INPUT 0x03 - -#define OUTPUT_OOB_BLINK 0x00 -#define OUTPUT_OOB_BEEP 0x01 -#define OUTPUT_OOB_VIBRATE 0x02 -#define OUTPUT_OOB_NUMBER 0x03 -#define OUTPUT_OOB_STRING 0x04 - -#define INPUT_OOB_PUSH 0x00 -#define INPUT_OOB_TWIST 0x01 -#define INPUT_OOB_NUMBER 0x02 -#define INPUT_OOB_STRING 0x03 - -#define PUB_KEY_NO_OOB 0x00 -#define PUB_KEY_OOB 0x01 - -#define PROV_ERR_NONE 0x00 -#define PROV_ERR_NVAL_PDU 0x01 -#define PROV_ERR_NVAL_FMT 0x02 -#define PROV_ERR_UNEXP_PDU 0x03 -#define PROV_ERR_CFM_FAILED 0x04 -#define PROV_ERR_RESOURCES 0x05 -#define PROV_ERR_DECRYPT 0x06 -#define PROV_ERR_UNEXP_ERR 0x07 -#define PROV_ERR_ADDR 0x08 - -#define PROV_INVITE 0x00 -#define PROV_CAPABILITIES 0x01 -#define PROV_START 0x02 -#define PROV_PUB_KEY 0x03 -#define PROV_INPUT_COMPLETE 0x04 -#define PROV_CONFIRM 0x05 -#define PROV_RANDOM 0x06 -#define PROV_DATA 0x07 -#define PROV_COMPLETE 0x08 -#define PROV_FAILED 0x09 - -#define PROV_ALG_P256 0x00 - -#define GPCF(gpc) (gpc & 0x03) -#define GPC_START(last_seg) (((last_seg) << 2) | 0x00) -#define GPC_ACK 0x01 -#define GPC_CONT(seg_id) (((seg_id) << 2) | 0x02) -#define GPC_CTL(op) (((op) << 2) | 0x03) - -#define START_PAYLOAD_MAX 20 -#define CONT_PAYLOAD_MAX 23 -#define START_LAST_SEG_MAX 2 - -#define START_LAST_SEG(gpc) (gpc >> 2) -#define CONT_SEG_INDEX(gpc) (gpc >> 2) - -#define BEARER_CTL(gpc) (gpc >> 2) -#define LINK_OPEN 0x00 -#define LINK_ACK 0x01 -#define LINK_CLOSE 0x02 - -#define CLOSE_REASON_SUCCESS 0x00 -#define CLOSE_REASON_TIMEOUT 0x01 -#define CLOSE_REASON_FAILED 0x02 - -#define XACT_SEG_DATA(_seg) (&link.rx.buf->data[20 + ((_seg - 1) * 23)]) -#define XACT_SEG_RECV(_seg) (link.rx.seg &= ~(1 << (_seg))) - -#define XACT_NVAL 0xff - -enum { - REMOTE_PUB_KEY, /* Remote key has been received */ - OOB_PUB_KEY, /* OOB public key is available */ - LINK_ACTIVE, /* Link has been opened */ - HAVE_DHKEY, /* DHKey has been calculated */ - SEND_CONFIRM, /* Waiting to send Confirm value */ - WAIT_NUMBER, /* Waiting for number input from user */ - WAIT_STRING, /* Waiting for string input from user */ - LINK_INVALID, /* Error occurred during provisioning */ - - NUM_FLAGS, -}; +#if CONFIG_BLE_MESH_NODE -struct prov_link { - BLE_MESH_ATOMIC_DEFINE(flags, NUM_FLAGS); -#if defined(CONFIG_BLE_MESH_PB_GATT) - struct bt_mesh_conn *conn; /* GATT connection */ -#endif - uint8_t dhkey[32]; /* Calculated DHKey */ - uint8_t expect; /* Next expected PDU */ - - bool oob_pk_flag; /* Flag indicates whether using OOB public key */ - - uint8_t oob_method; - uint8_t oob_action; - uint8_t oob_size; - - uint8_t conf[16]; /* Remote Confirmation */ - uint8_t rand[16]; /* Local Random */ - uint8_t auth[16]; /* Authentication Value */ - - uint8_t conf_salt[16]; /* ConfirmationSalt */ - uint8_t conf_key[16]; /* ConfirmationKey */ - uint8_t conf_inputs[145]; /* ConfirmationInputs */ - uint8_t prov_salt[16]; /* Provisioning Salt */ - -#if defined(CONFIG_BLE_MESH_PB_ADV) - uint32_t id; /* Link ID */ - uint8_t tx_pdu_type; /* The previously transmitted Provisioning PDU type */ - - struct { - uint8_t id; /* Transaction ID */ - uint8_t prev_id; /* Previous Transaction ID */ - uint8_t seg; /* Bit-field of unreceived segments */ - uint8_t last_seg; /* Last segment (to check length) */ - uint8_t fcs; /* Expected FCS value */ - struct net_buf_simple *buf; - } rx; - - struct { - /* Start timestamp of the transaction */ - int64_t start; - - /* Transaction id*/ - uint8_t id; - - /* Pending outgoing buffer(s) */ - struct net_buf *buf[3]; - - /* Retransmit timer */ - struct k_delayed_work retransmit; - } tx; +#if CONFIG_BLE_MESH_PB_ADV && !CONFIG_BLE_MESH_PB_GATT +NET_BUF_SIMPLE_DEFINE_STATIC(rx_buf, PROV_RX_BUF_SIZE); #endif - struct k_delayed_work prot_timer; -}; - -struct prov_rx { - uint32_t link_id; - uint8_t xact_id; - uint8_t gpc; -}; - -#define BUF_TIMEOUT K_MSEC(400) +static struct bt_mesh_prov_link prov_link; -#if defined(CONFIG_BLE_MESH_FAST_PROV) -#define RETRANSMIT_TIMEOUT K_MSEC(360) -#define TRANSACTION_TIMEOUT K_SECONDS(3) -#define PROTOCOL_TIMEOUT K_SECONDS(6) -#else -#define RETRANSMIT_TIMEOUT K_MSEC(500) -#define TRANSACTION_TIMEOUT K_SECONDS(30) -#define PROTOCOL_TIMEOUT K_SECONDS(60) -#endif /* CONFIG_BLE_MESH_FAST_PROV */ - -#if defined(CONFIG_BLE_MESH_PB_GATT) -#define PROV_BUF_HEADROOM 5 -#else -#define PROV_BUF_HEADROOM 0 -NET_BUF_SIMPLE_DEFINE_STATIC(rx_buf, 65); +#if CONFIG_BLE_MESH_PB_ADV +static void reset_adv_link(struct bt_mesh_prov_link *link, uint8_t reason); +extern uint8_t node_next_xact_id(struct bt_mesh_prov_link *link); #endif -#define PROV_BUF(name, len) \ - NET_BUF_SIMPLE_DEFINE(name, PROV_BUF_HEADROOM + len) - -static struct prov_link link; - -static const struct bt_mesh_prov *prov; +#if CONFIG_BLE_MESH_PB_GATT +static int prov_send_gatt(struct bt_mesh_prov_link *link, struct net_buf_simple *msg); +#endif -#if defined(CONFIG_BLE_MESH_PB_ADV) -static bt_mesh_mutex_t pb_buf_lock; +static void prov_send_fail_msg(uint8_t err); -static inline void bt_mesh_pb_buf_mutex_new(void) +struct bt_mesh_prov_link *bt_mesh_prov_node_get_link(void) { - if (!pb_buf_lock.mutex) { - bt_mesh_mutex_create(&pb_buf_lock); - } + return &prov_link; } -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_pb_buf_mutex_free(void) +static void close_link(uint8_t reason) { - bt_mesh_mutex_free(&pb_buf_lock); -} -#endif /* CONFIG_BLE_MESH_DEINIT */ + if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) { + if (prov_link.pb_remote_close) { + prov_link.pb_remote_close(&prov_link, reason); + } + return; + } -static inline void bt_mesh_pb_buf_lock(void) -{ - bt_mesh_mutex_lock(&pb_buf_lock); + prov_send_fail_msg(reason); } -static inline void bt_mesh_pb_buf_unlock(void) +void bt_mesh_prov_node_close_link(uint8_t reason) { - bt_mesh_mutex_unlock(&pb_buf_lock); + close_link(reason); } -#endif /* CONFIG_BLE_MESH_PB_ADV */ static void reset_state(void) { - k_delayed_work_cancel(&link.prot_timer); + k_delayed_work_cancel(&prov_link.prot_timer); /* Disable Attention Timer if it was set */ - if (link.conf_inputs[0]) { + if (prov_link.conf_inputs[0]) { bt_mesh_attention(NULL, 0); } -#if defined(CONFIG_BLE_MESH_PB_GATT) - if (link.conn) { - bt_mesh_conn_unref(link.conn); +#if CONFIG_BLE_MESH_PB_GATT + if (prov_link.conn) { + bt_mesh_conn_unref(prov_link.conn); } -#endif +#endif /* CONFIG_BLE_MESH_PB_GATT */ -#if defined(CONFIG_BLE_MESH_PB_ADV) +#if CONFIG_BLE_MESH_PB_ADV /* Clear everything except the retransmit and protocol timer * delayed work objects. */ - (void)memset(&link, 0, offsetof(struct prov_link, tx.retransmit)); - link.rx.prev_id = XACT_NVAL; + (void)memset(&prov_link, 0, offsetof(struct bt_mesh_prov_link, tx.retransmit)); -#if defined(CONFIG_BLE_MESH_PB_GATT) - link.rx.buf = bt_mesh_proxy_server_get_buf(); + prov_link.pending_ack = PROV_XACT_NVAL; + + prov_link.rx.prev_id = PROV_XACT_NVAL; +#if CONFIG_BLE_MESH_PB_GATT + prov_link.rx.buf = bt_mesh_proxy_server_get_buf(); #else net_buf_simple_reset(&rx_buf); - link.rx.buf = &rx_buf; -#endif /* PB_GATT */ - -#else /* !PB_ADV */ - /* Clear everything except the protocol timer (k_delayed_work) */ - (void)memset(&link, 0, offsetof(struct prov_link, prot_timer)); -#endif /* PB_ADV */ -} - -#if defined(CONFIG_BLE_MESH_PB_ADV) -static void buf_sent(int err, void *user_data) -{ - if (!link.tx.buf[0]) { - return; - } - - k_delayed_work_submit(&link.tx.retransmit, RETRANSMIT_TIMEOUT); -} - -static struct bt_mesh_send_cb buf_sent_cb = { - .end = buf_sent, -}; - -static void free_segments(void) -{ - int i; - - bt_mesh_pb_buf_lock(); + prov_link.rx.buf = &rx_buf; +#endif /* CONFIG_BLE_MESH_PB_GATT */ - for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) { - struct net_buf *buf = link.tx.buf[i]; + prov_link.next_xact_id = node_next_xact_id; + prov_link.reset_adv_link = reset_adv_link; + prov_link.retrans_timeout = reset_adv_link; + prov_link.invite_recv = false; - if (!buf) { - break; - } +#if CONFIG_BLE_MESH_FAST_PROV + prov_link.last_tx_pdu = PROV_COMPLETE; +#endif /* CONFIG_BLE_MESH_FAST_PROV */ - link.tx.buf[i] = NULL; - bt_mesh_adv_buf_ref_debug(__func__, buf, 3U, BLE_MESH_BUF_REF_SMALL); - /* Mark as canceled */ - BLE_MESH_ADV(buf)->busy = 0U; - net_buf_unref(buf); - } +#else /* !CONFIG_BLE_MESH_PB_ADV */ + /* Clear everything except the protocol timer (k_delayed_work) */ + (void)memset(&prov_link, 0, offsetof(struct bt_mesh_prov_link, prot_timer)); +#endif /* CONFIG_BLE_MESH_PB_ADV */ - bt_mesh_pb_buf_unlock(); +#if CONFIG_BLE_MESH_PB_GATT + prov_link.pb_gatt_send = prov_send_gatt; +#endif /* CONFIG_BLE_MESH_PB_GATT */ } -static void prov_clear_tx(void) +#if CONFIG_BLE_MESH_PB_ADV +static void reset_adv_link(struct bt_mesh_prov_link *link, uint8_t reason) { - BT_DBG("%s", __func__); - - k_delayed_work_cancel(&link.tx.retransmit); - - free_segments(); -} + ARG_UNUSED(link); -static void reset_adv_link(void) -{ - prov_clear_tx(); + bt_mesh_prov_clear_tx(&prov_link, true); - if (prov->link_close) { - prov->link_close(BLE_MESH_PROV_ADV); + if (bt_mesh_prov_get()->link_close) { + bt_mesh_prov_get()->link_close(BLE_MESH_PROV_ADV, reason); } -#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN /* Remove the link id from exceptional list */ bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE, - BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID, &link.id); + BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID, + &prov_link.link_id); #endif reset_state(); } +#endif /* CONFIG_BLE_MESH_PB_ADV */ -static struct net_buf *adv_buf_create(void) -{ - struct net_buf *buf = NULL; - - buf = bt_mesh_adv_create(BLE_MESH_ADV_PROV, PROV_XMIT, BUF_TIMEOUT); - if (!buf) { - BT_ERR("Out of provisioning buffers"); - return NULL; - } - - return buf; -} - -static uint8_t pending_ack = XACT_NVAL; - -static void ack_complete(uint16_t duration, int err, void *user_data) -{ - BT_DBG("xact %u complete", (uint8_t)pending_ack); - pending_ack = XACT_NVAL; -} - -static void gen_prov_ack_send(uint8_t xact_id) -{ - static const struct bt_mesh_send_cb cb = { - .start = ack_complete, - }; - const struct bt_mesh_send_cb *complete = NULL; - struct net_buf *buf = NULL; - - BT_DBG("xact_id %u", xact_id); - - if (pending_ack == xact_id) { - BT_DBG("Not sending duplicate ack"); - return; - } - - buf = adv_buf_create(); - if (!buf) { - return; - } - - if (pending_ack == XACT_NVAL) { - pending_ack = xact_id; - complete = &cb; - } else { - complete = NULL; - } - - net_buf_add_be32(buf, link.id); - net_buf_add_u8(buf, xact_id); - net_buf_add_u8(buf, GPC_ACK); - - bt_mesh_adv_send(buf, complete, NULL); - net_buf_unref(buf); -} - -static void send_reliable(void) -{ - int i; - - link.tx.start = k_uptime_get(); - - for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) { - struct net_buf *buf = link.tx.buf[i]; - - if (!buf) { - break; - } - - if (i + 1 < ARRAY_SIZE(link.tx.buf) && link.tx.buf[i + 1]) { - bt_mesh_adv_send(buf, NULL, NULL); - } else { - bt_mesh_adv_send(buf, &buf_sent_cb, NULL); - } - } -} - -static int bearer_ctl_send(uint8_t op, void *data, uint8_t data_len) -{ - struct net_buf *buf = NULL; - - BT_DBG("op 0x%02x data_len %u", op, data_len); - - prov_clear_tx(); - - buf = adv_buf_create(); - if (!buf) { - return -ENOBUFS; - } - - net_buf_add_be32(buf, link.id); - /* Transaction ID, always 0 for Bearer messages */ - net_buf_add_u8(buf, 0x00); - net_buf_add_u8(buf, GPC_CTL(op)); - net_buf_add_mem(buf, data, data_len); - - link.tx.buf[0] = buf; - send_reliable(); - - return 0; -} - -static uint8_t last_seg(uint8_t len) -{ - if (len <= START_PAYLOAD_MAX) { - return 0; - } - - len -= START_PAYLOAD_MAX; - - return 1 + (len / CONT_PAYLOAD_MAX); -} - -static inline uint8_t next_transaction_id(void) -{ - if (link.tx.id != 0U && link.tx.id != 0xFF) { - return ++link.tx.id; - } - - link.tx.id = 0x80; - return link.tx.id; -} - -static int prov_send_adv(struct net_buf_simple *msg) +#if CONFIG_BLE_MESH_PB_GATT +static int prov_send_gatt(struct bt_mesh_prov_link *link, struct net_buf_simple *msg) { - struct net_buf *start = NULL, *buf = NULL; - uint8_t seg_len = 0U, seg_id = 0U; - uint8_t xact_id = 0U; - int32_t timeout = PROTOCOL_TIMEOUT; - - BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); - - prov_clear_tx(); - - start = adv_buf_create(); - if (!start) { - return -ENOBUFS; - } - - xact_id = next_transaction_id(); - net_buf_add_be32(start, link.id); - net_buf_add_u8(start, xact_id); - - net_buf_add_u8(start, GPC_START(last_seg(msg->len))); - net_buf_add_be16(start, msg->len); - net_buf_add_u8(start, bt_mesh_fcs_calc(msg->data, msg->len)); - - link.tx.buf[0] = start; - /* Changed by Espressif, get message type */ - link.tx_pdu_type = msg->data[0]; - - seg_len = MIN(msg->len, START_PAYLOAD_MAX); - BT_DBG("seg 0 len %u: %s", seg_len, bt_hex(msg->data, seg_len)); - net_buf_add_mem(start, msg->data, seg_len); - net_buf_simple_pull(msg, seg_len); - - buf = start; - for (seg_id = 1U; msg->len > 0; seg_id++) { - if (seg_id >= ARRAY_SIZE(link.tx.buf)) { - BT_ERR("Too big message (seg_id %d)", seg_id); - free_segments(); - return -E2BIG; - } - - buf = adv_buf_create(); - if (!buf) { - free_segments(); - return -ENOBUFS; - } - - link.tx.buf[seg_id] = buf; - - seg_len = MIN(msg->len, CONT_PAYLOAD_MAX); + int err = 0; - BT_DBG("seg_id %u len %u: %s", seg_id, seg_len, - bt_hex(msg->data, seg_len)); + ARG_UNUSED(link); - net_buf_add_be32(buf, link.id); - net_buf_add_u8(buf, xact_id); - net_buf_add_u8(buf, GPC_CONT(seg_id)); - net_buf_add_mem(buf, msg->data, seg_len); - net_buf_simple_pull(msg, seg_len); + if (!prov_link.conn) { + BT_ERR("PB-GATT send, not connected"); + return -ENOTCONN; } - send_reliable(); - /* Changed by Espressif, add provisioning timeout timer operations. * When sending a provisioning PDU successfully, restart the 60s timer. */ -#if defined(CONFIG_BLE_MESH_FAST_PROV) - if (link.tx_pdu_type >= PROV_COMPLETE) { - timeout = K_SECONDS(60); - } -#endif - k_delayed_work_submit(&link.prot_timer, timeout); - - return 0; -} +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT && CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) { + err = bt_mesh_proxy_client_send(link->conn, BLE_MESH_PROXY_PROV, msg); -#endif /* CONFIG_BLE_MESH_PB_ADV */ - -#if defined(CONFIG_BLE_MESH_PB_GATT) -static int prov_send_gatt(struct net_buf_simple *msg) -{ - int err = 0; - - if (!link.conn) { - return -ENOTCONN; + /* TODO: do we need to check the err here? */ + bt_mesh_rpr_srv_send_outbound_report(link->pb_remote_uuid, true); + } else +#endif + { + err = bt_mesh_proxy_server_send(prov_link.conn, BLE_MESH_PROXY_PROV, msg); } - /* Changed by Espressif, add provisioning timeout timer operations. - * When sending a provisioning PDU successfully, restart the 60s timer. - */ - err = bt_mesh_proxy_server_send(link.conn, BLE_MESH_PROXY_PROV, msg); if (err) { BT_ERR("Failed to send provisioning PDU"); return err; } - k_delayed_work_submit(&link.prot_timer, PROTOCOL_TIMEOUT); + k_delayed_work_submit(&prov_link.prot_timer, PROTOCOL_TIMEOUT); return 0; } #endif /* CONFIG_BLE_MESH_PB_GATT */ -static inline int prov_send(struct net_buf_simple *buf) -{ -#if defined(CONFIG_BLE_MESH_PB_GATT) - if (link.conn) { - return prov_send_gatt(buf); - } -#endif -#if defined(CONFIG_BLE_MESH_PB_ADV) - return prov_send_adv(buf); -#else - return 0; -#endif -} - -static void prov_buf_init(struct net_buf_simple *buf, uint8_t type) -{ - net_buf_simple_reserve(buf, PROV_BUF_HEADROOM); - net_buf_simple_add_u8(buf, type); -} - static void prov_send_fail_msg(uint8_t err) { PROV_BUF(buf, 2); - prov_buf_init(&buf, PROV_FAILED); + /** + * For the case MESH/NODE/PROV/BV-10-C, Node must send Transaction ACK + * before Provisioning Failed message is transmitted. + */ + bt_mesh_gen_prov_ack_send(&prov_link, prov_link.rx.id); + + bt_mesh_prov_buf_init(&buf, PROV_FAILED); net_buf_simple_add_u8(&buf, err); - if (prov_send(&buf)) { + if (bt_mesh_prov_send(&prov_link, &buf)) { BT_ERR("Failed to send Provisioning Failed message"); } - bt_mesh_atomic_set_bit(link.flags, LINK_INVALID); + bt_mesh_atomic_set_bit(prov_link.flags, LINK_INVALID); } static void prov_invite(const uint8_t *data) @@ -587,108 +205,71 @@ static void prov_invite(const uint8_t *data) bt_mesh_attention(NULL, data[0]); } - link.conf_inputs[0] = data[0]; +#if CONFIG_BLE_MESH_CERT_BASED_PROV + /* Indicate prov_invite is received */ + prov_link.invite_recv = true; +#endif + + prov_link.conf_inputs[0] = data[0]; - prov_buf_init(&buf, PROV_CAPABILITIES); + bt_mesh_prov_buf_init(&buf, PROV_CAPABILITIES); /* Number of Elements supported */ net_buf_simple_add_u8(&buf, bt_mesh_elem_count()); - /* Supported algorithms - FIPS P-256 Eliptic Curve */ - net_buf_simple_add_be16(&buf, BIT(PROV_ALG_P256)); + /* NOTE: If bit 1 of the OOB Type field is set to 1, bit 0 of + * the Algorithms field shall be set to 0. + */ + /* Supported algorithms - FIPS P-256 Elliptic Curve */ +#if CONFIG_BLE_MESH_PROV_EPA + if (bt_mesh_prov_get()->oob_type & BIT(PROV_ONLY_OOB_AUTH_SUPPORT)) { + net_buf_simple_add_be16(&buf, BIT(PROV_ALG_P256_HMAC_SHA256)); + } else { + net_buf_simple_add_be16(&buf, (BIT(PROV_ALG_P256_CMAC_AES128) | + BIT(PROV_ALG_P256_HMAC_SHA256))); + } +#else + net_buf_simple_add_be16(&buf, BIT(PROV_ALG_P256_CMAC_AES128)); +#endif /* Public Key Type */ - net_buf_simple_add_u8(&buf, prov->oob_pub_key); + net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->oob_pub_key); - /* Static OOB Type */ - net_buf_simple_add_u8(&buf, prov->static_val ? BIT(0) : 0x00); + /* OOB Type */ + net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->oob_type); /* Output OOB Size */ - net_buf_simple_add_u8(&buf, prov->output_size); + net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->output_size); /* Output OOB Action */ - net_buf_simple_add_be16(&buf, prov->output_actions); + net_buf_simple_add_be16(&buf, bt_mesh_prov_get()->output_actions); /* Input OOB Size */ - net_buf_simple_add_u8(&buf, prov->input_size); + net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->input_size); /* Input OOB Action */ - net_buf_simple_add_be16(&buf, prov->input_actions); + net_buf_simple_add_be16(&buf, bt_mesh_prov_get()->input_actions); - memcpy(&link.conf_inputs[1], &buf.data[1], 11); + memcpy(&prov_link.conf_inputs[1], &buf.data[1], 11); - if (prov_send(&buf)) { + if (bt_mesh_prov_send(&prov_link, &buf)) { BT_ERR("Failed to send capabilities"); return; } - link.expect = PROV_START; + prov_link.expect = PROV_START; } static void prov_capabilities(const uint8_t *data) -{ - uint16_t algorithms = 0U, output_action = 0U, input_action = 0U; - - BT_DBG("Elements: %u", data[0]); - - algorithms = sys_get_be16(&data[1]); - BT_DBG("Algorithms: %u", algorithms); - - BT_DBG("Public Key Type: 0x%02x", data[3]); - BT_DBG("Static OOB Type: 0x%02x", data[4]); - BT_DBG("Output OOB Size: %u", data[5]); - - output_action = sys_get_be16(&data[6]); - BT_DBG("Output OOB Action: 0x%04x", output_action); - - BT_DBG("Input OOB Size: %u", data[8]); - - 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(uint8_t action) -{ - switch (action) { - case OUTPUT_OOB_BLINK: - return BLE_MESH_BLINK; - case OUTPUT_OOB_BEEP: - return BLE_MESH_BEEP; - case OUTPUT_OOB_VIBRATE: - return BLE_MESH_VIBRATE; - case OUTPUT_OOB_NUMBER: - return BLE_MESH_DISPLAY_NUMBER; - case OUTPUT_OOB_STRING: - return BLE_MESH_DISPLAY_STRING; - default: - return BLE_MESH_NO_OUTPUT; - } -} - -static bt_mesh_input_action_t input_action(uint8_t action) -{ - switch (action) { - case INPUT_OOB_PUSH: - return BLE_MESH_PUSH; - case INPUT_OOB_TWIST: - return BLE_MESH_TWIST; - case INPUT_OOB_NUMBER: - return BLE_MESH_ENTER_NUMBER; - case INPUT_OOB_STRING: - return BLE_MESH_ENTER_STRING; - default: - return BLE_MESH_NO_INPUT; - } -} +{} static int prov_auth(uint8_t method, uint8_t action, uint8_t size) { bt_mesh_output_action_t output = 0U; bt_mesh_input_action_t input = 0U; + uint8_t auth_size = 0; + + auth_size = PROV_AUTH_SIZE(&prov_link); switch (method) { case AUTH_METHOD_NO_OOB: @@ -696,30 +277,36 @@ static int prov_auth(uint8_t method, uint8_t action, uint8_t size) return -EINVAL; } - (void)memset(link.auth, 0, sizeof(link.auth)); + (void)memset(prov_link.auth, 0, sizeof(prov_link.auth)); return 0; + case AUTH_METHOD_STATIC: if (action || size) { return -EINVAL; } - memcpy(link.auth + 16 - prov->static_val_len, - prov->static_val, prov->static_val_len); - (void)memset(link.auth, 0, - sizeof(link.auth) - prov->static_val_len); + if (bt_mesh_prov_get()->static_val_len > auth_size) { + memcpy(prov_link.auth, bt_mesh_prov_get()->static_val, auth_size); + } else { + memcpy(prov_link.auth + auth_size - bt_mesh_prov_get()->static_val_len, + bt_mesh_prov_get()->static_val, bt_mesh_prov_get()->static_val_len); + (void)memset(prov_link.auth, 0, + auth_size - bt_mesh_prov_get()->static_val_len); + } + return 0; case AUTH_METHOD_OUTPUT: - output = output_action(action); + output = bt_mesh_prov_output_action(action); if (!output) { return -EINVAL; } - if (!(prov->output_actions & output)) { + if (!(bt_mesh_prov_get()->output_actions & output)) { return -EINVAL; } - if (size > prov->output_size) { + if (size > bt_mesh_prov_get()->output_size) { return -EINVAL; } @@ -740,11 +327,11 @@ static int prov_auth(uint8_t method, uint8_t action, uint8_t size) } str[size] = '\0'; - memcpy(link.auth, str, size); - (void)memset(link.auth + size, 0, - sizeof(link.auth) - size); + memcpy(prov_link.auth, str, size); + (void)memset(prov_link.auth + size, 0, + sizeof(prov_link.auth) - size); - return prov->output_string((char *)str); + return bt_mesh_prov_get()->output_string((char *)str); } else { uint32_t div[8] = { 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 @@ -765,33 +352,33 @@ static int prov_auth(uint8_t method, uint8_t action, uint8_t size) num %= div[size - 1]; } - sys_put_be32(num, &link.auth[12]); - (void)memset(link.auth, 0, 12); + sys_put_be32(num, &prov_link.auth[auth_size - 4]); + (void)memset(prov_link.auth, 0, auth_size - 4); - return prov->output_number(output, num); + return bt_mesh_prov_get()->output_number(output, num); } case AUTH_METHOD_INPUT: - input = input_action(action); + input = bt_mesh_prov_input_action(action); if (!input) { return -EINVAL; } - if (!(prov->input_actions & input)) { + if (!(bt_mesh_prov_get()->input_actions & input)) { return -EINVAL; } - if (size > prov->input_size) { + if (size > bt_mesh_prov_get()->input_size) { return -EINVAL; } if (input == BLE_MESH_ENTER_STRING) { - bt_mesh_atomic_set_bit(link.flags, WAIT_STRING); + bt_mesh_atomic_set_bit(prov_link.flags, WAIT_STRING); } else { - bt_mesh_atomic_set_bit(link.flags, WAIT_NUMBER); + bt_mesh_atomic_set_bit(prov_link.flags, WAIT_NUMBER); } - return prov->input(input, size); + return bt_mesh_prov_get()->input(input, size); default: return -EINVAL; @@ -806,124 +393,193 @@ static void prov_start(const uint8_t *data) BT_INFO("Auth Action: 0x%02x", data[3]); BT_INFO("Auth Size: 0x%02x", data[4]); - if (data[0] != PROV_ALG_P256) { + prov_link.algorithm = data[0]; + if (data[0] >= PROV_ALG_METHOD_MAX_NUM) { BT_ERR("Unknown algorithm 0x%02x", data[0]); - prov_send_fail_msg(PROV_ERR_NVAL_FMT); + close_link(PROV_ERR_NVAL_FMT); return; } - if (data[1] != prov->oob_pub_key) { + /* If bit 1 of the OOB Type field of the Provisioning Capabilities PDU + * is set to 1 (Only OOB authenticated provisioning supported) and any + * of the following conditions is met: + * 1. the Algorithm field of the Provisioning Start PDU is not set to + * BTM_ECDH_P256_CMAC_AES128_AES_CCM and the Provisioning Start PDU + * has the Authentication Method field set to 0x00 (Authentication + * with No OOB). + * 2. the Algorithm field is set to BTM_ECDH_P256_CMAC_AES128_AES_CCM. + * the provisioning protocol shall fail and the message shall be treated + * by the Provisionee as an error in the provisioning protocol. + */ +#if CONFIG_BLE_MESH_PROV_EPA + if ((bt_mesh_prov_get()->oob_type & BIT(PROV_ONLY_OOB_AUTH_SUPPORT)) && + ((data[0] == PROV_ALG_P256_HMAC_SHA256 && data[2] == AUTH_METHOD_NO_OOB) || + data[0] == PROV_ALG_P256_CMAC_AES128)) { + close_link(PROV_ERR_NVAL_FMT); + return; + } +#endif + + if (data[1] != bt_mesh_prov_get()->oob_pub_key) { BT_ERR("Invalid public key type: 0x%02x", data[1]); - prov_send_fail_msg(PROV_ERR_NVAL_FMT); + close_link(PROV_ERR_NVAL_FMT); return; } - memcpy(&link.conf_inputs[12], data, 5); + prov_link.public_key = data[1]; - link.expect = PROV_PUB_KEY; + memcpy(&prov_link.conf_inputs[12], data, 5); + + prov_link.expect = PROV_PUB_KEY; /* If Provisioning Start PDU indicates that provisioner chooses * OOB public key, then callback to the application layer to let * users input public & private key pair. */ - link.oob_pk_flag = data[1] ? true : false; - if (link.oob_pk_flag) { - prov->oob_pub_key_cb(); + if (prov_link.public_key == PROV_OOB_PUB_KEY) { + bt_mesh_prov_get()->oob_pub_key_cb(); } - if (prov_auth(data[2], data[3], data[4]) < 0) { + if (prov_auth(data[2], data[3], data[4])) { BT_ERR("Invalid authentication method: 0x%02x; " "action: 0x%02x; size: 0x%02x", data[2], data[3], data[4]); - prov_send_fail_msg(PROV_ERR_NVAL_FMT); + close_link(PROV_ERR_NVAL_FMT); } } static void send_confirm(void) { uint8_t *local_conf = NULL; - PROV_BUF(cfm, 17); + uint8_t conf_val_size = 0; + uint8_t rand_val_size = 0; + uint8_t conf_salt_size = 0; + uint8_t conf_key_size = 0; - BT_DBG("ConfInputs[0] %s", bt_hex(link.conf_inputs, 64)); - BT_DBG("ConfInputs[64] %s", bt_hex(&link.conf_inputs[64], 64)); - BT_DBG("ConfInputs[128] %s", bt_hex(&link.conf_inputs[128], 17)); + conf_val_size = PROV_CONF_SIZE(&prov_link); + rand_val_size = PROV_RAND_SIZE(&prov_link); + conf_salt_size = PROV_CONF_SALT_SIZE(&prov_link); + conf_key_size = PROV_CONF_KEY_SIZE(&prov_link); - if (bt_mesh_prov_conf_salt(link.conf_inputs, link.conf_salt)) { - BT_ERR("Unable to generate confirmation salt"); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); - return; - } + ARG_UNUSED(conf_salt_size); + ARG_UNUSED(conf_key_size); - BT_DBG("ConfirmationSalt: %s", bt_hex(link.conf_salt, 16)); + PROV_BUF(cfm, (conf_val_size + 1)); - if (bt_mesh_prov_conf_key(link.dhkey, link.conf_salt, link.conf_key)) { - BT_ERR("Unable to generate confirmation key"); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); - return; + BT_DBG("ConfInputs[0] %s", bt_hex(prov_link.conf_inputs, 64)); + BT_DBG("ConfInputs[64] %s", bt_hex(&prov_link.conf_inputs[64], 64)); + BT_DBG("ConfInputs[128] %s", bt_hex(&prov_link.conf_inputs[128], 17)); + + if (prov_link.algorithm == PROV_ALG_P256_CMAC_AES128) { + if (bt_mesh_prov_conf_salt(prov_link.conf_inputs, prov_link.conf_salt)) { + BT_ERR("Unable to generate confirmation salt"); + close_link(PROV_ERR_UNEXP_ERR); + return; + } + + if (bt_mesh_prov_conf_key(prov_link.dhkey, prov_link.conf_salt, + prov_link.conf_key)) { + BT_ERR("Unable to generate confirmation key"); + close_link(PROV_ERR_UNEXP_ERR); + return; + } } +#if CONFIG_BLE_MESH_PROV_EPA + else { + if (bt_mesh_prov_conf_salt_epa(prov_link.conf_inputs, prov_link.conf_salt)) { + BT_ERR("Unable to generate confirmation salt(epa)"); + close_link(PROV_ERR_UNEXP_ERR); + return; + } - BT_DBG("ConfirmationKey: %s", bt_hex(link.conf_key, 16)); + if (bt_mesh_prov_conf_key_epa(prov_link.dhkey, prov_link.auth, + prov_link.conf_salt, prov_link.conf_key)) { + BT_ERR("Unable to generate confirmation key(epa)"); + close_link(PROV_ERR_UNEXP_ERR); + return; + } + } +#endif - if (bt_mesh_rand(link.rand, 16)) { + if (bt_mesh_rand(prov_link.rand, rand_val_size)) { BT_ERR("Unable to generate random number"); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + close_link(PROV_ERR_UNEXP_ERR); return; } - BT_DBG("LocalRandom: %s", bt_hex(link.rand, 16)); + BT_DBG("ConfirmationSalt: %s", bt_hex(prov_link.conf_salt, conf_salt_size)); + BT_DBG("ConfirmationKey: %s", bt_hex(prov_link.conf_key, conf_key_size)); + BT_DBG("LocalRandom: %s", bt_hex(prov_link.rand, rand_val_size)); - prov_buf_init(&cfm, PROV_CONFIRM); + bt_mesh_prov_buf_init(&cfm, PROV_CONFIRM); - local_conf = net_buf_simple_add(&cfm, 16); + local_conf = net_buf_simple_add(&cfm, conf_val_size); - if (bt_mesh_prov_conf(link.conf_key, link.rand, link.auth, - local_conf)) { - BT_ERR("Unable to generate confirmation value"); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); - return; + if (prov_link.algorithm == PROV_ALG_P256_CMAC_AES128) { + if (bt_mesh_prov_conf(prov_link.conf_key, prov_link.rand, + prov_link.auth, local_conf)) { + BT_ERR("Unable to generate confirmation value"); + close_link(PROV_ERR_UNEXP_ERR); + return; + } + } +#if CONFIG_BLE_MESH_PROV_EPA + else { + if (bt_mesh_prov_conf_epa(prov_link.conf_key, prov_link.rand, + local_conf)) { + BT_ERR("Unable to generate confirmation value(epa)"); + close_link(PROV_ERR_UNEXP_ERR); + return; + } } +#endif - if (!memcmp(link.conf, local_conf, 16)) { + if (!memcmp(prov_link.conf, local_conf, conf_val_size)) { BT_ERR("Confirmation value is identical to ours, rejecting."); prov_send_fail_msg(PROV_ERR_NVAL_FMT); return; } - if (prov_send(&cfm)) { + if (bt_mesh_prov_send(&prov_link, &cfm)) { BT_ERR("Unable to send Provisioning Confirm"); return; } - link.expect = PROV_RANDOM; + prov_link.expect = PROV_RANDOM; } static void send_input_complete(void) { PROV_BUF(buf, 1); - prov_buf_init(&buf, PROV_INPUT_COMPLETE); - if (prov_send(&buf)) { + bt_mesh_prov_buf_init(&buf, PROV_INPUT_COMPLETE); + + if (bt_mesh_prov_send(&prov_link, &buf)) { BT_ERR("Failed to send Provisioning Input Complete"); } } int bt_mesh_input_number(uint32_t num) { + uint8_t auth_size = 0; + + auth_size = PROV_AUTH_SIZE(&prov_link); + BT_INFO("%u", num); - if (!bt_mesh_atomic_test_and_clear_bit(link.flags, WAIT_NUMBER)) { + if (!bt_mesh_atomic_test_and_clear_bit(prov_link.flags, WAIT_NUMBER)) { return -EINVAL; } - sys_put_be32(num, &link.auth[12]); + sys_put_be32(num, &prov_link.auth[auth_size - 4]); send_input_complete(); - if (!bt_mesh_atomic_test_bit(link.flags, HAVE_DHKEY)) { + if (!bt_mesh_atomic_test_bit(prov_link.flags, HAVE_DHKEY)) { return 0; } - if (bt_mesh_atomic_test_and_clear_bit(link.flags, SEND_CONFIRM)) { + if (bt_mesh_atomic_test_and_clear_bit(prov_link.flags, SEND_CONFIRM)) { send_confirm(); } @@ -934,116 +590,113 @@ int bt_mesh_input_string(const char *str) { BT_INFO("%s", str); - if (!bt_mesh_atomic_test_and_clear_bit(link.flags, WAIT_STRING)) { + if (!bt_mesh_atomic_test_and_clear_bit(prov_link.flags, WAIT_STRING)) { return -EINVAL; } - (void)memcpy(link.auth, str, prov->input_size); + (void)memcpy(prov_link.auth, str, bt_mesh_prov_get()->input_size); send_input_complete(); - if (!bt_mesh_atomic_test_bit(link.flags, HAVE_DHKEY)) { + if (!bt_mesh_atomic_test_bit(prov_link.flags, HAVE_DHKEY)) { return 0; } - if (bt_mesh_atomic_test_and_clear_bit(link.flags, SEND_CONFIRM)) { + if (bt_mesh_atomic_test_and_clear_bit(prov_link.flags, SEND_CONFIRM)) { send_confirm(); } return 0; } -static void prov_dh_key_cb(const uint8_t key[32], const uint8_t idx) -{ - BT_DBG("%p", key); - - if (!key) { - BT_ERR("DHKey generation failed"); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); - return; - } - - sys_memcpy_swap(link.dhkey, key, 32); - - BT_DBG("DHkey: %s", bt_hex(link.dhkey, 32)); - - bt_mesh_atomic_set_bit(link.flags, HAVE_DHKEY); - - if (bt_mesh_atomic_test_bit(link.flags, WAIT_NUMBER) || - bt_mesh_atomic_test_bit(link.flags, WAIT_STRING)) { - return; - } - - if (bt_mesh_atomic_test_and_clear_bit(link.flags, SEND_CONFIRM)) { - send_confirm(); - } -} - static void send_pub_key(void) { - PROV_BUF(buf, 65); const uint8_t *key = NULL; + uint8_t dhkey[32] = {0}; + PROV_BUF(buf, 65); - /* Copy remote key in little-endian for bt_mesh_dh_key_gen(). + /* Copy remote key in little-endian for generating DHKey. * X and Y halves are swapped independently. Use response - * buffer as a temporary storage location. The validating of - * the remote public key is finished when it is received. + * buffer as a temporary storage location. The validating + * of the remote public key is finished when it is received. */ - sys_memcpy_swap(buf.data, &link.conf_inputs[17], 32); - sys_memcpy_swap(&buf.data[32], &link.conf_inputs[49], 32); + sys_memcpy_swap(buf.data, &prov_link.conf_inputs[17], 32); + sys_memcpy_swap(&buf.data[32], &prov_link.conf_inputs[49], 32); - if (bt_mesh_dh_key_gen(buf.data, prov_dh_key_cb, 0)) { + if (bt_mesh_dh_key_gen(buf.data, dhkey)) { BT_ERR("Unable to generate DHKey"); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + close_link(PROV_ERR_UNEXP_ERR); return; } + sys_memcpy_swap(prov_link.dhkey, dhkey, 32); + + BT_DBG("DHkey: %s", bt_hex(prov_link.dhkey, 32)); + + bt_mesh_atomic_set_bit(prov_link.flags, HAVE_DHKEY); + key = bt_mesh_pub_key_get(); if (!key) { BT_ERR("No public key available"); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + close_link(PROV_ERR_UNEXP_ERR); return; } BT_DBG("Local Public Key: %s", bt_hex(key, 64)); - prov_buf_init(&buf, PROV_PUB_KEY); + bt_mesh_prov_buf_init(&buf, PROV_PUB_KEY); /* Swap X and Y halves independently to big-endian */ sys_memcpy_swap(net_buf_simple_add(&buf, 32), key, 32); sys_memcpy_swap(net_buf_simple_add(&buf, 32), &key[32], 32); - memcpy(&link.conf_inputs[81], &buf.data[1], 64); + memcpy(&prov_link.conf_inputs[81], &buf.data[1], 64); - if (prov_send(&buf)) { + if (bt_mesh_prov_send(&prov_link, &buf)) { BT_ERR("Failed to send Public Key"); return; } - link.expect = PROV_CONFIRM; + prov_link.expect = PROV_CONFIRM; } static int bt_mesh_calc_dh_key(void) { - NET_BUF_SIMPLE_DEFINE(buf, 64); + uint8_t pub_key[64] = {0}; + uint8_t dhkey[32] = {0}; - /* Copy remote key in little-endian for bt_mesh_dh_key_gen(). + /* Copy remote key in little-endian for generating DHKey. * X and Y halves are swapped independently. */ - net_buf_simple_reset(&buf); - sys_memcpy_swap(buf.data, &link.conf_inputs[17], 32); - sys_memcpy_swap(&buf.data[32], &link.conf_inputs[49], 32); + sys_memcpy_swap(&pub_key[0], &prov_link.conf_inputs[17], 32); + sys_memcpy_swap(&pub_key[32], &prov_link.conf_inputs[49], 32); - if (bt_mesh_dh_key_gen(buf.data, prov_dh_key_cb, 0)) { + if (bt_mesh_dh_key_gen(pub_key, dhkey)) { BT_ERR("Unable to generate DHKey"); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + close_link(PROV_ERR_UNEXP_ERR); return -EIO; } + sys_memcpy_swap(prov_link.dhkey, dhkey, 32); + + BT_DBG("DHkey: %s", bt_hex(prov_link.dhkey, 32)); + + bt_mesh_atomic_set_bit(prov_link.flags, HAVE_DHKEY); + + if (bt_mesh_atomic_test_bit(prov_link.flags, WAIT_NUMBER) || + bt_mesh_atomic_test_bit(prov_link.flags, WAIT_STRING)) { + return 0; + } + + if (bt_mesh_atomic_test_and_clear_bit(prov_link.flags, SEND_CONFIRM)) { + send_confirm(); + } + return 0; } -int bt_mesh_set_oob_pub_key(const uint8_t pub_key_x[32], const uint8_t pub_key_y[32], +int bt_mesh_set_oob_pub_key(const uint8_t pub_key_x[32], + const uint8_t pub_key_y[32], const uint8_t pri_key[32]) { if (!pub_key_x || !pub_key_y || !pri_key) { @@ -1055,14 +708,14 @@ int bt_mesh_set_oob_pub_key(const uint8_t pub_key_x[32], const uint8_t pub_key_y * X and Y halves are swapped independently. * And set input private key to mesh_bearer_adapt.c */ - sys_memcpy_swap(&link.conf_inputs[81], pub_key_x, 32); - sys_memcpy_swap(&link.conf_inputs[81] + 32, pub_key_y, 32); + sys_memcpy_swap(&prov_link.conf_inputs[81], pub_key_x, 32); + sys_memcpy_swap(&prov_link.conf_inputs[81] + 32, pub_key_y, 32); bt_mesh_set_private_key(pri_key); - bt_mesh_atomic_set_bit(link.flags, OOB_PUB_KEY); + bt_mesh_atomic_set_bit(prov_link.flags, OOB_PUB_KEY); /* If remote public key is not got, just return */ - if (!bt_mesh_atomic_test_bit(link.flags, REMOTE_PUB_KEY)) { + if (!bt_mesh_atomic_test_bit(prov_link.flags, REMOTE_PUB_KEY)) { return 0; } @@ -1081,47 +734,41 @@ static void prov_pub_key(const uint8_t *data) */ if (!bt_mesh_check_public_key(data)) { BT_ERR("Invalid public key"); - prov_send_fail_msg(PROV_ERR_UNEXP_PDU); + close_link(PROV_ERR_UNEXP_PDU); return; } - memcpy(&link.conf_inputs[17], data, 64); - bt_mesh_atomic_set_bit(link.flags, REMOTE_PUB_KEY); - - if (!bt_mesh_pub_key_get()) { - /* Clear retransmit timer */ -#if defined(CONFIG_BLE_MESH_PB_ADV) - prov_clear_tx(); -#endif - BT_WARN("Waiting for a local public key"); - return; - } + memcpy(&prov_link.conf_inputs[17], data, 64); + bt_mesh_atomic_set_bit(prov_link.flags, REMOTE_PUB_KEY); - if (!link.oob_pk_flag) { + if (prov_link.public_key == PROV_NO_OOB_PUB_KEY) { send_pub_key(); } else { - link.expect = PROV_CONFIRM; + prov_link.expect = PROV_CONFIRM; } } static void prov_input_complete(const uint8_t *data) -{ - BT_DBG("%s", __func__); -} +{} static void prov_confirm(const uint8_t *data) { - BT_DBG("Remote Confirm: %s", bt_hex(data, 16)); + uint8_t conf_val_size = 0; - memcpy(link.conf, data, 16); + conf_val_size = PROV_CONF_SIZE(&prov_link); - if (!bt_mesh_atomic_test_bit(link.flags, HAVE_DHKEY)) { -#if defined(CONFIG_BLE_MESH_PB_ADV) - prov_clear_tx(); + BT_DBG("Remote Confirm: %s", bt_hex(data, conf_val_size)); + + memcpy(prov_link.conf, data, conf_val_size); + + if (!bt_mesh_atomic_test_bit(prov_link.flags, HAVE_DHKEY)) { +#if CONFIG_BLE_MESH_PB_ADV + bt_mesh_prov_clear_tx(&prov_link, true); #endif - bt_mesh_atomic_set_bit(link.flags, SEND_CONFIRM); + bt_mesh_atomic_set_bit(prov_link.flags, SEND_CONFIRM); /* If using OOB public key and it has already got, calculates dhkey */ - if (link.oob_pk_flag && bt_mesh_atomic_test_bit(link.flags, OOB_PUB_KEY)) { + if (prov_link.public_key == PROV_OOB_PUB_KEY && + bt_mesh_atomic_test_bit(prov_link.flags, OOB_PUB_KEY)) { bt_mesh_calc_dh_key(); } } else { @@ -1131,49 +778,77 @@ static void prov_confirm(const uint8_t *data) static void prov_random(const uint8_t *data) { - PROV_BUF(rnd, 17); - uint8_t conf_verify[16] = {0}; + uint8_t conf_verify[32] = {0}; + uint8_t rand_val_size = 0; + uint8_t conf_val_size = 0; - BT_DBG("Remote Random: %s", bt_hex(data, 16)); + rand_val_size = PROV_RAND_SIZE(&prov_link); + conf_val_size = PROV_CONF_SIZE(&prov_link); + PROV_BUF(rnd, rand_val_size); - if (bt_mesh_prov_conf(link.conf_key, data, link.auth, conf_verify)) { - BT_ERR("Unable to calculate confirmation verification"); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); - return; + BT_INFO("Remote Random: %s", bt_hex(data, rand_val_size)); + + if (prov_link.algorithm == PROV_ALG_P256_CMAC_AES128) { + if (bt_mesh_prov_conf(prov_link.conf_key, data, prov_link.auth, conf_verify)) { + BT_ERR("Unable to calculate confirmation verification"); + close_link(PROV_ERR_UNEXP_ERR); + return; + } } +#if CONFIG_BLE_MESH_PROV_EPA + else { + if (bt_mesh_prov_conf_epa(prov_link.conf_key, data, conf_verify)) { + BT_ERR("Unable to calculate confirmation verification"); + close_link(PROV_ERR_UNEXP_ERR); + return; + } + } +#endif - if (memcmp(conf_verify, link.conf, 16)) { + if (memcmp(conf_verify, prov_link.conf, conf_val_size)) { BT_ERR("Invalid confirmation value"); - BT_DBG("Received: %s", bt_hex(link.conf, 16)); - BT_DBG("Calculated: %s", bt_hex(conf_verify, 16)); - prov_send_fail_msg(PROV_ERR_CFM_FAILED); + BT_ERR("Received: %s", bt_hex(prov_link.conf, conf_val_size)); + BT_ERR("Calculated: %s", bt_hex(conf_verify, conf_val_size)); + close_link(PROV_ERR_CFM_FAILED); return; } - prov_buf_init(&rnd, PROV_RANDOM); - net_buf_simple_add_mem(&rnd, link.rand, 16); + bt_mesh_prov_buf_init(&rnd, PROV_RANDOM); + net_buf_simple_add_mem(&rnd, prov_link.rand, rand_val_size); - if (prov_send(&rnd)) { + if (bt_mesh_prov_send(&prov_link, &rnd)) { BT_ERR("Failed to send Provisioning Random"); return; } - if (bt_mesh_prov_salt(link.conf_salt, data, link.rand, - link.prov_salt)) { - BT_ERR("Failed to generate provisioning salt"); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); - return; + if (prov_link.algorithm == PROV_ALG_P256_CMAC_AES128) { + if (bt_mesh_prov_salt(prov_link.conf_salt, data, prov_link.rand, + prov_link.prov_salt)) { + BT_ERR("Failed to generate provisioning salt"); + close_link(PROV_ERR_UNEXP_ERR); + return; + } } +#if CONFIG_BLE_MESH_PROV_EPA + else { + if (bt_mesh_prov_salt_epa(prov_link.conf_salt, data, prov_link.rand, + prov_link.prov_salt)) { + BT_ERR("Failed to generate provisioning salt"); + close_link(PROV_ERR_UNEXP_ERR); + return; + } + } +#endif - BT_DBG("ProvisioningSalt: %s", bt_hex(link.prov_salt, 16)); + BT_DBG("ProvisioningSalt: %s", bt_hex(prov_link.prov_salt, 16)); - link.expect = PROV_DATA; + prov_link.expect = PROV_DATA; } static inline bool is_pb_gatt(void) { -#if defined(CONFIG_BLE_MESH_PB_GATT) - return !!link.conn; +#if CONFIG_BLE_MESH_PB_GATT + return !!prov_link.conn; #else return false; #endif @@ -1181,33 +856,31 @@ static inline bool is_pb_gatt(void) static void prov_data(const uint8_t *data) { - PROV_BUF(msg, 1); uint8_t session_key[16] = {0}; - uint8_t nonce[13] = {0}; + bool identity_enable = false; uint8_t dev_key[16] = {0}; - uint8_t pdu[25] = {0}; - uint8_t flags = 0U; + uint8_t nonce[13] = {0}; uint32_t iv_index = 0U; - uint16_t addr = 0U; uint16_t net_idx = 0U; + uint8_t pdu[25] = {0}; + uint16_t addr = 0U; + uint8_t flags = 0U; + PROV_BUF(msg, 1); int err = 0; - bool identity_enable = false; - - BT_DBG("%s", __func__); - err = bt_mesh_session_key(link.dhkey, link.prov_salt, session_key); + err = bt_mesh_session_key(prov_link.dhkey, prov_link.prov_salt, session_key); if (err) { BT_ERR("Unable to generate session key"); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + close_link(PROV_ERR_UNEXP_ERR); return; } BT_DBG("SessionKey: %s", bt_hex(session_key, 16)); - err = bt_mesh_prov_nonce(link.dhkey, link.prov_salt, nonce); + err = bt_mesh_prov_nonce(prov_link.dhkey, prov_link.prov_salt, nonce); if (err) { BT_ERR("Unable to generate session nonce"); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + close_link(PROV_ERR_UNEXP_ERR); return; } @@ -1216,35 +889,69 @@ static void prov_data(const uint8_t *data) err = bt_mesh_prov_decrypt(session_key, nonce, data, pdu); if (err) { BT_ERR("Unable to decrypt provisioning data"); - prov_send_fail_msg(PROV_ERR_DECRYPT); + close_link(PROV_ERR_DECRYPT); return; } - err = bt_mesh_dev_key(link.dhkey, link.prov_salt, dev_key); + net_idx = sys_get_be16(&pdu[16]); + flags = pdu[18]; + iv_index = sys_get_be32(&pdu[19]); + addr = sys_get_be16(&pdu[23]); + + BT_DBG("net_idx %u iv_index 0x%08x, addr 0x%04x", + net_idx, iv_index, addr); + +#if CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_atomic_test_bit(prov_link.flags, PB_NPPI)) { + uint8_t reason = 0; + if (bt_mesh_rpr_srv_nppi_check(prov_link.pb_remote_nppi, pdu, net_idx, + iv_index, addr, &reason) == false) { + close_link(reason); + return; + } + } +#endif /* CONFIG_BLE_MESH_RPR_SRV */ + + err = bt_mesh_dev_key(prov_link.dhkey, prov_link.prov_salt, dev_key); if (err) { BT_ERR("Unable to generate device key"); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + close_link(PROV_ERR_UNEXP_ERR); return; } BT_DBG("DevKey: %s", bt_hex(dev_key, 16)); - net_idx = sys_get_be16(&pdu[16]); - flags = pdu[18]; - iv_index = sys_get_be32(&pdu[19]); - addr = sys_get_be16(&pdu[23]); +#if CONFIG_BLE_MESH_RPR_SRV + /* Store NPPI data */ + if (bt_mesh_atomic_test_bit(prov_link.flags, PB_NPPI)) { + err = bt_mesh_rpr_srv_store_nppi_data(prov_link.pb_remote_uuid, + pdu, net_idx, flags, + iv_index, addr, dev_key); + if (err) { + close_link(PROV_ERR_UNEXP_ERR); + return; + } + } +#endif /* CONFIG_BLE_MESH_RPR_SRV */ - BT_DBG("net_idx %u iv_index 0x%08x, addr 0x%04x", - net_idx, iv_index, addr); + bt_mesh_prov_buf_init(&msg, PROV_COMPLETE); - prov_buf_init(&msg, PROV_COMPLETE); - if (prov_send(&msg)) { + if (bt_mesh_prov_send(&prov_link, &msg)) { BT_ERR("Failed to send Provisioning Complete"); return; } /* Ignore any further PDUs on this link */ - link.expect = 0U; + prov_link.expect = 0U; + + reset_state(); + +#if CONFIG_BLE_MESH_RPR_SRV + /* For NPPI, no need to perform the following actions */ + if (bt_mesh_atomic_test_bit(prov_link.flags, PB_NPPI)) { + return; + } +#endif /* CONFIG_BLE_MESH_RPR_SRV */ /* Store info, since bt_mesh_provision() will end up clearing it */ if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { @@ -1268,82 +975,46 @@ static void prov_data(const uint8_t *data) } static void prov_complete(const uint8_t *data) -{ - BT_DBG("%s", __func__); -} +{} static void prov_failed(const uint8_t *data) { BT_WARN("Error: 0x%02x", data[0]); + +#if CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) { + prov_link.pb_remote_cbd = true; + /* In this case, no need to send Link Close */ + prov_link.pb_remote_reset = true; + close_link(data[0]); + return; + } +#endif /* CONFIG_BLE_MESH_RPR_SRV */ } static const struct { void (*func)(const uint8_t *data); - uint16_t len; + bool pb_remote; /* Indicate if the PDU could be received when PB-Remote is used */ } prov_handlers[] = { - { prov_invite, 1 }, - { prov_capabilities, 11 }, - { prov_start, 5, }, - { prov_pub_key, 64 }, - { prov_input_complete, 0 }, - { prov_confirm, 16 }, - { prov_random, 16 }, - { prov_data, 33 }, - { prov_complete, 0 }, - { prov_failed, 1 }, -}; - -#if defined(CONFIG_BLE_MESH_PB_ADV) -static void prov_retransmit(struct k_work *work) -{ - int64_t timeout = TRANSACTION_TIMEOUT; - int i; - - BT_DBG("%s", __func__); - - if (!bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE)) { - BT_WARN("Link not active"); - return; - } - -#if defined(CONFIG_BLE_MESH_FAST_PROV) - /* When Provisioning Failed PDU is sent, 3s may be used here. */ - if (link.tx_pdu_type >= PROV_COMPLETE) { - timeout = K_SECONDS(30); - } + { prov_invite, false }, + { prov_capabilities, true }, + { prov_start, false }, + { prov_pub_key, true }, + { prov_input_complete, true }, + { prov_confirm, true }, + { prov_random, true }, + { prov_data, false }, + { prov_complete, true }, + { prov_failed, true }, +#if CONFIG_BLE_MESH_CERT_BASED_PROV + { bt_mesh_prov_record_req, false }, + { bt_mesh_prov_record_rsp, false }, + { bt_mesh_prov_records_get, false }, + { bt_mesh_prov_records_list, false }, #endif - if (k_uptime_get() - link.tx.start > timeout) { - BT_WARN("Node timeout, giving up transaction"); - reset_adv_link(); - return; - } - - bt_mesh_pb_buf_lock(); - - for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) { - struct net_buf *buf = link.tx.buf[i]; - - if (!buf) { - break; - } - - if (BLE_MESH_ADV(buf)->busy) { - continue; - } - - BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); - - if (i + 1 < ARRAY_SIZE(link.tx.buf) && link.tx.buf[i + 1]) { - bt_mesh_adv_send(buf, NULL, NULL); - } else { - bt_mesh_adv_send(buf, &buf_sent_cb, NULL); - } - - } - - bt_mesh_pb_buf_unlock(); -} +}; +#if CONFIG_BLE_MESH_PB_ADV static void link_open(struct prov_rx *rx, struct net_buf_simple *buf) { BT_DBG("len %u", buf->len); @@ -1353,52 +1024,93 @@ static void link_open(struct prov_rx *rx, struct net_buf_simple *buf) return; } - if (bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE)) { + if (bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE)) { /* Send another link ack if the provisioner missed the last */ - if (link.id == rx->link_id && link.expect == PROV_INVITE) { + if (prov_link.link_id == rx->link_id && prov_link.expect == PROV_INVITE) { BT_DBG("Resending link ack"); - bearer_ctl_send(LINK_ACK, NULL, 0); + bt_mesh_prov_bearer_ctl_send(&prov_link, LINK_ACK, NULL, 0); } else { BT_INFO("Ignoring bearer open: link already active"); } - return; } - if (memcmp(buf->data, prov->uuid, 16)) { + if (memcmp(buf->data, bt_mesh_prov_get()->uuid, 16)) { BT_DBG("Bearer open message not for us"); return; } - if (prov->link_open) { - prov->link_open(BLE_MESH_PROV_ADV); + if (bt_mesh_prov_get()->link_open) { + bt_mesh_prov_get()->link_open(BLE_MESH_PROV_ADV); } - link.id = rx->link_id; - bt_mesh_atomic_set_bit(link.flags, LINK_ACTIVE); - net_buf_simple_reset(link.rx.buf); + prov_link.link_id = rx->link_id; + + bt_mesh_atomic_set_bit(prov_link.flags, LINK_ACTIVE); + + net_buf_simple_reset(prov_link.rx.buf); -#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN /* Add the link id into exceptional list */ bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD, - BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID, &link.id); + BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID, + &prov_link.link_id); #endif - bearer_ctl_send(LINK_ACK, NULL, 0); + bt_mesh_prov_bearer_ctl_send(&prov_link, LINK_ACK, NULL, 0); - link.expect = PROV_INVITE; +#if CONFIG_BLE_MESH_CERT_BASED_PROV + if (PROV_REC_SUPPORT(bt_mesh_prov_get()->oob_info)) { + prov_link.expect = PROV_REC_EXP; + } else +#endif + { + prov_link.expect = PROV_INVITE; + } } static void link_ack(struct prov_rx *rx, struct net_buf_simple *buf) { BT_DBG("len %u", buf->len); + +#if CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) { + if (bt_mesh_atomic_test_and_clear_bit(prov_link.flags, PBR_OPENING)) { + BT_INFO("PB-Remote, receive Link ACK"); + /* Cancel the retransmit timer, in case timeout is caused. */ + bt_mesh_prov_clear_tx(&prov_link, true); + bt_mesh_rpr_srv_recv_link_ack(prov_link.pb_remote_uuid, true); + } else { + BT_INFO("Link ACK for PB-Remote already received"); + } + } +#endif /* CONFIG_BLE_MESH_RPR_SRV */ } static void link_close(struct prov_rx *rx, struct net_buf_simple *buf) { + uint8_t reason = 0; + BT_DBG("len %u", buf->len); - reset_adv_link(); + if (buf->len != 1) { + BT_ERR("Invalid Link Close length %d", buf->len); + return; + } + + reason = net_buf_simple_pull_u8(buf); + +#if CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) { + prov_link.pb_remote_cbd = true; + /* In this case, no need to send Link Close */ + prov_link.pb_remote_reset = true; + close_link(reason); + return; + } +#endif /* CONFIG_BLE_MESH_RPR_SRV */ + + reset_adv_link(&prov_link, reason); } static void gen_prov_ctl(struct prov_rx *rx, struct net_buf_simple *buf) @@ -1407,22 +1119,31 @@ static void gen_prov_ctl(struct prov_rx *rx, struct net_buf_simple *buf) switch (BEARER_CTL(rx->gpc)) { case LINK_OPEN: +#if CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) { + return; + } +#endif /* CONFIG_BLE_MESH_RPR_SRV */ + link_open(rx, buf); break; + case LINK_ACK: - if (!bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE)) { + if (!bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE)) { return; } link_ack(rx, buf); break; + case LINK_CLOSE: - if (!bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE)) { + if (!bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE)) { return; } link_close(rx, buf); break; + default: BT_ERR("Unknown bearer opcode: 0x%02x", BEARER_CTL(rx->gpc)); return; @@ -1431,25 +1152,32 @@ static void gen_prov_ctl(struct prov_rx *rx, struct net_buf_simple *buf) static void prov_msg_recv(void) { - uint8_t type = link.rx.buf->data[0]; + uint8_t err_code = 0; + uint8_t type = 0; - BT_DBG("type 0x%02x len %u", type, link.rx.buf->len); - - if (!bt_mesh_fcs_check(link.rx.buf, link.rx.fcs)) { - BT_ERR("Incorrect FCS"); + if (bt_mesh_atomic_test_bit(prov_link.flags, LINK_INVALID)) { + BT_WARN("Unexpected msg 0x%02x on invalidated link", type); + close_link(PROV_ERR_UNEXP_PDU); return; } - gen_prov_ack_send(link.rx.id); - link.rx.prev_id = link.rx.id; - link.rx.id = 0U; + /* When Link Close is being sent, any received Provisioning PDU + * should be ignored. + */ + if (bt_mesh_atomic_test_bit(prov_link.flags, LINK_CLOSING)) { + BT_WARN("Link is closing, unexpected msg 0x%02x", type); + return; + } - if (bt_mesh_atomic_test_bit(link.flags, LINK_INVALID)) { - BT_WARN("Unexpected msg 0x%02x on invalidated link", type); - prov_send_fail_msg(PROV_ERR_UNEXP_PDU); + if (!bt_mesh_fcs_check(prov_link.rx.buf, prov_link.rx.fcs)) { + BT_ERR("Incorrect FCS"); return; } + type = net_buf_simple_pull_u8(prov_link.rx.buf); + + BT_DBG("type 0x%02x len %u", type, prov_link.rx.buf->len); + /* For case MESH/NODE/PROV/BI-15-C, when the node receive a Provisioning PDU * with the Type field set to the lowest unsupported or RFU value, it sends a * Provisioning Failed PDU with the Error Code field set to Invalid PDU(0x01). @@ -1460,141 +1188,109 @@ static void prov_msg_recv(void) return; } - if (type != PROV_FAILED && type != link.expect) { - BT_WARN("Unexpected msg 0x%02x != 0x%02x", type, link.expect); - prov_send_fail_msg(PROV_ERR_UNEXP_PDU); - return; + if (type != PROV_FAILED && type != prov_link.expect) { +#if CONFIG_BLE_MESH_CERT_BASED_PROV + if (prov_link.expect == PROV_REC_EXP && + (type == PROV_INVITE || type == PROV_REC_GET || type == PROV_REC_REQ)) { + if (prov_link.invite_recv) { + BT_ERR("%s, Provisioning invite PDU already received", __func__); + close_link(PROV_ERR_UNEXP_PDU); + return; + } + } else +#endif + { + BT_WARN("Unexpected msg 0x%02x != 0x%02x", type, prov_link.expect); + close_link(PROV_ERR_UNEXP_PDU); + return; + } } - if (1 + prov_handlers[type].len != link.rx.buf->len) { - BT_ERR("Invalid length %u for type 0x%02x", - link.rx.buf->len, type); - prov_send_fail_msg(PROV_ERR_NVAL_FMT); + if (!bt_mesh_prov_pdu_check(type, prov_link.rx.buf->len, &err_code)) { + close_link(err_code); return; } - /* Changed by Espressif, add provisioning timeout timer operations. - * When received a provisioning PDU, restart the 60s timer. - */ - k_delayed_work_submit(&link.prot_timer, PROTOCOL_TIMEOUT); + bt_mesh_gen_prov_ack_send(&prov_link, prov_link.rx.id); + prov_link.rx.prev_id = prov_link.rx.id; + prov_link.rx.id = 0U; - prov_handlers[type].func(&link.rx.buf->data[1]); -} + k_delayed_work_submit(&prov_link.prot_timer, PROTOCOL_TIMEOUT); -static void gen_prov_cont(struct prov_rx *rx, struct net_buf_simple *buf) -{ - uint8_t seg = CONT_SEG_INDEX(rx->gpc); - - BT_DBG("len %u, seg_index %u", buf->len, seg); - - if (!link.rx.seg && link.rx.prev_id == rx->xact_id) { - BT_INFO("Resending ack"); - gen_prov_ack_send(rx->xact_id); - return; - } - - if (rx->xact_id != link.rx.id) { - BT_WARN("Data for unknown transaction (%u != %u)", - rx->xact_id, link.rx.id); - return; - } - - if (seg > link.rx.last_seg) { - BT_ERR("Invalid segment index %u", seg); - prov_send_fail_msg(PROV_ERR_NVAL_FMT); - return; - } else if (seg == link.rx.last_seg) { - uint8_t expect_len = 0U; - - expect_len = (link.rx.buf->len - 20U - - ((link.rx.last_seg - 1) * 23U)); - if (expect_len != buf->len) { - BT_ERR("Incorrect last seg len: %u != %u", - expect_len, buf->len); - prov_send_fail_msg(PROV_ERR_NVAL_FMT); - return; +#if CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) { + if (prov_handlers[type].pb_remote) { + BT_INFO("PB-Remote, receive prov pdu 0x%02x", type); + /* Note: + * Provisioning implementation should make sure that + * no duplicate provisioning pdu is received by the + * Remote Provisioning Server. + */ + bt_mesh_rpr_srv_send_pdu_report(prov_link.pb_remote_uuid, type, + prov_link.rx.buf); } - } - - if (!(link.rx.seg & BIT(seg))) { - BT_INFO("Ignoring already received segment"); return; } +#endif /* CONFIG_BLE_MESH_RPR_SRV */ - memcpy(XACT_SEG_DATA(seg), buf->data, buf->len); - XACT_SEG_RECV(seg); - - if (!link.rx.seg) { - prov_msg_recv(); - } + prov_handlers[type].func(prov_link.rx.buf->data); } -static void gen_prov_ack(struct prov_rx *rx, struct net_buf_simple *buf) +static void gen_prov_cont(struct prov_rx *rx, struct net_buf_simple *buf) { - BT_DBG("len %u", buf->len); + bool close = false; - if (!link.tx.buf[0]) { + if (!bt_mesh_gen_prov_cont(&prov_link, buf, rx, &close)) { + if (close) { + close_link(PROV_ERR_NVAL_FMT); + } return; } - if (rx->xact_id == link.tx.id) { - prov_clear_tx(); - } + prov_msg_recv(); } -static void gen_prov_start(struct prov_rx *rx, struct net_buf_simple *buf) +static void gen_prov_ack(struct prov_rx *rx, struct net_buf_simple *buf) { - if (link.rx.seg) { - BT_INFO("Got Start while there are unreceived segments"); - return; - } + BT_DBG("len %u", buf->len); - if (link.rx.prev_id == rx->xact_id) { - BT_INFO("Resending ack"); - gen_prov_ack_send(rx->xact_id); + if (!prov_link.tx.buf[0]) { return; } - link.rx.buf->len = net_buf_simple_pull_be16(buf); - link.rx.id = rx->xact_id; - link.rx.fcs = net_buf_simple_pull_u8(buf); +#if CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) { + if (prov_link.tx.id == 0) { + return; + } - BT_DBG("len %u last_seg %u total_len %u fcs 0x%02x", buf->len, - START_LAST_SEG(rx->gpc), link.rx.buf->len, link.rx.fcs); + if (rx->xact_id == prov_link.tx.id - 1) { + bt_mesh_prov_clear_tx(&prov_link, true); - if (link.rx.buf->len < 1) { - BT_ERR("Ignoring zero-length provisioning PDU"); - prov_send_fail_msg(PROV_ERR_NVAL_FMT); + BT_INFO("PB-Remote, receive Transaction ACK"); + bt_mesh_rpr_srv_send_outbound_report(prov_link.pb_remote_uuid, true); + } return; } +#endif /* CONFIG_BLE_MESH_RPR_SRV */ - if (START_LAST_SEG(rx->gpc) > START_LAST_SEG_MAX) { - BT_ERR("Invalid SegN 0x%02x", START_LAST_SEG(rx->gpc)); - prov_send_fail_msg(PROV_ERR_UNEXP_ERR); - return; + if (rx->xact_id == prov_link.tx.id) { + bt_mesh_prov_clear_tx(&prov_link, true); } +} - if (link.rx.buf->len > link.rx.buf->size) { - BT_ERR("Too large provisioning PDU (%u bytes)", - link.rx.buf->len); - prov_send_fail_msg(PROV_ERR_NVAL_FMT); - return; - } +static void gen_prov_start(struct prov_rx *rx, struct net_buf_simple *buf) +{ + bool close = false; - if (START_LAST_SEG(rx->gpc) > 0 && link.rx.buf->len <= 20U) { - BT_ERR("Too small total length for multi-segment PDU"); - prov_send_fail_msg(PROV_ERR_NVAL_FMT); + if (!bt_mesh_gen_prov_start(&prov_link, buf, rx, &close)) { + if (close) { + close_link(PROV_ERR_NVAL_FMT); + } return; } - link.rx.seg = (1 << (START_LAST_SEG(rx->gpc) + 1)) - 1; - link.rx.last_seg = START_LAST_SEG(rx->gpc); - memcpy(link.rx.buf->data, buf->data, buf->len); - XACT_SEG_RECV(0); - - if (!link.rx.seg) { - prov_msg_recv(); - } + prov_msg_recv(); } static const struct { @@ -1602,10 +1298,10 @@ static const struct { bool require_link; uint8_t min_len; } gen_prov[] = { - { gen_prov_start, true, 3 }, - { gen_prov_ack, true, 0 }, - { gen_prov_cont, true, 0 }, - { gen_prov_ctl, false, 0 }, + { gen_prov_start, true, 3 }, + { gen_prov_ack, true, 0 }, + { gen_prov_cont, true, 0 }, + { gen_prov_ctl, false, 0 }, }; static void gen_prov_recv(struct prov_rx *rx, struct net_buf_simple *buf) @@ -1615,8 +1311,8 @@ static void gen_prov_recv(struct prov_rx *rx, struct net_buf_simple *buf) return; } - if (!bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE) && - gen_prov[GPCF(rx->gpc)].require_link) { + if (!bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE) && + gen_prov[GPCF(rx->gpc)].require_link) { BT_DBG("Ignoring message that requires active link"); return; } @@ -1628,7 +1324,8 @@ void bt_mesh_pb_adv_recv(struct net_buf_simple *buf) { struct prov_rx rx = {0}; - if (!bt_prov_active() && bt_mesh_is_provisioned()) { + if (!bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE) && + bt_mesh_is_provisioned()) { BT_DBG("Ignoring provisioning PDU - already provisioned"); return; } @@ -1644,7 +1341,8 @@ void bt_mesh_pb_adv_recv(struct net_buf_simple *buf) BT_DBG("link_id 0x%08x xact_id %u", rx.link_id, rx.xact_id); - if (bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE) && link.id != rx.link_id) { + if (bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE) && + prov_link.link_id != rx.link_id) { BT_DBG("Ignoring mesh beacon for unknown link"); return; } @@ -1653,14 +1351,15 @@ void bt_mesh_pb_adv_recv(struct net_buf_simple *buf) } #endif /* CONFIG_BLE_MESH_PB_ADV */ -#if defined(CONFIG_BLE_MESH_PB_GATT) +#if CONFIG_BLE_MESH_PB_GATT int bt_mesh_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf) { + uint8_t err_code = 0U; uint8_t type = 0U; BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); - if (link.conn != conn) { + if (prov_link.conn != conn) { BT_WARN("Data for unexpected connection"); return -ENOTCONN; } @@ -1674,7 +1373,7 @@ int bt_mesh_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf) * a Provisioning PDU , it will send a Provisioning Failed PDU with the Error Code * field set to Unexpected PDU(0x03). */ - if (bt_mesh_atomic_test_bit(link.flags, LINK_INVALID)) { + if (bt_mesh_atomic_test_bit(prov_link.flags, LINK_INVALID)) { BT_WARN("Unexpected msg 0x%02x on invalid link", type); prov_send_fail_msg(PROV_ERR_UNEXP_PDU); return -EINVAL; @@ -1691,21 +1390,46 @@ int bt_mesh_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf) return -EINVAL; } - if (type != PROV_FAILED && type != link.expect) { - BT_WARN("Unexpected msg 0x%02x != 0x%02x", type, link.expect); - prov_send_fail_msg(PROV_ERR_UNEXP_PDU); - return -EINVAL; + if (type != PROV_FAILED && type != prov_link.expect) { +#if CONFIG_BLE_MESH_CERT_BASED_PROV + if (prov_link.expect == PROV_REC_EXP && + (type == PROV_INVITE || type == PROV_REC_GET || type == PROV_REC_REQ)) { + if (prov_link.invite_recv) { + BT_ERR("%s, Provisioning invite PDU already received", __func__); + close_link(PROV_ERR_UNEXP_PDU); + return -EINVAL; + } + } else +#endif + { + BT_WARN("Unexpected msg 0x%02x != 0x%02x", type, prov_link.expect); + close_link(PROV_ERR_UNEXP_PDU); + return -EINVAL; + } } - if (prov_handlers[type].len != buf->len) { - BT_ERR("Invalid length %u for type 0x%02x", buf->len, type); + if (!bt_mesh_prov_pdu_check(type, buf->len, &err_code)) { + close_link(err_code); return -EINVAL; } - /* Changed by Espressif, add provisioning timeout timer operations. - * When received a provisioning PDU, restart the 60s timer. - */ - k_delayed_work_submit(&link.prot_timer, PROTOCOL_TIMEOUT); + k_delayed_work_submit(&prov_link.prot_timer, PROTOCOL_TIMEOUT); + +#if CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) { + if (prov_handlers[type].pb_remote) { + BT_INFO("PB-Remote, receive prov pdu 0x%02x by GATT %s", + type, bt_hex(buf->data, buf->len)); + /* Note: + * Provisioning implementation should make sure that + * no duplicate provisioning pdu is received by the + * Remote Provisioning Server. + */ + bt_mesh_rpr_srv_send_pdu_report(prov_link.pb_remote_uuid, type, buf); + } + return 0; + } +#endif /* CONFIG_BLE_MESH_RPR_SRV */ prov_handlers[type].func(buf->data); @@ -1716,31 +1440,49 @@ int bt_mesh_pb_gatt_open(struct bt_mesh_conn *conn) { BT_DBG("conn %p", conn); - if (bt_mesh_atomic_test_and_set_bit(link.flags, LINK_ACTIVE)) { + if (!bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE) && + bt_mesh_atomic_test_and_set_bit(prov_link.flags, LINK_ACTIVE)) { + BT_ERR("Link is busy"); return -EBUSY; } - link.conn = bt_mesh_conn_ref(conn); - link.expect = PROV_INVITE; + prov_link.conn = bt_mesh_conn_ref(conn); + +#if CONFIG_BLE_MESH_CERT_BASED_PROV + if (PROV_REC_SUPPORT(bt_mesh_prov_get()->oob_info)) { + prov_link.expect = PROV_REC_EXP; + } else +#endif + { + prov_link.expect = PROV_INVITE; + } - if (prov->link_open) { - prov->link_open(BLE_MESH_PROV_GATT); + if (bt_mesh_prov_get()->link_open) { + bt_mesh_prov_get()->link_open(BLE_MESH_PROV_GATT); } return 0; } -int bt_mesh_pb_gatt_close(struct bt_mesh_conn *conn) +int bt_mesh_pb_gatt_close(struct bt_mesh_conn *conn, uint8_t reason) { BT_DBG("conn %p", conn); - if (link.conn != conn) { + if (prov_link.conn != conn) { BT_ERR("Not connected"); return -ENOTCONN; } - if (prov->link_close) { - prov->link_close(BLE_MESH_PROV_GATT); +#if CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT + if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) { + prov_link.pb_remote_cbd = true; + prov_link.pb_remote_reset = true; + prov_link.pb_remote_close(&prov_link, reason); + } +#endif + + if (bt_mesh_prov_get()->link_close) { + bt_mesh_prov_get()->link_close(BLE_MESH_PROV_GATT, reason); } reset_state(); @@ -1749,106 +1491,114 @@ int bt_mesh_pb_gatt_close(struct bt_mesh_conn *conn) } #endif /* CONFIG_BLE_MESH_PB_GATT */ -const struct bt_mesh_prov *bt_mesh_prov_get(void) +bool bt_mesh_prov_active(void) { - return prov; -} - -bool bt_prov_active(void) -{ - return bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE); + return bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE); } static void protocol_timeout(struct k_work *work) { BT_WARN("Protocol timeout"); -#if defined(CONFIG_BLE_MESH_PB_GATT) - if (link.conn) { - bt_mesh_pb_gatt_close(link.conn); +#if CONFIG_BLE_MESH_PB_GATT + if (prov_link.conn) { +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT && CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) { + prov_link.pb_remote_reset = true; + bt_mesh_gattc_disconnect(prov_link.conn); + return; + } +#endif + bt_mesh_pb_gatt_close(prov_link.conn, CLOSE_REASON_TIMEOUT); return; } -#endif +#endif /* CONFIG_BLE_MESH_PB_GATT */ -#if defined(CONFIG_BLE_MESH_PB_ADV) +#if CONFIG_BLE_MESH_PB_ADV uint8_t reason = CLOSE_REASON_TIMEOUT; - - link.rx.seg = 0U; - bearer_ctl_send(LINK_CLOSE, &reason, sizeof(reason)); - - reset_state(); -#endif + prov_link.rx.seg = 0U; + bt_mesh_prov_bearer_ctl_send(&prov_link, LINK_CLOSE, &reason, sizeof(reason)); +#endif /* CONFIG_BLE_MESH_PB_ADV */ } -int bt_mesh_prov_init(const struct bt_mesh_prov *prov_info) +int bt_mesh_prov_init(void) { const uint8_t *key = NULL; - if (!prov_info) { + if (bt_mesh_prov_get() == NULL) { BT_ERR("No provisioning context provided"); return -EINVAL; } - if (prov_info->static_val_len > BLE_MESH_PROV_STATIC_OOB_MAX_LEN || - prov_info->output_size > BLE_MESH_PROV_OUTPUT_OOB_MAX_LEN || - prov_info->input_size > BLE_MESH_PROV_INPUT_OOB_MAX_LEN) { + if (bt_mesh_prov_get()->static_val_len > BLE_MESH_PROV_STATIC_OOB_MAX_LEN || + bt_mesh_prov_get()->output_size > BLE_MESH_PROV_OUTPUT_OOB_MAX_LEN || + bt_mesh_prov_get()->input_size > BLE_MESH_PROV_INPUT_OOB_MAX_LEN) { BT_ERR("Invalid authentication oob length"); return -EINVAL; } - __ASSERT(prov_info->uuid, "Device UUID not initialized"); + __ASSERT(bt_mesh_prov_get()->uuid, "Device UUID not initialized"); - /* Changed by Espressif. Use micro-ecc to generate public key now. */ key = bt_mesh_pub_key_get(); if (!key) { BT_ERR("Failed to generate public key"); return -EIO; } - k_delayed_work_init(&link.prot_timer, protocol_timeout); - - prov = prov_info; + k_delayed_work_init(&prov_link.prot_timer, protocol_timeout); -#if defined(CONFIG_BLE_MESH_PB_ADV) - k_delayed_work_init(&link.tx.retransmit, prov_retransmit); +#if CONFIG_BLE_MESH_PB_ADV + bt_mesh_prov_retransmit_init(&prov_link); #endif reset_state(); -#if defined(CONFIG_BLE_MESH_PB_ADV) - bt_mesh_pb_buf_mutex_new(); +#if CONFIG_BLE_MESH_PB_ADV + bt_mesh_mutex_create(&prov_link.buf_lock); #endif +#if CONFIG_BLE_MESH_CERT_BASED_PROV + if (PROV_REC_SUPPORT(bt_mesh_prov_get()->oob_info)) { + int err = bt_mesh_node_cert_based_prov_init(); + if (err) { + BT_ERR("Loading provisioning records failed!"); + return err; + } + } +#endif /* CONFIG_BLE_MESH_CERT_BASED_PROV */ + return 0; } #if CONFIG_BLE_MESH_DEINIT int bt_mesh_prov_deinit(void) { - if (prov == NULL) { + if (bt_mesh_prov_get() == NULL) { BT_ERR("No provisioning context provided"); return -EINVAL; } - k_delayed_work_free(&link.prot_timer); + k_delayed_work_free(&prov_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) +#if CONFIG_BLE_MESH_PB_ADV + bt_mesh_prov_clear_tx(&prov_link, true); + k_delayed_work_free(&prov_link.tx.retransmit); +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN /* Remove the link id from exceptional list */ bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE, - BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID, &link.id); + BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID, + &prov_link.link_id); #endif /* CONFIG_BLE_MESH_USE_DUPLICATE_SCAN */ + bt_mesh_mutex_free(&prov_link.buf_lock); #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 +#if CONFIG_BLE_MESH_CERT_BASED_PROV + if (PROV_REC_SUPPORT(bt_mesh_prov_get()->oob_info)) { + bt_mesh_node_cert_based_prov_deinit(); + } +#endif /* CONFIG_BLE_MESH_CERT_BASED_PROV */ - prov = NULL; + (void)memset(&prov_link, 0, sizeof(prov_link)); return 0; } @@ -1857,16 +1607,46 @@ int bt_mesh_prov_deinit(void) void bt_mesh_prov_complete(uint16_t net_idx, const uint8_t net_key[16], uint16_t addr, uint8_t flags, uint32_t iv_index) { - if (prov->complete) { - prov->complete(net_idx, net_key, addr, flags, iv_index); + if (bt_mesh_prov_get()->complete) { + bt_mesh_prov_get()->complete(net_idx, net_key, addr, flags, iv_index); } } void bt_mesh_prov_reset(void) { - if (prov->reset) { - prov->reset(); + if (bt_mesh_prov_get()->reset) { + bt_mesh_prov_get()->reset(); + } +} + +#if CONFIG_BLE_MESH_RPR_SRV +void bt_mesh_rpr_srv_reset_prov_link(struct bt_mesh_prov_link *link, uint8_t reason) +{ +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN + /* Remove the link id from exceptional list */ + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE, + BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID, + &link->link_id); +#endif + + reset_state(); +} + +int bt_mesh_rpr_srv_nppi_pdu_recv(uint8_t type, const uint8_t *data) +{ + if (!bt_mesh_atomic_test_bit(prov_link.flags, PB_NPPI)) { + BT_ERR("Not a NPPI provisioning link"); + return -EINVAL; } + + if (type != prov_link.expect) { + BT_ERR("NPPI, unexpected msg 0x%02x != 0x%02x", type, prov_link.expect); + return -EINVAL; + } + + prov_handlers[type].func(data); + return 0; } +#endif /* CONFIG_BLE_MESH_RPR_SRV */ #endif /* CONFIG_BLE_MESH_NODE */ diff --git a/components/bt/esp_ble_mesh/core/prov_node.h b/components/bt/esp_ble_mesh/core/prov_node.h index e71c12de5b3b..b548e730b203 100644 --- a/components/bt/esp_ble_mesh/core/prov_node.h +++ b/components/bt/esp_ble_mesh/core/prov_node.h @@ -18,24 +18,28 @@ extern "C" { void bt_mesh_pb_adv_recv(struct net_buf_simple *buf); -bool bt_prov_active(void); +bool bt_mesh_prov_active(void); int bt_mesh_pb_gatt_open(struct bt_mesh_conn *conn); -int bt_mesh_pb_gatt_close(struct bt_mesh_conn *conn); +int bt_mesh_pb_gatt_close(struct bt_mesh_conn *conn, uint8_t reason); int bt_mesh_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf); int bt_mesh_set_oob_pub_key(const uint8_t pub_key_x[32], const uint8_t pub_key_y[32], const uint8_t pri_key[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_init(void); int bt_mesh_prov_deinit(void); void bt_mesh_prov_complete(uint16_t net_idx, const uint8_t net_key[16], uint16_t addr, uint8_t flags, uint32_t iv_index); void bt_mesh_prov_reset(void); +struct bt_mesh_prov_link *bt_mesh_prov_node_get_link(void); + +void bt_mesh_rpr_srv_reset_prov_link(struct bt_mesh_prov_link *link, uint8_t reason); + +int bt_mesh_rpr_srv_nppi_pdu_recv(uint8_t type, const uint8_t *data); + #ifdef __cplusplus } #endif diff --git a/components/bt/esp_ble_mesh/core/prov_pvnr.c b/components/bt/esp_ble_mesh/core/prov_pvnr.c index e194deb50ce6..ef547613d3ee 100644 --- a/components/bt/esp_ble_mesh/core/prov_pvnr.c +++ b/components/bt/esp_ble_mesh/core/prov_pvnr.c @@ -15,197 +15,33 @@ #include "settings.h" #include "fast_prov.h" #include "mesh/common.h" +#include "proxy_common.h" #include "proxy_client.h" +#include "prov_common.h" +#include "prov_node.h" #include "prov_pvnr.h" #include "pvnr_mgmt.h" +#include "mesh_v1.1/utils.h" + #if CONFIG_BLE_MESH_PROVISIONER _Static_assert(BLE_MESH_MAX_CONN >= CONFIG_BLE_MESH_PBG_SAME_TIME, "Too large BLE Mesh PB-GATT count"); -/* 3 transmissions, 20ms interval */ -#define PROV_XMIT BLE_MESH_TRANSMIT(2, 20) - -#define AUTH_METHOD_NO_OOB 0x00 -#define AUTH_METHOD_STATIC 0x01 -#define AUTH_METHOD_OUTPUT 0x02 -#define AUTH_METHOD_INPUT 0x03 - -#define OUTPUT_OOB_BLINK 0x00 -#define OUTPUT_OOB_BEEP 0x01 -#define OUTPUT_OOB_VIBRATE 0x02 -#define OUTPUT_OOB_NUMBER 0x03 -#define OUTPUT_OOB_STRING 0x04 - -#define INPUT_OOB_PUSH 0x00 -#define INPUT_OOB_TWIST 0x01 -#define INPUT_OOB_NUMBER 0x02 -#define INPUT_OOB_STRING 0x03 - -#define PROV_ERR_NONE 0x00 -#define PROV_ERR_NVAL_PDU 0x01 -#define PROV_ERR_NVAL_FMT 0x02 -#define PROV_ERR_UNEXP_PDU 0x03 -#define PROV_ERR_CFM_FAILED 0x04 -#define PROV_ERR_RESOURCES 0x05 -#define PROV_ERR_DECRYPT 0x06 -#define PROV_ERR_UNEXP_ERR 0x07 -#define PROV_ERR_ADDR 0x08 - -#define PROV_INVITE 0x00 -#define PROV_CAPABILITIES 0x01 -#define PROV_START 0x02 -#define PROV_PUB_KEY 0x03 -#define PROV_INPUT_COMPLETE 0x04 -#define PROV_CONFIRM 0x05 -#define PROV_RANDOM 0x06 -#define PROV_DATA 0x07 -#define PROV_COMPLETE 0x08 -#define PROV_FAILED 0x09 - -#define PROV_ALG_P256 0x00 - -#define GPCF(gpc) (gpc & 0x03) -#define GPC_START(last_seg) (((last_seg) << 2) | 0x00) -#define GPC_ACK 0x01 -#define GPC_CONT(seg_id) (((seg_id) << 2) | 0x02) -#define GPC_CTL(op) (((op) << 2) | 0x03) - -#define START_PAYLOAD_MAX 20 -#define CONT_PAYLOAD_MAX 23 -#define START_LAST_SEG_MAX 2 - -#define START_LAST_SEG(gpc) (gpc >> 2) -#define CONT_SEG_INDEX(gpc) (gpc >> 2) - -#define BEARER_CTL(gpc) (gpc >> 2) -#define LINK_OPEN 0x00 -#define LINK_ACK 0x01 -#define LINK_CLOSE 0x02 - -#define CLOSE_REASON_SUCCESS 0x00 -#define CLOSE_REASON_TIMEOUT 0x01 -#define CLOSE_REASON_FAILED 0x02 - -#define PROV_AUTH_VAL_SIZE 0x10 -#define PROV_CONF_SALT_SIZE 0x10 -#define PROV_CONF_KEY_SIZE 0x10 -#define PROV_DH_KEY_SIZE 0x20 -#define PROV_CONFIRM_SIZE 0x10 -#define PROV_RANDOM_SIZE 0x10 -#define PROV_PROV_SALT_SIZE 0x10 -#define PROV_CONF_INPUTS_SIZE 0x91 - -#define XACT_SEG_DATA(_idx, _seg) (&link[_idx].rx.buf->data[20 + ((_seg - 1) * 23)]) -#define XACT_SEG_RECV(_idx, _seg) (link[_idx].rx.seg &= ~(1 << (_seg))) - -#define XACT_NVAL 0xff - -enum { - REMOTE_PUB_KEY, /* Remote key has been received */ - LOCAL_PUB_KEY, /* Local public key is available */ - LINK_ACTIVE, /* Link has been opened */ - WAIT_GEN_DHKEY, /* Waiting for remote public key to generate DHKey */ - HAVE_DHKEY, /* DHKey has been calculated */ - SEND_CONFIRM, /* Waiting to send Confirm value */ - WAIT_NUMBER, /* Waiting for number input from user */ - WAIT_STRING, /* Waiting for string input from user */ - TIMEOUT_START, /* Provision timeout timer has started */ - NUM_FLAGS, -}; +/* Number of devices can be provisioned at the same time equals to PB-ADV + PB-GATT */ +#define BLE_MESH_PROV_SAME_TIME \ + (CONFIG_BLE_MESH_PBA_SAME_TIME + CONFIG_BLE_MESH_PBG_SAME_TIME) -/** Provisioner link structure allocation +#define UNICAST_ADDR_LIMIT 0x7FFF + +/* Provisioner link structure allocation * |--------------------------------------------------------| * | Link(PB-ADV) | Link(PB-GATT) | * |--------------------------------------------------------| * |<----------------------Total Link---------------------->| */ -struct prov_link { - BLE_MESH_ATOMIC_DEFINE(flags, NUM_FLAGS); - uint8_t uuid[16]; /* check if device is being provisioned*/ - uint16_t oob_info; /* oob info of this device */ - uint8_t element_num; /* element num of device */ - uint8_t ki_flags; /* Key refresh flag and iv update flag */ - uint32_t iv_index; /* IV Index */ - uint8_t auth_method; /* Choosen authentication method */ - uint8_t auth_action; /* Choosen authentication action */ - uint8_t auth_size; /* Choosen authentication size */ - uint16_t assign_addr; /* Application assigned address for the device */ - uint16_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 */ -#endif - uint8_t expect; /* Next expected PDU */ - - uint8_t *dhkey; /* Calculated DHKey */ - uint8_t *auth; /* Authentication Value */ - - uint8_t *conf_salt; /* ConfirmationSalt */ - uint8_t *conf_key; /* ConfirmationKey */ - uint8_t *conf_inputs; /* ConfirmationInputs */ - - uint8_t *rand; /* Local Random */ - uint8_t *conf; /* Remote Confirmation */ - uint8_t *local_conf; /* Local Confirmation */ - - uint8_t *prov_salt; /* Provisioning Salt */ - -#if defined(CONFIG_BLE_MESH_PB_ADV) - bool linking; /* Linking is being establishing */ - uint16_t send_link_close; /* Link close is being sent flag */ - uint32_t link_id; /* Link ID */ - uint8_t pending_ack; /* Decide which transaction id ack is pending */ - uint8_t expect_ack_for; /* Transaction ACK expected for provisioning pdu */ - uint8_t tx_pdu_type; /* The current transmitted Provisioning PDU type */ - - struct { - uint8_t trans_id; /* Transaction ID */ - uint8_t prev_id; /* Previous Transaction ID */ - uint8_t seg; /* Bit-field of unreceived segments */ - uint8_t last_seg; /* Last segment (to check length) */ - uint8_t fcs; /* Expected FCS value */ - uint8_t adv_buf_id; /* index of buf allocated in adv_buf_data */ - struct net_buf_simple *buf; - } rx; - - struct { - /* Start timestamp of the transaction */ - int64_t start; - - /* Transaction id*/ - uint8_t trans_id; - - /* Pending outgoing buffer(s) */ - struct net_buf *buf[3]; - - /* Retransmit timer */ - struct k_delayed_work retransmit; - } tx; -#endif - - /** Provision timeout timer. Spec P259 says: The provisioning protocol - * shall have a minimum timeout of 60 seconds that is reset each time - * a provisioning protocol PDU is sent or received. - */ - struct k_delayed_work timeout; -}; - -/* Number of devices can be provisioned at the same time equals to PB-ADV + PB-GATT */ -#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 { - uint32_t link_id; - uint8_t xact_id; - uint8_t gpc; -}; +static struct bt_mesh_prov_link prov_links[BLE_MESH_PROV_SAME_TIME]; struct bt_mesh_prov_ctx { /* Primary element address of Provisioner */ @@ -214,29 +50,27 @@ struct bt_mesh_prov_ctx { /* Provisioning bearers used by Provisioner */ bt_mesh_prov_bearer_t bearers; +#if CONFIG_BLE_MESH_PB_ADV /* Current number of PB-ADV provisioned devices simultaneously */ uint8_t pba_count; +#endif /* CONFIG_BLE_MESH_PB_ADV */ +#if CONFIG_BLE_MESH_PB_GATT /* Current number of PB-GATT provisioned devices simultaneously */ uint8_t pbg_count; +#endif /* CONFIG_BLE_MESH_PB_GATT */ /* Current unicast address going to allocated */ - uint16_t curr_alloc_addr; + uint16_t alloc_addr; /* Current net_idx going to be used in provisioning data */ - uint16_t curr_net_idx; - - /* Current flags going to be used in provisioning data */ - uint8_t curr_flags; - - /* Current iv_index going to be used in provisioning data */ - uint16_t curr_iv_index; + uint16_t net_idx; /* Length of Static OOB value */ uint8_t static_oob_len; /* Static OOB value */ - uint8_t static_oob_val[16]; + uint8_t static_oob_val[32]; /* Offset of the device uuid to be matched, based on zero */ uint8_t match_offset; @@ -248,27 +82,22 @@ struct bt_mesh_prov_ctx { uint8_t match_value[16]; /* Indicate when received uuid_match adv_pkts, can provision it at once */ - bool prov_after_match; + bool prov_after_match; -#if defined(CONFIG_BLE_MESH_PB_ADV) +#if CONFIG_BLE_MESH_PB_ADV /* Mutex used to protect the PB-ADV procedure */ bt_mesh_mutex_t pb_adv_lock; +#endif /* CONFIG_BLE_MESH_PB_ADV */ - /* 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) +#if CONFIG_BLE_MESH_PB_GATT /* Mutex used to protect the PB-GATT procedure */ bt_mesh_mutex_t pb_gatt_lock; -#endif +#endif /* CONFIG_BLE_MESH_PB_GATT */ /* Fast provisioning related information */ struct { bool enable; uint16_t net_idx; - uint8_t flags; - uint32_t iv_index; uint16_t unicast_addr_min; uint16_t unicast_addr_max; } fast_prov; @@ -284,7 +113,7 @@ struct unprov_dev_queue { uint16_t oob_info; uint8_t bearer; uint8_t flags; -} __packed unprov_dev[CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM] = { +} __attribute__((packed)) unprov_dev[CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM] = { [0 ... (CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM - 1)] = { .addr.type = 0xff, .bearer = 0, @@ -292,124 +121,67 @@ struct unprov_dev_queue { }, }; -static unprov_adv_pkt_cb_t notify_unprov_adv_pkt_cb; - -#define BUF_TIMEOUT K_MSEC(400) - -#if defined(CONFIG_BLE_MESH_FAST_PROV) -#define RETRANSMIT_TIMEOUT K_MSEC(360) -#define TRANSACTION_TIMEOUT K_SECONDS(3) -#define PROVISION_TIMEOUT K_SECONDS(6) -#else -#define RETRANSMIT_TIMEOUT K_MSEC(500) -#define TRANSACTION_TIMEOUT K_SECONDS(30) -#define PROVISION_TIMEOUT K_SECONDS(60) -#endif /* CONFIG_BLE_MESH_FAST_PROV */ - -#if defined(CONFIG_BLE_MESH_PB_GATT) -#define PROV_BUF_HEADROOM 5 -#else -#define PROV_BUF_HEADROOM 0 -#endif - -#define PROV_BUF(name, len) \ - NET_BUF_SIMPLE_DEFINE(name, PROV_BUF_HEADROOM + len) +static unprov_adv_pkt_cb_t notify_unprov_adv_pkt_cb; -static const struct bt_mesh_prov *prov; - -#if defined(CONFIG_BLE_MESH_PB_ADV) -static void send_link_open(const uint8_t idx); +#if CONFIG_BLE_MESH_PB_ADV +static void send_link_open(struct bt_mesh_prov_link *link); +extern uint8_t pvnr_next_xact_id(struct bt_mesh_prov_link *link); #endif -static void prov_gen_dh_key(const uint8_t idx); - -static void send_pub_key(const uint8_t idx, uint8_t oob); - -static void close_link(const uint8_t idx, uint8_t reason); +static void prov_gen_dh_key(struct bt_mesh_prov_link *link); +static void send_pub_key(struct bt_mesh_prov_link *link); +static void close_link(struct bt_mesh_prov_link *link, uint8_t reason); +static void send_invite(struct bt_mesh_prov_link *link); -#if defined(CONFIG_BLE_MESH_PB_ADV) -#define ADV_BUF_SIZE 65 - -static struct prov_adv_buf { +#if CONFIG_BLE_MESH_PB_ADV +static struct prov_rx_buf { struct net_buf_simple buf; -} adv_buf[CONFIG_BLE_MESH_PBA_SAME_TIME]; +} rx_buf[CONFIG_BLE_MESH_PBA_SAME_TIME]; -static uint8_t adv_buf_data[ADV_BUF_SIZE * CONFIG_BLE_MESH_PBA_SAME_TIME]; +static uint8_t rx_buf_data[PROV_RX_BUF_SIZE * CONFIG_BLE_MESH_PBA_SAME_TIME]; #endif /* CONFIG_BLE_MESH_PB_ADV */ -#define PROV_FREE_MEM(_idx, member) \ -{ \ - if (link[_idx].member) { \ - bt_mesh_free(link[_idx].member); \ - link[_idx].member = NULL; \ - } \ -} - -#if defined(CONFIG_BLE_MESH_PB_ADV) -static inline void bt_mesh_pb_adv_mutex_new(void) -{ - if (!prov_ctx.pb_adv_lock.mutex) { - bt_mesh_mutex_create(&prov_ctx.pb_adv_lock); - } -} - -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_pb_adv_mutex_free(void) -{ - bt_mesh_mutex_free(&prov_ctx.pb_adv_lock); +#define PROV_FREE_MEM(_idx, member) \ +{ \ + if (prov_links[_idx].member) { \ + bt_mesh_free(prov_links[_idx].member); \ + prov_links[_idx].member = NULL; \ + } \ } -#endif /* CONFIG_BLE_MESH_DEINIT */ -static inline void bt_mesh_pb_adv_lock(void) +struct bt_mesh_prov_link *bt_mesh_prov_pvnr_get_link(void) { - bt_mesh_mutex_lock(&prov_ctx.pb_adv_lock); + return &prov_links[0]; } -static inline void bt_mesh_pb_adv_unlock(void) +uint8_t bt_mesh_prov_pvnr_get_link_count(void) { - bt_mesh_mutex_unlock(&prov_ctx.pb_adv_lock); + return BLE_MESH_PROV_SAME_TIME; } -static inline void bt_mesh_pb_buf_mutex_new(void) +void bt_mesh_prov_pvnr_close_link(struct bt_mesh_prov_link *link, uint8_t reason) { - if (!prov_ctx.pb_buf_lock.mutex) { - bt_mesh_mutex_create(&prov_ctx.pb_buf_lock); - } + close_link(link, reason); } -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_pb_buf_mutex_free(void) +void bt_mesh_prov_pvnr_send_invite(struct bt_mesh_prov_link *link) { - bt_mesh_mutex_free(&prov_ctx.pb_buf_lock); + send_invite(link); } -#endif /* CONFIG_BLE_MESH_DEINIT */ -static inline void bt_mesh_pb_buf_lock(void) +#if CONFIG_BLE_MESH_PB_ADV +static inline void bt_mesh_pb_adv_lock(void) { - bt_mesh_mutex_lock(&prov_ctx.pb_buf_lock); + bt_mesh_mutex_lock(&prov_ctx.pb_adv_lock); } -static inline void bt_mesh_pb_buf_unlock(void) +static inline void bt_mesh_pb_adv_unlock(void) { - bt_mesh_mutex_unlock(&prov_ctx.pb_buf_lock); + bt_mesh_mutex_unlock(&prov_ctx.pb_adv_lock); } #endif /* CONFIG_BLE_MESH_PB_ADV */ -#if defined(CONFIG_BLE_MESH_PB_GATT) -static inline void bt_mesh_pb_gatt_mutex_new(void) -{ - if (!prov_ctx.pb_gatt_lock.mutex) { - bt_mesh_mutex_create(&prov_ctx.pb_gatt_lock); - } -} - -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_pb_gatt_mutex_free(void) -{ - bt_mesh_mutex_free(&prov_ctx.pb_gatt_lock); -} -#endif /* CONFIG_BLE_MESH_DEINIT */ - +#if CONFIG_BLE_MESH_PB_GATT static inline void bt_mesh_pb_gatt_lock(void) { bt_mesh_mutex_lock(&prov_ctx.pb_gatt_lock); @@ -444,43 +216,36 @@ void bt_mesh_provisioner_clear_link_info(const uint8_t addr[6]) BT_DBG("Clear device info, addr %s", bt_hex(addr, BLE_MESH_ADDR_LEN)); for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) { - if (!memcmp(link[i].addr.val, addr, BLE_MESH_ADDR_LEN)) { - link[i].connecting = false; - link[i].conn = NULL; - link[i].oob_info = 0x0; - memset(link[i].uuid, 0, 16); - memset(&link[i].addr, 0, sizeof(bt_mesh_addr_t)); - bt_mesh_atomic_test_and_clear_bit(link[i].flags, LINK_ACTIVE); - if (bt_mesh_atomic_test_and_clear_bit(link[i].flags, TIMEOUT_START)) { - k_delayed_work_cancel(&link[i].timeout); - } + if (!memcmp(prov_links[i].addr.val, addr, BLE_MESH_ADDR_LEN)) { + bt_mesh_atomic_clear_bit(prov_links[i].flags, CONNECTING); + prov_links[i].conn = NULL; + prov_links[i].oob_info = 0x0; + memset(prov_links[i].uuid, 0, 16); + memset(&prov_links[i].addr, 0, sizeof(bt_mesh_addr_t)); + bt_mesh_atomic_clear_bit(prov_links[i].flags, LINK_ACTIVE); + k_delayed_work_cancel(&prov_links[i].prot_timer); return; } } BT_WARN("Device not found, addr %s", bt_hex(addr, BLE_MESH_ADDR_LEN)); - return; } #endif /* CONFIG_BLE_MESH_PB_GATT */ -const struct bt_mesh_prov *bt_mesh_provisioner_get_prov_info(void) -{ - return prov; -} - +#if CONFIG_BLE_MESH_SETTINGS void bt_mesh_provisioner_restore_prov_info(uint16_t primary_addr, uint16_t alloc_addr) { prov_ctx.primary_addr = primary_addr; - prov_ctx.curr_alloc_addr = alloc_addr; + prov_ctx.alloc_addr = alloc_addr; } +#endif /* CONFIG_BLE_MESH_SETTINGS */ static bool is_unprov_dev_being_provision(const uint8_t uuid[16]) { int i; -#if defined(CONFIG_BLE_MESH_FAST_PROV) - /** - * During Fast Provisioning test, we found that if a device has already being +#if CONFIG_BLE_MESH_FAST_PROV + /* During Fast Provisioning test, we found that if a device has already being * provisioned, there is still a chance that the Provisioner can receive the * Unprovisioned Device Beacon from the device (because the device will stop * Unprovisioned Device Beacon when Transaction ACK for Provisioning Complete @@ -490,18 +255,15 @@ static bool is_unprov_dev_being_provision(const uint8_t uuid[16]) BT_WARN("Device has already been provisioned"); return true; } -#endif +#endif /* CONFIG_BLE_MESH_FAST_PROV */ 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)) { -#elif defined(CONFIG_BLE_MESH_PB_ADV) && !defined(CONFIG_BLE_MESH_PB_GATT) - if (link[i].linking || bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { -#else - if (link[i].connecting || bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { + if (bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE) +#if CONFIG_BLE_MESH_PB_GATT + || bt_mesh_atomic_test_bit(prov_links[i].flags, CONNECTING) #endif - if (!memcmp(link[i].uuid, uuid, 16)) { + ) { + if (!memcmp(prov_links[i].uuid, uuid, 16)) { BT_DBG("Device is being provisioning"); return true; } @@ -513,11 +275,14 @@ static bool is_unprov_dev_being_provision(const uint8_t uuid[16]) static bool is_unprov_dev_uuid_match(const uint8_t uuid[16]) { - if (prov_ctx.match_length) { - if (memcmp(uuid + prov_ctx.match_offset, - prov_ctx.match_value, prov_ctx.match_length)) { - return false; - } + /* Match value is not configured */ + if (prov_ctx.match_length == 0) { + return true; + } + + if (memcmp(uuid + prov_ctx.match_offset, + prov_ctx.match_value, prov_ctx.match_length)) { + return false; } return true; @@ -546,23 +311,25 @@ static int provisioner_check_unprov_dev_info(const uint8_t uuid[16], bt_mesh_pro return -EALREADY; } +#if CONFIG_BLE_MESH_PB_ADV /* Check if the current PB-ADV link is full */ - if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && - (prov_ctx.bearers & BLE_MESH_PROV_ADV) && + if ((prov_ctx.bearers & BLE_MESH_PROV_ADV) && (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; } +#endif /* CONFIG_BLE_MESH_PB_ADV */ +#if CONFIG_BLE_MESH_PB_GATT /* Check if the current PB-GATT link is full */ - if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && - (prov_ctx.bearers & BLE_MESH_PROV_GATT) && + if ((prov_ctx.bearers & BLE_MESH_PROV_GATT) && (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; } +#endif /* CONFIG_BLE_MESH_PB_GATT */ /* Check if the device has already been provisioned */ if (bt_mesh_provisioner_get_node_with_uuid(uuid)) { @@ -572,7 +339,7 @@ static int provisioner_check_unprov_dev_info(const uint8_t uuid[16], bt_mesh_pro return 0; } -#if defined(CONFIG_BLE_MESH_PB_ADV) +#if CONFIG_BLE_MESH_PB_ADV static int provisioner_start_prov_pb_adv(const uint8_t uuid[16], const bt_mesh_addr_t *addr, uint16_t oob_info, uint16_t assign_addr) { @@ -585,11 +352,11 @@ static int provisioner_start_prov_pb_adv(const uint8_t uuid[16], const bt_mesh_a 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 the unicast address of the device is allocated internally, + * then we need to check if there are addresses available. */ if (assign_addr == BLE_MESH_ADDR_UNASSIGNED && - prov_ctx.curr_alloc_addr == BLE_MESH_ADDR_UNASSIGNED) { + prov_ctx.alloc_addr == BLE_MESH_ADDR_UNASSIGNED) { BT_ERR("No available unicast address to assign"); bt_mesh_pb_adv_unlock(); return -EIO; @@ -601,21 +368,21 @@ static int provisioner_start_prov_pb_adv(const uint8_t uuid[16], const bt_mesh_a } 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; + if (!bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE)) { + memcpy(prov_links[i].uuid, uuid, 16); + prov_links[i].oob_info = oob_info; if (addr) { - link[i].addr.type = addr->type; - memcpy(link[i].addr.val, addr->val, BLE_MESH_ADDR_LEN); + prov_links[i].addr.type = addr->type; + memcpy(prov_links[i].addr.val, addr->val, BLE_MESH_ADDR_LEN); } - send_link_open(i); + send_link_open(&prov_links[i]); /* If a specific unicast address is assigned for the device, then * Provisioner will use this address in the Provisioning Data PDU. */ if (BLE_MESH_ADDR_IS_UNICAST(assign_addr)) { - link[i].assign_addr = assign_addr; + prov_links[i].assign_addr = assign_addr; } /* Increase PB-ADV link count */ @@ -633,7 +400,7 @@ static int provisioner_start_prov_pb_adv(const uint8_t uuid[16], const bt_mesh_a } #endif /* CONFIG_BLE_MESH_PB_ADV */ -#if defined(CONFIG_BLE_MESH_PB_GATT) +#if CONFIG_BLE_MESH_PB_GATT static int provisioner_start_prov_pb_gatt(const uint8_t uuid[16], const bt_mesh_addr_t *addr, uint16_t oob_info, uint16_t assign_addr) { @@ -646,11 +413,11 @@ static int provisioner_start_prov_pb_gatt(const uint8_t uuid[16], const bt_mesh_ 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 the unicast address of the device is allocated internally, + * then we need to check if there are addresses available. */ if (assign_addr == BLE_MESH_ADDR_UNASSIGNED && - prov_ctx.curr_alloc_addr == BLE_MESH_ADDR_UNASSIGNED) { + prov_ctx.alloc_addr == BLE_MESH_ADDR_UNASSIGNED) { BT_ERR("No available unicast address to assign"); bt_mesh_pb_gatt_unlock(); return -EIO; @@ -662,26 +429,27 @@ static int provisioner_start_prov_pb_gatt(const uint8_t uuid[16], const bt_mesh_ } for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) { - if (!link[i].connecting && !bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { + if (!bt_mesh_atomic_test_bit(prov_links[i].flags, CONNECTING) && + !bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE)) { if (bt_mesh_gattc_conn_create(addr, BLE_MESH_UUID_MESH_PROV_VAL)) { bt_mesh_pb_gatt_unlock(); return -EIO; } - memcpy(link[i].uuid, uuid, 16); - link[i].oob_info = oob_info; - link[i].addr.type = addr->type; - memcpy(link[i].addr.val, addr->val, BLE_MESH_ADDR_LEN); + memcpy(prov_links[i].uuid, uuid, 16); + prov_links[i].oob_info = oob_info; + prov_links[i].addr.type = addr->type; + memcpy(prov_links[i].addr.val, addr->val, BLE_MESH_ADDR_LEN); /* If the application layer assigned a specific unicast address for the device, * then Provisioner will use this address in the Provisioning Data PDU. */ if (BLE_MESH_ADDR_IS_UNICAST(assign_addr)) { - link[i].assign_addr = assign_addr; + prov_links[i].assign_addr = assign_addr; } - /* If creating connection successfully, set connecting flag to 1 */ - link[i].connecting = true; + /* If creating connection successfully, set CONNECTING flag */ + bt_mesh_atomic_set_bit(prov_links[i].flags, CONNECTING); /* Increase PB-GATT link count */ provisioner_pbg_count_inc(); @@ -722,14 +490,14 @@ int bt_mesh_provisioner_add_unprov_dev(struct bt_mesh_unprov_dev_add *add_dev, u } if ((!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) || - !(prov_ctx.bearers & BLE_MESH_PROV_GATT)) + !(prov_ctx.bearers & BLE_MESH_PROV_GATT)) && (add_dev->bearer & BLE_MESH_PROV_GATT)) { BT_ERR("Not support PB-GATT"); return -EINVAL; } if ((!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) || - !(prov_ctx.bearers & BLE_MESH_PROV_ADV)) + !(prov_ctx.bearers & BLE_MESH_PROV_ADV)) && (add_dev->bearer & BLE_MESH_PROV_ADV)) { BT_ERR("Not support PB-ADV"); return -EINVAL; @@ -757,11 +525,10 @@ int bt_mesh_provisioner_add_unprov_dev(struct bt_mesh_unprov_dev_add *add_dev, u } /* Check if the provisioned nodes array is full */ - if (bt_mesh_provisioner_get_node_with_uuid(add_dev->uuid) == NULL) { - if (bt_mesh_provisioner_get_node_count() == CONFIG_BLE_MESH_MAX_PROV_NODES) { - BT_WARN("Current provisioned devices reach max limit"); - return -ENOMEM; - } + if (bt_mesh_provisioner_get_node_with_uuid(add_dev->uuid) == NULL && + bt_mesh_provisioner_get_node_count() == CONFIG_BLE_MESH_MAX_PROV_NODES) { + BT_WARN("Current provisioned devices reach max limit"); + return -ENOMEM; } /* Check if the device already exists in queue */ @@ -818,8 +585,14 @@ int bt_mesh_provisioner_add_unprov_dev(struct bt_mesh_unprov_dev_add *add_dev, u /* Check if current provisioned node count + active link reach max limit */ if (bt_mesh_provisioner_get_node_with_uuid(add_dev->uuid) == NULL) { - if (bt_mesh_provisioner_get_node_count() + prov_ctx.pba_count + \ - prov_ctx.pbg_count >= CONFIG_BLE_MESH_MAX_PROV_NODES) { + if (bt_mesh_provisioner_get_node_count() +#if CONFIG_BLE_MESH_PB_ADV + + prov_ctx.pba_count /* Add current PB-ADV link count */ +#endif +#if CONFIG_BLE_MESH_PB_GATT + + prov_ctx.pbg_count /* Add current PB-GATT link count */ +#endif + >= CONFIG_BLE_MESH_MAX_PROV_NODES) { BT_WARN("Node count + active link count reach max limit"); return -EIO; } @@ -829,22 +602,21 @@ int bt_mesh_provisioner_add_unprov_dev(struct bt_mesh_unprov_dev_add *add_dev, u return err; } +#if CONFIG_BLE_MESH_PB_ADV if (add_dev->bearer == BLE_MESH_PROV_ADV) { -#if defined(CONFIG_BLE_MESH_PB_ADV) - if ((err = provisioner_start_prov_pb_adv(add_dev->uuid, addr_valid ? &add_addr : NULL, - add_dev->oob_info, BLE_MESH_ADDR_UNASSIGNED))) { - return err; - } -#endif - } else if (add_dev->bearer == BLE_MESH_PROV_GATT) { -#if defined(CONFIG_BLE_MESH_PB_GATT) - if ((err = provisioner_start_prov_pb_gatt(add_dev->uuid, &add_addr, add_dev->oob_info, - BLE_MESH_ADDR_UNASSIGNED))) { - return err; - } -#endif + return provisioner_start_prov_pb_adv(add_dev->uuid, addr_valid ? &add_addr : NULL, + add_dev->oob_info, BLE_MESH_ADDR_UNASSIGNED); + } +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +#if CONFIG_BLE_MESH_PB_GATT + if (add_dev->bearer == BLE_MESH_PROV_GATT) { + return provisioner_start_prov_pb_gatt(add_dev->uuid, &add_addr, add_dev->oob_info, + BLE_MESH_ADDR_UNASSIGNED); } +#endif /* CONFIG_BLE_MESH_PB_GATT */ + /* Shall not reach here. */ return 0; } @@ -866,14 +638,14 @@ int bt_mesh_provisioner_prov_device_with_addr(const uint8_t uuid[16], const uint } if ((!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) || - !(prov_ctx.bearers & BLE_MESH_PROV_ADV)) && + !(prov_ctx.bearers & BLE_MESH_PROV_ADV)) && (bearer == BLE_MESH_PROV_ADV)) { BT_ERR("Not support PB-ADV"); return -ENOTSUP; } if ((!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) || - !(prov_ctx.bearers & BLE_MESH_PROV_GATT)) && + !(prov_ctx.bearers & BLE_MESH_PROV_GATT)) && (bearer == BLE_MESH_PROV_GATT)) { BT_ERR("Not support PB-GATT"); return -ENOTSUP; @@ -892,7 +664,7 @@ int bt_mesh_provisioner_prov_device_with_addr(const uint8_t uuid[16], const uint /* Here we will not check if the assigned unicast address is overlapped * with the unicast addresses of other nodes or Provisioner, because: * 1. At this moment, the element number of the device is unknown - * 2. If the node is a reprovisioned device, then the original allocated + * 2. If the node is a re-provisioned 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. @@ -906,8 +678,14 @@ int bt_mesh_provisioner_prov_device_with_addr(const uint8_t uuid[16], const uint } /* Check if current provisioned node count + active link reach max limit */ - if (bt_mesh_provisioner_get_node_count() + prov_ctx.pba_count + \ - prov_ctx.pbg_count >= CONFIG_BLE_MESH_MAX_PROV_NODES) { + if (bt_mesh_provisioner_get_node_count() +#if CONFIG_BLE_MESH_PB_ADV + + prov_ctx.pba_count /* Add current PB-ADV link count */ +#endif +#if CONFIG_BLE_MESH_PB_GATT + + prov_ctx.pbg_count /* Add current PB-GATT link count */ +#endif + >= CONFIG_BLE_MESH_MAX_PROV_NODES) { BT_WARN("Node count + active link count reach max limit"); return -EIO; } @@ -922,20 +700,19 @@ int bt_mesh_provisioner_prov_device_with_addr(const uint8_t uuid[16], const uint memcpy(dev_addr.val, addr, BLE_MESH_ADDR_LEN); } +#if CONFIG_BLE_MESH_PB_ADV if (bearer == BLE_MESH_PROV_ADV) { -#if defined(CONFIG_BLE_MESH_PB_ADV) - if ((err = provisioner_start_prov_pb_adv(uuid, addr ? &dev_addr : NULL, 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 + return provisioner_start_prov_pb_adv(uuid, addr ? &dev_addr : NULL, oob_info, unicast_addr); + } +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +#if CONFIG_BLE_MESH_PB_GATT + if (bearer == BLE_MESH_PROV_GATT) { + return provisioner_start_prov_pb_gatt(uuid, &dev_addr, oob_info, unicast_addr); } +#endif /* CONFIG_BLE_MESH_PB_GATT */ + /* Shall not reach here. */ return 0; } @@ -963,9 +740,9 @@ int bt_mesh_provisioner_delete_device(struct bt_mesh_device_delete *del_dev) } /* Find if the device is being provisioned */ - for (i = 0; i < ARRAY_SIZE(link); i++) { - if (!memcmp(link[i].uuid, del_dev->uuid, 16)) { - close_link(i, CLOSE_REASON_FAILED); + for (i = 0; i < ARRAY_SIZE(prov_links); i++) { + if (!memcmp(prov_links[i].uuid, del_dev->uuid, 16)) { + close_link(&prov_links[i], CLOSE_REASON_FAILED); break; } } @@ -976,7 +753,7 @@ int bt_mesh_provisioner_delete_device(struct bt_mesh_device_delete *del_dev) int bt_mesh_provisioner_set_dev_uuid_match(uint8_t offset, uint8_t length, const uint8_t *match, bool prov_flag) { - if (length && (!match || (offset + length > 16))) { + if (length && (match == NULL || (offset + length > 16))) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } @@ -1008,7 +785,7 @@ int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info) { const uint8_t *key = NULL; - if (!info || info->flag == 0) { + if (info == NULL || info->flag == 0) { return -EINVAL; } @@ -1018,31 +795,34 @@ int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info) BT_ERR("Failed to get NetKey"); return -EINVAL; } - prov_ctx.curr_net_idx = info->net_idx; - } else if (info->flag & FLAGS_FLAG) { - prov_ctx.curr_flags = info->flags; - } else if (info->flag & IV_INDEX_FLAG) { - prov_ctx.curr_iv_index = info->iv_index; + + prov_ctx.net_idx = info->net_idx; } return 0; } +static inline uint8_t get_net_flags(uint16_t net_idx) +{ + return bt_mesh_net_flags(bt_mesh_subnet_get(net_idx)); +} + int bt_mesh_provisioner_init_prov_info(void) { 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 initialization procedure will be used. */ - if (prov == NULL) { + if (bt_mesh_prov_get() == NULL) { BT_ERR("No provisioning context provided"); return -EINVAL; } - if (!BLE_MESH_ADDR_IS_UNICAST(prov->prov_unicast_addr) || - !BLE_MESH_ADDR_IS_UNICAST(prov->prov_start_address)) { + if (!BLE_MESH_ADDR_IS_UNICAST(bt_mesh_prov_get()->prov_unicast_addr) || + !BLE_MESH_ADDR_IS_UNICAST(bt_mesh_prov_get()->prov_start_address)) { BT_ERR("Invalid address, own 0x%04x, start 0x%04x", - prov->prov_unicast_addr, prov->prov_start_address); + bt_mesh_prov_get()->prov_unicast_addr, + bt_mesh_prov_get()->prov_start_address); return -EINVAL; } @@ -1052,26 +832,26 @@ int bt_mesh_provisioner_init_prov_info(void) return -EINVAL; } - if (prov->prov_unicast_addr + comp->elem_count > prov->prov_start_address) { + if (bt_mesh_prov_get()->prov_unicast_addr + comp->elem_count > + bt_mesh_prov_get()->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; + bt_mesh_prov_get()->prov_start_address, + bt_mesh_prov_get()->prov_unicast_addr + comp->elem_count); + + prov_ctx.alloc_addr = bt_mesh_prov_get()->prov_unicast_addr + comp->elem_count; } else { - prov_ctx.curr_alloc_addr = prov->prov_start_address; + prov_ctx.alloc_addr = bt_mesh_prov_get()->prov_start_address; } /* Update primary element address with the initialized value here. */ - prov_ctx.primary_addr = prov->prov_unicast_addr; + prov_ctx.primary_addr = bt_mesh_prov_get()->prov_unicast_addr; if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.curr_alloc_addr); + bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.alloc_addr); } } - prov_ctx.curr_net_idx = BLE_MESH_KEY_PRIMARY; - struct bt_mesh_subnet *sub = bt_mesh_provisioner_subnet_get(BLE_MESH_KEY_PRIMARY); - prov_ctx.curr_flags = bt_mesh_net_flags(sub); - prov_ctx.curr_iv_index = bt_mesh.iv_index; + prov_ctx.net_idx = BLE_MESH_KEY_PRIMARY; return 0; } @@ -1094,22 +874,22 @@ int bt_mesh_provisioner_set_static_oob_value(const uint8_t *value, uint8_t lengt { int i; - if (value == NULL || length == 0U || length > 16U) { + if (value == NULL || length == 0U || length > BLE_MESH_PROV_STATIC_OOB_MAX_LEN) { 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) { + if (prov_links[i].auth_method == AUTH_METHOD_STATIC) { BT_ERR("Static OOB is being used"); return -EINVAL; } } - (void)memset(prov_ctx.static_oob_val, 0, 16); + (void)memset(prov_ctx.static_oob_val, 0, BLE_MESH_PROV_STATIC_OOB_MAX_LEN); - prov_ctx.static_oob_len = MIN(16, length); + prov_ctx.static_oob_len = MIN(BLE_MESH_PROV_STATIC_OOB_MAX_LEN, length); memcpy(prov_ctx.static_oob_val, value, prov_ctx.static_oob_len); return 0; @@ -1141,19 +921,19 @@ int bt_mesh_provisioner_set_primary_elem_addr(uint16_t 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 + /* If the current can-be allocated address is larger than "primary address + * + element number", then the 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; + if (addr + comp->elem_count > prov_ctx.alloc_addr) { + prov_ctx.alloc_addr = addr + comp->elem_count; } BT_INFO("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_store_prov_info(prov_ctx.primary_addr, prov_ctx.alloc_addr); } bt_mesh_comp_provision(addr); @@ -1164,17 +944,17 @@ int bt_mesh_provisioner_set_primary_elem_addr(uint16_t addr) #if CONFIG_BLE_MESH_TEST_AUTO_ENTER_NETWORK int bt_mesh_test_provisioner_update_alloc_addr(uint16_t unicast_addr, uint16_t element_num) { - uint16_t max_addr = FAST_PROV_ENABLE() ? prov_ctx.fast_prov.unicast_addr_max : PROV_MAX_ADDR_TO_ASSIGN; + uint16_t max_addr = FAST_PROV_ENABLE() ? prov_ctx.fast_prov.unicast_addr_max : UNICAST_ADDR_LIMIT; if (unicast_addr + element_num > max_addr) { BT_WARN("Not enough unicast address to allocate"); - prov_ctx.curr_alloc_addr = BLE_MESH_ADDR_UNASSIGNED; + prov_ctx.alloc_addr = BLE_MESH_ADDR_UNASSIGNED; } else { - prov_ctx.curr_alloc_addr = unicast_addr + element_num; + prov_ctx.alloc_addr = unicast_addr + element_num; } if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.curr_alloc_addr); + bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.alloc_addr); } return 0; @@ -1218,518 +998,216 @@ uint8_t bt_mesh_set_fast_prov_unicast_addr_range(uint16_t min, uint16_t max) prov_ctx.fast_prov.unicast_addr_min = min; prov_ctx.fast_prov.unicast_addr_max = max; - prov_ctx.curr_alloc_addr = prov_ctx.fast_prov.unicast_addr_min; + prov_ctx.alloc_addr = prov_ctx.fast_prov.unicast_addr_min; return 0x0; /* status: success */ } -void bt_mesh_set_fast_prov_flags_iv_index(uint8_t flags, uint32_t iv_index) +static void prov_memory_free(struct bt_mesh_prov_link *link) { - /* BIT0: Key Refresh flag, BIT1: IV Update flag */ - prov_ctx.fast_prov.flags = flags & BIT_MASK(2); - prov_ctx.fast_prov.iv_index = iv_index; + /* TODO: memory optimization - free */ } -#if defined(CONFIG_BLE_MESH_PB_ADV) -static struct net_buf_simple *bt_mesh_pba_get_buf(const uint8_t idx) +#if CONFIG_BLE_MESH_PB_ADV +static struct net_buf_simple *get_rx_buf(const uint8_t idx) { - struct net_buf_simple *buf = &(adv_buf[idx].buf); + struct net_buf_simple *buf = &(rx_buf[idx].buf); net_buf_simple_reset(buf); return buf; } -#endif /* CONFIG_BLE_MESH_PB_ADV */ - -static void prov_memory_free(const uint8_t idx) -{ - PROV_FREE_MEM(idx, dhkey); - PROV_FREE_MEM(idx, auth); - PROV_FREE_MEM(idx, rand); - PROV_FREE_MEM(idx, conf); - PROV_FREE_MEM(idx, local_conf); - PROV_FREE_MEM(idx, conf_salt); - PROV_FREE_MEM(idx, conf_key); - PROV_FREE_MEM(idx, conf_inputs); - PROV_FREE_MEM(idx, prov_salt); -} - -#if defined(CONFIG_BLE_MESH_PB_ADV) -static void buf_sent(int err, void *user_data) -{ - uint8_t idx = (int)user_data; - - if (!link[idx].tx.buf[0]) { - return; - } - - k_delayed_work_submit(&link[idx].tx.retransmit, RETRANSMIT_TIMEOUT); -} - -static struct bt_mesh_send_cb buf_sent_cb = { - .end = buf_sent, -}; -static void free_segments(const uint8_t idx) +static void reset_adv_link(struct bt_mesh_prov_link *link, uint8_t reason) { - int 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) { - break; - } + bt_mesh_prov_clear_tx(link, true); - link[idx].tx.buf[i] = NULL; - bt_mesh_adv_buf_ref_debug(__func__, buf, 3U, BLE_MESH_BUF_REF_SMALL); - /* Mark as canceled */ - BLE_MESH_ADV(buf)->busy = 0U; - net_buf_unref(buf); + if (bt_mesh_prov_get()->prov_link_close) { + bt_mesh_prov_get()->prov_link_close(BLE_MESH_PROV_ADV, reason); } - bt_mesh_pb_buf_unlock(); -} - -static void prov_clear_tx(const uint8_t idx) -{ - BT_DBG("%s", __func__); - - k_delayed_work_cancel(&link[idx].tx.retransmit); - - free_segments(idx); -} + prov_memory_free(link); -static void reset_link(const uint8_t idx, uint8_t reason) -{ - prov_clear_tx(idx); - - if (bt_mesh_atomic_test_and_clear_bit(link[idx].flags, TIMEOUT_START)) { - k_delayed_work_cancel(&link[idx].timeout); - } + k_delayed_work_cancel(&link->prot_timer); - if (prov->prov_link_close) { - prov->prov_link_close(BLE_MESH_PROV_ADV, reason); +#if CONFIG_BLE_MESH_PB_GATT + if (link->conn) { + bt_mesh_conn_unref(link->conn); } +#endif - prov_memory_free(idx); - -#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN /* Remove the link id from exceptional list */ bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE, - BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID, &link[idx].link_id); + BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID, + &link->link_id); #endif /* Clear everything except the retransmit delayed work config */ - memset(&link[idx], 0, offsetof(struct prov_link, tx.retransmit)); + memset(link, 0, offsetof(struct bt_mesh_prov_link, tx.retransmit)); - link[idx].pending_ack = XACT_NVAL; - link[idx].rx.prev_id = XACT_NVAL; + link->pending_ack = PROV_XACT_NVAL; - if (bt_mesh_pub_key_get()) { - bt_mesh_atomic_set_bit(link[idx].flags, LOCAL_PUB_KEY); - } + link->rx.prev_id = PROV_XACT_NVAL; + link->rx.buf = get_rx_buf(link - prov_links); + + link->next_xact_id = pvnr_next_xact_id; + link->reset_adv_link = reset_adv_link; + link->retrans_timeout = close_link; - link[idx].rx.buf = bt_mesh_pba_get_buf(idx); +#if CONFIG_BLE_MESH_FAST_PROV + link->last_tx_pdu = PROV_DATA; +#endif /* CONFIG_BLE_MESH_FAST_PROV */ if (prov_ctx.pba_count) { prov_ctx.pba_count--; } } -static struct net_buf *adv_buf_create(void) +static void send_link_open(struct bt_mesh_prov_link *link) { - struct net_buf *buf = NULL; + int i; + + /* 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->link_id, sizeof(link->link_id)); - buf = bt_mesh_adv_create(BLE_MESH_ADV_PROV, PROV_XMIT, BUF_TIMEOUT); - if (!buf) { - BT_ERR("Out of provisioning buffers"); - return NULL; + while (1) { + for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { + if (bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE) && + prov_links[i].link_id == link->link_id) { + bt_mesh_rand(&link->link_id, sizeof(link->link_id)); + break; + } + } + if (i == CONFIG_BLE_MESH_PBA_SAME_TIME) { + break; + } } - return buf; -} +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN + /* Add the link id into exceptional list */ + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD, + BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID, + &link->link_id); +#endif -static void ack_complete(uint16_t duration, int err, void *user_data) -{ - uint8_t idx = (int)user_data; + bt_mesh_prov_bearer_ctl_send(link, LINK_OPEN, link->uuid, 16); - BT_DBG("xact %u complete", link[idx].pending_ack); + /* Set LINK_ACTIVE just to be in compatibility with current Zephyr code */ + bt_mesh_atomic_set_bit(link->flags, LINK_ACTIVE); - link[idx].pending_ack = XACT_NVAL; + if (bt_mesh_prov_get()->prov_link_open) { + bt_mesh_prov_get()->prov_link_open(BLE_MESH_PROV_ADV); + } } +#endif /* CONFIG_BLE_MESH_PB_ADV */ -static void gen_prov_ack_send(const uint8_t idx, uint8_t xact_id) +#if CONFIG_BLE_MESH_PB_GATT +static int prov_send_gatt(struct bt_mesh_prov_link *link, struct net_buf_simple *msg) { - static const struct bt_mesh_send_cb cb = { - .start = ack_complete, - }; - const struct bt_mesh_send_cb *complete = NULL; - struct net_buf *buf = NULL; - - BT_DBG("xact_id %u", xact_id); - - if (link[idx].pending_ack == xact_id) { - BT_DBG("Not sending duplicate ack"); - return; - } + int err = 0; - buf = adv_buf_create(); - if (!buf) { - return; + if (link->conn == NULL) { + BT_ERR("PB-GATT send, not connected"); + return -ENOTCONN; } - if (link[idx].pending_ack == XACT_NVAL) { - link[idx].pending_ack = xact_id; - complete = &cb; - } else { - complete = NULL; + err = bt_mesh_proxy_client_send(link->conn, BLE_MESH_PROXY_PROV, msg); + if (err) { + BT_ERR("Failed to send PB-GATT pdu"); + return err; } - net_buf_add_be32(buf, link[idx].link_id); - net_buf_add_u8(buf, xact_id); - net_buf_add_u8(buf, GPC_ACK); + k_delayed_work_submit(&link->prot_timer, PROTOCOL_TIMEOUT); - bt_mesh_adv_send(buf, complete, (void *)(int)idx); - net_buf_unref(buf); + return 0; } +#endif /* CONFIG_BLE_MESH_PB_GATT */ -static void send_reliable(const uint8_t idx) -{ - int i; - - link[idx].tx.start = k_uptime_get(); - - for (i = 0; i < ARRAY_SIZE(link[idx].tx.buf); i++) { - struct net_buf *buf = link[idx].tx.buf[i]; +static void prov_invite(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf) +{} - if (!buf) { - break; - } +static void prov_start(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf) +{} - if (i + 1 < ARRAY_SIZE(link[idx].tx.buf) && link[idx].tx.buf[i + 1]) { - bt_mesh_adv_send(buf, NULL, NULL); - } else { - bt_mesh_adv_send(buf, &buf_sent_cb, (void *)(int)idx); - } - } -} +static void prov_data(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf) +{} -static int bearer_ctl_send(const uint8_t idx, uint8_t op, void *data, uint8_t data_len) +static void send_invite(struct bt_mesh_prov_link *link) { - struct net_buf *buf = NULL; - - BT_DBG("op 0x%02x data_len %u", op, data_len); - - prov_clear_tx(idx); + PROV_BUF(buf, 2); - buf = adv_buf_create(); - if (!buf) { - return -ENOBUFS; - } + bt_mesh_prov_buf_init(&buf, PROV_INVITE); - net_buf_add_be32(buf, link[idx].link_id); - /* Transaction ID, always 0 for Bearer messages */ - net_buf_add_u8(buf, 0x00); - net_buf_add_u8(buf, GPC_CTL(op)); - net_buf_add_mem(buf, data, data_len); + net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->prov_attention); - link[idx].tx.buf[0] = buf; - send_reliable(idx); + link->conf_inputs[0] = bt_mesh_prov_get()->prov_attention; - /** We can also use buf->ref and a flag to decide that - * link close has been sent 3 times. - * Here we use another way: use retransmit timer and need - * to make sure the timer is not cancelled during sending - * link close pdu, so we add link[i].tx.id = 0 - */ - if (op == LINK_CLOSE) { - uint8_t reason = *(uint8_t *)data; - link[idx].send_link_close = ((reason & BIT_MASK(2)) << 1) | BIT(0); - link[idx].tx.trans_id = 0; + if (bt_mesh_prov_send(link, &buf)) { + BT_ERR("Failed to send Provisioning Invite"); + close_link(link, CLOSE_REASON_FAILED); + return; } - return 0; + link->expect = PROV_CAPABILITIES; } -static void send_link_open(const uint8_t idx) +static void prov_capabilities(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf) { - int j; + uint16_t output_action = 0U, input_action = 0U; + uint8_t output_size = 0U, input_size = 0U; + uint8_t oob_type = 0U; + uint8_t pub_key_type = 0U; + uint16_t algorithms = 0U; + uint8_t *data = buf->data; + PROV_BUF(prov_buf, 6); - /** 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(uint32_t)); - while (1) { - 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(uint32_t)); - break; - } - } - } - if (j == CONFIG_BLE_MESH_PBA_SAME_TIME) { - break; - } + link->element_num = net_buf_simple_pull_u8(buf); + BT_INFO("Elements: 0x%02x", link->element_num); + if (link->element_num == 0) { + BT_ERR("Invalid element number %d", link->element_num); + goto fail; } -#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) - /* Add the link id into exceptional list */ - bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD, - BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID, &link[idx].link_id); + algorithms = net_buf_simple_pull_be16(buf); + BT_INFO("Algorithms: 0x%04x", algorithms); + if (algorithms != BIT(PROV_ALG_P256_CMAC_AES128) +#if CONFIG_BLE_MESH_PROV_EPA + && algorithms != BIT(PROV_ALG_P256_HMAC_SHA256) #endif - - bearer_ctl_send(idx, LINK_OPEN, link[idx].uuid, 16); - - /* If Provisioner sets LINK_ACTIVE flag once Link Open is sent, we have - * no need to use linking flag (like PB-GATT connecting) to prevent the - * stored device info (UUID, oob_info) being replaced by other received - * unprovisioned device beacons. - * But if Provisioner sets LINK_ACTIVE flag after Link ACK is received, - * we need to use linking flag to prevent device info being replaced. - * Currently we set LINK_ACTIVE flag after sending Link Open. - */ - link[idx].linking = true; - - /* Set LINK_ACTIVE just to be in compatibility with current Zephyr code */ - bt_mesh_atomic_set_bit(link[idx].flags, LINK_ACTIVE); - - if (prov->prov_link_open) { - prov->prov_link_open(BLE_MESH_PROV_ADV); + ) { + BT_ERR("Invalid algorithms 0x%04x", algorithms); + goto fail; } -} -static uint8_t last_seg(uint8_t len) -{ - if (len <= START_PAYLOAD_MAX) { - return 0; + pub_key_type = net_buf_simple_pull_u8(buf); + BT_INFO("Public Key Type: 0x%02x", pub_key_type); + if (pub_key_type > PROV_OOB_PUB_KEY) { + BT_ERR("Invalid public key type 0x%02x", pub_key_type); + goto fail; } - len -= START_PAYLOAD_MAX; + link->public_key = ((bt_mesh_prov_get()->prov_pub_key_oob && + bt_mesh_prov_get()->prov_pub_key_oob_cb) ? + pub_key_type : PROV_NO_OOB_PUB_KEY); - return 1 + (len / CONT_PAYLOAD_MAX); -} + oob_type = net_buf_simple_pull_u8(buf); + BT_INFO("OOB Type: 0x%02x", oob_type); -static inline uint8_t next_transaction_id(const uint8_t idx) -{ - if (link[idx].tx.trans_id > 0x7F) { - link[idx].tx.trans_id = 0x0; + output_size = net_buf_simple_pull_u8(buf); + BT_INFO("Output OOB Size: 0x%02x", output_size); + if (output_size > 0x08) { + BT_ERR("Invalid Output OOB size %d", output_size); + goto fail; } - return link[idx].tx.trans_id++; -} - -static int prov_send_adv(const uint8_t idx, struct net_buf_simple *msg) -{ - struct net_buf *start = NULL, *buf = NULL; - uint8_t seg_len = 0U, seg_id = 0U; - uint8_t xact_id = 0U; - int32_t timeout = PROVISION_TIMEOUT; - - BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); - prov_clear_tx(idx); - - start = adv_buf_create(); - if (!start) { - return -ENOBUFS; - } - - xact_id = next_transaction_id(idx); - net_buf_add_be32(start, link[idx].link_id); - net_buf_add_u8(start, xact_id); - - net_buf_add_u8(start, GPC_START(last_seg(msg->len))); - net_buf_add_be16(start, msg->len); - net_buf_add_u8(start, bt_mesh_fcs_calc(msg->data, msg->len)); - - link[idx].tx.buf[0] = start; - /* Changed by Espressif, get message type */ - link[idx].tx_pdu_type = msg->data[0]; - - seg_len = MIN(msg->len, START_PAYLOAD_MAX); - BT_DBG("seg 0 len %u: %s", seg_len, bt_hex(msg->data, seg_len)); - net_buf_add_mem(start, msg->data, seg_len); - net_buf_simple_pull(msg, seg_len); - - buf = start; - for (seg_id = 1; msg->len > 0; seg_id++) { - if (seg_id >= ARRAY_SIZE(link[idx].tx.buf)) { - BT_ERR("Too big message (seg_id %d)", seg_id); - free_segments(idx); - return -E2BIG; - } - - buf = adv_buf_create(); - if (!buf) { - free_segments(idx); - return -ENOBUFS; - } - - link[idx].tx.buf[seg_id] = buf; - - seg_len = MIN(msg->len, CONT_PAYLOAD_MAX); - - BT_DBG("seg_id %u len %u: %s", seg_id, seg_len, - bt_hex(msg->data, seg_len)); - - net_buf_add_be32(buf, link[idx].link_id); - net_buf_add_u8(buf, xact_id); - net_buf_add_u8(buf, GPC_CONT(seg_id)); - net_buf_add_mem(buf, msg->data, seg_len); - net_buf_simple_pull(msg, seg_len); - } - - send_reliable(idx); - -#if defined(CONFIG_BLE_MESH_FAST_PROV) - if (link[idx].tx_pdu_type >= PROV_DATA) { - timeout = K_SECONDS(60); - } -#endif - if (!bt_mesh_atomic_test_and_set_bit(link[idx].flags, TIMEOUT_START)) { - k_delayed_work_submit(&link[idx].timeout, timeout); - } - - return 0; -} -#endif /* CONFIG_BLE_MESH_PB_ADV */ - -#if defined(CONFIG_BLE_MESH_PB_GATT) -static int prov_send_gatt(const uint8_t idx, struct net_buf_simple *msg) -{ - int err = 0; - - if (!link[idx].conn) { - return -ENOTCONN; - } - - err = bt_mesh_proxy_client_send(link[idx].conn, BLE_MESH_PROXY_PROV, msg); - if (err) { - BT_ERR("Failed to send PB-GATT pdu"); - return err; - } - - if (!bt_mesh_atomic_test_and_set_bit(link[idx].flags, TIMEOUT_START)) { - k_delayed_work_submit(&link[idx].timeout, PROVISION_TIMEOUT); - } - - return 0; -} -#endif /* CONFIG_BLE_MESH_PB_GATT */ - -static inline int prov_send(const uint8_t idx, struct net_buf_simple *buf) -{ -#if defined(CONFIG_BLE_MESH_PB_ADV) - if (idx < CONFIG_BLE_MESH_PBA_SAME_TIME) { - return prov_send_adv(idx, buf); - } -#endif - -#if defined(CONFIG_BLE_MESH_PB_GATT) - if (idx < BLE_MESH_PROV_SAME_TIME -#if defined(CONFIG_BLE_MESH_PB_ADV) - && idx >= CONFIG_BLE_MESH_PBA_SAME_TIME -#endif - ) { - return prov_send_gatt(idx, buf); - } -#endif - - BT_ERR("Invalid link index %d", idx); - return -EINVAL; -} - -static void prov_buf_init(struct net_buf_simple *buf, uint8_t type) -{ - net_buf_simple_reserve(buf, PROV_BUF_HEADROOM); - net_buf_simple_add_u8(buf, type); -} - -static void prov_invite(const uint8_t idx, const uint8_t *data) -{ - BT_DBG("%s", __func__); -} - -static void prov_start(const uint8_t idx, const uint8_t *data) -{ - BT_DBG("%s", __func__); -} - -static void prov_data(const uint8_t idx, const uint8_t *data) -{ - BT_DBG("%s", __func__); -} - -static void send_invite(const uint8_t idx) -{ - PROV_BUF(buf, 2); - - prov_buf_init(&buf, PROV_INVITE); - - net_buf_simple_add_u8(&buf, prov->prov_attention); - - link[idx].conf_inputs[0] = prov->prov_attention; - - if (prov_send(idx, &buf)) { - BT_ERR("Failed to send Provisioning Invite"); - close_link(idx, CLOSE_REASON_FAILED); - return; - } - - link[idx].expect = PROV_CAPABILITIES; -} - -static void prov_capabilities(const uint8_t idx, const uint8_t *data) -{ - PROV_BUF(buf, 6); - uint16_t algorithms = 0U, output_action = 0U, input_action = 0U; - uint8_t element_num = 0U, pub_key_oob = 0U, static_oob = 0U, - output_size = 0U, input_size = 0U; - uint8_t auth_method = 0U, auth_action = 0U, auth_size = 0U; - - element_num = data[0]; - BT_INFO("Elements: 0x%02x", element_num); - if (!element_num) { - BT_ERR("Invalid element number %d", element_num); - goto fail; - } - link[idx].element_num = element_num; - - algorithms = sys_get_be16(&data[1]); - BT_INFO("Algorithms: 0x%04x", algorithms); - if (algorithms != BIT(PROV_ALG_P256)) { - BT_ERR("Invalid algorithms 0x%04x", algorithms); - goto fail; - } - - pub_key_oob = data[3]; - BT_INFO("Public Key Type: 0x%02x", pub_key_oob); - if (pub_key_oob > 0x01) { - BT_ERR("Invalid public key type 0x%02x", pub_key_oob); - goto fail; - } - pub_key_oob = ((prov->prov_pub_key_oob && - prov->prov_pub_key_oob_cb) ? pub_key_oob : 0x00); - - static_oob = data[4]; - BT_INFO("Static OOB Type: 0x%02x", static_oob); - if (static_oob > 0x01) { - BT_ERR("Invalid Static OOB type 0x%02x", static_oob); - goto fail; - } - static_oob = (prov_ctx.static_oob_len ? static_oob : 0x00); - - output_size = data[5]; - BT_INFO("Output OOB Size: 0x%02x", output_size); - if (output_size > 0x08) { - BT_ERR("Invalid Output OOB size %d", output_size); - goto fail; - } - - output_action = sys_get_be16(&data[6]); + output_action = net_buf_simple_pull_be16(buf); BT_INFO("Output OOB Action: 0x%04x", output_action); if (output_action > 0x1f) { BT_ERR("Invalid Output OOB action 0x%04x", output_action); @@ -1737,191 +1215,192 @@ static void prov_capabilities(const uint8_t idx, const uint8_t *data) } /* Provisioner select output action */ - if (prov->prov_input_num && output_size) { + if (bt_mesh_prov_get()->prov_input_num && output_size) { output_action = __builtin_ctz(output_action); } else { output_size = 0x0; output_action = 0x0; } - input_size = data[8]; + input_size = net_buf_simple_pull_u8(buf); BT_INFO("Input OOB Size: 0x%02x", input_size); if (input_size > 0x08) { BT_ERR("Invalid Input OOB size %d", input_size); goto fail; } - input_action = sys_get_be16(&data[9]); + input_action = net_buf_simple_pull_be16(buf); BT_INFO("Input OOB Action: 0x%04x", input_action); if (input_action > 0x0f) { BT_ERR("Invalid Input OOB action 0x%04x", input_action); goto fail; } - /* 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); + /* NOTE: If bit 1 of the OOB Type field is set to 1, bit 0 of + * the Algorithms field shall be set to 0 (case_1)and at least one of + * the conditions listed below shall be met: + * • Bit 0 of the OOB Type field is set to 1.(case_2) + * • The Output OOB Size field is not equal to 0x00.(case_3) + * • The Input OOB Size field is not equal to 0x00.(case_4) + * That means: + * !(case_1 && (case_2 || case_3 || case_4)) goto fail + * equals: + * !case1 || !(case_2 || case_3 || case_4) goto fail + */ + if (oob_type & BIT(PROV_ONLY_OOB_AUTH_SUPPORT)) { + if ((algorithms & BIT(PROV_ALG_P256_CMAC_AES128)) || + (!((oob_type & BIT(PROV_STATIC_OOB_AVAILABLE)) == 0x00 || + output_size == 0x00 || input_size == 0x00))) { + goto fail; + } } /* Provisioner select input action */ - if (prov->prov_output_num && input_size) { + if (bt_mesh_prov_get()->prov_output_num && input_size) { input_action = __builtin_ctz(input_action); } else { input_size = 0x0; input_action = 0x0; } - if (static_oob) { + if (oob_type & BIT(PROV_STATIC_OOB_AVAILABLE)) { /* if static oob is valid, just use static oob */ - auth_method = AUTH_METHOD_STATIC; - auth_action = 0x00; - auth_size = 0x00; + link->auth_method = AUTH_METHOD_STATIC; + link->auth_action = 0x00; + link->auth_size = 0x00; } else { if (!output_size && !input_size) { - auth_method = AUTH_METHOD_NO_OOB; - auth_action = 0x00; - auth_size = 0x00; + link->auth_method = AUTH_METHOD_NO_OOB; + link->auth_action = 0x00; + link->auth_size = 0x00; } else if (!output_size && input_size) { - auth_method = AUTH_METHOD_INPUT; - auth_action = (uint8_t)input_action; - auth_size = input_size; + link->auth_method = AUTH_METHOD_INPUT; + link->auth_action = (uint8_t)input_action; + link->auth_size = input_size; } else { - auth_method = AUTH_METHOD_OUTPUT; - auth_action = (uint8_t)output_action; - auth_size = output_size; + link->auth_method = AUTH_METHOD_OUTPUT; + link->auth_action = (uint8_t)output_action; + link->auth_size = output_size; } } /* Store provisioning capabilities value in conf_inputs */ - memcpy(&link[idx].conf_inputs[1], data, 11); + memcpy(&link->conf_inputs[1], data, 11); - prov_buf_init(&buf, PROV_START); - net_buf_simple_add_u8(&buf, prov->prov_algorithm); - net_buf_simple_add_u8(&buf, pub_key_oob); - net_buf_simple_add_u8(&buf, auth_method); - net_buf_simple_add_u8(&buf, auth_action); - net_buf_simple_add_u8(&buf, auth_size); + bt_mesh_prov_buf_init(&prov_buf, PROV_START); - memcpy(&link[idx].conf_inputs[12], &buf.data[1], 5); +#if CONFIG_BLE_MESH_PROV_EPA + if (algorithms & BIT(PROV_ALG_P256_HMAC_SHA256)) { + net_buf_simple_add_u8(&prov_buf, PROV_ALG_P256_HMAC_SHA256); + link->algorithm = PROV_ALG_P256_HMAC_SHA256; + } else { + net_buf_simple_add_u8(&prov_buf, PROV_ALG_P256_CMAC_AES128); + link->algorithm = PROV_ALG_P256_CMAC_AES128; + } +#else + net_buf_simple_add_u8(&prov_buf, PROV_ALG_P256_CMAC_AES128); + link->algorithm = PROV_ALG_P256_CMAC_AES128; +#endif + + net_buf_simple_add_u8(&prov_buf, link->public_key); + net_buf_simple_add_u8(&prov_buf, link->auth_method); + net_buf_simple_add_u8(&prov_buf, link->auth_action); + net_buf_simple_add_u8(&prov_buf, link->auth_size); + + memcpy(&link->conf_inputs[12], &prov_buf.data[1], 5); - if (prov_send(idx, &buf)) { + if (bt_mesh_prov_send(link, &prov_buf)) { BT_ERR("Failed to send Provisioning Start"); goto fail; } - link[idx].auth_method = auth_method; - link[idx].auth_action = auth_action; - link[idx].auth_size = auth_size; + /* After prov start is sent, use OOB to get remote public key. + * And we just follow the procedure in Figure 5.15 of Section + * 5.4.2.3 of Mesh Profile Spec. + */ + if (link->public_key == PROV_OOB_PUB_KEY && + bt_mesh_prov_get()->prov_pub_key_oob_cb(link - prov_links)) { + BT_ERR("Failed to notify input OOB Public Key"); + goto fail; + } - /** After prov start sent, use OOB to get remote public key. - * And we just follow the procedure in Figure 5.15 of Section - * 5.4.2.3 of Mesh Profile Spec. + /* If the link is used by PB-Remote, return here. Need to + * wait for Remote Provisioning PDU Outbound Report, then + * send Remote Provisioning PDU Send with Public Key. */ - if (pub_key_oob) { - if (prov->prov_pub_key_oob_cb(idx)) { - BT_ERR("Failed to notify input OOB Public Key"); - goto fail; - } + if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) { + return; } - /** If using PB-ADV, need to listen for transaction ack, - * after ack is received, provisioner can send public key. +#if CONFIG_BLE_MESH_PB_ADV + /* If the link is used by PB-ADV, need to wait for the + * Transaction ACK for Provisioning Start. When ACK is + * received, we can send Provisioning Public Key. */ -#if defined(CONFIG_BLE_MESH_PB_ADV) - if (idx < CONFIG_BLE_MESH_PBA_SAME_TIME) { - link[idx].expect_ack_for = PROV_START; + if (link - prov_links < CONFIG_BLE_MESH_PBA_SAME_TIME) { + link->expect_ack_for = PROV_START; return; } #endif /* CONFIG_BLE_MESH_PB_ADV */ - send_pub_key(idx, pub_key_oob); + send_pub_key(link); return; fail: - close_link(idx, CLOSE_REASON_FAILED); - return; -} - -static bt_mesh_output_action_t output_action(uint8_t action) -{ - switch (action) { - case OUTPUT_OOB_BLINK: - return BLE_MESH_BLINK; - case OUTPUT_OOB_BEEP: - return BLE_MESH_BEEP; - case OUTPUT_OOB_VIBRATE: - return BLE_MESH_VIBRATE; - case OUTPUT_OOB_NUMBER: - return BLE_MESH_DISPLAY_NUMBER; - case OUTPUT_OOB_STRING: - return BLE_MESH_DISPLAY_STRING; - default: - return BLE_MESH_NO_OUTPUT; - } -} - -static bt_mesh_input_action_t input_action(uint8_t action) -{ - switch (action) { - case INPUT_OOB_PUSH: - return BLE_MESH_PUSH; - case INPUT_OOB_TWIST: - return BLE_MESH_TWIST; - case INPUT_OOB_NUMBER: - return BLE_MESH_ENTER_NUMBER; - case INPUT_OOB_STRING: - return BLE_MESH_ENTER_STRING; - default: - return BLE_MESH_NO_INPUT; - } + close_link(link, CLOSE_REASON_FAILED); } -static int prov_auth(const uint8_t idx, uint8_t method, uint8_t action, uint8_t size) +static int prov_auth(struct bt_mesh_prov_link *link, + uint8_t method, uint8_t action, + uint8_t size) { bt_mesh_output_action_t output = 0U; bt_mesh_input_action_t input = 0U; - - link[idx].auth = (uint8_t *)bt_mesh_calloc(PROV_AUTH_VAL_SIZE); - if (!link[idx].auth) { - BT_ERR("%s, Out of memory", __func__); - close_link(idx, CLOSE_REASON_FAILED); - return -ENOMEM; - } + uint8_t auth_size = PROV_AUTH_SIZE(link); switch (method) { case AUTH_METHOD_NO_OOB: if (action || size) { return -EINVAL; } - memset(link[idx].auth, 0, 16); + + memset(link->auth, 0, PROV_AUTH_MAX_SIZE); return 0; case AUTH_METHOD_STATIC: if (action || size) { return -EINVAL; } - 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); + + if (prov_ctx.static_oob_len > auth_size) { + memcpy(link->auth, prov_ctx.static_oob_val, auth_size); + } else { + memcpy(link->auth + auth_size - prov_ctx.static_oob_len, + prov_ctx.static_oob_val, prov_ctx.static_oob_len); + memset(link->auth, 0, auth_size - prov_ctx.static_oob_len); + } + return 0; case AUTH_METHOD_OUTPUT: /* Use auth_action to get device output action */ - output = output_action(action); + output = bt_mesh_prov_output_action(action); if (!output) { return -EINVAL; } - return prov->prov_input_num(AUTH_METHOD_OUTPUT, output, size, idx); + + return bt_mesh_prov_get()->prov_input_num(AUTH_METHOD_OUTPUT, output, + size, link - prov_links); case AUTH_METHOD_INPUT: /* Use auth_action to get device input action */ - input = input_action(action); + input = bt_mesh_prov_input_action(action); if (!input) { return -EINVAL; } /* Provisioner ouput number/string and wait for device's Provisioning Input Complete PDU */ - link[idx].expect = PROV_INPUT_COMPLETE; + link->expect = PROV_INPUT_COMPLETE; /* NOTE: The Bluetooth SIG recommends that mesh implementations enforce a randomly * selected AuthValue using all of the available bits, where permitted by the @@ -1953,10 +1432,11 @@ static int prov_auth(const uint8_t idx, uint8_t method, uint8_t action, uint8_t } str[size] = '\0'; - memcpy(link[idx].auth, str, size); - memset(link[idx].auth + size, 0, PROV_AUTH_VAL_SIZE - size); + memcpy(link->auth, str, size); + memset(link->auth + size, 0, auth_size - size); - return prov->prov_output_num(AUTH_METHOD_INPUT, input, str, size, idx); + return bt_mesh_prov_get()->prov_output_num(AUTH_METHOD_INPUT, input, str, + size, link - prov_links); } else { uint32_t div[8] = { 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 }; uint32_t num = 0U; @@ -1974,10 +1454,11 @@ static int prov_auth(const uint8_t idx, uint8_t method, uint8_t action, uint8_t num %= div[size - 1]; } - sys_put_be32(num, &link[idx].auth[12]); - memset(link[idx].auth, 0, 12); + sys_put_be32(num, &link->auth[auth_size - 4]); + memset(link->auth, 0, auth_size - 4); - return prov->prov_output_num(AUTH_METHOD_INPUT, input, &num, size, idx); + return bt_mesh_prov_get()->prov_output_num(AUTH_METHOD_INPUT, input, &num, + size, link - prov_links); } default: @@ -1985,152 +1466,205 @@ static int prov_auth(const uint8_t idx, uint8_t method, uint8_t action, uint8_t } } -static void send_confirm(const uint8_t idx) +static void send_confirm(struct bt_mesh_prov_link *link) { - PROV_BUF(buf, 17); uint8_t *conf = NULL; + uint8_t conf_salt_size = 0; + uint8_t conf_key_size = 0; + uint8_t rand_size = 0; + uint8_t conf_size = 0; - BT_DBG("ConfInputs[0] %s", bt_hex(link[idx].conf_inputs, 64)); - 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)); + conf_salt_size = PROV_CONF_SALT_SIZE(link); + conf_key_size = PROV_CONF_KEY_SIZE(link); + rand_size = PROV_RAND_SIZE(link); + conf_size = PROV_CONF_SIZE(link); - link[idx].conf_salt = (uint8_t *)bt_mesh_calloc(PROV_CONF_SALT_SIZE); - if (!link[idx].conf_salt) { - BT_ERR("%s, Out of memory", __func__); - goto fail; - } + PROV_BUF(buf, (conf_size + 1)); - link[idx].conf_key = (uint8_t *)bt_mesh_calloc(PROV_CONF_KEY_SIZE); - if (!link[idx].conf_key) { - BT_ERR("%s, Out of memory", __func__); - goto fail; + if (!bt_mesh_atomic_test_bit(link->flags, HAVE_DHKEY)) { + BT_WARN("Wait for generating DHKey"); + return; } - if (bt_mesh_prov_conf_salt(link[idx].conf_inputs, link[idx].conf_salt)) { - BT_ERR("Failed to generate confirmation salt"); - goto fail; - } + BT_DBG("ConfInputs[0] %s", bt_hex(link->conf_inputs, 64)); + BT_DBG("ConfInputs[64] %s", bt_hex(link->conf_inputs + 64, 64)); + BT_DBG("ConfInputs[128] %s", bt_hex(link->conf_inputs + 128, 17)); - BT_DBG("ConfirmationSalt: %s", bt_hex(link[idx].conf_salt, 16)); + if (link->algorithm == PROV_ALG_P256_CMAC_AES128) { + if (bt_mesh_prov_conf_salt(link->conf_inputs, link->conf_salt)) { + BT_ERR("Failed to generate confirmation salt"); + goto fail; + } - if (bt_mesh_prov_conf_key(link[idx].dhkey, link[idx].conf_salt, link[idx].conf_key)) { - BT_ERR("Failed to generate confirmation key"); - goto fail; + if (bt_mesh_prov_conf_key(link->dhkey, link->conf_salt, link->conf_key)) { + BT_ERR("Failed to generate confirmation key"); + goto fail; + } } +#if CONFIG_BLE_MESH_PROV_EPA + else { + if (bt_mesh_prov_conf_salt_epa(link->conf_inputs, link->conf_salt)) { + BT_ERR("Failed to generate confirmation salt"); + goto fail; + } - BT_DBG("ConfirmationKey: %s", bt_hex(link[idx].conf_key, 16)); - - link[idx].rand = bt_mesh_calloc(PROV_RANDOM_SIZE); - if (!link[idx].rand) { - BT_ERR("%s, Out of memory", __func__); - goto fail; + if (bt_mesh_prov_conf_key_epa(link->dhkey, link->auth, link->conf_salt, link->conf_key)) { + BT_ERR("Failed to generate confirmation key"); + goto fail; + } } +#endif - if (bt_mesh_rand(link[idx].rand, PROV_RANDOM_SIZE)) { + if (bt_mesh_rand(link->rand, rand_size)) { BT_ERR("Failed to generate random number"); goto fail; } - BT_DBG("LocalRandom: %s", bt_hex(link[idx].rand, 16)); + BT_DBG("ConfirmationSalt: %s", bt_hex(link->conf_salt, conf_salt_size)); + BT_DBG("ConfirmationKey: %s", bt_hex(link->conf_key, conf_key_size)); + BT_DBG("LocalRandom: %s", bt_hex(link->rand, rand_size)); - prov_buf_init(&buf, PROV_CONFIRM); + bt_mesh_prov_buf_init(&buf, PROV_CONFIRM); - conf = net_buf_simple_add(&buf, 16); + conf = net_buf_simple_add(&buf, conf_size); - if (bt_mesh_prov_conf(link[idx].conf_key, link[idx].rand, - link[idx].auth, conf)) { - BT_ERR("Failed to generate confirmation value"); - goto fail; + if (link->algorithm == PROV_ALG_P256_CMAC_AES128) { + if (bt_mesh_prov_conf(link->conf_key, link->rand, link->auth, conf)) { + BT_ERR("Failed to generate confirmation value"); + goto fail; + } } - - link[idx].local_conf = bt_mesh_calloc(PROV_CONFIRM_SIZE); - if (!link[idx].local_conf) { - BT_ERR("%s, Out of memory", __func__); - goto fail; +#if CONFIG_BLE_MESH_PROV_EPA + else { + if (bt_mesh_prov_conf_epa(link->conf_key, link->rand, conf)) { + BT_ERR("Failed to generate confirmation value"); + goto fail; + } } +#endif - memcpy(link[idx].local_conf, conf, PROV_CONFIRM_SIZE); + memcpy(link->local_conf, conf, conf_size); - if (prov_send(idx, &buf)) { + if (bt_mesh_prov_send(link, &buf)) { BT_ERR("Failed to send Provisioning Confirm"); goto fail; } - link[idx].expect = PROV_CONFIRM; + link->expect = PROV_CONFIRM; return; fail: - close_link(idx, CLOSE_REASON_FAILED); - return; + close_link(link, CLOSE_REASON_FAILED); } int bt_mesh_provisioner_set_oob_input_data(const uint8_t idx, const uint8_t *val, bool num_flag) { - /** This function should be called in the prov_input_num - * callback, after the data output by device has been - * input by provisioner. - * Paramter size is used to indicate the length of data - * indicated by Pointer val, for example, if device output - * data is 12345678(decimal), the data in auth value will - * be 0xBC614E. - * Parameter num_flag is used to indicate whether the value - * input by provisioner is number or string. + /* This function should be called in the prov_input_num callback, + * after the data output by device has been input by Provisioner. + * + * "val" is the auth value, for example, if device output data is + * 12345678(decimal), the data in auth value will be 0xBC614E. + * + * "num_flag" is used to indicate whether the value input by + * Provisioner is number or string. */ - if (!link[idx].auth) { - BT_ERR("Invalid link auth"); + struct bt_mesh_prov_link *link = NULL; + uint8_t auth_size = 0; + + if (idx >= ARRAY_SIZE(prov_links) || val == NULL) { + BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } - BT_INFO("Link idx %d, type %s", idx, num_flag ? "number" : "string"); + link = &prov_links[idx]; + auth_size = PROV_AUTH_SIZE(link); + + if (!bt_mesh_atomic_test_bit(link->flags, LINK_ACTIVE) || + link->auth_method != AUTH_METHOD_OUTPUT) { + BT_ERR("Not ready for Output OOB, link idx %d", idx); + return -EIO; + } + + memset(link->auth, 0, PROV_AUTH_MAX_SIZE); - memset(link[idx].auth, 0, 16); if (num_flag) { /* Provisioner inputs number */ - sys_memcpy_swap(link[idx].auth + 12, val, sizeof(uint32_t)); + sys_memcpy_swap(link->auth + auth_size - 4, val, sizeof(uint32_t)); } else { /* Provisioner inputs string */ - memcpy(link[idx].auth, val, link[idx].auth_size); + memcpy(link->auth, val, link->auth_size); } - send_confirm(idx); + BT_INFO("Output OOB, idx %d, type %s, auth %s", idx, + num_flag ? "number" : "string", bt_hex(link->auth, auth_size)); + + /* If the link is used by PB-Remote, and we are + * waiting for Remote Provisioning PDU Outbound + * Report for Public Key, return here. + */ + if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) { + if (bt_mesh_atomic_test_bit(link->flags, WAIT_PK_OBR)) { + BT_INFO("PB-Remote, wait for outbound report for public key"); + /* Use SEND_CONFIRM flag to indicate that Remote + * Provisioning PDU Send with confirmation could + * be sent when the Remote Provisioning Outbound + * Report for Public Key is received. + */ + bt_mesh_atomic_set_bit(link->flags, SEND_CONFIRM); + return 0; + } + } + + send_confirm(link); + return 0; } int bt_mesh_provisioner_set_oob_output_data(const uint8_t idx, const uint8_t *num, uint8_t size, bool num_flag) { - /** This function should be called in the prov_output_num - * callback, after the data has been output by provisioner. - * Parameter size is used to indicate the length of data - * indicated by Pointer num, for example, if provisioner - * output data is 12345678(decimal), the data in auth value - * will be 0xBC614E. - * Parameter num_flag is used to indicate whether the value - * output by provisioner is number or string. + /* This function should be called in the prov_output_num callback, + * after the data has been output by Provisioner. + * + * "size" is used to indicate the length of "num", for example, if + * Provisioner output data is 12345678(decimal), the data in auth + * value will be 0xBC614E. + * + * "num_flag" is used to indicate whether the value output by + * Provisioner is number or string. */ - if (num == NULL || size > BLE_MESH_PROV_INPUT_OOB_MAX_LEN) { + struct bt_mesh_prov_link *link = NULL; + uint8_t auth_size = 0; + + if (idx >= ARRAY_SIZE(prov_links) || num == NULL || + size > BLE_MESH_PROV_INPUT_OOB_MAX_LEN) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } - if (!link[idx].auth) { - BT_ERR("Invalid link auth"); - return -EINVAL; + link = &prov_links[idx]; + auth_size = PROV_AUTH_SIZE(link); + + if (!bt_mesh_atomic_test_bit(link->flags, LINK_ACTIVE) || + link->auth_method != AUTH_METHOD_INPUT) { + BT_ERR("Not ready for Input OOB, link idx %d", idx); + return -EIO; } - BT_INFO("Link idx %d, type %s", idx, num_flag ? "number" : "string"); + memset(link->auth, 0, PROV_AUTH_MAX_SIZE); if (num_flag) { /* Provisioner output number */ - memset(link[idx].auth, 0, 16); - sys_memcpy_swap(link[idx].auth + 16 - size, num, size); + sys_memcpy_swap(link->auth + auth_size - size, num, size); } else { /* Provisioner output string */ - memset(link[idx].auth, 0, 16); - memcpy(link[idx].auth, num, size); + memcpy(link->auth, num, size); } - link[idx].expect = PROV_INPUT_COMPLETE; + BT_INFO("Input OOB, idx %d, type %s, auth %s", idx, + num_flag ? "number" : "string", bt_hex(link->auth, auth_size)); + + link->expect = PROV_INPUT_COMPLETE; return 0; } @@ -2138,234 +1672,270 @@ int bt_mesh_provisioner_set_oob_output_data(const uint8_t idx, const uint8_t *nu int bt_mesh_provisioner_read_oob_pub_key(const uint8_t idx, const uint8_t pub_key_x[32], const uint8_t pub_key_y[32]) { - if (!link[idx].conf_inputs) { - BT_ERR("Invalid link conf_inputs"); + struct bt_mesh_prov_link *link = NULL; + + if (idx >= ARRAY_SIZE(prov_links) || pub_key_x == NULL || pub_key_y == NULL) { + BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } + link = &prov_links[idx]; + + if (!bt_mesh_atomic_test_bit(link->flags, LINK_ACTIVE) || + link->public_key != PROV_OOB_PUB_KEY) { + BT_ERR("Not ready for OOB Public Key, link idx %d", idx); + return -EIO; + } + + BT_INFO("OOB Public Key, idx %d, x %s, y %s", idx, + bt_hex(pub_key_x, 32), bt_hex(pub_key_y, 32)); + /* Swap X and Y halves independently to big-endian */ - sys_memcpy_swap(&link[idx].conf_inputs[81], pub_key_x, 32); - sys_memcpy_swap(&link[idx].conf_inputs[81] + 32, pub_key_y, 32); + sys_memcpy_swap(&link->conf_inputs[81], pub_key_x, 32); + sys_memcpy_swap(&link->conf_inputs[81] + 32, pub_key_y, 32); - bt_mesh_atomic_set_bit(link[idx].flags, REMOTE_PUB_KEY); + bt_mesh_atomic_set_bit(link->flags, REMOTE_PUB_KEY); - if (bt_mesh_atomic_test_and_clear_bit(link[idx].flags, WAIT_GEN_DHKEY)) { - prov_gen_dh_key(idx); + if (bt_mesh_atomic_test_and_clear_bit(link->flags, WAIT_GEN_DHKEY)) { + prov_gen_dh_key(link); } return 0; } -static void prov_dh_key_cb(const uint8_t key[32], const uint8_t idx) +static void prov_gen_dh_key(struct bt_mesh_prov_link *link) { - BT_DBG("%p", key); + uint8_t pub_key[64] = {0}; + uint8_t dhkey[32] = {0}; - if (!key) { + /* Copy public key in little-endian for generating DHKey. + * X and Y halves are swapped independently. + */ + sys_memcpy_swap(&pub_key[0], &link->conf_inputs[81], 32); + sys_memcpy_swap(&pub_key[32], &link->conf_inputs[113], 32); + + if (bt_mesh_dh_key_gen(pub_key, dhkey)) { BT_ERR("Failed to generate DHKey"); - goto fail; + close_link(link, CLOSE_REASON_FAILED); + return; } - link[idx].dhkey = (uint8_t *)bt_mesh_calloc(PROV_DH_KEY_SIZE); - if (!link[idx].dhkey) { - BT_ERR("%s, Out of memory", __func__); - goto fail; - } - sys_memcpy_swap(link[idx].dhkey, key, 32); + sys_memcpy_swap(link->dhkey, dhkey, 32); - BT_DBG("DHkey: %s", bt_hex(link[idx].dhkey, 32)); + BT_DBG("DHKey: %s", bt_hex(link->dhkey, 32)); - bt_mesh_atomic_set_bit(link[idx].flags, HAVE_DHKEY); + bt_mesh_atomic_set_bit(link->flags, HAVE_DHKEY); - /** After dhkey is generated, if auth_method is No OOB or - * Static OOB, provisioner can start to send confirmation. - * If output OOB is used by the device, provisioner need - * to watch out the output number and input it as auth_val. - * If input OOB is used by the device, provisioner need - * to output a value, and wait for prov input complete pdu. + /* After DHKey is generated, if auth_method is No OOB or + * Static OOB, Provisioner can start to send confirmation. + * + * If output OOB is used by the device, Provisioner needs + * to watch out the output number and input it as auth_val. + * + * If input OOB is used by the device, Provisioner needs + * to output a value, and wait for prov input complete pdu. */ - if (prov_auth(idx, link[idx].auth_method, - link[idx].auth_action, link[idx].auth_size) < 0) { + if (prov_auth(link, link->auth_method, + link->auth_action, link->auth_size)) { BT_ERR("Failed to authenticate"); - goto fail; - } - if (link[idx].auth_method == AUTH_METHOD_OUTPUT || - link[idx].auth_method == AUTH_METHOD_INPUT) { + close_link(link, CLOSE_REASON_FAILED); return; } - if (link[idx].expect != PROV_INPUT_COMPLETE) { - send_confirm(idx); + /* If authentication method is Output OOB, wait for the + * authentication value; + * If authentication method is Input OOB, wait for the + * Provisioning Input Complete. + */ + if (link->auth_method == AUTH_METHOD_OUTPUT || + link->auth_method == AUTH_METHOD_INPUT) { + return; } - return; - -fail: - close_link(idx, CLOSE_REASON_FAILED); - return; -} - -static void prov_gen_dh_key(const uint8_t idx) -{ - uint8_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. + /* This function will be invoked when the Device Public + * Key is received. + * If the link is used by PB-Remote, we can decide the + * next move depends on the Public Key type. */ - sys_memcpy_swap(&pub_key[0], &link[idx].conf_inputs[81], 32); - sys_memcpy_swap(&pub_key[32], &link[idx].conf_inputs[113], 32); + if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) { + if (bt_mesh_atomic_test_bit(link->flags, WAIT_PK_OBR)) { + BT_INFO("PB-Remote, wait for outbound report for public key"); + /* Use SEND_CONFIRM flag to indicate that Remote + * Provisioning PDU Send with confirmation could + * be sent when the Remote Provisioning Outbound + * Report for Public Key is received. + */ + bt_mesh_atomic_set_bit(link->flags, SEND_CONFIRM); + return; + } + } - if (bt_mesh_dh_key_gen(pub_key, prov_dh_key_cb, idx)) { - BT_ERR("Failed to generate DHKey"); - close_link(idx, CLOSE_REASON_FAILED); - return; + /* If the link is not used by PB-Remote, send confirmation here. + * If the link is used by PB-Remote: + * a) If No OOB Public Key is used, which means Device Public + * Key is received, and WAIT_PK_OBR will not be set, send + * Remote Provisioning PDU Send with confirmation here. + * b) If OOB Public Key is used, which means Device Public Key + * has been input through OOB, and WAIT_PK_OBR is cleared + * (i.e. Remote Provisioning Outbound Report for Public Key + * is received), send Remote Provisioning PDU Send with + * confirmation here. + * Note: + * No need to check if the next expect pdu is Provisioning + * Input Complete, because if the authentication method is + * Output OOB or Input OOB, it will directly return above. + */ + if (link->expect != PROV_INPUT_COMPLETE) { + send_confirm(link); } } -static void send_pub_key(const uint8_t idx, uint8_t oob) +static void send_pub_key(struct bt_mesh_prov_link *link) { - PROV_BUF(buf, 65); const uint8_t *key = NULL; + PROV_BUF(buf, 65); key = bt_mesh_pub_key_get(); if (!key) { BT_ERR("No public key available"); - close_link(idx, CLOSE_REASON_FAILED); + close_link(link, CLOSE_REASON_FAILED); return; } BT_DBG("Local Public Key: %s", bt_hex(key, 64)); - bt_mesh_atomic_set_bit(link[idx].flags, LOCAL_PUB_KEY); - - prov_buf_init(&buf, PROV_PUB_KEY); + bt_mesh_prov_buf_init(&buf, PROV_PUB_KEY); /* Swap X and Y halves independently to big-endian */ sys_memcpy_swap(net_buf_simple_add(&buf, 32), key, 32); sys_memcpy_swap(net_buf_simple_add(&buf, 32), &key[32], 32); /* Store provisioner public key value in conf_inputs */ - memcpy(&link[idx].conf_inputs[17], &buf.data[1], 64); + memcpy(&link->conf_inputs[17], &buf.data[1], 64); - if (prov_send(idx, &buf)) { + if (bt_mesh_prov_send(link, &buf)) { BT_ERR("Failed to send Provisioning Public Key"); - close_link(idx, CLOSE_REASON_FAILED); + close_link(link, CLOSE_REASON_FAILED); return; } - if (!oob) { - link[idx].expect = PROV_PUB_KEY; - } else { - /** Have already got device public key. If next is to - * send confirm(not wait for input complete), need to - * wait for transactiona ack for public key then send - * provisioning confirm pdu. - */ -#if defined(CONFIG_BLE_MESH_PB_ADV) - if (idx < CONFIG_BLE_MESH_PBA_SAME_TIME) { - link[idx].expect_ack_for = PROV_PUB_KEY; - return; - } + if (link->public_key == PROV_NO_OOB_PUB_KEY) { + link->expect = PROV_PUB_KEY; + return; + } + +#if CONFIG_BLE_MESH_PB_ADV + /* If the link is used by PB-ADV and OOB Public Key is + * used, need to wait for the Transaction ACK for Public + * Key. When ACK is received, we can start to generate + * DHKey and send Provisioning Confirm. + */ + if (!bt_mesh_atomic_test_bit(link->flags, PB_REMOTE) && + (link - prov_links < CONFIG_BLE_MESH_PBA_SAME_TIME)) { + link->expect_ack_for = PROV_PUB_KEY; + return; + } #endif /* CONFIG_BLE_MESH_PB_ADV */ - /* If remote public key has been read, then start to generate DHkey, - * otherwise wait for device oob public key. + if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) { + /* If OOB Public Key is used, try to generate DHKey, + * and we need to wait for the Remote Provisioning + * Outbound Report for Public Key, then we can send + * Remote Provisioning PDU Send with confirmation. */ - if (bt_mesh_atomic_test_bit(link[idx].flags, REMOTE_PUB_KEY)) { - prov_gen_dh_key(idx); - } else { - bt_mesh_atomic_set_bit(link[idx].flags, WAIT_GEN_DHKEY); - } + bt_mesh_atomic_set_bit(link->flags, WAIT_PK_OBR); } -} -static void prov_pub_key(const uint8_t idx, const uint8_t *data) -{ - BT_DBG("Remote Public Key: %s", bt_hex(data, 64)); - - /* 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 OOB Public Key has been read, start to generate + * DHKey; otherwise need to wait for OOB Public Key. + */ + if (bt_mesh_atomic_test_bit(link->flags, REMOTE_PUB_KEY)) { + prov_gen_dh_key(link); + } else { + bt_mesh_atomic_set_bit(link->flags, WAIT_GEN_DHKEY); } +} - memcpy(&link[idx].conf_inputs[81], data, 64); +static void prov_pub_key(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf) +{ + BT_DBG("Remote Public Key: %s", bt_hex(buf->data, 64)); - if (!bt_mesh_atomic_test_bit(link[idx].flags, LOCAL_PUB_KEY)) { - /* Clear retransmit timer */ -#if defined(CONFIG_BLE_MESH_PB_ADV) - prov_clear_tx(idx); -#endif - bt_mesh_atomic_set_bit(link[idx].flags, REMOTE_PUB_KEY); - BT_WARN("Waiting for local public key"); + /* Check public key using the following rules: + * 1. X > 0, Y > 0 + * 2. X > 0, Y = 0 + * 3. X = 0, Y = 0 + */ + if (!bt_mesh_check_public_key(buf->data)) { + BT_ERR("Invalid public key"); + close_link(link, CLOSE_REASON_FAILED); return; } - prov_gen_dh_key(idx); + memcpy(&link->conf_inputs[81], buf->data, 64); + + prov_gen_dh_key(link); } -static void prov_input_complete(const uint8_t idx, const uint8_t *data) +static void prov_input_complete(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf) { - /* 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); - } + /* TODO: + * Add a flag to make sure authentication value has been set + * when using Input OOB. + * + * Currently the authentication value is generated and set + * internally, so this should works fine at this moment. + */ /* Provisioner receives input complete and send confirm */ - send_confirm(idx); + send_confirm(link); } -static void prov_confirm(const uint8_t idx, const uint8_t *data) +static void prov_confirm(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf) { - PROV_BUF(buf, 17); + uint8_t conf_size = PROV_CONF_SIZE(link); + + PROV_BUF(prov_buf, conf_size + 1); - BT_DBG("Remote Confirm: %s", bt_hex(data, 16)); + BT_DBG("Remote Confirm: %s", bt_hex(buf->data, conf_size)); /* NOTE: The Bluetooth SIG recommends that potentially vulnerable mesh provisioners * restrict the authentication procedure and not accept provisioning random and * provisioning confirmation numbers from a remote peer that are the same as those * selected by the local device (CVE-2020-26560). */ - if (!memcmp(data, link[idx].local_conf, 16)) { + if (!memcmp(buf->data, link->local_conf, conf_size)) { BT_ERR("Confirmation value is identical to ours, rejecting."); - close_link(idx, CLOSE_REASON_FAILED); + close_link(link, CLOSE_REASON_FAILED); return; } - /* 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); - } + memcpy(link->conf, buf->data, conf_size); - link[idx].conf = (uint8_t *)bt_mesh_calloc(PROV_CONFIRM_SIZE); - if (!link[idx].conf) { - BT_ERR("%s, Out of memory", __func__); - close_link(idx, CLOSE_REASON_FAILED); - return; - } - - memcpy(link[idx].conf, data, 16); - - if (!bt_mesh_atomic_test_bit(link[idx].flags, HAVE_DHKEY)) { -#if defined(CONFIG_BLE_MESH_PB_ADV) - prov_clear_tx(idx); + if (!bt_mesh_atomic_test_bit(link->flags, HAVE_DHKEY)) { +#if CONFIG_BLE_MESH_PB_ADV + bt_mesh_prov_clear_tx(link, true); #endif - bt_mesh_atomic_set_bit(link[idx].flags, SEND_CONFIRM); + bt_mesh_atomic_set_bit(link->flags, SEND_CONFIRM); } - prov_buf_init(&buf, PROV_RANDOM); + bt_mesh_prov_buf_init(&prov_buf, PROV_RANDOM); - net_buf_simple_add_mem(&buf, link[idx].rand, 16); + net_buf_simple_add_mem(&prov_buf, link->rand, PROV_RAND_SIZE(link)); - if (prov_send(idx, &buf)) { + if (bt_mesh_prov_send(link, &prov_buf)) { BT_ERR("Failed to send Provisioning Random"); - close_link(idx, CLOSE_REASON_FAILED); + close_link(link, CLOSE_REASON_FAILED); return; } - link[idx].expect = PROV_RANDOM; + link->expect = PROV_RANDOM; } -static void send_prov_data(const uint8_t idx) +static void send_prov_data(struct bt_mesh_prov_link *link) { - PROV_BUF(buf, 34); uint16_t prev_addr = BLE_MESH_ADDR_UNASSIGNED; uint16_t max_addr = BLE_MESH_ADDR_UNASSIGNED; struct bt_mesh_node *node = NULL; @@ -2373,20 +1943,23 @@ static void send_prov_data(const uint8_t idx) uint8_t session_key[16] = {0}; uint8_t nonce[13] = {0}; uint8_t pdu[25] = {0}; + PROV_BUF(buf, 34); int err = 0; - err = bt_mesh_session_key(link[idx].dhkey, link[idx].prov_salt, session_key); + err = bt_mesh_session_key(link->dhkey, link->prov_salt, session_key); if (err) { BT_ERR("Failed to generate session key"); goto fail; } + BT_DBG("SessionKey: %s", bt_hex(session_key, 16)); - err = bt_mesh_prov_nonce(link[idx].dhkey, link[idx].prov_salt, nonce); + err = bt_mesh_prov_nonce(link->dhkey, link->prov_salt, nonce); if (err) { BT_ERR("Failed to generate session nonce"); goto fail; } + BT_DBG("Nonce: %s", bt_hex(nonce, 13)); /* Assign provisioning data for the device. Currently all provisioned devices @@ -2399,24 +1972,25 @@ static void send_prov_data(const uint8_t idx) BT_ERR("No NetKey for fast provisioning"); goto fail; } + memcpy(pdu, netkey, 16); 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]); + pdu[18] = get_net_flags(prov_ctx.fast_prov.net_idx); + sys_put_be32(bt_mesh.iv_index, &pdu[19]); } else { - netkey = bt_mesh_provisioner_net_key_get(prov_ctx.curr_net_idx); + netkey = bt_mesh_provisioner_net_key_get(prov_ctx.net_idx); if (!netkey) { BT_ERR("No NetKey for provisioning data"); goto fail; } + memcpy(pdu, netkey, 16); - sys_put_be16(prov_ctx.curr_net_idx, &pdu[16]); - pdu[18] = prov_ctx.curr_flags; - sys_put_be32(prov_ctx.curr_iv_index, &pdu[19]); + sys_put_be16(prov_ctx.net_idx, &pdu[16]); + pdu[18] = get_net_flags(prov_ctx.net_idx); + sys_put_be32(bt_mesh.iv_index, &pdu[19]); } - /** - * The Provisioner must not reuse unicast addresses that have been + /* 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 @@ -2424,38 +1998,49 @@ static void send_prov_data(const uint8_t idx) */ /* Check if this device is a re-provisioned device */ - node = bt_mesh_provisioner_get_node_with_uuid(link[idx].uuid); + node = bt_mesh_provisioner_get_node_with_uuid(link->uuid); if (node) { - if (link[idx].element_num <= node->element_num) { - /** - * If the device is provisioned before, but the element number of + if (link->element_num <= node->element_num && + link->pb_remote_nppi != NPPI_NODE_ADDR_REFRESH) { + /* If the device is provisioned before, but the element number of * the device is bigger now, then we treat it as a new device. */ prev_addr = node->unicast_addr; } - bt_mesh_provisioner_remove_node(link[idx].uuid); + + /* If the link is a normal provisioning link, or it is a PB-Remote one + * and not used by NPPI, then we could remove the device. + */ + if (!bt_mesh_atomic_test_bit(link->flags, PB_REMOTE) || + link->pb_remote_nppi == NPPI_UNKNOWN) { + bt_mesh_provisioner_remove_node(link->uuid); + } } - max_addr = FAST_PROV_ENABLE() ? prov_ctx.fast_prov.unicast_addr_max : PROV_MAX_ADDR_TO_ASSIGN; + max_addr = FAST_PROV_ENABLE() ? prov_ctx.fast_prov.unicast_addr_max : UNICAST_ADDR_LIMIT; if (BLE_MESH_ADDR_IS_UNICAST(prev_addr)) { + /* For Node Address Refresh, the previous node address + * shall not be used. + */ sys_put_be16(prev_addr, &pdu[23]); - link[idx].unicast_addr = prev_addr; + link->unicast_addr = prev_addr; } else { uint16_t alloc_addr = BLE_MESH_ADDR_UNASSIGNED; - if (BLE_MESH_ADDR_IS_UNICAST(link[idx].assign_addr)) { - alloc_addr = link[idx].assign_addr; + if (BLE_MESH_ADDR_IS_UNICAST(link->assign_addr)) { + alloc_addr = link->assign_addr; } else { /* If this device to be provisioned is a new device */ - if (prov_ctx.curr_alloc_addr == BLE_MESH_ADDR_UNASSIGNED) { + if (prov_ctx.alloc_addr == BLE_MESH_ADDR_UNASSIGNED) { BT_ERR("Not enough unicast address to be allocated"); goto fail; } - alloc_addr = prov_ctx.curr_alloc_addr; + + alloc_addr = prov_ctx.alloc_addr; } - if (alloc_addr + link[idx].element_num - 1 > max_addr) { + if (alloc_addr + link->element_num - 1 > max_addr) { BT_ERR("Not enough unicast address for the device"); goto fail; } @@ -2464,16 +2049,16 @@ static void send_prov_data(const uint8_t idx) * address of other nodes. And make sure the address is not identical with * any unicast address of Provisioner. */ - if (bt_mesh_provisioner_check_is_addr_dup(alloc_addr, link[idx].element_num, true)) { + if (bt_mesh_provisioner_check_is_addr_dup(alloc_addr, link->element_num, true)) { BT_ERR("Duplicate assigned address 0x%04x", alloc_addr); goto fail; } sys_put_be16(alloc_addr, &pdu[23]); - link[idx].unicast_addr = alloc_addr; + link->unicast_addr = alloc_addr; } - prov_buf_init(&buf, PROV_DATA); + bt_mesh_prov_buf_init(&buf, PROV_DATA); err = bt_mesh_prov_encrypt(session_key, nonce, pdu, net_buf_simple_add(&buf, 33)); if (err) { @@ -2481,327 +2066,298 @@ static void send_prov_data(const uint8_t idx) goto fail; } - if (prov_send(idx, &buf)) { + if (bt_mesh_prov_send(link, &buf)) { BT_ERR("Failed to send Provisioning Data"); goto fail; } - /** - * We update the next unicast address to be allocated here because if + /* 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 + * not update the prov_ctx.alloc_addr after the proper provisioning * complete pdu is received. */ if (!BLE_MESH_ADDR_IS_UNICAST(prev_addr)) { - if (BLE_MESH_ADDR_IS_UNICAST(link[idx].assign_addr)) { + if (BLE_MESH_ADDR_IS_UNICAST(link->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 + * application, we will also update the prov_ctx.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; + if (prov_ctx.alloc_addr < link->assign_addr + link->element_num) { + prov_ctx.alloc_addr = link->assign_addr + link->element_num; } } else { - prov_ctx.curr_alloc_addr += link[idx].element_num; - if (prov_ctx.curr_alloc_addr > max_addr) { + prov_ctx.alloc_addr += link->element_num; + if (prov_ctx.alloc_addr > max_addr) { /* No unicast address will be used for further provisioning */ - prov_ctx.curr_alloc_addr = BLE_MESH_ADDR_UNASSIGNED; + prov_ctx.alloc_addr = BLE_MESH_ADDR_UNASSIGNED; } } + /* 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); + bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.alloc_addr); } } if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && FAST_PROV_ENABLE()) { - link[idx].ki_flags = prov_ctx.fast_prov.flags; - link[idx].iv_index = prov_ctx.fast_prov.iv_index; + link->kri_flags = get_net_flags(prov_ctx.fast_prov.net_idx); } else { - link[idx].ki_flags = prov_ctx.curr_flags; - link[idx].iv_index = prov_ctx.curr_iv_index; + link->kri_flags = get_net_flags(prov_ctx.net_idx); } - link[idx].expect = PROV_COMPLETE; + link->expect = PROV_COMPLETE; return; fail: - close_link(idx, CLOSE_REASON_FAILED); - return; + close_link(link, CLOSE_REASON_FAILED); } -static void prov_random(const uint8_t idx, const uint8_t *data) +static void prov_random(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf) { - uint8_t conf_verify[16] = {0}; + uint8_t conf_verify[32] = {0}; + uint8_t rand_size = 0; + uint8_t *data = buf->data; - BT_DBG("Remote Random: %s", bt_hex(data, 16)); + rand_size = PROV_RAND_SIZE(link); + + BT_DBG("Remote Random: %s", bt_hex(data, rand_size)); /* NOTE: The Bluetooth SIG recommends that potentially vulnerable mesh provisioners * restrict the authentication procedure and not accept provisioning random and * provisioning confirmation numbers from a remote peer that are the same as those * selected by the local device (CVE-2020-26560). */ - if (!memcmp(data, link[idx].rand, 16)) { + if (!memcmp(data, link->rand, rand_size)) { BT_ERR("Random value is identical to ours, rejecting."); goto fail; } - if (bt_mesh_prov_conf(link[idx].conf_key, data, link[idx].auth, conf_verify)) { - BT_ERR("Failed to calculate confirmation verification"); - goto fail; + if (link->algorithm == PROV_ALG_P256_CMAC_AES128) { + if (bt_mesh_prov_conf(link->conf_key, data, link->auth, conf_verify)) { + BT_ERR("Failed to calculate confirmation verification"); + goto fail; + } + } +#if CONFIG_BLE_MESH_PROV_EPA + else { + if (bt_mesh_prov_conf_epa(link->conf_key, data, conf_verify)) { + BT_ERR("Failed to calculate confirmation verification"); + goto fail; + } } +#endif - if (memcmp(conf_verify, link[idx].conf, 16)) { + if (memcmp(conf_verify, link->conf, PROV_CONF_SIZE(link))) { BT_ERR("Invalid confirmation value"); - BT_DBG("Received: %s", bt_hex(link[idx].conf, 16)); - BT_DBG("Calculated: %s", bt_hex(conf_verify, 16)); + BT_ERR("Received: %s", bt_hex(link->conf, PROV_CONF_SIZE(link))); + BT_ERR("Calculated: %s", bt_hex(conf_verify, PROV_CONF_SIZE(link))); goto fail; } - /*Verify received confirm 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); - } - - /** After provisioner receives provisioning random from device, - * and successfully check the confirmation, the following - * should be done: - * 1. bt_mesh_calloc memory for prov_salt - * 2. calculate prov_salt - * 3. prepare provisioning data and send + /* After Provisioner received Provisioning Random PDU from + * the device, and successfully checked the confirmation, + * the following needs to be done: + * - Calculate prov_salt + * - Prepare and send Provisioning Data PDU */ - link[idx].prov_salt = (uint8_t *)bt_mesh_calloc(PROV_PROV_SALT_SIZE); - if (!link[idx].prov_salt) { - BT_ERR("%s, Out of memory", __func__); - goto fail; + if (link->algorithm == PROV_ALG_P256_CMAC_AES128) { + if (bt_mesh_prov_salt(link->conf_salt, link->rand, data, + link->prov_salt)) { + BT_ERR("Failed to generate ProvisioningSalt"); + goto fail; + } } - - if (bt_mesh_prov_salt(link[idx].conf_salt, link[idx].rand, data, - link[idx].prov_salt)) { - BT_ERR("Failed to generate ProvisioningSalt"); - goto fail; +#if CONFIG_BLE_MESH_PROV_EPA + else { + if (bt_mesh_prov_salt_epa(link->conf_salt, link->rand, data, + link->prov_salt)) { + BT_ERR("Failed to generate ProvisioningSalt"); + goto fail; + } } +#endif - BT_DBG("ProvisioningSalt: %s", bt_hex(link[idx].prov_salt, 16)); + BT_DBG("ProvisioningSalt: %s", bt_hex(link->prov_salt, PROV_CONF_SALT_SIZE(link))); - send_prov_data(idx); + send_prov_data(link); return; fail: - close_link(idx, CLOSE_REASON_FAILED); - return; + close_link(link, CLOSE_REASON_FAILED); } -static void prov_complete(const uint8_t idx, const uint8_t *data) +static void prov_complete(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf) { uint8_t device_key[16] = {0}; uint16_t net_idx = 0U; uint16_t index = 0U; + bool nppi = false; int err = 0; int i; - /* 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); - } - - err = bt_mesh_dev_key(link[idx].dhkey, link[idx].prov_salt, device_key); + err = bt_mesh_dev_key(link->dhkey, link->prov_salt, device_key); if (err) { BT_ERR("Failed to generate device key"); - close_link(idx, CLOSE_REASON_FAILED); + close_link(link, CLOSE_REASON_FAILED); return; } if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && FAST_PROV_ENABLE()) { net_idx = prov_ctx.fast_prov.net_idx; } else { - net_idx = prov_ctx.curr_net_idx; + net_idx = prov_ctx.net_idx; + } + + if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE) && + link->pb_remote_nppi != NPPI_UNKNOWN) { + nppi = true; } - 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); + + err = bt_mesh_provisioner_provision(&link->addr, link->uuid, link->oob_info, + link->unicast_addr, link->element_num, + net_idx, link->kri_flags, bt_mesh.iv_index, + device_key, &index, nppi); if (err) { BT_ERR("Failed to store node info"); - close_link(idx, CLOSE_REASON_FAILED); + close_link(link, CLOSE_REASON_FAILED); + return; + } + +#if CONFIG_BLE_MESH_RPR_CLI + if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) { + /* TODO: + * If NPPI info is activated after RP Link Close with + * a success reason is received, then this could be + * removed. + */ + if (link->pb_remote_nppi == NPPI_NODE_ADDR_REFRESH) { + bt_mesh_rpr_cli_node_addr_update(link); + } + + /* Notify to the application layer that provisioning + * is completed successfully. + */ + bt_mesh_rpr_cli_prov_complete(link, index, net_idx); + + close_link(link, CLOSE_REASON_SUCCESS); return; } +#endif /* CONFIG_BLE_MESH_RPR_CLI */ - if (prov->prov_complete) { - prov->prov_complete(index, link[idx].uuid, link[idx].unicast_addr, - link[idx].element_num, net_idx); + if (bt_mesh_prov_get()->prov_complete) { + bt_mesh_prov_get()->prov_complete(index, link->uuid, link->unicast_addr, + link->element_num, net_idx); } /* Find if the device is in the device queue */ for (i = 0; i < ARRAY_SIZE(unprov_dev); i++) { - if (!memcmp(unprov_dev[i].uuid, link[idx].uuid, 16) && + if (!memcmp(unprov_dev[i].uuid, link->uuid, 16) && (unprov_dev[i].flags & RM_AFTER_PROV)) { memset(&unprov_dev[i], 0, sizeof(struct unprov_dev_queue)); break; } } - close_link(idx, CLOSE_REASON_SUCCESS); + close_link(link, CLOSE_REASON_SUCCESS); } -static void prov_failed(const uint8_t idx, const uint8_t *data) +static void prov_failed(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf) { - BT_WARN("Error 0x%02x", data[0]); + BT_WARN("Error 0x%02x", buf->data[0]); - close_link(idx, CLOSE_REASON_FAILED); + close_link(link, CLOSE_REASON_FAILED); } static const struct { - void (*func)(const uint8_t idx, const uint8_t *data); - uint16_t len; + void (*func)(struct bt_mesh_prov_link *link, struct net_buf_simple *buf); } prov_handlers[] = { - { prov_invite, 1 }, - { prov_capabilities, 11 }, - { prov_start, 5 }, - { prov_pub_key, 64 }, - { prov_input_complete, 0 }, - { prov_confirm, 16 }, - { prov_random, 16 }, - { prov_data, 33 }, - { prov_complete, 0 }, - { prov_failed, 1 }, + { prov_invite, }, + { prov_capabilities, }, + { prov_start, }, + { prov_pub_key, }, + { prov_input_complete, }, + { prov_confirm, }, + { prov_random, }, + { prov_data, }, + { prov_complete, }, + { prov_failed, }, +#if CONFIG_BLE_MESH_CERT_BASED_PROV + { bt_mesh_pvnr_record_req, }, + { bt_mesh_pvnr_record_rsp, }, + { bt_mesh_pvnr_records_get, }, + { bt_mesh_pvnr_records_list, }, +#endif }; -static void close_link(const uint8_t idx, uint8_t reason) +static void close_link(struct bt_mesh_prov_link *link, uint8_t reason) { -#if defined(CONFIG_BLE_MESH_PB_ADV) - if (idx < CONFIG_BLE_MESH_PBA_SAME_TIME) { - bearer_ctl_send(idx, LINK_CLOSE, &reason, sizeof(reason)); - return; - } -#endif - -#if defined(CONFIG_BLE_MESH_PB_GATT) - if (idx < BLE_MESH_PROV_SAME_TIME -#if defined(CONFIG_BLE_MESH_PB_ADV) - && idx >= CONFIG_BLE_MESH_PBA_SAME_TIME -#endif - ) { - if (link[idx].conn) { - bt_mesh_gattc_disconnect(link[idx].conn); + if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) { + if (link->pb_remote_close) { + link->pb_remote_close(link, reason); } return; } -#endif - - BT_ERR("Invalid link idx %d", idx); - return; -} - -static void prov_timeout(struct k_work *work) -{ - uint8_t idx = (uint8_t)work->index; - - BT_WARN("%s", __func__); - - close_link(idx, CLOSE_REASON_TIMEOUT); -} - -#if defined(CONFIG_BLE_MESH_PB_ADV) -static void prov_retransmit(struct k_work *work) -{ - int64_t timeout = TRANSACTION_TIMEOUT; - uint8_t idx = (uint8_t)work->index; - int i; - - BT_DBG("%s", __func__); - - if (!bt_mesh_atomic_test_bit(link[idx].flags, LINK_ACTIVE)) { - BT_WARN("Link is not active"); - return; - } -#if defined(CONFIG_BLE_MESH_FAST_PROV) - if (link[idx].tx_pdu_type >= PROV_DATA) { - timeout = K_SECONDS(30); - } -#endif - if (k_uptime_get() - link[idx].tx.start > timeout) { - BT_WARN("Provisioner timeout, giving up transaction"); - /* Provisioner should send Link Close here */ - close_link(idx, CLOSE_REASON_TIMEOUT); +#if CONFIG_BLE_MESH_PB_GATT + if (link->conn) { + bt_mesh_gattc_disconnect(link->conn); return; } +#endif /* CONFIG_BLE_MESH_PB_GATT */ - if (link[idx].send_link_close & BIT(0)) { - uint8_t reason = (link[idx].send_link_close >> 1) & BIT_MASK(2); - uint16_t count = (link[idx].send_link_close >> 3); - if (count >= 2) { - reset_link(idx, reason); - return; - } - link[idx].send_link_close += BIT(3); - } - - 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) { - break; - } - - if (BLE_MESH_ADV(buf)->busy) { - continue; - } - - BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); - - if (i + 1 < ARRAY_SIZE(link[idx].tx.buf) && link[idx].tx.buf[i + 1]) { - bt_mesh_adv_send(buf, NULL, NULL); - } else { - bt_mesh_adv_send(buf, &buf_sent_cb, (void *)(int)idx); - } - } - - bt_mesh_pb_buf_unlock(); +#if CONFIG_BLE_MESH_PB_ADV + bt_mesh_prov_bearer_ctl_send(link, LINK_CLOSE, &reason, sizeof(reason)); +#endif /* CONFIG_BLE_MESH_PB_ADV */ } -static void link_ack(const uint8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +#if CONFIG_BLE_MESH_PB_ADV +static void link_ack(struct bt_mesh_prov_link *link, struct prov_rx *rx, struct net_buf_simple *buf) { BT_DBG("len %u", buf->len); if (buf->len) { BT_ERR("Invalid Link ACK length %d", buf->len); - close_link(idx, CLOSE_REASON_FAILED); + close_link(link, CLOSE_REASON_FAILED); return; } - if (link[idx].expect == PROV_CAPABILITIES) { + if (link->expect == PROV_CAPABILITIES || + link->expect == PROV_REC_LIST || link->expect == PROV_REC_RSP) { BT_INFO("Link ACK is already received"); return; } - link[idx].conf_inputs = (uint8_t *)bt_mesh_calloc(PROV_CONF_INPUTS_SIZE); - if (!link[idx].conf_inputs) { - BT_ERR("%s, Out of memory", __func__); - close_link(idx, CLOSE_REASON_FAILED); - return; +#if CONFIG_BLE_MESH_CERT_BASED_PROV + if (PROV_REC_SUPPORT(link->oob_info)) { + if (bt_mesh_prov_get()->cert_based_prov_start) { + bt_mesh_prov_get()->cert_based_prov_start(link - prov_links); + } + } else +#endif + { + send_invite(link); } - - send_invite(idx); } -static void link_close(const uint8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +static void link_close(struct bt_mesh_prov_link *link, struct prov_rx *rx, struct net_buf_simple *buf) { - uint8_t reason = 0U; - BT_DBG("len %u", buf->len); - reason = net_buf_simple_pull_u8(buf); + if (buf->len != 1) { + BT_ERR("Invalid Link Close length %d", buf->len); + return; + } - reset_link(idx, reason); + reset_adv_link(link, net_buf_simple_pull_u8(buf)); } -static void gen_prov_ctl(const uint8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +static void gen_prov_ctl(struct bt_mesh_prov_link *link, struct prov_rx *rx, struct net_buf_simple *buf) { BT_DBG("op 0x%02x len %u", BEARER_CTL(rx->gpc), buf->len); @@ -2810,17 +2366,19 @@ static void gen_prov_ctl(const uint8_t idx, struct prov_rx *rx, struct net_buf_s break; case LINK_ACK: - if (!bt_mesh_atomic_test_bit(link[idx].flags, LINK_ACTIVE)) { + if (!bt_mesh_atomic_test_bit(link->flags, LINK_ACTIVE)) { return; } - link_ack(idx, rx, buf); + + link_ack(link, rx, buf); break; case LINK_CLOSE: - if (!bt_mesh_atomic_test_bit(link[idx].flags, LINK_ACTIVE)) { + if (!bt_mesh_atomic_test_bit(link->flags, LINK_ACTIVE)) { return; } - link_close(idx, rx, buf); + + link_close(link, rx, buf); break; default: @@ -2829,41 +2387,54 @@ static void gen_prov_ctl(const uint8_t idx, struct prov_rx *rx, struct net_buf_s } } -static void prov_msg_recv(const uint8_t idx) +static void prov_msg_recv(struct bt_mesh_prov_link *link) { - uint8_t type = link[idx].rx.buf->data[0]; + uint8_t type = 0; - BT_DBG("type 0x%02x len %u", type, link[idx].rx.buf->len); - - /** - * Provisioner first checks information within the received - * Provisioning PDU. If the check succeeds then check fcs. + /* When Link Close is being sent, any received Provisioning PDU + * should be ignored. */ - if (type != PROV_FAILED && type != link[idx].expect) { - BT_ERR("Unexpected msg 0x%02x != 0x%02x", type, link[idx].expect); - goto fail; + if (bt_mesh_atomic_test_bit(link->flags, LINK_CLOSING)) { + BT_WARN("Link is closing, unexpected msg 0x%02x", type); + return; + } + + if (!bt_mesh_fcs_check(link->rx.buf, link->rx.fcs)) { + BT_ERR("Incorrect FCS"); + return; } - if (type >= 0x0A) { + type = net_buf_simple_pull_u8(link->rx.buf); + + BT_DBG("type 0x%02x len %u", type, link->rx.buf->len); + + if (type >= ARRAY_SIZE(prov_handlers)) { BT_ERR("Unknown provisioning PDU type 0x%02x", type); goto fail; } - if (1 + prov_handlers[type].len != link[idx].rx.buf->len) { - BT_ERR("Invalid length %u for type 0x%02x", link[idx].rx.buf->len, type); + bt_mesh_gen_prov_ack_send(link, link->rx.id); + link->rx.prev_id = link->rx.id; + link->rx.id = 0; + + /* Provisioner first checks information within the received + * Provisioning PDU. If the check succeeds then check fcs. + */ + if (type != PROV_FAILED && type != link->expect) { + BT_ERR("Unexpected msg 0x%02x != 0x%02x", type, link->expect); goto fail; } - if (!bt_mesh_fcs_check(link[idx].rx.buf, link[idx].rx.fcs)) { - BT_ERR("Incorrect FCS"); + if (!bt_mesh_prov_pdu_check(type, link->rx.buf->len, NULL)) { goto fail; } - gen_prov_ack_send(idx, link[idx].rx.trans_id); - link[idx].rx.prev_id = link[idx].rx.trans_id; - link[idx].rx.trans_id = 0; + k_delayed_work_submit(&link->prot_timer, PROTOCOL_TIMEOUT); + + prov_handlers[type].func(link, link->rx.buf); + + net_buf_simple_reset(link->rx.buf); - prov_handlers[type].func(idx, &link[idx].rx.buf->data[1]); return; fail: @@ -2871,85 +2442,49 @@ static void prov_msg_recv(const uint8_t idx) * For the case MESH/PVNR/PROV/BV-10-C and MESH/PVNR/PROV/BI-14-C, * provisioner should send transaction ack before closing the link. */ - gen_prov_ack_send(idx, link[idx].rx.trans_id); + bt_mesh_gen_prov_ack_send(link, link->rx.id); - close_link(idx, CLOSE_REASON_FAILED); - return; + close_link(link, CLOSE_REASON_FAILED); } -static void gen_prov_cont(const uint8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +static void gen_prov_cont(struct bt_mesh_prov_link *link, + struct prov_rx *rx, + struct net_buf_simple *buf) { - uint8_t seg = CONT_SEG_INDEX(rx->gpc); - - BT_DBG("len %u, seg_index %u", buf->len, seg); - - if (!link[idx].rx.seg && link[idx].rx.prev_id == rx->xact_id) { - BT_INFO("Resending ack"); - gen_prov_ack_send(idx, rx->xact_id); - return; - } - - if (rx->xact_id != link[idx].rx.trans_id) { - BT_WARN("Data for unknown transaction (%u != %u)", - rx->xact_id, link[idx].rx.trans_id); - return; - } + bool close = false; - if (seg > link[idx].rx.last_seg) { - BT_ERR("Invalid segment index %u", seg); - goto fail; - } else if (seg == link[idx].rx.last_seg) { - uint8_t expect_len = 0U; - - expect_len = (link[idx].rx.buf->len - 20 - - (23 * (link[idx].rx.last_seg - 1))); - if (expect_len != buf->len) { - BT_ERR("Incorrect last seg len: %u != %u", - expect_len, buf->len); - goto fail; + if (!bt_mesh_gen_prov_cont(link, buf, rx, &close)) { + if (close) { + close_link(link, CLOSE_REASON_FAILED); } - } - - if (!(link[idx].rx.seg & BIT(seg))) { - BT_INFO("Ignore already received segment"); return; } - memcpy(XACT_SEG_DATA(idx, seg), buf->data, buf->len); - XACT_SEG_RECV(idx, seg); - - if (!link[idx].rx.seg) { - prov_msg_recv(idx); - } - return; - -fail: - close_link(idx, CLOSE_REASON_FAILED); - return; + prov_msg_recv(link); } -static void gen_prov_ack(const uint8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +static void gen_prov_ack(struct bt_mesh_prov_link *link, + struct prov_rx *rx, + struct net_buf_simple *buf) { - uint8_t ack_type = 0U, pub_key_oob = 0U; - BT_DBG("len %u", buf->len); - if (!link[idx].tx.buf[0]) { + if (!link->tx.buf[0]) { return; } - if (!link[idx].tx.trans_id) { + if (!link->tx.id) { return; } - if (rx->xact_id == (link[idx].tx.trans_id - 1)) { - prov_clear_tx(idx); + if (rx->xact_id == (link->tx.id - 1)) { + bt_mesh_prov_clear_tx(link, true); - ack_type = link[idx].expect_ack_for; - switch (ack_type) { + switch (link->expect_ack_for) { case PROV_START: - pub_key_oob = link[idx].conf_inputs[13]; - send_pub_key(idx, pub_key_oob); + send_pub_key(link); + + uint8_t pub_key_oob = link->conf_inputs[13]; /* For case MESH/PVNR/PROV/BV-04-C, if using OOB public key, * the value of expect_ack_for shall be PROV_PUB_KEY. */ @@ -2958,163 +2493,114 @@ static void gen_prov_ack(const uint8_t idx, struct prov_rx *rx, struct net_buf_s } break; case PROV_PUB_KEY: - prov_gen_dh_key(idx); + prov_gen_dh_key(link); break; default: break; } - link[idx].expect_ack_for = 0x00; + + link->expect_ack_for = 0x00; } } -static void gen_prov_start(const uint8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +static void gen_prov_start(struct bt_mesh_prov_link *link, + struct prov_rx *rx, + struct net_buf_simple *buf) { - if (link[idx].rx.seg) { - BT_INFO("Get Start while there are unreceived segments"); - return; - } - - if (link[idx].rx.prev_id == rx->xact_id) { - BT_INFO("Resending ack"); - gen_prov_ack_send(idx, rx->xact_id); - return; - } - - link[idx].rx.buf->len = net_buf_simple_pull_be16(buf); - link[idx].rx.trans_id = rx->xact_id; - link[idx].rx.fcs = net_buf_simple_pull_u8(buf); - - BT_DBG("len %u last_seg %u total_len %u fcs 0x%02x", buf->len, - START_LAST_SEG(rx->gpc), link[idx].rx.buf->len, link[idx].rx.fcs); - - /* Provisioner can not receive zero-length provisioning pdu */ - if (link[idx].rx.buf->len < 1) { - BT_ERR("Ignoring zero-length provisioning PDU"); - close_link(idx, CLOSE_REASON_FAILED); - return; - } - - if (START_LAST_SEG(rx->gpc) > START_LAST_SEG_MAX) { - BT_ERR("Invalid SegN 0x%02x", START_LAST_SEG(rx->gpc)); - close_link(idx, CLOSE_REASON_FAILED); - return; - } - - if (link[idx].rx.buf->len > link[idx].rx.buf->size) { - BT_ERR("Too large provisioning PDU (%u bytes)", - link[idx].rx.buf->len); - close_link(idx, CLOSE_REASON_FAILED); - return; - } + bool close = false; - if (START_LAST_SEG(rx->gpc) > 0 && link[idx].rx.buf->len <= 20) { - BT_ERR("Too small total length for multi-segment PDU"); - close_link(idx, CLOSE_REASON_FAILED); + if (!bt_mesh_gen_prov_start(link, buf, rx, &close)) { + if (close) { + close_link(link, CLOSE_REASON_FAILED); + } return; } - link[idx].rx.seg = (1 << (START_LAST_SEG(rx->gpc) + 1)) - 1; - link[idx].rx.last_seg = START_LAST_SEG(rx->gpc); - memcpy(link[idx].rx.buf->data, buf->data, buf->len); - XACT_SEG_RECV(idx, 0); - - if (!link[idx].rx.seg) { - prov_msg_recv(idx); - } + prov_msg_recv(link); } static const struct { - void (*const func)(const uint8_t idx, struct prov_rx *rx, struct net_buf_simple *buf); + void (*const func)(struct bt_mesh_prov_link *link, + struct prov_rx *rx, + struct net_buf_simple *buf); const uint8_t require_link; const uint8_t min_len; } gen_prov[] = { - { gen_prov_start, true, 3 }, - { gen_prov_ack, true, 0 }, - { gen_prov_cont, true, 0 }, - { gen_prov_ctl, true, 0 }, + { gen_prov_start, true, 3 }, + { gen_prov_ack, true, 0 }, + { gen_prov_cont, true, 0 }, + { gen_prov_ctl, true, 0 }, }; -static void gen_prov_recv(const uint8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +static void gen_prov_recv(struct bt_mesh_prov_link *link, + struct prov_rx *rx, + struct net_buf_simple *buf) { if (buf->len < gen_prov[GPCF(rx->gpc)].min_len) { BT_ERR("Too short GPC message type %u", GPCF(rx->gpc)); - close_link(idx, CLOSE_REASON_FAILED); return; } - /** - * require_link can be used combining with link[].linking flag to - * set LINK_ACTIVE status after Link ACK is received. In this case - * there is no need to check LINK_ACTIVE status in find_link(). - */ - if (!bt_mesh_atomic_test_bit(link[idx].flags, LINK_ACTIVE) && - gen_prov[GPCF(rx->gpc)].require_link) { + if (!bt_mesh_atomic_test_bit(link->flags, LINK_ACTIVE) && + gen_prov[GPCF(rx->gpc)].require_link) { BT_DBG("Ignoring message that requires active link"); return; } - gen_prov[GPCF(rx->gpc)].func(idx, rx, buf); + gen_prov[GPCF(rx->gpc)].func(link, rx, buf); } -static int find_link(uint32_t link_id, uint8_t *idx) +static struct bt_mesh_prov_link *find_pba_link(uint32_t link_id) { int i; - /* link for PB-ADV is from 0 to CONFIG_BLE_MESH_PBA_SAME_TIME */ + /* PB-ADV link is from 0 to CONFIG_BLE_MESH_PBA_SAME_TIME */ 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) { - *idx = i; - } - return 0; - } + if (bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE) && + prov_links[i].link_id == link_id) { + return &prov_links[i]; } } - return -1; + return NULL; } void bt_mesh_provisioner_pb_adv_recv(struct net_buf_simple *buf) { + struct bt_mesh_prov_link *link = NULL; struct prov_rx rx = {0}; - uint8_t idx = 0U; - - rx.link_id = net_buf_simple_pull_be32(buf); - if (find_link(rx.link_id, &idx) < 0) { - BT_DBG("Data for unexpected link"); - return; - } - if (buf->len < 2) { + if (buf->len < 6) { BT_ERR("Too short provisioning packet (len %u)", buf->len); - close_link(idx, CLOSE_REASON_FAILED); return; } + rx.link_id = net_buf_simple_pull_be32(buf); rx.xact_id = net_buf_simple_pull_u8(buf); rx.gpc = net_buf_simple_pull_u8(buf); BT_DBG("link_id 0x%08x xact_id %u", rx.link_id, rx.xact_id); - gen_prov_recv(idx, &rx, buf); + link = find_pba_link(rx.link_id); + if (link == NULL) { + BT_DBG("Ignoring mesh beacon for unknown link"); + return; + } + + gen_prov_recv(link, &rx, buf); } #endif /* CONFIG_BLE_MESH_PB_ADV */ -#if defined(CONFIG_BLE_MESH_PB_GATT) -static struct bt_mesh_conn *find_conn(struct bt_mesh_conn *conn, uint8_t *idx) +#if CONFIG_BLE_MESH_PB_GATT +static struct bt_mesh_prov_link *find_pbg_link(struct bt_mesh_conn *conn) { int i; - /* link for PB-GATT is from CONFIG_BLE_MESH_PBA_SAME_TIME to BLE_MESH_PROV_SAME_TIME */ + /* PB-GATT link 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++) { - if (bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { - if (link[i].conn == conn) { - if (idx) { - *idx = i; - } - return conn; - } + if (bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE) && + prov_links[i].conn == conn) { + return &prov_links[i]; } } @@ -3123,161 +2609,150 @@ static struct bt_mesh_conn *find_conn(struct bt_mesh_conn *conn, uint8_t *idx) int bt_mesh_provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf) { + struct bt_mesh_prov_link *link = NULL; uint8_t type = 0U; - uint8_t idx = 0U; BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); - if (!find_conn(conn, &idx)) { - BT_ERR("Data for unexpected connection"); + link = find_pbg_link(conn); + if (link == NULL) { + BT_ERR("Link not found, conn %p", conn); return -ENOTCONN; } if (buf->len < 1) { BT_ERR("Too short provisioning packet (len %u)", buf->len); - goto fail; + return -EINVAL; } type = net_buf_simple_pull_u8(buf); - if (type != PROV_FAILED && type != link[idx].expect) { - BT_ERR("Unexpected msg 0x%02x != 0x%02x", type, link[idx].expect); - goto fail; - } - if (type >= 0x0A) { + if (type >= ARRAY_SIZE(prov_handlers)) { BT_ERR("Unknown provisioning PDU type 0x%02x", type); - goto fail; + close_link(link, CLOSE_REASON_FAILED); + return -EINVAL; } - if (prov_handlers[type].len != buf->len) { - BT_ERR("Invalid length %u for type 0x%02x", buf->len, type); - goto fail; + if (type != PROV_FAILED && type != link->expect) { + BT_ERR("Unexpected msg 0x%02x != 0x%02x", type, link->expect); + close_link(link, CLOSE_REASON_FAILED); + return -EINVAL; } - prov_handlers[type].func(idx, buf->data); + if (!bt_mesh_prov_pdu_check(type, buf->len, NULL)) { + close_link(link, CLOSE_REASON_FAILED); + return -EINVAL; + } - return 0; + k_delayed_work_submit(&link->prot_timer, PROTOCOL_TIMEOUT); -fail: - /* Mesh Spec Section 5.4.4 Provisioning errors */ - close_link(idx, CLOSE_REASON_FAILED); - return -EINVAL; + prov_handlers[type].func(link, buf); + + return 0; } -int bt_mesh_provisioner_set_prov_conn(const uint8_t addr[6], struct bt_mesh_conn *conn) +int bt_mesh_provisioner_pb_gatt_open(struct bt_mesh_conn *conn, const uint8_t addr[6]) { + struct bt_mesh_prov_link *link = NULL; int i; - if (!addr || !conn) { + if (conn == NULL || addr == NULL) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) { - if (!memcmp(link[i].addr.val, addr, BLE_MESH_ADDR_LEN)) { - link[i].conn = bt_mesh_conn_ref(conn); - return 0; - } - } - - BT_ERR("Addr %s not found", bt_hex(addr, BLE_MESH_ADDR_LEN)); - return -ENOMEM; -} - -int bt_mesh_provisioner_pb_gatt_open(struct bt_mesh_conn *conn, uint8_t *addr) -{ - uint8_t idx = 0U; - int i; - - BT_DBG("conn %p", conn); - - /** - * Double check if the device is currently being provisioned using PB-ADV. - * Provisioner binds conn with proper device when proxy_prov_connected() - * is invoked, and here after proper GATT procedures are completed, we just - * check if this conn already exists in the proxy servers array. - */ - for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) { - if (link[i].conn == conn) { - idx = i; + if (!memcmp(prov_links[i].addr.val, addr, BLE_MESH_ADDR_LEN)) { + prov_links[i].conn = bt_mesh_conn_ref(conn); + link = &prov_links[i]; break; } } - if (i == BLE_MESH_PROV_SAME_TIME) { - BT_ERR("Link not found"); - return -ENOTCONN; + if (link == NULL) { + BT_ERR("Device address %s not found", bt_hex(addr, BLE_MESH_ADDR_LEN)); + return -ENODEV; } -#if defined(CONFIG_BLE_MESH_PB_ADV) + /* Double check if the device is currently being provisioned using PB-ADV. */ +#if CONFIG_BLE_MESH_PB_ADV 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)) { + if (bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE)) { + if (!memcmp(prov_links[i].uuid, link->uuid, 16)) { BT_WARN("Provision using PB-GATT & PB-ADV same time"); - close_link(idx, CLOSE_REASON_FAILED); + close_link(link, CLOSE_REASON_FAILED); return -EALREADY; } } } -#endif +#endif /* CONFIG_BLE_MESH_PB_ADV */ - bt_mesh_atomic_set_bit(link[idx].flags, LINK_ACTIVE); - link[idx].conn = bt_mesh_conn_ref(conn); + bt_mesh_atomic_set_bit(link->flags, LINK_ACTIVE); /* May use lcd to indicate starting provisioning each device */ - if (prov->prov_link_open) { - prov->prov_link_open(BLE_MESH_PROV_GATT); + if (bt_mesh_prov_get()->prov_link_open) { + bt_mesh_prov_get()->prov_link_open(BLE_MESH_PROV_GATT); } - link[idx].conf_inputs = (uint8_t *)bt_mesh_calloc(PROV_CONF_INPUTS_SIZE); - if (!link[idx].conf_inputs) { - /* Disconnect this connection, clear corresponding informations */ - BT_ERR("%s, Out of memory", __func__); - close_link(idx, CLOSE_REASON_FAILED); - return -ENOMEM; + /* TODO: memory optimization - calloc */ + +#if CONFIG_BLE_MESH_CERT_BASED_PROV + if (PROV_REC_SUPPORT(link->oob_info)) { + if (bt_mesh_prov_get()->cert_based_prov_start) { + bt_mesh_prov_get()->cert_based_prov_start(link - prov_links); + } + } else +#endif + { + send_invite(link); } - send_invite(idx); return 0; } int bt_mesh_provisioner_pb_gatt_close(struct bt_mesh_conn *conn, uint8_t reason) { - uint8_t idx = 0U; + struct bt_mesh_prov_link *link = NULL; BT_DBG("conn %p", conn); - if (!find_conn(conn, &idx)) { - BT_ERR("Conn %p not found", conn); + link = find_pbg_link(conn); + if (link == NULL) { + BT_ERR("Not connected, conn %p", conn); return -ENOTCONN; } - if (bt_mesh_atomic_test_and_clear_bit(link[idx].flags, TIMEOUT_START)) { - k_delayed_work_cancel(&link[idx].timeout); - } + k_delayed_work_cancel(&link->prot_timer); - if (prov->prov_link_close) { - prov->prov_link_close(BLE_MESH_PROV_GATT, reason); + if (bt_mesh_prov_get()->prov_link_close) { + bt_mesh_prov_get()->prov_link_close(BLE_MESH_PROV_GATT, reason); } - prov_memory_free(idx); + prov_memory_free(link); - memset(&link[idx], 0, offsetof(struct prov_link, timeout)); + memset(link, 0, offsetof(struct bt_mesh_prov_link, prot_timer)); - if (bt_mesh_pub_key_get()) { - bt_mesh_atomic_set_bit(link[idx].flags, LOCAL_PUB_KEY); - } + link->pb_gatt_send = prov_send_gatt; return 0; } #endif /* CONFIG_BLE_MESH_PB_GATT */ -int bt_mesh_provisioner_prov_init(const struct bt_mesh_prov *prov_info) +static void protocol_timeout(struct k_work *work) +{ + struct bt_mesh_prov_link *link = work->user_data; + + BT_WARN("Protocol timeout"); + + close_link(link, CLOSE_REASON_TIMEOUT); +} + +int bt_mesh_provisioner_prov_init(void) { const uint8_t *key = NULL; int i; - if (!prov_info) { + if (bt_mesh_prov_get() == NULL) { BT_ERR("No provisioning context provided"); return -EINVAL; } @@ -3288,40 +2763,56 @@ int bt_mesh_provisioner_prov_init(const struct bt_mesh_prov *prov_info) return -EIO; } - 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 (bt_mesh_prov_get()->prov_static_oob_val && + bt_mesh_prov_get()->prov_static_oob_len) { + prov_ctx.static_oob_len = MIN(BLE_MESH_PROV_STATIC_OOB_MAX_LEN, bt_mesh_prov_get()->prov_static_oob_len); + memcpy(prov_ctx.static_oob_val, + bt_mesh_prov_get()->prov_static_oob_val, + prov_ctx.static_oob_len); } -#if defined(CONFIG_BLE_MESH_PB_ADV) +#if CONFIG_BLE_MESH_PB_ADV 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); + rx_buf[i].buf.size = PROV_RX_BUF_SIZE; + rx_buf[i].buf.__buf = rx_buf_data + (i * PROV_RX_BUF_SIZE); + + prov_links[i].pending_ack = PROV_XACT_NVAL; + + bt_mesh_prov_retransmit_init(&prov_links[i]); + + prov_links[i].rx.prev_id = PROV_XACT_NVAL; + prov_links[i].rx.buf = get_rx_buf(i); + + prov_links[i].next_xact_id = pvnr_next_xact_id; + prov_links[i].reset_adv_link = reset_adv_link; + prov_links[i].retrans_timeout = close_link; + +#if CONFIG_BLE_MESH_FAST_PROV + prov_links[i].last_tx_pdu = PROV_DATA; +#endif /* CONFIG_BLE_MESH_FAST_PROV */ - link[i].pending_ack = XACT_NVAL; - k_delayed_work_init(&link[i].tx.retransmit, prov_retransmit); - link[i].tx.retransmit.work.index = (int)i; - link[i].rx.prev_id = XACT_NVAL; - link[i].rx.buf = bt_mesh_pba_get_buf(i); + bt_mesh_mutex_create(&prov_links[i].buf_lock); } -#endif +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +#if CONFIG_BLE_MESH_PB_GATT + for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) { + prov_links[i].pb_gatt_send = prov_send_gatt; + } +#endif /* CONFIG_BLE_MESH_PB_GATT */ 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; + k_delayed_work_init(&prov_links[i].prot_timer, protocol_timeout); + prov_links[i].prot_timer.work.user_data = &prov_links[i]; } -#if defined(CONFIG_BLE_MESH_PB_ADV) - bt_mesh_pb_adv_mutex_new(); - bt_mesh_pb_buf_mutex_new(); +#if CONFIG_BLE_MESH_PB_ADV + bt_mesh_mutex_create(&prov_ctx.pb_adv_lock); #endif -#if defined(CONFIG_BLE_MESH_PB_GATT) - bt_mesh_pb_gatt_mutex_new(); +#if CONFIG_BLE_MESH_PB_GATT + bt_mesh_mutex_create(&prov_ctx.pb_gatt_lock); #endif return 0; @@ -3331,35 +2822,41 @@ int bt_mesh_provisioner_prov_reset(bool erase) { int i; - if (prov == NULL) { + if (bt_mesh_prov_get() == NULL) { BT_ERR("No provisioning context provided"); return -EINVAL; } for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) { - k_delayed_work_cancel(&link[i].timeout); + k_delayed_work_cancel(&prov_links[i].prot_timer); - prov_memory_free(i); + prov_memory_free(&prov_links[i]); if (i < CONFIG_BLE_MESH_PBA_SAME_TIME) { #if CONFIG_BLE_MESH_PB_ADV - prov_clear_tx(i); + bt_mesh_prov_clear_tx(&prov_links[i], true); #if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE, - BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID, &link[i].link_id); + BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID, + &prov_links[i].link_id); #endif - memset(&link[i], 0, offsetof(struct prov_link, tx.retransmit)); - link[i].pending_ack = XACT_NVAL; - link[i].rx.prev_id = XACT_NVAL; - link[i].rx.buf = bt_mesh_pba_get_buf(i); + memset(&prov_links[i], 0, offsetof(struct bt_mesh_prov_link, tx.retransmit)); + prov_links[i].pending_ack = PROV_XACT_NVAL; + prov_links[i].rx.prev_id = PROV_XACT_NVAL; + prov_links[i].rx.buf = get_rx_buf(i); #endif /* CONFIG_BLE_MESH_PB_ADV */ } else { - memset(&link[i], 0, offsetof(struct prov_link, timeout)); + memset(&prov_links[i], 0, offsetof(struct bt_mesh_prov_link, prot_timer)); } - if (bt_mesh_pub_key_get()) { - bt_mesh_atomic_set_bit(link[i].flags, LOCAL_PUB_KEY); +#if CONFIG_BLE_MESH_CERT_BASED_PROV + for (size_t j = 0; j < BLE_MESH_REC_MAX_ID; j++) { + if (prov_links[i].records[j]) { + bt_mesh_free(prov_links[i].records[j]); + prov_links[i].records[j] = NULL; + } } +#endif /* CONFIG_BLE_MESH_CERT_BASED_PROV */ } /* static_oob_len & static_oob_val are initialized during mesh init. @@ -3388,46 +2885,44 @@ int bt_mesh_provisioner_prov_deinit(bool erase) { int i; - if (prov == NULL) { - BT_ERR("%s, No provisioning context provided", __func__); + if (bt_mesh_prov_get() == NULL) { + BT_ERR("No provisioning context provided"); return -EINVAL; } bt_mesh_provisioner_prov_reset(erase); for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) { -#if defined(CONFIG_BLE_MESH_PB_ADV) +#if CONFIG_BLE_MESH_PB_ADV if (i < CONFIG_BLE_MESH_PBA_SAME_TIME) { - k_delayed_work_free(&link[i].tx.retransmit); + k_delayed_work_free(&prov_links[i].tx.retransmit); + bt_mesh_mutex_free(&prov_links[i].buf_lock); } #endif - k_delayed_work_free(&link[i].timeout); - memset(&link[i], 0, sizeof(link[i])); + k_delayed_work_free(&prov_links[i].prot_timer); + memset(&prov_links[i], 0, sizeof(prov_links[i])); } -#if defined(CONFIG_BLE_MESH_PB_ADV) - bt_mesh_pb_adv_mutex_free(); - bt_mesh_pb_buf_mutex_free(); +#if CONFIG_BLE_MESH_PB_ADV + bt_mesh_mutex_free(&prov_ctx.pb_adv_lock); #endif -#if defined(CONFIG_BLE_MESH_PB_GATT) - bt_mesh_pb_gatt_mutex_free(); +#if CONFIG_BLE_MESH_PB_GATT + bt_mesh_mutex_free(&prov_ctx.pb_gatt_lock); #endif prov_ctx.static_oob_len = 0U; memset(prov_ctx.static_oob_val, 0, sizeof(prov_ctx.static_oob_val)); -#if defined(CONFIG_BLE_MESH_PB_ADV) - memset(adv_buf, 0, sizeof(adv_buf)); - memset(adv_buf_data, 0, sizeof(adv_buf_data)); +#if CONFIG_BLE_MESH_PB_ADV + memset(rx_buf, 0, sizeof(rx_buf)); + memset(rx_buf_data, 0, sizeof(rx_buf_data)); #endif - prov = NULL; - return 0; } #endif /* CONFIG_BLE_MESH_DEINIT */ -static bool is_unprov_dev_info_callback_to_app(bt_mesh_prov_bearer_t bearer, const uint8_t uuid[16], - const bt_mesh_addr_t *addr, uint16_t oob_info, int8_t rssi) +static bool notify_unprov_dev_info(bt_mesh_prov_bearer_t bearer, const uint8_t uuid[16], + const bt_mesh_addr_t *addr, uint16_t oob_info, int8_t rssi) { int i; @@ -3444,7 +2939,8 @@ static bool is_unprov_dev_info_callback_to_app(bt_mesh_prov_bearer_t bearer, con if (i == ARRAY_SIZE(unprov_dev)) { BT_DBG("Device not in queue, notify to app layer"); if (notify_unprov_adv_pkt_cb) { - notify_unprov_adv_pkt_cb(addr->val, addr->type, adv_type, uuid, oob_info, bearer, rssi); + notify_unprov_adv_pkt_cb(addr->val, addr->type, adv_type, + uuid, oob_info, bearer, rssi); } return true; } @@ -3453,7 +2949,8 @@ static bool is_unprov_dev_info_callback_to_app(bt_mesh_prov_bearer_t bearer, con 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, rssi); + notify_unprov_adv_pkt_cb(addr->val, addr->type, adv_type, + uuid, oob_info, bearer, rssi); } return true; } @@ -3462,9 +2959,9 @@ static bool is_unprov_dev_info_callback_to_app(bt_mesh_prov_bearer_t bearer, con return false; } +#if CONFIG_BLE_MESH_PB_ADV void bt_mesh_provisioner_unprov_beacon_recv(struct net_buf_simple *buf, int8_t rssi) { -#if defined(CONFIG_BLE_MESH_PB_ADV) const bt_mesh_addr_t *addr = NULL; const uint8_t *uuid = NULL; uint16_t oob_info = 0U; @@ -3489,19 +2986,18 @@ void bt_mesh_provisioner_unprov_beacon_recv(struct net_buf_simple *buf, int8_t r return; } - if (is_unprov_dev_info_callback_to_app( - BLE_MESH_PROV_ADV, uuid, addr, oob_info, rssi)) { + if (notify_unprov_dev_info(BLE_MESH_PROV_ADV, uuid, addr, oob_info, rssi)) { return; } provisioner_start_prov_pb_adv(uuid, addr, oob_info, BLE_MESH_ADDR_UNASSIGNED); -#endif /* CONFIG_BLE_MESH_PB_ADV */ } +#endif /* CONFIG_BLE_MESH_PB_ADV */ +#if CONFIG_BLE_MESH_PB_GATT void bt_mesh_provisioner_prov_adv_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr, int8_t rssi) { -#if defined(CONFIG_BLE_MESH_PB_GATT) const uint8_t *uuid = NULL; uint16_t oob_info = 0U; @@ -3524,8 +3020,7 @@ void bt_mesh_provisioner_prov_adv_recv(struct net_buf_simple *buf, return; } - if (is_unprov_dev_info_callback_to_app( - BLE_MESH_PROV_GATT, uuid, addr, oob_info, rssi)) { + if (notify_unprov_dev_info(BLE_MESH_PROV_GATT, uuid, addr, oob_info, rssi)) { return; } @@ -3538,11 +3033,133 @@ void bt_mesh_provisioner_prov_adv_recv(struct net_buf_simple *buf, * activated first, the other one will be dropped finally and the link struct * occupied by the dropped link will be used by other devices (because the link * is not activated). - * Use connecting flag to prevent if two devices's adv pkts are both received, + * 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, BLE_MESH_ADDR_UNASSIGNED); +} #endif /* CONFIG_BLE_MESH_PB_GATT */ + +/* This function can be invoked by Remote Provisioning Client + * to handle received Provisioning PDUs. + */ +int bt_mesh_rpr_cli_pdu_recv(struct bt_mesh_prov_link *link, uint8_t type, + struct net_buf_simple *buf) +{ + if (type != link->expect) { + BT_ERR("PB-Remote, unexpected msg 0x%02x != 0x%02x", type, link->expect); + return -EINVAL; + } + + prov_handlers[type].func(link, buf); + return 0; +} + +int bt_mesh_rpr_cli_pdu_send(struct bt_mesh_prov_link *link, uint8_t type) +{ + switch (type) { + case PROV_INVITE: + send_invite(link); + break; + case PROV_PUB_KEY: + send_pub_key(link); + break; + case PROV_CONFIRM: + send_confirm(link); + break; + default: + break; + } + + return 0; +} + +/* The behavior of sending confirmation based on the combination + * of Public Key and authentication: + * + * 1. No OOB Public Key + No OOB + * - No need to send confirmation manually, it will be sent when + * Device Public Key is received. + * + * 2. No OOB Public Key + Static OOB + * - Same with 1. + * + * 3. No OOB Public Key + Output OOB + * - Confirmation will be sent after the authentication value is + * set with bt_mesh_provisioner_set_oob_input_data(). + * + * 4. No OOB Public Key + Input OOB + * - No need to send confirmation manually, it will be sent when + * Input Complete is received. + * Note: + * Need to make sure the authentication value has been set with + * bt_mesh_provisioner_set_oob_output_data() before sending + * confirmation. + * + * 5. OOB Public Key + No OOB + * - If OOB Public Key is set before sending Public Key, we need + * to wait for the Remote Provisioning PDU Outbound Report for + * Public Key, then send confirmation manually; + * - If OOB Public Key is set after sending Public Key, and before + * receiving the Remote Provisioning PDU Outbound Report for + * Public Key, need to send confirmation manually; + * - If OOB Public Key is set after sending Public Key, and after + * receiving the Remote Provisioning PDU Outbound Report for + * Public Key, no need to sending confirmation manually, it will + * be sent after DHKey is generated successfully. + * + * 6. OOB Public Key + Static OOB + * - Same with 5. + * + * 7. OOB Public Key + Output OOB + * - If OOB Public Key is set before sending Public Key, DHKey + * will be generated after Public Key is sent, and: + * - If authentication value is set before receiving Remote + * Provisioning PDU Outbound Report, we need to wait for + * the Remote Provisioning PDU Outbound Report for Public + * Key, then send confirmation manually; + * - If authentication value is set after receiving Remote + * Provisioning PDU Outbound Report, no need to send + * confirmation manually, it will be sent in + * bt_mesh_provisioner_set_oob_input_data(). + * - If OOB Public Key is set after sending Public Key, and the + * OOB Public Key should be set before setting authentication + * value. After OOB Public Key is set, DHKey will be generated. + * And after the authentication value is set: + * - If authentication value is set before receiving Remote + * Provisioning PDU Outbound Report, we need to wait for + * the Remote Provisioning PDU Outbound Report for Public + * Key, then send confirmation manually; + * - If authentication value is set after receiving Remote + * Provisioning PDU Outbound Report, no need to send + * confirmation manually, it will be sent in + * bt_mesh_provisioner_set_oob_input_data(). + * + * 8. OOB Public Key + Input OOB + * - No need to send confirmation manually, it will be sent + * when Input Complete is received. + * Note: + * Need to make sure the authentication value has been set + * with bt_mesh_provisioner_set_oob_output_data() before + * sending confirmation. + * + * Can change the behavior that after sending Public Key, then + * notify the application layer to input OOB Public Key, this + * could simplify the implementation. + */ + +/* The following two functions need to be used based on the above + * considerations. + */ +int bt_mesh_rpr_cli_recv_pub_key_outbound_report(struct bt_mesh_prov_link *link) +{ + bt_mesh_atomic_test_and_clear_bit(link->flags, WAIT_PK_OBR); + + if (bt_mesh_atomic_test_and_clear_bit(link->flags, SEND_CONFIRM)) { + send_confirm(link); + } + + return 0; } #endif /* CONFIG_BLE_MESH_PROVISIONER */ diff --git a/components/bt/esp_ble_mesh/core/prov_pvnr.h b/components/bt/esp_ble_mesh/core/prov_pvnr.h index 22ca891fa840..0553a72d3617 100644 --- a/components/bt/esp_ble_mesh/core/prov_pvnr.h +++ b/components/bt/esp_ble_mesh/core/prov_pvnr.h @@ -8,20 +8,13 @@ #define _PROV_PVNR_H_ #include "mesh/main.h" +#include "prov_common.h" #include "mesh/adapter.h" #ifdef __cplusplus extern "C" { #endif -#ifndef CONFIG_BLE_MESH_PBA_SAME_TIME -#define CONFIG_BLE_MESH_PBA_SAME_TIME 0 -#endif - -#ifndef CONFIG_BLE_MESH_PBG_SAME_TIME -#define CONFIG_BLE_MESH_PBG_SAME_TIME 0 -#endif - #define RM_AFTER_PROV BIT(0) #define START_PROV_NOW BIT(1) #define FLUSHABLE_DEV BIT(2) @@ -80,17 +73,6 @@ void bt_mesh_provisioner_clear_link_info(const uint8_t addr[6]); */ void bt_mesh_provisioner_pb_adv_recv(struct net_buf_simple *buf); -/** - * @brief This function sends provisioning invite to start - * provisioning this unprovisioned device. - * - * @param[in] addr: Remote device address - * @param[in] conn: Pointer to the bt_conn structure - * - * @return Zero - success, otherwise - fail - */ -int bt_mesh_provisioner_set_prov_conn(const uint8_t addr[6], struct bt_mesh_conn *conn); - /** * @brief This function sends provisioning invite to start * provisioning this unprovisioned device. @@ -100,7 +82,7 @@ int bt_mesh_provisioner_set_prov_conn(const uint8_t addr[6], struct bt_mesh_conn * * @return Zero - success, otherwise - fail */ -int bt_mesh_provisioner_pb_gatt_open(struct bt_mesh_conn *conn, uint8_t *addr); +int bt_mesh_provisioner_pb_gatt_open(struct bt_mesh_conn *conn, const uint8_t addr[6]); /** * @brief This function resets the used information when @@ -128,11 +110,9 @@ int bt_mesh_provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_s * @brief This function initializes provisioner's PB-GATT and PB-ADV * related information. * - * @param[in] prov_info: Pointer to the application-initialized provisioner info. - * * @return Zero - success, otherwise - fail */ -int bt_mesh_provisioner_prov_init(const struct bt_mesh_prov *prov_info); +int bt_mesh_provisioner_prov_init(void); int bt_mesh_provisioner_prov_reset(bool erase); @@ -161,13 +141,6 @@ void bt_mesh_provisioner_unprov_beacon_recv(struct net_buf_simple *buf, int8_t r void bt_mesh_provisioner_prov_adv_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr, int8_t rssi); -/** - * @brief This function gets the bt_mesh_prov pointer. - * - * @return bt_mesh_prov pointer(prov) - */ -const struct bt_mesh_prov *bt_mesh_provisioner_get_prov_info(void); - void bt_mesh_provisioner_restore_prov_info(uint16_t primary_addr, uint16_t alloc_addr); /* The following APIs are for primary provisioner application use */ @@ -406,15 +379,12 @@ uint16_t bt_mesh_provisioner_get_fast_prov_net_idx(void); */ uint8_t bt_mesh_set_fast_prov_unicast_addr_range(uint16_t min, uint16_t max); -/** - * @brief This function is called to set flags & iv_index used for fast provisioning. - * - * @param[in] flags: Key refresh flag and iv update flag - * @param[in] iv_index: IV index - * - * @return None - */ -void bt_mesh_set_fast_prov_flags_iv_index(uint8_t flags, uint32_t iv_index); +int bt_mesh_rpr_cli_pdu_recv(struct bt_mesh_prov_link *link, uint8_t type, + struct net_buf_simple *buf); + +int bt_mesh_rpr_cli_pdu_send(struct bt_mesh_prov_link *link, uint8_t type); + +int bt_mesh_rpr_cli_recv_pub_key_outbound_report(struct bt_mesh_prov_link *link); #ifdef __cplusplus } diff --git a/components/bt/esp_ble_mesh/core/proxy_client.c b/components/bt/esp_ble_mesh/core/proxy_client.c index 891d8ea45a7a..9382cf61478e 100644 --- a/components/bt/esp_ble_mesh/core/proxy_client.c +++ b/components/bt/esp_ble_mesh/core/proxy_client.c @@ -8,49 +8,51 @@ #include #include "mesh.h" +#include "rpl.h" #include "access.h" #include "beacon.h" #include "transport.h" #include "mesh/common.h" #include "foundation.h" +#include "proxy_common.h" #include "proxy_client.h" #include "prov_pvnr.h" +#include "prov_node.h" #include "pvnr_mgmt.h" #include "mesh/adapter.h" -#define PDU_TYPE(data) (data[0] & BIT_MASK(6)) -#define PDU_SAR(data) (data[0] >> 6) - -#define PROXY_SAR_TIMEOUT K_SECONDS(20) - -#define SAR_COMPLETE 0x00 -#define SAR_FIRST 0x01 -#define SAR_CONT 0x02 -#define SAR_LAST 0x03 - -#define PDU_HDR(sar, type) (sar << 6 | (type & BIT_MASK(6))) - -#define SERVER_BUF_SIZE 68 +#include "mesh_v1.1/utils.h" #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT + CONFIG_BLE_MESH_GATT_PROXY_CLIENT static struct bt_mesh_proxy_server { struct bt_mesh_conn *conn; - enum __packed { - NONE, - PROV, - PROXY, + + enum __attribute__((packed)) { + CLI_NONE, + CLI_PROV, + CLI_PROXY, } conn_type; + #if CONFIG_BLE_MESH_GATT_PROXY_CLIENT uint16_t net_idx; #endif uint8_t msg_type; + struct k_delayed_work sar_timer; + struct net_buf_simple buf; } servers[BLE_MESH_MAX_CONN]; -static uint8_t server_buf_data[SERVER_BUF_SIZE * BLE_MESH_MAX_CONN]; +#if CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT +static struct { + struct bt_mesh_prov_link *link; + bt_mesh_addr_t addr; +} waiting_conn_link[BLE_MESH_MAX_CONN]; +#endif + +static uint8_t server_buf_data[BLE_MESH_PROXY_BUF_SIZE * BLE_MESH_MAX_CONN]; static struct bt_mesh_proxy_server *find_server(struct bt_mesh_conn *conn) { @@ -111,6 +113,22 @@ void bt_mesh_proxy_client_set_filter_status_cb(proxy_client_recv_filter_status_c proxy_client_filter_status_recv_cb = cb; } +#if (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) +int bt_mesh_rpr_srv_set_waiting_prov_link(struct bt_mesh_prov_link *link, + bt_mesh_addr_t *addr) +{ + for (size_t i = 0; i < ARRAY_SIZE(waiting_conn_link);i++) { + if (waiting_conn_link[i].link == NULL) { + waiting_conn_link[i].link = link; + memcpy(&waiting_conn_link[i].addr, addr, sizeof(bt_mesh_addr_t)); + return 0; + } + } + + return -ENOBUFS; +} +#endif /* CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT */ + static void filter_status(struct bt_mesh_proxy_server *server, struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) @@ -136,9 +154,22 @@ static void filter_status(struct bt_mesh_proxy_server *server, 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); } +} + +#if CONFIG_BLE_MESH_DF_SRV +static void recv_directed_proxy_caps_status(struct bt_mesh_proxy_server *server, + struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + uint8_t directed_proxy = net_buf_simple_pull_u8(buf); + uint8_t use_directed = net_buf_simple_pull_u8(buf); - return; + BT_INFO("Directed Proxy 0x%02x, Use Directed 0x%02x", directed_proxy, use_directed); + + ARG_UNUSED(directed_proxy); + ARG_UNUSED(use_directed); } +#endif /* CONFIG_BLE_MESH_DF_SRV */ static void proxy_cfg(struct bt_mesh_proxy_server *server) { @@ -188,6 +219,18 @@ static void proxy_cfg(struct bt_mesh_proxy_server *server) case BLE_MESH_PROXY_CFG_FILTER_STATUS: filter_status(server, &rx, &buf); break; + +#if CONFIG_BLE_MESH_DF_SRV + case BLE_MESH_PROXY_CFG_DIRECTED_PROXY_CAPS_STATUS: + if (buf.len != 2) { + BT_WARN("Invalid Directed Proxy Caps Status (len %d)", buf.len); + break; + } + + recv_directed_proxy_caps_status(server, &rx, &buf); + break; +#endif /* CONFIG_BLE_MESH_DF_SRV */ + default: BT_WARN("Unknown Proxy Configuration OpCode 0x%02x", opcode); break; @@ -212,10 +255,17 @@ static void proxy_complete_pdu(struct bt_mesh_proxy_server *server) proxy_cfg(server); break; #endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ -#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT +#if CONFIG_BLE_MESH_PB_GATT && (CONFIG_BLE_MESH_PROVISIONER || CONFIG_BLE_MESH_RPR_SRV) case BLE_MESH_PROXY_PROV: BT_DBG("Mesh Provisioning PDU"); - bt_mesh_provisioner_pb_gatt_recv(server->conn, &server->buf); +#if CONFIG_BLE_MESH_RPR_SRV + if (server->conn == bt_mesh_prov_node_get_link()->conn) { + bt_mesh_pb_gatt_recv(server->conn, &server->buf); + } else +#endif + { + bt_mesh_provisioner_pb_gatt_recv(server->conn, &server->buf); + } break; #endif default: @@ -252,7 +302,7 @@ static ssize_t proxy_recv(struct bt_mesh_conn *conn, return -ENOTCONN; } - if (ATTR_IS_PROV(srvc_uuid) != (PDU_TYPE(data) == BLE_MESH_PROXY_PROV)) { + if (ATTR_IS_PROV(srvc_uuid) != (BLE_MESH_PROXY_PDU_TYPE(data) == BLE_MESH_PROXY_PROV)) { BT_WARN("Proxy PDU type doesn't match GATT service uuid"); return -EINVAL; } @@ -262,51 +312,51 @@ static ssize_t proxy_recv(struct bt_mesh_conn *conn, return -EINVAL; } - switch (PDU_SAR(data)) { - case SAR_COMPLETE: + switch (BLE_MESH_PROXY_PDU_SAR(data)) { + case BLE_MESH_PROXY_SAR_COMP: if (server->buf.len) { BT_WARN("Complete PDU while a pending incomplete one"); return -EINVAL; } - server->msg_type = PDU_TYPE(data); + server->msg_type = BLE_MESH_PROXY_PDU_TYPE(data); net_buf_simple_add_mem(&server->buf, data + 1, len - 1); proxy_complete_pdu(server); break; - case SAR_FIRST: + case BLE_MESH_PROXY_SAR_FIRST: if (server->buf.len) { BT_WARN("First PDU while a pending incomplete one"); return -EINVAL; } - k_delayed_work_submit(&server->sar_timer, PROXY_SAR_TIMEOUT); - server->msg_type = PDU_TYPE(data); + k_delayed_work_submit(&server->sar_timer, BLE_MESH_PROXY_SAR_TIMEOUT); + server->msg_type = BLE_MESH_PROXY_PDU_TYPE(data); net_buf_simple_add_mem(&server->buf, data + 1, len - 1); break; - case SAR_CONT: + case BLE_MESH_PROXY_SAR_CONT: if (!server->buf.len) { BT_WARN("Continuation with no prior data"); return -EINVAL; } - if (server->msg_type != PDU_TYPE(data)) { + if (server->msg_type != BLE_MESH_PROXY_PDU_TYPE(data)) { BT_WARN("Unexpected message type in continuation"); return -EINVAL; } - k_delayed_work_submit(&server->sar_timer, PROXY_SAR_TIMEOUT); + k_delayed_work_submit(&server->sar_timer, BLE_MESH_PROXY_SAR_TIMEOUT); net_buf_simple_add_mem(&server->buf, data + 1, len - 1); break; - case SAR_LAST: + case BLE_MESH_PROXY_SAR_LAST: if (!server->buf.len) { BT_WARN("Last SAR PDU with no prior data"); return -EINVAL; } - if (server->msg_type != PDU_TYPE(data)) { + if (server->msg_type != BLE_MESH_PROXY_PDU_TYPE(data)) { BT_WARN("Unexpected message type in last SAR PDU"); return -EINVAL; } @@ -320,7 +370,6 @@ static ssize_t proxy_recv(struct bt_mesh_conn *conn, return len; } - static int proxy_send(struct bt_mesh_conn *conn, const void *data, uint16_t len) { BT_DBG("%u bytes: %s", len, bt_hex(data, len)); @@ -328,8 +377,8 @@ static int proxy_send(struct bt_mesh_conn *conn, const void *data, uint16_t len) return bt_mesh_gattc_write_no_rsp(conn, NULL, data, len); } -static int proxy_segment_and_send(struct bt_mesh_conn *conn, uint8_t type, - struct net_buf_simple *msg) +int bt_mesh_proxy_client_segment_send(struct bt_mesh_conn *conn, uint8_t type, + struct net_buf_simple *msg) { uint16_t mtu = 0U; int err = 0; @@ -351,22 +400,22 @@ static int proxy_segment_and_send(struct bt_mesh_conn *conn, uint8_t type, /* ATT_MTU - OpCode (1 byte) - Handle (2 bytes) */ mtu -= 3; if (mtu > msg->len) { - net_buf_simple_push_u8(msg, PDU_HDR(SAR_COMPLETE, type)); + net_buf_simple_push_u8(msg, BLE_MESH_PROXY_PDU_HDR(BLE_MESH_PROXY_SAR_COMP, type)); return proxy_send(conn, msg->data, msg->len); } - net_buf_simple_push_u8(msg, PDU_HDR(SAR_FIRST, type)); + net_buf_simple_push_u8(msg, BLE_MESH_PROXY_PDU_HDR(BLE_MESH_PROXY_SAR_FIRST, type)); err = proxy_send(conn, msg->data, mtu); net_buf_simple_pull(msg, mtu); while (msg->len) { if (msg->len + 1 < mtu) { - net_buf_simple_push_u8(msg, PDU_HDR(SAR_LAST, type)); + net_buf_simple_push_u8(msg, BLE_MESH_PROXY_PDU_HDR(BLE_MESH_PROXY_SAR_LAST, type)); err = proxy_send(conn, msg->data, msg->len); break; } - net_buf_simple_push_u8(msg, PDU_HDR(SAR_CONT, type)); + net_buf_simple_push_u8(msg, BLE_MESH_PROXY_PDU_HDR(BLE_MESH_PROXY_SAR_CONT, type)); err = proxy_send(conn, msg->data, mtu); net_buf_simple_pull(msg, mtu); } @@ -384,12 +433,12 @@ int bt_mesh_proxy_client_send(struct bt_mesh_conn *conn, uint8_t type, return -ENOTCONN; } - if ((server->conn_type == PROV) != (type == BLE_MESH_PROXY_PROV)) { + if ((server->conn_type == CLI_PROV) != (type == BLE_MESH_PROXY_PROV)) { BT_ERR("Invalid PDU type for Proxy Server"); return -EINVAL; } - return proxy_segment_and_send(conn, type, msg); + return bt_mesh_proxy_client_segment_send(conn, type, msg); } static void proxy_connected(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn, int id) @@ -402,19 +451,27 @@ static void proxy_connected(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn, int if (!server) { BT_ERR("No free Proxy Server objects"); - /** Disconnect current connection, clear part of prov_link - * information, like uuid, dev_addr, linking flag, etc. + /* Disconnect current connection, clear part of prov_link + * information, like uuid, dev_addr, linking flag, etc. */ bt_mesh_gattc_disconnect(conn); return; } server->conn = bt_mesh_conn_ref(conn); - server->conn_type = NONE; + server->conn_type = CLI_NONE; net_buf_simple_reset(&server->buf); +#if CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT + for (size_t i = 0; i < ARRAY_SIZE(waiting_conn_link); i++) { + if (!memcmp(addr, &waiting_conn_link[i].addr, sizeof(bt_mesh_addr_t))) { + waiting_conn_link[i].link->conn = conn; + break; + } + } +#endif + bt_mesh_gattc_exchange_mtu(id); - return; } static void proxy_disconnected(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn, uint8_t reason) @@ -428,14 +485,29 @@ static void proxy_disconnected(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn, return; } -#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT - if (server->conn_type == PROV) { - bt_mesh_provisioner_pb_gatt_close(conn, reason); +#if CONFIG_BLE_MESH_PB_GATT && \ + (CONFIG_BLE_MESH_PROVISIONER || CONFIG_BLE_MESH_RPR_SRV) + if (server->conn_type == CLI_PROV) { +#if CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_prov_node_get_link()->conn == conn) { + for (size_t i = 0; i < ARRAY_SIZE(waiting_conn_link); i++) { + if (waiting_conn_link[i].link->conn == conn) { + memset(&waiting_conn_link[i].addr, 0, sizeof(bt_mesh_addr_t)); + break; + } + } + + bt_mesh_pb_gatt_close(conn, reason); + } else +#endif /* CONFIG_BLE_MESH_RPR_SRV */ + { + bt_mesh_provisioner_pb_gatt_close(conn, reason); + } } -#endif +#endif /* CONFIG_BLE_MESH_PB_GATT && (CONFIG_BLE_MESH_PROVISIONER || CONFIG_BLE_MESH_RPR_SRV) */ #if CONFIG_BLE_MESH_GATT_PROXY_CLIENT - if (server->conn_type == PROXY) { + if (server->conn_type == CLI_PROXY) { if (proxy_client_disconnect_cb) { proxy_client_disconnect_cb(addr, server - servers, server->net_idx, reason); } @@ -444,15 +516,14 @@ static void proxy_disconnected(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn, k_delayed_work_cancel(&server->sar_timer); server->conn = NULL; - server->conn_type = NONE; + server->conn_type = CLI_NONE; #if CONFIG_BLE_MESH_GATT_PROXY_CLIENT server->net_idx = BLE_MESH_KEY_UNUSED; #endif - - return; } -#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT +#if CONFIG_BLE_MESH_PB_GATT && \ + (CONFIG_BLE_MESH_PROVISIONER || CONFIG_BLE_MESH_RPR_SRV) static ssize_t prov_write_ccc(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn) { struct bt_mesh_proxy_server *server = find_server(conn); @@ -462,13 +533,21 @@ static ssize_t prov_write_ccc(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn) return -ENOTCONN; } - if (server->conn_type == NONE) { - server->conn_type = PROV; + if (server->conn_type == CLI_NONE) { + server->conn_type = CLI_PROV; - if (bt_mesh_provisioner_set_prov_conn(addr->val, server->conn)) { - bt_mesh_gattc_disconnect(server->conn); - return -EIO; +#if CONFIG_BLE_MESH_RPR_SRV + if (bt_mesh_prov_node_get_link()->conn == conn) { + int err = bt_mesh_pb_gatt_open(conn); + if (err) { + BT_ERR("proxy write ccc error %d", err); + return err; + } + + return bt_mesh_rpr_srv_recv_link_ack(addr->val, false); } +#endif + return bt_mesh_provisioner_pb_gatt_open(conn, addr->val); } @@ -484,7 +563,7 @@ static ssize_t prov_recv_ntf(struct bt_mesh_conn *conn, uint8_t *data, uint16_t return -ENOTCONN; } - if (server->conn_type == PROV) { + if (server->conn_type == CLI_PROV) { return proxy_recv(conn, NULL, data, len, 0, 0); } @@ -495,11 +574,9 @@ int bt_mesh_proxy_client_prov_enable(void) { int i; - BT_DBG("%s", __func__); - for (i = 0; i < ARRAY_SIZE(servers); i++) { if (servers[i].conn) { - servers[i].conn_type = PROV; + servers[i].conn_type = CLI_PROV; } } @@ -510,22 +587,20 @@ int bt_mesh_proxy_client_prov_disable(void) { int i; - BT_DBG("%s", __func__); - for (i = 0; i < ARRAY_SIZE(servers); i++) { struct bt_mesh_proxy_server *server = &servers[i]; - if (server->conn && server->conn_type == PROV) { + if (server->conn && server->conn_type == CLI_PROV) { bt_mesh_gattc_disconnect(server->conn); - server->conn_type = NONE; + server->conn_type = CLI_NONE; } } return 0; } -#endif /* CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT */ +#endif /* CONFIG_BLE_MESH_PB_GATT */ -#if defined(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT static ssize_t proxy_write_ccc(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn) { struct bt_mesh_proxy_server *server = find_server(conn); @@ -535,8 +610,8 @@ static ssize_t proxy_write_ccc(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn) return -ENOTCONN; } - if (server->conn_type == NONE) { - server->conn_type = PROXY; + if (server->conn_type == CLI_NONE) { + server->conn_type = CLI_PROXY; if (proxy_client_connect_cb) { proxy_client_connect_cb(addr, server - servers, server->net_idx); @@ -556,7 +631,7 @@ static ssize_t proxy_recv_ntf(struct bt_mesh_conn *conn, uint8_t *data, uint16_t return -ENOTCONN; } - if (server->conn_type == PROXY) { + if (server->conn_type == CLI_PROXY) { return proxy_recv(conn, NULL, data, len, 0, 0); } @@ -575,16 +650,13 @@ int bt_mesh_proxy_client_gatt_enable(void) { int i; - BT_DBG("%s", __func__); - for (i = 0; i < ARRAY_SIZE(servers); i++) { if (servers[i].conn) { - servers[i].conn_type = PROXY; + servers[i].conn_type = CLI_PROXY; } } - /** - * TODO: + /* TODO: * Once at least one device has been provisioned, proxy client can be * set to allow receiving and parsing node_id & net_id adv packets, * and we may use a global flag to indicate this. @@ -597,8 +669,6 @@ int bt_mesh_proxy_client_gatt_disable(void) { int i; - BT_DBG("%s", __func__); - /** * TODO: * Once this function is invoked, proxy client shall stop handling @@ -609,9 +679,9 @@ int bt_mesh_proxy_client_gatt_disable(void) for (i = 0; i < ARRAY_SIZE(servers); i++) { struct bt_mesh_proxy_server *server = &servers[i]; - if (server->conn && server->conn_type == PROXY) { + if (server->conn && server->conn_type == CLI_PROXY) { bt_mesh_gattc_disconnect(server->conn); - server->conn_type = NONE; + server->conn_type = CLI_NONE; } } @@ -626,13 +696,13 @@ static struct bt_mesh_prov_conn_cb conn_callbacks = { .prov_write_descr = prov_write_ccc, .prov_notify = prov_recv_ntf, #endif /* CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT */ -#if defined(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT .proxy_write_descr = proxy_write_ccc, .proxy_notify = proxy_recv_ntf, #endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ }; -#if defined(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT static struct bt_mesh_subnet *bt_mesh_is_net_id_exist(const uint8_t net_id[8]) { struct bt_mesh_subnet *sub = NULL; @@ -688,6 +758,12 @@ void bt_mesh_proxy_client_gatt_adv_recv(struct net_buf_simple *buf, * the hash value (8 octets) with the received one. */ return; + case BLE_MESH_PROXY_ADV_PRIVATE_NET_ID: + BT_DBG("Mesh Proxy Private Network ID adv 0x%02x", type); + return; + case BLE_MESH_PROXY_ADV_PRIVATE_NODE_ID: + BT_DBG("Mesh Proxy Private Node Identity adv 0x%02x", type); + return; default: BT_DBG("Unknown Mesh Proxy adv type 0x%02x", type); return; @@ -752,14 +828,14 @@ bool bt_mesh_proxy_client_relay(struct net_buf_simple *buf, uint16_t dst) struct bt_mesh_proxy_server *server = &servers[i]; NET_BUF_SIMPLE_DEFINE(msg, 32); - if (!server->conn || server->conn_type != PROXY) { + if (!server->conn || server->conn_type != CLI_PROXY) { continue; } /* Proxy PDU sending modifies the original buffer, * so we need to make a copy. */ - net_buf_simple_init(&msg, 1); + net_buf_simple_reserve(&msg, 1); net_buf_simple_add_mem(&msg, buf->data, buf->len); err = bt_mesh_proxy_client_send(server->conn, BLE_MESH_PROXY_NET_PDU, &msg); @@ -774,29 +850,36 @@ bool bt_mesh_proxy_client_relay(struct net_buf_simple *buf, uint16_t dst) return send; } -static int beacon_send(struct bt_mesh_conn *conn, struct bt_mesh_subnet *sub) +static int beacon_send(struct bt_mesh_conn *conn, struct bt_mesh_subnet *sub, bool private) { - NET_BUF_SIMPLE_DEFINE(buf, 23); + NET_BUF_SIMPLE_DEFINE(buf, 28); - net_buf_simple_init(&buf, 1); - bt_mesh_beacon_create(sub, &buf); + net_buf_simple_reserve(&buf, 1); +#if CONFIG_BLE_MESH_PRB_SRV + if (private) { + bt_mesh_private_beacon_create(sub, &buf); + } else +#endif + { + bt_mesh_secure_beacon_create(sub, &buf); + } return bt_mesh_proxy_client_send(conn, BLE_MESH_PROXY_BEACON, &buf); } -bool bt_mesh_proxy_client_beacon_send(struct bt_mesh_subnet *sub) +bool bt_mesh_proxy_client_beacon_send(struct bt_mesh_subnet *sub, bool private) { bool send = false; int err = 0; int i; - /* NULL means we send Secure Network Beacon on all subnets */ + /* NULL means we send Secure Network Beacon or Mesh Private Beacon on all subnets */ if (!sub) { #if CONFIG_BLE_MESH_NODE 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]); + send = bt_mesh_proxy_client_beacon_send(&bt_mesh.sub[i], private); } } return send; @@ -806,7 +889,7 @@ bool bt_mesh_proxy_client_beacon_send(struct bt_mesh_subnet *sub) 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]); + send = bt_mesh_proxy_client_beacon_send(bt_mesh.p_sub[i], private); } } return send; @@ -816,8 +899,8 @@ bool bt_mesh_proxy_client_beacon_send(struct bt_mesh_subnet *sub) } 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 (servers[i].conn && servers[i].conn_type == CLI_PROXY) { + err = beacon_send(servers[i].conn, sub, private); if (err) { BT_ERR("Failed to send proxy beacon message (err %d)", err); } else { @@ -832,10 +915,11 @@ bool bt_mesh_proxy_client_beacon_send(struct bt_mesh_subnet *sub) static int send_proxy_cfg(struct bt_mesh_conn *conn, uint16_t net_idx, struct bt_mesh_proxy_cfg_pdu *cfg) { struct bt_mesh_msg_ctx ctx = { - .net_idx = net_idx, - .app_idx = BLE_MESH_KEY_UNUSED, /* CTL shall be set to 1 */ - .addr = BLE_MESH_ADDR_UNASSIGNED, /* DST shall be set to the unassigned address */ - .send_ttl = 0U, /* TTL shall be set to 0 */ + .net_idx = net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, /* CTL shall be set to 1 */ + .addr = BLE_MESH_ADDR_UNASSIGNED, /* DST shall be set to the unassigned address */ + .send_ttl = 0U, /* TTL shall be set to 0 */ + .send_cred = BLE_MESH_FLOODING_CRED, }; struct bt_mesh_net_tx tx = { .ctx = &ctx, @@ -845,13 +929,9 @@ static int send_proxy_cfg(struct bt_mesh_conn *conn, uint16_t net_idx, struct bt uint16_t alloc_len = 0U; int err = 0; - if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { - tx.sub = bt_mesh_subnet_get(net_idx); - } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { - tx.sub = bt_mesh_provisioner_subnet_get(net_idx); - } + tx.sub = bt_mesh_subnet_get(net_idx); if (!tx.sub) { - BT_ERR("Invalid NetKeyIndex 0x%04x", net_idx); + BT_ERR("NetKey 0x%04x not found", net_idx); return -EIO; } @@ -864,6 +944,7 @@ static int send_proxy_cfg(struct bt_mesh_conn *conn, uint16_t net_idx, struct bt alloc_len = sizeof(cfg->opcode) + sizeof(cfg->set.filter_type); break; + case BLE_MESH_PROXY_CFG_FILTER_ADD: if (cfg->add.addr == NULL || cfg->add.addr_num == 0) { BT_ERR("Empty proxy addr list to add"); @@ -872,6 +953,7 @@ static int send_proxy_cfg(struct bt_mesh_conn *conn, uint16_t net_idx, struct bt alloc_len = sizeof(cfg->opcode) + (cfg->add.addr_num << 1); break; + case BLE_MESH_PROXY_CFG_FILTER_REMOVE: if (cfg->remove.addr == NULL || cfg->remove.addr_num == 0) { BT_ERR("Empty proxy addr list to remove"); @@ -880,14 +962,33 @@ static int send_proxy_cfg(struct bt_mesh_conn *conn, uint16_t net_idx, struct bt alloc_len = sizeof(cfg->opcode) + (cfg->remove.addr_num << 1); break; + +#if CONFIG_BLE_MESH_DF_SRV + case BLE_MESH_PROXY_CFG_DIRECTED_PROXY_CONTROL: + if (cfg->direct_proxy_ctrl.use_directed > BLE_MESH_PROXY_USE_DIRECTED_ENABLED) { + BT_ERR("Invalid Use Directed 0x%02x", cfg->direct_proxy_ctrl.use_directed); + return -EINVAL; + } + + if (cfg->direct_proxy_ctrl.use_directed == BLE_MESH_PROXY_USE_DIRECTED_ENABLED && + !bt_mesh_uar_valid(&cfg->direct_proxy_ctrl.proxy_client_uar)) { + BT_ERR("Invalid Proxy Client Unicast Addr Range (0x%04x, %d)", + cfg->direct_proxy_ctrl.proxy_client_uar.range_start, + cfg->direct_proxy_ctrl.proxy_client_uar.range_length); + return -EINVAL; + } + + alloc_len = sizeof(cfg->opcode) + 4; + break; +#endif /* CONFIG_BLE_MESH_DF_SRV */ + default: BT_ERR("Unknown Proxy Configuration opcode 0x%02x", cfg->opcode); return -EINVAL; } - /** - * For Proxy Configuration PDU: - * 1 octet Proxy PDU type + 9 octets network pdu header + Tranport PDU + 8 octets NetMIC + /* For Proxy Configuration PDU: + * 1 octet Proxy PDU type + 9 octets network pdu header + Transport PDU + 8 octets NetMIC */ buf = bt_mesh_alloc_buf(1 + BLE_MESH_NET_HDR_LEN + alloc_len + 8); if (!buf) { @@ -902,32 +1003,43 @@ static int send_proxy_cfg(struct bt_mesh_conn *conn, uint16_t net_idx, struct bt case BLE_MESH_PROXY_CFG_FILTER_SET: net_buf_simple_add_u8(buf, cfg->set.filter_type); break; + case BLE_MESH_PROXY_CFG_FILTER_ADD: for (uint16_t i = 0U; i < cfg->add.addr_num; i++) { net_buf_simple_add_le16(buf, cfg->add.addr[i]); } break; + case BLE_MESH_PROXY_CFG_FILTER_REMOVE: for (uint16_t i = 0U; i < cfg->remove.addr_num; i++) { net_buf_simple_add_le16(buf, cfg->remove.addr[i]); } break; + +#if CONFIG_BLE_MESH_DF_SRV + case BLE_MESH_PROXY_CFG_DIRECTED_PROXY_CONTROL: + net_buf_simple_add_u8(buf, cfg->direct_proxy_ctrl.use_directed); + if (cfg->direct_proxy_ctrl.use_directed == BLE_MESH_PROXY_USE_DIRECTED_ENABLED) { + bt_mesh_add_uar_be(buf, &cfg->direct_proxy_ctrl.proxy_client_uar); + } + break; +#endif /* CONFIG_BLE_MESH_DF_SRV */ } BT_DBG("len %u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); err = bt_mesh_net_encode(&tx, buf, true); if (err) { - BT_ERR("Encoding proxy message failed (err %d)", err); - bt_mesh_free_buf(buf); - return err; + BT_ERR("Encoding proxy cfg message failed (err %d)", err); + goto end; } err = bt_mesh_proxy_client_send(conn, BLE_MESH_PROXY_CONFIG, buf); if (err) { - BT_ERR("Failed to send proxy cfg message (err %d)", err); + BT_ERR("Failed to send proxy cfg message 0x%02x (err %d)", cfg->opcode, err); } +end: bt_mesh_free_buf(buf); return err; } @@ -937,7 +1049,7 @@ int bt_mesh_proxy_client_cfg_send(uint8_t conn_handle, uint16_t net_idx, { struct bt_mesh_conn *conn = NULL; - if (conn_handle >= BLE_MESH_MAX_CONN || !pdu || pdu->opcode > BLE_MESH_PROXY_CFG_FILTER_REMOVE) { + if (conn_handle >= BLE_MESH_MAX_CONN || !pdu || pdu->opcode > BLE_MESH_PROXY_CFG_DIRECTED_PROXY_CONTROL) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } @@ -950,8 +1062,7 @@ int bt_mesh_proxy_client_cfg_send(uint8_t conn_handle, uint16_t net_idx, return -ENOTCONN; } - /** - * Check if net_idx used to encrypt Proxy Configuration are the same + /* Check if net_idx used to encrypt Proxy Configuration are the same * with the one added when creating proxy connection. */ if (servers[conn_handle].net_idx != net_idx) { @@ -973,8 +1084,8 @@ int bt_mesh_proxy_client_init(void) struct bt_mesh_proxy_server *server = &servers[i]; k_delayed_work_init(&server->sar_timer, proxy_sar_timeout); - server->buf.size = SERVER_BUF_SIZE; - server->buf.__buf = server_buf_data + (i * SERVER_BUF_SIZE); + server->buf.size = BLE_MESH_PROXY_BUF_SIZE; + server->buf.__buf = server_buf_data + (i * BLE_MESH_PROXY_BUF_SIZE); #if CONFIG_BLE_MESH_GATT_PROXY_CLIENT server->net_idx = BLE_MESH_KEY_UNUSED; #endif @@ -984,7 +1095,8 @@ int bt_mesh_proxy_client_init(void) #if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN && CONFIG_BLE_MESH_GATT_PROXY_CLIENT bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD, - BLE_MESH_EXCEP_LIST_TYPE_MESH_PROXY_ADV, NULL); + BLE_MESH_EXCEP_LIST_TYPE_MESH_PROXY_ADV, + NULL); #endif return 0; @@ -1010,4 +1122,5 @@ int bt_mesh_proxy_client_deinit(void) } #endif /* CONFIG_BLE_MESH_DEINIT */ -#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ diff --git a/components/bt/esp_ble_mesh/core/proxy_client.h b/components/bt/esp_ble_mesh/core/proxy_client.h index f7a9d509ea86..d2d85017019b 100644 --- a/components/bt/esp_ble_mesh/core/proxy_client.h +++ b/components/bt/esp_ble_mesh/core/proxy_client.h @@ -9,24 +9,14 @@ #include "net.h" #include "mesh/adapter.h" +#include "prov_common.h" + +#include "mesh_v1.1/utils.h" #ifdef __cplusplus extern "C" { #endif -#define BLE_MESH_PROXY_ADV_NET_ID 0x00 -#define BLE_MESH_PROXY_ADV_NODE_ID 0x01 - -#define BLE_MESH_PROXY_NET_PDU 0x00 -#define BLE_MESH_PROXY_BEACON 0x01 -#define BLE_MESH_PROXY_CONFIG 0x02 -#define BLE_MESH_PROXY_PROV 0x03 - -#define BLE_MESH_PROXY_CFG_FILTER_SET 0x00 -#define BLE_MESH_PROXY_CFG_FILTER_ADD 0x01 -#define BLE_MESH_PROXY_CFG_FILTER_REMOVE 0x02 -#define BLE_MESH_PROXY_CFG_FILTER_STATUS 0x03 - typedef union { struct { uint8_t net_id[8]; @@ -35,6 +25,13 @@ typedef union { struct { uint16_t src; } node_id; + struct { + uint8_t net_id[8]; + uint16_t net_idx; + } private_net_id; + struct { + uint16_t src; + } private_node_id; } bt_mesh_proxy_adv_ctx_t; struct bt_mesh_proxy_net_pdu { @@ -55,6 +52,14 @@ struct bt_mesh_proxy_cfg_pdu { uint16_t *addr; uint16_t addr_num; } remove; + struct cfg_direct_proxy_ctrl { + uint8_t use_directed; + struct { + uint16_t len_present:1, + range_start:15; + uint8_t range_length; + } proxy_client_uar; + } direct_proxy_ctrl; }; }; @@ -95,13 +100,18 @@ void bt_mesh_proxy_client_gatt_adv_recv(struct net_buf_simple *buf, int bt_mesh_proxy_client_connect(const uint8_t addr[6], uint8_t addr_type, uint16_t net_idx); int bt_mesh_proxy_client_disconnect(uint8_t conn_handle); -bool bt_mesh_proxy_client_beacon_send(struct bt_mesh_subnet *sub); +bool bt_mesh_proxy_client_beacon_send(struct bt_mesh_subnet *sub, bool private); bool bt_mesh_proxy_client_relay(struct net_buf_simple *buf, uint16_t dst); int bt_mesh_proxy_client_cfg_send(uint8_t conn_handle, uint16_t net_idx, struct bt_mesh_proxy_cfg_pdu *pdu); +#if CONFIG_BLE_MESH_RPR_SRV +int bt_mesh_rpr_srv_set_waiting_prov_link(struct bt_mesh_prov_link* link, + bt_mesh_addr_t *addr); +#endif + int bt_mesh_proxy_client_init(void); int bt_mesh_proxy_client_deinit(void); diff --git a/components/bt/esp_ble_mesh/core/proxy_common.h b/components/bt/esp_ble_mesh/core/proxy_common.h new file mode 100644 index 000000000000..4ffb5e95f635 --- /dev/null +++ b/components/bt/esp_ble_mesh/core/proxy_common.h @@ -0,0 +1,67 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _PROXY_COMMON_H_ +#define _PROXY_COMMON_H_ + +#include "mesh/config.h" +#include "mesh/utils.h" +#include "mesh/timer.h" +#include "net.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_MESH_PROXY_ADV_NET_ID 0x00 +#define BLE_MESH_PROXY_ADV_NODE_ID 0x01 +#define BLE_MESH_PROXY_ADV_PRIVATE_NET_ID 0x02 +#define BLE_MESH_PROXY_ADV_PRIVATE_NODE_ID 0x03 + +#define BLE_MESH_PROXY_NET_PDU 0x00 +#define BLE_MESH_PROXY_BEACON 0x01 +#define BLE_MESH_PROXY_CONFIG 0x02 +#define BLE_MESH_PROXY_PROV 0x03 + +#define BLE_MESH_PROXY_SAR_COMP 0x00 +#define BLE_MESH_PROXY_SAR_FIRST 0x01 +#define BLE_MESH_PROXY_SAR_CONT 0x02 +#define BLE_MESH_PROXY_SAR_LAST 0x03 + +#define BLE_MESH_PROXY_CFG_FILTER_SET 0x00 +#define BLE_MESH_PROXY_CFG_FILTER_ADD 0x01 +#define BLE_MESH_PROXY_CFG_FILTER_REMOVE 0x02 +#define BLE_MESH_PROXY_CFG_FILTER_STATUS 0x03 +#define BLE_MESH_PROXY_CFG_DIRECTED_PROXY_CAPS_STATUS 0x04 +#define BLE_MESH_PROXY_CFG_DIRECTED_PROXY_CONTROL 0x05 + +#define BLE_MESH_PROXY_PRIVACY_DISABLED 0x00 +#define BLE_MESH_PROXY_PRIVACY_ENABLED 0x01 +#define BLE_MESH_PROXY_PRIVACY_NOT_SUPPORTED 0x02 + +#define BLE_MESH_PROXY_CLI_TYPE_UNSET BIT(0) +#define BLE_MESH_PROXY_CLI_TYPE_DIRECT_PROXY_CLIENT BIT(1) +#define BLE_MESH_PROXY_CLI_TYPE_BLACK_LIST_CLIENT BIT(2) +#define BLE_MESH_PROXY_CLI_TYPE_PROXY_CLIENT BIT(3) + +#define BLE_MESH_PROXY_PDU_TYPE(data) (data[0] & BIT_MASK(6)) +#define BLE_MESH_PROXY_PDU_SAR(data) (data[0] >> 6) + +#define BLE_MESH_PROXY_PDU_HDR(sar, type) ((sar) << 6 | ((type) & BIT_MASK(6))) + +/* Mesh spec 1.0.1 Section 6.6: + * "The timeout for the SAR transfer is 20 seconds. When the timeout + * expires, the Proxy Server shall disconnect." + */ +#define BLE_MESH_PROXY_SAR_TIMEOUT K_SECONDS(20) + +#define BLE_MESH_PROXY_BUF_SIZE 68 + +#ifdef __cplusplus +} +#endif + +#endif /* _PROXY_COMMON_H_ */ diff --git a/components/bt/esp_ble_mesh/core/proxy_server.c b/components/bt/esp_ble_mesh/core/proxy_server.c index bf907303a21a..aa360b85f8af 100644 --- a/components/bt/esp_ble_mesh/core/proxy_server.c +++ b/components/bt/esp_ble_mesh/core/proxy_server.c @@ -10,48 +10,40 @@ #include #include "mesh.h" +#include "rpl.h" #include "adv.h" -#include "prov_node.h" #include "beacon.h" #include "access.h" #include "transport.h" #include "foundation.h" #include "mesh/common.h" +#include "proxy_common.h" #include "proxy_server.h" +#include "prov_common.h" +#include "prov_node.h" + +#include "mesh_v1.1/utils.h" #if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_SERVER + CONFIG_BLE_MESH_GATT_PROXY_SERVER #if !CONFIG_BLE_MESH_BQB_TEST /* 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)), +_Static_assert(!(IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)), "Not support Proxy Server and Proxy Client simultaneously"); #endif -#define PDU_TYPE(data) (data[0] & BIT_MASK(6)) -#define PDU_SAR(data) (data[0] >> 6) - -/* Mesh Profile 1.0 Section 6.6: - * "The timeout for the SAR transfer is 20 seconds. When the timeout - * expires, the Proxy Server shall disconnect." - */ -#define PROXY_SAR_TIMEOUT K_SECONDS(20) - -#define SAR_COMPLETE 0x00 -#define SAR_FIRST 0x01 -#define SAR_CONT 0x02 -#define SAR_LAST 0x03 - -#define CFG_FILTER_SET 0x00 -#define CFG_FILTER_ADD 0x01 -#define CFG_FILTER_REMOVE 0x02 -#define CFG_FILTER_STATUS 0x03 - -#define PDU_HDR(sar, type) (sar << 6 | (type & BIT_MASK(6))) +#define ADV_OPT (BLE_MESH_ADV_OPT_CONNECTABLE | BLE_MESH_ADV_OPT_ONE_TIME) -#define CLIENT_BUF_SIZE 68 +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV +#define RAND_UPDATE_INTERVAL K_MINUTES(10) -#define ADV_OPT (BLE_MESH_ADV_OPT_CONNECTABLE | BLE_MESH_ADV_OPT_ONE_TIME) +/* The Random field of Private Network Identity + * Advertisement should be updated every 10 minutes. + */ +static struct k_delayed_work rand_upd_timer; +static uint8_t net_id_random[8] = {0}; +#endif static const struct bt_mesh_adv_param slow_adv_param = { .options = ADV_OPT, @@ -67,37 +59,26 @@ static const struct bt_mesh_adv_param fast_adv_param = { static bool proxy_adv_enabled; -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER static void proxy_send_beacons(struct k_work *work); static uint16_t proxy_ccc_val; #endif -#if defined(CONFIG_BLE_MESH_PB_GATT) +#if CONFIG_BLE_MESH_PB_GATT static uint16_t prov_ccc_val; static bool prov_fast_adv; static uint32_t prov_start_time; #endif -static struct bt_mesh_proxy_client { - struct bt_mesh_conn *conn; -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) - uint16_t filter[CONFIG_BLE_MESH_PROXY_FILTER_SIZE]; -#endif - enum __packed { - NONE, - WHITELIST, - BLACKLIST, - PROV, - } filter_type; - uint8_t msg_type; -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) - struct k_work send_beacons; +static struct bt_mesh_proxy_client clients[BLE_MESH_MAX_CONN] = { + [0 ... (BLE_MESH_MAX_CONN - 1)] = { +#if CONFIG_BLE_MESH_PROXY_PRIVACY + .proxy_privacy = BLE_MESH_PROXY_PRIVACY_DISABLED, #endif - struct k_delayed_work sar_timer; - struct net_buf_simple buf; -} clients[BLE_MESH_MAX_CONN]; + }, +}; -static uint8_t client_buf_data[CLIENT_BUF_SIZE * BLE_MESH_MAX_CONN]; +static uint8_t client_buf_data[BLE_MESH_PROXY_BUF_SIZE * BLE_MESH_MAX_CONN]; /* Track which service is enabled */ static enum { @@ -108,6 +89,16 @@ static enum { static char device_name[DEVICE_NAME_SIZE + 1]; +struct bt_mesh_proxy_client *bt_mesh_proxy_server_get_client(uint8_t index) +{ + return &clients[0]; +} + +uint8_t bt_mesh_proxy_server_get_client_count(void) +{ + return ARRAY_SIZE(clients); +} + int bt_mesh_set_device_name(const char *name) { if (!name) { @@ -126,6 +117,11 @@ int bt_mesh_set_device_name(const char *name) return bt_mesh_gatts_set_local_device_name(device_name); } +const char *bt_mesh_get_device_name(void) +{ + return device_name; +} + static struct bt_mesh_proxy_client *find_client(struct bt_mesh_conn *conn) { int i; @@ -155,7 +151,7 @@ static void proxy_sar_timeout(struct k_work *work) bt_mesh_gatts_disconnect(client->conn, 0x13); } -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER /** * The following callbacks are used to notify proper information * to the application layer. @@ -176,8 +172,37 @@ void bt_mesh_proxy_server_set_disconn_cb(proxy_server_disconnect_cb_t cb) /* Next subnet in queue to be advertised */ static int next_idx; -static int proxy_segment_and_send(struct bt_mesh_conn *conn, uint8_t type, - struct net_buf_simple *msg); +bool bt_mesh_proxy_server_find_client_by_addr(uint16_t addr) +{ + for (size_t i = 0; i < ARRAY_SIZE(clients); i++) { + if (clients[i].conn) { + for (size_t j = 0; j < ARRAY_SIZE(clients[i].filter); j++) { + if (clients[i].filter[j].proxy_client && clients[i].filter[j].addr == addr) { + return true; + } + } + } + } + + return false; +} + +uint8_t bt_mesh_proxy_server_get_all_client_type(void) +{ + uint8_t client_type = 0; + + for (size_t i = 0; i < ARRAY_SIZE(clients); i++) { + if (clients[i].conn) { + for (size_t j = 0; j < ARRAY_SIZE(clients[i].filter); j++) { + if (clients[i].filter[j].proxy_client) { + client_type |= clients[i].proxy_client_type; + } + } + } + } + + return client_type; +} static int filter_set(struct bt_mesh_proxy_client *client, struct net_buf_simple *buf) @@ -195,11 +220,14 @@ static int filter_set(struct bt_mesh_proxy_client *client, switch (type) { case 0x00: (void)memset(client->filter, 0, sizeof(client->filter)); - client->filter_type = WHITELIST; + client->filter_type = SRV_WHITELIST; break; case 0x01: +#if CONFIG_BLE_MESH_DF_SRV + bt_mesh_directed_proxy_server_set_blacklist(client); +#endif /* CONFIG_BLE_MESH_DF_SRV */ (void)memset(client->filter, 0, sizeof(client->filter)); - client->filter_type = BLACKLIST; + client->filter_type = SRV_BLACKLIST; break; default: BT_WARN("Prohibited Filter Type 0x%02x", type); @@ -209,26 +237,41 @@ static int filter_set(struct bt_mesh_proxy_client *client, return 0; } -static void filter_add(struct bt_mesh_proxy_client *client, uint16_t addr) +static void filter_add(struct bt_mesh_proxy_client *client, + uint16_t addr, bool proxy_client) { int i; + /* The flag proxy_client is used to indicate if the added address + * is the element address of Proxy Client. + */ + BT_DBG("addr 0x%04x", addr); if (addr == BLE_MESH_ADDR_UNASSIGNED) { return; } + if (!BLE_MESH_ADDR_IS_UNICAST(addr) && proxy_client == true) { + BT_ERR("Invalid proxy client element addr 0x%04x", addr); + return; + } + for (i = 0; i < ARRAY_SIZE(client->filter); i++) { - if (client->filter[i] == addr) { + if (client->filter[i].addr == addr) { return; } } for (i = 0; i < ARRAY_SIZE(client->filter); i++) { - if (client->filter[i] == BLE_MESH_ADDR_UNASSIGNED) { - client->filter[i] = addr; - BT_INFO("Add filter addr 0x%04x", addr); + if (client->filter[i].addr == addr) { + BT_INFO("client addr 0x%04x already added", addr); + return; + } + if (client->filter[i].addr == BLE_MESH_ADDR_UNASSIGNED) { + BT_INFO("Add client or filter addr 0x%04x", addr); + client->filter[i].addr = addr; + client->filter[i].proxy_client = proxy_client; return; } } @@ -247,8 +290,9 @@ static void filter_remove(struct bt_mesh_proxy_client *client, uint16_t addr) } for (i = 0; i < ARRAY_SIZE(client->filter); i++) { - if (client->filter[i] == addr) { - client->filter[i] = BLE_MESH_ADDR_UNASSIGNED; + if (client->filter[i].addr == addr) { + client->filter[i].addr = BLE_MESH_ADDR_UNASSIGNED; + client->filter[i].proxy_client = false; BT_INFO("Remove filter addr 0x%04x", addr); return; } @@ -269,20 +313,21 @@ static void send_filter_status(struct bt_mesh_proxy_client *client, /* Configuration messages always have dst unassigned */ tx.ctx->addr = BLE_MESH_ADDR_UNASSIGNED; + tx.ctx->send_cred = BLE_MESH_FLOODING_CRED, net_buf_simple_reset(buf); net_buf_simple_reserve(buf, 10); - net_buf_simple_add_u8(buf, CFG_FILTER_STATUS); + net_buf_simple_add_u8(buf, BLE_MESH_PROXY_CFG_FILTER_STATUS); - if (client->filter_type == WHITELIST) { + if (client->filter_type == SRV_WHITELIST) { net_buf_simple_add_u8(buf, 0x00); } else { net_buf_simple_add_u8(buf, 0x01); } for (filter_size = 0U, i = 0; i < ARRAY_SIZE(client->filter); i++) { - if (client->filter[i] != BLE_MESH_ADDR_UNASSIGNED) { + if (client->filter[i].addr != BLE_MESH_ADDR_UNASSIGNED) { filter_size++; } } @@ -293,13 +338,13 @@ static void send_filter_status(struct bt_mesh_proxy_client *client, err = bt_mesh_net_encode(&tx, buf, true); if (err) { - BT_ERR("Encoding proxy cfg message failed (err %d)", err); + BT_ERR("Encoding proxy filter status failed (err %d)", err); return; } - err = proxy_segment_and_send(client->conn, BLE_MESH_PROXY_CONFIG, buf); + err = bt_mesh_proxy_server_segment_send(client->conn, BLE_MESH_PROXY_CONFIG, buf); if (err) { - BT_ERR("Failed to send proxy cfg message (err %d)", err); + BT_ERR("Failed to send proxy filter status (err %d)", err); } } @@ -342,42 +387,70 @@ static void proxy_cfg(struct bt_mesh_proxy_client *client) opcode = net_buf_simple_pull_u8(&buf); switch (opcode) { - case CFG_FILTER_SET: + case BLE_MESH_PROXY_CFG_FILTER_SET: filter_set(client, &buf); send_filter_status(client, &rx, &buf); break; - case CFG_FILTER_ADD: - while (buf.len >= 2) { - uint16_t addr = 0U; - addr = net_buf_simple_pull_be16(&buf); - filter_add(client, addr); + case BLE_MESH_PROXY_CFG_FILTER_ADD: + while (buf.len >= 2) { + uint16_t addr = net_buf_simple_pull_be16(&buf); + filter_add(client, addr, rx.ctx.addr == addr); } send_filter_status(client, &rx, &buf); +#if CONFIG_BLE_MESH_DF_SRV + bt_mesh_directed_proxy_server_solicitation(client, rx.sub); +#endif /* CONFIG_BLE_MESH_DF_SRV */ break; - case CFG_FILTER_REMOVE: - while (buf.len >= 2) { - uint16_t addr = 0U; - addr = net_buf_simple_pull_be16(&buf); + case BLE_MESH_PROXY_CFG_FILTER_REMOVE: + while (buf.len >= 2) { + uint16_t addr = net_buf_simple_pull_be16(&buf); filter_remove(client, addr); } send_filter_status(client, &rx, &buf); break; + +#if CONFIG_BLE_MESH_DF_SRV + case BLE_MESH_PROXY_CFG_DIRECTED_PROXY_CONTROL: + if (buf.len < 1) { + BT_WARN("Invalid Directed Proxy Control (len %d)", buf.len); + break; + } + + if (rx.sub->directed_proxy != BLE_MESH_DIRECTED_PROXY_NOT_SUPPORTED) { + bt_mesh_directed_proxy_server_directed_proxy_ctrl_recv(client, &rx, &buf); + } + break; +#endif /* CONFIG_BLE_MESH_DF_SRV */ + default: BT_WARN("Unhandled configuration OpCode 0x%02x", opcode); break; } } -static int beacon_send(struct bt_mesh_conn *conn, struct bt_mesh_subnet *sub) +static int beacon_send(struct bt_mesh_proxy_client *client, struct bt_mesh_subnet *sub) { - NET_BUF_SIMPLE_DEFINE(buf, 23); + NET_BUF_SIMPLE_DEFINE(buf, 28); net_buf_simple_reserve(&buf, 1); - bt_mesh_beacon_create(sub, &buf); - return proxy_segment_and_send(conn, BLE_MESH_PROXY_BEACON, &buf); +#if CONFIG_BLE_MESH_PROXY_PRIVACY + if (client->proxy_privacy == BLE_MESH_PROXY_PRIVACY_ENABLED) { + bt_mesh_private_beacon_create(sub, &buf); + + /* NOTE: Each time a Mesh Private beacon for a subnet is sent to a Proxy Client, + * the Random field in the Mesh Private beacon shall be regenerated. + */ + bt_mesh_private_beacon_update_random(sub); + } else +#endif + { + bt_mesh_secure_beacon_create(sub, &buf); + } + + return bt_mesh_proxy_server_segment_send(client->conn, BLE_MESH_PROXY_BEACON, &buf); } static void proxy_send_beacons(struct k_work *work) @@ -387,12 +460,56 @@ static void proxy_send_beacons(struct k_work *work) client = CONTAINER_OF(work, struct bt_mesh_proxy_client, send_beacons); + /* Upon connection, the Proxy Server shall evaluate Proxy Privacy parameter + * for the connection and the Proxy Server shall retain the value of the + * Proxy Privacy parameter for the lifetime of the connection. The Proxy + * Server shall send a mesh beacon for each known subnet to the Proxy Client. + * + * When either the GATT Proxy state or the Node Identity state is enabled, + * the Proxy Privacy parameter for the connection shall be Disabled. + * + * When both the GATT Proxy state and the Node Identity state are disabled, + * and either the Private GATT Proxy state or the Private Node Identity state + * is enabled, the Proxy Privacy parameter for the connection shall be Enabled. + */ + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; +#if 0 + if (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED || + sub->node_id == BLE_MESH_NODE_IDENTITY_RUNNING) { + sub->proxy_privacy = BLE_MESH_PROXY_PRIVACY_DISABLED; + } else if (true) { +#if CONFIG_BLE_MESH_PRB_SRV + /* TODO: Check if Private GATT Proxy or Private Node Identity is enabled */ +#endif + sub->proxy_privacy = BLE_MESH_PROXY_PRIVACY_ENABLED; + } else { + sub->proxy_privacy = BLE_MESH_PROXY_PRIVACY_NOT_SUPPORTED; + } +#endif + + if (sub->net_idx != BLE_MESH_KEY_UNUSED) { + beacon_send(client, sub); +#if CONFIG_BLE_MESH_DF_SRV + if (sub->directed_proxy != BLE_MESH_DIRECTED_PROXY_NOT_SUPPORTED) { + bt_mesh_directed_proxy_server_directed_proxy_caps_status_send(client->conn, sub); + } +#endif /* CONFIG_BLE_MESH_DF_SRV */ + } + +#if 0 if (sub->net_idx != BLE_MESH_KEY_UNUSED) { - beacon_send(client->conn, sub); + if (sub->proxy_privacy == BLE_MESH_PROXY_PRIVACY_DISABLED) { + beacon_send(client->conn, sub); + } else if (sub->proxy_privacy == BLE_MESH_PROXY_PRIVACY_ENABLED) { +#if CONFIG_BLE_MESH_PRB_SRV + /* TODO: Send Mesh Private Beacon */ +#endif + } } +#endif } } @@ -413,7 +530,7 @@ void bt_mesh_proxy_server_beacon_send(struct bt_mesh_subnet *sub) for (i = 0; i < ARRAY_SIZE(clients); i++) { if (clients[i].conn) { - beacon_send(clients[i].conn, sub); + beacon_send(&clients[i], sub); } } } @@ -437,8 +554,6 @@ int bt_mesh_proxy_identity_enable(void) { int i, count = 0; - BT_DBG("%s", __func__); - if (!bt_mesh_is_provisioned()) { return -EAGAIN; } @@ -465,12 +580,74 @@ int bt_mesh_proxy_identity_enable(void) return 0; } +#if CONFIG_BLE_MESH_PRB_SRV +void bt_mesh_proxy_server_private_identity_start(struct bt_mesh_subnet *sub) +{ + sub->private_node_id = BLE_MESH_PRIVATE_NODE_IDENTITY_RUNNING; + sub->node_id_start = k_uptime_get_32(); + + /* Prioritize the recently enabled subnet */ + next_idx = sub - bt_mesh.sub; +} + +void bt_mesh_proxy_server_private_identity_stop(struct bt_mesh_subnet *sub) +{ + sub->private_node_id = BLE_MESH_PRIVATE_NODE_IDENTITY_STOPPED; + sub->node_id_start = 0U; +} + +bool bt_mesh_proxy_server_is_node_id_enable(void) +{ + for (size_t i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + /* If the Node Identity state of the node for any subnet + * is 0x01 (i.e. running), return true. + */ + struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + + if (sub->net_idx != BLE_MESH_KEY_UNUSED && + sub->node_id == BLE_MESH_PRIVATE_NODE_IDENTITY_RUNNING) { + return true; + } + } + + return false; +} + +static bool is_exist_private_node_id_enable(void) +{ + for (size_t i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + /* If the value of the Node Identity state of the node + * for any subnet is 0x01,If exist return true. + */ + struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + + if (sub->net_idx != BLE_MESH_KEY_UNUSED && + sub->private_node_id == BLE_MESH_PRIVATE_NODE_IDENTITY_RUNNING) { + return true; + } + } + + return false; +} + +void disable_all_private_node_identity(void) +{ + for (size_t i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + /* NOTE: Set private node identity state of all valid subnets disabled */ + struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + + if (sub->net_idx != BLE_MESH_KEY_UNUSED) { + bt_mesh_proxy_server_private_identity_stop(sub); + } + } +} +#endif /* CONFIG_BLE_MESH_PRB_SRV */ #endif /* GATT_PROXY */ static void proxy_complete_pdu(struct bt_mesh_proxy_client *client) { switch (client->msg_type) { -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER case BLE_MESH_PROXY_NET_PDU: BT_DBG("Mesh Network PDU"); bt_mesh_net_recv(&client->buf, 0, BLE_MESH_NET_IF_PROXY); @@ -484,7 +661,7 @@ static void proxy_complete_pdu(struct bt_mesh_proxy_client *client) proxy_cfg(client); break; #endif -#if defined(CONFIG_BLE_MESH_PB_GATT) +#if CONFIG_BLE_MESH_PB_GATT case BLE_MESH_PROXY_PROV: BT_DBG("Mesh Provisioning PDU"); bt_mesh_pb_gatt_recv(client->conn, &client->buf); @@ -496,6 +673,31 @@ static void proxy_complete_pdu(struct bt_mesh_proxy_client *client) } net_buf_simple_reset(&client->buf); + +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + if (client->msg_type < BLE_MESH_PROXY_PROV && + client->proxy_msg_recv == false) { + client->proxy_msg_recv = true; + /** + * @Spec: P626 + * When a new connection is established between a Proxy Client and the Directed Proxy Server, and the + * first message received from the Proxy Client is a successfully processed DIRECTED_PROXY_CONTROL + * message, then the Directed Proxy Server shall set the Proxy_Client_Type parameter to Directed Proxy Client, + * shall set the Use_Directed parameter to Disable for all subnets known to the Directed Proxy Server + * except the subnet identified by the received message; + * otherwise, the Directed Proxy Server shall set the Proxy_Client_Type parameter to Proxy Client. + * + * If the first message received is DIRECTED_PROXY_CONTROL, proxy_client_type will be set to Directed Proxy Client, + * But if device didn't receive DIRECTED_PROXY_CONTROL message and all received is normal proxy message, That + * client type will be always in UNSET state, because we set client type in handle function of DIRECTED_PROXY_CONTROL. + * + * So the flowing code was used to avoid that situation. + */ + if (client->proxy_client_type == BLE_MESH_PROXY_CLI_TYPE_UNSET) { + client->proxy_client_type = BLE_MESH_PROXY_CLI_TYPE_PROXY_CLIENT; + } + } +#endif } #define ATTR_IS_PROV(attr) (attr->user_data != NULL) @@ -508,6 +710,7 @@ static ssize_t proxy_recv(struct bt_mesh_conn *conn, const uint8_t *data = buf; if (!client) { + BT_ERR("No Proxy Client found"); return -ENOTCONN; } @@ -516,7 +719,7 @@ static ssize_t proxy_recv(struct bt_mesh_conn *conn, return -EINVAL; } - if (ATTR_IS_PROV(attr) != (PDU_TYPE(data) == BLE_MESH_PROXY_PROV)) { + if (ATTR_IS_PROV(attr) != (BLE_MESH_PROXY_PDU_TYPE(data) == BLE_MESH_PROXY_PROV)) { BT_WARN("Proxy PDU type doesn't match GATT service"); return -EINVAL; } @@ -526,51 +729,51 @@ static ssize_t proxy_recv(struct bt_mesh_conn *conn, return -EINVAL; } - switch (PDU_SAR(data)) { - case SAR_COMPLETE: + switch (BLE_MESH_PROXY_PDU_SAR(data)) { + case BLE_MESH_PROXY_SAR_COMP: if (client->buf.len) { BT_WARN("Complete PDU while a pending incomplete one"); return -EINVAL; } - client->msg_type = PDU_TYPE(data); + client->msg_type = BLE_MESH_PROXY_PDU_TYPE(data); net_buf_simple_add_mem(&client->buf, data + 1, len - 1); proxy_complete_pdu(client); break; - case SAR_FIRST: + case BLE_MESH_PROXY_SAR_FIRST: if (client->buf.len) { BT_WARN("First PDU while a pending incomplete one"); return -EINVAL; } - k_delayed_work_submit(&client->sar_timer, PROXY_SAR_TIMEOUT); - client->msg_type = PDU_TYPE(data); + k_delayed_work_submit(&client->sar_timer, BLE_MESH_PROXY_SAR_TIMEOUT); + client->msg_type = BLE_MESH_PROXY_PDU_TYPE(data); net_buf_simple_add_mem(&client->buf, data + 1, len - 1); break; - case SAR_CONT: + case BLE_MESH_PROXY_SAR_CONT: if (!client->buf.len) { BT_WARN("Continuation with no prior data"); return -EINVAL; } - if (client->msg_type != PDU_TYPE(data)) { + if (client->msg_type != BLE_MESH_PROXY_PDU_TYPE(data)) { BT_WARN("Unexpected message type in continuation"); return -EINVAL; } - k_delayed_work_submit(&client->sar_timer, PROXY_SAR_TIMEOUT); + k_delayed_work_submit(&client->sar_timer, BLE_MESH_PROXY_SAR_TIMEOUT); net_buf_simple_add_mem(&client->buf, data + 1, len - 1); break; - case SAR_LAST: + case BLE_MESH_PROXY_SAR_LAST: if (!client->buf.len) { BT_WARN("Last SAR PDU with no prior data"); return -EINVAL; } - if (client->msg_type != PDU_TYPE(data)) { + if (client->msg_type != BLE_MESH_PROXY_PDU_TYPE(data)) { BT_WARN("Unexpected message type in last SAR PDU"); return -EINVAL; } @@ -598,6 +801,14 @@ static void proxy_connected(struct bt_mesh_conn *conn, uint8_t err) /* Since we use ADV_OPT_ONE_TIME */ proxy_adv_enabled = false; +#if CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX + /* Before re-enabling advertising, stop advertising + * the Service Data associated with the Private + * Network Identity type. + */ + bt_mesh_proxy_server_stop_solic_adv_priv_net_id(); +#endif + /* Try to re-enable advertising in case it's possible */ if (conn_count < BLE_MESH_MAX_CONN) { bt_mesh_adv_update(); @@ -616,8 +827,8 @@ static void proxy_connected(struct bt_mesh_conn *conn, uint8_t err) } client->conn = bt_mesh_conn_ref(conn); - client->filter_type = NONE; -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + client->filter_type = SRV_NONE; +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER (void)memset(client->filter, 0, sizeof(client->filter)); if (proxy_server_connect_cb) { @@ -645,10 +856,18 @@ static void proxy_disconnected(struct bt_mesh_conn *conn, uint8_t reason) } #endif if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && - client->filter_type == PROV) { - bt_mesh_pb_gatt_close(conn); + client->filter_type == SRV_PROV) { + bt_mesh_pb_gatt_close(conn, reason); } +#if CONFIG_BLE_MESH_PROXY_PRIVACY + client->proxy_privacy = BLE_MESH_PROXY_PRIVACY_DISABLED; +#endif + +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV + k_delayed_work_cancel(&rand_upd_timer); +#endif + k_delayed_work_cancel(&client->sar_timer); bt_mesh_conn_unref(client->conn); client->conn = NULL; @@ -657,6 +876,17 @@ static void proxy_disconnected(struct bt_mesh_conn *conn, uint8_t reason) } bt_mesh_adv_update(); + +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_DF_SRV + if (i == ARRAY_SIZE(clients)) { + BT_WARN("Proxy disconnected, but no connection found"); + return; + } + + if (bt_mesh_directed_proxy_server_update_dep_node(NULL, &clients[i], 0)) { + BT_ERR("Proxy disconnected, failed to update dependent node"); + } +#endif /* CONFIG_BLE_MESH_DF_SRV */ } struct net_buf_simple *bt_mesh_proxy_server_get_buf(void) @@ -668,7 +898,7 @@ struct net_buf_simple *bt_mesh_proxy_server_get_buf(void) return buf; } -#if defined(CONFIG_BLE_MESH_PB_GATT) +#if CONFIG_BLE_MESH_PB_GATT static ssize_t prov_ccc_write(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, const void *buf, uint16_t len, @@ -696,8 +926,8 @@ static ssize_t prov_ccc_write(struct bt_mesh_conn *conn, return 0; } - if (client->filter_type == NONE) { - client->filter_type = PROV; + if (client->filter_type == SRV_NONE) { + client->filter_type = SRV_PROV; bt_mesh_pb_gatt_open(conn); } @@ -739,8 +969,6 @@ int bt_mesh_proxy_server_prov_enable(void) { int i; - BT_DBG("%s", __func__); - if (gatt_svc == MESH_GATT_PROV) { BT_WARN("%s, Already", __func__); return -EALREADY; @@ -757,7 +985,7 @@ int bt_mesh_proxy_server_prov_enable(void) for (i = 0; i < ARRAY_SIZE(clients); i++) { if (clients[i].conn) { - clients[i].filter_type = PROV; + clients[i].filter_type = SRV_PROV; } } @@ -769,8 +997,6 @@ int bt_mesh_proxy_server_prov_disable(bool disconnect) { int i; - BT_DBG("%s", __func__); - if (gatt_svc == MESH_GATT_NONE) { BT_WARN("%s, Already", __func__); return -EALREADY; @@ -787,15 +1013,15 @@ int bt_mesh_proxy_server_prov_disable(bool disconnect) for (i = 0; i < ARRAY_SIZE(clients); i++) { struct bt_mesh_proxy_client *client = &clients[i]; - if (!client->conn || client->filter_type != PROV) { + if (!client->conn || client->filter_type != SRV_PROV) { continue; } if (disconnect) { bt_mesh_gatts_disconnect(client->conn, 0x13); } else { - bt_mesh_pb_gatt_close(client->conn); - client->filter_type = NONE; + bt_mesh_pb_gatt_close(client->conn, CLOSE_REASON_SUCCESS); + client->filter_type = SRV_NONE; } } @@ -803,10 +1029,9 @@ int bt_mesh_proxy_server_prov_disable(bool disconnect) return 0; } - #endif /* CONFIG_BLE_MESH_PB_GATT */ -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER static ssize_t proxy_ccc_write(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, const void *buf, uint16_t len, @@ -834,10 +1059,34 @@ static ssize_t proxy_ccc_write(struct bt_mesh_conn *conn, return 0; } - if (client->filter_type == NONE) { - client->filter_type = WHITELIST; - k_work_submit(&client->send_beacons); + /* Spec section 6.7 + * Upon connection, the Proxy Server shall evaluate Proxy Privacy parameter (for GATT Proxy bearer see + * Section 7.2.2.2.6) for the connection and the Proxy Server shall retain the value of the Proxy Privacy + * parameter for the lifetime of the connection. The Proxy Server shall send a mesh beacon for each known + * subnet to the Proxy Client + */ +#if CONFIG_BLE_MESH_PRB_SRV + if (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED || + bt_mesh_proxy_server_is_node_id_enable()) { + client->proxy_privacy = BLE_MESH_PROXY_PRIVACY_DISABLED; + } else if (bt_mesh_private_gatt_proxy_state_get() == BLE_MESH_PRIVATE_GATT_PROXY_ENABLED || + is_exist_private_node_id_enable()) { + client->proxy_privacy = BLE_MESH_PROXY_PRIVACY_ENABLED; } +#endif /* CONFIG_BLE_MESH_PRB_SRV */ + + if (client->filter_type == SRV_NONE) { + client->filter_type = SRV_WHITELIST; + k_delayed_work_submit(&client->send_beacons, K_MSEC(15)); + } + + if (client->proxy_client_type == 0) { + client->proxy_client_type = BLE_MESH_PROXY_CLI_TYPE_UNSET; + } + +#if CONFIG_BLE_MESH_DF_SRV + bt_mesh_directed_proxy_server_connected(client); +#endif /* CONFIG_BLE_MESH_DF_SRV */ return len; } @@ -877,8 +1126,6 @@ int bt_mesh_proxy_server_gatt_enable(void) { int i; - BT_DBG("%s", __func__); - if (gatt_svc == MESH_GATT_PROXY) { BT_WARN("%s, Already", __func__); return -EALREADY; @@ -894,7 +1141,7 @@ int bt_mesh_proxy_server_gatt_enable(void) for (i = 0; i < ARRAY_SIZE(clients); i++) { if (clients[i].conn) { - clients[i].filter_type = WHITELIST; + clients[i].filter_type = SRV_WHITELIST; } } @@ -905,14 +1152,12 @@ void bt_mesh_proxy_server_gatt_disconnect(void) { int i; - BT_DBG("%s", __func__); - for (i = 0; i < ARRAY_SIZE(clients); i++) { struct bt_mesh_proxy_client *client = &clients[i]; - if (client->conn && (client->filter_type == WHITELIST || - client->filter_type == BLACKLIST)) { - client->filter_type = NONE; + if (client->conn && (client->filter_type == SRV_WHITELIST || + client->filter_type == SRV_BLACKLIST)) { + client->filter_type = SRV_NONE; bt_mesh_gatts_disconnect(client->conn, 0x13); } } @@ -920,8 +1165,6 @@ void bt_mesh_proxy_server_gatt_disconnect(void) int bt_mesh_proxy_server_gatt_disable(void) { - BT_DBG("%s", __func__); - if (gatt_svc == MESH_GATT_NONE) { BT_WARN("%s, Already", __func__); return -EALREADY; @@ -942,14 +1185,15 @@ int bt_mesh_proxy_server_gatt_disable(void) void bt_mesh_proxy_server_addr_add(struct net_buf_simple *buf, uint16_t addr) { - struct bt_mesh_proxy_client *client = - CONTAINER_OF(buf, struct bt_mesh_proxy_client, buf); + 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); - if (client->filter_type == WHITELIST) { - filter_add(client, addr); - } else if (client->filter_type == BLACKLIST) { + if (client->filter_type == SRV_WHITELIST) { + filter_add(client, addr, true); + } else if (client->filter_type == SRV_BLACKLIST) { filter_remove(client, addr); } } @@ -961,9 +1205,9 @@ static bool client_filter_match(struct bt_mesh_proxy_client *client, BT_DBG("filter_type %u addr 0x%04x", client->filter_type, addr); - if (client->filter_type == BLACKLIST) { + if (client->filter_type == SRV_BLACKLIST) { for (i = 0; i < ARRAY_SIZE(client->filter); i++) { - if (client->filter[i] == addr) { + if (client->filter[i].addr == addr) { return false; } } @@ -975,9 +1219,9 @@ static bool client_filter_match(struct bt_mesh_proxy_client *client, return true; } - if (client->filter_type == WHITELIST) { + if (client->filter_type == SRV_WHITELIST) { for (i = 0; i < ARRAY_SIZE(client->filter); i++) { - if (client->filter[i] == addr) { + if (client->filter[i].addr == addr) { return true; } } @@ -1024,13 +1268,13 @@ static int proxy_send(struct bt_mesh_conn *conn, const void *data, uint16_t len) { BT_DBG("%u bytes: %s", len, bt_hex(data, len)); -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER if (gatt_svc == MESH_GATT_PROXY) { return bt_mesh_gatts_notify(conn, &proxy_attrs[4], data, len); } #endif -#if defined(CONFIG_BLE_MESH_PB_GATT) +#if CONFIG_BLE_MESH_PB_GATT if (gatt_svc == MESH_GATT_PROV) { return bt_mesh_gatts_notify(conn, &prov_attrs[4], data, len); } @@ -1039,8 +1283,8 @@ static int proxy_send(struct bt_mesh_conn *conn, const void *data, uint16_t len) return 0; } -static int proxy_segment_and_send(struct bt_mesh_conn *conn, uint8_t type, - struct net_buf_simple *msg) +int bt_mesh_proxy_server_segment_send(struct bt_mesh_conn *conn, uint8_t type, + struct net_buf_simple *msg) { uint16_t mtu = 0U; @@ -1050,22 +1294,22 @@ static int proxy_segment_and_send(struct bt_mesh_conn *conn, uint8_t type, /* ATT_MTU - OpCode (1 byte) - Handle (2 bytes) */ mtu = bt_mesh_gatt_get_mtu(conn) - 3; if (mtu > msg->len) { - net_buf_simple_push_u8(msg, PDU_HDR(SAR_COMPLETE, type)); + net_buf_simple_push_u8(msg, BLE_MESH_PROXY_PDU_HDR(BLE_MESH_PROXY_SAR_COMP, type)); return proxy_send(conn, msg->data, msg->len); } - net_buf_simple_push_u8(msg, PDU_HDR(SAR_FIRST, type)); + net_buf_simple_push_u8(msg, BLE_MESH_PROXY_PDU_HDR(BLE_MESH_PROXY_SAR_FIRST, type)); proxy_send(conn, msg->data, mtu); net_buf_simple_pull(msg, mtu); while (msg->len) { if (msg->len + 1 < mtu) { - net_buf_simple_push_u8(msg, PDU_HDR(SAR_LAST, type)); + net_buf_simple_push_u8(msg, BLE_MESH_PROXY_PDU_HDR(BLE_MESH_PROXY_SAR_LAST, type)); proxy_send(conn, msg->data, msg->len); break; } - net_buf_simple_push_u8(msg, PDU_HDR(SAR_CONT, type)); + net_buf_simple_push_u8(msg, BLE_MESH_PROXY_PDU_HDR(BLE_MESH_PROXY_SAR_CONT, type)); proxy_send(conn, msg->data, mtu); net_buf_simple_pull(msg, mtu); } @@ -1083,15 +1327,15 @@ int bt_mesh_proxy_server_send(struct bt_mesh_conn *conn, uint8_t type, return -ENOTCONN; } - if ((client->filter_type == PROV) != (type == BLE_MESH_PROXY_PROV)) { + if ((client->filter_type == SRV_PROV) != (type == BLE_MESH_PROXY_PROV)) { BT_ERR("Invalid PDU type for Proxy Client"); return -EINVAL; } - return proxy_segment_and_send(conn, type, msg); + return bt_mesh_proxy_server_segment_send(conn, type, msg); } -#if defined(CONFIG_BLE_MESH_PB_GATT) +#if CONFIG_BLE_MESH_PB_GATT static uint8_t prov_svc_data[20] = { 0x27, 0x18, }; static const struct bt_mesh_adv_data prov_ad[] = { @@ -1101,17 +1345,21 @@ static const struct bt_mesh_adv_data prov_ad[] = { }; #endif /* PB_GATT */ -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) - -#define ID_TYPE_NET 0x00 -#define ID_TYPE_NODE 0x01 +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER -#define NODE_ID_LEN 19 -#define NET_ID_LEN 11 +#define NET_ID_LEN 11 +#define NODE_ID_LEN 19 +#define PRIVATE_NET_ID_LEN 19 +#define PRIVATE_NODE_ID_LEN 19 +#define PROXY_ADV_MAX_LEN 19 -#define NODE_ID_TIMEOUT K_SECONDS(CONFIG_BLE_MESH_NODE_ID_TIMEOUT) +#define NODE_ID_TIMEOUT K_SECONDS(CONFIG_BLE_MESH_NODE_ID_TIMEOUT) -static uint8_t proxy_svc_data[NODE_ID_LEN] = { 0x28, 0x18, }; +/* Note: + * The "proxy_svc_data" should be large enough to hold the maximum + * payload of the Node ID/Net ID/Private Node ID/Private Net ID. + */ +static uint8_t proxy_svc_data[PROXY_ADV_MAX_LEN] = { 0x28, 0x18, }; static const struct bt_mesh_adv_data node_id_ad[] = { BLE_MESH_ADV_DATA_BYTES(BLE_MESH_DATA_FLAGS, (BLE_MESH_AD_GENERAL | BLE_MESH_AD_NO_BREDR)), @@ -1125,6 +1373,20 @@ static const struct bt_mesh_adv_data net_id_ad[] = { BLE_MESH_ADV_DATA(BLE_MESH_DATA_SVC_DATA16, proxy_svc_data, NET_ID_LEN), }; +#if CONFIG_BLE_MESH_PRB_SRV +static const struct bt_mesh_adv_data private_node_id_ad[] = { + BLE_MESH_ADV_DATA_BYTES(BLE_MESH_DATA_FLAGS, (BLE_MESH_AD_GENERAL | BLE_MESH_AD_NO_BREDR)), + BLE_MESH_ADV_DATA_BYTES(BLE_MESH_DATA_UUID16_ALL, 0x28, 0x18), + BLE_MESH_ADV_DATA(BLE_MESH_DATA_SVC_DATA16, proxy_svc_data, PRIVATE_NODE_ID_LEN), +}; + +static const struct bt_mesh_adv_data private_net_id_ad[] = { + BLE_MESH_ADV_DATA_BYTES(BLE_MESH_DATA_FLAGS, (BLE_MESH_AD_GENERAL | BLE_MESH_AD_NO_BREDR)), + BLE_MESH_ADV_DATA_BYTES(BLE_MESH_DATA_UUID16_ALL, 0x28, 0x18), + BLE_MESH_ADV_DATA(BLE_MESH_DATA_SVC_DATA16, proxy_svc_data, PRIVATE_NET_ID_LEN), +}; +#endif /* CONFIG_BLE_MESH_PRB_SRV */ + static size_t gatt_proxy_adv_create(struct bt_mesh_adv_data *proxy_sd) { const char *name = device_name; @@ -1152,9 +1414,7 @@ static int node_id_adv(struct bt_mesh_subnet *sub) uint8_t tmp[16] = {0}; int err = 0; - BT_DBG("%s", __func__); - - proxy_svc_data[2] = ID_TYPE_NODE; + proxy_svc_data[2] = BLE_MESH_PROXY_ADV_NODE_ID; err = bt_mesh_rand(proxy_svc_data + 11, 8); if (err) { @@ -1191,9 +1451,7 @@ static int net_id_adv(struct bt_mesh_subnet *sub) size_t proxy_sd_len = 0U; int err = 0; - BT_DBG("%s", __func__); - - proxy_svc_data[2] = ID_TYPE_NET; + proxy_svc_data[2] = BLE_MESH_PROXY_ADV_NET_ID; BT_DBG("Advertising with NetId %s", bt_hex(sub->keys[sub->kr_flag].net_id, 8)); @@ -1213,6 +1471,132 @@ static int net_id_adv(struct bt_mesh_subnet *sub) return 0; } +#if CONFIG_BLE_MESH_PRB_SRV +void bt_mesh_proxy_server_update_net_id_rand(void) +{ + k_delayed_work_submit(&rand_upd_timer, RAND_UPDATE_INTERVAL); +} + +void bt_mesh_proxy_server_update_net_id_rand_stop(void) +{ + k_delayed_work_cancel(&rand_upd_timer); +} + +static void random_update_timeout(struct k_work *work) +{ + int err = 0; + + err = bt_mesh_rand(net_id_random, 8); + if (err) { + BT_ERR("Generate random value failed"); + return; + } + + k_delayed_work_submit(&rand_upd_timer, RAND_UPDATE_INTERVAL); +} + +static int private_node_id_adv(struct bt_mesh_subnet *sub) +{ + struct bt_mesh_adv_data proxy_sd = {0}; + size_t proxy_sd_len = 0U; + uint8_t tmp[16] = {0}; + int err = 0; + + proxy_svc_data[2] = BLE_MESH_PROXY_ADV_PRIVATE_NODE_ID; + + err = bt_mesh_rand(proxy_svc_data + 11, 8); + if (err) { + return err; + } + + memset(tmp, 0, 5); + tmp[5] = 0x03; + memcpy(tmp + 6, proxy_svc_data + 11, 8); + sys_put_be16(bt_mesh_primary_addr(), tmp + 14); + + err = bt_mesh_encrypt_be(sub->keys[sub->kr_flag].identity, tmp, tmp); + if (err) { + return err; + } + + memcpy(proxy_svc_data + 3, tmp + 8, 8); + proxy_sd_len = gatt_proxy_adv_create(&proxy_sd); + + err = bt_le_adv_start(&fast_adv_param, private_node_id_ad, + ARRAY_SIZE(private_node_id_ad), &proxy_sd, proxy_sd_len); + if (err) { + BT_WARN("Failed to advertise with Private Node ID (err %d)", err); + return err; + } + + proxy_adv_enabled = true; + + return 0; +} + +void bt_mesh_prb_pnid_adv_local_set(bool start) +{ + for (size_t i = 0U; i < ARRAY_SIZE(bt_mesh.sub); i++) { + struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + + if (sub->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + if (start) { + bt_mesh_proxy_server_private_identity_start(sub); + } else { + bt_mesh_proxy_server_private_identity_stop(sub); + } + } +} +#endif /* CONFIG_BLE_MESH_PRB_SRV */ + +#if (CONFIG_BLE_MESH_PRB_SRV || \ + CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX) +static int private_net_id_adv(struct bt_mesh_subnet *sub) +{ + struct bt_mesh_adv_data proxy_sd = {0}; + size_t proxy_sd_len = 0U; + uint8_t tmp[16] = {0}; + int err = 0; + + proxy_svc_data[2] = BLE_MESH_PROXY_ADV_PRIVATE_NET_ID; + + /* TODO: + * The Private Network Identity advertisements shall use a resolvable private + * address or a non-resolvable private address in the AdvA field of the connectable + * advertising PDU. The address used for the AdvA field shall be regenerated + * whenever the Random field is regenerated. + */ + /* The Random field should be updated every 10 minutes. */ + memcpy(proxy_svc_data + 11, net_id_random, 8); + memcpy(tmp, sub->keys[sub->kr_flag].net_id, 8); + memcpy(tmp + 8, net_id_random, 8); + + err = bt_mesh_encrypt_be(sub->keys[sub->kr_flag].identity, tmp, tmp); + if (err) { + return err; + } + + memcpy(proxy_svc_data + 3, tmp + 8, 8); /* Hash */ + + proxy_sd_len = gatt_proxy_adv_create(&proxy_sd); + + err = bt_le_adv_start(&fast_adv_param, private_net_id_ad, + ARRAY_SIZE(private_net_id_ad), &proxy_sd, proxy_sd_len); + if (err) { + BT_WARN("Failed to advertise with Private Net ID (err %d)", err); + return err; + } + + proxy_adv_enabled = true; + + return 0; +} +#endif /* (CONFIG_BLE_MESH_PRB_SRV || \ + CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX) */ + static bool advertise_subnet(struct bt_mesh_subnet *sub) { if (sub->net_idx == BLE_MESH_KEY_UNUSED) { @@ -1220,7 +1604,12 @@ static bool advertise_subnet(struct bt_mesh_subnet *sub) } return (sub->node_id == BLE_MESH_NODE_IDENTITY_RUNNING || - bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_NOT_SUPPORTED); + bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED +#if CONFIG_BLE_MESH_PRB_SRV + || sub->private_node_id == BLE_MESH_PRIVATE_NODE_IDENTITY_RUNNING || + bt_mesh_private_gatt_proxy_state_get() == BLE_MESH_PRIVATE_GATT_PROXY_ENABLED +#endif + ); } static struct bt_mesh_subnet *next_sub(void) @@ -1236,8 +1625,7 @@ static struct bt_mesh_subnet *next_sub(void) } } - /** - * If among [next_idx, ARRAY_SIZE(bt_mesh.sub)], there is no subnet + /* If among [next_idx, ARRAY_SIZE(bt_mesh.sub)], there is no subnet * to advertise, then try to start advertising from Primary subnet. */ for (i = 0; i < next_idx; i++) { @@ -1270,11 +1658,10 @@ static int32_t gatt_proxy_advertise(struct bt_mesh_subnet *sub) { int32_t remaining = K_FOREVER; int subnet_count = 0; - - BT_DBG("%s", __func__); + uint32_t active = 0U; if (conn_count == BLE_MESH_MAX_CONN) { - BT_WARN("Connectable advertising deferred (max connections)"); + BT_WARN("Connectable advertising deferred (max connections %d)", conn_count); return remaining; } @@ -1284,25 +1671,50 @@ static int32_t gatt_proxy_advertise(struct bt_mesh_subnet *sub) } if (sub->node_id == BLE_MESH_NODE_IDENTITY_RUNNING) { - uint32_t active = k_uptime_get_32() - sub->node_id_start; + active = k_uptime_get_32() - sub->node_id_start; if (active < NODE_ID_TIMEOUT) { remaining = NODE_ID_TIMEOUT - active; - BT_DBG("Node ID active for %u ms, %d ms remaining", - active, remaining); + BT_DBG("Node ID active for %u ms, %d ms remaining", active, remaining); node_id_adv(sub); } else { bt_mesh_proxy_server_identity_stop(sub); BT_DBG("Node ID stopped"); } } +#if CONFIG_BLE_MESH_PRB_SRV + else if (sub->private_node_id == BLE_MESH_PRIVATE_NODE_IDENTITY_RUNNING) { + active = k_uptime_get_32() - sub->node_id_start; - if (sub->node_id == BLE_MESH_NODE_IDENTITY_STOPPED) { - net_id_adv(sub); + if (active < NODE_ID_TIMEOUT) { + remaining = NODE_ID_TIMEOUT - active; + BT_DBG("Private Node ID active for %u ms, %d ms remaining", active, remaining); + private_node_id_adv(sub); + } else { + bt_mesh_proxy_server_private_identity_stop(sub); + BT_DBG("Private Node ID stopped"); + } + } +#endif /* CONFIG_BLE_MESH_PRB_SRV */ + + if (sub->node_id == BLE_MESH_NODE_IDENTITY_STOPPED +#if CONFIG_BLE_MESH_PRB_SRV + && sub->private_node_id == BLE_MESH_PRIVATE_NODE_IDENTITY_STOPPED +#endif + ) { + if (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED) { + net_id_adv(sub); + } +#if CONFIG_BLE_MESH_PRB_SRV + else if (bt_mesh_private_gatt_proxy_state_get() == BLE_MESH_PRIVATE_GATT_PROXY_ENABLED) { + private_net_id_adv(sub); + } +#endif } subnet_count = sub_count(); BT_DBG("sub_count %u", subnet_count); + if (subnet_count > 1) { int32_t max_timeout = 0; @@ -1325,20 +1737,24 @@ static int32_t gatt_proxy_advertise(struct bt_mesh_subnet *sub) } #endif /* GATT_PROXY */ -#if defined(CONFIG_BLE_MESH_PB_GATT) +#if CONFIG_BLE_MESH_PB_GATT 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 = 0U; size_t sd_space = 31U; - memcpy(prov_svc_data + 2, prov->uuid, 16); - sys_put_be16(prov->oob_info, prov_svc_data + 18); + if (bt_mesh_prov_get() == NULL) { + BT_ERR("No provisioning context provided"); + return 0; + } + + memcpy(prov_svc_data + 2, bt_mesh_prov_get()->uuid, 16); + sys_put_be16(bt_mesh_prov_get()->oob_info, prov_svc_data + 18); - if (prov->uri) { - size_t uri_len = strlen(prov->uri); + if (bt_mesh_prov_get()->uri) { + size_t uri_len = strlen(bt_mesh_prov_get()->uri); if (uri_len > 29) { /* There's no way to shorten an URI */ @@ -1346,7 +1762,7 @@ static size_t gatt_prov_adv_create(struct bt_mesh_adv_data prov_sd[2]) } else { prov_sd[0].type = BLE_MESH_DATA_URI; prov_sd[0].data_len = uri_len; - prov_sd[0].data = (const uint8_t *)prov->uri; + prov_sd[0].data = (const uint8_t *)bt_mesh_prov_get()->uri; sd_space -= 2 + uri_len; prov_sd_len++; } @@ -1371,15 +1787,40 @@ static size_t gatt_prov_adv_create(struct bt_mesh_adv_data prov_sd[2]) } #endif /* CONFIG_BLE_MESH_PB_GATT */ -int32_t bt_mesh_proxy_server_adv_start(void) +#if CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX +static int32_t solic_adv_private_net_id(void) { - BT_DBG("%s", __func__); + uint16_t net_idx = BLE_MESH_KEY_UNUSED; + struct bt_mesh_subnet *sub = NULL; + int32_t remaining = 0; + + remaining = bt_mesh_proxy_server_get_solic_adv_remaining(); + if (remaining == 0) { + return 0; + } + + net_idx = bt_mesh_proxy_server_get_solic_adv_net_idx(); + if (net_idx == BLE_MESH_KEY_UNUSED) { + return 0; + } + + sub = bt_mesh_subnet_get(net_idx); + if (sub == NULL) { + return 0; + } + + private_net_id_adv(sub); + return remaining; +} +#endif /* CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX */ +int32_t bt_mesh_proxy_server_adv_start(void) +{ if (gatt_svc == MESH_GATT_NONE) { return K_FOREVER; } -#if defined(CONFIG_BLE_MESH_PB_GATT) +#if CONFIG_BLE_MESH_PB_GATT if (prov_fast_adv) { prov_start_time = k_uptime_get_32(); } @@ -1410,8 +1851,21 @@ int32_t bt_mesh_proxy_server_adv_start(void) } #endif /* PB_GATT */ -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER if (bt_mesh_is_provisioned()) { +#if CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX + /* When the server starts advertising with Private Network Identity + * as a result of the reception of a Solicitation PDU, the server + * shall only advertise the subnet corresponding to the network key + * that is used to authenticate the Solicitation PDU; otherwise, the + * server shall interleave the advertising of each of its subnets. + */ + int32_t remaining = solic_adv_private_net_id(); + if (remaining) { + return remaining; + } +#endif /* CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX */ + return gatt_proxy_advertise(next_sub()); } #endif /* GATT_PROXY */ @@ -1446,11 +1900,11 @@ int bt_mesh_proxy_server_init(void) { int i; -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER bt_mesh_gatts_service_register(&proxy_svc); #endif -#if defined(CONFIG_BLE_MESH_PB_GATT) +#if CONFIG_BLE_MESH_PB_GATT bt_mesh_gatts_service_register(&prov_svc); #endif @@ -1458,14 +1912,21 @@ int bt_mesh_proxy_server_init(void) for (i = 0; i < ARRAY_SIZE(clients); i++) { struct bt_mesh_proxy_client *client = &clients[i]; - client->buf.size = CLIENT_BUF_SIZE; - client->buf.__buf = client_buf_data + (i * CLIENT_BUF_SIZE); + client->buf.size = BLE_MESH_PROXY_BUF_SIZE; + client->buf.__buf = client_buf_data + (i * BLE_MESH_PROXY_BUF_SIZE); #if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) - k_work_init(&client->send_beacons, proxy_send_beacons); + k_delayed_work_init(&client->send_beacons, proxy_send_beacons); #endif k_delayed_work_init(&client->sar_timer, proxy_sar_timeout); } +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV + if (k_delayed_work_init(&rand_upd_timer, random_update_timeout)) { + BT_ERR("Failed to create a random update timer"); + return -EIO; + } +#endif + bt_mesh_gatts_conn_cb_register(&conn_callbacks); strncpy(device_name, "ESP-BLE-MESH", DEVICE_NAME_SIZE); @@ -1480,21 +1941,28 @@ int bt_mesh_proxy_server_deinit(void) proxy_adv_enabled = false; gatt_svc = MESH_GATT_NONE; -#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER bt_mesh_gatts_service_deregister(&proxy_svc); next_idx = 0; #endif -#if defined(CONFIG_BLE_MESH_PB_GATT) +#if 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]; +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + k_delayed_work_free(&client->send_beacons); +#endif k_delayed_work_free(&client->sar_timer); memset(client, 0, sizeof(struct bt_mesh_proxy_client)); } +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV + k_delayed_work_free(&rand_upd_timer); +#endif + memset(client_buf_data, 0, sizeof(client_buf_data)); memset(device_name, 0, sizeof(device_name)); @@ -1505,4 +1973,5 @@ int bt_mesh_proxy_server_deinit(void) } #endif /* CONFIG_BLE_MESH_DEINIT */ -#endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +#endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER */ diff --git a/components/bt/esp_ble_mesh/core/proxy_server.h b/components/bt/esp_ble_mesh/core/proxy_server.h index 0de68884605a..5ae0a081268c 100644 --- a/components/bt/esp_ble_mesh/core/proxy_server.h +++ b/components/bt/esp_ble_mesh/core/proxy_server.h @@ -7,8 +7,8 @@ * SPDX-License-Identifier: Apache-2.0 */ -#ifndef _PROXY_H_ -#define _PROXY_H_ +#ifndef _PROXY_SERVER_H_ +#define _PROXY_SERVER_H_ #include "net.h" #include "mesh/adapter.h" @@ -17,11 +17,6 @@ extern "C" { #endif -#define BLE_MESH_PROXY_NET_PDU 0x00 -#define BLE_MESH_PROXY_BEACON 0x01 -#define BLE_MESH_PROXY_CONFIG 0x02 -#define BLE_MESH_PROXY_PROV 0x03 - #if CONFIG_BLE_MESH_PROXY /** * Device Name Characteristic: @@ -38,11 +33,48 @@ extern "C" { #define DEVICE_NAME_SIZE (BLE_MESH_GAP_ADV_MAX_LEN - 2) #endif +struct bt_mesh_proxy_client { + struct bt_mesh_conn *conn; + + enum __attribute__((packed)) { + SRV_NONE, + SRV_WHITELIST, + SRV_BLACKLIST, + SRV_PROV, + } filter_type; + + uint8_t msg_type; + +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + struct { + uint16_t addr; + bool proxy_client; /* Indicate if the address is the element address of Proxy Client. */ + } filter[CONFIG_BLE_MESH_PROXY_FILTER_SIZE]; + struct k_delayed_work send_beacons; + + uint8_t proxy_client_type; + uint8_t proxy_msg_recv : 1; /* Indicate if proxy server has received a message from proxy client */ + +#if CONFIG_BLE_MESH_PROXY_PRIVACY + uint8_t proxy_privacy; +#endif +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ + + struct k_delayed_work sar_timer; + + struct net_buf_simple buf; +}; + typedef void (*proxy_server_connect_cb_t)(uint8_t conn_handle); typedef void (*proxy_server_disconnect_cb_t)(uint8_t conn_handle, uint8_t reason); int bt_mesh_set_device_name(const char *name); +const char *bt_mesh_get_device_name(void); + +int bt_mesh_proxy_server_segment_send(struct bt_mesh_conn *conn, uint8_t type, + struct net_buf_simple *msg); + int bt_mesh_proxy_server_send(struct bt_mesh_conn *conn, uint8_t type, struct net_buf_simple *msg); @@ -64,6 +96,22 @@ struct net_buf_simple *bt_mesh_proxy_server_get_buf(void); int32_t bt_mesh_proxy_server_adv_start(void); void bt_mesh_proxy_server_adv_stop(void); +void bt_mesh_proxy_server_update_net_id_rand(void); +void bt_mesh_proxy_server_update_net_id_rand_stop(void); + +#if CONFIG_BLE_MESH_PRB_SRV +void bt_mesh_proxy_server_private_identity_start(struct bt_mesh_subnet *sub); +void bt_mesh_proxy_server_private_identity_stop(struct bt_mesh_subnet *sub); + +void bt_mesh_disable_private_gatt_proxy(void); + +bool bt_mesh_proxy_server_is_node_id_enable(void); + +void disable_all_private_node_identity(void); + +void bt_mesh_prb_pnid_adv_local_set(bool start); +#endif /* CONFIG_BLE_MESH_PRB_SRV */ + void bt_mesh_proxy_server_identity_start(struct bt_mesh_subnet *sub); void bt_mesh_proxy_server_identity_stop(struct bt_mesh_subnet *sub); @@ -73,8 +121,11 @@ void bt_mesh_proxy_server_addr_add(struct net_buf_simple *buf, uint16_t addr); int bt_mesh_proxy_server_init(void); int bt_mesh_proxy_server_deinit(void); +bool bt_mesh_proxy_server_find_client_by_addr(uint16_t addr); +uint8_t bt_mesh_proxy_server_get_all_client_type(void); + #ifdef __cplusplus } #endif -#endif /* _PROXY_H_ */ +#endif /* _PROXY_SERVER_H_ */ diff --git a/components/bt/esp_ble_mesh/core/pvnr_mgmt.c b/components/bt/esp_ble_mesh/core/pvnr_mgmt.c index da3530d745bc..55d18ad51e2a 100644 --- a/components/bt/esp_ble_mesh/core/pvnr_mgmt.c +++ b/components/bt/esp_ble_mesh/core/pvnr_mgmt.c @@ -10,6 +10,7 @@ #include "mesh.h" #include "crypto.h" #include "adv.h" +#include "rpl.h" #include "access.h" #include "settings.h" #include "friend.h" @@ -17,9 +18,12 @@ #include "foundation.h" #include "mesh/common.h" #include "proxy_client.h" +#include "prov_common.h" #include "prov_pvnr.h" #include "pvnr_mgmt.h" +#include "mesh_v1.1/utils.h" + #if CONFIG_BLE_MESH_PROVISIONER static struct bt_mesh_node *mesh_nodes[CONFIG_BLE_MESH_MAX_PROV_NODES]; @@ -66,14 +70,10 @@ int bt_mesh_provisioner_init(void) */ int bt_mesh_provisioner_net_create(void) { - const struct bt_mesh_prov *prov = NULL; struct bt_mesh_subnet *sub = NULL; uint8_t p_key[16] = {0}; - BT_DBG("%s", __func__); - - prov = bt_mesh_provisioner_get_prov_info(); - if (!prov) { + if (bt_mesh_prov_get() == NULL) { BT_ERR("No provisioning context provided"); return -EINVAL; } @@ -99,7 +99,7 @@ int bt_mesh_provisioner_net_create(void) return -ENOMEM; } - sub->kr_flag = BLE_MESH_KEY_REFRESH(prov->flags); + sub->kr_flag = BLE_MESH_KEY_REFRESH(bt_mesh_prov_get()->flags); if (sub->kr_flag) { if (bt_mesh_net_keys_create(&sub->keys[1], p_key)) { BT_ERR("Failed to generate net-related keys"); @@ -119,6 +119,10 @@ int bt_mesh_provisioner_net_create(void) sub->net_idx = BLE_MESH_KEY_PRIMARY; sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; +#if CONFIG_BLE_MESH_DF_SRV + bt_mesh_directed_forwarding_sub_init(sub); +#endif + bt_mesh.p_sub[0] = sub; /* Dynamically added appkey & netkey will use these key_idx */ @@ -131,9 +135,9 @@ int bt_mesh_provisioner_net_create(void) bt_mesh_store_p_subnet(bt_mesh.p_sub[0]); } - bt_mesh.iv_index = prov->iv_index; + bt_mesh.iv_index = bt_mesh_prov_get()->iv_index; bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS, - BLE_MESH_IV_UPDATE(prov->flags)); + BLE_MESH_IV_UPDATE(bt_mesh_prov_get()->flags)); /* Set minimum required hours, since the 96-hour minimum requirement * doesn't apply straight after provisioning (since we can't know how @@ -311,12 +315,10 @@ int bt_mesh_provisioner_provision(const bt_mesh_addr_t *addr, const uint8_t uuid uint16_t oob_info, uint16_t unicast_addr, uint8_t element_num, uint16_t net_idx, uint8_t flags, uint32_t iv_index, - const uint8_t dev_key[16], uint16_t *index) + const uint8_t dev_key[16], uint16_t *index, bool nppi) { struct bt_mesh_node node = {0}; - BT_DBG("%s", __func__); - if (!addr || !uuid || !dev_key || !index) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; @@ -327,6 +329,11 @@ int bt_mesh_provisioner_provision(const bt_mesh_addr_t *addr, const uint8_t uuid BT_INFO("UUID %s", bt_hex(uuid, 16)); BT_INFO("DevKey %s", bt_hex(dev_key, 16)); + if (nppi == true) { + /* For NPPI, remove the node from database firstly. */ + bt_mesh_provisioner_remove_node(uuid); + } + memcpy(node.addr, addr->val, BLE_MESH_ADDR_LEN); node.addr_type = addr->type; memcpy(node.dev_uuid, uuid, 16); @@ -367,6 +374,9 @@ static int provisioner_remove_node(uint16_t index, bool erase) for (i = 0; i < node->element_num; i++) { bt_mesh_tx_reset_single(node->unicast_addr + i); } + for (i = 0; i < node->element_num; i++) { + bt_mesh_rpl_reset_single(node->unicast_addr + i, erase); + } if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { bt_mesh_friend_remove_lpn(node->unicast_addr); @@ -392,8 +402,6 @@ static struct bt_mesh_node *provisioner_find_node_with_uuid(const uint8_t uuid[1 { int i; - BT_DBG("%s", __func__); - if (uuid == NULL) { BT_ERR("Invalid device uuid"); return NULL; @@ -445,8 +453,6 @@ static struct bt_mesh_node *provisioner_find_node_with_addr(uint16_t addr, uint1 struct bt_mesh_node *node = NULL; int i; - BT_DBG("%s", __func__); - if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { BT_ERR("Invalid unicast address 0x%04x", addr); return NULL; @@ -711,37 +717,14 @@ const uint8_t *bt_mesh_provisioner_net_key_get(uint16_t net_idx) struct bt_mesh_subnet *sub = NULL; int i; - BT_DBG("%s", __func__); - for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { sub = bt_mesh.p_sub[i]; if (sub && sub->net_idx == net_idx) { if (sub->kr_flag) { return sub->keys[1].net; - } else { - return sub->keys[0].net; } - } - } - - return NULL; -} - -struct bt_mesh_subnet *bt_mesh_provisioner_subnet_get(uint16_t net_idx) -{ - struct bt_mesh_subnet *sub = NULL; - int i; - - BT_DBG("%s", __func__); - if (net_idx == BLE_MESH_KEY_ANY) { - return bt_mesh.p_sub[0]; - } - - 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; + return sub->keys[0].net; } } @@ -753,8 +736,6 @@ bool bt_mesh_provisioner_check_msg_dst(uint16_t dst) struct bt_mesh_node *node = NULL; int i; - BT_DBG("%s", __func__); - if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { return true; } @@ -770,44 +751,24 @@ bool bt_mesh_provisioner_check_msg_dst(uint16_t dst) return false; } -const uint8_t *bt_mesh_provisioner_dev_key_get(uint16_t dst) +const uint8_t *bt_mesh_provisioner_dev_key_get(uint16_t addr) { /* 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 *node = NULL; + * Configuration model shall only be supported by the primary + * element which uses the primary unicast address. + */ int i; - BT_DBG("%s", __func__); - - if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { - BT_ERR("Invalid unicast address 0x%04x", dst); + if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { + BT_ERR("Invalid unicast address 0x%04x", addr); return NULL; } for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { - node = mesh_nodes[i]; - if (node && node->unicast_addr == dst) { - return node->dev_key; - } - } - - return NULL; -} - -struct bt_mesh_app_key *bt_mesh_provisioner_app_key_find(uint16_t app_idx) -{ - struct bt_mesh_app_key *key = NULL; - int i; - - BT_DBG("%s", __func__); + struct bt_mesh_node *node = mesh_nodes[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 != BLE_MESH_KEY_UNUSED && - key->app_idx == app_idx) { - return key; + if (node && node->unicast_addr == addr) { + return node->dev_key; } } @@ -1054,9 +1015,9 @@ int bt_mesh_provisioner_local_app_key_update(const uint8_t app_key[16], return -ENODEV; } - key = bt_mesh_provisioner_app_key_find(app_idx); + key = bt_mesh_app_key_get(app_idx); if (key == NULL) { - BT_ERR("Invalid AppKeyIndex 0x%04x", app_idx); + BT_ERR("AppKey 0x%04x not found", app_idx); return -ENODEV; } @@ -1084,8 +1045,6 @@ const uint8_t *bt_mesh_provisioner_local_app_key_get(uint16_t net_idx, uint16_t struct bt_mesh_app_key *key = NULL; int i; - BT_DBG("%s", __func__); - if (provisioner_check_net_idx(net_idx, false)) { BT_ERR("Invalid NetKeyIndex 0x%04x", net_idx); return NULL; @@ -1135,8 +1094,6 @@ static void model_pub_clear(struct bt_mesh_model *model, bool store) if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { bt_mesh_store_mod_pub(model); } - - return; } static void model_unbind(struct bt_mesh_model *model, uint16_t app_idx, bool store) @@ -1179,8 +1136,6 @@ int bt_mesh_provisioner_local_app_key_del(uint16_t net_idx, uint16_t app_idx, bo struct bt_mesh_app_key *key = NULL; int i; - BT_DBG("%s", __func__); - if (provisioner_check_net_idx(net_idx, false)) { BT_ERR("Invalid NetKeyIndex 0x%04x", net_idx); return -ENODEV; @@ -1290,6 +1245,10 @@ int bt_mesh_provisioner_local_net_key_add(const uint8_t net_key[16], uint16_t *n sub->kr_flag = false; sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; +#if CONFIG_BLE_MESH_DF_SRV + bt_mesh_directed_forwarding_sub_init(sub); +#endif + bt_mesh.p_sub[add] = sub; if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { @@ -1312,9 +1271,9 @@ int bt_mesh_provisioner_local_net_key_update(const uint8_t net_key[16], uint16_t BT_INFO("NetKey %s, net_idx 0x%03x", bt_hex(net_key, 16), net_idx); - sub = bt_mesh_provisioner_subnet_get(net_idx); + sub = bt_mesh_subnet_get(net_idx); if (sub == NULL) { - BT_ERR("Invalid NetKeyIndex 0x%04x", net_idx); + BT_ERR("NetKey 0x%04x not found", net_idx); return -ENODEV; } @@ -1331,7 +1290,7 @@ int bt_mesh_provisioner_local_net_key_update(const uint8_t net_key[16], uint16_t sub->kr_flag = false; sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; - err = bt_mesh_net_beacon_update(sub); + err = bt_mesh_net_secure_beacon_update(sub); if (err) { BT_ERR("Failed to update secure beacon"); return -EIO; @@ -1344,38 +1303,11 @@ int bt_mesh_provisioner_local_net_key_update(const uint8_t net_key[16], uint16_t return 0; } -const uint8_t *bt_mesh_provisioner_local_net_key_get(uint16_t net_idx) -{ - struct bt_mesh_subnet *sub = NULL; - int i; - - BT_DBG("%s", __func__); - - if (provisioner_check_net_idx(net_idx, false)) { - BT_ERR("Invalid NetKeyIndex 0x%04x", net_idx); - return NULL; - } - - for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { - sub = bt_mesh.p_sub[i]; - if (sub && sub->net_idx == net_idx) { - if (sub->kr_flag) { - return sub->keys[1].net; - } - return sub->keys[0].net; - } - } - - return NULL; -} - int bt_mesh_provisioner_local_net_key_del(uint16_t net_idx, bool store) { struct bt_mesh_subnet *sub = NULL; int i, j; - BT_DBG("%s", __func__); - if (provisioner_check_net_idx(net_idx, false)) { BT_ERR("Invalid NetKeyIndex 0x%04x", net_idx); return -ENODEV; @@ -1457,42 +1389,6 @@ int bt_mesh_provisioner_bind_local_model_app_idx(uint16_t elem_addr, uint16_t mo return -ENOMEM; } -int bt_mesh_print_local_composition_data(void) -{ - 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("Invalid composition data"); - return -EINVAL; - } - - 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_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_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_INFO("* vnd_model %d: id - 0x%04x, cid - 0x%04x *", j, model->vnd.id, model->vnd.company); - } - } - BT_INFO("************************************************"); - - ((void) model); - - return 0; -} - #if CONFIG_BLE_MESH_TEST_AUTO_ENTER_NETWORK int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node) { diff --git a/components/bt/esp_ble_mesh/core/pvnr_mgmt.h b/components/bt/esp_ble_mesh/core/pvnr_mgmt.h index dbfd7601f4cd..20fdc6d13028 100644 --- a/components/bt/esp_ble_mesh/core/pvnr_mgmt.h +++ b/components/bt/esp_ble_mesh/core/pvnr_mgmt.h @@ -37,7 +37,7 @@ struct bt_mesh_node { char name[BLE_MESH_NODE_NAME_SIZE + 1]; /* Node name */ uint16_t comp_length; /* Length of Composition Data */ uint8_t *comp_data; /* Value of Composition Data */ -} __packed; +} __attribute__((packed)); int bt_mesh_provisioner_init(void); @@ -57,7 +57,7 @@ int bt_mesh_provisioner_provision(const bt_mesh_addr_t *addr, const uint8_t uuid uint16_t oob_info, uint16_t unicast_addr, uint8_t element_num, uint16_t net_idx, uint8_t flags, uint32_t iv_index, - const uint8_t dev_key[16], uint16_t *index); + const uint8_t dev_key[16], uint16_t *index, bool nppi); int bt_mesh_provisioner_remove_node(const uint8_t uuid[16]); @@ -89,13 +89,9 @@ int bt_mesh_provisioner_store_node_comp_data(uint16_t addr, const uint8_t *data, const uint8_t *bt_mesh_provisioner_net_key_get(uint16_t net_idx); -struct bt_mesh_subnet *bt_mesh_provisioner_subnet_get(uint16_t net_idx); - bool bt_mesh_provisioner_check_msg_dst(uint16_t dst); -const uint8_t *bt_mesh_provisioner_dev_key_get(uint16_t dst); - -struct bt_mesh_app_key *bt_mesh_provisioner_app_key_find(uint16_t app_idx); +const uint8_t *bt_mesh_provisioner_dev_key_get(uint16_t addr); int bt_mesh_provisioner_local_app_key_add(const uint8_t app_key[16], uint16_t net_idx, uint16_t *app_idx); @@ -111,8 +107,6 @@ int bt_mesh_provisioner_local_net_key_add(const uint8_t net_key[16], uint16_t *n int bt_mesh_provisioner_local_net_key_update(const uint8_t net_key[16], uint16_t net_idx); -const uint8_t *bt_mesh_provisioner_local_net_key_get(uint16_t net_idx); - int bt_mesh_provisioner_local_net_key_del(uint16_t net_idx, bool store); /* Provisioner bind local client model with proper appkey index */ @@ -133,9 +127,6 @@ void bt_mesh_provisioner_heartbeat(uint16_t hb_src, uint16_t hb_dst, uint8_t init_ttl, uint8_t rx_ttl, uint8_t hops, uint16_t feat, int8_t rssi); -/* Provisioner print own element information */ -int bt_mesh_print_local_composition_data(void); - int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node); #ifdef __cplusplus diff --git a/components/bt/esp_ble_mesh/core/rpl.c b/components/bt/esp_ble_mesh/core/rpl.c new file mode 100644 index 000000000000..cbf763bd0175 --- /dev/null +++ b/components/bt/esp_ble_mesh/core/rpl.c @@ -0,0 +1,141 @@ +/* Bluetooth Mesh */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "mesh/config.h" +#include "mesh/trace.h" +#include "mesh.h" +#include "settings.h" + +void bt_mesh_update_rpl(struct bt_mesh_rpl *rpl, struct bt_mesh_net_rx *rx) +{ + rpl->src = rx->ctx.addr; + rpl->seq = rx->seq; + rpl->old_iv = rx->old_iv; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_rpl(rpl); + } +} + +/* Check the Replay Protection List for a replay attempt. If non-NULL match + * parameter is given the RPL slot is returned but it is not immediately + * updated (needed for segmented messages), whereas if a NULL match is given + * the RPL is immediately updated (used for unsegmented messages). + */ +static bool rpl_check_and_store(struct bt_mesh_net_rx *rx, struct bt_mesh_rpl **match) +{ + for (size_t i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { + struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i]; + + /* Empty slot */ + if (rpl->src == BLE_MESH_ADDR_UNASSIGNED) { + if (match) { + *match = rpl; + } else { + bt_mesh_update_rpl(rpl, rx); + } + + return false; + } + + /* Existing slot for given address */ + if (rpl->src == rx->ctx.addr) { + if (rx->old_iv && !rpl->old_iv) { + return true; + } + + if ((!rx->old_iv && rpl->old_iv) || + rpl->seq < rx->seq) { + if (match) { + *match = rpl; + } else { + bt_mesh_update_rpl(rpl, rx); + } + + return false; + } + + return true; + } + } + + BT_ERR("RPL is full!"); + return true; +} + +bool bt_mesh_rpl_check(struct bt_mesh_net_rx *rx, struct bt_mesh_rpl **match) +{ + /* Don't bother checking messages from ourselves */ + if (rx->net_if == BLE_MESH_NET_IF_LOCAL) { + return false; + } + + /* The RPL is used only for the local node */ + if (!rx->local_match) { + return false; + } + + return rpl_check_and_store(rx, match); +} + +bool bt_mesh_bridge_rpl_check(struct bt_mesh_net_rx *rx, struct bt_mesh_rpl **match) +{ + return rpl_check_and_store(rx, match); +} + +void bt_mesh_rpl_update(void) +{ + /* Discard "old old" IV Index entries from RPL and flag + * any other ones (which are valid) as old. + */ + for (size_t i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { + struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i]; + + if (rpl->src) { + if (rpl->old_iv) { + (void)memset(rpl, 0, sizeof(*rpl)); + } else { + rpl->old_iv = true; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_rpl(rpl); + } + } + } +} + +void bt_mesh_rpl_reset_single(uint16_t src, bool erase) +{ + if (!BLE_MESH_ADDR_IS_UNICAST(src)) { + return; + } + + for (size_t i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { + if (src == bt_mesh.rpl[i].src) { + memset(&bt_mesh.rpl[i], 0, sizeof(struct bt_mesh_rpl)); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && erase) { + bt_mesh_clear_rpl_single(src); + } + } + } +} + +void bt_mesh_rpl_reset(bool erase) +{ + (void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl)); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && erase) { + bt_mesh_clear_rpl(); + } +} diff --git a/components/bt/esp_ble_mesh/core/rpl.h b/components/bt/esp_ble_mesh/core/rpl.h new file mode 100644 index 000000000000..ae4e49f73417 --- /dev/null +++ b/components/bt/esp_ble_mesh/core/rpl.h @@ -0,0 +1,35 @@ +/* Bluetooth Mesh */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _RPL_H_ +#define _RPL_H_ + +#include "net.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void bt_mesh_update_rpl(struct bt_mesh_rpl *rpl, struct bt_mesh_net_rx *rx); + +bool bt_mesh_rpl_check(struct bt_mesh_net_rx *rx, struct bt_mesh_rpl **match); + +bool bt_mesh_bridge_rpl_check(struct bt_mesh_net_rx *rx, struct bt_mesh_rpl **match); + +void bt_mesh_rpl_update(void); + +void bt_mesh_rpl_reset_single(uint16_t src, bool erase); + +void bt_mesh_rpl_reset(bool erase); + +#ifdef __cplusplus +} +#endif + +#endif /* _RPL_H_ */ diff --git a/components/bt/esp_ble_mesh/core/scan.c b/components/bt/esp_ble_mesh/core/scan.c index 8bfbcf8feada..e1b43dd2f631 100644 --- a/components/bt/esp_ble_mesh/core/scan.c +++ b/components/bt/esp_ble_mesh/core/scan.c @@ -9,6 +9,7 @@ #include #include +#include #include #include "btc_ble_mesh_ble.h" @@ -26,25 +27,144 @@ #include "prov_pvnr.h" #include "mesh/adapter.h" +#include "mesh_v1.1/utils.h" + /* Scan Window and Interval are equal for continuous scanning */ #define SCAN_INTERVAL 0x20 #define SCAN_WINDOW 0x20 -#define PROV_SVC_DATA_LEN 0x12 -#define PROXY_SVC_DATA_LEN_NET_ID 0x09 -#define PROXY_SVC_DATA_LEN_NODE_ID 0x11 +#define PROV_SVC_DATA_LEN 0x12 +#define PROXY_SVC_DATA_LEN_NET_ID 0x09 +#define PROXY_SVC_DATA_LEN_NODE_ID 0x11 +#define PROXY_SVC_DATA_LEN_PRIVATE_NET_ID 0x11 +#define PROXY_SVC_DATA_LEN_PRIVATE_NODE_ID 0x11 -#if CONFIG_BLE_MESH_PROVISIONER +#if (CONFIG_BLE_MESH_PROVISIONER || CONFIG_BLE_MESH_RPR_SRV) static const bt_mesh_addr_t *unprov_dev_addr; +static uint8_t current_adv_type; + +static struct { + uint8_t start_idx; + uint8_t end_idx; + uint8_t pair_num; + struct { + uint8_t uuid[16]; + uint8_t adv_type; + uint8_t addr[6]; + } info[BLE_MESH_STORE_UNPROV_INFO_MAX_NUM]; +} unprov_dev_info_fifo; + +int bt_mesh_unprov_dev_fifo_dequeue(uint8_t *uuid, uint8_t *addr) +{ + uint8_t idx = 0; + + if (unprov_dev_info_fifo.pair_num == 0) { + return 0; + } + + idx = unprov_dev_info_fifo.start_idx; + + if (uuid) { + memcpy(uuid, unprov_dev_info_fifo.info[idx].uuid, 16); + } + + if (addr) { + memcpy(addr, unprov_dev_info_fifo.info[idx].addr, 6); + } + + idx = (idx + 1) % BLE_MESH_STORE_UNPROV_INFO_MAX_NUM; + unprov_dev_info_fifo.start_idx = idx; + + unprov_dev_info_fifo.pair_num--; + return 0; +} + +int bt_mesh_unprov_dev_info_query(uint8_t uuid[16], uint8_t addr[6], + uint8_t *adv_type, uint8_t query_type) +{ + uint8_t idx = 0; + uint8_t cnt = 0; + + if (uuid == NULL || addr == NULL) { + BT_WARN("No available information to query"); + return -1; + } + + while (cnt < unprov_dev_info_fifo.pair_num) { + idx = (cnt + unprov_dev_info_fifo.start_idx) % BLE_MESH_STORE_UNPROV_INFO_MAX_NUM; + if (query_type & BLE_MESH_STORE_UNPROV_INFO_QUERY_TYPE_UUID) { + if (!memcmp(unprov_dev_info_fifo.info[idx].addr, addr, 6)) { + if (query_type & BLE_MESH_STORE_UNPROV_INFO_QUERY_TYPE_EXISTS) { + return 0; + } else { + memcpy(uuid, unprov_dev_info_fifo.info[idx].uuid, 16); + *adv_type = unprov_dev_info_fifo.info[idx].adv_type; + break; + } + } + } else { + if (!memcmp(unprov_dev_info_fifo.info[idx].uuid, uuid, 16)) { + if (query_type & BLE_MESH_STORE_UNPROV_INFO_QUERY_TYPE_EXISTS) { + return 0; + } else { + memcpy(addr, unprov_dev_info_fifo.info[idx].addr, 6); + *adv_type = unprov_dev_info_fifo.info[idx].adv_type; + break; + } + } + } + cnt++; + } + + if (cnt == unprov_dev_info_fifo.pair_num) { + BT_WARN("Didn't find info for %d", query_type); + return -1; + } + + return 0; + +} + +int bt_mesh_unprov_dev_fifo_enqueue(uint8_t uuid[16], const uint8_t addr[6], uint8_t adv_type) +{ + uint8_t idx = 0; + + if (uuid == NULL || addr == NULL) { + BT_ERR("Invalid argument %s", __func__); + return -EINVAL; + } + + if (unprov_dev_info_fifo.pair_num == BLE_MESH_STORE_UNPROV_INFO_MAX_NUM) { + bt_mesh_unprov_dev_fifo_dequeue(NULL, NULL); + } + + idx = unprov_dev_info_fifo.end_idx; + + memcpy(unprov_dev_info_fifo.info[idx].uuid, uuid, 16); + memcpy(unprov_dev_info_fifo.info[idx].addr, addr, 6); + unprov_dev_info_fifo.info[idx].adv_type = adv_type; + + idx = (idx + 1) % BLE_MESH_STORE_UNPROV_INFO_MAX_NUM; + unprov_dev_info_fifo.end_idx = idx; + unprov_dev_info_fifo.pair_num++; + return 0; +} const bt_mesh_addr_t *bt_mesh_get_unprov_dev_addr(void) { return unprov_dev_addr; } -#endif /* CONFIG_BLE_MESH_PROVISIONER */ + +uint8_t bt_mesh_get_adv_type(void) +{ + return current_adv_type; +} + +#endif /* (CONFIG_BLE_MESH_PROVISIONER || CONFIG_BLE_MESH_RPR_SRV) */ #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX static bool adv_flags_valid(struct net_buf_simple *buf) { uint8_t flags = 0U; @@ -76,20 +196,40 @@ static bool adv_service_uuid_valid(struct net_buf_simple *buf, uint16_t *uuid) BT_DBG("Received adv pkt with service UUID: %d", *uuid); if (*uuid != BLE_MESH_UUID_MESH_PROV_VAL && - *uuid != BLE_MESH_UUID_MESH_PROXY_VAL) { + *uuid != BLE_MESH_UUID_MESH_PROXY_VAL && + *uuid != BLE_MESH_UUID_MESH_PROXY_SOLIC_VAL) { return false; } + /** + * @brief In remote provisioning. + * A Node could handle the unprovisioned beacon. + * CASE: MESH/SR/RPR/SCN/BV-01-C + */ +#if CONFIG_BLE_MESH_RPR_SRV + if (*uuid == BLE_MESH_UUID_MESH_PROV_VAL && + !IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { + return false; + } +#else if (*uuid == BLE_MESH_UUID_MESH_PROV_VAL && - bt_mesh_is_provisioner_en() == false) { + (bt_mesh_is_provisioner_en() == false || + !IS_ENABLED(CONFIG_BLE_MESH_PB_GATT))) { return false; } +#endif + if (*uuid == BLE_MESH_UUID_MESH_PROXY_VAL && !IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)) { return false; } + if (*uuid == BLE_MESH_UUID_MESH_PROXY_SOLIC_VAL && + !IS_ENABLED(CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX)) { + return false; + } + return true; } @@ -122,12 +262,23 @@ static void handle_adv_service_data(struct net_buf_simple *buf, BT_DBG("Start to handle Mesh Prov Service Data"); bt_mesh_provisioner_prov_adv_recv(buf, addr, rssi); } + + if (IS_ENABLED(CONFIG_BLE_MESH_RPR_SRV) && + bt_mesh_is_provisioned()) { + const bt_mesh_addr_t *addr = bt_mesh_get_unprov_dev_addr(); + bt_mesh_unprov_dev_fifo_enqueue(buf->data, addr->val, bt_mesh_get_adv_type()); + bt_mesh_rpr_srv_unprov_beacon_recv(buf, bt_mesh_get_adv_type(), addr, rssi); + } break; #endif #if CONFIG_BLE_MESH_GATT_PROXY_CLIENT case BLE_MESH_UUID_MESH_PROXY_VAL: if (buf->len != PROXY_SVC_DATA_LEN_NET_ID && buf->len != PROXY_SVC_DATA_LEN_NODE_ID) { + /* PROXY_SVC_DATA_LEN_NODE_ID, + * PROXY_SVC_DATA_LEN_PRIVATE_NET_ID and + * PROXY_SVC_DATA_LEN_PRIVATE_NODE_ID are equal to 0x11 + */ BT_WARN("Invalid Mesh Proxy Service Data length %d", buf->len); return; } @@ -135,13 +286,25 @@ static void handle_adv_service_data(struct net_buf_simple *buf, BT_DBG("Start to handle Mesh Proxy Service Data"); bt_mesh_proxy_client_gatt_adv_recv(buf, addr, rssi); break; +#endif +#if CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX + case BLE_MESH_UUID_MESH_PROXY_SOLIC_VAL: + if (buf->len != (1 + BLE_MESH_NET_HDR_LEN + 8)) { + BT_WARN("Invalid Mesh Proxy Solic Service Data length %d", buf->len); + return; + } + + BT_DBG("Start to handle Mesh Proxy Solic Service Data"); + bt_mesh_proxy_server_solic_recv(buf, addr, rssi); + break; #endif default: break; } } #endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX */ #if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN static bool ble_scan_en; @@ -180,15 +343,35 @@ static void inline callback_ble_adv_pkt(const bt_mesh_addr_t *addr, } #endif /* CONFIG_BLE_MESH_SUPPORT_BLE_SCAN */ +#if CONFIG_BLE_MESH_RPR_SRV +static bool rpr_ext_scan_handle_adv_pkt(const bt_mesh_addr_t *addr, + uint8_t data[], uint16_t length) +{ + struct net_buf_simple buf = {0}; + bool rpr_adv = false; + + if (bt_mesh_is_provisioned() == false) { + return false; + } + + net_buf_simple_init_with_data(&buf, data, length); + bt_mesh_rpr_srv_extended_scan(&buf, addr, &rpr_adv); + + return rpr_adv; +} +#endif /* CONFIG_BLE_MESH_RPR_SRV */ + static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, int8_t rssi, uint8_t adv_type, - struct net_buf_simple *buf) + struct net_buf_simple *buf, + uint8_t scan_rsp_len) { #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX uint16_t uuid = 0U; #endif -#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN +#if (CONFIG_BLE_MESH_RPR_SRV || CONFIG_BLE_MESH_SUPPORT_BLE_SCAN) uint8_t *adv_data = buf->data; uint16_t adv_len = buf->len; #endif @@ -202,8 +385,9 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, BT_DBG("scan, len %u: %s", buf->len, bt_hex(buf->data, buf->len)); -#if CONFIG_BLE_MESH_PROVISIONER +#if (CONFIG_BLE_MESH_PROVISIONER || CONFIG_BLE_MESH_RPR_SRV) unprov_dev_addr = addr; + current_adv_type = adv_type; #endif while (buf->len > 1) { @@ -257,7 +441,8 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, bt_mesh_beacon_recv(buf, rssi); break; #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX case BLE_MESH_DATA_FLAGS: if (!adv_flags_valid(buf)) { BT_DBG("Adv Flags mismatch, ignore this adv pkt"); @@ -270,6 +455,14 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, case BLE_MESH_DATA_UUID16_ALL: if (!adv_service_uuid_valid(buf, &uuid)) { BT_DBG("Adv Service UUID mismatch, ignore this adv pkt"); +#if CONFIG_BLE_MESH_RPR_SRV + if (rpr_ext_scan_handle_adv_pkt(addr, adv_data, adv_len)) { + /* If handled as extended scan report successfully, then not + * notify to the application layer as normal BLE adv packet. + */ + return; + } +#endif #if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN callback_ble_adv_pkt(addr, adv_type, adv_data, adv_len, rssi); #endif @@ -281,6 +474,14 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, break; #endif default: +#if CONFIG_BLE_MESH_RPR_SRV + if (rpr_ext_scan_handle_adv_pkt(addr, adv_data, adv_len)) { + /* If handled as extended scan report successfully, then not + * notify to the application layer as normal BLE adv packet. + */ + return; + } +#endif #if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN callback_ble_adv_pkt(addr, adv_type, adv_data, adv_len, rssi); #endif @@ -290,6 +491,14 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, net_buf_simple_restore(buf, &state); net_buf_simple_pull(buf, len); } +#if CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_RPR_SRV_ACTIVE_SCAN + if (scan_rsp_len != 0) { + /** + * scan response is only visible for remote provisioning extend scan. + */ + rpr_ext_scan_handle_adv_pkt(addr, adv_data + adv_len, scan_rsp_len); + } +#endif /* CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_RPR_SRV_ACTIVE_SCAN */ } int bt_mesh_scan_enable(void) @@ -297,7 +506,11 @@ int bt_mesh_scan_enable(void) int err = 0; struct bt_mesh_scan_param scan_param = { +#if CONFIG_BLE_MESH_RPR_SRV_ACTIVE_SCAN + .type = BLE_MESH_SCAN_ACTIVE, +#else .type = BLE_MESH_SCAN_PASSIVE, +#endif #if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN .filter_dup = BLE_MESH_SCAN_FILTER_DUP_ENABLE, #else @@ -308,8 +521,6 @@ int bt_mesh_scan_enable(void) .scan_fil_policy = BLE_MESH_SP_ADV_ALL, }; - BT_DBG("%s", __func__); - err = bt_le_scan_start(&scan_param, bt_mesh_scan_cb); if (err && err != -EALREADY) { BT_ERR("starting scan failed (err %d)", err); @@ -323,8 +534,6 @@ int bt_mesh_scan_disable(void) { int err = 0; - BT_DBG("%s", __func__); - err = bt_le_scan_stop(); if (err && err != -EALREADY) { BT_ERR("stopping scan failed (err %d)", err); @@ -351,8 +560,6 @@ int bt_mesh_scan_with_wl_enable(void) .scan_fil_policy = BLE_MESH_SP_ADV_WL, }; - BT_DBG("%s", __func__); - err = bt_le_scan_start(&scan_param, bt_mesh_scan_cb); if (err && err != -EALREADY) { BT_ERR("starting scan failed (err %d)", err); diff --git a/components/bt/esp_ble_mesh/core/scan.h b/components/bt/esp_ble_mesh/core/scan.h index 5b6937f84d26..b7d0555c8342 100644 --- a/components/bt/esp_ble_mesh/core/scan.h +++ b/components/bt/esp_ble_mesh/core/scan.h @@ -16,7 +16,18 @@ extern "C" { #endif +#define BLE_MESH_STORE_UNPROV_INFO_MAX_NUM 10 +#define BLE_MESH_STORE_UNPROV_INFO_QUERY_TYPE_ADDR 0 +#define BLE_MESH_STORE_UNPROV_INFO_QUERY_TYPE_UUID BIT(0) +#define BLE_MESH_STORE_UNPROV_INFO_QUERY_TYPE_EXISTS BIT(1) + const bt_mesh_addr_t *bt_mesh_get_unprov_dev_addr(void); +uint8_t bt_mesh_get_adv_type(void); + +int bt_mesh_unprov_dev_fifo_enqueue(uint8_t uuid[16], const uint8_t addr[6], uint8_t adv_type); + +int bt_mesh_unprov_dev_info_query(uint8_t uuid[16], uint8_t addr[6], + uint8_t *adv_type, uint8_t query_type); int bt_mesh_scan_enable(void); diff --git a/components/bt/esp_ble_mesh/core/storage/settings.c b/components/bt/esp_ble_mesh/core/storage/settings.c index ee18e3c6b171..34a40d2d303f 100644 --- a/components/bt/esp_ble_mesh/core/storage/settings.c +++ b/components/bt/esp_ble_mesh/core/storage/settings.c @@ -22,6 +22,8 @@ #include "pvnr_mgmt.h" #include "prov_pvnr.h" +#include "mesh_v1.1/utils.h" + /* BLE Mesh NVS Key and corresponding data struct. * Note: The length of nvs key must be <= 15. * "xxxx" (2 octet) means the rpl_src, net_idx, app_idx, model_key, etc. @@ -47,6 +49,7 @@ * key: "mesh/v/xxxx/p" -> write/read to set/get VENDOR MODEL Publication * key: "mesh/vaddr" -> write/read to set/get all virtual addresses * key: "mesh/va/xxxx" -> write/read to set/get the "xxxx" virtual address + * key: "mesh/dkca" -> write/read to set/get Device Key Candidate */ #if CONFIG_BLE_MESH_SETTINGS @@ -70,12 +73,12 @@ static void store_pending(struct k_work *work); struct net_val { uint16_t primary_addr; uint8_t dev_key[16]; -} __packed; +} __attribute__((packed)); /* Sequence number storage */ struct seq_val { uint8_t val[3]; -} __packed; +} __attribute__((packed)); /* Heartbeat Publication storage */ struct hb_pub_val { @@ -103,7 +106,7 @@ struct iv_val { uint32_t iv_index; uint8_t iv_update:1, iv_duration:7; -} __packed; +} __attribute__((packed)); /* Replay Protection List storage */ struct rpl_val { @@ -116,14 +119,14 @@ struct net_key_val { uint8_t kr_flag:1, kr_phase:7; uint8_t val[2][16]; -} __packed; +} __attribute__((packed)); /* AppKey storage information */ struct app_key_val { uint16_t net_idx; bool updated; uint8_t val[2][16]; -} __packed; +} __attribute__((packed)); struct mod_pub_val { uint16_t addr; @@ -133,6 +136,9 @@ struct mod_pub_val { uint8_t period; uint8_t period_div:4, cred:1; +#if CONFIG_BLE_MESH_DF_SRV + uint8_t directed_pub_policy; /* Directed publish policy */ +#endif /* CONFIG_BLE_MESH_DF_SRV */ }; /* Virtual Address information */ @@ -140,7 +146,7 @@ struct va_val { uint16_t ref; uint16_t addr; uint8_t uuid[16]; -} __packed; +} __attribute__((packed)); /* We need this so we don't overwrite app-hardcoded values in case FCB * contains a history of changes but then has a NULL at the end. @@ -166,7 +172,7 @@ struct node_info { uint8_t flags; uint32_t iv_index; uint8_t dev_key[16]; -} __packed; +} __attribute__((packed)); static bt_mesh_mutex_t settings_lock; @@ -199,8 +205,6 @@ static int role_set(const char *name) bool exist = false; int err = 0; - BT_DBG("%s", __func__); - err = bt_mesh_load_core_settings(name, (uint8_t *)bt_mesh.flags, sizeof(bt_mesh.flags), &exist); if (err) { BT_ERR("Failed to load mesh device role"); @@ -227,8 +231,6 @@ static int net_set(const char *name) bool exist = false; int err = 0; - BT_DBG("%s", __func__); - err = bt_mesh_load_core_settings(name, (uint8_t *)&net, sizeof(net), &exist); if (err) { BT_ERR("Failed to load node net info"); @@ -250,14 +252,32 @@ static int net_set(const char *name) return 0; } +static int dkca_set(const char *name) +{ + bool exist = false; + int err = 0; + + err = bt_mesh_load_core_settings(name, bt_mesh.dev_key_ca, sizeof(bt_mesh.dev_key_ca), &exist); + if (err) { + BT_ERR("Failed to load DevKey Candidate"); + return 0; + } + + if (exist == false) { + return 0; + } + + BT_INFO("Restored DevKey Candidate %s", bt_hex(bt_mesh.dev_key_ca, 16)); + + return 0; +} + static int iv_set(const char *name) { struct iv_val iv = {0}; bool exist = false; int err = 0; - BT_DBG("%s", __func__); - err = bt_mesh_load_core_settings(name, (uint8_t *)&iv, sizeof(iv), &exist); if (err) { BT_ERR("Failed to load iv_index"); @@ -286,8 +306,6 @@ static int seq_set(const char *name) bool exist = false; int err = 0; - BT_DBG("%s", __func__); - err = bt_mesh_load_core_settings(name, (uint8_t *)&seq, sizeof(seq), &exist); if (err) { BT_ERR("Failed to load sequence number"); @@ -354,8 +372,6 @@ static int rpl_set(const char *name) int err = 0; int i; - BT_DBG("%s", __func__); - buf = bt_mesh_get_core_settings_item(name); if (!buf) { return 0; @@ -405,6 +421,19 @@ static int rpl_set(const char *name) return err; } +static struct bt_mesh_subnet *subnet_exist(uint16_t net_idx) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + if (bt_mesh.sub[i].net_idx == net_idx) { + return &bt_mesh.sub[i]; + } + } + + return NULL; +} + static struct bt_mesh_subnet *subnet_alloc(uint16_t net_idx) { int i; @@ -419,6 +448,20 @@ static struct bt_mesh_subnet *subnet_alloc(uint16_t net_idx) return NULL; } +static struct bt_mesh_app_key *appkey_exist(uint16_t app_idx) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { + if (bt_mesh.app_keys[i].net_idx != BLE_MESH_KEY_UNUSED && + bt_mesh.app_keys[i].app_idx == app_idx) { + return &bt_mesh.app_keys[i]; + } + } + + return NULL; +} + static int net_key_set(const char *name) { struct net_buf_simple *buf = NULL; @@ -430,8 +473,6 @@ static int net_key_set(const char *name) int err = 0; int i; - BT_DBG("%s", __func__); - buf = bt_mesh_get_core_settings_item(name); if (!buf) { return 0; @@ -453,7 +494,7 @@ static int net_key_set(const char *name) continue; } - sub = bt_mesh_subnet_get(net_idx); + sub = subnet_exist(net_idx); if (!sub) { sub = subnet_alloc(net_idx); if (!sub) { @@ -490,8 +531,6 @@ static int app_key_set(const char *name) int err = 0; int i; - BT_DBG("%s", __func__); - buf = bt_mesh_get_core_settings_item(name); if (!buf) { return 0; @@ -513,13 +552,13 @@ static int app_key_set(const char *name) continue; } - sub = bt_mesh_subnet_get(key.net_idx); + sub = subnet_exist(key.net_idx); if (!sub) { BT_ERR("Failed to find subnet 0x%03x", key.net_idx); continue; } - app = bt_mesh_app_key_find(app_idx); + app = appkey_exist(app_idx); if (!app) { app = bt_mesh_app_key_alloc(app_idx); if (!app) { @@ -554,8 +593,6 @@ static int hb_pub_set(const char *name) bool exist = false; int err = 0; - BT_DBG("%s", __func__); - if (!hb_pub) { BT_ERR("Invalid heartbeat publication"); return -EINVAL; @@ -600,8 +637,6 @@ static int cfg_set(const char *name) bool exist = false; int err = 0; - BT_DBG("%s", __func__); - if (!cfg) { BT_ERR("Invalid configuration"); stored_cfg.valid = false; @@ -689,7 +724,7 @@ static int model_set_pub(bool vnd, struct bt_mesh_model *model, uint16_t model_k if (!model->pub) { BT_INFO("Not support publication, model_id 0x%04x, cid 0x%04x", - vnd ? model->vnd.id : model->id, vnd ? model->vnd.company : 0xFFFF); + vnd ? model->vnd.id : model->id, vnd ? model->vnd.company : 0xFFFF); return 0; } @@ -719,6 +754,11 @@ static int model_set_pub(bool vnd, struct bt_mesh_model *model, uint16_t model_k model->pub->retransmit = pub.retransmit; model->pub->count = 0U; +#if CONFIG_BLE_MESH_DF_SRV + model->pub->directed_pub_policy = pub.directed_pub_policy; /* Directed publish policy */ + bt_mesh_power_up_create_path_origin_fsm(model); +#endif /* CONFIG_BLE_MESH_DF_SRV */ + BT_INFO("Restored Model Publication, address 0x%04x, app_idx 0x%03x", pub.addr, pub.key); return 0; @@ -732,8 +772,6 @@ static int model_set(bool vnd, const char *name) size_t length = 0U; int i; - BT_DBG("%s", __func__); - buf = bt_mesh_get_core_settings_item(name); if (!buf) { return 0; @@ -785,8 +823,6 @@ static int va_set(const char *name) int err = 0; int i; - BT_DBG("%s", __func__); - buf = bt_mesh_get_core_settings_item(name); if (!buf) { return 0; @@ -840,8 +876,6 @@ static int p_prov_set(const char *name) bool exist = false; int err = 0; - BT_DBG("%s", __func__); - err = bt_mesh_load_core_settings(name, (uint8_t *)&val, sizeof(val), &exist); if (err) { BT_ERR("Failed to load next address allocation"); @@ -866,8 +900,6 @@ static int p_net_idx_set(const char *name) bool exist = false; int err = 0; - BT_DBG("%s", __func__); - err = bt_mesh_load_core_settings(name, (uint8_t *)&net_idx, sizeof(net_idx), &exist); if (err) { BT_ERR("Failed to load next NetKeyIndex alloc"); @@ -891,8 +923,6 @@ static int p_app_idx_set(const char *name) bool exist = false; int err = 0; - BT_DBG("%s", __func__); - err = bt_mesh_load_core_settings(name, (uint8_t *)&app_idx, sizeof(app_idx), &exist); if (err) { BT_ERR("Failed to load next AppKeyIndex alloc"); @@ -910,6 +940,20 @@ static int p_app_idx_set(const char *name) return 0; } +static struct bt_mesh_subnet *p_subnet_exist(uint16_t net_idx) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + if (bt_mesh.p_sub[i] && + bt_mesh.p_sub[i]->net_idx == net_idx) { + return bt_mesh.p_sub[i]; + } + } + + return NULL; +} + static struct bt_mesh_subnet *p_subnet_alloc(void) { int i; @@ -929,6 +973,21 @@ static struct bt_mesh_subnet *p_subnet_alloc(void) return NULL; } +static struct bt_mesh_app_key *p_appkey_exist(uint16_t app_idx) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + if (bt_mesh.p_app_keys[i] && + bt_mesh.p_app_keys[i]->net_idx != BLE_MESH_KEY_UNUSED && + bt_mesh.p_app_keys[i]->app_idx == app_idx) { + return bt_mesh.p_app_keys[i]; + } + } + + return NULL; +} + static struct bt_mesh_app_key *p_appkey_alloc(void) { int i; @@ -959,8 +1018,6 @@ static int p_net_key_set(const char *name) int err = 0; int i; - BT_DBG("%s", __func__); - buf = bt_mesh_get_core_settings_item(name); if (!buf) { return 0; @@ -982,7 +1039,7 @@ static int p_net_key_set(const char *name) continue; } - sub = bt_mesh_provisioner_subnet_get(net_idx); + sub = p_subnet_exist(net_idx); if (!sub) { sub = p_subnet_alloc(); if (!sub) { @@ -1019,8 +1076,6 @@ static int p_app_key_set(const char *name) int err = 0; int i; - BT_DBG("%s", __func__); - buf = bt_mesh_get_core_settings_item(name); if (!buf) { return 0; @@ -1042,13 +1097,13 @@ static int p_app_key_set(const char *name) continue; } - sub = bt_mesh_provisioner_subnet_get(key.net_idx); + sub = p_subnet_exist(key.net_idx); if (!sub) { BT_ERR("Failed to find subnet 0x%03x", key.net_idx); continue; } - app = bt_mesh_provisioner_app_key_find(app_idx); + app = p_appkey_exist(app_idx); if (!app) { app = p_appkey_alloc(); if (!app) { @@ -1217,6 +1272,7 @@ const struct bt_mesh_setting { } settings[] = { { "mesh/role", role_set }, /* For Node & Provisioner */ { "mesh/net", net_set }, /* For Node */ + { "mesh/dkca", dkca_set }, /* For Node */ { "mesh/iv", iv_set }, /* For Node & Provisioner */ { "mesh/seq", seq_set }, /* For Node & Provisioner */ { "mesh/rpl", rpl_set }, /* For Node & Provisioner */ @@ -1258,8 +1314,6 @@ int settings_core_load(void) { int i; - 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") || @@ -1333,7 +1387,12 @@ static int subnet_init(struct bt_mesh_subnet *sub) } /* Make sure we have valid beacon data to be sent */ - bt_mesh_net_beacon_update(sub); + bt_mesh_net_secure_beacon_update(sub); + +#if CONFIG_BLE_MESH_DF_SRV + bt_mesh_directed_forwarding_sub_init(sub); + bt_mesh_recovery_directed_forwarding_table(sub); +#endif return 0; } @@ -1357,7 +1416,7 @@ int settings_core_commit(void) int err = 0; int i; -#if defined(CONFIG_BLE_MESH_NODE) +#if CONFIG_BLE_MESH_NODE if (bt_mesh_is_node()) { if (bt_mesh.sub[0].net_idx == BLE_MESH_KEY_UNUSED) { /* Nothing to do since we're not yet provisioned */ @@ -1385,7 +1444,7 @@ int settings_core_commit(void) } #endif /* CONFIG_BLE_MESH_NODE */ -#if defined(CONFIG_BLE_MESH_PROVISIONER) +#if 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) { @@ -1417,16 +1476,16 @@ int settings_core_commit(void) bt_mesh_model_foreach(commit_model, NULL); } -#if defined(CONFIG_BLE_MESH_NODE) +#if CONFIG_BLE_MESH_NODE 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) { + hb_pub->count && hb_pub->period) { BT_DBG("Starting heartbeat publication"); - k_work_submit(&hb_pub->timer.work); + k_delayed_work_submit(&hb_pub->timer, K_NO_WAIT); } cfg = bt_mesh_cfg_get(); @@ -1568,7 +1627,7 @@ static void store_pending_seq(void) void bt_mesh_store_seq(void) { if (CONFIG_BLE_MESH_SEQ_STORE_RATE && - (bt_mesh.seq % CONFIG_BLE_MESH_SEQ_STORE_RATE)) { + (bt_mesh.seq % CONFIG_BLE_MESH_SEQ_STORE_RATE)) { return; } @@ -1603,8 +1662,6 @@ static void store_rpl(struct bt_mesh_rpl *entry) if (err) { BT_ERR("Failed to add 0x%04x to mesh/rpl", entry->src); } - - return; } static void clear_rpl(void) @@ -1615,8 +1672,6 @@ static void clear_rpl(void) uint16_t src = 0U; int i; - BT_DBG("%s", __func__); - buf = bt_mesh_get_core_settings_item("mesh/rpl"); if (!buf) { bt_mesh_erase_core_settings("mesh/rpl"); @@ -1640,15 +1695,12 @@ static void clear_rpl(void) bt_mesh_erase_core_settings("mesh/rpl"); bt_mesh_free_buf(buf); - return; } static void store_pending_rpl(void) { int i; - BT_DBG("%s", __func__); - for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i]; @@ -1726,8 +1778,6 @@ static void clear_app_key(uint16_t app_idx) if (err) { BT_ERR("Failed to remove 0x%03x from mesh/appkey", app_idx); } - - return; } static void clear_net_key(uint16_t net_idx) @@ -1745,7 +1795,9 @@ static void clear_net_key(uint16_t net_idx) BT_ERR("Failed to remove 0x%03x from mesh/netkey", net_idx); } - return; +#if CONFIG_BLE_MESH_DF_SRV + bt_mesh_clear_directed_forwarding_table_data(net_idx); +#endif } static void store_net_key(struct bt_mesh_subnet *sub) @@ -1773,8 +1825,6 @@ static void store_net_key(struct bt_mesh_subnet *sub) if (err) { BT_ERR("Failed to add 0x%03x to mesh/netkey", sub->net_idx); } - - return; } static void store_app_key(struct bt_mesh_app_key *app) @@ -1799,8 +1849,6 @@ static void store_app_key(struct bt_mesh_app_key *app) if (err) { BT_ERR("Failed to add 0x%03x to mesh/appkey", app->app_idx); } - - return; } static void store_pending_keys(void) @@ -1823,7 +1871,7 @@ static void store_pending_keys(void) } else { if (update->app_key) { struct bt_mesh_app_key *key = NULL; - key = bt_mesh_app_key_find(update->key_idx); + key = bt_mesh_app_key_get(update->key_idx); if (key) { store_app_key(key); } else { @@ -1864,8 +1912,6 @@ static void store_pending_mod_bind(struct bt_mesh_model *model, bool vnd) BT_ERR("Failed to add bound key to %s, model_key 0x%04x", vnd ? "mesh/vnd" : "mesh/sig", model_key); } - - return; } static void store_pending_mod_sub(struct bt_mesh_model *model, bool vnd) @@ -1888,8 +1934,6 @@ static void store_pending_mod_sub(struct bt_mesh_model *model, bool vnd) BT_ERR("Failed to add subscription to %s, model_key 0x%04x", vnd ? "mesh/vnd" : "mesh/sig", model_key); } - - return; } static void store_pending_mod_pub(struct bt_mesh_model *model, bool vnd) @@ -1915,6 +1959,10 @@ static void store_pending_mod_pub(struct bt_mesh_model *model, bool vnd) pub.period_div = model->pub->period_div; pub.cred = model->pub->cred; +#if CONFIG_BLE_MESH_DF_SRV + pub.directed_pub_policy = model->pub->directed_pub_policy; /**< Directed publish policy */ +#endif + err = bt_mesh_save_core_settings(name, (const uint8_t *)&pub, sizeof(pub)); if (err) { BT_ERR("Failed to store %s", name); @@ -1926,8 +1974,6 @@ static void store_pending_mod_pub(struct bt_mesh_model *model, bool vnd) BT_ERR("Failed to add publication to %s, model_key 0x%04x", vnd ? "mesh/vnd" : "mesh/sig", model_key); } - - return; } static void store_pending_mod(struct bt_mesh_model *model, @@ -2041,7 +2087,7 @@ static void store_pending_va(void) } if (err) { BT_ERR("Failed to %s virtual address %s", - IS_VA_DEL(lab) ? "delete" : "store", name); + IS_VA_DEL(lab) ? "delete" : "store", name); return; } @@ -2052,7 +2098,7 @@ static void store_pending_va(void) } if (err) { BT_ERR("Failed to %s 0x%04x in mesh/vaddr", - IS_VA_DEL(lab) ? "delete" : "store", i); + IS_VA_DEL(lab) ? "delete" : "store", i); return; } @@ -2062,8 +2108,6 @@ static void store_pending_va(void) 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_is_provisioned() || bt_mesh_is_provisioner_en()) { store_pending_rpl(); @@ -2301,6 +2345,25 @@ void bt_mesh_clear_app_key(struct bt_mesh_app_key *key) schedule_store(BLE_MESH_KEYS_PENDING); } +void bt_mesh_clear_rpl_single(uint16_t src) +{ + char name[16] = {'\0'}; + int err = 0; + + if (!BLE_MESH_ADDR_IS_UNICAST(src)) { + BT_ERR("Invalid src 0x%04x", src); + return; + } + + sprintf(name, "mesh/rpl/%04x", src); + bt_mesh_erase_core_settings(name); + + err = bt_mesh_remove_core_settings_item("mesh/rpl", src); + if (err) { + BT_ERR("Failed to remove 0x%04x from mesh/rpl", src); + } +} + void bt_mesh_clear_rpl(void) { schedule_store(BLE_MESH_RPL_PENDING); @@ -2329,6 +2392,16 @@ void bt_mesh_store_label(void) schedule_store(BLE_MESH_VA_PENDING); } +void bt_mesh_store_dkca(void) +{ + bt_mesh_save_core_settings("mesh/dkca", bt_mesh.dev_key_ca, sizeof(bt_mesh.dev_key_ca)); +} + +void bt_mesh_clear_dkca(void) +{ + bt_mesh_erase_core_settings("mesh/dkca"); +} + #if CONFIG_BLE_MESH_PROVISIONER /** * key: "mesh/p_prov" -> write/read to set/get prov_ctx.curr_addr @@ -2495,25 +2568,6 @@ void bt_mesh_clear_p_app_key(uint16_t app_idx) } } -void bt_mesh_clear_rpl_single(uint16_t src) -{ - char name[16] = {'\0'}; - int err = 0; - - if (!BLE_MESH_ADDR_IS_UNICAST(src)) { - BT_ERR("Invalid src 0x%04x", src); - return; - } - - sprintf(name, "mesh/rpl/%04x", src); - bt_mesh_erase_core_settings(name); - - err = bt_mesh_remove_core_settings_item("mesh/rpl", src); - if (err) { - BT_ERR("Failed to remove 0x%04x from mesh/rpl", src); - } -} - void bt_mesh_store_node_info(struct bt_mesh_node *node) { struct node_info val = {0}; diff --git a/components/bt/esp_ble_mesh/core/storage/settings.h b/components/bt/esp_ble_mesh/core/storage/settings.h index ebf29132acfb..255eb2c34340 100644 --- a/components/bt/esp_ble_mesh/core/storage/settings.h +++ b/components/bt/esp_ble_mesh/core/storage/settings.h @@ -39,8 +39,12 @@ 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_single(uint16_t src); void bt_mesh_clear_rpl(void); +void bt_mesh_store_dkca(void); +void bt_mesh_clear_dkca(void); + #if CONFIG_BLE_MESH_PROVISIONER void bt_mesh_store_prov_info(uint16_t primary_addr, uint16_t alloc_addr); void bt_mesh_clear_prov_info(void); @@ -52,7 +56,6 @@ 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(uint16_t net_idx); void bt_mesh_clear_p_app_key(uint16_t app_idx); -void bt_mesh_clear_rpl_single(uint16_t src); void bt_mesh_store_node_info(struct bt_mesh_node *node); void bt_mesh_clear_node_info(uint16_t unicast_addr); void bt_mesh_store_node_name(struct bt_mesh_node *node); diff --git a/components/bt/esp_ble_mesh/core/storage/settings_nvs.c b/components/bt/esp_ble_mesh/core/storage/settings_nvs.c index 155937a0767d..ca3ac144ef5d 100644 --- a/components/bt/esp_ble_mesh/core/storage/settings_nvs.c +++ b/components/bt/esp_ble_mesh/core/storage/settings_nvs.c @@ -432,7 +432,7 @@ struct net_buf_simple *bt_mesh_get_uid_settings_item(const char *key) /* API used to check if the settings item exists */ -static bool is_settings_item_exist(struct net_buf_simple *buf, const uint16_t val) +bool bt_mesh_is_settings_item_exist(struct net_buf_simple *buf, const uint16_t val) { struct net_buf_simple_state state = {0}; size_t length = 0U; @@ -469,7 +469,7 @@ static int settings_add_item(bt_mesh_nvs_handle_t handle, const char *key, const buf = settings_get_item(handle, key); /* Check if val already exists */ - if (is_settings_item_exist(buf, val) == true) { + if (bt_mesh_is_settings_item_exist(buf, val) == true) { BT_DBG("0x%04x already exists", val); bt_mesh_free_buf(buf); return 0; @@ -533,7 +533,7 @@ static int settings_remove_item(bt_mesh_nvs_handle_t handle, const char *key, co buf = settings_get_item(handle, key); /* Check if val does exist */ - if (is_settings_item_exist(buf, val) == false) { + if (bt_mesh_is_settings_item_exist(buf, val) == false) { BT_DBG("0x%04x not exists", val); bt_mesh_free_buf(buf); return 0; diff --git a/components/bt/esp_ble_mesh/core/storage/settings_nvs.h b/components/bt/esp_ble_mesh/core/storage/settings_nvs.h index 566cbea0ccc1..6aecce418fcc 100644 --- a/components/bt/esp_ble_mesh/core/storage/settings_nvs.h +++ b/components/bt/esp_ble_mesh/core/storage/settings_nvs.h @@ -57,6 +57,8 @@ int bt_mesh_remove_settings_item(bt_mesh_nvs_handle_t handle, const char *key, c int bt_mesh_remove_core_settings_item(const char *key, const uint16_t val); int bt_mesh_remove_uid_settings_item(const char *key, const uint16_t val); +bool bt_mesh_is_settings_item_exist(struct net_buf_simple *buf, const uint16_t val); + int bt_mesh_settings_erase_key(bt_mesh_nvs_handle_t handle, const char *key); int bt_mesh_settings_erase_all(bt_mesh_nvs_handle_t handle); diff --git a/components/bt/esp_ble_mesh/core/storage/settings_uid.c b/components/bt/esp_ble_mesh/core/storage/settings_uid.c index 81d9ffa61f71..511709f1ecdc 100644 --- a/components/bt/esp_ble_mesh/core/storage/settings_uid.c +++ b/components/bt/esp_ble_mesh/core/storage/settings_uid.c @@ -9,6 +9,7 @@ #include #include "mesh.h" +#include "rpl.h" #include "mesh/main.h" #include "mesh/common.h" #include "settings_nvs.h" @@ -354,8 +355,9 @@ static int settings_close(uint8_t index, bool erase) bt_mesh_provisioner_prov_reset(erase); bt_mesh_provisioner_main_reset(erase); bt_mesh_net_reset(); - bt_mesh_rx_reset(erase); + bt_mesh_rx_reset(); bt_mesh_tx_reset(); + bt_mesh_rpl_reset(erase); bt_mesh_settings_reset(erase); if (erase == true) { diff --git a/components/bt/esp_ble_mesh/core/test.c b/components/bt/esp_ble_mesh/core/test.c index 233716ed4abe..3d3cc3bbd902 100644 --- a/components/bt/esp_ble_mesh/core/test.c +++ b/components/bt/esp_ble_mesh/core/test.c @@ -18,8 +18,11 @@ #include "access.h" #include "foundation.h" #include "mesh/main.h" +#include "transport.h" +#include "proxy_common.h" +#include "proxy_server.h" -#if defined(CONFIG_BLE_MESH_SELF_TEST) +#if CONFIG_BLE_MESH_SELF_TEST int bt_mesh_test(void) { @@ -39,7 +42,7 @@ int bt_mesh_device_auto_enter_network(struct bt_mesh_device_network_info *info) int err = 0; if (info == NULL || !BLE_MESH_ADDR_IS_UNICAST(info->unicast_addr) || - !BLE_MESH_ADDR_IS_GROUP(info->group_addr)) { + !BLE_MESH_ADDR_IS_GROUP(info->group_addr)) { return -EINVAL; } @@ -158,9 +161,9 @@ int bt_mesh_test_start_scanning(bool wl_en) if (wl_en) { return bt_mesh_scan_with_wl_enable(); - } else { - return bt_mesh_scan_enable(); } + + return bt_mesh_scan_enable(); } int bt_mesh_test_stop_scanning(void) @@ -169,4 +172,21 @@ int bt_mesh_test_stop_scanning(void) } #endif /* CONFIG_BLE_MESH_TEST_USE_WHITE_LIST */ +bt_mesh_test_net_pdu_cb_t net_pdu_test_cb; + +void bt_mesh_test_register_net_pdu_cb(bt_mesh_test_net_pdu_cb_t cb) +{ + net_pdu_test_cb = cb; +} + +void bt_mesh_test_set_seq(uint32_t seq) +{ + if (seq > 0xFFFFFF) { + BT_ERR("Invalid SEQ 0x%08x", seq); + return; + } + + bt_mesh.seq = seq; +} + #endif /* CONFIG_BLE_MESH_SELF_TEST */ diff --git a/components/bt/esp_ble_mesh/core/test.h b/components/bt/esp_ble_mesh/core/test.h index 4d4e96f2fb44..23ef6a842789 100644 --- a/components/bt/esp_ble_mesh/core/test.h +++ b/components/bt/esp_ble_mesh/core/test.h @@ -10,14 +10,14 @@ #ifndef _BLE_MESH_TEST_H_ #define _BLE_MESH_TEST_H_ +#include "net.h" +#include "proxy_client.h" #include "mesh/adapter.h" #ifdef __cplusplus extern "C" { #endif -int bt_mesh_test(void); - struct bt_mesh_device_network_info { uint8_t net_key[16]; uint16_t net_idx; @@ -45,6 +45,14 @@ int bt_mesh_test_start_scanning(bool wl_en); int bt_mesh_test_stop_scanning(void); +typedef void (* bt_mesh_test_net_pdu_cb_t)(const uint8_t *data, uint16_t length); + +extern bt_mesh_test_net_pdu_cb_t net_pdu_test_cb; + +void bt_mesh_test_register_net_pdu_cb(bt_mesh_test_net_pdu_cb_t cb); + +void bt_mesh_test_set_seq(uint32_t seq); + #ifdef __cplusplus } #endif diff --git a/components/bt/esp_ble_mesh/core/transport.c b/components/bt/esp_ble_mesh/core/transport.c index dbf5543e6b38..b3428216e6a8 100644 --- a/components/bt/esp_ble_mesh/core/transport.c +++ b/components/bt/esp_ble_mesh/core/transport.c @@ -14,15 +14,19 @@ #include "adv.h" #include "mesh.h" #include "lpn.h" +#include "rpl.h" #include "friend.h" #include "access.h" #include "foundation.h" #include "settings.h" #include "transport.h" #include "mesh/main.h" +#include "fast_prov.h" #include "mesh/common.h" #include "mesh/cfg_srv.h" +#include "mesh_v1.1/utils.h" + /* The transport layer needs at least three buffers for itself to avoid * deadlocks. Ensure that there are a sufficient number of advertising * buffers available compared to the maximum supported outgoing segment @@ -74,12 +78,15 @@ static struct seg_tx { struct net_buf *seg[CONFIG_BLE_MESH_TX_SEG_MAX]; uint64_t seq_auth; uint16_t dst; + uint8_t xmit; /* Segment transmit */ uint8_t seg_n:5, /* Last segment index */ new_key:1; /* New/old key */ uint8_t nack_count; /* Number of unacked segs */ uint8_t ttl; uint8_t seg_pending; /* Number of segments pending */ uint8_t attempts; /* Transmit attempts */ + uint8_t cred; /* Security credentials */ + uint8_t tag; /* Additional metadata */ const struct bt_mesh_send_cb *cb; void *cb_data; struct k_delayed_work retransmit; /* Retransmit timer */ @@ -111,55 +118,27 @@ static uint8_t seg_rx_buf_data[(CONFIG_BLE_MESH_RX_SEG_MSG_COUNT * static uint16_t hb_sub_dst = BLE_MESH_ADDR_UNASSIGNED; -static bt_mesh_mutex_t tx_seg_lock; -static bt_mesh_mutex_t rx_seg_lock; - -static inline void bt_mesh_tx_seg_mutex_new(void) -{ - if (!tx_seg_lock.mutex) { - bt_mesh_mutex_create(&tx_seg_lock); - } -} - -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_tx_seg_mutex_free(void) -{ - bt_mesh_mutex_free(&tx_seg_lock); -} -#endif /* CONFIG_BLE_MESH_DEINIT */ - -static inline void bt_mesh_tx_seg_lock(void) -{ - bt_mesh_mutex_lock(&tx_seg_lock); -} - -static inline void bt_mesh_tx_seg_unlock(void) -{ - bt_mesh_mutex_unlock(&tx_seg_lock); -} +static bt_mesh_mutex_t seg_tx_lock; +static bt_mesh_mutex_t seg_rx_lock; -static inline void bt_mesh_rx_seg_mutex_new(void) +static inline void bt_mesh_seg_tx_lock(void) { - if (!rx_seg_lock.mutex) { - bt_mesh_mutex_create(&rx_seg_lock); - } + bt_mesh_mutex_lock(&seg_tx_lock); } -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_rx_seg_mutex_free(void) +static inline void bt_mesh_seg_tx_unlock(void) { - bt_mesh_mutex_free(&rx_seg_lock); + bt_mesh_mutex_unlock(&seg_tx_lock); } -#endif /* CONFIG_BLE_MESH_DEINIT */ -static inline void bt_mesh_rx_seg_lock(void) +static inline void bt_mesh_seg_rx_lock(void) { - bt_mesh_mutex_lock(&rx_seg_lock); + bt_mesh_mutex_lock(&seg_rx_lock); } -static inline void bt_mesh_rx_seg_unlock(void) +static inline void bt_mesh_seg_rx_unlock(void) { - bt_mesh_mutex_unlock(&rx_seg_lock); + bt_mesh_mutex_unlock(&seg_rx_lock); } uint8_t bt_mesh_get_seg_retrans_num(void) @@ -187,14 +166,15 @@ void bt_mesh_set_hb_sub_dst(uint16_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) + const struct bt_mesh_send_cb *cb, void *cb_data, + const uint8_t *ctl_op) { 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); - buf = bt_mesh_adv_create(BLE_MESH_ADV_DATA, tx->xmit, BUF_TIMEOUT); + buf = bt_mesh_adv_create(BLE_MESH_ADV_DATA, BUF_TIMEOUT); if (!buf) { BT_ERR("Out of network buffers"); return -ENOBUFS; @@ -202,7 +182,9 @@ static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, net_buf_reserve(buf, BLE_MESH_NET_HDR_LEN); - if (tx->ctx->app_idx == BLE_MESH_KEY_DEV) { + if (ctl_op) { + net_buf_add_u8(buf, TRANS_CTL_HDR(*ctl_op, 0)); + } else if (tx->ctx->app_idx == BLE_MESH_KEY_DEV) { net_buf_add_u8(buf, UNSEG_HDR(0, 0)); } else { net_buf_add_u8(buf, UNSEG_HDR(1, tx->aid)); @@ -218,15 +200,15 @@ static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, BT_ERR("Not enough space in Friend Queue"); net_buf_unref(buf); return -ENOBUFS; - } else { - BT_WARN("No space in Friend Queue"); - goto send; } + + BT_WARN("No space in Friend Queue"); + goto send; } if (bt_mesh_friend_enqueue_tx(tx, BLE_MESH_FRIEND_PDU_SINGLE, NULL, 1, &buf->b) && - BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { + BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { /* PDUs for a specific Friend should only go * out through the Friend Queue. */ @@ -240,6 +222,15 @@ static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, return bt_mesh_net_send(tx, buf, cb, cb_data); } +static inline uint8_t seg_len(bool ctl) +{ + if (ctl) { + return BLE_MESH_CTL_SEG_SDU_MAX; + } + + return BLE_MESH_APP_SEG_SDU_MAX; +} + bool bt_mesh_tx_in_progress(void) { int i; @@ -267,7 +258,7 @@ static void seg_tx_reset(struct seg_tx *tx) { int i; - bt_mesh_tx_seg_lock(); + bt_mesh_seg_tx_lock(); k_delayed_work_cancel(&tx->retransmit); @@ -287,10 +278,10 @@ static void seg_tx_reset(struct seg_tx *tx) tx->nack_count = 0U; - bt_mesh_tx_seg_unlock(); + bt_mesh_seg_tx_unlock(); if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_IVU_PENDING)) { - BT_DBG("Proceding with pending IV Update"); + BT_DBG("Proceeding with pending IV Update"); /* bt_mesh_net_iv_update() will re-enable the flag if this * wasn't the only transfer. */ @@ -370,11 +361,11 @@ static void seg_tx_send_unacked(struct seg_tx *tx) { int i, err = 0; - bt_mesh_tx_seg_lock(); + bt_mesh_seg_tx_lock(); if (!(tx->attempts--)) { BT_WARN("Ran out of retransmit attempts"); - bt_mesh_tx_seg_unlock(); + bt_mesh_seg_tx_unlock(); seg_tx_complete(tx, -ETIMEDOUT); return; } @@ -395,19 +386,25 @@ static void seg_tx_send_unacked(struct seg_tx *tx) tx->seg_pending++; - BT_DBG("resending %u/%u", i, tx->seg_n); + BT_INFO("Resending %u/%u, cred 0x%02x", i, tx->seg_n, tx->cred); + /* TODO: + * tx->new_key should be replaced with sub->kr_flag, + * since there is a chance that the key is refreshed + * during the retransmission of segments. + */ err = bt_mesh_net_resend(tx->sub, seg, tx->new_key, + &tx->cred, tx->tag, &seg_sent_cb, tx); if (err) { BT_ERR("Sending segment failed"); - bt_mesh_tx_seg_unlock(); + bt_mesh_seg_tx_unlock(); seg_tx_complete(tx, -EIO); return; } } - bt_mesh_tx_seg_unlock(); + bt_mesh_seg_tx_unlock(); } static void seg_retransmit(struct k_work *work) @@ -418,27 +415,19 @@ 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) + const struct bt_mesh_send_cb *cb, void *cb_data, + const uint8_t *ctl_op) { - uint8_t seg_hdr = 0U, seg_o = 0U; - uint16_t seq_zero = 0U; struct seg_tx *tx = NULL; + uint16_t seq_zero = 0U; + uint8_t seg_hdr = 0U; + uint8_t seg_o = 0U; int i; BT_DBG("src 0x%04x dst 0x%04x app_idx 0x%04x aszmic %u sdu_len %u", net_tx->src, net_tx->ctx->addr, net_tx->ctx->app_idx, net_tx->aszmic, sdu->len); - if (sdu->len < 1) { - BT_ERR("Zero-length SDU not allowed"); - return -EINVAL; - } - - if (sdu->len > BLE_MESH_TX_SDU_MAX) { - BT_ERR("Not enough segment buffers for length %u", sdu->len); - return -EMSGSIZE; - } - for (tx = NULL, i = 0; i < ARRAY_SIZE(seg_tx); i++) { if (!seg_tx[i].nack_count) { tx = &seg_tx[i]; @@ -451,21 +440,32 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, return -EBUSY; } - if (net_tx->ctx->app_idx == BLE_MESH_KEY_DEV) { + if (ctl_op) { + seg_hdr = TRANS_CTL_HDR(*ctl_op, 1); + } else if (net_tx->ctx->app_idx == BLE_MESH_KEY_DEV) { seg_hdr = SEG_HDR(0, 0); } else { seg_hdr = SEG_HDR(1, net_tx->aid); } - seg_o = 0U; tx->dst = net_tx->ctx->addr; - tx->seg_n = (sdu->len - 1) / BLE_MESH_APP_SEG_SDU_MAX; + /* TODO: + * When SAR Transmitter is introduced, the xmit may be + * updated with "bt_mesh_get_sar_seg_transmit()". + */ + if (sdu->len) { + tx->seg_n = (sdu->len - 1) / seg_len(!!ctl_op); + } else { + tx->seg_n = 0; + } tx->nack_count = tx->seg_n + 1; tx->seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_TX, bt_mesh.seq); tx->sub = net_tx->sub; tx->new_key = net_tx->sub->kr_flag; tx->attempts = SEG_RETRANSMIT_ATTEMPTS; tx->seg_pending = 0; + tx->cred = net_tx->ctx->send_cred; + tx->tag = net_tx->ctx->send_tag; tx->cb = cb; tx->cb_data = cb_data; @@ -480,10 +480,10 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, BT_DBG("SeqZero 0x%04x", seq_zero); if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && - !bt_mesh_friend_queue_has_space(tx->sub->net_idx, net_tx->src, - tx->dst, &tx->seq_auth, - tx->seg_n + 1) && - BLE_MESH_ADDR_IS_UNICAST(tx->dst)) { + !bt_mesh_friend_queue_has_space(tx->sub->net_idx, net_tx->src, + tx->dst, &tx->seq_auth, + tx->seg_n + 1) && + BLE_MESH_ADDR_IS_UNICAST(tx->dst)) { BT_ERR("Not enough space in Friend Queue for %u segments", tx->seg_n + 1); seg_tx_reset(tx); @@ -495,8 +495,7 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, uint16_t len = 0U; int err = 0; - seg = bt_mesh_adv_create(BLE_MESH_ADV_DATA, net_tx->xmit, - BUF_TIMEOUT); + seg = bt_mesh_adv_create(BLE_MESH_ADV_DATA, BUF_TIMEOUT); if (!seg) { BT_ERR("Out of segment buffers"); seg_tx_reset(tx); @@ -507,13 +506,11 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, net_buf_add_u8(seg, seg_hdr); net_buf_add_u8(seg, (net_tx->aszmic << 7) | seq_zero >> 6); - net_buf_add_u8(seg, (((seq_zero & 0x3f) << 2) | - (seg_o >> 3))); + net_buf_add_u8(seg, (((seq_zero & 0x3f) << 2) | (seg_o >> 3))); net_buf_add_u8(seg, ((seg_o & 0x07) << 5) | tx->seg_n); - len = MIN(sdu->len, BLE_MESH_APP_SEG_SDU_MAX); - net_buf_add_mem(seg, sdu->data, len); - net_buf_simple_pull(sdu, len); + len = MIN(sdu->len, seg_len(!!ctl_op)); + net_buf_add_mem(seg, net_buf_simple_pull_mem(sdu, len), len); if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { enum bt_mesh_friend_pdu_type type = BLE_MESH_FRIEND_PDU_PARTIAL; @@ -528,7 +525,7 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, &tx->seq_auth, tx->seg_n + 1, &seg->b) && - BLE_MESH_ADDR_IS_UNICAST(net_tx->ctx->addr)) { + BLE_MESH_ADDR_IS_UNICAST(net_tx->ctx->addr)) { /* PDUs for a specific Friend should only go * out through the Friend Queue. */ @@ -546,10 +543,18 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, seg_o ? &seg_sent_cb : &first_sent_cb, tx); if (err) { - BT_ERR("Sending segment failed"); + BT_ERR("Sending segment failed (err %d)", err); seg_tx_reset(tx); return err; } + + /* If security credentials is updated in the network layer, + * need to store the security credentials for the segments, + * which will be used for retransmission later. + */ + if (tx->cred != net_tx->ctx->send_cred) { + tx->cred = net_tx->ctx->send_cred; + } } /* This can happen if segments only went into the Friend Queue */ @@ -570,17 +575,31 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, return 0; } -struct bt_mesh_app_key *bt_mesh_app_key_find(uint16_t app_idx) +struct bt_mesh_app_key *bt_mesh_app_key_get(uint16_t app_idx) { - int i; - - for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { - struct bt_mesh_app_key *key = &bt_mesh.app_keys[i]; - - if (key->net_idx != BLE_MESH_KEY_UNUSED && - key->app_idx == app_idx) { - return key; + if (bt_mesh_is_provisioned()) { +#if CONFIG_BLE_MESH_NODE + if (!IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) { + for (int i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { + if (bt_mesh.app_keys[i].net_idx != BLE_MESH_KEY_UNUSED && + bt_mesh.app_keys[i].app_idx == app_idx) { + return &bt_mesh.app_keys[i]; + } + } + } else { + return bt_mesh_fast_prov_app_key_find(app_idx); } +#endif + } else if (bt_mesh_is_provisioner_en()) { +#if CONFIG_BLE_MESH_PROVISIONER + for (int i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + if (bt_mesh.p_app_keys[i] && + bt_mesh.p_app_keys[i]->net_idx != BLE_MESH_KEY_UNUSED && + bt_mesh.p_app_keys[i]->app_idx == app_idx) { + return bt_mesh.p_app_keys[i]; + } + } +#endif } return NULL; @@ -590,43 +609,43 @@ 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 uint8_t *key = NULL; - uint8_t *ad = NULL, role = 0U; + uint8_t *ad = NULL; uint8_t aid = 0U; int err = 0; - if (net_buf_simple_tailroom(msg) < BLE_MESH_MIC_SHORT) { - BT_ERR("Insufficient tailroom for Transport MIC"); + if (msg->len < 1) { + BT_ERR("Zero-length SDU not allowed"); return -EINVAL; } if (msg->len > BLE_MESH_SDU_UNSEG_MAX) { - tx->ctx->send_rel = 1U; + tx->ctx->send_tag |= BLE_MESH_TAG_SEND_SEGMENTED; } BT_DBG("net_idx 0x%04x app_idx 0x%04x dst 0x%04x", tx->sub->net_idx, tx->ctx->app_idx, tx->ctx->addr); BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); - role = bt_mesh_get_device_role(tx->ctx->model, tx->ctx->srv_send); - if (role == ROLE_NVAL) { - BT_ERR("Failed to get model role"); - return -EINVAL; - } - - err = bt_mesh_app_key_get(tx->sub, tx->ctx->app_idx, &key, - &aid, role, tx->ctx->addr); + err = bt_mesh_upper_key_get(tx->sub, tx->ctx->app_idx, &key, + &aid, tx->ctx->addr); if (err) { return err; } tx->aid = aid; - if (!tx->ctx->send_rel || net_buf_simple_tailroom(msg) < BLE_MESH_MIC_LONG) { + if (!bt_mesh_tag_send_segmented(tx->ctx->send_tag) || + tx->ctx->send_szmic == BLE_MESH_SEG_SZMIC_SHORT || + net_buf_simple_tailroom(msg) < BLE_MESH_MIC_LONG) { tx->aszmic = 0U; } else { tx->aszmic = 1U; } + BT_INFO("%s, send_tag 0x%02x, send_szmic %d, aszmic %d", + bt_mesh_tag_send_segmented(tx->ctx->send_tag) ? "Seg" : "Unseg", + tx->ctx->send_tag, tx->ctx->send_szmic, tx->aszmic); + if (BLE_MESH_ADDR_IS_VIRTUAL(tx->ctx->addr)) { ad = bt_mesh_label_uuid_get(tx->ctx->addr); } else { @@ -642,82 +661,26 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, return err; } - if (tx->ctx->send_rel) { - err = send_seg(tx, msg, cb, cb_data); - } else { - err = send_unseg(tx, msg, cb, cb_data); + if (bt_mesh_tag_send_segmented(tx->ctx->send_tag)) { + return send_seg(tx, msg, cb, cb_data, NULL); } - return err; -} - -static void update_rpl(struct bt_mesh_rpl *rpl, struct bt_mesh_net_rx *rx) -{ - rpl->src = rx->ctx.addr; - rpl->seq = rx->seq; - rpl->old_iv = rx->old_iv; - - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_store_rpl(rpl); - } + return send_unseg(tx, msg, cb, cb_data, NULL); } -/* Check the Replay Protection List for a replay attempt. If non-NULL match - * parameter is given the RPL slot is returned but it is not immediately - * updated (needed for segmented messages), whereas if a NULL match is given - * the RPL is immediately updated (used for unsegmented messages). - */ -bool bt_mesh_rpl_check(struct bt_mesh_net_rx *rx, struct bt_mesh_rpl **match) +static void revoke_dev_key(const uint8_t *dev_key) { - int i; - - /* Don't bother checking messages from ourselves */ - if (rx->net_if == BLE_MESH_NET_IF_LOCAL) { - return false; - } + if (!memcmp(dev_key, bt_mesh.dev_key_ca, 16)) { + BT_INFO("Revoke Device Key"); - /* The RPL is used only for the local node */ - if (!rx->local_match) { - return false; - } - - for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { - struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i]; + memcpy(bt_mesh.dev_key, bt_mesh.dev_key_ca, 16); + memset(bt_mesh.dev_key_ca, 0, 16); - /* Empty slot */ - if (!rpl->src) { - if (match) { - *match = rpl; - } else { - update_rpl(rpl, rx); - } - - return false; - } - - /* Existing slot for given address */ - if (rpl->src == rx->ctx.addr) { - if (rx->old_iv && !rpl->old_iv) { - return true; - } - - if ((!rx->old_iv && rpl->old_iv) || - rpl->seq < rx->seq) { - if (match) { - *match = rpl; - } else { - update_rpl(rpl, rx); - } - - return false; - } else { - return true; - } + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_net(); + bt_mesh_clear_dkca(); } } - - BT_ERR("RPL is full!"); - return true; } static int sdu_recv(struct bt_mesh_net_rx *rx, uint32_t seq, uint8_t hdr, @@ -753,7 +716,7 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, uint32_t seq, uint8_t hdr, buf->len -= APP_MIC_LEN(aszmic); /* Use bt_mesh_alloc_buf() instead of NET_BUF_SIMPLE_DEFINE to avoid - * causing btu task stackoverflow. + * causing btu task stack overflow. */ sdu = bt_mesh_alloc_buf(CONFIG_BLE_MESH_RX_SDU_MAX - BLE_MESH_MIC_SHORT); if (!sdu) { @@ -774,6 +737,7 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, uint32_t seq, uint8_t hdr, } net_buf_simple_reset(sdu); + err = bt_mesh_app_decrypt(dev_key, true, aszmic, buf, sdu, ad, rx->ctx.addr, rx->ctx.recv_dst, seq, @@ -782,11 +746,14 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, uint32_t seq, uint8_t hdr, continue; } - BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT, + BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | \ + BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT, "\nTNPTRecv: ctl: 0x%04x, ttl: 0x%04x, src: 0x%04x, dst: 0x%04x, payload: 0x%s", rx->ctl, rx->ctx.recv_ttl, rx->ctx.addr, rx->ctx.recv_dst, bt_hex(sdu->data, sdu->len)); + revoke_dev_key(dev_key); + rx->ctx.app_idx = BLE_MESH_KEY_DEV; bt_mesh_model_recv(rx, sdu); @@ -828,12 +795,14 @@ static int sdu_recv(struct bt_mesh_net_rx *rx, uint32_t seq, uint8_t hdr, } net_buf_simple_reset(sdu); + err = bt_mesh_app_decrypt(keys->val, false, aszmic, buf, sdu, ad, rx->ctx.addr, rx->ctx.recv_dst, seq, BLE_MESH_NET_IVI_RX(rx)); - BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT, + BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | \ + BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT, "\nTNPTRecv: ctl: 0x%04x, ttl: 0x%04x, src: 0x%04x, dst: 0x%04x, payload: 0x%s", rx->ctl, rx->ctx.recv_ttl, rx->ctx.addr, rx->ctx.recv_dst, bt_hex(sdu->data, sdu->len)); @@ -897,8 +866,8 @@ static int trans_ack(struct bt_mesh_net_rx *rx, uint8_t hdr, uint16_t seq_zero = 0U; uint8_t obo = 0U; - if (buf->len < 6) { - BT_ERR("Too short ack message (len %u)", buf->len); + if (buf->len != 6) { + BT_ERR("Malformed Segment Ack (len %u)", buf->len); return -EINVAL; } @@ -919,7 +888,7 @@ static int trans_ack(struct bt_mesh_net_rx *rx, uint8_t hdr, tx = seg_tx_lookup(seq_zero, obo, rx->ctx.addr); if (!tx) { - BT_WARN("No matching TX context for ack"); + BT_INFO("No matching TX context for Seg Ack"); return -EINVAL; } @@ -945,10 +914,10 @@ static int trans_ack(struct bt_mesh_net_rx *rx, uint8_t hdr, while ((bit = find_lsb_set(ack))) { if (tx->seg[bit - 1]) { - BT_DBG("seg %u/%u acked", bit - 1, tx->seg_n); - bt_mesh_tx_seg_lock(); + BT_INFO("Seg %u/%u acked", bit - 1, tx->seg_n); + bt_mesh_seg_tx_lock(); seg_tx_done(tx, bit - 1); - bt_mesh_tx_seg_unlock(); + bt_mesh_seg_tx_unlock(); } ack &= ~BIT(bit - 1); @@ -970,8 +939,8 @@ static int trans_heartbeat(struct bt_mesh_net_rx *rx, uint8_t init_ttl = 0U, hops = 0U; uint16_t feat = 0U; - if (buf->len < 3) { - BT_ERR("Too short heartbeat message (len %u)", buf->len); + if (buf->len != 3) { + BT_ERR("Malformed heartbeat message (len %u)", buf->len); return -EINVAL; } @@ -1008,7 +977,8 @@ static int ctl_recv(struct bt_mesh_net_rx *rx, uint8_t hdr, BT_DBG("OpCode 0x%02x len %u", ctl_op, buf->len); - BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT, + BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | \ + BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT, "\nTNPTRecv: ctl: 0x%04x, ttl: 0x%04x, src: 0x%04x, dst: 0x%04x, payload: 0x%s", rx->ctl, rx->ctx.recv_ttl, rx->ctx.addr, rx->ctx.recv_dst, bt_hex(buf->data, buf->len)); @@ -1025,6 +995,19 @@ static int ctl_recv(struct bt_mesh_net_rx *rx, uint8_t hdr, return 0; } + if (IS_ENABLED(CONFIG_BLE_MESH_DF_SRV)) { + switch (ctl_op) { + case TRANS_CTL_OP_PATH_REQ: + case TRANS_CTL_OP_PATH_REPLY: + case TRANS_CTL_OP_PATH_CFM: + case TRANS_CTL_OP_PATH_ECHO_REQ: + case TRANS_CTL_OP_PATH_ECHO_REPLY: + case TRANS_CTL_OP_DEP_NODE_UPDATE: + case TRANS_CTL_OP_PATH_REQ_SOLIC: + return bt_mesh_directed_forwarding_ctl_recv(ctl_op, rx, buf); + } + } + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && !bt_mesh_lpn_established()) { switch (ctl_op) { case TRANS_CTL_OP_FRIEND_POLL: @@ -1042,7 +1025,7 @@ static int ctl_recv(struct bt_mesh_net_rx *rx, uint8_t hdr, } } -#if defined(CONFIG_BLE_MESH_LOW_POWER) +#if CONFIG_BLE_MESH_LOW_POWER if (ctl_op == TRANS_CTL_OP_FRIEND_OFFER) { return bt_mesh_lpn_friend_offer(rx, buf); } @@ -1052,7 +1035,7 @@ static int ctl_recv(struct bt_mesh_net_rx *rx, uint8_t hdr, return bt_mesh_lpn_friend_clear_cfm(rx, buf); } - if (!rx->friend_cred) { + if (rx->ctx.recv_cred != BLE_MESH_FRIENDSHIP_CRED) { BT_WARN("Message from friend with wrong credentials"); return -EINVAL; } @@ -1093,14 +1076,14 @@ static int trans_unseg(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx, if (rx->ctl) { return ctl_recv(rx, hdr, buf, seq_auth); - } else { - /* SDUs must match a local element or an LPN of this Friend. */ - if (!rx->local_match && !rx->friend_match) { - return 0; - } + } - return sdu_recv(rx, rx->seq, hdr, 0, buf); + /* SDUs must match a local element or an LPN of this Friend. */ + if (!rx->local_match && !rx->friend_match) { + return 0; } + + return sdu_recv(rx, rx->seq, hdr, 0, buf); } static inline int32_t ack_timeout(struct seg_rx *rx) @@ -1128,154 +1111,50 @@ static inline int32_t ack_timeout(struct seg_rx *rx) return MAX(to, K_MSEC(400)); } -static int ctl_send_unseg(struct bt_mesh_net_tx *tx, uint8_t ctl_op, void *data, - size_t data_len, const struct bt_mesh_send_cb *cb, - void *cb_data) -{ - struct net_buf *buf = NULL; - - buf = bt_mesh_adv_create(BLE_MESH_ADV_DATA, tx->xmit, BUF_TIMEOUT); - if (!buf) { - BT_ERR("Out of transport buffers"); - return -ENOBUFS; - } - - net_buf_reserve(buf, BLE_MESH_NET_HDR_LEN); - - net_buf_add_u8(buf, TRANS_CTL_HDR(ctl_op, 0)); - - net_buf_add_mem(buf, data, data_len); - - if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { - if (bt_mesh_friend_enqueue_tx(tx, BLE_MESH_FRIEND_PDU_SINGLE, - NULL, 1, &buf->b) && - BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { - /* PDUs for a specific Friend should only go - * out through the Friend Queue. - */ - net_buf_unref(buf); - return 0; - } - } - - return bt_mesh_net_send(tx, buf, cb, cb_data); -} - -static int ctl_send_seg(struct bt_mesh_net_tx *tx, uint8_t ctl_op, void *data, - size_t data_len, const struct bt_mesh_send_cb *cb, - void *cb_data) +int bt_mesh_ctl_send(struct bt_mesh_net_tx *tx, uint8_t ctl_op, void *data, + size_t data_len, const struct bt_mesh_send_cb *cb, + void *cb_data) { - struct seg_tx *tx_seg = NULL; - uint16_t unsent = data_len; - uint16_t seq_zero = 0; - uint8_t seg_o = 0; - int i; - - for (tx_seg = NULL, i = 0; i < ARRAY_SIZE(seg_tx); i++) { - if (!seg_tx[i].nack_count) { - tx_seg = &seg_tx[i]; - break; - } - } - - if (!tx_seg) { - BT_ERR("No multi-segment message contexts available"); - return -EBUSY; - } - - tx_seg->dst = tx->ctx->addr; - tx_seg->seg_n = (data_len - 1) / BLE_MESH_CTL_SEG_SDU_MAX; - tx_seg->nack_count = tx_seg->seg_n + 1; - tx_seg->seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_TX, bt_mesh.seq); - tx_seg->sub = tx->sub; - tx_seg->new_key = tx->sub->kr_flag; - tx_seg->attempts = SEG_RETRANSMIT_ATTEMPTS; - tx_seg->seg_pending = 0; - tx_seg->cb = cb; - tx_seg->cb_data = cb_data; - - if (tx->ctx->send_ttl == BLE_MESH_TTL_DEFAULT) { - tx_seg->ttl = bt_mesh_default_ttl_get(); - } else { - tx_seg->ttl = tx->ctx->send_ttl; - } + struct net_buf_simple buf = {0}; - seq_zero = tx_seg->seq_auth & TRANS_SEQ_ZERO_MASK; + net_buf_simple_init_with_data(&buf, data, data_len); - BT_DBG("SeqZero 0x%04x", seq_zero); - - for (seg_o = 0; seg_o <= tx_seg->seg_n; seg_o++) { - struct net_buf *seg = NULL; - uint16_t len = 0; - int err = 0; - - seg = bt_mesh_adv_create(BLE_MESH_ADV_DATA, tx->xmit, - BUF_TIMEOUT); - if (!seg) { - BT_ERR("Out of segment buffers"); - seg_tx_reset(tx_seg); - return -ENOBUFS; - } - - net_buf_reserve(seg, BLE_MESH_NET_HDR_LEN); - - net_buf_add_u8(seg, TRANS_CTL_HDR(ctl_op, 1)); - net_buf_add_u8(seg, (tx->aszmic << 7) | seq_zero >> 6); - net_buf_add_u8(seg, (((seq_zero & 0x3f) << 2) | (seg_o >> 3))); - net_buf_add_u8(seg, ((seg_o & 0x07) << 5) | tx_seg->seg_n); - - len = MIN(unsent, BLE_MESH_CTL_SEG_SDU_MAX); - net_buf_add_mem(seg, (uint8_t *)data + (data_len - unsent), len); - unsent -= len; - - tx_seg->seg[seg_o] = net_buf_ref(seg); - - BT_DBG("Sending %u/%u", seg_o, tx_seg->seg_n); - tx_seg->seg_pending++; - - err = bt_mesh_net_send(tx, seg, - seg_o ? &seg_sent_cb : &first_sent_cb, - tx_seg); - if (err) { - BT_ERR("Sending segment failed (err %d)", err); - seg_tx_reset(tx_seg); - return err; - } + if (data_len > BLE_MESH_SDU_UNSEG_MAX) { + tx->ctx->send_tag |= BLE_MESH_TAG_SEND_SEGMENTED; } - return 0; -} + /* Set app_idx to unused here since CTL is only encrypted with NetKey */ + tx->ctx->app_idx = BLE_MESH_KEY_UNUSED; -int bt_mesh_ctl_send(struct bt_mesh_net_tx *tx, uint8_t ctl_op, void *data, - size_t data_len, const struct bt_mesh_send_cb *cb, - void *cb_data) -{ 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); BT_DBG("len %zu: %s", data_len, bt_hex(data, data_len)); - if (data_len <= BLE_MESH_SDU_UNSEG_MAX) { - return ctl_send_unseg(tx, ctl_op, data, data_len, - cb, cb_data); - } else { - return ctl_send_seg(tx, ctl_op, data, data_len, - cb, cb_data); + if (bt_mesh_tag_send_segmented(tx->ctx->send_tag)) { + return send_seg(tx, &buf, cb, cb_data, &ctl_op); } + + return send_unseg(tx, &buf, cb, cb_data, &ctl_op); } static int send_ack(struct bt_mesh_subnet *sub, uint16_t src, uint16_t dst, uint8_t ttl, uint64_t *seq_auth, uint32_t block, uint8_t obo) { struct bt_mesh_msg_ctx ctx = { - .net_idx = sub->net_idx, - .app_idx = BLE_MESH_KEY_UNUSED, - .addr = dst, - .send_ttl = ttl, + .net_idx = sub->net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = dst, + .send_ttl = ttl, + /* TODO: + * Could be Managed flooding/Friendship/Directed security credentials. + * The "recv_cred" could be used to initialize "send_cred". + */ + .send_cred = BLE_MESH_FLOODING_CRED, }; struct bt_mesh_net_tx tx = { - .sub = sub, - .ctx = &ctx, - .src = obo ? bt_mesh_primary_addr() : src, + .sub = sub, + .ctx = &ctx, + .src = obo ? bt_mesh_primary_addr() : src, .xmit = bt_mesh_net_transmit_get(), }; uint16_t seq_zero = *seq_auth & TRANS_SEQ_ZERO_MASK; @@ -1307,12 +1186,12 @@ static void seg_rx_reset(struct seg_rx *rx, bool full_reset) { BT_DBG("rx %p", rx); - bt_mesh_rx_seg_lock(); + bt_mesh_seg_rx_lock(); k_delayed_work_cancel(&rx->ack); if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && rx->obo && - rx->block != BLOCK_COMPLETE(rx->seg_n)) { + rx->block != BLOCK_COMPLETE(rx->seg_n)) { BT_WARN("Clearing incomplete buffers from Friend queue"); bt_mesh_friend_clear_incomplete(rx->sub, rx->src, rx->dst, &rx->seq_auth); @@ -1331,7 +1210,7 @@ static void seg_rx_reset(struct seg_rx *rx, bool full_reset) rx->dst = BLE_MESH_ADDR_UNASSIGNED; } - bt_mesh_rx_seg_unlock(); + bt_mesh_seg_rx_unlock(); } static uint32_t incomplete_timeout(struct seg_rx *rx) @@ -1360,11 +1239,11 @@ static void seg_ack(struct k_work *work) BT_DBG("rx %p", rx); - bt_mesh_rx_seg_lock(); + bt_mesh_seg_rx_lock(); if (k_uptime_get_32() - rx->last > incomplete_timeout(rx)) { BT_WARN("Incomplete timer expired"); - bt_mesh_rx_seg_unlock(); + bt_mesh_seg_rx_unlock(); seg_rx_reset(rx, false); return; } @@ -1373,7 +1252,7 @@ static void seg_ack(struct k_work *work) * after the seg_rx_reset() which may reset rx->sub to NULL. */ if (rx->sub == NULL) { - bt_mesh_rx_seg_unlock(); + bt_mesh_seg_rx_unlock(); return; } @@ -1382,16 +1261,7 @@ static void seg_ack(struct k_work *work) k_delayed_work_submit(&rx->ack, ack_timeout(rx)); - bt_mesh_rx_seg_unlock(); -} - -static inline uint8_t seg_len(bool ctl) -{ - if (ctl) { - return BLE_MESH_CTL_SEG_SDU_MAX; - } else { - return BLE_MESH_APP_SEG_SDU_MAX; - } + bt_mesh_seg_rx_unlock(); } static inline bool sdu_len_is_ok(bool ctl, uint8_t seg_n) @@ -1408,7 +1278,7 @@ static struct seg_rx *seg_rx_find(struct bt_mesh_net_rx *net_rx, struct seg_rx *rx = &seg_rx[i]; if (rx->src != net_rx->ctx.addr || - rx->dst != net_rx->ctx.recv_dst) { + rx->dst != net_rx->ctx.recv_dst) { continue; } @@ -1424,8 +1294,7 @@ static struct seg_rx *seg_rx_find(struct bt_mesh_net_rx *net_rx, } if (rx->in_use) { - BT_WARN("Duplicate SDU from src 0x%04x", - net_rx->ctx.addr); + BT_WARN("Duplicate SDU from src 0x%04x", net_rx->ctx.addr); /* Clear out the old context since the sender * has apparently started sending a new SDU. @@ -1551,8 +1420,7 @@ static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, */ *seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_RX(net_rx), (net_rx->seq - - ((((net_rx->seq & BIT_MASK(14)) - seq_zero)) & - BIT_MASK(13)))); + ((((net_rx->seq & BIT_MASK(14)) - seq_zero)) & BIT_MASK(13)))); *seg_count = seg_n + 1; @@ -1561,7 +1429,8 @@ static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, if (rx) { /* Discard old SeqAuth packet */ if (rx->seq_auth > *seq_auth) { - BT_WARN("Ignoring old SeqAuth"); + BT_WARN("Ignoring old SeqAuth, src 0x%04x, dst 0x%04x", + rx->src, rx->dst); return -EINVAL; } @@ -1581,7 +1450,7 @@ static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, seq_auth, rx->block, rx->obo); if (rpl) { - update_rpl(rpl, net_rx); + bt_mesh_update_rpl(rpl, net_rx); } return -EALREADY; @@ -1608,11 +1477,11 @@ static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, * case this message is destined to an LPN of ours. */ if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && - net_rx->friend_match && !net_rx->local_match && - !bt_mesh_friend_queue_has_space(net_rx->sub->net_idx, - net_rx->ctx.addr, - net_rx->ctx.recv_dst, seq_auth, - *seg_count)) { + net_rx->friend_match && !net_rx->local_match && + !bt_mesh_friend_queue_has_space(net_rx->sub->net_idx, + net_rx->ctx.addr, + net_rx->ctx.recv_dst, seq_auth, + *seg_count)) { BT_ERR("No space in Friend Queue for %u segments", *seg_count); send_ack(net_rx->sub, net_rx->ctx.recv_dst, net_rx->ctx.addr, net_rx->ctx.send_ttl, seq_auth, 0, @@ -1635,7 +1504,7 @@ static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, found_rx: if (BIT(seg_o) & rx->block) { - BT_WARN("Received already received fragment"); + BT_INFO("Received already received fragment"); return -EALREADY; } @@ -1668,14 +1537,14 @@ static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, rx->last = k_uptime_get_32(); if (!k_delayed_work_remaining_get(&rx->ack) && - !bt_mesh_lpn_established()) { + !bt_mesh_lpn_established()) { k_delayed_work_submit(&rx->ack, ack_timeout(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_INFO("Received %u/%u", seg_o, seg_n); + BT_INFO("Seg %u/%u received", seg_o, seg_n); /* Mark segment as received */ rx->block |= BIT(seg_o); @@ -1688,7 +1557,7 @@ static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, BT_DBG("Complete SDU"); if (rpl) { - update_rpl(rpl, net_rx); + bt_mesh_update_rpl(rpl, net_rx); } *pdu_type = BLE_MESH_FRIEND_PDU_COMPLETE; @@ -1738,7 +1607,8 @@ int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx) */ if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && bt_mesh_lpn_established() && rx->net_if == BLE_MESH_NET_IF_ADV && - (!bt_mesh_lpn_waiting_update() || !rx->friend_cred)) { + (!bt_mesh_lpn_waiting_update() || + rx->ctx.recv_cred != BLE_MESH_FRIENDSHIP_CRED)) { BT_WARN("Ignoring unexpected message in Low Power mode"); return -EAGAIN; } @@ -1794,35 +1664,24 @@ int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx) return err; } -void bt_mesh_rx_reset(bool erase) +void bt_mesh_rx_reset(void) { int i; - BT_DBG("%s", __func__); - for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { seg_rx_reset(&seg_rx[i], true); } - - (void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl)); - - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && erase) { - bt_mesh_clear_rpl(); - } } void bt_mesh_tx_reset(void) { int i; - BT_DBG("%s", __func__); - for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { seg_tx_reset(&seg_tx[i]); } } -#if CONFIG_BLE_MESH_PROVISIONER void bt_mesh_rx_reset_single(uint16_t src) { int i; @@ -1837,16 +1696,6 @@ void bt_mesh_rx_reset_single(uint16_t src) seg_rx_reset(rx, true); } } - - for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { - struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i]; - if (src == rpl->src) { - memset(rpl, 0, sizeof(struct bt_mesh_rpl)); - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_clear_rpl_single(src); - } - } - } } void bt_mesh_tx_reset_single(uint16_t dst) @@ -1864,12 +1713,13 @@ void bt_mesh_tx_reset_single(uint16_t dst) } } } -#endif /* CONFIG_BLE_MESH_PROVISIONER */ void bt_mesh_trans_init(void) { int i; + bt_mesh_sar_init(); + for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { k_delayed_work_init(&seg_tx[i].retransmit, seg_retransmit); } @@ -1881,8 +1731,8 @@ void bt_mesh_trans_init(void) seg_rx[i].buf.data = seg_rx[i].buf.__buf; } - bt_mesh_tx_seg_mutex_new(); - bt_mesh_rx_seg_mutex_new(); + bt_mesh_mutex_create(&seg_tx_lock); + bt_mesh_mutex_create(&seg_rx_lock); } #if CONFIG_BLE_MESH_DEINIT @@ -1890,8 +1740,9 @@ void bt_mesh_trans_deinit(bool erase) { int i; - bt_mesh_rx_reset(erase); + bt_mesh_rx_reset(); bt_mesh_tx_reset(); + bt_mesh_rpl_reset(erase); for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { k_delayed_work_free(&seg_tx[i].retransmit); @@ -1901,8 +1752,8 @@ void bt_mesh_trans_deinit(bool erase) k_delayed_work_free(&seg_rx[i].ack); } - bt_mesh_tx_seg_mutex_free(); - bt_mesh_rx_seg_mutex_free(); + bt_mesh_mutex_free(&seg_tx_lock); + bt_mesh_mutex_free(&seg_rx_lock); } #endif /* CONFIG_BLE_MESH_DEINIT */ @@ -1910,20 +1761,21 @@ void bt_mesh_heartbeat_send(void) { struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get(); uint16_t feat = 0U; - struct __packed { + struct __attribute__((packed)) { uint8_t init_ttl; uint16_t feat; } hb; struct bt_mesh_msg_ctx ctx = { - .net_idx = cfg->hb_pub.net_idx, - .app_idx = BLE_MESH_KEY_UNUSED, - .addr = cfg->hb_pub.dst, - .send_ttl = cfg->hb_pub.ttl, + .net_idx = cfg->hb_pub.net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = cfg->hb_pub.dst, + .send_ttl = cfg->hb_pub.ttl, + .send_cred = BLE_MESH_FLOODING_CRED, }; struct bt_mesh_net_tx tx = { - .sub = bt_mesh_subnet_get(cfg->hb_pub.net_idx), - .ctx = &ctx, - .src = bt_mesh_model_elem(cfg->model)->addr, + .sub = bt_mesh_subnet_get(cfg->hb_pub.net_idx), + .ctx = &ctx, + .src = bt_mesh_model_elem(cfg->model)->addr, .xmit = bt_mesh_net_transmit_get(), }; @@ -1958,15 +1810,15 @@ void bt_mesh_heartbeat_send(void) NULL, NULL); } -int bt_mesh_app_key_get(const struct bt_mesh_subnet *subnet, uint16_t app_idx, - const uint8_t **key, uint8_t *aid, uint8_t role, uint16_t dst) +int bt_mesh_upper_key_get(const struct bt_mesh_subnet *subnet, uint16_t app_idx, + const uint8_t **key, uint8_t *aid, uint16_t dst) { struct bt_mesh_app_key *app_key = NULL; if (app_idx == BLE_MESH_KEY_DEV) { - *key = bt_mesh_tx_devkey_get(role, dst); + *key = bt_mesh_dev_key_get(dst); if (!*key) { - BT_ERR("DevKey not found"); + BT_ERR("DevKey of 0x%04x not found", dst); return -EINVAL; } @@ -1979,9 +1831,9 @@ int bt_mesh_app_key_get(const struct bt_mesh_subnet *subnet, uint16_t app_idx, return -EINVAL; } - app_key = bt_mesh_tx_appkey_get(role, app_idx); + app_key = bt_mesh_app_key_get(app_idx); if (!app_key) { - BT_ERR("Invalid AppKeyIndex 0x%04x", app_idx); + BT_ERR("AppKey 0x%04x not found", app_idx); return -ENOENT; } diff --git a/components/bt/esp_ble_mesh/core/transport.h b/components/bt/esp_ble_mesh/core/transport.h index 015b51e4df40..1feccb998098 100644 --- a/components/bt/esp_ble_mesh/core/transport.h +++ b/components/bt/esp_ble_mesh/core/transport.h @@ -16,39 +16,46 @@ extern "C" { #endif -#define TRANS_SEQ_AUTH_NVAL 0xffffffffffffffff - -#define BLE_MESH_SDU_UNSEG_MAX 11 -#define BLE_MESH_CTL_SEG_SDU_MAX 8 -#define BLE_MESH_APP_SEG_SDU_MAX 12 -#define BLE_MESH_TX_SDU_MAX (CONFIG_BLE_MESH_TX_SEG_MAX * 12) - -#define TRANS_SEQ_ZERO_MASK ((uint16_t)BIT_MASK(13)) -#define TRANS_CTL_OP_MASK ((uint8_t)BIT_MASK(7)) -#define TRANS_CTL_OP(data) ((data)[0] & TRANS_CTL_OP_MASK) -#define TRANS_CTL_HDR(op, seg) ((op & TRANS_CTL_OP_MASK) | (seg << 7)) - -#define TRANS_CTL_OP_ACK 0x00 -#define TRANS_CTL_OP_FRIEND_POLL 0x01 -#define TRANS_CTL_OP_FRIEND_UPDATE 0x02 -#define TRANS_CTL_OP_FRIEND_REQ 0x03 -#define TRANS_CTL_OP_FRIEND_OFFER 0x04 -#define TRANS_CTL_OP_FRIEND_CLEAR 0x05 -#define TRANS_CTL_OP_FRIEND_CLEAR_CFM 0x06 -#define TRANS_CTL_OP_FRIEND_SUB_ADD 0x07 -#define TRANS_CTL_OP_FRIEND_SUB_REM 0x08 -#define TRANS_CTL_OP_FRIEND_SUB_CFM 0x09 -#define TRANS_CTL_OP_HEARTBEAT 0x0a +#define TRANS_SEQ_AUTH_NVAL 0xffffffffffffffff + +#define BLE_MESH_SDU_UNSEG_MAX 11 +#define BLE_MESH_CTL_SEG_SDU_MAX 8 +#define BLE_MESH_APP_SEG_SDU_MAX 12 +#define BLE_MESH_TX_SDU_MAX (CONFIG_BLE_MESH_TX_SEG_MAX * 12) + +#define TRANS_SEQ_ZERO_MASK ((uint16_t)BIT_MASK(13)) +#define TRANS_CTL_OP_MASK ((uint8_t)BIT_MASK(7)) +#define TRANS_CTL_OP(data) ((data)[0] & TRANS_CTL_OP_MASK) +#define TRANS_CTL_HDR(op, seg) (((op) & TRANS_CTL_OP_MASK) | ((seg) << 7)) + +#define TRANS_CTL_OP_ACK 0x00 +#define TRANS_CTL_OP_FRIEND_POLL 0x01 +#define TRANS_CTL_OP_FRIEND_UPDATE 0x02 +#define TRANS_CTL_OP_FRIEND_REQ 0x03 +#define TRANS_CTL_OP_FRIEND_OFFER 0x04 +#define TRANS_CTL_OP_FRIEND_CLEAR 0x05 +#define TRANS_CTL_OP_FRIEND_CLEAR_CFM 0x06 +#define TRANS_CTL_OP_FRIEND_SUB_ADD 0x07 +#define TRANS_CTL_OP_FRIEND_SUB_REM 0x08 +#define TRANS_CTL_OP_FRIEND_SUB_CFM 0x09 +#define TRANS_CTL_OP_HEARTBEAT 0x0A +#define TRANS_CTL_OP_PATH_REQ 0x0B +#define TRANS_CTL_OP_PATH_REPLY 0x0C +#define TRANS_CTL_OP_PATH_CFM 0x0D +#define TRANS_CTL_OP_PATH_ECHO_REQ 0x0E +#define TRANS_CTL_OP_PATH_ECHO_REPLY 0x0F +#define TRANS_CTL_OP_DEP_NODE_UPDATE 0x10 +#define TRANS_CTL_OP_PATH_REQ_SOLIC 0x11 struct bt_mesh_ctl_friend_poll { uint8_t fsn; -} __packed; +} __attribute__((packed)); struct bt_mesh_ctl_friend_update { uint8_t flags; uint32_t iv_index; uint8_t md; -} __packed; +} __attribute__((packed)); struct bt_mesh_ctl_friend_req { uint8_t criteria; @@ -57,7 +64,7 @@ struct bt_mesh_ctl_friend_req { uint16_t prev_addr; uint8_t num_elem; uint16_t lpn_counter; -} __packed; +} __attribute__((packed)); struct bt_mesh_ctl_friend_offer { uint8_t recv_win; @@ -65,27 +72,27 @@ struct bt_mesh_ctl_friend_offer { uint8_t sub_list_size; int8_t rssi; uint16_t frnd_counter; -} __packed; +} __attribute__((packed)); struct bt_mesh_ctl_friend_clear { uint16_t lpn_addr; uint16_t lpn_counter; -} __packed; +} __attribute__((packed)); struct bt_mesh_ctl_friend_clear_confirm { uint16_t lpn_addr; uint16_t lpn_counter; -} __packed; +} __attribute__((packed)); #define BLE_MESH_FRIEND_SUB_MIN_LEN (1 + 2) struct bt_mesh_ctl_friend_sub { uint8_t xact; uint16_t addr_list[5]; -} __packed; +} __attribute__((packed)); struct bt_mesh_ctl_friend_sub_confirm { uint8_t xact; -} __packed; +} __attribute__((packed)); uint8_t bt_mesh_get_seg_retrans_num(void); @@ -93,11 +100,11 @@ int32_t bt_mesh_get_seg_retrans_timeout(uint8_t ttl); void bt_mesh_set_hb_sub_dst(uint16_t addr); -struct bt_mesh_app_key *bt_mesh_app_key_find(uint16_t app_idx); +struct bt_mesh_app_key *bt_mesh_app_key_get(uint16_t app_idx); bool bt_mesh_tx_in_progress(void); -void bt_mesh_rx_reset(bool erase); +void bt_mesh_rx_reset(void); void bt_mesh_tx_reset(void); void bt_mesh_rx_reset_single(uint16_t src); void bt_mesh_tx_reset_single(uint16_t dst); @@ -114,12 +121,10 @@ 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); -bool bt_mesh_rpl_check(struct bt_mesh_net_rx *rx, struct bt_mesh_rpl **match); - void bt_mesh_heartbeat_send(void); -int bt_mesh_app_key_get(const struct bt_mesh_subnet *subnet, uint16_t app_idx, - const uint8_t **key, uint8_t *aid, uint8_t role, uint16_t dst); +int bt_mesh_upper_key_get(const struct bt_mesh_subnet *subnet, uint16_t app_idx, + const uint8_t **key, uint8_t *aid, uint16_t dst); #ifdef __cplusplus } diff --git a/components/bt/esp_ble_mesh/models/client/client_common.c b/components/bt/esp_ble_mesh/models/client/client_common.c index 724087c059ba..3b4495fe53c3 100644 --- a/components/bt/esp_ble_mesh/models/client/client_common.c +++ b/components/bt/esp_ble_mesh/models/client/client_common.c @@ -14,6 +14,8 @@ #include "mesh/client_common.h" #include "mesh/common.h" +#include "mesh_v1.1/utils.h" + #define HCI_TIME_FOR_START_ADV K_MSEC(5) /* Three adv related hci commands may take 4 ~ 5ms */ static bt_mesh_client_node_t *bt_mesh_client_pick_node(sys_slist_t *list, uint16_t tx_dst) @@ -28,7 +30,7 @@ static bt_mesh_client_node_t *bt_mesh_client_pick_node(sys_slist_t *list, uint16 } for (cur = sys_slist_peek_head(list); - cur != NULL; cur = sys_slist_peek_next(cur)) { + cur != NULL; cur = sys_slist_peek_next(cur)) { node = (bt_mesh_client_node_t *)cur; if (node->ctx.addr == tx_dst) { bt_mesh_list_unlock(); @@ -121,7 +123,7 @@ static bool bt_mesh_client_check_node_in_list(sys_slist_t *list, uint16_t tx_dst } for (cur = sys_slist_peek_head(list); - cur != NULL; cur = sys_slist_peek_next(cur)) { + cur != NULL; cur = sys_slist_peek_next(cur)) { node = (bt_mesh_client_node_t *)cur; if (node->ctx.addr == tx_dst) { bt_mesh_list_unlock(); @@ -151,12 +153,22 @@ static uint32_t bt_mesh_client_get_status_op(const bt_mesh_client_op_pair_t *op_ return 0; } -static int32_t bt_mesh_get_adv_duration(void) +static int32_t bt_mesh_get_adv_duration(struct bt_mesh_msg_ctx *ctx) { - uint16_t duration, adv_int; - uint8_t xmit; + uint16_t duration = 0, adv_int = 0; + uint8_t xmit = 0; + + /* Initialize with network transmission */ + xmit = bt_mesh_net_transmit_get(); + + if (bt_mesh_tag_immutable_cred(ctx->send_tag)) { +#if CONFIG_BLE_MESH_DF_SRV + if (ctx->send_cred == BLE_MESH_DIRECTED_CRED) { + xmit = bt_mesh_direct_net_transmit_get(); /* Directed network transmission */ + } +#endif + } - xmit = bt_mesh_net_transmit_get(); /* Network transmit */ adv_int = BLE_MESH_TRANSMIT_INT(xmit); duration = (BLE_MESH_TRANSMIT_COUNT(xmit) + 1) * (adv_int + 10); @@ -167,29 +179,31 @@ static int32_t bt_mesh_client_calc_timeout(struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *msg, uint32_t opcode, int32_t timeout) { - int32_t seg_retrans_to = 0, duration = 0, time = 0; - uint8_t seg_count = 0, seg_retrans_num = 0; + int32_t seg_rtx_to = 0, duration = 0, time = 0; + uint8_t seg_count = 0, seg_rtx_num = 0; bool need_seg = false; uint8_t mic_size = 0; - if (msg->len > BLE_MESH_SDU_UNSEG_MAX || ctx->send_rel) { + if (msg->len > BLE_MESH_SDU_UNSEG_MAX || + bt_mesh_tag_send_segmented(ctx->send_tag)) { need_seg = true; /* Needs segmentation */ } - mic_size = (need_seg && net_buf_simple_tailroom(msg) >= BLE_MESH_MIC_LONG) ? + mic_size = (need_seg && ctx->send_szmic == BLE_MESH_SEG_SZMIC_LONG && + net_buf_simple_tailroom(msg) >= BLE_MESH_MIC_LONG) ? BLE_MESH_MIC_LONG : BLE_MESH_MIC_SHORT; if (need_seg) { /* Based on the message length, calculate how many segments are needed. * All the messages sent from here are access messages. */ - seg_retrans_num = bt_mesh_get_seg_retrans_num(); - seg_retrans_to = bt_mesh_get_seg_retrans_timeout(ctx->send_ttl); + seg_rtx_num = bt_mesh_get_seg_retrans_num(); + seg_rtx_to = bt_mesh_get_seg_retrans_timeout(ctx->send_ttl); seg_count = (msg->len + mic_size - 1) / 12U + 1U; - duration = bt_mesh_get_adv_duration(); + duration = bt_mesh_get_adv_duration(ctx); - /* Currenlty only consider the time consumption of the same segmented + /* Currently only consider the time consumption of the same segmented * messages, but if there are other messages between any two retrans- * missions of the same segmented messages, then the whole time will * be longer. @@ -200,7 +214,7 @@ static int32_t bt_mesh_client_calc_timeout(struct bt_mesh_msg_ctx *ctx, * the attempts reaches ZERO when the dst is a unicast address. */ int32_t seg_duration = seg_count * (duration + HCI_TIME_FOR_START_ADV); - time = (seg_duration + seg_retrans_to) * seg_retrans_num; + time = (seg_duration + seg_rtx_to) * seg_rtx_num; BT_INFO("Original timeout %dms, calculated timeout %dms", timeout, time); @@ -274,11 +288,6 @@ int bt_mesh_client_send_msg(bt_mesh_client_common_param_t *param, return -EINVAL; } - if (bt_mesh_set_client_model_role(param->model, param->msg_role)) { - BT_ERR("Failed to set client role"); - return -EIO; - } - if (need_ack == false || !BLE_MESH_ADDR_IS_UNICAST(param->ctx.addr)) { /* 1. If this is an unacknowledged message, send it directly. * 2. If this is an acknowledged message, but the destination @@ -311,11 +320,11 @@ int bt_mesh_client_send_msg(bt_mesh_client_common_param_t *param, } memcpy(&node->ctx, ¶m->ctx, sizeof(struct bt_mesh_msg_ctx)); - node->ctx.model = param->model; + node->model = param->model; node->opcode = param->opcode; node->op_pending = bt_mesh_client_get_status_op(client->op_pair, client->op_pair_size, param->opcode); if (node->op_pending == 0U) { - BT_ERR("Not found the status opcode in op_pair list"); + BT_ERR("Status opcode not found in op_pair list, opcode 0x%08x", param->opcode); bt_mesh_free(node); return -EINVAL; } @@ -348,20 +357,6 @@ int bt_mesh_client_send_msg(bt_mesh_client_common_param_t *param, static bt_mesh_mutex_t client_model_lock; -static inline void bt_mesh_client_model_mutex_new(void) -{ - if (!client_model_lock.mutex) { - bt_mesh_mutex_create(&client_model_lock); - } -} - -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_client_model_mutex_free(void) -{ - bt_mesh_mutex_free(&client_model_lock); -} -#endif /* CONFIG_BLE_MESH_DEINIT */ - void bt_mesh_client_model_lock(void) { bt_mesh_mutex_lock(&client_model_lock); @@ -374,7 +369,7 @@ void bt_mesh_client_model_unlock(void) int bt_mesh_client_init(struct bt_mesh_model *model) { - bt_mesh_client_internal_data_t *data = NULL; + bt_mesh_client_internal_data_t *internal = NULL; bt_mesh_client_user_data_t *client = NULL; if (!model || !model->op) { @@ -388,23 +383,23 @@ int bt_mesh_client_init(struct bt_mesh_model *model) return -EINVAL; } - if (!client->internal_data) { - data = bt_mesh_calloc(sizeof(bt_mesh_client_internal_data_t)); - if (!data) { - BT_ERR("%s, Out of memory", __func__); - return -ENOMEM; - } - - /* Init the client data queue */ - sys_slist_init(&data->queue); + if (client->internal_data) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } - client->model = model; - client->internal_data = data; - } else { - bt_mesh_client_clear_list(client->internal_data); + internal = bt_mesh_calloc(sizeof(bt_mesh_client_internal_data_t)); + if (!internal) { + BT_ERR("%s, Out of memory", __func__); + return -ENOMEM; } - bt_mesh_client_model_mutex_new(); + sys_slist_init(&internal->queue); + + client->model = model; + client->internal_data = internal; + + bt_mesh_mutex_create(&client_model_lock); return 0; } @@ -434,7 +429,7 @@ int bt_mesh_client_deinit(struct bt_mesh_model *model) client->internal_data = NULL; } - bt_mesh_client_model_mutex_free(); + bt_mesh_mutex_free(&client_model_lock); return 0; } @@ -445,12 +440,12 @@ int bt_mesh_client_free_node(bt_mesh_client_node_t *node) bt_mesh_client_internal_data_t *internal = NULL; bt_mesh_client_user_data_t *client = NULL; - if (!node || !node->ctx.model) { + if (!node || !node->model) { BT_ERR("Invalid client list item"); return -EINVAL; } - client = (bt_mesh_client_user_data_t *)node->ctx.model->user_data; + client = (bt_mesh_client_user_data_t *)node->model->user_data; if (!client) { BT_ERR("Invalid client user data"); return -EINVAL; @@ -466,6 +461,7 @@ int bt_mesh_client_free_node(bt_mesh_client_node_t *node) bt_mesh_list_lock(); sys_slist_find_and_remove(&internal->queue, &node->client_node); bt_mesh_list_unlock(); + // Free the node bt_mesh_free(node); @@ -494,27 +490,3 @@ int bt_mesh_client_clear_list(void *data) return 0; } - -int bt_mesh_set_client_model_role(struct bt_mesh_model *model, uint8_t role) -{ - bt_mesh_client_user_data_t *client = NULL; - - if (!model) { - BT_ERR("Invalid client model"); - return -EINVAL; - } - - client = (bt_mesh_client_user_data_t *)model->user_data; - if (!client) { - BT_ERR("Invalid client user data"); - return -EINVAL; - } - - if (role >= ROLE_NVAL) { - BT_ERR("Invalid client role 0x%02x", role); - return -EINVAL; - } - - client->msg_role = role; - return 0; -} diff --git a/components/bt/esp_ble_mesh/models/client/generic_client.c b/components/bt/esp_ble_mesh/models/client/generic_client.c index 1bb1ce131a45..af09dc1befc2 100644 --- a/components/bt/esp_ble_mesh/models/client/generic_client.c +++ b/components/bt/esp_ble_mesh/models/client/generic_client.c @@ -65,7 +65,7 @@ #define BLE_MESH_GEN_MANU_PROPERTIES_GET_MSG_LEN (2 + 0 + 4) #define BLE_MESH_GEN_MANU_PROPERTY_GET_MSG_LEN (2 + 2 + 4) #define BLE_MESH_GEN_MANU_PROPERTY_SET_MSG_LEN (1 + 3 + 4) -#define BLE_MESH_GEN_CLINET_PROPERTIES_GET_MSG_LEN (1 + 2 + 4) +#define BLE_MESH_GEN_CLIENT_PROPERTIES_GET_MSG_LEN (1 + 2 + 4) #define BLE_MESH_GEN_GET_STATE_MSG_LEN (2 + 2 + 4) @@ -106,40 +106,17 @@ static const bt_mesh_client_op_pair_t gen_op_pair[] = { static bt_mesh_mutex_t generic_client_lock; -static inline void bt_mesh_generic_client_mutex_new(void) -{ - if (!generic_client_lock.mutex) { - bt_mesh_mutex_create(&generic_client_lock); - } -} - -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_generic_client_mutex_free(void) -{ - bt_mesh_mutex_free(&generic_client_lock); -} -#endif /* CONFIG_BLE_MESH_DEINIT */ - -static inline void bt_mesh_generic_client_lock(void) -{ - bt_mesh_mutex_lock(&generic_client_lock); -} - -static inline void bt_mesh_generic_client_unlock(void) -{ - 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_model *model = NULL; struct bt_mesh_msg_ctx ctx = {0}; uint32_t opcode = 0U; BT_WARN("Receive generic status message timeout"); - bt_mesh_generic_client_lock(); + bt_mesh_mutex_lock(&generic_client_lock); timer = CONTAINER_OF(work, struct k_delayed_work, work); @@ -148,15 +125,14 @@ static void timeout_handler(struct k_work *work) if (node) { memcpy(&ctx, &node->ctx, sizeof(ctx)); opcode = node->opcode; + model = node->model; 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); + opcode, BTC_BLE_MESH_EVT_GENERIC_CLIENT_TIMEOUT, model, &ctx, NULL, 0); } } - bt_mesh_generic_client_unlock(); - - return; + bt_mesh_mutex_unlock(&generic_client_lock); } static void generic_status(struct bt_mesh_model *model, @@ -525,7 +501,7 @@ static void generic_status(struct bt_mesh_model *model, buf->data = val; buf->len = len; - bt_mesh_generic_client_lock(); + bt_mesh_mutex_lock(&generic_client_lock); node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true); if (!node) { @@ -579,7 +555,7 @@ static void generic_status(struct bt_mesh_model *model, } } - bt_mesh_generic_client_unlock(); + bt_mesh_mutex_unlock(&generic_client_lock); switch (ctx->recv_op) { case BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS: { @@ -629,8 +605,6 @@ static void generic_status(struct bt_mesh_model *model, } bt_mesh_free(val); - - return; } const struct bt_mesh_model_op bt_mesh_gen_onoff_cli_op[] = { @@ -1147,24 +1121,25 @@ static int generic_client_init(struct bt_mesh_model *model) return -EINVAL; } - if (!client->internal_data) { - internal = bt_mesh_calloc(sizeof(generic_internal_data_t)); - if (!internal) { - BT_ERR("%s, Out of memory", __func__); - return -ENOMEM; - } - - sys_slist_init(&internal->queue); + if (client->internal_data) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } - client->model = model; - client->op_pair_size = ARRAY_SIZE(gen_op_pair); - client->op_pair = gen_op_pair; - client->internal_data = internal; - } else { - bt_mesh_client_clear_list(client->internal_data); + internal = bt_mesh_calloc(sizeof(generic_internal_data_t)); + if (!internal) { + BT_ERR("%s, Out of memory", __func__); + return -ENOMEM; } - bt_mesh_generic_client_mutex_new(); + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(gen_op_pair); + client->op_pair = gen_op_pair; + client->internal_data = internal; + + bt_mesh_mutex_create(&generic_client_lock); return 0; } @@ -1194,7 +1169,7 @@ static int generic_client_deinit(struct bt_mesh_model *model) client->internal_data = NULL; } - bt_mesh_generic_client_mutex_free(); + bt_mesh_mutex_free(&generic_client_lock); return 0; } diff --git a/components/bt/esp_ble_mesh/models/client/include/mesh/client_common.h b/components/bt/esp_ble_mesh/models/client/include/mesh/client_common.h index 973c00535991..25834a1d6bcf 100644 --- a/components/bt/esp_ble_mesh/models/client/include/mesh/client_common.h +++ b/components/bt/esp_ble_mesh/models/client/include/mesh/client_common.h @@ -25,7 +25,7 @@ typedef struct { struct bt_mesh_model *model; /** Size of the opcode pair table */ - int op_pair_size; + uint32_t op_pair_size; /** Pointer to the opcode pair table */ const bt_mesh_client_op_pair_t *op_pair; @@ -41,23 +41,29 @@ typedef struct { * * @return None */ - void (*publish_status)(uint32_t opcode, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); + void (*publish_status)(uint32_t opcode, struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); /** Pointer to the internal data of client model */ void *internal_data; + /** Pointer to the vendor data of client model */ + void *vendor_data; + /** Role of the device to which the client model belongs */ - uint8_t msg_role; + uint8_t msg_role __attribute__((deprecated)); } bt_mesh_client_user_data_t; /** Client model internal data context */ -typedef struct { +typedef struct { sys_slist_t queue; } bt_mesh_client_internal_data_t; /** Client model sending message related context */ typedef struct { sys_snode_t client_node; + struct bt_mesh_model *model; /* Pointer to the client model */ struct bt_mesh_msg_ctx ctx; /* Message context */ uint32_t opcode; /* Message opcode */ uint32_t op_pending; /* Expected status message opcode */ @@ -71,7 +77,7 @@ typedef struct { struct bt_mesh_model *model; /* Pointer to the client model */ struct bt_mesh_msg_ctx ctx; /* Message context */ int32_t msg_timeout; /* Time to get corresponding response */ - uint8_t msg_role; /* Role (Node/Provisioner) of the device */ + uint8_t msg_role __attribute__((deprecated)); /* Role (Node/Provisioner) of the device */ const struct bt_mesh_send_cb *cb; /* User defined callback function */ void *cb_data; /* User defined callback value */ } bt_mesh_client_common_param_t; @@ -95,7 +101,8 @@ int bt_mesh_client_deinit(struct bt_mesh_model *model); */ bt_mesh_client_node_t *bt_mesh_is_client_recv_publish_msg(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf, bool need_pub); + struct net_buf_simple *buf, + bool need_pub); int bt_mesh_client_send_msg(bt_mesh_client_common_param_t *param, struct net_buf_simple *msg, bool need_ack, @@ -105,16 +112,6 @@ int bt_mesh_client_free_node(bt_mesh_client_node_t *node); int bt_mesh_client_clear_list(void *data); -/** - * @brief Set role of the client model for internal use. - * - * @param[in] model: Pointer to the client model - * @param[in] role: Role of the device - * - * @return Zero - success, otherwise - fail - */ -int bt_mesh_set_client_model_role(struct bt_mesh_model *model, uint8_t role); - #ifdef __cplusplus } #endif diff --git a/components/bt/esp_ble_mesh/models/client/lighting_client.c b/components/bt/esp_ble_mesh/models/client/lighting_client.c index b83984ecacb4..6448a4108165 100644 --- a/components/bt/esp_ble_mesh/models/client/lighting_client.c +++ b/components/bt/esp_ble_mesh/models/client/lighting_client.c @@ -113,42 +113,19 @@ 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 bt_mesh_mutex_t light_client_lock; - -static void bt_mesh_light_client_mutex_new(void) -{ - if (!light_client_lock.mutex) { - bt_mesh_mutex_create(&light_client_lock); - } -} - -#if CONFIG_BLE_MESH_DEINIT -static void bt_mesh_light_client_mutex_free(void) -{ - bt_mesh_mutex_free(&light_client_lock); -} -#endif /* CONFIG_BLE_MESH_DEINIT */ - -static void bt_mesh_light_client_lock(void) -{ - bt_mesh_mutex_lock(&light_client_lock); -} - -static void bt_mesh_light_client_unlock(void) -{ - bt_mesh_mutex_unlock(&light_client_lock); -} +static bt_mesh_mutex_t lighting_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_model *model = NULL; struct bt_mesh_msg_ctx ctx = {0}; uint32_t opcode = 0U; BT_WARN("Receive light status message timeout"); - bt_mesh_light_client_lock(); + bt_mesh_mutex_lock(&lighting_client_lock); timer = CONTAINER_OF(work, struct k_delayed_work, work); @@ -157,15 +134,14 @@ static void timeout_handler(struct k_work *work) if (node) { memcpy(&ctx, &node->ctx, sizeof(ctx)); opcode = node->opcode; + model = node->model; 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); + opcode, BTC_BLE_MESH_EVT_LIGHTING_CLIENT_TIMEOUT, model, &ctx, NULL, 0); } } - bt_mesh_light_client_unlock(); - - return; + bt_mesh_mutex_unlock(&lighting_client_lock); } static void light_status(struct bt_mesh_model *model, @@ -640,7 +616,7 @@ static void light_status(struct bt_mesh_model *model, buf->data = val; buf->len = len; - bt_mesh_light_client_lock(); + bt_mesh_mutex_lock(&lighting_client_lock); node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true); if (!node) { @@ -705,7 +681,7 @@ static void light_status(struct bt_mesh_model *model, } } - bt_mesh_light_client_unlock(); + bt_mesh_mutex_unlock(&lighting_client_lock); switch (ctx->recv_op) { case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS: { @@ -719,8 +695,6 @@ static void light_status(struct bt_mesh_model *model, } bt_mesh_free(val); - - return; } const struct bt_mesh_model_op bt_mesh_light_lightness_cli_op[] = { @@ -1337,24 +1311,25 @@ static int lighting_client_init(struct bt_mesh_model *model) return -EINVAL; } - if (!client->internal_data) { - internal = bt_mesh_calloc(sizeof(light_internal_data_t)); - if (!internal) { - BT_ERR("%s, Out of memory", __func__); - return -ENOMEM; - } - - sys_slist_init(&internal->queue); + if (client->internal_data) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } - client->model = model; - client->op_pair_size = ARRAY_SIZE(light_op_pair); - client->op_pair = light_op_pair; - client->internal_data = internal; - } else { - bt_mesh_client_clear_list(client->internal_data); + internal = bt_mesh_calloc(sizeof(light_internal_data_t)); + if (!internal) { + BT_ERR("%s, Out of memory", __func__); + return -ENOMEM; } - bt_mesh_light_client_mutex_new(); + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(light_op_pair); + client->op_pair = light_op_pair; + client->internal_data = internal; + + bt_mesh_mutex_create(&lighting_client_lock); return 0; } @@ -1384,7 +1359,7 @@ static int lighting_client_deinit(struct bt_mesh_model *model) client->internal_data = NULL; } - bt_mesh_light_client_mutex_free(); + bt_mesh_mutex_free(&lighting_client_lock); return 0; } diff --git a/components/bt/esp_ble_mesh/models/client/sensor_client.c b/components/bt/esp_ble_mesh/models/client/sensor_client.c index 882d9ce0a001..fa093ff6fb24 100644 --- a/components/bt/esp_ble_mesh/models/client/sensor_client.c +++ b/components/bt/esp_ble_mesh/models/client/sensor_client.c @@ -44,40 +44,17 @@ static const bt_mesh_client_op_pair_t sensor_op_pair[] = { static bt_mesh_mutex_t sensor_client_lock; -static inline void bt_mesh_sensor_client_mutex_new(void) -{ - if (!sensor_client_lock.mutex) { - bt_mesh_mutex_create(&sensor_client_lock); - } -} - -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_sensor_client_mutex_free(void) -{ - bt_mesh_mutex_free(&sensor_client_lock); -} -#endif /* CONFIG_BLE_MESH_DEINIT */ - -static inline void bt_mesh_sensor_client_lock(void) -{ - bt_mesh_mutex_lock(&sensor_client_lock); -} - -static inline void bt_mesh_sensor_client_unlock(void) -{ - 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_model *model = NULL; struct bt_mesh_msg_ctx ctx = {0}; uint32_t opcode = 0U; BT_WARN("Receive sensor status message timeout"); - bt_mesh_sensor_client_lock(); + bt_mesh_mutex_lock(&sensor_client_lock); timer = CONTAINER_OF(work, struct k_delayed_work, work); @@ -86,15 +63,14 @@ static void timeout_handler(struct k_work *work) if (node) { memcpy(&ctx, &node->ctx, sizeof(ctx)); opcode = node->opcode; + model = node->model; 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); + opcode, BTC_BLE_MESH_EVT_SENSOR_CLIENT_TIMEOUT, model, &ctx, NULL, 0); } } - bt_mesh_sensor_client_unlock(); - - return; + bt_mesh_mutex_unlock(&sensor_client_lock); } static void sensor_status(struct bt_mesh_model *model, @@ -253,7 +229,7 @@ static void sensor_status(struct bt_mesh_model *model, buf->data = val; buf->len = len; - bt_mesh_sensor_client_lock(); + bt_mesh_mutex_lock(&sensor_client_lock); node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true); if (!node) { @@ -284,7 +260,7 @@ static void sensor_status(struct bt_mesh_model *model, } } - bt_mesh_sensor_client_unlock(); + bt_mesh_mutex_unlock(&sensor_client_lock); switch (ctx->recv_op) { case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS: { @@ -334,8 +310,6 @@ static void sensor_status(struct bt_mesh_model *model, } bt_mesh_free(val); - - return; } const struct bt_mesh_model_op bt_mesh_sensor_cli_op[] = { @@ -587,24 +561,25 @@ static int sensor_client_init(struct bt_mesh_model *model) return -EINVAL; } - if (!client->internal_data) { - internal = bt_mesh_calloc(sizeof(sensor_internal_data_t)); - if (!internal) { - BT_ERR("%s, Out of memory", __func__); - return -ENOMEM; - } - - sys_slist_init(&internal->queue); + if (client->internal_data) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } - client->model = model; - client->op_pair_size = ARRAY_SIZE(sensor_op_pair); - client->op_pair = sensor_op_pair; - client->internal_data = internal; - } else { - bt_mesh_client_clear_list(client->internal_data); + internal = bt_mesh_calloc(sizeof(sensor_internal_data_t)); + if (!internal) { + BT_ERR("%s, Out of memory", __func__); + return -ENOMEM; } - bt_mesh_sensor_client_mutex_new(); + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(sensor_op_pair); + client->op_pair = sensor_op_pair; + client->internal_data = internal; + + bt_mesh_mutex_create(&sensor_client_lock); return 0; } @@ -634,7 +609,7 @@ static int sensor_client_deinit(struct bt_mesh_model *model) client->internal_data = NULL; } - bt_mesh_sensor_client_mutex_free(); + bt_mesh_mutex_free(&sensor_client_lock); return 0; } diff --git a/components/bt/esp_ble_mesh/models/client/time_scene_client.c b/components/bt/esp_ble_mesh/models/client/time_scene_client.c index 1584b5cdbeca..8cdc4800ee90 100644 --- a/components/bt/esp_ble_mesh/models/client/time_scene_client.c +++ b/components/bt/esp_ble_mesh/models/client/time_scene_client.c @@ -60,40 +60,17 @@ static const bt_mesh_client_op_pair_t time_scene_op_pair[] = { static bt_mesh_mutex_t time_scene_client_lock; -static inline void bt_mesh_time_scene_client_mutex_new(void) -{ - if (!time_scene_client_lock.mutex) { - bt_mesh_mutex_create(&time_scene_client_lock); - } -} - -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_time_scene_client_mutex_free(void) -{ - bt_mesh_mutex_free(&time_scene_client_lock); -} -#endif /* CONFIG_BLE_MESH_DEINIT */ - -static inline void bt_mesh_time_scene_client_lock(void) -{ - bt_mesh_mutex_lock(&time_scene_client_lock); -} - -static inline void bt_mesh_time_scene_client_unlock(void) -{ - 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_model *model = NULL; struct bt_mesh_msg_ctx ctx = {0}; uint32_t opcode = 0U; BT_WARN("Receive time scene status message timeout"); - bt_mesh_time_scene_client_lock(); + bt_mesh_mutex_lock(&time_scene_client_lock); timer = CONTAINER_OF(work, struct k_delayed_work, work); @@ -102,15 +79,14 @@ static void timeout_handler(struct k_work *work) if (node) { memcpy(&ctx, &node->ctx, sizeof(ctx)); opcode = node->opcode; + model = node->model; 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); + opcode, BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_TIMEOUT, model, &ctx, NULL, 0); } } - bt_mesh_time_scene_client_unlock(); - - return; + bt_mesh_mutex_unlock(&time_scene_client_lock); } static void time_scene_status(struct bt_mesh_model *model, @@ -290,7 +266,7 @@ static void time_scene_status(struct bt_mesh_model *model, buf->data = val; buf->len = len; - bt_mesh_time_scene_client_lock(); + bt_mesh_mutex_lock(&time_scene_client_lock); node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true); if (!node) { @@ -328,7 +304,7 @@ static void time_scene_status(struct bt_mesh_model *model, } } - bt_mesh_time_scene_client_unlock(); + bt_mesh_mutex_unlock(&time_scene_client_lock); switch (ctx->recv_op) { case BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS: { @@ -342,8 +318,6 @@ static void time_scene_status(struct bt_mesh_model *model, } bt_mesh_free(val); - - return; } const struct bt_mesh_model_op bt_mesh_time_cli_op[] = { @@ -643,24 +617,25 @@ static int time_scene_client_init(struct bt_mesh_model *model) return -EINVAL; } - if (!client->internal_data) { - internal = bt_mesh_calloc(sizeof(time_scene_internal_data_t)); - if (!internal) { - BT_ERR("%s, Out of memory", __func__); - return -ENOMEM; - } - - sys_slist_init(&internal->queue); + if (client->internal_data) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } - client->model = model; - client->op_pair_size = ARRAY_SIZE(time_scene_op_pair); - client->op_pair = time_scene_op_pair; - client->internal_data = internal; - } else { - bt_mesh_client_clear_list(client->internal_data); + internal = bt_mesh_calloc(sizeof(time_scene_internal_data_t)); + if (!internal) { + BT_ERR("%s, Out of memory", __func__); + return -ENOMEM; } - bt_mesh_time_scene_client_mutex_new(); + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(time_scene_op_pair); + client->op_pair = time_scene_op_pair; + client->internal_data = internal; + + bt_mesh_mutex_create(&time_scene_client_lock); return 0; } @@ -690,7 +665,7 @@ static int time_scene_client_deinit(struct bt_mesh_model *model) client->internal_data = NULL; } - bt_mesh_time_scene_client_mutex_free(); + bt_mesh_mutex_free(&time_scene_client_lock); return 0; } diff --git a/components/bt/esp_ble_mesh/models/common/include/mesh/device_property.h b/components/bt/esp_ble_mesh/models/common/include/mesh/device_property.h index 643387436b53..33d75d4bd0dc 100644 --- a/components/bt/esp_ble_mesh/models/common/include/mesh/device_property.h +++ b/components/bt/esp_ble_mesh/models/common/include/mesh/device_property.h @@ -852,7 +852,7 @@ typedef uint16_t bt_mesh_voltage_t; /* This characteristic aggregates the Electric Current characteristic and instance of * the Time Exponential 8 characteristic. */ -typedef struct __packed average_current { +typedef struct __attribute__((packed)) average_current { bt_mesh_electric_current_t electric_current; bt_mesh_time_exponential_8_t sensing_duration; } bt_mesh_average_current_t; @@ -860,7 +860,7 @@ typedef struct __packed average_current { /* This characteristic aggregates the Voltage characteristic and instance of the Time * Exponential 8 characteristic. */ -typedef struct __packed average_voltage { +typedef struct __attribute__((packed)) average_voltage { bt_mesh_voltage_t voltage; bt_mesh_time_exponential_8_t sensing_duration; } bt_mesh_average_voltage_t; @@ -896,7 +896,7 @@ typedef uint16_t bt_mesh_chromaticity_coordinate_t; /* This characteristic represents a chromaticity coordinate as a tuple with an x and * y coordinate. */ -typedef struct __packed chromaticity_coordinates { +typedef struct __attribute__((packed)) chromaticity_coordinates { bt_mesh_chromaticity_coordinate_t chromaticity_x_coordinate; bt_mesh_chromaticity_coordinate_t chromaticity_y_coordinate; } bt_mesh_chromaticity_coordinates_t; @@ -913,7 +913,7 @@ typedef uint16_t bt_mesh_correlated_color_temperature_t; * consisting of the Correlated Color Temperature characteristic and the Chromatic * Distance From Planckian characteristic. */ -typedef struct __packed chromaticity_in_cct_and_duv_values { +typedef struct __attribute__((packed)) chromaticity_in_cct_and_duv_values { bt_mesh_correlated_color_temperature_t correlated_color_temperature; bt_mesh_chromatic_distance_from_planckian_t chromaticity_distance_from_planckian; } bt_mesh_chromaticity_in_cct_and_duv_values_t; @@ -969,7 +969,7 @@ typedef uint8_t bt_mesh_date_utc_t[3]; /* This characteristic aggregates two instances of the Electric Current characteristic * to represent a range of Electric Current values. */ -typedef struct __packed electric_current_range { +typedef struct __attribute__((packed)) electric_current_range { bt_mesh_electric_current_t minimum_electric_current_value; bt_mesh_electric_current_t maximum_electric_current_value; } bt_mesh_electric_current_range_t; @@ -977,7 +977,7 @@ typedef struct __packed electric_current_range { /* This characteristic aggregates three instances of the Electric Current characteristic * to represent a specification of electric current values. */ -typedef struct __packed electric_current_specification { +typedef struct __attribute__((packed)) electric_current_specification { bt_mesh_electric_current_t minimum_electric_current_value; bt_mesh_electric_current_t typical_electric_current_value; bt_mesh_electric_current_t maximum_electric_current_value; @@ -986,7 +986,7 @@ typedef struct __packed electric_current_specification { /* This characteristic aggregates four instances of the Electric Current characteristic * with a Sensing Duration to represent a set of statistical electric current values. */ -typedef struct __packed electric_current_statistics { +typedef struct __attribute__((packed)) electric_current_statistics { bt_mesh_electric_current_t average_electric_current_value; bt_mesh_electric_current_t standard_electric_current_value; bt_mesh_electric_current_t minimum_electric_current_value; @@ -1013,7 +1013,7 @@ typedef uint8_t bt_mesh_time_decihour_8_t; /* This characteristic aggregates the Energy characteristic, and two instances of * the Time Decihour 8 characteristic, to represent energy use in a period of day. */ -typedef struct __packed energy_in_a_period_of_day { +typedef struct __attribute__((packed)) energy_in_a_period_of_day { bt_mesh_energy_t energy_value; bt_mesh_time_decihour_8_t start_time; bt_mesh_time_decihour_8_t end_time; @@ -1031,7 +1031,7 @@ typedef uint16_t bt_mesh_time_second_16_t; * Time Decihour 8 characteristic and an instance of the Sensing Duration characteristic, * to represent statistical values of events. */ -typedef struct __packed event_statistics { +typedef struct __attribute__((packed)) event_statistics { bt_mesh_count_16_t number_of_events; bt_mesh_time_second_16_t average_event_duration; bt_mesh_time_exponential_8_t time_elapsed_since_last_event; @@ -1106,7 +1106,7 @@ typedef uint16_t bt_mesh_luminous_flux_t; /* This characteristic aggregates two instances of the Luminous Flux characteristic * to represent a luminous flux range. */ -typedef struct __packed luminous_flux_range { +typedef struct __attribute__((packed)) luminous_flux_range { bt_mesh_luminous_flux_t minimum_luminous_flux; bt_mesh_luminous_flux_t maximum_luminous_flux; } bt_mesh_luminous_flux_range_t; @@ -1178,7 +1178,7 @@ typedef uint8_t bt_mesh_power_t[3]; /* This characteristic aggregates three instances of the Power characteristic to * represent a specification of Power values. */ -typedef struct __packed power_specification { +typedef struct __attribute__((packed)) power_specification { bt_mesh_power_t minimum_power_value; bt_mesh_power_t typical_power_value; bt_mesh_power_t maximum_power_value; @@ -1188,7 +1188,7 @@ typedef struct __packed power_specification { * the Electric Current characteristic to represent a relative value in an electric * current range. */ -typedef struct __packed relative_runtime_in_a_current_range { +typedef struct __attribute__((packed)) relative_runtime_in_a_current_range { bt_mesh_percentage_8_t relative_runtime_value; bt_mesh_electric_current_t minimum_current; bt_mesh_electric_current_t maximum_current; @@ -1197,7 +1197,7 @@ typedef struct __packed relative_runtime_in_a_current_range { /* This characteristic aggregates the Percentage 8 characteristic and two instances of * the Generic Level characteristic to represent a runtime in a generic level range. */ -typedef struct __packed relative_runtime_in_a_generic_level_range { +typedef struct __attribute__((packed)) relative_runtime_in_a_generic_level_range { bt_mesh_percentage_8_t relative_value; bt_mesh_generic_level_t minimum_generic_level; bt_mesh_generic_level_t maximum_generic_level; @@ -1206,7 +1206,7 @@ typedef struct __packed relative_runtime_in_a_generic_level_range { /* This characteristic aggregates the Percentage 8 characteristic, and two instances of * the Time Decihour 8 characteristic. */ -typedef struct __packed relative_value_in_a_period_of_day { +typedef struct __attribute__((packed)) relative_value_in_a_period_of_day { bt_mesh_percentage_8_t relative_value; bt_mesh_time_decihour_8_t start_time; bt_mesh_time_decihour_8_t end_time; @@ -1215,7 +1215,7 @@ typedef struct __packed relative_value_in_a_period_of_day { /* This characteristic aggregates the Percentage 8 characteristic, and two instances of * the Temperature characteristic. */ -typedef struct __packed relative_value_in_a_temperature_range { +typedef struct __attribute__((packed)) relative_value_in_a_temperature_range { bt_mesh_percentage_8_t relative_value; bt_mesh_temperature_t minimum_temperature_value; bt_mesh_temperature_t maximum_temperature_value; @@ -1224,7 +1224,7 @@ typedef struct __packed relative_value_in_a_temperature_range { /* This characteristic aggregates the Percentage 8 characteristic and two instances of * the Voltage characteristic to represent a relative value in a voltage range. */ -typedef struct __packed relative_value_in_a_voltage_range { +typedef struct __attribute__((packed)) relative_value_in_a_voltage_range { bt_mesh_percentage_8_t relative_value; bt_mesh_voltage_t minimum_voltage; bt_mesh_voltage_t maximum_voltage; @@ -1233,7 +1233,7 @@ typedef struct __packed relative_value_in_a_voltage_range { /* This characteristic aggregates the Percentage 8 characteristic and two instances of * the Illuminance characteristic to represent a relative value in a illuminance range. */ -typedef struct __packed relative_value_in_an_illuminance_range { +typedef struct __attribute__((packed)) relative_value_in_an_illuminance_range { bt_mesh_percentage_8_t relative_value; bt_mesh_illuminance_t minimum_illuminance; bt_mesh_illuminance_t maximum_illuminance; @@ -1251,7 +1251,7 @@ typedef int8_t bt_mesh_temperature_8_t; * of the Time Decihour 8 characteristic, to represent a temperature value in a period * of day. */ -typedef struct __packed temperature_8_in_a_period_of_day { +typedef struct __attribute__((packed)) temperature_8_in_a_period_of_day { bt_mesh_temperature_8_t temperature; bt_mesh_time_decihour_8_t start_time; bt_mesh_time_decihour_8_t end_time; @@ -1260,7 +1260,7 @@ typedef struct __packed temperature_8_in_a_period_of_day { /* This characteristic aggregates four instances of the Temperature 8 characteristic, * and one instance of the Time Exponential 8 characteristic. */ -typedef struct __packed temperature_8_statistics { +typedef struct __attribute__((packed)) temperature_8_statistics { bt_mesh_temperature_8_t average; bt_mesh_temperature_8_t standard_deviation_value; bt_mesh_temperature_8_t minimum_value; @@ -1271,7 +1271,7 @@ typedef struct __packed temperature_8_statistics { /* This characteristic aggregates two instances of the Temperature characteristic to * represent a temperature range. */ -typedef struct __packed temperature_range { +typedef struct __attribute__((packed)) temperature_range { bt_mesh_temperature_t minimum_temperature; bt_mesh_temperature_t maximum_temperature; } bt_mesh_temperature_range_t; @@ -1279,7 +1279,7 @@ typedef struct __packed temperature_range { /* This characteristic aggregates four instances of the Temperature characteristic, * and one instance of the Time Exponential 8 characteristic. */ -typedef struct __packed temperature_statistics { +typedef struct __attribute__((packed)) temperature_statistics { bt_mesh_temperature_t average_temperature; bt_mesh_temperature_t standard_deviation_temperature; bt_mesh_temperature_t minimum_temperature; @@ -1313,7 +1313,7 @@ typedef uint8_t bt_mesh_time_second_8_t; /* This characteristic aggregates three instances of the Voltage characteristic to * represent a specification of voltage values. */ -typedef struct __packed voltage_specification { +typedef struct __attribute__((packed)) voltage_specification { bt_mesh_voltage_t minimum_voltage_value; bt_mesh_voltage_t typical_voltage_value; bt_mesh_voltage_t maximum_voltage_value; @@ -1323,7 +1323,7 @@ typedef struct __packed voltage_specification { * instance of the Time Exponential 8 characteristic to represent a set of statistical * voltage values over a period of time. */ -typedef struct __packed voltage_statistics { +typedef struct __attribute__((packed)) voltage_statistics { bt_mesh_voltage_t average_voltage_value; bt_mesh_voltage_t standard_deviation_voltage_value; bt_mesh_voltage_t minimum_voltage_value; diff --git a/components/bt/esp_ble_mesh/models/common/include/mesh/model_common.h b/components/bt/esp_ble_mesh/models/common/include/mesh/model_common.h new file mode 100644 index 000000000000..49712504a9f9 --- /dev/null +++ b/components/bt/esp_ble_mesh/models/common/include/mesh/model_common.h @@ -0,0 +1,27 @@ +/* + * SPDX-FileCopyrightText: 2018 Vikrant More + * SPDX-FileContributor: 2018-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _MODEL_COMMON_H_ +#define _MODEL_COMMON_H_ + +#include "mesh/types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +float bt_mesh_sqrt(float square); + +int32_t bt_mesh_ceil(float num); + +float bt_mesh_log2(float num); + +#ifdef __cplusplus +} +#endif + +#endif /* _MODEL_COMMON_H_ */ diff --git a/components/bt/esp_ble_mesh/models/common/include/mesh/model_opcode.h b/components/bt/esp_ble_mesh/models/common/include/mesh/model_opcode.h index 9ea4f7baf43d..70f971610062 100644 --- a/components/bt/esp_ble_mesh/models/common/include/mesh/model_opcode.h +++ b/components/bt/esp_ble_mesh/models/common/include/mesh/model_opcode.h @@ -14,260 +14,273 @@ extern "C" { #endif /* Generic OnOff Message Opcode */ -#define BLE_MESH_MODEL_OP_GEN_ONOFF_GET BLE_MESH_MODEL_OP_2(0x82, 0x01) -#define BLE_MESH_MODEL_OP_GEN_ONOFF_SET BLE_MESH_MODEL_OP_2(0x82, 0x02) -#define BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x03) -#define BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x04) +#define BLE_MESH_MODEL_OP_GEN_ONOFF_GET BLE_MESH_MODEL_OP_2(0x82, 0x01) +#define BLE_MESH_MODEL_OP_GEN_ONOFF_SET BLE_MESH_MODEL_OP_2(0x82, 0x02) +#define BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x03) +#define BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x04) /* Generic Level Message Opcode */ -#define BLE_MESH_MODEL_OP_GEN_LEVEL_GET BLE_MESH_MODEL_OP_2(0x82, 0x05) -#define BLE_MESH_MODEL_OP_GEN_LEVEL_SET BLE_MESH_MODEL_OP_2(0x82, 0x06) -#define BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x07) -#define BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x08) -#define BLE_MESH_MODEL_OP_GEN_DELTA_SET BLE_MESH_MODEL_OP_2(0x82, 0x09) -#define BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x0A) -#define BLE_MESH_MODEL_OP_GEN_MOVE_SET BLE_MESH_MODEL_OP_2(0x82, 0x0B) -#define BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x0C) +#define BLE_MESH_MODEL_OP_GEN_LEVEL_GET BLE_MESH_MODEL_OP_2(0x82, 0x05) +#define BLE_MESH_MODEL_OP_GEN_LEVEL_SET BLE_MESH_MODEL_OP_2(0x82, 0x06) +#define BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x07) +#define BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x08) +#define BLE_MESH_MODEL_OP_GEN_DELTA_SET BLE_MESH_MODEL_OP_2(0x82, 0x09) +#define BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x0A) +#define BLE_MESH_MODEL_OP_GEN_MOVE_SET BLE_MESH_MODEL_OP_2(0x82, 0x0B) +#define BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x0C) /* Generic Default Transition Time Message Opcode*/ -#define BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET BLE_MESH_MODEL_OP_2(0x82, 0x0D) -#define BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET BLE_MESH_MODEL_OP_2(0x82, 0x0E) -#define BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x0F) -#define BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x10) +#define BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET BLE_MESH_MODEL_OP_2(0x82, 0x0D) +#define BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET BLE_MESH_MODEL_OP_2(0x82, 0x0E) +#define BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x0F) +#define BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x10) /* Generic Power OnOff Message Opcode*/ -#define BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET BLE_MESH_MODEL_OP_2(0x82, 0x11) -#define BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x12) +#define BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET BLE_MESH_MODEL_OP_2(0x82, 0x11) +#define BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x12) /* Generic Power OnOff Setup Message Opcode */ -#define BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET BLE_MESH_MODEL_OP_2(0x82, 0x13) -#define BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x14) +#define BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET BLE_MESH_MODEL_OP_2(0x82, 0x13) +#define BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x14) /* Generic Power Level Message Opcode */ -#define BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET BLE_MESH_MODEL_OP_2(0x82, 0x15) -#define BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET BLE_MESH_MODEL_OP_2(0x82, 0x16) -#define BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x17) -#define BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x18) -#define BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET BLE_MESH_MODEL_OP_2(0x82, 0x19) -#define BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x1A) -#define BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x1B) -#define BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x1C) -#define BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x1D) -#define BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x1E) +#define BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET BLE_MESH_MODEL_OP_2(0x82, 0x15) +#define BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET BLE_MESH_MODEL_OP_2(0x82, 0x16) +#define BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x17) +#define BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x18) +#define BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET BLE_MESH_MODEL_OP_2(0x82, 0x19) +#define BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x1A) +#define BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x1B) +#define BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x1C) +#define BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x1D) +#define BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x1E) /* Generic Power Level Setup Message Opcode */ -#define BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x1F) -#define BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x20) -#define BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x21) -#define BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x22) +#define BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x1F) +#define BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x20) +#define BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x21) +#define BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x22) /* Generic Battery Message Opcode */ -#define BLE_MESH_MODEL_OP_GEN_BATTERY_GET BLE_MESH_MODEL_OP_2(0x82, 0x23) -#define BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x24) +#define BLE_MESH_MODEL_OP_GEN_BATTERY_GET BLE_MESH_MODEL_OP_2(0x82, 0x23) +#define BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x24) /* Generic Location Message Opcode */ -#define BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET BLE_MESH_MODEL_OP_2(0x82, 0x25) -#define BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS BLE_MESH_MODEL_OP_1(0x40) -#define BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET BLE_MESH_MODEL_OP_2(0x82, 0x26) -#define BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x27) +#define BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET BLE_MESH_MODEL_OP_2(0x82, 0x25) +#define BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS BLE_MESH_MODEL_OP_1(0x40) +#define BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET BLE_MESH_MODEL_OP_2(0x82, 0x26) +#define BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x27) /* Generic Location Setup Message Opcode */ -#define BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET BLE_MESH_MODEL_OP_1(0x41) -#define BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK BLE_MESH_MODEL_OP_1(0x42) -#define BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET BLE_MESH_MODEL_OP_2(0x82, 0x28) -#define BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x29) +#define BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET BLE_MESH_MODEL_OP_1(0x41) +#define BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK BLE_MESH_MODEL_OP_1(0x42) +#define BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET BLE_MESH_MODEL_OP_2(0x82, 0x28) +#define BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x29) /* Generic Manufacturer Property Message Opcode */ -#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET BLE_MESH_MODEL_OP_2(0x82, 0x2A) -#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS BLE_MESH_MODEL_OP_1(0x43) -#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET BLE_MESH_MODEL_OP_2(0x82, 0x2B) -#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET BLE_MESH_MODEL_OP_1(0x44) -#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_1(0x45) -#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS BLE_MESH_MODEL_OP_1(0x46) +#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET BLE_MESH_MODEL_OP_2(0x82, 0x2A) +#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS BLE_MESH_MODEL_OP_1(0x43) +#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET BLE_MESH_MODEL_OP_2(0x82, 0x2B) +#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET BLE_MESH_MODEL_OP_1(0x44) +#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_1(0x45) +#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS BLE_MESH_MODEL_OP_1(0x46) /* Generic Admin Property Message Opcode */ -#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET BLE_MESH_MODEL_OP_2(0x82, 0x2C) -#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS BLE_MESH_MODEL_OP_1(0x47) -#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET BLE_MESH_MODEL_OP_2(0x82, 0x2D) -#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET BLE_MESH_MODEL_OP_1(0x48) -#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_1(0x49) -#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS BLE_MESH_MODEL_OP_1(0x4A) +#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET BLE_MESH_MODEL_OP_2(0x82, 0x2C) +#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS BLE_MESH_MODEL_OP_1(0x47) +#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET BLE_MESH_MODEL_OP_2(0x82, 0x2D) +#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET BLE_MESH_MODEL_OP_1(0x48) +#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_1(0x49) +#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS BLE_MESH_MODEL_OP_1(0x4A) /* Generic User Property Message Opcode */ -#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET BLE_MESH_MODEL_OP_2(0x82, 0x2E) -#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS BLE_MESH_MODEL_OP_1(0x4B) -#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET BLE_MESH_MODEL_OP_2(0x82, 0x2F) -#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET BLE_MESH_MODEL_OP_1(0x4C) -#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_1(0x4D) -#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS BLE_MESH_MODEL_OP_1(0x4E) +#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET BLE_MESH_MODEL_OP_2(0x82, 0x2E) +#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS BLE_MESH_MODEL_OP_1(0x4B) +#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET BLE_MESH_MODEL_OP_2(0x82, 0x2F) +#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET BLE_MESH_MODEL_OP_1(0x4C) +#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_1(0x4D) +#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS BLE_MESH_MODEL_OP_1(0x4E) /* Generic Client Property Message Opcode */ -#define BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET BLE_MESH_MODEL_OP_1(0x4F) -#define BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS BLE_MESH_MODEL_OP_1(0x50) +#define BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET BLE_MESH_MODEL_OP_1(0x4F) +#define BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS BLE_MESH_MODEL_OP_1(0x50) /* Sensor Message Opcode */ -#define BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET BLE_MESH_MODEL_OP_2(0x82, 0x30) -#define BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS BLE_MESH_MODEL_OP_1(0x51) -#define BLE_MESH_MODEL_OP_SENSOR_GET BLE_MESH_MODEL_OP_2(0x82, 0x31) -#define BLE_MESH_MODEL_OP_SENSOR_STATUS BLE_MESH_MODEL_OP_1(0x52) -#define BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET BLE_MESH_MODEL_OP_2(0x82, 0x32) -#define BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS BLE_MESH_MODEL_OP_1(0x53) -#define BLE_MESH_MODEL_OP_SENSOR_SERIES_GET BLE_MESH_MODEL_OP_2(0x82, 0x33) -#define BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS BLE_MESH_MODEL_OP_1(0x54) +#define BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET BLE_MESH_MODEL_OP_2(0x82, 0x30) +#define BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS BLE_MESH_MODEL_OP_1(0x51) +#define BLE_MESH_MODEL_OP_SENSOR_GET BLE_MESH_MODEL_OP_2(0x82, 0x31) +#define BLE_MESH_MODEL_OP_SENSOR_STATUS BLE_MESH_MODEL_OP_1(0x52) +#define BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET BLE_MESH_MODEL_OP_2(0x82, 0x32) +#define BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS BLE_MESH_MODEL_OP_1(0x53) +#define BLE_MESH_MODEL_OP_SENSOR_SERIES_GET BLE_MESH_MODEL_OP_2(0x82, 0x33) +#define BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS BLE_MESH_MODEL_OP_1(0x54) /* Sensor Setup Message Opcode */ -#define BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET BLE_MESH_MODEL_OP_2(0x82, 0x34) -#define BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET BLE_MESH_MODEL_OP_1(0x55) -#define BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK BLE_MESH_MODEL_OP_1(0x56) -#define BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS BLE_MESH_MODEL_OP_1(0x57) -#define BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET BLE_MESH_MODEL_OP_2(0x82, 0x35) -#define BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS BLE_MESH_MODEL_OP_1(0x58) -#define BLE_MESH_MODEL_OP_SENSOR_SETTING_GET BLE_MESH_MODEL_OP_2(0x82, 0x36) -#define BLE_MESH_MODEL_OP_SENSOR_SETTING_SET BLE_MESH_MODEL_OP_1(0x59) -#define BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK BLE_MESH_MODEL_OP_1(0x5A) -#define BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS BLE_MESH_MODEL_OP_1(0x5B) +#define BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET BLE_MESH_MODEL_OP_2(0x82, 0x34) +#define BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET BLE_MESH_MODEL_OP_1(0x55) +#define BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK BLE_MESH_MODEL_OP_1(0x56) +#define BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS BLE_MESH_MODEL_OP_1(0x57) +#define BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET BLE_MESH_MODEL_OP_2(0x82, 0x35) +#define BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS BLE_MESH_MODEL_OP_1(0x58) +#define BLE_MESH_MODEL_OP_SENSOR_SETTING_GET BLE_MESH_MODEL_OP_2(0x82, 0x36) +#define BLE_MESH_MODEL_OP_SENSOR_SETTING_SET BLE_MESH_MODEL_OP_1(0x59) +#define BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK BLE_MESH_MODEL_OP_1(0x5A) +#define BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS BLE_MESH_MODEL_OP_1(0x5B) /* Time Message Opcode */ -#define BLE_MESH_MODEL_OP_TIME_GET BLE_MESH_MODEL_OP_2(0x82, 0x37) -#define BLE_MESH_MODEL_OP_TIME_SET BLE_MESH_MODEL_OP_1(0x5C) -#define BLE_MESH_MODEL_OP_TIME_STATUS BLE_MESH_MODEL_OP_1(0x5D) -#define BLE_MESH_MODEL_OP_TIME_ROLE_GET BLE_MESH_MODEL_OP_2(0x82, 0x38) -#define BLE_MESH_MODEL_OP_TIME_ROLE_SET BLE_MESH_MODEL_OP_2(0x82, 0x39) -#define BLE_MESH_MODEL_OP_TIME_ROLE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x3A) -#define BLE_MESH_MODEL_OP_TIME_ZONE_GET BLE_MESH_MODEL_OP_2(0x82, 0x3B) -#define BLE_MESH_MODEL_OP_TIME_ZONE_SET BLE_MESH_MODEL_OP_2(0x82, 0x3C) -#define BLE_MESH_MODEL_OP_TIME_ZONE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x3D) -#define BLE_MESH_MODEL_OP_TAI_UTC_DELTA_GET BLE_MESH_MODEL_OP_2(0x82, 0x3E) -#define BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET BLE_MESH_MODEL_OP_2(0x82, 0x3F) -#define BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x40) +#define BLE_MESH_MODEL_OP_TIME_GET BLE_MESH_MODEL_OP_2(0x82, 0x37) +#define BLE_MESH_MODEL_OP_TIME_SET BLE_MESH_MODEL_OP_1(0x5C) +#define BLE_MESH_MODEL_OP_TIME_STATUS BLE_MESH_MODEL_OP_1(0x5D) +#define BLE_MESH_MODEL_OP_TIME_ROLE_GET BLE_MESH_MODEL_OP_2(0x82, 0x38) +#define BLE_MESH_MODEL_OP_TIME_ROLE_SET BLE_MESH_MODEL_OP_2(0x82, 0x39) +#define BLE_MESH_MODEL_OP_TIME_ROLE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x3A) +#define BLE_MESH_MODEL_OP_TIME_ZONE_GET BLE_MESH_MODEL_OP_2(0x82, 0x3B) +#define BLE_MESH_MODEL_OP_TIME_ZONE_SET BLE_MESH_MODEL_OP_2(0x82, 0x3C) +#define BLE_MESH_MODEL_OP_TIME_ZONE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x3D) +#define BLE_MESH_MODEL_OP_TAI_UTC_DELTA_GET BLE_MESH_MODEL_OP_2(0x82, 0x3E) +#define BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET BLE_MESH_MODEL_OP_2(0x82, 0x3F) +#define BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x40) /* Scene Message Opcode */ -#define BLE_MESH_MODEL_OP_SCENE_GET BLE_MESH_MODEL_OP_2(0x82, 0x41) -#define BLE_MESH_MODEL_OP_SCENE_RECALL BLE_MESH_MODEL_OP_2(0x82, 0x42) -#define BLE_MESH_MODEL_OP_SCENE_RECALL_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x43) -#define BLE_MESH_MODEL_OP_SCENE_STATUS BLE_MESH_MODEL_OP_1(0x5E) -#define BLE_MESH_MODEL_OP_SCENE_REGISTER_GET BLE_MESH_MODEL_OP_2(0x82, 0x44) -#define BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x45) +#define BLE_MESH_MODEL_OP_SCENE_GET BLE_MESH_MODEL_OP_2(0x82, 0x41) +#define BLE_MESH_MODEL_OP_SCENE_RECALL BLE_MESH_MODEL_OP_2(0x82, 0x42) +#define BLE_MESH_MODEL_OP_SCENE_RECALL_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x43) +#define BLE_MESH_MODEL_OP_SCENE_STATUS BLE_MESH_MODEL_OP_1(0x5E) +#define BLE_MESH_MODEL_OP_SCENE_REGISTER_GET BLE_MESH_MODEL_OP_2(0x82, 0x44) +#define BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x45) /* Scene Setup Message Opcode */ -#define BLE_MESH_MODEL_OP_SCENE_STORE BLE_MESH_MODEL_OP_2(0x82, 0x46) -#define BLE_MESH_MODEL_OP_SCENE_STORE_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x47) -#define BLE_MESH_MODEL_OP_SCENE_DELETE BLE_MESH_MODEL_OP_2(0x82, 0x9E) -#define BLE_MESH_MODEL_OP_SCENE_DELETE_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x9F) +#define BLE_MESH_MODEL_OP_SCENE_STORE BLE_MESH_MODEL_OP_2(0x82, 0x46) +#define BLE_MESH_MODEL_OP_SCENE_STORE_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x47) +#define BLE_MESH_MODEL_OP_SCENE_DELETE BLE_MESH_MODEL_OP_2(0x82, 0x9E) +#define BLE_MESH_MODEL_OP_SCENE_DELETE_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x9F) /* Scheduler Message Opcode */ -#define BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET BLE_MESH_MODEL_OP_2(0x82, 0x48) -#define BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS BLE_MESH_MODEL_OP_1(0x5F) -#define BLE_MESH_MODEL_OP_SCHEDULER_GET BLE_MESH_MODEL_OP_2(0x82, 0x49) -#define BLE_MESH_MODEL_OP_SCHEDULER_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x4A) +#define BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET BLE_MESH_MODEL_OP_2(0x82, 0x48) +#define BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS BLE_MESH_MODEL_OP_1(0x5F) +#define BLE_MESH_MODEL_OP_SCHEDULER_GET BLE_MESH_MODEL_OP_2(0x82, 0x49) +#define BLE_MESH_MODEL_OP_SCHEDULER_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x4A) /* Scheduler Setup Message Opcode */ -#define BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET BLE_MESH_MODEL_OP_1(0x60) -#define BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET_UNACK BLE_MESH_MODEL_OP_1(0x61) +#define BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET BLE_MESH_MODEL_OP_1(0x60) +#define BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET_UNACK BLE_MESH_MODEL_OP_1(0x61) /* Light Lightness Message Opcode */ -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET BLE_MESH_MODEL_OP_2(0x82, 0x4B) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET BLE_MESH_MODEL_OP_2(0x82, 0x4C) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x4D) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x4E) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET BLE_MESH_MODEL_OP_2(0x82, 0x4F) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET BLE_MESH_MODEL_OP_2(0x82, 0x50) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x51) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x52) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET BLE_MESH_MODEL_OP_2(0x82, 0x53) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x54) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x55) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x56) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x57) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x58) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET BLE_MESH_MODEL_OP_2(0x82, 0x4B) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET BLE_MESH_MODEL_OP_2(0x82, 0x4C) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x4D) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x4E) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET BLE_MESH_MODEL_OP_2(0x82, 0x4F) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET BLE_MESH_MODEL_OP_2(0x82, 0x50) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x51) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x52) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET BLE_MESH_MODEL_OP_2(0x82, 0x53) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x54) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x55) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x56) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x57) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x58) /* Light Lightness Setup Message Opcode */ -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x59) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x5A) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x5B) -#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x5C) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x59) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x5A) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x5B) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x5C) /* Light CTL Message Opcode */ -#define BLE_MESH_MODEL_OP_LIGHT_CTL_GET BLE_MESH_MODEL_OP_2(0x82, 0x5D) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_SET BLE_MESH_MODEL_OP_2(0x82, 0x5E) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x5F) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x60) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET BLE_MESH_MODEL_OP_2(0x82, 0x61) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x62) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x63) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET BLE_MESH_MODEL_OP_2(0x82, 0x64) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x65) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x66) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x67) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x68) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_GET BLE_MESH_MODEL_OP_2(0x82, 0x5D) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_SET BLE_MESH_MODEL_OP_2(0x82, 0x5E) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x5F) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x60) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET BLE_MESH_MODEL_OP_2(0x82, 0x61) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x62) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x63) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET BLE_MESH_MODEL_OP_2(0x82, 0x64) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x65) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x66) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x67) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x68) /* Light CTL Setup Message Opcode */ -#define BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x69) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x6A) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x6B) -#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x6C) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x69) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x6A) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x6B) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x6C) /* Light HSL Message Opcode */ -#define BLE_MESH_MODEL_OP_LIGHT_HSL_GET BLE_MESH_MODEL_OP_2(0x82, 0x6D) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET BLE_MESH_MODEL_OP_2(0x82, 0x6E) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET BLE_MESH_MODEL_OP_2(0x82, 0x6F) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x70) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x71) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET BLE_MESH_MODEL_OP_2(0x82, 0x72) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET BLE_MESH_MODEL_OP_2(0x82, 0x73) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x74) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x75) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_SET BLE_MESH_MODEL_OP_2(0x82, 0x76) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x77) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x78) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET BLE_MESH_MODEL_OP_2(0x82, 0x79) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x7A) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x7B) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x7C) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x7D) -#define BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x7E) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_GET BLE_MESH_MODEL_OP_2(0x82, 0x6D) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET BLE_MESH_MODEL_OP_2(0x82, 0x6E) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET BLE_MESH_MODEL_OP_2(0x82, 0x6F) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x70) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x71) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET BLE_MESH_MODEL_OP_2(0x82, 0x72) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET BLE_MESH_MODEL_OP_2(0x82, 0x73) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x74) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x75) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_SET BLE_MESH_MODEL_OP_2(0x82, 0x76) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x77) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x78) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET BLE_MESH_MODEL_OP_2(0x82, 0x79) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x7A) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x7B) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x7C) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x7D) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x7E) /* Light HSL Setup Message Opcode */ -#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) +#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) /* Light xyL Message Opcode */ -#define BLE_MESH_MODEL_OP_LIGHT_XYL_GET BLE_MESH_MODEL_OP_2(0x82, 0x83) -#define BLE_MESH_MODEL_OP_LIGHT_XYL_SET BLE_MESH_MODEL_OP_2(0x82, 0x84) -#define BLE_MESH_MODEL_OP_LIGHT_XYL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x85) -#define BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x86) -#define BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET BLE_MESH_MODEL_OP_2(0x82, 0x87) -#define BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x88) -#define BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x89) -#define BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x8A) -#define BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x8B) -#define BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x8C) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_GET BLE_MESH_MODEL_OP_2(0x82, 0x83) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_SET BLE_MESH_MODEL_OP_2(0x82, 0x84) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x85) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x86) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET BLE_MESH_MODEL_OP_2(0x82, 0x87) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x88) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x89) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x8A) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x8B) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x8C) /* Light xyL Setup Message Opcode */ -#define BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x8D) -#define BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x8E) -#define BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x8F) -#define BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x90) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x8D) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x8E) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x8F) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x90) /* Light Control Message Opcode */ -#define BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET BLE_MESH_MODEL_OP_2(0x82, 0x91) -#define BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET BLE_MESH_MODEL_OP_2(0x82, 0x92) -#define BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x93) -#define BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x94) -#define BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET BLE_MESH_MODEL_OP_2(0x82, 0x95) -#define BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET BLE_MESH_MODEL_OP_2(0x82, 0x96) -#define BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x97) -#define BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x98) -#define BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET BLE_MESH_MODEL_OP_2(0x82, 0x99) -#define BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET BLE_MESH_MODEL_OP_2(0x82, 0x9A) -#define BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x9B) -#define BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x9C) -#define BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET BLE_MESH_MODEL_OP_2(0x82, 0x9D) -#define BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET BLE_MESH_MODEL_OP_1(0x62) -#define BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_1(0x63) -#define BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS BLE_MESH_MODEL_OP_1(0x64) +#define BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET BLE_MESH_MODEL_OP_2(0x82, 0x91) +#define BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET BLE_MESH_MODEL_OP_2(0x82, 0x92) +#define BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x93) +#define BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x94) +#define BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET BLE_MESH_MODEL_OP_2(0x82, 0x95) +#define BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET BLE_MESH_MODEL_OP_2(0x82, 0x96) +#define BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x97) +#define BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x98) +#define BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET BLE_MESH_MODEL_OP_2(0x82, 0x99) +#define BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET BLE_MESH_MODEL_OP_2(0x82, 0x9A) +#define BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x9B) +#define BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x9C) +#define BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET BLE_MESH_MODEL_OP_2(0x82, 0x9D) +#define BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET BLE_MESH_MODEL_OP_1(0x62) +#define BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_1(0x63) +#define BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS BLE_MESH_MODEL_OP_1(0x64) + +/* Blob Transfer Message Opcode - TBD */ +#define BLE_MESH_BLOB_TRANSFER_GET BLE_MESH_MODEL_OP_2(0x83, 0x00) +#define BLE_MESH_BLOB_TRANSFER_START BLE_MESH_MODEL_OP_2(0x83, 0x01) +#define BLE_MESH_BLOB_TRANSFER_CANCEL BLE_MESH_MODEL_OP_2(0x83, 0x02) +#define BLE_MESH_BLOB_TRANSFER_STATUS BLE_MESH_MODEL_OP_2(0x83, 0x03) +#define BLE_MESH_BLOB_BLOCK_GET BLE_MESH_MODEL_OP_2(0x83, 0x05) +#define BLE_MESH_BLOB_BLOCK_START BLE_MESH_MODEL_OP_2(0x83, 0x04) +#define BLE_MESH_BLOB_PARTIAL_BLOCK_REPORT BLE_MESH_MODEL_OP_1(0x65) +#define BLE_MESH_BLOB_BLOCK_STATUS BLE_MESH_MODEL_OP_1(0x67) +#define BLE_MESH_BLOB_CHUNK_TRANSFER BLE_MESH_MODEL_OP_1(0x66) +#define BLE_MESH_BLOB_INFORMATION_GET BLE_MESH_MODEL_OP_2(0x83, 0x06) +#define BLE_MESH_BLOB_INFORMATION_STATUS BLE_MESH_MODEL_OP_2(0x83, 0x07) #ifdef __cplusplus } diff --git a/components/bt/esp_ble_mesh/models/common/model_common.c b/components/bt/esp_ble_mesh/models/common/model_common.c new file mode 100644 index 000000000000..eb54ee1501d7 --- /dev/null +++ b/components/bt/esp_ble_mesh/models/common/model_common.c @@ -0,0 +1,47 @@ +/* + * SPDX-FileCopyrightText: 2018 Vikrant More + * SPDX-FileContributor: 2018-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "mesh/types.h" + +#define MINDIFF (2.25e-308) + +float bt_mesh_sqrt(float square) +{ + float root = 0.0, last = 0.0, diff = 0.0; + + root = square / 3.0; + diff = 1; + + if (square <= 0) { + return 0; + } + + do { + last = root; + root = (root + square / root) / 2.0; + diff = root - last; + } while (diff > MINDIFF || diff < -MINDIFF); + + return root; +} + +int32_t bt_mesh_ceil(float num) +{ + int32_t inum = (int32_t)num; + + if (num == (float)inum) { + return inum; + } + + return inum + 1; +} + +float bt_mesh_log2(float num) +{ + return log2f(num); +} diff --git a/components/bt/esp_ble_mesh/models/server/generic_server.c b/components/bt/esp_ble_mesh/models/server/generic_server.c index a10e467b28f4..747d78b25fa7 100644 --- a/components/bt/esp_ble_mesh/models/server/generic_server.c +++ b/components/bt/esp_ble_mesh/models/server/generic_server.c @@ -21,20 +21,6 @@ static bt_mesh_mutex_t generic_server_lock; -static inline void bt_mesh_generic_server_mutex_new(void) -{ - if (!generic_server_lock.mutex) { - bt_mesh_mutex_create(&generic_server_lock); - } -} - -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_generic_server_mutex_free(void) -{ - bt_mesh_mutex_free(&generic_server_lock); -} -#endif /* CONFIG_BLE_MESH_DEINIT */ - void bt_mesh_generic_server_lock(void) { bt_mesh_mutex_lock(&generic_server_lock); @@ -88,7 +74,6 @@ static void send_gen_onoff_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void gen_onoff_get(struct bt_mesh_model *model, @@ -104,13 +89,12 @@ static void gen_onoff_get(struct bt_mesh_model *model, /* Callback the received message to the application layer */ if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } send_gen_onoff_status(model, ctx, false); - return; } void gen_onoff_publish(struct bt_mesh_model *model) @@ -121,7 +105,6 @@ void gen_onoff_publish(struct bt_mesh_model *model) } send_gen_onoff_status(model, NULL, true); - return; } static void gen_onoff_set(struct bt_mesh_model *model, @@ -158,8 +141,8 @@ static void gen_onoff_set(struct bt_mesh_model *model, .onoff_set.trans_time = trans_time, .onoff_set.delay = delay, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -185,8 +168,8 @@ static void gen_onoff_set(struct bt_mesh_model *model, bt_mesh_gen_server_state_change_t change = { .gen_onoff_set.onoff = srv->state.onoff, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { send_gen_onoff_status(model, ctx, false); @@ -198,8 +181,8 @@ static void gen_onoff_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->transition.timer.work._reserved) { - memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->transition.timer.work.user_data) { + memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -216,7 +199,6 @@ static void gen_onoff_set(struct bt_mesh_model *model, bt_mesh_generic_server_unlock(); bt_mesh_server_start_transition(&srv->transition); - return; } /* Generic Level Server message handlers */ @@ -269,7 +251,6 @@ static void send_gen_level_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void gen_level_get(struct bt_mesh_model *model, @@ -285,13 +266,12 @@ static void gen_level_get(struct bt_mesh_model *model, /* Callback the received message to the application layer */ if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } send_gen_level_status(model, ctx, false); - return; } void gen_level_publish(struct bt_mesh_model *model) @@ -302,7 +282,6 @@ void gen_level_publish(struct bt_mesh_model *model) } send_gen_level_status(model, NULL, true); - return; } static void gen_level_set(struct bt_mesh_model *model, @@ -336,8 +315,8 @@ static void gen_level_set(struct bt_mesh_model *model, .level_set.trans_time = trans_time, .level_set.delay = delay, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -367,8 +346,8 @@ static void gen_level_set(struct bt_mesh_model *model, bt_mesh_gen_server_state_change_t change = { .gen_level_set.level = srv->state.level, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LEVEL_SET) { send_gen_level_status(model, ctx, false); @@ -380,8 +359,8 @@ static void gen_level_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->transition.timer.work._reserved) { - memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->transition.timer.work.user_data) { + memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -398,7 +377,6 @@ static void gen_level_set(struct bt_mesh_model *model, bt_mesh_generic_server_unlock(); bt_mesh_server_start_transition(&srv->transition); - return; } static void gen_delta_set(struct bt_mesh_model *model, @@ -432,8 +410,8 @@ static void gen_delta_set(struct bt_mesh_model *model, .delta_set.trans_time = trans_time, .delta_set.delay = delay, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -488,8 +466,8 @@ static void gen_delta_set(struct bt_mesh_model *model, bt_mesh_gen_server_state_change_t change = { .gen_delta_set.level = srv->state.level, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DELTA_SET) { send_gen_level_status(model, ctx, false); @@ -501,8 +479,8 @@ static void gen_delta_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->transition.timer.work._reserved) { - memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->transition.timer.work.user_data) { + memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -519,7 +497,6 @@ static void gen_delta_set(struct bt_mesh_model *model, bt_mesh_generic_server_unlock(); bt_mesh_server_start_transition(&srv->transition); - return; } static void gen_move_set(struct bt_mesh_model *model, @@ -554,8 +531,8 @@ static void gen_move_set(struct bt_mesh_model *model, .move_set.trans_time = trans_time, .move_set.delay = delay, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -593,8 +570,8 @@ static void gen_move_set(struct bt_mesh_model *model, bt_mesh_gen_server_state_change_t change = { .gen_move_set.level = srv->state.level, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MOVE_SET) { send_gen_level_status(model, ctx, false); @@ -607,8 +584,8 @@ static void gen_move_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->transition.timer.work._reserved) { - memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->transition.timer.work.user_data) { + memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } if (delta) { @@ -639,13 +616,12 @@ static void gen_move_set(struct bt_mesh_model *model, bt_mesh_gen_server_state_change_t change = { .gen_move_set.level = srv->state.level, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); return; } bt_mesh_server_start_transition(&srv->transition); - return; } /* Generic Default Transition Time Server message handlers */ @@ -684,7 +660,6 @@ static void send_gen_def_trans_time_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void gen_def_trans_time_get(struct bt_mesh_model *model, @@ -700,13 +675,12 @@ static void gen_def_trans_time_get(struct bt_mesh_model *model, /* Callback the received message to the application layer */ if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } send_gen_def_trans_time_status(model, ctx, false); - return; } static void gen_def_trans_time_set(struct bt_mesh_model *model, @@ -732,8 +706,8 @@ static void gen_def_trans_time_set(struct bt_mesh_model *model, bt_mesh_gen_server_recv_set_msg_t set = { .def_trans_time_set.trans_time = trans_time, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -744,15 +718,13 @@ static void gen_def_trans_time_set(struct bt_mesh_model *model, bt_mesh_gen_server_state_change_t change = { .gen_def_trans_time_set.trans_time = trans_time, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET) { send_gen_def_trans_time_status(model, ctx, false); } send_gen_def_trans_time_status(model, NULL, true); - - return; } /* Generic Power OnOff Server message handlers */ @@ -807,7 +779,6 @@ static void send_gen_onpowerup_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void gen_onpowerup_get(struct bt_mesh_model *model, @@ -823,13 +794,12 @@ static void gen_onpowerup_get(struct bt_mesh_model *model, /* Callback the received message to the application layer */ if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } send_gen_onpowerup_status(model, ctx, false); - return; } /* Generic Power OnOff Setup Server message handlers */ @@ -863,7 +833,6 @@ void gen_onpowerup_publish(struct bt_mesh_model *model) } send_gen_onpowerup_status(model, NULL, true); - return; } static void gen_onpowerup_set(struct bt_mesh_model *model, @@ -889,8 +858,8 @@ static void gen_onpowerup_set(struct bt_mesh_model *model, bt_mesh_gen_server_recv_set_msg_t set = { .onpowerup_set.onpowerup = onpowerup, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -901,15 +870,13 @@ static void gen_onpowerup_set(struct bt_mesh_model *model, bt_mesh_gen_server_state_change_t change = { .gen_onpowerup_set.onpowerup = onpowerup, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET) { send_gen_onpowerup_status(model, ctx, false); } send_gen_onpowerup_status(model, NULL, true); - - return; } /* Generic Power Level Server message handlers */ @@ -991,7 +958,6 @@ static void send_gen_power_level_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void gen_power_level_get(struct bt_mesh_model *model, @@ -1008,8 +974,8 @@ static void gen_power_level_get(struct bt_mesh_model *model, /* Callback the received message to the application layer */ if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } @@ -1032,7 +998,6 @@ static void gen_power_level_get(struct bt_mesh_model *model, } send_gen_power_level_status(model, ctx, false, opcode); - return; } void gen_power_level_publish(struct bt_mesh_model *model, uint16_t opcode) @@ -1065,7 +1030,6 @@ void gen_power_level_publish(struct bt_mesh_model *model, uint16_t opcode) } send_gen_power_level_status(model, NULL, true, opcode); - return; } static void gen_power_level_set(struct bt_mesh_model *model, @@ -1099,8 +1063,8 @@ static void gen_power_level_set(struct bt_mesh_model *model, .power_level_set.trans_time = trans_time, .power_level_set.delay = delay, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -1136,8 +1100,8 @@ static void gen_power_level_set(struct bt_mesh_model *model, bt_mesh_gen_server_state_change_t change = { .gen_power_level_set.power = srv->state->power_actual, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET) { send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS); @@ -1149,8 +1113,8 @@ static void gen_power_level_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->transition.timer.work._reserved) { - memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->transition.timer.work.user_data) { + memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -1175,7 +1139,6 @@ static void gen_power_level_set(struct bt_mesh_model *model, bt_mesh_generic_server_unlock(); bt_mesh_server_start_transition(&srv->transition); - return; } /* Generic Power Level Setup Server message handlers */ @@ -1198,8 +1161,8 @@ static void gen_power_default_set(struct bt_mesh_model *model, bt_mesh_gen_server_recv_set_msg_t set = { .power_default_set.power = power, /* Just callback the actual received value */ }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -1218,15 +1181,13 @@ static void gen_power_default_set(struct bt_mesh_model *model, bt_mesh_gen_server_state_change_t change = { .gen_power_default_set.power = power, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET) { send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS); } send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS); - - return; } static void gen_power_range_set(struct bt_mesh_model *model, @@ -1256,8 +1217,8 @@ static void gen_power_range_set(struct bt_mesh_model *model, .power_range_set.range_min = range_min, .power_range_set.range_max = range_max, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -1281,15 +1242,13 @@ static void gen_power_range_set(struct bt_mesh_model *model, .gen_power_range_set.range_min = srv->state->power_range_min, .gen_power_range_set.range_max = srv->state->power_range_max, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET) { send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS); } send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS); - - return; } /* Generic Battery Server message handlers */ @@ -1307,8 +1266,8 @@ static void gen_battery_get(struct bt_mesh_model *model, /* Callback the received message to the application layer */ if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } @@ -1317,7 +1276,6 @@ static void gen_battery_get(struct bt_mesh_model *model, net_buf_simple_add_le32(&msg, srv->state.battery_flags << 24 | srv->state.time_to_charge); BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, &msg, NULL, NULL)); - return; } /* Generic Location Server message handlers */ @@ -1392,7 +1350,6 @@ static void send_gen_location_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void gen_location_get(struct bt_mesh_model *model, @@ -1409,8 +1366,8 @@ static void gen_location_get(struct bt_mesh_model *model, /* Callback the received message to the application layer */ if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } @@ -1427,7 +1384,6 @@ static void gen_location_get(struct bt_mesh_model *model, } send_gen_location_status(model, ctx, false, opcode); - return; } /* Generic Location Setup Server message handlers */ @@ -1458,8 +1414,8 @@ static void gen_location_set(struct bt_mesh_model *model, .loc_global_set.longitude = longitude, .loc_global_set.altitude = altitude, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -1478,8 +1434,8 @@ static void gen_location_set(struct bt_mesh_model *model, .gen_loc_global_set.longitude = srv->state->global_longitude, .gen_loc_global_set.altitude = srv->state->global_altitude, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); break; } case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET: @@ -1500,8 +1456,8 @@ static void gen_location_set(struct bt_mesh_model *model, .loc_local_set.floor_number = floor, .loc_local_set.uncertainty = uncertainty, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -1526,8 +1482,8 @@ static void gen_location_set(struct bt_mesh_model *model, .gen_loc_local_set.floor_number = srv->state->floor_number, .gen_loc_local_set.uncertainty = srv->state->uncertainty, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); break; } default: @@ -1540,8 +1496,6 @@ static void gen_location_set(struct bt_mesh_model *model, send_gen_location_status(model, ctx, false, opcode); } send_gen_location_status(model, NULL, true, opcode); - - return; } /* Generic User Property Server message handlers */ @@ -1617,7 +1571,6 @@ static void send_gen_user_prop_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void gen_user_prop_get(struct bt_mesh_model *model, @@ -1634,7 +1587,7 @@ static void gen_user_prop_get(struct bt_mesh_model *model, /* Callback the received message to the application layer */ if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { /** - * Also we can use __packed for esp_ble_mesh_gen_user_property_get_t, + * Also we can use __attribute__((packed)) for esp_ble_mesh_gen_user_property_get_t, * and directly callback with buf->data & buf->len. */ bt_mesh_gen_server_recv_get_msg_t get = {0}; @@ -1645,8 +1598,8 @@ static void gen_user_prop_get(struct bt_mesh_model *model, param = (const uint8_t *)&get; len = sizeof(get); } - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, param, len); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, + model, ctx, param, len); return; } @@ -1662,7 +1615,7 @@ static void gen_user_prop_get(struct bt_mesh_model *model, bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS); for (i = 0U; i < srv->property_count; i++) { if (srv->properties[i].admin_access != ADMIN_NOT_USER_PROP && - srv->properties[i].manu_access != MANU_NOT_USER_PROP) { + srv->properties[i].manu_access != MANU_NOT_USER_PROP) { net_buf_simple_add_le16(msg, srv->properties[i].id); } } @@ -1703,14 +1656,14 @@ static void gen_user_prop_set(struct bt_mesh_model *model, .user_property_set.id = property_id, .user_property_set.value = buf, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } property = gen_get_user_property(model, property_id); if (property == NULL || property->user_access == USER_ACCESS_PROHIBIT || - property->user_access == USER_ACCESS_READ) { + property->user_access == USER_ACCESS_READ) { if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET) { send_gen_user_prop_status(model, ctx, property_id, false); } @@ -1737,15 +1690,13 @@ static void gen_user_prop_set(struct bt_mesh_model *model, .gen_user_prop_set.id = property_id, .gen_user_prop_set.value = property->val, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET) { send_gen_user_prop_status(model, ctx, property_id, false); } send_gen_user_prop_status(model, ctx, property_id, true); - - return; } /* Generic Admin Property Server message handlers */ @@ -1816,7 +1767,6 @@ static void send_gen_admin_prop_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void gen_admin_prop_get(struct bt_mesh_model *model, @@ -1840,8 +1790,8 @@ static void gen_admin_prop_get(struct bt_mesh_model *model, param = (const uint8_t *)&get; len = sizeof(get); } - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, param, len); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, + model, ctx, param, len); return; } @@ -1901,8 +1851,8 @@ static void gen_admin_prop_set(struct bt_mesh_model *model, .admin_property_set.access = access, .admin_property_set.value = buf, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -1925,15 +1875,13 @@ static void gen_admin_prop_set(struct bt_mesh_model *model, .gen_admin_prop_set.access = property->admin_access, .gen_admin_prop_set.value = property->val, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET) { send_gen_admin_prop_status(model, ctx, property_id, false); } send_gen_admin_prop_status(model, ctx, property_id, true); - - return; } /* Generic Manufacturer Property Server message handlers */ @@ -2001,7 +1949,6 @@ static void send_gen_manu_prop_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void gen_manu_prop_get(struct bt_mesh_model *model, @@ -2025,8 +1972,8 @@ static void gen_manu_prop_get(struct bt_mesh_model *model, param = (const uint8_t *)&get; len = sizeof(get); } - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, param, len); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, + model, ctx, param, len); return; } @@ -2085,8 +2032,8 @@ static void gen_manu_prop_set(struct bt_mesh_model *model, .manu_property_set.id = property_id, .manu_property_set.access = access, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -2105,15 +2052,13 @@ static void gen_manu_prop_set(struct bt_mesh_model *model, .gen_manu_prop_set.id = property_id, .gen_manu_prop_set.access = property->manu_access, }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET) { send_gen_manu_prop_status(model, ctx, property_id, false); } send_gen_manu_prop_status(model, ctx, property_id, true); - - return; } /* Generic Client Property Server message handlers */ @@ -2130,9 +2075,9 @@ static int search_prop_id_index(const uint16_t *array, uint8_t array_idx, uint16 if (array_idx == 0U) { if (*array >= id) { return array - start; - } else { - return -1; } + + return -1; } index = array_idx / 2; @@ -2140,11 +2085,13 @@ static int search_prop_id_index(const uint16_t *array, uint8_t array_idx, uint16 if (temp == id) { return array + index - start; - } else if (temp > id) { + } + + if (temp > id) { return search_prop_id_index(array, index, id); - } else { - return search_prop_id_index(array + index + 1, array_idx - 1 - index, id); } + + return search_prop_id_index(array + index + 1, array_idx - 1 - index, id); } static void gen_client_prop_get(struct bt_mesh_model *model, @@ -2167,14 +2114,14 @@ static void gen_client_prop_get(struct bt_mesh_model *model, bt_mesh_gen_server_recv_get_msg_t get = { .client_properties_get.id = net_buf_simple_pull_le16(buf), }; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, + model, ctx, (const uint8_t *)&get, sizeof(get)); return; } /* The sequence shall be in an ascending order of Property ID values and shall * start with a smallest Property ID that is greater than or equal to the value - * of the Generic Client Property field of the Generic Client Properities Get + * of the Generic Client Property field of the Generic Client Properties Get * message that it is responding to. */ @@ -2205,8 +2152,8 @@ static void gen_client_prop_get(struct bt_mesh_model *model, } BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, sdu, NULL, NULL)); + bt_mesh_free_buf(sdu); - return; } /* message handlers (End) */ @@ -2332,9 +2279,11 @@ static inline int property_id_compare(const void *p1, const void *p2) if (*(uint16_t *)p1 < * (uint16_t *)p2) { return -1; } + if (*(uint16_t *)p1 > *(uint16_t *)p2) { return 1; } + return 0; } @@ -2475,7 +2424,7 @@ static int generic_server_init(struct bt_mesh_model *model) return -EINVAL; } - bt_mesh_generic_server_mutex_new(); + bt_mesh_mutex_create(&generic_server_lock); return 0; } @@ -2683,7 +2632,7 @@ static int generic_server_deinit(struct bt_mesh_model *model) return -EINVAL; } - bt_mesh_generic_server_mutex_free(); + bt_mesh_mutex_free(&generic_server_lock); return 0; } diff --git a/components/bt/esp_ble_mesh/models/server/include/mesh/sensor_server.h b/components/bt/esp_ble_mesh/models/server/include/mesh/sensor_server.h index 797b45d71752..a9fe77eff3bc 100644 --- a/components/bt/esp_ble_mesh/models/server/include/mesh/sensor_server.h +++ b/components/bt/esp_ble_mesh/models/server/include/mesh/sensor_server.h @@ -158,7 +158,7 @@ struct bt_mesh_sensor_state { /* 1. Multiple instances of the Sensor states may be present within the * same model, provided that each instance has a unique value of the * Sensor Property ID to allow the instances to be differentiated. - * 2. Note: The number of sensors within a multisensor is limited by the + * 2. Note: The number of sensors within a multi-sensor is limited by the * size of the message payload for the Sensor Descriptor Status message. * A single Sensor Descriptor may be sent using a single Unsegmented * Access message. Using Segmentation and Reassembly (SAR), up to 38 diff --git a/components/bt/esp_ble_mesh/models/server/include/mesh/time_scene_server.h b/components/bt/esp_ble_mesh/models/server/include/mesh/time_scene_server.h index a351dd4fef65..7f23cdde0cdb 100644 --- a/components/bt/esp_ble_mesh/models/server/include/mesh/time_scene_server.h +++ b/components/bt/esp_ble_mesh/models/server/include/mesh/time_scene_server.h @@ -63,7 +63,7 @@ extern "C" { #define TIME_NONE 0x00 #define TIME_AUTHORITY 0x01 #define TIME_RELAY 0x02 -#define TIME_CLINET 0x03 +#define TIME_CLIENT 0x03 #define SCENE_SUCCESS 0x00 #define SCENE_REG_FULL 0x01 diff --git a/components/bt/esp_ble_mesh/models/server/lighting_server.c b/components/bt/esp_ble_mesh/models/server/lighting_server.c index 571775511669..6df51458a474 100644 --- a/components/bt/esp_ble_mesh/models/server/lighting_server.c +++ b/components/bt/esp_ble_mesh/models/server/lighting_server.c @@ -21,20 +21,6 @@ static bt_mesh_mutex_t light_server_lock; -static inline void bt_mesh_light_server_mutex_new(void) -{ - if (!light_server_lock.mutex) { - bt_mesh_mutex_create(&light_server_lock); - } -} - -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_light_server_mutex_free(void) -{ - bt_mesh_mutex_free(&light_server_lock); -} -#endif /* CONFIG_BLE_MESH_DEINIT */ - void bt_mesh_light_server_lock(void) { bt_mesh_mutex_lock(&light_server_lock); @@ -137,7 +123,6 @@ static void send_light_lightness_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void light_lightness_get(struct bt_mesh_model *model, @@ -154,8 +139,8 @@ static void light_lightness_get(struct bt_mesh_model *model, /* Callback the received message to the application layer */ if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } @@ -181,7 +166,6 @@ static void light_lightness_get(struct bt_mesh_model *model, } send_light_lightness_status(model, ctx, false, opcode); - return; } void light_lightness_publish(struct bt_mesh_model *model, uint16_t opcode) @@ -214,7 +198,6 @@ void light_lightness_publish(struct bt_mesh_model *model, uint16_t opcode) } send_light_lightness_status(model, NULL, true, opcode); - return; } static void light_lightness_set(struct bt_mesh_model *model, @@ -248,8 +231,8 @@ static void light_lightness_set(struct bt_mesh_model *model, .lightness_set.trans_time = trans_time, .lightness_set.delay = delay, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -286,8 +269,8 @@ static void light_lightness_set(struct bt_mesh_model *model, bt_mesh_light_server_state_change_t change = { .lightness_set.lightness = srv->state->lightness_actual, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) { send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS); @@ -299,8 +282,8 @@ static void light_lightness_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->actual_transition.timer.work._reserved) { - memcpy(srv->actual_transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->actual_transition.timer.work.user_data) { + memcpy(srv->actual_transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -326,7 +309,6 @@ static void light_lightness_set(struct bt_mesh_model *model, bt_mesh_light_server_unlock(); bt_mesh_server_start_transition(&srv->actual_transition); - return; } static void light_lightness_linear_set(struct bt_mesh_model *model, @@ -360,8 +342,8 @@ static void light_lightness_linear_set(struct bt_mesh_model *model, .lightness_linear_set.trans_time = trans_time, .lightness_linear_set.delay = delay, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -391,8 +373,8 @@ static void light_lightness_linear_set(struct bt_mesh_model *model, bt_mesh_light_server_state_change_t change = { .lightness_linear_set.lightness = srv->state->lightness_actual, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) { send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS); @@ -404,8 +386,8 @@ static void light_lightness_linear_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->linear_transition.timer.work._reserved) { - memcpy(srv->linear_transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->linear_transition.timer.work.user_data) { + memcpy(srv->linear_transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -422,7 +404,6 @@ static void light_lightness_linear_set(struct bt_mesh_model *model, bt_mesh_light_server_unlock(); bt_mesh_server_start_transition(&srv->linear_transition); - return; } static void light_lightness_default_set(struct bt_mesh_model *model, @@ -444,8 +425,8 @@ static void light_lightness_default_set(struct bt_mesh_model *model, bt_mesh_light_server_recv_set_msg_t set = { .lightness_default_set.lightness = lightness, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -455,16 +436,14 @@ static void light_lightness_default_set(struct bt_mesh_model *model, bt_mesh_light_server_state_change_t change = { .lightness_default_set.lightness = lightness, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); } if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET) { send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS); } send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS); - - return; } static void light_lightness_range_set(struct bt_mesh_model *model, @@ -494,8 +473,8 @@ static void light_lightness_range_set(struct bt_mesh_model *model, .lightness_range_set.range_min = range_min, .lightness_range_set.range_max = range_max, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -533,15 +512,13 @@ static void light_lightness_range_set(struct bt_mesh_model *model, .lightness_range_set.range_min = srv->state->lightness_range_min, .lightness_range_set.range_max = srv->state->lightness_range_max, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET) { send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS); } send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS); - - return; } /* Light CTL Server/Temperature Server/Setup Server message handlers */ @@ -638,7 +615,6 @@ static void send_light_ctl_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void light_ctl_get(struct bt_mesh_model *model, @@ -679,8 +655,8 @@ static void light_ctl_get(struct bt_mesh_model *model, /* Callback the received message to the application layer */ if (rsp_ctrl->get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } @@ -703,7 +679,6 @@ static void light_ctl_get(struct bt_mesh_model *model, } send_light_ctl_status(model, ctx, false, opcode); - return; } void light_ctl_publish(struct bt_mesh_model *model, uint16_t opcode) @@ -744,7 +719,6 @@ void light_ctl_publish(struct bt_mesh_model *model, uint16_t opcode) } send_light_ctl_status(model, NULL, true, opcode); - return; } static void light_ctl_set(struct bt_mesh_model *model, @@ -788,8 +762,8 @@ static void light_ctl_set(struct bt_mesh_model *model, .ctl_set.trans_time = trans_time, .ctl_set.delay = delay, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -809,8 +783,8 @@ static void light_ctl_set(struct bt_mesh_model *model, srv->state->target_lightness = lightness; if (srv->state->temperature_range_min && - srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN && - temperature < srv->state->temperature_range_min) { + srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN && + temperature < srv->state->temperature_range_min) { temperature = srv->state->temperature_range_min; } else if (srv->state->temperature_range_max && srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN && @@ -821,8 +795,8 @@ static void light_ctl_set(struct bt_mesh_model *model, srv->state->target_delta_uv = delta_uv; if (srv->state->target_lightness != srv->state->lightness || - srv->state->target_temperature != srv->state->temperature || - srv->state->target_delta_uv != srv->state->delta_uv) { + srv->state->target_temperature != srv->state->temperature || + srv->state->target_delta_uv != srv->state->delta_uv) { light_ctl_tt_values(srv, trans_time, delay); } else { bt_mesh_light_server_state_change_t change = { @@ -830,8 +804,8 @@ static void light_ctl_set(struct bt_mesh_model *model, .ctl_set.temperature = srv->state->temperature, .ctl_set.delta_uv = srv->state->delta_uv, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) { send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS); @@ -843,8 +817,8 @@ static void light_ctl_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->transition.timer.work._reserved) { - memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->transition.timer.work.user_data) { + memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -863,7 +837,6 @@ static void light_ctl_set(struct bt_mesh_model *model, bt_mesh_light_server_unlock(); bt_mesh_server_start_transition(&srv->transition); - return; } static void light_ctl_default_set(struct bt_mesh_model *model, @@ -895,14 +868,14 @@ static void light_ctl_default_set(struct bt_mesh_model *model, .ctl_default_set.temperature = temperature, .ctl_default_set.delta_uv = delta_uv, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } if (srv->state->temperature_range_min && - srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN && - temperature < srv->state->temperature_range_min) { + srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN && + temperature < srv->state->temperature_range_min) { temperature = srv->state->temperature_range_min; } else if (srv->state->temperature_range_max && srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN && @@ -919,15 +892,13 @@ static void light_ctl_default_set(struct bt_mesh_model *model, .ctl_default_set.temperature = srv->state->temperature_default, .ctl_default_set.delta_uv = srv->state->delta_uv_default, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET) { send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS); } send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS); - - return; } static void light_ctl_temp_range_set(struct bt_mesh_model *model, @@ -960,8 +931,8 @@ static void light_ctl_temp_range_set(struct bt_mesh_model *model, .ctl_temp_range_set.range_min = min, .ctl_temp_range_set.range_max = max, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -985,15 +956,13 @@ static void light_ctl_temp_range_set(struct bt_mesh_model *model, .ctl_temp_range_set.range_min = srv->state->temperature_range_min, .ctl_temp_range_set.range_max = srv->state->temperature_range_max, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET) { send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS); } send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS); - - return; } static void light_ctl_temp_set(struct bt_mesh_model *model, @@ -1035,8 +1004,8 @@ static void light_ctl_temp_set(struct bt_mesh_model *model, .ctl_temp_set.trans_time = trans_time, .ctl_temp_set.delay = delay, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -1055,8 +1024,8 @@ static void light_ctl_temp_set(struct bt_mesh_model *model, bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); if (srv->state->temperature_range_min && - srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN && - temperature < srv->state->temperature_range_min) { + srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN && + temperature < srv->state->temperature_range_min) { temperature = srv->state->temperature_range_min; } else if (srv->state->temperature_range_max && srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN && @@ -1074,8 +1043,8 @@ static void light_ctl_temp_set(struct bt_mesh_model *model, .ctl_temp_set.temperature = srv->state->temperature, .ctl_temp_set.delta_uv = srv->state->delta_uv, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET) { send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS); @@ -1087,8 +1056,8 @@ static void light_ctl_temp_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->transition.timer.work._reserved) { - memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->transition.timer.work.user_data) { + memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -1106,7 +1075,6 @@ static void light_ctl_temp_set(struct bt_mesh_model *model, bt_mesh_light_server_unlock(); bt_mesh_server_start_transition(&srv->transition); - return; } /* Light HSL Server/Hue Server/Saturation Server/Setup Server message handlers */ @@ -1224,7 +1192,6 @@ static void send_light_hsl_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void light_hsl_get(struct bt_mesh_model *model, @@ -1274,8 +1241,8 @@ static void light_hsl_get(struct bt_mesh_model *model, /* Callback the received message to the application layer */ if (rsp_ctrl->get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } @@ -1304,7 +1271,6 @@ static void light_hsl_get(struct bt_mesh_model *model, } send_light_hsl_status(model, ctx, false, opcode); - return; } void light_hsl_publish(struct bt_mesh_model *model, uint16_t opcode) @@ -1353,7 +1319,6 @@ void light_hsl_publish(struct bt_mesh_model *model, uint16_t opcode) } send_light_hsl_status(model, NULL, true, opcode); - return; } static void light_hsl_set(struct bt_mesh_model *model, @@ -1391,8 +1356,8 @@ static void light_hsl_set(struct bt_mesh_model *model, .hsl_set.trans_time = trans_time, .hsl_set.delay = delay, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -1438,8 +1403,8 @@ static void light_hsl_set(struct bt_mesh_model *model, .hsl_set.hue = srv->state->hue, .hsl_set.saturation = srv->state->saturation, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SET) { send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS); @@ -1451,8 +1416,8 @@ static void light_hsl_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->transition.timer.work._reserved) { - memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->transition.timer.work.user_data) { + memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -1471,7 +1436,6 @@ static void light_hsl_set(struct bt_mesh_model *model, bt_mesh_light_server_unlock(); bt_mesh_server_start_transition(&srv->transition); - return; } static void light_hsl_default_set(struct bt_mesh_model *model, @@ -1497,8 +1461,8 @@ static void light_hsl_default_set(struct bt_mesh_model *model, .hsl_default_set.hue = hue, .hsl_default_set.saturation = saturation, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -1523,15 +1487,13 @@ static void light_hsl_default_set(struct bt_mesh_model *model, .hsl_default_set.hue = srv->state->hue_default, .hsl_default_set.saturation = srv->state->saturation_default, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET) { send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS); } send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS); - - return; } static void light_hsl_range_set(struct bt_mesh_model *model, @@ -1571,8 +1533,8 @@ static void light_hsl_range_set(struct bt_mesh_model *model, .hsl_range_set.sat_range_min = saturation_min, .hsl_range_set.sat_range_max = saturation_max, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -1588,15 +1550,13 @@ static void light_hsl_range_set(struct bt_mesh_model *model, .hsl_range_set.sat_range_min = srv->state->saturation_range_min, .hsl_range_set.sat_range_max = srv->state->saturation_range_max, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET) { send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS); } send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS); - - return; } static void light_hsl_hue_set(struct bt_mesh_model *model, @@ -1630,8 +1590,8 @@ static void light_hsl_hue_set(struct bt_mesh_model *model, .hsl_hue_set.trans_time = trans_time, .hsl_hue_set.delay = delay, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -1666,8 +1626,8 @@ static void light_hsl_hue_set(struct bt_mesh_model *model, bt_mesh_light_server_state_change_t change = { .hsl_hue_set.hue = srv->state->hue, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET) { send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS); @@ -1679,8 +1639,8 @@ static void light_hsl_hue_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->transition.timer.work._reserved) { - memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->transition.timer.work.user_data) { + memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -1697,7 +1657,6 @@ static void light_hsl_hue_set(struct bt_mesh_model *model, bt_mesh_light_server_unlock(); bt_mesh_server_start_transition(&srv->transition); - return; } static void light_hsl_sat_set(struct bt_mesh_model *model, @@ -1731,8 +1690,8 @@ static void light_hsl_sat_set(struct bt_mesh_model *model, .hsl_saturation_set.trans_time = trans_time, .hsl_saturation_set.delay = delay, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -1767,8 +1726,8 @@ static void light_hsl_sat_set(struct bt_mesh_model *model, bt_mesh_light_server_state_change_t change = { .hsl_saturation_set.saturation = srv->state->saturation, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET) { send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS); @@ -1780,8 +1739,8 @@ static void light_hsl_sat_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->transition.timer.work._reserved) { - memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->transition.timer.work.user_data) { + memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -1798,7 +1757,6 @@ static void light_hsl_sat_set(struct bt_mesh_model *model, bt_mesh_light_server_unlock(); bt_mesh_server_start_transition(&srv->transition); - return; } /* Light xyL Server/Setup Server message handlers */ @@ -1896,7 +1854,6 @@ static void send_light_xyl_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void light_xyl_get(struct bt_mesh_model *model, @@ -1913,8 +1870,8 @@ static void light_xyl_get(struct bt_mesh_model *model, /* Callback the received message to the application layer */ if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } @@ -1937,7 +1894,6 @@ static void light_xyl_get(struct bt_mesh_model *model, } send_light_xyl_status(model, ctx, false, opcode); - return; } void light_xyl_publish(struct bt_mesh_model *model, uint16_t opcode) @@ -1970,7 +1926,6 @@ void light_xyl_publish(struct bt_mesh_model *model, uint16_t opcode) } send_light_xyl_status(model, NULL, true, opcode); - return; } static void light_xyl_set(struct bt_mesh_model *model, @@ -2008,8 +1963,8 @@ static void light_xyl_set(struct bt_mesh_model *model, .xyl_set.trans_time = trans_time, .xyl_set.delay = delay, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -2046,8 +2001,8 @@ static void light_xyl_set(struct bt_mesh_model *model, * be started and is considered complete. */ if (srv->state->target_lightness != srv->state->lightness || - srv->state->target_x != srv->state->x || - srv->state->target_y != srv->state->y) { + srv->state->target_x != srv->state->x || + srv->state->target_y != srv->state->y) { light_xyl_tt_values(srv, trans_time, delay); } else { bt_mesh_light_server_state_change_t change = { @@ -2055,8 +2010,8 @@ static void light_xyl_set(struct bt_mesh_model *model, .xyl_set.x = srv->state->x, .xyl_set.y = srv->state->y, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_SET) { send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS); @@ -2068,8 +2023,8 @@ static void light_xyl_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->transition.timer.work._reserved) { - memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->transition.timer.work.user_data) { + memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -2088,7 +2043,6 @@ static void light_xyl_set(struct bt_mesh_model *model, bt_mesh_light_server_unlock(); bt_mesh_server_start_transition(&srv->transition); - return; } static void light_xyl_default_set(struct bt_mesh_model *model, @@ -2114,8 +2068,8 @@ static void light_xyl_default_set(struct bt_mesh_model *model, .xyl_default_set.x = x, .xyl_default_set.y = y, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -2140,15 +2094,13 @@ static void light_xyl_default_set(struct bt_mesh_model *model, .xyl_default_set.x = srv->state->x_default, .xyl_default_set.y = srv->state->y_default, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET) { send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS); } send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS); - - return; } static void light_xyl_range_set(struct bt_mesh_model *model, @@ -2188,8 +2140,8 @@ static void light_xyl_range_set(struct bt_mesh_model *model, .xyl_range_set.y_range_min = y_min, .xyl_range_set.y_range_max = y_max, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -2205,15 +2157,13 @@ static void light_xyl_range_set(struct bt_mesh_model *model, .xyl_range_set.y_range_min = srv->state->y_range_min, .xyl_range_set.y_range_max = srv->state->y_range_max, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET) { send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS); } send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS); - - return; } /* Light LC Server/Setup Server message handlers */ @@ -2273,7 +2223,6 @@ static void send_light_lc_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void light_lc_get(struct bt_mesh_model *model, @@ -2290,8 +2239,8 @@ static void light_lc_get(struct bt_mesh_model *model, /* Callback the received message to the application layer */ if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } @@ -2311,7 +2260,6 @@ static void light_lc_get(struct bt_mesh_model *model, } send_light_lc_status(model, ctx, false, opcode); - return; } void light_lc_publish(struct bt_mesh_model *model, uint16_t opcode) @@ -2324,7 +2272,6 @@ void light_lc_publish(struct bt_mesh_model *model, uint16_t opcode) } send_light_lc_status(model, NULL, true, opcode); - return; } static void light_lc_mode_set(struct bt_mesh_model *model, @@ -2350,8 +2297,8 @@ static void light_lc_mode_set(struct bt_mesh_model *model, bt_mesh_light_server_recv_set_msg_t set = { .lc_mode_set.mode = mode, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -2360,15 +2307,13 @@ static void light_lc_mode_set(struct bt_mesh_model *model, bt_mesh_light_server_state_change_t change = { .lc_mode_set.mode = srv->lc->state.mode, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET) { send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS); } send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS); - - return; } static void light_lc_om_set(struct bt_mesh_model *model, @@ -2394,8 +2339,8 @@ static void light_lc_om_set(struct bt_mesh_model *model, bt_mesh_light_server_recv_set_msg_t set = { .lc_om_set.mode = om, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -2404,15 +2349,13 @@ static void light_lc_om_set(struct bt_mesh_model *model, bt_mesh_light_server_state_change_t change = { .lc_om_set.mode = srv->lc->state.occupancy_mode, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET) { send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS); } send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS); - - return; } static void light_lc_light_onoff_set(struct bt_mesh_model *model, @@ -2446,8 +2389,8 @@ static void light_lc_light_onoff_set(struct bt_mesh_model *model, .lc_light_onoff_set.trans_time = trans_time, .lc_light_onoff_set.delay = delay, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -2473,8 +2416,8 @@ static void light_lc_light_onoff_set(struct bt_mesh_model *model, bt_mesh_light_server_state_change_t change = { .lc_light_onoff_set.onoff = srv->lc->state.light_onoff, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET) { send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS); @@ -2486,8 +2429,8 @@ static void light_lc_light_onoff_set(struct bt_mesh_model *model, } /* Copy the ctx of the received message */ - if (srv->transition.timer.work._reserved) { - memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->transition.timer.work.user_data) { + memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -2504,7 +2447,6 @@ static void light_lc_light_onoff_set(struct bt_mesh_model *model, bt_mesh_light_server_unlock(); bt_mesh_server_start_transition(&srv->transition); - return; } static void light_lc_sensor_status(struct bt_mesh_model *model, @@ -2550,8 +2492,8 @@ static void light_lc_sensor_status(struct bt_mesh_model *model, bt_mesh_light_server_recv_status_msg_t status = { .sensor_status.data = buf, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_STATUS_MSG, model, ctx, (const uint8_t *)&status, sizeof(status)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_STATUS_MSG, + model, ctx, (const uint8_t *)&status, sizeof(status)); return; } @@ -2578,8 +2520,8 @@ static void light_lc_sensor_status(struct bt_mesh_model *model, srv->lc->state.occupancy = BLE_MESH_STATE_ON; change.sensor_status.state.occupancy = srv->lc->state.occupancy; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); } break; } @@ -2593,8 +2535,8 @@ static void light_lc_sensor_status(struct bt_mesh_model *model, srv->lc->state.occupancy = BLE_MESH_STATE_ON; change.sensor_status.state.occupancy = srv->lc->state.occupancy; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); } break; } @@ -2608,8 +2550,8 @@ static void light_lc_sensor_status(struct bt_mesh_model *model, srv->lc->state.occupancy = BLE_MESH_STATE_ON; change.sensor_status.state.occupancy = srv->lc->state.occupancy; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); } break; } @@ -2624,8 +2566,8 @@ static void light_lc_sensor_status(struct bt_mesh_model *model, srv->lc->prop_state.time_occupancy_delay - val; change.sensor_status.state.set_occupancy_to_1_delay = srv->lc->prop_state.set_occupancy_to_1_delay; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); } break; } @@ -2645,8 +2587,8 @@ static void light_lc_sensor_status(struct bt_mesh_model *model, srv->lc->state.ambient_luxlevel = (msb << 16) | lsb; change.sensor_status.state.ambient_luxlevel = srv->lc->state.ambient_luxlevel; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); break; } default: @@ -2766,7 +2708,6 @@ static void send_light_lc_prop_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void light_lc_prop_get(struct bt_mesh_model *model, @@ -2792,13 +2733,12 @@ static void light_lc_prop_get(struct bt_mesh_model *model, bt_mesh_light_server_recv_get_msg_t get = { .lc_property_get.id = net_buf_simple_pull_le16(buf), }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, + model, ctx, (const uint8_t *)&get, sizeof(get)); return; } send_light_lc_prop_status(model, ctx, prop_id, false); - return; } static void light_lc_prop_set(struct bt_mesh_model *model, @@ -2825,8 +2765,8 @@ static void light_lc_prop_set(struct bt_mesh_model *model, .lc_property_set.id = net_buf_simple_pull_le16(buf), .lc_property_set.value = buf, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -2849,15 +2789,13 @@ static void light_lc_prop_set(struct bt_mesh_model *model, .lc_property_set.id = prop_id, .lc_property_set.value = buf, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET) { send_light_lc_prop_status(model, ctx, prop_id, false); } send_light_lc_prop_status(model, ctx, prop_id, true); - - return; } /* message handlers (End) */ @@ -3155,7 +3093,7 @@ static int light_server_init(struct bt_mesh_model *model) return -EINVAL; } - bt_mesh_light_server_mutex_new(); + bt_mesh_mutex_create(&light_server_lock); return 0; } @@ -3238,8 +3176,8 @@ static int light_hsl_srv_init(struct bt_mesh_model *model) * the corresponding Light HSL Setup Server model shall also be present. * The model requires three elements: the main element and the Hue element * and the Saturation element. The Hue element contains the corresponding - * Light HSL Hue Server model, and the Saturation element contains the corr- - * esponding Light HSL Saturation Server model. + * Light HSL Hue Server model, and the Saturation element contains the + * corresponding Light HSL Saturation Server model. */ struct bt_mesh_elem *element = bt_mesh_model_elem(model); if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV) == NULL) { @@ -3449,7 +3387,7 @@ static int light_server_deinit(struct bt_mesh_model *model) return -EINVAL; } - bt_mesh_light_server_mutex_free(); + bt_mesh_mutex_free(&light_server_lock); return 0; } diff --git a/components/bt/esp_ble_mesh/models/server/sensor_server.c b/components/bt/esp_ble_mesh/models/server/sensor_server.c index 0d3797cc7e36..b920283ec6b9 100644 --- a/components/bt/esp_ble_mesh/models/server/sensor_server.c +++ b/components/bt/esp_ble_mesh/models/server/sensor_server.c @@ -50,6 +50,7 @@ static void send_sensor_descriptor_status(struct bt_mesh_model *model, BT_WARN("Too large sensor descriptor status"); break; } + net_buf_simple_add_le16(msg, state->sensor_property_id); net_buf_simple_add_le32(msg, (state->descriptor.sample_function << 24) | (state->descriptor.negative_tolerance << 12) | @@ -62,7 +63,7 @@ static void send_sensor_descriptor_status(struct bt_mesh_model *model, 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->sensor_property_id == prop_id) { total_len += SENSOR_DESCRIPTOR_LEN; if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) { /* Add this in case the message is too long */ @@ -71,8 +72,8 @@ static void send_sensor_descriptor_status(struct bt_mesh_model *model, } net_buf_simple_add_le16(msg, state->sensor_property_id); net_buf_simple_add_le32(msg, (state->descriptor.sample_function << 24) | - (state->descriptor.negative_tolerance << 12) | - (state->descriptor.positive_tolerance)); + (state->descriptor.negative_tolerance << 12) | + (state->descriptor.positive_tolerance)); net_buf_simple_add_u8(msg, state->descriptor.measure_period); net_buf_simple_add_u8(msg, state->descriptor.update_interval); break; @@ -85,8 +86,8 @@ static void send_sensor_descriptor_status(struct bt_mesh_model *model, } BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); - return; } static void send_sensor_data_status(struct bt_mesh_model *model, @@ -120,6 +121,7 @@ static void send_sensor_data_status(struct bt_mesh_model *model, BT_WARN("Too large sensor status"); break; } + if (state->sensor_data.format == SENSOR_DATA_FORMAT_A) { uint16_t mpid = ((state->sensor_property_id & BIT_MASK(11)) << 5) | ((state->sensor_data.length & BIT_MASK(4)) << 1) | @@ -139,7 +141,7 @@ static void send_sensor_data_status(struct bt_mesh_model *model, 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->sensor_property_id == prop_id) { uint8_t mpid_len = (state->sensor_data.format == SENSOR_DATA_FORMAT_A) ? SENSOR_DATA_FORMAT_A_MPID_LEN : SENSOR_DATA_FORMAT_B_MPID_LEN; total_len += (mpid_len + (state->sensor_data.raw_value ? @@ -175,8 +177,8 @@ static void send_sensor_data_status(struct bt_mesh_model *model, } BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); - return; } static void send_sensor_cadence_status(struct bt_mesh_model *model, @@ -192,7 +194,7 @@ static void send_sensor_cadence_status(struct bt_mesh_model *model, 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) { + state->sensor_property_id == prop_id && state->cadence) { length = SENSOR_PROPERTY_ID_LEN + 1 + 1; if (state->cadence->trigger_delta_down) { if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) { @@ -240,7 +242,7 @@ static void send_sensor_cadence_status(struct bt_mesh_model *model, if (i != srv->state_count) { if (state->cadence) { net_buf_simple_add_u8(msg, (state->cadence->trigger_type << 7) | - state->cadence->period_divisor); + state->cadence->period_divisor); if (state->cadence->trigger_delta_down) { if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) { net_buf_simple_add_mem(msg, state->cadence->trigger_delta_down->data, @@ -277,7 +279,6 @@ static void send_sensor_cadence_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void send_sensor_settings_status(struct bt_mesh_model *model, @@ -325,8 +326,8 @@ static void send_sensor_settings_status(struct bt_mesh_model *model, } BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); - return; } static struct sensor_setting *find_sensor_setting(struct bt_mesh_model *model, @@ -340,12 +341,12 @@ static struct sensor_setting *find_sensor_setting(struct bt_mesh_model *model, 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->setting_count && state->settings) { + state->sensor_property_id == prop_id && + state->setting_count && state->settings) { 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) { + item->property_id == set_prop_id) { return item; } } @@ -420,7 +421,6 @@ static void send_sensor_setting_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void send_sensor_column_status(struct bt_mesh_model *model, @@ -437,7 +437,7 @@ static void send_sensor_column_status(struct bt_mesh_model *model, 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->sensor_property_id == prop_id) { length = SENSOR_PROPERTY_ID_LEN; if (state->series_column.raw_value_x) { length += state->series_column.raw_value_x->len; @@ -494,8 +494,8 @@ static void send_sensor_column_status(struct bt_mesh_model *model, } BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); - return; } static void send_sensor_series_status(struct bt_mesh_model *model, @@ -512,7 +512,7 @@ static void send_sensor_series_status(struct bt_mesh_model *model, 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->sensor_property_id == prop_id) { length = SENSOR_PROPERTY_ID_LEN; /* TODO: raw value x, column width & raw value y in Sensor Series * Status are optional, here we need to add some conditions to @@ -569,8 +569,8 @@ static void send_sensor_series_status(struct bt_mesh_model *model, } BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); - return; } static void sensor_get(struct bt_mesh_model *model, @@ -611,8 +611,8 @@ static void sensor_get(struct bt_mesh_model *model, .sensor_descriptor_get.op_en = !get_all, .sensor_descriptor_get.id = prop_id, }; - bt_mesh_sensor_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get)); + bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, + model, ctx, (const uint8_t *)&get, sizeof(get)); } else { send_sensor_descriptor_status(model, ctx, prop_id, get_all); } @@ -622,8 +622,8 @@ static void sensor_get(struct bt_mesh_model *model, .sensor_get.op_en = !get_all, .sensor_get.id = prop_id, }; - bt_mesh_sensor_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get)); + bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, + model, ctx, (const uint8_t *)&get, sizeof(get)); } else { send_sensor_data_status(model, ctx, prop_id, get_all); } @@ -640,8 +640,8 @@ static void sensor_get(struct bt_mesh_model *model, .sensor_column_get.id = prop_id, .sensor_column_get.raw_x = buf, }; - bt_mesh_sensor_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get)); + bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, + model, ctx, (const uint8_t *)&get, sizeof(get)); } else { send_sensor_column_status(model, ctx, buf, prop_id); } @@ -651,8 +651,8 @@ static void sensor_get(struct bt_mesh_model *model, .sensor_series_get.id = prop_id, .sensor_series_get.raw = buf, }; - bt_mesh_sensor_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get)); + bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, + model, ctx, (const uint8_t *)&get, sizeof(get)); } else { send_sensor_series_status(model, ctx, buf, prop_id); } @@ -680,8 +680,8 @@ static void sensor_get(struct bt_mesh_model *model, bt_mesh_sensor_server_recv_get_msg_t get = { .sensor_cadence_get.id = prop_id, }; - bt_mesh_sensor_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get)); + bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, + model, ctx, (const uint8_t *)&get, sizeof(get)); } else { send_sensor_cadence_status(model, ctx, prop_id, false); } @@ -690,8 +690,8 @@ static void sensor_get(struct bt_mesh_model *model, bt_mesh_sensor_server_recv_get_msg_t get = { .sensor_settings_get.id = prop_id, }; - bt_mesh_sensor_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get)); + bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, + model, ctx, (const uint8_t *)&get, sizeof(get)); } else { send_sensor_settings_status(model, ctx, prop_id); } @@ -713,8 +713,8 @@ static void sensor_get(struct bt_mesh_model *model, .sensor_setting_get.id = prop_id, .sensor_setting_get.setting_id = set_prop_id, }; - bt_mesh_sensor_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get)); + bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, + model, ctx, (const uint8_t *)&get, sizeof(get)); } else { send_sensor_setting_status(model, ctx, prop_id, set_prop_id, false); } @@ -756,15 +756,15 @@ static void sensor_cadence_set(struct bt_mesh_model *model, .sensor_cadence_set.id = prop_id, .sensor_cadence_set.cadence = buf, }; - bt_mesh_sensor_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } 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->sensor_property_id == prop_id) { break; } } @@ -850,8 +850,8 @@ static void sensor_cadence_set(struct bt_mesh_model *model, change.sensor_cadence_set.min_interval = state->cadence->min_interval; change.sensor_cadence_set.fast_cadence_low = state->cadence->fast_cadence_low; change.sensor_cadence_set.fast_cadence_high = state->cadence->fast_cadence_high; - bt_mesh_sensor_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_SENSOR_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET) { send_sensor_cadence_status(model, ctx, prop_id, false); @@ -871,7 +871,6 @@ static void sensor_cadence_set(struct bt_mesh_model *model, * status publication mechanism. */ update_sensor_periodic_pub(sensor_model, prop_id); - return; } static void update_sensor_periodic_pub(struct bt_mesh_model *model, uint16_t prop_id) @@ -894,7 +893,7 @@ static void update_sensor_periodic_pub(struct bt_mesh_model *model, uint16_t pro 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->sensor_property_id == prop_id) { break; } } @@ -948,8 +947,8 @@ static void sensor_setting_set(struct bt_mesh_model *model, .sensor_setting_set.setting_id = set_prop_id, .sensor_setting_set.raw = buf, }; - bt_mesh_sensor_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -963,8 +962,8 @@ static void sensor_setting_set(struct bt_mesh_model *model, change.sensor_setting_set.id = prop_id; change.sensor_setting_set.setting_id = set_prop_id; change.sensor_setting_set.value = item->raw; - bt_mesh_sensor_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_SENSOR_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_sensor_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_SENSOR_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); } } @@ -974,8 +973,6 @@ static void sensor_setting_set(struct bt_mesh_model *model, if (item) { send_sensor_setting_status(model, ctx, prop_id, set_prop_id, true); } - - return; } /* message handlers (End) */ @@ -1006,7 +1003,7 @@ 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; - int i, j; + int i, j, k; for (i = 0; i < state_count; i++) { state = &state_start[i]; @@ -1015,7 +1012,7 @@ static int check_sensor_server_init(struct bt_mesh_sensor_state *state_start, return -EINVAL; } /* Check if the same Sensor Property ID exists */ - for (int k = i + 1; k < state_count; k++) { + for (k = i + 1; k < state_count; k++) { if (state->sensor_property_id == state_start[k].sensor_property_id) { BT_ERR("Same Sensor Property ID 0x%04x exists", state->sensor_property_id); return -EINVAL; @@ -1029,7 +1026,7 @@ static int check_sensor_server_init(struct bt_mesh_sensor_state *state_start, return -EINVAL; } /* Check if the same Sensor Setting Property ID exists */ - for (int k = j + 1; k < state->setting_count; k++) { + for (k = j + 1; k < state->setting_count; k++) { if (setting->property_id == state->settings[k].property_id) { BT_ERR("Same Sensor Setting Property ID 0x%04x exists", setting->property_id); return -EINVAL; @@ -1039,9 +1036,9 @@ static int check_sensor_server_init(struct bt_mesh_sensor_state *state_start, } if (state->cadence) { if (state->cadence->trigger_delta_down == NULL || - state->cadence->trigger_delta_up == NULL || - state->cadence->fast_cadence_low == NULL || - state->cadence->fast_cadence_high == NULL) { + state->cadence->trigger_delta_up == NULL || + state->cadence->fast_cadence_low == NULL || + state->cadence->fast_cadence_high == NULL) { BT_ERR("Invalid Sensor Cadence state"); return -EINVAL; } @@ -1069,9 +1066,11 @@ static int sensor_server_init(struct bt_mesh_model *model) BT_ERR("Invalid Sensor state, model id 0x%04x", model->id); return -EINVAL; } + if (check_sensor_server_init(srv->states, srv->state_count)) { return -EINVAL; } + srv->model = model; break; } @@ -1081,9 +1080,11 @@ static int sensor_server_init(struct bt_mesh_model *model) BT_ERR("Invalid Sensor state, model id 0x%04x", model->id); return -EINVAL; } + if (check_sensor_server_init(srv->states, srv->state_count)) { return -EINVAL; } + srv->model = model; break; } diff --git a/components/bt/esp_ble_mesh/models/server/server_common.c b/components/bt/esp_ble_mesh/models/server/server_common.c index 8e7879cc4f6e..a12ce6ba38ea 100644 --- a/components/bt/esp_ble_mesh/models/server/server_common.c +++ b/components/bt/esp_ble_mesh/models/server/server_common.c @@ -171,9 +171,9 @@ void bt_mesh_server_alloc_ctx(struct k_work *work) * Here we use the allocated heap memory to store the "struct bt_mesh_msg_ctx". */ __ASSERT(work, "Invalid parameter"); - if (!work->_reserved) { - work->_reserved = bt_mesh_calloc(sizeof(struct bt_mesh_msg_ctx)); - __ASSERT(work->_reserved, "Out of memory"); + if (!work->user_data) { + work->user_data = bt_mesh_calloc(sizeof(struct bt_mesh_msg_ctx)); + __ASSERT(work->user_data, "Out of memory"); } } @@ -181,9 +181,9 @@ void bt_mesh_server_alloc_ctx(struct k_work *work) void bt_mesh_server_free_ctx(struct k_work *work) { __ASSERT(work, "Invalid parameter"); - if (work->_reserved) { - bt_mesh_free(work->_reserved); - work->_reserved = NULL; + if (work->user_data) { + bt_mesh_free(work->user_data); + work->user_data = NULL; } } #endif /* CONFIG_BLE_MESH_DEINIT */ @@ -199,7 +199,7 @@ bool bt_mesh_is_server_recv_last_msg(struct bt_mesh_last_msg_info *last, } if (last->tid == tid && last->src == src && last->dst == dst && - (*now - last->timestamp <= K_SECONDS(6))) { + (*now - last->timestamp <= K_SECONDS(6))) { return true; } @@ -218,7 +218,6 @@ void bt_mesh_server_update_last_msg(struct bt_mesh_last_msg_info *last, last->src = src; last->dst = dst; last->timestamp = *now; - return; } struct net_buf_simple *bt_mesh_server_get_pub_msg(struct bt_mesh_model *model, uint16_t msg_len) @@ -231,7 +230,7 @@ struct net_buf_simple *bt_mesh_server_get_pub_msg(struct bt_mesh_model *model, u } if (model->pub == NULL || model->pub->msg == NULL || - model->pub->addr == BLE_MESH_ADDR_UNASSIGNED) { + model->pub->addr == BLE_MESH_ADDR_UNASSIGNED) { BT_DBG("No publication support, model id 0x%04x", model->id); return NULL; } diff --git a/components/bt/esp_ble_mesh/models/server/state_binding.c b/components/bt/esp_ble_mesh/models/server/state_binding.c index 3896d6c25196..4a2da3aa1887 100644 --- a/components/bt/esp_ble_mesh/models/server/state_binding.c +++ b/components/bt/esp_ble_mesh/models/server/state_binding.c @@ -10,50 +10,18 @@ #include "mesh/config.h" #include "mesh/common.h" +#include "mesh/model_common.h" #include "mesh/model_opcode.h" #include "mesh/state_binding.h" #include "mesh/state_transition.h" #if CONFIG_BLE_MESH_SERVER_MODEL -#define MINDIFF (2.25e-308) - -static float bt_mesh_sqrt(float square) -{ - float root = 0.0, last = 0.0, diff = 0.0; - - root = square / 3.0; - diff = 1; - - if (square <= 0) { - return 0; - } - - do { - last = root; - root = (root + square / root) / 2.0; - diff = root - last; - } while (diff > MINDIFF || diff < -MINDIFF); - - return root; -} - -static int32_t bt_mesh_ceiling(float num) -{ - int32_t inum = (int32_t)num; - - if (num == (float)inum) { - return inum; - } - - return inum + 1; -} - uint16_t bt_mesh_convert_lightness_actual_to_linear(uint16_t actual) { float tmp = ((float) actual / UINT16_MAX); - return bt_mesh_ceiling(UINT16_MAX * tmp * tmp); + return bt_mesh_ceil(UINT16_MAX * tmp * tmp); } uint16_t bt_mesh_convert_lightness_linear_to_actual(uint16_t linear) @@ -99,7 +67,7 @@ int bt_mesh_update_binding_state(struct bt_mesh_model *model, bt_mesh_server_state_value_t *value) { if (model == NULL || model->user_data == NULL || - value == NULL || type > BIND_STATE_MAX) { + value == NULL || type > BIND_STATE_MAX) { BT_ERR("%s, Invalid parameter", __func__); return -EINVAL; } diff --git a/components/bt/esp_ble_mesh/models/server/state_transition.c b/components/bt/esp_ble_mesh/models/server/state_transition.c index 8a8d04e06cb5..46c828169722 100644 --- a/components/bt/esp_ble_mesh/models/server/state_transition.c +++ b/components/bt/esp_ble_mesh/models/server/state_transition.c @@ -238,26 +238,27 @@ void scene_tt_values(struct bt_mesh_scene_srv *srv, uint8_t trans_time, uint8_t #if CONFIG_BLE_MESH_GENERIC_SERVER void generic_onoff_work_handler(struct k_work *work) { - struct bt_mesh_gen_onoff_srv *srv = - CONTAINER_OF(work, struct bt_mesh_gen_onoff_srv, transition.timer.work); - struct bt_mesh_msg_ctx *ctx = NULL; + struct bt_mesh_gen_onoff_srv *srv = CONTAINER_OF(work, + struct bt_mesh_gen_onoff_srv, + transition.timer.work); bt_mesh_gen_server_state_change_t change = {0}; + struct bt_mesh_msg_ctx *ctx = NULL; - if (srv == NULL || srv->transition.timer.work._reserved == NULL) { + if (srv == NULL || srv->transition.timer.work.user_data == NULL) { BT_ERR("%s, Invalid parameter", __func__); return; } bt_mesh_generic_server_lock(); - ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work.user_data; if (srv->transition.just_started) { srv->transition.just_started = false; if (srv->transition.counter == 0U) { change.gen_onoff_set.onoff = srv->state.onoff; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); } else { /** @@ -269,8 +270,8 @@ void generic_onoff_work_handler(struct k_work *work) if (srv->state.target_onoff == BLE_MESH_STATE_ON) { srv->state.onoff = BLE_MESH_STATE_ON; change.gen_onoff_set.onoff = srv->state.onoff; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); } transition_timer_start(&srv->transition); } @@ -288,32 +289,32 @@ void generic_onoff_work_handler(struct k_work *work) srv->state.onoff = srv->state.target_onoff; if (srv->state.target_onoff != BLE_MESH_STATE_ON) { change.gen_onoff_set.onoff = srv->state.onoff; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); } } gen_onoff_publish(srv->model); bt_mesh_generic_server_unlock(); - return; } void generic_level_work_handler(struct k_work *work) { - struct bt_mesh_gen_level_srv *srv = - CONTAINER_OF(work, struct bt_mesh_gen_level_srv, transition.timer.work); - struct bt_mesh_msg_ctx *ctx = NULL; + struct bt_mesh_gen_level_srv *srv = CONTAINER_OF(work, + struct bt_mesh_gen_level_srv, + transition.timer.work); bt_mesh_gen_server_state_change_t change = {0}; + struct bt_mesh_msg_ctx *ctx = NULL; - if (srv == NULL || srv->transition.timer.work._reserved == NULL) { + if (srv == NULL || srv->transition.timer.work.user_data == NULL) { BT_ERR("%s, Invalid parameter", __func__); return; } bt_mesh_generic_server_lock(); - ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work.user_data; if (srv->transition.just_started) { srv->transition.just_started = false; @@ -332,8 +333,8 @@ void generic_level_work_handler(struct k_work *work) change.gen_move_set.level = srv->state.level; break; } - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); } else { transition_timer_start(&srv->transition); @@ -367,38 +368,38 @@ void generic_level_work_handler(struct k_work *work) change.gen_move_set.level = srv->state.level; break; } - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); gen_level_publish(srv->model); bt_mesh_generic_server_unlock(); - return; } void generic_power_level_work_handler(struct k_work *work) { - struct bt_mesh_gen_power_level_srv *srv = - CONTAINER_OF(work, struct bt_mesh_gen_power_level_srv, transition.timer.work); - struct bt_mesh_msg_ctx *ctx = NULL; + struct bt_mesh_gen_power_level_srv *srv = CONTAINER_OF(work, + struct bt_mesh_gen_power_level_srv, + transition.timer.work); bt_mesh_gen_server_state_change_t change = {0}; + struct bt_mesh_msg_ctx *ctx = NULL; if (srv == NULL || srv->state == NULL || - srv->transition.timer.work._reserved == NULL) { + srv->transition.timer.work.user_data == NULL) { BT_ERR("%s, Invalid parameter", __func__); return; } bt_mesh_generic_server_lock(); - ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work.user_data; if (srv->transition.just_started) { srv->transition.just_started = false; if (srv->transition.counter == 0U) { change.gen_power_level_set.power = srv->state->power_actual; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); } else { transition_timer_start(&srv->transition); @@ -429,40 +430,40 @@ void generic_power_level_work_handler(struct k_work *work) } change.gen_power_level_set.power = srv->state->power_actual; - bt_mesh_generic_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); gen_power_level_publish(srv->model, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS); bt_mesh_generic_server_unlock(); - return; } #endif /* CONFIG_BLE_MESH_GENERIC_SERVER */ #if CONFIG_BLE_MESH_LIGHTING_SERVER void light_lightness_actual_work_handler(struct k_work *work) { - struct bt_mesh_light_lightness_srv *srv = - CONTAINER_OF(work, struct bt_mesh_light_lightness_srv, actual_transition.timer.work); - struct bt_mesh_msg_ctx *ctx = NULL; + struct bt_mesh_light_lightness_srv *srv = CONTAINER_OF(work, + struct bt_mesh_light_lightness_srv, + actual_transition.timer.work); bt_mesh_light_server_state_change_t change = {0}; + struct bt_mesh_msg_ctx *ctx = NULL; if (srv == NULL || srv->state == NULL || - srv->actual_transition.timer.work._reserved == NULL) { + srv->actual_transition.timer.work.user_data == NULL) { BT_ERR("%s, Invalid parameter", __func__); return; } bt_mesh_light_server_lock(); - ctx = (struct bt_mesh_msg_ctx *)srv->actual_transition.timer.work._reserved; + ctx = (struct bt_mesh_msg_ctx *)srv->actual_transition.timer.work.user_data; if (srv->actual_transition.just_started) { srv->actual_transition.just_started = false; if (srv->actual_transition.counter == 0U) { change.lightness_set.lightness = srv->state->lightness_actual; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_atomic_clear_bit(srv->actual_transition.flag, BLE_MESH_TRANS_TIMER_START); } else { transition_timer_start(&srv->actual_transition); @@ -493,38 +494,38 @@ void light_lightness_actual_work_handler(struct k_work *work) } change.lightness_set.lightness = srv->state->lightness_actual; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); light_lightness_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS); bt_mesh_light_server_unlock(); - return; } void light_lightness_linear_work_handler(struct k_work *work) { - struct bt_mesh_light_lightness_srv *srv = - CONTAINER_OF(work, struct bt_mesh_light_lightness_srv, linear_transition.timer.work); - struct bt_mesh_msg_ctx *ctx = NULL; + struct bt_mesh_light_lightness_srv *srv = CONTAINER_OF(work, + struct bt_mesh_light_lightness_srv, + linear_transition.timer.work); bt_mesh_light_server_state_change_t change = {0}; + struct bt_mesh_msg_ctx *ctx = NULL; if (srv == NULL || srv->state == NULL || - srv->linear_transition.timer.work._reserved == NULL) { + srv->linear_transition.timer.work.user_data == NULL) { BT_ERR("%s, Invalid parameter", __func__); return; } bt_mesh_light_server_lock(); - ctx = (struct bt_mesh_msg_ctx *)srv->linear_transition.timer.work._reserved; + ctx = (struct bt_mesh_msg_ctx *)srv->linear_transition.timer.work.user_data; if (srv->linear_transition.just_started) { srv->linear_transition.just_started = false; if (srv->linear_transition.counter == 0U) { change.lightness_linear_set.lightness = srv->state->lightness_linear; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_atomic_clear_bit(srv->linear_transition.flag, BLE_MESH_TRANS_TIMER_START); } else { transition_timer_start(&srv->linear_transition); @@ -545,31 +546,31 @@ void light_lightness_linear_work_handler(struct k_work *work) } change.lightness_linear_set.lightness = srv->state->lightness_linear; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); light_lightness_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS); bt_mesh_light_server_unlock(); - return; } void light_ctl_work_handler(struct k_work *work) { - struct bt_mesh_light_ctl_srv *srv = - CONTAINER_OF(work, struct bt_mesh_light_ctl_srv, transition.timer.work); - struct bt_mesh_msg_ctx *ctx = NULL; + struct bt_mesh_light_ctl_srv *srv = CONTAINER_OF(work, + struct bt_mesh_light_ctl_srv, + transition.timer.work); bt_mesh_light_server_state_change_t change = {0}; + struct bt_mesh_msg_ctx *ctx = NULL; if (srv == NULL || srv->state == NULL || - srv->transition.timer.work._reserved == NULL) { + srv->transition.timer.work.user_data == NULL) { BT_ERR("%s, Invalid parameter", __func__); return; } bt_mesh_light_server_lock(); - ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work.user_data; if (srv->transition.just_started) { srv->transition.just_started = false; @@ -577,8 +578,8 @@ void light_ctl_work_handler(struct k_work *work) change.ctl_set.lightness = srv->state->lightness; change.ctl_set.temperature = srv->state->temperature; change.ctl_set.delta_uv = srv->state->delta_uv; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); } else { transition_timer_start(&srv->transition); @@ -605,39 +606,39 @@ void light_ctl_work_handler(struct k_work *work) change.ctl_set.lightness = srv->state->lightness; change.ctl_set.temperature = srv->state->temperature; change.ctl_set.delta_uv = srv->state->delta_uv; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); light_ctl_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS); bt_mesh_light_server_unlock(); - return; } void light_ctl_temp_work_handler(struct k_work *work) { - struct bt_mesh_light_ctl_temp_srv *srv = - CONTAINER_OF(work, struct bt_mesh_light_ctl_temp_srv, transition.timer.work); - struct bt_mesh_msg_ctx *ctx = NULL; + struct bt_mesh_light_ctl_temp_srv *srv = CONTAINER_OF(work, + struct bt_mesh_light_ctl_temp_srv, + transition.timer.work); bt_mesh_light_server_state_change_t change = {0}; + struct bt_mesh_msg_ctx *ctx = NULL; if (srv == NULL || srv->state == NULL || - srv->transition.timer.work._reserved == NULL) { + srv->transition.timer.work.user_data == NULL) { BT_ERR("%s, Invalid parameter", __func__); return; } bt_mesh_light_server_lock(); - ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work.user_data; if (srv->transition.just_started) { srv->transition.just_started = false; if (srv->transition.counter == 0U) { change.ctl_temp_set.temperature = srv->state->temperature; change.ctl_temp_set.delta_uv = srv->state->delta_uv; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); } else { transition_timer_start(&srv->transition); @@ -661,31 +662,31 @@ void light_ctl_temp_work_handler(struct k_work *work) change.ctl_temp_set.temperature = srv->state->temperature; change.ctl_temp_set.delta_uv = srv->state->delta_uv; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); light_ctl_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS); bt_mesh_light_server_unlock(); - return; } void light_hsl_work_handler(struct k_work *work) { - struct bt_mesh_light_hsl_srv *srv = - CONTAINER_OF(work, struct bt_mesh_light_hsl_srv, transition.timer.work); - struct bt_mesh_msg_ctx *ctx = NULL; + struct bt_mesh_light_hsl_srv *srv = CONTAINER_OF(work, + struct bt_mesh_light_hsl_srv, + transition.timer.work); bt_mesh_light_server_state_change_t change = {0}; + struct bt_mesh_msg_ctx *ctx = NULL; if (srv == NULL || srv->state == NULL || - srv->transition.timer.work._reserved == NULL) { + srv->transition.timer.work.user_data == NULL) { BT_ERR("%s, Invalid parameter", __func__); return; } bt_mesh_light_server_lock(); - ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work.user_data; if (srv->transition.just_started) { srv->transition.just_started = false; @@ -693,8 +694,8 @@ void light_hsl_work_handler(struct k_work *work) change.hsl_set.lightness = srv->state->lightness; change.hsl_set.hue = srv->state->hue; change.hsl_set.saturation = srv->state->saturation; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); } else { transition_timer_start(&srv->transition); @@ -721,38 +722,38 @@ void light_hsl_work_handler(struct k_work *work) change.hsl_set.lightness = srv->state->lightness; change.hsl_set.hue = srv->state->hue; change.hsl_set.saturation = srv->state->saturation; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); light_hsl_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS); bt_mesh_light_server_unlock(); - return; } void light_hsl_hue_work_handler(struct k_work *work) { - struct bt_mesh_light_hsl_hue_srv *srv = - CONTAINER_OF(work, struct bt_mesh_light_hsl_hue_srv, transition.timer.work); - struct bt_mesh_msg_ctx *ctx = NULL; + struct bt_mesh_light_hsl_hue_srv *srv = CONTAINER_OF(work, + struct bt_mesh_light_hsl_hue_srv, + transition.timer.work); bt_mesh_light_server_state_change_t change = {0}; + struct bt_mesh_msg_ctx *ctx = NULL; if (srv == NULL || srv->state == NULL || - srv->transition.timer.work._reserved == NULL) { + srv->transition.timer.work.user_data == NULL) { BT_ERR("%s, Invalid parameter", __func__); return; } bt_mesh_light_server_lock(); - ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work.user_data; if (srv->transition.just_started) { srv->transition.just_started = false; if (srv->transition.counter == 0U) { change.hsl_hue_set.hue = srv->state->hue; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); } else { transition_timer_start(&srv->transition); @@ -773,38 +774,38 @@ void light_hsl_hue_work_handler(struct k_work *work) } change.hsl_hue_set.hue = srv->state->hue; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); light_hsl_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS); bt_mesh_light_server_unlock(); - return; } void light_hsl_sat_work_handler(struct k_work *work) { - struct bt_mesh_light_hsl_sat_srv *srv = - CONTAINER_OF(work, struct bt_mesh_light_hsl_sat_srv, transition.timer.work); - struct bt_mesh_msg_ctx *ctx = NULL; + struct bt_mesh_light_hsl_sat_srv *srv = CONTAINER_OF(work, + struct bt_mesh_light_hsl_sat_srv, + transition.timer.work); bt_mesh_light_server_state_change_t change = {0}; + struct bt_mesh_msg_ctx *ctx = NULL; if (srv == NULL || srv->state == NULL || - srv->transition.timer.work._reserved == NULL) { + srv->transition.timer.work.user_data == NULL) { BT_ERR("%s, Invalid parameter", __func__); return; } bt_mesh_light_server_lock(); - ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work.user_data; if (srv->transition.just_started) { srv->transition.just_started = false; if (srv->transition.counter == 0U) { change.hsl_saturation_set.saturation = srv->state->saturation; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); } else { transition_timer_start(&srv->transition); @@ -825,31 +826,31 @@ void light_hsl_sat_work_handler(struct k_work *work) } change.hsl_saturation_set.saturation = srv->state->saturation; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); light_hsl_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS); bt_mesh_light_server_unlock(); - return; } void light_xyl_work_handler(struct k_work *work) { - struct bt_mesh_light_xyl_srv *srv = - CONTAINER_OF(work, struct bt_mesh_light_xyl_srv, transition.timer.work); - struct bt_mesh_msg_ctx *ctx = NULL; + struct bt_mesh_light_xyl_srv *srv = CONTAINER_OF(work, + struct bt_mesh_light_xyl_srv, + transition.timer.work); bt_mesh_light_server_state_change_t change = {0}; + struct bt_mesh_msg_ctx *ctx = NULL; if (srv == NULL || srv->state == NULL || - srv->transition.timer.work._reserved == NULL) { + srv->transition.timer.work.user_data == NULL) { BT_ERR("%s, Invalid parameter", __func__); return; } bt_mesh_light_server_lock(); - ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work.user_data; if (srv->transition.just_started) { srv->transition.just_started = false; @@ -857,8 +858,8 @@ void light_xyl_work_handler(struct k_work *work) change.xyl_set.lightness = srv->state->lightness; change.xyl_set.x = srv->state->x; change.xyl_set.y = srv->state->y; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); } else { transition_timer_start(&srv->transition); @@ -885,37 +886,37 @@ void light_xyl_work_handler(struct k_work *work) change.xyl_set.lightness = srv->state->lightness; change.xyl_set.x = srv->state->x; change.xyl_set.y = srv->state->y; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); light_xyl_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS); bt_mesh_light_server_unlock(); - return; } void light_lc_work_handler(struct k_work *work) { - struct bt_mesh_light_lc_srv *srv = - CONTAINER_OF(work, struct bt_mesh_light_lc_srv, transition.timer.work); - struct bt_mesh_msg_ctx *ctx = NULL; + struct bt_mesh_light_lc_srv *srv = CONTAINER_OF(work, + struct bt_mesh_light_lc_srv, + transition.timer.work); bt_mesh_light_server_state_change_t change = {0}; + struct bt_mesh_msg_ctx *ctx = NULL; - if (srv == NULL || srv->transition.timer.work._reserved == NULL) { + if (srv == NULL || srv->transition.timer.work.user_data == NULL) { BT_ERR("%s, Invalid parameter", __func__); return; } bt_mesh_light_server_lock(); - ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work.user_data; if (srv->transition.just_started) { srv->transition.just_started = false; if (srv->transition.counter == 0U) { change.lc_light_onoff_set.onoff = srv->lc->state.light_onoff; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); } else { /** @@ -929,8 +930,8 @@ void light_lc_work_handler(struct k_work *work) bt_mesh_light_server_state_change_t change = { .lc_light_onoff_set.onoff = srv->lc->state.light_onoff, }; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); } transition_timer_start(&srv->transition); } @@ -948,42 +949,42 @@ void light_lc_work_handler(struct k_work *work) srv->lc->state.light_onoff = srv->lc->state.target_light_onoff; if (srv->lc->state.light_onoff != BLE_MESH_STATE_ON) { change.lc_light_onoff_set.onoff = srv->lc->state.light_onoff; - bt_mesh_lighting_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_lighting_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); } } light_lc_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS); bt_mesh_light_server_unlock(); - return; } #endif /* CONFIG_BLE_MESH_LIGHTING_SERVER */ #if CONFIG_BLE_MESH_TIME_SCENE_SERVER void scene_recall_work_handler(struct k_work *work) { - struct bt_mesh_scene_srv *srv = - CONTAINER_OF(work, struct bt_mesh_scene_srv, transition.timer.work); - struct bt_mesh_msg_ctx *ctx = NULL; + struct bt_mesh_scene_srv *srv = CONTAINER_OF(work, + struct bt_mesh_scene_srv, + transition.timer.work); bt_mesh_time_scene_server_state_change_t change = {0}; + struct bt_mesh_msg_ctx *ctx = NULL; if (srv == NULL || srv->state == NULL || - srv->transition.timer.work._reserved == NULL) { + srv->transition.timer.work.user_data == NULL) { BT_ERR("%s, Invalid parameter", __func__); return; } bt_mesh_time_scene_server_lock(); - ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work.user_data; if (srv->transition.just_started) { srv->transition.just_started = false; if (srv->transition.counter == 0U) { change.scene_recall.scene_number = srv->state->current_scene; - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); } else { transition_timer_start(&srv->transition); @@ -1005,13 +1006,12 @@ void scene_recall_work_handler(struct k_work *work) } change.scene_recall.scene_number = srv->state->current_scene; - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, srv->model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, + srv->model, ctx, (const uint8_t *)&change, sizeof(change)); scene_publish(srv->model, ctx, BLE_MESH_MODEL_OP_SCENE_STATUS); bt_mesh_time_scene_server_unlock(); - return; } #endif /* CONFIG_BLE_MESH_TIME_SCENE_SERVER */ @@ -1027,11 +1027,9 @@ void bt_mesh_server_stop_transition(struct bt_mesh_state_transition *transition) void bt_mesh_server_start_transition(struct bt_mesh_state_transition *transition) { + k_delayed_work_submit(&transition->timer, K_MSEC(5 * transition->delay)); if (transition->delay) { - k_delayed_work_submit(&transition->timer, K_MSEC(5 * transition->delay)); bt_mesh_atomic_set_bit(transition->flag, BLE_MESH_TRANS_TIMER_START); - } else { - k_work_submit(&transition->timer.work); } } diff --git a/components/bt/esp_ble_mesh/models/server/time_scene_server.c b/components/bt/esp_ble_mesh/models/server/time_scene_server.c index 4166a087664c..3494653aaa68 100644 --- a/components/bt/esp_ble_mesh/models/server/time_scene_server.c +++ b/components/bt/esp_ble_mesh/models/server/time_scene_server.c @@ -18,20 +18,6 @@ static bt_mesh_mutex_t time_scene_server_lock; -static inline void bt_mesh_time_scene_server_mutex_new(void) -{ - if (!time_scene_server_lock.mutex) { - bt_mesh_mutex_create(&time_scene_server_lock); - } -} - -#if CONFIG_BLE_MESH_DEINIT -static inline void bt_mesh_time_scene_server_mutex_free(void) -{ - bt_mesh_mutex_free(&time_scene_server_lock); -} -#endif /* CONFIG_BLE_MESH_DEINIT */ - void bt_mesh_time_scene_server_lock(void) { bt_mesh_mutex_lock(&time_scene_server_lock); @@ -148,7 +134,6 @@ static void send_time_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void time_get(struct bt_mesh_model *model, @@ -191,8 +176,8 @@ static void time_get(struct bt_mesh_model *model, if (rsp_ctrl->get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { if (ctx->recv_op != BLE_MESH_MODEL_OP_TIME_STATUS) { - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } } @@ -208,7 +193,7 @@ static void time_get(struct bt_mesh_model *model, return; } if (srv->state->time_role != TIME_RELAY && - srv->state->time_role != TIME_CLINET) { + srv->state->time_role != TIME_CLIENT) { /** * If the value of the Time Role state of the element is 0x00 (None) or * 0x01 (Time Authority), the message shall be ignored. @@ -231,8 +216,8 @@ static void time_get(struct bt_mesh_model *model, status.time_status.tai_utc_delta = (val >> 1) & BIT_MASK(15); status.time_status.time_zone_offset = net_buf_simple_pull_u8(buf); } - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_STATUS_MSG, model, ctx, (const uint8_t *)&status, sizeof(status)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_STATUS_MSG, + model, ctx, (const uint8_t *)&status, sizeof(status)); return; } memcpy(srv->state->time.tai_seconds, buf->data, TAI_SECONDS_LEN); @@ -260,8 +245,8 @@ static void time_get(struct bt_mesh_model *model, change.time_status.uncertainty = srv->state->time.uncertainty; change.time_status.time_authority = srv->state->time.time_authority; change.time_status.tai_utc_delta_curr = srv->state->time.subsecond; - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (model->pub == NULL || model->pub->msg == NULL || model->pub->addr == BLE_MESH_ADDR_UNASSIGNED) { @@ -296,7 +281,6 @@ static void time_get(struct bt_mesh_model *model, } send_time_status(model, ctx, false, opcode); - return; } static void time_set(struct bt_mesh_model *model, @@ -325,8 +309,8 @@ static void time_set(struct bt_mesh_model *model, set.time_set.time_authority = val & BIT(0); set.time_set.tai_utc_delta = (val >> 1) & BIT_MASK(15); set.time_set.time_zone_offset = net_buf_simple_pull_u8(buf); - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } memcpy(srv->state->time.tai_seconds, buf->data, TAI_SECONDS_LEN); @@ -345,8 +329,8 @@ static void time_set(struct bt_mesh_model *model, set.time_zone_set.time_zone_offset_new = net_buf_simple_pull_u8(buf); memcpy(set.time_zone_set.tai_zone_change, buf->data, TAI_OF_ZONE_CHANGE_LEN); net_buf_simple_pull(buf, TAI_OF_ZONE_CHANGE_LEN); - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } srv->state->time.time_zone_offset_new = net_buf_simple_pull_u8(buf); @@ -365,8 +349,8 @@ static void time_set(struct bt_mesh_model *model, set.tai_utc_delta_set.tai_utc_delta_new = val & BIT_MASK(15); memcpy(set.tai_utc_delta_set.tai_delta_change, buf->data, TAI_OF_DELTA_CHANGE_LEN); net_buf_simple_pull(buf, TAI_OF_DELTA_CHANGE_LEN); - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } srv->state->time.tai_utc_delta_new = val & BIT_MASK(15); @@ -376,7 +360,7 @@ static void time_set(struct bt_mesh_model *model, break; case BLE_MESH_MODEL_OP_TIME_ROLE_SET: role = net_buf_simple_pull_u8(buf); - if (role > TIME_CLINET) { + if (role > TIME_CLIENT) { BT_ERR("Invalid Time Role 0x%02x", role); return; } @@ -384,8 +368,8 @@ static void time_set(struct bt_mesh_model *model, bt_mesh_time_scene_server_recv_set_msg_t set = { .time_role_set.time_role = role, }; - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } srv->state->time_role = role; @@ -419,12 +403,11 @@ static void time_set(struct bt_mesh_model *model, return; } - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); /* Send corresponding time status message */ send_time_status(model, ctx, false, opcode); - return; } /* Scene Server & Scene Setup Server message handlers */ @@ -480,7 +463,6 @@ static void send_scene_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void send_scene_register_status(struct bt_mesh_model *model, @@ -520,7 +502,7 @@ static void send_scene_register_status(struct bt_mesh_model *model, if (scene->scene_number != INVALID_SCENE_NUMBER) { total_len += SCENE_NUMBER_LEN; if ((publish == false && total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) || - (publish == true && total_len > msg->size + BLE_MESH_SERVER_TRANS_MIC_SIZE)) { + (publish == true && total_len > msg->size + BLE_MESH_SERVER_TRANS_MIC_SIZE)) { /* Add this in case the message is too long */ BT_WARN("Too large scene register status"); break; @@ -535,7 +517,6 @@ static void send_scene_register_status(struct bt_mesh_model *model, } else { BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); } - return; } static void scene_get(struct bt_mesh_model *model, @@ -550,8 +531,8 @@ static void scene_get(struct bt_mesh_model *model, } if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } @@ -583,7 +564,6 @@ void scene_publish(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, uin } send_scene_status(model, ctx, true); - return; } static void scene_recall(struct bt_mesh_model *model, @@ -622,8 +602,8 @@ static void scene_recall(struct bt_mesh_model *model, .scene_recall.trans_time = trans_time, .scene_recall.delay = delay, }; - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -681,16 +661,16 @@ static void scene_recall(struct bt_mesh_model *model, bt_mesh_time_scene_server_state_change_t change = { .scene_recall.scene_number = scene_number, }; - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); bt_mesh_time_scene_server_unlock(); return; } /* Copy the ctx of the received message */ - if (srv->transition.timer.work._reserved) { - memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + if (srv->transition.timer.work.user_data) { + memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx)); } /* For Instantaneous Transition */ @@ -715,7 +695,6 @@ static void scene_recall(struct bt_mesh_model *model, bt_mesh_time_scene_server_unlock(); bt_mesh_server_start_transition(&srv->transition); - return; } static void scene_action(struct bt_mesh_model *model, @@ -745,8 +724,8 @@ static void scene_action(struct bt_mesh_model *model, bt_mesh_time_scene_server_recv_set_msg_t set = { .scene_store.scene_number = scene_number, }; - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } /* Try to find a matching Scene Number */ @@ -800,8 +779,8 @@ static void scene_action(struct bt_mesh_model *model, bt_mesh_time_scene_server_state_change_t change = { .scene_store.scene_number = scene_number, }; - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); } break; } @@ -811,8 +790,8 @@ static void scene_action(struct bt_mesh_model *model, bt_mesh_time_scene_server_recv_set_msg_t set = { .scene_delete.scene_number = scene_number, }; - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } for (i = 0; i < srv->state->scene_count; i++) { @@ -904,8 +883,8 @@ static void scene_action(struct bt_mesh_model *model, bt_mesh_time_scene_server_state_change_t change = { .scene_delete.scene_number = scene_number, }; - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); break; } default: @@ -918,8 +897,6 @@ static void scene_action(struct bt_mesh_model *model, send_scene_register_status(model, ctx, srv->state->status_code, false); } send_scene_register_status(model, NULL, srv->state->status_code, true); - - return; } static uint16_t get_schedule_reg_bit(struct bt_mesh_scheduler_state *state) @@ -981,7 +958,6 @@ static void send_scheduler_act_status(struct bt_mesh_model *model, } BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, &msg, NULL, NULL)); - return; } static void scheduler_get(struct bt_mesh_model *model, @@ -999,8 +975,8 @@ static void scheduler_get(struct bt_mesh_model *model, switch (ctx->recv_op) { case BLE_MESH_MODEL_OP_SCHEDULER_GET: { if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG, + model, ctx, NULL, 0); return; } @@ -1020,8 +996,8 @@ static void scheduler_get(struct bt_mesh_model *model, bt_mesh_time_scene_server_recv_get_msg_t get = { .scheduler_act_get.index = index, }; - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG, + model, ctx, (const uint8_t *)&get, sizeof(get)); return; } @@ -1109,8 +1085,8 @@ static void scheduler_act_set(struct bt_mesh_model *model, .scheduler_act_set.trans_time = trans_time, .scheduler_act_set.scene_number = scene_number, }; - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, + model, ctx, (const uint8_t *)&set, sizeof(set)); return; } @@ -1139,14 +1115,12 @@ static void scheduler_act_set(struct bt_mesh_model *model, .scheduler_act_set.trans_time = trans_time, .scheduler_act_set.scene_number = scene_number, }; - bt_mesh_time_scene_server_cb_evt_to_btc( - BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change)); + bt_mesh_time_scene_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, + model, ctx, (const uint8_t *)&change, sizeof(change)); if (ctx->recv_op == BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET) { send_scheduler_act_status(model, ctx, index); } - - return; } /* message handlers (End) */ @@ -1235,6 +1209,7 @@ static int time_scene_server_init(struct bt_mesh_model *model) BT_ERR("Invalid Time State"); return -EINVAL; } + srv->model = model; break; } @@ -1244,6 +1219,7 @@ static int time_scene_server_init(struct bt_mesh_model *model) BT_ERR("Invalid Time State"); return -EINVAL; } + srv->model = model; break; } @@ -1253,9 +1229,11 @@ static int time_scene_server_init(struct bt_mesh_model *model) BT_ERR("Invalid Scene State"); 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_alloc_ctx(&srv->transition.timer.work); k_delayed_work_init(&srv->transition.timer, scene_recall_work_handler); @@ -1269,9 +1247,11 @@ static int time_scene_server_init(struct bt_mesh_model *model) BT_ERR("Invalid Scene State"); return -EINVAL; } + if (check_scene_server_init(srv->state)) { return -EINVAL; } + srv->model = model; break; } @@ -1281,10 +1261,12 @@ static int time_scene_server_init(struct bt_mesh_model *model) BT_ERR("Invalid Scheduler State"); return -EINVAL; } + if (srv->state->schedule_count == 0U || srv->state->schedules == NULL) { BT_ERR("Invalid Register Schedule"); return -EINVAL; } + srv->model = model; break; } @@ -1294,10 +1276,12 @@ static int time_scene_server_init(struct bt_mesh_model *model) BT_ERR("Invalid Scheduler State"); return -EINVAL; } + if (srv->state->schedule_count == 0U || srv->state->schedules == NULL) { BT_ERR("Invalid Register Schedule"); return -EINVAL; } + srv->model = model; break; } @@ -1306,7 +1290,7 @@ static int time_scene_server_init(struct bt_mesh_model *model) return -EINVAL; } - bt_mesh_time_scene_server_mutex_new(); + bt_mesh_mutex_create(&time_scene_server_lock); return 0; } @@ -1429,9 +1413,11 @@ static int time_scene_server_deinit(struct bt_mesh_model *model) BT_ERR("Invalid Scene State"); 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); @@ -1449,7 +1435,7 @@ static int time_scene_server_deinit(struct bt_mesh_model *model) return -EINVAL; } - bt_mesh_time_scene_server_mutex_free(); + bt_mesh_mutex_free(&time_scene_server_lock); return 0; } diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_agg_model_api.c b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_agg_model_api.c new file mode 100644 index 000000000000..5e4ac36b2309 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_agg_model_api.c @@ -0,0 +1,56 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_agg_model.h" +#include "esp_ble_mesh_agg_model_api.h" + +#if CONFIG_BLE_MESH_AGG_CLI +esp_err_t esp_ble_mesh_register_agg_client_callback(esp_ble_mesh_agg_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_AGG_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_agg_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_agg_client_msg_t *msg) +{ + btc_ble_mesh_agg_client_args_t btc_arg = {0}; + btc_msg_t btc_msg = {0}; + + if (params == NULL || params->model == NULL || msg == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + btc_msg.sig = BTC_SIG_API_CALL; + btc_msg.pid = BTC_PID_AGG_CLIENT; + btc_msg.act = BTC_BLE_MESH_ACT_AGG_CLIENT_SEND; + + btc_arg.agg_send.params = params; + btc_arg.agg_send.msg = msg; + + return (btc_transfer_context(&btc_msg, &btc_arg, sizeof(btc_ble_mesh_agg_client_args_t), + btc_ble_mesh_agg_client_arg_deep_copy, + btc_ble_mesh_agg_client_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_AGG_CLI */ + +#if CONFIG_BLE_MESH_AGG_SRV +esp_err_t esp_ble_mesh_register_agg_server_callback(esp_ble_mesh_agg_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_AGG_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_AGG_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_brc_model_api.c b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_brc_model_api.c new file mode 100644 index 000000000000..a75b788c9be7 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_brc_model_api.c @@ -0,0 +1,70 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "btc/btc_manage.h" +#include "btc_ble_mesh_brc_model.h" +#include "esp_ble_mesh_brc_model_api.h" + +#if CONFIG_BLE_MESH_BRC_CLI +esp_err_t esp_ble_mesh_register_brc_client_callback(esp_ble_mesh_brc_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_BRC_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +static bool bridge_cfg_client_send_need_param(esp_ble_mesh_opcode_t opcode) +{ + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_BRIDGED_SUBNETS_GET: + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_GET: + case ESP_BLE_MESH_MODEL_OP_SUBNET_BRIDGE_SET: + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_ADD: + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_REMOVE: + return true; + default: + return false; + } +} + +esp_err_t esp_ble_mesh_brc_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_brc_client_msg_t *msg) +{ + btc_ble_mesh_brc_client_args_t btc_arg = {0}; + btc_msg_t btc_msg = {0}; + + if (params == NULL || params->model == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr) || + (bridge_cfg_client_send_need_param(params->opcode) && msg == NULL)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + btc_msg.sig = BTC_SIG_API_CALL; + btc_msg.pid = BTC_PID_BRC_CLIENT; + btc_msg.act = BTC_BLE_MESH_ACT_BRC_CLIENT_SEND; + + btc_arg.brc_send.params = params; + btc_arg.brc_send.msg = msg; + + return (btc_transfer_context(&btc_msg, &btc_arg, sizeof(btc_ble_mesh_brc_client_args_t), + btc_ble_mesh_brc_client_arg_deep_copy, + btc_ble_mesh_brc_client_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_BRC_CLI */ + +#if CONFIG_BLE_MESH_BRC_SRV +esp_err_t esp_ble_mesh_register_brc_server_callback(esp_ble_mesh_brc_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_BRC_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_BRC_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_cm_data_api.c b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_cm_data_api.c new file mode 100644 index 000000000000..08aa94e97131 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_cm_data_api.c @@ -0,0 +1,38 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "esp_err.h" + +#include "mesh_v1.1/utils.h" +#include "esp_ble_mesh_defs.h" +#include "esp_ble_mesh_cm_data_api.h" + +extern int bt_mesh_comp_1_register(const void *comp); +extern int bt_mesh_models_metadata_register(const void *metadata, uint8_t metadata_page); + +esp_err_t esp_ble_mesh_comp_1_register(const esp_ble_mesh_comp_1_t *comp) +{ + if (comp == NULL || comp->element_count == 0 || + comp->elements == NULL) { + return ESP_ERR_INVALID_ARG; + } + + return bt_mesh_comp_1_register(comp); +} + +esp_err_t esp_ble_mesh_models_metadata_register(const esp_ble_mesh_models_metadata_t *metadata, + uint8_t metadata_page) +{ + if (metadata == NULL || metadata->element_count == 0 || + metadata->elements == NULL || + (metadata_page != 0 && metadata_page != 128)) { + return ESP_ERR_INVALID_ARG; + } + + return bt_mesh_models_metadata_register(metadata, metadata_page); +} diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_df_model_api.c b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_df_model_api.c new file mode 100644 index 000000000000..9868cd9e2370 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_df_model_api.c @@ -0,0 +1,102 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_df_model.h" +#include "esp_ble_mesh_df_model_api.h" + +#if CONFIG_BLE_MESH_DF_CLI +esp_err_t esp_ble_mesh_register_df_client_callback(esp_ble_mesh_df_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_DF_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +static bool directed_forwarding_client_get_need_param(esp_ble_mesh_opcode_t opcode) +{ + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_GET: + case ESP_BLE_MESH_MODEL_OP_PATH_METRIC_GET: + case ESP_BLE_MESH_MODEL_OP_DISCOVERY_TABLE_CAPS_GET: + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_CNT_GET: + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_GET: + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_GET: + case ESP_BLE_MESH_MODEL_OP_WANTED_LANES_GET: + case ESP_BLE_MESH_MODEL_OP_TWO_WAY_PATH_GET: + case ESP_BLE_MESH_MODEL_OP_PATH_ECHO_INTERVAL_GET: + case ESP_BLE_MESH_MODEL_OP_DIRECTED_PUB_POLICY_GET: + return true; + default: + return false; + } +} + +esp_err_t esp_ble_mesh_df_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_df_client_get_t *get) +{ + btc_ble_mesh_df_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr) || + (directed_forwarding_client_get_need_param(params->opcode) && get == 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_DF_CLIENT; + msg.act = BTC_BLE_MESH_ACT_DF_CLIENT_GET_STATE; + + arg.df_get.params = params; + arg.df_get.get = get; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_df_client_args_t), + btc_ble_mesh_df_client_arg_deep_copy, + btc_ble_mesh_df_client_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_df_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_df_client_set_t *set) +{ + btc_ble_mesh_df_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || set == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.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_DF_CLIENT; + msg.act = BTC_BLE_MESH_ACT_DF_CLIENT_SET_STATE; + + arg.df_set.params = params; + arg.df_set.set = set; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_df_client_args_t), + btc_ble_mesh_df_client_arg_deep_copy, + btc_ble_mesh_df_client_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_DF_CLI */ + +#if CONFIG_BLE_MESH_DF_SRV +esp_err_t esp_ble_mesh_register_df_server_callback(esp_ble_mesh_df_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_DF_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_DF_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_lcd_model_api.c b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_lcd_model_api.c new file mode 100644 index 000000000000..c6f3f0f1a3e0 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_lcd_model_api.c @@ -0,0 +1,56 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_lcd_model.h" +#include "esp_ble_mesh_lcd_model_api.h" + +#if CONFIG_BLE_MESH_LCD_CLI +esp_err_t esp_ble_mesh_register_lcd_client_callback(esp_ble_mesh_lcd_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_LCD_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_lcd_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_lcd_client_msg_t *msg) +{ + btc_ble_mesh_lcd_client_args_t btc_arg = {0}; + btc_msg_t btc_msg = {0}; + + if (params == NULL || params->model == NULL || msg == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + btc_msg.sig = BTC_SIG_API_CALL; + btc_msg.pid = BTC_PID_LCD_CLIENT; + btc_msg.act = BTC_BLE_MESH_ACT_LCD_CLIENT_SEND; + + btc_arg.lcd_send.params = params; + btc_arg.lcd_send.msg = msg; + + return (btc_transfer_context(&btc_msg, &btc_arg, sizeof(btc_ble_mesh_lcd_client_args_t), + btc_ble_mesh_lcd_client_arg_deep_copy, + btc_ble_mesh_lcd_client_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_LCD_CLI */ + +#if CONFIG_BLE_MESH_LCD_SRV +esp_err_t esp_ble_mesh_register_lcd_server_callback(esp_ble_mesh_lcd_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_LCD_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_LCD_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_odp_model_api.c b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_odp_model_api.c new file mode 100644 index 000000000000..e5000ff55339 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_odp_model_api.c @@ -0,0 +1,57 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_odp_model.h" +#include "esp_ble_mesh_odp_model_api.h" + +#if CONFIG_BLE_MESH_ODP_CLI +esp_err_t esp_ble_mesh_register_odp_client_callback(esp_ble_mesh_odp_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_ODP_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_odp_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_odp_client_msg_t *msg) +{ + btc_ble_mesh_odp_client_args_t btc_arg = {0}; + btc_msg_t btc_msg = {0}; + + if (params == NULL || params->model == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr) || + (params->opcode == ESP_BLE_MESH_MODEL_OP_OD_PRIV_PROXY_SET && msg == NULL)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + btc_msg.sig = BTC_SIG_API_CALL; + btc_msg.pid = BTC_PID_ODP_CLIENT; + btc_msg.act = BTC_BLE_MESH_ACT_ODP_CLIENT_SEND; + + btc_arg.odp_send.params = params; + btc_arg.odp_send.msg = msg; + + return (btc_transfer_context(&btc_msg, &btc_arg, sizeof(btc_ble_mesh_odp_client_args_t), + btc_ble_mesh_odp_client_arg_deep_copy, + btc_ble_mesh_odp_client_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_ODP_CLI */ + +#if CONFIG_BLE_MESH_ODP_SRV +esp_err_t esp_ble_mesh_register_odp_server_callback(esp_ble_mesh_odp_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_ODP_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_ODP_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_prb_model_api.c b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_prb_model_api.c new file mode 100644 index 000000000000..8c855f2ee7f0 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_prb_model_api.c @@ -0,0 +1,69 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "btc/btc_manage.h" +#include "btc_ble_mesh_prb_model.h" +#include "esp_ble_mesh_prb_model_api.h" + +#if CONFIG_BLE_MESH_PRB_CLI +esp_err_t esp_ble_mesh_register_prb_client_callback(esp_ble_mesh_prb_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_PRB_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +static bool private_beacon_client_send_need_param(esp_ble_mesh_opcode_t opcode) +{ + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_PRIV_BEACON_SET: + case ESP_BLE_MESH_MODEL_OP_PRIV_GATT_PROXY_SET: + case ESP_BLE_MESH_MODEL_OP_PRIV_NODE_IDENTITY_GET: + case ESP_BLE_MESH_MODEL_OP_PRIV_NODE_IDENTITY_SET: + return true; + default: + return false; + } +} + +esp_err_t esp_ble_mesh_prb_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_prb_client_msg_t *msg) +{ + btc_ble_mesh_prb_client_args_t btc_arg = {0}; + btc_msg_t btc_msg = {0}; + + if (params == NULL || params->model == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr) || + (private_beacon_client_send_need_param(params->opcode) && msg == NULL)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + btc_msg.sig = BTC_SIG_API_CALL; + btc_msg.pid = BTC_PID_PRB_CLIENT; + btc_msg.act = BTC_BLE_MESH_ACT_PRB_CLIENT_SEND; + + btc_arg.prb_send.params = params; + btc_arg.prb_send.msg = msg; + + return (btc_transfer_context(&btc_msg, &btc_arg, sizeof(btc_ble_mesh_prb_client_args_t), + btc_ble_mesh_prb_client_arg_deep_copy, + btc_ble_mesh_prb_client_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_PRB_CLI */ + +#if CONFIG_BLE_MESH_PRB_SRV +esp_err_t esp_ble_mesh_register_prb_server_callback(esp_ble_mesh_prb_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_PRB_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_PRB_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_rpr_model_api.c b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_rpr_model_api.c new file mode 100644 index 000000000000..5ab2cd17d9f2 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_rpr_model_api.c @@ -0,0 +1,94 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_rpr_model.h" +#include "esp_ble_mesh_rpr_model_api.h" + +#if CONFIG_BLE_MESH_RPR_CLI +esp_err_t esp_ble_mesh_register_rpr_client_callback(esp_ble_mesh_rpr_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_RPR_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +static bool remote_prov_client_get_need_param(esp_ble_mesh_opcode_t opcode) +{ + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START: + case ESP_BLE_MESH_MODEL_OP_RPR_EXT_SCAN_START: + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN: + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_CLOSE: + return true; + default: + return false; + } +} + +esp_err_t esp_ble_mesh_rpr_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_rpr_client_msg_t *msg) +{ + btc_ble_mesh_rpr_client_args_t arg = {0}; + btc_msg_t btc_msg = {0}; + + if (params == NULL || params->model == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr) || + (remote_prov_client_get_need_param(params->opcode) && msg == NULL)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + btc_msg.sig = BTC_SIG_API_CALL; + btc_msg.pid = BTC_PID_RPR_CLIENT; + btc_msg.act = BTC_BLE_MESH_ACT_RPR_CLIENT_SEND; + + arg.rpr_send.params = params; + arg.rpr_send.msg = msg; + + return (btc_transfer_context(&btc_msg, &arg, sizeof(btc_ble_mesh_rpr_client_args_t), + btc_ble_mesh_rpr_client_arg_deep_copy, + btc_ble_mesh_rpr_client_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_rpr_client_action(esp_ble_mesh_rpr_client_act_type_t type, + esp_ble_mesh_rpr_client_act_param_t *param) +{ + btc_ble_mesh_rpr_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (type > ESP_BLE_MESH_RPR_CLIENT_ACT_MAX || param == 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_RPR_CLIENT; + msg.act = BTC_BLE_MESH_ACT_RPR_CLIENT_ACT; + + arg.rpr_act.type = type; + arg.rpr_act.param = param; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_rpr_client_args_t), + btc_ble_mesh_rpr_client_arg_deep_copy, + btc_ble_mesh_rpr_client_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_RPR_CLI */ + +#if CONFIG_BLE_MESH_RPR_SRV +esp_err_t esp_ble_mesh_register_rpr_server_callback(esp_ble_mesh_rpr_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_RPR_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_RPR_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_sar_model_api.c b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_sar_model_api.c new file mode 100644 index 000000000000..51a051ec12f9 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_sar_model_api.c @@ -0,0 +1,58 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_sar_model.h" +#include "esp_ble_mesh_sar_model_api.h" + +#if CONFIG_BLE_MESH_SAR_CLI +esp_err_t esp_ble_mesh_register_sar_client_callback(esp_ble_mesh_sar_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_SAR_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_sar_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_sar_client_msg_t *msg) +{ + btc_ble_mesh_sar_client_args_t btc_arg = {0}; + btc_msg_t btc_msg = {0}; + + if (params == NULL || params->model == NULL || (msg == NULL && + (params->opcode == ESP_BLE_MESH_MODEL_OP_SAR_TRANSMITTER_SET || + params->opcode == ESP_BLE_MESH_MODEL_OP_SAR_RECEIVER_SET)) || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + btc_msg.sig = BTC_SIG_API_CALL; + btc_msg.pid = BTC_PID_SAR_CLIENT; + btc_msg.act = BTC_BLE_MESH_ACT_SAR_CLIENT_SEND; + + btc_arg.sar_send.params = params; + btc_arg.sar_send.msg = msg; + + return (btc_transfer_context(&btc_msg, &btc_arg, sizeof(btc_ble_mesh_sar_client_args_t), + btc_ble_mesh_sar_client_arg_deep_copy, + btc_ble_mesh_sar_client_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_SAR_CLI */ + +#if CONFIG_BLE_MESH_SAR_SRV +esp_err_t esp_ble_mesh_register_sar_server_callback(esp_ble_mesh_sar_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_SAR_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_SAR_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_srpl_model_api.c b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_srpl_model_api.c new file mode 100644 index 000000000000..8d5aa16b4dad --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/esp_ble_mesh_srpl_model_api.c @@ -0,0 +1,56 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_srpl_model.h" +#include "esp_ble_mesh_srpl_model_api.h" + +#if CONFIG_BLE_MESH_SRPL_CLI +esp_err_t esp_ble_mesh_register_srpl_client_callback(esp_ble_mesh_srpl_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_SRPL_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_srpl_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_srpl_client_msg_t *msg) +{ + btc_ble_mesh_srpl_client_args_t btc_arg = {0}; + btc_msg_t btc_msg = {0}; + + if (params == NULL || params->model == NULL || msg == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + btc_msg.sig = BTC_SIG_API_CALL; + btc_msg.pid = BTC_PID_SRPL_CLIENT; + btc_msg.act = BTC_BLE_MESH_ACT_SRPL_CLIENT_SEND; + + btc_arg.srpl_send.params = params; + btc_arg.srpl_send.msg = msg; + + return (btc_transfer_context(&btc_msg, &btc_arg, sizeof(btc_ble_mesh_srpl_client_args_t), + btc_ble_mesh_srpl_client_arg_deep_copy, + btc_ble_mesh_srpl_client_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_SRPL_CLI */ + +#if CONFIG_BLE_MESH_SRPL_SRV +esp_err_t esp_ble_mesh_register_srpl_server_callback(esp_ble_mesh_srpl_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_SRPL_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_SRPL_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_agg_model_api.h b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_agg_model_api.h new file mode 100644 index 000000000000..08f436fa6d4b --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_agg_model_api.h @@ -0,0 +1,218 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ESP_BLE_MESH_AGG_MODEL_API_H_ +#define _ESP_BLE_MESH_AGG_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Defines the Opcodes Aggregator message opcode. */ +#define ESP_BLE_MESH_MODEL_OP_AGG_SEQUENCE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x72) +#define ESP_BLE_MESH_MODEL_OP_AGG_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x73) + +/** Defines the status codes for Opcodes Aggregator messages. */ +#define ESP_BLE_MESH_AGG_STATUS_SUCCESS 0x00 +#define ESP_BLE_MESH_AGG_STATUS_INVALID_ADDRESS 0x01 +#define ESP_BLE_MESH_AGG_STATUS_INVALID_MODEL 0x02 +#define ESP_BLE_MESH_AGG_STATUS_WRONG_ACCESS_KEY 0x03 +#define ESP_BLE_MESH_AGG_STATUS_WRONG_OPCODE 0x04 +#define ESP_BLE_MESH_AGG_STATUS_MSG_NOT_UNDERSTOOD 0x05 + +/* A message that is not understood includes messages that have + * one or more of the following conditions: + * • The application opcode is unknown by the receiving element. + * • The access message size for the application opcode is incorrect. + * • The application parameters contain values that are currently + * Prohibited. + */ + +/** Values of the Length_Format */ +#define ESP_BLE_MESH_AGG_ITEM_LENGTH_FORMAT_SHORT 0 +#define ESP_BLE_MESH_AGG_ITEM_LENGTH_FORMAT_LONG 1 + +/** @def ESP_BLE_MESH_MODEL_AGG_SRV + * + * @brief Define a new Opcodes Aggregator Server model. + * + * @note If supported, the Opcodes Aggregator Server model shall be + * supported by a primary element. + * + * @param srv_data Pointer to a unique Opcodes Aggregator Server + * model user_data. + * + * @return New Opcodes Aggregator Server model instance. + */ +#define ESP_BLE_MESH_MODEL_AGG_SRV(srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_AGG_SRV, \ + NULL, NULL, srv_data) + +/** @def ESP_BLE_MESH_MODEL_AGG_CLI + * + * @brief Define a new Opcodes Aggregator Client model. + * + * @note If supported, the model shall be supported by the primary + * element and shall not be supported by any secondary elements. + * + * @param cli_data Pointer to a unique Opcodes Aggregator Client + * model user_data. + * + * @return New Opcodes Aggregator Client model instance. + */ +#define ESP_BLE_MESH_MODEL_AGG_CLI(cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_AGG_CLI, \ + NULL, NULL, cli_data) + +/** Opcodes Aggregator Server model context */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to Opcodes Aggregator Server model */ +} esp_ble_mesh_agg_srv_t; /*!< Opcodes Aggregator Server model context */ + +/** Parameters of Aggregator Item */ +typedef struct { + uint16_t length_format:1, /*!< 0: Length_Short; 1: Length_Long */ + length:15; /*!< Size of Opcode_And_Parameters field */ + const uint8_t *data; /*!< Opcode and parameters */ +} esp_ble_mesh_agg_item_t; /*!< Parameters of Aggregator Item */ + +/** Parameters of Opcodes Aggregator Sequence */ +typedef struct { + uint16_t element_addr; /*!< Element address */ + struct net_buf_simple *items; /*!< List of items with each item represented as an Aggregator Item */ +} esp_ble_mesh_agg_sequence_t; /*!< Parameters of Opcodes Aggregator Sequence */ + +/** + * @brief Opcodes Aggregator Client model message union + */ +typedef union { + esp_ble_mesh_agg_sequence_t agg_sequence; /*!< For ESP_BLE_MESH_MODEL_OP_AGG_SEQUENCE */ +} esp_ble_mesh_agg_client_msg_t; /*!< Opcodes Aggregator Client model message union */ + +/** Parameters of Opcodes Aggregator Status */ +typedef struct { + uint8_t status; /*!< Status of the most recent operation */ + uint16_t element_addr; /*!< Element Address */ + struct net_buf_simple *items; /*!< List of status items with each status item containing an unacknowledged access layer message or empty item (Optional) */ +} esp_ble_mesh_agg_status_t; /*!< Parameters of Opcodes Aggregator Status */ + +/** Result of sending Opcodes Aggregator Client messages */ +typedef struct { + int err_code; /*!< Result of sending a message */ +} esp_ble_mesh_agg_client_send_cb_t; /*!< Result of sending Opcodes Aggregator Client messages */ + +/** + * @brief Opcodes Aggregator Client model received message union + */ +typedef union { + esp_ble_mesh_agg_status_t agg_status; /*!< For ESP_BLE_MESH_MODEL_OP_AGG_STATUS */ +} esp_ble_mesh_agg_client_recv_cb_t; /*!< Opcodes Aggregator Client model received message union */ + +/** Opcodes Aggregator Client model callback parameters */ +typedef struct { + esp_ble_mesh_client_common_param_t *params; /*!< Client common parameters, used by all events */ + /** Union of AGG Client callback */ + union { + esp_ble_mesh_agg_client_send_cb_t send; /*!< Result of sending a message */ + esp_ble_mesh_agg_client_recv_cb_t recv; /*!< Parameters of received status message */ + }; +} esp_ble_mesh_agg_client_cb_param_t; /*!< Opcodes Aggregator Client model callback parameters */ + +/** This enum value is the event of Opcodes Aggregator Client model */ +typedef enum { + ESP_BLE_MESH_AGG_CLIENT_SEND_COMP_EVT, + ESP_BLE_MESH_AGG_CLIENT_SEND_TIMEOUT_EVT, + ESP_BLE_MESH_AGG_CLIENT_RECV_RSP_EVT, + ESP_BLE_MESH_AGG_CLIENT_RECV_PUB_EVT, + ESP_BLE_MESH_AGG_CLIENT_EVT_MAX, +} esp_ble_mesh_agg_client_cb_event_t; + +/** + * @brief Opcodes Aggregator Server model related context. + */ + +/** + * @brief Opcodes Aggregator Server model received message union + */ +typedef union { + esp_ble_mesh_agg_sequence_t agg_sequence; /*!< For ESP_BLE_MESH_MODEL_OP_AGG_SEQUENCE */ +} esp_ble_mesh_agg_server_recv_msg_t; /*!< Opcodes Aggregator Server model received message union */ + +/** Opcodes Aggregator Server model callback parameters */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + esp_ble_mesh_msg_ctx_t ctx; /*!< Context of the received message */ + /** Union of AGG Server callback */ + union { + esp_ble_mesh_agg_server_recv_msg_t recv; /*!< Received message callback values */ + }; +} esp_ble_mesh_agg_server_cb_param_t; /*!< Opcodes Aggregator Server model callback parameters */ + +/** This enum value is the event of Opcodes Aggregator Server model */ +typedef enum { + ESP_BLE_MESH_AGG_SERVER_RECV_MSG_EVT, + ESP_BLE_MESH_AGG_SERVER_EVT_MAX, +} esp_ble_mesh_agg_server_cb_event_t; + +/** + * @brief Bluetooth Mesh Opcodes Aggregator client and server model functions. + */ + +/** + * @brief Opcodes Aggregator Client model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_agg_client_cb_t)(esp_ble_mesh_agg_client_cb_event_t event, + esp_ble_mesh_agg_client_cb_param_t *param); + +/** + * @brief Register BLE Mesh Opcodes Aggregator Client model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_agg_client_callback(esp_ble_mesh_agg_client_cb_t callback); + +/** + * @brief Set the value of Opcodes Aggregator Server model state with the corresponding set message. + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] msg: Pointer to Opcodes Aggregator Client message. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_agg_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_agg_client_msg_t *msg); + +/** + * @brief Opcodes Aggregator Server model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_agg_server_cb_t)(esp_ble_mesh_agg_server_cb_event_t event, + esp_ble_mesh_agg_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh Opcodes Aggregator Server model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_agg_server_callback(esp_ble_mesh_agg_server_cb_t callback); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_AGG_MODEL_API_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_brc_model_api.h b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_brc_model_api.h new file mode 100644 index 000000000000..e1d4d336e8db --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_brc_model_api.h @@ -0,0 +1,322 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ESP_BLE_MESH_BRC_MODEL_API_H_ +#define _ESP_BLE_MESH_BRC_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ESP_BLE_MESH_MODEL_OP_SUBNET_BRIDGE_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xB1) +#define ESP_BLE_MESH_MODEL_OP_SUBNET_BRIDGE_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xB2) +#define ESP_BLE_MESH_MODEL_OP_SUBNET_BRIDGE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0xB3) +#define ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_ADD ESP_BLE_MESH_MODEL_OP_2(0x80, 0xB4) +#define ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_REMOVE ESP_BLE_MESH_MODEL_OP_2(0x80, 0xB5) +#define ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0xB6) +#define ESP_BLE_MESH_MODEL_OP_BRIDGED_SUBNETS_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xB7) +#define ESP_BLE_MESH_MODEL_OP_BRIDGED_SUBNETS_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0xB8) +#define ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xB9) +#define ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0xBA) +#define ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_SIZE_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xBB) +#define ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_SIZE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0xBC) + +/** @def ESP_BLE_MESH_MODEL_BRC_SRV + * + * @brief Define a new Bridge Configuration Server model. + * + * @note If supported, the model shall be supported by a primary element + * and shall not be supported by any secondary elements. + * + * @param srv_data Pointer to a unique Bridge Configuration Server + * model user_data. + * + * @return New Bridge Configuration Server model instance. + */ +#define ESP_BLE_MESH_MODEL_BRC_SRV(srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_BRC_SRV, \ + NULL, NULL, srv_data) + +/** @def ESP_BLE_MESH_MODEL_BRC_CLI + * + * @brief Define a new Bridge Configuration Client model. + * + * @note If supported, the model shall be supported by a primary element + * and shall not be supported by any secondary elements. + * + * @param cli_data Pointer to a unique Bridge Configuration Client + * model user_data. + * + * @return New Bridge Configuration Client model instance. + */ +#define ESP_BLE_MESH_MODEL_BRC_CLI(cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_BRC_CLI, \ + NULL, NULL, cli_data) + +/** Parameters of subnet bridge table */ +typedef struct { + uint8_t bridge_direction; /*!< Allowed directions for the bridged traffic */ + uint8_t bridge_net_idx[3]; /*!< Two NetKey Indexes are packed into three octets */ + uint16_t bridge_addr_1; /*!< Address of the node in the first subnet */ + uint16_t bridge_addr_2; /*!< Address of the node in the second subnet */ +} esp_ble_mesh_subnet_bridge_table_t; /*!< Entries of subnet bridge table */ + +#if CONFIG_BLE_MESH_BRC_SRV +/** Bridge Configuration Server model context */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to Bridge Configuration Server model */ + + uint8_t subnet_bridge; /*!< Subnet Bridge state */ + uint16_t bridging_table_size; /*!< Bridging Table Size */ + + esp_ble_mesh_subnet_bridge_table_t bridge_table[CONFIG_BLE_MESH_MAX_BRIDGING_TABLE_ENTRY_COUNT]; /*!< Subnet Bridge table, used to storage bridging table entries */ +} esp_ble_mesh_brc_srv_t; /*!< Bridge Configuration Server model context */ +#endif + +/** Parameters of Subnet Bridge Set */ +typedef struct { + uint8_t subnet_bridge; /*!< New Subnet Bridge state */ +} esp_ble_mesh_subnet_bridge_set_t; /*!< Parameters of Subnet Bridge Set */ + +/** Parameters of Bridging Table Add */ +typedef struct { + uint8_t bridge_direction; /*!< Allowed directions for the bridged traffic */ + uint16_t bridge_net_idx_1; /*!< NetKey Index of the first subnet */ + uint16_t bridge_net_idx_2; /*!< NetKey Index of the second subnet */ + uint16_t bridge_addr_1; /*!< Address of the node in the first subnet */ + uint16_t bridge_addr_2; /*!< Address of the node in the second subnet */ +} esp_ble_mesh_bridging_table_add_t; /*!< Parameters of Bridging Table Add */ + +/** Parameters of Bridging Table Remove */ +typedef struct { + uint16_t bridge_net_idx_1; /*!< NetKey Index of the first subnet */ + uint16_t bridge_net_idx_2; /*!< NetKey Index of the second subnet */ + uint16_t bridge_addr_1; /*!< Address of the node in the first subnet */ + uint16_t bridge_addr_2; /*!< Address of the node in the second subnet */ +} esp_ble_mesh_bridging_table_remove_t; /*!< Parameters of Bridging Table Remove */ + +/** Parameters of Bridged Subnets Get */ +typedef struct { + uint16_t bridge_filter:2, /*!< Filter to be applied when reporting the set of pairs of NetKey Indexes */ + bridge_net_idx:12; /*!< NetKey Index of any of the subnets */ + uint8_t bridge_start_idx; /*!< Start offset in units of Bridging Table state entries */ +} esp_ble_mesh_bridged_subnets_get_t; /*!< Parameters of Bridged Subnets Get */ + +/** Parameters of Bridging Table Get */ +typedef struct { + uint16_t bridge_net_idx_1; /*!< NetKey Index of first subnet */ + uint16_t bridge_net_idx_2; /*!< NetKey Index of the second subnet */ + uint16_t bridge_start_idx; /*!< Start offset in units of Bridging Table state entries */ +} esp_ble_mesh_bridging_table_get_t; /*!< Parameters of Bridging Table Get */ + +/** + * @brief Bridge Configuration Client model message union + */ +typedef union { + esp_ble_mesh_bridged_subnets_get_t bridged_subnets_get; /*!< For ESP_BLE_MESH_MODEL_OP_BRIDGED_SUBNETS_GET */ + esp_ble_mesh_bridging_table_get_t bridging_table_get; /*!< For ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_GET */ + esp_ble_mesh_subnet_bridge_set_t subnet_bridge_set; /*!< For ESP_BLE_MESH_MODEL_OP_SUBNET_BRIDGE_SET */ + esp_ble_mesh_bridging_table_add_t bridging_table_add; /*!< For ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_ADD */ + esp_ble_mesh_bridging_table_remove_t bridging_table_remove; /*!< For ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_REMOVE */ +} esp_ble_mesh_brc_client_msg_t; /*!< Bridge Configuration Client model message union */ + +/** Parameters of Subnet Bridge Status */ +typedef struct { + uint8_t subnet_bridge; /*!< Current Subnet Bridge state */ +} esp_ble_mesh_subnet_bridge_status_t; /*!< Parameters of Subnet Bridge Status */ + +/** Parameters of Bridging Table Status */ +typedef struct { + uint8_t status; /*!< Status Code for the requesting message */ + uint8_t bridge_direction; /*!< Allowed directions for the bridged traffic */ + uint16_t bridge_net_idx_1; /*!< NetKey Index of the first subnet */ + uint16_t bridge_net_idx_2; /*!< NetKey Index of the second subnet */ + uint16_t bridge_addr_1; /*!< Address of the node in the first subnet */ + uint16_t bridge_addr_2; /*!< Address of the node in the second subnet */ +} esp_ble_mesh_bridging_table_status_t; /*!< Parameters of Bridging Table Status */ + +/** Bridged_Subnets_List entry format */ +typedef struct { + uint16_t bridge_net_idx_1; /*!< NetKey Index of the first subnet */ + uint16_t bridge_net_idx_2; /*!< NetKey Index of the second subnet */ +} esp_ble_mesh_bridge_net_idx_pair_entry_t; /*!< Bridged_Subnets_List entry format */ + +/** Parameters of Bridged Subnets List */ +typedef struct { + uint16_t bridge_filter:2, /*!< Filter applied to the set of pairs of NetKey Indexes */ + bridge_net_idx:12; /*!< NetKey Index used for filtering or ignored */ + uint8_t bridge_start_idx; /*!< Start offset in units of bridges */ + + uint8_t bridged_entry_list_size; /*!< Num of pairs of NetKey Indexes */ + esp_ble_mesh_bridge_net_idx_pair_entry_t *net_idx_pair; /*!< Filtered set of N pairs of NetKey Indexes */ +} esp_ble_mesh_bridged_subnets_list_t; /*!< Parameters of Bridged Subnets List */ + +/** Bridged_Addresses_List entry format */ +typedef struct { + uint8_t bridge_direction; /*!< Allowed directions for bridged traffic */ + uint16_t bridge_addr_1; /*!< Address of the node in the first subnet */ + uint16_t bridge_addr_2; /*!< Address of the node in the second subnet */ +}esp_ble_mesh_bridged_addr_list_entry_t; /*!< Bridged_Addresses_List entry format */ + +/** Parameters of Bridging Table List */ +typedef struct { + uint8_t status; /*!< Status Code for the requesting message */ + uint16_t bridge_net_idx_1; /*!< NetKey Index of the first subnet */ + uint16_t bridge_net_idx_2; /*!< NetKey Index of the second subnet */ + uint16_t bridge_start_idx; /*!< Start offset in units of Bridging Table state entries */ + uint16_t bridged_addr_list_size; /*!< Num of pairs of entry */ + esp_ble_mesh_bridged_addr_list_entry_t *bridged_addr_list; /*!< List of bridged addresses and allowed traffic directions */ +} esp_ble_mesh_bridging_table_list_t; /*!< Parameters of Bridging Table List */ + +/** Parameters of Bridging Table Size Status */ +typedef struct { + uint16_t bridging_table_size; /*!< Bridging Table Size state */ +} esp_ble_mesh_bridging_table_size_status_t; /*!< Parameters of Bridging Table Size Status */ + +/** + * @brief Bridge Configuration Client model received message union + */ +typedef union { + esp_ble_mesh_subnet_bridge_status_t subnet_bridge_status; /*!< ESP_BLE_MESH_MODEL_OP_SUBNET_BRIDGE_STATUS */ + esp_ble_mesh_bridging_table_status_t bridging_table_status; /*!< ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_STATUS */ + esp_ble_mesh_bridged_subnets_list_t bridged_subnets_list; /*!< ESP_BLE_MESH_MODEL_OP_BRIDGED_SUBNETS_LIST */ + esp_ble_mesh_bridging_table_list_t bridging_table_list; /*!< ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_LIST */ + esp_ble_mesh_bridging_table_size_status_t bridging_table_size_status; /*!< ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_SIZE_STATUS */ +} esp_ble_mesh_brc_client_recv_cb_t;/*!< */ + +/** Result of sending Bridge Configuration Client messages */ +typedef struct { + int err_code; /*!< Result of sending a message */ +} esp_ble_mesh_brc_client_send_cb_t; /*!< Result of sending Bridge Configuration Client messages */ + +/** Bridge Configuration Client model callback parameters */ +typedef struct { + esp_ble_mesh_client_common_param_t *params; /*!< Client common parameters, used by all events. */ + /** Union of Bridge Configuration Client callback */ + union { + esp_ble_mesh_brc_client_send_cb_t send; /*!< Result of sending a message */ + esp_ble_mesh_brc_client_recv_cb_t recv; /*!< Parameters of received status message */ + }; +} esp_ble_mesh_brc_client_cb_param_t; /*!< Bridge Configuration Client model callback parameters */ + +/** This enum value is the event of Bridge Configuration Client model */ +typedef enum { + ESP_BLE_MESH_BRC_CLIENT_SEND_COMP_EVT, + ESP_BLE_MESH_BRC_CLIENT_SEND_TIMEOUT_EVT, + ESP_BLE_MESH_BRC_CLIENT_RECV_RSP_EVT, + ESP_BLE_MESH_BRC_CLIENT_RECV_PUB_EVT, + ESP_BLE_MESH_BRC_CLIENT_EVT_MAX, +} esp_ble_mesh_brc_client_cb_event_t; + +/** + * @brief Bridge Configuration Server model related context. + */ +/** Parameters of Bridging Table Add */ +typedef struct { + uint8_t bridge_direction; /*!< Allowed directions for the bridged traffic */ + uint16_t bridge_net_idx_1; /*!< NetKey Index of the first subnet */ + uint16_t bridge_net_idx_2; /*!< NetKey Index of the second subnet */ + uint16_t bridge_addr_1; /*!< Address of the node in the first subnet */ + uint16_t bridge_addr_2; /*!< Address of the node in the second subnet */ +} esp_ble_mesh_state_change_bridging_table_add_t; /*!< Parameters of Bridging Table Add */ + +/** Parameters of Bridging Table Remove */ +typedef struct { + uint16_t bridge_net_idx_1; /*!< NetKey Index of the first subnet */ + uint16_t bridge_net_idx_2; /*!< NetKey Index of the second subnet */ + uint16_t bridge_addr_1; /*!< Address of the node in the first subnet */ + uint16_t bridge_addr_2; /*!< Address of the node in the second subnet */ +} esp_ble_mesh_state_change_bridging_table_remove_t; /*!< Parameters of Bridging Table Remove */ + +/** + * @brief Bridge Configuration Server model state change value union + */ +typedef union { + /** + * The recv_op in ctx can be used to decide which state is changed. + */ + esp_ble_mesh_state_change_bridging_table_add_t bridging_table_add; /*!< Bridging Table Add */ + esp_ble_mesh_state_change_bridging_table_remove_t bridging_table_remove; /*!< Bridging Table Remove*/ +} esp_ble_mesh_brc_server_state_change_t; /*!< Bridge Configuration Server model state change value union */ + +/** + * @brief Bridge Configuration Server model callback value union + */ +typedef union { + esp_ble_mesh_brc_server_state_change_t state_change; /*!< For ESP_BLE_MESH_BRC_SERVER_STATE_CHANGE_EVT */ +} esp_ble_mesh_brc_server_cb_value_t; /*!< Bridge Configuration Server model callback value union */ + +/** Bridge Configuration Server model callback parameters */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + esp_ble_mesh_msg_ctx_t ctx; /*!< Context of the received message */ + esp_ble_mesh_brc_server_cb_value_t value; /*!< Value of the received configuration messages */ +} esp_ble_mesh_brc_server_cb_param_t; /*!< Bridge Configuration Server model callback parameters */ + +/** This enum value is the event of Bridge Configuration Server model */ +typedef enum { + ESP_BLE_MESH_BRC_SERVER_STATE_CHANGE_EVT, + ESP_BLE_MESH_BRC_SERVER_EVT_MAX, +} esp_ble_mesh_brc_server_cb_event_t; + +/** + * @brief Bluetooth Mesh Bridge Configuration client and server model functions. + */ + +/** + * @brief Bridge Configuration Client model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_brc_client_cb_t)(esp_ble_mesh_brc_client_cb_event_t event, + esp_ble_mesh_brc_client_cb_param_t *param); + +/** + * @brief Bridge Configuration Server model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_brc_server_cb_t)(esp_ble_mesh_brc_server_cb_event_t event, + esp_ble_mesh_brc_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh Bridge Configuration Client model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_brc_client_callback(esp_ble_mesh_brc_client_cb_t callback); + +/** + * @brief Register BLE Mesh Bridge Configuration Server model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_brc_server_callback(esp_ble_mesh_brc_server_cb_t callback); + +/** + * @brief Get/Set the value of Bridge Configuration Server model state with the corresponding message. + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] msg: Pointer to Bridge Configuration Client message. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_brc_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_brc_client_msg_t *msg); +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_BRC_MODEL_API_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_cm_data_api.h b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_cm_data_api.h new file mode 100644 index 000000000000..ed8d2882449b --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_cm_data_api.h @@ -0,0 +1,136 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ESP_BLE_MESH_CM_DATA_API_H_ +#define _ESP_BLE_MESH_CM_DATA_API_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/*!< Definitions of the format of Extended_Model_Items indicator */ +#define ESP_BLE_MESH_MODEL_ITEM_SHORT 0 +#define ESP_BLE_MESH_MODEL_ITEM_LONG 1 + +/* The value of the Element_Offset field is mapped to an integer value. + * |----------------|-------------------| + * | Element_Offset | Represented Value | + * |----------------|-------------------| + * | 0 | 0 | + * | 1 | 1 | + * | 2 | 2 | + * | 3 | 3 | + * | 4 | -4 | + * | 5 | -3 | + * | 6 | -2 | + * | 7 | -1 | + * |----------------|-------------------| + */ + +/** Format of Extended Model Item */ +typedef struct { + /** Union of Extended Model Item */ + union { + uint8_t element_offset:3, /*!< Element address modifier, in the range -4 to 3. See above. */ + model_item_idx:5; /*!< Model Index, in the range 0 to 31 */ + /** Extended Model Item long format */ + struct { + int8_t element_offset; /*!< Element address modifier, in the range -128 to 127 */ + uint8_t model_item_idx; /*!< Model index, in the range 0 to 255 */ + } long_fmt; /*!< Extended Model Item long format */ + }; +} esp_ble_mesh_extended_model_item_t; /*!< Format of Extended Model Item */ + +/** Format of Model Item */ +typedef struct { + uint8_t corresponding_present:1, /*!< Corresponding_Group_ID field indicator */ + format:1, /*!< Format of Extended_Model_Items indicator */ + extended_items_count:6; /*!< Number of Extended Model Items in the Extended_Model_Items field */ + uint8_t corresponding_group_id; /*!< Corresponding group identifier */ + esp_ble_mesh_extended_model_item_t *const extended_model_items; /*!< List of Extended Model Items */ +} esp_ble_mesh_model_item_t; /*!< Format of Model Item */ + +/** Format of element of Composition Data Page 1 */ +typedef struct { + const uint8_t num_s; /*!< A count of SIG Models Items in this element */ + const uint8_t num_v; /*!< A count of Vendor Models Items in this element */ + + esp_ble_mesh_model_item_t *const model_items_s; /*!< A sequence of "num_s" SIG Model Items */ + esp_ble_mesh_model_item_t *const model_items_v; /*!< A sequence of "num_v" Vendor Model Items */ +} esp_ble_mesh_comp_1_elem_t; /*!< Format of element of Composition Data Page 1 */ + +/** Format of Composition Data Page 1 */ +typedef struct { + size_t element_count; /*!< Element count */ + esp_ble_mesh_comp_1_elem_t *elements; /*!< A sequence of element descriptions */ +} esp_ble_mesh_comp_1_t; /*!< Format of Composition Data Page 1 */ + +/** Format of Metadata entry */ +typedef struct { + uint16_t metadata_len; /*!< Size of the Metadata field */ + uint16_t metadata_id; /*!< Bluetooth assigned number for the Metadata Identifier */ + const uint8_t *metadata; /*!< Model’s metadata */ +} esp_ble_mesh_metadata_entry_t; /*!< Format of Metadata entry */ + +/** Format of Metadata item */ +typedef struct { + /** Union of model ID */ + union { + uint16_t model_id; /*!< Model ID */ + /** Vendor model identifier */ + struct { + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ + } vnd; /*!< Vendor model identifier */ + }; + uint8_t metadata_entries_num; /*!< Number of metadata entries */ + esp_ble_mesh_metadata_entry_t *const metadata_entries; /*!< List of model’s metadata */ +} esp_ble_mesh_metadata_item_t; /*!< Format of Metadata item */ + +/** Format of Metadata element of Models Metadata Page 0/128 */ +typedef struct { + const uint8_t items_num_s; /*!< Number of metadata items for SIG models in the element */ + const uint8_t items_num_v; /*!< Number of metadata items for Vendor models in the element */ + + esp_ble_mesh_metadata_item_t *const metadata_items_s; /*!< List of metadata items for SIG models in the element */ + esp_ble_mesh_metadata_item_t *const metadata_items_v; /*!< List of metadata items for Vendor models in the element */ +} esp_ble_mesh_metadata_elem_t; /*!< Format of Metadata element of Models Metadata Page 0/128 */ + +/** Format of the Models Metadata Page 0/128 */ +typedef struct { + size_t element_count; /*!< Element count */ + esp_ble_mesh_metadata_elem_t *elements; /*!< List of metadata for models for each element */ +} esp_ble_mesh_models_metadata_t; /*!< Format of the Models Metadata Page 0/128 */ + +/** + * @brief Register Composition Data Page 1. + * + * @param[in] comp: Pointer to Composition Data Page 1. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_comp_1_register(const esp_ble_mesh_comp_1_t *comp); + +/** + * @brief Register Models Metadata Page 0 or 128. + * + * @param[in] metadata: Pointer to Models Metadata Page 0 or 128. + * @param[in] metadata_page: Models Metadata Page number, i.e. 0 or 128. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_models_metadata_register(const esp_ble_mesh_models_metadata_t *metadata, + uint8_t metadata_page); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_CM_DATA_API_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_df_model_api.h b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_df_model_api.h new file mode 100644 index 000000000000..f03b0a00ce3f --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_df_model_api.h @@ -0,0 +1,746 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ESP_BLE_MESH_DF_MODEL_API_H_ +#define _ESP_BLE_MESH_DF_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x7B) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x7C) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x7D) +#define ESP_BLE_MESH_MODEL_OP_PATH_METRIC_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x7E) +#define ESP_BLE_MESH_MODEL_OP_PATH_METRIC_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x7F) +#define ESP_BLE_MESH_MODEL_OP_PATH_METRIC_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x80) +#define ESP_BLE_MESH_MODEL_OP_DISCOVERY_TABLE_CAPS_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x81) +#define ESP_BLE_MESH_MODEL_OP_DISCOVERY_TABLE_CAPS_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x82) +#define ESP_BLE_MESH_MODEL_OP_DISCOVERY_TABLE_CAPS_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x83) +#define ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ADD ESP_BLE_MESH_MODEL_OP_2(0x80, 0x84) +#define ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEL ESP_BLE_MESH_MODEL_OP_2(0x80, 0x85) +#define ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x86) +#define ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_ADD ESP_BLE_MESH_MODEL_OP_2(0x80, 0x87) +#define ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_DEL ESP_BLE_MESH_MODEL_OP_2(0x80, 0x88) +#define ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x89) +#define ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x8A) +#define ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_GET_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x8B) +#define ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_CNT_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x8C) +#define ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_CNT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x8D) +#define ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x8E) +#define ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x8F) +#define ESP_BLE_MESH_MODEL_OP_WANTED_LANES_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x90) +#define ESP_BLE_MESH_MODEL_OP_WANTED_LANES_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x91) +#define ESP_BLE_MESH_MODEL_OP_WANTED_LANES_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x92) +#define ESP_BLE_MESH_MODEL_OP_TWO_WAY_PATH_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x93) +#define ESP_BLE_MESH_MODEL_OP_TWO_WAY_PATH_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x94) +#define ESP_BLE_MESH_MODEL_OP_TWO_WAY_PATH_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x95) +#define ESP_BLE_MESH_MODEL_OP_PATH_ECHO_INTERVAL_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x96) +#define ESP_BLE_MESH_MODEL_OP_PATH_ECHO_INTERVAL_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x97) +#define ESP_BLE_MESH_MODEL_OP_PATH_ECHO_INTERVAL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x98) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_NET_TRANSMIT_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x99) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_NET_TRANSMIT_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x9A) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_NET_TRANSMIT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x9B) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_RELAY_RETRANSMIT_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x9C) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_RELAY_RETRANSMIT_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x9D) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_RELAY_RETRANSMIT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x9E) +#define ESP_BLE_MESH_MODEL_OP_RSSI_THRESHOLD_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x9F) +#define ESP_BLE_MESH_MODEL_OP_RSSI_THRESHOLD_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xA0) +#define ESP_BLE_MESH_MODEL_OP_RSSI_THRESHOLD_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0xA1) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_PATHS_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xA2) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_PATHS_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0xA3) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_PUB_POLICY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xA4) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_PUB_POLICY_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xA5) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_PUB_POLICY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0xA6) +#define ESP_BLE_MESH_MODEL_OP_PATH_DISCOVERY_TIMING_CTL_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xA7) +#define ESP_BLE_MESH_MODEL_OP_PATH_DISCOVERY_TIMING_CTL_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xA8) +#define ESP_BLE_MESH_MODEL_OP_PATH_DISCOVERY_TIMING_CTL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0xA9) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_NET_TRANSMIT_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xAB) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_NET_TRANSMIT_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xAC) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_NET_TRANSMIT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0xAD) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_RELAY_RETRANSMIT_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xAE) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_RELAY_RETRANSMIT_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0xAF) +#define ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_RELAY_RETRANSMIT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0xB0) + +#define ESP_BLE_MESH_PATH_DISC_INTERVAL_5_SEC 0 +#define ESP_BLE_MESH_PATH_DISC_INTERVAL_30_SEC 1 + +#define ESP_BLE_MESH_LANE_DISC_GUARD_INTERVAL_2_SEC 0 +#define ESP_BLE_MESH_LANE_DISC_GUARD_INTERVAL_10_SEC 1 + +#define ESP_BLE_MESH_DIRECTED_PUB_POLICY_MANAGED_FLOODING 0x00 +#define ESP_BLE_MESH_DIRECTED_PUB_POLICY_DIRECTED_FORWARDING 0x01 + +#define ESP_BLE_MESH_GET_FILTER_MASK(fp, nfp, pom, dm) \ + (((dm) << 3) | ((pom) << 2) | ((nfp) << 1) | (fp)) + +/** @def ESP_BLE_MESH_MODEL_DF_SRV + * + * @brief Define a new Directed Forwarding Configuration Server model. + * + * @note If supported, the model shall be supported by a primary element + * and shall not be supported by any secondary elements. + * + * @param srv_data Pointer to a unique Directed Forwarding Configuration + * Server model user_data. + * + * @return New Directed Forwarding Configuration Server model instance. + */ +#define ESP_BLE_MESH_MODEL_DF_SRV(srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_DF_SRV, \ + NULL, NULL, srv_data) + +/** @def ESP_BLE_MESH_MODEL_DF_CLI + * + * @brief Define a new Directed Forwarding Configuration Client model. + * + * @note If supported, the model shall be supported by a primary element + * and shall not be supported by any secondary elements. + * + * @param cli_data Pointer to a unique Directed Forwarding Configuration + * Client model user_data. + * + * @return New Directed Forwarding Configuration Client model instance. + */ +#define ESP_BLE_MESH_MODEL_DF_CLI(cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_DF_CLI, \ + NULL, NULL, cli_data) + +/** Directed Forwarding Configuration Server model context */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to Directed Forwarding Configuration Server model */ + + uint8_t directed_net_transmit; /*!< Directed Network Transmit state */ + uint8_t directed_relay_retransmit; /*!< Directed Relay Retransmit state */ + + int8_t default_rssi_threshold; /*!< Default RSSI Threshold state */ + uint8_t rssi_margin; /*!< RSSI Margin state */ + + uint16_t directed_node_paths; /*!< Directed Node Paths state */ + uint16_t directed_relay_paths; /*!< Directed Relay Paths state */ + uint16_t directed_proxy_paths; /*!< Directed Proxy Paths state */ + uint16_t directed_friend_paths; /*!< Directed Friend Paths state */ + + uint16_t path_monitor_interval; /*!< Path Monitoring Interval state */ + uint16_t path_disc_retry_interval; /*!< Path Discovery Retry Interval state */ + uint8_t path_disc_interval:1, /*!< Path Discovery Interval state */ + lane_disc_guard_interval:1; /*!< Lane Discovery Guard Interval state */ + + uint8_t directed_ctl_net_transmit; /*!< Directed Control Network Transmit state */ + uint8_t directed_ctl_relay_retransmit; /*!< Directed Control Relay Retransmit state */ +} esp_ble_mesh_df_srv_t; + +/** Parameters of Directed Control Get. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ +} esp_ble_mesh_directed_control_get_t; + +/** Parameters of Directed Control Set. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint8_t directed_forwarding; /*!< New Directed Forwarding state */ + uint8_t directed_relay; /*!< New Directed Relay state */ + uint8_t directed_proxy; /*!< New Directed Proxy state */ + uint8_t directed_proxy_use_default; /*!< New Directed Proxy Use Directed Default state or value to ignore */ + uint8_t directed_friend; /*!< New Directed Friend state or value to ignore */ +} esp_ble_mesh_directed_control_set_t; + +/** Parameters of Path Metric Get. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ +} esp_ble_mesh_path_metric_get_t; + +/** Parameters of Path Metric Set. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint8_t path_metric_type:3, /*!< New Path Metric Type state */ + path_lifetime:2; /*!< New Path Lifetime state */ +} esp_ble_mesh_path_metric_set_t; + +/** Parameters of Discovery Table Capabilities Get. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ +} esp_ble_mesh_discovery_table_caps_get_t; + +/** Parameters of Discovery Table Capabilities Set. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint8_t max_concurr_init; /*!< New Max Concurrent Init state */ +} esp_ble_mesh_discovery_table_caps_set_t; + +/** Parameters of Forwarding Table Add. */ +typedef struct { + uint16_t net_idx:12, /*!< NetKey Index */ + unicast_dst:1, /*!< Indicates whether or not the destination of the path is a unicast address */ + bw_path_validated:1; /*!< Indicates whether or not the backward path has been validated */ + esp_ble_mesh_uar_t path_origin; /*!< Unicast address range of the Path Origin */ + /** Path target address */ + union { + esp_ble_mesh_uar_t path_target; /*!< Unicast address range of the Path Target */ + uint16_t multicast_dst; /*!< Multicast destination address */ + }; + uint16_t bearer_twd_path_origin; /*!< Index of the bearer toward the Path Origin */ + uint16_t bearer_twd_path_target; /*!< Index of the bearer toward the Path Target */ +} esp_ble_mesh_forwarding_table_add_t; /*!< Parameters of Forwarding Table Add */ + +/** Parameters of Forwarding Table Delete. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint16_t path_origin; /*!< Primary element address of the Path Origin */ + uint16_t dst; /*!< Destination address */ +} esp_ble_mesh_forwarding_table_delete_t; + +/** Parameters of Forwarding Table Dependents Add. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint16_t path_origin; /*!< Primary element address of the Path Origin */ + uint16_t dst; /*!< Destination address */ + uint8_t dep_origin_uar_list_size; /*!< Number of entries in the Dependent_Origin_Unicast_Addr_Range_List field */ + uint8_t dep_target_uar_list_size; /*!< Number of entries in the Dependent_Target_Unicast_Addr_Range_List field */ + esp_ble_mesh_uar_t *dep_origin_uar_list; /*!< List of the unicast address ranges of the dependent nodes of the Path Origin */ + esp_ble_mesh_uar_t *dep_target_uar_list; /*!< List of the unicast address ranges of the dependent nodes of the Path Target */ +} esp_ble_mesh_forwarding_table_deps_add_t; + +/** Parameters of Forwarding Table Dependents Delete. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint16_t path_origin; /*!< Primary element address of the Path Origin */ + uint16_t dst; /*!< Destination address */ + uint8_t dep_origin_list_size; /*!< Number of entries in the Dependent_Origin_List field */ + uint8_t dep_target_list_size; /*!< Number of entries in the Dependent_Target_List field */ + uint16_t *dep_origin_list; /*!< List of the primary element addresses of the dependent nodes of the Path Origin */ + uint16_t *dep_target_list; /*!< List of the primary element addresses of the dependent nodes of the Path Target */ +} esp_ble_mesh_forwarding_table_deps_delete_t; + +/** Parameters of Forwarding Table Entries Count Get. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ +} esp_ble_mesh_forwarding_table_entries_cnt_get_t; + +/** Parameters of Forwarding Table Entries Get. */ +typedef struct { + uint16_t net_idx:12, /*!< NetKey Index */ + filter_mask:4; /*!< Filter to be applied to the Forwarding Table entries */ + uint16_t start_index; /*!< Start offset to read in units of Forwarding Table entries */ + uint16_t path_origin; /*!< Primary element address of the Path Origin */ + uint16_t dst; /*!< Destination address */ + + bool include_id; /*!< Indicate whether or not the Forwarding Table Update Identifier is present */ + uint16_t update_id; /*!< Last saved Forwarding Table Update Identifier (Optional) */ +} esp_ble_mesh_forwarding_table_entries_get_t; + +/** Parameters of Forwarding Table Dependents Get. */ +typedef struct { + uint16_t net_idx:12, /*!< NetKey Index */ + dep_list_mask:2, /*!< Filter applied to the lists of unicast address ranges for dependent nodes */ + fixed_path_flag:1; /*!< Indicate whether or not to return the unicast address ranges of dependent nodes in a fixed path entry */ + uint16_t start_index; /*!< Start offset in units of unicast address ranges */ + uint16_t path_origin; /*!< Primary element address of the Path Origin */ + uint16_t dst; /*!< Destination address */ + + bool include_id; /*!< Indicate whether or not the Forwarding Table Update Identifier is present */ + uint16_t update_id; /*!< Last saved Forwarding Table Update Identifier (Optional) */ +} esp_ble_mesh_forwarding_table_deps_get_t; + +/** Parameters of Wanted Lanes Get. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ +} esp_ble_mesh_wanted_lanes_get_t; + +/** Parameters of Wanted Lanes Set. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint8_t wanted_lanes; /*!< New Wanted Lanes state */ +} esp_ble_mesh_wanted_lanes_set_t; + +/** Parameters of Two Way Path Get. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ +} esp_ble_mesh_two_way_path_get_t; + +/** Parameters of Two Way Path Set. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint8_t two_way_path:1; /*!< Two way path flag */ +} esp_ble_mesh_two_way_path_set_t; + +/** Parameters of Path Echo Interval Get. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ +} esp_ble_mesh_path_echo_interval_get_t; + +/** Parameters of Path Echo Interval Set. */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint8_t unicast_echo_interval; /*!< New Unicast Echo Interval state or indication of no state change */ + uint8_t multicast_echo_interval; /*!< New Multicast Echo Interval state or indication of no state change */ +} esp_ble_mesh_path_echo_interval_set_t; + +/** Parameters of Directed Network Transmit Set. */ +typedef struct { + uint8_t net_transmit; /*!< New Directed Network Transmit state */ +} esp_ble_mesh_directed_net_transmit_set_t; + +/** Parameters of Directed Relay Retransmit Set. */ +typedef struct { + uint8_t relay_retransmit; /*!< New Directed Relay Retransmit state */ +} esp_ble_mesh_directed_relay_retransmit_set_t; + +/** Parameters of RSSI Threshold Set. */ +typedef struct { + uint8_t rssi_margin; /*!< New RSSI Margin state */ +} esp_ble_mesh_rssi_threshold_set_t; + +/** Parameters of Directed Publish Policy Get. */ +typedef struct { + uint16_t element_addr; /*!< Address of the element */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ +} esp_ble_mesh_directed_publish_policy_get_t; + +/** Parameters of Directed Publish Policy Set. */ +typedef struct { + uint8_t direct_pub_policy; /*!< New Directed Publish Policy state */ + uint16_t element_addr; /*!< Address of the element */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ +} esp_ble_mesh_directed_publish_policy_set_t; + +/** Parameters of Path Discovery Timing Control Set. */ +typedef struct { + uint16_t path_monitor_interval; /*!< New Path Monitoring Interval state */ + uint16_t path_disc_retry_interval; /*!< New Path Discovery Retry Interval state */ + uint8_t path_disc_interval:1, /*!< New Path Discovery Interval state */ + lane_disc_guard_interval:1; /*!< New Lane Discovery Guard Interval state */ +} esp_ble_mesh_path_discovery_timing_ctl_set_t; + +/** Parameters of Directed Control Network Transmit Set. */ +typedef struct { + uint8_t net_transmit; /*!< New Directed Control Network Transmit Count state */ +} esp_ble_mesh_directed_ctl_net_transmit_set_t; + +/** Parameters of Directed Control Relay Retransmit Set. */ +typedef struct { + uint8_t relay_retransmit; /*!< New Directed Control Relay Retransmit Count state */ +} esp_ble_mesh_directed_ctl_relay_retransmit_set_t; + +/** + * @brief Directed Forwarding Configuration Client model get message union + */ +typedef union { + esp_ble_mesh_directed_control_get_t directed_control_get; /*!< For ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_GET */ + esp_ble_mesh_path_metric_get_t path_metric_get; /*!< For ESP_BLE_MESH_MODEL_OP_PATH_METRIC_GET */ + esp_ble_mesh_discovery_table_caps_get_t disc_table_caps_get; /*!< For ESP_BLE_MESH_MODEL_OP_DISCOVERY_TABLE_CAPS_GET */ + esp_ble_mesh_forwarding_table_entries_cnt_get_t forwarding_table_entries_cnt_get; /*!< For ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_CNT_GET */ + esp_ble_mesh_forwarding_table_entries_get_t forwarding_table_entries_get; /*!< For ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_GET */ + esp_ble_mesh_forwarding_table_deps_get_t forwarding_table_deps_get; /*!< For ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_GET */ + esp_ble_mesh_wanted_lanes_get_t wanted_lanes_get; /*!< For ESP_BLE_MESH_MODEL_OP_WANTED_LANES_GET */ + esp_ble_mesh_two_way_path_get_t two_way_path_get; /*!< For ESP_BLE_MESH_MODEL_OP_TWO_WAY_PATH_GET */ + esp_ble_mesh_path_echo_interval_get_t path_echo_interval_get; /*!< For ESP_BLE_MESH_MODEL_OP_PATH_ECHO_INTERVAL_GET */ + esp_ble_mesh_directed_publish_policy_get_t directed_pub_policy_get; /*!< For ESP_BLE_MESH_MODEL_OP_DIRECTED_PUB_POLICY_GET */ +} esp_ble_mesh_df_client_get_t; + +/** + * @brief Directed Forwarding Configuration Client model set message union + */ +typedef union { + esp_ble_mesh_directed_control_set_t directed_control_set; /*!< For ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_SET */ + esp_ble_mesh_path_metric_set_t path_metric_set; /*!< For ESP_BLE_MESH_MODEL_OP_PATH_METRIC_SET */ + esp_ble_mesh_discovery_table_caps_set_t disc_table_caps_set; /*!< For ESP_BLE_MESH_MODEL_OP_DISCOVERY_TABLE_CAPS_SET */ + esp_ble_mesh_forwarding_table_add_t forwarding_table_add; /*!< For ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ADD */ + esp_ble_mesh_forwarding_table_delete_t forwarding_table_del; /*!< For ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEL */ + esp_ble_mesh_forwarding_table_deps_add_t forwarding_table_deps_add; /*!< For ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_ADD */ + esp_ble_mesh_forwarding_table_deps_delete_t forwarding_table_deps_del; /*!< For ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_DEL */ + esp_ble_mesh_wanted_lanes_set_t wanted_lanes_set; /*!< For ESP_BLE_MESH_MODEL_OP_WANTED_LANES_SET */ + esp_ble_mesh_two_way_path_set_t two_way_path_set; /*!< For ESP_BLE_MESH_MODEL_OP_TWO_WAY_PATH_SET */ + esp_ble_mesh_path_echo_interval_set_t path_echo_interval_set; /*!< For ESP_BLE_MESH_MODEL_OP_PATH_ECHO_INTERVAL_SET */ + esp_ble_mesh_directed_net_transmit_set_t directed_net_transmit_set; /*!< For ESP_BLE_MESH_MODEL_OP_DIRECTED_NET_TRANSMIT_SET */ + esp_ble_mesh_directed_relay_retransmit_set_t directed_relay_retransmit_set; /*!< For ESP_BLE_MESH_MODEL_OP_DIRECTED_RELAY_RETRANSMIT_SET */ + esp_ble_mesh_rssi_threshold_set_t rssi_threshold_set; /*!< For ESP_BLE_MESH_MODEL_OP_RSSI_THRESHOLD_SET */ + esp_ble_mesh_directed_publish_policy_set_t directed_pub_policy_set; /*!< For ESP_BLE_MESH_MODEL_OP_DIRECTED_PUB_POLICY_SET */ + esp_ble_mesh_path_discovery_timing_ctl_set_t path_disc_timing_ctl_set; /*!< For ESP_BLE_MESH_MODEL_OP_PATH_DISCOVERY_TIMING_CTL_SET */ + esp_ble_mesh_directed_ctl_net_transmit_set_t directed_ctl_net_transmit_set; /*!< For ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_NET_TRANSMIT_SET */ + esp_ble_mesh_directed_ctl_relay_retransmit_set_t directed_ctl_relay_retransmit_set; /*!< For ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_RELAY_RETRANSMIT_SET */ +} esp_ble_mesh_df_client_set_t; + +/** Parameters of Directed Control Status. */ +typedef struct { + uint8_t status; /*!< Status code for the requesting message */ + uint16_t net_idx; /*!< NetKey Index */ + uint8_t directed_forwarding; /*!< Current Directed Forwarding state */ + uint8_t directed_relay; /*!< Current Directed Relay state */ + uint8_t directed_proxy; /*!< Current Directed Proxy state */ + uint8_t directed_proxy_use_default; /*!< Current Directed Proxy Use Directed Default state or 0xFF */ + uint8_t directed_friend; /*!< Current Directed Friend state */ +} esp_ble_mesh_directed_control_status_t; + +/** Parameters of Path Metric Status. */ +typedef struct { + uint8_t status; /*!< Status code for the requesting message */ + uint16_t net_idx; /*!< NetKey Index */ + uint8_t path_metric_type:3, /*!< Current Path Metric Type state */ + path_lifetime:2; /*!< Current Path Lifetime state */ +} esp_ble_mesh_path_metric_status_t; + +/** Parameters of Discovery Table Capabilities Status. */ +typedef struct { + uint8_t status; /*!< Status code for the requesting message */ + uint16_t net_idx; /*!< NetKey Index */ + uint8_t max_concurr_init; /*!< Current Max Concurrent Init state */ + uint8_t max_disc_entries; /*!< Max Discovery Table Entries Count state */ +} esp_ble_mesh_discovery_table_caps_status_t; + +/** Parameters of Forwarding Table Status. */ +typedef struct { + uint8_t status; /*!< Status code for the requesting message */ + uint16_t net_idx; /*!< NetKey Index */ + uint16_t path_origin; /*!< Primary element address of the Path Origin */ + uint16_t dst; /*!< Destination address */ +} esp_ble_mesh_forwarding_table_status_t; + +/** Parameters of Forwarding Table Dependent Status. */ +typedef struct { + uint8_t status; /*!< Status code for the requesting message */ + uint16_t net_idx; /*!< NetKey Index */ + uint16_t path_origin; /*!< Primary element address of the Path Origin */ + uint16_t dst; /*!< Destination address */ +} esp_ble_mesh_forwarding_table_deps_status_t; + +/** Parameters of Forwarding Table Entries Count Status. */ +typedef struct { + uint8_t status; /*!< Status code for the requesting message */ + uint16_t net_idx; /*!< NetKey Index */ + uint16_t update_id; /*!< Current Forwarding Table Update Identifier state */ + uint16_t fixed_entry_cnt; /*!< Number of fixed path entries in the Forwarding Table */ + uint16_t non_fixed_entry_cnt; /*!< Number of non-fixed path entries in the Forwarding Table */ +} esp_ble_mesh_forwarding_table_entries_cnt_status_t; + +/** Parameters of Forwarding Table Entry. */ +typedef struct { + uint16_t fixed_path_flag:1, /*!< Indicates whether the table entry is a fixed path entry or a non-fixed path entry */ + unicast_dst_flag:1, /*!< Indicates whether or not the destination of the path is a unicast address */ + bw_path_validated_flag:1, /*!< Indicates whether or not the backward path has been validated */ + bearer_twd_path_origin_ind:1, /*!< Indicates the presence or absence of the Bearer_Toward_Path_Origin field */ + bearer_twd_path_target_ind:1, /*!< Indicates the presence or absence of the Bearer_Toward_Path_Target field */ + dep_origin_list_size_ind:2, /*!< Indicates the size of the Dependent_Origin_List field */ + dep_target_list_size_ind:2; /*!< Indicates the size of the Dependent_Target_List field */ + + /* Only for non-fixed path entry */ + uint8_t lane_counter; /*!< Number of lanes in the path */ + uint16_t path_remaining_time; /*!< Path lifetime remaining */ + uint8_t path_origin_forward_number; /*!< Forwarding number of the Path Origin */ + + /* For fixed path entry and non-fixed path entry */ + esp_ble_mesh_uar_t path_origin; /*!< Path Origin unicast address range */ + uint16_t dep_origin_list_size; /*!< Current number of entries in the list of dependent nodes of the Path Origin */ + uint16_t bearer_twd_path_origin; /*!< Index of the bearer toward the Path Origin */ + esp_ble_mesh_uar_t path_target; /*!< Path Target unicast address range */ + uint16_t multicast_dst; /*!< Multicast destination address */ + uint16_t dep_target_list_size; /*!< Current number of entries in the list of dependent nodes of the Path Target */ + uint16_t bearer_twd_path_target; /*!< Index of the bearer toward the Path Target */ +} esp_ble_mesh_forwarding_table_entry_t; + +/** Parameters of Forwarding Table Entries Status. */ +typedef struct { + uint8_t status; /*!< Status code for the requesting message */ + uint16_t net_idx:12, /*!< NetKey Index */ + filter_mask:4; /*!< Filter applied to the Forwarding Table entries */ + uint16_t start_index; /*!< Start offset in units of Forwarding Table entries */ + uint16_t path_origin; /*!< Primary element address of the Path Origin */ + uint16_t dst; /*!< Destination address */ + + bool include_id; /*!< Indicate whether or not the Forwarding Table Update Identifier is present */ + uint16_t update_id; /*!< Current Forwarding Table Update Identifier state */ + + uint8_t entry_list_size; /*!< Current number of entries in the list of Forwarding Table entries */ + esp_ble_mesh_forwarding_table_entry_t *entry_list; /*!< List of Forwarding Table entries */ +} esp_ble_mesh_forwarding_table_entries_status_t; + +/** Parameters of Forwarding Table Dependents Get Status. */ +typedef struct { + uint8_t status; /*!< Status code for the requesting message */ + uint16_t net_idx:12, /*!< NetKey Index */ + dep_list_mask:2, /*!< Filter applied to the lists of unicast address ranges for dependent nodes */ + fixed_path_flag:1; /*!< Flag indicating whether or not to return the unicast address ranges of dependent nodes in a fixed path entry */ + uint16_t start_index; /*!< Start offset in units of unicast address ranges */ + uint16_t path_origin; /*!< Primary element address of the Path Origin */ + uint16_t dst; /*!< Destination address */ + + bool include_id; /*!< Indicate whether or not the Forwarding Table Update Identifier is present */ + uint16_t update_id; /*!< Current Forwarding Table Update Identifier state */ + + uint8_t dep_origin_uar_list_size; /*!< Number of unicast address ranges in the Dependent_Origin_Unicast_Addr_Range_List field */ + uint8_t dep_target_uar_list_size; /*!< Number of unicast address ranges in the Dependent_Target_Unicast_Addr_Range_List field */ + esp_ble_mesh_uar_t *dep_origin_uar_list; /*!< List of unicast address ranges of dependent nodes of the Path Origin */ + esp_ble_mesh_uar_t *dep_target_uar_list; /*!< List of unicast address ranges of dependent nodes of the Path Target */ +} esp_ble_mesh_forwarding_table_deps_get_status_t; + +/** Parameters of Wanted Lanes Status. */ +typedef struct { + uint8_t status; /*!< Status code for the requesting message */ + uint16_t net_idx; /*!< NetKey Index */ + uint8_t wanted_lanes; /*!< Current Wanted Lanes state */ +} esp_ble_mesh_wanted_lanes_status_t; + +/** Parameters of Two Way Path Status. */ +typedef struct { + uint8_t status; /*!< Status code for the requesting message */ + uint16_t net_idx; /*!< NetKey Index */ + uint8_t two_way_path; /*!< Current Two Way Path state */ +} esp_ble_mesh_two_way_path_status_t; + +/** Parameters of Path Echo Interval Status. */ +typedef struct { + uint8_t status; /*!< Status code for the requesting message */ + uint16_t net_idx; /*!< NetKey Index */ + uint8_t unicast_echo_interval; /*!< Current Unicast Echo Interval state */ + uint8_t multicast_echo_interval; /*!< Current Multicast Echo Interval state */ +} esp_ble_mesh_path_echo_interval_status_t; + +/** Parameters of Directed Network Transmit Status. */ +typedef struct { + uint8_t net_transmit; /*!< Current Directed Network Transmit state */ +} esp_ble_mesh_directed_net_transmit_status_t; + +/** Parameters of Directed Relay Retransmit Status. */ +typedef struct { + uint8_t relay_retransmit; /*!< Current Directed Relay Retransmit state */ +} esp_ble_mesh_directed_relay_retransmit_status_t; + +/** Parameters of RSSI Threshold Status. */ +typedef struct { + uint8_t default_rssi_threshold; /*!< Default RSSI Threshold state */ + uint8_t rssi_margin; /*!< Current RSSI Margin state */ +} esp_ble_mesh_rssi_threshold_status_t; + +/** Parameters of Directed Paths Status. */ +typedef struct { + uint16_t directed_node_paths; /*!< Directed Node Paths state */ + uint16_t directed_relay_paths; /*!< Directed Relay Paths state */ + uint16_t directed_proxy_paths; /*!< Directed Proxy Paths state */ + uint16_t directed_friend_paths; /*!< Directed Friend Paths state */ +} esp_ble_mesh_directed_paths_status_t; + +/** Parameters of Directed Publish Policy Status. */ +typedef struct { + uint8_t status; /*!< Status code for the requesting message */ + uint8_t directed_pub_policy; /*!< Current Directed Publish Policy state */ + uint16_t element_addr; /*!< Address of the element */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ +} esp_ble_mesh_directed_pub_policy_status_t; + +/** Parameters of Path Discovery Timing Control Status. */ +typedef struct { + uint16_t path_monitor_interval; /*!< Current Path Monitoring Interval state */ + uint16_t path_disc_retry_interval; /*!< Current Path Discovery Retry Interval state */ + uint8_t path_disc_interval:1, /*!< Current Path Discovery Interval state */ + lane_disc_guard_interval:1; /*!< Current Lane Discovery Guard Interval state */ +} esp_ble_mesh_path_disc_timing_ctl_status_cb_t; + +/** Parameters of Directed Control Network Transmit Status. */ +typedef struct { + uint8_t net_transmit; /*!< Current Directed Control Network Transmit state */ +} esp_ble_mesh_directed_ctl_net_transmit_status_t; + +/** Parameters of Directed Control Relay Retransmit Status. */ +typedef struct { + uint8_t relay_retransmit; /*!< Current Directed Control Relay Retransmit state */ +} esp_ble_mesh_directed_ctl_relay_retransmit_status_t; + +/** Result of sending Directed Forwarding Configuration Client messages */ +typedef struct { + int err_code; /*!< Result of sending a message */ +} esp_ble_mesh_df_client_send_cb_t; + +/** + * @brief Directed Forwarding Configuration Client model received message union + */ +typedef union { + esp_ble_mesh_directed_control_status_t directed_control_status; /*!< ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_STATUS */ + esp_ble_mesh_path_metric_status_t path_metric_status; /*!< ESP_BLE_MESH_MODEL_OP_PATH_METRIC_STATUS */ + esp_ble_mesh_discovery_table_caps_status_t disc_table_caps_status; /*!< ESP_BLE_MESH_MODEL_OP_DISCOVERY_TABLE_CAPS_STATUS */ + esp_ble_mesh_forwarding_table_status_t forwarding_table_status; /*!< ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_STATUS */ + esp_ble_mesh_forwarding_table_deps_status_t forwarding_table_deps_status; /*!< ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_STATUS */ + esp_ble_mesh_forwarding_table_entries_cnt_status_t forwarding_table_entries_cnt_status; /*!< ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_CNT_STATUS */ + esp_ble_mesh_forwarding_table_entries_status_t forwarding_table_entries_status; /*!< ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_STATUS */ + esp_ble_mesh_forwarding_table_deps_get_status_t forwarding_table_deps_get_status; /*!< ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_GET_STATUS */ + esp_ble_mesh_wanted_lanes_status_t wanted_lanes_status; /*!< ESP_BLE_MESH_MODEL_OP_WANTED_LANES_STATUS */ + esp_ble_mesh_two_way_path_status_t two_way_path_status; /*!< ESP_BLE_MESH_MODEL_OP_TWO_WAY_PATH_STATUS */ + esp_ble_mesh_path_echo_interval_status_t path_echo_interval_status; /*!< ESP_BLE_MESH_MODEL_OP_PATH_ECHO_INTERVAL_STATUS */ + esp_ble_mesh_directed_net_transmit_status_t directed_net_transmit_status; /*!< ESP_BLE_MESH_MODEL_OP_DIRECTED_NET_TRANSMIT_STATUS */ + esp_ble_mesh_directed_relay_retransmit_status_t directed_relay_retransmit_status; /*!< ESP_BLE_MESH_MODEL_OP_DIRECTED_RELAY_RETRANSMIT_STATUS */ + esp_ble_mesh_rssi_threshold_status_t rssi_threshold_status; /*!< ESP_BLE_MESH_MODEL_OP_RSSI_THRESHOLD_STATUS */ + esp_ble_mesh_directed_paths_status_t directed_paths_status; /*!< ESP_BLE_MESH_MODEL_OP_DIRECTED_PATHS_STATUS */ + esp_ble_mesh_directed_pub_policy_status_t directed_pub_policy_status; /*!< ESP_BLE_MESH_MODEL_OP_DIRECTED_PUB_POLICY_STATUS */ + esp_ble_mesh_path_disc_timing_ctl_status_cb_t path_disc_timing_ctl_status; /*!< ESP_BLE_MESH_MODEL_OP_PATH_DISCOVERY_TIMING_CTL_STATUS */ + esp_ble_mesh_directed_ctl_net_transmit_status_t directed_ctl_net_transmit_status; /*!< ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_NET_TRANSMIT_STATUS */ + esp_ble_mesh_directed_ctl_relay_retransmit_status_t directed_ctl_relay_retransmit_status; /*!< ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_RELAY_RETRANSMIT_STATUS */ +} esp_ble_mesh_df_client_recv_cb_t; + +/** Directed Forwarding Configuration Client model callback parameters */ +typedef struct { + esp_ble_mesh_client_common_param_t *params; /*!< Client common parameters, used by all events. */ + /** Union of DF Client callback */ + union { + esp_ble_mesh_df_client_send_cb_t send; /*!< Result of sending a message */ + esp_ble_mesh_df_client_recv_cb_t recv; /*!< Parameters of received status message */ + }; +} esp_ble_mesh_df_client_cb_param_t; + +/** This enum value is the event of Directed Forwarding Configuration Client model */ +typedef enum { + ESP_BLE_MESH_DF_CLIENT_SEND_COMP_EVT, + ESP_BLE_MESH_DF_CLIENT_SEND_TIMEOUT_EVT, + ESP_BLE_MESH_DF_CLIENT_RECV_GET_RSP_EVT, + ESP_BLE_MESH_DF_CLIENT_RECV_SET_RSP_EVT, + ESP_BLE_MESH_DF_CLIENT_RECV_PUB_EVT, + ESP_BLE_MESH_DF_CLIENT_EVT_MAX, +} esp_ble_mesh_df_client_cb_event_t; + +/** + * @brief Directed Forwarding Configuration Server model related context. + */ + +/** + * @brief Directed Forwarding Configuration Server model state change value union + */ +typedef union { + uint8_t dummy; /*!< Event not used currently */ +} esp_ble_mesh_df_server_state_change_t; + +typedef enum { + ESP_BLE_MESH_DF_TABLE_ACT_EMPTY, + ESP_BLE_MESH_DF_TABLE_ADD, + ESP_BLE_MESH_DF_TABLE_REMOVE, + ESP_BLE_MESH_DF_TABLE_ENTRY_CHANGE, + ESP_BLE_MESH_DF_TABLE_ACT_MAX_LIMIT, +} esp_ble_mesh_df_table_action_t; + +/** Parameters of directed forwarding table entry change */ +typedef struct { + esp_ble_mesh_df_table_action_t action; /*!< Action of directed forarding table */ + /** Union of directed forwarding table information */ + union { + /** Structure of directed forwarding table add and remove */ + struct { + esp_ble_mesh_uar_t path_origin; /*!< Primary element address of the Path Origin */ + esp_ble_mesh_uar_t path_target; /*!< Primary element address of the Path Target */ + + esp_ble_mesh_uar_t *dep_origin_data; /*!< List of the primary element addresses of the dependent nodes of the Path Origin */ + uint32_t dep_origin_num; /*!< Number of entries in the Dependent_Origin_Listfield of the message */ + + esp_ble_mesh_uar_t *dep_target_data; /*!< List of the primary element addresses of the dependent nodes of the Path Target */ + uint32_t dep_target_num; /*!< Number of entries in the Dependent_Target_List field of the message */ + + uint8_t fixed_path:1, /*!< Indicates whether the table entry is a fixed path entry or a non-fixed path entry */ + bw_path_validate:1, /*!< Indicates whether or not the backward path has been validated */ + path_not_ready:1; /*!< Flag indicating whether or not the path is ready for use */ + + uint8_t forward_number; /*!< Forwarding number of the Path Origin; If the entry is associated with a fixed path, the value is 0 */ + + uint8_t lane_counter; /*!< Number of lanes discovered; if the entry is associated with a fixed path, the value is 1.*/ + } df_table_entry_add_remove; /*!< Structure of directed forwarding table add and remove */ + /** Structure of directed forwarding table entry change */ + struct { + uint8_t dummy; /*!< Event not used currently */ + } df_table_entry_change; /*!< Directed forwarding table entry change */ + } df_table_info; /*!< Directed forwarding table information */ +} esp_ble_mesh_df_server_table_change_t; /*!< Structure of directed forwarding table entry change */ + +/** + * @brief Directed Forwarding Configuration Server model callback value union + */ +typedef union { + esp_ble_mesh_df_server_state_change_t state_change; /*!< For ESP_BLE_MESH_DF_SERVER_STATE_CHANGE_EVT */ + esp_ble_mesh_df_server_table_change_t table_change; /*!< For ESP_BLE_MESH_DF_SERVER_TABLE_CHANGE_EVT */ +} esp_ble_mesh_df_server_cb_value_t; + +/** Directed Forwarding Configuration Server model callback parameters */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + esp_ble_mesh_msg_ctx_t ctx; /*!< Context of the received message */ + esp_ble_mesh_df_server_cb_value_t value; /*!< Value of the received configuration messages */ +} esp_ble_mesh_df_server_cb_param_t; + +/** This enum value is the event of Directed Forwarding Configuration Server model */ +typedef enum { + ESP_BLE_MESH_DF_SERVER_STATE_CHANGE_EVT, + ESP_BLE_MESH_DF_SERVER_TABLE_CHANGE_EVT, + ESP_BLE_MESH_DF_SERVER_EVT_MAX, +} esp_ble_mesh_df_server_cb_event_t; + +/** + * @brief Bluetooth Mesh Directed Forwarding Configuration client and server model functions. + */ + +/** + * @brief Directed Forwarding Configuration Client model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_df_client_cb_t)(esp_ble_mesh_df_client_cb_event_t event, + esp_ble_mesh_df_client_cb_param_t *param); + +/** + * @brief Directed Forwarding Configuration Server model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_df_server_cb_t)(esp_ble_mesh_df_server_cb_event_t event, + esp_ble_mesh_df_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh Directed Forwarding Configuration Client model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_df_client_callback(esp_ble_mesh_df_client_cb_t callback); + +/** + * @brief Register BLE Mesh Directed Forwarding Configuration Server model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_df_server_callback(esp_ble_mesh_df_server_cb_t callback); + +/** + * @brief Get the value of Directed Forwarding Configuration Server model state with the corresponding get message. + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] get: Pointer to a union, each kind of opcode corresponds to one structure inside. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_df_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_df_client_get_t *get); + +/** + * @brief Set the value of Directed Forwarding Configuration Server model state with the corresponding set message. + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] set: Pointer to a union, each kind of opcode corresponds to one structure inside. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_df_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_df_client_set_t *set); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_DF_MODEL_API_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_lcd_model_api.h b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_lcd_model_api.h new file mode 100644 index 000000000000..63d7c18e612c --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_lcd_model_api.h @@ -0,0 +1,211 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ESP_BLE_MESH_LCD_MODEL_API_H_ +#define _ESP_BLE_MESH_LCD_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ESP_BLE_MESH_MODEL_OP_LARGE_COMP_DATA_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x74) +#define ESP_BLE_MESH_MODEL_OP_LARGE_COMP_DATA_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x75) +#define ESP_BLE_MESH_MODEL_OP_MODELS_METADATA_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x76) +#define ESP_BLE_MESH_MODEL_OP_MODELS_METADATA_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x77) + +/** @def ESP_BLE_MESH_MODEL_LCD_SRV + * + * @brief Define a new Large Composition Data Server model. + * + * @note If supported, the model shall be supported by a primary element + * and shall not be supported by any secondary elements. + * + * @param srv_data Pointer to a unique Large Composition Data Server model user_data. + * + * @return New Large Composition Data Server model instance. + */ +#define ESP_BLE_MESH_MODEL_LCD_SRV(srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LCD_SRV, \ + NULL, NULL, srv_data) + +/** @def ESP_BLE_MESH_MODEL_LCD_CLI + * + * @brief Define a new Large Composition Data Client model. + * + * @note If supported, the model shall be supported by the primary element + * and shall not be supported by any secondary elements. + * + * @param cli_data Pointer to a unique Large Composition Data Client model user_data. + * + * @return New Large Composition Data Client model instance. + */ +#define ESP_BLE_MESH_MODEL_LCD_CLI(cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LCD_CLI, \ + NULL, NULL, cli_data) + +/** Large Composition Data Server model context */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to Large Composition Data Server model */ +} esp_ble_mesh_lcd_srv_t; + +/** Parameters of Large Composition Data Get */ +typedef struct { + uint8_t page; /*!< Page number of the Composition Data */ + uint16_t offset; /*!< Offset within the page */ +} esp_ble_mesh_large_comp_data_get_t; + +/** Parameters of Models Metadata Get */ +typedef struct { + uint8_t metadata_page; /*!< Page number of the Models Metadata */ + uint16_t offset; /*!< Offset within the page */ +} esp_ble_mesh_models_metadata_get_t; + +/** + * @brief Large Composition Data Client model message union + */ +typedef union { + esp_ble_mesh_large_comp_data_get_t large_comp_data_get; /*!< For ESP_BLE_MESH_MODEL_OP_LARGE_COMP_DATA_GET */ + esp_ble_mesh_models_metadata_get_t models_metadata_get; /*!< For ESP_BLE_MESH_MODEL_OP_MODELS_METADATA_GET */ +} esp_ble_mesh_lcd_client_msg_t; + +/** Parameters of Large Composition Data Status */ +typedef struct { + uint8_t page; /*!< Page number of the Composition Data */ + uint16_t offset; /*!< Offset within the page */ + uint16_t total_size; /*!< Total size of the page */ + struct net_buf_simple *data; /*!< Composition Data for the identified portion of the page */ +} esp_ble_mesh_large_comp_data_status_t; + +/** Parameters of Models Metadata Data Status */ +typedef struct { + uint8_t metadata_page; /*!< Page number of the Models Metadata */ + uint16_t offset; /*!< Offset within the page */ + uint16_t total_size; /*!< Total size of the page */ + struct net_buf_simple *data; /*!< Models Metadata for the identified portion of the page */ +} esp_ble_mesh_models_metadata_status_t; + +/** Result of sending Large Composition Data Client messages */ +typedef struct { + int err_code; /*!< Result of sending a message */ +} esp_ble_mesh_lcd_client_send_cb_t; + +/** + * @brief Large Composition Data Client model received message union + */ +typedef union { + esp_ble_mesh_large_comp_data_status_t large_comp_data_status; /*!< For ESP_BLE_MESH_MODEL_OP_LARGE_COMP_DATA_STATUS */ + esp_ble_mesh_models_metadata_status_t models_metadata_status; /*!< For ESP_BLE_MESH_MODEL_OP_MODELS_METADATA_STATUS */ +} esp_ble_mesh_lcd_client_recv_cb_t; + +/** Large Composition Data Client model callback parameters */ +typedef struct { + esp_ble_mesh_client_common_param_t *params; /*!< Client common parameters, used by all events. */ + /** Union of LCD Client callback */ + union { + esp_ble_mesh_lcd_client_send_cb_t send; /*!< Result of sending a message */ + esp_ble_mesh_lcd_client_recv_cb_t recv; /*!< Parameters of received status message */ + }; +} esp_ble_mesh_lcd_client_cb_param_t; /*!< Large Composition Data Client model callback parameters */ + +/** This enum value is the event of Large Composition Data Client model */ +typedef enum { + ESP_BLE_MESH_LCD_CLIENT_SEND_COMP_EVT, + ESP_BLE_MESH_LCD_CLIENT_SEND_TIMEOUT_EVT, + ESP_BLE_MESH_LCD_CLIENT_RECV_RSP_EVT, + ESP_BLE_MESH_LCD_CLIENT_RECV_PUB_EVT, + ESP_BLE_MESH_LCD_CLIENT_EVT_MAX, +} esp_ble_mesh_lcd_client_cb_event_t; + +/** + * @brief Large Composition Data Server model related context. + */ + +/** + * @brief Large Composition Data Server model state change value union + */ +typedef union { + uint8_t dummy; /*!< Event not used currently */ +} esp_ble_mesh_lcd_server_state_change_t; + +/** + * @brief Large Composition Data Server model callback value union + */ +typedef union { + esp_ble_mesh_lcd_server_state_change_t state_change; /*!< For ESP_BLE_MESH_LCD_SERVER_STATE_CHANGE_EVT */ +} esp_ble_mesh_lcd_server_cb_value_t; + +/** Large Composition Data Server model callback parameters */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + esp_ble_mesh_msg_ctx_t ctx; /*!< Context of the received message */ + esp_ble_mesh_lcd_server_cb_value_t value; /*!< Value of the received configuration messages */ +} esp_ble_mesh_lcd_server_cb_param_t; + +/** This enum value is the event of Large Composition Data Server model */ +typedef enum { + ESP_BLE_MESH_LCD_SERVER_STATE_CHANGE_EVT, + ESP_BLE_MESH_LCD_SERVER_EVT_MAX, +} esp_ble_mesh_lcd_server_cb_event_t; + +/** + * @brief Large Composition Data client and server model functions. + */ + +/** + * @brief Large Composition Data Client model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_lcd_client_cb_t)(esp_ble_mesh_lcd_client_cb_event_t event, + esp_ble_mesh_lcd_client_cb_param_t *param); + +/** + * @brief Register BLE Mesh Large Composition Data Client model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_lcd_client_callback(esp_ble_mesh_lcd_client_cb_t callback); + +/** + * @brief Get the value of Large Composition Data Server model state with the corresponding get message. + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] msg: Pointer to Large Composition Data Client message. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_lcd_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_lcd_client_msg_t *msg); + +/** + * @brief Large Composition Data Server model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_lcd_server_cb_t)(esp_ble_mesh_lcd_server_cb_event_t event, + esp_ble_mesh_lcd_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh Large Composition Data Server model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_lcd_server_callback(esp_ble_mesh_lcd_server_cb_t callback); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_LCD_MODEL_API_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_odp_model_api.h b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_odp_model_api.h new file mode 100644 index 000000000000..d09c031f447f --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_odp_model_api.h @@ -0,0 +1,205 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ESP_BLE_MESH_ODP_MODEL_API_H_ +#define _ESP_BLE_MESH_ODP_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ESP_BLE_MESH_MODEL_OP_OD_PRIV_PROXY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x69) +#define ESP_BLE_MESH_MODEL_OP_OD_PRIV_PROXY_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x6A) +#define ESP_BLE_MESH_MODEL_OP_OD_PRIV_PROXY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x6B) + +/** @def ESP_BLE_MESH_MODEL_ODP_SRV + * + * @brief Define a new On-Demand Private Proxy Config Server model. + * + * @note The On-Demand Private Proxy Server model is used to represent the + * ability to enable advertising with Private Network Identity type + * of a node. This model extends the Mesh Private Beacon Server model. + * When this model is present on an element, the corresponding + * Solicitation PDU RPL Configuration Server model shall also be present. + * The model shall be supported by a primary element and shall not be + * supported by any secondary elements. + * + * @param srv_data Pointer to a unique On-Demand Private Proxy Config Server + * model user_data. + * + * @return New On-Demand Private Proxy Config Server model instance. + */ +#define ESP_BLE_MESH_MODEL_ODP_SRV(srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_ODP_SRV, \ + NULL, NULL, srv_data) + +/** @def ESP_BLE_MESH_MODEL_ODP_CLI + * + * @brief Define a new On-Demand Private Proxy Config Client model. + * + * @note The model shall be supported by a primary element and shall not be + * supported by any secondary elements. + * + * @param cli_data Pointer to a unique On-Demand Private Proxy Config Client + * model user_data. + * + * @return New On-Demand Private Proxy Config Client model instance. + */ +#define ESP_BLE_MESH_MODEL_ODP_CLI(cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_ODP_CLI, \ + NULL, NULL, cli_data) + +/** On-Demand Private Proxy Config Server model context */ +typedef struct { + /** Pointer to On-Demand Private Proxy Config Server model */ + esp_ble_mesh_model_t *model; + + /** Duration in seconds of the interval during which advertising + * with Private Network Identity type is enabled after receiving + * a Solicitation PDU or after a client disconnect. + * Note: Binding with the Private GATT Proxy state. + */ + uint8_t on_demand_private_gatt_proxy; +} esp_ble_mesh_odp_srv_t; + +/** Parameter of On-Demand Private Proxy Set */ +typedef struct { + uint8_t gatt_proxy; /*!< On-Demand Private GATT Proxy */ +} esp_ble_mesh_od_priv_proxy_set_t; + +/** + * @brief On-Demand Private Proxy Client model message union + */ +typedef union { + esp_ble_mesh_od_priv_proxy_set_t od_priv_proxy_set; /*!< For ESP_BLE_MESH_MODEL_OP_OD_PRIV_PROXY_SET */ +} esp_ble_mesh_odp_client_msg_t; + +/** Parameter of On-Demand Private Proxy Status */ +typedef struct { + uint8_t gatt_proxy; /*!< On-Demand Private GATT Proxy */ +} esp_ble_mesh_od_priv_proxy_status_t; + +/** Result of sending On-Demand Private Proxy Client messages */ +typedef struct { + int err_code; /*!< Result of sending a message */ +} esp_ble_mesh_odp_client_send_cb_t; + +/** + * @brief On-Demand Private Proxy Client model received message union + */ +typedef union { + esp_ble_mesh_od_priv_proxy_status_t od_priv_proxy_status; /*!< For ESP_BLE_MESH_MODEL_OP_OD_PRIV_PROXY_STATUS */ +} esp_ble_mesh_odp_client_recv_cb_t; + +/** On-Demand Private Proxy Config Client model callback parameters */ +typedef struct { + esp_ble_mesh_client_common_param_t *params; /*!< Client common parameters, used by all events */ + /** Union of ODP Client callback */ + union { + esp_ble_mesh_odp_client_send_cb_t send; /*!< Result of sending a message */ + esp_ble_mesh_odp_client_recv_cb_t recv; /*!< Parameters of received status message */ + }; +} esp_ble_mesh_odp_client_cb_param_t; /*!< On-Demand Private Proxy Config Client model callback parameters */ + +/** This enum value is the event of On-Demand Private Proxy Config Client model */ +typedef enum { + ESP_BLE_MESH_ODP_CLIENT_SEND_COMP_EVT, + ESP_BLE_MESH_ODP_CLIENT_SEND_TIMEOUT_EVT, + ESP_BLE_MESH_ODP_CLIENT_RECV_RSP_EVT, + ESP_BLE_MESH_ODP_CLIENT_RECV_PUB_EVT, + ESP_BLE_MESH_ODP_CLIENT_EVT_MAX, +} esp_ble_mesh_odp_client_cb_event_t; + +/** + * @brief On-Demand Private Proxy Config Server model related context. + */ + +/** + * @brief On-Demand Private Proxy Config Server model state change value union + */ +typedef union { + uint8_t dummy; /*!< Event not used currently */ +} esp_ble_mesh_odp_server_state_change_t; + +/** + * @brief On-Demand Private Proxy Config Server model callback value union + */ +typedef union { + esp_ble_mesh_odp_server_state_change_t state_change; /*!< For ESP_BLE_MESH_ODP_SERVER_STATE_CHANGE_EVT */ +} esp_ble_mesh_odp_server_cb_value_t; + +/** On-Demand Private Proxy Config Server model callback parameters */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + esp_ble_mesh_msg_ctx_t ctx; /*!< Context of the received message */ + esp_ble_mesh_odp_server_cb_value_t value; /*!< Value of the received configuration messages */ +} esp_ble_mesh_odp_server_cb_param_t; + +/** This enum value is the event of On-Demand Private Proxy Config Server model */ +typedef enum { + ESP_BLE_MESH_ODP_SERVER_STATE_CHANGE_EVT, + ESP_BLE_MESH_ODP_SERVER_EVT_MAX, +} esp_ble_mesh_odp_server_cb_event_t; + +/** + * @brief Bluetooth Mesh On-Demand Private Proxy Config client and server model functions. + */ + +/** + * @brief On-Demand Private Proxy Config Client model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_odp_client_cb_t)(esp_ble_mesh_odp_client_cb_event_t event, + esp_ble_mesh_odp_client_cb_param_t *param); + +/** + * @brief Register BLE Mesh On-Demand Private Proxy Config Client model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_odp_client_callback(esp_ble_mesh_odp_client_cb_t callback); + +/** + * @brief Get the value of On-Demand Private Proxy Config Server model state with the corresponding get message. + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] msg: Pointer to On-Demand Private Proxy Config Client message. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_odp_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_odp_client_msg_t *msg); + +/** + * @brief On-Demand Private Proxy Config Server model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_odp_server_cb_t)(esp_ble_mesh_odp_server_cb_event_t event, + esp_ble_mesh_odp_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh On-Demand Private Proxy Config Server model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_odp_server_callback(esp_ble_mesh_odp_server_cb_t callback); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_ODP_MODEL_API_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_prb_model_api.h b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_prb_model_api.h new file mode 100644 index 000000000000..115283f8fc8b --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_prb_model_api.h @@ -0,0 +1,258 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ESP_BLE_MESH_PRB_MODEL_API_H_ +#define _ESP_BLE_MESH_PRB_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ESP_BLE_MESH_MODEL_OP_PRIV_BEACON_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x60) +#define ESP_BLE_MESH_MODEL_OP_PRIV_BEACON_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x61) +#define ESP_BLE_MESH_MODEL_OP_PRIV_BEACON_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x62) +#define ESP_BLE_MESH_MODEL_OP_PRIV_GATT_PROXY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x63) +#define ESP_BLE_MESH_MODEL_OP_PRIV_GATT_PROXY_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x64) +#define ESP_BLE_MESH_MODEL_OP_PRIV_GATT_PROXY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x65) +#define ESP_BLE_MESH_MODEL_OP_PRIV_NODE_IDENTITY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x66) +#define ESP_BLE_MESH_MODEL_OP_PRIV_NODE_IDENTITY_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x67) +#define ESP_BLE_MESH_MODEL_OP_PRIV_NODE_IDENTITY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x68) + +/** @def ESP_BLE_MESH_MODEL_PRB_SRV + * + * @brief Define a new Private Beacon Server Model. + * + * @note The Private Beacon Server Model can only be included by a Primary Element. + * + * @param srv_data Pointer to a unique Private Beacon Server Model user_data. + * + * @return New Private Beacon Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_PRB_SRV(srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_PRB_SRV, \ + NULL, NULL, srv_data) + +/** @def ESP_BLE_MESH_MODEL_PRB_CLI + * + * @brief Define a new Private Beacon Client Model. + * + * @note The Private Beacon Client Model can only be included by a Primary Element. + * + * @param cli_data Pointer to a unique struct esp_ble_mesh_client_t. + * + * @return New Private Beacon Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_PRB_CLI(cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_PRB_CLI, \ + NULL, NULL, cli_data) + +/** Private Beacon Server Model context */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to Private Beacon Server Model */ + + uint8_t private_beacon; /*!< Value of Private Beacon state */ + uint8_t random_update_interval; /*!< Value of Random Update Interval Steps state */ + uint8_t private_gatt_proxy; /*!< Value of Private GATT Proxy state */ + + struct k_delayed_work update_timer; /*!< Timer for update the random field of private beacon */ +} esp_ble_mesh_prb_srv_t; + +/** Parameter of private Beacon Set */ +typedef struct { + uint8_t private_beacon; /*!< New Private Beacon state */ + bool is_effect; /*!< Decide if update_interval exists */ + uint8_t update_interval; /*!< New Random Update Interval Steps state */ +} esp_ble_mesh_priv_beacon_set_t; + +/** Parameter of Private GATT Proxy Set */ +typedef struct { + uint8_t private_gatt_proxy; /*!< New Private GATT Proxy state */ +} esp_ble_mesh_priv_gatt_proxy_set_t; + +/** Parameter of Private node identity Get */ +typedef struct { + uint16_t net_idx; /*!< Index of the NetKey */ +} esp_ble_mesh_priv_node_id_get_t; + +/** Parameter of Private node identity Set */ +typedef struct { + uint16_t net_idx; /*!< Index of the NetKey */ + uint8_t private_node_id; /*!< New Private Node Identity state */ +} esp_ble_mesh_priv_node_id_set_t; + +/** + * @brief Mesh Private Beacon Client model message union + */ +typedef union { + esp_ble_mesh_priv_beacon_set_t priv_beacon_set; /*!< ESP_BLE_MESH_MODEL_OP_PRIV_BEACON_SET. */ + esp_ble_mesh_priv_gatt_proxy_set_t priv_gatt_proxy_set; /*!< ESP_BLE_MESH_MODEL_OP_PRIV_GATT_PROXY_SET. */ + esp_ble_mesh_priv_node_id_get_t priv_node_id_get; /*!< ESP_BLE_MESH_MODEL_OP_PRIV_NODE_IDENTITY_GET. */ + esp_ble_mesh_priv_node_id_set_t priv_node_id_set; /*!< ESP_BLE_MESH_MODEL_OP_PRIV_NODE_IDENTITY_SET. */ +} esp_ble_mesh_prb_client_msg_t; + +/** Parameter of Private Beacon Status */ +typedef struct { + uint8_t private_beacon; /*!< Current value of the Private Beacon state */ + uint8_t update_interval; /*!< Current value of the Random Update Interval Steps state */ +} esp_ble_mesh_priv_beacon_status_cb_t; + +/** Parameter of Private GATT Proxy Status */ +typedef struct { + uint8_t private_gatt_proxy; /*!< Private GATT Proxy state */ +} esp_ble_mesh_priv_gatt_proxy_status_cb_t; + +/** Parameters of Private Node Identity Status */ +typedef struct { + uint8_t status; /*!< Status Code for the requesting message */ + uint16_t net_idx; /*!< Index of the NetKey */ + uint8_t private_node_id; /*!< Private Node Identity state */ +} esp_ble_mesh_priv_node_identity_status_cb_t; + +/** + * @brief Private Beacon Client Model received message union + */ +typedef union { + esp_ble_mesh_priv_beacon_status_cb_t priv_beacon_status; /*!< The private beacon status value */ + esp_ble_mesh_priv_gatt_proxy_status_cb_t priv_gatt_proxy_status; /*!< The private gatt proxy status value */ + esp_ble_mesh_priv_node_identity_status_cb_t priv_node_id_status; /*!< The private node identity status value */ +} esp_ble_mesh_prb_client_recv_cb_t; + +/** Result of sending Bridge Configuration Client messages */ +typedef struct { + int err_code; /*!< Result of sending a message */ +} esp_ble_mesh_prb_client_send_cb_t; + +/** Mesh Private Beacon Client Model callback parameters */ +typedef struct { + esp_ble_mesh_client_common_param_t *params; /*!< The client common parameters. */ + /** Union of Private Beacon Client callback */ + union { + esp_ble_mesh_prb_client_send_cb_t send; /*!< Result of sending a message */ + esp_ble_mesh_prb_client_recv_cb_t recv; /*!< The private beacon message status callback values */ + }; +} esp_ble_mesh_prb_client_cb_param_t; /*!< Mesh Private Beacon Client Model callback parameters */ + +/** This enum value is the event of Private Beacon Client Model */ +typedef enum { + ESP_BLE_MESH_PRB_CLIENT_SEND_COMP_EVT, + ESP_BLE_MESH_PRB_CLIENT_SEND_TIMEOUT_EVT, + ESP_BLE_MESH_PRB_CLIENT_RECV_RSP_EVT, + ESP_BLE_MESH_PRB_CLIENT_RECV_PUB_EVT, + ESP_BLE_MESH_PRB_CLIENT_EVT_MAX, +} esp_ble_mesh_prb_client_cb_event_t; + +/** + * @brief Mesh Private Beacon Server model related context. + */ +/** Parameters of Private Beacon Set. */ +typedef struct { + uint8_t private_beacon; /*!< Private Beacon state */ + bool is_effect; /*!< Decide whether update_interval effect */ + uint8_t update_interval; /*!< Random Update Interval Steps state */ +} esp_ble_mesh_state_change_priv_beacon_set_t; + +/** Parameters of Private GATT Proxy Set. */ +typedef struct { + uint8_t private_gatt_proxy; /*!< Private GATT Proxy state */ +} esp_ble_mesh_state_change_priv_gatt_proxy_set_t; + +/** Parameters of Private Node Identity Set. */ +typedef struct { + uint16_t net_idx; /*!< Index of the NetKey */ + uint8_t private_node_id; /*!< Private Node Identity state */ +} esp_ble_mesh_state_change_priv_node_id_set_t; + +/** + * @brief Mesh Private Beacon Server model state change value union + */ +typedef union { + /** + * The recv_op in ctx can be used to decide which state is changed. + */ + esp_ble_mesh_state_change_priv_beacon_set_t priv_beacon_set; /*!< Private Beacon Set */ + esp_ble_mesh_state_change_priv_gatt_proxy_set_t priv_gatt_proxy_set; /*!< Private GATT Proxy Set */ + esp_ble_mesh_state_change_priv_node_id_set_t priv_node_id_set; /*!< Private Node Identity Set */ +} esp_ble_mesh_prb_server_state_change_t; + +/** + * @brief Private Beacon Server model callback value union + */ +typedef union { + esp_ble_mesh_prb_server_state_change_t state_change; /*!< ESP_BLE_MESH_PRB_SERVER_STATE_CHANGE_EVT */ +} esp_ble_mesh_prb_server_cb_value_t; + +/** Private Beacon Server model callback parameters */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + esp_ble_mesh_msg_ctx_t ctx; /*!< Context of the received message */ + esp_ble_mesh_prb_server_cb_value_t value; /*!< Value of the received private beacon messages */ +} esp_ble_mesh_prb_server_cb_param_t; + +/** This enum value is the event of Private Beacon Server model */ +typedef enum { + ESP_BLE_MESH_PRB_SERVER_STATE_CHANGE_EVT, + ESP_BLE_MESH_PRB_SERVER_EVT_MAX, +} esp_ble_mesh_prb_server_cb_event_t; + +/** + * @brief Bluetooth Mesh Private Beacon Client and Server Model functions. + */ + +/** + * @brief Private Beacon Client Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_prb_client_cb_t)(esp_ble_mesh_prb_client_cb_event_t event, + esp_ble_mesh_prb_client_cb_param_t *param); + +/** + * @brief Private Beacon Server Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_prb_server_cb_t)(esp_ble_mesh_prb_server_cb_event_t event, + esp_ble_mesh_prb_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh Private Beacon Client Model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_prb_client_callback(esp_ble_mesh_prb_client_cb_t callback); + +/** + * @brief Register BLE Mesh Private Beacon Server Model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_prb_server_callback(esp_ble_mesh_prb_server_cb_t callback); + +/** + * @brief Get/Set the value of Private Beacon Server Model states using the corresponding messages of Private Beacon Client Model. + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] msg: Pointer to Mesh Private Beacon Client message. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_prb_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_prb_client_msg_t *msg); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_PRB_MODEL_API_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_rpr_model_api.h b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_rpr_model_api.h new file mode 100644 index 000000000000..87f9a9b7fbc6 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_rpr_model_api.h @@ -0,0 +1,479 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ESP_BLE_MESH_RPR_MODEL_API_H_ +#define _ESP_BLE_MESH_RPR_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ESP_BLE_MESH_MODEL_OP_RPR_SCAN_CAPS_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x4F) +#define ESP_BLE_MESH_MODEL_OP_RPR_SCAN_CAPS_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x50) +#define ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x51) +#define ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START ESP_BLE_MESH_MODEL_OP_2(0x80, 0x52) +#define ESP_BLE_MESH_MODEL_OP_RPR_SCAN_STOP ESP_BLE_MESH_MODEL_OP_2(0x80, 0x53) +#define ESP_BLE_MESH_MODEL_OP_RPR_SCAN_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x54) +#define ESP_BLE_MESH_MODEL_OP_RPR_SCAN_REPORT ESP_BLE_MESH_MODEL_OP_2(0x80, 0x55) +#define ESP_BLE_MESH_MODEL_OP_RPR_EXT_SCAN_START ESP_BLE_MESH_MODEL_OP_2(0x80, 0x56) +#define ESP_BLE_MESH_MODEL_OP_RPR_EXT_SCAN_REPORT ESP_BLE_MESH_MODEL_OP_2(0x80, 0x57) +#define ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x58) +#define ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN ESP_BLE_MESH_MODEL_OP_2(0x80, 0x59) +#define ESP_BLE_MESH_MODEL_OP_RPR_LINK_CLOSE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x5A) +#define ESP_BLE_MESH_MODEL_OP_RPR_LINK_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x5B) +#define ESP_BLE_MESH_MODEL_OP_RPR_LINK_REPORT ESP_BLE_MESH_MODEL_OP_2(0x80, 0x5C) +#define ESP_BLE_MESH_MODEL_OP_RPR_PDU_SEND ESP_BLE_MESH_MODEL_OP_2(0x80, 0x5D) +#define ESP_BLE_MESH_MODEL_OP_RPR_PDU_OUTBOUND_REPORT ESP_BLE_MESH_MODEL_OP_2(0x80, 0x5E) +#define ESP_BLE_MESH_MODEL_OP_RPR_PDU_REPORT ESP_BLE_MESH_MODEL_OP_2(0x80, 0x5F) + +#define ESP_BLE_MESH_RPR_SRV_MAX_SCANNED_ITEMS_MIN 0x04 + +#define ESP_BLE_MESH_RPR_NOT_SUPPORT_ACTIVE_SCAN 0x00 +#define ESP_BLE_MESH_RPR_SUPPORT_ACTIVE_SCAN 0x01 + +#define ESP_BLE_MESH_RPR_SCAN_IDLE 0x00 +#define ESP_BLE_MESH_RPR_SCAN_MULTIPLE_DEVICE 0x01 +#define ESP_BLE_MESH_RPR_SCAN_SINGLE_DEVICE 0x02 + +#define ESP_BLE_MESH_RPR_SCAN_NOT_IN_PROGRESS 0x00 + +#define ESP_BLE_MESH_RPR_PROHIBIT_SCAN_TIMEOUT 0x00 + +#define ESP_BLE_MESH_RPR_EXT_SCAN_TIMEOUT_MIN 0x01 +#define ESP_BLE_MESH_RPR_EXT_SCAN_TIMEOUT_MAX 0x15 + +#define ESP_BLE_MESH_RPR_AD_TYPE_FILTER_CNT_MIN 0x01 +#define ESP_BLE_MESH_RPR_AD_TYPE_FILTER_CNT_MAX 0x10 + +#define ESP_BLE_MESH_RPR_LINK_OPEN_TIMEOUT_MIN 0x01 +#define ESP_BLE_MESH_RPR_LINK_OPEN_TIMEOUT_MAX 0x3C + +/* The default value of the Timeout parameter is 10 seconds */ +#define ESP_BLE_MESH_RPR_LINK_TIMEOUT_DEFAULT 0x0A + +#define ESP_BLE_MESH_RPR_REASON_SUCCESS 0x00 +#define ESP_BLE_MESH_RPR_REASON_FAIL 0x02 + +#define ESP_BLE_MESH_RPR_LINK_IDLE 0x00 +#define ESP_BLE_MESH_RPR_LINK_OPENING 0x01 +#define ESP_BLE_MESH_RPR_LINK_ACTIVE 0x02 +#define ESP_BLE_MESH_RPR_OUTBOUND_PACKET_TRANSFER 0x03 +#define ESP_BLE_MESH_RPR_LINK_CLOSING 0x04 + +#define ESP_BLE_MESH_RPR_STATUS_SUCCESS 0x00 +#define ESP_BLE_MESH_RPR_STATUS_SCANNING_CANNOT_START 0x01 +#define ESP_BLE_MESH_RPR_STATUS_INVALID_STATE 0x02 +#define ESP_BLE_MESH_RPR_STATUS_LIMITED_RESOURCES 0x03 +#define ESP_BLE_MESH_RPR_STATUS_LINK_CANNOT_OPEN 0x04 +#define ESP_BLE_MESH_RPR_STATUS_LINK_OPEN_FAILED 0x05 +#define ESP_BLE_MESH_RPR_STATUS_LINK_CLOSED_BY_DEVICE 0x06 +#define ESP_BLE_MESH_RPR_STATUS_LINK_CLOSED_BY_SERVER 0x07 +#define ESP_BLE_MESH_RPR_STATUS_LINK_CLOSED_BY_CLIENT 0x08 +#define ESP_BLE_MESH_RPR_STATUS_LINK_CLOSED_AS_CANNOT_RECEIVE_PDU 0x09 +#define ESP_BLE_MESH_RPR_STATUS_LINK_CLOSED_AS_CANNOT_SEND_PDU 0x0A +#define ESP_BLE_MESH_RPR_STATUS_LINK_CLOSED_AS_CANNOT_DELIVER_PDU_REPORT 0x0B + +/** @def ESP_BLE_MESH_MODEL_RPR_SRV + * + * @brief Define a new Remote Provisioning Server model. + * + * @note If supported, the model shall be supported by a primary element + * and may be supported by any secondary element. + * + * @param srv_data Pointer to a unique Remote Provisioning Server model + * user_data. + * + * @return New Remote Provisioning Server model instance. + */ +#define ESP_BLE_MESH_MODEL_RPR_SRV(srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_RPR_SRV, \ + NULL, NULL, srv_data) + +/** @def ESP_BLE_MESH_MODEL_RPR_CLI + * + * @brief Define a new Remote Provisioning Client model. + * + * @note If supported, the model shall be supported by a primary element + * and may be supported by any secondary element. + * + * @param cli_data Pointer to a unique Remote Provisioning Client model + * user_data. + * + * @return New Remote Provisioning Client model instance. + */ +#define ESP_BLE_MESH_MODEL_RPR_CLI(cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_RPR_CLI, \ + NULL, NULL, cli_data) + +/** Remote Provisioning Server model context */ +/* typedef struct { + * void *dummy; + * } esp_ble_mesh_rpr_srv_t; + */ + +/** Parameters of Remote Provisioning Scan Start */ +typedef struct { + uint8_t scan_items_limit; /*!< Maximum number of scanned items to be reported */ + uint8_t timeout; /*!< Time limit for a scan (in seconds) */ + + bool uuid_en; /*!< Indicate if Device UUID is present */ + uint8_t uuid[16]; /*!< Device UUID (Optional) */ +} esp_ble_mesh_rpr_scan_start_t; + +/** Parameters of Remote Provisioning Extended Scan Start */ +typedef struct { + uint8_t ad_type_filter_count; /*!< Number of AD Types in the ADTypeFilter field */ + uint8_t ad_type_filter[16]; /*!< List of AD Types to be reported. Minimum is 1, maximum is 16 */ + + bool uuid_en; /*!< Indicate if Device UUID is present */ + uint8_t uuid[16]; /*!< Device UUID (Optional) */ + uint8_t timeout; /*!< Time limit for a scan (in seconds) (C.1) */ +} esp_ble_mesh_rpr_ext_scan_start_t; + +/** Parameters of Remote Provisioning Link Open */ +typedef struct { + bool uuid_en; /*!< Indicate if Device UUID is present */ + uint8_t uuid[16]; /*!< Device UUID (Optional) */ + + bool timeout_en; /*!< Indicate if Link open timeout is present */ + uint8_t timeout; /*!< Link open timeout in seconds (C.1) */ + + uint8_t nppi; /*!< Node Provisioning Protocol Interface (C.2) */ +} esp_ble_mesh_rpr_link_open_t; + +/** Parameters of Remote Provisioning Link Close */ +typedef struct { + uint8_t reason; /*!< Provisioning bearer link close reason code */ +} esp_ble_mesh_rpr_link_close_t; + +/** + * @brief Remote Provisioning Client model message union + */ +typedef union { + esp_ble_mesh_rpr_scan_start_t scan_start; /*!< For ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START */ + esp_ble_mesh_rpr_ext_scan_start_t ext_scan_start; /*!< For ESP_BLE_MESH_MODEL_OP_RPR_EXT_SCAN_START */ + esp_ble_mesh_rpr_link_open_t link_open; /*!< For ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN */ + esp_ble_mesh_rpr_link_close_t link_close; /*!< For ESP_BLE_MESH_MODEL_OP_RPR_LINK_CLOSE */ +} esp_ble_mesh_rpr_client_msg_t; + +/** This enum value is the action of Remote Provisioning Client model */ +typedef enum { + ESP_BLE_MESH_RPR_CLIENT_ACT_START_RPR, + ESP_BLE_MESH_RPR_CLIENT_ACT_MAX, +} esp_ble_mesh_rpr_client_act_type_t; + +/** Parameters of starting remote provisioning */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer of Remote Provisioning Client */ + uint16_t rpr_srv_addr; /*!< Unicast address of Remote Provisioning Server */ +} esp_ble_mesh_rpr_client_start_rpr_t; + +/** + * @brief Remote Provisioning Client model action union + */ +typedef union { + esp_ble_mesh_rpr_client_start_rpr_t start_rpr; /*!< Start remote provisioning */ +} esp_ble_mesh_rpr_client_act_param_t; + +/** Parameters of Remote Provisioning Scan Capabilities Status */ +typedef struct { + uint8_t max_scan_items; /*!< The maximum number of UUIDs that can be reported during scanning */ + uint8_t active_scan; /*!< Indication if active scan is supported */ +} esp_ble_mesh_rpr_scan_caps_status_t; + +/** Parameters of Remote Provisioning Scan Status */ +typedef struct { + uint8_t status; /*!< Status for the requesting message */ + uint8_t rpr_scanning; /*!< The Remote Provisioning Scan state value */ + uint8_t scan_items_limit; /*!< Maximum number of scanned items to be reported */ + uint8_t timeout; /*!< Time limit for a scan (in seconds) */ +} esp_ble_mesh_rpr_scan_status_t; + +/** Parameters of Remote Provisioning Scan Report */ +typedef struct { + int8_t rssi; /*!< An indication of received signal strength measured in dBm */ + uint8_t uuid[16]; /*!< Device UUID */ + uint16_t oob_info; /*!< OOB information */ + uint32_t uri_hash; /*!< URI Hash (Optional) */ +} esp_ble_mesh_rpr_scan_report_t; + +/** Parameters of Remote Provisioning Extended Scan Report */ +typedef struct { + uint8_t status; /*!< Status for the requesting message */ + uint8_t uuid[16]; /*!< Device UUID */ + + bool oob_info_en; /*!< Indicate if OOB Information is present */ + uint16_t oob_info; /*!< OOB Information (Optional) */ + struct net_buf_simple *adv_structures; /*!< Concatenated list of AD Structures (C.1) */ +} esp_ble_mesh_rpr_ext_scan_report_t; + +/** Parameters of Remote Provisioning Link Status */ +typedef struct { + uint8_t status; /*!< Status for the requesting message */ + uint8_t rpr_state; /*!< Remote Provisioning Link state */ +} esp_ble_mesh_rpr_link_status_t; + +/** Parameters of Remote Provisioning Link Report */ +typedef struct { + uint8_t status; /*!< Status of the provisioning bearer or the NPPI */ + uint8_t rpr_state; /*!< Remote Provisioning Link state */ + bool reason_en; /*!< Indicate if Link close Reason code is present */ + uint8_t reason; /*!< Link close Reason code (Optional) */ +} esp_ble_mesh_rpr_link_report_t; + +/** + * @brief Remote Provisioning Client model received message union + */ +typedef union { + esp_ble_mesh_rpr_scan_caps_status_t scan_caps_status; /*!< For ESP_BLE_MESH_MODEL_OP_RPR_SCAN_CAPS_STATUS */ + esp_ble_mesh_rpr_scan_status_t scan_status; /*!< For ESP_BLE_MESH_MODEL_OP_RPR_SCAN_STATUS */ + esp_ble_mesh_rpr_scan_report_t scan_report; /*!< For ESP_BLE_MESH_MODEL_OP_RPR_SCAN_REPORT */ + esp_ble_mesh_rpr_ext_scan_report_t ext_scan_report; /*!< For ESP_BLE_MESH_MODEL_OP_RPR_EXT_SCAN_REPORT */ + esp_ble_mesh_rpr_link_status_t link_status; /*!< For ESP_BLE_MESH_MODEL_OP_RPR_LINK_STATUS */ + esp_ble_mesh_rpr_link_report_t link_report; /*!< For ESP_BLE_MESH_MODEL_OP_RPR_LINK_REPORT */ +} esp_ble_mesh_rpr_client_recv_cb_t; + +/** Remote Provisioning Client model callback parameters */ +typedef union { + /** Event parameters of sending messages */ + struct { + int err_code; /*!< Result of sending a message */ + esp_ble_mesh_client_common_param_t *params; /*!< Client common parameters */ + } send; /*!< Event parameters of sending messages */ + /** Event parameters of receiving messages */ + struct { + esp_ble_mesh_client_common_param_t *params; /*!< Client common parameters */ + esp_ble_mesh_rpr_client_recv_cb_t val; /*!< Parameters of received status message */ + } recv; /*!< Event parameters of receiving messages */ + /** Event parameters of performed actions */ + struct { + /** Event type of the performed action */ + enum { + ESP_BLE_MESH_START_RPR_COMP_SUB_EVT, + } sub_evt; /*!< Event type of the performed action */ + /** + * @brief ESP_BLE_MESH_START_RPR_COMP_SUB_EVT + */ + struct { + int err_code; /*!< Result of starting remote provisioning */ + esp_ble_mesh_model_t *model; /*!< Pointer of Remote Provisioning Client */ + uint16_t rpr_srv_addr; /*!< Unicast address of Remote Provisioning Server */ + } start_rpr_comp; /*!< Event parameter of ESP_BLE_MESH_START_RPR_COMP_SUB_EVT */ + } act; /*!< Event parameters of performed actions */ + /** + * @brief ESP_BLE_MESH_RPR_CLIENT_LINK_OPEN_EVT + */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer of Remote Provisioning Client */ + uint16_t rpr_srv_addr; /*!< Unicast address of Remote Provisioning Server */ + } link_open; /*!< Event parameters of ESP_BLE_MESH_RPR_CLIENT_LINK_OPEN_EVT */ + /** + * @brief ESP_BLE_MESH_RPR_CLIENT_LINK_CLOSE_EVT + */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer of Remote Provisioning Client */ + uint16_t rpr_srv_addr; /*!< Unicast address of Remote Provisioning Server */ + uint8_t reason; /*!< Reason of closing provisioning link */ + } link_close; /*!< Event parameters of ESP_BLE_MESH_RPR_CLIENT_LINK_CLOSE_EVT */ + /** + * @brief ESP_BLE_MESH_RPR_CLIENT_PROV_COMP_EVT + */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer of Remote Provisioning Client */ + uint16_t rpr_srv_addr; /*!< Unicast address of Remote Provisioning Server */ + uint8_t nppi; /*!< NPPI Procedure */ + uint16_t index; /*!< Index of the provisioned node */ + uint8_t uuid[16]; /*!< Device UUID */ + uint16_t unicast_addr; /*!< Primary element address */ + uint8_t element_num; /*!< Element number */ + uint16_t net_idx; /*!< NetKey Index */ + } prov; /*!< Event parameters of ESP_BLE_MESH_RPR_CLIENT_PROV_COMP_EVT */ +} esp_ble_mesh_rpr_client_cb_param_t; /*!< Remote Provisioning Client model callback parameters */ + +/** This enum value is the event of Remote Provisioning Client model */ +typedef enum { + ESP_BLE_MESH_RPR_CLIENT_SEND_COMP_EVT, + ESP_BLE_MESH_RPR_CLIENT_SEND_TIMEOUT_EVT, + ESP_BLE_MESH_RPR_CLIENT_RECV_RSP_EVT, + ESP_BLE_MESH_RPR_CLIENT_RECV_PUB_EVT, + ESP_BLE_MESH_RPR_CLIENT_ACT_COMP_EVT, + ESP_BLE_MESH_RPR_CLIENT_LINK_OPEN_EVT, + ESP_BLE_MESH_RPR_CLIENT_LINK_CLOSE_EVT, + ESP_BLE_MESH_RPR_CLIENT_PROV_COMP_EVT, + ESP_BLE_MESH_RPR_CLIENT_EVT_MAX, +} esp_ble_mesh_rpr_client_cb_event_t; + +/** + * @brief Remote Provisioning Server model related context. + */ + +/** + * @brief Remote Provisioning Server model callback value union + */ +typedef union { + /** + * @brief ESP_BLE_MESH_RPR_SERVER_SCAN_START_EVT + */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + uint8_t scan_items_limit; /*!< Maximum number of scanned items to be reported */ + uint8_t timeout; /*!< Time limit for a scan (in seconds) */ + uint8_t uuid[16]; /*!< Device UUID (All ZERO if not present) */ + uint16_t net_idx; /*!< NetKey Index used by Remote Provisioning Client */ + uint16_t rpr_cli_addr; /*!< Unicast address of Remote Provisioning Client */ + } scan_start; + /** + * @brief ESP_BLE_MESH_RPR_SERVER_SCAN_STOP_EVT + */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + uint8_t uuid[16]; /*!< Device UUID (All ZERO if not present) */ + uint16_t net_idx; /*!< NetKey Index used by Remote Provisioning Client */ + uint16_t rpr_cli_addr; /*!< Unicast address of Remote Provisioning Client */ + } scan_stop; + /** + * @brief ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_START_EVT + */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + uint8_t ad_type_filter_count; /*!< Number of AD Types in the ADTypeFilter field */ + uint8_t *ad_type_filter; /*!< List of AD Types to be reported */ + uint8_t uuid[16]; /*!< Device UUID (All ZERO if not present) */ + uint8_t timeout; /*!< Time limit for a scan (in seconds) */ + uint8_t index; /*!< Index of the extended scan instance */ + uint16_t net_idx; /*!< NetKey Index used by Remote Provisioning Client */ + uint16_t rpr_cli_addr; /*!< Unicast address of Remote Provisioning Client */ + } ext_scan_start; + /** + * @brief ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_STOP_EVT + */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + uint8_t uuid[16]; /*!< Device UUID (ZERO if not present)*/ + uint8_t timeout; /*!< Time limit for extended scan (in seconds) */ + uint8_t index; /*!< Index of the extended scan instance */ + uint16_t net_idx; /*!< NetKey Index used by Remote Provisioning Client */ + uint16_t rpr_cli_addr; /*!< Unicast address of Remote Provisioning Client */ + } ext_scan_stop; + /** + * @brief ESP_BLE_MESH_RPR_SERVER_LINK_OPEN_EVT + */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + uint8_t uuid[16]; /*!< Device UUID (ZERO if not present)*/ + uint8_t status; /*!< Status of Link Open procedure */ + uint8_t timeout; /*!< Time limit for opening a link (in seconds) */ + uint8_t nppi; /*!< Node Provisioning Protocol Interface */ + uint16_t net_idx; /*!< NetKey Index used by Remote Provisioning Client */ + uint16_t rpr_cli_addr; /*!< Unicast address of Remote Provisioning Client */ + } link_open; + /** + * @brief ESP_BLE_MESH_RPR_SERVER_LINK_CLOSE_EVT + */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + uint8_t uuid[16]; /*!< Device UUID (ZERO if not present)*/ + uint8_t nppi; /*!< Node Provisioning Protocol Interface */ + bool close_by_device; /*!< Indicate if the link is closed by the Unprovisioned Device */ + uint8_t reason; /*!< Provisioning bearer link close reason code */ + uint16_t net_idx; /*!< NetKey Index used by Remote Provisioning Client */ + uint16_t rpr_cli_addr; /*!< Unicast address of Remote Provisioning Client */ + } link_close; + /** + * @brief ESP_BLE_MESH_RPR_SERVER_PROV_COMP_EVT. TODO: Duplicate with Link Close event? + */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + uint8_t uuid[16]; /*!< Device UUID (ZERO if not present)*/ + uint8_t nppi; /*!< Provisioning bearer link close reason code */ + uint16_t net_idx; /*!< NetKey Index used by Remote Provisioning Client */ + uint16_t rpr_cli_addr; /*!< Unicast address of Remote Provisioning Client */ + } prov_comp; +} esp_ble_mesh_rpr_server_cb_param_t; + +/** This enum value is the event of Remote Provisioning Server model */ +typedef enum { + ESP_BLE_MESH_RPR_SERVER_SCAN_START_EVT, + ESP_BLE_MESH_RPR_SERVER_SCAN_STOP_EVT, + ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_START_EVT, + ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_STOP_EVT, + ESP_BLE_MESH_RPR_SERVER_LINK_OPEN_EVT, + ESP_BLE_MESH_RPR_SERVER_LINK_CLOSE_EVT, + ESP_BLE_MESH_RPR_SERVER_PROV_COMP_EVT, + ESP_BLE_MESH_RPR_SERVER_EVT_MAX, +} esp_ble_mesh_rpr_server_cb_event_t; + +/** + * @brief Remote Provisioning client and server model functions. + */ + +/** + * @brief Remote Provisioning Client model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_rpr_client_cb_t)(esp_ble_mesh_rpr_client_cb_event_t event, + esp_ble_mesh_rpr_client_cb_param_t *param); + +/** + * @brief Register BLE Mesh Remote Provisioning Client model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_rpr_client_callback(esp_ble_mesh_rpr_client_cb_t callback); + +/** + * @brief Get the value of Remote Provisioning Server model state with the corresponding get message. + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] msg: Pointer to Remote Provisioning Client message. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_rpr_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_rpr_client_msg_t *msg); + +/** + * @brief Remote Provisioning Client model perform related actions, e.g. start remote provisioning. + * + * @param[in] type: Type of the action to be performed. + * @param[in] param: Parameters of the action to be performed. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_rpr_client_action(esp_ble_mesh_rpr_client_act_type_t type, + esp_ble_mesh_rpr_client_act_param_t *param); + +/** + * @brief Remote Provisioning Server model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_rpr_server_cb_t)(esp_ble_mesh_rpr_server_cb_event_t event, + esp_ble_mesh_rpr_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh Remote Provisioning Server model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_rpr_server_callback(esp_ble_mesh_rpr_server_cb_t callback); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_RPR_MODEL_API_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_sar_model_api.h b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_sar_model_api.h new file mode 100644 index 000000000000..74a4b593742c --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_sar_model_api.h @@ -0,0 +1,223 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ESP_BLE_MESH_SAR_MODEL_API_H_ +#define _ESP_BLE_MESH_SAR_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ESP_BLE_MESH_MODEL_OP_SAR_TRANSMITTER_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x6C) +#define ESP_BLE_MESH_MODEL_OP_SAR_TRANSMITTER_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x6D) +#define ESP_BLE_MESH_MODEL_OP_SAR_TRANSMITTER_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x6E) +#define ESP_BLE_MESH_MODEL_OP_SAR_RECEIVER_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x6F) +#define ESP_BLE_MESH_MODEL_OP_SAR_RECEIVER_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x70) +#define ESP_BLE_MESH_MODEL_OP_SAR_RECEIVER_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x71) + +/** @def ESP_BLE_MESH_MODEL_SAR_SRV + * + * @brief Define a new SAR Configuration Server model. + * + * @note If supported, the model shall be supported by a primary element + * and shall not be supported by any secondary elements. + * + * @param srv_data Pointer to a unique SAR Configuration Server model user_data. + * + * @return New SAR Configuration Server model instance. + */ +#define ESP_BLE_MESH_MODEL_SAR_SRV(srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SAR_SRV, \ + NULL, NULL, srv_data) + +/** @def ESP_BLE_MESH_MODEL_SAR_CLI + * + * @brief Define a new SAR Configuration Client model. + * + * @note If supported, the model shall be supported by the primary element + * and shall not be supported by any secondary elements. + * + * @param cli_data Pointer to a unique SAR Configuration Client model user_data. + * + * @return New SAR Configuration Client model instance. + */ +#define ESP_BLE_MESH_MODEL_SAR_CLI(cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SAR_CLI, \ + NULL, NULL, cli_data) + +/** SAR Configuration Server model context */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to SAR Configuration Server model */ +} esp_ble_mesh_sar_srv_t; + +/** Parameters of SAR Transmitter Set */ +typedef struct { + uint8_t sar_segment_interval_step:4, /*!< SAR Segment Interval Step state value */ + sar_unicast_retrans_count:4; /*!< SAR Unicast Retransmissions Count state */ + uint8_t sar_unicast_retrans_without_progress_count:4, /*!< SAR Unicast Retransmissions Without Progress Count state */ + sar_unicast_retrans_interval_step:4; /*!< SAR Unicast Retransmissions Interval Step state */ + uint8_t sar_unicast_retrans_interval_increment:4, /*!< SAR Unicast Retransmissions Interval Increment state */ + sar_multicast_retrans_count:4; /*!< SAR Multicast Retransmissions Count state */ + uint8_t sar_multicast_retrans_interval:4; /*!< SAR Multicast Retransmissions Interval state */ +} esp_ble_mesh_sar_transmitter_set_t; + +/** Parameters of SAR Receiver Set */ +typedef struct { + uint8_t sar_segments_threshold:5, /*!< SAR Segments Threshold state */ + sar_ack_delay_increment:3; /*!< SAR Acknowledgment Delay Increment state */ + uint8_t sar_ack_retrans_count:2, /*!< SAR Acknowledgment Retransmissions Count state */ + sar_discard_timeout:4; /*!< SAR Discard Timeout state */ +} esp_ble_mesh_sar_receiver_set_t; + +/** + * @brief SAR Configuration Client model message union + */ +typedef union { + esp_ble_mesh_sar_transmitter_set_t sar_transmitter_set; /*!< For ESP_BLE_MESH_MODEL_OP_SAR_TRANSMITTER_SET */ + esp_ble_mesh_sar_receiver_set_t sar_receiver_set; /*!< For ESP_BLE_MESH_MODEL_OP_SAR_RECEIVER_SET */ +} esp_ble_mesh_sar_client_msg_t; + +/** Parameters of SAR Transmitter Status */ +typedef struct { + uint8_t sar_segment_interval_step:4, /*!< SAR Segment Interval Step state value */ + sar_unicast_retrans_count:4; /*!< SAR Unicast Retransmissions Count state */ + uint8_t sar_unicast_retrans_without_progress_count:4, /*!< SAR Unicast Retransmissions Without Progress Count state */ + sar_unicast_retrans_interval_step:4; /*!< SAR Unicast Retransmissions Interval Step state */ + uint8_t sar_unicast_retrans_interval_increment:4, /*!< SAR Unicast Retransmissions Interval Increment state */ + sar_multicast_retrans_count:4; /*!< SAR Multicast Retransmissions Count state */ + uint8_t sar_multicast_retrans_interval:4; /*!< SAR Multicast Retransmissions Interval state */ +} esp_ble_mesh_sar_transmitter_status_t; + +/** Parameters of SAR Receiver Status */ +typedef struct { + uint8_t sar_segments_threshold:5, /*!< SAR Segments Threshold state */ + sar_ack_delay_increment:3; /*!< SAR Acknowledgment Delay Increment state */ + uint8_t sar_ack_retrans_count:2, /*!< SAR Acknowledgment Retransmissions Count state */ + sar_discard_timeout:4; /*!< SAR Discard Timeout state */ +} esp_ble_mesh_sar_receiver_status_t; + +/** Result of sending SAR Configuration Client messages */ +typedef struct { + int err_code; /*!< Result of sending a message */ +} esp_ble_mesh_sar_client_send_cb_t; + +/** + * @brief SAR Configuration Client model received message union + */ +typedef union { + esp_ble_mesh_sar_transmitter_status_t sar_transmitter_status; /*!< For ESP_BLE_MESH_MODEL_OP_SAR_TRANSMITTER_STATUS */ + esp_ble_mesh_sar_receiver_status_t sar_receiver_status; /*!< For ESP_BLE_MESH_MODEL_OP_SAR_RECEIVE_STATUS */ +} esp_ble_mesh_sar_client_recv_cb_t; + +/** SAR Configuration Client model callback parameters */ +typedef struct { + esp_ble_mesh_client_common_param_t *params; /*!< Client common parameters, used by all events. */ + /** Union of SAR Client callback */ + union { + esp_ble_mesh_sar_client_send_cb_t send; /*!< Result of sending a message */ + esp_ble_mesh_sar_client_recv_cb_t recv; /*!< Parameters of received status message */ + }; +} esp_ble_mesh_sar_client_cb_param_t; /*!< SAR Configuration Client model callback parameters */ + +/** This enum value is the event of SAR Configuration Client model */ +typedef enum { + ESP_BLE_MESH_SAR_CLIENT_SEND_COMP_EVT, + ESP_BLE_MESH_SAR_CLIENT_SEND_TIMEOUT_EVT, + ESP_BLE_MESH_SAR_CLIENT_RECV_RSP_EVT, + ESP_BLE_MESH_SAR_CLIENT_RECV_PUB_EVT, + ESP_BLE_MESH_SAR_CLIENT_EVT_MAX, +} esp_ble_mesh_sar_client_cb_event_t; + +/** + * @brief SAR Configuration Server model related context. + */ + +/** + * @brief SAR Configuration Server model state change value union + */ +typedef union { + uint8_t dummy; /*!< Event not used currently */ +} esp_ble_mesh_sar_server_state_change_t; + +/** + * @brief SAR Configuration Server model callback value union + */ +typedef union { + esp_ble_mesh_sar_server_state_change_t state_change; /*!< For ESP_BLE_MESH_SAR_SERVER_STATE_CHANGE_EVT */ +} esp_ble_mesh_sar_server_cb_value_t; + +/** SAR Configuration Server model callback parameters */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + esp_ble_mesh_msg_ctx_t ctx; /*!< Context of the received message */ + esp_ble_mesh_sar_server_cb_value_t value; /*!< Value of the received configuration messages */ +} esp_ble_mesh_sar_server_cb_param_t; + +/** This enum value is the event of SAR Configuration Server model */ +typedef enum { + ESP_BLE_MESH_SAR_SERVER_STATE_CHANGE_EVT, + ESP_BLE_MESH_SAR_SERVER_EVT_MAX, +} esp_ble_mesh_sar_server_cb_event_t; + +/** + * @brief Bluetooth Mesh SAR Configuration client and server model functions. + */ + +/** + * @brief SAR Configuration Client model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_sar_client_cb_t)(esp_ble_mesh_sar_client_cb_event_t event, + esp_ble_mesh_sar_client_cb_param_t *param); + +/** + * @brief Register BLE Mesh SAR Configuration Client model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_sar_client_callback(esp_ble_mesh_sar_client_cb_t callback); + +/** + * @brief Get the value of SAR Configuration Server model state with the corresponding get message. + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] msg: Pointer to SAR Configuration Client message. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_sar_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_sar_client_msg_t *msg); + +/** + * @brief SAR Configuration Server model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_sar_server_cb_t)(esp_ble_mesh_sar_server_cb_event_t event, + esp_ble_mesh_sar_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh SAR Configuration Server model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_sar_server_callback(esp_ble_mesh_sar_server_cb_t callback); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_SAR_MODEL_API_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_srpl_model_api.h b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_srpl_model_api.h new file mode 100644 index 000000000000..efe60f71d0bb --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_srpl_model_api.h @@ -0,0 +1,198 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ESP_BLE_MESH_SRPL_MODEL_API_H_ +#define _ESP_BLE_MESH_SRPL_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ESP_BLE_MESH_MODEL_OP_SRPL_ITEMS_CLEAR ESP_BLE_MESH_MODEL_OP_2(0x80, 0x78) +#define ESP_BLE_MESH_MODEL_OP_SRPL_ITEMS_CLEAR_UNACK ESP_BLE_MESH_MODEL_OP_2(0x80, 0x79) +#define ESP_BLE_MESH_MODEL_OP_SRPL_ITEMS_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x7A) + +/** @def ESP_BLE_MESH_MODEL_SRPL_SRV + * + * @brief Define a new Solicitation PDU RPL Configuration Server model. + * + * @note The Solicitation PDU RPL Configuration Server model extends + * the On-Demand Private Proxy Server model. + * If the model is supported, the model shall be supported by a + * primary element and shall not be supported by any secondary + * elements. + * + * @param srv_data Pointer to a unique Solicitation PDU RPL Configuration Server + * model user_data. + * + * @return New Solicitation PDU RPL Configuration Server model instance. + */ +#define ESP_BLE_MESH_MODEL_SRPL_SRV(srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SRPL_SRV, \ + NULL, NULL, srv_data) + +/** @def ESP_BLE_MESH_MODEL_SRPL_CLI + * + * @brief Define a new Solicitation PDU RPL Configuration Client model. + * + * @note If supported, the model shall be supported by the primary + * element and shall not be supported by any secondary elements. + * + * @param cli_data Pointer to a unique Solicitation PDU RPL Configuration Client + * model user_data. + * + * @return New Solicitation PDU RPL Configuration Client model instance. + */ +#define ESP_BLE_MESH_MODEL_SRPL_CLI(cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SRPL_CLI, \ + NULL, NULL, cli_data) + +/** Solicitation PDU RPL Configuration Server model context */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to Solicitation PDU RPL Configuration Server model */ + + /* This model does not define any states. */ +} esp_ble_mesh_srpl_srv_t; + +/** Parameter of Solicitation PDU RPL Items Clear */ +typedef struct { + esp_ble_mesh_uar_t addr_range; /*!< Unicast address range */ +} esp_ble_mesh_srpl_items_clear_t; + +/** + * @brief Solicitation PDU RPL Configuration Client model message union + */ +typedef union { + esp_ble_mesh_srpl_items_clear_t srpl_items_clear; /*!< For ESP_BLE_MESH_MODEL_OP_SRPL_ITEMS_CLEAR */ +} esp_ble_mesh_srpl_client_msg_t; + +/** Parameter of Solicitation PDU RPL Items Clear Status */ +typedef struct { + esp_ble_mesh_uar_t addr_range; /*!< Unicast address range */ +} esp_ble_mesh_srpl_items_status_t; + +/** Result of sending Solicitation PDU RPL Configuration Client messages */ +typedef struct { + int err_code; /*!< Result of sending a message */ +} esp_ble_mesh_srpl_client_send_cb_t; + +/** + * @brief Solicitation PDU RPL Configuration Client model received message union + */ +typedef union { + esp_ble_mesh_srpl_items_status_t srpl_items_status; /*!< For ESP_BLE_MESH_MODEL_OP_SRPL_ITEMS_STATUS */ +} esp_ble_mesh_srpl_client_recv_cb_t; + +/** Solicitation PDU RPL Configuration Client model callback parameters */ +typedef struct { + esp_ble_mesh_client_common_param_t *params; /*!< Client common parameters, used by all events. */ + /** Union of SRPL Client callback */ + union { + esp_ble_mesh_srpl_client_send_cb_t send; /*!< Result of sending a message */ + esp_ble_mesh_srpl_client_recv_cb_t recv; /*!< Parameters of received status message */ + }; +} esp_ble_mesh_srpl_client_cb_param_t; /*!< Solicitation PDU RPL Configuration Client model callback parameters */ + +/** This enum value is the event of Solicitation PDU RPL Configuration Client model */ +typedef enum { + ESP_BLE_MESH_SRPL_CLIENT_SEND_COMP_EVT, + ESP_BLE_MESH_SRPL_CLIENT_SEND_TIMEOUT_EVT, + ESP_BLE_MESH_SRPL_CLIENT_RECV_RSP_EVT, + ESP_BLE_MESH_SRPL_CLIENT_RECV_PUB_EVT, + ESP_BLE_MESH_SRPL_CLIENT_EVT_MAX, +} esp_ble_mesh_srpl_client_cb_event_t; + +/** + * @brief Solicitation PDU RPL Configuration Server model related context. + */ + +/** + * @brief Solicitation PDU RPL Configuration Server model state change value union + */ +typedef union { + uint8_t dummy; /*!< Currently this event is not used. */ +} esp_ble_mesh_srpl_server_state_change_t; + +/** + * @brief Solicitation PDU RPL Configuration Server model callback value union + */ +typedef union { + esp_ble_mesh_srpl_server_state_change_t state_change; /*!< ESP_BLE_MESH_SRPL_SERVER_STATE_CHANGE_EVT */ +} esp_ble_mesh_srpl_server_cb_value_t; + +/** Solicitation PDU RPL Configuration Server model callback parameters */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + esp_ble_mesh_msg_ctx_t ctx; /*!< Context of the received message */ + esp_ble_mesh_srpl_server_cb_value_t value; /*!< Value of the received configuration messages */ +} esp_ble_mesh_srpl_server_cb_param_t; + +/** This enum value is the event of Solicitation PDU RPL Configuration Server model */ +typedef enum { + ESP_BLE_MESH_SRPL_SERVER_STATE_CHANGE_EVT, + ESP_BLE_MESH_SRPL_SERVER_EVT_MAX, +} esp_ble_mesh_srpl_server_cb_event_t; + +/** + * @brief Bluetooth Mesh Solicitation PDU RPL Configuration client and server model functions. + */ + +/** + * @brief Solicitation PDU RPL Configuration Client model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_srpl_client_cb_t)(esp_ble_mesh_srpl_client_cb_event_t event, + esp_ble_mesh_srpl_client_cb_param_t *param); + +/** + * @brief Register BLE Mesh Solicitation PDU RPL Configuration Client model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_srpl_client_callback(esp_ble_mesh_srpl_client_cb_t callback); + +/** + * @brief Set the value of Solicitation PDU RPL Configuration Server model state with the corresponding set message. + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] msg: Pointer to Solicitation PDU RPL Configuration Client message. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_srpl_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_srpl_client_msg_t *msg); + + +/** + * @brief Solicitation PDU RPL Configuration Server model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_srpl_server_cb_t)(esp_ble_mesh_srpl_server_cb_event_t event, + esp_ble_mesh_srpl_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh Solicitation PDU RPL Configuration Server model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_srpl_server_callback(esp_ble_mesh_srpl_server_cb_t callback); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_SRPL_MODEL_API_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/models/esp_ble_mesh_mbt_model_api.c b/components/bt/esp_ble_mesh/v1.1/api/models/esp_ble_mesh_mbt_model_api.c new file mode 100644 index 000000000000..5760a4ce93b9 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/models/esp_ble_mesh_mbt_model_api.c @@ -0,0 +1,421 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_mbt_model.h" +#include "esp_ble_mesh_mbt_model_api.h" + +#if CONFIG_BLE_MESH_MBT_CLI + +extern const void *bt_mesh_get_blob_receiver(void *model, uint16_t unicast_addr); +extern const void **bt_mesh_get_active_blob_receiver(void *model); +extern int bt_mesh_get_transfer_progress(void *model, uint16_t unicast_addr, + uint8_t *block_percent, uint8_t *chunk_percent); +extern int bt_mesh_get_blob_reception_progress(void *model, uint8_t *reception_progress); + +esp_err_t esp_ble_mesh_register_blob_transfer_client_callback(esp_ble_mesh_mbt_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_MBT_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_client_retrieve_capabilities(esp_ble_mesh_retrieve_capabilities_t *input) +{ + btc_ble_mesh_mbt_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (input == NULL || input->model == NULL || + (input->unicast_addr_count && input->unicast_addr == 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_MBT_CLIENT; + msg.act = BTC_BLE_MESH_ACT_MBT_CLIENT_RETRIEVE_CAPABILITIES; + + memcpy(&arg.retrieve_capabilities, input, sizeof(esp_ble_mesh_retrieve_capabilities_t)); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_client_args_t), + btc_ble_mesh_mbt_client_arg_deep_copy, + btc_ble_mesh_mbt_client_arg_deep_free) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_client_transfer_blob(esp_ble_mesh_transfer_blob_t *input) +{ + btc_ble_mesh_mbt_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (input == NULL || input->model == NULL || + (input->unicast_addr_count && input->unicast_addr == NULL) || + input->blob_data == 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_MBT_CLIENT; + msg.act = BTC_BLE_MESH_ACT_MBT_CLIENT_TRANSFER_BLOB; + + memcpy(&arg.transfer_blob, input, sizeof(esp_ble_mesh_transfer_blob_t)); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_client_args_t), + btc_ble_mesh_mbt_client_arg_deep_copy, + btc_ble_mesh_mbt_client_arg_deep_free) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_client_send_block(esp_ble_mesh_send_block_t *input) +{ + btc_ble_mesh_mbt_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (input == NULL || input->model == 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_MBT_CLIENT; + msg.act = BTC_BLE_MESH_ACT_MBT_CLIENT_SEND_BLOCK; + + memcpy(&arg.send_block, input, sizeof(esp_ble_mesh_send_block_t)); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_client_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_client_send_data(esp_ble_mesh_send_data_t *input) +{ + btc_ble_mesh_mbt_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (input == NULL || input->model == 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_MBT_CLIENT; + msg.act = BTC_BLE_MESH_ACT_MBT_CLIENT_SEND_DATA; + + memcpy(&arg.send_data, input, sizeof(esp_ble_mesh_send_data_t)); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_client_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_client_determine_block_status(esp_ble_mesh_determine_block_status_t *input) +{ + btc_ble_mesh_mbt_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (input == NULL || input->model == 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_MBT_CLIENT; + msg.act = BTC_BLE_MESH_ACT_MBT_CLIENT_DETERMINE_BLOCK_STATUS; + + memcpy(&arg.determine_block_status, input, sizeof(esp_ble_mesh_determine_block_status_t)); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_client_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_client_determine_transfer_status(esp_ble_mesh_determine_transfer_status_t *input) +{ + btc_ble_mesh_mbt_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (input == NULL || input->model == NULL || + (input->unicast_addr_count && input->unicast_addr == 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_MBT_CLIENT; + msg.act = BTC_BLE_MESH_ACT_MBT_CLIENT_DETERMINE_TRANSFER_STATUS; + + memcpy(&arg.determine_transfer_status, input, sizeof(esp_ble_mesh_determine_transfer_status_t)); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_client_args_t), + btc_ble_mesh_mbt_client_arg_deep_copy, + btc_ble_mesh_mbt_client_arg_deep_free) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_client_cancel_transfer(esp_ble_mesh_cancel_transfer_t *input) +{ + btc_ble_mesh_mbt_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (input == NULL || input->model == NULL || + (input->unicast_addr_count && input->unicast_addr == 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_MBT_CLIENT; + msg.act = BTC_BLE_MESH_ACT_MBT_CLIENT_CANCEL_TRANSFER; + + memcpy(&arg.cancel_transfer, input, sizeof(esp_ble_mesh_cancel_transfer_t)); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_client_args_t), + btc_ble_mesh_mbt_client_arg_deep_copy, + btc_ble_mesh_mbt_client_arg_deep_free) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +const esp_ble_mesh_blob_receiver_t *esp_ble_mesh_mbt_client_get_blob_receiver(esp_ble_mesh_model_t *model, + uint16_t unicast_addr) +{ + return (const esp_ble_mesh_blob_receiver_t *)bt_mesh_get_blob_receiver((struct bt_mesh_model *)model, + unicast_addr); +} + +const esp_ble_mesh_blob_receiver_t **esp_ble_mesh_mbt_client_get_active_blob_receiver(esp_ble_mesh_model_t *model) +{ + return (const esp_ble_mesh_blob_receiver_t **)bt_mesh_get_active_blob_receiver((struct bt_mesh_model *)model); +} + +esp_err_t esp_ble_mesh_mbt_client_get_transfer_progress(esp_ble_mesh_model_t *model, + uint16_t unicast_addr, + uint8_t *block_percent, + uint8_t *chunk_percent) +{ + return (bt_mesh_get_transfer_progress((struct bt_mesh_model *)model, unicast_addr, + block_percent, chunk_percent) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_client_set_transfer_ttl(esp_ble_mesh_model_t *model, + uint8_t transfer_ttl) +{ + btc_ble_mesh_mbt_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (model == 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_MBT_CLIENT; + msg.act = BTC_BLE_MESH_ACT_MBT_CLIENT_SET_TRANSFER_TTL; + + arg.set_transfer_ttl.model = model; + arg.set_transfer_ttl.transfer_ttl = transfer_ttl; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_client_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_client_clear_transfer_ttl(esp_ble_mesh_model_t *model) +{ + btc_ble_mesh_mbt_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (model == 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_MBT_CLIENT; + msg.act = BTC_BLE_MESH_ACT_MBT_CLIENT_CLEAR_TRANSFER_TTL; + + arg.clear_transfer_ttl.model = model; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_client_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_client_set_app_idx(esp_ble_mesh_model_t *model, + uint16_t app_idx) +{ + btc_ble_mesh_mbt_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (model == 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_MBT_CLIENT; + msg.act = BTC_BLE_MESH_ACT_MBT_CLIENT_SET_APP_IDX; + + arg.set_app_idx.model = model; + arg.set_app_idx.app_idx = app_idx; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_client_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_client_clear_app_idx(esp_ble_mesh_model_t *model) +{ + btc_ble_mesh_mbt_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (model == 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_MBT_CLIENT; + msg.act = BTC_BLE_MESH_ACT_MBT_CLIENT_CLEAR_APP_IDX; + + arg.clear_app_idx.model = model; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_client_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_client_set_multicast_addr(esp_ble_mesh_model_t *model, + uint16_t multicast_addr) +{ + btc_ble_mesh_mbt_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (model == 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_MBT_CLIENT; + msg.act = BTC_BLE_MESH_ACT_MBT_CLIENT_SET_MULTICAST_ADDR; + + arg.set_multicast_addr.model = model; + arg.set_multicast_addr.multicast_addr = multicast_addr; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_client_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_client_clear_multicast_addr(esp_ble_mesh_model_t *model) +{ + btc_ble_mesh_mbt_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (model == 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_MBT_CLIENT; + msg.act = BTC_BLE_MESH_ACT_MBT_CLIENT_CLEAR_MULTICAST_ADDR; + + arg.clear_multicast_addr.model = model; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_client_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_MBT_CLI */ + +#if CONFIG_BLE_MESH_MBT_SRV +esp_err_t esp_ble_mesh_register_mbt_server_callback(esp_ble_mesh_mbt_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_MBT_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_server_initialize_blob_receive(esp_ble_mesh_initialize_blob_receive_t *input) +{ + btc_ble_mesh_mbt_server_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (input == NULL || input->model == 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_MBT_SERVER; + msg.act = BTC_BLE_MESH_ACT_MBT_SERVER_INITIALIZE_BLOB_RECEIVE; + + memcpy(&arg.initialize_blob_receive, input, sizeof(esp_ble_mesh_initialize_blob_receive_t)); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_server_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_server_cancel_blob_receive(esp_ble_mesh_cancel_blob_receive_t *input) +{ + btc_ble_mesh_mbt_server_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (input == NULL || input->model == 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_MBT_SERVER; + msg.act = BTC_BLE_MESH_ACT_MBT_SERVER_CANCEL_BLOB_RECEIVE; + + memcpy(&arg.cancel_blob_receive, input, sizeof(esp_ble_mesh_cancel_blob_receive_t)); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_server_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_server_set_blob_capabilities(esp_ble_mesh_set_blob_capabilities_t *input) +{ + btc_ble_mesh_mbt_server_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (input == NULL || input->model == 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_MBT_SERVER; + msg.act = BTC_BLE_MESH_ACT_MBT_SERVER_SET_BLOB_CAPABILITIES; + + memcpy(&arg.set_blob_capabilities, input, sizeof(esp_ble_mesh_set_blob_capabilities_t)); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_mbt_server_args_t), NULL, NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_mbt_server_get_blob_reception_progress(esp_ble_mesh_model_t *model, + uint8_t *reception_progress) +{ + return (bt_mesh_get_blob_reception_progress((struct bt_mesh_model *)model, + reception_progress) == 0 ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_MBT_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/api/models/include/esp_ble_mesh_mbt_model_api.h b/components/bt/esp_ble_mesh/v1.1/api/models/include/esp_ble_mesh_mbt_model_api.h new file mode 100644 index 000000000000..87e51948ae82 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/api/models/include/esp_ble_mesh_mbt_model_api.h @@ -0,0 +1,693 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ESP_BLE_MESH_MBT_MODEL_API_H_ +#define _ESP_BLE_MESH_MBT_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ESP_BLE_MESH_MODEL_OP_BLOB_TRANSFER_GET ESP_BLE_MESH_MODEL_OP_2(0x83, 0x00) +#define ESP_BLE_MESH_MODEL_OP_BLOB_TRANSFER_START ESP_BLE_MESH_MODEL_OP_2(0x83, 0x01) +#define ESP_BLE_MESH_MODEL_OP_BLOB_TRANSFER_CANCEL ESP_BLE_MESH_MODEL_OP_2(0x83, 0x02) +#define ESP_BLE_MESH_MODEL_OP_BLOB_TRANSFER_STATUS ESP_BLE_MESH_MODEL_OP_2(0x83, 0x03) +#define ESP_BLE_MESH_MODEL_OP_BLOB_BLOCK_GET ESP_BLE_MESH_MODEL_OP_2(0x83, 0x05) +#define ESP_BLE_MESH_MODEL_OP_BLOB_BLOCK_START ESP_BLE_MESH_MODEL_OP_2(0x83, 0x04) +#define ESP_BLE_MESH_MODEL_OP_BLOB_PARTIAL_BLOCK_REPORT ESP_BLE_MESH_MODEL_OP_1(0x65) +#define ESP_BLE_MESH_MODEL_OP_BLOB_BLOCK_STATUS ESP_BLE_MESH_MODEL_OP_1(0x67) +#define ESP_BLE_MESH_MODEL_OP_BLOB_CHUNK_TRANSFER ESP_BLE_MESH_MODEL_OP_1(0x66) +#define ESP_BLE_MESH_MODEL_OP_BLOB_INFORMATION_GET ESP_BLE_MESH_MODEL_OP_2(0x83, 0x06) +#define ESP_BLE_MESH_MODEL_OP_BLOB_INFORMATION_STATUS ESP_BLE_MESH_MODEL_OP_2(0x83, 0x07) + +#define ESP_BLE_MESH_BLOB_ID_SIZE 8 + +/** @def ESP_BLE_MESH_MODEL_MBT_CLI + * + * @brief Define a new BLOB Transfer Client model. + * + * @note This API needs to be called for each element on which + * the application needs to have a BLOB Transfer Client model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New BLOB Transfer Client model instance. + */ +#define ESP_BLE_MESH_MODEL_MBT_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_MBT_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_MBT_SRV + * + * @brief Define a new BLOB Transfer Server model. + * + * @note This API needs to be called for each element on which + * the application needs to have a BLOB Transfer Server model. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_blob_trans_srv_t. + * + * @return New BLOB Transfer Server model instance. + */ +#define ESP_BLE_MESH_MODEL_MBT_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_MBT_SRV, \ + NULL, srv_pub, srv_data) + +/** BLOB Transfer Server model context */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to BLOB Transfer Server model */ +} esp_ble_mesh_mbt_srv_t; + +/** Parameters of BLOB receiver */ +typedef struct { + uint16_t unicast_addr; /*!< Unicast address of the server */ + uint8_t retrieved_transfer_phase; /*!< Retrieved transfer phase of the server */ + uint8_t status:4; /*!< Status of the last operation */ + uint16_t blocks_not_received_len; /*!< Indicates the length which blocks were not received by the server. */ + uint8_t *blocks_not_received; /*!< Indicates which blocks were not received by the server. */ + uint16_t missing_chunks_len; /*!< Indicates which chunks were not received in the current block */ + uint8_t *missing_chunks; /*!< Indicates which chunks were not received by the server in the current block */ +/* The followings are the additional information contained in status messages. */ + uint8_t transfer_mode:2; /*!< BLOB transfer mode */ + uint8_t expected_blob_id[ESP_BLE_MESH_BLOB_ID_SIZE]; /*!< Expected BLOB identifier list */ + uint32_t blob_size; /*!< BLOB size in octets */ + uint8_t block_size_log; /*!< Indicates the block size */ + uint16_t transfer_mtu_size; /*!< MTU size in octets */ + bool block_status_recv; /*!< Indicate if Blob Block Status is received as a response. */ +} esp_ble_mesh_blob_receiver_t; /*!< Structure of BLOB receiver */ + +/** Parameters of BLOB Information Status */ +typedef struct { + uint8_t min_block_size_log; /*!< Min Block Size Log */ + uint8_t max_block_size_log; /*!< Max Block Size Log */ + uint16_t max_total_chunks; /*!< Max Total Chunks */ + uint16_t max_chunk_size; /*!< Max Chunk Size */ + uint32_t max_blob_size; /*!< Max BLOB Size */ + uint16_t server_mtu_size; /*!< Server MTU size */ + uint8_t supported_transfer_mode; /*!< Supported Transfer Mode */ +} esp_ble_mesh_blob_capabilities_t; /*!< Parameters of BLOB Information Status */ + +/** Parameters of BLOB retrieve capabilities */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to BLOB Transfer Server model */ + uint8_t msg_role; /*!< Role of the device - Node/Provisioner */ + + uint16_t multicast_addr; /*!< Multicast Address state */ + uint16_t app_idx; /*!< AppKey Index state */ + uint8_t transfer_ttl; /*!< Transfer TTL state */ + uint8_t unicast_addr_count; /*!< The count of unicast address */ + uint16_t *unicast_addr; /*!< Unlicast address list */ +} esp_ble_mesh_retrieve_capabilities_t; /*!< Parameters of BLOB retrieve capabilities */ + +/** Parameters of BLOB transfer */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to BLOB Transfer Server model */ + uint8_t msg_role; /*!< Role of the device - Node/Provisioner */ + + uint8_t unicast_addr_count; /*!< The count of unicast address */ + uint16_t *unicast_addr; /*!< Unlicast address list */ + uint16_t multicast_addr; /*!< Multicast Address state */ + uint16_t app_idx; /*!< AppKey Index state */ + uint8_t transfer_ttl; /*!< Transfer TTL state */ + uint8_t blob_id[ESP_BLE_MESH_BLOB_ID_SIZE]; /*!< BLOB identifier list */ + uint32_t blob_size; /*!< BLOB size in octets */ + uint8_t *blob_data; /*!< BLOB data */ + uint8_t transfer_mode; /*!< BLOB transfer mode */ + uint16_t client_timeout_base; /*!< Time wait for messages from the serve */ +} esp_ble_mesh_transfer_blob_t; /*!< Parameters of BLOB transfer */ + +/** Parameters of BLOB Block Status message */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to BLOB Transfer Server model */ + uint8_t msg_role; /*!< Role of the device - Node/Provisioner */ + + uint16_t multicast_addr; /*!< Multicast Address state */ + uint16_t app_idx; /*!< AppKey Index state */ + uint8_t transfer_ttl; /*!< Transfer TTL state */ + + uint16_t block_number; /*!< Block number of the current block */ + uint16_t chunk_size; /*!< Chunk Size (in octets) for the current block */ +} esp_ble_mesh_send_block_t; /*!< BLOB Block Status message structure */ + +/** Parameters of BLOB send message */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to BLOB Transfer Server model */ + uint8_t msg_role; /*!< Role of the device - Node/Provisioner */ + + uint16_t multicast_addr; /*!< Multicast Address state */ + uint16_t app_idx; /*!< AppKey Index state */ + uint8_t transfer_ttl; /*!< Transfer TTL state */ +} esp_ble_mesh_send_data_t; /*!< Parameters of BLOB send message */ + +/** Parameters of determine Block Status message */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to BLOB Transfer Server model */ + uint8_t msg_role; /*!< Role of the device - Node/Provisioner */ + + uint16_t multicast_addr; /*!< Multicast Address state */ + uint16_t app_idx; /*!< AppKey Index state */ + uint8_t transfer_ttl; /*!< Transfer TTL state */ +} esp_ble_mesh_determine_block_status_t; /*!< Parameters of determine Block Status message */ + +/** Parameters of determine Block Status message */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to BLOB Transfer Server model */ + uint8_t msg_role; /*!< Role of the device - Node/Provisioner */ + + uint16_t multicast_addr; /*!< Multicast Address state */ + uint16_t app_idx; /*!< AppKey Index state */ + uint8_t transfer_ttl; /*!< Transfer TTL state */ + uint8_t unicast_addr_count; /*!< The count of unicast address */ + uint16_t *unicast_addr; /*!< Unlicast address list */ +} esp_ble_mesh_determine_transfer_status_t; /*!< Parameters of determine Block Status message */ + +/** Parameters of cancel transfer message */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to BLOB Transfer Server model */ + uint8_t msg_role; /*!< Role of the device - Node/Provisioner */ + + uint16_t multicast_addr; /*!< Multicast Address state */ + uint16_t app_idx; /*!< AppKey Index state */ + uint8_t transfer_ttl; /*!< Transfer TTL state */ + uint8_t unicast_addr_count; /*!< The count of unicast address */ + uint16_t *unicast_addr; /*!< Unlicast address list */ + + uint8_t blob_id[ESP_BLE_MESH_BLOB_ID_SIZE]; /*!< BLOB identifier list */ +} esp_ble_mesh_cancel_transfer_t; /*!< Parameters of cancel transfer message */ + +/** + * @brief BLOB Transfer Client model procedure result + */ +#define ESP_BLE_MESH_MBT_CLIENT_RESULT_COMPLETE 0x00 +#define ESP_BLE_MESH_MBT_CLIENT_RESULT_FAIL 0x01 + +/** + * @brief BLOB Transfer Client model callback values union + */ +typedef union { + /** Retrieve capabilities status */ + struct { + int error_code; /*!< Result of starting Retrieve Capabilities procedure */ + esp_ble_mesh_retrieve_capabilities_t input; /*!< Input of starting Retrieve Capabilities procedure */ + } retrieve_capabilities_status; /*!< Retrieve capabilities status */ + /** Transfer BLOB status */ + struct { + int error_code; /*!< Result of starting Transfer BLOB procedure */ + esp_ble_mesh_transfer_blob_t input; /*!< Input of starting Transfer BLOB procedure */ + } transfer_blob_status; /*!< Transfer BLOB status */ + /** Send block status */ + struct { + int error_code; /*!< Result of starting Send Block sub-procedure */ + esp_ble_mesh_send_block_t input; /*!< Input of starting Send Block sub-procedure */ + } send_block_status; /*!< Send block status */ + /** Send data status */ + struct { + int error_code; /*!< Result of starting Send Data sub-procedure */ + esp_ble_mesh_send_data_t input; /*!< Input of starting Send Data sub-procedure */ + } send_data_status; /*!< Send data status */ + /** Determine block status */ + struct { + int error_code; /*!< Result of starting Determine Block Status sub-procedure */ + esp_ble_mesh_determine_block_status_t input; /*!< Input of starting Determine Block Status sub-procedure */ + } determine_block_status_status; /*!< Determine block status */ + /** Determine transfer status */ + struct { + int error_code; /*!< Result of starting Determine Transfer Status procedure */ + esp_ble_mesh_determine_transfer_status_t input; /*!< Input of starting Determine Transfer Status procedure */ + } determine_transfer_status_status; /*!< Determine transfer status */ + /** Cancel transfer status */ + struct { + int error_code; /*!< Result of starting Cancel Transfer procedure */ + esp_ble_mesh_cancel_transfer_t input; /*!< Input of starting Cancel Transfer procedure */ + } cancel_transfer_status; /*!< Cancel transfer status */ + /** Retrieve capabilities complete */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + uint8_t result; /*!< Result of Retrieve Capabilities procedure */ + } retrieve_capabilities_comp; /*!< Retrieve capabilities complete */ + /** Transfer BLOB complete */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + uint8_t result; /*!< Result of Transfer BLOB procedure */ + } transfer_blob_comp; /*!< Transfer BLOB complete */ + /** Send block complete */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + uint8_t result; /*!< Result of Send Block sub-procedure */ + } send_block_comp; /*!< Send block complete */ + /** Send data complete */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + uint8_t result; /*!< Result of Send Data sub-procedure */ + } send_data_comp; /*!< Send data complete */ + /** Determine block status complete */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + uint8_t result; /*!< Result of Determine Block Status sub-procedure */ + } determine_block_status_comp; /*!< Determine block status complete */ + /** Determine transfer status complete */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + uint8_t result; /*!< Result of Determine Transfer Status procedure */ + } determine_transfer_status_comp; /*!< Determine transfer status complete */ + /** Cancel transfer complete */ + struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + uint8_t result; /*!< Result of Cancel Transfer procedure */ + } cancel_transfer_comp; /*!< Cancel transfer complete */ + /** Set transfer TTL */ + struct { + int error_code; /*!< Result of setting Transfer TTL state */ + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + uint8_t transfer_ttl; /*!< Transfer TTL state */ + } set_transfer_ttl; /*!< Set transfer TTL */ + /** Clear transfer TTL */ + struct { + int error_code; /*!< Result of clearing Transfer TTL state */ + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + } clear_transfer_ttl; /*!< Clear transfer TTL */ + /** Set application index */ + struct { + int error_code; /*!< Result of setting AppKey Index state */ + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + uint16_t app_idx; /*!< AppKey Index state */ + } set_app_idx; /*!< Set application index */ + /** Clear application index */ + struct { + int error_code; /*!< Result of clearing AppKey Index state */ + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + } clear_app_idx; /*!< Clear application index */ + /** Set multicast address */ + struct { + int error_code; /*!< Result of setting Multicast Address state */ + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + uint16_t multicast_addr; /*!< Multicast Address state */ + } set_multicast_addr; /*!< Set multicast address */ + /** Clear multicast address */ + struct { + int error_code; /*!< Result of clearing Multicast Address state */ + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + } clear_multicast_addr; /*!< Clear multicast address */ +} esp_ble_mesh_mbt_client_cb_value_t; /*!< BLOB Transfer Client model callback values union */ + +/** BLOB Transfer Client model callback parameters */ +typedef struct { + esp_ble_mesh_mbt_client_cb_value_t value; /*!< BLOB Transfer Client model callback values */ +} esp_ble_mesh_mbt_client_cb_param_t; /*!< BLOB Transfer Client model callback parameters */ + +/** + * This enum value is the event of BLOB Transfer Client model. + * Note: The idea of status/complete event comes from HCI Commands. + */ +typedef enum { + ESP_BLE_MESH_MBT_CLIENT_RETRIEVE_CAPABILITIES_STATUS_EVT, + ESP_BLE_MESH_MBT_CLIENT_TRANSFER_BLOB_STATUS_EVT, + ESP_BLE_MESH_MBT_CLIENT_SEND_BLOCK_STATUS_EVT, + ESP_BLE_MESH_MBT_CLIENT_SEND_DATA_STATUS_EVT, + ESP_BLE_MESH_MBT_CLIENT_DETERMINE_BLOCK_STATUS_STATUS_EVT, + ESP_BLE_MESH_MBT_CLIENT_DETERMINE_TRANSFER_STATUS_STATUS_EVT, + ESP_BLE_MESH_MBT_CLIENT_CANCEL_TRANSFER_STATUS_EVT, + ESP_BLE_MESH_MBT_CLIENT_RETRIEVE_CAPABILITIES_COMP_EVT, + ESP_BLE_MESH_MBT_CLIENT_TRANSFER_BLOB_COMP_EVT, + ESP_BLE_MESH_MBT_CLIENT_SEND_BLOCK_COMP_EVT, + ESP_BLE_MESH_MBT_CLIENT_SEND_DATA_COMP_EVT, + ESP_BLE_MESH_MBT_CLIENT_DETERMINE_BLOCK_STATUS_COMP_EVT, + ESP_BLE_MESH_MBT_CLIENT_DETERMINE_TRANSFER_STATUS_COMP_EVT, + ESP_BLE_MESH_MBT_CLIENT_CANCEL_TRANSFER_COMP_EVT, + ESP_BLE_MESH_MBT_CLIENT_SET_TRANSFER_TTL_COMP_EVT, + ESP_BLE_MESH_MBT_CLIENT_CLEAR_TRANSFER_TTL_COMP_EVT, + ESP_BLE_MESH_MBT_CLIENT_SET_APP_IDX_COMP_EVT, + ESP_BLE_MESH_MBT_CLIENT_CLEAR_APP_IDX_COMP_EVT, + ESP_BLE_MESH_MBT_CLIENT_SET_MULTICAST_ADDR_COMP_EVT, + ESP_BLE_MESH_MBT_CLIENT_CLEAR_MULTICAST_ADDR_COMP_EVT, + ESP_BLE_MESH_MBT_CLIENT_EVT_MAX, +} esp_ble_mesh_mbt_client_cb_event_t; + +/** Parameters of initialize BLOB receive */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + + uint8_t blob_id[ESP_BLE_MESH_BLOB_ID_SIZE]; /*!< BLOB identifier list */ + uint16_t timeout; /*!< Timeout */ + uint8_t transfer_ttl; /*!< Transfer TTL state */ +} esp_ble_mesh_initialize_blob_receive_t; /*!< Structure of initialize BLOB receive */ + +/** Parameters of cancel BLOB receive */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + + uint8_t blob_id[ESP_BLE_MESH_BLOB_ID_SIZE]; /*!< BLOB identifier list */ +} esp_ble_mesh_cancel_blob_receive_t;/*!< */ + +/** Parameters of cancel BLOB receive */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the BLOB Transfer Client model */ + + esp_ble_mesh_blob_capabilities_t caps; /*!< Parameters of BLOB Information Status */ +} esp_ble_mesh_set_blob_capabilities_t;/*!< */ + +/** + * @brief BLOB Transfer Server model callback value union + */ +typedef union { + /** Initialize BLOB receive complete */ + struct { + int error_code; /*!< Result of initializing BLOB receive */ + esp_ble_mesh_initialize_blob_receive_t input; /*!< Input of initializing BLOB receive */ + } initialize_blob_receive_comp; /*!< Initialize BLOB receive complete */ + /** Cancel BLOB receive complete */ + struct { + int error_code; /*!< Result of canceling BLOB receive */ + esp_ble_mesh_cancel_blob_receive_t input; /*!< Input of canceling BLOB receive */ + } cancel_blob_receive_comp; /*!< Cancel BLOB receive complete */ + /** Set BLOB capabilities complete */ + struct { + int error_code; /*!< Result of setting BLOB capabilities */ + esp_ble_mesh_set_blob_capabilities_t input; /*!< Input of setting BLOB capabilities */ + } set_blob_capabilities_comp; /*!< Set BLOB capabilities complete */ + /** BLOB transfer get */ + struct { + esp_ble_mesh_msg_ctx_t ctx; /*!< BLOB Transfer Get message context */ + } blob_transfer_get; /*!< BLOB transfer get */ + /** BLOB transfer start */ + struct { + esp_ble_mesh_msg_ctx_t ctx; /*!< BLOB Transfer Start message context */ + } blob_transfer_start; /*!< BLOB transfer start */ + /** BLOB transfer cancel */ + struct { + esp_ble_mesh_msg_ctx_t ctx; /*!< BLOB Transfer Cancel message context */ + } blob_transfer_cancel; /*!< BLOB transfer cancel */ + /** BLOB block get */ + struct { + esp_ble_mesh_msg_ctx_t ctx; /*!< BLOB Block Get message context */ + } blob_block_get; /*!< BLOB block get */ + /** BLOB block start */ + struct { + esp_ble_mesh_msg_ctx_t ctx; /*!< BLOB Block Start message context */ + } blob_block_start; /*!< BLOB block start */ + /** BLOB chunk transfer */ + struct { + esp_ble_mesh_msg_ctx_t ctx; /*!< BLOB Chunk Transfer message context */ + } blob_chunk_transfer; /*!< BLOB chunk transfer */ + /** BLOB information get */ + struct { + esp_ble_mesh_msg_ctx_t ctx; /*!< BLOB Information Get message context */ + } blob_information_get; /*!< BLOB information get */ + /** Block receive complete */ + struct { + esp_ble_mesh_msg_ctx_t ctx; /*!< Information of receiving BLOB block completely */ + } block_receive_comp; /*!< Block receive complete */ + /** BLOB receive complete */ + struct { + esp_ble_mesh_msg_ctx_t ctx; /*!< Information of receiving BLOB completely */ + } blob_receive_comp; /*!< BLOB receive complete */ +} esp_ble_mesh_mbt_server_cb_value_t; /*!< BLOB Transfer Server model callback value union */ + +/** BLOB Transfer Server model callback parameters */ +typedef struct { + esp_ble_mesh_mbt_server_cb_value_t value; /*!< Value of the received blob transfer messages */ +} esp_ble_mesh_mbt_server_cb_param_t; /*!< BLOB Transfer Server model callback parameters */ + +/** This enum value is the event of BLOB Transfer Server model */ +typedef enum { + ESP_BLE_MESH_MBT_SERVER_INITIALIZE_BLOB_RECEIVE_COMP_EVT, + ESP_BLE_MESH_MBT_SERVER_CANCEL_BLOB_RECEIVE_COMP_EVT, + ESP_BLE_MESH_MBT_SERVER_SET_BLOB_CAPABILITIES_COMP_EVT, + ESP_BLE_MESH_MBT_SERVER_BLOB_TRANSFER_GET_EVT, + ESP_BLE_MESH_MBT_SERVER_BLOB_TRANSFER_START_EVT, + ESP_BLE_MESH_MBT_SERVER_BLOB_TRANSFER_CANCEL_EVT, + ESP_BLE_MESH_MBT_SERVER_BLOB_BLOCK_GET_EVT, + ESP_BLE_MESH_MBT_SERVER_BLOB_BLOCK_START_EVT, + ESP_BLE_MESH_MBT_SERVER_BLOB_CHUNK_TRANSFER_EVT, + ESP_BLE_MESH_MBT_SERVER_BLOB_INFORMATION_GET_EVT, + ESP_BLE_MESH_MBT_SERVER_BLOCK_RECEIVE_COMP_EVT, + ESP_BLE_MESH_MBT_SERVER_BLOB_RECEIVE_COMP_EVT, + ESP_BLE_MESH_MBT_SERVER_BLOB_RECEIVE_TIMEOUT_EVT, + ESP_BLE_MESH_MBT_SERVER_EVT_MAX, +} esp_ble_mesh_mbt_server_cb_event_t; + +/** + * @brief BLOB Transfer Client model callback function type + * + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_mbt_client_cb_t)(esp_ble_mesh_mbt_client_cb_event_t event, + esp_ble_mesh_mbt_client_cb_param_t *param); + +/** + * @brief BLOB Transfer Server model callback function type + * + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_mbt_server_cb_t)(esp_ble_mesh_mbt_server_cb_event_t event, + esp_ble_mesh_mbt_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh BLOB Transfer Client model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_mbt_client_callback(esp_ble_mesh_mbt_client_cb_t callback); + +/** + * @brief Register BLE Mesh BLOB Transfer Server model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_mbt_server_callback(esp_ble_mesh_mbt_server_cb_t callback); + +/** + * @brief BLOB Transfer Client starts Retrieve Capabilities procedure. + * + * @param[in] input: The input of Retrieve Capabilities procedure. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_retrieve_capabilities(esp_ble_mesh_retrieve_capabilities_t *input); + +/** + * @brief BLOB Transfer Client starts Transfer BLOB procedure. + * + * @param[in] input: The input of Transfer BLOB procedure. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_transfer_blob(esp_ble_mesh_transfer_blob_t *input); + +/** + * @brief BLOB Transfer Client starts Send Block sub-procedure. + * + * @param[in] input: The input of Send Block sub-procedure. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_send_block(esp_ble_mesh_send_block_t *input); + +/** + * @brief BLOB Transfer Client starts Send Data sub-procedure. + * + * @param[in] input: The input of Send Data sub-procedure. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_send_data(esp_ble_mesh_send_data_t *input); + +/** + * @brief BLOB Transfer Client starts Determine Block Status sub-procedure. + * + * @param[in] input: The input of Determine Block Status sub-procedure. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_determine_block_status(esp_ble_mesh_determine_block_status_t *input); + +/** + * @brief BLOB Transfer Client starts Determine Transfer Status procedure. + * + * @param[in] input: The input of Determine Transfer Status procedure. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_determine_transfer_status(esp_ble_mesh_determine_transfer_status_t *input); + +/** + * @brief BLOB Transfer Client starts Cancel Transfer procedure. + * + * @param[in] input: The input of Cancel Transfer procedure. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_cancel_transfer(esp_ble_mesh_cancel_transfer_t *input); + +/** + * @brief BLOB Transfer Client gets BLOB receiver. + * + * @param[in] model: BLOB Transfer Client model. + * @param[in] unicast_addr: Unicast address of the BLOB receiver. + * + * @return BLOB receiver on success or NULL on failure. + * + */ +const esp_ble_mesh_blob_receiver_t *esp_ble_mesh_mbt_client_get_blob_receiver(esp_ble_mesh_model_t *model, + uint16_t unicast_addr); + +/** + * @brief BLOB Transfer Client gets active BLOB receiver list. + * + * @param[in] model: BLOB Transfer Client model. + * + * @return Active BLOB receiver list on success or NULL on failure. + * + */ +const esp_ble_mesh_blob_receiver_t **esp_ble_mesh_mbt_client_get_active_blob_receiver(esp_ble_mesh_model_t *model); + +/** + * @brief BLOB Transfer Client gets BLOB transfer progress. + * + * @param[in] model: BLOB Transfer Client model. + * @param[in] unicast_addr: Unicast address of the BLOB receiver. + * @param[in] block_percent: Block reception percent to be updated. + * @param[in] chunk_percent: Chunk reception percent of the current block to be updated. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_get_transfer_progress(esp_ble_mesh_model_t *model, + uint16_t unicast_addr, + uint8_t *block_percent, + uint8_t *chunk_percent); + +/** + * @brief BLOB Transfer Client sets Transfer TTL state. + * + * @param[in] model: BLOB Transfer Client model. + * @param[in] transfer_ttl: Transfer TTL state value. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_set_transfer_ttl(esp_ble_mesh_model_t *model, + uint8_t transfer_ttl); + +/** + * @brief BLOB Transfer Client clear Transfer TTL state. + * + * @param[in] model: BLOB Transfer Client model. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_clear_transfer_ttl(esp_ble_mesh_model_t *model); + +/** + * @brief BLOB Transfer Client sets AppKey Index state. + * + * @param[in] model: BLOB Transfer Client model. + * @param[in] app_idx: AppKey Index state value. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_set_app_idx(esp_ble_mesh_model_t *model, + uint16_t app_idx); + +/** + * @brief BLOB Transfer Client clear AppKey Index state. + * + * @param[in] model: BLOB Transfer Client model. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_clear_app_idx(esp_ble_mesh_model_t *model); + +/** + * @brief BLOB Transfer Client sets Multicast Address state. + * + * @param[in] model: BLOB Transfer Client model. + * @param[in] multicast_addr: Multicast Address state value. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_set_multicast_addr(esp_ble_mesh_model_t *model, + uint16_t multicast_addr); + +/** + * @brief BLOB Transfer Client clear Multicast Address state. + * + * @param[in] model: BLOB Transfer Client model. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_client_clear_multicast_addr(esp_ble_mesh_model_t *model); + +/** + * @brief BLOB Transfer Server initializes BLOB receive. + * + * @param[in] input: The input of initializing BLOB receive. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_server_initialize_blob_receive(esp_ble_mesh_initialize_blob_receive_t *input); + +/** + * @brief BLOB Transfer Server cancels BLOB receive. + * + * @param[in] input: The input of cancelling BLOB receive. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_server_cancel_blob_receive(esp_ble_mesh_cancel_blob_receive_t *input); + +/** + * @brief BLOB Transfer Server sets BLOB capabilities. + * + * @param[in] input: The input of setting BLOB capabilities. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_server_set_blob_capabilities(esp_ble_mesh_set_blob_capabilities_t *input); + +/** + * @brief BLOB Transfer Server gets current BLOB reception progress. + * + * @param[in] model: BLOB Transfer Server model. + * @param[in] reception_progress: Reception progress to be updated. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_mbt_server_get_blob_reception_progress(esp_ble_mesh_model_t *model, + uint8_t *reception_progress); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_MBT_MODEL_API_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_agg_model.c b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_agg_model.c new file mode 100644 index 000000000000..3da11c10d200 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_agg_model.c @@ -0,0 +1,490 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "btc_ble_mesh_model_common.h" +#include "btc_ble_mesh_agg_model.h" +#include "esp_ble_mesh_agg_model_api.h" + +#if CONFIG_BLE_MESH_AGG_CLI + +extern int bt_mesh_agg_sequence(void *param, void *sequence); + +/* Opcodes Aggregator Client model related functions */ + +static inline void btc_ble_mesh_agg_client_cb_to_app(esp_ble_mesh_agg_client_cb_event_t event, + esp_ble_mesh_agg_client_cb_param_t *param) +{ + esp_ble_mesh_agg_client_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_AGG_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_agg_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_agg_client_args_t *dst = p_dest; + btc_ble_mesh_agg_client_args_t *src = p_src; + uint16_t length = 0; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_AGG_CLIENT_SEND: + dst->agg_send.params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->agg_send.msg = bt_mesh_calloc(sizeof(esp_ble_mesh_agg_client_msg_t)); + if (dst->agg_send.params && dst->agg_send.msg) { + memcpy(dst->agg_send.params, src->agg_send.params, + sizeof(esp_ble_mesh_client_common_param_t)); + memcpy(dst->agg_send.msg, src->agg_send.msg, + sizeof(esp_ble_mesh_agg_client_msg_t)); + if (src->agg_send.params->opcode == ESP_BLE_MESH_MODEL_OP_AGG_SEQUENCE) { + if (src->agg_send.msg->agg_sequence.items) { + length = src->agg_send.msg->agg_sequence.items->len; + dst->agg_send.msg->agg_sequence.items = bt_mesh_alloc_buf(length); + if (!dst->agg_send.msg->agg_sequence.items) { + BT_ERR("%s, Out of memory", __func__); + break; + } + + net_buf_simple_add_mem(dst->agg_send.msg->agg_sequence.items, + src->agg_send.msg->agg_sequence.items->data, + src->agg_send.msg->agg_sequence.items->len); + } + } + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + break; + default: + BT_DBG("%s, Unknown act %d", __func__, msg->act); + break; + } +} + +void btc_ble_mesh_agg_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_agg_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_agg_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_AGG_CLIENT_SEND: + if (arg->agg_send.msg) { + if (arg->agg_send.params) { + if (arg->agg_send.params->opcode == ESP_BLE_MESH_MODEL_OP_AGG_SEQUENCE) { + bt_mesh_free_buf(arg->agg_send.msg->agg_sequence.items); + } + } + bt_mesh_free(arg->agg_send.msg); + } + if (arg->agg_send.params) { + bt_mesh_free(arg->agg_send.params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_agg_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_agg_client_cb_param_t *p_dest_data = p_dest; + esp_ble_mesh_agg_client_cb_param_t *p_src_data = p_src; + uint16_t length = 0; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + + switch (msg->act) { + case ESP_BLE_MESH_AGG_CLIENT_RECV_RSP_EVT: + case ESP_BLE_MESH_AGG_CLIENT_RECV_PUB_EVT: + if (p_src_data->params) { + switch (p_src_data->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_AGG_SEQUENCE: + case ESP_BLE_MESH_MODEL_OP_AGG_STATUS: + if (p_src_data->recv.agg_status.items) { + length = p_src_data->recv.agg_status.items->len; + p_dest_data->recv.agg_status.items = bt_mesh_alloc_buf(length); + if (!p_dest_data->recv.agg_status.items) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + net_buf_simple_add_mem(p_dest_data->recv.agg_status.items, + p_src_data->recv.agg_status.items->data, + p_src_data->recv.agg_status.items->len); + } + break; + default: + break; + } + } + case ESP_BLE_MESH_AGG_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_AGG_CLIENT_SEND_TIMEOUT_EVT: + break; + default: + break; + } +} + +static void btc_ble_mesh_agg_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_agg_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_agg_client_cb_param_t *)msg->arg; + + switch (msg->act) { + case ESP_BLE_MESH_AGG_CLIENT_RECV_RSP_EVT: + case ESP_BLE_MESH_AGG_CLIENT_RECV_PUB_EVT: + if (arg->params) { + switch (arg->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_AGG_SEQUENCE: + case ESP_BLE_MESH_MODEL_OP_AGG_STATUS: + bt_mesh_free_buf(arg->recv.agg_status.items); + break; + default: + break; + } + } + case ESP_BLE_MESH_AGG_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_AGG_CLIENT_SEND_TIMEOUT_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_agg_client_cb(esp_ble_mesh_agg_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_AGG_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_AGG_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_agg_client_cb_param_t), + btc_ble_mesh_agg_client_copy_req_data, + btc_ble_mesh_agg_client_free_req_data); +} + +void bt_mesh_agg_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len) +{ + esp_ble_mesh_agg_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + uint8_t act = 0; + + if (!model || !ctx || len > sizeof(cb_params.recv)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_AGG_CLIENT_SEND_TIMEOUT: + act = ESP_BLE_MESH_AGG_CLIENT_SEND_TIMEOUT_EVT; + break; + case BTC_BLE_MESH_EVT_AGG_CLIENT_RECV_RSP: + act = ESP_BLE_MESH_AGG_CLIENT_RECV_RSP_EVT; + break; + case BTC_BLE_MESH_EVT_AGG_CLIENT_RECV_PUB: + act = ESP_BLE_MESH_AGG_CLIENT_RECV_PUB_EVT; + break; + default: + BT_ERR("Unknown Opcodes Aggregator client event type %d", event); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + params.ctx.recv_rssi = ctx->recv_rssi; + params.ctx.send_ttl = ctx->send_ttl; + + cb_params.params = ¶ms; + + if (val && len) { + memcpy(&cb_params.recv, val, len); + } + + btc_ble_mesh_agg_client_cb(&cb_params, act); +} + +void btc_ble_mesh_agg_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_agg_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_AGG_CLIENT_RECV_PUB, + model, ctx, buf->data, buf->len); +} + +static int btc_ble_mesh_agg_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_agg_client_msg_t *set) +{ + bt_mesh_client_common_param_t param = {0}; + + if (params == NULL || set == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + btc_ble_mesh_set_client_common_param(params, ¶m, false); + + switch (param.opcode) { + case ESP_BLE_MESH_MODEL_OP_AGG_SEQUENCE: + return bt_mesh_agg_sequence(¶m, &set->agg_sequence); + default: + BT_ERR("Invalid Opcodes Aggregator opcode 0x%04x", param.opcode); + return -EINVAL; + } +} + +void btc_ble_mesh_agg_client_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_agg_client_cb_param_t cb = {0}; + btc_ble_mesh_agg_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_agg_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_AGG_CLIENT_SEND: + cb.params = arg->agg_send.params; + cb.send.err_code = btc_ble_mesh_agg_client_send(arg->agg_send.params, + arg->agg_send.msg); + btc_ble_mesh_agg_client_cb(&cb, + ESP_BLE_MESH_AGG_CLIENT_SEND_COMP_EVT); + break; + default: + break; + } + + btc_ble_mesh_agg_client_arg_deep_free(msg); +} + +void btc_ble_mesh_agg_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_agg_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_agg_client_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_AGG_CLIENT_EVT_MAX) { + btc_ble_mesh_agg_client_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } + + btc_ble_mesh_agg_client_free_req_data(msg); +} + +#endif /* CONFIG_BLE_MESH_AGG_CLI */ + +#if CONFIG_BLE_MESH_AGG_SRV + +/* Opcodes Aggregator Server model related functions */ + +static inline void btc_ble_mesh_agg_server_cb_to_app(esp_ble_mesh_agg_server_cb_event_t event, + esp_ble_mesh_agg_server_cb_param_t *param) +{ + esp_ble_mesh_agg_server_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_AGG_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_agg_server_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_agg_server_cb_param_t *p_dest_data = p_dest; + esp_ble_mesh_agg_server_cb_param_t *p_src_data = p_src; + uint16_t length = 0U; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case ESP_BLE_MESH_AGG_SERVER_RECV_MSG_EVT: + if (p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_AGG_SEQUENCE) { + if (p_src_data->recv.agg_sequence.items) { + length = p_src_data->recv.agg_sequence.items->len; + p_dest_data->recv.agg_sequence.items = bt_mesh_alloc_buf(length); + if (!p_dest_data->recv.agg_sequence.items) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + break; + } + + net_buf_simple_add_mem(p_dest_data->recv.agg_sequence.items, + p_src_data->recv.agg_sequence.items->data, + p_src_data->recv.agg_sequence.items->len); + } + } + break; + default: + break; + } +} + +static void btc_ble_mesh_agg_server_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_agg_server_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_agg_server_cb_param_t *)msg->arg; + + switch (msg->act) { + case ESP_BLE_MESH_AGG_SERVER_RECV_MSG_EVT: + if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_AGG_SEQUENCE) { + bt_mesh_free_buf(arg->recv.agg_sequence.items); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_agg_server_cb( + esp_ble_mesh_agg_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_AGG_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_AGG_SERVER; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_agg_server_cb_param_t), + btc_ble_mesh_agg_server_copy_req_data, + btc_ble_mesh_agg_server_free_req_data); +} + +void bt_mesh_agg_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const void *val, size_t len) +{ + esp_ble_mesh_agg_server_cb_param_t cb_params = {0}; + uint8_t act = 0; + + if (!model || !ctx || len > sizeof(cb_params.recv)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_AGG_SERVER_RECV_MSG: + act = ESP_BLE_MESH_AGG_SERVER_RECV_MSG_EVT; + if (val && len) { + memcpy(&cb_params.recv, val, len); + } + break; + default: + BT_ERR("Unknown Opcodes Aggregator server event type %d", event); + return; + } + + cb_params.model = (esp_ble_mesh_model_t *)model; + cb_params.ctx.net_idx = ctx->net_idx; + cb_params.ctx.app_idx = ctx->app_idx; + cb_params.ctx.addr = ctx->addr; + cb_params.ctx.recv_ttl = ctx->recv_ttl; + cb_params.ctx.recv_op = ctx->recv_op; + cb_params.ctx.recv_dst = ctx->recv_dst; + cb_params.ctx.recv_rssi = ctx->recv_rssi; + cb_params.ctx.send_ttl = ctx->send_ttl; + + btc_ble_mesh_agg_server_cb(&cb_params, act); +} + +void btc_ble_mesh_agg_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_agg_server_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_agg_server_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_AGG_SERVER_EVT_MAX) { + btc_ble_mesh_agg_server_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } + + btc_ble_mesh_agg_server_free_req_data(msg); +} + +#endif /* CONFIG_BLE_MESH_AGG_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_brc_model.c b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_brc_model.c new file mode 100644 index 000000000000..36e944602478 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_brc_model.c @@ -0,0 +1,462 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "btc_ble_mesh_model_common.h" +#include "btc_ble_mesh_brc_model.h" +#include "esp_ble_mesh_brc_model_api.h" + +#if CONFIG_BLE_MESH_BRC_CLI + +extern int bt_mesh_subnet_bridge_get(void *param); +extern int bt_mesh_subnet_bridge_set(void *param, uint8_t subnet_bridge); +extern int bt_mesh_bridging_table_add(void *param, void *add); +extern int bt_mesh_bridging_table_remove(void *param, void *remove); +extern int bt_mesh_bridged_subnets_get(void *param, uint8_t bridge_filter, + uint16_t net_idx, uint8_t bridge_start_idx); +extern int bt_mesh_bridging_table_get(void *param, + uint16_t bridge_net_idx_1, + uint16_t bridge_net_idx_2, + uint16_t bridge_start_idx); +extern int bt_mesh_bridging_table_size_get(void *param); + +/* Bridge Configuration Client model related functions */ + +static inline void btc_ble_mesh_brc_client_cb_to_app(esp_ble_mesh_brc_client_cb_event_t event, + esp_ble_mesh_brc_client_cb_param_t *param) +{ + esp_ble_mesh_brc_client_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_BRC_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_brc_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_brc_client_args_t *dst = p_dest; + btc_ble_mesh_brc_client_args_t *src = p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_BRC_CLIENT_SEND: + dst->brc_send.params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->brc_send.params) { + memcpy(dst->brc_send.params, src->brc_send.params, + sizeof(esp_ble_mesh_client_common_param_t)); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + break; + } + if (src->brc_send.msg) { + dst->brc_send.msg = bt_mesh_calloc(sizeof(esp_ble_mesh_brc_client_msg_t)); + if (dst->brc_send.msg) { + memcpy(dst->brc_send.msg, src->brc_send.msg, + sizeof(esp_ble_mesh_brc_client_msg_t)); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; + default: + BT_DBG("%s, Unknown act %d", __func__, msg->act); + break; + } +} + +void btc_ble_mesh_brc_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_brc_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_brc_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_BRC_CLIENT_SEND: + if (arg->brc_send.msg) { + bt_mesh_free(arg->brc_send.msg); + } + if (arg->brc_send.params) { + bt_mesh_free(arg->brc_send.params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_brc_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_brc_client_cb_param_t *p_dest_data = p_dest; + esp_ble_mesh_brc_client_cb_param_t *p_src_data = p_src; + uint16_t length = 0; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + + switch (msg->act) { + case ESP_BLE_MESH_BRC_CLIENT_RECV_RSP_EVT: + case ESP_BLE_MESH_BRC_CLIENT_RECV_PUB_EVT: + if (p_src_data->params) { + switch (p_src_data->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_BRIDGED_SUBNETS_GET: + case ESP_BLE_MESH_MODEL_OP_BRIDGED_SUBNETS_LIST: + if (p_src_data->recv.bridged_subnets_list.net_idx_pair) { + length = p_src_data->recv.bridged_subnets_list.bridged_entry_list_size * sizeof(esp_ble_mesh_bridge_net_idx_pair_entry_t); + p_dest_data->recv.bridged_subnets_list.net_idx_pair = bt_mesh_calloc(length); + if (!p_dest_data->recv.bridged_subnets_list.net_idx_pair) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + memcpy(p_dest_data->recv.bridged_subnets_list.net_idx_pair, + p_src_data->recv.bridged_subnets_list.net_idx_pair, length); + } + break; + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_GET: + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_LIST: + if (p_src_data->recv.bridging_table_list.bridged_addr_list) { + length = p_src_data->recv.bridging_table_list.bridged_addr_list_size * sizeof(esp_ble_mesh_bridged_addr_list_entry_t); + p_dest_data->recv.bridging_table_list.bridged_addr_list = bt_mesh_calloc(length); + if (!p_dest_data->recv.bridging_table_list.bridged_addr_list) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + memcpy(p_dest_data->recv.bridging_table_list.bridged_addr_list, + p_src_data->recv.bridging_table_list.bridged_addr_list, length); + } + break; + default: + break; + } + } + case ESP_BLE_MESH_BRC_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_BRC_CLIENT_SEND_TIMEOUT_EVT: + break; + default: + break; + } +} + +static void btc_ble_mesh_brc_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_brc_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_brc_client_cb_param_t *)msg->arg; + + switch (msg->act) { + case ESP_BLE_MESH_BRC_CLIENT_RECV_RSP_EVT: + case ESP_BLE_MESH_BRC_CLIENT_RECV_PUB_EVT: + if (arg->params) { + switch (arg->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_BRIDGED_SUBNETS_GET: + case ESP_BLE_MESH_MODEL_OP_BRIDGED_SUBNETS_LIST: + bt_mesh_free(arg->recv.bridged_subnets_list.net_idx_pair); + break; + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_GET: + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_LIST: + bt_mesh_free(arg->recv.bridging_table_list.bridged_addr_list); + break; + default: + break; + } + } + case ESP_BLE_MESH_BRC_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_BRC_CLIENT_SEND_TIMEOUT_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_brc_client_cb(esp_ble_mesh_brc_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_BRC_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BRC_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_brc_client_cb_param_t), + btc_ble_mesh_brc_client_copy_req_data, + btc_ble_mesh_brc_client_free_req_data); +} + +void bt_mesh_brc_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len) +{ + esp_ble_mesh_brc_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + uint8_t act = 0; + + if (!model || !ctx || len > sizeof(cb_params.recv)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_BRC_CLIENT_RECV_RSP: + act = ESP_BLE_MESH_BRC_CLIENT_RECV_RSP_EVT; + break; + case BTC_BLE_MESH_EVT_BRC_CLIENT_RECV_PUB: + act = ESP_BLE_MESH_BRC_CLIENT_RECV_PUB_EVT; + break; + case BTC_BLE_MESH_EVT_BRC_CLIENT_SEND_TIMEOUT: + act = ESP_BLE_MESH_BRC_CLIENT_SEND_TIMEOUT_EVT; + break; + default: + BT_ERR("Unknown Bridge Config client event type %d", event); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + params.ctx.recv_rssi = ctx->recv_rssi; + params.ctx.send_ttl = ctx->send_ttl; + + cb_params.params = ¶ms; + + if (val && len) { + memcpy(&cb_params.recv, val, len); + } + + btc_ble_mesh_brc_client_cb(&cb_params, act); +} + +void btc_ble_mesh_brc_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_brc_client_cb_evt_to_btc(opcode, + ESP_BLE_MESH_BRC_CLIENT_RECV_PUB_EVT, + model, ctx, buf->data, buf->len); +} + +static int btc_ble_mesh_brc_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_brc_client_msg_t *msg) +{ + bt_mesh_client_common_param_t param = {0}; + + if (params == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + switch (params->opcode) { + case ESP_BLE_MESH_MODEL_OP_BRIDGED_SUBNETS_GET: + case ESP_BLE_MESH_MODEL_OP_SUBNET_BRIDGE_SET: + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_GET: + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_ADD: + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_REMOVE: + if (msg == NULL) { + BT_ERR("Invalid Bridge Config message, opcode 0x%04x", params->opcode); + return -EINVAL; + } + break; + default: + break; + } + + btc_ble_mesh_set_client_common_param(params, ¶m, true); + + switch (param.opcode) { + case ESP_BLE_MESH_MODEL_OP_SUBNET_BRIDGE_GET: + return bt_mesh_subnet_bridge_get(¶m); + case ESP_BLE_MESH_MODEL_OP_BRIDGED_SUBNETS_GET: + return bt_mesh_bridged_subnets_get(¶m, msg->bridged_subnets_get.bridge_filter, msg->bridged_subnets_get.bridge_net_idx, msg->bridged_subnets_get.bridge_start_idx); + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_GET: + return bt_mesh_bridging_table_get(¶m, msg->bridging_table_get.bridge_net_idx_1, msg->bridging_table_get.bridge_net_idx_2, msg->bridging_table_get.bridge_start_idx); + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_SIZE_GET: + return bt_mesh_bridging_table_size_get(¶m); + case ESP_BLE_MESH_MODEL_OP_SUBNET_BRIDGE_SET: + return bt_mesh_subnet_bridge_set(¶m, msg->subnet_bridge_set.subnet_bridge); + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_ADD: + return bt_mesh_bridging_table_add(¶m, &msg->bridging_table_add); + case ESP_BLE_MESH_MODEL_OP_BRIDGING_TABLE_REMOVE: + return bt_mesh_bridging_table_remove(¶m, &msg->bridging_table_remove); + default: + BT_ERR("Invalid Bridge Config opcode 0x%04x", param.opcode); + return -EINVAL; + } +} + +void btc_ble_mesh_brc_client_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_brc_client_cb_param_t cb = {0}; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + btc_ble_mesh_brc_client_args_t *arg = (btc_ble_mesh_brc_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_BRC_CLIENT_SEND: + cb.params = arg->brc_send.params; + cb.send.err_code = btc_ble_mesh_brc_client_send(arg->brc_send.params, + arg->brc_send.msg); + btc_ble_mesh_brc_client_cb(&cb, + ESP_BLE_MESH_BRC_CLIENT_SEND_COMP_EVT); + break; + default: + break; + } + + btc_ble_mesh_brc_client_arg_deep_free(msg); +} + +void btc_ble_mesh_brc_client_cb_handler(btc_msg_t *msg) +{ + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + esp_ble_mesh_brc_client_cb_param_t *arg = (esp_ble_mesh_brc_client_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_BRC_CLIENT_EVT_MAX) { + btc_ble_mesh_brc_client_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } + + btc_ble_mesh_brc_client_free_req_data(msg); +} +#endif /* CONFIG_BLE_MESH_BRC_CLI */ + +#if CONFIG_BLE_MESH_BRC_SRV + +/* Bridge Config Server model related functions */ +static inline void btc_ble_mesh_brc_server_cb_to_app(esp_ble_mesh_brc_server_cb_event_t event, + esp_ble_mesh_brc_server_cb_param_t *param) +{ + esp_ble_mesh_brc_server_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_BRC_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_brc_server_cb(esp_ble_mesh_brc_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_BRC_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BRC_SERVER; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_brc_server_cb_param_t), NULL, NULL); +} + +void bt_mesh_brc_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len) +{ + esp_ble_mesh_brc_server_cb_param_t cb_params = {0}; + uint8_t act = 0; + + if (!model || !ctx || len > sizeof(cb_params.value)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_BRC_SERVER_STATE_CHANGE: + act = ESP_BLE_MESH_BRC_SERVER_STATE_CHANGE_EVT; + break; + default: + BT_ERR("Unknown Bridge Config server event type %d", event); + return; + } + + cb_params.model = (esp_ble_mesh_model_t *)model; + cb_params.ctx.net_idx = ctx->net_idx; + cb_params.ctx.app_idx = ctx->app_idx; + cb_params.ctx.addr = ctx->addr; + cb_params.ctx.recv_ttl = ctx->recv_ttl; + cb_params.ctx.recv_op = ctx->recv_op; + cb_params.ctx.recv_dst = ctx->recv_dst; + cb_params.ctx.recv_rssi = ctx->recv_rssi; + cb_params.ctx.send_ttl = ctx->send_ttl; + + if (val && len) { + memcpy(&cb_params.value, val, len); + } + + btc_ble_mesh_brc_server_cb(&cb_params, act); +} + +void btc_ble_mesh_brc_server_cb_handler(btc_msg_t *msg) +{ + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + esp_ble_mesh_brc_server_cb_param_t *arg = (esp_ble_mesh_brc_server_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_BRC_SERVER_EVT_MAX) { + btc_ble_mesh_brc_server_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } +} +#endif /* CONFIG_BLE_MESH_BRC_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_df_model.c b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_df_model.c new file mode 100644 index 000000000000..aa4cadad11da --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_df_model.c @@ -0,0 +1,711 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "btc_ble_mesh_model_common.h" +#include "btc_ble_mesh_df_model.h" +#include "esp_ble_mesh_df_model_api.h" + +#if CONFIG_BLE_MESH_DF_CLI + +extern int bt_mesh_directed_control_get(void *param, uint16_t net_idx); +extern int bt_mesh_directed_control_set(void *param, void *set); +extern int bt_mesh_path_metric_get(void *param, uint16_t net_idx); +extern int bt_mesh_path_metric_set(void *param, void *set); +extern int bt_mesh_discovery_table_caps_get(void *param, uint16_t net_idx); +extern int bt_mesh_discovery_table_caps_set(void *param, void *set); +extern int bt_mesh_forwarding_table_add(void *param, void *add); +extern int bt_mesh_forwarding_table_del(void *param, void *del); +extern int bt_mesh_forwarding_table_deps_add(void *param, void *add); +extern int bt_mesh_forwarding_table_deps_del(void *param, void *del); +extern int bt_mesh_forwarding_table_entries_cnt_get(void *param, uint16_t net_idx); +extern int bt_mesh_forwarding_table_entries_get(void *param, void *get); +extern int bt_mesh_forwarding_table_deps_get(void *param, void *get); +extern int bt_mesh_wanted_lanes_get(void *param, uint16_t net_idx); +extern int bt_mesh_wanted_lanes_set(void *param, uint16_t net_idx, uint8_t wanted_lanes); +extern int bt_mesh_two_way_path_get(void *param, uint16_t net_idx); +extern int bt_mesh_two_way_path_set(void *param, uint16_t net_idx, uint8_t two_way_path); +extern int bt_mesh_path_echo_interval_get(void *param, uint16_t net_idx); +extern int bt_mesh_path_echo_interval_set(void *param, uint16_t net_idx, + uint8_t unicast_echo_interval, + uint8_t multicast_echo_interval); +extern int bt_mesh_directed_net_transmit_get(void *param); +extern int bt_mesh_directed_net_transmit_set(void *param, uint8_t transmit); +extern int bt_mesh_directed_relay_retransmit_get(void *param); +extern int bt_mesh_directed_relay_retransmit_set(void *param, uint8_t retransmit); +extern int bt_mesh_rssi_threshold_get(void *param); +extern int bt_mesh_rssi_threshold_set(void *param, uint8_t rssi_margin); +extern int bt_mesh_directed_paths_get(void *param); +extern int bt_mesh_directed_pub_policy_get(void *param, void *get); +extern int bt_mesh_directed_pub_policy_set(void *param, void *set); +extern int bt_mesh_path_discovery_timing_ctl_get(void *param); +extern int bt_mesh_path_discovery_timing_ctl_set(void *param, void *set); +extern int bt_mesh_directed_ctl_net_transmit_get(void *param); +extern int bt_mesh_directed_ctl_net_transmit_set(void *param, uint8_t transmit); +extern int bt_mesh_directed_ctl_relay_retransmit_get(void *param); +extern int bt_mesh_directed_ctl_relay_retransmit_set(void *param, uint8_t retransmit); + +/* Directed Forwarding Config Client model related functions */ + +static inline void btc_ble_mesh_df_client_cb_to_app(esp_ble_mesh_df_client_cb_event_t event, + esp_ble_mesh_df_client_cb_param_t *param) +{ + esp_ble_mesh_df_client_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_DF_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_df_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_df_client_args_t *dst = p_dest; + btc_ble_mesh_df_client_args_t *src = p_src; + uint16_t length = 0; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_DF_CLIENT_GET_STATE: { + dst->df_get.params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->df_get.params) { + memcpy(dst->df_get.params, src->df_get.params, + sizeof(esp_ble_mesh_client_common_param_t)); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + break; + } + if (src->df_get.get) { + dst->df_get.get = bt_mesh_calloc(sizeof(esp_ble_mesh_df_client_get_t)); + if (dst->df_get.get) { + memcpy(dst->df_get.get, src->df_get.get, + sizeof(esp_ble_mesh_df_client_get_t)); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; + } + case BTC_BLE_MESH_ACT_DF_CLIENT_SET_STATE: { + dst->df_set.params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->df_set.set = bt_mesh_calloc(sizeof(esp_ble_mesh_df_client_set_t)); + if (dst->df_set.params && dst->df_set.set) { + memcpy(dst->df_set.params, src->df_set.params, + sizeof(esp_ble_mesh_client_common_param_t)); + memcpy(dst->df_set.set, src->df_set.set, + sizeof(esp_ble_mesh_df_client_set_t)); + + switch (src->df_set.params->opcode) { + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_ADD: + if (src->df_set.set->forwarding_table_deps_add.dep_origin_uar_list && + src->df_set.set->forwarding_table_deps_add.dep_origin_uar_list_size) { + length = src->df_set.set->forwarding_table_deps_add.dep_origin_uar_list_size * sizeof(esp_ble_mesh_uar_t); + dst->df_set.set->forwarding_table_deps_add.dep_origin_uar_list = bt_mesh_calloc(length); + if (!dst->df_set.set->forwarding_table_deps_add.dep_origin_uar_list) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(dst->df_set.set->forwarding_table_deps_add.dep_origin_uar_list, + src->df_set.set->forwarding_table_deps_add.dep_origin_uar_list, + length); + } + if (src->df_set.set->forwarding_table_deps_add.dep_target_uar_list && + src->df_set.set->forwarding_table_deps_add.dep_target_uar_list_size) { + length = src->df_set.set->forwarding_table_deps_add.dep_target_uar_list_size * sizeof(esp_ble_mesh_uar_t); + dst->df_set.set->forwarding_table_deps_add.dep_target_uar_list = bt_mesh_calloc(length); + if (!dst->df_set.set->forwarding_table_deps_add.dep_target_uar_list) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(dst->df_set.set->forwarding_table_deps_add.dep_target_uar_list, + src->df_set.set->forwarding_table_deps_add.dep_target_uar_list, + length); + } + break; + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_DEL: + if (src->df_set.set->forwarding_table_deps_del.dep_origin_list && + src->df_set.set->forwarding_table_deps_del.dep_origin_list_size) { + length = src->df_set.set->forwarding_table_deps_del.dep_origin_list_size * 2; + dst->df_set.set->forwarding_table_deps_del.dep_origin_list = bt_mesh_calloc(length); + if (!dst->df_set.set->forwarding_table_deps_del.dep_origin_list) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(dst->df_set.set->forwarding_table_deps_del.dep_origin_list, + src->df_set.set->forwarding_table_deps_del.dep_origin_list, + length); + } + if (src->df_set.set->forwarding_table_deps_del.dep_target_list && + src->df_set.set->forwarding_table_deps_del.dep_target_list_size) { + length = src->df_set.set->forwarding_table_deps_del.dep_target_list_size * 2; + dst->df_set.set->forwarding_table_deps_del.dep_target_list = bt_mesh_calloc(length); + if (!dst->df_set.set->forwarding_table_deps_del.dep_target_list) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(dst->df_set.set->forwarding_table_deps_del.dep_target_list, + src->df_set.set->forwarding_table_deps_del.dep_target_list, + length); + } + break; + default: + break; + } + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + break; + } + default: + BT_DBG("%s, Unknown act %d", __func__, msg->act); + break; + } +} + +void btc_ble_mesh_df_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_df_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_df_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_DF_CLIENT_GET_STATE: + if (arg->df_get.params) { + bt_mesh_free(arg->df_get.params); + } + if (arg->df_get.get) { + bt_mesh_free(arg->df_get.get); + } + break; + case BTC_BLE_MESH_ACT_DF_CLIENT_SET_STATE: + if (arg->df_set.set) { + if (arg->df_set.params) { + switch (arg->df_set.params->opcode) { + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_ADD: + bt_mesh_free(arg->df_set.set->forwarding_table_deps_add.dep_origin_uar_list); + bt_mesh_free(arg->df_set.set->forwarding_table_deps_add.dep_target_uar_list); + break; + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_DEL: + bt_mesh_free(arg->df_set.set->forwarding_table_deps_del.dep_origin_list); + bt_mesh_free(arg->df_set.set->forwarding_table_deps_del.dep_target_list); + break; + default: + break; + } + } + bt_mesh_free(arg->df_set.set); + } + if (arg->df_set.params) { + bt_mesh_free(arg->df_set.params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_df_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_df_client_cb_param_t *p_dest_data = p_dest; + esp_ble_mesh_df_client_cb_param_t *p_src_data = p_src; + uint16_t length = 0; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + + switch (msg->act) { + case ESP_BLE_MESH_DF_CLIENT_RECV_GET_RSP_EVT: + case ESP_BLE_MESH_DF_CLIENT_RECV_SET_RSP_EVT: + case ESP_BLE_MESH_DF_CLIENT_RECV_PUB_EVT: + if (p_src_data->params) { + switch (p_src_data->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_GET: + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_STATUS: + if (p_src_data->recv.forwarding_table_entries_status.entry_list) { + length = p_src_data->recv.forwarding_table_entries_status.entry_list_size * sizeof(esp_ble_mesh_forwarding_table_entry_t); + p_dest_data->recv.forwarding_table_entries_status.entry_list = bt_mesh_calloc(length); + if (!p_dest_data->recv.forwarding_table_entries_status.entry_list) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->recv.forwarding_table_entries_status.entry_list, + p_src_data->recv.forwarding_table_entries_status.entry_list, + length); + } + break; + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_GET: + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_GET_STATUS: + if (p_src_data->recv.forwarding_table_deps_get_status.dep_origin_uar_list) { + length = p_src_data->recv.forwarding_table_deps_get_status.dep_origin_uar_list_size * sizeof(esp_ble_mesh_uar_t); + p_dest_data->recv.forwarding_table_deps_get_status.dep_origin_uar_list = bt_mesh_calloc(length); + if (!p_dest_data->recv.forwarding_table_deps_get_status.dep_origin_uar_list) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->recv.forwarding_table_deps_get_status.dep_origin_uar_list, + p_src_data->recv.forwarding_table_deps_get_status.dep_origin_uar_list, + length); + } + if (p_src_data->recv.forwarding_table_deps_get_status.dep_target_uar_list) { + length = p_src_data->recv.forwarding_table_deps_get_status.dep_target_uar_list_size * sizeof(esp_ble_mesh_uar_t); + p_dest_data->recv.forwarding_table_deps_get_status.dep_target_uar_list = bt_mesh_calloc(length); + if (!p_dest_data->recv.forwarding_table_deps_get_status.dep_target_uar_list) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->recv.forwarding_table_deps_get_status.dep_target_uar_list, + p_src_data->recv.forwarding_table_deps_get_status.dep_target_uar_list, + length); + } + break; + default: + break; + } + } + case ESP_BLE_MESH_DF_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_DF_CLIENT_SEND_TIMEOUT_EVT: + break; + default: + break; + } +} + +static void btc_ble_mesh_df_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_df_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_df_client_cb_param_t *)msg->arg; + + switch (msg->act) { + case ESP_BLE_MESH_DF_CLIENT_RECV_GET_RSP_EVT: + case ESP_BLE_MESH_DF_CLIENT_RECV_SET_RSP_EVT: + case ESP_BLE_MESH_DF_CLIENT_RECV_PUB_EVT: + if (arg->params) { + switch (arg->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_GET: + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_STATUS: + bt_mesh_free(arg->recv.forwarding_table_entries_status.entry_list); + break; + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_GET: + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_GET_STATUS: + bt_mesh_free(arg->recv.forwarding_table_deps_get_status.dep_origin_uar_list); + bt_mesh_free(arg->recv.forwarding_table_deps_get_status.dep_target_uar_list); + break; + default: + break; + } + } + case ESP_BLE_MESH_DF_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_DF_CLIENT_SEND_TIMEOUT_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_df_client_cb(esp_ble_mesh_df_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_DF_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_DF_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_df_client_cb_param_t), + btc_ble_mesh_df_client_copy_req_data, + btc_ble_mesh_df_client_free_req_data); +} + +void bt_mesh_df_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len) +{ + esp_ble_mesh_df_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + uint8_t act = 0; + + if (!model || !ctx || len > sizeof(cb_params.recv)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_DF_CLIENT_SEND_TIMEOUT: + act = ESP_BLE_MESH_DF_CLIENT_SEND_TIMEOUT_EVT; + break; + case BTC_BLE_MESH_EVT_DF_CLIENT_RECV_GET_RSP: + act = ESP_BLE_MESH_DF_CLIENT_RECV_GET_RSP_EVT; + break; + case BTC_BLE_MESH_EVT_DF_CLIENT_RECV_SET_RSP: + act = ESP_BLE_MESH_DF_CLIENT_RECV_SET_RSP_EVT; + break; + case BTC_BLE_MESH_EVT_DF_CLIENT_RECV_PUB: + act = ESP_BLE_MESH_DF_CLIENT_RECV_PUB_EVT; + break; + default: + BT_ERR("Unknown Directed Forward Config client event type %d", event); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + params.ctx.recv_rssi = ctx->recv_rssi; + params.ctx.send_ttl = ctx->send_ttl; + + cb_params.params = ¶ms; + + if (val && len) { + memcpy(&cb_params.recv, val, len); + } + + btc_ble_mesh_df_client_cb(&cb_params, act); +} + +void btc_ble_mesh_df_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_df_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_DF_CLIENT_RECV_PUB, + model, ctx, buf->data, buf->len); +} + +static int btc_ble_mesh_df_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_df_client_get_t *get) +{ + bt_mesh_client_common_param_t param = {0}; + + if (params == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + switch (params->opcode) { + case ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_GET: + case ESP_BLE_MESH_MODEL_OP_PATH_METRIC_GET: + case ESP_BLE_MESH_MODEL_OP_DISCOVERY_TABLE_CAPS_GET: + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_CNT_GET: + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_GET: + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_GET: + case ESP_BLE_MESH_MODEL_OP_WANTED_LANES_GET: + case ESP_BLE_MESH_MODEL_OP_TWO_WAY_PATH_GET: + case ESP_BLE_MESH_MODEL_OP_PATH_ECHO_INTERVAL_GET: + case ESP_BLE_MESH_MODEL_OP_DIRECTED_PUB_POLICY_GET: + if (get == NULL) { + BT_ERR("Invalid Directed Forward Config Get"); + return -EINVAL; + } + break; + default: + break; + } + + btc_ble_mesh_set_client_common_param(params, ¶m, true); + + switch (param.opcode) { + case ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_GET: + return bt_mesh_directed_control_get(¶m, get->directed_control_get.net_idx); + case ESP_BLE_MESH_MODEL_OP_PATH_METRIC_GET: + return bt_mesh_path_metric_get(¶m, get->path_metric_get.net_idx); + case ESP_BLE_MESH_MODEL_OP_DISCOVERY_TABLE_CAPS_GET: + return bt_mesh_discovery_table_caps_get(¶m, get->disc_table_caps_get.net_idx); + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_CNT_GET: + return bt_mesh_forwarding_table_entries_cnt_get(¶m, get->forwarding_table_entries_cnt_get.net_idx); + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ENTRIES_GET: + return bt_mesh_forwarding_table_entries_get(¶m, &get->forwarding_table_entries_get); + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_GET: + return bt_mesh_forwarding_table_deps_get(¶m, &get->forwarding_table_deps_get); + case ESP_BLE_MESH_MODEL_OP_WANTED_LANES_GET: + return bt_mesh_wanted_lanes_get(¶m, get->wanted_lanes_get.net_idx); + case ESP_BLE_MESH_MODEL_OP_TWO_WAY_PATH_GET: + return bt_mesh_two_way_path_get(¶m, get->two_way_path_get.net_idx); + case ESP_BLE_MESH_MODEL_OP_PATH_ECHO_INTERVAL_GET: + return bt_mesh_path_echo_interval_get(¶m, get->path_echo_interval_get.net_idx); + case ESP_BLE_MESH_MODEL_OP_DIRECTED_NET_TRANSMIT_GET: + return bt_mesh_directed_net_transmit_get(¶m); + case ESP_BLE_MESH_MODEL_OP_DIRECTED_RELAY_RETRANSMIT_GET: + return bt_mesh_directed_relay_retransmit_get(¶m); + case ESP_BLE_MESH_MODEL_OP_RSSI_THRESHOLD_GET: + return bt_mesh_rssi_threshold_get(¶m); + case ESP_BLE_MESH_MODEL_OP_DIRECTED_PATHS_GET: + return bt_mesh_directed_paths_get(¶m); + case ESP_BLE_MESH_MODEL_OP_DIRECTED_PUB_POLICY_GET: + return bt_mesh_directed_pub_policy_get(¶m, &get->directed_pub_policy_get); + case ESP_BLE_MESH_MODEL_OP_PATH_DISCOVERY_TIMING_CTL_GET: + return bt_mesh_path_discovery_timing_ctl_get(¶m); + case ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_NET_TRANSMIT_GET: + return bt_mesh_directed_ctl_net_transmit_get(¶m); + case ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_RELAY_RETRANSMIT_GET: + return bt_mesh_directed_ctl_relay_retransmit_get(¶m); + default: + BT_ERR("Invalid Directed Forward Config Get opcode 0x%04x", param.opcode); + return -EINVAL; + } +} + +static int btc_ble_mesh_df_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_df_client_set_t *set) +{ + bt_mesh_client_common_param_t param = {0}; + + if (params == NULL || set == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + btc_ble_mesh_set_client_common_param(params, ¶m, true); + + switch (param.opcode) { + case ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_SET: + return bt_mesh_directed_control_set(¶m, &set->directed_control_set); + case ESP_BLE_MESH_MODEL_OP_PATH_METRIC_SET: + return bt_mesh_path_metric_set(¶m, &set->path_metric_set); + case ESP_BLE_MESH_MODEL_OP_DISCOVERY_TABLE_CAPS_SET: + return bt_mesh_discovery_table_caps_set(¶m, &set->disc_table_caps_set); + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_ADD: + return bt_mesh_forwarding_table_add(¶m, &set->forwarding_table_add); + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEL: + return bt_mesh_forwarding_table_del(¶m, &set->forwarding_table_del); + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_ADD: + return bt_mesh_forwarding_table_deps_add(¶m, &set->forwarding_table_deps_add); + case ESP_BLE_MESH_MODEL_OP_FORWARDING_TABLE_DEPS_DEL: + return bt_mesh_forwarding_table_deps_del(¶m, &set->forwarding_table_deps_del); + case ESP_BLE_MESH_MODEL_OP_WANTED_LANES_SET: + return bt_mesh_wanted_lanes_set(¶m, set->wanted_lanes_set.net_idx, + set->wanted_lanes_set.wanted_lanes); + case ESP_BLE_MESH_MODEL_OP_TWO_WAY_PATH_SET: + return bt_mesh_two_way_path_set(¶m, set->two_way_path_set.net_idx, + set->two_way_path_set.two_way_path); + case ESP_BLE_MESH_MODEL_OP_PATH_ECHO_INTERVAL_SET: + return bt_mesh_path_echo_interval_set(¶m, set->path_echo_interval_set.net_idx, + set->path_echo_interval_set.unicast_echo_interval, + set->path_echo_interval_set.multicast_echo_interval); + case ESP_BLE_MESH_MODEL_OP_DIRECTED_NET_TRANSMIT_SET: + return bt_mesh_directed_net_transmit_set(¶m, set->directed_net_transmit_set.net_transmit); + case ESP_BLE_MESH_MODEL_OP_DIRECTED_RELAY_RETRANSMIT_SET: + return bt_mesh_directed_relay_retransmit_set(¶m, set->directed_relay_retransmit_set.relay_retransmit); + case ESP_BLE_MESH_MODEL_OP_RSSI_THRESHOLD_SET: + return bt_mesh_rssi_threshold_set(¶m, set->rssi_threshold_set.rssi_margin); + case ESP_BLE_MESH_MODEL_OP_DIRECTED_PUB_POLICY_SET: + return bt_mesh_directed_pub_policy_set(¶m, &set->directed_pub_policy_set); + case ESP_BLE_MESH_MODEL_OP_PATH_DISCOVERY_TIMING_CTL_SET: + return bt_mesh_path_discovery_timing_ctl_set(¶m, &set->path_disc_timing_ctl_set); + case ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_NET_TRANSMIT_SET: + return bt_mesh_directed_ctl_net_transmit_set(¶m, set->directed_ctl_net_transmit_set.net_transmit); + case ESP_BLE_MESH_MODEL_OP_DIRECTED_CTL_RELAY_RETRANSMIT_SET: + return bt_mesh_directed_ctl_relay_retransmit_set(¶m, set->directed_ctl_relay_retransmit_set.relay_retransmit); + default: + BT_ERR("Invalid Directed Forward Config Set opcode 0x%04x", param.opcode); + return -EINVAL; + } +} + +void btc_ble_mesh_df_client_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_df_client_cb_param_t cb = {0}; + btc_ble_mesh_df_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_df_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_DF_CLIENT_GET_STATE: + cb.params = arg->df_get.params; + cb.send.err_code = btc_ble_mesh_df_client_get_state(arg->df_get.params, + arg->df_get.get); + btc_ble_mesh_df_client_cb(&cb, + ESP_BLE_MESH_DF_CLIENT_SEND_COMP_EVT); + break; + case BTC_BLE_MESH_ACT_DF_CLIENT_SET_STATE: + cb.params = arg->df_set.params; + cb.send.err_code = btc_ble_mesh_df_client_set_state(arg->df_set.params, + arg->df_set.set); + btc_ble_mesh_df_client_cb(&cb, + ESP_BLE_MESH_DF_CLIENT_SEND_COMP_EVT); + break; + default: + break; + } + + btc_ble_mesh_df_client_arg_deep_free(msg); +} + +void btc_ble_mesh_df_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_df_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_df_client_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_DF_CLIENT_EVT_MAX) { + btc_ble_mesh_df_client_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } + + btc_ble_mesh_df_client_free_req_data(msg); +} +#endif /* CONFIG_BLE_MESH_DF_CLI */ + +#if CONFIG_BLE_MESH_DF_SRV + +/* Directed Forwarding Config Server model related functions */ + +static inline void btc_ble_mesh_df_server_cb_to_app(esp_ble_mesh_df_server_cb_event_t event, + esp_ble_mesh_df_server_cb_param_t *param) +{ + esp_ble_mesh_df_server_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_DF_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_df_server_cb( + esp_ble_mesh_df_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_DF_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_DF_SERVER; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_df_server_cb_param_t), NULL, NULL); +} + +void bt_mesh_df_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len) +{ + esp_ble_mesh_df_server_cb_param_t cb_params = {0}; + uint8_t act = 0; + + if (len > sizeof(cb_params.value)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_DF_SERVER_STATE_CHANGE: + act = ESP_BLE_MESH_DF_SERVER_STATE_CHANGE_EVT; + break; + case BTC_BLE_MESH_EVT_DF_SERVER_TABLE_CHANGE: + act = ESP_BLE_MESH_DF_SERVER_TABLE_CHANGE_EVT; + break; + default: + BT_ERR("Unknown Directed Forward Config server event type %d", event); + return; + } + + if (model) { + cb_params.model = (esp_ble_mesh_model_t *)model; + } + + if (ctx) { + cb_params.ctx.net_idx = ctx->net_idx; + cb_params.ctx.app_idx = ctx->app_idx; + cb_params.ctx.addr = ctx->addr; + cb_params.ctx.recv_ttl = ctx->recv_ttl; + cb_params.ctx.recv_op = ctx->recv_op; + cb_params.ctx.recv_dst = ctx->recv_dst; + cb_params.ctx.recv_rssi = ctx->recv_rssi; + cb_params.ctx.send_ttl = ctx->send_ttl; + } + + if (val && len) { + memcpy(&cb_params.value, val, len); + } + + btc_ble_mesh_df_server_cb(&cb_params, act); +} + +void btc_ble_mesh_df_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_df_server_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_df_server_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_DF_SERVER_EVT_MAX) { + btc_ble_mesh_df_server_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } +} +#endif /* CONFIG_BLE_MESH_DF_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_lcd_model.c b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_lcd_model.c new file mode 100644 index 000000000000..25fc2380323a --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_lcd_model.c @@ -0,0 +1,435 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "btc_ble_mesh_model_common.h" +#include "btc_ble_mesh_lcd_model.h" +#include "esp_ble_mesh_lcd_model_api.h" + +#if CONFIG_BLE_MESH_LCD_CLI + +extern int bt_mesh_large_comp_data_get(void *param, uint8_t page, uint16_t offset); +extern int bt_mesh_models_metadata_get(void *param, uint8_t metadata_page, uint16_t offset); + +/* Large Composition Data Client model related functions */ + +static inline void btc_ble_mesh_lcd_client_cb_to_app(esp_ble_mesh_lcd_client_cb_event_t event, + esp_ble_mesh_lcd_client_cb_param_t *param) +{ + esp_ble_mesh_lcd_client_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_LCD_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_lcd_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_lcd_client_args_t *dst = p_dest; + btc_ble_mesh_lcd_client_args_t *src = p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_LCD_CLIENT_SEND: + dst->lcd_send.params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->lcd_send.msg = bt_mesh_calloc(sizeof(esp_ble_mesh_lcd_client_msg_t)); + if (dst->lcd_send.params && dst->lcd_send.msg) { + memcpy(dst->lcd_send.params, src->lcd_send.params, + sizeof(esp_ble_mesh_client_common_param_t)); + memcpy(dst->lcd_send.msg, src->lcd_send.msg, + sizeof(esp_ble_mesh_lcd_client_msg_t)); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + break; + default: + BT_DBG("%s, Unknown act %d", __func__, msg->act); + break; + } +} + +void btc_ble_mesh_lcd_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_lcd_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_lcd_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_LCD_CLIENT_SEND: + if (arg->lcd_send.msg) { + bt_mesh_free(arg->lcd_send.msg); + } + if (arg->lcd_send.params) { + bt_mesh_free(arg->lcd_send.params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_lcd_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_lcd_client_cb_param_t *p_dest_data = p_dest; + esp_ble_mesh_lcd_client_cb_param_t *p_src_data = p_src; + uint16_t length = 0; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + + switch (msg->act) { + case ESP_BLE_MESH_LCD_CLIENT_RECV_RSP_EVT: + case ESP_BLE_MESH_LCD_CLIENT_RECV_PUB_EVT: + if (p_src_data->params) { + switch (p_src_data->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_LARGE_COMP_DATA_GET: + case ESP_BLE_MESH_MODEL_OP_LARGE_COMP_DATA_STATUS: + if (p_src_data->recv.large_comp_data_status.data) { + length = p_src_data->recv.large_comp_data_status.data->len; + p_dest_data->recv.large_comp_data_status.data = bt_mesh_alloc_buf(length); + if (!p_dest_data->recv.large_comp_data_status.data) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + net_buf_simple_add_mem(p_dest_data->recv.large_comp_data_status.data, + p_src_data->recv.large_comp_data_status.data->data, + p_src_data->recv.large_comp_data_status.data->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_MODELS_METADATA_GET: + case ESP_BLE_MESH_MODEL_OP_MODELS_METADATA_STATUS: + if (p_src_data->recv.models_metadata_status.data) { + length = p_src_data->recv.models_metadata_status.data->len; + p_dest_data->recv.models_metadata_status.data = bt_mesh_alloc_buf(length); + if (!p_dest_data->recv.models_metadata_status.data) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + net_buf_simple_add_mem(p_dest_data->recv.models_metadata_status.data, + p_src_data->recv.models_metadata_status.data->data, + p_src_data->recv.models_metadata_status.data->len); + } + break; + default: + break; + } + } + case ESP_BLE_MESH_LCD_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_LCD_CLIENT_SEND_TIMEOUT_EVT: + break; + default: + break; + } +} + +static void btc_ble_mesh_lcd_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_lcd_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_lcd_client_cb_param_t *)msg->arg; + + switch (msg->act) { + case ESP_BLE_MESH_LCD_CLIENT_RECV_RSP_EVT: + case ESP_BLE_MESH_LCD_CLIENT_RECV_PUB_EVT: + if (arg->params) { + switch (arg->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_LARGE_COMP_DATA_GET: + case ESP_BLE_MESH_MODEL_OP_LARGE_COMP_DATA_STATUS: + bt_mesh_free_buf(arg->recv.large_comp_data_status.data); + break; + case ESP_BLE_MESH_MODEL_OP_MODELS_METADATA_GET: + case ESP_BLE_MESH_MODEL_OP_MODELS_METADATA_STATUS: + bt_mesh_free_buf(arg->recv.models_metadata_status.data); + break; + default: + break; + } + } + case ESP_BLE_MESH_LCD_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_LCD_CLIENT_SEND_TIMEOUT_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_lcd_client_cb(esp_ble_mesh_lcd_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_LCD_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_LCD_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_lcd_client_cb_param_t), + btc_ble_mesh_lcd_client_copy_req_data, + btc_ble_mesh_lcd_client_free_req_data); +} + +void bt_mesh_lcd_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const void *val, size_t len) +{ + esp_ble_mesh_lcd_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + uint8_t act = 0; + + if (!model || !ctx || len > sizeof(cb_params.recv)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_LCD_CLIENT_SEND_TIMEOUT: + act = ESP_BLE_MESH_LCD_CLIENT_SEND_TIMEOUT_EVT; + break; + case BTC_BLE_MESH_EVT_LCD_CLIENT_RECV_RSP: + act = ESP_BLE_MESH_LCD_CLIENT_RECV_RSP_EVT; + break; + case BTC_BLE_MESH_EVT_LCD_CLIENT_RECV_PUB: + act = ESP_BLE_MESH_LCD_CLIENT_RECV_PUB_EVT; + break; + default: + BT_ERR("Unknown Large Comp Data client event type %d", event); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + params.ctx.recv_rssi = ctx->recv_rssi; + params.ctx.send_ttl = ctx->send_ttl; + + cb_params.params = ¶ms; + + if (val && len) { + memcpy(&cb_params.recv, val, len); + } + + btc_ble_mesh_lcd_client_cb(&cb_params, act); +} + +void btc_ble_mesh_lcd_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_lcd_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_LCD_CLIENT_RECV_PUB, + model, ctx, buf->data, buf->len); +} + +static int btc_ble_mesh_lcd_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_lcd_client_msg_t *msg) +{ + bt_mesh_client_common_param_t param = {0}; + + if (params == NULL || msg == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + btc_ble_mesh_set_client_common_param(params, ¶m, true); + + switch (param.opcode) { + case ESP_BLE_MESH_MODEL_OP_LARGE_COMP_DATA_GET: + return bt_mesh_large_comp_data_get(¶m, msg->large_comp_data_get.page, + msg->large_comp_data_get.offset); + case ESP_BLE_MESH_MODEL_OP_MODELS_METADATA_GET: + return bt_mesh_models_metadata_get(¶m, msg->models_metadata_get.metadata_page, + msg->models_metadata_get.offset); + default: + BT_ERR("Invalid Large Comp Data Set opcode 0x%04x", param.opcode); + return -EINVAL; + } +} + +void btc_ble_mesh_lcd_client_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_lcd_client_cb_param_t cb = {0}; + btc_ble_mesh_lcd_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_lcd_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_LCD_CLIENT_SEND: + cb.params = arg->lcd_send.params; + cb.send.err_code = btc_ble_mesh_lcd_client_send(arg->lcd_send.params, + arg->lcd_send.msg); + btc_ble_mesh_lcd_client_cb(&cb, ESP_BLE_MESH_LCD_CLIENT_SEND_COMP_EVT); + break; + default: + break; + } + + btc_ble_mesh_lcd_client_arg_deep_free(msg); +} + +void btc_ble_mesh_lcd_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_lcd_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_lcd_client_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_LCD_CLIENT_EVT_MAX) { + btc_ble_mesh_lcd_client_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } + + btc_ble_mesh_lcd_client_free_req_data(msg); +} + +#endif /* CONFIG_BLE_MESH_LCD_CLI */ + +#if CONFIG_BLE_MESH_LCD_SRV + +/* Large Comp Data Server model related functions */ + +static inline void btc_ble_mesh_lcd_server_cb_to_app(esp_ble_mesh_lcd_server_cb_event_t event, + esp_ble_mesh_lcd_server_cb_param_t *param) +{ + esp_ble_mesh_lcd_server_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_LCD_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_lcd_server_cb( + esp_ble_mesh_lcd_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_LCD_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_LCD_SERVER; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_lcd_server_cb_param_t), NULL, NULL); +} + +void bt_mesh_lcd_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const void *val, size_t len) +{ + esp_ble_mesh_lcd_server_cb_param_t cb_params = {0}; + uint8_t act = 0; + + if (!model || !ctx || len > sizeof(cb_params.value)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_LCD_SERVER_STATE_CHANGE: + act = ESP_BLE_MESH_LCD_SERVER_STATE_CHANGE_EVT; + break; + default: + BT_ERR("Unknown Large Comp Data server event type %d", event); + return; + } + + cb_params.model = (esp_ble_mesh_model_t *)model; + cb_params.ctx.net_idx = ctx->net_idx; + cb_params.ctx.app_idx = ctx->app_idx; + cb_params.ctx.addr = ctx->addr; + cb_params.ctx.recv_ttl = ctx->recv_ttl; + cb_params.ctx.recv_op = ctx->recv_op; + cb_params.ctx.recv_dst = ctx->recv_dst; + cb_params.ctx.recv_rssi = ctx->recv_rssi; + cb_params.ctx.send_ttl = ctx->send_ttl; + + if (val && len) { + memcpy(&cb_params.value, val, len); + } + + btc_ble_mesh_lcd_server_cb(&cb_params, act); +} + +void btc_ble_mesh_lcd_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_lcd_server_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_lcd_server_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_LCD_SERVER_EVT_MAX) { + btc_ble_mesh_lcd_server_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } +} + +#endif /* CONFIG_BLE_MESH_LCD_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_mbt_model.c b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_mbt_model.c new file mode 100644 index 000000000000..430bd31eeee7 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_mbt_model.c @@ -0,0 +1,600 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "btc_ble_mesh_mbt_model.h" +#include "esp_ble_mesh_mbt_model_api.h" + +#if CONFIG_BLE_MESH_MBT_CLI + +extern int bt_mesh_retrieve_capabilities(void *input); +extern int bt_mesh_transfer_blob(void *input); +extern int bt_mesh_send_block(void *input); +extern int bt_mesh_send_data(void *input); +extern int bt_mesh_determine_block_status(void *input); +extern int bt_mesh_determine_transfer_status(void *input); +extern int bt_mesh_cancel_transfer(void *input); +extern int bt_mesh_set_transfer_ttl_state(void *model, uint8_t transfer_ttl); +extern int bt_mesh_clear_transfer_ttl_state(void *model); +extern int bt_mesh_set_app_idx_state(void *model, uint16_t app_idx); +extern int bt_mesh_clear_app_idx_state(void *model); +extern int bt_mesh_set_multicast_addr_state(void *model, uint16_t multicast_addr); +extern int bt_mesh_clear_multicast_addr_state(void *model); +extern int bt_mesh_initialize_blob_receive(void *input); +extern int bt_mesh_cancel_blob_receive(void *input); +extern int bt_mesh_set_blob_capabilities(void *model, void *input); + +static inline void btc_ble_mesh_blob_trans_client_cb_to_app(esp_ble_mesh_mbt_client_cb_event_t event, + esp_ble_mesh_mbt_client_cb_param_t *param) +{ + esp_ble_mesh_mbt_client_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_mbt_client_cb_t)btc_profile_cb_get(BTC_PID_MBT_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_mbt_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_mbt_client_args_t *dst = (btc_ble_mesh_mbt_client_args_t *)p_dest; + btc_ble_mesh_mbt_client_args_t *src = (btc_ble_mesh_mbt_client_args_t *)p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_MBT_CLIENT_RETRIEVE_CAPABILITIES: + if (src->retrieve_capabilities.unicast_addr_count && src->retrieve_capabilities.unicast_addr) { + dst->retrieve_capabilities.unicast_addr = bt_mesh_calloc(src->retrieve_capabilities.unicast_addr_count * 2); + if (dst->retrieve_capabilities.unicast_addr) { + memcpy(dst->retrieve_capabilities.unicast_addr, src->retrieve_capabilities.unicast_addr, + src->retrieve_capabilities.unicast_addr_count * 2); + dst->retrieve_capabilities.unicast_addr_count = src->retrieve_capabilities.unicast_addr_count; + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_TRANSFER_BLOB: + if (src->transfer_blob.unicast_addr_count && src->transfer_blob.unicast_addr) { + dst->transfer_blob.unicast_addr = bt_mesh_calloc(src->transfer_blob.unicast_addr_count * 2); + if (dst->transfer_blob.unicast_addr) { + memcpy(dst->transfer_blob.unicast_addr, src->transfer_blob.unicast_addr, + src->transfer_blob.unicast_addr_count * 2); + dst->transfer_blob.unicast_addr_count = src->transfer_blob.unicast_addr_count; + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_DETERMINE_TRANSFER_STATUS: + if (src->determine_transfer_status.unicast_addr_count && src->determine_transfer_status.unicast_addr) { + dst->determine_transfer_status.unicast_addr = bt_mesh_calloc(src->determine_transfer_status.unicast_addr_count * 2); + if (dst->determine_transfer_status.unicast_addr) { + memcpy(dst->determine_transfer_status.unicast_addr, src->determine_transfer_status.unicast_addr, + src->determine_transfer_status.unicast_addr_count * 2); + dst->determine_transfer_status.unicast_addr_count = src->determine_transfer_status.unicast_addr_count; + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_CANCEL_TRANSFER: + if (src->cancel_transfer.unicast_addr_count && src->cancel_transfer.unicast_addr) { + dst->cancel_transfer.unicast_addr = bt_mesh_calloc(src->cancel_transfer.unicast_addr_count * 2); + if (dst->cancel_transfer.unicast_addr) { + memcpy(dst->cancel_transfer.unicast_addr, src->cancel_transfer.unicast_addr, + src->cancel_transfer.unicast_addr_count * 2); + dst->cancel_transfer.unicast_addr_count = src->cancel_transfer.unicast_addr_count; + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; + default: + BT_DBG("%s, Unknown act %d", __func__, msg->act); + break; + } +} + +void btc_ble_mesh_mbt_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_mbt_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_mbt_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_MBT_CLIENT_RETRIEVE_CAPABILITIES: + bt_mesh_free(arg->retrieve_capabilities.unicast_addr); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_TRANSFER_BLOB: + bt_mesh_free(arg->transfer_blob.unicast_addr); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_DETERMINE_TRANSFER_STATUS: + bt_mesh_free(arg->determine_transfer_status.unicast_addr); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_CANCEL_TRANSFER: + bt_mesh_free(arg->cancel_transfer.unicast_addr); + break; + default: + break; + } +} + +static void btc_ble_mesh_blob_trans_client_copy_req_data(btc_msg_t *msg, void *p_dst, void *p_src) +{ + esp_ble_mesh_mbt_client_cb_param_t *dst = (esp_ble_mesh_mbt_client_cb_param_t *)p_dst; + esp_ble_mesh_mbt_client_cb_param_t *src = (esp_ble_mesh_mbt_client_cb_param_t *)p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_MBT_CLIENT_RETRIEVE_CAPABILITIES: + if (src->value.retrieve_capabilities_status.input.unicast_addr_count && + src->value.retrieve_capabilities_status.input.unicast_addr) { + dst->value.retrieve_capabilities_status.input.unicast_addr = bt_mesh_calloc(src->value.retrieve_capabilities_status.input.unicast_addr_count * 2); + if (dst->value.retrieve_capabilities_status.input.unicast_addr) { + memcpy(dst->value.retrieve_capabilities_status.input.unicast_addr, src->value.retrieve_capabilities_status.input.unicast_addr, + src->value.retrieve_capabilities_status.input.unicast_addr_count * 2); + dst->value.retrieve_capabilities_status.input.unicast_addr_count = src->value.retrieve_capabilities_status.input.unicast_addr_count; + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_TRANSFER_BLOB: + if (src->value.transfer_blob_status.input.unicast_addr_count && + src->value.transfer_blob_status.input.unicast_addr) { + dst->value.transfer_blob_status.input.unicast_addr = bt_mesh_calloc(src->value.transfer_blob_status.input.unicast_addr_count * 2); + if (dst->value.transfer_blob_status.input.unicast_addr) { + memcpy(dst->value.transfer_blob_status.input.unicast_addr, src->value.transfer_blob_status.input.unicast_addr, + src->value.transfer_blob_status.input.unicast_addr_count * 2); + dst->value.transfer_blob_status.input.unicast_addr_count = src->value.transfer_blob_status.input.unicast_addr_count; + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_DETERMINE_TRANSFER_STATUS: + if (src->value.determine_transfer_status_status.input.unicast_addr_count && + src->value.determine_transfer_status_status.input.unicast_addr) { + dst->value.determine_transfer_status_status.input.unicast_addr = bt_mesh_calloc(src->value.determine_transfer_status_status.input.unicast_addr_count * 2); + if (dst->value.determine_transfer_status_status.input.unicast_addr) { + memcpy(dst->value.determine_transfer_status_status.input.unicast_addr, src->value.determine_transfer_status_status.input.unicast_addr, + src->value.determine_transfer_status_status.input.unicast_addr_count * 2); + dst->value.determine_transfer_status_status.input.unicast_addr_count = src->value.determine_transfer_status_status.input.unicast_addr_count; + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_CANCEL_TRANSFER: + if (src->value.cancel_transfer_status.input.unicast_addr_count && + src->value.cancel_transfer_status.input.unicast_addr) { + dst->value.cancel_transfer_status.input.unicast_addr = bt_mesh_calloc(src->value.cancel_transfer_status.input.unicast_addr_count * 2); + if (dst->value.cancel_transfer_status.input.unicast_addr) { + memcpy(dst->value.cancel_transfer_status.input.unicast_addr, src->value.cancel_transfer_status.input.unicast_addr, + src->value.cancel_transfer_status.input.unicast_addr_count * 2); + dst->value.cancel_transfer_status.input.unicast_addr_count = src->value.cancel_transfer_status.input.unicast_addr_count; + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; + default: + BT_DBG("%s, Unknown act %d", __func__, msg->act); + break; + } +} + +static void btc_ble_mesh_blob_trans_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_mbt_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_mbt_client_cb_param_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_MBT_CLIENT_RETRIEVE_CAPABILITIES: + bt_mesh_free(arg->value.retrieve_capabilities_status.input.unicast_addr); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_TRANSFER_BLOB: + bt_mesh_free(arg->value.transfer_blob_status.input.unicast_addr); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_DETERMINE_TRANSFER_STATUS: + bt_mesh_free(arg->value.determine_transfer_status_status.input.unicast_addr); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_CANCEL_TRANSFER: + bt_mesh_free(arg->value.cancel_transfer_status.input.unicast_addr); + break; + default: + break; + } +} + +static void btc_ble_mesh_blob_trans_client_callback(esp_ble_mesh_mbt_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_MBT_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_MBT_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_mbt_client_cb_param_t), + btc_ble_mesh_blob_trans_client_copy_req_data, + btc_ble_mesh_blob_trans_client_free_req_data); +} + +void bt_mesh_mbt_client_cb_evt_to_btc(uint8_t event, uint8_t result, + struct bt_mesh_model *model) +{ + esp_ble_mesh_mbt_client_cb_param_t cb_params = {0}; + uint8_t cb_event = 0; + + if (model == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_MBT_CLIENT_RETRIEVE_CAPABILITIES_COMP: + cb_event = ESP_BLE_MESH_MBT_CLIENT_RETRIEVE_CAPABILITIES_COMP_EVT; + cb_params.value.retrieve_capabilities_comp.model = (esp_ble_mesh_model_t *)model; + cb_params.value.retrieve_capabilities_comp.result = result; + break; + case BTC_BLE_MESH_EVT_MBT_CLIENT_TRANSFER_BLOB_COMP: + cb_event = ESP_BLE_MESH_MBT_CLIENT_TRANSFER_BLOB_COMP_EVT; + cb_params.value.transfer_blob_comp.model = (esp_ble_mesh_model_t *)model; + cb_params.value.transfer_blob_comp.result = result; + break; + case BTC_BLE_MESH_EVT_MBT_CLIENT_SEND_BLOCK_COMP: + cb_event = ESP_BLE_MESH_MBT_CLIENT_SEND_BLOCK_COMP_EVT; + cb_params.value.send_block_comp.model = (esp_ble_mesh_model_t *)model; + cb_params.value.send_block_comp.result = result; + break; + case BTC_BLE_MESH_EVT_MBT_CLIENT_SEND_DATA_COMP: + cb_event = ESP_BLE_MESH_MBT_CLIENT_SEND_DATA_COMP_EVT; + cb_params.value.send_data_comp.model = (esp_ble_mesh_model_t *)model; + cb_params.value.send_data_comp.result = result; + break; + case BTC_BLE_MESH_EVT_MBT_CLIENT_DETERMINE_BLOCK_STATUS_COMP: + cb_event = ESP_BLE_MESH_MBT_CLIENT_DETERMINE_BLOCK_STATUS_COMP_EVT; + cb_params.value.determine_block_status_comp.model = (esp_ble_mesh_model_t *)model; + cb_params.value.determine_block_status_comp.result = result; + break; + case BTC_BLE_MESH_EVT_MBT_CLIENT_DETERMINE_TRANSFER_STATUS_COMP: + cb_event = ESP_BLE_MESH_MBT_CLIENT_DETERMINE_TRANSFER_STATUS_COMP_EVT; + cb_params.value.determine_transfer_status_comp.model = (esp_ble_mesh_model_t *)model; + cb_params.value.determine_transfer_status_comp.result = result; + break; + case BTC_BLE_MESH_EVT_MBT_CLIENT_CANCEL_TRANSFER_COMP: + cb_event = ESP_BLE_MESH_MBT_CLIENT_CANCEL_TRANSFER_COMP_EVT; + cb_params.value.cancel_transfer_comp.model = (esp_ble_mesh_model_t *)model; + cb_params.value.cancel_transfer_comp.result = result; + break; + default: + BT_ERR("Unknown Blob Transfer Client event type %d", event); + return; + } + + btc_ble_mesh_blob_trans_client_callback(&cb_params, cb_event); +} + +void btc_ble_mesh_mbt_client_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + /* TBD */ +} + +void btc_ble_mesh_mbt_client_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_mbt_client_cb_param_t cb = {0}; + btc_ble_mesh_mbt_client_args_t *arg = NULL; + uint8_t event = 0; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_mbt_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_MBT_CLIENT_RETRIEVE_CAPABILITIES: + event = ESP_BLE_MESH_MBT_CLIENT_RETRIEVE_CAPABILITIES_STATUS_EVT; + memcpy(&cb.value.retrieve_capabilities_status.input, + &arg->retrieve_capabilities, sizeof(arg->retrieve_capabilities)); + cb.value.retrieve_capabilities_status.error_code = + bt_mesh_retrieve_capabilities(&arg->retrieve_capabilities); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_TRANSFER_BLOB: + event = ESP_BLE_MESH_MBT_CLIENT_TRANSFER_BLOB_STATUS_EVT; + memcpy(&cb.value.transfer_blob_status.input, &arg->transfer_blob, sizeof(arg->transfer_blob)); + cb.value.transfer_blob_status.error_code = bt_mesh_transfer_blob(&arg->transfer_blob); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_SEND_BLOCK: + event = ESP_BLE_MESH_MBT_CLIENT_SEND_BLOCK_STATUS_EVT; + memcpy(&cb.value.send_block_status.input, &arg->send_block, sizeof(arg->send_block)); + cb.value.send_block_status.error_code = bt_mesh_send_block(&arg->send_block); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_SEND_DATA: + event = ESP_BLE_MESH_MBT_CLIENT_SEND_DATA_STATUS_EVT; + memcpy(&cb.value.send_data_status.input, &arg->send_data, sizeof(arg->send_data)); + cb.value.send_data_status.error_code = bt_mesh_send_data(&arg->send_data); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_DETERMINE_BLOCK_STATUS: + event = ESP_BLE_MESH_MBT_CLIENT_DETERMINE_BLOCK_STATUS_STATUS_EVT; + memcpy(&cb.value.determine_block_status_status.input, + &arg->determine_block_status, sizeof(arg->determine_block_status)); + cb.value.determine_block_status_status.error_code = + bt_mesh_determine_block_status(&arg->determine_block_status); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_DETERMINE_TRANSFER_STATUS: + event = ESP_BLE_MESH_MBT_CLIENT_DETERMINE_TRANSFER_STATUS_STATUS_EVT; + memcpy(&cb.value.determine_transfer_status_status.input, + &arg->determine_transfer_status, sizeof(arg->determine_transfer_status)); + cb.value.determine_transfer_status_status.error_code = + bt_mesh_determine_transfer_status(&arg->determine_transfer_status); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_CANCEL_TRANSFER: + event = ESP_BLE_MESH_MBT_CLIENT_CANCEL_TRANSFER_STATUS_EVT; + memcpy(&cb.value.cancel_transfer_status.input, &arg->cancel_transfer, sizeof(arg->cancel_transfer)); + cb.value.cancel_transfer_status.error_code = bt_mesh_cancel_transfer(&arg->cancel_transfer); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_SET_TRANSFER_TTL: + event = ESP_BLE_MESH_MBT_CLIENT_SET_TRANSFER_TTL_COMP_EVT; + cb.value.set_transfer_ttl.model = arg->set_transfer_ttl.model; + cb.value.set_transfer_ttl.transfer_ttl = arg->set_transfer_ttl.transfer_ttl; + cb.value.set_transfer_ttl.error_code = + bt_mesh_set_transfer_ttl_state((struct bt_mesh_model *)arg->set_transfer_ttl.model, + arg->set_transfer_ttl.transfer_ttl); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_CLEAR_TRANSFER_TTL: + event = ESP_BLE_MESH_MBT_CLIENT_CLEAR_TRANSFER_TTL_COMP_EVT; + cb.value.clear_transfer_ttl.model = arg->clear_transfer_ttl.model; + cb.value.clear_transfer_ttl.error_code = + bt_mesh_clear_transfer_ttl_state((struct bt_mesh_model *)arg->clear_transfer_ttl.model); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_SET_APP_IDX: + event = ESP_BLE_MESH_MBT_CLIENT_SET_APP_IDX_COMP_EVT; + cb.value.set_app_idx.model = arg->set_app_idx.model; + cb.value.set_app_idx.app_idx = arg->set_app_idx.app_idx; + cb.value.set_app_idx.error_code = + bt_mesh_set_app_idx_state((struct bt_mesh_model *)arg->set_app_idx.model, + arg->set_app_idx.app_idx); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_CLEAR_APP_IDX: + event = ESP_BLE_MESH_MBT_CLIENT_CLEAR_APP_IDX_COMP_EVT; + cb.value.clear_app_idx.model = arg->clear_app_idx.model; + cb.value.clear_app_idx.error_code = + bt_mesh_clear_app_idx_state((struct bt_mesh_model *)arg->clear_app_idx.model); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_SET_MULTICAST_ADDR: + event = ESP_BLE_MESH_MBT_CLIENT_SET_MULTICAST_ADDR_COMP_EVT; + cb.value.set_multicast_addr.model = arg->set_multicast_addr.model; + cb.value.set_multicast_addr.multicast_addr = arg->set_multicast_addr.multicast_addr; + cb.value.set_multicast_addr.error_code = + bt_mesh_set_multicast_addr_state((struct bt_mesh_model *)arg->set_multicast_addr.model, + arg->set_multicast_addr.multicast_addr); + break; + case BTC_BLE_MESH_ACT_MBT_CLIENT_CLEAR_MULTICAST_ADDR: + event = ESP_BLE_MESH_MBT_CLIENT_CLEAR_MULTICAST_ADDR_COMP_EVT; + cb.value.clear_multicast_addr.model = arg->clear_multicast_addr.model; + cb.value.clear_multicast_addr.error_code = + bt_mesh_clear_multicast_addr_state((struct bt_mesh_model *)arg->clear_multicast_addr.model); + break; + default: + BT_ERR("Unknown BLOB Transfer Client act %d", msg->act); + return; + } + + btc_ble_mesh_blob_trans_client_callback(&cb, event); + + btc_ble_mesh_mbt_client_arg_deep_free(msg); +} + +void btc_ble_mesh_mbt_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_mbt_client_cb_param_t *param = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_mbt_client_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_MBT_CLIENT_EVT_MAX) { + btc_ble_mesh_blob_trans_client_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } + + btc_ble_mesh_blob_trans_client_free_req_data(msg); +} + +#endif /* CONFIG_BLE_MESH_MBT_CLI */ + +#if CONFIG_BLE_MESH_MBT_SRV + +static inline void btc_ble_mesh_blob_trans_server_cb_to_app(esp_ble_mesh_mbt_server_cb_event_t event, + esp_ble_mesh_mbt_server_cb_param_t *param) +{ + esp_ble_mesh_mbt_server_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_mbt_server_cb_t)btc_profile_cb_get(BTC_PID_MBT_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_blob_trans_server_callback(esp_ble_mesh_mbt_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_MBT_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_MBT_SERVER; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_mbt_server_cb_param_t), NULL, NULL); +} + +void bt_mesh_mbt_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx) +{ + esp_ble_mesh_mbt_server_cb_param_t cb_params = {0}; + uint8_t cb_event = 0; + + if (model == NULL || (ctx == NULL && + event != BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_RECEIVE_TIMEOUT)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_TRANSFER_GET: + cb_event = ESP_BLE_MESH_MBT_SERVER_BLOB_TRANSFER_GET_EVT; + memcpy(&cb_params.value.blob_transfer_get.ctx, ctx, sizeof(struct bt_mesh_msg_ctx)); + break; + case BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_TRANSFER_START: + cb_event = ESP_BLE_MESH_MBT_SERVER_BLOB_TRANSFER_START_EVT; + memcpy(&cb_params.value.blob_transfer_start.ctx, ctx, sizeof(struct bt_mesh_msg_ctx)); + break; + case BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_TRANSFER_CANCEL: + cb_event = ESP_BLE_MESH_MBT_SERVER_BLOB_TRANSFER_CANCEL_EVT; + memcpy(&cb_params.value.blob_transfer_cancel.ctx, ctx, sizeof(struct bt_mesh_msg_ctx)); + break; + case BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_BLOCK_GET: + cb_event = ESP_BLE_MESH_MBT_SERVER_BLOB_BLOCK_GET_EVT; + memcpy(&cb_params.value.blob_block_get.ctx, ctx, sizeof(struct bt_mesh_msg_ctx)); + break; + case BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_BLOCK_START: + cb_event = ESP_BLE_MESH_MBT_SERVER_BLOB_BLOCK_START_EVT; + memcpy(&cb_params.value.blob_block_start.ctx, ctx, sizeof(struct bt_mesh_msg_ctx)); + break; + case BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_CHUNK_TRANSFER: + cb_event = ESP_BLE_MESH_MBT_SERVER_BLOB_CHUNK_TRANSFER_EVT; + memcpy(&cb_params.value.blob_chunk_transfer.ctx, ctx, sizeof(struct bt_mesh_msg_ctx)); + break; + case BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_INFORMATION_GET: + cb_event = ESP_BLE_MESH_MBT_SERVER_BLOB_INFORMATION_GET_EVT; + memcpy(&cb_params.value.blob_information_get.ctx, ctx, sizeof(struct bt_mesh_msg_ctx)); + break; + case BTC_BLE_MESH_EVT_MBT_SERVER_BLOCK_RECEIVE_COMP: + cb_event = ESP_BLE_MESH_MBT_SERVER_BLOCK_RECEIVE_COMP_EVT; + memcpy(&cb_params.value.block_receive_comp.ctx, ctx, sizeof(struct bt_mesh_msg_ctx)); + break; + case BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_RECEIVE_COMP: + cb_event = ESP_BLE_MESH_MBT_SERVER_BLOB_RECEIVE_COMP_EVT; + memcpy(&cb_params.value.blob_receive_comp.ctx, ctx, sizeof(struct bt_mesh_msg_ctx)); + break; + case BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_RECEIVE_TIMEOUT: + cb_event = ESP_BLE_MESH_MBT_SERVER_BLOB_RECEIVE_TIMEOUT_EVT; + break; + default: + BT_ERR("Unknown Blob Transfer server event type %d", event); + return; + } + + btc_ble_mesh_blob_trans_server_callback(&cb_params, cb_event); +} + +void btc_ble_mesh_mbt_server_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_mbt_server_cb_param_t cb = {0}; + btc_ble_mesh_mbt_server_args_t *arg = NULL; + uint8_t event = 0; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_mbt_server_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_MBT_SERVER_INITIALIZE_BLOB_RECEIVE: + event = ESP_BLE_MESH_MBT_SERVER_INITIALIZE_BLOB_RECEIVE_COMP_EVT; + memcpy(&cb.value.initialize_blob_receive_comp.input, + &arg->initialize_blob_receive, sizeof(arg->initialize_blob_receive)); + cb.value.initialize_blob_receive_comp.error_code = + bt_mesh_initialize_blob_receive(&arg->initialize_blob_receive); + break; + case BTC_BLE_MESH_ACT_MBT_SERVER_CANCEL_BLOB_RECEIVE: + event = ESP_BLE_MESH_MBT_SERVER_CANCEL_BLOB_RECEIVE_COMP_EVT; + memcpy(&cb.value.cancel_blob_receive_comp.input, + &arg->cancel_blob_receive, sizeof(arg->cancel_blob_receive)); + cb.value.cancel_blob_receive_comp.error_code = + bt_mesh_cancel_blob_receive(&arg->cancel_blob_receive); + break; + case BTC_BLE_MESH_ACT_MBT_SERVER_SET_BLOB_CAPABILITIES: + event = ESP_BLE_MESH_MBT_SERVER_SET_BLOB_CAPABILITIES_COMP_EVT; + memcpy(&cb.value.set_blob_capabilities_comp.input, + &arg->set_blob_capabilities, sizeof(arg->set_blob_capabilities)); + cb.value.set_blob_capabilities_comp.error_code = + bt_mesh_set_blob_capabilities((struct bt_mesh_model *)arg->set_blob_capabilities.model, + &arg->set_blob_capabilities.caps); + break; + default: + BT_ERR("Unknown BLOB Transfer Server act %d", msg->act); + return; + } + + btc_ble_mesh_blob_trans_server_callback(&cb, event); +} + +void btc_ble_mesh_mbt_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_mbt_server_cb_param_t *param = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_mbt_server_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_MBT_SERVER_EVT_MAX) { + btc_ble_mesh_blob_trans_server_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } +} + +#endif /* CONFIG_BLE_MESH_MBT_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_odp_model.c b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_odp_model.c new file mode 100644 index 000000000000..52d89880a6ed --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_odp_model.c @@ -0,0 +1,380 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "btc_ble_mesh_model_common.h" +#include "btc_ble_mesh_odp_model.h" +#include "esp_ble_mesh_odp_model_api.h" + +#if CONFIG_BLE_MESH_ODP_CLI + +extern int bt_mesh_odp_get(void *param); +extern int bt_mesh_odp_set(void *param, uint8_t on_demand_private_gatt_proxy); + +/* On-Demand Private Proxy Configuration Client model related functions */ + +static inline void btc_ble_mesh_odp_client_cb_to_app(esp_ble_mesh_odp_client_cb_event_t event, + esp_ble_mesh_odp_client_cb_param_t *param) +{ + esp_ble_mesh_odp_client_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_ODP_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_odp_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_odp_client_args_t *dst = p_dest; + btc_ble_mesh_odp_client_args_t *src = p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_ODP_CLIENT_SEND: + dst->odp_send.params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->odp_send.params) { + memcpy(dst->odp_send.params, src->odp_send.params, + sizeof(esp_ble_mesh_client_common_param_t)); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + break; + } + if (src->odp_send.msg) { + dst->odp_send.msg = bt_mesh_calloc(sizeof(esp_ble_mesh_odp_client_msg_t)); + if (dst->odp_send.msg) { + memcpy(dst->odp_send.msg, src->odp_send.msg, + sizeof(esp_ble_mesh_odp_client_msg_t)); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; + default: + BT_DBG("%s, Unknown act %d", __func__, msg->act); + break; + } +} + +void btc_ble_mesh_odp_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_odp_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_odp_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_ODP_CLIENT_SEND: + if (arg->odp_send.params) { + bt_mesh_free(arg->odp_send.params); + } + if (arg->odp_send.msg) { + bt_mesh_free(arg->odp_send.msg); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_odp_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_odp_client_cb_param_t *p_dest_data = p_dest; + esp_ble_mesh_odp_client_cb_param_t *p_src_data = p_src; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } +} + +static void btc_ble_mesh_odp_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_odp_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_odp_client_cb_param_t *)msg->arg; + + switch (msg->act) { + case ESP_BLE_MESH_ODP_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_ODP_CLIENT_RECV_RSP_EVT: + case ESP_BLE_MESH_ODP_CLIENT_RECV_PUB_EVT: + case ESP_BLE_MESH_ODP_CLIENT_SEND_TIMEOUT_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_odp_client_cb(esp_ble_mesh_odp_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_ODP_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_ODP_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_odp_client_cb_param_t), + btc_ble_mesh_odp_client_copy_req_data, + btc_ble_mesh_odp_client_free_req_data); +} + +void bt_mesh_odp_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len) +{ + esp_ble_mesh_odp_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + uint8_t act = 0; + + if (!model || !ctx || len > sizeof(cb_params.recv)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_ODP_CLIENT_SEND_TIMEOUT: + act = ESP_BLE_MESH_ODP_CLIENT_SEND_TIMEOUT_EVT; + break; + case BTC_BLE_MESH_EVT_ODP_CLIENT_RECV_RSP: + act = ESP_BLE_MESH_ODP_CLIENT_RECV_RSP_EVT; + break; + case BTC_BLE_MESH_EVT_ODP_CLIENT_RECV_PUB: + act = ESP_BLE_MESH_ODP_CLIENT_RECV_PUB_EVT; + break; + default: + BT_ERR("Unknown On-Demand Private Proxy client event type %d", event); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + params.ctx.recv_rssi = ctx->recv_rssi; + params.ctx.send_ttl = ctx->send_ttl; + + cb_params.params = ¶ms; + + if (val && len) { + memcpy(&cb_params.recv, val, len); + } + + btc_ble_mesh_odp_client_cb(&cb_params, act); +} + +void btc_ble_mesh_odp_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_odp_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_ODP_CLIENT_RECV_PUB, + model, ctx, buf->data, buf->len); +} + +static int btc_ble_mesh_odp_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_odp_client_msg_t *msg) +{ + bt_mesh_client_common_param_t param = {0}; + + if (params == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (params->opcode == ESP_BLE_MESH_MODEL_OP_OD_PRIV_PROXY_SET && msg == NULL) { + BT_ERR("Invalid On-Demand Private Proxy message, opcode 0x%04x", params->opcode); + return -EINVAL; + } + + btc_ble_mesh_set_client_common_param(params, ¶m, true); + + switch (param.opcode) { + case ESP_BLE_MESH_MODEL_OP_OD_PRIV_PROXY_GET: + return bt_mesh_odp_get(¶m); + case ESP_BLE_MESH_MODEL_OP_OD_PRIV_PROXY_SET: + return bt_mesh_odp_set(¶m, msg->od_priv_proxy_set.gatt_proxy); + default: + BT_ERR("Invalid On-Demand Private Proxy opcode 0x%04x", param.opcode); + return -EINVAL; + } +} + +void btc_ble_mesh_odp_client_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_odp_client_cb_param_t cb = {0}; + btc_ble_mesh_odp_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_odp_client_args_t *)msg->arg; + + if (msg->act == BTC_BLE_MESH_ACT_ODP_CLIENT_SEND) { + cb.params = arg->odp_send.params; + cb.send.err_code = btc_ble_mesh_odp_client_send(arg->odp_send.params, + arg->odp_send.msg); + btc_ble_mesh_odp_client_cb(&cb, + ESP_BLE_MESH_ODP_CLIENT_SEND_COMP_EVT); + } + + btc_ble_mesh_odp_client_arg_deep_free(msg); +} + +void btc_ble_mesh_odp_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_odp_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_odp_client_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_ODP_CLIENT_EVT_MAX) { + btc_ble_mesh_odp_client_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } + + btc_ble_mesh_odp_client_free_req_data(msg); +} + +#endif /* CONFIG_BLE_MESH_ODP_CLI */ + +#if CONFIG_BLE_MESH_ODP_SRV + +/* On-Demand Private Proxy Config Server model related functions */ + +static inline void btc_ble_mesh_odp_server_cb_to_app(esp_ble_mesh_odp_server_cb_event_t event, + esp_ble_mesh_odp_server_cb_param_t *param) +{ + esp_ble_mesh_odp_server_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_ODP_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_odp_server_cb(esp_ble_mesh_odp_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_ODP_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_ODP_SERVER; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_odp_server_cb_param_t), NULL, NULL); +} + +void bt_mesh_odp_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len) +{ + esp_ble_mesh_odp_server_cb_param_t cb_params = {0}; + uint8_t act = 0; + + if (!model || !ctx || len > sizeof(cb_params.value)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_ODP_SERVER_STATE_CHANGE: + act = ESP_BLE_MESH_ODP_SERVER_STATE_CHANGE_EVT; + break; + default: + BT_ERR("Unknown On-Demand Private Proxy server event type %d", event); + return; + } + + cb_params.model = (esp_ble_mesh_model_t *)model; + cb_params.ctx.net_idx = ctx->net_idx; + cb_params.ctx.app_idx = ctx->app_idx; + cb_params.ctx.addr = ctx->addr; + cb_params.ctx.recv_ttl = ctx->recv_ttl; + cb_params.ctx.recv_op = ctx->recv_op; + cb_params.ctx.recv_dst = ctx->recv_dst; + cb_params.ctx.recv_rssi = ctx->recv_rssi; + cb_params.ctx.send_ttl = ctx->send_ttl; + + if (val && len) { + memcpy(&cb_params.value, val, len); + } + + btc_ble_mesh_odp_server_cb(&cb_params, act); +} + +void btc_ble_mesh_odp_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_odp_server_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_odp_server_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_ODP_SERVER_EVT_MAX) { + btc_ble_mesh_odp_server_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } +} + +#endif /* CONFIG_BLE_MESH_ODP_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_prb_model.c b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_prb_model.c new file mode 100644 index 000000000000..837fc79b5708 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_prb_model.c @@ -0,0 +1,404 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "btc_ble_mesh_model_common.h" +#include "btc_ble_mesh_prb_model.h" +#include "esp_ble_mesh_prb_model_api.h" + +#if CONFIG_BLE_MESH_PRB_CLI + +extern int bt_mesh_private_beacon_get(void *param); +extern int bt_mesh_private_beacon_set(void *param, void *set); +extern int bt_mesh_private_gatt_proxy_get(void *param); +extern int bt_mesh_private_gatt_proxy_set(void *param, uint8_t private_gatt_proxy); +extern int bt_mesh_private_node_identity_get(void *param, uint16_t net_idx); +extern int bt_mesh_private_node_identity_set(void *param, uint16_t net_idx, + uint8_t private_node_id); + +/* Private Beacon Client Model related functions */ + +static inline void btc_ble_mesh_prb_client_cb_to_app(esp_ble_mesh_prb_client_cb_event_t event, + esp_ble_mesh_prb_client_cb_param_t *param) +{ + esp_ble_mesh_prb_client_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_PRB_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_prb_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_prb_client_args_t *dst = p_dest; + btc_ble_mesh_prb_client_args_t *src = p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_PRB_CLIENT_SEND: + dst->prb_send.params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->prb_send.params) { + memcpy(dst->prb_send.params, src->prb_send.params, + sizeof(esp_ble_mesh_client_common_param_t)); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + break; + } + if (src->prb_send.msg) { + dst->prb_send.msg = bt_mesh_malloc(sizeof(esp_ble_mesh_prb_client_msg_t)); + if (dst->prb_send.msg) { + memcpy(dst->prb_send.msg, src->prb_send.msg, + sizeof(esp_ble_mesh_prb_client_msg_t)); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; + default: + BT_DBG("%s, Unknown act %d", __func__, msg->act); + break; + } +} + +void btc_ble_mesh_prb_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_prb_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_prb_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_PRB_CLIENT_SEND: + if (arg->prb_send.params) { + bt_mesh_free(arg->prb_send.params); + } + if (arg->prb_send.msg) { + bt_mesh_free(arg->prb_send.msg); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_prb_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_prb_client_cb_param_t *p_dest_data = p_dest; + esp_ble_mesh_prb_client_cb_param_t *p_src_data = p_src; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } +} + +static void btc_ble_mesh_prb_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_prb_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_prb_client_cb_param_t *)(msg->arg); + + switch (msg->act) { + case ESP_BLE_MESH_PRB_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_PRB_CLIENT_SEND_TIMEOUT_EVT: + case ESP_BLE_MESH_PRB_CLIENT_RECV_RSP_EVT: + case ESP_BLE_MESH_PRB_CLIENT_RECV_PUB_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_prb_client_cb(esp_ble_mesh_prb_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t btc_msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_PRB_CLIENT)) { + return; + } + + btc_msg.sig = BTC_SIG_API_CB; + btc_msg.pid = BTC_PID_PRB_CLIENT; + btc_msg.act = act; + + btc_transfer_context(&btc_msg, cb_params, sizeof(esp_ble_mesh_prb_client_cb_param_t), + btc_ble_mesh_prb_client_copy_req_data, + btc_ble_mesh_prb_client_free_req_data); +} + +void bt_mesh_prb_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, uint16_t len) +{ + esp_ble_mesh_prb_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + uint8_t act = 0U; + + if (!model || !ctx || len > sizeof(cb_params.recv)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_PRB_CLIENT_SEND_TIMEOUT: + act = ESP_BLE_MESH_PRB_CLIENT_SEND_TIMEOUT_EVT; + break; + case BTC_BLE_MESH_EVT_PRB_CLIENT_RECV_RSP: + act = ESP_BLE_MESH_PRB_CLIENT_RECV_RSP_EVT; + break; + case BTC_BLE_MESH_EVT_PRB_CLIENT_RECV_PUB: + act = ESP_BLE_MESH_PRB_CLIENT_RECV_PUB_EVT; + break; + default: + BT_ERR("Unknown Private Beacon client event type %d", evt_type); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + params.ctx.recv_rssi = ctx->recv_rssi; + params.ctx.send_ttl = ctx->send_ttl; + + cb_params.params = ¶ms; + + if (val && len) { + memcpy(&cb_params.recv, val, len); + } + + btc_ble_mesh_prb_client_cb(&cb_params, act); +} + +void btc_ble_mesh_prb_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_prb_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_PRB_CLIENT_RECV_PUB, + model, ctx, buf->data, buf->len); +} + +static int btc_ble_mesh_prb_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_prb_client_msg_t *msg) +{ + bt_mesh_client_common_param_t param = {0}; + + if (params == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + switch (params->opcode) { + case ESP_BLE_MESH_MODEL_OP_PRIV_BEACON_SET: + case ESP_BLE_MESH_MODEL_OP_PRIV_GATT_PROXY_SET: + case ESP_BLE_MESH_MODEL_OP_PRIV_NODE_IDENTITY_GET: + case ESP_BLE_MESH_MODEL_OP_PRIV_NODE_IDENTITY_SET: + if (msg == NULL) { + BT_ERR("Invalid Mesh Private Beacon message, opcode 0x%04x", params->opcode); + return -EINVAL; + } + break; + default: + break; + } + + btc_ble_mesh_set_client_common_param(params, ¶m, true); + + switch (param.opcode) { + case ESP_BLE_MESH_MODEL_OP_PRIV_BEACON_GET: + return bt_mesh_private_beacon_get(¶m); + case ESP_BLE_MESH_MODEL_OP_PRIV_BEACON_SET: + return bt_mesh_private_beacon_set(¶m, &msg->priv_beacon_set); + case ESP_BLE_MESH_MODEL_OP_PRIV_GATT_PROXY_GET: + return bt_mesh_private_gatt_proxy_get(¶m); + case ESP_BLE_MESH_MODEL_OP_PRIV_GATT_PROXY_SET: + return bt_mesh_private_gatt_proxy_set(¶m, msg->priv_gatt_proxy_set.private_gatt_proxy); + case ESP_BLE_MESH_MODEL_OP_PRIV_NODE_IDENTITY_GET: + return bt_mesh_private_node_identity_get(¶m, msg->priv_node_id_get.net_idx); + case ESP_BLE_MESH_MODEL_OP_PRIV_NODE_IDENTITY_SET: + return bt_mesh_private_node_identity_set(¶m, msg->priv_node_id_set.net_idx , msg->priv_node_id_set.private_node_id); + default: + BT_ERR("Invalid Private Beacon opcode 0x%04x", param.opcode); + return -EINVAL; + } +} + +void btc_ble_mesh_prb_client_call_handler(btc_msg_t *msg) +{ + btc_ble_mesh_prb_client_args_t *arg = NULL; + esp_ble_mesh_prb_client_cb_param_t cb = {0}; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_prb_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_PRB_CLIENT_SEND: + cb.params = arg->prb_send.params; + cb.send.err_code = btc_ble_mesh_prb_client_send(arg->prb_send.params, + arg->prb_send.msg); + btc_ble_mesh_prb_client_cb(&cb, + ESP_BLE_MESH_PRB_CLIENT_SEND_COMP_EVT); + break; + default: + break; + } + + btc_ble_mesh_prb_client_arg_deep_free(msg); +} + +void btc_ble_mesh_prb_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_prb_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_prb_client_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_PRB_CLIENT_EVT_MAX) { + btc_ble_mesh_prb_client_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } + + btc_ble_mesh_prb_client_free_req_data(msg); +} +#endif /* CONFIG_BLE_MESH_PRB_CLI */ + +#if CONFIG_BLE_MESH_PRB_SRV +/* Private Beacon Server Model related functions */ + +static inline void btc_ble_mesh_prb_server_cb_to_app(esp_ble_mesh_prb_server_cb_event_t event, + esp_ble_mesh_prb_server_cb_param_t *param) +{ + esp_ble_mesh_prb_server_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_PRB_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_prb_server_cb( + esp_ble_mesh_prb_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_PRB_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_PRB_SERVER; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_prb_server_cb_param_t), NULL, NULL); +} + +void bt_mesh_prb_server_cb_evt_to_btc(uint8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len) +{ + esp_ble_mesh_prb_server_cb_param_t cb_params = {0}; + uint8_t act = 0U; + + if (!model || !ctx || len > sizeof(cb_params.value)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_PRB_SERVER_STATE_CHANGE: + act = ESP_BLE_MESH_PRB_SERVER_STATE_CHANGE_EVT; + break; + default: + BT_ERR("Unknown Private Beacon server event type %d", evt_type); + return; + } + + cb_params.model = (esp_ble_mesh_model_t *)model; + cb_params.ctx.net_idx = ctx->net_idx; + cb_params.ctx.app_idx = ctx->app_idx; + cb_params.ctx.addr = ctx->addr; + cb_params.ctx.recv_ttl = ctx->recv_ttl; + cb_params.ctx.recv_op = ctx->recv_op; + cb_params.ctx.recv_dst = ctx->recv_dst; + cb_params.ctx.recv_rssi = ctx->recv_rssi; + cb_params.ctx.send_ttl = ctx->send_ttl; + + if (val && len) { + memcpy(&cb_params.value, val, len); + } + + btc_ble_mesh_prb_server_cb(&cb_params, act); +} + +void btc_ble_mesh_prb_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_prb_server_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_prb_server_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_PRB_SERVER_EVT_MAX) { + btc_ble_mesh_prb_server_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } +} +#endif /* CONFIG_BLE_MESH_PRB_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_rpr_model.c b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_rpr_model.c new file mode 100644 index 000000000000..5e4437f4b1f1 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_rpr_model.c @@ -0,0 +1,543 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "btc_ble_mesh_model_common.h" +#include "btc_ble_mesh_rpr_model.h" +#include "esp_ble_mesh_rpr_model_api.h" + +#if CONFIG_BLE_MESH_RPR_CLI + +extern int bt_mesh_rpr_scan_caps_get(void *param); +extern int bt_mesh_rpr_scan_get(void *param); +extern int bt_mesh_rpr_scan_start(void *param, void *start); +extern int bt_mesh_rpr_scan_stop(void *param); +extern int bt_mesh_rpr_ext_scan_start(void *param, void *start); +extern int bt_mesh_rpr_link_get(void *param); +extern int bt_mesh_rpr_link_open(void *param, void *open); +extern int bt_mesh_rpr_link_close(void *param, uint8_t reason); +extern int bt_mesh_rpr_start_prov(void *model, uint16_t rp_srv_addr); + +/* Remote Provisioning Client model related functions */ + +static inline void btc_ble_mesh_rpr_client_cb_to_app(esp_ble_mesh_rpr_client_cb_event_t event, + esp_ble_mesh_rpr_client_cb_param_t *param) +{ + esp_ble_mesh_rpr_client_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_RPR_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_rpr_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_rpr_client_args_t *dst = p_dest; + btc_ble_mesh_rpr_client_args_t *src = p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_RPR_CLIENT_SEND: + dst->rpr_send.params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->rpr_send.msg = src->rpr_send.msg ? bt_mesh_calloc(sizeof(esp_ble_mesh_rpr_client_msg_t)) : NULL; + if (dst->rpr_send.params) { + memcpy(dst->rpr_send.params, src->rpr_send.params, + sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->rpr_send.msg) { + memcpy(dst->rpr_send.msg, src->rpr_send.msg, + sizeof(esp_ble_mesh_rpr_client_msg_t)); + } + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + break; + case BTC_BLE_MESH_ACT_RPR_CLIENT_ACT: + dst->rpr_act.param = bt_mesh_calloc(sizeof(esp_ble_mesh_rpr_client_act_param_t)); + if (dst->rpr_act.param) { + memcpy(dst->rpr_act.param, src->rpr_act.param, + sizeof(esp_ble_mesh_rpr_client_act_param_t)); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + break; + default: + BT_DBG("%s, Unknown act %d", __func__, msg->act); + break; + } +} + +void btc_ble_mesh_rpr_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_rpr_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_rpr_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_RPR_CLIENT_SEND: + if (arg->rpr_send.params) { + bt_mesh_free(arg->rpr_send.params); + } + break; + case BTC_BLE_MESH_ACT_RPR_CLIENT_ACT: + if (arg->rpr_act.param) { + bt_mesh_free(arg->rpr_act.param); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_rpr_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_rpr_client_cb_param_t *p_dest_data = p_dest; + esp_ble_mesh_rpr_client_cb_param_t *p_src_data = p_src; + uint16_t length = 0; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case ESP_BLE_MESH_RPR_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_RPR_CLIENT_SEND_TIMEOUT_EVT: + if (p_src_data->send.params) { + p_dest_data->send.params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->send.params) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->send.params, p_src_data->send.params, sizeof(esp_ble_mesh_client_common_param_t)); + } + break; + case ESP_BLE_MESH_RPR_CLIENT_RECV_RSP_EVT: + case ESP_BLE_MESH_RPR_CLIENT_RECV_PUB_EVT: + if (p_src_data->recv.params) { + p_dest_data->recv.params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->recv.params) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->recv.params, p_src_data->recv.params, sizeof(esp_ble_mesh_client_common_param_t)); + + /* Remote Provisioning Extended Scan Start is an unacknowledged message, + * so the corresponding report could only be received through the recv + * publish event. + */ + if (msg->act == ESP_BLE_MESH_RPR_CLIENT_RECV_PUB_EVT && + p_src_data->recv.params->opcode == ESP_BLE_MESH_MODEL_OP_RPR_EXT_SCAN_REPORT) { + if (p_src_data->recv.val.ext_scan_report.adv_structures) { + length = p_src_data->recv.val.ext_scan_report.adv_structures->len; + p_dest_data->recv.val.ext_scan_report.adv_structures = bt_mesh_alloc_buf(length); + if (!p_dest_data->recv.val.ext_scan_report.adv_structures) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + net_buf_simple_add_mem(p_dest_data->recv.val.ext_scan_report.adv_structures, + p_src_data->recv.val.ext_scan_report.adv_structures->data, + p_src_data->recv.val.ext_scan_report.adv_structures->len); + } + } + } + break; + case ESP_BLE_MESH_RPR_CLIENT_ACT_COMP_EVT: + break; + default: + break; + } +} + +static void btc_ble_mesh_rpr_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_rpr_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_rpr_client_cb_param_t *)msg->arg; + + switch (msg->act) { + case ESP_BLE_MESH_RPR_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_RPR_CLIENT_SEND_TIMEOUT_EVT: + if (arg->send.params) { + bt_mesh_free(arg->send.params); + } + break; + case ESP_BLE_MESH_RPR_CLIENT_RECV_RSP_EVT: + case ESP_BLE_MESH_RPR_CLIENT_RECV_PUB_EVT: + if (arg->recv.params && + msg->act == ESP_BLE_MESH_RPR_CLIENT_RECV_PUB_EVT && + arg->recv.params->opcode == ESP_BLE_MESH_MODEL_OP_RPR_EXT_SCAN_REPORT) { + bt_mesh_free_buf(arg->recv.val.ext_scan_report.adv_structures); + } + if (arg->recv.params) { + bt_mesh_free(arg->recv.params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_rpr_client_cb(esp_ble_mesh_rpr_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_RPR_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_RPR_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_rpr_client_cb_param_t), + btc_ble_mesh_rpr_client_copy_req_data, + btc_ble_mesh_rpr_client_free_req_data); +} + +void bt_mesh_rpr_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const void *val, size_t len) +{ + esp_ble_mesh_rpr_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + uint8_t act = 0; + + if (model == NULL || ctx == NULL || + ((event == BTC_BLE_MESH_EVT_RPR_CLIENT_RECV_RSP || + event == BTC_BLE_MESH_EVT_RPR_CLIENT_RECV_PUB) && + (len > sizeof(cb_params.recv.val)))) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + params.ctx.recv_rssi = ctx->recv_rssi; + params.ctx.send_ttl = ctx->send_ttl; + + switch (event) { + case BTC_BLE_MESH_EVT_RPR_CLIENT_SEND_TIMEOUT: + act = ESP_BLE_MESH_RPR_CLIENT_SEND_TIMEOUT_EVT; + cb_params.send.params = ¶ms; + break; + case BTC_BLE_MESH_EVT_RPR_CLIENT_RECV_RSP: + act = ESP_BLE_MESH_RPR_CLIENT_RECV_RSP_EVT; + cb_params.recv.params = ¶ms; + if (val && len) { + memcpy(&cb_params.recv.val, val, len); + } + break; + case BTC_BLE_MESH_EVT_RPR_CLIENT_RECV_PUB: + act = ESP_BLE_MESH_RPR_CLIENT_RECV_PUB_EVT; + cb_params.recv.params = ¶ms; + if (val && len) { + memcpy(&cb_params.recv.val, val, len); + } + break; + default: + BT_ERR("Unknown Remote Provisioning client event type %d", event); + return; + } + + btc_ble_mesh_rpr_client_cb(&cb_params, act); +} + +void btc_ble_mesh_rpr_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_rpr_client_cb_evt_to_btc(opcode, BTC_BLE_MESH_EVT_RPR_CLIENT_RECV_PUB, + model, ctx, buf->data, buf->len); +} + +void btc_ble_mesh_rpr_client_link_close_cb(struct bt_mesh_model *model, + uint16_t rpr_srv_addr, uint8_t reason) +{ + esp_ble_mesh_rpr_client_cb_param_t cb_params = {0}; + + cb_params.link_close.model = (esp_ble_mesh_model_t *)model; + cb_params.link_close.rpr_srv_addr = rpr_srv_addr; + cb_params.link_close.reason = reason; + + btc_ble_mesh_rpr_client_cb(&cb_params, ESP_BLE_MESH_RPR_CLIENT_LINK_CLOSE_EVT); +} + +void btc_ble_mesh_rpr_client_prov_comp_cb(struct bt_mesh_model *model, uint16_t rpr_srv_addr, + uint8_t nppi, uint16_t index, uint8_t uuid[16], + uint16_t unicast_addr, uint8_t element_num, + uint16_t net_idx) +{ + esp_ble_mesh_rpr_client_cb_param_t cb_params = {0}; + + cb_params.prov.model = (esp_ble_mesh_model_t *)model; + cb_params.prov.rpr_srv_addr = rpr_srv_addr; + cb_params.prov.nppi = nppi; + cb_params.prov.index = index; + cb_params.prov.unicast_addr = unicast_addr; + cb_params.prov.element_num = element_num; + cb_params.prov.net_idx = net_idx; + memcpy(cb_params.prov.uuid, uuid, 16); + + btc_ble_mesh_rpr_client_cb(&cb_params, ESP_BLE_MESH_RPR_CLIENT_PROV_COMP_EVT); +} + +static int btc_ble_mesh_rpr_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_rpr_client_msg_t *msg) +{ + bt_mesh_client_common_param_t param = {0}; + + if (params == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + switch (params->opcode) { + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START: + case ESP_BLE_MESH_MODEL_OP_RPR_EXT_SCAN_START: + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN: + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_CLOSE: + if (msg == NULL) { + BT_ERR("Invalid Remote Provisioning message opcode 0x%04x", params->opcode); + return -EINVAL; + } + break; + default: + break; + } + + btc_ble_mesh_set_client_common_param(params, ¶m, true); + + switch (param.opcode) { + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_CAPS_GET: + return bt_mesh_rpr_scan_caps_get(¶m); + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET: + return bt_mesh_rpr_scan_get(¶m); + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START: + return bt_mesh_rpr_scan_start(¶m, &msg->scan_start); + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_STOP: + return bt_mesh_rpr_scan_stop(¶m); + case ESP_BLE_MESH_MODEL_OP_RPR_EXT_SCAN_START: + return bt_mesh_rpr_ext_scan_start(¶m, &msg->ext_scan_start); + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET: + return bt_mesh_rpr_link_get(¶m); + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN: + return bt_mesh_rpr_link_open(¶m, &msg->link_open); + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_CLOSE: + return bt_mesh_rpr_link_close(¶m, msg->link_close.reason); + case ESP_BLE_MESH_MODEL_OP_RPR_PDU_SEND: + BT_WARN("Remote Provisioning PDU Send will be sent internally"); + return 0; + default: + BT_ERR("Invalid Remote Provisioning msg opcode 0x%04x", param.opcode); + return -EINVAL; + } +} + +static int btc_ble_mesh_rpr_client_act(esp_ble_mesh_rpr_client_act_type_t type, + esp_ble_mesh_rpr_client_act_param_t *param, + esp_ble_mesh_rpr_client_cb_param_t *cb) +{ + if (param == NULL || cb == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + switch (type) { + case ESP_BLE_MESH_RPR_CLIENT_ACT_START_RPR: + cb->act.sub_evt = ESP_BLE_MESH_START_RPR_COMP_SUB_EVT; + cb->act.start_rpr_comp.model = param->start_rpr.model; + cb->act.start_rpr_comp.rpr_srv_addr = param->start_rpr.rpr_srv_addr; + cb->act.start_rpr_comp.err_code = + bt_mesh_rpr_start_prov((struct bt_mesh_model *)param->start_rpr.model, + param->start_rpr.rpr_srv_addr); + break; + default: + BT_ERR("Invalid Remote Provisioning action 0x%02x", type); + return -EINVAL; + } + + return 0; +} + +void btc_ble_mesh_rpr_client_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_rpr_client_cb_param_t cb = {0}; + btc_ble_mesh_rpr_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_rpr_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_RPR_CLIENT_SEND: + cb.send.params = arg->rpr_send.params; + cb.send.err_code = btc_ble_mesh_rpr_client_send(arg->rpr_send.params, + arg->rpr_send.msg); + btc_ble_mesh_rpr_client_cb(&cb, ESP_BLE_MESH_RPR_CLIENT_SEND_COMP_EVT); + break; + case BTC_BLE_MESH_ACT_RPR_CLIENT_ACT: + btc_ble_mesh_rpr_client_act(arg->rpr_act.type, + arg->rpr_act.param, &cb); + btc_ble_mesh_rpr_client_cb(&cb, ESP_BLE_MESH_RPR_CLIENT_ACT_COMP_EVT); + break; + default: + break; + } + + btc_ble_mesh_rpr_client_arg_deep_free(msg); +} + +void btc_ble_mesh_rpr_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_rpr_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_rpr_client_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_RPR_CLIENT_EVT_MAX) { + btc_ble_mesh_rpr_client_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } + + btc_ble_mesh_rpr_client_free_req_data(msg); +} + +#endif /* CONFIG_BLE_MESH_RPR_CLI */ + +#if CONFIG_BLE_MESH_RPR_SRV + +/* Remote Provisioning Server model related functions */ + +static inline void btc_ble_mesh_rpr_server_cb_to_app(esp_ble_mesh_rpr_server_cb_event_t event, + esp_ble_mesh_rpr_server_cb_param_t *param) +{ + esp_ble_mesh_rpr_server_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_RPR_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_rpr_server_cb(esp_ble_mesh_rpr_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_RPR_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_RPR_SERVER; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_rpr_server_cb_param_t), NULL, NULL); +} + +void bt_mesh_rpr_server_cb_evt_to_btc(uint8_t event, const void *val, size_t len) +{ + esp_ble_mesh_rpr_server_cb_param_t cb_params = {0}; + uint8_t act = 0; + + if (len > sizeof(cb_params)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_RPR_SERVER_SCAN_START: + act = ESP_BLE_MESH_RPR_SERVER_SCAN_START_EVT; + break; + case BTC_BLE_MESH_EVT_RPR_SERVER_SCAN_STOP: + act = ESP_BLE_MESH_RPR_SERVER_SCAN_STOP_EVT; + break; + case BTC_BLE_MESH_EVT_RPR_SERVER_EXT_SCAN_START: + act = ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_START_EVT; + break; + case BTC_BLE_MESH_EVT_RPR_SERVER_EXT_SCAN_STOP: + act = ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_STOP_EVT; + break; + case BTC_BLE_MESH_EVT_RPR_SERVER_LINK_OPEN: + act = ESP_BLE_MESH_RPR_SERVER_LINK_OPEN_EVT; + break; + case BTC_BLE_MESH_EVT_RPR_SERVER_LINK_CLOSE: + act = ESP_BLE_MESH_RPR_SERVER_LINK_CLOSE_EVT; + break; + case BTC_BLE_MESH_EVT_RPR_SERVER_PROV_COMP: + act = ESP_BLE_MESH_RPR_SERVER_PROV_COMP_EVT; + break; + default: + BT_ERR("Unknown Remote Provisioning server event type %d", event); + return; + } + + if (val && len) { + memcpy(&cb_params, val, len); + } + + btc_ble_mesh_rpr_server_cb(&cb_params, act); +} + +void btc_ble_mesh_rpr_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_rpr_server_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_rpr_server_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_RPR_SERVER_EVT_MAX) { + btc_ble_mesh_rpr_server_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } +} + +#endif /* CONFIG_BLE_MESH_RPR_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_sar_model.c b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_sar_model.c new file mode 100644 index 000000000000..536d219cc0a7 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_sar_model.c @@ -0,0 +1,389 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "btc_ble_mesh_model_common.h" +#include "btc_ble_mesh_sar_model.h" +#include "esp_ble_mesh_sar_model_api.h" + +#if CONFIG_BLE_MESH_SAR_CLI + +extern int bt_mesh_sar_transmitter_get(void *param); +extern int bt_mesh_sar_transmitter_set(void *param, void *set); +extern int bt_mesh_sar_receiver_get(void *param); +extern int bt_mesh_sar_receiver_set(void *param, void *set); + +/* SAR Configuration Client model related functions */ + +static inline void btc_ble_mesh_sar_client_cb_to_app(esp_ble_mesh_sar_client_cb_event_t event, + esp_ble_mesh_sar_client_cb_param_t *param) +{ + esp_ble_mesh_sar_client_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_SAR_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_sar_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_sar_client_args_t *dst = p_dest; + btc_ble_mesh_sar_client_args_t *src = p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_SAR_CLIENT_SEND: + dst->sar_send.params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->sar_send.params) { + memcpy(dst->sar_send.params, src->sar_send.params, + sizeof(esp_ble_mesh_client_common_param_t)); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + break; + } + if (src->sar_send.msg) { + dst->sar_send.msg = bt_mesh_calloc(sizeof(esp_ble_mesh_sar_client_msg_t)); + if (dst->sar_send.msg) { + memcpy(dst->sar_send.msg, src->sar_send.msg, + sizeof(esp_ble_mesh_sar_client_msg_t)); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; + default: + BT_DBG("%s, Unknown act %d", __func__, msg->act); + break; + } +} + +void btc_ble_mesh_sar_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_sar_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_sar_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_SAR_CLIENT_SEND: + if (arg->sar_send.params) { + bt_mesh_free(arg->sar_send.params); + } + if (arg->sar_send.msg) { + bt_mesh_free(arg->sar_send.msg); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_sar_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_sar_client_cb_param_t *p_dest_data = p_dest; + esp_ble_mesh_sar_client_cb_param_t *p_src_data = p_src; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } +} + +static void btc_ble_mesh_sar_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_sar_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_sar_client_cb_param_t *)msg->arg; + + switch (msg->act) { + case ESP_BLE_MESH_SAR_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_SAR_CLIENT_SEND_TIMEOUT_EVT: + case ESP_BLE_MESH_SAR_CLIENT_RECV_RSP_EVT: + case ESP_BLE_MESH_SAR_CLIENT_RECV_PUB_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_sar_client_cb(esp_ble_mesh_sar_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_SAR_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_SAR_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_sar_client_cb_param_t), + btc_ble_mesh_sar_client_copy_req_data, + btc_ble_mesh_sar_client_free_req_data); +} + +void bt_mesh_sar_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len) +{ + esp_ble_mesh_sar_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + uint8_t act = 0; + + if (!model || !ctx || len > sizeof(cb_params.recv)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_SAR_CLIENT_SEND_TIMEOUT: + act = ESP_BLE_MESH_SAR_CLIENT_SEND_TIMEOUT_EVT; + break; + case BTC_BLE_MESH_EVT_SAR_CLIENT_RECV_RSP: + act = ESP_BLE_MESH_SAR_CLIENT_RECV_RSP_EVT; + break; + case BTC_BLE_MESH_EVT_SAR_CLIENT_RECV_PUB: + act = ESP_BLE_MESH_SAR_CLIENT_RECV_PUB_EVT; + break; + default: + BT_ERR("Unknown SAR Config client event type %d", event); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + params.ctx.recv_rssi = ctx->recv_rssi; + params.ctx.send_ttl = ctx->send_ttl; + + cb_params.params = ¶ms; + + if (val && len) { + memcpy(&cb_params.recv, val, len); + } + + btc_ble_mesh_sar_client_cb(&cb_params, act); +} + +void btc_ble_mesh_sar_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_sar_client_cb_evt_to_btc(opcode, BTC_BLE_MESH_EVT_SAR_CLIENT_RECV_PUB, + model, ctx, buf->data, buf->len); +} + +static int btc_ble_mesh_sar_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_sar_client_msg_t *msg) +{ + bt_mesh_client_common_param_t param = {0}; + + if (params == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if ((params->opcode == ESP_BLE_MESH_MODEL_OP_SAR_TRANSMITTER_SET || + params->opcode == ESP_BLE_MESH_MODEL_OP_SAR_RECEIVER_SET) && msg == NULL) { + BT_ERR("Invalid SAR Config message, opcode 0x%04x", params->opcode); + return -EINVAL; + } + + btc_ble_mesh_set_client_common_param(params, ¶m, true); + + switch (param.opcode) { + case ESP_BLE_MESH_MODEL_OP_SAR_TRANSMITTER_GET: + return bt_mesh_sar_transmitter_get(¶m); + case ESP_BLE_MESH_MODEL_OP_SAR_TRANSMITTER_SET: + return bt_mesh_sar_transmitter_set(¶m, &msg->sar_transmitter_set); + case ESP_BLE_MESH_MODEL_OP_SAR_RECEIVER_GET: + return bt_mesh_sar_receiver_get(¶m); + case ESP_BLE_MESH_MODEL_OP_SAR_RECEIVER_SET: + return bt_mesh_sar_receiver_set(¶m, &msg->sar_receiver_set); + default: + BT_ERR("Invalid SAR Config opcode 0x%04x", param.opcode); + return -EINVAL; + } +} + +void btc_ble_mesh_sar_client_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_sar_client_cb_param_t cb = {0}; + btc_ble_mesh_sar_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_sar_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_SAR_CLIENT_SEND: + cb.params = arg->sar_send.params; + cb.send.err_code = btc_ble_mesh_sar_client_send(arg->sar_send.params, + arg->sar_send.msg); + btc_ble_mesh_sar_client_cb(&cb, ESP_BLE_MESH_SAR_CLIENT_SEND_COMP_EVT); + break; + default: + break; + } + + btc_ble_mesh_sar_client_arg_deep_free(msg); +} + +void btc_ble_mesh_sar_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_sar_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_sar_client_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_SAR_CLIENT_EVT_MAX) { + btc_ble_mesh_sar_client_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } + + btc_ble_mesh_sar_client_free_req_data(msg); +} + +#endif /* CONFIG_BLE_MESH_SAR_CLI */ + +#if CONFIG_BLE_MESH_SAR_SRV + +/* SAR Config Server model related functions */ + +static inline void btc_ble_mesh_sar_server_cb_to_app(esp_ble_mesh_sar_server_cb_event_t event, + esp_ble_mesh_sar_server_cb_param_t *param) +{ + esp_ble_mesh_sar_server_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_SAR_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_sar_server_cb(esp_ble_mesh_sar_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_SAR_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_SAR_SERVER; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_sar_server_cb_param_t), NULL, NULL); +} + +void bt_mesh_sar_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len) +{ + esp_ble_mesh_sar_server_cb_param_t cb_params = {0}; + uint8_t act = 0; + + if (!model || !ctx || len > sizeof(cb_params.value)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_SAR_SERVER_STATE_CHANGE: + act = ESP_BLE_MESH_SAR_SERVER_STATE_CHANGE_EVT; + break; + default: + BT_ERR("Unknown SAR Config server event type %d", event); + return; + } + + cb_params.model = (esp_ble_mesh_model_t *)model; + cb_params.ctx.net_idx = ctx->net_idx; + cb_params.ctx.app_idx = ctx->app_idx; + cb_params.ctx.addr = ctx->addr; + cb_params.ctx.recv_ttl = ctx->recv_ttl; + cb_params.ctx.recv_op = ctx->recv_op; + cb_params.ctx.recv_dst = ctx->recv_dst; + cb_params.ctx.recv_rssi = ctx->recv_rssi; + cb_params.ctx.send_ttl = ctx->send_ttl; + + if (val && len) { + memcpy(&cb_params.value, val, len); + } + + btc_ble_mesh_sar_server_cb(&cb_params, act); +} + +void btc_ble_mesh_sar_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_sar_server_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_sar_server_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_SAR_SERVER_EVT_MAX) { + btc_ble_mesh_sar_server_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } +} + +#endif /* CONFIG_BLE_MESH_SAR_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_srpl_model.c b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_srpl_model.c new file mode 100644 index 000000000000..568c255cdbee --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/btc_ble_mesh_srpl_model.c @@ -0,0 +1,370 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "btc_ble_mesh_model_common.h" +#include "btc_ble_mesh_srpl_model.h" +#include "esp_ble_mesh_srpl_model_api.h" + +#if CONFIG_BLE_MESH_SRPL_CLI + +extern int bt_mesh_solic_pdu_rpl_items_clear(void *param, void *uar); + +/* Solicitation PDU RPL Configuration Client model related functions */ + +static inline void btc_ble_mesh_srpl_client_cb_to_app(esp_ble_mesh_srpl_client_cb_event_t event, + esp_ble_mesh_srpl_client_cb_param_t *param) +{ + esp_ble_mesh_srpl_client_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_SRPL_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_srpl_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_srpl_client_args_t *dst = p_dest; + btc_ble_mesh_srpl_client_args_t *src = p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_SRPL_CLIENT_SEND: + dst->srpl_send.params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->srpl_send.msg = bt_mesh_calloc(sizeof(esp_ble_mesh_srpl_client_msg_t)); + if (dst->srpl_send.params && dst->srpl_send.msg) { + memcpy(dst->srpl_send.params, src->srpl_send.params, + sizeof(esp_ble_mesh_client_common_param_t)); + memcpy(dst->srpl_send.msg, src->srpl_send.msg, + sizeof(esp_ble_mesh_srpl_client_msg_t)); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + break; + default: + BT_DBG("%s, Unknown act %d", __func__, msg->act); + break; + } +} + +void btc_ble_mesh_srpl_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_srpl_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_srpl_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_SRPL_CLIENT_SEND: + if (arg->srpl_send.msg) { + bt_mesh_free(arg->srpl_send.msg); + } + if (arg->srpl_send.params) { + bt_mesh_free(arg->srpl_send.params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_srpl_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_srpl_client_cb_param_t *p_dest_data = p_dest; + esp_ble_mesh_srpl_client_cb_param_t *p_src_data = p_src; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_calloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } +} + +static void btc_ble_mesh_srpl_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_srpl_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_srpl_client_cb_param_t *)msg->arg; + + switch (msg->act) { + case ESP_BLE_MESH_SRPL_CLIENT_SEND_COMP_EVT: + case ESP_BLE_MESH_SRPL_CLIENT_SEND_TIMEOUT_EVT: + case ESP_BLE_MESH_SRPL_CLIENT_RECV_RSP_EVT: + case ESP_BLE_MESH_SRPL_CLIENT_RECV_PUB_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_srpl_client_cb(esp_ble_mesh_srpl_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_SRPL_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_SRPL_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_srpl_client_cb_param_t), + btc_ble_mesh_srpl_client_copy_req_data, + btc_ble_mesh_srpl_client_free_req_data); +} + +void bt_mesh_srpl_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len) +{ + esp_ble_mesh_srpl_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + uint8_t act = 0; + + if (!model || !ctx || len > sizeof(cb_params.recv)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_SRPL_CLIENT_SEND_TIMEOUT: + act = ESP_BLE_MESH_SRPL_CLIENT_SEND_TIMEOUT_EVT; + break; + case BTC_BLE_MESH_EVT_SRPL_CLIENT_RECV_RSP: + act = ESP_BLE_MESH_SRPL_CLIENT_RECV_RSP_EVT; + break; + case BTC_BLE_MESH_EVT_SRPL_CLIENT_RECV_PUB: + act = ESP_BLE_MESH_SRPL_CLIENT_RECV_PUB_EVT; + break; + default: + BT_ERR("Unknown Solicitation PDU RPL Config client event type %d", event); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + params.ctx.recv_rssi = ctx->recv_rssi; + params.ctx.send_ttl = ctx->send_ttl; + + cb_params.params = ¶ms; + + if (val && len) { + memcpy(&cb_params.recv, val, len); + } + + btc_ble_mesh_srpl_client_cb(&cb_params, act); +} + +void btc_ble_mesh_srpl_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_srpl_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_SRPL_CLIENT_RECV_PUB, + model, ctx, buf->data, buf->len); +} + +static int btc_ble_mesh_srpl_client_send(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_srpl_client_msg_t *msg) +{ + bt_mesh_client_common_param_t param = {0}; + + if (params == NULL || msg == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + btc_ble_mesh_set_client_common_param(params, ¶m, false); + + switch (param.opcode) { + case ESP_BLE_MESH_MODEL_OP_SRPL_ITEMS_CLEAR: + case ESP_BLE_MESH_MODEL_OP_SRPL_ITEMS_CLEAR_UNACK: + return bt_mesh_solic_pdu_rpl_items_clear(¶m, &msg->srpl_items_clear.addr_range); + default: + BT_ERR("Invalid Solicitation PDU RPL Config Set opcode 0x%04x", param.opcode); + return -EINVAL; + } +} + +void btc_ble_mesh_srpl_client_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_srpl_client_cb_param_t cb = {0}; + btc_ble_mesh_srpl_client_args_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_srpl_client_args_t *)msg->arg; + + switch (msg->act) { + case BTC_BLE_MESH_ACT_SRPL_CLIENT_SEND: + cb.params = arg->srpl_send.params; + cb.send.err_code = btc_ble_mesh_srpl_client_send(arg->srpl_send.params, + arg->srpl_send.msg); + btc_ble_mesh_srpl_client_cb(&cb, + ESP_BLE_MESH_SRPL_CLIENT_SEND_COMP_EVT); + break; + default: + break; + } + + btc_ble_mesh_srpl_client_arg_deep_free(msg); +} + +void btc_ble_mesh_srpl_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_srpl_client_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_srpl_client_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_SRPL_CLIENT_EVT_MAX) { + btc_ble_mesh_srpl_client_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } + + btc_ble_mesh_srpl_client_free_req_data(msg); +} + +#endif /* CONFIG_BLE_MESH_SRPL_CLI */ + +#if CONFIG_BLE_MESH_SRPL_SRV + +/* Solicitation PDU RPL Config Server model related functions */ + +static inline void btc_ble_mesh_srpl_server_cb_to_app(esp_ble_mesh_srpl_server_cb_event_t event, + esp_ble_mesh_srpl_server_cb_param_t *param) +{ + esp_ble_mesh_srpl_server_cb_t btc_ble_mesh_cb = + btc_profile_cb_get(BTC_PID_SRPL_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_srpl_server_cb(esp_ble_mesh_srpl_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_SRPL_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_SRPL_SERVER; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_srpl_server_cb_param_t), NULL, NULL); +} + +void bt_mesh_srpl_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len) +{ + esp_ble_mesh_srpl_server_cb_param_t cb_params = {0}; + uint8_t act = 0; + + if (!model || !ctx || len > sizeof(cb_params.value)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (event) { + case BTC_BLE_MESH_EVT_SRPL_SERVER_STATE_CHANGE: + act = ESP_BLE_MESH_SRPL_SERVER_STATE_CHANGE_EVT; + break; + default: + BT_ERR("Unknown Solicitation PDU RPL Config server event type %d", event); + return; + } + + cb_params.model = (esp_ble_mesh_model_t *)model; + cb_params.ctx.net_idx = ctx->net_idx; + cb_params.ctx.app_idx = ctx->app_idx; + cb_params.ctx.addr = ctx->addr; + cb_params.ctx.recv_ttl = ctx->recv_ttl; + cb_params.ctx.recv_op = ctx->recv_op; + cb_params.ctx.recv_dst = ctx->recv_dst; + cb_params.ctx.recv_rssi = ctx->recv_rssi; + cb_params.ctx.send_ttl = ctx->send_ttl; + + if (val && len) { + memcpy(&cb_params.value, val, len); + } + + btc_ble_mesh_srpl_server_cb(&cb_params, act); +} + +void btc_ble_mesh_srpl_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_srpl_server_cb_param_t *arg = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_srpl_server_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_SRPL_SERVER_EVT_MAX) { + btc_ble_mesh_srpl_server_cb_to_app(msg->act, arg); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } +} + +#endif /* CONFIG_BLE_MESH_SRPL_SRV */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_agg_model.h b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_agg_model.h new file mode 100644 index 000000000000..14e4dcecd356 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_agg_model.h @@ -0,0 +1,70 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BTC_BLE_MESH_AGG_MODEL_H_ +#define _BTC_BLE_MESH_AGG_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_agg_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_AGG_CLIENT_SEND, + BTC_BLE_MESH_ACT_AGG_CLIENT_MAX, +} btc_ble_mesh_agg_client_act_t; + +typedef union { + struct { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_agg_client_msg_t *msg; + } agg_send; +} btc_ble_mesh_agg_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_AGG_CLIENT_SEND_TIMEOUT, + BTC_BLE_MESH_EVT_AGG_CLIENT_RECV_RSP, + BTC_BLE_MESH_EVT_AGG_CLIENT_RECV_PUB, + BTC_BLE_MESH_EVT_AGG_CLIENT_MAX, +} btc_ble_mesh_agg_client_evt_t; + +void btc_ble_mesh_agg_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_agg_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_agg_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_agg_client_arg_deep_free(btc_msg_t *msg); + +void btc_ble_mesh_agg_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_agg_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len); + +typedef enum { + BTC_BLE_MESH_EVT_AGG_SERVER_RECV_MSG, + BTC_BLE_MESH_EVT_AGG_SERVER_MAX, +} btc_ble_mesh_agg_server_evt_t; + +void btc_ble_mesh_agg_server_cb_handler(btc_msg_t *msg); + +void bt_mesh_agg_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const void *val, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_AGG_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_brc_model.h b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_brc_model.h new file mode 100644 index 000000000000..ab601fa2b591 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_brc_model.h @@ -0,0 +1,70 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BTC_BLE_MESH_BRC_MODEL_H_ +#define _BTC_BLE_MESH_BRC_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_brc_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_BRC_CLIENT_SEND, + BTC_BLE_MESH_ACT_BRC_CLIENT_MAX, +} btc_ble_mesh_brc_client_act_t; + +typedef union { + struct { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_brc_client_msg_t *msg; + } brc_send; +} btc_ble_mesh_brc_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_BRC_CLIENT_RECV_RSP, + BTC_BLE_MESH_EVT_BRC_CLIENT_RECV_PUB, + BTC_BLE_MESH_EVT_BRC_CLIENT_SEND_TIMEOUT, + BTC_BLE_MESH_EVT_BRC_CLIENT_MAX, +} btc_ble_mesh_brc_client_evt_t; + +void btc_ble_mesh_brc_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_brc_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_brc_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_brc_client_arg_deep_free(btc_msg_t *msg); + +void btc_ble_mesh_brc_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_brc_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len); + +typedef enum { + BTC_BLE_MESH_EVT_BRC_SERVER_STATE_CHANGE, + BTC_BLE_MESH_EVT_BRC_SERVER_MAX, +} btc_ble_mesh_brc_server_evt_t; + +void btc_ble_mesh_brc_server_cb_handler(btc_msg_t *msg); + +void bt_mesh_brc_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_BRC_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_df_model.h b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_df_model.h new file mode 100644 index 000000000000..b0b8531f8003 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_df_model.h @@ -0,0 +1,77 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BTC_BLE_MESH_DF_MODEL_H_ +#define _BTC_BLE_MESH_DF_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_df_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_DF_CLIENT_GET_STATE, + BTC_BLE_MESH_ACT_DF_CLIENT_SET_STATE, + BTC_BLE_MESH_ACT_DF_CLIENT_MAX, +} btc_ble_mesh_df_client_act_t; + +typedef union { + struct { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_df_client_get_t *get; + } df_get; + struct { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_df_client_set_t *set; + } df_set; +} btc_ble_mesh_df_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_DF_CLIENT_SEND_TIMEOUT, + BTC_BLE_MESH_EVT_DF_CLIENT_RECV_GET_RSP, + BTC_BLE_MESH_EVT_DF_CLIENT_RECV_SET_RSP, + BTC_BLE_MESH_EVT_DF_CLIENT_RECV_PUB, + BTC_BLE_MESH_EVT_DF_CLIENT_MAX, +} btc_ble_mesh_df_client_evt_t; + +void btc_ble_mesh_df_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_df_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_df_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_df_client_arg_deep_free(btc_msg_t *msg); + +void btc_ble_mesh_df_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_df_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len); + +typedef enum { + BTC_BLE_MESH_EVT_DF_SERVER_STATE_CHANGE, + BTC_BLE_MESH_EVT_DF_SERVER_TABLE_CHANGE, + BTC_BLE_MESH_EVT_DF_SERVER_MAX, +} btc_ble_mesh_df_server_evt_t; + +void btc_ble_mesh_df_server_cb_handler(btc_msg_t *msg); + +void bt_mesh_df_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_DF_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_lcd_model.h b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_lcd_model.h new file mode 100644 index 000000000000..d25ae77f708c --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_lcd_model.h @@ -0,0 +1,70 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BTC_BLE_MESH_LCD_MODEL_H_ +#define _BTC_BLE_MESH_LCD_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_lcd_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_LCD_CLIENT_SEND, + BTC_BLE_MESH_ACT_LCD_CLIENT_MAX, +} btc_ble_mesh_lcd_client_act_t; + +typedef union { + struct { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_lcd_client_msg_t *msg; + } lcd_send; +} btc_ble_mesh_lcd_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_LCD_CLIENT_SEND_TIMEOUT, + BTC_BLE_MESH_EVT_LCD_CLIENT_RECV_RSP, + BTC_BLE_MESH_EVT_LCD_CLIENT_RECV_PUB, + BTC_BLE_MESH_EVT_LCD_CLIENT_MAX, +} btc_ble_mesh_lcd_client_evt_t; + +void btc_ble_mesh_lcd_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_lcd_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_lcd_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_lcd_client_arg_deep_free(btc_msg_t *msg); + +void btc_ble_mesh_lcd_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_lcd_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const void *val, size_t len); + +typedef enum { + BTC_BLE_MESH_EVT_LCD_SERVER_STATE_CHANGE, + BTC_BLE_MESH_EVT_LCD_SERVER_MAX, +} btc_ble_mesh_lcd_server_evt_t; + +void btc_ble_mesh_lcd_server_cb_handler(btc_msg_t *msg); + +void bt_mesh_lcd_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const void *val, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_LCD_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_mbt_model.h b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_mbt_model.h new file mode 100644 index 000000000000..070888ca55df --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_mbt_model.h @@ -0,0 +1,143 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BTC_BLE_MESH_MBT_MODEL_H_ +#define _BTC_BLE_MESH_MBT_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_mbt_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_MBT_CLIENT_RETRIEVE_CAPABILITIES, + BTC_BLE_MESH_ACT_MBT_CLIENT_TRANSFER_BLOB, + BTC_BLE_MESH_ACT_MBT_CLIENT_SEND_BLOCK, + BTC_BLE_MESH_ACT_MBT_CLIENT_SEND_DATA, + BTC_BLE_MESH_ACT_MBT_CLIENT_DETERMINE_BLOCK_STATUS, + BTC_BLE_MESH_ACT_MBT_CLIENT_DETERMINE_TRANSFER_STATUS, + BTC_BLE_MESH_ACT_MBT_CLIENT_CANCEL_TRANSFER, + BTC_BLE_MESH_ACT_MBT_CLIENT_SET_TRANSFER_TTL, + BTC_BLE_MESH_ACT_MBT_CLIENT_CLEAR_TRANSFER_TTL, + BTC_BLE_MESH_ACT_MBT_CLIENT_SET_APP_IDX, + BTC_BLE_MESH_ACT_MBT_CLIENT_CLEAR_APP_IDX, + BTC_BLE_MESH_ACT_MBT_CLIENT_SET_MULTICAST_ADDR, + BTC_BLE_MESH_ACT_MBT_CLIENT_CLEAR_MULTICAST_ADDR, + BTC_BLE_MESH_ACT_MBT_CLIENT_MAX, +} btc_ble_mesh_mbt_client_act_t; + +typedef union { + esp_ble_mesh_retrieve_capabilities_t retrieve_capabilities; + esp_ble_mesh_transfer_blob_t transfer_blob; + esp_ble_mesh_send_block_t send_block; + esp_ble_mesh_send_data_t send_data; + esp_ble_mesh_determine_block_status_t determine_block_status; + esp_ble_mesh_determine_transfer_status_t determine_transfer_status; + esp_ble_mesh_cancel_transfer_t cancel_transfer; + struct { + esp_ble_mesh_model_t *model; + uint8_t transfer_ttl; + } set_transfer_ttl; + struct { + esp_ble_mesh_model_t *model; + } clear_transfer_ttl; + struct { + esp_ble_mesh_model_t *model; + uint16_t app_idx; + } set_app_idx; + struct { + esp_ble_mesh_model_t *model; + } clear_app_idx; + struct { + esp_ble_mesh_model_t *model; + uint16_t multicast_addr; + } set_multicast_addr; + struct { + esp_ble_mesh_model_t *model; + } clear_multicast_addr; +} btc_ble_mesh_mbt_client_args_t; + +#define BTC_BLE_MESH_MBT_CLIENT_RESULT_COMPLETE 0x00 +#define BTC_BLE_MESH_MBT_CLIENT_RESULT_FAIL 0x01 + +typedef enum { + BTC_BLE_MESH_EVT_MBT_CLIENT_RETRIEVE_CAPABILITIES_COMP, + BTC_BLE_MESH_EVT_MBT_CLIENT_TRANSFER_BLOB_COMP, + BTC_BLE_MESH_EVT_MBT_CLIENT_SEND_BLOCK_COMP, + BTC_BLE_MESH_EVT_MBT_CLIENT_SEND_DATA_COMP, + BTC_BLE_MESH_EVT_MBT_CLIENT_DETERMINE_BLOCK_STATUS_COMP, + BTC_BLE_MESH_EVT_MBT_CLIENT_DETERMINE_TRANSFER_STATUS_COMP, + BTC_BLE_MESH_EVT_MBT_CLIENT_CANCEL_TRANSFER_COMP, + BTC_BLE_MESH_EVT_MBT_CLIENT_SET_TRANSFER_TTL_COMP, + BTC_BLE_MESH_EVT_MBT_CLIENT_CLEAR_TRANSFER_TTL_COMP, + BTC_BLE_MESH_EVT_MBT_CLIENT_SET_APP_IDX_COMP, + BTC_BLE_MESH_EVT_MBT_CLIENT_CLEAR_APP_IDX_COMP, + BTC_BLE_MESH_EVT_MBT_CLIENT_SET_MULTICAST_ADDR_COMP, + BTC_BLE_MESH_EVT_MBT_CLIENT_CLEAR_MULTICAST_ADDR_COMP, + BTC_BLE_MESH_EVT_MBT_CLIENT_MAX, +} btc_ble_mesh_mbt_client_evt_t; + +void btc_ble_mesh_mbt_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_mbt_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_mbt_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_mbt_client_arg_deep_free(btc_msg_t *msg); + +void btc_ble_mesh_mbt_client_publish_callback(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_mbt_client_cb_evt_to_btc(uint8_t event, uint8_t result, + struct bt_mesh_model *model); + +typedef enum { + BTC_BLE_MESH_ACT_MBT_SERVER_INITIALIZE_BLOB_RECEIVE, + BTC_BLE_MESH_ACT_MBT_SERVER_CANCEL_BLOB_RECEIVE, + BTC_BLE_MESH_ACT_MBT_SERVER_SET_BLOB_CAPABILITIES, + BTC_BLE_MESH_ACT_MBT_SERVER_MAX, +} btc_ble_mesh_mbt_server_act_t; + +typedef union { + esp_ble_mesh_initialize_blob_receive_t initialize_blob_receive; + esp_ble_mesh_cancel_blob_receive_t cancel_blob_receive; + esp_ble_mesh_set_blob_capabilities_t set_blob_capabilities; +} btc_ble_mesh_mbt_server_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_MBT_SERVER_INITIALIZE_BLOB_RECEIVE_COMP, + BTC_BLE_MESH_EVT_MBT_SERVER_CANCEL_BLOB_RECEIVE_COMP, + BTC_BLE_MESH_EVT_MBT_SERVER_SET_BLOB_CAPABILITIES_COMP, + BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_TRANSFER_GET, + BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_TRANSFER_START, + BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_TRANSFER_CANCEL, + BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_BLOCK_GET, + BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_BLOCK_START, + BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_CHUNK_TRANSFER, + BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_INFORMATION_GET, + BTC_BLE_MESH_EVT_MBT_SERVER_BLOCK_RECEIVE_COMP, + BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_RECEIVE_COMP, + BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_RECEIVE_TIMEOUT, + BTC_BLE_MESH_EVT_MBT_SERVER_MAX, +} btc_ble_mesh_mbt_server_evt_t; + +void btc_ble_mesh_mbt_server_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_mbt_server_cb_handler(btc_msg_t *msg); + +void bt_mesh_mbt_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_MBT_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_odp_model.h b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_odp_model.h new file mode 100644 index 000000000000..032c35775622 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_odp_model.h @@ -0,0 +1,70 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BTC_BLE_MESH_ODP_MODEL_H_ +#define _BTC_BLE_MESH_ODP_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_odp_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_ODP_CLIENT_SEND, + BTC_BLE_MESH_ACT_ODP_CLIENT_MAX, +} btc_ble_mesh_odp_client_act_t; + +typedef union { + struct { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_odp_client_msg_t *msg; + } odp_send; +} btc_ble_mesh_odp_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_ODP_CLIENT_SEND_TIMEOUT, + BTC_BLE_MESH_EVT_ODP_CLIENT_RECV_RSP, + BTC_BLE_MESH_EVT_ODP_CLIENT_RECV_PUB, + BTC_BLE_MESH_EVT_ODP_CLIENT_MAX, +} btc_ble_mesh_odp_client_evt_t; + +void btc_ble_mesh_odp_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_odp_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_odp_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_odp_client_arg_deep_free(btc_msg_t *msg); + +void btc_ble_mesh_odp_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_odp_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len); + +typedef enum { + BTC_BLE_MESH_EVT_ODP_SERVER_STATE_CHANGE, + BTC_BLE_MESH_EVT_ODP_SERVER_MAX, +} btc_ble_mesh_odp_server_evt_t; + +void btc_ble_mesh_odp_server_cb_handler(btc_msg_t *msg); + +void bt_mesh_odp_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_ODP_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_prb_model.h b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_prb_model.h new file mode 100644 index 000000000000..feef97013afc --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_prb_model.h @@ -0,0 +1,70 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BTC_BLE_MESH_PRB_MODEL_H_ +#define _BTC_BLE_MESH_PRB_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_prb_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_PRB_CLIENT_SEND, + BTC_BLE_MESH_ACT_PRB_CLIENT_MAX, +} btc_ble_mesh_prb_client_act_t; + +typedef union { + struct { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_prb_client_msg_t *msg; + } prb_send; +} btc_ble_mesh_prb_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_PRB_CLIENT_RECV_RSP, + BTC_BLE_MESH_EVT_PRB_CLIENT_RECV_PUB, + BTC_BLE_MESH_EVT_PRB_CLIENT_SEND_TIMEOUT, + BTC_BLE_MESH_EVT_PRB_CLIENT_MAX, +} btc_ble_mesh_prb_client_evt_t; + +void btc_ble_mesh_prb_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_prb_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_prb_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_prb_client_arg_deep_free(btc_msg_t *msg); + +void btc_ble_mesh_prb_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_prb_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, uint16_t len); + +typedef enum { + BTC_BLE_MESH_EVT_PRB_SERVER_STATE_CHANGE, + BTC_BLE_MESH_EVT_PRB_SERVER_MAX, +} btc_ble_mesh_prb_server_evt_t; + +void btc_ble_mesh_prb_server_cb_handler(btc_msg_t *msg); + +void bt_mesh_prb_server_cb_evt_to_btc(uint8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_PRB_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_rpr_model.h b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_rpr_model.h new file mode 100644 index 000000000000..e521778221f5 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_rpr_model.h @@ -0,0 +1,86 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BTC_BLE_MESH_RPR_MODEL_H_ +#define _BTC_BLE_MESH_RPR_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_rpr_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_RPR_CLIENT_SEND, + BTC_BLE_MESH_ACT_RPR_CLIENT_ACT, + BTC_BLE_MESH_ACT_RPR_CLIENT_MAX, +} btc_ble_mesh_rpr_client_act_t; + +typedef union { + struct { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_rpr_client_msg_t *msg; + } rpr_send; + struct { + esp_ble_mesh_rpr_client_act_type_t type; + esp_ble_mesh_rpr_client_act_param_t *param; + } rpr_act; +} btc_ble_mesh_rpr_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_RPR_CLIENT_SEND_TIMEOUT, + BTC_BLE_MESH_EVT_RPR_CLIENT_RECV_RSP, + BTC_BLE_MESH_EVT_RPR_CLIENT_RECV_PUB, + BTC_BLE_MESH_EVT_RPR_CLIENT_MAX, +} btc_ble_mesh_rpr_client_evt_t; + +void btc_ble_mesh_rpr_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_rpr_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_rpr_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_rpr_client_arg_deep_free(btc_msg_t *msg); + +void btc_ble_mesh_rpr_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void btc_ble_mesh_rpr_client_link_close_cb(struct bt_mesh_model *model, + uint16_t rpr_srv_addr, uint8_t reason); + +void btc_ble_mesh_rpr_client_prov_comp_cb(struct bt_mesh_model *model, uint16_t rpr_srv_addr, + uint8_t nppi, uint16_t index, uint8_t uuid[16], + uint16_t unicast_addr, uint8_t element_num, + uint16_t net_idx); + +void bt_mesh_rpr_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const void *val, size_t len); + +typedef enum { + BTC_BLE_MESH_EVT_RPR_SERVER_SCAN_START, + BTC_BLE_MESH_EVT_RPR_SERVER_SCAN_STOP, + BTC_BLE_MESH_EVT_RPR_SERVER_EXT_SCAN_START, + BTC_BLE_MESH_EVT_RPR_SERVER_EXT_SCAN_STOP, + BTC_BLE_MESH_EVT_RPR_SERVER_LINK_OPEN, + BTC_BLE_MESH_EVT_RPR_SERVER_LINK_CLOSE, + BTC_BLE_MESH_EVT_RPR_SERVER_PROV_COMP, + BTC_BLE_MESH_EVT_RPR_SERVER_MAX, +} btc_ble_mesh_rpr_server_evt_t; + +void btc_ble_mesh_rpr_server_cb_handler(btc_msg_t *msg); + +void bt_mesh_rpr_server_cb_evt_to_btc(uint8_t event, const void *val, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_RPR_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_sar_model.h b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_sar_model.h new file mode 100644 index 000000000000..433c4a02f949 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_sar_model.h @@ -0,0 +1,70 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BTC_BLE_MESH_SAR_MODEL_H_ +#define _BTC_BLE_MESH_SAR_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_sar_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_SAR_CLIENT_SEND, + BTC_BLE_MESH_ACT_SAR_CLIENT_MAX, +} btc_ble_mesh_sar_client_act_t; + +typedef union { + struct { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_sar_client_msg_t *msg; + } sar_send; +} btc_ble_mesh_sar_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_SAR_CLIENT_SEND_TIMEOUT, + BTC_BLE_MESH_EVT_SAR_CLIENT_RECV_RSP, + BTC_BLE_MESH_EVT_SAR_CLIENT_RECV_PUB, + BTC_BLE_MESH_EVT_SAR_CLIENT_MAX, +} btc_ble_mesh_sar_client_evt_t; + +void btc_ble_mesh_sar_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_sar_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_sar_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_sar_client_arg_deep_free(btc_msg_t *msg); + +void btc_ble_mesh_sar_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_sar_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len); + +void btc_ble_mesh_sar_server_cb_handler(btc_msg_t *msg); + +typedef enum { + BTC_BLE_MESH_EVT_SAR_SERVER_STATE_CHANGE, + BTC_BLE_MESH_EVT_SAR_SERVER_MAX, +} btc_ble_mesh_sar_server_evt_t; + +void bt_mesh_sar_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_SAR_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_srpl_model.h b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_srpl_model.h new file mode 100644 index 000000000000..8d2f0c3d0df7 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/btc/include/btc_ble_mesh_srpl_model.h @@ -0,0 +1,70 @@ +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BTC_BLE_MESH_SRPL_MODEL_H_ +#define _BTC_BLE_MESH_SRPL_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_srpl_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_SRPL_CLIENT_SEND, + BTC_BLE_MESH_ACT_SRPL_CLIENT_MAX, +} btc_ble_mesh_srpl_client_act_t; + +typedef union { + struct { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_srpl_client_msg_t *msg; + } srpl_send; +} btc_ble_mesh_srpl_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_SRPL_CLIENT_SEND_TIMEOUT, + BTC_BLE_MESH_EVT_SRPL_CLIENT_RECV_RSP, + BTC_BLE_MESH_EVT_SRPL_CLIENT_RECV_PUB, + BTC_BLE_MESH_EVT_SRPL_CLIENT_MAX, +} btc_ble_mesh_srpl_client_evt_t; + +void btc_ble_mesh_srpl_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_srpl_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_srpl_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_srpl_client_arg_deep_free(btc_msg_t *msg); + +void btc_ble_mesh_srpl_client_recv_pub_cb(uint32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_srpl_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len); + +typedef enum { + BTC_BLE_MESH_EVT_SRPL_SERVER_STATE_CHANGE, + BTC_BLE_MESH_EVT_SRPL_SERVER_MAX, +} btc_ble_mesh_srpl_server_evt_t; + +void btc_ble_mesh_srpl_server_cb_handler(btc_msg_t *msg); + +void bt_mesh_srpl_server_cb_evt_to_btc(uint8_t event, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const uint8_t *val, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_SRPL_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/ext.c b/components/bt/esp_ble_mesh/v1.1/ext.c new file mode 100644 index 000000000000..b6d30d7a0e52 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/ext.c @@ -0,0 +1,4836 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#if CONFIG_BT_BLUEDROID_ENABLED +#include "bta/bta_api.h" +#endif + +#include "btc_ble_mesh_agg_model.h" +#include "btc_ble_mesh_brc_model.h" +#include "btc_ble_mesh_df_model.h" +#include "btc_ble_mesh_lcd_model.h" +#include "btc_ble_mesh_mbt_model.h" +#include "btc_ble_mesh_odp_model.h" +#include "btc_ble_mesh_prb_model.h" +#include "btc_ble_mesh_rpr_model.h" +#include "btc_ble_mesh_sar_model.h" +#include "btc_ble_mesh_srpl_model.h" + +#include "adv.h" +#include "net.h" +#include "scan.h" +#include "crypto.h" +#include "access.h" +#include "beacon.h" +#include "friend.h" +#include "lpn.h" +#include "rpl.h" +#include "foundation.h" +#include +#include +#include "mesh/buf.h" +#include "mesh/slist.h" +#include "mesh/config.h" +#include "mesh/adapter.h" +#include "mesh/main.h" +#include "mesh/timer.h" +#include "mesh/mutex.h" +#include "mesh/common.h" +#include "mesh/access.h" +#include "prov_common.h" +#include "prov_node.h" +#include "prov_pvnr.h" +#include "pvnr_mgmt.h" +#include "transport.h" +#include "proxy_client.h" +#include "proxy_server.h" +#include "settings.h" +#include "settings_nvs.h" +#include "mesh/model_common.h" +#include "mesh/client_common.h" + +#include "mesh_v1.1/utils.h" + +#define NET_BUF(a) ((struct net_buf *)(a)) +#define k_WORK(a) ((struct k_work *)(a)) +#define COMP(a) ((const struct bt_mesh_comp *)(a)) +#define ELEM(a) ((struct bt_mesh_elem *)(a)) +#define MODEL(a) ((struct bt_mesh_model *)(a)) +#define APP_KEY(a) ((struct bt_mesh_app_key *)(a)) +#define NODE(a) ((struct bt_mesh_node *)(a)) +#define LINK(a) ((struct bt_mesh_prov_link *)(a)) +#define PROXY_CLI(a) ((struct bt_mesh_proxy_client *)(a)) +#define PROXY_CFG(a) ((struct bt_mesh_proxy_cfg_pdu *)(a)) +#define FRIEND(a) ((struct bt_mesh_friend *)(a)) +#define SUBNET(a) ((struct bt_mesh_subnet *)(a)) +#define NET_TX(a) ((struct bt_mesh_net_tx *)(a)) +#define NET_RX(a) ((struct bt_mesh_net_rx *)(a)) +#define MSG_CTX(a) ((struct bt_mesh_msg_ctx *)(a)) +#define CLI_PARAM(a) ((bt_mesh_client_common_param_t *)(a)) +#define CLI_NODE(a) ((bt_mesh_client_node_t *)(a)) +#define ADV_DATA(a) ((const struct bt_mesh_adv_data *)(a)) +#define VOID(a) ((void *)(a)) + +/* Sys utilities */ +void bt_mesh_ext_put_be16(uint16_t val, uint8_t dst[2]) +{ + sys_put_be16(val, dst); +} + +void bt_mesh_ext_put_be24(uint32_t val, uint8_t dst[3]) +{ + sys_put_be24(val, dst); +} + +void bt_mesh_ext_put_be32(uint32_t val, uint8_t dst[4]) +{ + sys_put_be32(val, dst); +} + +void bt_mesh_ext_put_be48(uint64_t val, uint8_t dst[6]) +{ + sys_put_be48(val, dst); +} + +void bt_mesh_ext_put_be64(uint64_t val, uint8_t dst[8]) +{ + sys_put_be64(val, dst); +} + +void bt_mesh_ext_put_le16(uint16_t val, uint8_t dst[2]) +{ + sys_put_le16(val, dst); +} + +void bt_mesh_ext_put_le24(uint32_t val, uint8_t dst[3]) +{ + sys_put_le24(val, dst); +} + +void bt_mesh_ext_put_le32(uint32_t val, uint8_t dst[4]) +{ + sys_put_le32(val, dst); +} + +void bt_mesh_ext_put_le48(uint64_t val, uint8_t dst[6]) +{ + sys_put_le48(val, dst); +} + +void bt_mesh_ext_put_le64(uint64_t val, uint8_t dst[8]) +{ + sys_put_le64(val, dst); +} + +uint16_t bt_mesh_ext_get_be16(const uint8_t src[2]) +{ + return sys_get_be16(src); +} + +uint32_t bt_mesh_ext_get_be24(const uint8_t src[3]) +{ + return sys_get_be24(src); +} + +uint32_t bt_mesh_ext_get_be32(const uint8_t src[4]) +{ + return sys_get_be32(src); +} + +uint64_t bt_mesh_ext_get_be48(const uint8_t src[6]) +{ + return sys_get_be48(src); +} + +uint64_t bt_mesh_ext_get_be64(const uint8_t src[8]) +{ + return sys_get_be64(src); +} + +uint16_t bt_mesh_ext_get_le16(const uint8_t src[2]) +{ + return sys_get_le16(src); +} + +uint32_t bt_mesh_ext_get_le24(const uint8_t src[3]) +{ + return sys_get_le24(src); +} + +uint32_t bt_mesh_ext_get_le32(const uint8_t src[4]) +{ + return sys_get_le32(src); +} + +uint64_t bt_mesh_ext_get_le48(const uint8_t src[6]) +{ + return sys_get_le48(src); +} + +uint64_t bt_mesh_ext_get_le64(const uint8_t src[8]) +{ + return sys_get_le64(src); +} + +void bt_mesh_ext_memcpy_swap(void *dst, const void *src, size_t length) +{ + sys_memcpy_swap(dst, src, length); +} + +void bt_mesh_ext_mem_swap(void *buf, size_t length) +{ + sys_mem_swap(buf, length); +} + +/* Net buf */ +void bt_mesh_ext_buf_simple_init(struct net_buf_simple *buf, size_t reserve_head) +{ + net_buf_simple_init(buf, reserve_head); +} + +void bt_mesh_ext_buf_simple_init_with_data(struct net_buf_simple *buf, void *data, size_t size) +{ + net_buf_simple_init_with_data(buf, data, size); +} + +void bt_mesh_ext_buf_simple_reset(struct net_buf_simple *buf) +{ + net_buf_simple_reset(buf); +} + +void bt_mesh_ext_buf_simple_clone(const struct net_buf_simple *original, struct net_buf_simple *clone) +{ + net_buf_simple_clone(original, clone); +} + +void *bt_mesh_ext_buf_simple_add(struct net_buf_simple *buf, size_t len) +{ + return net_buf_simple_add(buf, len); +} + +void *bt_mesh_ext_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem, size_t len) +{ + return net_buf_simple_add_mem(buf, mem, len); +} + +uint8_t *bt_mesh_ext_buf_simple_add_u8(struct net_buf_simple *buf, uint8_t val) +{ + return net_buf_simple_add_u8(buf, val); +} + +void bt_mesh_ext_buf_simple_add_le16(struct net_buf_simple *buf, uint16_t val) +{ + net_buf_simple_add_le16(buf, val); +} + +void bt_mesh_ext_buf_simple_add_be16(struct net_buf_simple *buf, uint16_t val) +{ + net_buf_simple_add_be16(buf, val); +} + +void bt_mesh_ext_buf_simple_add_le24(struct net_buf_simple *buf, uint32_t val) +{ + net_buf_simple_add_le24(buf, val); +} + +void bt_mesh_ext_buf_simple_add_be24(struct net_buf_simple *buf, uint32_t val) +{ + net_buf_simple_add_be24(buf, val); +} + +void bt_mesh_ext_buf_simple_add_le32(struct net_buf_simple *buf, uint32_t val) +{ + net_buf_simple_add_le32(buf, val); +} + +void bt_mesh_ext_buf_simple_add_be32(struct net_buf_simple *buf, uint32_t val) +{ + net_buf_simple_add_be32(buf, val); +} + +void bt_mesh_ext_buf_simple_add_le48(struct net_buf_simple *buf, uint64_t val) +{ + net_buf_simple_add_le48(buf, val); +} + +void bt_mesh_ext_buf_simple_add_be48(struct net_buf_simple *buf, uint64_t val) +{ + net_buf_simple_add_be48(buf, val); +} + +void bt_mesh_ext_buf_simple_add_le64(struct net_buf_simple *buf, uint64_t val) +{ + net_buf_simple_add_le64(buf, val); +} + +void bt_mesh_ext_buf_simple_add_be64(struct net_buf_simple *buf, uint64_t val) +{ + net_buf_simple_add_be64(buf, val); +} + +void *bt_mesh_ext_buf_simple_push(struct net_buf_simple *buf, size_t len) +{ + return net_buf_simple_push(buf, len); +} + +void bt_mesh_ext_buf_simple_push_le16(struct net_buf_simple *buf, uint16_t val) +{ + net_buf_simple_push_le16(buf, val); +} + +void bt_mesh_ext_buf_simple_push_be16(struct net_buf_simple *buf, uint16_t val) +{ + net_buf_simple_push_be16(buf, val); +} + +void bt_mesh_ext_buf_simple_push_u8(struct net_buf_simple *buf, uint8_t val) +{ + net_buf_simple_push_u8(buf, val); +} + +void bt_mesh_ext_buf_simple_push_le24(struct net_buf_simple *buf, uint32_t val) +{ + net_buf_simple_push_le24(buf, val); +} + +void bt_mesh_ext_buf_simple_push_be24(struct net_buf_simple *buf, uint32_t val) +{ + net_buf_simple_push_be24(buf, val); +} + +void bt_mesh_ext_buf_simple_push_le32(struct net_buf_simple *buf, uint32_t val) +{ + net_buf_simple_push_le32(buf, val); +} + +void bt_mesh_ext_buf_simple_push_be32(struct net_buf_simple *buf, uint32_t val) +{ + net_buf_simple_push_be32(buf, val); +} + +void bt_mesh_ext_buf_simple_push_le48(struct net_buf_simple *buf, uint64_t val) +{ + net_buf_simple_push_le48(buf, val); +} + +void bt_mesh_ext_buf_simple_push_be48(struct net_buf_simple *buf, uint64_t val) +{ + net_buf_simple_push_be48(buf, val); +} + +void bt_mesh_ext_buf_simple_push_le64(struct net_buf_simple *buf, uint64_t val) +{ + net_buf_simple_push_le64(buf, val); +} + +void bt_mesh_ext_buf_simple_push_be64(struct net_buf_simple *buf, uint64_t val) +{ + net_buf_simple_push_be64(buf, val); +} + +void *bt_mesh_ext_buf_simple_pull(struct net_buf_simple *buf, size_t len) +{ + return net_buf_simple_pull(buf, len); +} + +void *bt_mesh_ext_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len) +{ + return net_buf_simple_pull_mem(buf, len); +} + +uint8_t bt_mesh_ext_buf_simple_pull_u8(struct net_buf_simple *buf) +{ + return net_buf_simple_pull_u8(buf); +} + +uint16_t bt_mesh_ext_buf_simple_pull_le16(struct net_buf_simple *buf) +{ + return net_buf_simple_pull_le16(buf); +} + +uint16_t bt_mesh_ext_buf_simple_pull_be16(struct net_buf_simple *buf) +{ + return net_buf_simple_pull_be16(buf); +} + +uint32_t bt_mesh_ext_buf_simple_pull_le24(struct net_buf_simple *buf) +{ + return net_buf_simple_pull_le24(buf); +} + +uint32_t bt_mesh_ext_buf_simple_pull_be24(struct net_buf_simple *buf) +{ + return net_buf_simple_pull_be24(buf); +} + +uint32_t bt_mesh_ext_buf_simple_pull_le32(struct net_buf_simple *buf) +{ + return net_buf_simple_pull_le32(buf); +} + +uint32_t bt_mesh_ext_buf_simple_pull_be32(struct net_buf_simple *buf) +{ + return net_buf_simple_pull_be32(buf); +} + +uint64_t bt_mesh_ext_buf_simple_pull_le48(struct net_buf_simple *buf) +{ + return net_buf_simple_pull_le48(buf); +} + +uint64_t bt_mesh_ext_buf_simple_pull_be48(struct net_buf_simple *buf) +{ + return net_buf_simple_pull_be48(buf); +} + +uint64_t bt_mesh_ext_buf_simple_pull_le64(struct net_buf_simple *buf) +{ + return net_buf_simple_pull_le64(buf); +} + +uint64_t bt_mesh_ext_buf_simple_pull_be64(struct net_buf_simple *buf) +{ + return net_buf_simple_pull_be64(buf); +} + +uint8_t *bt_mesh_ext_buf_simple_tail(struct net_buf_simple *buf) +{ + return net_buf_simple_tail(buf); +} + +size_t bt_mesh_ext_buf_simple_headroom(struct net_buf_simple *buf) +{ + return net_buf_simple_headroom(buf); +} + +size_t bt_mesh_ext_buf_simple_tailroom(struct net_buf_simple *buf) +{ + return net_buf_simple_tailroom(buf); +} + +void bt_mesh_ext_buf_simple_save(struct net_buf_simple *buf, struct net_buf_simple_state *state) +{ + net_buf_simple_save(buf, state); +} + +void bt_mesh_ext_buf_simple_restore(struct net_buf_simple *buf, struct net_buf_simple_state *state) +{ + net_buf_simple_restore(buf, state); +} + +void bt_mesh_ext_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve) +{ + net_buf_simple_reserve(buf, reserve); +} + +struct net_buf_simple *bt_mesh_ext_buf_get_simple(void *buf) +{ + return &NET_BUF(buf)->b; +} + +void *bt_mesh_ext_buf_add_mem(void *buf, const void *mem, size_t len) +{ + return net_buf_simple_add_mem(&NET_BUF(buf)->b, mem, len); +} + +uint8_t *bt_mesh_ext_buf_add_u8(void *buf, uint8_t val) +{ + return net_buf_simple_add_u8(&NET_BUF(buf)->b, val); +} + +void bt_mesh_ext_buf_add_le16(void *buf, uint16_t val) +{ + net_buf_simple_add_le16(&NET_BUF(buf)->b, val); +} + +void bt_mesh_ext_buf_unref(void *buf) +{ + net_buf_unref(buf); +} + +/* Memory */ +void *bt_mesh_ext_malloc(size_t size) +{ + return bt_mesh_malloc(size); +} + +void *bt_mesh_ext_calloc(size_t size) +{ + return bt_mesh_calloc(size); +} + +void bt_mesh_ext_free(void *ptr) +{ + bt_mesh_free(ptr); +} + +struct net_buf_simple *bt_mesh_ext_alloc_buf(uint16_t size) +{ + return bt_mesh_alloc_buf(size); +} + +void bt_mesh_ext_free_buf(struct net_buf_simple *buf) +{ + bt_mesh_free_buf(buf); +} + +/* Utilities */ +int bt_mesh_ext_rand(void *buf, size_t len) +{ + return bt_mesh_rand(buf, len); +} + +int32_t bt_mesh_ext_ceil(float num) +{ + return bt_mesh_ceil(num); +} + +float bt_mesh_ext_log2(float num) +{ + return bt_mesh_log2(num); +} + +/* Crypto */ +bool bt_mesh_ext_s1(const char *m, uint8_t salt[16]) +{ + return bt_mesh_s1(m, salt); +} + +int bt_mesh_ext_aes_cmac(const uint8_t key[16], void *sg, size_t sg_len, uint8_t mac[16]) +{ + return bt_mesh_aes_cmac(key, sg, sg_len, mac); +} + +int bt_mesh_ext_id128(const uint8_t n[16], const char *s, uint8_t out[16]) +{ + return bt_mesh_id128(n, s, out); +} + +int bt_mesh_ext_encrypt_be(const uint8_t key[16], const uint8_t plaintext[16], uint8_t enc_data[16]) +{ + return bt_mesh_encrypt_be(key, plaintext, enc_data); +} + +int bt_mesh_ext_net_obfuscate(uint8_t *pdu, uint32_t iv_index, const uint8_t privacy_key[16]) +{ + return bt_mesh_net_obfuscate(pdu, iv_index, privacy_key); +} + +int bt_mesh_ext_net_encrypt(const uint8_t key[16], struct net_buf_simple *buf, + uint32_t iv_index, bool proxy, bool proxy_solic) +{ + return bt_mesh_net_encrypt(key, buf, iv_index, proxy, proxy_solic); +} + +int bt_mesh_ext_net_decrypt(const uint8_t key[16], struct net_buf_simple *buf, + uint32_t iv_index, bool proxy, bool proxy_solic) +{ + return bt_mesh_net_decrypt(key, buf, iv_index, proxy, proxy_solic); +} + +int bt_mesh_ext_tc_hmac_set_key(void *ctx, const uint8_t *key, unsigned int key_size) +{ + return tc_hmac_set_key(ctx, key, key_size); +} + +int bt_mesh_ext_tc_hmac_init(void *ctx) +{ + return tc_hmac_init(ctx); +} + +int bt_mesh_ext_tc_hmac_update(void *ctx, const void *data, unsigned int data_length) +{ + return tc_hmac_update(ctx, data, data_length); +} + +int bt_mesh_ext_tc_hmac_final(uint8_t *tag, unsigned int taglen, void *ctx) +{ + return tc_hmac_final(tag, taglen, ctx); +} + +/* Mutex */ +void bt_mesh_ext_mutex_create(void *mutex) +{ + bt_mesh_mutex_create(mutex); +} + +void bt_mesh_ext_mutex_free(void *mutex) +{ + bt_mesh_mutex_free(mutex); +} + +void bt_mesh_ext_mutex_lock(void *mutex) +{ + bt_mesh_mutex_lock(mutex); +} + +void bt_mesh_ext_mutex_unlock(void *mutex) +{ + bt_mesh_mutex_unlock(mutex); +} + +void bt_mesh_ext_r_mutex_create(void *mutex) +{ + bt_mesh_r_mutex_create(mutex); +} + +void bt_mesh_ext_r_mutex_free(void *mutex) +{ + bt_mesh_r_mutex_free(mutex); +} + +void bt_mesh_ext_r_mutex_lock(void *mutex) +{ + bt_mesh_r_mutex_lock(mutex); +} + +void bt_mesh_ext_r_mutex_unlock(void *mutex) +{ + bt_mesh_r_mutex_unlock(mutex); +} + +/* Timer */ +int bt_mesh_ext_timer_init(void *work, void (*timeout_cb)(void *work)) +{ + return k_delayed_work_init(work, VOID(timeout_cb)); +} + +int bt_mesh_ext_timer_free(void *work) +{ + return k_delayed_work_free(work); +} + +int bt_mesh_ext_timer_submit(void *work, int32_t delay) +{ + return k_delayed_work_submit(work, delay); +} + +int bt_mesh_ext_timer_cancel(void *work) +{ + return k_delayed_work_cancel(work); +} + +int32_t bt_mesh_ext_timer_remaining_get(void *work) +{ + return k_delayed_work_remaining_get(work); +} + +uint32_t bt_mesh_ext_timer_get_32(void) +{ + return k_uptime_get_32(); +} + +void *bt_mesh_ext_timer_get_with_work(void *work) +{ + return CONTAINER_OF(work, struct k_delayed_work, work); +} + +void *bt_mesh_ext_timer_get_user_data(void *work) +{ + return k_WORK(work)->user_data; +} + +void bt_mesh_ext_timer_set_user_data(void *work, void *user_data) +{ + k_WORK(work)->user_data = user_data; +} + +/* Atomic */ +int bt_mesh_ext_atomic_inc(int *target) +{ + return bt_mesh_atomic_inc(target); +} + +int bt_mesh_ext_atomic_dec(int *target) +{ + return bt_mesh_atomic_dec(target); +} + +int bt_mesh_ext_atomic_get(const int *target) +{ + return bt_mesh_atomic_get(target); +} + +int bt_mesh_ext_atomic_set(int *target, int value) +{ + return bt_mesh_atomic_set(target, value); +} + +int bt_mesh_ext_atomic_or(int *target, int value) +{ + return bt_mesh_atomic_or(target, value); +} + +int bt_mesh_ext_atomic_and(int *target, int value) +{ + return bt_mesh_atomic_and(target, value); +} + +int bt_mesh_ext_atomic_test_bit(const int *target, int bit) +{ + return bt_mesh_atomic_test_bit(target, bit); +} + +int bt_mesh_ext_atomic_test_and_clear_bit(int *target, int bit) +{ + return bt_mesh_atomic_test_and_clear_bit(target, bit); +} + +int bt_mesh_ext_atomic_test_and_set_bit(int *target, int bit) +{ + return bt_mesh_atomic_test_and_set_bit(target, bit); +} + +void bt_mesh_ext_atomic_clear_bit(int *target, int bit) +{ + return bt_mesh_atomic_clear_bit(target, bit); +} + +void bt_mesh_ext_atomic_set_bit(int *target, int bit) +{ + return bt_mesh_atomic_set_bit(target, bit); +} + +void bt_mesh_ext_atomic_set_bit_to(int *target, int bit, bool val) +{ + return bt_mesh_atomic_set_bit_to(target, bit, val); +} + +/* Access */ +void bt_mesh_ext_comp_provision(uint16_t addr) +{ + bt_mesh_comp_provision(addr); +} + +int bt_mesh_ext_get_opcode(struct net_buf_simple *buf, uint32_t *opcode, bool pull_buf) +{ + return bt_mesh_get_opcode(buf, opcode, pull_buf); +} + +size_t bt_mesh_ext_rx_netkey_size(void) +{ + return bt_mesh_rx_netkey_size(); +} + +void *bt_mesh_ext_rx_netkey_get(size_t index) +{ + return bt_mesh_rx_netkey_get(index); +} + +uint16_t bt_mesh_ext_primary_addr(void) +{ + return bt_mesh_primary_addr(); +} + +const void *bt_mesh_ext_comp_get(void) +{ + return bt_mesh_comp_get(); +} + +size_t bt_mesh_ext_comp_get_elem_count(const void *comp) +{ + return COMP(comp)->elem_count; +} + +void *bt_mesh_ext_comp_get_elem(const void *comp, uint8_t index) +{ + return &COMP(comp)->elem[index]; +} + +uint16_t bt_mesh_ext_comp_get_cid(const void *comp) +{ + return COMP(comp)->cid; +} + +uint16_t bt_mesh_ext_comp_get_pid(const void *comp) +{ + return COMP(comp)->pid; +} + +uint16_t bt_mesh_ext_comp_get_vid(const void *comp) +{ + return COMP(comp)->vid; +} + +void *bt_mesh_ext_elem_find(uint16_t addr) +{ + return bt_mesh_elem_find(addr); +} + +uint8_t bt_mesh_ext_elem_count(void) +{ + return bt_mesh_elem_count(); +} + +uint16_t bt_mesh_ext_elem_get_loc(void *elem) +{ + return ELEM(elem)->loc; +} + +uint8_t bt_mesh_ext_elem_get_model_count(void *elem) +{ + return ELEM(elem)->model_count; +} + +void *bt_mesh_ext_elem_get_model(void *elem, uint8_t index) +{ + return &ELEM(elem)->models[index]; +} + +uint8_t bt_mesh_ext_elem_get_vnd_model_count(void *elem) +{ + return ELEM(elem)->vnd_model_count; +} + +void *bt_mesh_ext_elem_get_vnd_model(void *elem, uint8_t index) +{ + return &ELEM(elem)->vnd_models[index]; +} + +bool bt_mesh_ext_model_in_primary(const void *model) +{ + return bt_mesh_model_in_primary(model); +} + +void *bt_mesh_ext_model_elem(void *mod) +{ + return bt_mesh_model_elem(mod); +} + +void *bt_mesh_ext_model_find(void *elem, uint16_t id) +{ + return bt_mesh_model_find(elem, id); +} + +void *bt_mesh_ext_model_find_vnd(void *elem, uint16_t company, uint16_t id) +{ + return bt_mesh_model_find_vnd(elem, company, id); +} + +void bt_mesh_ext_model_msg_init(struct net_buf_simple *msg, uint32_t opcode) +{ + bt_mesh_model_msg_init(msg, opcode); +} + +int bt_mesh_ext_model_send(void *model, void *ctx, struct net_buf_simple *msg, + const struct bt_mesh_send_cb *cb, void *cb_data) +{ + return bt_mesh_model_send(model, ctx, msg, cb, cb_data); +} + +uint16_t bt_mesh_ext_model_get_id(void *model) +{ + return MODEL(model)->id; +} + +uint16_t bt_mesh_ext_model_get_vnd_id(void *model) +{ + return MODEL(model)->vnd.id; +} + +uint16_t bt_mesh_ext_model_get_vnd_cid(void *model) +{ + return MODEL(model)->vnd.company; +} + +void *bt_mesh_ext_model_get_pub(void *model) +{ + return MODEL(model)->pub; +} + +uint16_t bt_mesh_ext_model_get_pub_addr(void *model) +{ + return MODEL(model)->pub->addr; +} + +uint16_t bt_mesh_ext_model_get_pub_key(void *model) +{ + return MODEL(model)->pub->key; +} + +uint8_t bt_mesh_ext_model_get_pub_directed_pub_policy(void *model) +{ +#if CONFIG_BLE_MESH_DF_SRV + return MODEL(model)->pub->directed_pub_policy; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_model_set_pub_directed_pub_policy(void *model, uint8_t directed_pub_policy) +{ +#if CONFIG_BLE_MESH_DF_SRV + MODEL(model)->pub->directed_pub_policy = directed_pub_policy; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void *bt_mesh_ext_model_get_pub_msg(void *model) +{ + return MODEL(model)->pub->msg; +} + +uint8_t bt_mesh_ext_model_get_keys_count(void *model) +{ + return ARRAY_SIZE(MODEL(model)->keys); +} + +uint16_t bt_mesh_ext_model_get_keys_size(void *model) +{ + return sizeof(MODEL(model)->keys); +} + +uint16_t bt_mesh_ext_model_get_key(void *model, uint8_t index) +{ + return MODEL(model)->keys[index]; +} + +void bt_mesh_ext_model_set_key(void *model, uint8_t index, uint16_t key) +{ + MODEL(model)->keys[index] = key; +} + +uint8_t bt_mesh_ext_model_get_groups_count(void *model) +{ + return ARRAY_SIZE(MODEL(model)->groups); +} + +uint16_t bt_mesh_ext_model_get_groups_size(void *model) +{ + return sizeof(MODEL(model)->groups); +} + +uint16_t bt_mesh_ext_model_get_group(void *model, uint8_t index) +{ + return MODEL(model)->groups[index]; +} + +void bt_mesh_ext_model_set_group(void *model, uint8_t index, uint16_t group) +{ + MODEL(model)->groups[index] = group; +} + +void *bt_mesh_ext_model_get_user_data(void *model) +{ + return MODEL(model)->user_data; +} + +void bt_mesh_ext_model_set_user_data(void *model, void *user_data) +{ + MODEL(model)->user_data = user_data; +} + +void bt_mesh_ext_model_free_user_data(void *model) +{ + assert(MODEL(model)->user_data); + bt_mesh_free(MODEL(model)->user_data); + MODEL(model)->user_data = NULL; +} + +void bt_mesh_ext_model_set_user_data_model(void *model, void *mod) +{ + bt_mesh_client_user_data_t *client = MODEL(model)->user_data; + assert(client); + client->model = mod; +} + +void bt_mesh_ext_model_set_user_data_op_pair_size(void *model, uint32_t op_pair_size) +{ + bt_mesh_client_user_data_t *client = MODEL(model)->user_data; + assert(client); + client->op_pair_size = op_pair_size; +} + +void bt_mesh_ext_model_set_user_data_op_pair(void *model, const void *op_pair) +{ + bt_mesh_client_user_data_t *client = MODEL(model)->user_data; + assert(client); + client->op_pair = op_pair; +} + +void *bt_mesh_ext_model_get_user_data_internal_data(void *model) +{ + bt_mesh_client_user_data_t *client = MODEL(model)->user_data; + assert(client); + return client->internal_data; +} + +void bt_mesh_ext_model_set_user_data_internal_data(void *model, void *internal_data) +{ + bt_mesh_client_user_data_t *client = MODEL(model)->user_data; + assert(client); + client->internal_data = internal_data; +} + +void bt_mesh_ext_model_free_user_data_internal_data(void *model) +{ + bt_mesh_client_user_data_t *client = MODEL(model)->user_data; + assert(client); + assert(client->internal_data); + bt_mesh_free(client->internal_data); + client->internal_data = NULL; +} + +void *bt_mesh_ext_model_get_user_data_internal_data_queue(void *model) +{ + bt_mesh_client_user_data_t *client = MODEL(model)->user_data; + assert(client); + bt_mesh_client_internal_data_t *internal = client->internal_data; + assert(internal); + return &internal->queue; +} + +void *bt_mesh_ext_model_get_user_data_vendor_data(void *model) +{ + bt_mesh_client_user_data_t *client = MODEL(model)->user_data; + assert(client); + return client->vendor_data; +} + +void bt_mesh_ext_model_set_user_data_vendor_data(void *model, void *vendor_data) +{ + bt_mesh_client_user_data_t *client = MODEL(model)->user_data; + assert(client); + client->vendor_data = vendor_data; +} + +void bt_mesh_ext_model_free_user_data_vendor_data(void *model) +{ + bt_mesh_client_user_data_t *client = MODEL(model)->user_data; + assert(client); + assert(client->vendor_data); + bt_mesh_free(client->vendor_data); + client->vendor_data = NULL; +} + +bool bt_mesh_ext_model_is_opcode_belongs(void *models, uint8_t model_count, uint32_t opcode) +{ + const struct bt_mesh_model_op *op = NULL; + struct bt_mesh_model *model = NULL; + + for (size_t i = 0; i < model_count; i++) { + model = models + i; + for (op = model->op; op->func; op++) { + if (op->opcode == opcode) { + return true; + } + } + } + + return false; +} + +/* Transport */ +void *bt_mesh_ext_app_key_get(uint16_t app_idx) +{ + return bt_mesh_app_key_get(app_idx); +} + +uint16_t bt_mesh_ext_app_key_get_net_idx(void *key) +{ + return APP_KEY(key)->net_idx; +} + +int bt_mesh_ext_ctl_send(void *tx, uint8_t ctl_op, void *data, size_t data_len, + const struct bt_mesh_send_cb *cb, void *cb_data) +{ + return bt_mesh_ctl_send(tx, ctl_op, data, data_len, cb, cb_data); +} + +uint16_t bt_mesh_ext_get_tx_sdu_max(void) +{ + return BLE_MESH_TX_SDU_MAX; +} + +void bt_mesh_ext_tx_reset(void) +{ + bt_mesh_tx_reset(); +} + +void bt_mesh_ext_rx_reset(void) +{ + bt_mesh_rx_reset(); +} + +/* Net */ +void *bt_mesh_ext_subnet_get(uint16_t net_idx) +{ + return bt_mesh_subnet_get(net_idx); +} + +int bt_mesh_ext_net_encode(void *tx, struct net_buf_simple *buf, bool proxy) +{ + return bt_mesh_net_encode(tx, buf, proxy); +} + +uint8_t bt_mesh_ext_net_flags(void *sub) +{ + return bt_mesh_net_flags(sub); +} + +bool bt_mesh_ext_net_iv_update(uint32_t iv_index, bool iv_update) +{ + return bt_mesh_net_iv_update(iv_index, iv_update); +} + +bool bt_mesh_ext_kr_update(void *sub, uint8_t new_kr, bool new_key) +{ + return bt_mesh_kr_update(sub, new_kr, new_key); +} + +void bt_mesh_ext_net_sec_update(void *sub) +{ + bt_mesh_net_sec_update(sub); +} + +int bt_mesh_ext_net_pdu_decrypt(void *sub, const uint8_t *enc, + const uint8_t *priv, + const uint8_t *data, + size_t data_len, void *rx, + struct net_buf_simple *buf) +{ + extern int net_decrypt(struct bt_mesh_subnet *sub, + const uint8_t *enc, const uint8_t *priv, + const uint8_t *data, size_t data_len, + void *rx, + struct net_buf_simple *buf); + return net_decrypt(sub, enc, priv, data, data_len, rx, buf); +} + +uint16_t bt_mesh_ext_net_get_sub_net_idx(uint8_t index) +{ + return bt_mesh.sub[index].net_idx; +} + +uint8_t bt_mesh_ext_net_get_sub_count(void) +{ + return ARRAY_SIZE(bt_mesh.sub); +} + +void *bt_mesh_ext_net_get_sub(uint8_t index) +{ + return &bt_mesh.sub[index]; +} + +uint32_t bt_mesh_ext_net_get_iv_index(void) +{ + return bt_mesh.iv_index; +} + +void bt_mesh_ext_net_set_seq(uint32_t seq) +{ + bt_mesh.seq = seq; +} + +uint8_t *bt_mesh_ext_net_get_dev_key(void) +{ + return bt_mesh.dev_key; +} + +uint8_t *bt_mesh_ext_net_get_dev_key_ca(void) +{ + return bt_mesh.dev_key_ca; +} + +uint8_t bt_mesh_ext_net_get_rpl_count(void) +{ + return ARRAY_SIZE(bt_mesh.rpl); +} + +uint16_t bt_mesh_ext_net_get_rpl_src(uint8_t index) +{ + return bt_mesh.rpl[index].src; +} + +void bt_mesh_ext_net_reset_rpl(uint8_t index) +{ + memset(&bt_mesh.rpl[index], 0, sizeof(bt_mesh.rpl[index])); +} + +int bt_mesh_ext_net_is_ivu_initiator(void) +{ + return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_INITIATOR); +} + +int bt_mesh_ext_net_is_ivu_in_progress(void) +{ + return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS); +} + +bool bt_mesh_ext_primary_subnet_exist(void) +{ + return bt_mesh_primary_subnet_exist(); +} + +/* Beacon */ +int bt_mesh_ext_net_secure_beacon_update(void *sub) +{ + return bt_mesh_net_secure_beacon_update(sub); +} + +uint8_t *bt_mesh_ext_net_get_sub_mpb_rand(uint8_t index) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + return bt_mesh.sub[index].mpb_random; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +void *bt_mesh_ext_beacon_cache_check(uint8_t data[21], bool private_beacon) +{ + extern struct bt_mesh_subnet *cache_check(uint8_t data[21], + bool private_beacon); + return cache_check(data, private_beacon); +} + +void bt_mesh_ext_beacon_cache_add(uint8_t data[21], void *sub, bool private_beacon) +{ + extern void cache_add(uint8_t data[21], + struct bt_mesh_subnet *sub, + bool private_beacon); + cache_add(data, sub, private_beacon); +} + +void bt_mesh_ext_beacon_update_observation(bool private_beacon) +{ + extern void update_beacon_observation(bool private_beacon); + update_beacon_observation(private_beacon); +} + +void bt_mesh_ext_beacon_ivu_initiator(bool enable) +{ + bt_mesh_beacon_ivu_initiator(enable); +} + +/* Foundation */ +uint8_t bt_mesh_ext_net_transmit_get(void) +{ + return bt_mesh_net_transmit_get(); +} + +uint8_t bt_mesh_ext_relay_get(void) +{ + return bt_mesh_relay_get(); +} + +uint8_t bt_mesh_ext_friend_get(void) +{ + return bt_mesh_friend_get(); +} + +uint8_t bt_mesh_ext_relay_retransmit_get(void) +{ + return bt_mesh_relay_retransmit_get(); +} + +uint8_t bt_mesh_ext_secure_beacon_get(void) +{ + return bt_mesh_secure_beacon_get(); +} + +uint8_t bt_mesh_ext_gatt_proxy_get(void) +{ + return bt_mesh_gatt_proxy_get(); +} + +uint8_t bt_mesh_ext_default_ttl_get(void) +{ + return bt_mesh_default_ttl_get(); +} + +void bt_mesh_ext_key_idx_pack(struct net_buf_simple *buf, + uint16_t idx1, uint16_t idx2) +{ + return key_idx_pack(buf, idx1, idx2); +} + +void bt_mesh_ext_key_idx_unpack(struct net_buf_simple *buf, + uint16_t *idx1, uint16_t *idx2) +{ + return key_idx_unpack(buf, idx1, idx2); +} + +/* Provisioning */ +const void *bt_mesh_ext_prov_get(void) +{ + return bt_mesh_prov_get(); +} + +uint8_t bt_mesh_ext_prov_buf_headroom(void) +{ + return PROV_BUF_HEADROOM; +} + +void bt_mesh_ext_prov_buf_init(struct net_buf_simple *buf, uint8_t type) +{ + bt_mesh_prov_buf_init(buf, type); +} + +bool bt_mesh_ext_prov_pdu_check(uint8_t type, uint16_t length, uint8_t *reason) +{ + return bt_mesh_prov_pdu_check(type, length, reason); +} + +int bt_mesh_ext_prov_send(void *link, struct net_buf_simple *buf) +{ + return bt_mesh_prov_send(link, buf); +} + +int bt_mesh_ext_prov_send_adv(void *link, struct net_buf_simple *msg) +{ + return bt_mesh_prov_send_adv(link, msg); +} + +int bt_mesh_ext_prov_bearer_ctl_send(void *link, uint8_t op, + void *data, uint8_t data_len) +{ + return bt_mesh_prov_bearer_ctl_send(link, op, data, data_len); +} + +void bt_mesh_ext_prov_clear_tx(void *link, bool cancel) +{ + bt_mesh_prov_clear_tx(link, cancel); +} + +uint8_t bt_mesh_ext_prov_node_next_xact_id(void *link) +{ +#if CONFIG_BLE_MESH_NODE + extern uint8_t node_next_xact_id(struct bt_mesh_prov_link *link); + return node_next_xact_id(link); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_NODE */ +} + +void *bt_mesh_ext_prov_node_get_link(void) +{ +#if CONFIG_BLE_MESH_NODE + extern struct bt_mesh_prov_link *bt_mesh_prov_node_get_link(void); + return bt_mesh_prov_node_get_link(); +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_NODE */ +} + +void bt_mesh_ext_prov_node_close_link(uint8_t reason) +{ +#if CONFIG_BLE_MESH_NODE + extern void bt_mesh_prov_node_close_link(uint8_t reason); + return bt_mesh_prov_node_close_link(reason); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_NODE */ +} + +const uint8_t *bt_mesh_ext_prov_node_get_uuid(void) +{ +#if CONFIG_BLE_MESH_NODE + assert(bt_mesh_prov_get()); + return bt_mesh_prov_get()->uuid; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_NODE */ +} + +uint16_t bt_mesh_ext_prov_node_get_oob_info(void) +{ +#if CONFIG_BLE_MESH_NODE + assert(bt_mesh_prov_get()); + return bt_mesh_prov_get()->oob_info; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_NODE */ +} + +const char *bt_mesh_ext_prov_node_get_uri(void) +{ +#if CONFIG_BLE_MESH_NODE + assert(bt_mesh_prov_get()); + return bt_mesh_prov_get()->uri; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_NODE */ +} + +uint8_t bt_mesh_ext_prov_pvnr_next_xact_id(void *link) +{ +#if CONFIG_BLE_MESH_PB_ADV + extern uint8_t pvnr_next_xact_id(struct bt_mesh_prov_link *link); + return pvnr_next_xact_id(link); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_PB_ADV */ +} + +void *bt_mesh_ext_prov_pvnr_get_link(void) +{ +#if CONFIG_BLE_MESH_PROVISIONER + extern struct bt_mesh_prov_link *bt_mesh_prov_pvnr_get_link(void); + return bt_mesh_prov_pvnr_get_link(); +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_PROVISIONER */ +} + +uint8_t bt_mesh_ext_prov_pvnr_get_link_count(void) +{ +#if CONFIG_BLE_MESH_PROVISIONER + extern uint8_t bt_mesh_prov_pvnr_get_link_count(void); + return bt_mesh_prov_pvnr_get_link_count(); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_PROVISIONER */ +} + +void bt_mesh_ext_prov_pvnr_send_invite(void *link) +{ +#if CONFIG_BLE_MESH_PROVISIONER + extern void bt_mesh_prov_pvnr_send_invite(struct bt_mesh_prov_link *link); + bt_mesh_prov_pvnr_send_invite(link); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_PROVISIONER */ +} + +void bt_mesh_ext_prov_pvnr_close_link(void *link, uint8_t reason) +{ +#if CONFIG_BLE_MESH_PROVISIONER + extern void bt_mesh_prov_pvnr_close_link(struct bt_mesh_prov_link *link, uint8_t reason); + bt_mesh_prov_pvnr_close_link(link, reason); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_PROVISIONER */ +} + +void *bt_mesh_ext_pvnr_get_node_with_addr(uint16_t unicast_addr) +{ +#if CONFIG_BLE_MESH_PROVISIONER + return bt_mesh_provisioner_get_node_with_addr(unicast_addr); +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_PROVISIONER */ +} + +void bt_mesh_ext_pvnr_records_list_get_cb(uint16_t link_idx, struct net_buf_simple *data) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) + assert(bt_mesh_prov_get()); + if (bt_mesh_prov_get()->records_list_get) { + bt_mesh_prov_get()->records_list_get(link_idx, data); + } +#else + assert(0); +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) */ +} + +void bt_mesh_ext_pvnr_records_recv_comp_cb(uint8_t status, uint16_t link_idx, + uint16_t record_id, uint16_t frag_offset, + uint16_t total_len, uint8_t *record) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) + assert(bt_mesh_prov_get()); + if (bt_mesh_prov_get()->prov_record_recv_comp) { + bt_mesh_prov_get()->prov_record_recv_comp(status, link_idx, record_id, + frag_offset, total_len, record); + } +#else + assert(0); +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) */ +} + +uint8_t *bt_mesh_ext_pvnr_get_node_uuid(void *node) +{ +#if CONFIG_BLE_MESH_PROVISIONER + return NODE(node)->dev_uuid; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_PROVISIONER */ +} + +int bt_mesh_ext_prov_link_test_bit(void *link, int bit) +{ + return bt_mesh_atomic_test_bit(LINK(link)->flags, bit); +} + +void bt_mesh_ext_prov_link_clear_bit(void *link, int bit) +{ + bt_mesh_atomic_clear_bit(LINK(link)->flags, bit); +} + +void bt_mesh_ext_prov_link_set_bit(void *link, int bit) +{ + bt_mesh_atomic_set_bit(LINK(link)->flags, bit); +} + +void bt_mesh_ext_prov_link_set_expect(void *link, uint8_t val) +{ + LINK(link)->expect = val; +} + +uint8_t bt_mesh_ext_prov_link_get_pub_key_type(void *link) +{ + return LINK(link)->public_key; +} + +uint8_t bt_mesh_ext_prov_link_get_auth_method(void *link) +{ + return LINK(link)->auth_method; +} + +void *bt_mesh_ext_prov_link_get_addr(void *link) +{ + return &LINK(link)->addr; +} + +uint8_t *bt_mesh_ext_prov_link_get_uuid(void *link) +{ +#if CONFIG_BLE_MESH_PROVISIONER + return LINK(link)->uuid; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_PROVISIONER */ +} + +uint8_t bt_mesh_ext_prov_link_get_elem_num(void *link) +{ +#if CONFIG_BLE_MESH_PROVISIONER + return LINK(link)->element_num; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_PROVISIONER */ +} + +uint16_t bt_mesh_ext_prov_link_get_unicast_addr(void *link) +{ +#if CONFIG_BLE_MESH_PROVISIONER + return LINK(link)->unicast_addr; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_PROVISIONER */ +} + +uint16_t bt_mesh_ext_prov_link_get_record_id_expect(void *link) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) + return LINK(link)->record_id_expect; +#else + assert(0); + return 0; +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) */ +} + +void bt_mesh_ext_prov_link_set_record_id_expect(void *link, uint16_t record_id_expect) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) + LINK(link)->record_id_expect = record_id_expect; +#else + assert(0); +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) */ +} + +uint16_t bt_mesh_ext_prov_link_get_offset_expect(void *link) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) + return LINK(link)->offset_expect; +#else + assert(0); + return 0; +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) */ +} + +void bt_mesh_ext_prov_link_set_offset_expect(void *link, uint16_t offset_expect) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) + LINK(link)->offset_expect = offset_expect; +#else + assert(0); +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) */ +} + +uint16_t bt_mesh_ext_prov_link_get_max_size(void *link) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) + return LINK(link)->max_size; +#else + assert(0); + return 0; +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) */ +} + +void bt_mesh_ext_prov_link_set_max_size(void *link, uint16_t max_size) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) + LINK(link)->max_size = max_size; +#else + assert(0); +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) */ +} + +void *bt_mesh_ext_prov_link_get_conn(void *link) +{ +#if CONFIG_BLE_MESH_PB_GATT + return LINK(link)->conn; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_PB_GATT */ +} + +uint32_t *bt_mesh_ext_prov_link_get_link_id(void *link) +{ +#if CONFIG_BLE_MESH_PB_ADV + return &LINK(link)->link_id; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_PB_ADV */ +} + +uint8_t *bt_mesh_ext_prov_link_get_pb_remote_uuid(void *link) +{ + return LINK(link)->pb_remote_uuid; +} + +uint8_t bt_mesh_ext_prov_link_get_pb_remote_timeout(void *link) +{ + return LINK(link)->pb_remote_timeout; +} + +void bt_mesh_ext_prov_link_set_pb_remote_timeout(void *link, uint8_t pb_remote_timeout) +{ + LINK(link)->pb_remote_timeout = pb_remote_timeout; +} + +uint8_t bt_mesh_ext_prov_link_get_pb_remote_nppi(void *link) +{ + return LINK(link)->pb_remote_nppi; +} + +void bt_mesh_ext_prov_link_set_pb_remote_nppi(void *link, uint8_t pb_remote_nppi) +{ + LINK(link)->pb_remote_nppi = pb_remote_nppi; +} + +uint8_t bt_mesh_ext_prov_link_get_pb_remote_pub_key(void *link) +{ + return LINK(link)->pb_remote_pub_key; +} + +void bt_mesh_ext_prov_link_set_pb_remote_pub_key(void *link, uint8_t pb_remote_pub_key) +{ + LINK(link)->pb_remote_pub_key = pb_remote_pub_key; +} + +uint8_t bt_mesh_ext_prov_link_get_pb_remote_cbd(void *link) +{ + return LINK(link)->pb_remote_cbd; +} + +void bt_mesh_ext_prov_link_set_pb_remote_cbd(void *link, uint8_t pb_remote_cbd) +{ + LINK(link)->pb_remote_cbd = pb_remote_cbd; +} + +uint8_t bt_mesh_ext_prov_link_get_pb_remote_csp(void *link) +{ + return LINK(link)->pb_remote_csp; +} + +void bt_mesh_ext_prov_link_set_pb_remote_csp(void *link, uint8_t pb_remote_csp) +{ + LINK(link)->pb_remote_csp = pb_remote_csp; +} + +uint8_t bt_mesh_ext_prov_link_get_pb_remote_reset(void *link) +{ + return LINK(link)->pb_remote_reset; +} + +void bt_mesh_ext_prov_link_set_pb_remote_reset(void *link, uint8_t pb_remote_reset) +{ + LINK(link)->pb_remote_reset = pb_remote_reset; +} + +void *bt_mesh_ext_prov_link_get_pb_remote_data(void *link) +{ + return LINK(link)->pb_remote_data; +} + +void bt_mesh_ext_prov_link_set_pb_remote_data(void *link, void *data) +{ + LINK(link)->pb_remote_data = data; +} + +void bt_mesh_ext_prov_link_free_pb_remote_data(void *link) +{ + assert(LINK(link)->pb_remote_data); + bt_mesh_free(LINK(link)->pb_remote_data); + LINK(link)->pb_remote_data = NULL; +} + +uint8_t *bt_mesh_ext_prov_link_get_record(void *link, uint16_t id) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) + return LINK(link)->records[id]; +#else + assert(0); + return NULL; +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) */ +} + +uint8_t *bt_mesh_ext_prov_link_alloc_record(void *link, uint16_t id, uint16_t len) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) + LINK(link)->records[id] = bt_mesh_calloc(len * sizeof(uint8_t)); + return LINK(link)->records[id]; +#else + assert(0); + return NULL; +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_CERT_BASED_PROV) */ +} + +bool bt_mesh_ext_prov_link_pb_gatt_exist(void *link) +{ +#if CONFIG_BLE_MESH_PB_GATT + return !!LINK(link)->pb_gatt_send; +#else + assert(0); + return false; +#endif /* CONFIG_BLE_MESH_PB_GATT */ +} + +int bt_mesh_ext_prov_link_pb_gatt_send(void *link, struct net_buf_simple *msg) +{ +#if CONFIG_BLE_MESH_PB_GATT + assert(LINK(link)->pb_gatt_send); + return LINK(link)->pb_gatt_send(link, msg); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_PB_GATT */ +} + +void bt_mesh_ext_prov_link_set_pb_remote_close(void *link, void (*pb_remote_close)(void *link, uint8_t reason)) +{ + LINK(link)->pb_remote_close = VOID(pb_remote_close); +} + +void bt_mesh_ext_prov_link_pb_remote_close(void *link, uint8_t reason) +{ + if (LINK(link)->pb_remote_close) { + LINK(link)->pb_remote_close(link, reason); + } +} + +void bt_mesh_ext_prov_link_set_pb_remote_send(void *link, int (*pb_remote_send)(void *link, struct net_buf_simple *buf)) +{ + LINK(link)->pb_remote_send = VOID(pb_remote_send); +} + +void bt_mesh_ext_prov_link_set_next_xact_id(void *link, uint8_t (*next_xact_id)(void *link)) +{ +#if CONFIG_BLE_MESH_PB_ADV + LINK(link)->next_xact_id = VOID(next_xact_id); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_PB_ADV */ +} + +/* Proxy Server */ +uint8_t bt_mesh_ext_proxy_server_get_all_client_type(void) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + return bt_mesh_proxy_server_get_all_client_type(); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +int bt_mesh_ext_proxy_server_segment_send(void *conn, uint8_t type, + struct net_buf_simple *msg) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + return bt_mesh_proxy_server_segment_send(conn, type, msg); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +bool bt_mesh_ext_proxy_server_find_client_by_addr(uint16_t addr) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + return bt_mesh_proxy_server_find_client_by_addr(addr); +#else + assert(0); + return false; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +void bt_mesh_ext_proxy_server_update_net_id_rand_stop(void) +{ +#if (CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV) + bt_mesh_proxy_server_update_net_id_rand_stop(); +#else + assert(0); +#endif /* (CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV) */ +} + +void *bt_mesh_ext_proxy_server_get_client(uint8_t index) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + extern struct bt_mesh_proxy_client *bt_mesh_proxy_server_get_client(uint8_t index); + return bt_mesh_proxy_server_get_client(index); +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +uint8_t bt_mesh_ext_proxy_server_get_client_count(void) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + extern uint8_t bt_mesh_proxy_server_get_client_count(void); + return bt_mesh_proxy_server_get_client_count(); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +void bt_mesh_ext_proxy_server_update_net_id_rand(void) +{ +#if (CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV) + bt_mesh_proxy_server_update_net_id_rand(); +#else + assert(0); +#endif /* (CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV) */ +} + +bool bt_mesh_ext_proxy_server_is_node_id_enable(void) +{ +#if (CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV) + return bt_mesh_proxy_server_is_node_id_enable(); +#else + assert(0); + return false; +#endif /* (CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV) */ +} + +void bt_mesh_ext_proxy_server_private_identity_start(void *sub) +{ +#if (CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV) + bt_mesh_proxy_server_private_identity_start(sub); +#else + assert(0); +#endif /* (CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV) */ +} + +void bt_mesh_ext_proxy_server_private_identity_stop(void *sub) +{ +#if (CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV) + bt_mesh_proxy_server_private_identity_stop(sub); +#else + assert(0); +#endif /* (CONFIG_BLE_MESH_GATT_PROXY_SERVER && CONFIG_BLE_MESH_PRB_SRV) */ +} + +uint8_t bt_mesh_ext_proxy_server_get_filter_type(void *client) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + return PROXY_CLI(client)->filter_type; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +uint8_t bt_mesh_ext_proxy_server_get_filter_count(void *client) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + return ARRAY_SIZE(PROXY_CLI(client)->filter); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +uint16_t bt_mesh_ext_proxy_server_get_filter_size(void *client) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + return sizeof(PROXY_CLI(client)->filter); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +uint16_t bt_mesh_ext_proxy_server_get_filter_addr(void *client, uint8_t index) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + return PROXY_CLI(client)->filter[index].addr; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +bool bt_mesh_ext_proxy_server_filter_is_client(void *client, uint8_t index) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + return PROXY_CLI(client)->filter[index].proxy_client; +#else + assert(0); + return false; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +uint8_t bt_mesh_ext_proxy_server_get_client_type(void *client) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + return PROXY_CLI(client)->proxy_client_type; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +bool bt_mesh_ext_proxy_server_is_proxy_msg_recv(void *client) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + return PROXY_CLI(client)->proxy_msg_recv; +#else + assert(0); + return false; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +void *bt_mesh_ext_proxy_server_get_conn(void *client) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + return PROXY_CLI(client)->conn; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +void bt_mesh_ext_proxy_server_set_client_type(void *client, uint8_t type) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + PROXY_CLI(client)->proxy_client_type = type; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +void bt_mesh_ext_proxy_server_set_msg_recv(void *client, uint8_t val) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_SERVER + PROXY_CLI(client)->proxy_msg_recv = !!val; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +/* Proxy Client */ +bool bt_mesh_ext_proxy_client_beacon_send(void *sub, bool private) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + return bt_mesh_proxy_client_beacon_send(sub, private); +#else + assert(0); + return false; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ +} + +uint8_t bt_mesh_ext_proxy_client_get_opcode(void *cfg) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + return PROXY_CFG(cfg)->opcode; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ +} + +uint8_t bt_mesh_ext_proxy_client_get_use_directed(void *cfg) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + return PROXY_CFG(cfg)->direct_proxy_ctrl.use_directed; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ +} + +void *bt_mesh_ext_proxy_client_get_client_uar(void *cfg) +{ +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + return &PROXY_CFG(cfg)->direct_proxy_ctrl.proxy_client_uar; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ +} + +/* Remote Provisioning */ +int bt_mesh_ext_rpr_cli_pdu_send(void *link, uint8_t type) +{ +#if CONFIG_BLE_MESH_RPR_CLI + return bt_mesh_rpr_cli_pdu_send(link, type); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_RPR_CLI */ +} + +int bt_mesh_ext_rpr_cli_recv_pub_key_outbound_report(void *link) +{ +#if CONFIG_BLE_MESH_RPR_CLI + return bt_mesh_rpr_cli_recv_pub_key_outbound_report(link); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_RPR_CLI */ +} + +int bt_mesh_ext_rpr_cli_pdu_recv(void *link, uint8_t type, struct net_buf_simple *buf) +{ +#if CONFIG_BLE_MESH_RPR_CLI + return bt_mesh_rpr_cli_pdu_recv(link, type, buf); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_RPR_CLI */ +} + +#if CONFIG_BLE_MESH_RPR_CLI +static struct bt_mesh_prov_link rpr_links[CONFIG_BLE_MESH_RPR_CLI_PROV_SAME_TIME]; +#endif /* CONFIG_BLE_MESH_RPR_CLI */ + +void *bt_mesh_ext_rpr_cli_get_rpr_link(uint8_t index) +{ +#if CONFIG_BLE_MESH_RPR_CLI + return &rpr_links[index]; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_RPR_CLI */ +} + +void bt_mesh_ext_rpr_srv_reset_prov_link(void *link, uint8_t reason) +{ +#if CONFIG_BLE_MESH_RPR_SRV + bt_mesh_rpr_srv_reset_prov_link(link, reason); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_RPR_SRV */ +} + +int bt_mesh_ext_rpr_srv_nppi_pdu_recv(uint8_t type, const uint8_t *data) +{ +#if CONFIG_BLE_MESH_RPR_SRV + return bt_mesh_rpr_srv_nppi_pdu_recv(type, data); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_RPR_SRV */ +} + +int bt_mesh_ext_rpr_srv_set_waiting_prov_link(void* link, bt_mesh_addr_t *addr) +{ +#if (CONFIG_BLE_MESH_GATT_PROXY_CLIENT && \ + CONFIG_BLE_MESH_PB_GATT && \ + CONFIG_BLE_MESH_RPR_SRV) + return bt_mesh_rpr_srv_set_waiting_prov_link(link, addr); +#else + assert(0); + return 0; +#endif /* (CONFIG_BLE_MESH_GATT_PROXY_CLIENT && \ + CONFIG_BLE_MESH_PB_GATT && \ + CONFIG_BLE_MESH_RPR_SRV) */ +} + +/* Friend */ +uint16_t bt_mesh_ext_frnd_get_lpn(void *frnd) +{ +#if CONFIG_BLE_MESH_FRIEND + return FRIEND(frnd)->lpn; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +uint8_t bt_mesh_ext_frnd_get_num_elem(void *frnd) +{ +#if CONFIG_BLE_MESH_FRIEND + return FRIEND(frnd)->num_elem; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +bool bt_mesh_ext_frnd_is_valid(void *frnd) +{ +#if CONFIG_BLE_MESH_FRIEND + return FRIEND(frnd)->valid; +#else + assert(0); + return false; +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +bool bt_mesh_ext_frnd_is_established(void *frnd) +{ +#if CONFIG_BLE_MESH_FRIEND + return FRIEND(frnd)->established; +#else + assert(0); + return false; +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +uint16_t bt_mesh_ext_frnd_get_net_idx(void *frnd) +{ +#if CONFIG_BLE_MESH_FRIEND + return FRIEND(frnd)->net_idx; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +uint8_t bt_mesh_ext_frnd_get_sub_list_count(void *frnd) +{ +#if CONFIG_BLE_MESH_FRIEND + return ARRAY_SIZE(FRIEND(frnd)->sub_list); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +uint16_t bt_mesh_ext_frnd_get_sub_list_size(void *frnd) +{ +#if CONFIG_BLE_MESH_FRIEND + return sizeof(FRIEND(frnd)->sub_list); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +uint16_t bt_mesh_ext_frnd_get_sub_addr(void *frnd, uint8_t index) +{ +#if CONFIG_BLE_MESH_FRIEND + return FRIEND(frnd)->sub_list[index]; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +uint8_t bt_mesh_ext_net_get_frnd_count(void) +{ +#if CONFIG_BLE_MESH_FRIEND + return ARRAY_SIZE(bt_mesh.frnd); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +void *bt_mesh_ext_net_get_frnd(uint8_t index) +{ +#if CONFIG_BLE_MESH_FRIEND + return &bt_mesh.frnd[index]; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +bool bt_mesh_ext_friend_match(uint16_t net_idx, uint16_t addr) +{ +#if CONFIG_BLE_MESH_FRIEND + return bt_mesh_friend_match(net_idx, addr); +#else + assert(0); + return false; +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +bool bt_mesh_ext_friend_unicast_match(uint16_t net_idx, uint16_t addr, uint8_t *selem) +{ +#if CONFIG_BLE_MESH_FRIEND + return bt_mesh_friend_unicast_match(net_idx, addr, selem); +#else + assert(0); + return false; +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +void *bt_mesh_ext_friend_find(uint16_t net_idx, uint16_t lpn_addr, bool valid, bool established) +{ +#if CONFIG_BLE_MESH_FRIEND + return bt_mesh_friend_find(net_idx, lpn_addr, valid, established); +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +void bt_mesh_ext_friend_clear_net_idx(uint16_t net_idx) +{ +#if CONFIG_BLE_MESH_FRIEND + bt_mesh_friend_clear_net_idx(net_idx); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_FRIEND */ +} + +/* Low Power */ +bool bt_mesh_ext_lpn_match(uint16_t addr) +{ +#if CONFIG_BLE_MESH_LOW_POWER + return bt_mesh_lpn_match(addr); +#else + assert(0); + return false; +#endif /* CONFIG_BLE_MESH_LOW_POWER */ +} + +/* Adv */ +uint8_t bt_mesh_ext_adv_data_get_type(const void *ad) +{ + return ADV_DATA(ad)->type; +} + +const uint8_t *bt_mesh_ext_adv_data_get_data(const void *ad) +{ + return ADV_DATA(ad)->data; +} + +void bt_mesh_ext_adv_update(void) +{ + bt_mesh_adv_update(); +} + +void *bt_mesh_ext_adv_create(enum bt_mesh_adv_type type, int32_t timeout) +{ + return bt_mesh_adv_create(type, timeout); +} + +void bt_mesh_ext_adv_send(void *buf, uint8_t xmit, + const struct bt_mesh_send_cb *cb, + void *cb_data) +{ + bt_mesh_adv_send(buf, xmit, cb, cb_data); +} + +uint8_t bt_mesh_ext_set_random_addr(uint8_t random_bda[6]) +{ +#if CONFIG_BT_BLUEDROID_ENABLED + extern tBTM_STATUS btm_ble_set_random_addr(BD_ADDR random_bda); + return btm_ble_set_random_addr(random_bda); +#else + return 0; +#endif +} + +void bt_mesh_ext_set_static_addr(uint8_t rand_addr[6]) +{ +#if CONFIG_BT_BLUEDROID_ENABLED + extern void BTM_BleSetStaticAddr(BD_ADDR rand_addr); + BTM_BleSetStaticAddr(rand_addr); +#else +#endif +} + +const char *bt_mesh_ext_get_device_name(void) +{ +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER + return bt_mesh_get_device_name(); +#else + assert(0); + return NULL; +#endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +} + +/* SCAN */ +int bt_mesh_ext_update_exceptional_list(uint8_t sub_code, uint32_t type, void *info) +{ +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN + return bt_mesh_update_exceptional_list(sub_code, type, info); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_USE_DUPLICATE_SCAN */ +} + +int bt_mesh_ext_unprov_dev_info_query(uint8_t uuid[16], uint8_t addr[6], + uint8_t *adv_type, uint8_t query_type) +{ +#if (CONFIG_BLE_MESH_PROVISIONER || CONFIG_BLE_MESH_RPR_SRV) + return bt_mesh_unprov_dev_info_query(uuid, addr, adv_type, query_type); +#else + assert(0); + return 0; +#endif /* (CONFIG_BLE_MESH_PROVISIONER || CONFIG_BLE_MESH_RPR_SRV) */ +} + +/* GATT */ +int bt_mesh_ext_gattc_conn_create(const void *addr, uint16_t service_uuid) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) + return bt_mesh_gattc_conn_create(addr, service_uuid); +#else + assert(0); + return 0; +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) */ +} + +void bt_mesh_ext_gattc_disconnect(void *conn) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) + bt_mesh_gattc_disconnect(conn); +#else + assert(0); +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) */ +} + +/* Settings */ +void bt_mesh_ext_store_dkca(void) +{ +#if CONFIG_BLE_MESH_SETTINGS + bt_mesh_store_dkca(); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_SETTINGS */ +} + +void bt_mesh_ext_clear_dkca(void) +{ +#if CONFIG_BLE_MESH_SETTINGS + bt_mesh_clear_dkca(); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_SETTINGS */ +} + +void bt_mesh_ext_clear_rpl_single(uint16_t src) +{ +#if CONFIG_BLE_MESH_SETTINGS + bt_mesh_clear_rpl_single(src); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_SETTINGS */ +} + +void bt_mesh_ext_store_net(void) +{ +#if CONFIG_BLE_MESH_SETTINGS + bt_mesh_store_net(); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_SETTINGS */ +} + +void bt_mesh_ext_store_seq(void) +{ +#if CONFIG_BLE_MESH_SETTINGS + bt_mesh_store_seq(); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_SETTINGS */ +} + +struct net_buf_simple *bt_mesh_ext_get_core_settings_item(const char *key) +{ +#if CONFIG_BLE_MESH_SETTINGS + return bt_mesh_get_core_settings_item(key); +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_SETTINGS */ +} + +bool bt_mesh_ext_is_settings_item_exist(struct net_buf_simple *buf, const uint16_t val) +{ +#if CONFIG_BLE_MESH_SETTINGS + return bt_mesh_is_settings_item_exist(buf, val); +#else + assert(0); + return false; +#endif /* CONFIG_BLE_MESH_SETTINGS */ +} + +int bt_mesh_ext_add_core_settings_item(const char *key, const uint16_t val) +{ +#if CONFIG_BLE_MESH_SETTINGS + return bt_mesh_add_core_settings_item(key, val); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_SETTINGS */ +} + +int bt_mesh_ext_remove_core_settings_item(const char *key, const uint16_t val) +{ +#if CONFIG_BLE_MESH_SETTINGS + return bt_mesh_remove_core_settings_item(key, val); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_SETTINGS */ +} + +int bt_mesh_ext_save_core_settings(const char *key, const uint8_t *val, size_t len) +{ +#if CONFIG_BLE_MESH_SETTINGS + return bt_mesh_save_core_settings(key, val, len); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_SETTINGS */ +} + +int bt_mesh_ext_erase_core_settings(const char *key) +{ +#if CONFIG_BLE_MESH_SETTINGS + return bt_mesh_erase_core_settings(key); +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_SETTINGS */ +} + +/* struct bt_mesh_subnet */ +uint32_t bt_mesh_ext_sub_get_mpb_sent(void *sub) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + return SUBNET(sub)->mpb_sent; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +void bt_mesh_ext_sub_set_mpb_sent(void *sub, uint32_t mpb_sent) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + SUBNET(sub)->mpb_sent = mpb_sent; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +uint8_t bt_mesh_ext_sub_get_mpb_last(void *sub) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + return SUBNET(sub)->mpb_last; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +void bt_mesh_ext_sub_set_mpb_last(void *sub, uint8_t mpb_last) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + SUBNET(sub)->mpb_last = mpb_last; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +uint8_t bt_mesh_ext_sub_get_mpb_cur(void *sub) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + return SUBNET(sub)->mpb_cur; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +void bt_mesh_ext_sub_set_mpb_cur(void *sub, uint8_t mpb_cur) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + SUBNET(sub)->mpb_cur = mpb_cur; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +void bt_mesh_ext_sub_inc_mpb_cur(void *sub) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + SUBNET(sub)->mpb_cur++; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +uint8_t bt_mesh_ext_sub_get_mpb_flags_last(void *sub) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + return SUBNET(sub)->mpb_flags_last; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +void bt_mesh_ext_sub_set_mpb_flags_last(void *sub, uint8_t mpb_flags_last) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + SUBNET(sub)->mpb_flags_last = mpb_flags_last; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +uint8_t bt_mesh_ext_sub_get_mpb_ivi_last(void *sub) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + return SUBNET(sub)->mpb_ivi_last; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +void bt_mesh_ext_sub_set_mpb_ivi_last(void *sub, uint8_t mpb_ivi_last) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + SUBNET(sub)->mpb_ivi_last = !!mpb_ivi_last; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +uint8_t *bt_mesh_ext_sub_get_mpb_random(void *sub) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + return SUBNET(sub)->mpb_random; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +uint8_t *bt_mesh_ext_sub_get_mpb_random_last(void *sub) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + return SUBNET(sub)->mpb_random_last; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +uint8_t bt_mesh_ext_sub_get_private_node_id(void *sub) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + return SUBNET(sub)->private_node_id; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +uint16_t bt_mesh_ext_sub_get_net_idx(void *sub) +{ + return SUBNET(sub)->net_idx; +} + +uint16_t bt_mesh_ext_sub_get_sbr_net_idx(void *sub) +{ +#if CONFIG_BLE_MESH_BRC_SRV + return SUBNET(sub)->sbr_net_idx; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_BRC_SRV */ +} + +void bt_mesh_ext_sub_set_sbr_net_idx(void *sub, uint16_t sbr_net_idx) +{ +#if CONFIG_BLE_MESH_BRC_SRV + SUBNET(sub)->sbr_net_idx = sbr_net_idx; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_BRC_SRV */ +} + +bool bt_mesh_ext_sub_get_kr_flag(void *sub) +{ + return SUBNET(sub)->kr_flag; +} + +uint8_t bt_mesh_ext_sub_get_kr_phase(void *sub) +{ + return SUBNET(sub)->kr_phase; +} + +uint8_t *bt_mesh_ext_sub_get_keys_net(void *sub, uint8_t index) +{ + return SUBNET(sub)->keys[index].net; +} + +uint8_t bt_mesh_ext_sub_get_keys_nid(void *sub, uint8_t index) +{ + return SUBNET(sub)->keys[index].nid; +} + +uint8_t *bt_mesh_ext_sub_get_keys_enc(void *sub, uint8_t index) +{ + return SUBNET(sub)->keys[index].enc; +} + +uint8_t *bt_mesh_ext_sub_get_keys_privacy(void *sub, uint8_t index) +{ + return SUBNET(sub)->keys[index].privacy; +} + +uint8_t *bt_mesh_ext_sub_get_keys_private_beacon(void *sub, uint8_t index) +{ +#if CONFIG_BLE_MESH_PRIVATE_BEACON + return SUBNET(sub)->keys[index].private_beacon; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */ +} + +uint8_t bt_mesh_ext_sub_get_keys_direct_nid(void *sub, uint8_t index) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->keys[index].direct_nid; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t *bt_mesh_ext_sub_get_keys_direct_enc(void *sub, uint8_t index) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->keys[index].direct_enc; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t *bt_mesh_ext_sub_get_keys_direct_privacy(void *sub, uint8_t index) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->keys[index].direct_privacy; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_proxy_privacy(void *sub) +{ + return SUBNET(sub)->proxy_privacy; +} + +void bt_mesh_ext_sub_set_proxy_privacy(void *sub, uint8_t proxy_privacy) +{ + SUBNET(sub)->proxy_privacy = proxy_privacy; +} + +uint8_t bt_mesh_ext_sub_get_directed_forwarding(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->directed_forwarding; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_directed_forwarding(void *sub, uint8_t directed_forwarding) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->directed_forwarding = directed_forwarding; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_directed_relay(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->directed_relay; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_directed_relay(void *sub, uint8_t directed_relay) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->directed_relay = directed_relay; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_directed_proxy(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->directed_proxy; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_directed_proxy(void *sub, uint8_t directed_proxy) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->directed_proxy = directed_proxy; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_directed_proxy_use_default(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->directed_proxy_use_default; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_directed_proxy_use_default(void *sub, uint8_t directed_proxy_use_default) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->directed_proxy_use_default = directed_proxy_use_default; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_directed_friend(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->directed_friend; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_directed_friend(void *sub, uint8_t directed_friend) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->directed_friend = directed_friend; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_use_directed(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->use_directed; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_use_directed(void *sub, uint8_t use_directed) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->use_directed = use_directed; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void *bt_mesh_ext_sub_get_proxy_client_uar(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return &SUBNET(sub)->proxy_client_uar; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +bool bt_mesh_ext_sub_get_proxy_client_uar_len_present(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->proxy_client_uar.len_present; +#else + assert(0); + return false; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_proxy_client_uar_len_present(void *sub, bool len_present) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->proxy_client_uar.len_present = len_present; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint16_t bt_mesh_ext_sub_get_proxy_client_uar_range_start(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->proxy_client_uar.range_start; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_proxy_client_uar_range_start(void *sub, uint16_t range_start) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->proxy_client_uar.range_start = range_start; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_proxy_client_uar_range_length(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->proxy_client_uar.range_length; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_proxy_client_uar_range_length(void *sub, uint8_t range_length) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->proxy_client_uar.range_length = range_length; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_path_metric_type(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->path_metric_type; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_path_metric_type(void *sub, uint8_t path_metric_type) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->path_metric_type = path_metric_type; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_path_lifetime_type(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->path_lifetime_type; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_path_lifetime_type(void *sub, uint8_t path_lifetime_type) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->path_lifetime_type = path_lifetime_type; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +bool bt_mesh_ext_sub_get_two_way_path(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->two_way_path; +#else + assert(0); + return false; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_two_way_path(void *sub, bool two_way_path) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->two_way_path = two_way_path; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_forward_number(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->forward_number; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_forward_number(void *sub, uint8_t forward_number) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->forward_number = forward_number; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_disc_table_max_disc_entries(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->discovery_table.max_disc_entries; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_disc_table_max_disc_entries(void *sub, uint8_t max_disc_entries) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->discovery_table.max_disc_entries = max_disc_entries; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_disc_table_max_concurr_init(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->discovery_table.max_concurr_init; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_disc_table_max_concurr_init(void *sub, uint8_t max_concurr_init) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->discovery_table.max_concurr_init = max_concurr_init; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_disc_table_concurr_init(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->discovery_table.concurr_init; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_disc_table_concurr_init(void *sub, uint8_t concurr_init) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->discovery_table.concurr_init = concurr_init; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_inc_disc_table_concurr_init(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->discovery_table.concurr_init++; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_dec_disc_table_concurr_init(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->discovery_table.concurr_init--; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void *bt_mesh_ext_sub_get_disc_table_mutex(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return &SUBNET(sub)->discovery_table.mutex; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void *bt_mesh_ext_sub_get_disc_table_entries(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return &SUBNET(sub)->discovery_table.entries; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_fwd_table_max_ford_entries(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->forward_table.max_ford_entries; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_fwd_table_max_ford_entries(void *sub, uint8_t max_ford_entries) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->forward_table.max_ford_entries = max_ford_entries; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_fwd_table_max_deps_nodes(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->forward_table.max_deps_nodes; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_fwd_table_max_deps_nodes(void *sub, uint8_t max_deps_nodes) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->forward_table.max_deps_nodes = max_deps_nodes; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint16_t bt_mesh_ext_sub_get_fwd_table_update_id(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->forward_table.update_id; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_fwd_table_update_id(void *sub, uint16_t update_id) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->forward_table.update_id = update_id; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_inc_fwd_table_update_id(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->forward_table.update_id++; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void *bt_mesh_ext_sub_get_fwd_table_mutex(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return &SUBNET(sub)->forward_table.mutex; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void *bt_mesh_ext_sub_get_fwd_table_entries(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return &SUBNET(sub)->forward_table.entries; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_wanted_lanes(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->wanted_lanes; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_wanted_lanes(void *sub, uint8_t wanted_lanes) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->wanted_lanes = wanted_lanes; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_unicast_echo_interval(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->unicast_echo_interval; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_unicast_echo_interval(void *sub, uint8_t unicast_echo_interval) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->unicast_echo_interval = unicast_echo_interval; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +uint8_t bt_mesh_ext_sub_get_multicast_echo_interval(void *sub) +{ +#if CONFIG_BLE_MESH_DF_SRV + return SUBNET(sub)->multicast_echo_interval; +#else + assert(0); + return 0; +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_sub_set_multicast_echo_interval(void *sub, uint8_t multicast_echo_interval) +{ +#if CONFIG_BLE_MESH_DF_SRV + SUBNET(sub)->multicast_echo_interval = multicast_echo_interval; +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +/* struct bt_mesh_net_tx */ +void *bt_mesh_ext_net_tx_get_sub(void *tx) +{ + return NET_TX(tx)->sub; +} + +void bt_mesh_ext_net_tx_set_sub(void *tx, void *sub) +{ + NET_TX(tx)->sub = sub; +} + +void bt_mesh_ext_net_tx_set_ctx(void *tx, void *ctx) +{ + NET_TX(tx)->ctx = ctx; +} + +uint16_t bt_mesh_ext_net_tx_get_net_idx(void *tx) +{ + return NET_TX(tx)->ctx->net_idx; +} + +uint16_t bt_mesh_ext_net_tx_get_app_idx(void *tx) +{ + return NET_TX(tx)->ctx->app_idx; +} + +uint16_t bt_mesh_ext_net_tx_get_addr(void *tx) +{ + return NET_TX(tx)->ctx->addr; +} + +uint8_t bt_mesh_ext_net_tx_get_send_ttl(void *tx) +{ + return NET_TX(tx)->ctx->send_ttl; +} + +uint8_t bt_mesh_ext_net_tx_get_send_cred(void *tx) +{ + return NET_TX(tx)->ctx->send_cred; +} + +void bt_mesh_ext_net_tx_set_send_cred(void *tx, uint8_t send_cred) +{ + NET_TX(tx)->ctx->send_cred = send_cred; +} + +uint8_t bt_mesh_ext_net_tx_get_send_tag(void *tx) +{ + return NET_TX(tx)->ctx->send_tag; +} + +void bt_mesh_ext_net_tx_set_send_tag(void *tx, uint8_t tag) +{ + NET_TX(tx)->ctx->send_tag = tag; +} + +uint16_t bt_mesh_ext_net_tx_get_src(void *tx) +{ + return NET_TX(tx)->src; +} + +void bt_mesh_ext_net_tx_set_src(void *tx, uint16_t src) +{ + NET_TX(tx)->src = src; +} + +void bt_mesh_ext_net_tx_set_xmit(void *tx, uint8_t xmit) +{ + NET_TX(tx)->xmit = xmit; +} + +/* struct bt_mesh_net_rx */ +void *bt_mesh_ext_net_rx_get_sub(void *rx) +{ + return NET_RX(rx)->sub; +} + +void bt_mesh_ext_net_rx_set_sub(void *rx, void *sub) +{ + NET_RX(rx)->sub = sub; +} + +uint16_t bt_mesh_ext_net_rx_get_net_idx(void *rx) +{ + return NET_RX(rx)->ctx.net_idx; +} + +void bt_mesh_ext_net_rx_set_net_idx(void *rx, uint16_t net_idx) +{ + NET_RX(rx)->ctx.net_idx = net_idx; +} + +uint16_t bt_mesh_ext_net_rx_get_app_idx(void *rx) +{ + return NET_RX(rx)->ctx.app_idx; +} + +void bt_mesh_ext_net_rx_set_app_idx(void *rx, uint16_t app_idx) +{ + NET_RX(rx)->ctx.app_idx = app_idx; +} + +uint16_t bt_mesh_ext_net_rx_get_addr(void *rx) +{ + return NET_RX(rx)->ctx.addr; +} + +void bt_mesh_ext_net_rx_set_addr(void *rx, uint16_t addr) +{ + NET_RX(rx)->ctx.addr = addr; +} + +uint16_t bt_mesh_ext_net_rx_get_recv_dst(void *rx) +{ + return NET_RX(rx)->ctx.recv_dst; +} + +void bt_mesh_ext_net_rx_set_recv_dst(void *rx, uint16_t recv_dst) +{ + NET_RX(rx)->ctx.recv_dst = recv_dst; +} + +int8_t bt_mesh_ext_net_rx_get_recv_rssi(void *rx) +{ + return NET_RX(rx)->ctx.recv_rssi; +} + +void bt_mesh_ext_net_rx_set_recv_rssi(void *rx, int8_t recv_rssi) +{ + NET_RX(rx)->ctx.recv_rssi = recv_rssi; +} + +uint32_t bt_mesh_ext_net_rx_get_recv_op(void *rx) +{ + return NET_RX(rx)->ctx.recv_op; +} + +void bt_mesh_ext_net_rx_set_recv_op(void *rx, uint32_t recv_op) +{ + NET_RX(rx)->ctx.recv_op = recv_op; +} + +uint8_t bt_mesh_ext_net_rx_get_recv_ttl(void *rx) +{ + return NET_RX(rx)->ctx.recv_ttl; +} + +void bt_mesh_ext_net_rx_set_recv_ttl(void *rx, uint8_t recv_ttl) +{ + NET_RX(rx)->ctx.recv_ttl = recv_ttl; +} + +uint8_t bt_mesh_ext_net_rx_get_recv_cred(void *rx) +{ + return NET_RX(rx)->ctx.recv_cred; +} + +void bt_mesh_ext_net_rx_set_recv_cred(void *rx, uint8_t recv_cred) +{ + NET_RX(rx)->ctx.recv_cred = recv_cred; +} + +uint8_t bt_mesh_ext_net_rx_get_recv_tag(void *rx) +{ + return NET_RX(rx)->ctx.recv_tag; +} + +void bt_mesh_ext_net_rx_set_recv_tag(void *rx, uint8_t recv_tag) +{ + NET_RX(rx)->ctx.recv_tag = recv_tag; +} + +uint8_t bt_mesh_ext_net_rx_get_send_ttl(void *rx) +{ + return NET_RX(rx)->ctx.send_ttl; +} + +void bt_mesh_ext_net_rx_set_send_ttl(void *rx, uint8_t send_ttl) +{ + NET_RX(rx)->ctx.send_ttl = send_ttl; +} + +uint8_t bt_mesh_ext_net_rx_get_send_cred(void *rx) +{ + return NET_RX(rx)->ctx.send_cred; +} + +void bt_mesh_ext_net_rx_set_send_cred(void *rx, uint8_t send_cred) +{ + NET_RX(rx)->ctx.send_cred = send_cred; +} + +uint8_t bt_mesh_ext_net_rx_get_send_tag(void *rx) +{ + return NET_RX(rx)->ctx.send_tag; +} + +void bt_mesh_ext_net_rx_set_send_tag(void *rx, uint8_t send_tag) +{ + NET_RX(rx)->ctx.send_tag = send_tag; +} + +uint32_t bt_mesh_ext_net_rx_get_seq(void *rx) +{ + return NET_RX(rx)->seq; +} + +void bt_mesh_ext_net_rx_set_seq(void *rx, uint32_t seq) +{ + NET_RX(rx)->seq = seq; +} + +bool bt_mesh_ext_net_rx_get_new_key(void *rx) +{ + return NET_RX(rx)->new_key; +} + +void bt_mesh_ext_net_rx_set_new_key(void *rx, bool new_key) +{ + NET_RX(rx)->new_key = new_key; +} + +bool bt_mesh_ext_net_rx_get_ctl(void *rx) +{ + return NET_RX(rx)->ctl; +} + +void bt_mesh_ext_net_rx_set_ctl(void *rx, bool ctl) +{ + NET_RX(rx)->ctl = ctl; +} + +uint8_t bt_mesh_ext_net_rx_get_net_if(void *rx) +{ + return NET_RX(rx)->net_if; +} + +void bt_mesh_ext_net_rx_set_net_if(void *rx, uint8_t net_if) +{ + NET_RX(rx)->net_if = net_if; +} + +/* struct bt_mesh_msg_ctx */ +uint16_t bt_mesh_ext_msg_ctx_get_net_idx(void *ctx) +{ + return MSG_CTX(ctx)->net_idx; +} + +void bt_mesh_ext_msg_ctx_set_net_idx(void *ctx, uint16_t net_idx) +{ + MSG_CTX(ctx)->net_idx = net_idx; +} + +uint16_t bt_mesh_ext_msg_ctx_get_app_idx(void *ctx) +{ + return MSG_CTX(ctx)->app_idx; +} + +void bt_mesh_ext_msg_ctx_set_app_idx(void *ctx, uint16_t app_idx) +{ + MSG_CTX(ctx)->app_idx = app_idx; +} + +uint16_t bt_mesh_ext_msg_ctx_get_addr(void *ctx) +{ + return MSG_CTX(ctx)->addr; +} + +void bt_mesh_ext_msg_ctx_set_addr(void *ctx, uint16_t addr) +{ + MSG_CTX(ctx)->addr = addr; +} + +uint16_t bt_mesh_ext_msg_ctx_get_recv_dst(void *ctx) +{ + return MSG_CTX(ctx)->recv_dst; +} + +void bt_mesh_ext_msg_ctx_set_recv_dst(void *ctx, uint16_t recv_dst) +{ + MSG_CTX(ctx)->recv_dst = recv_dst; +} + +int8_t bt_mesh_ext_msg_ctx_get_recv_rssi(void *ctx) +{ + return MSG_CTX(ctx)->recv_rssi; +} + +void bt_mesh_ext_msg_ctx_set_recv_rssi(void *ctx, int8_t recv_rssi) +{ + MSG_CTX(ctx)->recv_rssi = recv_rssi; +} + +uint32_t bt_mesh_ext_msg_ctx_get_recv_op(void *ctx) +{ + return MSG_CTX(ctx)->recv_op; +} + +void bt_mesh_ext_msg_ctx_set_recv_op(void *ctx, uint32_t recv_op) +{ + MSG_CTX(ctx)->recv_op = recv_op; +} + +uint8_t bt_mesh_ext_msg_ctx_get_recv_ttl(void *ctx) +{ + return MSG_CTX(ctx)->recv_ttl; +} + +void bt_mesh_ext_msg_ctx_set_recv_ttl(void *ctx, uint8_t recv_ttl) +{ + MSG_CTX(ctx)->recv_ttl = recv_ttl; +} + +uint8_t bt_mesh_ext_msg_ctx_get_recv_cred(void *ctx) +{ + return MSG_CTX(ctx)->recv_cred; +} + +void bt_mesh_ext_msg_ctx_set_recv_cred(void *ctx, uint8_t recv_cred) +{ + MSG_CTX(ctx)->recv_cred = recv_cred; +} + +uint8_t bt_mesh_ext_msg_ctx_get_recv_tag(void *ctx) +{ + return MSG_CTX(ctx)->recv_tag; +} + +void bt_mesh_ext_msg_ctx_set_recv_tag(void *ctx, uint8_t recv_tag) +{ + MSG_CTX(ctx)->recv_tag = recv_tag; +} + +uint8_t bt_mesh_ext_msg_ctx_get_send_szmic(void *ctx) +{ + return MSG_CTX(ctx)->send_szmic; +} + +void bt_mesh_ext_msg_ctx_set_send_szmic(void *ctx, uint8_t send_szmic) +{ + MSG_CTX(ctx)->send_szmic = send_szmic; +} + +uint8_t bt_mesh_ext_msg_ctx_get_send_ttl(void *ctx) +{ + return MSG_CTX(ctx)->send_ttl; +} + +void bt_mesh_ext_msg_ctx_set_send_ttl(void *ctx, uint8_t send_ttl) +{ + MSG_CTX(ctx)->send_ttl = send_ttl; +} + +uint8_t bt_mesh_ext_msg_ctx_get_send_cred(void *ctx) +{ + return MSG_CTX(ctx)->send_cred; +} + +void bt_mesh_ext_msg_ctx_set_send_cred(void *ctx, uint8_t send_cred) +{ + MSG_CTX(ctx)->send_cred = send_cred; +} + +uint8_t bt_mesh_ext_msg_ctx_get_send_tag(void *ctx) +{ + return MSG_CTX(ctx)->send_tag; +} + +void bt_mesh_ext_msg_ctx_set_send_tag(void *ctx, uint8_t send_tag) +{ + MSG_CTX(ctx)->send_tag = send_tag; +} + +/* Client */ +int bt_mesh_ext_client_clear_list(void *data) +{ + return bt_mesh_client_clear_list(data); +} + +int bt_mesh_ext_client_free_node(void *node) +{ + return bt_mesh_client_free_node(node); +} + +uint32_t bt_mesh_ext_client_common_get_opcode(void *param) +{ + return CLI_PARAM(param)->opcode; +} + +void *bt_mesh_ext_client_common_get_model(void *param) +{ + return CLI_PARAM(param)->model; +} + +void bt_mesh_ext_client_common_set_opcode(void *param, uint32_t opcode) +{ + CLI_PARAM(param)->opcode = opcode; +} + +void bt_mesh_ext_client_common_set_model(void *param, void *model) +{ + CLI_PARAM(param)->model = model; +} + +void *bt_mesh_ext_client_common_get_ctx(void *param) +{ + return &CLI_PARAM(param)->ctx; +} + +uint16_t bt_mesh_ext_client_common_get_net_idx(void *param) +{ + return CLI_PARAM(param)->ctx.net_idx; +} + +void bt_mesh_ext_client_common_set_net_idx(void *param, uint16_t net_idx) +{ + CLI_PARAM(param)->ctx.net_idx = net_idx; +} + +uint16_t bt_mesh_ext_client_common_get_app_idx(void *param) +{ + return CLI_PARAM(param)->ctx.app_idx; +} + +void bt_mesh_ext_client_common_set_app_idx(void *param, uint16_t app_idx) +{ + CLI_PARAM(param)->ctx.app_idx = app_idx; +} + +uint16_t bt_mesh_ext_client_common_get_addr(void *param) +{ + return CLI_PARAM(param)->ctx.addr; +} + +void bt_mesh_ext_client_common_set_addr(void *param, uint16_t addr) +{ + CLI_PARAM(param)->ctx.addr = addr; +} + +uint8_t bt_mesh_ext_client_common_get_send_ttl(void *param) +{ + return CLI_PARAM(param)->ctx.send_ttl; +} + +void bt_mesh_ext_client_common_set_send_ttl(void *param, uint8_t send_ttl) +{ + CLI_PARAM(param)->ctx.send_ttl = send_ttl; +} + +void bt_mesh_ext_client_common_set_cb(void *param, void *cb) +{ + CLI_PARAM(param)->cb = cb; +} + +void bt_mesh_ext_client_common_set_cb_data(void *param, void *cb_data) +{ + CLI_PARAM(param)->cb_data = cb_data; +} + +void *bt_mesh_ext_client_node_get_with_work(void *work) +{ + return CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); +} + +uint32_t bt_mesh_ext_client_node_get_opcode(void *node) +{ + return CLI_NODE(node)->opcode; +} + +void *bt_mesh_ext_client_node_get_model(void *node) +{ + return CLI_NODE(node)->model; +} + +void *bt_mesh_ext_client_node_get_timer(void *node) +{ + return &CLI_NODE(node)->timer; +} + +void *bt_mesh_ext_client_node_get_ctx(void *node) +{ + return &CLI_NODE(node)->ctx; +} + +void *bt_mesh_ext_is_client_recv_publish_msg(void *model, void *ctx, + struct net_buf_simple *buf, + bool need_pub) +{ + return bt_mesh_is_client_recv_publish_msg(model, ctx, buf, need_pub); +} + +int bt_mesh_ext_client_send_msg(void *param, struct net_buf_simple *msg, + bool need_ack, void (*timeout_cb)(void *work)) +{ + return bt_mesh_client_send_msg(param, msg, need_ack, VOID(timeout_cb)); +} + +/* Bridge Configuration */ +bool bt_mesh_ext_bridge_rpl_check(void *rx, void **match) +{ + return bt_mesh_bridge_rpl_check(rx, (struct bt_mesh_rpl **)match); +} + +#if CONFIG_BLE_MESH_BRC_SRV +struct bt_mesh_subnet_bridge_table { + uint8_t bridge_direction; + uint8_t bridge_net_idx[3]; + uint16_t bridge_addr_1; + uint16_t bridge_addr_2; +}; + +struct bt_mesh_bridge_cfg_srv { + struct bt_mesh_model *model; + + uint8_t subnet_bridge; + uint16_t bridging_table_size; + struct bt_mesh_subnet_bridge_table bridge_table[CONFIG_BLE_MESH_MAX_BRIDGING_TABLE_ENTRY_COUNT]; +}; +#endif /* CONFIG_BLE_MESH_BRC_SRV */ + +void *bt_mesh_ext_brc_srv_get_bridge_table_entry(void *srv, uint8_t index) +{ +#if CONFIG_BLE_MESH_BRC_SRV + return &((struct bt_mesh_bridge_cfg_srv *)srv)->bridge_table[index]; +#else + assert(0); + return NULL; +#endif /* CONFIG_BLE_MESH_BRC_SRV */ +} + +/* BTC */ +void bt_mesh_ext_agg_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const uint8_t *val, size_t len) +{ +#if CONFIG_BLE_MESH_AGG_CLI + bt_mesh_agg_client_cb_evt_to_btc(opcode, event, model, ctx, val, len); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_AGG_CLI */ +} + +void bt_mesh_ext_agg_server_cb_evt_to_btc(uint8_t event, void *model, void *ctx, + const void *val, size_t len) +{ +#if CONFIG_BLE_MESH_AGG_SRV + bt_mesh_agg_server_cb_evt_to_btc(event, model, ctx, val, len); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_AGG_SRV */ +} + +void bt_mesh_ext_brc_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const uint8_t *val, size_t len) +{ +#if CONFIG_BLE_MESH_BRC_CLI + bt_mesh_brc_client_cb_evt_to_btc(opcode, event, model, ctx, val, len); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_BRC_CLI */ +} + +void bt_mesh_ext_brc_server_cb_evt_to_btc(uint8_t event, void *model, void *ctx, + const uint8_t *val, size_t len) +{ +#if CONFIG_BLE_MESH_BRC_SRV + bt_mesh_brc_server_cb_evt_to_btc(event, model, ctx, val, len); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_BRC_SRV */ +} + +void bt_mesh_ext_df_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const uint8_t *val, size_t len) +{ +#if CONFIG_BLE_MESH_DF_CLI + bt_mesh_df_client_cb_evt_to_btc(opcode, event, model, ctx, val, len); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_CLI */ +} + +void bt_mesh_ext_df_server_cb_evt_to_btc(uint8_t event, void *model, void *ctx, + const uint8_t *val, size_t len) +{ +#if CONFIG_BLE_MESH_DF_SRV + bt_mesh_df_server_cb_evt_to_btc(event, model, ctx, val, len); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_DF_SRV */ +} + +void bt_mesh_ext_lcd_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const void *val, size_t len) +{ +#if CONFIG_BLE_MESH_LCD_CLI + bt_mesh_lcd_client_cb_evt_to_btc(opcode, event, model, ctx, val, len); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_LCD_CLI */ +} + +void bt_mesh_ext_odp_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const uint8_t *val, size_t len) +{ +#if CONFIG_BLE_MESH_ODP_CLI + bt_mesh_odp_client_cb_evt_to_btc(opcode, event, model, ctx, val, len); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_ODP_CLI */ +} + +void bt_mesh_ext_prb_client_cb_evt_to_btc(uint32_t opcode, uint8_t evt_type, + void *model, void *ctx, + const uint8_t *val, uint16_t len) +{ +#if CONFIG_BLE_MESH_PRB_CLI + bt_mesh_prb_client_cb_evt_to_btc(opcode, evt_type, model, ctx, val, len); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_PRB_CLI */ +} + +void btc_ble_mesh_ext_rpr_client_link_close_cb(void *model, uint16_t rpr_srv_addr, uint8_t reason) +{ +#if CONFIG_BLE_MESH_RPR_CLI + btc_ble_mesh_rpr_client_link_close_cb(model, rpr_srv_addr, reason); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_RPR_CLI */ +} + +void btc_ble_mesh_ext_rpr_client_prov_comp_cb(void *model, uint16_t rpr_srv_addr, + uint8_t nppi, uint16_t index, + uint8_t uuid[16], uint16_t unicast_addr, + uint8_t element_num, uint16_t net_idx) +{ +#if CONFIG_BLE_MESH_RPR_CLI + btc_ble_mesh_rpr_client_prov_comp_cb(model, rpr_srv_addr, nppi, index, uuid, + unicast_addr, element_num, net_idx); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_RPR_CLI */ +} + +void bt_mesh_ext_rpr_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const void *val, size_t len) +{ +#if CONFIG_BLE_MESH_RPR_CLI + bt_mesh_rpr_client_cb_evt_to_btc(opcode, event, model, ctx, val, len); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_RPR_CLI */ +} + +void bt_mesh_ext_rpr_server_cb_evt_to_btc(uint8_t event, const void *val, size_t len) +{ +#if CONFIG_BLE_MESH_RPR_SRV + bt_mesh_rpr_server_cb_evt_to_btc(event, val, len); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_RPR_SRV */ +} + +void bt_mesh_ext_sar_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const uint8_t *val, size_t len) +{ +#if CONFIG_BLE_MESH_SAR_CLI + bt_mesh_sar_client_cb_evt_to_btc(opcode, event, model, ctx, val, len); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_SAR_CLI */ +} + +void bt_mesh_ext_srpl_client_cb_evt_to_btc(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const uint8_t *val, size_t len) +{ +#if CONFIG_BLE_MESH_SRPL_CLI + bt_mesh_srpl_client_cb_evt_to_btc(opcode, event, model, ctx, val, len); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_SRPL_CLI */ +} + +void bt_mesh_ext_mbt_client_cb_evt_to_btc(uint8_t event, uint8_t result, void *model) +{ +#if CONFIG_BLE_MESH_MBT_CLI + bt_mesh_mbt_client_cb_evt_to_btc(event, result, model); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_MBT_CLI */ +} + +void bt_mesh_ext_mbt_server_cb_evt_to_btc(uint8_t event, void *model, void *ctx) +{ +#if CONFIG_BLE_MESH_MBT_SRV + bt_mesh_mbt_server_cb_evt_to_btc(event, model, ctx); +#else + assert(0); +#endif /* CONFIG_BLE_MESH_MBT_SRV */ +} + +typedef struct { + uint64_t config_ble_mesh_use_duplicate_scan : 1; + uint64_t config_ble_mesh_pb_adv : 1; + uint64_t config_ble_mesh_pb_gatt : 1; + uint64_t config_ble_mesh_prov_epa : 1; + uint64_t config_ble_mesh_cert_based_prov : 1; + uint64_t config_ble_mesh_gatt_proxy_client : 1; + uint64_t config_ble_mesh_gatt_proxy_server : 1; + uint64_t config_ble_mesh_proxy_solic_pdu_rx : 1; + uint64_t config_ble_mesh_proxy_solic_pdu_tx : 1; + uint64_t config_ble_mesh_proxy_solic : 1; + uint64_t config_ble_mesh_support_directed_proxy : 1; + uint64_t config_ble_mesh_relay : 1; + uint64_t config_ble_mesh_settings : 1; + uint64_t config_ble_mesh_friend : 1; + uint64_t config_ble_mesh_low_power : 1; + uint64_t config_ble_mesh_agg_cli : 1; + uint64_t config_ble_mesh_agg_srv : 1; + uint64_t config_ble_mesh_brc_cli : 1; + uint64_t config_ble_mesh_brc_srv : 1; + uint64_t config_ble_mesh_df_cli : 1; + uint64_t config_ble_mesh_df_srv : 1; + uint64_t config_ble_mesh_path_monitor_test : 1; + uint64_t config_ble_mesh_lcd_cli : 1; + uint64_t config_ble_mesh_lcd_srv : 1; + uint64_t config_ble_mesh_comp_data_1 : 1; + uint64_t config_ble_mesh_comp_data_128 : 1; + uint64_t config_ble_mesh_models_metadata_0 : 1; + uint64_t config_ble_mesh_models_metadata_128 : 1; + uint64_t config_ble_mesh_mbt_cli : 1; + uint64_t config_ble_mesh_mbt_srv : 1; + uint64_t config_ble_mesh_odp_cli : 1; + uint64_t config_ble_mesh_odp_srv : 1; + uint64_t config_ble_mesh_prb_cli : 1; + uint64_t config_ble_mesh_prb_srv : 1; + uint64_t config_ble_mesh_private_beacon : 1; + uint64_t config_ble_mesh_rpr_cli : 1; + uint64_t config_ble_mesh_rpr_srv : 1; + uint64_t config_ble_mesh_rpr_srv_active_scan : 1; + uint64_t config_ble_mesh_sar_cli : 1; + uint64_t config_ble_mesh_sar_srv : 1; + uint64_t config_ble_mesh_srpl_cli : 1; + uint64_t config_ble_mesh_srpl_srv : 1; + + uint16_t config_ble_mesh_record_frag_max_size; + uint16_t config_ble_mesh_crpl; + uint16_t config_ble_mesh_proxy_solic_rx_crpl; + uint16_t config_ble_mesh_proxy_solic_tx_src_count; + uint16_t config_ble_mesh_max_bridging_table_entry_count; + uint16_t config_ble_mesh_max_disc_table_entry_count; + uint16_t config_ble_mesh_max_forward_table_entry_count; + uint16_t config_ble_mesh_max_deps_nodes_per_path; + uint16_t config_ble_mesh_rpr_cli_prov_same_time; + uint16_t config_ble_mesh_rpr_srv_max_scanned_items; + uint16_t config_ble_mesh_rpr_srv_max_ext_scan; + uint16_t config_ble_mesh_max_blob_receivers; + + uint8_t struct_net_tx_size; + uint8_t struct_net_rx_size; + uint8_t struct_client_common_size; + uint8_t struct_client_internal_data_size; + uint8_t struct_msg_ctx_size; + uint8_t struct_mutex_size; + uint8_t struct_timer_size; + uint8_t struct_snode_size; + uint8_t struct_slist_size; + uint8_t struct_slist_off_tail; + uint8_t struct_net_buf_simple_size; + uint8_t struct_net_buf_simple_off_len; + uint8_t struct_net_buf_simple_off_size; + uint8_t struct_net_buf_simple_off_buf; + uint8_t struct_net_buf_simple_state_size; + uint8_t struct_net_buf_simple_state_off_len; + uint8_t struct_client_op_pair_size; + uint8_t struct_client_op_pair_off_status_op; + uint8_t struct_model_op_size; + uint8_t struct_model_op_off_min_len; + uint8_t struct_model_op_off_func; + uint8_t struct_model_cb_size; + uint8_t struct_model_cb_off_deinit; + uint8_t struct_send_cb_size; + uint8_t struct_send_cb_off_end; + uint8_t struct_addr_size; + uint8_t struct_addr_off_val; + uint8_t struct_sg_size; + uint8_t struct_sg_off_len; + uint8_t struct_tc_sha256_state; + uint8_t struct_tc_sha256_off_bits_hashed; + uint8_t struct_tc_sha256_off_leftover; + uint8_t struct_tc_sha256_off_leftover_offset; + uint8_t struct_tc_hmac_state_size; + uint8_t struct_tc_hmac_state_off_key; + + uint8_t btc_ble_mesh_evt_agg_client_send_timeout; + uint8_t btc_ble_mesh_evt_agg_client_recv_rsp; + uint8_t btc_ble_mesh_evt_agg_client_recv_pub; + uint8_t btc_ble_mesh_evt_agg_server_recv_msg; + uint8_t btc_ble_mesh_evt_brc_client_recv_rsp; + uint8_t btc_ble_mesh_evt_brc_client_recv_pub; + uint8_t btc_ble_mesh_evt_brc_client_send_timeout; + uint8_t btc_ble_mesh_evt_brc_server_state_change; + uint8_t btc_ble_mesh_evt_df_client_send_timeout; + uint8_t btc_ble_mesh_evt_df_client_recv_get_rsp; + uint8_t btc_ble_mesh_evt_df_client_recv_set_rsp; + uint8_t btc_ble_mesh_evt_df_client_recv_pub; + uint8_t btc_ble_mesh_evt_df_server_state_change; + uint8_t btc_ble_mesh_evt_df_server_table_change; + uint8_t btc_ble_mesh_evt_lcd_client_send_timeout; + uint8_t btc_ble_mesh_evt_lcd_client_recv_rsp; + uint8_t btc_ble_mesh_evt_lcd_client_recv_pub; + uint8_t btc_ble_mesh_evt_mbt_client_retrieve_capabilities_comp; + uint8_t btc_ble_mesh_evt_mbt_client_transfer_blob_comp; + uint8_t btc_ble_mesh_evt_mbt_client_send_block_comp; + uint8_t btc_ble_mesh_evt_mbt_client_send_data_comp; + uint8_t btc_ble_mesh_evt_mbt_client_determine_block_status_comp; + uint8_t btc_ble_mesh_evt_mbt_client_determine_transfer_status_comp; + uint8_t btc_ble_mesh_evt_mbt_client_cancel_transfer_comp; + uint8_t btc_ble_mesh_evt_mbt_client_set_transfer_ttl_comp; + uint8_t btc_ble_mesh_evt_mbt_client_clear_transfer_ttl_comp; + uint8_t btc_ble_mesh_evt_mbt_client_set_app_idx_comp; + uint8_t btc_ble_mesh_evt_mbt_client_clear_app_idx_comp; + uint8_t btc_ble_mesh_evt_mbt_client_set_multicast_addr_comp; + uint8_t btc_ble_mesh_evt_mbt_client_clear_multicast_addr_comp; + uint8_t btc_ble_mesh_mbt_client_result_complete; + uint8_t btc_ble_mesh_mbt_client_result_fail; + uint8_t btc_ble_mesh_evt_mbt_server_initialize_blob_receive_comp; + uint8_t btc_ble_mesh_evt_mbt_server_cancel_blob_receive_comp; + uint8_t btc_ble_mesh_evt_mbt_server_set_blob_capabilities_comp; + uint8_t btc_ble_mesh_evt_mbt_server_blob_transfer_get; + uint8_t btc_ble_mesh_evt_mbt_server_blob_transfer_start; + uint8_t btc_ble_mesh_evt_mbt_server_blob_transfer_cancel; + uint8_t btc_ble_mesh_evt_mbt_server_blob_block_get; + uint8_t btc_ble_mesh_evt_mbt_server_blob_block_start; + uint8_t btc_ble_mesh_evt_mbt_server_blob_chunk_transfer; + uint8_t btc_ble_mesh_evt_mbt_server_blob_information_get; + uint8_t btc_ble_mesh_evt_mbt_server_block_receive_comp; + uint8_t btc_ble_mesh_evt_mbt_server_blob_receive_comp; + uint8_t btc_ble_mesh_evt_mbt_server_blob_receive_timeout; + uint8_t btc_ble_mesh_evt_odp_client_send_timeout; + uint8_t btc_ble_mesh_evt_odp_client_recv_rsp; + uint8_t btc_ble_mesh_evt_odp_client_recv_pub; + uint8_t btc_ble_mesh_evt_prb_client_recv_rsp; + uint8_t btc_ble_mesh_evt_prb_client_recv_pub; + uint8_t btc_ble_mesh_evt_prb_client_send_timeout; + uint8_t btc_ble_mesh_evt_rpr_client_send_timeout; + uint8_t btc_ble_mesh_evt_rpr_client_recv_rsp; + uint8_t btc_ble_mesh_evt_rpr_client_recv_pub; + uint8_t btc_ble_mesh_evt_rpr_server_scan_start; + uint8_t btc_ble_mesh_evt_rpr_server_scan_stop; + uint8_t btc_ble_mesh_evt_rpr_server_ext_scan_start; + uint8_t btc_ble_mesh_evt_rpr_server_ext_scan_stop; + uint8_t btc_ble_mesh_evt_rpr_server_link_open; + uint8_t btc_ble_mesh_evt_rpr_server_link_close; + uint8_t btc_ble_mesh_evt_sar_client_send_timeout; + uint8_t btc_ble_mesh_evt_sar_client_recv_rsp; + uint8_t btc_ble_mesh_evt_sar_client_recv_pub; + uint8_t btc_ble_mesh_evt_srpl_client_send_timeout; + uint8_t btc_ble_mesh_evt_srpl_client_recv_rsp; + uint8_t btc_ble_mesh_evt_srpl_client_recv_pub; +} bt_mesh_ext_config_t; + +static const bt_mesh_ext_config_t bt_mesh_ext_cfg = { + .config_ble_mesh_use_duplicate_scan = IS_ENABLED(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN), + .config_ble_mesh_pb_adv = IS_ENABLED(CONFIG_BLE_MESH_PB_ADV), + .config_ble_mesh_pb_gatt = IS_ENABLED(CONFIG_BLE_MESH_PB_GATT), + .config_ble_mesh_prov_epa = IS_ENABLED(CONFIG_BLE_MESH_PROV_EPA), + .config_ble_mesh_cert_based_prov = IS_ENABLED(CONFIG_BLE_MESH_CERT_BASED_PROV), + .config_ble_mesh_gatt_proxy_client = IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT), + .config_ble_mesh_gatt_proxy_server = IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER), + .config_ble_mesh_proxy_solic_pdu_rx = IS_ENABLED(CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX), + .config_ble_mesh_proxy_solic_pdu_tx = IS_ENABLED(CONFIG_BLE_MESH_PROXY_SOLIC_PDU_TX), + .config_ble_mesh_proxy_solic = (IS_ENABLED(CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX) | \ + IS_ENABLED(CONFIG_BLE_MESH_PROXY_SOLIC_PDU_TX)), + .config_ble_mesh_support_directed_proxy = IS_ENABLED(CONFIG_BLE_MESH_SUPPORT_DIRECTED_PROXY), + .config_ble_mesh_relay = IS_ENABLED(CONFIG_BLE_MESH_RELAY), + .config_ble_mesh_settings = IS_ENABLED(CONFIG_BLE_MESH_SETTINGS), + .config_ble_mesh_friend = IS_ENABLED(CONFIG_BLE_MESH_FRIEND), + .config_ble_mesh_low_power = IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER), + .config_ble_mesh_agg_cli = IS_ENABLED(CONFIG_BLE_MESH_AGG_CLI), + .config_ble_mesh_agg_srv = IS_ENABLED(CONFIG_BLE_MESH_AGG_SRV), + .config_ble_mesh_brc_cli = IS_ENABLED(CONFIG_BLE_MESH_BRC_CLI), + .config_ble_mesh_brc_srv = IS_ENABLED(CONFIG_BLE_MESH_BRC_SRV), + .config_ble_mesh_df_cli = IS_ENABLED(CONFIG_BLE_MESH_DF_CLI), + .config_ble_mesh_df_srv = IS_ENABLED(CONFIG_BLE_MESH_DF_SRV), + .config_ble_mesh_path_monitor_test = IS_ENABLED(CONFIG_BLE_MESH_PATH_MONITOR_TEST), + .config_ble_mesh_lcd_cli = IS_ENABLED(CONFIG_BLE_MESH_LCD_CLI), + .config_ble_mesh_lcd_srv = IS_ENABLED(CONFIG_BLE_MESH_LCD_SRV), + .config_ble_mesh_comp_data_1 = IS_ENABLED(CONFIG_BLE_MESH_COMP_DATA_1), + .config_ble_mesh_comp_data_128 = IS_ENABLED(CONFIG_BLE_MESH_COMP_DATA_128), + .config_ble_mesh_models_metadata_0 = IS_ENABLED(CONFIG_BLE_MESH_MODELS_METADATA_0), + .config_ble_mesh_models_metadata_128 = IS_ENABLED(CONFIG_BLE_MESH_MODELS_METADATA_128), + .config_ble_mesh_mbt_cli = IS_ENABLED(CONFIG_BLE_MESH_MBT_CLI), + .config_ble_mesh_mbt_srv = IS_ENABLED(CONFIG_BLE_MESH_MBT_SRV), + .config_ble_mesh_odp_cli = IS_ENABLED(CONFIG_BLE_MESH_ODP_CLI), + .config_ble_mesh_odp_srv = IS_ENABLED(CONFIG_BLE_MESH_ODP_SRV), + .config_ble_mesh_prb_cli = IS_ENABLED(CONFIG_BLE_MESH_PRB_CLI), + .config_ble_mesh_prb_srv = IS_ENABLED(CONFIG_BLE_MESH_PRB_SRV), + .config_ble_mesh_private_beacon = (IS_ENABLED(CONFIG_BLE_MESH_PRB_SRV) | \ + IS_ENABLED(CONFIG_BLE_MESH_PRB_SRV)), + .config_ble_mesh_rpr_cli = IS_ENABLED(CONFIG_BLE_MESH_RPR_CLI), + .config_ble_mesh_rpr_srv = IS_ENABLED(CONFIG_BLE_MESH_RPR_SRV), + .config_ble_mesh_rpr_srv_active_scan = IS_ENABLED(CONFIG_BLE_MESH_RPR_SRV_ACTIVE_SCAN), + .config_ble_mesh_sar_cli = IS_ENABLED(CONFIG_BLE_MESH_SAR_CLI), + .config_ble_mesh_sar_srv = IS_ENABLED(CONFIG_BLE_MESH_SAR_SRV), + .config_ble_mesh_srpl_cli = IS_ENABLED(CONFIG_BLE_MESH_SRPL_CLI), + .config_ble_mesh_srpl_srv = IS_ENABLED(CONFIG_BLE_MESH_SRPL_SRV), + +#if CONFIG_BLE_MESH_CERT_BASED_PROV + .config_ble_mesh_record_frag_max_size = CONFIG_BLE_MESH_RECORD_FRAG_MAX_SIZE, +#endif /* CONFIG_BLE_MESH_CERT_BASED_PROV */ + .config_ble_mesh_crpl = CONFIG_BLE_MESH_CRPL, +#if CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX + .config_ble_mesh_proxy_solic_rx_crpl = CONFIG_BLE_MESH_PROXY_SOLIC_RX_CRPL, +#endif /* CONFIG_BLE_MESH_PROXY_SOLIC_PDU_RX */ +#if CONFIG_BLE_MESH_PROXY_SOLIC_PDU_TX + .config_ble_mesh_proxy_solic_tx_src_count = CONFIG_BLE_MESH_PROXY_SOLIC_TX_SRC_COUNT, +#endif /* CONFIG_BLE_MESH_PROXY_SOLIC_PDU_TX */ +#if CONFIG_BLE_MESH_BRC_SRV + .config_ble_mesh_max_bridging_table_entry_count = CONFIG_BLE_MESH_MAX_BRIDGING_TABLE_ENTRY_COUNT, +#endif /* CONFIG_BLE_MESH_BRC_SRV */ +#if CONFIG_BLE_MESH_DF_SRV + .config_ble_mesh_max_disc_table_entry_count = CONFIG_BLE_MESH_MAX_DISC_TABLE_ENTRY_COUNT, + .config_ble_mesh_max_forward_table_entry_count = CONFIG_BLE_MESH_MAX_FORWARD_TABLE_ENTRY_COUNT, + .config_ble_mesh_max_deps_nodes_per_path = CONFIG_BLE_MESH_MAX_DEPS_NODES_PER_PATH, +#endif /* CONFIG_BLE_MESH_DF_SRV */ +#if CONFIG_BLE_MESH_RPR_CLI + .config_ble_mesh_rpr_cli_prov_same_time = CONFIG_BLE_MESH_RPR_CLI_PROV_SAME_TIME, +#endif /* CONFIG_BLE_MESH_RPR_CLI */ +#if CONFIG_BLE_MESH_RPR_SRV + .config_ble_mesh_rpr_srv_max_scanned_items = CONFIG_BLE_MESH_RPR_SRV_MAX_SCANNED_ITEMS, + .config_ble_mesh_rpr_srv_max_ext_scan = CONFIG_BLE_MESH_RPR_SRV_MAX_EXT_SCAN, +#endif /* CONFIG_BLE_MESH_RPR_SRV */ +#if CONFIG_BLE_MESH_MBT_CLI + .config_ble_mesh_max_blob_receivers = CONFIG_BLE_MESH_MAX_BLOB_RECEIVERS, +#endif /* CONFIG_BLE_MESH_MBT_CLI */ + + .struct_net_tx_size = sizeof(struct bt_mesh_net_tx), + .struct_net_rx_size = sizeof(struct bt_mesh_net_rx), + .struct_client_common_size = sizeof(bt_mesh_client_common_param_t), + .struct_client_internal_data_size = sizeof(bt_mesh_client_internal_data_t), + .struct_msg_ctx_size = sizeof(struct bt_mesh_msg_ctx), + .struct_mutex_size = sizeof(bt_mesh_mutex_t), + .struct_timer_size = sizeof(struct k_delayed_work), + .struct_snode_size = sizeof(struct _snode), + .struct_slist_size = sizeof(struct _slist), + .struct_slist_off_tail = offsetof(struct _slist, tail), + .struct_net_buf_simple_size = sizeof(struct net_buf_simple), + .struct_net_buf_simple_off_len = offsetof(struct net_buf_simple, len), + .struct_net_buf_simple_off_size = offsetof(struct net_buf_simple, size), + .struct_net_buf_simple_off_buf = offsetof(struct net_buf_simple, __buf), + .struct_net_buf_simple_state_size = sizeof(struct net_buf_simple_state), + .struct_net_buf_simple_state_off_len = offsetof(struct net_buf_simple_state, len), + .struct_client_op_pair_size = sizeof(bt_mesh_client_op_pair_t), + .struct_client_op_pair_off_status_op = offsetof(bt_mesh_client_op_pair_t, status_op), + .struct_model_op_size = sizeof(struct bt_mesh_model_op), + .struct_model_op_off_min_len = offsetof(struct bt_mesh_model_op, min_len), + .struct_model_op_off_func = offsetof(struct bt_mesh_model_op, func), +#if 0 + .struct_model_cb_size = sizeof(struct bt_mesh_model_cb), + .struct_model_cb_off_deinit = offsetof(struct bt_mesh_model_cb, deinit), +#endif + .struct_send_cb_size = sizeof(struct bt_mesh_send_cb), + .struct_send_cb_off_end = offsetof(struct bt_mesh_send_cb, end), + .struct_addr_size = sizeof(bt_mesh_addr_t), + .struct_addr_off_val = offsetof(bt_mesh_addr_t, val), + .struct_sg_size = sizeof(struct bt_mesh_sg), + .struct_sg_off_len = offsetof(struct bt_mesh_sg, len), + .struct_tc_sha256_state = sizeof(struct tc_sha256_state_struct), + .struct_tc_sha256_off_bits_hashed = offsetof(struct tc_sha256_state_struct, bits_hashed), + .struct_tc_sha256_off_leftover = offsetof(struct tc_sha256_state_struct, leftover), + .struct_tc_sha256_off_leftover_offset = offsetof(struct tc_sha256_state_struct, leftover_offset), + .struct_tc_hmac_state_size = sizeof(struct tc_hmac_state_struct), + .struct_tc_hmac_state_off_key = offsetof(struct tc_hmac_state_struct, key), + + .btc_ble_mesh_evt_agg_client_send_timeout = BTC_BLE_MESH_EVT_AGG_CLIENT_SEND_TIMEOUT, + .btc_ble_mesh_evt_agg_client_recv_rsp = BTC_BLE_MESH_EVT_AGG_CLIENT_RECV_RSP, + .btc_ble_mesh_evt_agg_client_recv_pub = BTC_BLE_MESH_EVT_AGG_CLIENT_RECV_PUB, + .btc_ble_mesh_evt_agg_server_recv_msg = BTC_BLE_MESH_EVT_AGG_SERVER_RECV_MSG, + .btc_ble_mesh_evt_brc_client_recv_rsp = BTC_BLE_MESH_EVT_BRC_CLIENT_RECV_RSP, + .btc_ble_mesh_evt_brc_client_recv_pub = BTC_BLE_MESH_EVT_BRC_CLIENT_RECV_PUB, + .btc_ble_mesh_evt_brc_client_send_timeout = BTC_BLE_MESH_EVT_BRC_CLIENT_SEND_TIMEOUT, + .btc_ble_mesh_evt_brc_server_state_change = BTC_BLE_MESH_EVT_BRC_SERVER_STATE_CHANGE, + .btc_ble_mesh_evt_df_client_send_timeout = BTC_BLE_MESH_EVT_DF_CLIENT_SEND_TIMEOUT, + .btc_ble_mesh_evt_df_client_recv_get_rsp = BTC_BLE_MESH_EVT_DF_CLIENT_RECV_GET_RSP, + .btc_ble_mesh_evt_df_client_recv_set_rsp = BTC_BLE_MESH_EVT_DF_CLIENT_RECV_SET_RSP, + .btc_ble_mesh_evt_df_client_recv_pub = BTC_BLE_MESH_EVT_DF_CLIENT_RECV_PUB, + .btc_ble_mesh_evt_df_server_state_change = BTC_BLE_MESH_EVT_DF_SERVER_STATE_CHANGE, + .btc_ble_mesh_evt_df_server_table_change = BTC_BLE_MESH_EVT_DF_SERVER_TABLE_CHANGE, + .btc_ble_mesh_evt_lcd_client_send_timeout = BTC_BLE_MESH_EVT_LCD_CLIENT_SEND_TIMEOUT, + .btc_ble_mesh_evt_lcd_client_recv_rsp = BTC_BLE_MESH_EVT_LCD_CLIENT_RECV_RSP, + .btc_ble_mesh_evt_lcd_client_recv_pub = BTC_BLE_MESH_EVT_LCD_CLIENT_RECV_PUB, + .btc_ble_mesh_evt_mbt_client_retrieve_capabilities_comp = BTC_BLE_MESH_EVT_MBT_CLIENT_RETRIEVE_CAPABILITIES_COMP, + .btc_ble_mesh_evt_mbt_client_transfer_blob_comp = BTC_BLE_MESH_EVT_MBT_CLIENT_TRANSFER_BLOB_COMP, + .btc_ble_mesh_evt_mbt_client_send_block_comp = BTC_BLE_MESH_EVT_MBT_CLIENT_SEND_BLOCK_COMP, + .btc_ble_mesh_evt_mbt_client_send_data_comp = BTC_BLE_MESH_EVT_MBT_CLIENT_SEND_DATA_COMP, + .btc_ble_mesh_evt_mbt_client_determine_block_status_comp = BTC_BLE_MESH_EVT_MBT_CLIENT_DETERMINE_BLOCK_STATUS_COMP, + .btc_ble_mesh_evt_mbt_client_determine_transfer_status_comp = BTC_BLE_MESH_EVT_MBT_CLIENT_DETERMINE_TRANSFER_STATUS_COMP, + .btc_ble_mesh_evt_mbt_client_cancel_transfer_comp = BTC_BLE_MESH_EVT_MBT_CLIENT_CANCEL_TRANSFER_COMP, + .btc_ble_mesh_evt_mbt_client_set_transfer_ttl_comp = BTC_BLE_MESH_EVT_MBT_CLIENT_SET_TRANSFER_TTL_COMP, + .btc_ble_mesh_evt_mbt_client_clear_transfer_ttl_comp = BTC_BLE_MESH_EVT_MBT_CLIENT_CLEAR_TRANSFER_TTL_COMP, + .btc_ble_mesh_evt_mbt_client_set_app_idx_comp = BTC_BLE_MESH_EVT_MBT_CLIENT_SET_APP_IDX_COMP, + .btc_ble_mesh_evt_mbt_client_clear_app_idx_comp = BTC_BLE_MESH_EVT_MBT_CLIENT_CLEAR_APP_IDX_COMP, + .btc_ble_mesh_evt_mbt_client_set_multicast_addr_comp = BTC_BLE_MESH_EVT_MBT_CLIENT_SET_MULTICAST_ADDR_COMP, + .btc_ble_mesh_evt_mbt_client_clear_multicast_addr_comp = BTC_BLE_MESH_EVT_MBT_CLIENT_CLEAR_MULTICAST_ADDR_COMP, + .btc_ble_mesh_mbt_client_result_complete = BTC_BLE_MESH_MBT_CLIENT_RESULT_COMPLETE, + .btc_ble_mesh_mbt_client_result_fail = BTC_BLE_MESH_MBT_CLIENT_RESULT_FAIL, + .btc_ble_mesh_evt_odp_client_send_timeout = BTC_BLE_MESH_EVT_ODP_CLIENT_SEND_TIMEOUT, + .btc_ble_mesh_evt_odp_client_recv_rsp = BTC_BLE_MESH_EVT_ODP_CLIENT_RECV_RSP, + .btc_ble_mesh_evt_odp_client_recv_pub = BTC_BLE_MESH_EVT_ODP_CLIENT_RECV_PUB, + .btc_ble_mesh_evt_prb_client_recv_rsp = BTC_BLE_MESH_EVT_PRB_CLIENT_RECV_RSP, + .btc_ble_mesh_evt_prb_client_recv_pub = BTC_BLE_MESH_EVT_PRB_CLIENT_RECV_PUB, + .btc_ble_mesh_evt_prb_client_send_timeout = BTC_BLE_MESH_EVT_PRB_CLIENT_SEND_TIMEOUT, + .btc_ble_mesh_evt_rpr_client_send_timeout = BTC_BLE_MESH_EVT_RPR_CLIENT_SEND_TIMEOUT, + .btc_ble_mesh_evt_rpr_client_recv_rsp = BTC_BLE_MESH_EVT_RPR_CLIENT_RECV_RSP, + .btc_ble_mesh_evt_rpr_client_recv_pub = BTC_BLE_MESH_EVT_RPR_CLIENT_RECV_PUB, + .btc_ble_mesh_evt_sar_client_send_timeout = BTC_BLE_MESH_EVT_SAR_CLIENT_SEND_TIMEOUT, + .btc_ble_mesh_evt_sar_client_recv_rsp = BTC_BLE_MESH_EVT_SAR_CLIENT_RECV_RSP, + .btc_ble_mesh_evt_sar_client_recv_pub = BTC_BLE_MESH_EVT_SAR_CLIENT_RECV_PUB, + .btc_ble_mesh_evt_srpl_client_send_timeout = BTC_BLE_MESH_EVT_SRPL_CLIENT_SEND_TIMEOUT, + .btc_ble_mesh_evt_srpl_client_recv_rsp = BTC_BLE_MESH_EVT_SRPL_CLIENT_RECV_RSP, + .btc_ble_mesh_evt_srpl_client_recv_pub = BTC_BLE_MESH_EVT_SRPL_CLIENT_RECV_PUB, + .btc_ble_mesh_evt_rpr_server_scan_start = BTC_BLE_MESH_EVT_RPR_SERVER_SCAN_START, + .btc_ble_mesh_evt_rpr_server_scan_stop = BTC_BLE_MESH_EVT_RPR_SERVER_SCAN_STOP, + .btc_ble_mesh_evt_rpr_server_ext_scan_start = BTC_BLE_MESH_EVT_RPR_SERVER_EXT_SCAN_START, + .btc_ble_mesh_evt_rpr_server_ext_scan_stop = BTC_BLE_MESH_EVT_RPR_SERVER_EXT_SCAN_STOP, + .btc_ble_mesh_evt_rpr_server_link_open = BTC_BLE_MESH_EVT_RPR_SERVER_LINK_OPEN, + .btc_ble_mesh_evt_rpr_server_link_close = BTC_BLE_MESH_EVT_RPR_SERVER_LINK_CLOSE, + .btc_ble_mesh_evt_mbt_server_initialize_blob_receive_comp = BTC_BLE_MESH_EVT_MBT_SERVER_INITIALIZE_BLOB_RECEIVE_COMP, + .btc_ble_mesh_evt_mbt_server_cancel_blob_receive_comp = BTC_BLE_MESH_EVT_MBT_SERVER_CANCEL_BLOB_RECEIVE_COMP, + .btc_ble_mesh_evt_mbt_server_set_blob_capabilities_comp = BTC_BLE_MESH_EVT_MBT_SERVER_SET_BLOB_CAPABILITIES_COMP, + .btc_ble_mesh_evt_mbt_server_blob_transfer_get = BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_TRANSFER_GET, + .btc_ble_mesh_evt_mbt_server_blob_transfer_start = BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_TRANSFER_START, + .btc_ble_mesh_evt_mbt_server_blob_transfer_cancel = BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_TRANSFER_CANCEL, + .btc_ble_mesh_evt_mbt_server_blob_block_get = BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_BLOCK_GET, + .btc_ble_mesh_evt_mbt_server_blob_block_start = BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_BLOCK_START, + .btc_ble_mesh_evt_mbt_server_blob_chunk_transfer = BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_CHUNK_TRANSFER, + .btc_ble_mesh_evt_mbt_server_blob_information_get = BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_INFORMATION_GET, + .btc_ble_mesh_evt_mbt_server_block_receive_comp = BTC_BLE_MESH_EVT_MBT_SERVER_BLOCK_RECEIVE_COMP, + .btc_ble_mesh_evt_mbt_server_blob_receive_comp = BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_RECEIVE_COMP, + .btc_ble_mesh_evt_mbt_server_blob_receive_timeout = BTC_BLE_MESH_EVT_MBT_SERVER_BLOB_RECEIVE_TIMEOUT, +}; + +typedef struct { +/* CONFIG_BLE_MESH_NODE */ + uint8_t (*_bt_mesh_ext_prov_node_next_xact_id)(void *link); + void *(*_bt_mesh_ext_prov_node_get_link)(void); + void (*_bt_mesh_ext_prov_node_close_link)(uint8_t reason); + const uint8_t *(*_bt_mesh_ext_prov_node_get_uuid)(void); + uint16_t (*_bt_mesh_ext_prov_node_get_oob_info)(void); + const char *(*_bt_mesh_ext_prov_node_get_uri)(void); +/* CONFIG_BLE_MESH_NODE */ + +/* CONFIG_BLE_MESH_PROVISIONER */ + uint8_t (*_bt_mesh_ext_prov_pvnr_next_xact_id)(void *link); + void *(*_bt_mesh_ext_prov_pvnr_get_link)(void); + uint8_t (*_bt_mesh_ext_prov_pvnr_get_link_count)(void); + void (*_bt_mesh_ext_prov_pvnr_send_invite)(void *link); + void (*_bt_mesh_ext_prov_pvnr_close_link)(void *link, uint8_t reason); + void *(*_bt_mesh_ext_pvnr_get_node_with_addr)(uint16_t unicast_addr); +/* CONFIG_BLE_MESH_CERT_BASED_PROV */ + void (*_bt_mesh_ext_pvnr_records_list_get_cb)(uint16_t link_idx, struct net_buf_simple *data); + void (*_bt_mesh_ext_pvnr_records_recv_comp_cb)(uint8_t status, uint16_t link_idx, + uint16_t record_id, uint16_t frag_offset, + uint16_t total_len, uint8_t *record); + uint16_t (*_bt_mesh_ext_prov_link_get_record_id_expect)(void *link); + void (*_bt_mesh_ext_prov_link_set_record_id_expect)(void *link, uint16_t record_id_expect); + uint16_t (*_bt_mesh_ext_prov_link_get_offset_expect)(void *link); + void (*_bt_mesh_ext_prov_link_set_offset_expect)(void *link, uint16_t offset_expect); + uint16_t (*_bt_mesh_ext_prov_link_get_max_size)(void *link); + void (*_bt_mesh_ext_prov_link_set_max_size)(void *link, uint16_t max_size); + uint8_t *(*_bt_mesh_ext_prov_link_get_record)(void *link, uint16_t id); + uint8_t *(*_bt_mesh_ext_prov_link_alloc_record)(void *link, uint16_t id, uint16_t len); +/* CONFIG_BLE_MESH_CERT_BASED_PROV */ + uint8_t *(*_bt_mesh_ext_pvnr_get_node_uuid)(void *node); + uint8_t *(*_bt_mesh_ext_prov_link_get_uuid)(void *link); + uint8_t (*_bt_mesh_ext_prov_link_get_elem_num)(void *link); + uint16_t (*_bt_mesh_ext_prov_link_get_unicast_addr)(void *link); +/* CONFIG_BLE_MESH_PROVISIONER */ + +/* CONFIG_BLE_MESH_PB_GATT */ + void *(*_bt_mesh_ext_prov_link_get_conn)(void *link); + bool (*_bt_mesh_ext_prov_link_pb_gatt_exist)(void *link); + int (*_bt_mesh_ext_prov_link_pb_gatt_send)(void *link, struct net_buf_simple *msg); +/* CONFIG_BLE_MESH_PB_GATT */ + +/* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ + const char *(*_bt_mesh_ext_get_device_name)(void); +/* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ + +/* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ + uint8_t (*_bt_mesh_ext_proxy_server_get_all_client_type)(void); + int (*_bt_mesh_ext_proxy_server_segment_send)(void *conn, uint8_t type, + struct net_buf_simple *msg); + bool (*_bt_mesh_ext_proxy_server_find_client_by_addr)(uint16_t addr); + void (*_bt_mesh_ext_proxy_server_update_net_id_rand_stop)(void); + void *(*_bt_mesh_ext_proxy_server_get_client)(uint8_t index); + uint8_t (*_bt_mesh_ext_proxy_server_get_client_count)(void); +/* CONFIG_BLE_MESH_PRB_SRV */ + void (*_bt_mesh_ext_proxy_server_update_net_id_rand)(void); + bool (*_bt_mesh_ext_proxy_server_is_node_id_enable)(void); + void (*_bt_mesh_ext_proxy_server_private_identity_start)(void *sub); + void (*_bt_mesh_ext_proxy_server_private_identity_stop)(void *sub); +/* CONFIG_BLE_MESH_PRB_SRV */ + uint8_t (*_bt_mesh_ext_proxy_server_get_filter_type)(void *client); + uint8_t (*_bt_mesh_ext_proxy_server_get_filter_count)(void *client); + uint16_t (*_bt_mesh_ext_proxy_server_get_filter_size)(void *client); + uint16_t (*_bt_mesh_ext_proxy_server_get_filter_addr)(void *client, uint8_t index); + bool (*_bt_mesh_ext_proxy_server_filter_is_client)(void *client, uint8_t index); + uint8_t (*_bt_mesh_ext_proxy_server_get_client_type)(void *client); + bool (*_bt_mesh_ext_proxy_server_is_proxy_msg_recv)(void *client); + void *(*_bt_mesh_ext_proxy_server_get_conn)(void *client); + void (*_bt_mesh_ext_proxy_server_set_client_type)(void *client, uint8_t type); + void (*_bt_mesh_ext_proxy_server_set_msg_recv)(void *client, uint8_t val); +/* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ + +/* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + bool (*_bt_mesh_ext_proxy_client_beacon_send)(void *sub, bool private); + uint8_t (*_bt_mesh_ext_proxy_client_get_opcode)(void *cfg); + uint8_t (*_bt_mesh_ext_proxy_client_get_use_directed)(void *cfg); + void *(*_bt_mesh_ext_proxy_client_get_client_uar)(void *cfg); +/* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + +/* CONFIG_BLE_MESH_FRIEND */ + uint16_t (*_bt_mesh_ext_frnd_get_lpn)(void *frnd); + uint8_t (*_bt_mesh_ext_frnd_get_num_elem)(void *frnd); + bool (*_bt_mesh_ext_frnd_is_valid)(void *frnd); + bool (*_bt_mesh_ext_frnd_is_established)(void *frnd); + uint16_t (*_bt_mesh_ext_frnd_get_net_idx)(void *frnd); + uint8_t (*_bt_mesh_ext_frnd_get_sub_list_count)(void *frnd); + uint16_t (*_bt_mesh_ext_frnd_get_sub_list_size)(void *frnd); + uint16_t (*_bt_mesh_ext_frnd_get_sub_addr)(void *frnd, uint8_t index); + uint8_t (*_bt_mesh_ext_net_get_frnd_count)(void); + void *(*_bt_mesh_ext_net_get_frnd)(uint8_t index); + bool (*_bt_mesh_ext_friend_match)(uint16_t net_idx, uint16_t addr); + bool (*_bt_mesh_ext_friend_unicast_match)(uint16_t net_idx, uint16_t addr, uint8_t *selem); + void *(*_bt_mesh_ext_friend_find)(uint16_t net_idx, uint16_t lpn_addr, bool valid, bool established); + void (*_bt_mesh_ext_friend_clear_net_idx)(uint16_t net_idx); +/* CONFIG_BLE_MESH_FRIEND */ + +/* CONFIG_BLE_MESH_LOW_POWER */ + bool (*_bt_mesh_ext_lpn_match)(uint16_t addr); +/* CONFIG_BLE_MESH_LOW_POWER */ + +/* CONFIG_BLE_MESH_USE_DUPLICATE_SCAN */ + int (*_bt_mesh_ext_update_exceptional_list)(uint8_t sub_code, uint32_t type, void *info); +/* CONFIG_BLE_MESH_USE_DUPLICATE_SCAN */ +/* (CONFIG_BLE_MESH_PROVISIONER || CONFIG_BLE_MESH_RPR_SRV) */ + int (*_bt_mesh_ext_unprov_dev_info_query)(uint8_t uuid[16], uint8_t addr[6], + uint8_t *adv_type, uint8_t query_type); +/* (CONFIG_BLE_MESH_PROVISIONER || CONFIG_BLE_MESH_RPR_SRV) */ + +/* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || + (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) */ + int (*_bt_mesh_ext_gattc_conn_create)(const void *addr, uint16_t service_uuid); + void (*_bt_mesh_ext_gattc_disconnect)(void *conn); +/* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || + (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) */ + +/* CONFIG_BLE_MESH_SETTINGS */ + void (*_bt_mesh_ext_store_dkca)(void); + void (*_bt_mesh_ext_clear_dkca)(void); + void (*_bt_mesh_ext_clear_rpl_single)(uint16_t src); + void (*_bt_mesh_ext_store_net)(void); + void (*_bt_mesh_ext_store_seq)(void); + struct net_buf_simple *(*_bt_mesh_ext_get_core_settings_item)(const char *key); + bool (*_bt_mesh_ext_is_settings_item_exist)(struct net_buf_simple *buf, const uint16_t val); + int (*_bt_mesh_ext_add_core_settings_item)(const char *key, const uint16_t val); + int (*_bt_mesh_ext_remove_core_settings_item)(const char *key, const uint16_t val); + int (*_bt_mesh_ext_save_core_settings)(const char *key, const uint8_t *val, size_t len); + int (*_bt_mesh_ext_erase_core_settings)(const char *key); +/* CONFIG_BLE_MESH_SETTINGS */ + +/* CONFIG_BLE_MESH_DF_SRV */ + uint8_t (*_bt_mesh_ext_model_get_pub_directed_pub_policy)(void *model); + void (*_bt_mesh_ext_model_set_pub_directed_pub_policy)(void *model, uint8_t directed_pub_policy); + uint8_t (*_bt_mesh_ext_sub_get_keys_direct_nid)(void *sub, uint8_t index); + uint8_t *(*_bt_mesh_ext_sub_get_keys_direct_enc)(void *sub, uint8_t index); + uint8_t *(*_bt_mesh_ext_sub_get_keys_direct_privacy)(void *sub, uint8_t index); + uint8_t (*_bt_mesh_ext_sub_get_directed_forwarding)(void *sub); + void (*_bt_mesh_ext_sub_set_directed_forwarding)(void *sub, uint8_t directed_forwarding); + uint8_t (*_bt_mesh_ext_sub_get_directed_relay)(void *sub); + void (*_bt_mesh_ext_sub_set_directed_relay)(void *sub, uint8_t directed_relay); + uint8_t (*_bt_mesh_ext_sub_get_directed_proxy)(void *sub); + void (*_bt_mesh_ext_sub_set_directed_proxy)(void *sub, uint8_t directed_proxy); + uint8_t (*_bt_mesh_ext_sub_get_directed_proxy_use_default)(void *sub); + void (*_bt_mesh_ext_sub_set_directed_proxy_use_default)(void *sub, uint8_t directed_proxy_use_default); + uint8_t (*_bt_mesh_ext_sub_get_directed_friend)(void *sub); + void (*_bt_mesh_ext_sub_set_directed_friend)(void *sub, uint8_t directed_friend); + uint8_t (*_bt_mesh_ext_sub_get_use_directed)(void *sub); + void (*_bt_mesh_ext_sub_set_use_directed)(void *sub, uint8_t use_directed); + void *(*_bt_mesh_ext_sub_get_proxy_client_uar)(void *sub); + bool (*_bt_mesh_ext_sub_get_proxy_client_uar_len_present)(void *sub); + void (*_bt_mesh_ext_sub_set_proxy_client_uar_len_present)(void *sub, bool len_present); + uint16_t (*_bt_mesh_ext_sub_get_proxy_client_uar_range_start)(void *sub); + void (*_bt_mesh_ext_sub_set_proxy_client_uar_range_start)(void *sub, uint16_t range_start); + uint8_t (*_bt_mesh_ext_sub_get_proxy_client_uar_range_length)(void *sub); + void (*_bt_mesh_ext_sub_set_proxy_client_uar_range_length)(void *sub, uint8_t range_length); + uint8_t (*_bt_mesh_ext_sub_get_path_metric_type)(void *sub); + void (*_bt_mesh_ext_sub_set_path_metric_type)(void *sub, uint8_t path_metric_type); + uint8_t (*_bt_mesh_ext_sub_get_path_lifetime_type)(void *sub); + void (*_bt_mesh_ext_sub_set_path_lifetime_type)(void *sub, uint8_t path_lifetime_type); + bool (*_bt_mesh_ext_sub_get_two_way_path)(void *sub); + void (*_bt_mesh_ext_sub_set_two_way_path)(void *sub, bool two_way_path); + uint8_t (*_bt_mesh_ext_sub_get_forward_number)(void *sub); + void (*_bt_mesh_ext_sub_set_forward_number)(void *sub, uint8_t forward_number); + uint8_t (*_bt_mesh_ext_sub_get_disc_table_max_disc_entries)(void *sub); + void (*_bt_mesh_ext_sub_set_disc_table_max_disc_entries)(void *sub, uint8_t max_disc_entries); + uint8_t (*_bt_mesh_ext_sub_get_disc_table_max_concurr_init)(void *sub); + void (*_bt_mesh_ext_sub_set_disc_table_max_concurr_init)(void *sub, uint8_t max_concurr_init); + uint8_t (*_bt_mesh_ext_sub_get_disc_table_concurr_init)(void *sub); + void (*_bt_mesh_ext_sub_set_disc_table_concurr_init)(void *sub, uint8_t concurr_init); + void (*_bt_mesh_ext_sub_inc_disc_table_concurr_init)(void *sub); + void (*_bt_mesh_ext_sub_dec_disc_table_concurr_init)(void *sub); + void *(*_bt_mesh_ext_sub_get_disc_table_mutex)(void *sub); + void *(*_bt_mesh_ext_sub_get_disc_table_entries)(void *sub); + uint8_t (*_bt_mesh_ext_sub_get_fwd_table_max_ford_entries)(void *sub); + void (*_bt_mesh_ext_sub_set_fwd_table_max_ford_entries)(void *sub, uint8_t max_ford_entries); + uint8_t (*_bt_mesh_ext_sub_get_fwd_table_max_deps_nodes)(void *sub); + void (*_bt_mesh_ext_sub_set_fwd_table_max_deps_nodes)(void *sub, uint8_t max_deps_nodes); + uint16_t (*_bt_mesh_ext_sub_get_fwd_table_update_id)(void *sub); + void (*_bt_mesh_ext_sub_set_fwd_table_update_id)(void *sub, uint16_t update_id); + void (*_bt_mesh_ext_sub_inc_fwd_table_update_id)(void *sub); + void *(*_bt_mesh_ext_sub_get_fwd_table_mutex)(void *sub); + void *(*_bt_mesh_ext_sub_get_fwd_table_entries)(void *sub); + uint8_t (*_bt_mesh_ext_sub_get_wanted_lanes)(void *sub); + void (*_bt_mesh_ext_sub_set_wanted_lanes)(void *sub, uint8_t wanted_lanes); + uint8_t (*_bt_mesh_ext_sub_get_unicast_echo_interval)(void *sub); + void (*_bt_mesh_ext_sub_set_unicast_echo_interval)(void *sub, uint8_t unicast_echo_interval); + uint8_t (*_bt_mesh_ext_sub_get_multicast_echo_interval)(void *sub); + void (*_bt_mesh_ext_sub_set_multicast_echo_interval)(void *sub, uint8_t multicast_echo_interval); +/* CONFIG_BLE_MESH_DF_SRV */ + +/* CONFIG_BLE_MESH_RPR_CLI */ + int (*_bt_mesh_ext_rpr_cli_pdu_send)(void *link, uint8_t type); + int (*_bt_mesh_ext_rpr_cli_recv_pub_key_outbound_report)(void *link); + int (*_bt_mesh_ext_rpr_cli_pdu_recv)(void *link, uint8_t type, struct net_buf_simple *buf); + void *(*_bt_mesh_ext_rpr_cli_get_rpr_link)(uint8_t index); +/* CONFIG_BLE_MESH_RPR_CLI */ + +/* CONFIG_BLE_MESH_RPR_SRV */ + void (*_bt_mesh_ext_rpr_srv_reset_prov_link)(void *link, uint8_t reason); + int (*_bt_mesh_ext_rpr_srv_nppi_pdu_recv)(uint8_t type, const uint8_t *data); + int (*_bt_mesh_ext_rpr_srv_set_waiting_prov_link)(void* link, bt_mesh_addr_t *addr); +/* CONFIG_BLE_MESH_RPR_SRV */ + +/* CONFIG_BLE_MESH_PRIVATE_BEACON */ + uint8_t *(*_bt_mesh_ext_net_get_sub_mpb_rand)(uint8_t index); + uint32_t (*_bt_mesh_ext_sub_get_mpb_sent)(void *sub); + void (*_bt_mesh_ext_sub_set_mpb_sent)(void *sub, uint32_t mpb_sent); + uint8_t (*_bt_mesh_ext_sub_get_mpb_last)(void *sub); + void (*_bt_mesh_ext_sub_set_mpb_last)(void *sub, uint8_t mpb_last); + uint8_t (*_bt_mesh_ext_sub_get_mpb_cur)(void *sub); + void (*_bt_mesh_ext_sub_set_mpb_cur)(void *sub, uint8_t mpb_cur); + void (*_bt_mesh_ext_sub_inc_mpb_cur)(void *sub); + uint8_t (*_bt_mesh_ext_sub_get_mpb_flags_last)(void *sub); + void (*_bt_mesh_ext_sub_set_mpb_flags_last)(void *sub, uint8_t mpb_flags_last); + uint8_t (*_bt_mesh_ext_sub_get_mpb_ivi_last)(void *sub); + void (*_bt_mesh_ext_sub_set_mpb_ivi_last)(void *sub, uint8_t mpb_ivi_last); + uint8_t *(*_bt_mesh_ext_sub_get_mpb_random)(void *sub); + uint8_t *(*_bt_mesh_ext_sub_get_mpb_random_last)(void *sub); + uint8_t (*_bt_mesh_ext_sub_get_private_node_id)(void *sub); + uint8_t *(*_bt_mesh_ext_sub_get_keys_private_beacon)(void *sub, uint8_t index); +/* CONFIG_BLE_MESH_PRIVATE_BEACON */ + +/* CONFIG_BLE_MESH_BRC_SRV */ + uint16_t (*_bt_mesh_ext_sub_get_sbr_net_idx)(void *sub); + void (*_bt_mesh_ext_sub_set_sbr_net_idx)(void *sub, uint16_t sbr_net_idx); + void *(*_bt_mesh_ext_brc_srv_get_bridge_table_entry)(void *srv, uint8_t index); +/* CONFIG_BLE_MESH_BRC_SRV */ + +/* CONFIG_BLE_MESH_AGG_CLI */ + void (*_bt_mesh_ext_agg_client_cb_evt_to_btc)(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const uint8_t *val, size_t len); +/* CONFIG_BLE_MESH_AGG_CLI */ + +/* CONFIG_BLE_MESH_AGG_SRV */ + void (*_bt_mesh_ext_agg_server_cb_evt_to_btc)(uint8_t event, void *model, void *ctx, + const void *val, size_t len); +/* CONFIG_BLE_MESH_AGG_SRV */ + +/* CONFIG_BLE_MESH_BRC_CLI */ + void (*_bt_mesh_ext_brc_client_cb_evt_to_btc)(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const uint8_t *val, size_t len); +/* CONFIG_BLE_MESH_BRC_CLI */ + +/* CONFIG_BLE_MESH_BRC_SRV */ + void (*_bt_mesh_ext_brc_server_cb_evt_to_btc)(uint8_t event, void *model, void *ctx, + const uint8_t *val, size_t len); +/* CONFIG_BLE_MESH_BRC_SRV */ + +/* CONFIG_BLE_MESH_DF_CLI */ + void (*_bt_mesh_ext_df_client_cb_evt_to_btc)(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const uint8_t *val, size_t len); +/* CONFIG_BLE_MESH_DF_CLI */ + +/* CONFIG_BLE_MESH_DF_SRV */ + void (*_bt_mesh_ext_df_server_cb_evt_to_btc)(uint8_t event, void *model, void *ctx, + const uint8_t *val, size_t len); +/* CONFIG_BLE_MESH_DF_SRV */ + +/* CONFIG_BLE_MESH_LCD_CLI */ + void (*_bt_mesh_ext_lcd_client_cb_evt_to_btc)(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const void *val, size_t len); +/* CONFIG_BLE_MESH_LCD_CLI */ + +/* CONFIG_BLE_MESH_ODP_CLI */ + void (*_bt_mesh_ext_odp_client_cb_evt_to_btc)(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const uint8_t *val, size_t len); +/* CONFIG_BLE_MESH_ODP_CLI */ + +/* CONFIG_BLE_MESH_PRB_CLI */ + void (*_bt_mesh_ext_prb_client_cb_evt_to_btc)(uint32_t opcode, uint8_t evt_type, + void *model, void *ctx, + const uint8_t *val, uint16_t len); +/* CONFIG_BLE_MESH_PRB_CLI */ + +/* CONFIG_BLE_MESH_RPR_CLI */ + void (*_btc_ble_mesh_ext_rpr_client_link_close_cb)(void *model, uint16_t rpr_srv_addr, + uint8_t reason); + + void (*_btc_ble_mesh_ext_rpr_client_prov_comp_cb)(void *model, uint16_t rpr_srv_addr, + uint8_t nppi, uint16_t index, + uint8_t uuid[16], uint16_t unicast_addr, + uint8_t element_num, uint16_t net_idx); + + void (*_bt_mesh_ext_rpr_client_cb_evt_to_btc)(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const void *val, size_t len); +/* CONFIG_BLE_MESH_RPR_CLI */ + +/* CONFIG_BLE_MESH_RPR_SRV */ + void (*_bt_mesh_ext_rpr_server_cb_evt_to_btc)(uint8_t event, const void *val, size_t len); +/* CONFIG_BLE_MESH_RPR_SRV */ + +/* CONFIG_BLE_MESH_SAR_CLI */ + void (*_bt_mesh_ext_sar_client_cb_evt_to_btc)(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const uint8_t *val, size_t len); +/* CONFIG_BLE_MESH_SAR_CLI */ + +/* CONFIG_BLE_MESH_SRPL_CLI */ + void (*_bt_mesh_ext_srpl_client_cb_evt_to_btc)(uint32_t opcode, uint8_t event, + void *model, void *ctx, + const uint8_t *val, size_t len); +/* CONFIG_BLE_MESH_SRPL_CLI */ + +/* CONFIG_BLE_MESH_MBT_CLI */ + void (*_bt_mesh_ext_mbt_client_cb_evt_to_btc)(uint8_t event, uint8_t result, void *model); +/* CONFIG_BLE_MESH_MBT_CLI */ + +/* CONFIG_BLE_MESH_MBT_SRV */ + void (*_bt_mesh_ext_mbt_server_cb_evt_to_btc)(uint8_t event, void *model, void *ctx); +/* CONFIG_BLE_MESH_MBT_SRV */ +} bt_mesh_ext_funcs_t; + +static const bt_mesh_ext_funcs_t bt_mesh_ext_func = { +/* CONFIG_BLE_MESH_NODE */ + ._bt_mesh_ext_prov_node_next_xact_id = bt_mesh_ext_prov_node_next_xact_id, + ._bt_mesh_ext_prov_node_get_link = bt_mesh_ext_prov_node_get_link, + ._bt_mesh_ext_prov_node_close_link = bt_mesh_ext_prov_node_close_link, + ._bt_mesh_ext_prov_node_get_uuid = bt_mesh_ext_prov_node_get_uuid, + ._bt_mesh_ext_prov_node_get_oob_info = bt_mesh_ext_prov_node_get_oob_info, + ._bt_mesh_ext_prov_node_get_uri = bt_mesh_ext_prov_node_get_uri, +/* CONFIG_BLE_MESH_NODE */ + +/* CONFIG_BLE_MESH_PROVISIONER */ + ._bt_mesh_ext_prov_pvnr_next_xact_id = bt_mesh_ext_prov_pvnr_next_xact_id, + ._bt_mesh_ext_prov_pvnr_get_link = bt_mesh_ext_prov_pvnr_get_link, + ._bt_mesh_ext_prov_pvnr_get_link_count = bt_mesh_ext_prov_pvnr_get_link_count, + ._bt_mesh_ext_prov_pvnr_send_invite = bt_mesh_ext_prov_pvnr_send_invite, + ._bt_mesh_ext_prov_pvnr_close_link = bt_mesh_ext_prov_pvnr_close_link, + ._bt_mesh_ext_pvnr_get_node_with_addr = bt_mesh_ext_pvnr_get_node_with_addr, +/* CONFIG_BLE_MESH_CERT_BASED_PROV */ + ._bt_mesh_ext_pvnr_records_list_get_cb = bt_mesh_ext_pvnr_records_list_get_cb, + ._bt_mesh_ext_pvnr_records_recv_comp_cb = bt_mesh_ext_pvnr_records_recv_comp_cb, + ._bt_mesh_ext_prov_link_get_record_id_expect = bt_mesh_ext_prov_link_get_record_id_expect, + ._bt_mesh_ext_prov_link_set_record_id_expect = bt_mesh_ext_prov_link_set_record_id_expect, + ._bt_mesh_ext_prov_link_get_offset_expect = bt_mesh_ext_prov_link_get_offset_expect, + ._bt_mesh_ext_prov_link_set_offset_expect = bt_mesh_ext_prov_link_set_offset_expect, + ._bt_mesh_ext_prov_link_get_max_size = bt_mesh_ext_prov_link_get_max_size, + ._bt_mesh_ext_prov_link_set_max_size = bt_mesh_ext_prov_link_set_max_size, + ._bt_mesh_ext_prov_link_get_record = bt_mesh_ext_prov_link_get_record, + ._bt_mesh_ext_prov_link_alloc_record = bt_mesh_ext_prov_link_alloc_record, +/* CONFIG_BLE_MESH_CERT_BASED_PROV */ + ._bt_mesh_ext_pvnr_get_node_uuid = bt_mesh_ext_pvnr_get_node_uuid, + ._bt_mesh_ext_prov_link_get_uuid = bt_mesh_ext_prov_link_get_uuid, + ._bt_mesh_ext_prov_link_get_elem_num = bt_mesh_ext_prov_link_get_elem_num, + ._bt_mesh_ext_prov_link_get_unicast_addr = bt_mesh_ext_prov_link_get_unicast_addr, +/* CONFIG_BLE_MESH_PROVISIONER */ + +/* CONFIG_BLE_MESH_PB_GATT */ + ._bt_mesh_ext_prov_link_get_conn = bt_mesh_ext_prov_link_get_conn, + ._bt_mesh_ext_prov_link_pb_gatt_exist = bt_mesh_ext_prov_link_pb_gatt_exist, + ._bt_mesh_ext_prov_link_pb_gatt_send = bt_mesh_ext_prov_link_pb_gatt_send, +/* CONFIG_BLE_MESH_PB_GATT */ + +/* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER */ + ._bt_mesh_ext_get_device_name = bt_mesh_ext_get_device_name, +/* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER */ + +/* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ + ._bt_mesh_ext_proxy_server_get_all_client_type = bt_mesh_ext_proxy_server_get_all_client_type, + ._bt_mesh_ext_proxy_server_segment_send = bt_mesh_ext_proxy_server_segment_send, + ._bt_mesh_ext_proxy_server_find_client_by_addr = bt_mesh_ext_proxy_server_find_client_by_addr, +/* CONFIG_BLE_MESH_PRB_SRV */ + ._bt_mesh_ext_proxy_server_update_net_id_rand_stop = bt_mesh_ext_proxy_server_update_net_id_rand_stop, +/* CONFIG_BLE_MESH_PRB_SRV */ + ._bt_mesh_ext_proxy_server_get_client = bt_mesh_ext_proxy_server_get_client, + ._bt_mesh_ext_proxy_server_get_client_count = bt_mesh_ext_proxy_server_get_client_count, +/* CONFIG_BLE_MESH_PRB_SRV */ + ._bt_mesh_ext_proxy_server_update_net_id_rand = bt_mesh_ext_proxy_server_update_net_id_rand, + ._bt_mesh_ext_proxy_server_is_node_id_enable = bt_mesh_ext_proxy_server_is_node_id_enable, + ._bt_mesh_ext_proxy_server_private_identity_start = bt_mesh_ext_proxy_server_private_identity_start, + ._bt_mesh_ext_proxy_server_private_identity_stop = bt_mesh_ext_proxy_server_private_identity_stop, +/* CONFIG_BLE_MESH_PRB_SRV */ + ._bt_mesh_ext_proxy_server_get_filter_type = bt_mesh_ext_proxy_server_get_filter_type, + ._bt_mesh_ext_proxy_server_get_filter_count = bt_mesh_ext_proxy_server_get_filter_count, + ._bt_mesh_ext_proxy_server_get_filter_size = bt_mesh_ext_proxy_server_get_filter_size, + ._bt_mesh_ext_proxy_server_get_filter_addr = bt_mesh_ext_proxy_server_get_filter_addr, + ._bt_mesh_ext_proxy_server_filter_is_client = bt_mesh_ext_proxy_server_filter_is_client, + ._bt_mesh_ext_proxy_server_get_client_type = bt_mesh_ext_proxy_server_get_client_type, + ._bt_mesh_ext_proxy_server_is_proxy_msg_recv = bt_mesh_ext_proxy_server_is_proxy_msg_recv, + ._bt_mesh_ext_proxy_server_get_conn = bt_mesh_ext_proxy_server_get_conn, + ._bt_mesh_ext_proxy_server_set_client_type = bt_mesh_ext_proxy_server_set_client_type, + ._bt_mesh_ext_proxy_server_set_msg_recv = bt_mesh_ext_proxy_server_set_msg_recv, +/* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ + +/* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + ._bt_mesh_ext_proxy_client_beacon_send = bt_mesh_ext_proxy_client_beacon_send, + ._bt_mesh_ext_proxy_client_get_opcode = bt_mesh_ext_proxy_client_get_opcode, + ._bt_mesh_ext_proxy_client_get_use_directed = bt_mesh_ext_proxy_client_get_use_directed, + ._bt_mesh_ext_proxy_client_get_client_uar = bt_mesh_ext_proxy_client_get_client_uar, +/* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + +/* CONFIG_BLE_MESH_FRIEND */ + ._bt_mesh_ext_frnd_get_lpn = bt_mesh_ext_frnd_get_lpn, + ._bt_mesh_ext_frnd_get_num_elem = bt_mesh_ext_frnd_get_num_elem, + ._bt_mesh_ext_frnd_is_valid = bt_mesh_ext_frnd_is_valid, + ._bt_mesh_ext_frnd_is_established = bt_mesh_ext_frnd_is_established, + ._bt_mesh_ext_frnd_get_net_idx = bt_mesh_ext_frnd_get_net_idx, + ._bt_mesh_ext_frnd_get_sub_list_count = bt_mesh_ext_frnd_get_sub_list_count, + ._bt_mesh_ext_frnd_get_sub_list_size = bt_mesh_ext_frnd_get_sub_list_size, + ._bt_mesh_ext_frnd_get_sub_addr = bt_mesh_ext_frnd_get_sub_addr, + ._bt_mesh_ext_net_get_frnd_count = bt_mesh_ext_net_get_frnd_count, + ._bt_mesh_ext_net_get_frnd = bt_mesh_ext_net_get_frnd, + ._bt_mesh_ext_friend_match = bt_mesh_ext_friend_match, + ._bt_mesh_ext_friend_unicast_match = bt_mesh_ext_friend_unicast_match, + ._bt_mesh_ext_friend_find = bt_mesh_ext_friend_find, + ._bt_mesh_ext_friend_clear_net_idx = bt_mesh_ext_friend_clear_net_idx, +/* CONFIG_BLE_MESH_FRIEND */ + +/* CONFIG_BLE_MESH_LOW_POWER */ + ._bt_mesh_ext_lpn_match = bt_mesh_ext_lpn_match, +/* CONFIG_BLE_MESH_LOW_POWER */ + +/* CONFIG_BLE_MESH_USE_DUPLICATE_SCAN */ + ._bt_mesh_ext_update_exceptional_list = bt_mesh_ext_update_exceptional_list, +/* CONFIG_BLE_MESH_USE_DUPLICATE_SCAN */ +/* (CONFIG_BLE_MESH_PROVISIONER || CONFIG_BLE_MESH_RPR_SRV) */ + ._bt_mesh_ext_unprov_dev_info_query = bt_mesh_ext_unprov_dev_info_query, +/* (CONFIG_BLE_MESH_PROVISIONER || CONFIG_BLE_MESH_RPR_SRV) */ + +/* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) */ + ._bt_mesh_ext_gattc_conn_create = bt_mesh_ext_gattc_conn_create, + ._bt_mesh_ext_gattc_disconnect = bt_mesh_ext_gattc_disconnect, +/* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT || \ + (CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT) */ + +/* CONFIG_BLE_MESH_SETTINGS */ + ._bt_mesh_ext_store_dkca = bt_mesh_ext_store_dkca, + ._bt_mesh_ext_clear_dkca = bt_mesh_ext_clear_dkca, + ._bt_mesh_ext_clear_rpl_single = bt_mesh_ext_clear_rpl_single, + ._bt_mesh_ext_store_net = bt_mesh_ext_store_net, + ._bt_mesh_ext_store_seq = bt_mesh_ext_store_seq, + ._bt_mesh_ext_get_core_settings_item = bt_mesh_ext_get_core_settings_item, + ._bt_mesh_ext_is_settings_item_exist = bt_mesh_ext_is_settings_item_exist, + ._bt_mesh_ext_add_core_settings_item = bt_mesh_ext_add_core_settings_item, + ._bt_mesh_ext_remove_core_settings_item = bt_mesh_ext_remove_core_settings_item, + ._bt_mesh_ext_save_core_settings = bt_mesh_ext_save_core_settings, + ._bt_mesh_ext_erase_core_settings = bt_mesh_ext_erase_core_settings, +/* CONFIG_BLE_MESH_SETTINGS */ + +/* CONFIG_BLE_MESH_DF_SRV */ + ._bt_mesh_ext_model_get_pub_directed_pub_policy = bt_mesh_ext_model_get_pub_directed_pub_policy, + ._bt_mesh_ext_model_set_pub_directed_pub_policy = bt_mesh_ext_model_set_pub_directed_pub_policy, + ._bt_mesh_ext_sub_get_keys_direct_nid = bt_mesh_ext_sub_get_keys_direct_nid, + ._bt_mesh_ext_sub_get_keys_direct_enc = bt_mesh_ext_sub_get_keys_direct_enc, + ._bt_mesh_ext_sub_get_keys_direct_privacy = bt_mesh_ext_sub_get_keys_direct_privacy, + ._bt_mesh_ext_sub_get_directed_forwarding = bt_mesh_ext_sub_get_directed_forwarding, + ._bt_mesh_ext_sub_set_directed_forwarding = bt_mesh_ext_sub_set_directed_forwarding, + ._bt_mesh_ext_sub_get_directed_relay = bt_mesh_ext_sub_get_directed_relay, + ._bt_mesh_ext_sub_set_directed_relay = bt_mesh_ext_sub_set_directed_relay, + ._bt_mesh_ext_sub_get_directed_proxy = bt_mesh_ext_sub_get_directed_proxy, + ._bt_mesh_ext_sub_set_directed_proxy = bt_mesh_ext_sub_set_directed_proxy, + ._bt_mesh_ext_sub_get_directed_proxy_use_default = bt_mesh_ext_sub_get_directed_proxy_use_default, + ._bt_mesh_ext_sub_set_directed_proxy_use_default = bt_mesh_ext_sub_set_directed_proxy_use_default, + ._bt_mesh_ext_sub_get_directed_friend = bt_mesh_ext_sub_get_directed_friend, + ._bt_mesh_ext_sub_set_directed_friend = bt_mesh_ext_sub_set_directed_friend, + ._bt_mesh_ext_sub_get_use_directed = bt_mesh_ext_sub_get_use_directed, + ._bt_mesh_ext_sub_set_use_directed = bt_mesh_ext_sub_set_use_directed, + ._bt_mesh_ext_sub_get_proxy_client_uar = bt_mesh_ext_sub_get_proxy_client_uar, + ._bt_mesh_ext_sub_get_proxy_client_uar_len_present = bt_mesh_ext_sub_get_proxy_client_uar_len_present, + ._bt_mesh_ext_sub_set_proxy_client_uar_len_present = bt_mesh_ext_sub_set_proxy_client_uar_len_present, + ._bt_mesh_ext_sub_get_proxy_client_uar_range_start = bt_mesh_ext_sub_get_proxy_client_uar_range_start, + ._bt_mesh_ext_sub_set_proxy_client_uar_range_start = bt_mesh_ext_sub_set_proxy_client_uar_range_start, + ._bt_mesh_ext_sub_get_proxy_client_uar_range_length = bt_mesh_ext_sub_get_proxy_client_uar_range_length, + ._bt_mesh_ext_sub_set_proxy_client_uar_range_length = bt_mesh_ext_sub_set_proxy_client_uar_range_length, + ._bt_mesh_ext_sub_get_path_metric_type = bt_mesh_ext_sub_get_path_metric_type, + ._bt_mesh_ext_sub_set_path_metric_type = bt_mesh_ext_sub_set_path_metric_type, + ._bt_mesh_ext_sub_get_path_lifetime_type = bt_mesh_ext_sub_get_path_lifetime_type, + ._bt_mesh_ext_sub_set_path_lifetime_type = bt_mesh_ext_sub_set_path_lifetime_type, + ._bt_mesh_ext_sub_get_two_way_path = bt_mesh_ext_sub_get_two_way_path, + ._bt_mesh_ext_sub_set_two_way_path = bt_mesh_ext_sub_set_two_way_path, + ._bt_mesh_ext_sub_get_forward_number = bt_mesh_ext_sub_get_forward_number, + ._bt_mesh_ext_sub_set_forward_number = bt_mesh_ext_sub_set_forward_number, + ._bt_mesh_ext_sub_get_disc_table_max_disc_entries = bt_mesh_ext_sub_get_disc_table_max_disc_entries, + ._bt_mesh_ext_sub_set_disc_table_max_disc_entries = bt_mesh_ext_sub_set_disc_table_max_disc_entries, + ._bt_mesh_ext_sub_get_disc_table_max_concurr_init = bt_mesh_ext_sub_get_disc_table_max_concurr_init, + ._bt_mesh_ext_sub_set_disc_table_max_concurr_init = bt_mesh_ext_sub_set_disc_table_max_concurr_init, + ._bt_mesh_ext_sub_get_disc_table_concurr_init = bt_mesh_ext_sub_get_disc_table_concurr_init, + ._bt_mesh_ext_sub_set_disc_table_concurr_init = bt_mesh_ext_sub_set_disc_table_concurr_init, + ._bt_mesh_ext_sub_inc_disc_table_concurr_init = bt_mesh_ext_sub_inc_disc_table_concurr_init, + ._bt_mesh_ext_sub_dec_disc_table_concurr_init = bt_mesh_ext_sub_dec_disc_table_concurr_init, + ._bt_mesh_ext_sub_get_disc_table_mutex = bt_mesh_ext_sub_get_disc_table_mutex, + ._bt_mesh_ext_sub_get_disc_table_entries = bt_mesh_ext_sub_get_disc_table_entries, + ._bt_mesh_ext_sub_get_fwd_table_max_ford_entries = bt_mesh_ext_sub_get_fwd_table_max_ford_entries, + ._bt_mesh_ext_sub_set_fwd_table_max_ford_entries = bt_mesh_ext_sub_set_fwd_table_max_ford_entries, + ._bt_mesh_ext_sub_get_fwd_table_max_deps_nodes = bt_mesh_ext_sub_get_fwd_table_max_deps_nodes, + ._bt_mesh_ext_sub_set_fwd_table_max_deps_nodes = bt_mesh_ext_sub_set_fwd_table_max_deps_nodes, + ._bt_mesh_ext_sub_get_fwd_table_update_id = bt_mesh_ext_sub_get_fwd_table_update_id, + ._bt_mesh_ext_sub_set_fwd_table_update_id = bt_mesh_ext_sub_set_fwd_table_update_id, + ._bt_mesh_ext_sub_inc_fwd_table_update_id = bt_mesh_ext_sub_inc_fwd_table_update_id, + ._bt_mesh_ext_sub_get_fwd_table_mutex = bt_mesh_ext_sub_get_fwd_table_mutex, + ._bt_mesh_ext_sub_get_fwd_table_entries = bt_mesh_ext_sub_get_fwd_table_entries, + ._bt_mesh_ext_sub_get_wanted_lanes = bt_mesh_ext_sub_get_wanted_lanes, + ._bt_mesh_ext_sub_set_wanted_lanes = bt_mesh_ext_sub_set_wanted_lanes, + ._bt_mesh_ext_sub_get_unicast_echo_interval = bt_mesh_ext_sub_get_unicast_echo_interval, + ._bt_mesh_ext_sub_set_unicast_echo_interval = bt_mesh_ext_sub_set_unicast_echo_interval, + ._bt_mesh_ext_sub_get_multicast_echo_interval = bt_mesh_ext_sub_get_multicast_echo_interval, + ._bt_mesh_ext_sub_set_multicast_echo_interval = bt_mesh_ext_sub_set_multicast_echo_interval, +/* CONFIG_BLE_MESH_DF_SRV */ + +/* CONFIG_BLE_MESH_RPR_CLI */ + ._bt_mesh_ext_rpr_cli_pdu_send = bt_mesh_ext_rpr_cli_pdu_send, + ._bt_mesh_ext_rpr_cli_recv_pub_key_outbound_report = bt_mesh_ext_rpr_cli_recv_pub_key_outbound_report, + ._bt_mesh_ext_rpr_cli_pdu_recv = bt_mesh_ext_rpr_cli_pdu_recv, + ._bt_mesh_ext_rpr_cli_get_rpr_link = bt_mesh_ext_rpr_cli_get_rpr_link, +/* CONFIG_BLE_MESH_RPR_CLI */ + +/* CONFIG_BLE_MESH_RPR_SRV */ + ._bt_mesh_ext_rpr_srv_reset_prov_link = bt_mesh_ext_rpr_srv_reset_prov_link, + ._bt_mesh_ext_rpr_srv_nppi_pdu_recv = bt_mesh_ext_rpr_srv_nppi_pdu_recv, +/* (CONFIG_BLE_MESH_GATT_PROXY_CLIENT && CONFIG_BLE_MESH_PB_GATT) */ + ._bt_mesh_ext_rpr_srv_set_waiting_prov_link = bt_mesh_ext_rpr_srv_set_waiting_prov_link, +/* (CONFIG_BLE_MESH_GATT_PROXY_CLIENT && CONFIG_BLE_MESH_PB_GATT) */ +/* CONFIG_BLE_MESH_RPR_SRV */ + +/* CONFIG_BLE_MESH_PRIVATE_BEACON */ + ._bt_mesh_ext_net_get_sub_mpb_rand = bt_mesh_ext_net_get_sub_mpb_rand, + ._bt_mesh_ext_sub_get_mpb_sent = bt_mesh_ext_sub_get_mpb_sent, + ._bt_mesh_ext_sub_set_mpb_sent = bt_mesh_ext_sub_set_mpb_sent, + ._bt_mesh_ext_sub_get_mpb_last = bt_mesh_ext_sub_get_mpb_last, + ._bt_mesh_ext_sub_set_mpb_last = bt_mesh_ext_sub_set_mpb_last, + ._bt_mesh_ext_sub_get_mpb_cur = bt_mesh_ext_sub_get_mpb_cur, + ._bt_mesh_ext_sub_set_mpb_cur = bt_mesh_ext_sub_set_mpb_cur, + ._bt_mesh_ext_sub_inc_mpb_cur = bt_mesh_ext_sub_inc_mpb_cur, + ._bt_mesh_ext_sub_get_mpb_flags_last = bt_mesh_ext_sub_get_mpb_flags_last, + ._bt_mesh_ext_sub_set_mpb_flags_last = bt_mesh_ext_sub_set_mpb_flags_last, + ._bt_mesh_ext_sub_get_mpb_ivi_last = bt_mesh_ext_sub_get_mpb_ivi_last, + ._bt_mesh_ext_sub_set_mpb_ivi_last = bt_mesh_ext_sub_set_mpb_ivi_last, + ._bt_mesh_ext_sub_get_mpb_random = bt_mesh_ext_sub_get_mpb_random, + ._bt_mesh_ext_sub_get_mpb_random_last = bt_mesh_ext_sub_get_mpb_random_last, + ._bt_mesh_ext_sub_get_private_node_id = bt_mesh_ext_sub_get_private_node_id, + ._bt_mesh_ext_sub_get_keys_private_beacon = bt_mesh_ext_sub_get_keys_private_beacon, +/* CONFIG_BLE_MESH_PRIVATE_BEACON */ + +/* CONFIG_BLE_MESH_BRC_SRV */ + ._bt_mesh_ext_sub_get_sbr_net_idx = bt_mesh_ext_sub_get_sbr_net_idx, + ._bt_mesh_ext_sub_set_sbr_net_idx = bt_mesh_ext_sub_set_sbr_net_idx, + ._bt_mesh_ext_brc_srv_get_bridge_table_entry = bt_mesh_ext_brc_srv_get_bridge_table_entry, +/* CONFIG_BLE_MESH_BRC_SRV */ + +/* CONFIG_BLE_MESH_AGG_CLI */ + ._bt_mesh_ext_agg_client_cb_evt_to_btc = bt_mesh_ext_agg_client_cb_evt_to_btc, +/* CONFIG_BLE_MESH_AGG_CLI */ +/* CONFIG_BLE_MESH_AGG_SRV */ + ._bt_mesh_ext_agg_server_cb_evt_to_btc = bt_mesh_ext_agg_server_cb_evt_to_btc, +/* CONFIG_BLE_MESH_AGG_SRV */ +/* CONFIG_BLE_MESH_BRC_CLI */ + ._bt_mesh_ext_brc_client_cb_evt_to_btc = bt_mesh_ext_brc_client_cb_evt_to_btc, +/* CONFIG_BLE_MESH_BRC_CLI */ +/* CONFIG_BLE_MESH_BRC_SRV */ + ._bt_mesh_ext_brc_server_cb_evt_to_btc = bt_mesh_ext_brc_server_cb_evt_to_btc, +/* CONFIG_BLE_MESH_BRC_SRV */ +/* CONFIG_BLE_MESH_DF_CLI */ + ._bt_mesh_ext_df_client_cb_evt_to_btc = bt_mesh_ext_df_client_cb_evt_to_btc, +/* CONFIG_BLE_MESH_DF_CLI */ +/* CONFIG_BLE_MESH_DF_SRV */ + ._bt_mesh_ext_df_server_cb_evt_to_btc = bt_mesh_ext_df_server_cb_evt_to_btc, +/* CONFIG_BLE_MESH_DF_SRV */ +/* CONFIG_BLE_MESH_LCD_CLI */ + ._bt_mesh_ext_lcd_client_cb_evt_to_btc = bt_mesh_ext_lcd_client_cb_evt_to_btc, +/* CONFIG_BLE_MESH_LCD_CLI */ +/* CONFIG_BLE_MESH_ODP_CLI */ + ._bt_mesh_ext_odp_client_cb_evt_to_btc = bt_mesh_ext_odp_client_cb_evt_to_btc, +/* CONFIG_BLE_MESH_ODP_CLI */ +/* CONFIG_BLE_MESH_PRB_CLI */ + ._bt_mesh_ext_prb_client_cb_evt_to_btc = bt_mesh_ext_prb_client_cb_evt_to_btc, +/* CONFIG_BLE_MESH_PRB_CLI */ +/* CONFIG_BLE_MESH_RPR_CLI */ + ._btc_ble_mesh_ext_rpr_client_link_close_cb = btc_ble_mesh_ext_rpr_client_link_close_cb, + ._btc_ble_mesh_ext_rpr_client_prov_comp_cb = btc_ble_mesh_ext_rpr_client_prov_comp_cb, + ._bt_mesh_ext_rpr_client_cb_evt_to_btc = bt_mesh_ext_rpr_client_cb_evt_to_btc, +/* CONFIG_BLE_MESH_RPR_CLI */ +/* CONFIG_BLE_MESH_RPR_SRV */ + ._bt_mesh_ext_rpr_server_cb_evt_to_btc = bt_mesh_ext_rpr_server_cb_evt_to_btc, +/* CONFIG_BLE_MESH_RPR_SRV */ +/* CONFIG_BLE_MESH_SAR_CLI */ + ._bt_mesh_ext_sar_client_cb_evt_to_btc = bt_mesh_ext_sar_client_cb_evt_to_btc, +/* CONFIG_BLE_MESH_SAR_CLI */ +/* CONFIG_BLE_MESH_SRPL_CLI */ + ._bt_mesh_ext_srpl_client_cb_evt_to_btc = bt_mesh_ext_srpl_client_cb_evt_to_btc, +/* CONFIG_BLE_MESH_SRPL_CLI */ +/* CONFIG_BLE_MESH_MBT_CLI */ + ._bt_mesh_ext_mbt_client_cb_evt_to_btc = bt_mesh_ext_mbt_client_cb_evt_to_btc, +/* CONFIG_BLE_MESH_MBT_CLI */ +/* CONFIG_BLE_MESH_MBT_SRV */ + ._bt_mesh_ext_mbt_server_cb_evt_to_btc = bt_mesh_ext_mbt_server_cb_evt_to_btc, +/* CONFIG_BLE_MESH_MBT_SRV */ +}; + +int bt_mesh_v11_ext_init(void) +{ + return bt_mesh_v11_init(&bt_mesh_ext_cfg, sizeof(bt_mesh_ext_cfg), + &bt_mesh_ext_func, sizeof(bt_mesh_ext_func)); +} diff --git a/components/bt/esp_ble_mesh/v1.1/include/mesh_v1.1/utils.h b/components/bt/esp_ble_mesh/v1.1/include/mesh_v1.1/utils.h new file mode 100644 index 000000000000..ab87a87e6732 --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/include/mesh_v1.1/utils.h @@ -0,0 +1,248 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BLE_MESH_v11_UTILS_H_ +#define _BLE_MESH_v11_UTILS_H_ + +#include +#include + +#include "mesh/buf.h" + +#include "net.h" +#include "prov_common.h" +#include "proxy_server.h" +#include "mesh/adapter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int bt_mesh_v11_init(const void *cfg, size_t cfg_size, + const void *func, size_t func_size); + +int bt_mesh_v11_deinit(void); + +#define BLE_MESH_TAG_SEND_SEGMENTED BIT(0) +#define BLE_MESH_TAG_IMMUTABLE_CRED BIT(1) +#define BLE_MESH_TAG_USE_DIRECTED BIT(2) +#define BLE_MESH_TAG_RELAY BIT(3) +#define BLE_MESH_TAG_FRIENDSHIP BIT(4) + +bool bt_mesh_tag_send_segmented(uint8_t tag); + +bool bt_mesh_tag_immutable_cred(uint8_t tag); + +bool bt_mesh_tag_use_directed(uint8_t tag); + +bool bt_mesh_tag_relay(uint8_t tag); + +bool bt_mesh_tag_friendship(uint8_t tag); + +bool bt_mesh_uar_valid(void *uar); + +bool bt_mesh_addr_in_uar(void *uar, uint16_t addr); + +void bt_mesh_add_uar_be(struct net_buf_simple *buf, void *uar); + +struct bt_mesh_prov_link; + +void bt_mesh_prov_record_req(const uint8_t *data); + +void bt_mesh_prov_record_rsp(const uint8_t *data); + +void bt_mesh_prov_records_get(const uint8_t *data); + +void bt_mesh_prov_records_list(const uint8_t *data); + +int bt_mesh_node_cert_based_prov_init(void); + +void bt_mesh_node_cert_based_prov_deinit(void); + +void bt_mesh_pvnr_record_req(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf); + +void bt_mesh_pvnr_record_rsp(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf); + +void bt_mesh_pvnr_records_get(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf); + +void bt_mesh_pvnr_records_list(struct bt_mesh_prov_link *link, + struct net_buf_simple *buf); + +int bt_mesh_prov_salt_epa(const uint8_t conf_salt[32], + const uint8_t prov_rand[32], + const uint8_t dev_rand[32], + uint8_t prov_salt[16]); + +int bt_mesh_prov_conf_salt_epa(const uint8_t conf_inputs[145], uint8_t salt[32]); + +int bt_mesh_prov_conf_key_epa(const uint8_t dhkey[32], const uint8_t auth[32], + const uint8_t conf_salt[32], uint8_t conf_key[32]); + +int bt_mesh_prov_conf_epa(const uint8_t conf_key[32], const uint8_t rand[32], + uint8_t conf[32]); + +uint8_t bt_mesh_comp_page_check(uint8_t page, bool largest); + +int bt_mesh_get_comp_data(struct net_buf_simple *buf, uint8_t page, + uint16_t offset, bool full_element); + +int bt_mesh_private_beacon_key(const uint8_t net_key[16], uint8_t private_beacon_key[16]); + +void bt_mesh_directed_proxy_server_set_blacklist(void *client); + +int bt_mesh_directed_proxy_server_directed_proxy_caps_status_send(void *conn, void *sub); + +void bt_mesh_directed_proxy_server_connected(void *client); + +void bt_mesh_directed_proxy_server_directed_proxy_caps_send(void *sub, bool clear); + +void bt_mesh_directed_proxy_server_directed_proxy_ctrl_recv(void *client, void *rx, + struct net_buf_simple *buf); + +int bt_mesh_directed_proxy_server_solicitation(void *client, void *sub); + +int bt_mesh_directed_proxy_server_update_dep_node(void *sub, void *client, uint8_t type); + +int bt_mesh_directed_forwarding_node_solicitation(void *model, void *sub); + +int bt_mesh_directed_friend_solicitation(void *frnd, void *sub); + +int bt_mesh_directed_update_dependent_node(void *sub, uint8_t type, + uint16_t addr, uint8_t num_elem); + +int bt_mesh_directed_forwarding_ctl_recv(uint8_t ctl_op, void *rx, + struct net_buf_simple *buf); + +int bt_mesh_directed_forwarding_sub_init(void *sub); + +int bt_mesh_recovery_directed_forwarding_table(void *sub); + +int bt_mesh_clear_directed_forwarding_table_data(uint16_t net_idx); + +int bt_mesh_clear_all_directed_forwarding_table_data(void); + +void bt_mesh_store_directed_forwarding_table_data(uint16_t net_idx); + +int bt_mesh_power_up_create_path_origin_fsm(void *model); + +int bt_mesh_private_beacon_timer_init(void); + +int bt_mesh_private_beacon_timer_free(void); + +int bt_mesh_private_beacon_timer_submit(int32_t delay); + +int bt_mesh_private_beacon_timer_cancel(void); + +void bt_mesh_private_beacon_timer_reset(void); + +uint8_t bt_mesh_private_beacon_update_addr_type(const void *ad); + +void bt_mesh_private_beacon_update_random(void *sub); + +void bt_mesh_private_beacon_create(void *sub, struct net_buf_simple *buf); + +void bt_mesh_private_beacon_recv(struct net_buf_simple *buf); + +void bt_mesh_private_beacon_enable(void); + +void bt_mesh_private_beacon_disable(void); + +void bt_mesh_proxy_server_solic_recv(struct net_buf_simple *data, + const bt_mesh_addr_t *addr, + int8_t rssi); + +uint16_t bt_mesh_proxy_server_get_solic_adv_net_idx(void); + +int32_t bt_mesh_proxy_server_get_solic_adv_remaining(void); + +void bt_mesh_proxy_server_stop_solic_adv_priv_net_id(void); + +int bt_mesh_proxy_client_solic_send(uint16_t net_idx, uint16_t ssrc, uint16_t dst); + +void bt_mesh_create_proxy_solic_nonce(uint8_t nonce[13], const uint8_t *pdu, + uint32_t iv_index); + +int bt_mesh_proxy_solic_init(void); + +int bt_mesh_proxy_solic_deinit(void); + +void bt_mesh_sar_init(void); + +uint8_t bt_mesh_subnet_bridge_state_get(void); + +void bt_mesh_delete_netkey_in_bridge_table(uint16_t net_idx); + +bool bt_mesh_bridge_change_net_key(void *rx, const uint8_t **enc, + const uint8_t **priv, + uint8_t *nid, uint8_t cred); + +void bt_mesh_disable_directed_proxy_state(uint16_t net_idx); + +void bt_mesh_disable_directed_friend_state(uint16_t net_idx); + +uint8_t bt_mesh_direct_net_transmit_get(void); + +uint8_t bt_mesh_private_beacon_state_get(void); + +uint8_t bt_mesh_private_gatt_proxy_state_get(void); + +void bt_mesh_rpr_cli_prov_complete(void *link, uint16_t index, uint16_t net_idx); + +void bt_mesh_rpr_cli_node_addr_update(void *link); + +int bt_mesh_rpr_srv_unprov_beacon_recv(struct net_buf_simple *buf, + uint8_t adv_type, + const bt_mesh_addr_t *addr, + int8_t rssi); + +int bt_mesh_rpr_srv_extended_scan(struct net_buf_simple *buf, + const bt_mesh_addr_t *addr, + bool *rp_adv); + +int bt_mesh_rpr_srv_netkey_del(uint16_t del_idx); + +int bt_mesh_rpr_srv_recv_link_ack(uint8_t *data, bool is_uuid); + +int bt_mesh_rpr_srv_send_outbound_report(void *data, bool is_uuid); + +int bt_mesh_rpr_srv_send_pdu_report(uint8_t uuid[16], uint8_t type, + struct net_buf_simple *buf); + +int bt_mesh_rpr_srv_store_nppi_data(const uint8_t uuid[16], const uint8_t net_key[16], + uint16_t net_idx, uint8_t flags, uint32_t iv_index, + uint16_t addr, const uint8_t dev_key[16]); + +bool bt_mesh_rpr_srv_nppi_check(uint8_t nppi, uint8_t net_key[16], + uint16_t net_idx, uint32_t iv_index, + uint16_t addr, uint8_t *reason); + +bool bt_mesh_valid_security_cred(void *tx); + +void bt_mesh_choose_better_security_cred(void *tx); + +void bt_mesh_model_pub_use_directed(void *tx, uint8_t directed_pub_policy); + +void bt_mesh_is_directed_path_needed(void *tx); + +void bt_mesh_net_adv_xmit_update(void *tx); + +void bt_mesh_update_net_send_cred(void *tx, uint8_t *bearer); + +int bt_mesh_directed_decrypt(void *sub, const uint8_t *data, size_t data_len, + void *rx, struct net_buf_simple *buf); + +uint8_t bt_mesh_net_retrans_match(void *rx, uint8_t *cred, uint8_t *tag); + +bool bt_mesh_dev_key_ca_valid(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_v11_UTILS_H_ */ diff --git a/components/bt/esp_ble_mesh/v1.1/lib/esp32/libmesh_v1.1.a b/components/bt/esp_ble_mesh/v1.1/lib/esp32/libmesh_v1.1.a new file mode 100644 index 000000000000..7024f4a17d84 Binary files /dev/null and b/components/bt/esp_ble_mesh/v1.1/lib/esp32/libmesh_v1.1.a differ diff --git a/components/bt/esp_ble_mesh/v1.1/lib/esp32c3/libmesh_v1.1.a b/components/bt/esp_ble_mesh/v1.1/lib/esp32c3/libmesh_v1.1.a new file mode 100644 index 000000000000..ce019d13c535 Binary files /dev/null and b/components/bt/esp_ble_mesh/v1.1/lib/esp32c3/libmesh_v1.1.a differ diff --git a/components/bt/esp_ble_mesh/v1.1/lib/esp32c6/libmesh_v1.1.a b/components/bt/esp_ble_mesh/v1.1/lib/esp32c6/libmesh_v1.1.a new file mode 100644 index 000000000000..ce019d13c535 Binary files /dev/null and b/components/bt/esp_ble_mesh/v1.1/lib/esp32c6/libmesh_v1.1.a differ diff --git a/components/bt/esp_ble_mesh/v1.1/lib/esp32h2/libmesh_v1.1.a b/components/bt/esp_ble_mesh/v1.1/lib/esp32h2/libmesh_v1.1.a new file mode 100644 index 000000000000..ce019d13c535 Binary files /dev/null and b/components/bt/esp_ble_mesh/v1.1/lib/esp32h2/libmesh_v1.1.a differ diff --git a/components/bt/esp_ble_mesh/v1.1/lib/esp32s3/libmesh_v1.1.a b/components/bt/esp_ble_mesh/v1.1/lib/esp32s3/libmesh_v1.1.a new file mode 100644 index 000000000000..7024f4a17d84 Binary files /dev/null and b/components/bt/esp_ble_mesh/v1.1/lib/esp32s3/libmesh_v1.1.a differ diff --git a/components/bt/esp_ble_mesh/v1.1/lib/lib_copy.sh b/components/bt/esp_ble_mesh/v1.1/lib/lib_copy.sh new file mode 100755 index 000000000000..449c883d618a --- /dev/null +++ b/components/bt/esp_ble_mesh/v1.1/lib/lib_copy.sh @@ -0,0 +1,39 @@ +#! /usr/bin/env bash + +echo "Copy Bluetooth Mesh v1.1 lib:" + +chip=$1 + +if [[ $chip != "esp32" && + $chip != "esp32s3" && + $chip != "esp32c3" && + $chip != "esp32c6" && + $chip != "esp32h2" && + $chip != "all" ]]; then + echo "Invalid Chip Target: $chip" + exit 0 +fi + +if [[ $chip == "esp32" || + $chip == "esp32s3" || + $chip == "esp32c3" || + $chip == "esp32c6" || + $chip == "esp32h2" ]]; then + cp ./build/$chip/libmesh_v1.1.a ./$chip/ + echo "Copy for $chip done!" +elif [[ $chip == "all" ]]; then + cp ./build/esp32/libmesh_v1.1.a ./esp32/ + echo "Copy for esp32 done!" + + cp ./build/esp32s3/libmesh_v1.1.a ./esp32s3/ + echo "Copy for esp32s3 done!" + + cp ./build/esp32c3/libmesh_v1.1.a ./esp32c3/ + echo "Copy for esp32c3 done!" + + cp ./build/esp32c6/libmesh_v1.1.a ./esp32c6/ + echo "Copy for esp32c6 done!" + + cp ./build/esp32h2/libmesh_v1.1.a ./esp32h2/ + echo "Copy for esp32h2 done!" +fi diff --git a/docs/doxygen/Doxyfile b/docs/doxygen/Doxyfile index 4cd77968b637..31ac13c240c8 100644 --- a/docs/doxygen/Doxyfile +++ b/docs/doxygen/Doxyfile @@ -34,6 +34,16 @@ INPUT = \ $(PROJECT_PATH)/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h \ $(PROJECT_PATH)/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h \ $(PROJECT_PATH)/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_proxy_api.h \ + $(PROJECT_PATH)/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_agg_model_api.h \ + $(PROJECT_PATH)/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_brc_model_api.h \ + $(PROJECT_PATH)/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_cm_data_api.h \ + $(PROJECT_PATH)/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_df_model_api.h \ + $(PROJECT_PATH)/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_lcd_model_api.h \ + $(PROJECT_PATH)/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_odp_model_api.h \ + $(PROJECT_PATH)/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_prb_model_api.h \ + $(PROJECT_PATH)/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_rpr_model_api.h \ + $(PROJECT_PATH)/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_sar_model_api.h \ + $(PROJECT_PATH)/components/bt/esp_ble_mesh/v1.1/api/core/include/esp_ble_mesh_srpl_model_api.h \ $(PROJECT_PATH)/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h \ $(PROJECT_PATH)/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_config_model_api.h \ $(PROJECT_PATH)/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_generic_model_api.h \ @@ -41,6 +51,7 @@ INPUT = \ $(PROJECT_PATH)/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_lighting_model_api.h \ $(PROJECT_PATH)/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_sensor_model_api.h \ $(PROJECT_PATH)/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_time_scene_model_api.h \ + $(PROJECT_PATH)/components/bt/esp_ble_mesh/v1.1/api/models/include/esp_ble_mesh_mbt_model_api.h \ $(PROJECT_PATH)/components/bt/host/bluedroid/api/include/api/esp_a2dp_api.h \ $(PROJECT_PATH)/components/bt/host/bluedroid/api/include/api/esp_avrc_api.h \ $(PROJECT_PATH)/components/bt/host/bluedroid/api/include/api/esp_bt_defs.h \ 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 ec04f47bb9ac..f096c8a79750 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 @@ -390,7 +390,7 @@ When adopting the design of independent module, the two main factors should be c - ESP-BLE-MESH Proxy Server related functionalities * - :component_file:`proxy_client.c ` - ESP-BLE-MESH Proxy Client related functionalities - * - :component_file:`settings.c ` + * - :component_file:`settings.c ` - ESP-BLE-MESH NVS storage functionality * - :component_file:`main.c ` - ESP-BLE-MESH stack initialize, stack enable, node removal related functionalities diff --git a/docs/en/api-guides/esp-ble-mesh/ble-mesh-faq.rst b/docs/en/api-guides/esp-ble-mesh/ble-mesh-faq.rst index 2f4250da6d2f..bd5d22cf6a1c 100644 --- a/docs/en/api-guides/esp-ble-mesh/ble-mesh-faq.rst +++ b/docs/en/api-guides/esp-ble-mesh/ble-mesh-faq.rst @@ -568,7 +568,7 @@ Generally, a Provisioner is used to provision unprovisioned devices and form a m The **count** value is provided to the Proxy node which is provisioned by the App so as to determine when to start Proxy advertising in advance. -4.5 When will Configuration Client Model of the node running :example:`fast_prov_server ` example start to work? +4.5 When will Configuration Client Model of the node running :example:`fast_prov_server ` example start to work? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Configuration Client Model will start to work after the Temporary Provisioner functionality is enabled. diff --git a/docs/en/api-guides/esp-ble-mesh/ble-mesh-feature-list.rst b/docs/en/api-guides/esp-ble-mesh/ble-mesh-feature-list.rst index dcabebffd9ce..5b934314216d 100644 --- a/docs/en/api-guides/esp-ble-mesh/ble-mesh-feature-list.rst +++ b/docs/en/api-guides/esp-ble-mesh/ble-mesh-feature-list.rst @@ -7,13 +7,12 @@ Supported Features Mesh Core """"""""" -* Provisioning: Node Role - * PB-ADV and PB-GATT - * OOB Authentication - -* Provisioning: Provisioner Role - * PB-ADV and PB-GATT +* Provisioning: + * PB-ADV, PB-GATT and PB-Remote * OOB Authentication + * Certificate-based Provisioning + * Remote Provisioning + * Enhanced Provisioning Authentication * Networking * Relay @@ -24,6 +23,10 @@ Mesh Core * Low Power * Proxy Server * Proxy Client + * Directed Forwarding + * Private Beacon + * Subnet Bridge + * Minor Enhancements * Multiple Client Models Run Simultaneously * Support multiple client models send packets to different nodes simultaneously @@ -40,6 +43,24 @@ Mesh Models * Configuration Client model * Health Server model * Health Client model + * Remote Provisioning Server model + * Remote Provisioning Client model + * Directed Forwarding Configuration Server model + * Directed Forwarding Configuration Client model + * Bridge Configuration Server model + * Bridge Configuration Client model + * Mesh Private Beacon Server model + * Mesh Private Beacon Client model + * On-Demend Private Proxy Server model + * On-Demend Private Proxy Client model + * SAR Configuration Server model + * SAR Configuration Client model + * Solicitation PDU RPL Configuration Server model + * Solicitation PDU RPL Configuration Client model + * Opcodes Aggregator Server model + * Opcodes Aggregator Client model + * Large Composition Data Server model + * Large Composition Data Client model * Generic client models * Generic OnOff Client @@ -120,9 +141,9 @@ Mesh Applications * :example_file:`Tutorial ` * :example:`Example ` * ESP-BLE-MESH Fast Provisioning - * :example_file:`Fast Provisioning Client Model Tutorial ` - * :example_file:`Fast Provisioning Server Model Tutorial ` - * :example:`Example ` + * :example_file:`Fast Provisioning Client Model Tutorial ` + * :example_file:`Fast Provisioning Server Model Tutorial ` + * :example:`Example ` * `Demo Video `__ * ESP-BLE-MESH and Wi-Fi Coexistence * :example_file:`Tutorial ` diff --git a/docs/en/api-guides/esp-ble-mesh/ble-mesh-index.rst b/docs/en/api-guides/esp-ble-mesh/ble-mesh-index.rst index 8092c582dd07..2c32552961fc 100644 --- a/docs/en/api-guides/esp-ble-mesh/ble-mesh-index.rst +++ b/docs/en/api-guides/esp-ble-mesh/ble-mesh-index.rst @@ -219,10 +219,14 @@ ESP-BLE-MESH Examples * :example_file:`ESP-BLE-MESH Provisioner ` - shows how a device can act as an ESP-BLE-MESH Provisioner to provision devices. The Provisioner has a Configuration Server model, a Configuration Client model and a Generic OnOff Client model, see :example:`example code `. -* ESP-BLE-MESH Fast Provisioning - :example_file:`Client ` and :example_file:`Server ` - this example is used for showing how fast provisioning can be used in order to create a mesh network. It takes no more than 60 seconds to provision 100 devices, see :example:`example client code ` and :example:`example server code `. +* ESP-BLE-MESH Fast Provisioning - :example_file:`Client ` and :example_file:`Server ` - this example is used for showing how fast provisioning can be used in order to create a mesh network. It takes no more than 60 seconds to provision 100 devices, see :example:`example client code ` and :example:`example server code `. * :example_file:`ESP-BLE-MESH and Wi-Fi Coexistence ` - an example that demonstrates the Wi-Fi and Bluetooth (BLE/BR/EDR) coexistence feature of {IDF_TARGET_NAME}. Simply put, users can use the Wi-Fi while operating Bluetooth, see :example:`example code `. +* ESP-BLE-MESH Remote Provisioning(v1.1) - :example_file:`Client, Server and device ` - this example is used to demonstrate the new remote provisioning feature introduced in the Mesh Protocol v1.1, see :example:`example client code `, :example:`example server code ` and :example:`example device code `. + +* ESP-BLE-MESH Directed Forwarding(v1.1) - :example_file:`Client and Server ` - this example is used to demonstrate the new directed forwarding feature introduced in the Mesh Protocol v1.1. Only nodes along the path will forward the directed messages, while other nodes will not actively participate in forwarding, see :example:`example client code ` and :example:`example server code `. + .. _esp-ble-mesh-demo-videos: @@ -262,7 +266,9 @@ Bluetooth SIG Documentation --------------------------- - `BLE Mesh Core Specification `_ +- `BLE Mesh Protocol v1.1 Specification(draft) `_ - `BLE Mesh Model Specification `_ +- `BLE Mesh Model v1.1 Specification(draft) `_ - `An Intro to Bluetooth Mesh Part 1 `_ / `Part 2 `__ - `The Fundamental Concepts of Bluetooth Mesh Networking, Part 1 `_ / `Part 2 `__ - `Bluetooth Mesh Networking: Friendship `_ diff --git a/docs/en/api-guides/esp-ble-mesh/ble-mesh-terminology.rst b/docs/en/api-guides/esp-ble-mesh/ble-mesh-terminology.rst index 8ca7aea51b81..1c8dcaccce09 100644 --- a/docs/en/api-guides/esp-ble-mesh/ble-mesh-terminology.rst +++ b/docs/en/api-guides/esp-ble-mesh/ble-mesh-terminology.rst @@ -95,7 +95,10 @@ ESP-BLE-MESH Terminology - PB-ADV transfers packets generated during the provisioning process over the advertising channels. This way can only be used for provisioning when provisioner and unprovisioned device both support PB-ADV. * - PB-GATT - PB-GATT is a provisioning bearer used to provision a device using Proxy PDUs to encapsulate Provisioning PDUs within the Mesh Provisioning Service. - - PB-GATT uses connection channels to transfer packets generated during the provisioning process. If an unprovisioned device wants to be provisioned through this method, it needs to implement the related Mesh Provisioning Service. Unprovisioned devices which do not implement such service cannot be provisioned into mesh network through PB-GATT bearer. + - PB-GATT uses connection channels to transfer packets generated during the provisioning process. If an unprovisioned device wants to be provisioned through this method, it needs to implement the related Mesh Provisioning Service. Unprovisioned devices which don't implement such service cannot be provisioned into mesh network through PB-GATT bearer. + * - PB-Remote + - The PB-Remote provisioning bearer uses the existing mesh network to provision an unprovisioned device that is not within immediate radio range of the Provisioner. + - PB-Remote uses the PB-ADV bearer or the PB-GATT bearer for the last hop to the unprovisioned device. * - Provisioning - Provisioning is a process of adding an unprovisioned device to a mesh network, managed by a Provisioner. - The process of provisioning turns the "unprovisioned device" into a "node", making it a member of the ESP-BLE-MESH network. @@ -114,6 +117,9 @@ ESP-BLE-MESH Terminology * - No OOB - No Out-of-Band - Authentication method of No OOB: Set the value of the Static OOB field to 0. Using this way is like not authenticating the unprovisioned devices. + * - Certificate-based Provisioning + - Certificate-based Out-of-Band + - The certificate-based provisioning feature makes use of Public Key Infrastructure to authenticate unprovisioned device's public key and UUID information. .. _ble-mesh-terminology-address: @@ -154,10 +160,12 @@ ESP-BLE-MESH Terminology * - Application Key (AppKey) - Application keys are used to secure communications at the upper transport layer. - Application key is used for decryption of application data before delivering application data to application layer and encryption of them during the delivery of application layer. Some nodes in the network have a specific purpose and can restrict access to potentially sensitive data based on the needs of the application. With specific application keys, these nodes are associated with specific applications. Generally speaking, the fields using different application keys include security (access control of buildings, machine rooms and CEO offices), lighting (plant, exterior building and sidewalks) and HVAC systems. Application keys are bound to Network keys. This means application keys are only used in a context of a Network key they are bound to. An application key shall only be bound to a single Network key. - * - Master Security Material - - The master security material is derived from the network key (NetKey) and can be used by other nodes in the same network. Messages encrypted with master security material can be decoded by any node in the same network. - - The corresponding friendship messages encrypted with the friendship security material: 1. Friend Poll, 2. Friend Update, 3. Friend Subscription List, add/delete/confirm, 4. The Stored Messages" sent by friend nodes to Low Power node. The corresponding friendship messages encrypted with the master security material: 1. Friend Clear, 2. Friend Clear Confirm. Based on the setup of the applications, the messages sent from the Low Power node to the friend nodes will be encrypted with the friendship security material or master security material, with the former being used by the messages transmitted between Low Power node and friend nodes and the latter being used by other network messages. - + * - Flooding Security Material + - The flooding security material is derived from the network key (NetKey) and can be used by other nodes in the same network. Messages encrypted with flooding security material can be decoded by any node in the same network. + - The corresponding friendship messages encrypted with the friendship security material: 1. Friend Poll, 2. Friend Update, 3. Friend Subscription List, add/delete/confirm, 4. The Stored Messages" sent by friend nodes to Low Power node. The corresponding friendship messages encrypted with the flooding security material: 1. Friend Clear, 2. Friend Clear Confirm. Based on the setup of the applications, the messages sent from the Low Power node to the friend nodes will be encrypted with the friendship security material or flooding security material, with the former being used by the messages transmitted between Low Power node and friend nodes and the latter being used by other network messages. + * - Directed Security Material + - The directed security material is derived from the network key (NetKey) and can be used by other nodes in the directed forwarding path. + - The messages that need to be forwarded through the directed forwarding path need to be encrypted with the directed security material. Messages encrypted with directed security material can be decoded by any node in the same directed forwarding path. .. _ble-mesh-terminology-message: @@ -197,6 +205,60 @@ ESP-BLE-MESH Terminology * - Health Client Model - The model is used to represent an element that can control and monitor the health of a node. - The Health Client Model uses messages to control the state maintained by the Health Server Model. The model can get the self-test information of other nodes through the message "Health Fault Get". + * - Remote Provisioning Server model + - The model is used to support the functionality of provisioning a remote device over the mesh network and to perform the Node Provisioning Protocol Interface procedures. + - The Remote Provisioning Server model is a root model and a main model that does not extend any other models. + * - Remote Provisioning Client model + - The model is used to support the functionality of provisioning devices into a mesh network by interacting with a mesh node that supports the Remote Provisioning Server model. + - The Remote Provisioning Client is a root model and a main model that does not extend any other models. The Remote Provisioning Client may operate on states defined by the Remote Provisioning Server model using Remote Provisioning messages. + * - Directed Forwarding Configuration Server model + - The model is used to support the configuration of the directed forwarding functionality of a node. + - The Directed Forwarding Configuration Server model is a main model that extends the Configuration Server model. + * - Directed Forwarding Configuration Client model + - The model is used to support the functionality of a node that can configure the directed forwarding functionality of another node. + - The Directed Forwarding Configuration Client model is a root model and a main model that does not extend any other models. The Directed Forwarding Configuration Client model may operate on states defined by the Directed Forwarding Configuration Server model using Directed Forwarding Configuration messages. + * - Bridge Configuration Server model + - The model is used to support the configuration of the subnet bridge functionality of a node. + - The Bridge Configuration Server model is a main model that extends the Configuration Server model. + * - Bridge Configuration Client model + - The model is used to support the functionality of a node that can configure the subnet bridge functionality of another node. + - The Bridge Configuration Client model is a root model and a main model that does not extend any other models. The Bridge Configuration Client model may operate on states defined by the Bridge Configuration Server model using Bridge messages. + * - Mesh Private Beacon Server model + - The model is used to support the configuration of the Mesh Private beacons functionality of a node. + - The Mesh Private Beacon Server model is a main model that extends the Configuration Server model. + * - Mesh Private Beacon Client model + - The model is used to support the functionality of a node that can configure the Mesh Private beacons functionality of another node. + - The Mesh Private Beacon Client model is a root model and a main model that does not extend any other models. The Mesh Private Beacon Client model may operate on states defined by the Mesh Private Beacon Server model using Mesh Private Beacon messages. + * - On-Demend Private Proxy Server model + - The model is used to support the configuration of the advertising with Private Network Identity type functionality of a node. + - The On-Demand Private Proxy Server model is a main model that extends the Mesh Private Beacon Server model and corresponds with the Solicitation PDU RPL Configuration Server model. + * - On-Demend Private Proxy Client model + - The model is used to support the functionality of a node that can configure the advertising with Private Network Identity type functionality of another node. + - The On-Demand Private Proxy Client model is a root model and a main model that does not extend any other models. The On-Demand Private Proxy Client model may operate on states defined by the On-Demand Private Proxy Server model using On-Demand Private Proxy messages. + * - SAR Configuration Server model + - The model is used to support the configuration of the segmentation and reassembly behavior of a node. + - The SAR Configuration Server model is a root model and a main model that does not extend any other models. + * - SAR Configuration Client model + - The SAR Configuration Client model is used to support the functionality of configuring the behavior of the lower transport layer of a node that supports the SAR Configuration Server model. + - The SAR Configuration Client model is a root model and a main model that does not extend any other models. The SAR Configuration Client model may operate on states defined by the SAR Configuration Server model using SAR Configuration messages. + * - Solicitation PDU RPL Configuration Server model + - The Solicitation PDU RPL Configuration Server model is used to support the functionality of removing items from the solicitation replay protection list of a node. + - The Solicitation PDU RPL Configuration Server model corresponds with the On-Demand Private Proxy Server model. + * - Solicitation PDU RPL Configuration Client model + - The model is used to support the functionality of removing addresses from the solicitation replay protection list of a node that supports the Solicitation PDU RPL Configuration Server model. + - The Solicitation PDU RPL Configuration Client model is a root model and a main model that does not extend any other models. The Solicitation PDU RPL Configuration Client model may be used to remove items from a solicitation replay protection list of a peer node by using Solicitation PDU RPL Configuration messages. + * - Opcodes Aggregator Server model + - The model is used to support the functionality of processing a sequence of access layer messages. + - The Opcodes Aggregator Server model is a root model that does not extend any other models. + * - Opcodes Aggregator Client model + - The model is used to support the functionality of dispatching a sequence of access layer messages to nodes supporting the Opcodes Aggregator Server model. + - The Opcodes Aggregator Client model is a root model and a main model that does not extend any other models. + * - Large Composition Data Server model + - The model is used to support the functionality of exposing pages of Composition Data that do not fit in a Config Composition Data Status message and to expose metadata of the model instances. + - The Large Composition Data Server is a main model that extends the Configuration Server model. + * - Large Composition Data Client model + - The model is used to support the functionality of reading pages of Composition Data that do not fit in a Config Composition Data Status message and reading the metadata of the model instances. + - The Large Composition Data Client model is a root model that does not extend any other models. The Large Composition Data Client model may operate on states defined by the Large Composition Data Server model using Large Composition Data messages. .. _ble-mesh-terminology-network-management: diff --git a/docs/en/api-reference/bluetooth/esp-ble-mesh.rst b/docs/en/api-reference/bluetooth/esp-ble-mesh.rst index d7d22eb27b19..9968a382bdcd 100644 --- a/docs/en/api-reference/bluetooth/esp-ble-mesh.rst +++ b/docs/en/api-reference/bluetooth/esp-ble-mesh.rst @@ -1,6 +1,10 @@ ESP-BLE-MESH ============ +.. note:: + + The current ESP-BLE-MESH v1.1 related code is a preview version, so the Mesh Protocol v1.1 related Structures, MACROs, and APIs involved in the code may be changed. + With various features of ESP-BLE-MESH, users can create a managed flooding mesh network for several scenarios, such as lighting, sensor and etc. For an ESP32 to join and work on a ESP-BLE-MESH network, it must be provisioned firstly. By provisioning, the ESP32, as an unprovisioned device, will join the ESP-BLE-MESH network and become a ESP-BLE-MESH node, communicating with other nodes within or beyond the radio range. diff --git a/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-architecture.rst b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-architecture.rst index 90281f0e6709..8e59771ef37b 100644 --- a/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-architecture.rst +++ b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-architecture.rst @@ -390,7 +390,7 @@ Mesh Bearers 在实现时充分考虑了可移植性。当 ESP-BLE-MESH 协议 - ESP-BLE-MESH 代理服务器相关功能 * - :component_file:`proxy_client.c ` - ESP-BLE-MESH 代理客户端相关功能 - * - :component_file:`settings.c ` + * - :component_file:`settings.c ` - ESP-BLE-MESH NVS 存储器功能 * - :component_file:`main.c ` - ESP-BLE-MESH 协议栈初始化,协议栈使能,节点移除相关功能 diff --git a/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-faq.rst b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-faq.rst index 323735c7c7e0..5d17296b0bd0 100644 --- a/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-faq.rst +++ b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-faq.rst @@ -568,7 +568,7 @@ ESP-BLE-MESH 常见问题手册 此 **count** 值提供给 App 配置的代理节点,以决定何时提前开始 Proxy 广播信息。 -4.5 运行以下示例 :example:`fast_prov_server ` 的节点的 Configuration Client Model 何时开始工作? +4.5 运行以下示例 :example:`fast_prov_server ` 的节点的 Configuration Client Model 何时开始工作? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 使能了 Temporary Provisioner 功能后,Configuration Client Model 会开始工作。 diff --git a/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-index.rst b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-index.rst index 30e0fd9b1f43..762f5a1720b7 100644 --- a/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-index.rst +++ b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-index.rst @@ -219,10 +219,14 @@ ESP-BLE-MESH 示例 * :example_file:`ESP-BLE-MESH Provisioner ` - 展示了设备如何充当 ESP-BLE-MESH Provisioner 以配网设备。Provisioner 拥有 Configuration Server model、Configuration Client model 和 Generic OnOff Client model,示例请见 :example:`example code `。 -* ESP-BLE-MESH 快速配网 - :example_file:`Client ` 和 :example_file:`Server ` - 该示例用于演示快速配网。配网 100 个设备费时不超过 60 秒,示例请见::example:`example client code ` 和 :example:`example server code `。 +* ESP-BLE-MESH 快速配网 - :example_file:`Client ` 和 :example_file:`Server ` - 该示例用于演示快速配网。配网 100 个设备费时不超过 60 秒,示例请见::example:`example client code ` 和 :example:`example server code `。 * :example_file:`Wi-Fi 和 ESP-BLE-MESH 共存 ` - 该示例用于演示 Wi-Fi 和 ESP-BLE-MESH 共存的功能。简而言之,用户可在运行 ESP-BLE-MESH 时使用 Wi-Fi,示例请见 :example:`example code `。 +* ESP-BLE-MESH 远程配网(v1.1 新增) - :example_file:`Client, Server and device ` - 该示例用于演示 mesh 协议 v1.1 中新增的远程配网功能, 示例请见: :example:`example client code `, :example:`example server code ` and :example:`example device code `. + +* ESP-BLE-MESH 定向转发(v1.1 新增) - :example_file:`Client and Server ` - 该示例用于演示 mesh 协议 v1.1 中新增的定向转发功能。只有路径上的节点才会对定向消息进行转发,而其他节点不参与转发, 示例请见 :example:`example client code ` and :example:`example server code `. + .. _esp-ble-mesh-demo-videos: @@ -262,7 +266,9 @@ ESP-BLE-MESH 常见问题手册 ------------- - `BLE Mesh Core Specification `_ +- `BLE Mesh Protocol v1.1 Specification(draft) `_ - `BLE Mesh Model Specification `_ +- `BLE Mesh Model v1.1 Specification(draft) `_ - `An Intro to Bluetooth Mesh Part 1 `_ / `Part 2 `__ - `The Fundamental Concepts of Bluetooth Mesh Networking, Part 1 `_ / `Part 2 `__ - `Bluetooth Mesh Networking: Friendship `_ diff --git a/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-terminology.rst b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-terminology.rst index ce89b36dc051..2bba1491434b 100644 --- a/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-terminology.rst +++ b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-terminology.rst @@ -96,6 +96,9 @@ ESP-BLE-MESH Terminology * - PB-GATT - "PB-GATT is a provisioning bearer used to provision a device using Proxy PDUs to encapsulate Provisioning PDUs within the Mesh Provisioning Service." - PB-GATT 通过连接通道传输配网过程中产生的数据包。如果未配网设备想使用此方式进行配网,其需要实现相关的 Mesh Provisioning Service。未实现此服务的未配网设备不能通过 PB-GATT 承载层配网接入 mesh 网络。 + * - PB-Remote + - "The PB-Remote provisioning bearer uses the existing mesh network to provision an unprovisioned device that is not within immediate radio range of the Provisioner." + - PB-Remote 是一种使用现有的网状网络来为无法与配网器直接通信的未配网设备进行配网的配网承载方式。 * - 配置入网 - "Provisioning is a process of adding an unprovisioned device to a mesh network, managed by a Provisioner." - 经过配网,“未配网设备”的身份转变为“节点”,成为 ESP-BLE-MESH 网络中的一员。 @@ -114,7 +117,9 @@ ESP-BLE-MESH Terminology * - 无带外 (No OOB) - No Out-of-Band - 无 OOB 的认证方法:将“静态 OOB”字段赋值为 0。采用这种方式相当于不认证未配网的设备。 - + * - 基于证书的配网 (Certificate-based Provisioning) + - Certificate-based Out-of-Band + - 基于证书的配网功能利用公钥基础设施来验证未配网设备的公钥和UUID信息。 .. _ble-mesh-terminology-address: @@ -154,10 +159,12 @@ ESP-BLE-MESH Terminology * - 应用密钥 (AppKey) - "Application keys are used to secure communications at the upper transport layer." - 应用密钥用于应用数据传递至应用层过程中对应用数据的解密,和应用层下发过程中对数据的加密。网络中的一些节点有特定的用途,并且可以根据应用程序的需求对一些潜在敏感数据的访问进行限制。通过特定的应用密钥,这些节点与特定应用程序相关联。通常而言,使用不同应用密钥的领域有安全(楼宇门禁、机房门禁和 CEO 办公室门禁)、照明(工厂、外部楼宇和人行道)和 HVAC 系统。应用密钥绑定在网络密钥上,这意味着应用密钥仅在绑定网络密钥的情况下使用。每一个应用密钥仅可绑定到一个网络密钥。 - * - 主安全资料 + * - 泛洪安全资料 - "The master security material is derived from the network key (NetKey) and can be used by other nodes in the same network. Messages encrypted with master security material can be decoded by any node in the same network. " - - 使用好友安全材料加密的相应友谊消息有:1. 好友轮询 (Friend Poll),2. 好友更新 (Friend Update),3. 好友订阅列表 (Friend Subscription List),添加/删除/确认,4. 好友节点发送到低功耗节点的“已存储消息”,使用主安全材料加密的相应友谊消息有:1. 好友清除 (Friend Clear),2. 好友清除确认 (Friend Clear Confirm)。根据应用程序的设置,从低功耗节点发送到好友节点的消息会使用友谊安全材料或主安全材料进行加密,前者用于低功耗节点与好友节点之间的消息传输,而后者用于其他网络消息。 - + - 使用好友安全材料加密的相应友谊消息有:1. 好友轮询 (Friend Poll),2. 好友更新 (Friend Update),3. 好友订阅列表 (Friend Subscription List),添加/删除/确认,4. 好友节点发送到低功耗节点的“已存储消息”,使用泛洪安全材料加密的相应友谊消息有:1. 好友清除 (Friend Clear),2. 好友清除确认 (Friend Clear Confirm)。根据应用程序的设置,从低功耗节点发送到好友节点的消息会使用友谊安全材料或泛洪安全材料进行加密,前者用于低功耗节点与好友节点之间的消息传输,而后者用于其他网络消息。 + * - 定向安全资料 + - "The directed security material is derived from the network key (NetKey) and can be used by other nodes in the directed forwarding path." + - 定向安全资料是从网络密钥(NetKey)派生的,可以被定向转发路径中的其他节点使用。使用定向安全资料加密的消息可以被同一定向转发路径中的任何节点解码。 .. _ble-mesh-terminology-message: @@ -196,6 +203,60 @@ ESP-BLE-MESH Terminology * - Health Client Model - "The model is used to represent an element that can control and monitor the health of a node." - Health Client Model 通过消息控制 Health Server Model 维护的状态。该模型可通过消息 “Health Fault Get” 获取其他节点的自检信息。 + * - Remote Provisioning Server model + - "The model is used to support the functionality of provisioning a remote device over the mesh network and to perform the Node Provisioning Protocol Interface procedures." + - 该模型用于支持通过网状网络对远程设备进行供应,并执行节点供应协议接口程序。 + * - Remote Provisioning Client model + - "The model is used to support the functionality of provisioning devices into a mesh network by interacting with a mesh node that supports the Remote Provisioning Server model." + - 该模型用于与支持远程供应服务器模型的网状节点进行交互,以支持将设备供应到网状网络的功能。 + * - Directed Forwarding Configuration Server model + - "The model is used to support the configuration of the directed forwarding functionality of a node." + - 该模型用于支持节点的定向转发功能的配置。 + * - Directed Forwarding Configuration Client model + - "The model is used to support the functionality of a node that can configure the directed forwarding functionality of another node." + - 该模型用于支持一个节点配置另一个节点的定向转发功能的功能。 + * - Bridge Configuration Server model + - "The model is used to support the configuration of the subnet bridge functionality of a node." + - 该模型用于支持节点的子网桥接功能的配置。 + * - Bridge Configuration Client model + - "The model is used to support the functionality of a node that can configure the subnet bridge functionality of another node." + - 该模型用于支持一个节点配置另一个节点的子网桥接功能的功能。 + * - Mesh Private Beacon Server model + - "The model is used to support the configuration of the Mesh Private beacons functionality of a node." + - 该模型用于支持节点的 Mesh 私有信标功能的配置。 + * - Mesh Private Beacon Client model + - "The model is used to support the functionality of a node that can configure the Mesh Private beacons functionality of another node." + - 该模型用于支持一个节点配置另一个节点的 Mesh 私有信标功能的功能。 + * - On-Demend Private Proxy Server model + - "The model is used to support the configuration of the advertising with Private Network Identity type functionality of a node." + - 该模型用于支持节点的私有网络身份类型广告配置功能。 + * - On-Demend Private Proxy Client model + - "The model is used to support the functionality of a node that can configure the advertising with Private Network Identity type functionality of another node." + - 该模型用于支持一个节点配置另一个节点的私有网络身份类型广告功能的功能。 + * - SAR Configuration Server model + - "The model is used to support the configuration of the segmentation and reassembly behavior of a node." + - 该模型用于支持节点的分段和重组行为的配置。 + * - SAR Configuration Client model + - "The SAR Configuration Client model is used to support the functionality of configuring the behavior of the lower transport layer of a node that supports the SAR Configuration Server model." + - SAR配置客户端模型用于支持配置支持 SAR 配置服务器模型的节点的较低传输层行为的功能。 + * - Solicitation PDU RPL Configuration Server model + - "The Solicitation PDU RPL Configuration Server model is used to support the functionality of removing items from the solicitation replay protection list of a node." + - Solicitation PDU RPL 配置服务器模型用于支持从节点的请求重放保护列表中移除项目的功能。 + * - Solicitation PDU RPL Configuration Client model + - "The model is used to support the functionality of removing addresses from the solicitation replay protection list of a node that supports the Solicitation PDU RPL Configuration Server model." + - 该模型用于支持支持 Solicitation PDU RPL 配置服务器模型的节点从其请求重放保护列表中移除地址的功能。 + * - Opcodes Aggregator Server model + - "The model is used to support the functionality of processing a sequence of access layer messages." + - 该模型用于支持处理一系列访问层消息的功能。 + * - Opcodes Aggregator Client model + - "The model is used to support the functionality of dispatching a sequence of access layer messages to nodes supporting the Opcodes Aggregator Server model." + - 该模型用于支持将一系列访问层消息分派给支持 Opcodes Aggregator Server 模型的节点的功能。 + * - Large Composition Data Server model + - "The model is used to support the functionality of exposing pages of Composition Data that do not fit in a Config Composition Data Status message and to expose metadata of the model instances." + - 该模型用于支持暴露不适应于 Config Composition Data Status 消息中的组成数据页的功能,并暴露模型实例的元数据。 + * - Large Composition Data Client model + - "The model is used to support the functionality of reading pages of Composition Data that do not fit in a Config Composition Data Status message and reading the metadata of the model instances." + - 该模型用于支持读取不适应于 Config Composition Data Status 消息中的组成数据页的功能,并读取模型实例的元数据。 .. _ble-mesh-terminology-network-management: diff --git a/examples/bluetooth/esp_ble_mesh/aligenie_demo/components/vendor_model/genie_model_srv.c b/examples/bluetooth/esp_ble_mesh/aligenie_demo/components/vendor_model/genie_model_srv.c index d8885faa9d03..132c1d502a48 100644 --- a/examples/bluetooth/esp_ble_mesh/aligenie_demo/components/vendor_model/genie_model_srv.c +++ b/examples/bluetooth/esp_ble_mesh/aligenie_demo/components/vendor_model/genie_model_srv.c @@ -387,7 +387,6 @@ esp_err_t genie_model_msg_send(genie_model_msg_t *p_model_msg) ctx.net_idx = bt_mesh_model_get_netkey_id(p_model_msg->p_elem); ctx.addr = GENIE_RECV_ADDR; ctx.send_ttl = BLE_MESH_TTL_DEFAULT; - ctx.send_rel = 0; ESP_LOGI(TAG, "vendor message send: tid: 0x%02x, retry: %02d, len: %02d, opcode: 0x%02x, data: 0x%s", p_model_msg->tid, p_model_msg->retry, p_model_msg->len, p_model_msg->opid, util_hex2str(p_model_msg->data, p_model_msg->len)); ESP_LOGD(TAG, "vendor message send: element: %p, app_idx: %d, net_idx: %d, tid: 0x%02x, retry: %02d, len: %02d, opcode: 0x%02x, data: 0x%s", diff --git a/examples/bluetooth/esp_ble_mesh/common_components/fast_prov/ble_mesh_fast_prov_client_model.c b/examples/bluetooth/esp_ble_mesh/common_components/fast_prov/ble_mesh_fast_prov_client_model.c index bcaf06709d03..a4b2427ecdf3 100644 --- a/examples/bluetooth/esp_ble_mesh/common_components/fast_prov/ble_mesh_fast_prov_client_model.c +++ b/examples/bluetooth/esp_ble_mesh/common_components/fast_prov/ble_mesh_fast_prov_client_model.c @@ -129,7 +129,6 @@ void example_send_self_prov_node_addr(struct k_work *work) .app_idx = fast_prov_srv->app_idx, .dst = fast_prov_srv->prim_prov_addr, .timeout = 0, - .role = ROLE_FAST_PROV, }; err = example_send_fast_prov_self_prov_node_addr(model, &info, node_addr_send.addr); if (err != ESP_OK) { @@ -179,7 +178,6 @@ static void example_get_all_node_addr(struct k_work *work) .app_idx = node->app_idx, .dst = node->unicast_addr, .timeout = 10000, - .role = ROLE_PROVISIONER, }; err = example_send_fast_prov_all_node_addr_get(model, &info); if (err != ESP_OK) { @@ -234,12 +232,10 @@ esp_err_t example_fast_prov_client_recv_timeout(uint32_t opcode, esp_ble_mesh_mo set.ctx_flags = BIT(6); set.group_addr = fast_prov_srv->group_addr; } - info.role = ROLE_FAST_PROV; #else set.ctx_flags = 0x037F; memcpy(&set.node_addr_cnt, &node->node_addr_cnt, sizeof(example_node_info_t) - offsetof(example_node_info_t, node_addr_cnt)); - info.role = ROLE_PROVISIONER; #endif err = example_send_fast_prov_info_set(model, &info, &set); if (err != ESP_OK) { @@ -258,7 +254,6 @@ esp_err_t example_fast_prov_client_recv_timeout(uint32_t opcode, esp_ble_mesh_mo .app_idx = fast_prov_srv->app_idx, .dst = fast_prov_srv->prim_prov_addr, .timeout = 0, - .role = ROLE_FAST_PROV, }; err = example_send_fast_prov_self_prov_node_addr(model, &info, node_addr_send.addr); if (err != ESP_OK) { @@ -286,7 +281,6 @@ esp_err_t example_fast_prov_client_recv_timeout(uint32_t opcode, esp_ble_mesh_mo .app_idx = node->app_idx, .dst = node->unicast_addr, .timeout = 10000, - .role = ROLE_PROVISIONER, }; err = example_send_fast_prov_all_node_addr_get(model, &info); if (err != ESP_OK) { @@ -384,7 +378,6 @@ esp_err_t example_fast_prov_client_recv_status(esp_ble_mesh_model_t *model, .app_idx = node->app_idx, .dst = node->group_addr, .timeout = 0, - .role = ROLE_PROVISIONER, }; err = example_send_generic_onoff_set(cli_model, &info, LED_ON, 0x00, false); if (err != ESP_OK) { diff --git a/examples/bluetooth/esp_ble_mesh/common_components/fast_prov/ble_mesh_fast_prov_operation.c b/examples/bluetooth/esp_ble_mesh/common_components/fast_prov/ble_mesh_fast_prov_operation.c index 0c83accb60a1..c2e9251618b5 100644 --- a/examples/bluetooth/esp_ble_mesh/common_components/fast_prov/ble_mesh_fast_prov_operation.c +++ b/examples/bluetooth/esp_ble_mesh/common_components/fast_prov/ble_mesh_fast_prov_operation.c @@ -349,10 +349,8 @@ esp_err_t example_send_config_appkey_add(esp_ble_mesh_model_t *model, common.ctx.net_idx = info->net_idx; common.ctx.app_idx = 0x0000; /* not used for config messages */ common.ctx.addr = info->dst; - common.ctx.send_rel = false; common.ctx.send_ttl = 0; common.msg_timeout = info->timeout; - common.msg_role = info->role; return esp_ble_mesh_config_client_set_state(&common, &set); } @@ -372,10 +370,8 @@ esp_err_t example_send_generic_onoff_get(esp_ble_mesh_model_t *model, common.ctx.net_idx = info->net_idx; common.ctx.app_idx = info->app_idx; common.ctx.addr = info->dst; - common.ctx.send_rel = false; common.ctx.send_ttl = 0; common.msg_timeout = info->timeout; - common.msg_role = info->role; return esp_ble_mesh_generic_client_get_state(&common, &get); } @@ -405,10 +401,8 @@ esp_err_t example_send_generic_onoff_set(esp_ble_mesh_model_t *model, common.ctx.net_idx = info->net_idx; common.ctx.app_idx = info->app_idx; common.ctx.addr = info->dst; - common.ctx.send_rel = false; common.ctx.send_ttl = 0; common.msg_timeout = info->timeout; - common.msg_role = info->role; return esp_ble_mesh_generic_client_set_state(&common, &set); } @@ -472,7 +466,6 @@ esp_err_t example_send_fast_prov_info_set(esp_ble_mesh_model_t *model, .net_idx = info->net_idx, .app_idx = info->app_idx, .addr = info->dst, - .send_rel = false, .send_ttl = 0, }; err = esp_ble_mesh_client_model_send_msg(model, &ctx, @@ -495,7 +488,6 @@ esp_err_t example_send_fast_prov_net_key_add(esp_ble_mesh_model_t *model, .net_idx = info->net_idx, .app_idx = info->app_idx, .addr = info->dst, - .send_rel = false, .send_ttl = 0, }; @@ -518,7 +510,6 @@ esp_err_t example_send_fast_prov_self_prov_node_addr(esp_ble_mesh_model_t *model .net_idx = info->net_idx, .app_idx = info->app_idx, .addr = info->dst, - .send_rel = false, .send_ttl = 0, }; @@ -538,7 +529,6 @@ esp_err_t example_send_fast_prov_all_node_addr_get(esp_ble_mesh_model_t *model, .net_idx = info->net_idx, .app_idx = info->app_idx, .addr = info->dst, - .send_rel = false, .send_ttl = 0, }; @@ -561,7 +551,6 @@ esp_err_t example_send_fast_prov_status_msg(esp_ble_mesh_model_t *model, case ESP_BLE_MESH_VND_MODEL_OP_FAST_PROV_NODE_ADDR_ACK: case ESP_BLE_MESH_VND_MODEL_OP_FAST_PROV_NODE_ADDR_STATUS: ctx->send_ttl = 0; - ctx->send_rel = false; break; default: ESP_LOGW(TAG, "%s: Invalid fast prov status opcode 0x%04" PRIx32, __func__, opcode); diff --git a/examples/bluetooth/esp_ble_mesh/common_components/light_driver/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/common_components/light_driver/CMakeLists.txt index 12b940ee567a..6dc16c7094cb 100644 --- a/examples/bluetooth/esp_ble_mesh/common_components/light_driver/CMakeLists.txt +++ b/examples/bluetooth/esp_ble_mesh/common_components/light_driver/CMakeLists.txt @@ -1,6 +1,8 @@ -set(COMPONENT_SRCS "light_driver.c" - "iot_led.c") +set(COMPONENT_SRCS + "light_driver.c" + "iot_led.c" + "led_strip_encoder.c") set(COMPONENT_ADD_INCLUDEDIRS ". include") diff --git a/examples/bluetooth/esp_ble_mesh/common_components/light_driver/include/led_strip_encoder.h b/examples/bluetooth/esp_ble_mesh/common_components/light_driver/include/led_strip_encoder.h new file mode 100644 index 000000000000..b397b2dfc4ce --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/common_components/light_driver/include/led_strip_encoder.h @@ -0,0 +1,41 @@ +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include +#include "driver/rmt_encoder.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Type of led strip encoder configuration + */ +typedef struct { + uint32_t resolution; /*!< Encoder resolution, in Hz */ +} led_strip_encoder_config_t; + +/** + * @brief Create RMT encoder for encoding LED strip pixels into RMT symbols + * + * @param[in] config Encoder configuration + * @param[out] ret_encoder Returned encoder handle + * @return + * - ESP_ERR_INVALID_ARG for any invalid arguments + * - ESP_ERR_NO_MEM out of memory when creating led strip encoder + * - ESP_OK if creating encoder successfully + */ +esp_err_t rmt_new_led_strip_encoder(const led_strip_encoder_config_t *config, rmt_encoder_handle_t *ret_encoder); + + + +void rmt_encoder_init(void); +void rmt_led_set(uint8_t red, uint8_t green, uint8_t blue); + +#ifdef __cplusplus +} +#endif diff --git a/examples/bluetooth/esp_ble_mesh/common_components/light_driver/iot_led.c b/examples/bluetooth/esp_ble_mesh/common_components/light_driver/iot_led.c index 428996ea3cd1..777868524a53 100644 --- a/examples/bluetooth/esp_ble_mesh/common_components/light_driver/iot_led.c +++ b/examples/bluetooth/esp_ble_mesh/common_components/light_driver/iot_led.c @@ -29,6 +29,8 @@ #define GPTIMER_RESOLUTION_HZ 1000000 // 1MHz, 1 tick=1us +#if (CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S3) + typedef struct { int cur; int final; @@ -475,3 +477,5 @@ esp_err_t iot_led_set_gamma_table(const uint16_t gamma_table[GAMMA_TABLE_SIZE]) memcpy(g_gamma_table, gamma_table, GAMMA_TABLE_SIZE * sizeof(uint16_t)); return ESP_OK; } + +#endif /* (IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3) */ diff --git a/examples/bluetooth/esp_ble_mesh/common_components/light_driver/led_strip_encoder.c b/examples/bluetooth/esp_ble_mesh/common_components/light_driver/led_strip_encoder.c new file mode 100644 index 000000000000..03ba98447ed9 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/common_components/light_driver/led_strip_encoder.c @@ -0,0 +1,178 @@ +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "esp_check.h" +#include "led_strip_encoder.h" +#include "driver/rmt_tx.h" +#include "portmacro.h" + +static const char *TAG = "led_encoder"; + +typedef struct { + rmt_encoder_t base; + rmt_encoder_t *bytes_encoder; + rmt_encoder_t *copy_encoder; + int state; + rmt_symbol_word_t reset_code; +} rmt_led_strip_encoder_t; + +static size_t rmt_encode_led_strip(rmt_encoder_t *encoder, rmt_channel_handle_t channel, const void *primary_data, size_t data_size, rmt_encode_state_t *ret_state) +{ + rmt_led_strip_encoder_t *led_encoder = __containerof(encoder, rmt_led_strip_encoder_t, base); + rmt_encoder_handle_t bytes_encoder = led_encoder->bytes_encoder; + rmt_encoder_handle_t copy_encoder = led_encoder->copy_encoder; + rmt_encode_state_t session_state = RMT_ENCODING_RESET; + rmt_encode_state_t state = RMT_ENCODING_RESET; + size_t encoded_symbols = 0; + switch (led_encoder->state) { + case 0: // send RGB data + encoded_symbols += bytes_encoder->encode(bytes_encoder, channel, primary_data, data_size, &session_state); + if (session_state & RMT_ENCODING_COMPLETE) { + led_encoder->state = 1; // switch to next state when current encoding session finished + } + if (session_state & RMT_ENCODING_MEM_FULL) { + state |= RMT_ENCODING_MEM_FULL; + goto out; // yield if there's no free space for encoding artifacts + } + // fall-through + case 1: // send reset code + encoded_symbols += copy_encoder->encode(copy_encoder, channel, &led_encoder->reset_code, + sizeof(led_encoder->reset_code), &session_state); + if (session_state & RMT_ENCODING_COMPLETE) { + led_encoder->state = RMT_ENCODING_RESET; // back to the initial encoding session + state |= RMT_ENCODING_COMPLETE; + } + if (session_state & RMT_ENCODING_MEM_FULL) { + state |= RMT_ENCODING_MEM_FULL; + goto out; // yield if there's no free space for encoding artifacts + } + } +out: + *ret_state = state; + return encoded_symbols; +} + +static esp_err_t rmt_del_led_strip_encoder(rmt_encoder_t *encoder) +{ + rmt_led_strip_encoder_t *led_encoder = __containerof(encoder, rmt_led_strip_encoder_t, base); + rmt_del_encoder(led_encoder->bytes_encoder); + rmt_del_encoder(led_encoder->copy_encoder); + free(led_encoder); + return ESP_OK; +} + +static esp_err_t rmt_led_strip_encoder_reset(rmt_encoder_t *encoder) +{ + rmt_led_strip_encoder_t *led_encoder = __containerof(encoder, rmt_led_strip_encoder_t, base); + rmt_encoder_reset(led_encoder->bytes_encoder); + rmt_encoder_reset(led_encoder->copy_encoder); + led_encoder->state = RMT_ENCODING_RESET; + return ESP_OK; +} + +esp_err_t rmt_new_led_strip_encoder(const led_strip_encoder_config_t *config, rmt_encoder_handle_t *ret_encoder) +{ + esp_err_t ret = ESP_OK; + rmt_led_strip_encoder_t *led_encoder = NULL; + ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument"); + led_encoder = calloc(1, sizeof(rmt_led_strip_encoder_t)); + ESP_GOTO_ON_FALSE(led_encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for led strip encoder"); + led_encoder->base.encode = rmt_encode_led_strip; + led_encoder->base.del = rmt_del_led_strip_encoder; + led_encoder->base.reset = rmt_led_strip_encoder_reset; + // different led strip might have its own timing requirements, following parameter is for WS2812 + rmt_bytes_encoder_config_t bytes_encoder_config = { + .bit0 = { + .level0 = 1, + .duration0 = 0.3 * config->resolution / 1000000, // T0H=0.3us + .level1 = 0, + .duration1 = 0.9 * config->resolution / 1000000, // T0L=0.9us + }, + .bit1 = { + .level0 = 1, + .duration0 = 0.9 * config->resolution / 1000000, // T1H=0.9us + .level1 = 0, + .duration1 = 0.3 * config->resolution / 1000000, // T1L=0.3us + }, + .flags.msb_first = 1 // WS2812 transfer bit order: G7...G0R7...R0B7...B0 + }; + ESP_GOTO_ON_ERROR(rmt_new_bytes_encoder(&bytes_encoder_config, &led_encoder->bytes_encoder), err, TAG, "create bytes encoder failed"); + rmt_copy_encoder_config_t copy_encoder_config = {}; + ESP_GOTO_ON_ERROR(rmt_new_copy_encoder(©_encoder_config, &led_encoder->copy_encoder), err, TAG, "create copy encoder failed"); + + uint32_t reset_ticks = config->resolution / 1000000 * 50 / 2; // reset code duration defaults to 50us + led_encoder->reset_code = (rmt_symbol_word_t) { + .level0 = 0, + .duration0 = reset_ticks, + .level1 = 0, + .duration1 = reset_ticks, + }; + *ret_encoder = &led_encoder->base; + return ESP_OK; +err: + if (led_encoder) { + if (led_encoder->bytes_encoder) { + rmt_del_encoder(led_encoder->bytes_encoder); + } + if (led_encoder->copy_encoder) { + rmt_del_encoder(led_encoder->copy_encoder); + } + free(led_encoder); + } + return ret; +} + + +#define RMT_LED_STRIP_RESOLUTION_HZ 10000000 // 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution) + +#ifndef BLE_MESH_LED_STRIP_IO +#define RMT_LED_STRIP_GPIO_NUM 8 +#else +#define RMT_LED_STRIP_GPIO_NUM BLE_MESH_LED_STRIP_IO +#endif + +#define EXAMPLE_LED_NUMBERS 1 +#define EXAMPLE_CHASE_SPEED_MS 10 + +static uint8_t led_strip_pixels[EXAMPLE_LED_NUMBERS * 3]; +rmt_channel_handle_t led_chan = NULL; +rmt_encoder_handle_t led_encoder = NULL; +rmt_transmit_config_t tx_config; + +void rmt_encoder_init() +{ + led_chan = NULL; + rmt_tx_channel_config_t tx_chan_config = { + .clk_src = RMT_CLK_SRC_DEFAULT, // select source clock + .gpio_num = RMT_LED_STRIP_GPIO_NUM, + .mem_block_symbols = 64, // increase the block size can make the LED less flickering + .resolution_hz = RMT_LED_STRIP_RESOLUTION_HZ, + .trans_queue_depth = 4, // set the number of transactions that can be pending in the background + }; + ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &led_chan)); + + ESP_LOGI(TAG, "Install led strip encoder"); + led_encoder = NULL; + led_strip_encoder_config_t encoder_config = { + .resolution = RMT_LED_STRIP_RESOLUTION_HZ, + }; + ESP_ERROR_CHECK(rmt_new_led_strip_encoder(&encoder_config, &led_encoder)); + + ESP_LOGI(TAG, "Enable RMT TX channel"); + ESP_ERROR_CHECK(rmt_enable(led_chan)); + + tx_config.loop_count = 0; // no transfer loop +} + +void rmt_led_set(uint8_t red, uint8_t green, uint8_t blue) +{ + led_strip_pixels[0] = green; + led_strip_pixels[1] = red; + led_strip_pixels[2] = blue; + + ESP_ERROR_CHECK(rmt_transmit(led_chan, led_encoder, led_strip_pixels, sizeof(led_strip_pixels), &tx_config)); + ESP_ERROR_CHECK(rmt_tx_wait_all_done(led_chan, portMAX_DELAY)); +} diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/README.md b/examples/bluetooth/esp_ble_mesh/directed_forwarding/README.md new file mode 100644 index 000000000000..be11715aa9c2 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/README.md @@ -0,0 +1,56 @@ +| Supported Targets | ESP32 | ESP32-C3 | ESP32-C6 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | + +# Directed Forwarding + +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +This example is primarily intended to demonstrate the newly added directed forwarding feature in Mesh Protocol v1.1. + +## How to use example + +Please refer to this [tutorial](examples/bluetooth/esp_ble_mesh/directed_forwarding/tutorial/BLE_Mesh_Directed_Forwarding_Example_Walkthrough.md) for detailed instructions on how to run it. + + +### Hardware Required +You need prepare at least two ESP series development boards. We recommend using the [ESP32-C3-DevKitM-1](https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/hw-reference/esp32c3/user-guide-devkitm-1.html) or [ESP32-C6-DevKitC-1](https://docs.espressif.com/projects/espressif-esp-dev-kits/en/latest/esp32c6/esp32-c6-devkitc-1/user_guide.html#). Alternatively, you can use other development boards, but ensure that each board has at least one LED and one button for operation. +### Configure the project + +``` +idf.py menuconfig +``` + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(Replace PORT with the name of the serial port to use.) + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output +After all devices have joined the network, you need to press the button on the provisioner(the device which runs df_client) to complete the final setup. Once the setup is complete, provisioner will provide the following output: + +``` +You could click to send message by directed forwarding +After the message handled, you could click again to send message by flooding +``` +After seeing the above output, you can press the button again to send a network message. When the message is sent in directed forwarding, the following output will be displayed: +``` +Will send a message to node(`node unicast`) with DF +``` +When the message is sent in a flooding, the following output will be displayed: +``` +Will send a message to node(`node unicast`) with Flooding +``` +## Troubleshooting + +See common troubleshooting for Directed Forwarding examples from [ESP-BLE-MESH FAQ](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/esp-ble-mesh/ble-mesh-index.html#esp-ble-mesh-faq). + +(For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.) diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/CMakeLists.txt new file mode 100644 index 000000000000..b3ad88ab5a92 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/CMakeLists.txt @@ -0,0 +1,11 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/button + $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/light_driver + $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_init + $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_nvs) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(onoff_client) diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/CMakeLists.txt new file mode 100644 index 000000000000..b1a0bcb76196 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/CMakeLists.txt @@ -0,0 +1,7 @@ +set(srcs "main.c" + "board.c") + +idf_component_register(SRCS "${srcs}" + INCLUDE_DIRS ".") + +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/Kconfig.projbuild b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/Kconfig.projbuild new file mode 100644 index 000000000000..cbab996ec253 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/Kconfig.projbuild @@ -0,0 +1,38 @@ +menu "Example Configuration" + + choice BLE_MESH_EXAMPLE_BOARD + prompt "Board selection for BLE Mesh" + default BLE_MESH_ESP_WROOM_32 if IDF_TARGET_ESP32 + default BLE_MESH_ESP32C3_DEV if IDF_TARGET_ESP32C3 + default BLE_MESH_ESP32S3_DEV if IDF_TARGET_ESP32S3 + default BLE_MESH_ESP32C6_DEV if IDF_TARGET_ESP32C6 + help + Select this option to choose the board for BLE Mesh. The default is ESP32-WROOM-32 + + config BLE_MESH_ESP_WROOM_32 + bool "ESP32-WROOM-32" + depends on IDF_TARGET_ESP32 + + config BLE_MESH_ESP_WROVER + bool "ESP32-WROVER" + depends on IDF_TARGET_ESP32 + + config BLE_MESH_ESP32C3_DEV + bool "ESP32C3-DevKitC" + depends on IDF_TARGET_ESP32C3 + + config BLE_MESH_ESP32S3_DEV + bool "ESP32S3-DevKitC" + depends on IDF_TARGET_ESP32S3 + + config BLE_MESH_ESP32C6_DEV + bool "ESP32C6-DevKitC" + depends on IDF_TARGET_ESP32C6 + + config BLE_MESH_ESP32H2_DEV + bool "ESP32H2-DevKitC" + depends on IDF_TARGET_ESP32H2 + + endchoice + +endmenu diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/board.c b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/board.c new file mode 100644 index 000000000000..266ef381bc4c --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/board.c @@ -0,0 +1,88 @@ +/* board.c - Board-specific hooks */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "driver/gpio.h" +#include "esp_log.h" + +#include "iot_button.h" +#include "board.h" +#include "esp_timer.h" +#include "led_strip_encoder.h" + +#define TAG "BOARD" + +#define BUTTON_ACTIVE_LEVEL 0 + +extern void example_ble_mesh_start_example_configuration(void); +extern void example_ble_mesh_send_gen_onoff_set(bool by_df); +extern uint8_t in_configuration_phase; +esp_timer_handle_t led_timer_hdl; + +void board_led_operation(uint8_t r, uint8_t g, uint8_t b) +{ + rmt_led_set(r,g,b); +} + +static void led_timer_callback(void* arg) +{ + board_led_operation(0,0,0); +} + +static void esp_led_timer_init(void) +{ + const esp_timer_create_args_t led_timer_args = { + .callback = &led_timer_callback, + .arg = NULL, + .name = "led timer", + }; + + ESP_ERROR_CHECK(esp_timer_create(&led_timer_args, &led_timer_hdl)); +} + +void board_led_operation_auto_close(uint8_t r, uint8_t g, uint8_t b, uint32_t ms) +{ + esp_timer_stop(led_timer_hdl); + board_led_operation(r,g,b); + esp_timer_start_once(led_timer_hdl, ms * 1000); +} + + +static void board_led_init(void) +{ + rmt_encoder_init(); + esp_led_timer_init(); +} + +static void button_tap_cb(void* arg) +{ + ESP_LOGI(TAG, "tap cb (%s)", (char *)arg); + static bool use_df = true; + if (in_configuration_phase) { + example_ble_mesh_start_example_configuration(); + } else { + example_ble_mesh_send_gen_onoff_set(use_df); + use_df = !use_df; + } +} + +static void board_button_init(void) +{ + button_handle_t btn_handle = iot_button_create(BUTTON_IO_NUM, BUTTON_ACTIVE_LEVEL); + if (btn_handle) { + iot_button_set_evt_cb(btn_handle, BUTTON_CB_RELEASE, button_tap_cb, "RELEASE"); + } +} + +void board_init(void) +{ + board_led_init(); + board_button_init(); +} diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/board.h b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/board.h new file mode 100644 index 000000000000..9ebec462e4b3 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/board.h @@ -0,0 +1,56 @@ +/* board.h - Board-specific hooks */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BOARD_H_ +#define _BOARD_H_ + +#include "driver/gpio.h" +#include "esp_ble_mesh_defs.h" + +#if defined(CONFIG_BLE_MESH_ESP_WROOM_32) +#define LED_R GPIO_NUM_25 +#define LED_G GPIO_NUM_26 +#define LED_B GPIO_NUM_27 +#elif defined(CONFIG_BLE_MESH_ESP_WROVER) +#define LED_R GPIO_NUM_0 +#define LED_G GPIO_NUM_2 +#define LED_B GPIO_NUM_4 +#elif defined(CONFIG_BLE_MESH_ESP32C3_DEV) +#define LED_R GPIO_NUM_8 +#define LED_G GPIO_NUM_8 +#define LED_B GPIO_NUM_8 +#elif defined(CONFIG_BLE_MESH_ESP32S3_DEV) +#define LED_R GPIO_NUM_47 +#define LED_G GPIO_NUM_47 +#define LED_B GPIO_NUM_47 +#elif defined(CONFIG_BLE_MESH_ESP32C6_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8 +#elif defined(CONFIG_BLE_MESH_ESP32H2_DEV) +#define LED_R GPIO_NUM_8 +#define LED_G GPIO_NUM_8 +#define LED_B GPIO_NUM_8 +#endif + +#define LED_ON 1 +#define LED_OFF 0 + +#define BUTTON_IO_NUM GPIO_NUM_9 + +struct _led_state { + uint8_t current; + uint8_t previous; + uint8_t pin; + char *name; +}; + +void board_led_operation(uint8_t r, uint8_t g, uint8_t b); + +void board_init(void); + +#endif diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/main.c b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/main.c new file mode 100644 index 000000000000..ba81abd71aa6 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/main/main.c @@ -0,0 +1,916 @@ +/* main.c - Application main entry point */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#include "esp_log.h" +#include "esp_random.h" +#include "nvs_flash.h" +#include "esp_bt.h" +#include "esp_mac.h" +#include "foundation.h" + +#include "esp_ble_mesh_defs.h" +#include "esp_ble_mesh_common_api.h" +#include "esp_ble_mesh_provisioning_api.h" +#include "esp_ble_mesh_networking_api.h" +#include "esp_ble_mesh_config_model_api.h" +#include "esp_ble_mesh_generic_model_api.h" +#include "esp_ble_mesh_local_data_operation_api.h" +#include "esp_ble_mesh_df_model_api.h" + +#include "ble_mesh_example_init.h" +#include "board.h" + +#define TAG "EXAMPLE" + +#define CID_ESP 0x02E5 + +#define PROV_OWN_ADDR 0x0001 + +#define MSG_SEND_TTL 3 +#define MSG_TIMEOUT 0 +#define MSG_ROLE ROLE_PROVISIONER + +#define COMP_DATA_PAGE_0 0x00 + +#define APP_KEY_IDX 0x0000 +#define APP_KEY_OCTET 0x12 + +#define ESP_BLE_MESH_VND_MODEL_ID_CLIENT 0x0000 +#define ESP_BLE_MESH_VND_MODEL_ID_SERVER 0x0001 + +#define ESP_BLE_MESH_VND_MODEL_OP_SET ESP_BLE_MESH_MODEL_OP_3(0x00, CID_ESP) +#define ESP_BLE_MESH_VND_MODEL_OP_STATUS ESP_BLE_MESH_MODEL_OP_3(0x01, CID_ESP) + +static uint8_t dev_uuid[16]; + +typedef struct { + uint8_t uuid[16]; + uint16_t unicast; + uint8_t elem_num; + uint8_t onoff; + uint8_t tid; +} esp_ble_mesh_node_info_t; + +static esp_ble_mesh_node_info_t nodes[CONFIG_BLE_MESH_MAX_PROV_NODES] = { + [0 ... (CONFIG_BLE_MESH_MAX_PROV_NODES - 1)] = { + .unicast = ESP_BLE_MESH_ADDR_UNASSIGNED, + .elem_num = 0, + .onoff = LED_OFF, + } +}; + +static uint16_t last_node_idx; +static uint16_t curr_node_idx; +uint8_t in_configuration_phase = 1; + +static struct esp_ble_mesh_key { + uint16_t net_idx; + uint16_t app_idx; + uint8_t app_key[16]; +} prov_key; + +static esp_ble_mesh_client_t config_client; +static esp_ble_mesh_client_t onoff_client; +#if CONFIG_BLE_MESH_DF_CLI +static esp_ble_mesh_client_t directed_forwarding_client; +#endif + +static esp_ble_mesh_cfg_srv_t config_server = { + .relay = ESP_BLE_MESH_RELAY_DISABLED, + .beacon = ESP_BLE_MESH_BEACON_ENABLED, +#if defined(CONFIG_BLE_MESH_FRIEND) + .friend_state = ESP_BLE_MESH_FRIEND_ENABLED, +#else + .friend_state = ESP_BLE_MESH_FRIEND_NOT_SUPPORTED, +#endif +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_ENABLED, +#else + .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_NOT_SUPPORTED, +#endif + .default_ttl = 7, + /* 3 transmissions with 20ms interval */ + .net_transmit = ESP_BLE_MESH_TRANSMIT(2, 20), + .relay_retransmit = ESP_BLE_MESH_TRANSMIT(2, 20), +}; + +#if CONFIG_BLE_MESH_DF_SRV +static esp_ble_mesh_df_srv_t directed_forwarding_server = { + .directed_net_transmit = ESP_BLE_MESH_TRANSMIT(1, 100), + .directed_relay_retransmit = ESP_BLE_MESH_TRANSMIT(2, 100), + .default_rssi_threshold = (-80), + .rssi_margin = 20, + .directed_node_paths = 20, + .directed_relay_paths = 20, +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + .directed_proxy_paths = 20, +#else + .directed_proxy_paths = 0, +#endif +#if defined(CONFIG_BLE_MESH_FRIEND) + .directed_friend_paths = 20, +#else + .directed_friend_paths = 0, +#endif + .path_monitor_interval = 120, + .path_disc_retry_interval = 300, + .path_disc_interval = ESP_BLE_MESH_PATH_DISC_INTERVAL_30_SEC, + .lane_disc_guard_interval = ESP_BLE_MESH_LANE_DISC_GUARD_INTERVAL_10_SEC, + .directed_ctl_net_transmit = ESP_BLE_MESH_TRANSMIT(1, 100), + .directed_ctl_relay_retransmit = ESP_BLE_MESH_TRANSMIT(2, 100), +}; +#endif + +static const esp_ble_mesh_client_op_pair_t vnd_op_pair[] = { + { ESP_BLE_MESH_VND_MODEL_OP_SET, ESP_BLE_MESH_VND_MODEL_OP_STATUS }, +}; + +static esp_ble_mesh_client_t vendor_client = { + .op_pair_size = ARRAY_SIZE(vnd_op_pair), + .op_pair = vnd_op_pair, +}; + +static esp_ble_mesh_model_op_t vnd_op[] = { + ESP_BLE_MESH_MODEL_OP(ESP_BLE_MESH_VND_MODEL_OP_STATUS, 2), + ESP_BLE_MESH_MODEL_OP_END, +}; + +static esp_ble_mesh_model_t vnd_models[] = { + ESP_BLE_MESH_VENDOR_MODEL(CID_ESP, ESP_BLE_MESH_VND_MODEL_ID_CLIENT, + vnd_op, NULL, &vendor_client), +}; + +static esp_ble_mesh_model_t root_models[] = { + ESP_BLE_MESH_MODEL_CFG_SRV(&config_server), + ESP_BLE_MESH_MODEL_CFG_CLI(&config_client), +#if CONFIG_BLE_MESH_DF_CLI + ESP_BLE_MESH_MODEL_DF_CLI(&directed_forwarding_client), +#endif +#if CONFIG_BLE_MESH_DF_SRV + ESP_BLE_MESH_MODEL_DF_SRV(&directed_forwarding_server), +#endif + ESP_BLE_MESH_MODEL_GEN_ONOFF_CLI(NULL, &onoff_client), +}; + +static esp_ble_mesh_elem_t elements[] = { + ESP_BLE_MESH_ELEMENT(0, root_models, vnd_models), +}; + +static esp_ble_mesh_comp_t composition = { + .cid = CID_ESP, + .elements = elements, + .element_count = ARRAY_SIZE(elements), +}; + +static esp_ble_mesh_prov_t provision = { + .prov_uuid = dev_uuid, + .prov_unicast_addr = PROV_OWN_ADDR, + .prov_start_address = 0x0002, + .prov_attention = 0x00, + .prov_algorithm = 0x00, + .prov_pub_key_oob = 0x00, + .prov_static_oob_val = NULL, + .prov_static_oob_len = 0x00, + .flags = 0x00, + .iv_index = 0x00, +}; + +static esp_err_t example_ble_mesh_store_node_info(const uint8_t uuid[16], uint16_t unicast, + uint8_t elem_num, uint8_t onoff_state) +{ + int i; + + if (!uuid || !ESP_BLE_MESH_ADDR_IS_UNICAST(unicast)) { + return ESP_ERR_INVALID_ARG; + } + + if (last_node_idx >= ARRAY_SIZE(nodes)) { + ESP_LOGW(TAG, "Maximum node limit exceeded, Max nodes number is %d", CONFIG_BLE_MESH_MAX_PROV_NODES); + return ESP_ERR_NO_MEM; + } + + /* Judge if the device has been provisioned before */ + for (i = 0; i < last_node_idx; i++) { + if (!memcmp(nodes[i].uuid, uuid, 16)) { + ESP_LOGW(TAG, "%s: reprovisioned device 0x%04x", __func__, unicast); + nodes[i].unicast = unicast; + nodes[i].elem_num = elem_num; + nodes[i].onoff = onoff_state; + return ESP_OK; + } + } + + if (nodes[last_node_idx].unicast == ESP_BLE_MESH_ADDR_UNASSIGNED) { + nodes[last_node_idx].unicast = unicast; + nodes[last_node_idx].elem_num = elem_num; + nodes[last_node_idx].onoff = onoff_state; + memcpy(nodes[i].uuid, uuid, 16); + last_node_idx++; + return ESP_OK; + } + + for (i = ARRAY_SIZE(nodes) - 1; i >= 0; i--) { + if (nodes[i].unicast != ESP_BLE_MESH_ADDR_UNASSIGNED) { + nodes[i + 1].unicast = unicast; + nodes[i + 1].elem_num = elem_num; + nodes[i + 1].onoff = onoff_state; + last_node_idx = i + 2; + return ESP_OK; + } + } + + return ESP_FAIL; +} + +static esp_ble_mesh_node_info_t *example_ble_mesh_get_node_info(uint16_t unicast) +{ + int i; + + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast)) { + return NULL; + } + + for (i = 0; i < last_node_idx; i++) { + if (nodes[i].unicast <= unicast && + nodes[i].unicast + nodes[i].elem_num > unicast) { + return &nodes[i]; + } + } + + return NULL; +} + +static esp_err_t example_ble_mesh_set_msg_common(esp_ble_mesh_client_common_param_t *common, + esp_ble_mesh_node_info_t *node, + esp_ble_mesh_model_t *model, uint32_t opcode) +{ + if (!common || !node || !model) { + return ESP_ERR_INVALID_ARG; + } + + common->opcode = opcode; + common->model = model; + common->ctx.net_idx = prov_key.net_idx; + common->ctx.app_idx = prov_key.app_idx; + common->ctx.addr = node->unicast; + common->ctx.send_ttl = MSG_SEND_TTL; + common->msg_timeout = MSG_TIMEOUT; + + return ESP_OK; +} + +void example_ble_mesh_send_directed_forwarding_srv_control_set(esp_ble_mesh_node_info_t *node) +{ + esp_ble_mesh_df_client_set_t set = {0}; + esp_ble_mesh_client_common_param_t param = {0}; + esp_ble_mesh_elem_t *element = NULL; + esp_ble_mesh_model_t *model = NULL; + esp_err_t err = ESP_OK; + + element = esp_ble_mesh_find_element(esp_ble_mesh_get_primary_element_address()); + if (!element) { + ESP_LOGE(TAG, "Element 0x%04x not exists", esp_ble_mesh_get_primary_element_address()); + return; + } + + model = esp_ble_mesh_find_sig_model(element, ESP_BLE_MESH_MODEL_ID_DF_CLI); + if (!model) { + ESP_LOGE(TAG, "Directed Forwarding Client not exists"); + return; + } + + example_ble_mesh_set_msg_common(¶m, node, model, ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_SET); + + set.directed_control_set.net_idx = prov_key.net_idx; + set.directed_control_set.directed_forwarding = ESP_BLE_MESH_DIRECTED_FORWARDING_ENABLED; + set.directed_control_set.directed_relay = ESP_BLE_MESH_DIRECTED_RELAY_ENABLED; + set.directed_control_set.directed_proxy = ESP_BLE_MESH_DIRECTED_PROXY_IGNORE; + set.directed_control_set.directed_proxy_use_default = ESP_BLE_MESH_DIRECTED_PROXY_USE_DEFAULT_IGNORE; + set.directed_control_set.directed_friend = ESP_BLE_MESH_DIRECTED_FRIEND_IGNORE; + + + err = esp_ble_mesh_df_client_set_state(¶m, &set); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send Directed Forwarding Control Set"); + } + + return; +} + +void example_ble_mesh_send_gen_onoff_set(bool by_df) +{ + esp_ble_mesh_generic_client_set_state_t set = {0}; + esp_ble_mesh_client_common_param_t common = {0}; + esp_err_t err = ESP_OK; + + if (!last_node_idx) { + ESP_LOGE(TAG, "No node provisioned"); + return; + } + + curr_node_idx = last_node_idx - 1; + + esp_ble_mesh_node_info_t *node = &nodes[curr_node_idx]; + + example_ble_mesh_set_msg_common(&common, node, onoff_client.model, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET); + + if (by_df) { + common.ctx.send_tag |= ESP_BLE_MESH_TAG_USE_DIRECTED|ESP_BLE_MESH_TAG_IMMUTABLE_CRED; + } else { + common.ctx.send_tag |= ESP_BLE_MESH_TAG_IMMUTABLE_CRED; + } + + ESP_LOGI(TAG, "Will send a message to node(0x%04x) with %s", node->unicast, by_df ? "DF" : "Flooding"); + + set.onoff_set.op_en = false; + set.onoff_set.onoff = node->onoff; + set.onoff_set.tid = node->tid; + node->tid++; + + err = esp_ble_mesh_generic_client_set_state(&common, &set); + if (err) { + ESP_LOGE(TAG, "Send Generic OnOff Set Unack failed"); + return; + } + + node->onoff = !node->onoff; + board_led_operation(0, 100, 0); +} + +void example_ble_mesh_start_example_configuration(void) +{ + esp_ble_mesh_msg_ctx_t ctx = {0}; + uint32_t opcode; + esp_err_t err; + uint8_t address[12]; + + if (last_node_idx < 4) { + ESP_LOGE(TAG, "The network has too few nodes to run this example."); + return; + } + + memcpy(address, nodes[last_node_idx - 3].uuid + 2, 6); + memcpy(address + 6, nodes[last_node_idx - 2].uuid + 2, 6); + + ctx.net_idx = prov_key.net_idx; + ctx.app_idx = prov_key.app_idx; + ctx.addr = nodes[last_node_idx - 1].unicast; + ctx.send_ttl = MSG_SEND_TTL; + opcode = ESP_BLE_MESH_VND_MODEL_OP_SET; + + err = esp_ble_mesh_client_model_send_msg(vendor_client.model, &ctx, opcode, + sizeof(address), (uint8_t *)address, MSG_TIMEOUT, true, MSG_ROLE); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send vendor message 0x%06" PRIx32 "err: 0x%04x", opcode, err); + return; + } + +} + +static esp_err_t prov_complete(int node_idx, const esp_ble_mesh_octet16_t uuid, + uint16_t unicast, uint8_t elem_num, uint16_t net_idx) +{ + char name[11] = {0}; + int err; + + ESP_LOGI(TAG, "node index: 0x%x, unicast address: 0x%02x, element num: %d, netkey index: 0x%02x", + node_idx, unicast, elem_num, net_idx); + ESP_LOGI(TAG, "device uuid: %s", bt_hex(uuid, 16)); + + sprintf(name, "%s%d", "NODE-", node_idx); + err = esp_ble_mesh_provisioner_set_node_name(node_idx, name); + if (err) { + ESP_LOGE(TAG, "%s: Set node name failed", __func__); + return ESP_FAIL; + } + + err = example_ble_mesh_store_node_info(uuid, unicast, elem_num, LED_OFF); + if (err) { + ESP_LOGE(TAG, "%s: Store node info failed", __func__); + return ESP_FAIL; + } + + example_ble_mesh_send_directed_forwarding_srv_control_set(&nodes[last_node_idx - 1]); + return ESP_OK; +} + +static void prov_link_open(esp_ble_mesh_prov_bearer_t bearer) +{ + ESP_LOGI(TAG, "%s link open", bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT"); +} + +static void prov_link_close(esp_ble_mesh_prov_bearer_t bearer, uint8_t reason) +{ + ESP_LOGI(TAG, "%s link close, reason 0x%02x", + bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT", reason); +} + +static void recv_unprov_adv_pkt(uint8_t dev_uuid[16], uint8_t addr[BD_ADDR_LEN], + esp_ble_mesh_addr_type_t addr_type, uint16_t oob_info, + uint8_t adv_type, esp_ble_mesh_prov_bearer_t bearer) +{ + esp_ble_mesh_unprov_dev_add_t add_dev = {0}; + int err; + + /* Due to the API esp_ble_mesh_provisioner_set_dev_uuid_match, Provisioner will only + * use this callback to report the devices, whose device UUID starts with 0xdd & 0xdd, + * to the application layer. + */ + + ESP_LOGI(TAG, "address: %s, address type: %d, adv type: %d", bt_hex(addr, BD_ADDR_LEN), addr_type, adv_type); + ESP_LOGI(TAG, "device uuid: %s", bt_hex(dev_uuid, 16)); + ESP_LOGI(TAG, "oob info: %d, bearer: %s", oob_info, (bearer & ESP_BLE_MESH_PROV_ADV) ? "PB-ADV" : "PB-GATT"); + + memcpy(add_dev.addr, addr, BD_ADDR_LEN); + add_dev.addr_type = (uint8_t)addr_type; + memcpy(add_dev.uuid, dev_uuid, 16); + add_dev.oob_info = oob_info; + add_dev.bearer = (uint8_t)bearer; + /* Note: If unprovisioned device adv packets have not been received, we should not add + device with ADD_DEV_START_PROV_NOW_FLAG set. */ + err = esp_ble_mesh_provisioner_add_unprov_dev(&add_dev, + ADD_DEV_RM_AFTER_PROV_FLAG | ADD_DEV_START_PROV_NOW_FLAG | ADD_DEV_FLUSHABLE_DEV_FLAG); + if (err) { + ESP_LOGE(TAG, "%s: Add unprovisioned device into queue failed", __func__); + } + + board_led_operation(100, 100, 100); + return; +} + +static void example_ble_mesh_provisioning_cb(esp_ble_mesh_prov_cb_event_t event, + esp_ble_mesh_prov_cb_param_t *param) +{ + switch (event) { + case ESP_BLE_MESH_PROVISIONER_PROV_ENABLE_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_PROV_ENABLE_COMP_EVT, err_code %d", param->provisioner_prov_enable_comp.err_code); + break; + case ESP_BLE_MESH_PROVISIONER_PROV_DISABLE_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_PROV_DISABLE_COMP_EVT, err_code %d", param->provisioner_prov_disable_comp.err_code); + break; + case ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT"); + recv_unprov_adv_pkt(param->provisioner_recv_unprov_adv_pkt.dev_uuid, param->provisioner_recv_unprov_adv_pkt.addr, + param->provisioner_recv_unprov_adv_pkt.addr_type, param->provisioner_recv_unprov_adv_pkt.oob_info, + param->provisioner_recv_unprov_adv_pkt.adv_type, param->provisioner_recv_unprov_adv_pkt.bearer); + break; + case ESP_BLE_MESH_PROVISIONER_PROV_LINK_OPEN_EVT: + prov_link_open(param->provisioner_prov_link_open.bearer); + break; + case ESP_BLE_MESH_PROVISIONER_PROV_LINK_CLOSE_EVT: + prov_link_close(param->provisioner_prov_link_close.bearer, param->provisioner_prov_link_close.reason); + break; + case ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT: + prov_complete(param->provisioner_prov_complete.node_idx, param->provisioner_prov_complete.device_uuid, + param->provisioner_prov_complete.unicast_addr, param->provisioner_prov_complete.element_num, + param->provisioner_prov_complete.netkey_idx); + board_led_operation(0, 0, 0); + break; + case ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT, err_code %d", param->provisioner_add_unprov_dev_comp.err_code); + break; + case ESP_BLE_MESH_PROVISIONER_SET_DEV_UUID_MATCH_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_SET_DEV_UUID_MATCH_COMP_EVT, err_code %d", param->provisioner_set_dev_uuid_match_comp.err_code); + break; + case ESP_BLE_MESH_PROVISIONER_SET_NODE_NAME_COMP_EVT: { + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_SET_NODE_NAME_COMP_EVT, err_code %d", param->provisioner_set_node_name_comp.err_code); + if (param->provisioner_set_node_name_comp.err_code == ESP_OK) { + const char *name = NULL; + name = esp_ble_mesh_provisioner_get_node_name(param->provisioner_set_node_name_comp.node_index); + if (!name) { + ESP_LOGE(TAG, "Get node name failed"); + return; + } + ESP_LOGI(TAG, "Node %d name is: %s", param->provisioner_set_node_name_comp.node_index, name); + } + break; + } + case ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT: { + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT, err_code %d", param->provisioner_add_app_key_comp.err_code); + if (param->provisioner_add_app_key_comp.err_code == ESP_OK) { + esp_err_t err = 0; + prov_key.app_idx = param->provisioner_add_app_key_comp.app_idx; + err = esp_ble_mesh_provisioner_bind_app_key_to_local_model(PROV_OWN_ADDR, prov_key.app_idx, + ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI, ESP_BLE_MESH_CID_NVAL); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Provisioner bind local model appkey failed"); + return; + } + err = esp_ble_mesh_provisioner_bind_app_key_to_local_model(PROV_OWN_ADDR, prov_key.app_idx, + ESP_BLE_MESH_VND_MODEL_ID_CLIENT, CID_ESP); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Provisioner bind local model appkey failed"); + return; + } + } + break; + } + case ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT, err_code %d", param->provisioner_bind_app_key_to_model_comp.err_code); + break; + default: + break; + } + + return; +} + +static void example_ble_mesh_config_client_cb(esp_ble_mesh_cfg_client_cb_event_t event, + esp_ble_mesh_cfg_client_cb_param_t *param) +{ + esp_ble_mesh_client_common_param_t common = {0}; + esp_ble_mesh_node_info_t *node = NULL; + uint32_t opcode; + uint16_t addr; + int err; + + opcode = param->params->opcode; + addr = param->params->ctx.addr; + + ESP_LOGI(TAG, "%s, error_code = 0x%02x, event = 0x%02x, addr: 0x%04x, opcode: 0x%04" PRIx32, + __func__, param->error_code, event, param->params->ctx.addr, opcode); + + if (param->error_code) { + ESP_LOGE(TAG, "Send config client message failed, opcode 0x%04" PRIx32, opcode); + return; + } + + node = example_ble_mesh_get_node_info(addr); + if (!node) { + ESP_LOGE(TAG, "%s: Get node info failed", __func__); + return; + } + + switch (event) { + case ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT: + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET: { + ESP_LOGI(TAG, "composition data %s", bt_hex(param->status_cb.comp_data_status.composition_data->data, + param->status_cb.comp_data_status.composition_data->len)); + esp_ble_mesh_cfg_client_set_state_t set_state = {0}; + example_ble_mesh_set_msg_common(&common, node, config_client.model, ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD); + set_state.app_key_add.net_idx = prov_key.net_idx; + set_state.app_key_add.app_idx = prov_key.app_idx; + memcpy(set_state.app_key_add.app_key, prov_key.app_key, 16); + err = esp_ble_mesh_config_client_set_state(&common, &set_state); + if (err) { + ESP_LOGE(TAG, "%s: Config AppKey Add failed", __func__); + return; + } + break; + } + default: + break; + } + break; + case ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT: + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD: { + esp_ble_mesh_cfg_client_set_state_t set_state = {0}; + example_ble_mesh_set_msg_common(&common, node, config_client.model, ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND); + set_state.model_app_bind.element_addr = node->unicast; + set_state.model_app_bind.model_app_idx = prov_key.app_idx; + set_state.model_app_bind.model_id = ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV; + set_state.model_app_bind.company_id = ESP_BLE_MESH_CID_NVAL; + err = esp_ble_mesh_config_client_set_state(&common, &set_state); + if (err) { + ESP_LOGE(TAG, "%s: Config Model App Bind failed", __func__); + return; + } + break; + } + case ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND: { + } + default: + break; + } + break; + case ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT: + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_STATUS: + ESP_LOG_BUFFER_HEX("composition data %s", param->status_cb.comp_data_status.composition_data->data, + param->status_cb.comp_data_status.composition_data->len); + break; + case ESP_BLE_MESH_MODEL_OP_APP_KEY_STATUS: + break; + default: + break; + } + break; + case ESP_BLE_MESH_CFG_CLIENT_TIMEOUT_EVT: + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET: { + esp_ble_mesh_cfg_client_get_state_t get_state = {0}; + example_ble_mesh_set_msg_common(&common, node, config_client.model, ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET); + get_state.comp_data_get.page = COMP_DATA_PAGE_0; + err = esp_ble_mesh_config_client_get_state(&common, &get_state); + if (err) { + ESP_LOGE(TAG, "%s: Config Composition Data Get failed", __func__); + return; + } + break; + } + case ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD: { + esp_ble_mesh_cfg_client_set_state_t set_state = {0}; + example_ble_mesh_set_msg_common(&common, node, config_client.model, ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD); + set_state.app_key_add.net_idx = prov_key.net_idx; + set_state.app_key_add.app_idx = prov_key.app_idx; + memcpy(set_state.app_key_add.app_key, prov_key.app_key, 16); + err = esp_ble_mesh_config_client_set_state(&common, &set_state); + if (err) { + ESP_LOGE(TAG, "%s: Config AppKey Add failed", __func__); + return; + } + break; + } + case ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND: { + esp_ble_mesh_cfg_client_set_state_t set_state = {0}; + example_ble_mesh_set_msg_common(&common, node, config_client.model, ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND); + set_state.model_app_bind.element_addr = node->unicast; + set_state.model_app_bind.model_app_idx = prov_key.app_idx; + set_state.model_app_bind.model_id = ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV; + set_state.model_app_bind.company_id = ESP_BLE_MESH_CID_NVAL; + err = esp_ble_mesh_config_client_set_state(&common, &set_state); + if (err) { + ESP_LOGE(TAG, "%s: Config Model App Bind failed", __func__); + return; + } + break; + } + default: + break; + } + break; + default: + ESP_LOGE(TAG, "Not a config client status message event"); + break; + } +} + +static void example_ble_mesh_generic_client_cb(esp_ble_mesh_generic_client_cb_event_t event, + esp_ble_mesh_generic_client_cb_param_t *param) +{ + esp_ble_mesh_client_common_param_t common = {0}; + esp_ble_mesh_node_info_t *node = NULL; + uint32_t opcode; + uint16_t addr; + int err; + + opcode = param->params->opcode; + addr = param->params->ctx.addr; + + ESP_LOGI(TAG, "%s, error_code = 0x%02x, event = 0x%02x, addr: 0x%04x, opcode: 0x%04" PRIx32, + __func__, param->error_code, event, param->params->ctx.addr, opcode); + + if (param->error_code) { + ESP_LOGE(TAG, "Send generic client message failed, opcode 0x%04" PRIx32, opcode); + return; + } + + node = example_ble_mesh_get_node_info(addr); + if (!node) { + ESP_LOGE(TAG, "%s: Get node info failed", __func__); + return; + } + + switch (event) { + case ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT: + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET: { + node->onoff = param->status_cb.onoff_status.present_onoff; + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET onoff: 0x%02x", node->onoff); + } + default: + break; + } + break; + case ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT: + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET: + node->onoff = param->status_cb.onoff_status.present_onoff; + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET onoff: 0x%02x", node->onoff); + node->onoff = !node->onoff; + board_led_operation(0,0,0); + break; + default: + break; + } + break; + case ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT: + break; + case ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT: + /* If failed to receive the responses, these messages will be resend */ + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET: { + esp_ble_mesh_generic_client_get_state_t get_state = {0}; + example_ble_mesh_set_msg_common(&common, node, onoff_client.model, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET); + err = esp_ble_mesh_generic_client_get_state(&common, &get_state); + if (err) { + ESP_LOGE(TAG, "%s: Generic OnOff Get failed", __func__); + return; + } + break; + } + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET: { + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET TIMEOUT, node addr(0x%04x)", node->unicast); + break; + } + default: + break; + } + break; + default: + ESP_LOGE(TAG, "Not a generic client status message event"); + break; + } +} + +static void example_ble_mesh_custom_model_cb(esp_ble_mesh_model_cb_event_t event, + esp_ble_mesh_model_cb_param_t *param) +{ + switch (event) { + case ESP_BLE_MESH_MODEL_OPERATION_EVT: + if (param->model_operation.opcode == ESP_BLE_MESH_VND_MODEL_OP_STATUS) { + ESP_LOGI(TAG, "Recv 0x06%" PRIx32 "", param->model_operation.opcode); + in_configuration_phase = 0; + ESP_LOGI(TAG, "You could click to send message by directed forwarding"); + ESP_LOGI(TAG, "After the message handled, you could click again to send message by flooding"); + } + break; + case ESP_BLE_MESH_MODEL_SEND_COMP_EVT: + if (param->model_send_comp.err_code) { + ESP_LOGE(TAG, "Failed to send message 0x%06" PRIx32 "err: %04x", param->model_send_comp.opcode, param->model_send_comp.err_code); + break; + } + ESP_LOGI(TAG, "Send 0x%06" PRIx32, param->model_send_comp.opcode); + break; + case ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT: + ESP_LOGI(TAG, "Receive publish message 0x%06" PRIx32, param->client_recv_publish_msg.opcode); + break; + case ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT: + ESP_LOGW(TAG, "Client message 0x%06" PRIx32 " timeout", param->client_send_timeout.opcode); + example_ble_mesh_start_example_configuration(); + break; + default: + break; + } +} + +static void example_ble_mesh_directed_forwarding_client_cb(esp_ble_mesh_df_client_cb_event_t event, + esp_ble_mesh_df_client_cb_param_t *param) +{ + switch (event) { + case ESP_BLE_MESH_DF_CLIENT_RECV_SET_RSP_EVT: + ESP_LOGW(TAG, "Directed Forwarding Set, opcode 0x%04x, from 0x%04x", param->params->opcode, param->params->ctx.addr); + switch (param->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_SET: + if (param->recv.directed_control_status.status == STATUS_SUCCESS) { + ESP_LOGI(TAG, "Enable Directed Forwarding state success"); + } else { + ESP_LOGW(TAG, "Enable Directed Forwarding state fail"); + } + break; + } + break; + case ESP_BLE_MESH_DF_CLIENT_SEND_TIMEOUT_EVT: + ESP_LOGW(TAG, "Directed Forwarding Timeout, opcode 0x%04x, to 0x%04x", param->params->opcode, param->params->ctx.addr); + esp_ble_mesh_node_info_t node = {.unicast = param->params->ctx.addr}; + example_ble_mesh_send_directed_forwarding_srv_control_set(&node); + break; + default: + break; + } +} + +static void example_ble_mesh_directed_forwarding_server_cb(esp_ble_mesh_df_server_cb_event_t event, + esp_ble_mesh_df_server_cb_param_t *param) +{ + esp_ble_mesh_df_server_table_change_t change = {0}; + esp_ble_mesh_uar_t path_origin; + esp_ble_mesh_uar_t path_target; + + if (event == ESP_BLE_MESH_DF_SERVER_TABLE_CHANGE_EVT) { + memcpy(&change, ¶m->value.table_change, sizeof(esp_ble_mesh_df_server_table_change_t)); + + switch (change.action) { + case ESP_BLE_MESH_DF_TABLE_ADD: { + memcpy(&path_origin, &change.df_table_info.df_table_entry_add_remove.path_origin, sizeof(path_origin)); + memcpy(&path_target, &change.df_table_info.df_table_entry_add_remove.path_target, sizeof(path_target)); + ESP_LOGI(TAG, "Established a path from 0x%04x to 0x%04x", path_origin.range_start, path_target.range_start); + } + break; + case ESP_BLE_MESH_DF_TABLE_REMOVE: { + memcpy(&path_origin, &change.df_table_info.df_table_entry_add_remove.path_origin, sizeof(path_origin)); + memcpy(&path_target, &change.df_table_info.df_table_entry_add_remove.path_target, sizeof(path_target)); + ESP_LOGI(TAG, "Remove a path from 0x%04x to 0x%04x", path_origin.range_start, path_target.range_start); + } + break; + default: + ESP_LOGW(TAG, "Unknown action %d", change.action); + } + } + + return; +} + +static esp_err_t ble_mesh_init(void) +{ + uint8_t match[2] = {0xaa, 0x55}; + esp_err_t err = ESP_OK; + + prov_key.net_idx = ESP_BLE_MESH_KEY_PRIMARY; + prov_key.app_idx = APP_KEY_IDX; + memset(prov_key.app_key, APP_KEY_OCTET, sizeof(prov_key.app_key)); + + esp_ble_mesh_register_prov_callback(example_ble_mesh_provisioning_cb); + esp_ble_mesh_register_config_client_callback(example_ble_mesh_config_client_cb); + esp_ble_mesh_register_generic_client_callback(example_ble_mesh_generic_client_cb); + esp_ble_mesh_register_custom_model_callback(example_ble_mesh_custom_model_cb); + esp_ble_mesh_register_df_client_callback(example_ble_mesh_directed_forwarding_client_cb); + esp_ble_mesh_register_df_server_callback(example_ble_mesh_directed_forwarding_server_cb); + + err = esp_ble_mesh_init(&provision, &composition); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize mesh stack (err %d)", err); + return err; + } + + err = esp_ble_mesh_client_model_init(&vnd_models[0]); + if (err) { + ESP_LOGE(TAG, "Failed to initialize vendor client"); + return err; + } + + err = esp_ble_mesh_provisioner_set_dev_uuid_match(match, sizeof(match), 0x0, false); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to set matching device uuid (err %d)", err); + return err; + } + + err = esp_ble_mesh_provisioner_prov_enable(ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to enable mesh provisioner (err %d)", err); + return err; + } + + err = esp_ble_mesh_provisioner_add_local_app_key(prov_key.app_key, prov_key.net_idx, prov_key.app_idx); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to add local AppKey (err %d)", err); + return err; + } + + err = esp_ble_mesh_enable_directed_forwarding(ESP_BLE_MESH_KEY_PRIMARY, ESP_BLE_MESH_DIRECTED_FORWARDING_ENABLED, ESP_BLE_MESH_DIRECTED_RELAY_ENABLED); /* Enable Directed Forwarding on self */ + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to enable directed forwarding(err %d)", err); + return err; + } + + example_ble_mesh_store_node_info(dev_uuid, 0x01, 1, 0); + + ESP_LOGI(TAG, "BLE Mesh Provisioner initialized"); + board_led_operation(0,0,0); + return err; +} + +void app_main(void) +{ + esp_err_t err; + + ESP_LOGI(TAG, "Initializing..."); + + board_init(); + + err = nvs_flash_init(); + if (err == ESP_ERR_NVS_NO_FREE_PAGES) { + ESP_ERROR_CHECK(nvs_flash_erase()); + err = nvs_flash_init(); + } + ESP_ERROR_CHECK(err); + + err = bluetooth_init(); + if (err) { + ESP_LOGE(TAG, "esp32_bluetooth_init failed (err %d)", err); + return; + } + + /* Device uuis will be filled with bd address at dev_uuid + 2 */ + ble_mesh_get_dev_uuid(dev_uuid); + + ESP_LOG_BUFFER_HEX(TAG":Provisioner bd address: ", dev_uuid + 2, BD_ADDR_LEN); + + /* Initialize the Bluetooth Mesh Subsystem */ + err = ble_mesh_init(); + if (err) { + ESP_LOGE(TAG, "Bluetooth mesh init failed (err %d)", err); + } +} diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/sdkconfig.defaults new file mode 100644 index 000000000000..c3310f37bf06 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/sdkconfig.defaults @@ -0,0 +1,23 @@ +# Override some defaults so BT stack is enabled +# by default in this example +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_CTRL_BTDM_MODEM_SLEEP=n +CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# Override some defaults of ESP BLE Mesh +CONFIG_BLE_MESH=y +CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_PROVISIONER=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 +CONFIG_BLE_MESH_DF_CLI=y +CONFIG_BLE_MESH_DF_SRV=y diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/sdkconfig.defaults.esp32c3 b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/sdkconfig.defaults.esp32c3 new file mode 100644 index 000000000000..b64e6068ee95 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/sdkconfig.defaults.esp32c3 @@ -0,0 +1,17 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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/directed_forwarding/df_client/sdkconfig.defaults.esp32c6 b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/sdkconfig.defaults.esp32c6 new file mode 100644 index 000000000000..58ccc4d7a91f --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/sdkconfig.defaults.esp32c6 @@ -0,0 +1,19 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_BT_BLE_50_FEATURES_SUPPORTED=n +CONFIG_BT_LE_50_FEATURE_SUPPORT=n +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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/directed_forwarding/df_client/sdkconfig.defaults.esp32h2 b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/sdkconfig.defaults.esp32h2 new file mode 100644 index 000000000000..eeb1aec96e04 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/sdkconfig.defaults.esp32h2 @@ -0,0 +1,29 @@ +# ESP32H2-specific +##Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_BT_BLE_50_FEATURES_SUPPORTED=n +CONFIG_BT_LE_50_FEATURE_SUPPORT=n + +# 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=3 +CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=3 +CONFIG_BLE_MESH_CFG_CLI=y +CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y +CONFIG_BLE_MESH_PROVISIONER=y +CONFIG_BLE_MESH_SELF_TEST=y +CONFIG_BLE_MESH_TEST_AUTO_ENTER_NETWORK=y +CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM=80 +CONFIG_BLE_MESH_MAX_PROV_NODES=80 + +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# end of ESP32H2-specific diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/sdkconfig.defaults.esp32s3 b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/sdkconfig.defaults.esp32s3 new file mode 100644 index 000000000000..b64e6068ee95 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_client/sdkconfig.defaults.esp32s3 @@ -0,0 +1,17 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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/directed_forwarding/df_server/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/CMakeLists.txt new file mode 100644 index 000000000000..884a4ed7dfb6 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/CMakeLists.txt @@ -0,0 +1,10 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/button + $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/light_driver + $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_init + $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_nvs) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(onoff_server) diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/CMakeLists.txt new file mode 100644 index 000000000000..13a56c69f2d8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/CMakeLists.txt @@ -0,0 +1,5 @@ +set(srcs "main.c" + "board.c") + +idf_component_register(SRCS "${srcs}" + INCLUDE_DIRS ".") diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/Kconfig.projbuild b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/Kconfig.projbuild new file mode 100644 index 000000000000..cbab996ec253 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/Kconfig.projbuild @@ -0,0 +1,38 @@ +menu "Example Configuration" + + choice BLE_MESH_EXAMPLE_BOARD + prompt "Board selection for BLE Mesh" + default BLE_MESH_ESP_WROOM_32 if IDF_TARGET_ESP32 + default BLE_MESH_ESP32C3_DEV if IDF_TARGET_ESP32C3 + default BLE_MESH_ESP32S3_DEV if IDF_TARGET_ESP32S3 + default BLE_MESH_ESP32C6_DEV if IDF_TARGET_ESP32C6 + help + Select this option to choose the board for BLE Mesh. The default is ESP32-WROOM-32 + + config BLE_MESH_ESP_WROOM_32 + bool "ESP32-WROOM-32" + depends on IDF_TARGET_ESP32 + + config BLE_MESH_ESP_WROVER + bool "ESP32-WROVER" + depends on IDF_TARGET_ESP32 + + config BLE_MESH_ESP32C3_DEV + bool "ESP32C3-DevKitC" + depends on IDF_TARGET_ESP32C3 + + config BLE_MESH_ESP32S3_DEV + bool "ESP32S3-DevKitC" + depends on IDF_TARGET_ESP32S3 + + config BLE_MESH_ESP32C6_DEV + bool "ESP32C6-DevKitC" + depends on IDF_TARGET_ESP32C6 + + config BLE_MESH_ESP32H2_DEV + bool "ESP32H2-DevKitC" + depends on IDF_TARGET_ESP32H2 + + endchoice + +endmenu diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/board.c b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/board.c new file mode 100644 index 000000000000..e895db0347fc --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/board.c @@ -0,0 +1,78 @@ +/* board.c - Board-specific hooks */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "driver/gpio.h" +#include "esp_log.h" +#include "board.h" +#include "esp_timer.h" +#include "led_strip_encoder.h" +#include "iot_button.h" + +#include "mesh/adapter.h" + +#define TAG "BOARD" +#define BUTTON_ACTIVE_LEVEL 0 + +esp_timer_handle_t led_timer_hdl; + +void board_led_operation(uint8_t r, uint8_t g, uint8_t b) +{ + rmt_led_set(r,g,b); +} + +static void led_timer_callback(void* arg) +{ + board_led_operation(0,0,0); +} + +static void esp_led_timer_init(void) +{ + const esp_timer_create_args_t led_timer_args = { + .callback = &led_timer_callback, + .arg = NULL, + .name = "led timer", + }; + + ESP_ERROR_CHECK(esp_timer_create(&led_timer_args, &led_timer_hdl)); +} + +void board_led_operation_auto_close(uint8_t r, uint8_t g, uint8_t b, uint32_t ms) +{ + esp_timer_stop(led_timer_hdl); + board_led_operation(r,g,b); + esp_timer_start_once(led_timer_hdl, ms * 1000); +} + +static void board_led_init(void) +{ + rmt_encoder_init(); + esp_led_timer_init(); +} + +static void button_tap_cb(void* arg) +{ + ESP_LOGI(TAG, "tap cb (%s)", (char *)arg); +} + +static void board_button_init(void) +{ + button_handle_t btn_handle = iot_button_create(BUTTON_IO_NUM, BUTTON_ACTIVE_LEVEL); + if (btn_handle) { + iot_button_set_evt_cb(btn_handle, BUTTON_CB_RELEASE, button_tap_cb, "RELEASE"); + } +} + +void board_init(void) +{ + board_led_init(); + board_button_init(); +} diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/board.h b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/board.h new file mode 100644 index 000000000000..d468ce00294c --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/board.h @@ -0,0 +1,47 @@ +/* board.h - Board-specific hooks */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BOARD_H_ +#define _BOARD_H_ + +#include "driver/gpio.h" + +#if defined(CONFIG_BLE_MESH_ESP_WROOM_32) +#define LED_R GPIO_NUM_25 +#define LED_G GPIO_NUM_26 +#define LED_B GPIO_NUM_27 +#elif defined(CONFIG_BLE_MESH_ESP_WROVER) +#define LED_R GPIO_NUM_0 +#define LED_G GPIO_NUM_2 +#define LED_B GPIO_NUM_4 +#elif defined(CONFIG_BLE_MESH_ESP32C3_DEV) +#define LED_R GPIO_NUM_8 +#define LED_G GPIO_NUM_8 +#define LED_B GPIO_NUM_8 +#elif defined(CONFIG_BLE_MESH_ESP32S3_DEV) +#define LED_R GPIO_NUM_47 +#define LED_G GPIO_NUM_47 +#define LED_B GPIO_NUM_47 +#elif defined(CONFIG_BLE_MESH_ESP32C6_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8 +#elif defined(CONFIG_BLE_MESH_ESP32H2_DEV) +#define LED_R GPIO_NUM_8 +#define LED_G GPIO_NUM_8 +#define LED_B GPIO_NUM_8 +#endif + +#define LED_ON 1 +#define LED_OFF 0 +#define BUTTON_IO_NUM GPIO_NUM_9 + +void board_led_operation(uint8_t r, uint8_t g, uint8_t b); +void board_led_operation_auto_close(uint8_t r, uint8_t g, uint8_t b, uint32_t ms); +void board_init(void); + +#endif diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/main.c b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/main.c new file mode 100644 index 000000000000..63c329e6496d --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/main/main.c @@ -0,0 +1,501 @@ +/* main.c - Application main entry point */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#include "esp_log.h" +#include "nvs_flash.h" +#include "esp_bt.h" + +#include "mesh/adapter.h" + +#include "esp_ble_mesh_defs.h" +#include "esp_ble_mesh_common_api.h" +#include "esp_ble_mesh_networking_api.h" +#include "esp_ble_mesh_provisioning_api.h" +#include "esp_ble_mesh_config_model_api.h" +#include "esp_ble_mesh_generic_model_api.h" +#include "esp_ble_mesh_local_data_operation_api.h" +#include "esp_ble_mesh_df_model_api.h" + +#include "board.h" +#include "ble_mesh_example_init.h" + +#define TAG "EXAMPLE" + +#define CID_ESP 0x02E5 +#define APP_KEY_IDX 0x0000 +#define APP_KEY_OCTET 0x12 + +#define ESP_BLE_MESH_VND_MODEL_ID_CLIENT 0x0000 +#define ESP_BLE_MESH_VND_MODEL_ID_SERVER 0x0001 + +#define ESP_BLE_MESH_VND_MODEL_OP_SET ESP_BLE_MESH_MODEL_OP_3(0x00, CID_ESP) +#define ESP_BLE_MESH_VND_MODEL_OP_STATUS ESP_BLE_MESH_MODEL_OP_3(0x01, CID_ESP) + +extern int bt_mesh_test_update_white_list(struct bt_mesh_white_list *wl); +extern int bt_mesh_test_stop_scanning(void); +extern int bt_mesh_test_start_scanning(bool wl_en); + +static uint8_t dev_uuid[16] = { 0xaa, 0x55 }; +static uint8_t app_key[16] = {0}; + +static esp_ble_mesh_cfg_srv_t config_server = { + .relay = ESP_BLE_MESH_RELAY_ENABLED, + .beacon = ESP_BLE_MESH_BEACON_ENABLED, +#if defined(CONFIG_BLE_MESH_FRIEND) + .friend_state = ESP_BLE_MESH_FRIEND_ENABLED, +#else + .friend_state = ESP_BLE_MESH_FRIEND_NOT_SUPPORTED, +#endif +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_ENABLED, +#else + .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_NOT_SUPPORTED, +#endif + .default_ttl = 7, + /* 3 transmissions with 20ms interval */ + .net_transmit = ESP_BLE_MESH_TRANSMIT(2, 20), + .relay_retransmit = ESP_BLE_MESH_TRANSMIT(2, 20), +}; + +#if CONFIG_BLE_MESH_DF_SRV +static esp_ble_mesh_df_srv_t directed_forwarding_server = { + .directed_net_transmit = ESP_BLE_MESH_TRANSMIT(1, 100), + .directed_relay_retransmit = ESP_BLE_MESH_TRANSMIT(2, 100), + .default_rssi_threshold = (-80), + .rssi_margin = 20, + .directed_node_paths = 20, + .directed_relay_paths = 20, +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + .directed_proxy_paths = 20, +#else + .directed_proxy_paths = 0, +#endif +#if defined(CONFIG_BLE_MESH_FRIEND) + .directed_friend_paths = 20, +#else + .directed_friend_paths = 0, +#endif + .path_monitor_interval = 120, + .path_disc_retry_interval = 300, + .path_disc_interval = ESP_BLE_MESH_PATH_DISC_INTERVAL_30_SEC, + .lane_disc_guard_interval = ESP_BLE_MESH_LANE_DISC_GUARD_INTERVAL_10_SEC, + .directed_ctl_net_transmit = ESP_BLE_MESH_TRANSMIT(1, 100), + .directed_ctl_relay_retransmit = ESP_BLE_MESH_TRANSMIT(2, 100), +}; +#endif + +ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_pub_0, 2 + 3, ROLE_NODE); +static esp_ble_mesh_gen_onoff_srv_t onoff_server_0 = { + .rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP, + .rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP, +}; + +static esp_ble_mesh_model_t root_models[] = { + ESP_BLE_MESH_MODEL_CFG_SRV(&config_server), +#if CONFIG_BLE_MESH_DF_SRV + ESP_BLE_MESH_MODEL_DF_SRV(&directed_forwarding_server), +#endif + ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_0, &onoff_server_0), +}; + +static esp_ble_mesh_model_op_t vnd_op[] = { + ESP_BLE_MESH_MODEL_OP(ESP_BLE_MESH_VND_MODEL_OP_SET, 2), + ESP_BLE_MESH_MODEL_OP_END, +}; + +static esp_ble_mesh_model_t vnd_models[] = { + ESP_BLE_MESH_VENDOR_MODEL(CID_ESP, ESP_BLE_MESH_VND_MODEL_ID_SERVER, + vnd_op, NULL, NULL), +}; + +static esp_ble_mesh_elem_t elements[] = { + ESP_BLE_MESH_ELEMENT(0, root_models, vnd_models), +}; + +static esp_ble_mesh_comp_t composition = { + .cid = CID_ESP, + .elements = elements, + .element_count = ARRAY_SIZE(elements), +}; + +/* Disable OOB security for SILabs Android app */ +static esp_ble_mesh_prov_t provision = { + .uuid = dev_uuid, +#if 0 + .output_size = 4, + .output_actions = ESP_BLE_MESH_DISPLAY_NUMBER, + .input_actions = ESP_BLE_MESH_PUSH, + .input_size = 4, +#else + .output_size = 0, + .output_actions = 0, +#endif +}; + +static void prov_complete(uint16_t net_idx, uint16_t addr, uint8_t flags, uint32_t iv_index) +{ + ESP_LOGI(TAG, "net_idx: 0x%04x, addr: 0x%04x", net_idx, addr); + ESP_LOGI(TAG, "flags: 0x%02x, iv_index: 0x%08" PRIx32, flags, iv_index); + esp_ble_mesh_node_add_local_app_key(app_key, net_idx, APP_KEY_IDX); + board_led_operation(0, 0,0); +} + +static void example_change_led_state(esp_ble_mesh_model_t *model, + esp_ble_mesh_msg_ctx_t *ctx, uint8_t onoff) +{ + uint16_t primary_addr = esp_ble_mesh_get_primary_element_address(); + uint8_t elem_count = esp_ble_mesh_get_element_count(); + uint8_t i; + + if (ESP_BLE_MESH_ADDR_IS_UNICAST(ctx->recv_dst)) { + for (i = 0; i < elem_count; i++) { + if (ctx->recv_dst == (primary_addr + i)) { + if (ctx->recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { + if (ctx->recv_cred == ESP_BLE_MESH_DIRECTED_CRED) { + // Receive a message send by directed forwarding. + board_led_operation_auto_close(0,0,100,2000); + } else { + // Receive a message send by flooding. + board_led_operation_auto_close(100,0,0,2000); + } + } + } + } + } else if (ESP_BLE_MESH_ADDR_IS_GROUP(ctx->recv_dst)) { + if (esp_ble_mesh_is_model_subscribed_to_group(model, ctx->recv_dst)) { + if (ctx->recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { + if (ctx->recv_cred == ESP_BLE_MESH_DIRECTED_CRED) { + /* Receive a message send by directed forwarding. */ + board_led_operation_auto_close(0,0,100,2000); + } else { + /* Receive a message send by flooding. */ + board_led_operation_auto_close(100,0,0,2000); + } + } + } + } else if (ctx->recv_dst == 0xFFFF) { + if (ctx->recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { + if (ctx->recv_cred == ESP_BLE_MESH_DIRECTED_CRED) { + /* Receive a message send by directed forwarding. */ + board_led_operation_auto_close(0,0,100,2000); + } else { + /* Receive a message send by flooding. */ + board_led_operation_auto_close(100,0,0,2000); + } + } + } +} + +static void example_handle_gen_onoff_msg(esp_ble_mesh_model_t *model, + esp_ble_mesh_msg_ctx_t *ctx, + esp_ble_mesh_server_recv_gen_onoff_set_t *set) +{ + esp_ble_mesh_gen_onoff_srv_t *srv = model->user_data; + + switch (ctx->recv_op) { + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET: + esp_ble_mesh_server_model_send_msg(model, ctx, + ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS, sizeof(srv->state.onoff), &srv->state.onoff); + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK: + if (set->op_en == false) { + srv->state.onoff = set->onoff; + } else { + /* TODO: Delay and state transition */ + srv->state.onoff = set->onoff; + } + if (ctx->recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { + esp_ble_mesh_server_model_send_msg(model, ctx, + ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS, sizeof(srv->state.onoff), &srv->state.onoff); + } + esp_ble_mesh_model_publish(model, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS, + sizeof(srv->state.onoff), &srv->state.onoff, ROLE_NODE); + example_change_led_state(model, ctx, srv->state.onoff); + break; + default: + break; + } +} + +static void example_ble_mesh_provisioning_cb(esp_ble_mesh_prov_cb_event_t event, + esp_ble_mesh_prov_cb_param_t *param) +{ + switch (event) { + case ESP_BLE_MESH_PROV_REGISTER_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROV_REGISTER_COMP_EVT, err_code %d", param->prov_register_comp.err_code); + break; + case ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT, err_code %d", param->node_prov_enable_comp.err_code); + break; + case ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT, bearer %s", + param->node_prov_link_open.bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT"); + break; + case ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT, bearer %s", + param->node_prov_link_close.bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT"); + break; + case ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT"); + prov_complete(param->node_prov_complete.net_idx, param->node_prov_complete.addr, + param->node_prov_complete.flags, param->node_prov_complete.iv_index); + break; + case ESP_BLE_MESH_NODE_PROV_RESET_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_RESET_EVT"); + break; + case ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT, err_code %d", param->node_set_unprov_dev_name_comp.err_code); + break; + case ESP_BLE_MESH_NODE_ADD_LOCAL_APP_KEY_COMP_EVT:{ + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT, err_code %d", param->node_add_app_key_comp.err_code); + if (param->node_add_app_key_comp.err_code == ESP_OK) { + esp_err_t err = 0; + uint8_t app_idx = param->node_add_app_key_comp.app_idx; + if (app_idx == APP_KEY_IDX) { + err = esp_ble_mesh_node_bind_app_key_to_local_model(esp_ble_mesh_get_primary_element_address(), ESP_BLE_MESH_CID_NVAL, + ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV, APP_KEY_IDX); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Provisioner bind local model appkey failed %d", err); + return; + } + err = esp_ble_mesh_node_bind_app_key_to_local_model(esp_ble_mesh_get_primary_element_address(), CID_ESP, + ESP_BLE_MESH_VND_MODEL_ID_SERVER, APP_KEY_IDX); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Provisioner bind local model appkey failed %d", err); + return; + } + } + } + board_led_operation_auto_close(50,50,50, 1000); + break; + } + + default: + break; + } +} + +static void example_ble_mesh_generic_server_cb(esp_ble_mesh_generic_server_cb_event_t event, + esp_ble_mesh_generic_server_cb_param_t *param) +{ + esp_ble_mesh_gen_onoff_srv_t *srv; + ESP_LOGI(TAG, "event 0x%02x, opcode 0x%04" PRIx32 ", src 0x%04x, dst 0x%04x", + event, param->ctx.recv_op, param->ctx.addr, param->ctx.recv_dst); + + switch (event) { + case ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT"); + if (param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET || + param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK) { + ESP_LOGI(TAG, "onoff 0x%02x", param->value.state_change.onoff_set.onoff); + example_change_led_state(param->model, ¶m->ctx, param->value.state_change.onoff_set.onoff); + } + break; + case ESP_BLE_MESH_GENERIC_SERVER_RECV_GET_MSG_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_GENERIC_SERVER_RECV_GET_MSG_EVT"); + if (param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET) { + srv = param->model->user_data; + ESP_LOGI(TAG, "onoff 0x%02x", srv->state.onoff); + example_handle_gen_onoff_msg(param->model, ¶m->ctx, NULL); + } + break; + case ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT"); + if (param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET || + param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK) { + ESP_LOGI(TAG, "onoff 0x%02x, tid 0x%02x", param->value.set.onoff.onoff, param->value.set.onoff.tid); + if (param->value.set.onoff.op_en) { + ESP_LOGI(TAG, "trans_time 0x%02x, delay 0x%02x", + param->value.set.onoff.trans_time, param->value.set.onoff.delay); + } + example_handle_gen_onoff_msg(param->model, ¶m->ctx, ¶m->value.set.onoff); + } + break; + default: + ESP_LOGE(TAG, "Unknown Generic Server event 0x%02x", event); + break; + } +} + +static void example_ble_mesh_config_server_cb(esp_ble_mesh_cfg_server_cb_event_t event, + esp_ble_mesh_cfg_server_cb_param_t *param) +{ + if (event == ESP_BLE_MESH_CFG_SERVER_STATE_CHANGE_EVT) { + switch (param->ctx.recv_op) { + case ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD: + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD"); + ESP_LOGI(TAG, "net_idx 0x%04x, app_idx 0x%04x", + param->value.state_change.appkey_add.net_idx, + param->value.state_change.appkey_add.app_idx); + ESP_LOG_BUFFER_HEX("AppKey", param->value.state_change.appkey_add.app_key, 16); + break; + case ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND: + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND"); + ESP_LOGI(TAG, "elem_addr 0x%04x, app_idx 0x%04x, cid 0x%04x, mod_id 0x%04x", + param->value.state_change.mod_app_bind.element_addr, + param->value.state_change.mod_app_bind.app_idx, + param->value.state_change.mod_app_bind.company_id, + param->value.state_change.mod_app_bind.model_id); + break; + case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD: + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD"); + ESP_LOGI(TAG, "elem_addr 0x%04x, sub_addr 0x%04x, cid 0x%04x, mod_id 0x%04x", + param->value.state_change.mod_sub_add.element_addr, + param->value.state_change.mod_sub_add.sub_addr, + param->value.state_change.mod_sub_add.company_id, + param->value.state_change.mod_sub_add.model_id); + break; + default: + break; + } + } +} + +static void example_ble_mesh_directed_forwarding_server_cb(esp_ble_mesh_df_server_cb_event_t event, + esp_ble_mesh_df_server_cb_param_t *param) +{ + esp_ble_mesh_df_server_table_change_t change = {0}; + esp_ble_mesh_uar_t path_origin; + esp_ble_mesh_uar_t path_target; + + if (event == ESP_BLE_MESH_DF_SERVER_TABLE_CHANGE_EVT) { + memcpy(&change, ¶m->value.table_change, sizeof(esp_ble_mesh_df_server_table_change_t)); + + switch (change.action) { + case ESP_BLE_MESH_DF_TABLE_ADD: { + memcpy(&path_origin, &change.df_table_info.df_table_entry_add_remove.path_origin, sizeof(path_origin)); + memcpy(&path_target, &change.df_table_info.df_table_entry_add_remove.path_target, sizeof(path_target)); + ESP_LOGI(TAG, "Established a path from 0x%04x to 0x%04x", path_origin.range_start, path_target.range_start); + // board_led_operation(0, 0, 100); + + } + break; + case ESP_BLE_MESH_DF_TABLE_REMOVE: { + memcpy(&path_origin, &change.df_table_info.df_table_entry_add_remove.path_origin, sizeof(path_origin)); + memcpy(&path_target, &change.df_table_info.df_table_entry_add_remove.path_target, sizeof(path_target)); + ESP_LOGI(TAG, "Remove a path from 0x%04x to 0x%04x", path_origin.range_start, path_target.range_start); + board_led_operation(0, 0, 0); + } + break; + default: + ESP_LOGW(TAG, "Unknown action %d", change.action); + } + } + + return; +} + +static void example_ble_mesh_custom_model_cb(esp_ble_mesh_model_cb_event_t event, + esp_ble_mesh_model_cb_param_t *param) +{ + int i; + struct bt_mesh_white_list wl; + // ESP_LOGI(TAG, "event: %04x opcode %06x", event, param->model_operation.opcode); + switch (event) { + case ESP_BLE_MESH_MODEL_OPERATION_EVT: + if (param->model_operation.opcode == ESP_BLE_MESH_VND_MODEL_OP_SET) { + uint8_t *msg = (uint8_t *)param->model_operation.msg; + uint16_t len = param->model_operation.length; + ESP_LOGI(TAG, "recv len %d", len); + esp_err_t err = esp_ble_mesh_server_model_send_msg(&vnd_models[0], + param->model_operation.ctx, ESP_BLE_MESH_VND_MODEL_OP_STATUS, + sizeof(uint16_t), (uint8_t *)&len); + if (err) { + ESP_LOGE(TAG, "Failed to send message 0x%06x", ESP_BLE_MESH_VND_MODEL_OP_STATUS); + } + + wl.add_remove = BLE_MESH_WHITELIST_ADD; + wl.addr_type = BLE_MESH_ADDR_PUBLIC; + wl.update_wl_comp_cb = NULL; + bt_mesh_test_stop_scanning(); + for (i = 0; i < len; i+=6) { + memcpy(wl.remote_bda, msg + i , 6); + bt_mesh_test_update_white_list(&wl); + } + bt_mesh_test_start_scanning(true); + } + break; + case ESP_BLE_MESH_MODEL_SEND_COMP_EVT: + if (param->model_send_comp.err_code) { + ESP_LOGE(TAG, "Failed to send message 0x%06" PRIx32, param->model_send_comp.opcode); + break; + } + ESP_LOGI(TAG, "Send 0x%06" PRIx32, param->model_send_comp.opcode); + break; + default: + break; + } +} + +static esp_err_t ble_mesh_init(void) +{ + esp_err_t err = ESP_OK; + + memset(app_key, APP_KEY_OCTET, sizeof(app_key)); + + esp_ble_mesh_register_prov_callback(example_ble_mesh_provisioning_cb); + esp_ble_mesh_register_config_server_callback(example_ble_mesh_config_server_cb); + esp_ble_mesh_register_generic_server_callback(example_ble_mesh_generic_server_cb); + esp_ble_mesh_register_custom_model_callback(example_ble_mesh_custom_model_cb); + esp_ble_mesh_register_df_server_callback(example_ble_mesh_directed_forwarding_server_cb); + + err = esp_ble_mesh_init(&provision, &composition); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize mesh stack (err %d)", err); + return err; + } + + err = esp_ble_mesh_node_prov_enable(ESP_BLE_MESH_PROV_ADV|ESP_BLE_MESH_PROV_GATT); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to enable mesh node (err %d)", err); + return err; + } + + ESP_LOGI(TAG, "BLE Mesh Node initialized"); + + board_led_operation(0, 0, 0); + + return err; +} + +void app_main(void) +{ + esp_err_t err; + + ESP_LOGI(TAG, "Initializing..."); + + board_init(); + + err = nvs_flash_init(); + if (err == ESP_ERR_NVS_NO_FREE_PAGES) { + ESP_ERROR_CHECK(nvs_flash_erase()); + err = nvs_flash_init(); + } + ESP_ERROR_CHECK(err); + + err = bluetooth_init(); + if (err) { + ESP_LOGE(TAG, "esp32_bluetooth_init failed (err %d)", err); + return; + } + + ble_mesh_get_dev_uuid(dev_uuid); + + ESP_LOG_BUFFER_HEX(TAG":Provisioner bd address: ", dev_uuid + 2, BD_ADDR_LEN); + /* Initialize the Bluetooth Mesh Subsystem */ + err = ble_mesh_init(); + if (err) { + ESP_LOGE(TAG, "Bluetooth mesh init failed (err %d)", err); + } +} diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults new file mode 100644 index 000000000000..b8552a6969f9 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults @@ -0,0 +1,22 @@ +# Override some defaults so BT stack is enabled +# by default in this example +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_CTRL_BTDM_MODEM_SLEEP=n +CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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_SELF_TEST=y +CONFIG_BLE_MESH_TEST_USE_WHITE_LIST=y +CONFIG_BLE_MESH_DF_SRV=y diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults.esp32c3 b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults.esp32c3 new file mode 100644 index 000000000000..ad04a937e2e8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults.esp32c3 @@ -0,0 +1,16 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults.esp32c6 b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults.esp32c6 new file mode 100644 index 000000000000..78299bedf8d8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults.esp32c6 @@ -0,0 +1,18 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_BT_BLE_50_FEATURES_SUPPORTED=n +CONFIG_BT_LE_50_FEATURE_SUPPORT=n +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults.esp32h2 b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults.esp32h2 new file mode 100644 index 000000000000..78299bedf8d8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults.esp32h2 @@ -0,0 +1,18 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_BT_BLE_50_FEATURES_SUPPORTED=n +CONFIG_BT_LE_50_FEATURE_SUPPORT=n +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults.esp32s3 b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults.esp32s3 new file mode 100644 index 000000000000..ad04a937e2e8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/df_server/sdkconfig.defaults.esp32s3 @@ -0,0 +1,16 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/tutorial/BLE_Mesh_Directed_Forwarding_Example_Walkthrough.md b/examples/bluetooth/esp_ble_mesh/directed_forwarding/tutorial/BLE_Mesh_Directed_Forwarding_Example_Walkthrough.md new file mode 100644 index 000000000000..56d16805a8a9 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/directed_forwarding/tutorial/BLE_Mesh_Directed_Forwarding_Example_Walkthrough.md @@ -0,0 +1,318 @@ +# 1. Introduction +## 1.1 Basic Knowledge +### 1.1.1 Data propagation in Bluetooth LE-Mesh v1.0 +In Bluetooth LE-Mesh v1.0, messages in the network are propagated through bounded flooding, where bounded refers to the limited propagation range determined by the TTL (Time to Live) value, and flooding means that every receiving node participating in the message forwarding within the valid TTL range. + +### 1.1.2 Data propagation in Bluetooth LE-Mesh v1.1 +In Bluetooth LE-Mesh v1.1, messages in the network can be propagated through routing. This example demonstrates the Directed Forwarding method. Messages propagated using Directed Forwarding involve only those nodes on the path for forwarding, while other nodes do not participate in the forwarding. + +### 1.1.3 Creating a path +Creating a path in the Espressif IDF Bluetooth LE-Mesh v1.1 protocol stack is straightforward. By marking the `send_tag` as `ESP_BLE_MESH_TAG_USE_DIRECTED` when sending a message, the protocol stack will actively check whether there is an existing path to reach the destination node. If not, the message will be sent using flooding first and then a path to the destination node will be requested. Once the path is successfully created, subsequent messages will be sent using routing. + +### 1.1.4 Learning more about Directed Forwarding +For more information about Directed Forwarding, please refer to the detailed [documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/esp-ble-mesh/ble-mesh-index.html) in the Espressif IDF document. + +## 1.2 Example Description +### 1.2.1 Directory Structure and Explanation +``` +directed_forwarding +├── df_client +│ ├── CMakeLists.txt +│ └── main +│ ├── board.c +│ ├── board.h +│ ├── CMakeLists.txt +│ ├── Kconfig.projbuild +│ └── main.c +├── df_server +│ ├── CMakeLists.txt +│ └── main +│ ├── board.c +│ ├── board.h +│ ├── CMakeLists.txt +│ ├── Kconfig.projbuild +│ └── main.c +├── tutorial +│ ├── images +│ └── BLE_Mesh_Directed_Forwarding_Example_Walkthrough.md +└── README.md +``` +Under the directed_forwarding project, there are two main directories: df Client and df Server. In this example, the df Client assumes the roles of Provisioner, Path Origin, and Generic OnOff Client. The df Server assumes the roles of Generic OnOff Server and Path Target. + +### 1.2.2 Prerequisites +- One device running the `df_client` project. +- One or more devices running the `df_server` project. + +### 1.2.3 Running Phenomenon +The devices running the `df_client` project are referred to as the Provisioner node or Path Origin node in the following description. + +The devices running the `df_server` project are referred to as Nodes. + +When the Provisioner's LED is white, it indicates that it is provisioning a specific Node. When the provisioning process is complete, the LED of the provisioned Node will turn to white, and this process continues until all nodes join the network and the LEDs are white. + +The last node to join the network serves as the Path Target. Once all nodes have joined the network, the button on the Provisioner should be pressed for the first time. The Provisioner will send VendorModel messages to the Path Target node in order to add the bd_address of the second-to-last and third-to-last nodes to the Path Target's receive whitelist. This allows the Path Target to only receive messages from these two nodes, thus avoiding receiving messages from the Path Origin (Provisioner) within the one hop range. + +Once the configuration is complete, the button on the Provisioner should be pressed again for the second time. This will trigger the sending of a Generic Onoff Set message, which is expected to be sent using routing. However, since there is no existing path, it will also trigger a path establishment. After the path is successfully established, the LED on the nodes along the path will change from white to blue. However, the light on the Path Target will be green because there is no established path at the time of message sending. Therefore, the sending method will be changed from routing to flooding by the Mesh stack. Upon pressing the button for the third time, a Generic Onoff Set message will be sent using flooding, and the light on the Path Target will be green. On the fourth button press, a Generic Onoff Set message will be sent using routing, and the light on the Path Target will be blue. + +### 1.2.4 Message Sequence +![Packet interaction](images/directed_forwarding_sequence.png) + +### 1.2.5 Code Explanation +#### 1.2.5.1 Onoff Client +First, declare and register `Directed Forwarding Client` and `Directed Forwarding Server` as shown below: +```c +#if CONFIG_BLE_MESH_DF_CLI +static esp_ble_mesh_client_t directed_forwarding_client; +#endif + +#if CONFIG_BLE_MESH_DF_SRV +static esp_ble_mesh_df_srv_t directed_forwarding_server = { + .directed_net_transmit = ESP_BLE_MESH_TRANSMIT(1, 100), + .directed_relay_retransmit = ESP_BLE_MESH_TRANSMIT(2, 100), + .default_rssi_threshold = (-80), + .rssi_margin = 20, + .directed_node_paths = 20, + .directed_relay_paths = 20, +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + .directed_proxy_paths = 20, +#else + .directed_proxy_paths = 0, +#endif +#if defined(CONFIG_BLE_MESH_FRIEND) + .directed_friend_paths = 20, +#else + .directed_friend_paths = 0, +#endif + .path_monitor_interval = 120, + .path_disc_retry_interval = 300, + .path_disc_interval = ESP_BLE_MESH_PATH_DISC_INTERVAL_30_SEC, + .lane_disc_guard_interval = ESP_BLE_MESH_LANE_DISC_GUARD_INTERVAL_10_SEC, + .directed_ctl_net_transmit = ESP_BLE_MESH_TRANSMIT(1, 100), + .directed_ctl_relay_retransmit = ESP_BLE_MESH_TRANSMIT(2, 100), +}; +#endif + +static esp_ble_mesh_model_t root_models[] = { + ESP_BLE_MESH_MODEL_CFG_SRV(&config_server), + ESP_BLE_MESH_MODEL_CFG_CLI(&config_client), +#if CONFIG_BLE_MESH_DF_CLI + ESP_BLE_MESH_MODEL_DF_CLI(&directed_forwarding_client), +#endif +#if CONFIG_BLE_MESH_DF_SRV + ESP_BLE_MESH_MODEL_DF_SRV(&directed_forwarding_server), +#endif + ESP_BLE_MESH_MODEL_GEN_ONOFF_CLI(NULL, &onoff_client), +}; + +``` +The Directed Forwarding Client is used for configuring parameters of the Directed Forwarding Server. Every routing node must have a Directed Forwarding Server, but it is not necessary to have a Directed Forwarding Client. + +The parameters in the Directed Forwarding Server are as follows: +- The `directed_net_transmit` parameter controls the broadcasting parameters for sending non-control messages using Directed Forwarding technology. In this example, `ESP_BLE_MESH_TRANSMIT(1, 100)` indicates 1 retransmission and an interval of 100 milliseconds for broadcasting. This value is the default and generally should not be changed. +- The `directed_relay_retransmit` parameter controls the broadcasting parameters for forwarding non-control messages using Directed Forwarding technology. In this example, `ESP_BLE_MESH_TRANSMIT(2, 100)` indicates 2 retransmissions and an interval of 100 milliseconds for broadcasting. This value is the default and generally should not be changed. +- The `default_rssi_threshold` and `rssi_margin` are used to control the received RSSI strength of path requests during path establishment. The RSSI strength of the path requests that nodes can accept should be greater than the sum of `default_rssi_threshold` and `rssi_margin`. +- The `directed_node_paths` specifies the minimum number of paths when the node is either the starting or destination node in the path. Its value should be no less than 20. +- The `directed_relay_paths` specifies the minimum number of paths when the node is an intermediate node in the path. Its value should be no less than 20. +- The `directed_proxy_paths` specifies the minimum number of paths when the node acts as a proxy server to assist proxy clients. When proxy servers are not supported, the value should be 0; otherwise, it should be no less than 20. +- The `directed_friend_paths` specifies the minimum number of paths when the node acts as a friend node to assist low-power nodes. When friend nodes are not supported, the value should be 0; otherwise, it should be no less than 20. +- The `path_monitor_interval` specifies the timeout for path monitoring in seconds. +- The `path_disc_retry_interval` specifies the timeout for retrying path discovery when it fails in seconds. +- The `path_disc_interval` specifies the timeout for paths. +- The `lane_disc_guard_interval` specifies the minimum time interval between two path requests. +- The `directed_ctl_net_transmit` parameter controls the broadcasting parameters for sending control messages using Directed Forwarding technology. In this example, `ESP_BLE_MESH_TRANSMIT(1, 100)` indicates 1 retransmission and an interval of 100 milliseconds for broadcasting. This value is the default and generally should not be changed. +- The `directed_ctl_relay_retransmit` parameter controls the broadcasting parameters for forwarding control messages using Directed Forwarding technology. In this example, `ESP_BLE_MESH_TRANSMIT(2, 100)` indicates 2 retransmissions and an interval of 100 milliseconds for broadcasting. This value is the default and generally should not be changed. + +After registering the Directed Forwarding Client and Directed Forwarding Server, their callback functions also need to be registered. Here is an example code: +```c +static esp_err_t ble_mesh_init(void) +{ + // ... + esp_ble_mesh_register_df_client_callback(example_ble_mesh_directed_forwarding_client_cb); + esp_ble_mesh_register_df_server_callback(example_ble_mesh_directed_forwarding_server_cb); + // ... +} +``` +The implementation of the callback function `example_ble_mesh_directed_forwarding_client_cb` is as follows: +```c +static void example_ble_mesh_directed_forwarding_client_cb(esp_ble_mesh_df_client_cb_event_t event, + esp_ble_mesh_df_client_cb_param_t *param) +{ + switch (event) { + case ESP_BLE_MESH_DF_CLIENT_RECV_SET_RSP_EVT: + ESP_LOGW(TAG, "Directed Forwarding Set, opcode 0x%04x, from 0x%04x", param->params->opcode, param->params->ctx.addr); + switch (param->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_SET: + if (param->recv.directed_control_status.status == STATUS_SUCCESS) { + ESP_LOGI(TAG, "Enable Directed Forwarding state success"); + } else { + ESP_LOGW(TAG, "Enable Directed Forwarding state fail"); + } + break; + } + break; + case ESP_BLE_MESH_DF_CLIENT_SEND_TIMEOUT_EVT: + ESP_LOGW(TAG, "Directed Forwarding Timeout, opcode 0x%04x, to 0x%04x", param->params->opcode, param->params->ctx.addr); + esp_ble_mesh_node_info_t node = {.unicast = param->params->ctx.addr}; + example_ble_mesh_send_directed_forwarding_srv_control_set(&node); + break; + default: + break; + } +} +``` +The event `ESP_BLE_MESH_DF_CLIENT_RECV_SET_RSP_EVT` triggers the opcode `ESP_BLE_MESH_MODEL_OP_DIRECTED_CONTROL_SET` within the callback function. This event occurs when the Directed Forwarding Client sends a Directed Forwarding Control Set message to the Directed Forwarding Server and receives a response. The Directed Forwarding functionality is disabled by default on routing nodes, so the Directed Forwarding Client needs to enable this functionality on each routing node, sequentially. + +The implementation of the callback function `example_ble_mesh_directed_forwarding_server_cb` is as follows: +```c +static void example_ble_mesh_directed_forwarding_server_cb(esp_ble_mesh_df_server_cb_event_t event, + esp_ble_mesh_df_server_cb_param_t *param) +{ + esp_ble_mesh_df_server_table_change_t change = {0}; + esp_ble_mesh_uar_t path_origin; + esp_ble_mesh_uar_t path_target; + + if (event == ESP_BLE_MESH_DF_SERVER_TABLE_CHANGE_EVT) { + memcpy(&change, ¶m->value.table_change, sizeof(esp_ble_mesh_df_server_table_change_t)); + + switch (change.action) { + case ESP_BLE_MESH_DF_TABLE_ADD: { + memcpy(&path_origin, &change.df_table_info.df_table_entry_add_remove.path_origin, sizeof(path_origin)); + memcpy(&path_target, &change.df_table_info.df_table_entry_add_remove.path_target, sizeof(path_target)); + ESP_LOGI(TAG, "Established a path from 0x%04x to 0x%04x", path_origin.range_start, path_target.range_start); + } + break; + case ESP_BLE_MESH_DF_TABLE_REMOVE: { + memcpy(&path_origin, &change.df_table_info.df_table_entry_add_remove.path_origin, sizeof(path_origin)); + memcpy(&path_target, &change.df_table_info.df_table_entry_add_remove.path_target, sizeof(path_target)); + ESP_LOGI(TAG, "Remove a path from 0x%04x to 0x%04x", path_origin.range_start, path_target.range_start); + } + break; + default: + ESP_LOGW(TAG, "Unknown action %d", change.action); + } + } + + return; +} +``` +Currently, only the `ESP_BLE_MESH_DF_SERVER_TABLE_CHANGE_EVT` event is available, which contains two actions: `ESP_BLE_MESH_DF_TABLE_ADD` and `ESP_BLE_MESH_DF_TABLE_REMOVE`. When a table entry is added to the routing table (DIRECTED_FORWARDING_TABLE), the `ESP_BLE_MESH_DF_TABLE_ADD` action is triggered, indicating the establishment of a new path. When a table entry is removed from the routing table (DIRECTED_FORWARDING_TABLE), the `ESP_BLE_MESH_DF_TABLE_REMOVE` action is triggered, indicating the termination of a path. + +The `example_ble_mesh_send_gen_onoff_set` function demonstrates how to use Directed Forwarding to send a message. +```c +void example_ble_mesh_send_gen_onoff_set(bool by_df) +{ + esp_ble_mesh_generic_client_set_state_t set = {0}; + esp_ble_mesh_client_common_param_t common = {0}; + esp_err_t err = ESP_OK; + + if (!last_node_idx) { + ESP_LOGE(TAG, "No node provisioned"); + return; + } + + curr_node_idx = last_node_idx - 1; + + esp_ble_mesh_node_info_t *node = &nodes[curr_node_idx]; + + example_ble_mesh_set_msg_common(&common, node, onoff_client.model, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET); + + if (by_df) { + common.ctx.send_tag |= ESP_BLE_MESH_TAG_USE_DIRECTED|ESP_BLE_MESH_TAG_IMMUTABLE_CRED; + } else { + common.ctx.send_tag |= ESP_BLE_MESH_TAG_IMMUTABLE_CRED; + } + + ESP_LOGI(TAG, "Will send a message to node(0x%04x) with %s", node->unicast, by_df ? "DF" : "Flooding"); + + set.onoff_set.op_en = false; + set.onoff_set.onoff = node->onoff; + set.onoff_set.tid = node->tid; + node->tid++; + + err = esp_ble_mesh_generic_client_set_state(&common, &set); + if (err) { + ESP_LOGE(TAG, "Send Generic OnOff Set Unack failed"); + return; + } + + node->onoff = !node->onoff; + board_led_operation(0, 100, 0); +} + +``` +You can choose whether to use Directed Forwarding to send the Generic Onoff Set message before its transmission. Here is the code snippet: +```c + if (by_df) { + common.ctx.send_tag |= ESP_BLE_MESH_TAG_USE_DIRECTED|ESP_BLE_MESH_TAG_IMMUTABLE_CRED; + } else { + common.ctx.send_tag |= ESP_BLE_MESH_TAG_IMMUTABLE_CRED; + } +``` +Only setting `ESP_BLE_MESH_TAG_USE_DIRECTED` in `send_tag` is necessary. Additionally, it is not recommended to set `ESP_BLE_MESH_TAG_IMMUTABLE_CRED` in practical applications, as doing so will prevent the protocol stack from actively selecting the optimal way to transmit the message. In this example, setting this bit is solely for demonstrating the difference between flooding propagation mode and routing propagation mode. + +With this, the message can be broadcasted using routing. + +This message can be broadcasted via the routing method. +#### 1.2.5.2 Onoff Server +At Onoff Server, it is important to know how to determine if a message is sent by Directed Forwarding. The code in the example is as follows: +```c +static void example_change_led_state(esp_ble_mesh_model_t *model, + esp_ble_mesh_msg_ctx_t *ctx, uint8_t onoff) +{ + uint16_t primary_addr = esp_ble_mesh_get_primary_element_address(); + uint8_t elem_count = esp_ble_mesh_get_element_count(); + uint8_t i; + + if (ESP_BLE_MESH_ADDR_IS_UNICAST(ctx->recv_dst)) { + for (i = 0; i < elem_count; i++) { + if (ctx->recv_dst == (primary_addr + i)) { + if (ctx->recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { + if (ctx->recv_cred == ESP_BLE_MESH_DIRECTED_CRED) { + // Receive a message send by directed forwarding. + board_led_operation_auto_close(0,0,100,2000); + } else { + // Receive a message send by flooding. + board_led_operation_auto_close(100,0,0,2000); + } + } + } + } + } else if (ESP_BLE_MESH_ADDR_IS_GROUP(ctx->recv_dst)) { + if (esp_ble_mesh_is_model_subscribed_to_group(model, ctx->recv_dst)) { + if (ctx->recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { + if (ctx->recv_cred == ESP_BLE_MESH_DIRECTED_CRED) { + /* Receive a message send by directed forwarding. */ + board_led_operation_auto_close(0,0,100,2000); + } else { + /* Receive a message send by flooding. */ + board_led_operation_auto_close(100,0,0,2000); + } + } + } + } else if (ctx->recv_dst == 0xFFFF) { + if (ctx->recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { + if (ctx->recv_cred == ESP_BLE_MESH_DIRECTED_CRED) { + /* Receive a message send by directed forwarding. */ + board_led_operation_auto_close(0,0,100,2000); + } else { + /* Receive a message send by flooding. */ + board_led_operation_auto_close(100,0,0,2000); + } + } + } +} +``` +The propagation source of the message can be determined during use by `ctx->recv_cred`. +```c + if (ctx->recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { + if (ctx->recv_cred == ESP_BLE_MESH_DIRECTED_CRED) { + /* Receive a message send by directed forwarding. */ + board_led_operation_auto_close(0,0,100,2000); + } else { + /* Receive a message send by flooding. */ + board_led_operation_auto_close(100,0,0,2000); + } + } +``` + +Similarly, upon receiving `ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET` and replying with `ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS`, there is no restriction on the sending format, so once the path is established, messages are sent using Directed Forwarding. diff --git a/examples/bluetooth/esp_ble_mesh/directed_forwarding/tutorial/images/directed_forwarding_sequence.png b/examples/bluetooth/esp_ble_mesh/directed_forwarding/tutorial/images/directed_forwarding_sequence.png new file mode 100644 index 000000000000..e1f65c6e861d Binary files /dev/null and b/examples/bluetooth/esp_ble_mesh/directed_forwarding/tutorial/images/directed_forwarding_sequence.png differ diff --git a/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_client/main/main.c b/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_client/main/main.c index 55951da892b6..9ac4601074a1 100644 --- a/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_client/main/main.c +++ b/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_client/main/main.c @@ -168,7 +168,6 @@ static void provisioner_prov_complete(int node_index, const uint8_t uuid[16], ui .app_idx = node->app_idx, .dst = node->unicast_addr, .timeout = 0, - .role = ROLE_PROVISIONER, }; esp_ble_mesh_cfg_app_key_add_t add_key = { .net_idx = prov_info.net_idx, @@ -425,7 +424,6 @@ static void example_config_client_callback(esp_ble_mesh_cfg_client_cb_event_t ev .app_idx = node->app_idx, .dst = node->unicast_addr, .timeout = 0, - .role = ROLE_PROVISIONER, }; err = example_send_fast_prov_info_set(fast_prov_client.model, &info, &set); if (err != ESP_OK) { @@ -448,7 +446,6 @@ static void example_config_client_callback(esp_ble_mesh_cfg_client_cb_event_t ev .app_idx = node->app_idx, .dst = node->unicast_addr, .timeout = 0, - .role = ROLE_PROVISIONER, }; esp_ble_mesh_cfg_app_key_add_t add_key = { .net_idx = prov_info.net_idx, diff --git a/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_client/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_client/sdkconfig.defaults index ebb238db047b..87249841a351 100644 --- a/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_client/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_client/sdkconfig.defaults @@ -19,3 +19,5 @@ 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_PARTITION_TABLE_SINGLE_APP_LARGE=y diff --git a/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_client/tutorial/BLE_Mesh_Fast_Prov_Client_Example_Walkthrough.md b/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_client/tutorial/BLE_Mesh_Fast_Prov_Client_Example_Walkthrough.md index d1debcf72d35..d64046d3b704 100644 --- a/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_client/tutorial/BLE_Mesh_Fast_Prov_Client_Example_Walkthrough.md +++ b/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_client/tutorial/BLE_Mesh_Fast_Prov_Client_Example_Walkthrough.md @@ -151,10 +151,8 @@ common.model = model; common.ctx.net_idx = info->net_idx; common.ctx.app_idx = 0x0000; /* not used for config messages */ common.ctx.addr = info->dst; -common.ctx.send_rel = false; common.ctx.send_ttl = 0; common.msg_timeout = info->timeout; -common.msg_role = info->role; return esp_ble_mesh_config_client_set_state(&common, &set); ``` @@ -208,7 +206,6 @@ example_msg_common_info_t info = { .app_idx = node->app_idx, .dst = node->group_addr, .timeout = 0, - .role = ROLE_PROVISIONER, }; err = example_send_generic_onoff_set(cli_model, &info, LED_ON, 0x00, false); if (err != ESP_OK) { diff --git a/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_server/main/main.c b/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_server/main/main.c index ce2135e8cb7d..6fd267b9bf6d 100644 --- a/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_server/main/main.c +++ b/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_server/main/main.c @@ -282,7 +282,6 @@ static void provisioner_prov_complete(int node_idx, const uint8_t uuid[16], uint .app_idx = node->app_idx, .dst = node->unicast_addr, .timeout = 0, - .role = ROLE_FAST_PROV, }; err = example_send_config_appkey_add(config_client.model, &info, NULL); if (err != ESP_OK) { @@ -606,7 +605,6 @@ static void example_ble_mesh_config_client_cb(esp_ble_mesh_cfg_client_cb_event_t .app_idx = node->app_idx, .dst = node->unicast_addr, .timeout = 0, - .role = ROLE_FAST_PROV, }; err = example_send_fast_prov_info_set(fast_prov_client.model, &info, &set); if (err != ESP_OK) { @@ -629,7 +627,6 @@ static void example_ble_mesh_config_client_cb(esp_ble_mesh_cfg_client_cb_event_t .app_idx = node->app_idx, .dst = node->unicast_addr, .timeout = 0, - .role = ROLE_FAST_PROV, }; err = example_send_config_appkey_add(config_client.model, &info, NULL); if (err != ESP_OK) { diff --git a/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_server/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_server/sdkconfig.defaults index c548289c7a80..bd458f2c8faa 100644 --- a/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_server/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_server/sdkconfig.defaults @@ -26,3 +26,5 @@ CONFIG_BLE_MESH_CFG_CLI=y CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y + +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y diff --git a/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_server/tutorial/BLE_Mesh_Fast_Prov_Server_Example_Walkthrough.md b/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_server/tutorial/BLE_Mesh_Fast_Prov_Server_Example_Walkthrough.md index 6474e8da89c6..8dc103fb3854 100644 --- a/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_server/tutorial/BLE_Mesh_Fast_Prov_Server_Example_Walkthrough.md +++ b/examples/bluetooth/esp_ble_mesh/fast_provisioning/fast_prov_server/tutorial/BLE_Mesh_Fast_Prov_Server_Example_Walkthrough.md @@ -371,20 +371,17 @@ The Vendor Client Model calls the `esp_ble_mesh_client_model_send_msg` API to se | `ctx.app_idx` | The AppKey Index for the message encryption | | `ctx.addr` | The address of the destination nodes | | `ctx.send_ttl`| The TTL State, which determines how many times a message can be relayed| -| `ctx.send_rel`| This parameter determines whether the Model will wait for an acknowledgment after sending a message | | `opcode` | The message opcode | | `msg->len` | The length of the `msg->data`| | `msg->data` | The pointer to sent data| | `msg_timeout` | The maximum duration (4000 ms by default) that the Model waits for an acknowledgment. | |`true` | True: an acknowledgement is required; False: no acknowledgement is required | -| `msg_role` | The role of a message (node/provisioner) | ```c esp_ble_mesh_msg_ctx_t ctx = { .net_idx = info->net_idx, .app_idx = info->app_idx, .addr = info->dst, - .send_rel = false, .send_ttl = 0, }; err = esp_ble_mesh_client_model_send_msg(model, &ctx, diff --git a/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_client/main/main.c b/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_client/main/main.c index 398b699fce54..1cc61e115620 100644 --- a/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_client/main/main.c +++ b/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_client/main/main.c @@ -181,9 +181,7 @@ void example_ble_mesh_send_gen_onoff_set(void) common.ctx.app_idx = store.app_idx; common.ctx.addr = 0xFFFF; /* to all nodes */ common.ctx.send_ttl = 3; - common.ctx.send_rel = false; common.msg_timeout = 0; /* 0 indicates that timeout value from menuconfig will be used */ - common.msg_role = ROLE_NODE; set.onoff_set.op_en = false; set.onoff_set.onoff = store.onoff; diff --git a/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_client/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_client/sdkconfig.defaults index 43dadfee98fd..e4e550f41e97 100644 --- a/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_client/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_client/sdkconfig.defaults @@ -17,3 +17,5 @@ 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 + +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y diff --git a/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_client/tutorial/BLE_Mesh_Node_OnOff_Client_Example_Walkthrough.md b/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_client/tutorial/BLE_Mesh_Node_OnOff_Client_Example_Walkthrough.md index 74eb4d5c0605..b8bb33183878 100644 --- a/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_client/tutorial/BLE_Mesh_Node_OnOff_Client_Example_Walkthrough.md +++ b/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_client/tutorial/BLE_Mesh_Node_OnOff_Client_Example_Walkthrough.md @@ -130,9 +130,7 @@ The `esp_ble_mesh_set_msg_common` function is used to set the message controllin | `ctx.app_idx` | The AppKey Index for message encryption | | `ctx.addr` | The address of the destination nodes | | `ctx.send_ttl`| The TTL State, which determines how many times a message will be relayed | -| `ctx.send_rel`| This parameter determines if the Model will wait for an acknowledgement after sending a message | | `msg_timeout` | The maximum time the Model will wait for an acknowledgement | -| `msg_role` | The role of message (node/provisioner) | > Note: > diff --git a/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_server/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_server/sdkconfig.defaults index f285ddba6256..43e38d4b0681 100644 --- a/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_server/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/onoff_models/onoff_server/sdkconfig.defaults @@ -16,3 +16,5 @@ 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_PARTITION_TABLE_SINGLE_APP_LARGE=y diff --git a/examples/bluetooth/esp_ble_mesh/provisioner/main/main.c b/examples/bluetooth/esp_ble_mesh/provisioner/main/main.c index e7581a2a4049..9eb27a655fbf 100644 --- a/examples/bluetooth/esp_ble_mesh/provisioner/main/main.c +++ b/examples/bluetooth/esp_ble_mesh/provisioner/main/main.c @@ -33,7 +33,6 @@ #define PROV_OWN_ADDR 0x0001 #define MSG_SEND_TTL 3 -#define MSG_SEND_REL false #define MSG_TIMEOUT 0 #define MSG_ROLE ROLE_PROVISIONER @@ -181,9 +180,7 @@ static esp_err_t example_ble_mesh_set_msg_common(esp_ble_mesh_client_common_para common->ctx.app_idx = prov_key.app_idx; common->ctx.addr = node->unicast; common->ctx.send_ttl = MSG_SEND_TTL; - common->ctx.send_rel = MSG_SEND_REL; common->msg_timeout = MSG_TIMEOUT; - common->msg_role = MSG_ROLE; return ESP_OK; } diff --git a/examples/bluetooth/esp_ble_mesh/provisioner/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/provisioner/sdkconfig.defaults index e78884543082..70e2c3a10740 100644 --- a/examples/bluetooth/esp_ble_mesh/provisioner/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/provisioner/sdkconfig.defaults @@ -8,6 +8,7 @@ CONFIG_CTRL_BTDM_MODEM_SLEEP=n CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y # Override some defaults of ESP BLE Mesh CONFIG_BLE_MESH=y diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/README.md b/examples/bluetooth/esp_ble_mesh/remote_provisioning/README.md new file mode 100644 index 000000000000..07ceb2afab26 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/README.md @@ -0,0 +1,58 @@ +| Supported Targets | ESP32 | ESP32-C3 | ESP32-C6 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | +# Remote Provisioning + +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +The example is primarily used to demonstrate the new Remote Provisioning feature added in Mesh Protocol v1.1. +## How to use example + +Please refer to this [tutorial](examples/bluetooth/esp_ble_mesh/remote_provisioning/tutorial/BLE_Mesh_Remote_Provisioning_Example_Walkthrough.md) for detailed instructions on how to run it. + +### Hardware Required +You need prepare at least three ESP series development boards. We recommend using the [ESP32-C3-DevKitM-1](https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/hw-reference/esp32c3/user-guide-devkitm-1.html) or [ESP32-C6-DevKitC-1](https://docs.espressif.com/projects/espressif-esp-dev-kits/en/latest/esp32c6/esp32-c6-devkitc-1/user_guide.html#). Alternatively, you can use other development boards, but ensure that each board has at least one LED and one button for operation. +### Configure the project + +``` +idf.py menuconfig +``` + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(Replace PORT with the name of the serial port to use.) + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output +When a device that runs the `rpr_server` joins the network, the device that runs the `rpr_client` will have the following log output: + +``` +The Remote Provisioning Server have been provisioned, You could click button to start remote provisioning +``` +After seeing this output log, you can press the button on the device running `rpr_client` to initiate the Remote Provisioning process. + +After successfully joining the network through Remote Provisioning, the device running `rpr_client` will have the following log output: +``` +Remote Prov Client Prov Complete +Net Idx: `net index` +Node addr: `node address` +Node element num: `element num` +Node UUID: `UUID` +``` +(The content wrapped with ` ` will vary depending on the device you are actually using.) + +Afterwards, you can press the button on the device running `rpr_client` to toggle the LED settings of other devices in the network. + +## Troubleshooting + +See common troubleshooting for Directed Forwarding examples from [ESP-BLE-MESH FAQ](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/esp-ble-mesh/ble-mesh-index.html#esp-ble-mesh-faq). + +(For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.) \ No newline at end of file diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/CMakeLists.txt new file mode 100644 index 000000000000..0f89672c6d15 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/CMakeLists.txt @@ -0,0 +1,11 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/button + $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/light_driver + $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_init + $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_nvs) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(ble_mesh_provisioner) diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/CMakeLists.txt new file mode 100644 index 000000000000..5e9d18952173 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/CMakeLists.txt @@ -0,0 +1,6 @@ +set(srcs "main.c" + "board.c") + +idf_component_register(SRCS "${srcs}" + INCLUDE_DIRS ".") +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/Kconfig.projbuild b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/Kconfig.projbuild new file mode 100644 index 000000000000..cbab996ec253 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/Kconfig.projbuild @@ -0,0 +1,38 @@ +menu "Example Configuration" + + choice BLE_MESH_EXAMPLE_BOARD + prompt "Board selection for BLE Mesh" + default BLE_MESH_ESP_WROOM_32 if IDF_TARGET_ESP32 + default BLE_MESH_ESP32C3_DEV if IDF_TARGET_ESP32C3 + default BLE_MESH_ESP32S3_DEV if IDF_TARGET_ESP32S3 + default BLE_MESH_ESP32C6_DEV if IDF_TARGET_ESP32C6 + help + Select this option to choose the board for BLE Mesh. The default is ESP32-WROOM-32 + + config BLE_MESH_ESP_WROOM_32 + bool "ESP32-WROOM-32" + depends on IDF_TARGET_ESP32 + + config BLE_MESH_ESP_WROVER + bool "ESP32-WROVER" + depends on IDF_TARGET_ESP32 + + config BLE_MESH_ESP32C3_DEV + bool "ESP32C3-DevKitC" + depends on IDF_TARGET_ESP32C3 + + config BLE_MESH_ESP32S3_DEV + bool "ESP32S3-DevKitC" + depends on IDF_TARGET_ESP32S3 + + config BLE_MESH_ESP32C6_DEV + bool "ESP32C6-DevKitC" + depends on IDF_TARGET_ESP32C6 + + config BLE_MESH_ESP32H2_DEV + bool "ESP32H2-DevKitC" + depends on IDF_TARGET_ESP32H2 + + endchoice + +endmenu diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/board.c b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/board.c new file mode 100644 index 000000000000..35d76a885e81 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/board.c @@ -0,0 +1,73 @@ +/* board.c - Board-specific hooks */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "iot_button.h" +#include "driver/gpio.h" +#include "esp_log.h" +#include "board.h" + +#define TAG "BOARD" + +#define BUTTON_ACTIVE_LEVEL 0 + +extern uint8_t click_to_send_onoff_set; +extern void example_ble_mesh_send_gen_onoff_set(uint8_t onoff); +extern void example_ble_mesh_send_remote_provisioning_scan_start(void); + +void board_led_operation(uint8_t r, uint8_t g, uint8_t b) +{ +#ifdef BLE_MESH_LED_STRIP_IO + rmt_led_set(r, g, b); +#else + gpio_set_level(LED_R, r); + gpio_set_level(LED_G, g); + gpio_set_level(LED_B, b); +#endif +} + +static void board_led_init(void) +{ +#ifdef BLE_MESH_LED_STRIP_IO + rmt_encoder_init(); + rmt_led_set(LED_OFF,LED_OFF,LED_OFF); +#else + gpio_set_level(LED_R, LED_OFF); + gpio_set_level(LED_G, LED_OFF); + gpio_set_level(LED_B, LED_OFF); +#endif +} + +static void button_tap_cb(void* arg) +{ + static uint8_t onoff = 1; + ESP_LOGI(TAG, "tap cb (%s)", (char *)arg); + if (click_to_send_onoff_set) { + example_ble_mesh_send_gen_onoff_set(onoff); + onoff = !onoff; + } else { + example_ble_mesh_send_remote_provisioning_scan_start(); + } +} + + +static void board_button_init(void) +{ + button_handle_t btn_handle = iot_button_create(BUTTON_IO_NUM, BUTTON_ACTIVE_LEVEL); + if (btn_handle) { + iot_button_set_evt_cb(btn_handle, BUTTON_CB_RELEASE, button_tap_cb, "RELEASE"); + } +} + +void board_init(void) +{ + board_led_init(); + board_button_init(); +} diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/board.h b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/board.h new file mode 100644 index 000000000000..e754f6d95cce --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/board.h @@ -0,0 +1,51 @@ +/* board.h - Board-specific hooks */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BOARD_H_ +#define _BOARD_H_ + +#include "driver/gpio.h" +#include "led_strip_encoder.h" + +#if defined(CONFIG_BLE_MESH_ESP_WROOM_32) +#define LED_R GPIO_NUM_25 +#define LED_G GPIO_NUM_26 +#define LED_B GPIO_NUM_27 +#elif defined(CONFIG_BLE_MESH_ESP_WROVER) +#define LED_R GPIO_NUM_0 +#define LED_G GPIO_NUM_2 +#define LED_B GPIO_NUM_4 +#elif defined(CONFIG_BLE_MESH_ESP32C3_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8 +#elif defined(CONFIG_BLE_MESH_ESP32S3_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_47 +#elif defined(CONFIG_BLE_MESH_ESP32C6_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8 +#elif defined(CONFIG_BLE_MESH_ESP32H2_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8 +#endif + +#define BUTTON_IO_NUM GPIO_NUM_9 + +#ifndef BLE_MESH_LED_STRIP_IO +#define LED_ON 1 +#else +#define LED_R 0 +#define LED_G 1 +#define LED_B 2 +#define LED_ON 100 +#endif + +#define LED_OFF 0 + +void board_led_operation(uint8_t r, uint8_t g, uint8_t b); + +void board_init(void); + +#endif diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/main.c b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/main.c new file mode 100644 index 000000000000..978f403a2db1 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/main/main.c @@ -0,0 +1,1144 @@ +/* main.c - Application main entry point */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#include "esp_log.h" +#include "nvs_flash.h" + +#include "esp_ble_mesh_defs.h" +#include "esp_ble_mesh_common_api.h" +#include "esp_ble_mesh_provisioning_api.h" +#include "esp_ble_mesh_networking_api.h" +#include "esp_ble_mesh_config_model_api.h" +#include "esp_ble_mesh_generic_model_api.h" +#include "esp_ble_mesh_rpr_model_api.h" + +#include "mesh/adapter.h" +#include "ble_mesh_example_init.h" + +#include "board.h" +#define TAG "EXAMPLE" + +#define CID_ESP 0x02E5 +#define CID_NVAL 0xFFFF + +#define PROV_OWN_ADDR 0x0001 + +#define MSG_SEND_TTL 3 +#define MSG_TIMEOUT 0 +#define MSG_ROLE ROLE_PROVISIONER + +#define COMP_DATA_PAGE_0 0x00 + +#define APP_KEY_IDX 0x0000 +#define APP_KEY_OCTET 0x12 + +#define COMP_DATA_1_OCTET(msg, offset) (msg[offset]) +#define COMP_DATA_2_OCTET(msg, offset) (msg[offset + 1] << 8 | msg[offset]) + +static uint8_t dev_uuid[16]; + +/* That uuid mask used to specify the unprovision device which should be provisioning by remote provisioning */ +/* That uuid mask should be same as the dev_uuid in the ../node/main/main.c#L34 */ +static uint8_t remote_dev_uuid_match[2] = {0x55, 0x55}; +static uint16_t cur_rpr_cli_opcode; +uint8_t click_to_send_onoff_set = 0; +uint8_t message_tid = 0; +static uint16_t remote_rpr_srv_addr = 0; +typedef struct { + uint8_t uuid[16]; + uint16_t unicast; + uint8_t elem_num; + uint8_t onoff; + uint8_t *sig_model_num; + uint8_t *vnd_model_num; + uint16_t **sig_models; + uint32_t **vnd_models; +} esp_ble_mesh_node_info_t; + +static esp_ble_mesh_node_info_t nodes[CONFIG_BLE_MESH_MAX_PROV_NODES] = { + [0 ... (CONFIG_BLE_MESH_MAX_PROV_NODES - 1)] = { + .unicast = ESP_BLE_MESH_ADDR_UNASSIGNED, + .elem_num = 0, + .onoff = LED_OFF, + } +}; + +static struct esp_ble_mesh_key { + uint16_t net_idx; + uint16_t app_idx; + uint8_t app_key[16]; +} prov_key; + +#if CONFIG_BLE_MESH_RPR_CLI +static esp_ble_mesh_client_t remote_prov_client; +#endif +static esp_ble_mesh_client_t config_client; +static esp_ble_mesh_client_t onoff_client; + +static esp_ble_mesh_cfg_srv_t config_server = { + .relay = ESP_BLE_MESH_RELAY_DISABLED, + .beacon = ESP_BLE_MESH_BEACON_ENABLED, +#if defined(CONFIG_BLE_MESH_FRIEND) + .friend_state = ESP_BLE_MESH_FRIEND_ENABLED, +#else + .friend_state = ESP_BLE_MESH_FRIEND_NOT_SUPPORTED, +#endif +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_ENABLED, +#else + .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_NOT_SUPPORTED, +#endif + .default_ttl = 7, + /* 3 transmissions with 20ms interval */ + .net_transmit = ESP_BLE_MESH_TRANSMIT(2, 20), + .relay_retransmit = ESP_BLE_MESH_TRANSMIT(2, 20), +}; + +static esp_ble_mesh_model_t root_models[] = { + ESP_BLE_MESH_MODEL_CFG_SRV(&config_server), + ESP_BLE_MESH_MODEL_CFG_CLI(&config_client), +#if CONFIG_BLE_MESH_RPR_CLI + ESP_BLE_MESH_MODEL_RPR_CLI(&remote_prov_client), +#endif + ESP_BLE_MESH_MODEL_GEN_ONOFF_CLI(NULL, &onoff_client), +}; + +static esp_ble_mesh_elem_t elements[] = { + ESP_BLE_MESH_ELEMENT(0, root_models, ESP_BLE_MESH_MODEL_NONE), +}; + +static esp_ble_mesh_comp_t composition = { + .cid = CID_ESP, + .elements = elements, + .element_count = ARRAY_SIZE(elements), +}; + +static esp_ble_mesh_prov_t provision = { + .prov_uuid = dev_uuid, + .prov_unicast_addr = PROV_OWN_ADDR, + .prov_start_address = 0x0005, + .prov_attention = 0x00, + .prov_algorithm = 0x00, + .prov_pub_key_oob = 0x00, + .prov_static_oob_val = NULL, + .prov_static_oob_len = 0x00, + .flags = 0x00, + .iv_index = 0x00, +}; + +static esp_err_t example_ble_mesh_store_node_info(const uint8_t uuid[16], uint16_t unicast, + uint8_t elem_num, uint8_t onoff_state) +{ + int i; + + if (!uuid || !ESP_BLE_MESH_ADDR_IS_UNICAST(unicast)) { + return ESP_ERR_INVALID_ARG; + } + + /* Judge if the device has been provisioned before */ + for (i = 0; i < ARRAY_SIZE(nodes); i++) { + if (!memcmp(nodes[i].uuid, uuid, 16)) { + ESP_LOGW(TAG, "%s: reprovisioned device 0x%04x", __func__, unicast); + nodes[i].unicast = unicast; + nodes[i].elem_num = elem_num; + nodes[i].onoff = onoff_state; + return ESP_OK; + } + } + + for (i = 0; i < ARRAY_SIZE(nodes); i++) { + if (nodes[i].unicast == ESP_BLE_MESH_ADDR_UNASSIGNED) { + memcpy(nodes[i].uuid, uuid, 16); + nodes[i].unicast = unicast; + nodes[i].elem_num = elem_num; + nodes[i].onoff = onoff_state; + return ESP_OK; + } + } + + return ESP_FAIL; +} + +static esp_ble_mesh_node_info_t *example_ble_mesh_get_node_info(uint16_t unicast) +{ + int i; + + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast)) { + return NULL; + } + + for (i = 0; i < ARRAY_SIZE(nodes); i++) { + if (nodes[i].unicast <= unicast && + nodes[i].unicast + nodes[i].elem_num > unicast) { + return &nodes[i]; + } + } + + return NULL; +} + +static esp_err_t example_ble_mesh_set_msg_common(esp_ble_mesh_client_common_param_t *common, + uint16_t unicast, + esp_ble_mesh_model_t *model, uint32_t opcode) +{ + if (!common || !unicast || !model) { + return ESP_ERR_INVALID_ARG; + } + + common->opcode = opcode; + common->model = model; + common->ctx.net_idx = prov_key.net_idx; + common->ctx.app_idx = prov_key.app_idx; + common->ctx.addr = unicast; + common->ctx.send_ttl = MSG_SEND_TTL; + common->msg_timeout = MSG_TIMEOUT; + + return ESP_OK; +} + +static esp_err_t prov_complete(int node_idx, const esp_ble_mesh_octet16_t uuid, + uint16_t unicast, uint8_t elem_num, uint16_t net_idx) +{ + esp_ble_mesh_client_common_param_t common = {0}; + esp_ble_mesh_cfg_client_get_state_t get_state = {0}; + esp_ble_mesh_node_info_t *node = NULL; + char name[11] = {0}; + int err; + + ESP_LOGI(TAG, "node index: 0x%x, unicast address: 0x%02x, element num: %d, netkey index: 0x%02x", + node_idx, unicast, elem_num, net_idx); + ESP_LOGI(TAG, "device uuid: %s", bt_hex(uuid, 16)); + + sprintf(name, "%s%d", "NODE-", node_idx); + err = esp_ble_mesh_provisioner_set_node_name(node_idx, name); + if (err) { + ESP_LOGE(TAG, "%s: Set node name failed", __func__); + return ESP_FAIL; + } + + err = example_ble_mesh_store_node_info(uuid, unicast, elem_num, LED_OFF); + if (err) { + ESP_LOGE(TAG, "%s: Store node info failed", __func__); + return ESP_FAIL; + } + + node = example_ble_mesh_get_node_info(unicast); + if (!node) { + ESP_LOGE(TAG, "%s: Get node info failed", __func__); + return ESP_FAIL; + } + + ESP_LOGI(TAG, "Provisioning node by common methold"); + ESP_LOGI(TAG, "That node will be act as remote provisioning server to help Provisioner to provisioning another node"); + + example_ble_mesh_set_msg_common(&common, unicast, config_client.model, ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET); + get_state.comp_data_get.page = COMP_DATA_PAGE_0; + err = esp_ble_mesh_config_client_get_state(&common, &get_state); + if (err) { + ESP_LOGE(TAG, "%s: Send config comp data get failed", __func__); + return ESP_FAIL; + } + + return ESP_OK; +} + +static void prov_link_open(esp_ble_mesh_prov_bearer_t bearer) +{ + ESP_LOGI(TAG, "%s link open", bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT"); + board_led_operation(LED_OFF, LED_ON, LED_OFF); +} + +static void prov_link_close(esp_ble_mesh_prov_bearer_t bearer, uint8_t reason) +{ + ESP_LOGI(TAG, "%s link close, reason 0x%02x", + bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT", reason); +} + +static void recv_unprov_adv_pkt(uint8_t dev_uuid[16], uint8_t addr[BD_ADDR_LEN], + esp_ble_mesh_addr_type_t addr_type, uint16_t oob_info, + uint8_t adv_type, esp_ble_mesh_prov_bearer_t bearer) +{ + esp_ble_mesh_unprov_dev_add_t add_dev = {0}; + int err; + + /* Due to the API esp_ble_mesh_provisioner_set_dev_uuid_match, Provisioner will only + * use this callback to report the devices, whose device UUID starts with 0xdd & 0xdd, + * to the application layer. + */ + + ESP_LOGI(TAG, "address: %s, address type: %d, adv type: %d", bt_hex(addr, BD_ADDR_LEN), addr_type, adv_type); + ESP_LOGI(TAG, "device uuid: %s", bt_hex(dev_uuid, 16)); + ESP_LOGI(TAG, "oob info: %d, bearer: %s", oob_info, (bearer & ESP_BLE_MESH_PROV_ADV) ? "PB-ADV" : "PB-GATT"); + + memcpy(add_dev.addr, addr, BD_ADDR_LEN); + add_dev.addr_type = (uint8_t)addr_type; + memcpy(add_dev.uuid, dev_uuid, 16); + add_dev.oob_info = oob_info; + add_dev.bearer = (uint8_t)bearer; + /* Note: If unprovisioned device adv packets have not been received, we should not add + device with ADD_DEV_START_PROV_NOW_FLAG set. */ + err = esp_ble_mesh_provisioner_add_unprov_dev(&add_dev, + ADD_DEV_RM_AFTER_PROV_FLAG | ADD_DEV_START_PROV_NOW_FLAG | ADD_DEV_FLUSHABLE_DEV_FLAG); + if (err) { + ESP_LOGE(TAG, "%s: Add unprovisioned device into queue failed", __func__); + } + + return; +} + +void example_ble_mesh_send_gen_onoff_set(uint8_t onoff) +{ + esp_ble_mesh_generic_client_set_state_t set = {0}; + esp_ble_mesh_client_common_param_t common = {0}; + esp_err_t err = ESP_OK; + + example_ble_mesh_set_msg_common(&common, 0xffff, onoff_client.model, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK); + + set.onoff_set.op_en = false; + set.onoff_set.onoff = onoff; + set.onoff_set.tid = message_tid++; + + err = esp_ble_mesh_generic_client_set_state(&common, &set); + if (err) { + ESP_LOGE(TAG, "Send Generic OnOff Set Unack failed"); + return; + } +} + +void example_ble_mesh_send_remote_provisioning_scan_start(void) +{ + + esp_ble_mesh_client_common_param_t common = {0}; + esp_err_t err = ESP_OK; + + if (!remote_rpr_srv_addr) { + ESP_LOGE(TAG, "No valid remote provisioning server address"); + return; + } + + /* Send a ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET to get the scan status of remote provisioning server */ + example_ble_mesh_set_msg_common(&common, remote_rpr_srv_addr, remote_prov_client.model, ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET); + err = esp_ble_mesh_rpr_client_send(&common, NULL); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send Remote Provisioning Client msg: Scan Get"); + } + cur_rpr_cli_opcode = ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET; +} + + +static void example_ble_mesh_parse_node_comp_data(esp_ble_mesh_node_info_t* node, const uint8_t *data, uint16_t length) +{ + uint16_t cid, pid, vid, crpl, feat; + uint16_t loc, model_id, company_id; + uint8_t nums, numv; + uint16_t offset; + uint8_t seq = 0; + int i; + + if (!node || !data) { + ESP_LOGE(TAG, "Invalid Argument"); + return; + } + + cid = COMP_DATA_2_OCTET(data, 0); + pid = COMP_DATA_2_OCTET(data, 2); + vid = COMP_DATA_2_OCTET(data, 4); + crpl = COMP_DATA_2_OCTET(data, 6); + feat = COMP_DATA_2_OCTET(data, 8); + offset = 10; + + node->sig_model_num = (uint8_t *)calloc(node->elem_num, sizeof(uint8_t)); + if (!node->sig_model_num) { + ESP_LOGW(TAG, "No Free memory to store composition data"); + return; + } + + node->vnd_model_num = (uint8_t *)calloc(node->elem_num, sizeof(uint8_t)); + if (!node->vnd_model_num) { + ESP_LOGW(TAG, "No Free memory to store composition data"); + return; + } + + node->sig_models = (uint16_t **)calloc(node->elem_num, sizeof(uint16_t*)); + if (!node->sig_models) { + ESP_LOGW(TAG, "No Free memory to store composition data"); + return; + } + + node->vnd_models = (uint32_t **)calloc(node->elem_num, sizeof(uint32_t*)); + if (!node->sig_models) { + ESP_LOGW(TAG, "No Free memory to store composition data"); + return; + } + + ESP_LOGI(TAG, "********************** Composition Data Start **********************"); + ESP_LOGI(TAG, "* CID 0x%04x, PID 0x%04x, VID 0x%04x, CRPL 0x%04x, Features 0x%04x *", cid, pid, vid, crpl, feat); + for (; offset < length; ) { + loc = COMP_DATA_2_OCTET(data, offset); + nums = COMP_DATA_1_OCTET(data, offset + 2); + numv = COMP_DATA_1_OCTET(data, offset + 3); + node->sig_model_num[seq] = nums; + node->vnd_model_num[seq] = numv; + + if (nums) { + node->sig_models[seq] = (uint16_t *)calloc(nums, sizeof(uint16_t)); + if (!(node->sig_models[seq])) { + ESP_LOGW(TAG, "No Free memory to store composition data"); + return; + } + } else { + node->sig_models[seq] = NULL; + } + + if (numv) { + node->vnd_models[seq] = (uint32_t *)calloc(numv, sizeof(uint32_t)); + if (!(node->vnd_models[seq])) { + ESP_LOGW(TAG, "No Free memory to store composition data"); + return; + } + } else { + node->vnd_models[seq] = NULL; + } + + offset += 4; + ESP_LOGI(TAG, "* Loc 0x%04x, NumS 0x%02x, NumV 0x%02x *", loc, nums, numv); + for (i = 0; i < nums; i++) { + model_id = COMP_DATA_2_OCTET(data, offset); + node->sig_models[seq][i] = model_id; + ESP_LOGI(TAG, "* SIG Model ID 0x%04x *", model_id); + offset += 2; + } + for (i = 0; i < numv; i++) { + company_id = COMP_DATA_2_OCTET(data, offset); + model_id = COMP_DATA_2_OCTET(data, offset + 2); + node->vnd_models[seq][i] = company_id << 16 | model_id; + ESP_LOGI(TAG, "* Vendor Model ID 0x%04x, Company ID 0x%04x *", model_id, company_id); + offset += 4; + } + seq++; + } + ESP_LOGI(TAG, "*********************** Composition Data End ***********************"); +} + +static bool example_ble_mesh_query_element_have_model(uint16_t elem_addr, uint16_t model_id, uint16_t company_id) +{ + esp_ble_mesh_node_info_t *node = NULL; + uint8_t elem_idx = 0; + uint8_t model_num = 0; + int i = 0; + + node = example_ble_mesh_get_node_info(elem_addr); + + elem_idx = elem_addr - node->unicast; + + if (company_id == CID_NVAL) { + model_num = node->sig_model_num[elem_idx]; + for (i = 0; i < model_num; i++) { + if (node->sig_models[elem_idx][i] == model_id) { + return true; + } + } + } else { + model_num = node->vnd_model_num[elem_idx]; + for (i = 0; i < model_num; i++) { + if (node->vnd_models[elem_idx][i] == (company_id << 16 | model_id)) { + return true; + } + } + } + + return false; +} + +static void example_ble_mesh_provisioning_cb(esp_ble_mesh_prov_cb_event_t event, + esp_ble_mesh_prov_cb_param_t *param) +{ + switch (event) { + case ESP_BLE_MESH_PROVISIONER_PROV_ENABLE_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_PROV_ENABLE_COMP_EVT, err_code %d", param->provisioner_prov_enable_comp.err_code); + break; + case ESP_BLE_MESH_PROVISIONER_PROV_DISABLE_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_PROV_DISABLE_COMP_EVT, err_code %d", param->provisioner_prov_disable_comp.err_code); + break; + case ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT"); + recv_unprov_adv_pkt(param->provisioner_recv_unprov_adv_pkt.dev_uuid, param->provisioner_recv_unprov_adv_pkt.addr, + param->provisioner_recv_unprov_adv_pkt.addr_type, param->provisioner_recv_unprov_adv_pkt.oob_info, + param->provisioner_recv_unprov_adv_pkt.adv_type, param->provisioner_recv_unprov_adv_pkt.bearer); + break; + case ESP_BLE_MESH_PROVISIONER_PROV_LINK_OPEN_EVT: + prov_link_open(param->provisioner_prov_link_open.bearer); + break; + case ESP_BLE_MESH_PROVISIONER_PROV_LINK_CLOSE_EVT: + prov_link_close(param->provisioner_prov_link_close.bearer, param->provisioner_prov_link_close.reason); + break; + case ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT: + prov_complete(param->provisioner_prov_complete.node_idx, param->provisioner_prov_complete.device_uuid, + param->provisioner_prov_complete.unicast_addr, param->provisioner_prov_complete.element_num, + param->provisioner_prov_complete.netkey_idx); + break; + case ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT, err_code %d", param->provisioner_add_unprov_dev_comp.err_code); + break; + case ESP_BLE_MESH_PROVISIONER_SET_DEV_UUID_MATCH_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_SET_DEV_UUID_MATCH_COMP_EVT, err_code %d", param->provisioner_set_dev_uuid_match_comp.err_code); + break; + case ESP_BLE_MESH_PROVISIONER_SET_NODE_NAME_COMP_EVT: { + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_SET_NODE_NAME_COMP_EVT, err_code %d", param->provisioner_set_node_name_comp.err_code); + if (param->provisioner_set_node_name_comp.err_code == ESP_OK) { + const char *name = NULL; + name = esp_ble_mesh_provisioner_get_node_name(param->provisioner_set_node_name_comp.node_index); + if (!name) { + ESP_LOGE(TAG, "Get node name failed"); + return; + } + ESP_LOGI(TAG, "Node %d name is: %s", param->provisioner_set_node_name_comp.node_index, name); + } + break; + } + case ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT: { + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT, err_code %d", param->provisioner_add_app_key_comp.err_code); + if (param->provisioner_add_app_key_comp.err_code == ESP_OK) { + esp_err_t err = 0; + prov_key.app_idx = param->provisioner_add_app_key_comp.app_idx; + err = esp_ble_mesh_provisioner_bind_app_key_to_local_model(PROV_OWN_ADDR, prov_key.app_idx, + ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI, ESP_BLE_MESH_CID_NVAL); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Provisioner bind local model appkey failed"); + return; + } + } + break; + } + case ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT, err_code %d", param->provisioner_bind_app_key_to_model_comp.err_code); + break; + default: + break; + } + + return; +} + +static void example_ble_mesh_config_client_cb(esp_ble_mesh_cfg_client_cb_event_t event, + esp_ble_mesh_cfg_client_cb_param_t *param) +{ + esp_ble_mesh_client_common_param_t common = {0}; + esp_ble_mesh_node_info_t *node = NULL; + uint32_t opcode; + uint16_t addr; + int err; + + opcode = param->params->opcode; + addr = param->params->ctx.addr; + + ESP_LOGI(TAG, "%s, error_code = 0x%02x, event = 0x%02x, addr: 0x%04x, opcode: 0x%04" PRIx32, + __func__, param->error_code, event, param->params->ctx.addr, opcode); + + if (param->error_code) { + ESP_LOGE(TAG, "Send config client message failed, opcode 0x%04" PRIx32, opcode); + return; + } + + node = example_ble_mesh_get_node_info(addr); + if (!node) { + ESP_LOGE(TAG, "%s: Get node info failed", __func__); + return; + } + + switch (event) { + case ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT: + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET: { + ESP_LOGI(TAG, "composition data %s", bt_hex(param->status_cb.comp_data_status.composition_data->data, + param->status_cb.comp_data_status.composition_data->len)); + example_ble_mesh_parse_node_comp_data(node, param->status_cb.comp_data_status.composition_data->data, + param->status_cb.comp_data_status.composition_data->len); + esp_ble_mesh_cfg_client_set_state_t set_state = {0}; + example_ble_mesh_set_msg_common(&common, addr, config_client.model, ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD); + set_state.app_key_add.net_idx = prov_key.net_idx; + set_state.app_key_add.app_idx = prov_key.app_idx; + memcpy(set_state.app_key_add.app_key, prov_key.app_key, 16); + err = esp_ble_mesh_config_client_set_state(&common, &set_state); + if (err) { + ESP_LOGE(TAG, "%s: Config AppKey Add failed", __func__); + return; + } + break; + } + default: + break; + } + break; + case ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT: + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD: { + esp_ble_mesh_cfg_client_set_state_t set_state = {0}; + + if (!example_ble_mesh_query_element_have_model(addr, ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV, CID_NVAL)) { + ESP_LOGE(TAG, "Element (addr: 0x%04x) does not support onoff srv model"); + return; + } + + example_ble_mesh_set_msg_common(&common, addr, config_client.model, ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND); + set_state.model_app_bind.element_addr = node->unicast; + set_state.model_app_bind.model_app_idx = prov_key.app_idx; + set_state.model_app_bind.model_id = ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV; + set_state.model_app_bind.company_id = ESP_BLE_MESH_CID_NVAL; + err = esp_ble_mesh_config_client_set_state(&common, &set_state); + if (err) { + ESP_LOGE(TAG, "%s: Config Model App Bind failed", __func__); + return; + } + break; + } + case ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND: { + + if (!example_ble_mesh_query_element_have_model(addr, ESP_BLE_MESH_MODEL_ID_RPR_SRV, CID_NVAL)) { + if (remote_rpr_srv_addr) { + ESP_LOGI(TAG, "The last node have been provisioned, You could click button to send Generic Onoff Set"); + click_to_send_onoff_set = 1; + } else { + ESP_LOGE(TAG, "Element (addr: 0x%04x) does not support remote provisioning srv model"); + } + } else { + click_to_send_onoff_set = 0; + ESP_LOGI(TAG, "The Remote Provisioning Server have been provisioned, You could click button to start remote provisioning"); + remote_rpr_srv_addr = addr; + } + board_led_operation(LED_OFF, LED_OFF, LED_OFF); + break; + } + default: + break; + } + break; + case ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT: + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_STATUS: + ESP_LOG_BUFFER_HEX("composition data %s", param->status_cb.comp_data_status.composition_data->data, + param->status_cb.comp_data_status.composition_data->len); + break; + case ESP_BLE_MESH_MODEL_OP_APP_KEY_STATUS: + break; + default: + break; + } + break; + case ESP_BLE_MESH_CFG_CLIENT_TIMEOUT_EVT: + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET: { + esp_ble_mesh_cfg_client_get_state_t get_state = {0}; + example_ble_mesh_set_msg_common(&common, addr, config_client.model, ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET); + get_state.comp_data_get.page = COMP_DATA_PAGE_0; + err = esp_ble_mesh_config_client_get_state(&common, &get_state); + if (err) { + ESP_LOGE(TAG, "%s: Config Composition Data Get failed", __func__); + return; + } + break; + } + case ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD: { + esp_ble_mesh_cfg_client_set_state_t set_state = {0}; + example_ble_mesh_set_msg_common(&common, addr, config_client.model, ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD); + set_state.app_key_add.net_idx = prov_key.net_idx; + set_state.app_key_add.app_idx = prov_key.app_idx; + memcpy(set_state.app_key_add.app_key, prov_key.app_key, 16); + err = esp_ble_mesh_config_client_set_state(&common, &set_state); + if (err) { + ESP_LOGE(TAG, "%s: Config AppKey Add failed", __func__); + return; + } + break; + } + case ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND: { + esp_ble_mesh_cfg_client_set_state_t set_state = {0}; + example_ble_mesh_set_msg_common(&common, addr, config_client.model, ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND); + set_state.model_app_bind.element_addr = addr; + set_state.model_app_bind.model_app_idx = prov_key.app_idx; + set_state.model_app_bind.model_id = ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV; + set_state.model_app_bind.company_id = ESP_BLE_MESH_CID_NVAL; + err = esp_ble_mesh_config_client_set_state(&common, &set_state); + if (err) { + ESP_LOGE(TAG, "%s: Config Model App Bind failed", __func__); + return; + } + break; + } + default: + break; + } + break; + default: + ESP_LOGE(TAG, "Not a config client status message event"); + break; + } +} + +static void example_ble_mesh_generic_client_cb(esp_ble_mesh_generic_client_cb_event_t event, + esp_ble_mesh_generic_client_cb_param_t *param) +{ + esp_ble_mesh_client_common_param_t common = {0}; + esp_ble_mesh_node_info_t *node = NULL; + uint32_t opcode; + uint16_t addr; + int err; + + opcode = param->params->opcode; + addr = param->params->ctx.addr; + + ESP_LOGI(TAG, "%s, error_code = 0x%02x, event = 0x%02x, addr: 0x%04x, opcode: 0x%04" PRIx32, + __func__, param->error_code, event, param->params->ctx.addr, opcode); + + if (param->error_code) { + ESP_LOGE(TAG, "Send generic client message failed, opcode 0x%04" PRIx32, opcode); + return; + } + + node = example_ble_mesh_get_node_info(addr); + if (!node) { + ESP_LOGE(TAG, "%s: Get node info failed", __func__); + return; + } + + switch (event) { + case ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT: + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET: { + esp_ble_mesh_generic_client_set_state_t set_state = {0}; + node->onoff = param->status_cb.onoff_status.present_onoff; + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET onoff: 0x%02x", node->onoff); + /* After Generic OnOff Status for Generic OnOff Get is received, Generic OnOff Set will be sent */ + example_ble_mesh_set_msg_common(&common, addr, onoff_client.model, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET); + set_state.onoff_set.op_en = false; + set_state.onoff_set.onoff = !node->onoff; + set_state.onoff_set.tid = 0; + err = esp_ble_mesh_generic_client_set_state(&common, &set_state); + if (err) { + ESP_LOGE(TAG, "%s: Generic OnOff Set failed", __func__); + return; + } + break; + } + default: + break; + } + break; + case ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT: + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET: + node->onoff = param->status_cb.onoff_status.present_onoff; + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET onoff: 0x%02x", node->onoff); + break; + default: + break; + } + break; + case ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT: + break; + case ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT: + /* If failed to receive the responses, these messages will be resend */ + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET: { + esp_ble_mesh_generic_client_get_state_t get_state = {0}; + example_ble_mesh_set_msg_common(&common, addr, onoff_client.model, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET); + err = esp_ble_mesh_generic_client_get_state(&common, &get_state); + if (err) { + ESP_LOGE(TAG, "%s: Generic OnOff Get failed", __func__); + return; + } + break; + } + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET: { + esp_ble_mesh_generic_client_set_state_t set_state = {0}; + node->onoff = param->status_cb.onoff_status.present_onoff; + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET onoff: 0x%02x", node->onoff); + example_ble_mesh_set_msg_common(&common, addr, onoff_client.model, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET); + set_state.onoff_set.op_en = false; + set_state.onoff_set.onoff = !node->onoff; + set_state.onoff_set.tid = 0; + err = esp_ble_mesh_generic_client_set_state(&common, &set_state); + if (err) { + ESP_LOGE(TAG, "%s: Generic OnOff Set failed", __func__); + return; + } + break; + } + default: + break; + } + break; + default: + ESP_LOGE(TAG, "Not a generic client status message event"); + break; + } +} + +static void example_ble_mesh_remote_prov_client_callback(esp_ble_mesh_rpr_client_cb_event_t event, + esp_ble_mesh_rpr_client_cb_param_t *param) +{ + static uint8_t remote_dev_uuid[16] = {0}; + esp_ble_mesh_rpr_client_msg_t msg = {0}; + esp_ble_mesh_client_common_param_t common = {0}; + esp_err_t err = ESP_OK; + uint16_t addr = 0; + + switch (event) { + case ESP_BLE_MESH_RPR_CLIENT_SEND_COMP_EVT: + ESP_LOGW(TAG, "Remote Prov Client Send Comp, err_code %d", param->send.err_code); + break; + case ESP_BLE_MESH_RPR_CLIENT_SEND_TIMEOUT_EVT: + ESP_LOGW(TAG, "Remote Prov Client Send Timeout, opcode 0x%04x, to 0x%04x", + param->send.params->opcode, param->send.params->ctx.addr); + break; + case ESP_BLE_MESH_RPR_CLIENT_RECV_PUB_EVT: + case ESP_BLE_MESH_RPR_CLIENT_RECV_RSP_EVT: + ESP_LOGW(TAG, "Remote Prov Client Recv RSP, opcode 0x%04x, from 0x%04x", + param->recv.params->ctx.recv_op, param->recv.params->ctx.addr); + switch (param->recv.params->ctx.recv_op) { + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_CAPS_STATUS: + break; + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_STATUS: + addr = param->recv.params->ctx.addr; + ESP_LOGI(TAG, "scan_status, status 0x%02x", param->recv.val.scan_status.status); + ESP_LOGI(TAG, "scan_status, rpr_scanning 0x%02x", param->recv.val.scan_status.rpr_scanning); + ESP_LOGI(TAG, "scan_status, scan_items_limit 0x%02x", param->recv.val.scan_status.scan_items_limit); + ESP_LOGI(TAG, "scan_status, timeout 0x%02x", param->recv.val.scan_status.timeout); + switch (cur_rpr_cli_opcode) { + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET: { + if (param->recv.val.scan_status.status == ESP_BLE_MESH_RPR_STATUS_SUCCESS) { + switch (param->recv.val.scan_status.rpr_scanning) { + /** + * If the remote provisioning server's scan state is idle, + * that state indicates that remote provisioning server could + * start scan process. + */ + case ESP_BLE_MESH_RPR_SCAN_IDLE: { + err = example_ble_mesh_set_msg_common(&common, addr, remote_prov_client.model, + ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Set message common fail:%d", __LINE__); + return ; + } + + msg.scan_start.scan_items_limit = 0; /* 0 indicates there is no limit for scan items' count */ + msg.scan_start.timeout = 0x0A; /* 0x0A is the default timeout */ + msg.scan_start.uuid_en = 0; /* If uuid enabled, a specify device which have the same uuid will be report */ + /* If uuid disable, any unprovision device all will be report */ + + err = esp_ble_mesh_rpr_client_send(&common, &msg); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send Remote Provisioning Client msg: Scan start"); + } + cur_rpr_cli_opcode = ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START; + break; + } + default: + ESP_LOGW(TAG, "Remote Provisioning Server(addr: 0x%04x) Busy", addr); + break; + } + } else { + ESP_LOGE(TAG, "Remote Provisioning Client Scan Get Fail"); + } + } + break; + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START: { + if (param->recv.val.scan_status.status == ESP_BLE_MESH_RPR_STATUS_SUCCESS) { + ESP_LOGI(TAG, "Start Remote Provisioning Server(addr: 0x%04x) Scan Success", addr); + } else { + ESP_LOGE(TAG, "Remote Provisioning Client Scan Start Fail"); + } + break; + } + default: + ESP_LOGW(TAG, "Unknown Process opcode 0x%04x:%d", cur_rpr_cli_opcode,__LINE__); + break; + } + break; + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_REPORT: + addr = param->recv.params->ctx.addr; + ESP_LOGI(TAG, "scan_report, rssi %ddBm", param->recv.val.scan_report.rssi); + ESP_LOG_BUFFER_HEX(TAG": scan_report, uuid", param->recv.val.scan_report.uuid, 16); + ESP_LOGI(TAG, "scan_report, oob_info 0x%04x", param->recv.val.scan_report.oob_info); + ESP_LOGI(TAG, "scan_report, uri_hash 0x%08x", param->recv.val.scan_report.uri_hash); + + if (param->recv.val.scan_report.uuid[0] != remote_dev_uuid_match[0] || + param->recv.val.scan_report.uuid[1] != remote_dev_uuid_match[1]) { + ESP_LOGI(TAG, "This device is not expect device"); + return; + } + + memcpy(remote_dev_uuid, param->recv.val.scan_report.uuid, 16); + + /* Send ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET to remote provisioning server get link status */ + err = example_ble_mesh_set_msg_common(&common, addr,remote_prov_client.model + , ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Set message common fail:%d", __LINE__); + return; + } + + err = esp_ble_mesh_rpr_client_send(&common, NULL); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send Remote Provisioning Client msg:Link Get"); + } + + cur_rpr_cli_opcode = ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET; + break; + case ESP_BLE_MESH_MODEL_OP_RPR_EXT_SCAN_REPORT: + break; + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_STATUS: + addr = param->recv.params->ctx.addr; + ESP_LOGI(TAG, "link_status, status 0x%02x", param->recv.val.link_status.status); + ESP_LOGI(TAG, "link_status, rpr_state 0x%02x", param->recv.val.link_status.rpr_state); + switch (cur_rpr_cli_opcode) { + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET: { + if (param->recv.val.link_status.status == ESP_BLE_MESH_RPR_STATUS_SUCCESS) { + switch (param->recv.val.link_status.rpr_state) { + case ESP_BLE_MESH_RPR_LINK_IDLE: + /** + * Link status is idle, send ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN + * to remote provisioning server to open prov link + */ + err = example_ble_mesh_set_msg_common(&common, addr, remote_prov_client.model + , ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Set message common fail:%d", __LINE__); + return; + } + + msg.link_open.uuid_en = 1; + memcpy(msg.link_open.uuid, remote_dev_uuid, 16); + msg.link_open.timeout_en = 0; + + err = esp_ble_mesh_rpr_client_send(&common, &msg); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send Remote Provisioning Client msg:Link open"); + } + cur_rpr_cli_opcode = ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN; + break; + default: + ESP_LOGW(TAG, "Remote Provisioning Server(addr: 0x%04x) Busy", addr); + break; + } + } + break; + } + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN: { + if (param->recv.val.link_status.status == ESP_BLE_MESH_RPR_STATUS_SUCCESS) { + ESP_LOGI(TAG, "Remote Provisioning Server(addr: 0x%04x) Recv Link Open Success", addr); + } else { + ESP_LOGI(TAG, "Remote Provisioning Server(addr: 0x%04x) Recv Link Open Fail", addr); + } + } + break; + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_CLOSE: { + if (param->recv.val.link_status.status == ESP_BLE_MESH_RPR_STATUS_SUCCESS) { + ESP_LOGI(TAG, "Remote Provisioning Server(addr: 0x%04x) Recv Link Close Success", addr); + } else { + ESP_LOGI(TAG, "Remote Provisioning Server(addr: 0x%04x) Recv Link Close Fail", addr); + } + } + break; + default: + ESP_LOGW(TAG, "Unknown Process opcode 0x%04x:%d", cur_rpr_cli_opcode,__LINE__); + break; + } + break; + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_REPORT: + addr = param->recv.params->ctx.addr; + ESP_LOGI(TAG, "link_report, status 0x%02x", param->recv.val.link_report.status); + ESP_LOGI(TAG, "link_report, rpr_state 0x%02x", param->recv.val.link_report.rpr_state); + if (param->recv.val.link_report.reason_en) { + ESP_LOGI(TAG, "link_report, reason 0x%02x", param->recv.val.link_report.reason); + } + switch (cur_rpr_cli_opcode) { + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN: + if (param->recv.val.link_report.status == ESP_BLE_MESH_RPR_STATUS_SUCCESS) { + switch (param->recv.val.link_report.rpr_state) + { + case ESP_BLE_MESH_RPR_LINK_ACTIVE: + ESP_LOGI(TAG, "Remote Provisioning Server(addr: 0x%04x) Link Open Success", addr); + esp_ble_mesh_rpr_client_act_param_t param = {0}; + param.start_rpr.model = remote_prov_client.model; + param.start_rpr.rpr_srv_addr = addr; + + /* Let remote provisioning server start provisioning */ + err = esp_ble_mesh_rpr_client_action(ESP_BLE_MESH_RPR_CLIENT_ACT_START_RPR, + ¶m); + if (err) { + ESP_LOGE(TAG, "Failed to perform Remote Provisioning Client action: Start Prov"); + } + board_led_operation(LED_OFF, LED_ON, LED_OFF); + break; + default: + ESP_LOGI(TAG, "Remote Provisioning Server(addr: 0x%04x) Status error", addr); + break; + } + } else { + ESP_LOGW(TAG, "Remote Provisioning Server(addr: 0x%04x) Link open fail"); + } + break; + } + break; + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_CLOSE: + switch (param->recv.val.link_report.status) + { + case ESP_BLE_MESH_RPR_STATUS_LINK_CLOSED_BY_CLIENT: + ESP_LOGI(TAG, "Link closed by client"); + break; + case ESP_BLE_MESH_RPR_STATUS_LINK_CLOSED_BY_DEVICE: + ESP_LOGI(TAG, "Link closed by device"); + break; + case ESP_BLE_MESH_RPR_STATUS_LINK_CLOSED_BY_SERVER: + ESP_LOGI(TAG, "Link closed by server"); + break; + case ESP_BLE_MESH_RPR_STATUS_LINK_CLOSED_AS_CANNOT_RECEIVE_PDU: + ESP_LOGI(TAG, "Link closed as cannot receive pdu"); + break; + case ESP_BLE_MESH_RPR_STATUS_LINK_CLOSED_AS_CANNOT_SEND_PDU: + ESP_LOGI(TAG, "Link closed as cannot send pdu"); + break; + case ESP_BLE_MESH_RPR_STATUS_LINK_CLOSED_AS_CANNOT_DELIVER_PDU_REPORT: + ESP_LOGI(TAG, "Link closed as cannot send pdu report"); + break; + default: + ESP_LOGW(TAG, "Unknown link close status, %d", param->recv.val.link_report.status); + break; + } + break; + default: + ESP_LOGW(TAG, "Unknown Process opcode 0x%04x:%d", cur_rpr_cli_opcode,__LINE__); + break; + } + break; + case ESP_BLE_MESH_RPR_CLIENT_ACT_COMP_EVT: + ESP_LOGW(TAG, "Remote Prov Client Act Comp, sub_evt 0x%02x", param->act.sub_evt); + switch (param->act.sub_evt) { + case ESP_BLE_MESH_START_RPR_COMP_SUB_EVT: + ESP_LOGI(TAG, "Start Remote Prov Comp, err_code %d, rpr_srv_addr 0x%04x", + param->act.start_rpr_comp.err_code, + param->act.start_rpr_comp.rpr_srv_addr); + break; + default: + ESP_LOGE(TAG, "Unknown Remote Provisioning Client sub event"); + break; + } + break; + case ESP_BLE_MESH_RPR_CLIENT_LINK_OPEN_EVT: + ESP_LOGW(TAG, "Remote Prov Client Link Open"); + break; + case ESP_BLE_MESH_RPR_CLIENT_LINK_CLOSE_EVT: + ESP_LOGW(TAG, "Remote Prov Client Link Close"); + break; + case ESP_BLE_MESH_RPR_CLIENT_PROV_COMP_EVT: + ESP_LOGW(TAG, "Remote Prov Client Prov Complete"); + ESP_LOGI(TAG, "Net Idx: 0x%04x", param->prov.net_idx); + ESP_LOGI(TAG, "Node addr: 0x%04x", param->prov.unicast_addr); + ESP_LOGI(TAG, "Node element num: 0x%04x", param->prov.element_num); + ESP_LOG_BUFFER_HEX(TAG": Node UUID: ", param->prov.uuid, 16); + err = example_ble_mesh_set_msg_common(&common, param->prov.rpr_srv_addr, remote_prov_client.model, + ESP_BLE_MESH_MODEL_OP_RPR_LINK_CLOSE); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Set message common fail:%d", __LINE__); + return; + } + msg.link_close.reason = ESP_BLE_MESH_RPR_REASON_SUCCESS; + + err = esp_ble_mesh_rpr_client_send(&common, &msg); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send Remote Provisioning Client msg:Link open"); + } + cur_rpr_cli_opcode = ESP_BLE_MESH_MODEL_OP_RPR_LINK_CLOSE; + + prov_complete(param->prov.net_idx, param->prov.uuid, + param->prov.unicast_addr, param->prov.element_num, param->prov.net_idx); + break; + default: + break; + } +} + +static esp_err_t ble_mesh_init(void) +{ + uint8_t match[2] = {0x55, 0xaa}; + + esp_err_t err = ESP_OK; + + prov_key.net_idx = ESP_BLE_MESH_KEY_PRIMARY; + prov_key.app_idx = APP_KEY_IDX; + memset(prov_key.app_key, APP_KEY_OCTET, sizeof(prov_key.app_key)); + + esp_ble_mesh_register_prov_callback(example_ble_mesh_provisioning_cb); + esp_ble_mesh_register_config_client_callback(example_ble_mesh_config_client_cb); + esp_ble_mesh_register_generic_client_callback(example_ble_mesh_generic_client_cb); + esp_ble_mesh_register_rpr_client_callback(example_ble_mesh_remote_prov_client_callback); + + err = esp_ble_mesh_init(&provision, &composition); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize mesh stack (err %d)", err); + return err; + } + + err = esp_ble_mesh_provisioner_set_dev_uuid_match(match, sizeof(match), 0x0, false); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to set matching device uuid (err %d)", err); + return err; + } + + err = esp_ble_mesh_provisioner_prov_enable(ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to enable mesh provisioner (err %d)", err); + return err; + } + + err = esp_ble_mesh_provisioner_add_local_app_key(prov_key.app_key, prov_key.net_idx, prov_key.app_idx); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to add local AppKey (err %d)", err); + return err; + } + + ESP_LOGI(TAG, "BLE Mesh Provisioner initialized"); + + return err; +} + +void app_main(void) +{ + esp_err_t err; + + ESP_LOGI(TAG, "Initializing..."); + + board_init(); + + err = nvs_flash_init(); + if (err == ESP_ERR_NVS_NO_FREE_PAGES) { + ESP_ERROR_CHECK(nvs_flash_erase()); + err = nvs_flash_init(); + } + ESP_ERROR_CHECK(err); + + err = bluetooth_init(); + if (err) { + ESP_LOGE(TAG, "esp32_bluetooth_init failed (err %d)", err); + return; + } + + ble_mesh_get_dev_uuid(dev_uuid); + + /* Initialize the Bluetooth Mesh Subsystem */ + err = ble_mesh_init(); + if (err) { + ESP_LOGE(TAG, "Bluetooth mesh init failed (err %d)", err); + } +} diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/sdkconfig.defaults new file mode 100644 index 000000000000..499c5f40529f --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/sdkconfig.defaults @@ -0,0 +1,21 @@ +# Override some defaults so BT stack is enabled +# by default in this example +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_CTRL_BTDM_MODEM_SLEEP=n +CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 +CONFIG_BLE_MESH_RPR_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/sdkconfig.defaults.esp32c3 b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/sdkconfig.defaults.esp32c3 new file mode 100644 index 000000000000..5897d737c00b --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/sdkconfig.defaults.esp32c3 @@ -0,0 +1,17 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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/remote_provisioning/rpr_client/sdkconfig.defaults.esp32c6 b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/sdkconfig.defaults.esp32c6 new file mode 100644 index 000000000000..58ccc4d7a91f --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/sdkconfig.defaults.esp32c6 @@ -0,0 +1,19 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_BT_BLE_50_FEATURES_SUPPORTED=n +CONFIG_BT_LE_50_FEATURE_SUPPORT=n +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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/remote_provisioning/rpr_client/sdkconfig.defaults.esp32h2 b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/sdkconfig.defaults.esp32h2 new file mode 100644 index 000000000000..58ccc4d7a91f --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/sdkconfig.defaults.esp32h2 @@ -0,0 +1,19 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_BT_BLE_50_FEATURES_SUPPORTED=n +CONFIG_BT_LE_50_FEATURE_SUPPORT=n +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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/remote_provisioning/rpr_client/sdkconfig.defaults.esp32s3 b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/sdkconfig.defaults.esp32s3 new file mode 100644 index 000000000000..5897d737c00b --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_client/sdkconfig.defaults.esp32s3 @@ -0,0 +1,17 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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/remote_provisioning/rpr_server/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/CMakeLists.txt new file mode 100644 index 000000000000..47d782116b7c --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/CMakeLists.txt @@ -0,0 +1,10 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/light_driver + $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_init + $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_nvs) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(onoff_server) diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/CMakeLists.txt new file mode 100644 index 000000000000..13a56c69f2d8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/CMakeLists.txt @@ -0,0 +1,5 @@ +set(srcs "main.c" + "board.c") + +idf_component_register(SRCS "${srcs}" + INCLUDE_DIRS ".") diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/Kconfig.projbuild b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/Kconfig.projbuild new file mode 100644 index 000000000000..cbab996ec253 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/Kconfig.projbuild @@ -0,0 +1,38 @@ +menu "Example Configuration" + + choice BLE_MESH_EXAMPLE_BOARD + prompt "Board selection for BLE Mesh" + default BLE_MESH_ESP_WROOM_32 if IDF_TARGET_ESP32 + default BLE_MESH_ESP32C3_DEV if IDF_TARGET_ESP32C3 + default BLE_MESH_ESP32S3_DEV if IDF_TARGET_ESP32S3 + default BLE_MESH_ESP32C6_DEV if IDF_TARGET_ESP32C6 + help + Select this option to choose the board for BLE Mesh. The default is ESP32-WROOM-32 + + config BLE_MESH_ESP_WROOM_32 + bool "ESP32-WROOM-32" + depends on IDF_TARGET_ESP32 + + config BLE_MESH_ESP_WROVER + bool "ESP32-WROVER" + depends on IDF_TARGET_ESP32 + + config BLE_MESH_ESP32C3_DEV + bool "ESP32C3-DevKitC" + depends on IDF_TARGET_ESP32C3 + + config BLE_MESH_ESP32S3_DEV + bool "ESP32S3-DevKitC" + depends on IDF_TARGET_ESP32S3 + + config BLE_MESH_ESP32C6_DEV + bool "ESP32C6-DevKitC" + depends on IDF_TARGET_ESP32C6 + + config BLE_MESH_ESP32H2_DEV + bool "ESP32H2-DevKitC" + depends on IDF_TARGET_ESP32H2 + + endchoice + +endmenu diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/board.c b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/board.c new file mode 100644 index 000000000000..c729b6113b22 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/board.c @@ -0,0 +1,44 @@ +/* board.c - Board-specific hooks */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "driver/gpio.h" +#include "esp_log.h" +#include "board.h" + +#define TAG "BOARD" + +void board_led_operation(uint8_t r, uint8_t g, uint8_t b) +{ +#ifdef BLE_MESH_LED_STRIP_IO + rmt_led_set(r, g, b); +#else + gpio_set_level(LED_R, r); + gpio_set_level(LED_G, g); + gpio_set_level(LED_B, b); +#endif +} + +static void board_led_init(void) +{ +#ifdef BLE_MESH_LED_STRIP_IO + rmt_encoder_init(); + rmt_led_set(LED_OFF,LED_OFF,LED_OFF); +#else + gpio_set_level(LED_R, LED_OFF); + gpio_set_level(LED_G, LED_OFF); + gpio_set_level(LED_B, LED_OFF); +#endif +} + +void board_init(void) +{ + board_led_init(); +} diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/board.h b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/board.h new file mode 100644 index 000000000000..e754f6d95cce --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/board.h @@ -0,0 +1,51 @@ +/* board.h - Board-specific hooks */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BOARD_H_ +#define _BOARD_H_ + +#include "driver/gpio.h" +#include "led_strip_encoder.h" + +#if defined(CONFIG_BLE_MESH_ESP_WROOM_32) +#define LED_R GPIO_NUM_25 +#define LED_G GPIO_NUM_26 +#define LED_B GPIO_NUM_27 +#elif defined(CONFIG_BLE_MESH_ESP_WROVER) +#define LED_R GPIO_NUM_0 +#define LED_G GPIO_NUM_2 +#define LED_B GPIO_NUM_4 +#elif defined(CONFIG_BLE_MESH_ESP32C3_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8 +#elif defined(CONFIG_BLE_MESH_ESP32S3_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_47 +#elif defined(CONFIG_BLE_MESH_ESP32C6_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8 +#elif defined(CONFIG_BLE_MESH_ESP32H2_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8 +#endif + +#define BUTTON_IO_NUM GPIO_NUM_9 + +#ifndef BLE_MESH_LED_STRIP_IO +#define LED_ON 1 +#else +#define LED_R 0 +#define LED_G 1 +#define LED_B 2 +#define LED_ON 100 +#endif + +#define LED_OFF 0 + +void board_led_operation(uint8_t r, uint8_t g, uint8_t b); + +void board_init(void); + +#endif diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/main.c b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/main.c new file mode 100644 index 000000000000..58beb2979614 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/main/main.c @@ -0,0 +1,469 @@ +/* main.c - Application main entry point */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#include "esp_log.h" +#include "nvs_flash.h" + +#include "esp_ble_mesh_defs.h" +#include "esp_ble_mesh_common_api.h" +#include "esp_ble_mesh_networking_api.h" +#include "esp_ble_mesh_provisioning_api.h" +#include "esp_ble_mesh_config_model_api.h" +#include "esp_ble_mesh_generic_model_api.h" +#include "esp_ble_mesh_local_data_operation_api.h" +#if CONFIG_BLE_MESH_RPR_SRV +#include "esp_ble_mesh_rpr_model_api.h" +#endif + +#include "board.h" +#include "ble_mesh_example_init.h" + +#define TAG "EXAMPLE" + +#define CID_ESP 0x02E5 + +static uint8_t dev_uuid[16] = { 0x55, 0xaa }; + +static esp_ble_mesh_cfg_srv_t config_server = { + .relay = ESP_BLE_MESH_RELAY_DISABLED, + .beacon = ESP_BLE_MESH_BEACON_ENABLED, +#if defined(CONFIG_BLE_MESH_FRIEND) + .friend_state = ESP_BLE_MESH_FRIEND_ENABLED, +#else + .friend_state = ESP_BLE_MESH_FRIEND_NOT_SUPPORTED, +#endif +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_ENABLED, +#else + .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_NOT_SUPPORTED, +#endif + .default_ttl = 7, + /* 3 transmissions with 20ms interval */ + .net_transmit = ESP_BLE_MESH_TRANSMIT(2, 20), + .relay_retransmit = ESP_BLE_MESH_TRANSMIT(2, 20), +}; + +ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_pub_0, 2 + 3, ROLE_NODE); +static esp_ble_mesh_gen_onoff_srv_t onoff_server_0 = { + .rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP, + .rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP, +}; + +ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_pub_1, 2 + 3, ROLE_NODE); +static esp_ble_mesh_gen_onoff_srv_t onoff_server_1 = { + .rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_RSP_BY_APP, + .rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_RSP_BY_APP, +}; + +ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_pub_2, 2 + 3, ROLE_NODE); +static esp_ble_mesh_gen_onoff_srv_t onoff_server_2 = { + .rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP, + .rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_RSP_BY_APP, +}; + +static esp_ble_mesh_model_t root_models[] = { +#if CONFIG_BLE_MESH_RPR_SRV + ESP_BLE_MESH_MODEL_RPR_SRV(NULL), +#endif + ESP_BLE_MESH_MODEL_CFG_SRV(&config_server), + ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_0, &onoff_server_0), +}; + +static esp_ble_mesh_model_t extend_model_0[] = { + ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_1, &onoff_server_1), +}; + +static esp_ble_mesh_model_t extend_model_1[] = { + ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_2, &onoff_server_2), +}; + +static esp_ble_mesh_elem_t elements[] = { + ESP_BLE_MESH_ELEMENT(0, root_models, ESP_BLE_MESH_MODEL_NONE), + ESP_BLE_MESH_ELEMENT(0, extend_model_0, ESP_BLE_MESH_MODEL_NONE), + ESP_BLE_MESH_ELEMENT(0, extend_model_1, ESP_BLE_MESH_MODEL_NONE), +}; + +static esp_ble_mesh_comp_t composition = { + .cid = CID_ESP, + .elements = elements, + .element_count = ARRAY_SIZE(elements), +}; + +/* Disable OOB security for SILabs Android app */ +static esp_ble_mesh_prov_t provision = { + .uuid = dev_uuid, +#if 0 + .output_size = 4, + .output_actions = ESP_BLE_MESH_DISPLAY_NUMBER, + .input_actions = ESP_BLE_MESH_PUSH, + .input_size = 4, +#else + .output_size = 0, + .output_actions = 0, +#endif +}; + +static void prov_complete(uint16_t net_idx, uint16_t addr, uint8_t flags, uint32_t iv_index) +{ + ESP_LOGI(TAG, "net_idx: 0x%04x, addr: 0x%04x", net_idx, addr); + ESP_LOGI(TAG, "flags: 0x%02x, iv_index: 0x%08" PRIx32, flags, iv_index); +} + +static void example_change_led_state(esp_ble_mesh_model_t *model, + esp_ble_mesh_msg_ctx_t *ctx, uint8_t onoff) +{ + uint16_t primary_addr = esp_ble_mesh_get_primary_element_address(); + uint8_t elem_count = esp_ble_mesh_get_element_count(); + uint8_t rgb[3] = {0}; + uint8_t i; + + if (ESP_BLE_MESH_ADDR_IS_UNICAST(ctx->recv_dst)) { + for (i = 0; i < elem_count; i++) { + if (ctx->recv_dst == (primary_addr + i)) { + rgb[i] = onoff ? LED_ON : LED_OFF; + } + } + } else if (ESP_BLE_MESH_ADDR_IS_GROUP(ctx->recv_dst)) { + if (esp_ble_mesh_is_model_subscribed_to_group(model, ctx->recv_dst)) { + rgb[model->element->element_addr - primary_addr] = onoff ? LED_ON : LED_OFF; + } + } else if (ctx->recv_dst == 0xFFFF) { + rgb[0] = onoff ? LED_ON : LED_OFF; + rgb[1] = onoff ? LED_ON : LED_OFF; + rgb[2] = onoff ? LED_ON : LED_OFF; + } + + board_led_operation(rgb[0], rgb[1], rgb[2]); +} + +static void example_handle_gen_onoff_msg(esp_ble_mesh_model_t *model, + esp_ble_mesh_msg_ctx_t *ctx, + esp_ble_mesh_server_recv_gen_onoff_set_t *set) +{ + esp_ble_mesh_gen_onoff_srv_t *srv = model->user_data; + + switch (ctx->recv_op) { + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET: + esp_ble_mesh_server_model_send_msg(model, ctx, + ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS, sizeof(srv->state.onoff), &srv->state.onoff); + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK: + if (set->op_en == false) { + srv->state.onoff = set->onoff; + } else { + /* TODO: Delay and state transition */ + srv->state.onoff = set->onoff; + } + if (ctx->recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { + esp_ble_mesh_server_model_send_msg(model, ctx, + ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS, sizeof(srv->state.onoff), &srv->state.onoff); + } + esp_ble_mesh_model_publish(model, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS, + sizeof(srv->state.onoff), &srv->state.onoff, ROLE_NODE); + example_change_led_state(model, ctx, srv->state.onoff); + break; + default: + break; + } +} + +static void example_ble_mesh_provisioning_cb(esp_ble_mesh_prov_cb_event_t event, + esp_ble_mesh_prov_cb_param_t *param) +{ + switch (event) { + case ESP_BLE_MESH_PROV_REGISTER_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROV_REGISTER_COMP_EVT, err_code %d", param->prov_register_comp.err_code); + break; + case ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT, err_code %d", param->node_prov_enable_comp.err_code); + break; + case ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT, bearer %s", + param->node_prov_link_open.bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT"); + board_led_operation(LED_OFF, LED_ON, LED_OFF); + break; + case ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT, bearer %s", + param->node_prov_link_close.bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT"); + break; + case ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT"); + prov_complete(param->node_prov_complete.net_idx, param->node_prov_complete.addr, + param->node_prov_complete.flags, param->node_prov_complete.iv_index); + break; + case ESP_BLE_MESH_NODE_PROV_RESET_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_RESET_EVT"); + break; + case ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT, err_code %d", param->node_set_unprov_dev_name_comp.err_code); + break; + default: + break; + } +} + +static void example_ble_mesh_generic_server_cb(esp_ble_mesh_generic_server_cb_event_t event, + esp_ble_mesh_generic_server_cb_param_t *param) +{ + esp_ble_mesh_gen_onoff_srv_t *srv; + ESP_LOGI(TAG, "event 0x%02x, opcode 0x%04" PRIx32 ", src 0x%04x, dst 0x%04x", + event, param->ctx.recv_op, param->ctx.addr, param->ctx.recv_dst); + + switch (event) { + case ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT"); + if (param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET || + param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK) { + ESP_LOGI(TAG, "onoff 0x%02x", param->value.state_change.onoff_set.onoff); + example_change_led_state(param->model, ¶m->ctx, param->value.state_change.onoff_set.onoff); + } + break; + case ESP_BLE_MESH_GENERIC_SERVER_RECV_GET_MSG_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_GENERIC_SERVER_RECV_GET_MSG_EVT"); + if (param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET) { + srv = param->model->user_data; + ESP_LOGI(TAG, "onoff 0x%02x", srv->state.onoff); + example_handle_gen_onoff_msg(param->model, ¶m->ctx, NULL); + } + break; + case ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT"); + if (param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET || + param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK) { + ESP_LOGI(TAG, "onoff 0x%02x, tid 0x%02x", param->value.set.onoff.onoff, param->value.set.onoff.tid); + if (param->value.set.onoff.op_en) { + ESP_LOGI(TAG, "trans_time 0x%02x, delay 0x%02x", + param->value.set.onoff.trans_time, param->value.set.onoff.delay); + } + example_handle_gen_onoff_msg(param->model, ¶m->ctx, ¶m->value.set.onoff); + } + break; + default: + ESP_LOGE(TAG, "Unknown Generic Server event 0x%02x", event); + break; + } +} + +static void example_ble_mesh_config_server_cb(esp_ble_mesh_cfg_server_cb_event_t event, + esp_ble_mesh_cfg_server_cb_param_t *param) +{ + if (event == ESP_BLE_MESH_CFG_SERVER_STATE_CHANGE_EVT) { + switch (param->ctx.recv_op) { + case ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD: + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD"); + ESP_LOGI(TAG, "net_idx 0x%04x, app_idx 0x%04x", + param->value.state_change.appkey_add.net_idx, + param->value.state_change.appkey_add.app_idx); + ESP_LOG_BUFFER_HEX("AppKey", param->value.state_change.appkey_add.app_key, 16); + break; + case ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND: + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND"); + ESP_LOGI(TAG, "elem_addr 0x%04x, app_idx 0x%04x, cid 0x%04x, mod_id 0x%04x", + param->value.state_change.mod_app_bind.element_addr, + param->value.state_change.mod_app_bind.app_idx, + param->value.state_change.mod_app_bind.company_id, + param->value.state_change.mod_app_bind.model_id); + board_led_operation(LED_OFF, LED_OFF, LED_OFF); + break; + case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD: + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD"); + ESP_LOGI(TAG, "elem_addr 0x%04x, sub_addr 0x%04x, cid 0x%04x, mod_id 0x%04x", + param->value.state_change.mod_sub_add.element_addr, + param->value.state_change.mod_sub_add.sub_addr, + param->value.state_change.mod_sub_add.company_id, + param->value.state_change.mod_sub_add.model_id); + break; + default: + break; + } + } +} + +static void print_scan_start_evt(esp_ble_mesh_rpr_server_cb_param_t *param) +{ + ESP_LOGI(TAG, "scan_start, element_idx 0x%02x", param->scan_start.model->element_idx); + ESP_LOGI(TAG, "scan_start, model_idx 0x%02x", param->scan_start.model->model_idx); + ESP_LOGI(TAG, "scan_start, scan_items_limit 0x%02x", param->scan_start.scan_items_limit); + ESP_LOGI(TAG, "scan_start, timeout 0x%02x", param->scan_start.timeout); + ESP_LOGI(TAG, "scan_start, net_idx 0x%04x", param->scan_start.net_idx); + ESP_LOGI(TAG, "scan_start, rpr_cli_addr 0x%04x", param->scan_start.rpr_cli_addr); + ESP_LOG_BUFFER_HEX("CMD_RP: scan_start, uuid", param->scan_start.uuid, 16); +} + +static void print_scan_stop_evt(esp_ble_mesh_rpr_server_cb_param_t *param) +{ + ESP_LOGI(TAG, "scan_stop, element_idx 0x%02x", param->scan_stop.model->element_idx); + ESP_LOGI(TAG, "scan_stop, model_idx 0x%02x", param->scan_stop.model->model_idx); + ESP_LOGI(TAG, "scan_stop, net_idx 0x%04x", param->scan_stop.net_idx); + ESP_LOGI(TAG, "scan_stop, rpr_cli_addr 0x%04x", param->scan_stop.rpr_cli_addr); + ESP_LOG_BUFFER_HEX("CMD_RP: scan_stop, uuid", param->scan_stop.uuid, 16); +} + +static void print_ext_scan_start_evt(esp_ble_mesh_rpr_server_cb_param_t *param) +{ + ESP_LOGI(TAG, "ext_scan_start, element_idx 0x%02x", param->ext_scan_start.model->element_idx); + ESP_LOGI(TAG, "ext_scan_start, model_idx 0x%02x", param->ext_scan_start.model->model_idx); + if (param->ext_scan_start.ad_type_filter_count && param->ext_scan_start.ad_type_filter) { + ESP_LOG_BUFFER_HEX("CMD_RP: ext_scan_start, ad_type_filter", + param->ext_scan_start.ad_type_filter, + param->ext_scan_start.ad_type_filter_count); + } + ESP_LOGI(TAG, "ext_scan_start, timeout 0x%02x", param->ext_scan_start.timeout); + ESP_LOGI(TAG, "ext_scan_start, index 0x%02x", param->ext_scan_start.index); + ESP_LOGI(TAG, "ext_scan_start, net_idx 0x%04x", param->ext_scan_start.net_idx); + ESP_LOGI(TAG, "ext_scan_start, rpr_cli_addr 0x%04x", param->ext_scan_start.rpr_cli_addr); + ESP_LOG_BUFFER_HEX("CMD_RP: ext_scan_start, uuid", param->ext_scan_start.uuid, 16); +} + +static void print_ext_scan_stop_evt(esp_ble_mesh_rpr_server_cb_param_t *param) +{ + ESP_LOGI(TAG, "ext_scan_stop, element_idx 0x%02x", param->ext_scan_stop.model->element_idx); + ESP_LOGI(TAG, "ext_scan_stop, model_idx 0x%02x", param->ext_scan_stop.model->model_idx); + ESP_LOGI(TAG, "ext_scan_stop, timeout 0x%02x", param->ext_scan_stop.timeout); + ESP_LOGI(TAG, "ext_scan_stop, index 0x%02x", param->ext_scan_stop.index); + ESP_LOGI(TAG, "ext_scan_stop, net_idx 0x%04x", param->ext_scan_stop.net_idx); + ESP_LOGI(TAG, "ext_scan_stop, rpr_cli_addr 0x%04x", param->ext_scan_stop.rpr_cli_addr); + ESP_LOG_BUFFER_HEX("CMD_RP: ext_scan_stop, uuid", param->ext_scan_stop.uuid, 16); +} + +static void print_link_open_evt(esp_ble_mesh_rpr_server_cb_param_t *param) +{ + ESP_LOGI(TAG, "link_open, element_idx 0x%02x", param->link_open.model->element_idx); + ESP_LOGI(TAG, "link_open, model_idx 0x%02x", param->link_open.model->model_idx); + ESP_LOGI(TAG, "link_open, status 0x%02x", param->link_open.status); + ESP_LOGI(TAG, "link_open, timeout 0x%02x", param->link_open.timeout); + ESP_LOGI(TAG, "link_open, nppi 0x%02x", param->link_open.nppi); + ESP_LOGI(TAG, "link_open, net_idx 0x%04x", param->link_open.net_idx); + ESP_LOGI(TAG, "link_open, rpr_cli_addr 0x%04x", param->link_open.rpr_cli_addr); + ESP_LOG_BUFFER_HEX("CMD_RP: link_open, uuid", param->link_open.uuid, 16); +} + +static void print_link_close_evt(esp_ble_mesh_rpr_server_cb_param_t *param) +{ + ESP_LOGI(TAG, "link_close, element_idx 0x%02x", param->link_close.model->element_idx); + ESP_LOGI(TAG, "link_close, model_idx 0x%02x", param->link_close.model->model_idx); + ESP_LOGI(TAG, "link_close, nppi 0x%02x", param->link_close.nppi); + ESP_LOGI(TAG, "link_close, close_by_device %d", param->link_close.close_by_device); + ESP_LOGI(TAG, "link_close, reason 0x%02x", param->link_close.reason); + ESP_LOGI(TAG, "link_close, net_idx 0x%04x", param->link_close.net_idx); + ESP_LOGI(TAG, "link_close, rpr_cli_addr 0x%04x", param->link_close.rpr_cli_addr); + ESP_LOG_BUFFER_HEX("CMD_RP: link_close, uuid", param->link_close.uuid, 16); +} + +static void print_prov_comp_evt(esp_ble_mesh_rpr_server_cb_param_t *param) +{ + ESP_LOGI(TAG, "prov_comp, element_idx 0x%02x", param->prov_comp.model->element_idx); + ESP_LOGI(TAG, "prov_comp, model_idx 0x%02x", param->prov_comp.model->model_idx); + ESP_LOGI(TAG, "prov_comp, nppi 0x%02x", param->prov_comp.nppi); + ESP_LOGI(TAG, "prov_comp, net_idx 0x%04x", param->prov_comp.net_idx); + ESP_LOGI(TAG, "prov_comp, rpr_cli_addr 0x%04x", param->prov_comp.rpr_cli_addr); + ESP_LOG_BUFFER_HEX("CMD_RP: prov_comp, uuid", param->prov_comp.uuid, 16); +} + +static void example_remote_prov_server_callback(esp_ble_mesh_rpr_server_cb_event_t event, + esp_ble_mesh_rpr_server_cb_param_t *param) +{ + switch (event) { + case ESP_BLE_MESH_RPR_SERVER_SCAN_START_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_SCAN_START_EVT"); + print_scan_start_evt(param); + break; + case ESP_BLE_MESH_RPR_SERVER_SCAN_STOP_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_SCAN_STOP_EVT"); + print_scan_stop_evt(param); + break; + case ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_START_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_START_EVT"); + print_ext_scan_start_evt(param); + break; + case ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_STOP_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_STOP_EVT"); + print_ext_scan_stop_evt(param); + break; + case ESP_BLE_MESH_RPR_SERVER_LINK_OPEN_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_LINK_OPEN_EVT"); + print_link_open_evt(param); + board_led_operation(LED_OFF, LED_OFF, LED_ON); + break; + case ESP_BLE_MESH_RPR_SERVER_LINK_CLOSE_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_LINK_CLOSE_EVT"); + board_led_operation(LED_OFF, LED_OFF, LED_OFF); + print_link_close_evt(param); + break; + case ESP_BLE_MESH_RPR_SERVER_PROV_COMP_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_PROV_COMP_EVT"); + print_prov_comp_evt(param); + break; + default: + break; + } +} + +static esp_err_t ble_mesh_init(void) +{ + esp_err_t err = ESP_OK; + + esp_ble_mesh_register_prov_callback(example_ble_mesh_provisioning_cb); + esp_ble_mesh_register_config_server_callback(example_ble_mesh_config_server_cb); + esp_ble_mesh_register_generic_server_callback(example_ble_mesh_generic_server_cb); + esp_ble_mesh_register_rpr_server_callback(example_remote_prov_server_callback); + + err = esp_ble_mesh_init(&provision, &composition); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize mesh stack (err %d)", err); + return err; + } + + err = esp_ble_mesh_node_prov_enable(ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to enable mesh node (err %d)", err); + return err; + } + + ESP_LOGI(TAG, "BLE Mesh Node initialized"); + + board_led_operation(LED_ON, LED_OFF, LED_OFF); + return err; +} + +void app_main(void) +{ + esp_err_t err; + + ESP_LOGI(TAG, "Initializing..."); + + board_init(); + + err = nvs_flash_init(); + if (err == ESP_ERR_NVS_NO_FREE_PAGES) { + ESP_ERROR_CHECK(nvs_flash_erase()); + err = nvs_flash_init(); + } + ESP_ERROR_CHECK(err); + + err = bluetooth_init(); + if (err) { + ESP_LOGE(TAG, "esp32_bluetooth_init failed (err %d)", err); + return; + } + + ble_mesh_get_dev_uuid(dev_uuid); + ESP_LOG_BUFFER_HEX(TAG":uuid", dev_uuid, 16); + + /* Initialize the Bluetooth Mesh Subsystem */ + err = ble_mesh_init(); + if (err) { + ESP_LOGE(TAG, "Bluetooth mesh init failed (err %d)", err); + } +} diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults new file mode 100644 index 000000000000..92b956ae451d --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults @@ -0,0 +1,20 @@ +# Override some defaults so BT stack is enabled +# by default in this example +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_CTRL_BTDM_MODEM_SLEEP=n +CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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_RPR_SRV=y diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults.esp32c3 b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults.esp32c3 new file mode 100644 index 000000000000..ad04a937e2e8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults.esp32c3 @@ -0,0 +1,16 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults.esp32c6 b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults.esp32c6 new file mode 100644 index 000000000000..78299bedf8d8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults.esp32c6 @@ -0,0 +1,18 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_BT_BLE_50_FEATURES_SUPPORTED=n +CONFIG_BT_LE_50_FEATURE_SUPPORT=n +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults.esp32h2 b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults.esp32h2 new file mode 100644 index 000000000000..78299bedf8d8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults.esp32h2 @@ -0,0 +1,18 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_BT_BLE_50_FEATURES_SUPPORTED=n +CONFIG_BT_LE_50_FEATURE_SUPPORT=n +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults.esp32s3 b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults.esp32s3 new file mode 100644 index 000000000000..ad04a937e2e8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/rpr_server/sdkconfig.defaults.esp32s3 @@ -0,0 +1,16 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/tutorial/BLE_Mesh_Remote_Provisioning_Example_Walkthrough.md b/examples/bluetooth/esp_ble_mesh/remote_provisioning/tutorial/BLE_Mesh_Remote_Provisioning_Example_Walkthrough.md new file mode 100644 index 000000000000..385ec47a8877 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/tutorial/BLE_Mesh_Remote_Provisioning_Example_Walkthrough.md @@ -0,0 +1,504 @@ +# 1. Introduction +This example demonstrates how to use the Remote Provisioning feature in the Espressif IDF's Bluetooth LE Mesh v1.1 protocol stack. + +The Remote Provisioning feature allows for the inclusion of nodes that are not within the coverage range of the Provisioner. In Remote Provisioning, there are two roles: the Remote Provisioning Client and the Remote Provisioning Server. The Remote Provisioning Client is typically played by the Provisioner, while the Remote Provisioning Server can be played by any node within the network. As an intermediate node, the Remote Provisioning Server primarily performs three functions: +1. Reports information of unprovisioned devices to the Remote Provisioning Client. +2. Reports its own provisioning status to the Remote Provisioning Client. +3. Distributes provisioning data from the Remote Provisioning Client to unprovisioned devices. + +By coordinating with each other, the Remote Provisioning Client and Remote Provisioning Server work together to include unprovisioned devices into the network. +## 1.1 Example Description +### 1.1.1 Directory Structure and Explanation +``` +. +├── unprov_dev +│ ├── CMakeLists.txt +│ └── main +│ ├── board.c +│ ├── board.h +│ ├── CMakeLists.txt +│ ├── Kconfig.projbuild +│ └── main.c +├── rpr_client +│ ├── CMakeLists.txt +│ └── main +│ ├── board.c +│ ├── board.h +│ ├── CMakeLists.txt +│ ├── Kconfig.projbuild +│ └── main.c +├── rpr_server +│ ├── CMakeLists.txt +│ └── main +│ ├── board.c +│ ├── board.h +│ ├── CMakeLists.txt +│ ├── Kconfig.projbuild +│ └── main.c +├── tutorial +│ ├── images +│ └── BLE_Mesh_Remote_Provisioning_Example_Walkthrough.md +└── README.md +``` +In this example, there are three roles: unprovisioned device, remote provisioning client, and remote provisioning server. + +1. The node is used to simulate an unprovisioned device that is outside the signal coverage of the provisioner. In practice, it is not necessary to place it outside the signal coverage of the provisioner, as we can restrict the provisioner from provisioning it through software limitations. This is done to simulate the node being outside the provisioner's signal coverage. +2. The remote_provisioning_client refers to the Remote Provisioning Client mentioned earlier. +3. The remote_provisioning_server refers to the Remote Provisioning Server mentioned earlier. + +Based on this, you will need at least three devices to run this example: + +1 x Device running `unprov_dev`. + +1 x Device running `rpr_client`. + +1 x Device running `rpr_server`. + +### 1.1.2 Running Phenomenon +First, power on the devices running `rpr_client` and node. The LED on the node will be red, indicating that the node is in an unprovisioned state. + +The node will maintain a solid red LED, indicating that remote_provisioning_client is unable to provision the node device. + +Next, power on the remote_provisioning_server node. The LED on the remote_provisioning_server will change from red to green. The remote_provisioning_client will also have a green LED, indicating that it is preparing to provision the remote_provisioning_server and perform post-provisioning tasks (such as adding AppKey and binding AppKey). + +Once all the tasks are completed, the LEDs on both remote_provisioning_server and remote_provisioning_client will turn off. Pressing the button on the remote_provisioning_client will command the remote_provisioning_server to start searching for unprovisioned devices. + +When the remote_provisioning_server finds a device, its LED will turn blue, and the LEDs on both the remote_provisioning_client and the node will turn green. This indicates that the node is going through the provisioning process via the remote_provisioning_server. + +Once all LEDs on the remote_provisioning_client, remote_provisioning_server, and node are turned off, it means that all the preliminary work is completed. + +At this point, pressing the button on the remote_provisioning_client allows you to control the LED on/off state of both the remote_provisioning_server and the node. + +### 1.1.3 Message Sequence +![Message Sequence](./images/message_sequence.png) +### 1.1.4 Code Explanation +#### Remote Provisioning Client +---- +Declaration and registration of a remote provisioning client. +```c +#if CONFIG_BLE_MESH_RPR_CLI +static esp_ble_mesh_client_t remote_prov_client; +#endif +// ... +static esp_ble_mesh_model_t root_models[] = { + ESP_BLE_MESH_MODEL_CFG_SRV(&config_server), + ESP_BLE_MESH_MODEL_CFG_CLI(&config_client), +#if CONFIG_BLE_MESH_RPR_CLI + ESP_BLE_MESH_MODEL_RPR_CLI(&remote_prov_client), +#endif + ESP_BLE_MESH_MODEL_GEN_ONOFF_CLI(NULL, &onoff_client), +}; +``` +In the `ble_mesh_init` function, register callback functions for relevant events and set the prefix for the commissionable device. The prefix for node devices is 0x55,0x55, while the prefix set for the remote provisioning client is 0x55,0xaa. Therefore, the remote provisioning client does not directly commission node devices. + +```c +static esp_err_t ble_mesh_init(void) +{ + // ... + uint8_t match[2] = {0x55, 0xaa}; + + esp_ble_mesh_register_prov_callback(example_ble_mesh_provisioning_cb); + esp_ble_mesh_register_config_client_callback(example_ble_mesh_config_client_cb); + esp_ble_mesh_register_generic_client_callback(example_ble_mesh_generic_client_cb); + /* Register the callback function of remote provisioning client */ + esp_ble_mesh_register_rpr_client_callback(example_ble_mesh_remote_prov_client_callback); + + /* Set the accepted unprovisioned device uuid prefix */ + err = esp_ble_mesh_provisioner_set_dev_uuid_match(match, sizeof(match), 0x0, false); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to set matching device uuid (err %d)", err); + return err; + } + + // ... +} +``` +In the `example_ble_mesh_config_client_cb` function, under the event `ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT` and opcode `ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND`, use the function `example_ble_mesh_query_element_have_model` to check if the currently ended AppKeyBind node supports the remote provisioning server. If it does, record the node address and trigger the sending of a Remote Provisioning Server Scan Get command when a button is pressed to start remote provisioning. +```c +static void example_ble_mesh_config_client_cb(esp_ble_mesh_cfg_client_cb_event_t event, + esp_ble_mesh_cfg_client_cb_param_t *param) +{ + // ... + + switch (event) { + // ... + case ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT: + switch (opcode) { + // ... + case ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND: { + + if (!example_ble_mesh_query_element_have_model(addr, ESP_BLE_MESH_MODEL_ID_RPR_SRV, CID_NVAL)) { + if (remote_rpr_srv_addr) { + ESP_LOGI(TAG, "The last node have been provisioned, You could click button to send Generic Onoff Set"); + click_to_send_onoff_set = 1; + } else { + ESP_LOGE(TAG, "Element (addr: 0x%04x) does not support remote provisioning srv model"); + } + return; + } else { + click_to_send_onoff_set = 0; + ESP_LOGI(TAG, "The Remote Provisioning Server have been provisioned, You could click button to start remote provisioning"); + remote_rpr_srv_addr = addr; + } + board_led_operation(LED_G, LED_OFF); + break; + } + default: + break; + } + break; + // ... + default: + ESP_LOGE(TAG, "Not a config client status message event"); + break; + } +} +``` +The button callback function in board.c is as follows: +```c +static void button_tap_cb(void* arg) +{ + static uint8_t onoff = 1; + ESP_LOGI(TAG, "tap cb (%s)", (char *)arg); + if (click_to_send_onoff_set) { + example_ble_mesh_send_gen_onoff_set(onoff); + onoff = !onoff; + } else { + example_ble_mesh_send_remote_provisioning_scan_start(); + } +} +``` +By using `click_to_send_onoff_set`, it can be determined whether the current button press should trigger `Generic Onoff Set` or `Remote Provisioning Scan Start`. +The implementation of the function `example_ble_mesh_send_remote_provisioning_scan_start` is as follows: +```c +void example_ble_mesh_send_remote_provisioning_scan_start(void) +{ + + esp_ble_mesh_client_common_param_t common = {0}; + esp_err_t err = ESP_OK; + + if (!remote_rpr_srv_addr) { + ESP_LOGE(TAG, "No valid remote provisioning server address"); + return; + } + + /* Send a ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET to get the scan status of remote provisioning server */ + example_ble_mesh_set_msg_common(&common, remote_rpr_srv_addr, remote_prov_client.model, ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET); + err = esp_ble_mesh_rpr_client_send(&common, NULL); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send Remote Provisioning Client msg: Scan Get"); + } + cur_rpr_cli_opcode = ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET; +} +``` +Opcode `ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET` is used to obtain the scan status of the remote provisioning server. It can assist the remote provisioning client in scanning for unprovisioned devices only when the status is Idle. The remote provisioning server will immediately respond with `ESP_BLE_MESH_MODEL_OP_RPR_SCAN_STATUS` upon receiving `ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET`. + +The `ESP_BLE_MESH_MODEL_OP_RPR_SCAN_STATUS` has the following four fields: +1. `scan_status`: This field indicates the status of the remote provisioning server's handling of the scan command from the remote provisioning client. If the value is `ESP_BLE_MESH_RPR_STATUS_SUCCESS`, it means that the remote provisioning server successfully processed the `ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET` command in this example. + +2. `rpr_scanning`: This field indicates the current status of the remote provisioning server. + - `ESP_BLE_MESH_RPR_SCAN_IDLE`: Indicates that the remote provisioning server is in an idle state and can be started. + - `ESP_BLE_MESH_RPR_SCAN_MULTIPLE_DEVICE`: Indicates that the remote provisioning server is currently scanning in multiple device mode. When the remote provisioning server is in this mode, it is executing a scanning task and will report any unprovisioned devices until the scan count limit is reached. + - `ESP_BLE_MESH_RPR_SCAN_SINGLE_DEVICE`: Indicates that the remote provisioning server is scanning for a specific device. It will only report when it scans an unprovisioned beacon that meets the requirements of the remote provisioning client. + +3. `scan_items_limit`: This field represents the maximum number of unprovisioned device information reported by the remote provisioning server. The value is set by the remote provisioning client and defaults to the maximum value `CONFIG_BLE_MESH_RPR_SRV_MAX_SCANNED_ITEMS`, which is 10 if not set by the client. + +4. `timeout`: This value represents the timeout for the remote provisioning server to perform the scan operation in seconds. + +In this example, the parsing and manipulation of the above data are shown as follows: +```c +static void example_ble_mesh_remote_prov_client_callback(esp_ble_mesh_rpr_client_cb_event_t event, + esp_ble_mesh_rpr_client_cb_param_t *param) +{ + static uint8_t remote_dev_uuid[16] = {0}; + esp_ble_mesh_rpr_client_msg_t msg = {0}; + esp_ble_mesh_client_common_param_t common = {0}; + esp_err_t err = ESP_OK; + uint16_t addr = 0; + + switch (event) { + // ... + case ESP_BLE_MESH_RPR_CLIENT_RECV_PUB_EVT: + case ESP_BLE_MESH_RPR_CLIENT_RECV_RSP_EVT: + ESP_LOGW(TAG, "Remote Prov Client Recv RSP, opcode 0x%04x, from 0x%04x", + param->recv.params->ctx.recv_op, param->recv.params->ctx.addr); + switch (param->recv.params->ctx.recv_op) { + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_CAPS_STATUS: + break; + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_STATUS: + addr = param->recv.params->ctx.addr; + ESP_LOGI(TAG, "scan_status, status 0x%02x", param->recv.val.scan_status.status); + ESP_LOGI(TAG, "scan_status, rpr_scanning 0x%02x", param->recv.val.scan_status.rpr_scanning); + ESP_LOGI(TAG, "scan_status, scan_items_limit 0x%02x", param->recv.val.scan_status.scan_items_limit); + ESP_LOGI(TAG, "scan_status, timeout 0x%02x", param->recv.val.scan_status.timeout); + switch (cur_rpr_cli_opcode) { + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET: { + if (param->recv.val.scan_status.status == ESP_BLE_MESH_RPR_STATUS_SUCCESS) { + switch (param->recv.val.scan_status.rpr_scanning) { + /** + * If the remote provisioning server's scan state is idle, + * that state indicates that remote provisioning server could + * start scan process. + */ + case ESP_BLE_MESH_RPR_SCAN_IDLE: { + err = example_ble_mesh_set_msg_common(&common, addr, remote_prov_client.model, + ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Set message common fail:%d", __LINE__); + return ; + } + + msg.scan_start.scan_items_limit = 0; /* 0 indicates there is no limit for scan items' count */ + msg.scan_start.timeout = 0x0A; /* 0x0A is the default timeout */ + msg.scan_start.uuid_en = 0; /* If uuid enabled, a specify device which have the same uuid will be report */ + /* If uuid disable, any unprovision device all will be report */ + + err = esp_ble_mesh_rpr_client_send(&common, &msg); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send Remote Provisioning Client msg: Scan start"); + } + cur_rpr_cli_opcode = ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START; + break; + } + default: + ESP_LOGW(TAG, "Remote Provisioning Server(addr: 0x%04x) Busy", addr); + break; + } + } else { + ESP_LOGE(TAG, "Remote Provisioning Client Scan Get Fail"); + } + } + break; + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START: { + if (param->recv.val.scan_status.status == ESP_BLE_MESH_RPR_STATUS_SUCCESS) { + ESP_LOGI(TAG, "Start Remote Provisioning Server(addr: 0x%04x) Scan Success", addr); + } else { + ESP_LOGE(TAG, "Remote Provisioning Client Scan Start Fail"); + } + break; + } + default: + ESP_LOGW(TAG, "Unknown Process opcode 0x%04x:%d", cur_rpr_cli_opcode,__LINE__); + break; + } + break; + } + // ... + default: + break; + } +} +``` +Due to `ESP_BLE_MESH_MODEL_OP_RPR_SCAN_STATUS` being a response to multiple Remote Provisioning Client Opcodes, the Remote Provisioning Client needs to keep track of the current Opcode being sent in order to differentiate the Opcode corresponding to the Status. In this example, the variable `cur_rpr_cli_opcode` is used for this purpose. + +When receiving a response `ESP_BLE_MESH_MODEL_OP_RPR_SCAN_STATUS` for `ESP_BLE_MESH_MODEL_OP_RPR_SCAN_GET`, if the current Scan status of the Remote Provisioning Server is Idle, then send `ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START` to the Remote Provisioning Server to initiate scanning. +`ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START` requires three parameters: +1. `scan_items_limit`: The number of Unprovisioned Devices that can be scanned. This value cannot exceed the maximum scan limit of the Remote Provisioning Server. When set to 0, the Remote Provisioning Client does not limit this value, and the Remote Provisioning Server will set it to its own maximum scan limit. +2. `timeout`: The maximum scan time for the Remote Provisioning Server in seconds. This value cannot be set to 0. +3. `uuid_en`: Specifies whether the Unprovisioned Device's UUID is specified. 0 means no, 1 means yes. If specified, the UUID information of the Unprovisioned Device needs to be filled. An example is shown below. In this example, the UUID is not specified. +```c +msg.scan_start.uuid_en = 1; +memcpy(msg.scan_start.uuid, unprovisioning_device_uuid, 16); +``` +When `ESP_BLE_MESH_MODEL_OP_RPR_SCAN_START` is sent, the Remote Provisioning Server will immediately respond with `ESP_BLE_MESH_MODEL_OP_RPR_SCAN_STATUS`. If `scan_status.status` is `ESP_BLE_MESH_RPR_STATUS_SUCCESS`, it indicates that the Remote Provisioning Server's Scan has been successfully started. When it scans an unprovisioned device, it will respond with `ESP_BLE_MESH_MODEL_OP_RPR_SCAN_REPORT`. The handling of `ESP_BLE_MESH_MODEL_OP_RPR_SCAN_REPORT` in this example is as follows: +```c +static void example_ble_mesh_remote_prov_client_callback(esp_ble_mesh_rpr_client_cb_event_t event, + esp_ble_mesh_rpr_client_cb_param_t *param) +{ + // ... + case ESP_BLE_MESH_MODEL_OP_RPR_SCAN_REPORT: + addr = param->recv.params->ctx.addr; + ESP_LOGI(TAG, "scan_report, rssi %ddBm", param->recv.val.scan_report.rssi); + ESP_LOG_BUFFER_HEX(TAG": scan_report, uuid", param->recv.val.scan_report.uuid, 16); + ESP_LOGI(TAG, "scan_report, oob_info 0x%04x", param->recv.val.scan_report.oob_info); + ESP_LOGI(TAG, "scan_report, uri_hash 0x%08x", param->recv.val.scan_report.uri_hash); + + if (param->recv.val.scan_report.uuid[0] != remote_dev_uuid_match[0] || + param->recv.val.scan_report.uuid[1] != remote_dev_uuid_match[1]) { + ESP_LOGI(TAG, "This device is not expect device"); + return; + } + + memcpy(remote_dev_uuid, param->recv.val.scan_report.uuid, 16); + + /* Send ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET to remote provisioning server get link status */ + err = example_ble_mesh_set_msg_common(&common, addr,remote_prov_client.model + , ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Set message common fail:%d", __LINE__); + return; + } + + err = esp_ble_mesh_rpr_client_send(&common, NULL); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send Remote Provisioning Client msg:Link Get"); + } + + cur_rpr_cli_opcode = ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET; + break; + // ... +} +``` +If the scanned device is the expected device, use `ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET` to obtain the status of the Provisioning Link of the Remote Provisioning Server. The Provisioning Link can only be opened for network provisioning when its status is `ESP_BLE_MESH_RPR_LINK_IDLE`. Upon receiving `ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET`, the Remote Provisioning Server will respond with `ESP_BLE_MESH_MODEL_OP_RPR_LINK_STATUS`. + +The Status includes two main fields: +1. `status`: This field indicates whether the Remote Provisioning Server has successfully processed the command from the Remote Provisioning Client. In this example, the command is `ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET` and only when the result is `ESP_BLE_MESH_RPR_STATUS_SUCCESS`, it means that the Remote Provisioning Server has processed the `ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET` message successfully. +2. `rpr_status`: This field indicates the current status of the Provisioning Link on the Remote Provisioning Server. The Provisioning Link can have the following states: + - `ESP_BLE_MESH_RPR_LINK_IDLE`: Indicates that the Provisioning Link is in an idle state. + - `ESP_BLE_MESH_RPR_LINK_OPENING`: Indicates that the Provisioning Link is being opened. + - `ESP_BLE_MESH_RPR_LINK_ACTIVE`: Indicates that the Provisioning Link has been activated and is waiting for the Remote Provisioning Client to send the command to start network provisioning. + - `ESP_BLE_MESH_RPR_OUTBOUND_PACKET_TRANSFER`: Indicates that the Provisioning Link is in the process of network provisioning. + - `ESP_BLE_MESH_RPR_LINK_CLOSING`: Indicates that the Provisioning Link is about to be closed. + +In this example, only the `ESP_BLE_MESH_RPR_LINK_IDLE` state is processed as follows: +```c +static void example_ble_mesh_remote_prov_client_callback(esp_ble_mesh_rpr_client_cb_event_t event, + esp_ble_mesh_rpr_client_cb_param_t *param) +{ + // ... + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_GET: { + if (param->recv.val.link_status.status == ESP_BLE_MESH_RPR_STATUS_SUCCESS) { + switch (param->recv.val.link_status.rpr_state) { + case ESP_BLE_MESH_RPR_LINK_IDLE: + /** + * Link status is idle, send ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN + * to remote provisioning server to open prov link + */ + err = example_ble_mesh_set_msg_common(&common, addr, remote_prov_client.model + , ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Set message common fail:%d", __LINE__); + return; + } + + msg.link_open.uuid_en = 1; + memcpy(msg.link_open.uuid, remote_dev_uuid, 16); + msg.link_open.timeout_en = 0; + + err = esp_ble_mesh_rpr_client_send(&common, &msg); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to send Remote Provisioning Client msg:Link open"); + } + cur_rpr_cli_opcode = ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN; + break; + default: + ESP_LOGW(TAG, "Remote Provisioning Server(addr: 0x%04x) Busy", addr); + break; + } + } + break; + } + // ... +} +``` +If the status of the Provisioning Link of the Remote Provisioning Server is `ESP_BLE_MESH_RPR_LINK_IDLE`, the Remote Provisioning Client sends `ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN` to open the Provisioning Link of the Remote Provisioning Server. Once the Remote Provisioning Server receives `ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN`, it immediately replies with `ESP_BLE_MESH_MODEL_OP_RPR_LINK_STATUS` to indicate whether it has received the command from the Remote Provisioning Client. The Remote Provisioning Server then reports to the Remote Provisioning Client whether it has successfully established a Provisioning Link with the Unprovisioning Device by replying with `ESP_BLE_MESH_MODEL_OP_RPR_LINK_REPORT`. + +Only when the `status` in `ESP_BLE_MESH_MODEL_OP_RPR_LINK_REPORT` is `ESP_BLE_MESH_RPR_STATUS_SUCCESS` and `rpr_state` is `ESP_BLE_MESH_RPR_LINK_ACTIVE`, it means that the Remote Provisioning Server has successfully established a Provisioning Link with the Unprovisioning Device and the Link Open is successful. At this point, the Unprovisioning Device triggers the `ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT` event. + +In this example, the processing of receiving `ESP_BLE_MESH_MODEL_OP_RPR_LINK_REPORT` is as follows: +```c + switch (cur_rpr_cli_opcode) { + case ESP_BLE_MESH_MODEL_OP_RPR_LINK_OPEN: + if (param->recv.val.link_report.status == ESP_BLE_MESH_RPR_STATUS_SUCCESS) { + switch (param->recv.val.link_report.rpr_state) + { + case ESP_BLE_MESH_RPR_LINK_ACTIVE: + ESP_LOGI(TAG, "Remote Provisioning Server(addr: 0x%04x) Link Open Success", addr); + esp_ble_mesh_rpr_client_act_param_t param = {0}; + param.start_rpr.model = remote_prov_client.model; + param.start_rpr.rpr_srv_addr = addr; + + /* Let remote provisioning server start provisioning */ + err = esp_ble_mesh_rpr_client_action(ESP_BLE_MESH_RPR_CLIENT_ACT_START_RPR, + ¶m); + if (err) { + ESP_LOGE(TAG, "Failed to perform Remote Provisioning Client action: Start Prov"); + } + board_led_operation(LED_G, LED_ON); + break; + default: + ESP_LOGI(TAG, "Remote Provisioning Server(addr: 0x%04x) Status error", addr); + break; + } + } else { + ESP_LOGW(TAG, "Remote Provisioning Server(addr: 0x%04x) Link open fail"); + } + break; + } + break; +``` +Once the Remote Provisioning Server successfully establishes a Provisioning Link with the Unprovisioning Device, the network provisioning process can be initiated by setting the API `esp_ble_mesh_rpr_client_action` to `ESP_BLE_MESH_RPR_CLIENT_ACT_START_RPR` and passing the address of the Remote Provisioning Server. + +After the network provisioning is completed, the Remote Provisioning Client triggers the event `ESP_BLE_MESH_RPR_CLIENT_PROV_COMP_EVT`, which returns data about the network information of the nodes entering the network through Remote Provisioning. This includes node UUID, node address, and number of elements in the node. + +Once the network provisioning is completed, the Remote Provisioning Client sends `ESP_BLE_MESH_MODEL_OP_RPR_LINK_CLOSE` to the Remote Provisioning Server to close the Provisioning Link. It also configures the nodes entering the network through Remote Provisioning by obtaining their Composition Data, AppKey Add, and AppKey Bind. + +#### Remote Provisioning Server +---- + +To register and declare the Remote Provision Server Model, the remote_provisioning_server should be implemented as follows: +```c +static esp_ble_mesh_model_t root_models[] = { +#if CONFIG_BLE_MESH_RPR_SRV + ESP_BLE_MESH_MODEL_RPR_SRV(NULL), // Register Remote Provisioning Server +#endif + ESP_BLE_MESH_MODEL_CFG_SRV(&config_server), + ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_0, &onoff_server_0), +}; +``` +In the `ble_mesh_init` function, register the callback function for the Remote Provisioning Server as follows: +```c +static esp_err_t ble_mesh_init(void) +{ + esp_err_t err = ESP_OK; + + esp_ble_mesh_register_prov_callback(example_ble_mesh_provisioning_cb); + esp_ble_mesh_register_config_server_callback(example_ble_mesh_config_server_cb); + esp_ble_mesh_register_generic_server_callback(example_ble_mesh_generic_server_cb); + esp_ble_mesh_register_rpr_server_callback(example_remote_prov_server_callback); +} +``` +Since all the related functionalities of Remote Provisioning Server are handled by the protocol stack, the callback function `example_remote_prov_server_callback` of Remote Provisioning Server is only used to indicate the current step. The implementation is as follows: +```c +static void example_remote_prov_server_callback(esp_ble_mesh_rpr_server_cb_event_t event, + esp_ble_mesh_rpr_server_cb_param_t *param) +{ + switch (event) { + case ESP_BLE_MESH_RPR_SERVER_SCAN_START_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_SCAN_START_EVT"); + print_scan_start_evt(param); + break; + case ESP_BLE_MESH_RPR_SERVER_SCAN_STOP_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_SCAN_STOP_EVT"); + print_scan_stop_evt(param); + break; + case ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_START_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_START_EVT"); + print_ext_scan_start_evt(param); + break; + case ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_STOP_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_EXT_SCAN_STOP_EVT"); + print_ext_scan_stop_evt(param); + break; + case ESP_BLE_MESH_RPR_SERVER_LINK_OPEN_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_LINK_OPEN_EVT"); + print_link_open_evt(param); + board_led_operation_rmt(LED_OFF, LED_OFF, LED_ON); + break; + case ESP_BLE_MESH_RPR_SERVER_LINK_CLOSE_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_LINK_CLOSE_EVT"); + board_led_operation_rmt(LED_OFF, LED_OFF, LED_OFF); + print_link_close_evt(param); + break; + case ESP_BLE_MESH_RPR_SERVER_PROV_COMP_EVT: + ESP_LOGW(TAG, "ESP_BLE_MESH_RPR_SERVER_PROV_COMP_EVT"); + print_prov_comp_evt(param); + break; + default: + break; + } +} +``` +#### Unprovisioned Device +---- +The Remote Provisioning functionality is transparent to Unprovisioning Device. Therefore, regardless of whether Provisioning is done through Remote Provisioning or directly by the Provisioner, it will trigger the same events on the Unprovisioning Device side. Hence, in this example, it is not explained in detail. \ No newline at end of file diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/tutorial/images/message_sequence.png b/examples/bluetooth/esp_ble_mesh/remote_provisioning/tutorial/images/message_sequence.png new file mode 100644 index 000000000000..ff52a660ae3a Binary files /dev/null and b/examples/bluetooth/esp_ble_mesh/remote_provisioning/tutorial/images/message_sequence.png differ diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/CMakeLists.txt new file mode 100644 index 000000000000..47d782116b7c --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/CMakeLists.txt @@ -0,0 +1,10 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/light_driver + $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_init + $ENV{IDF_PATH}/examples/bluetooth/esp_ble_mesh/common_components/example_nvs) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(onoff_server) diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/CMakeLists.txt new file mode 100644 index 000000000000..13a56c69f2d8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/CMakeLists.txt @@ -0,0 +1,5 @@ +set(srcs "main.c" + "board.c") + +idf_component_register(SRCS "${srcs}" + INCLUDE_DIRS ".") diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/Kconfig.projbuild b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/Kconfig.projbuild new file mode 100644 index 000000000000..cbab996ec253 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/Kconfig.projbuild @@ -0,0 +1,38 @@ +menu "Example Configuration" + + choice BLE_MESH_EXAMPLE_BOARD + prompt "Board selection for BLE Mesh" + default BLE_MESH_ESP_WROOM_32 if IDF_TARGET_ESP32 + default BLE_MESH_ESP32C3_DEV if IDF_TARGET_ESP32C3 + default BLE_MESH_ESP32S3_DEV if IDF_TARGET_ESP32S3 + default BLE_MESH_ESP32C6_DEV if IDF_TARGET_ESP32C6 + help + Select this option to choose the board for BLE Mesh. The default is ESP32-WROOM-32 + + config BLE_MESH_ESP_WROOM_32 + bool "ESP32-WROOM-32" + depends on IDF_TARGET_ESP32 + + config BLE_MESH_ESP_WROVER + bool "ESP32-WROVER" + depends on IDF_TARGET_ESP32 + + config BLE_MESH_ESP32C3_DEV + bool "ESP32C3-DevKitC" + depends on IDF_TARGET_ESP32C3 + + config BLE_MESH_ESP32S3_DEV + bool "ESP32S3-DevKitC" + depends on IDF_TARGET_ESP32S3 + + config BLE_MESH_ESP32C6_DEV + bool "ESP32C6-DevKitC" + depends on IDF_TARGET_ESP32C6 + + config BLE_MESH_ESP32H2_DEV + bool "ESP32H2-DevKitC" + depends on IDF_TARGET_ESP32H2 + + endchoice + +endmenu diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/board.c b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/board.c new file mode 100644 index 000000000000..c729b6113b22 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/board.c @@ -0,0 +1,44 @@ +/* board.c - Board-specific hooks */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "driver/gpio.h" +#include "esp_log.h" +#include "board.h" + +#define TAG "BOARD" + +void board_led_operation(uint8_t r, uint8_t g, uint8_t b) +{ +#ifdef BLE_MESH_LED_STRIP_IO + rmt_led_set(r, g, b); +#else + gpio_set_level(LED_R, r); + gpio_set_level(LED_G, g); + gpio_set_level(LED_B, b); +#endif +} + +static void board_led_init(void) +{ +#ifdef BLE_MESH_LED_STRIP_IO + rmt_encoder_init(); + rmt_led_set(LED_OFF,LED_OFF,LED_OFF); +#else + gpio_set_level(LED_R, LED_OFF); + gpio_set_level(LED_G, LED_OFF); + gpio_set_level(LED_B, LED_OFF); +#endif +} + +void board_init(void) +{ + board_led_init(); +} diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/board.h b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/board.h new file mode 100644 index 000000000000..e754f6d95cce --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/board.h @@ -0,0 +1,51 @@ +/* board.h - Board-specific hooks */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BOARD_H_ +#define _BOARD_H_ + +#include "driver/gpio.h" +#include "led_strip_encoder.h" + +#if defined(CONFIG_BLE_MESH_ESP_WROOM_32) +#define LED_R GPIO_NUM_25 +#define LED_G GPIO_NUM_26 +#define LED_B GPIO_NUM_27 +#elif defined(CONFIG_BLE_MESH_ESP_WROVER) +#define LED_R GPIO_NUM_0 +#define LED_G GPIO_NUM_2 +#define LED_B GPIO_NUM_4 +#elif defined(CONFIG_BLE_MESH_ESP32C3_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8 +#elif defined(CONFIG_BLE_MESH_ESP32S3_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_47 +#elif defined(CONFIG_BLE_MESH_ESP32C6_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8 +#elif defined(CONFIG_BLE_MESH_ESP32H2_DEV) +#define BLE_MESH_LED_STRIP_IO GPIO_NUM_8 +#endif + +#define BUTTON_IO_NUM GPIO_NUM_9 + +#ifndef BLE_MESH_LED_STRIP_IO +#define LED_ON 1 +#else +#define LED_R 0 +#define LED_G 1 +#define LED_B 2 +#define LED_ON 100 +#endif + +#define LED_OFF 0 + +void board_led_operation(uint8_t r, uint8_t g, uint8_t b); + +void board_init(void); + +#endif diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/main.c b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/main.c new file mode 100644 index 000000000000..d5baf7436b27 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/main/main.c @@ -0,0 +1,343 @@ +/* main.c - Application main entry point */ + +/* + * SPDX-FileCopyrightText: 2017 Intel Corporation + * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#include "esp_log.h" +#include "nvs_flash.h" + +#include "esp_ble_mesh_defs.h" +#include "esp_ble_mesh_common_api.h" +#include "esp_ble_mesh_networking_api.h" +#include "esp_ble_mesh_provisioning_api.h" +#include "esp_ble_mesh_config_model_api.h" +#include "esp_ble_mesh_generic_model_api.h" +#include "esp_ble_mesh_local_data_operation_api.h" + +#include "board.h" +#include "ble_mesh_example_init.h" + +#define TAG "EXAMPLE" + +#define CID_ESP 0x02E5 + +static uint8_t dev_uuid[16] = { 0x55, 0x55 }; + +static esp_ble_mesh_cfg_srv_t config_server = { + .relay = ESP_BLE_MESH_RELAY_DISABLED, + .beacon = ESP_BLE_MESH_BEACON_ENABLED, +#if defined(CONFIG_BLE_MESH_FRIEND) + .friend_state = ESP_BLE_MESH_FRIEND_ENABLED, +#else + .friend_state = ESP_BLE_MESH_FRIEND_NOT_SUPPORTED, +#endif +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_ENABLED, +#else + .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_NOT_SUPPORTED, +#endif + .default_ttl = 7, + /* 3 transmissions with 20ms interval */ + .net_transmit = ESP_BLE_MESH_TRANSMIT(2, 20), + .relay_retransmit = ESP_BLE_MESH_TRANSMIT(2, 20), +}; + +ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_pub_0, 2 + 3, ROLE_NODE); +static esp_ble_mesh_gen_onoff_srv_t onoff_server_0 = { + .rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP, + .rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP, +}; + +ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_pub_1, 2 + 3, ROLE_NODE); +static esp_ble_mesh_gen_onoff_srv_t onoff_server_1 = { + .rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_RSP_BY_APP, + .rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_RSP_BY_APP, +}; + +ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_pub_2, 2 + 3, ROLE_NODE); +static esp_ble_mesh_gen_onoff_srv_t onoff_server_2 = { + .rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_AUTO_RSP, + .rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_RSP_BY_APP, +}; + +static esp_ble_mesh_model_t root_models[] = { + ESP_BLE_MESH_MODEL_CFG_SRV(&config_server), + ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_0, &onoff_server_0), +}; + +static esp_ble_mesh_model_t extend_model_0[] = { + ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_1, &onoff_server_1), +}; + +static esp_ble_mesh_model_t extend_model_1[] = { + ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_2, &onoff_server_2), +}; + +static esp_ble_mesh_elem_t elements[] = { + ESP_BLE_MESH_ELEMENT(0, root_models, ESP_BLE_MESH_MODEL_NONE), + ESP_BLE_MESH_ELEMENT(0, extend_model_0, ESP_BLE_MESH_MODEL_NONE), + ESP_BLE_MESH_ELEMENT(0, extend_model_1, ESP_BLE_MESH_MODEL_NONE), +}; + +static esp_ble_mesh_comp_t composition = { + .cid = CID_ESP, + .elements = elements, + .element_count = ARRAY_SIZE(elements), +}; + +/* Disable OOB security for SILabs Android app */ +static esp_ble_mesh_prov_t provision = { + .uuid = dev_uuid, +#if 0 + .output_size = 4, + .output_actions = ESP_BLE_MESH_DISPLAY_NUMBER, + .input_actions = ESP_BLE_MESH_PUSH, + .input_size = 4, +#else + .output_size = 0, + .output_actions = 0, +#endif +}; + +static void prov_complete(uint16_t net_idx, uint16_t addr, uint8_t flags, uint32_t iv_index) +{ + ESP_LOGI(TAG, "net_idx: 0x%04x, addr: 0x%04x", net_idx, addr); + ESP_LOGI(TAG, "flags: 0x%02x, iv_index: 0x%08" PRIx32, flags, iv_index); +} + +static void example_change_led_state(esp_ble_mesh_model_t *model, + esp_ble_mesh_msg_ctx_t *ctx, uint8_t onoff) +{ + uint16_t primary_addr = esp_ble_mesh_get_primary_element_address(); + uint8_t elem_count = esp_ble_mesh_get_element_count(); + uint8_t rgb[3] = {0}; + uint8_t i; + + if (ESP_BLE_MESH_ADDR_IS_UNICAST(ctx->recv_dst)) { + for (i = 0; i < elem_count; i++) { + if (ctx->recv_dst == (primary_addr + i)) { + rgb[i] = onoff ? LED_ON : LED_OFF; + } + } + } else if (ESP_BLE_MESH_ADDR_IS_GROUP(ctx->recv_dst)) { + if (esp_ble_mesh_is_model_subscribed_to_group(model, ctx->recv_dst)) { + rgb[model->element->element_addr - primary_addr] = onoff ? LED_ON : LED_OFF; + } + } else if (ctx->recv_dst == 0xFFFF) { + rgb[0] = onoff ? LED_ON : LED_OFF; + rgb[1] = onoff ? LED_ON : LED_OFF; + rgb[2] = onoff ? LED_ON : LED_OFF; + } + + board_led_operation(rgb[0], rgb[1], rgb[2]); +} + +static void example_handle_gen_onoff_msg(esp_ble_mesh_model_t *model, + esp_ble_mesh_msg_ctx_t *ctx, + esp_ble_mesh_server_recv_gen_onoff_set_t *set) +{ + esp_ble_mesh_gen_onoff_srv_t *srv = model->user_data; + + switch (ctx->recv_op) { + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET: + esp_ble_mesh_server_model_send_msg(model, ctx, + ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS, sizeof(srv->state.onoff), &srv->state.onoff); + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK: + if (set->op_en == false) { + srv->state.onoff = set->onoff; + } else { + /* TODO: Delay and state transition */ + srv->state.onoff = set->onoff; + } + if (ctx->recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { + esp_ble_mesh_server_model_send_msg(model, ctx, + ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS, sizeof(srv->state.onoff), &srv->state.onoff); + } + esp_ble_mesh_model_publish(model, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS, + sizeof(srv->state.onoff), &srv->state.onoff, ROLE_NODE); + example_change_led_state(model, ctx, srv->state.onoff); + break; + default: + break; + } +} + +static void example_ble_mesh_provisioning_cb(esp_ble_mesh_prov_cb_event_t event, + esp_ble_mesh_prov_cb_param_t *param) +{ + switch (event) { + case ESP_BLE_MESH_PROV_REGISTER_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_PROV_REGISTER_COMP_EVT, err_code %d", param->prov_register_comp.err_code); + break; + case ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT, err_code %d", param->node_prov_enable_comp.err_code); + break; + case ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT, bearer %s", + param->node_prov_link_open.bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT"); + board_led_operation(LED_OFF, LED_ON, LED_OFF); + break; + case ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT, bearer %s", + param->node_prov_link_close.bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT"); + break; + case ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT"); + prov_complete(param->node_prov_complete.net_idx, param->node_prov_complete.addr, + param->node_prov_complete.flags, param->node_prov_complete.iv_index); + break; + case ESP_BLE_MESH_NODE_PROV_RESET_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_RESET_EVT"); + break; + case ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT, err_code %d", param->node_set_unprov_dev_name_comp.err_code); + break; + default: + break; + } +} + +static void example_ble_mesh_generic_server_cb(esp_ble_mesh_generic_server_cb_event_t event, + esp_ble_mesh_generic_server_cb_param_t *param) +{ + esp_ble_mesh_gen_onoff_srv_t *srv; + ESP_LOGI(TAG, "event 0x%02x, opcode 0x%04" PRIx32 ", src 0x%04x, dst 0x%04x", + event, param->ctx.recv_op, param->ctx.addr, param->ctx.recv_dst); + + switch (event) { + case ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT"); + if (param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET || + param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK) { + ESP_LOGI(TAG, "onoff 0x%02x", param->value.state_change.onoff_set.onoff); + example_change_led_state(param->model, ¶m->ctx, param->value.state_change.onoff_set.onoff); + } + break; + case ESP_BLE_MESH_GENERIC_SERVER_RECV_GET_MSG_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_GENERIC_SERVER_RECV_GET_MSG_EVT"); + if (param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET) { + srv = param->model->user_data; + ESP_LOGI(TAG, "onoff 0x%02x", srv->state.onoff); + example_handle_gen_onoff_msg(param->model, ¶m->ctx, NULL); + } + break; + case ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT: + ESP_LOGI(TAG, "ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT"); + if (param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET || + param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK) { + ESP_LOGI(TAG, "onoff 0x%02x, tid 0x%02x", param->value.set.onoff.onoff, param->value.set.onoff.tid); + if (param->value.set.onoff.op_en) { + ESP_LOGI(TAG, "trans_time 0x%02x, delay 0x%02x", + param->value.set.onoff.trans_time, param->value.set.onoff.delay); + } + example_handle_gen_onoff_msg(param->model, ¶m->ctx, ¶m->value.set.onoff); + } + break; + default: + ESP_LOGE(TAG, "Unknown Generic Server event 0x%02x", event); + break; + } +} + +static void example_ble_mesh_config_server_cb(esp_ble_mesh_cfg_server_cb_event_t event, + esp_ble_mesh_cfg_server_cb_param_t *param) +{ + if (event == ESP_BLE_MESH_CFG_SERVER_STATE_CHANGE_EVT) { + switch (param->ctx.recv_op) { + case ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD: + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD"); + ESP_LOGI(TAG, "net_idx 0x%04x, app_idx 0x%04x", + param->value.state_change.appkey_add.net_idx, + param->value.state_change.appkey_add.app_idx); + ESP_LOG_BUFFER_HEX("AppKey", param->value.state_change.appkey_add.app_key, 16); + break; + case ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND: + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND"); + ESP_LOGI(TAG, "elem_addr 0x%04x, app_idx 0x%04x, cid 0x%04x, mod_id 0x%04x", + param->value.state_change.mod_app_bind.element_addr, + param->value.state_change.mod_app_bind.app_idx, + param->value.state_change.mod_app_bind.company_id, + param->value.state_change.mod_app_bind.model_id); + + board_led_operation(LED_OFF, LED_OFF, LED_OFF); + break; + case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD: + ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD"); + ESP_LOGI(TAG, "elem_addr 0x%04x, sub_addr 0x%04x, cid 0x%04x, mod_id 0x%04x", + param->value.state_change.mod_sub_add.element_addr, + param->value.state_change.mod_sub_add.sub_addr, + param->value.state_change.mod_sub_add.company_id, + param->value.state_change.mod_sub_add.model_id); + break; + default: + break; + } + } +} + +static esp_err_t ble_mesh_init(void) +{ + esp_err_t err = ESP_OK; + + esp_ble_mesh_register_prov_callback(example_ble_mesh_provisioning_cb); + esp_ble_mesh_register_config_server_callback(example_ble_mesh_config_server_cb); + esp_ble_mesh_register_generic_server_callback(example_ble_mesh_generic_server_cb); + + err = esp_ble_mesh_init(&provision, &composition); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize mesh stack (err %d)", err); + return err; + } + + err = esp_ble_mesh_node_prov_enable(ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to enable mesh node (err %d)", err); + return err; + } + + ESP_LOGI(TAG, "BLE Mesh Node initialized"); + + board_led_operation(LED_ON, LED_OFF, LED_OFF); + + return err; +} + +void app_main(void) +{ + esp_err_t err; + + ESP_LOGI(TAG, "Initializing..."); + + board_init(); + + err = nvs_flash_init(); + if (err == ESP_ERR_NVS_NO_FREE_PAGES) { + ESP_ERROR_CHECK(nvs_flash_erase()); + err = nvs_flash_init(); + } + ESP_ERROR_CHECK(err); + + err = bluetooth_init(); + if (err) { + ESP_LOGE(TAG, "esp32_bluetooth_init failed (err %d)", err); + return; + } + + ble_mesh_get_dev_uuid(dev_uuid); + + /* Initialize the Bluetooth Mesh Subsystem */ + err = ble_mesh_init(); + if (err) { + ESP_LOGE(TAG, "Bluetooth mesh init failed (err %d)", err); + } +} diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults new file mode 100644 index 000000000000..64b473336510 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults @@ -0,0 +1,19 @@ +# Override some defaults so BT stack is enabled +# by default in this example +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_CTRL_BTDM_MODEM_SLEEP=n +CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults.esp32c3 b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults.esp32c3 new file mode 100644 index 000000000000..ad04a937e2e8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults.esp32c3 @@ -0,0 +1,16 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults.esp32c6 b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults.esp32c6 new file mode 100644 index 000000000000..78299bedf8d8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults.esp32c6 @@ -0,0 +1,18 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_BT_BLE_50_FEATURES_SUPPORTED=n +CONFIG_BT_LE_50_FEATURE_SUPPORT=n +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults.esp32h2 b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults.esp32h2 new file mode 100644 index 000000000000..78299bedf8d8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults.esp32h2 @@ -0,0 +1,18 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_BT_BLE_50_FEATURES_SUPPORTED=n +CONFIG_BT_LE_50_FEATURE_SUPPORT=n +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 diff --git a/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults.esp32s3 b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults.esp32s3 new file mode 100644 index 000000000000..ad04a937e2e8 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/remote_provisioning/unprov_dev/sdkconfig.defaults.esp32s3 @@ -0,0 +1,16 @@ +# Override some defaults so BT stack is enabled +# by default in this example +CONFIG_BT_ENABLED=y +CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BT_CTRL_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y + +# 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 diff --git a/examples/bluetooth/esp_ble_mesh/sensor_models/sensor_client/main/main.c b/examples/bluetooth/esp_ble_mesh/sensor_models/sensor_client/main/main.c index 04eab7fee255..fd319b53e045 100644 --- a/examples/bluetooth/esp_ble_mesh/sensor_models/sensor_client/main/main.c +++ b/examples/bluetooth/esp_ble_mesh/sensor_models/sensor_client/main/main.c @@ -31,7 +31,6 @@ #define PROV_OWN_ADDR 0x0001 #define MSG_SEND_TTL 3 -#define MSG_SEND_REL false #define MSG_TIMEOUT 0 #define MSG_ROLE ROLE_PROVISIONER @@ -101,9 +100,7 @@ static void example_ble_mesh_set_msg_common(esp_ble_mesh_client_common_param_t * common->ctx.app_idx = prov_key.app_idx; common->ctx.addr = node->unicast_addr; common->ctx.send_ttl = MSG_SEND_TTL; - common->ctx.send_rel = MSG_SEND_REL; common->msg_timeout = MSG_TIMEOUT; - common->msg_role = MSG_ROLE; } static esp_err_t prov_complete(uint16_t node_index, const esp_ble_mesh_octet16_t uuid, diff --git a/examples/bluetooth/esp_ble_mesh/sensor_models/sensor_client/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/sensor_models/sensor_client/sdkconfig.defaults index 5b7f9f62227d..35516e8fa32d 100644 --- a/examples/bluetooth/esp_ble_mesh/sensor_models/sensor_client/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/sensor_models/sensor_client/sdkconfig.defaults @@ -8,6 +8,7 @@ CONFIG_CTRL_BTDM_MODEM_SLEEP=n CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y # Override some defaults of ESP BLE Mesh CONFIG_BLE_MESH=y diff --git a/examples/bluetooth/esp_ble_mesh/sensor_models/sensor_server/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/sensor_models/sensor_server/sdkconfig.defaults index f285ddba6256..64b473336510 100644 --- a/examples/bluetooth/esp_ble_mesh/sensor_models/sensor_server/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/sensor_models/sensor_server/sdkconfig.defaults @@ -9,6 +9,7 @@ CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y # Override some defaults of ESP BLE Mesh CONFIG_BLE_MESH=y diff --git a/examples/bluetooth/esp_ble_mesh/vendor_models/vendor_client/main/main.c b/examples/bluetooth/esp_ble_mesh/vendor_models/vendor_client/main/main.c index 75e5ba224e1f..461177a379d4 100644 --- a/examples/bluetooth/esp_ble_mesh/vendor_models/vendor_client/main/main.c +++ b/examples/bluetooth/esp_ble_mesh/vendor_models/vendor_client/main/main.c @@ -33,7 +33,6 @@ #define PROV_OWN_ADDR 0x0001 #define MSG_SEND_TTL 3 -#define MSG_SEND_REL false #define MSG_TIMEOUT 0 #define MSG_ROLE ROLE_PROVISIONER @@ -155,9 +154,7 @@ static void example_ble_mesh_set_msg_common(esp_ble_mesh_client_common_param_t * common->ctx.app_idx = prov_key.app_idx; common->ctx.addr = node->unicast_addr; common->ctx.send_ttl = MSG_SEND_TTL; - common->ctx.send_rel = MSG_SEND_REL; common->msg_timeout = MSG_TIMEOUT; - common->msg_role = MSG_ROLE; } static esp_err_t prov_complete(uint16_t node_index, const esp_ble_mesh_octet16_t uuid, @@ -460,7 +457,6 @@ void example_ble_mesh_send_vendor_message(bool resend) ctx.app_idx = prov_key.app_idx; ctx.addr = store.server_addr; ctx.send_ttl = MSG_SEND_TTL; - ctx.send_rel = MSG_SEND_REL; opcode = ESP_BLE_MESH_VND_MODEL_OP_SEND; if (resend == false) { diff --git a/examples/bluetooth/esp_ble_mesh/vendor_models/vendor_client/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/vendor_models/vendor_client/sdkconfig.defaults index 9cb1eea6e343..dce152c118ca 100644 --- a/examples/bluetooth/esp_ble_mesh/vendor_models/vendor_client/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/vendor_models/vendor_client/sdkconfig.defaults @@ -8,6 +8,7 @@ CONFIG_CTRL_BTDM_MODEM_SLEEP=n CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y # Override some defaults of ESP BLE Mesh CONFIG_BLE_MESH=y diff --git a/examples/bluetooth/esp_ble_mesh/vendor_models/vendor_server/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/vendor_models/vendor_server/sdkconfig.defaults index 7f319e03d136..54f6e7e334e4 100644 --- a/examples/bluetooth/esp_ble_mesh/vendor_models/vendor_server/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/vendor_models/vendor_server/sdkconfig.defaults @@ -9,6 +9,7 @@ CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y CONFIG_BT_BTU_TASK_STACK_SIZE=4512 +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y # Override some defaults of ESP BLE Mesh CONFIG_BLE_MESH=y diff --git a/examples/bluetooth/esp_ble_mesh/wifi_coexist/main/main.c b/examples/bluetooth/esp_ble_mesh/wifi_coexist/main/main.c index c2630ccde4f7..1c8f6d45cf12 100644 --- a/examples/bluetooth/esp_ble_mesh/wifi_coexist/main/main.c +++ b/examples/bluetooth/esp_ble_mesh/wifi_coexist/main/main.c @@ -294,7 +294,6 @@ static void provisioner_prov_complete(int node_idx, const uint8_t uuid[16], uint .app_idx = node->app_idx, .dst = node->unicast_addr, .timeout = 0, - .role = ROLE_FAST_PROV, }; err = example_send_config_appkey_add(config_client.model, &info, NULL); if (err != ESP_OK) { @@ -618,7 +617,6 @@ static void example_ble_mesh_config_client_cb(esp_ble_mesh_cfg_client_cb_event_t .app_idx = node->app_idx, .dst = node->unicast_addr, .timeout = 0, - .role = ROLE_FAST_PROV, }; err = example_send_fast_prov_info_set(fast_prov_client.model, &info, &set); if (err != ESP_OK) { @@ -641,7 +639,6 @@ static void example_ble_mesh_config_client_cb(esp_ble_mesh_cfg_client_cb_event_t .app_idx = node->app_idx, .dst = node->unicast_addr, .timeout = 0, - .role = ROLE_FAST_PROV, }; err = example_send_config_appkey_add(config_client.model, &info, NULL); if (err != ESP_OK) { diff --git a/tools/ci/executable-list.txt b/tools/ci/executable-list.txt index 1509cd8bdba8..bde19be313b9 100644 --- a/tools/ci/executable-list.txt +++ b/tools/ci/executable-list.txt @@ -1,4 +1,5 @@ components/app_update/otatool.py +components/bt/esp_ble_mesh/v1.1/lib/lib_copy.sh components/efuse/efuse_table_gen.py components/efuse/test_efuse_host/efuse_tests.py components/esp_coex/test_md5/test_md5.sh diff --git a/tools/test_apps/system/no_embedded_paths/sdkconfig.ci.noasserts.nimble b/tools/test_apps/system/no_embedded_paths/sdkconfig.ci.noasserts.nimble index 43d3b3621ded..a1367f9c9f46 100644 --- a/tools/test_apps/system/no_embedded_paths/sdkconfig.ci.noasserts.nimble +++ b/tools/test_apps/system/no_embedded_paths/sdkconfig.ci.noasserts.nimble @@ -8,6 +8,5 @@ CONFIG_COMPILER_HIDE_PATHS_MACROS=n CONFIG_BT_ENABLED=y CONFIG_BT_NIMBLE_ENABLED=y CONFIG_BT_NIMBLE_CRYPTO_STACK_MBEDTLS=n -CONFIG_BT_NIMBLE_MESH=y CONFIG_BLE_MESH=y CONFIG_BT_NIMBLE_MAX_CONNECTIONS=1 diff --git a/tools/test_apps/system/no_embedded_paths/sdkconfig.ci.silentasserts.nimble b/tools/test_apps/system/no_embedded_paths/sdkconfig.ci.silentasserts.nimble index 384215fbb62d..d60f1630dfeb 100644 --- a/tools/test_apps/system/no_embedded_paths/sdkconfig.ci.silentasserts.nimble +++ b/tools/test_apps/system/no_embedded_paths/sdkconfig.ci.silentasserts.nimble @@ -8,6 +8,5 @@ CONFIG_COMPILER_HIDE_PATHS_MACROS=n CONFIG_BT_ENABLED=y CONFIG_BT_NIMBLE_ENABLED=y CONFIG_BT_NIMBLE_CRYPTO_STACK_MBEDTLS=n -CONFIG_BT_NIMBLE_MESH=y CONFIG_BLE_MESH=y CONFIG_BT_NIMBLE_MAX_CONNECTIONS=1