소스 검색

components/bt: Add support of NimBLE host in ESP BLE Mesh

Hrishikesh Dhayagude 6 년 전
부모
커밋
5cb6a49c1e
59개의 변경된 파일3065개의 추가작업 그리고 474개의 파일을 삭제
  1. 9 2
      components/bt/CMakeLists.txt
  2. 0 1
      components/bt/Kconfig
  3. 3 1
      components/bt/common/btc/core/btc_task.c
  4. 0 2
      components/bt/common/btc/include/btc/btc_manage.h
  5. 1 1
      components/bt/common/include/bt_user_config.h
  6. 33 17
      components/bt/component.mk
  7. 1 0
      components/bt/esp_ble_mesh/Kconfig.in
  8. 8 3
      components/bt/esp_ble_mesh/api/core/esp_ble_mesh_common_api.c
  9. 0 2
      components/bt/esp_ble_mesh/api/core/esp_ble_mesh_local_data_operation_api.c
  10. 0 2
      components/bt/esp_ble_mesh/api/core/esp_ble_mesh_low_power_api.c
  11. 7 9
      components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c
  12. 20 22
      components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c
  13. 3 5
      components/bt/esp_ble_mesh/api/core/esp_ble_mesh_proxy_api.c
  14. 1 1
      components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h
  15. 39 7
      components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h
  16. 4 7
      components/bt/esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c
  17. 3 6
      components/bt/esp_ble_mesh/api/models/esp_ble_mesh_generic_model_api.c
  18. 5 8
      components/bt/esp_ble_mesh/api/models/esp_ble_mesh_health_model_api.c
  19. 3 6
      components/bt/esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c
  20. 3 6
      components/bt/esp_ble_mesh/api/models/esp_ble_mesh_sensor_model_api.c
  21. 3 6
      components/bt/esp_ble_mesh/api/models/esp_ble_mesh_time_scene_model_api.c
  22. 1 1
      components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c
  23. 0 1
      components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h
  24. 1 1
      components/bt/esp_ble_mesh/mesh_core/adv.c
  25. 38 0
      components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c
  26. 17 0
      components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h
  27. 6 0
      components/bt/esp_ble_mesh/mesh_core/include/mesh_trace.h
  28. 0 45
      components/bt/esp_ble_mesh/mesh_core/mesh_hci.c
  29. 1 2
      components/bt/esp_ble_mesh/mesh_core/mesh_kernel.c
  30. 1796 0
      components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c
  31. 1 0
      components/bt/host/bluedroid/api/esp_gap_bt_api.c
  32. 1 0
      components/bt/host/nimble/Kconfig.in
  33. 1 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_client_model/main/CMakeLists.txt
  34. 143 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_client_model/main/ble_mesh_demo_init.c
  35. 18 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_client_model/main/ble_mesh_demo_init.h
  36. 10 46
      examples/bluetooth/esp_ble_mesh/ble_mesh_client_model/main/ble_mesh_demo_main.c
  37. 3 3
      examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/main/ble_mesh_register_provisioner_cmd.c
  38. 2 1
      examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/CMakeLists.txt
  39. 143 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_init.c
  40. 18 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_init.h
  41. 16 53
      examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_main.c
  42. 1 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/CMakeLists.txt
  43. 143 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_init.c
  44. 18 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_init.h
  45. 13 53
      examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_main.c
  46. 1 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_node/main/CMakeLists.txt
  47. 143 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_node/main/ble_mesh_demo_init.c
  48. 18 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_node/main/ble_mesh_demo_init.h
  49. 10 47
      examples/bluetooth/esp_ble_mesh/ble_mesh_node/main/ble_mesh_demo_main.c
  50. 1 1
      examples/bluetooth/esp_ble_mesh/ble_mesh_node/tutorial/Ble_Mesh_Node_Example_Walkthrough.md
  51. 2 1
      examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/CMakeLists.txt
  52. 143 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_init.c
  53. 18 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_init.h
  54. 16 51
      examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_main.c
  55. 1 1
      examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/tutorial/Ble_Mesh_Provisioner_Example_Walkthrough.md
  56. 1 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/CMakeLists.txt
  57. 143 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_init.c
  58. 18 0
      examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_init.h
  59. 13 53
      examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_main.c

+ 9 - 2
components/bt/CMakeLists.txt

@@ -289,6 +289,11 @@ if(CONFIG_BT_ENABLED)
                    "host/bluedroid/stack/smp/smp_l2c.c"
                    "host/bluedroid/stack/smp/smp_main.c"
                    "host/bluedroid/stack/smp/smp_utils.c")
+
+        if(CONFIG_BLE_MESH)
+            list(APPEND srcs "esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c")
+        endif()
+
     endif()
 
     if(CONFIG_BLE_MESH)
@@ -335,9 +340,7 @@ if(CONFIG_BT_ENABLED)
                     "esp_ble_mesh/mesh_core/lpn.c"
                     "esp_ble_mesh/mesh_core/mesh_aes_encrypt.c"
                     "esp_ble_mesh/mesh_core/mesh_atomic.c"
-                    "esp_ble_mesh/mesh_core/mesh_bearer_adapt.c"
                     "esp_ble_mesh/mesh_core/mesh_buf.c"
-                    "esp_ble_mesh/mesh_core/mesh_hci.c"
                     "esp_ble_mesh/mesh_core/mesh_kernel.c"
                     "esp_ble_mesh/mesh_core/mesh_main.c"
                     "esp_ble_mesh/mesh_core/mesh_util.c"
@@ -469,6 +472,10 @@ if(CONFIG_BT_ENABLED)
                     "host/nimble/nimble/porting/nimble/src/os_cputime.c"
                     "host/nimble/esp-hci/src/esp_nimble_hci.c")
 
+        if (CONFIG_BLE_MESH)
+            list(APPEND srcs "esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c")
+        endif()
+
         if(CONFIG_BT_NIMBLE_MESH)
 
             list(APPEND include_dirs

+ 0 - 1
components/bt/Kconfig

@@ -410,7 +410,6 @@ endmenu
 
 menuconfig BLE_MESH
     bool "ESP BLE Mesh Support"
-    depends on BT_BLUEDROID_ENABLED
     help
         This option enables ESP BLE Mesh support. The specific features that are
         available may depend on other features that have been enabled in the

+ 3 - 1
components/bt/common/btc/core/btc_task.c

@@ -19,6 +19,7 @@
 #include "esp_log.h"
 #include "bt_common.h"
 #include "osi/allocator.h"
+#include "btc/btc_alarm.h"
 
 #ifdef CONFIG_BT_BLUEDROID_ENABLED
 #include "common/bt_target.h"
@@ -32,7 +33,6 @@
 #include "btc_blufi_prf.h"
 #include "blufi_int.h"
 #include "btc/btc_dm.h"
-#include "btc/btc_alarm.h"
 #include "bta/bta_gatt_api.h"
 #if CLASSIC_BT_INCLUDED
 #include "btc/btc_profile_queue.h"
@@ -94,7 +94,9 @@ static const btc_func_t profile_tab[BTC_PID_NUM] = {
     [BTC_PID_BLUFI]       = {btc_blufi_call_handler,      btc_blufi_cb_handler    },
 #endif  ///GATTS_INCLUDED == TRUE
     [BTC_PID_DM_SEC]      = {NULL,                        btc_dm_sec_cb_handler   },
+#endif
     [BTC_PID_ALARM]       = {btc_alarm_handler,           NULL                    },
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
 #if CLASSIC_BT_INCLUDED
 #if (BTC_GAP_BT_INCLUDED == TRUE)
     [BTC_PID_GAP_BT]    = {btc_gap_bt_call_handler,     btc_gap_bt_cb_handler   },

+ 0 - 2
components/bt/common/btc/include/btc/btc_manage.h

@@ -15,9 +15,7 @@
 #ifndef __BTC_MANAGE_H__
 #define __BTC_MANAGE_H__
 
-#include "bta/bta_api.h"
 #include "btc/btc_task.h"
-#include "esp_bt_defs.h"
 
 #if BTC_DYNAMIC_MEMORY == FALSE
 extern void *btc_profile_cb_tab[BTC_PID_NUM];

+ 1 - 1
components/bt/common/include/bt_user_config.h

@@ -49,7 +49,7 @@
 #ifdef CONFIG_BTC_TASK_STACK_SIZE
 #define UC_BTC_TASK_STACK_SIZE              CONFIG_BTC_TASK_STACK_SIZE
 #else
-#define UC_BTC_TASK_STACK_SIZE              3072
+#define UC_BTC_TASK_STACK_SIZE              4096
 #endif
 
 /**********************************************************

+ 33 - 17
components/bt/component.mk

@@ -129,26 +129,31 @@ COMPONENT_PRIV_INCLUDEDIRS += common/btc/include              	   \
 COMPONENT_SRCDIRS += common/osi                         		   \
 					 common/btc/core
 
+ifdef CONFIG_BLE_MESH
+
+COMPONENT_SRCDIRS += esp_ble_mesh/mesh_core/bluedroid_host
+
+endif
 endif
 
 ifdef CONFIG_BLE_MESH
-    COMPONENT_ADD_INCLUDEDIRS += esp_ble_mesh/mesh_core                     \
-                                 esp_ble_mesh/mesh_core/include             \
-                                 esp_ble_mesh/mesh_core/settings            \
-                                 esp_ble_mesh/btc/include                   \
-                                 esp_ble_mesh/mesh_models/common/include    \
-                                 esp_ble_mesh/mesh_models/client/include    \
-                                 esp_ble_mesh/api/core/include              \
-                                 esp_ble_mesh/api/models/include            \
-                                 esp_ble_mesh/api
-
-    COMPONENT_SRCDIRS +=    esp_ble_mesh/mesh_core               \
-                            esp_ble_mesh/mesh_core/settings      \
-                            esp_ble_mesh/btc                     \
-                            esp_ble_mesh/mesh_models/common      \
-                            esp_ble_mesh/mesh_models/client      \
-                            esp_ble_mesh/api/core                \
-                            esp_ble_mesh/api/models
+COMPONENT_ADD_INCLUDEDIRS += esp_ble_mesh/mesh_core                     \
+                             esp_ble_mesh/mesh_core/include             \
+                             esp_ble_mesh/mesh_core/settings            \
+                             esp_ble_mesh/btc/include                   \
+                             esp_ble_mesh/mesh_models/common/include    \
+                             esp_ble_mesh/mesh_models/client/include    \
+                             esp_ble_mesh/api/core/include              \
+                             esp_ble_mesh/api/models/include            \
+                             esp_ble_mesh/api
+
+COMPONENT_SRCDIRS += esp_ble_mesh/mesh_core               	\
+                     esp_ble_mesh/mesh_core/settings      	\
+                     esp_ble_mesh/btc                     	\
+                     esp_ble_mesh/mesh_models/common      	\
+                     esp_ble_mesh/mesh_models/client      	\
+                     esp_ble_mesh/api/core                	\
+                     esp_ble_mesh/api/models
 endif
 
 
@@ -196,6 +201,17 @@ endif
 
 COMPONENT_OBJEXCLUDE += host/nimble/nimble/nimble/host/store/config/src/ble_store_config_conf.o
 
+ifdef CONFIG_BLE_MESH
+COMPONENT_PRIV_INCLUDEDIRS += common/btc/include 	\
+			      common/include
+
+COMPONENT_SRCDIRS += common/osi 		\
+					 common/btc/core 	\
+					 esp_ble_mesh/mesh_core/nimble_host
+
+COMPONENT_ADD_INCLUDEDIRS += common/osi/include
+endif
+
 ifdef CONFIG_BT_NIMBLE_MESH
 
 COMPONENT_ADD_INCLUDEDIRS += host/nimble/nimble/nimble/host/mesh/include

+ 1 - 0
components/bt/esp_ble_mesh/Kconfig.in

@@ -8,6 +8,7 @@ if BLE_MESH
 
     config BLE_MESH_USE_DUPLICATE_SCAN
         bool "Support Duplicate Scan in BLE Mesh"
+        depends on BT_BLUEDROID_ENABLED
         select BLE_SCAN_DUPLICATE
         select BLE_MESH_SCAN_DUPLICATE_EN
         default y

+ 8 - 3
components/bt/esp_ble_mesh/api/core/esp_ble_mesh_common_api.c

@@ -19,10 +19,9 @@
 
 #include "btc/btc_task.h"
 #include "btc/btc_manage.h"
+#include "osi/alarm.h"
 
 #include "esp_err.h"
-#include "esp_bt_defs.h"
-#include "esp_bt_main.h"
 
 #include "btc_ble_mesh_prov.h"
 #include "esp_ble_mesh_defs.h"
@@ -32,12 +31,18 @@ esp_err_t esp_ble_mesh_init(esp_ble_mesh_prov_t *prov, esp_ble_mesh_comp_t *comp
     btc_ble_mesh_prov_args_t arg = {0};
     SemaphoreHandle_t semaphore = NULL;
     btc_msg_t msg = {0};
+    esp_err_t ret;
 
     if (prov == NULL || comp == NULL) {
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
+
+    ret = bt_mesh_host_init();
+    if (ret != ESP_OK) {
+        return ret;
+    }
 
     // Create a semaphore
     if ((semaphore = xSemaphoreCreateCounting(1, 0)) == NULL) {

+ 0 - 2
components/bt/esp_ble_mesh/api/core/esp_ble_mesh_local_data_operation_api.c

@@ -19,8 +19,6 @@
 #include "btc/btc_manage.h"
 
 #include "esp_err.h"
-#include "esp_bt_defs.h"
-#include "esp_bt_main.h"
 
 #include "btc_ble_mesh_prov.h"
 #include "esp_ble_mesh_defs.h"

+ 0 - 2
components/bt/esp_ble_mesh/api/core/esp_ble_mesh_low_power_api.c

@@ -18,8 +18,6 @@
 #include "btc/btc_manage.h"
 
 #include "esp_err.h"
-#include "esp_bt_defs.h"
-#include "esp_bt_main.h"
 
 #include "btc_ble_mesh_prov.h"
 #include "esp_ble_mesh_defs.h"

+ 7 - 9
components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c

@@ -19,8 +19,6 @@
 #include "btc/btc_manage.h"
 
 #include "esp_err.h"
-#include "esp_bt_defs.h"
-#include "esp_bt_main.h"
 
 #include "btc_ble_mesh_prov.h"
 #include "esp_ble_mesh_networking_api.h"
@@ -41,7 +39,7 @@ static esp_err_t ble_mesh_send_msg(esp_ble_mesh_model_t *model,
     btc_msg_t msg = {0};
     esp_err_t status;
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     if (device_role > ROLE_FAST_PROV) {
         return ESP_ERR_INVALID_ARG;
@@ -118,7 +116,7 @@ static esp_err_t ble_mesh_send_msg(esp_ble_mesh_model_t *model,
 
 esp_err_t esp_ble_mesh_register_custom_model_callback(esp_ble_mesh_model_cb_t callback)
 {
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     return (btc_profile_cb_set(BTC_PID_MODEL, callback) == 0 ? ESP_OK : ESP_FAIL);
 }
@@ -198,7 +196,7 @@ esp_err_t esp_ble_mesh_node_local_reset(void)
 {
     btc_msg_t msg = {0};
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -218,7 +216,7 @@ esp_err_t esp_ble_mesh_provisioner_set_node_name(int index, const char *name)
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -251,7 +249,7 @@ esp_err_t esp_ble_mesh_provisioner_add_local_app_key(const uint8_t app_key[16],
     btc_ble_mesh_prov_args_t arg = {0};
     btc_msg_t msg = {0};
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -283,7 +281,7 @@ esp_err_t esp_ble_mesh_provisioner_bind_app_key_to_local_model(uint16_t element_
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -306,7 +304,7 @@ esp_err_t esp_ble_mesh_provisioner_add_local_net_key(const uint8_t net_key[16],
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;

+ 20 - 22
components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c

@@ -18,8 +18,6 @@
 #include "btc/btc_manage.h"
 
 #include "esp_err.h"
-#include "esp_bt_defs.h"
-#include "esp_bt_main.h"
 
 #include "btc_ble_mesh_prov.h"
 #include "esp_ble_mesh_provisioning_api.h"
@@ -29,7 +27,7 @@
 
 esp_err_t esp_ble_mesh_register_prov_callback(esp_ble_mesh_prov_cb_t callback)
 {
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     return (btc_profile_cb_set(BTC_PID_PROV, callback) == 0 ? ESP_OK : ESP_FAIL);
 }
@@ -44,7 +42,7 @@ esp_err_t esp_ble_mesh_node_prov_enable(esp_ble_mesh_prov_bearer_t bearers)
     btc_ble_mesh_prov_args_t arg = {0};
     btc_msg_t msg = {0};
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -60,7 +58,7 @@ esp_err_t esp_ble_mesh_node_prov_disable(esp_ble_mesh_prov_bearer_t bearers)
     btc_ble_mesh_prov_args_t arg = {0};
     btc_msg_t msg = {0};
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -81,7 +79,7 @@ esp_err_t esp_ble_mesh_node_set_oob_pub_key(uint8_t pub_key_x[32], uint8_t pub_k
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -104,7 +102,7 @@ esp_err_t esp_ble_mesh_node_input_number(uint32_t number)
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -124,7 +122,7 @@ esp_err_t esp_ble_mesh_node_input_string(const char *string)
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -145,7 +143,7 @@ esp_err_t esp_ble_mesh_set_unprovisioned_device_name(const char *name)
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -168,7 +166,7 @@ esp_err_t esp_ble_mesh_provisioner_read_oob_pub_key(uint8_t link_idx, uint8_t pu
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -191,7 +189,7 @@ esp_err_t esp_ble_mesh_provisioner_input_string(const char *string, uint8_t link
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -214,7 +212,7 @@ esp_err_t esp_ble_mesh_provisioner_input_number(uint32_t number, uint8_t link_id
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -232,7 +230,7 @@ esp_err_t esp_ble_mesh_provisioner_prov_enable(esp_ble_mesh_prov_bearer_t bearer
     btc_ble_mesh_prov_args_t arg = {0};
     btc_msg_t msg = {0};
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -249,7 +247,7 @@ esp_err_t esp_ble_mesh_provisioner_prov_disable(esp_ble_mesh_prov_bearer_t beare
     btc_ble_mesh_prov_args_t arg = {0};
     btc_msg_t msg = {0};
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -271,7 +269,7 @@ esp_err_t esp_ble_mesh_provisioner_add_unprov_dev(esp_ble_mesh_unprov_dev_add_t
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -280,7 +278,7 @@ esp_err_t esp_ble_mesh_provisioner_add_unprov_dev(esp_ble_mesh_unprov_dev_add_t
     arg.provisioner_dev_add.add_dev.addr_type = add_dev->addr_type;
     arg.provisioner_dev_add.add_dev.oob_info = add_dev->oob_info;
     arg.provisioner_dev_add.add_dev.bearer = add_dev->bearer;
-    memcpy(arg.provisioner_dev_add.add_dev.addr, add_dev->addr, sizeof(esp_bd_addr_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)
@@ -297,7 +295,7 @@ esp_err_t esp_ble_mesh_provisioner_delete_dev(esp_ble_mesh_device_delete_t *del_
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -306,7 +304,7 @@ esp_err_t esp_ble_mesh_provisioner_delete_dev(esp_ble_mesh_device_delete_t *del_
     arg.provisioner_dev_del.del_dev.flag = del_dev->flag;
     if (del_dev->flag & DEL_DEV_ADDR_FLAG) {
         arg.provisioner_dev_del.del_dev.addr_type = del_dev->addr_type;
-        memcpy(arg.provisioner_dev_del.del_dev.addr, del_dev->addr, sizeof(esp_bd_addr_t));
+        memcpy(arg.provisioner_dev_del.del_dev.addr, del_dev->addr, sizeof(esp_ble_mesh_bd_addr_t));
     } else if (del_dev->flag & DEL_DEV_UUID_FLAG) {
         memcpy(arg.provisioner_dev_del.del_dev.uuid, del_dev->uuid, 16);
     }
@@ -320,7 +318,7 @@ esp_err_t esp_ble_mesh_provisioner_set_dev_uuid_match(const uint8_t *match_val,
     btc_ble_mesh_prov_args_t arg = {0};
     btc_msg_t msg = {0};
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -346,7 +344,7 @@ esp_err_t esp_ble_mesh_provisioner_set_prov_data_info(esp_ble_mesh_prov_data_inf
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -379,7 +377,7 @@ esp_err_t esp_ble_mesh_set_fast_prov_info(esp_ble_mesh_fast_prov_info_t *fast_pr
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -408,7 +406,7 @@ esp_err_t esp_ble_mesh_set_fast_prov_action(esp_ble_mesh_fast_prov_action_t acti
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;

+ 3 - 5
components/bt/esp_ble_mesh/api/core/esp_ble_mesh_proxy_api.c

@@ -18,8 +18,6 @@
 #include "btc/btc_manage.h"
 
 #include "esp_err.h"
-#include "esp_bt_defs.h"
-#include "esp_bt_main.h"
 
 #include "btc_ble_mesh_prov.h"
 #include "esp_ble_mesh_defs.h"
@@ -28,7 +26,7 @@ esp_err_t esp_ble_mesh_proxy_identity_enable(void)
 {
     btc_msg_t msg = {0};
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -41,7 +39,7 @@ esp_err_t esp_ble_mesh_proxy_gatt_enable(void)
 {
     btc_msg_t msg = {0};
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;
@@ -54,7 +52,7 @@ esp_err_t esp_ble_mesh_proxy_gatt_disable(void)
 {
     btc_msg_t msg = {0};
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_PROV;

+ 1 - 1
components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h

@@ -259,7 +259,7 @@ esp_err_t esp_ble_mesh_provisioner_delete_dev(esp_ble_mesh_device_delete_t *del_
  * @param[in]    bearer: Adv packet received from PB-GATT or PB-ADV bearer.
  *
  */
-typedef void (*esp_ble_mesh_prov_adv_cb_t)(const esp_bd_addr_t addr, const esp_ble_addr_type_t addr_type,
+typedef void (*esp_ble_mesh_prov_adv_cb_t)(const esp_ble_mesh_bd_addr_t addr, const esp_ble_mesh_addr_type_t addr_type,
         const uint8_t adv_type, const uint8_t *dev_uuid,
         uint16_t oob_info, esp_ble_mesh_prov_bearer_t bearer);
 

+ 39 - 7
components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h

@@ -17,8 +17,6 @@
 
 #include <stdint.h>
 
-#include "esp_bt_defs.h"
-
 #include "mesh_proxy.h"
 #include "mesh_access.h"
 #include "mesh_main.h"
@@ -31,6 +29,16 @@
 #include "model_opcode.h"
 #include "mesh_common.h"
 
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
+#include "esp_bt_defs.h"
+#include "esp_bt_main.h"
+#define ESP_BLE_HOST_STATUS_ENABLED ESP_BLUEDROID_STATUS_ENABLED
+#define ESP_BLE_HOST_STATUS_CHECK(status) ESP_BLUEDROID_STATUS_CHECK(status)
+#else
+#define ESP_BLE_HOST_STATUS_ENABLED 0
+#define ESP_BLE_HOST_STATUS_CHECK(status)  do {} while (0)
+#endif
+
 /*!< The maximum length of a BLE Mesh message, including Opcode, Payload and TransMIC */
 #define ESP_BLE_MESH_SDU_MAX_LEN            384
 
@@ -790,6 +798,30 @@ 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 */
+
+
+#ifndef BD_ADDR_LEN
+#define BD_ADDR_LEN     6
+typedef uint8_t BD_ADDR[BD_ADDR_LEN];
+#endif
+
+typedef uint8_t esp_ble_mesh_bd_addr_t[BD_ADDR_LEN];
+
+/// BLE device address type
+typedef enum {
+    ESP_BLE_MESH_ADDR_TYPE_PUBLIC        = 0x00,
+    ESP_BLE_MESH_ADDR_TYPE_RANDOM        = 0x01,
+    ESP_BLE_MESH_ADDR_TYPE_RPA_PUBLIC    = 0x02,
+    ESP_BLE_MESH_ADDR_TYPE_RPA_RANDOM    = 0x03,
+} esp_ble_mesh_addr_type_t;
+
 typedef struct esp_ble_mesh_model esp_ble_mesh_model_t;
 
 /** Abstraction that describes a BLE Mesh Element.
@@ -1110,8 +1142,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_bd_addr_t addr;                 /*!< Device address */
-    esp_ble_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 */
@@ -1124,8 +1156,8 @@ typedef struct {
 typedef struct {
     union {
         struct {
-            esp_bd_addr_t addr;             /*!< Device address */
-            esp_ble_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 */
     };
@@ -1340,7 +1372,7 @@ typedef union {
     struct ble_mesh_provisioner_recv_unprov_adv_pkt_param {
         uint8_t  dev_uuid[16];                  /*!< Device UUID of the unprovisoned device */
         uint8_t  addr[6];                       /*!< Device address of the unprovisoned device */
-        esp_ble_addr_type_t addr_type;          /*!< Device address type */
+        esp_ble_mesh_addr_type_t addr_type;          /*!< Device address type */
         uint16_t oob_info;                      /*!< OOB Info of the unprovisoned device */
         uint8_t  adv_type;                      /*!< Avertising type of the unprovisoned device */
         esp_ble_mesh_prov_bearer_t bearer;      /*!< Bearer of the unprovisoned device */

+ 4 - 7
components/bt/esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c

@@ -17,22 +17,19 @@
 #include "btc/btc_task.h"
 #include "btc/btc_manage.h"
 
-#include "esp_bt_defs.h"
-#include "esp_bt_main.h"
-
 #include "btc_ble_mesh_config_model.h"
 #include "esp_ble_mesh_config_model_api.h"
 
 esp_err_t esp_ble_mesh_register_config_client_callback(esp_ble_mesh_cfg_client_cb_t callback)
 {
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     return (btc_profile_cb_set(BTC_PID_CONFIG_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL);
 }
 
 esp_err_t esp_ble_mesh_register_config_server_callback(esp_ble_mesh_cfg_server_cb_t callback)
 {
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     return (btc_profile_cb_set(BTC_PID_CONFIG_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL);
 }
@@ -47,7 +44,7 @@ esp_err_t esp_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_CONFIG_CLIENT;
@@ -69,7 +66,7 @@ esp_err_t esp_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_CONFIG_CLIENT;

+ 3 - 6
components/bt/esp_ble_mesh/api/models/esp_ble_mesh_generic_model_api.c

@@ -17,15 +17,12 @@
 #include "btc/btc_task.h"
 #include "btc/btc_manage.h"
 
-#include "esp_bt_defs.h"
-#include "esp_bt_main.h"
-
 #include "btc_ble_mesh_generic_model.h"
 #include "esp_ble_mesh_generic_model_api.h"
 
 esp_err_t esp_ble_mesh_register_generic_client_callback(esp_ble_mesh_generic_client_cb_t callback)
 {
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     return (btc_profile_cb_set(BTC_PID_GENERIC_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL);
 }
@@ -40,7 +37,7 @@ esp_err_t esp_ble_mesh_generic_client_get_state(esp_ble_mesh_client_common_param
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GENERIC_CLIENT;
@@ -62,7 +59,7 @@ esp_err_t esp_ble_mesh_generic_client_set_state(esp_ble_mesh_client_common_param
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_GENERIC_CLIENT;

+ 5 - 8
components/bt/esp_ble_mesh/api/models/esp_ble_mesh_health_model_api.c

@@ -17,22 +17,19 @@
 #include "btc/btc_task.h"
 #include "btc/btc_manage.h"
 
-#include "esp_bt_defs.h"
-#include "esp_bt_main.h"
-
 #include "btc_ble_mesh_health_model.h"
 #include "esp_ble_mesh_health_model_api.h"
 
 esp_err_t esp_ble_mesh_register_health_client_callback(esp_ble_mesh_health_client_cb_t callback)
 {
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     return (btc_profile_cb_set(BTC_PID_HEALTH_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL);
 }
 
 esp_err_t esp_ble_mesh_register_health_server_callback(esp_ble_mesh_health_server_cb_t callback)
 {
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     return (btc_profile_cb_set(BTC_PID_HEALTH_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL);
 }
@@ -47,7 +44,7 @@ esp_err_t esp_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param_
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_HEALTH_CLIENT;
@@ -69,7 +66,7 @@ esp_err_t esp_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param_
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_HEALTH_CLIENT;
@@ -86,7 +83,7 @@ esp_err_t esp_ble_mesh_health_server_fault_update(esp_ble_mesh_elem_t *element)
     btc_ble_mesh_health_server_args_t arg = {0};
     btc_msg_t msg = {0};
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_HEALTH_SERVER;

+ 3 - 6
components/bt/esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c

@@ -17,15 +17,12 @@
 #include "btc/btc_task.h"
 #include "btc/btc_manage.h"
 
-#include "esp_bt_defs.h"
-#include "esp_bt_main.h"
-
 #include "btc_ble_mesh_lighting_model.h"
 #include "esp_ble_mesh_lighting_model_api.h"
 
 esp_err_t esp_ble_mesh_register_light_client_callback(esp_ble_mesh_light_client_cb_t callback)
 {
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     return (btc_profile_cb_set(BTC_PID_LIGHTING_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL);
 }
@@ -40,7 +37,7 @@ esp_err_t esp_ble_mesh_light_client_get_state(esp_ble_mesh_client_common_param_t
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_LIGHTING_CLIENT;
@@ -62,7 +59,7 @@ esp_err_t esp_ble_mesh_light_client_set_state(esp_ble_mesh_client_common_param_t
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_LIGHTING_CLIENT;

+ 3 - 6
components/bt/esp_ble_mesh/api/models/esp_ble_mesh_sensor_model_api.c

@@ -17,15 +17,12 @@
 #include "btc/btc_task.h"
 #include "btc/btc_manage.h"
 
-#include "esp_bt_defs.h"
-#include "esp_bt_main.h"
-
 #include "btc_ble_mesh_sensor_model.h"
 #include "esp_ble_mesh_sensor_model_api.h"
 
 esp_err_t esp_ble_mesh_register_sensor_client_callback(esp_ble_mesh_sensor_client_cb_t callback)
 {
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     return (btc_profile_cb_set(BTC_PID_SENSOR_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL);
 }
@@ -40,7 +37,7 @@ esp_err_t esp_ble_mesh_sensor_client_get_state(esp_ble_mesh_client_common_param_
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_SENSOR_CLIENT;
@@ -62,7 +59,7 @@ esp_err_t esp_ble_mesh_sensor_client_set_state(esp_ble_mesh_client_common_param_
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_SENSOR_CLIENT;

+ 3 - 6
components/bt/esp_ble_mesh/api/models/esp_ble_mesh_time_scene_model_api.c

@@ -17,15 +17,12 @@
 #include "btc/btc_task.h"
 #include "btc/btc_manage.h"
 
-#include "esp_bt_defs.h"
-#include "esp_bt_main.h"
-
 #include "btc_ble_mesh_time_scene_model.h"
 #include "esp_ble_mesh_time_scene_model_api.h"
 
 esp_err_t esp_ble_mesh_register_time_scene_client_callback(esp_ble_mesh_time_scene_client_cb_t callback)
 {
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     return (btc_profile_cb_set(BTC_PID_TIME_SCENE_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL);
 }
@@ -40,7 +37,7 @@ esp_err_t esp_ble_mesh_time_scene_client_get_state(esp_ble_mesh_client_common_pa
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_TIME_SCENE_CLIENT;
@@ -62,7 +59,7 @@ esp_err_t esp_ble_mesh_time_scene_client_set_state(esp_ble_mesh_client_common_pa
         return ESP_ERR_INVALID_ARG;
     }
 
-    ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
+    ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
 
     msg.sig = BTC_SIG_API_CALL;
     msg.pid = BTC_PID_TIME_SCENE_CLIENT;

+ 1 - 1
components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c

@@ -689,7 +689,7 @@ static void btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb(
     }
 
     memcpy(mesh_param.provisioner_recv_unprov_adv_pkt.dev_uuid, dev_uuid, 16);
-    memcpy(mesh_param.provisioner_recv_unprov_adv_pkt.addr, addr, ESP_BD_ADDR_LEN);
+    memcpy(mesh_param.provisioner_recv_unprov_adv_pkt.addr, addr, BLE_MESH_ADDR_LEN);
     mesh_param.provisioner_recv_unprov_adv_pkt.addr_type = addr_type;
     mesh_param.provisioner_recv_unprov_adv_pkt.oob_info = oob_info;
     mesh_param.provisioner_recv_unprov_adv_pkt.adv_type = adv_type;

+ 0 - 1
components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h

@@ -21,7 +21,6 @@
 #include "freertos/semphr.h"
 
 #include "btc/btc_task.h"
-#include "esp_bt_defs.h"
 
 #include "mesh_access.h"
 #include "mesh_buf.h"

+ 1 - 1
components/bt/esp_ble_mesh/mesh_core/adv.c

@@ -388,7 +388,7 @@ void bt_mesh_adv_init(void)
     xBleMeshQueue = xQueueCreate(150, sizeof(bt_mesh_msg_t));
     configASSERT(xBleMeshQueue);
     int ret = xTaskCreatePinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", 3072, NULL,
-                            configMAX_PRIORITIES - 7, NULL, TASK_PINNED_TO_CORE);
+                            configMAX_PRIORITIES - 7, NULL, ADV_TASK_CORE);
     configASSERT(ret == pdTRUE);
 }
 

+ 38 - 0
components/bt/esp_ble_mesh/mesh_core/mesh_bearer_adapt.c → components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c

@@ -19,6 +19,8 @@
 #include "stack/hcimsgs.h"
 #include "osi/future.h"
 #include "osi/allocator.h"
+#include "bt_common.h"
+#include "device/controller.h"
 
 #include "mbedtls/aes.h"
 
@@ -32,6 +34,8 @@
 #include "provisioner_prov.h"
 #include "mesh_common.h"
 
+struct bt_mesh_dev bt_mesh_dev;
+
 #define BLE_MESH_BTM_CHECK_STATUS(func) do {                                                     \
         tBTM_STATUS __status = (func);                                                           \
         if ((__status != BTM_SUCCESS) && (__status != BTM_CMD_STARTED)) {                        \
@@ -97,6 +101,40 @@ static struct bt_mesh_prov_conn_cb *bt_mesh_gattc_conn_cb;
 static tBTA_GATTC_IF bt_mesh_gattc_if;
 #endif /* defined(CONFIG_BLE_MESH_PROVISIONER) && CONFIG_BLE_MESH_PROVISIONER */
 
+esp_err_t bt_mesh_host_init(void)
+{
+    return ESP_OK;
+}
+
+void bt_mesh_hci_init(void)
+{
+    const uint8_t *features = controller_get_interface()->get_features_ble()->as_array;
+    if (features != NULL) {
+        memcpy(bt_mesh_dev.features[0], features, 8);
+        memcpy(bt_mesh_dev.le.features, features, 8);
+    }
+
+    /**
+     * Currently 20ms non-connectable adv interval is supported, and we need to add
+     * a flag to indicate this support.
+     */
+#ifdef CONFIG_BLE_MESH_HCI_5_0
+    bt_mesh_dev.hci_version = BLE_MESH_HCI_VERSION_5_0;
+#else
+    bt_mesh_dev.hci_version = controller_get_interface()->get_bt_version()->hci_version;
+#endif
+    bt_mesh_dev.lmp_version = controller_get_interface()->get_bt_version()->lmp_version;
+    bt_mesh_dev.hci_revision = controller_get_interface()->get_bt_version()->hci_revision;
+    bt_mesh_dev.lmp_subversion = controller_get_interface()->get_bt_version()->lmp_subversion;
+    bt_mesh_dev.manufacturer = controller_get_interface()->get_bt_version()->manufacturer;
+
+    const uint8_t *p = controller_get_interface()->get_ble_supported_states();
+    uint64_t states_fh = 0, states_sh = 0;
+    STREAM_TO_UINT32(states_fh, p);
+    STREAM_TO_UINT32(states_sh, p);
+    bt_mesh_dev.le.states = (states_sh << 32) | states_fh;
+}
+
 static void bt_mesh_scan_results_change_2_bta(tBTM_INQ_RESULTS *p_inq, u8_t *p_eir,
                 tBTA_DM_SEARCH_CBACK *p_scan_cback)
 {

+ 17 - 0
components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h

@@ -16,8 +16,23 @@
 #include "mesh_uuid.h"
 
 /* BLE Mesh Max Connection Count */
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
 #define BLE_MESH_MAX_CONN   CONFIG_BT_ACL_CONNECTIONS
 
+#define ADV_TASK_CORE TASK_PINNED_TO_CORE
+#endif
+
+#ifdef CONFIG_BT_NIMBLE_ENABLED
+#define BLE_MESH_MAX_CONN   CONFIG_BT_NIMBLE_MAX_CONNECTIONS
+
+#ifdef CONFIG_BT_NIMBLE_PINNED_TO_CORE
+#define ADV_TASK_CORE              (CONFIG_BT_NIMBLE_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BT_NIMBLE_PINNED_TO_CORE : tskNO_AFFINITY)
+#else
+#define ADV_TASK_CORE              (0)
+#endif
+
+#endif
+
 /* BD ADDR types */
 #define BLE_MESH_ADDR_PUBLIC         0x00
 #define BLE_MESH_ADDR_RANDOM         0x01
@@ -620,6 +635,8 @@ struct bt_mesh_gatt_attr {
     .attr_count = ARRAY_SIZE(_attrs),   \
 }
 
+esp_err_t bt_mesh_host_init(void);
+
 int bt_le_adv_start(const struct bt_mesh_adv_param *param,
                     const struct bt_mesh_adv_data *ad, size_t ad_len,
                     const struct bt_mesh_adv_data *sd, size_t sd_len);

+ 6 - 0
components/bt/esp_ble_mesh/mesh_core/include/mesh_trace.h

@@ -47,6 +47,12 @@
 
 #define MESH_TRACE_TAG      "BLE_MESH"
 
+#define LOG_ERROR(format, ... )             {if (LOG_LOCAL_LEVEL >= ESP_LOG_ERROR)    esp_log_write(ESP_LOG_ERROR,   "BT_LOG", LOG_FORMAT(E, format), esp_log_timestamp(), "BT_LOG", ##__VA_ARGS__); }
+#define LOG_WARN(format, ... )              {if (LOG_LOCAL_LEVEL >= ESP_LOG_WARN)     esp_log_write(ESP_LOG_WARN,    "BT_LOG", LOG_FORMAT(W, format), esp_log_timestamp(), "BT_LOG", ##__VA_ARGS__); }
+#define LOG_INFO(format, ... )              {if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO)     esp_log_write(ESP_LOG_INFO,    "BT_LOG", LOG_FORMAT(I, format), esp_log_timestamp(), "BT_LOG", ##__VA_ARGS__); }
+#define LOG_DEBUG(format, ... )             {if (LOG_LOCAL_LEVEL >= ESP_LOG_DEBUG)    esp_log_write(ESP_LOG_DEBUG,   "BT_LOG", LOG_FORMAT(D, format), esp_log_timestamp(), "BT_LOG", ##__VA_ARGS__); }
+#define LOG_VERBOSE(format, ... )           {if (LOG_LOCAL_LEVEL >= ESP_LOG_VERBOSE)  esp_log_write(ESP_LOG_VERBOSE, "BT_LOG", LOG_FORMAT(V, format), esp_log_timestamp(), "BT_LOG", ##__VA_ARGS__); }
+
 #if (LOG_LOCAL_LEVEL >= 4)
 #define BLE_MESH_LOG_LOCAL_LEVEL_MAPPING    (LOG_LOCAL_LEVEL + 1)
 #else

+ 0 - 45
components/bt/esp_ble_mesh/mesh_core/mesh_hci.c

@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2017 Nordic Semiconductor ASA
- * Copyright (c) 2015-2016 Intel Corporation
- * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD
- *
- * SPDX-License-Identifier: Apache-2.0
- */
-
-#include "sdkconfig.h"
-
-#include "stack/bt_types.h"
-#include "device/controller.h"
-
-#include "mesh_hci.h"
-
-struct bt_mesh_dev bt_mesh_dev;
-
-void bt_mesh_hci_init(void)
-{
-    const uint8_t *features = controller_get_interface()->get_features_ble()->as_array;
-    if (features != NULL) {
-        memcpy(bt_mesh_dev.features[0], features, 8);
-        memcpy(bt_mesh_dev.le.features, features, 8);
-    }
-
-    /**
-     * Currently 20ms non-connectable adv interval is supported, and we need to add
-     * a flag to indicate this support.
-     */
-#ifdef CONFIG_BLE_MESH_HCI_5_0
-    bt_mesh_dev.hci_version = BLE_MESH_HCI_VERSION_5_0;
-#else
-    bt_mesh_dev.hci_version = controller_get_interface()->get_bt_version()->hci_version;
-#endif
-    bt_mesh_dev.lmp_version = controller_get_interface()->get_bt_version()->lmp_version;
-    bt_mesh_dev.hci_revision = controller_get_interface()->get_bt_version()->hci_revision;
-    bt_mesh_dev.lmp_subversion = controller_get_interface()->get_bt_version()->lmp_subversion;
-    bt_mesh_dev.manufacturer = controller_get_interface()->get_bt_version()->manufacturer;
-
-    const uint8_t *p = controller_get_interface()->get_ble_supported_states();
-    uint64_t states_fh = 0, states_sh = 0;
-    STREAM_TO_UINT32(states_fh, p);
-    STREAM_TO_UINT32(states_sh, p);
-    bt_mesh_dev.le.states = (states_sh << 32) | states_fh;
-}

+ 1 - 2
components/bt/esp_ble_mesh/mesh_core/mesh_kernel.c

@@ -14,8 +14,7 @@
 #include "osi/alarm.h"
 #include "osi/hash_functions.h"
 
-#include "common/bt_trace.h"
-#include "common/bt_defs.h"
+#include "bt_common.h"
 
 #include "esp_timer.h"
 

+ 1796 - 0
components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c

@@ -0,0 +1,1796 @@
+/*
+ * Copyright (c) 2017 Nordic Semiconductor ASA
+ * Copyright (c) 2015-2016 Intel Corporation
+ * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <errno.h>
+
+#include "sdkconfig.h"
+
+#include "btc/btc_task.h"
+#include "osi/alarm.h"
+
+#include "mbedtls/aes.h"
+#include "mbedtls/ecp.h"
+
+#include "host/ble_hs.h"
+#include "host/ble_uuid.h"
+#include "host/ble_att.h"
+#include "host/ble_gatt.h"
+#include "services/gap/ble_svc_gap.h"
+#include "services/gatt/ble_svc_gatt.h"
+
+#include "mesh_hci.h"
+#include "mesh_aes_encrypt.h"
+#include "mesh_bearer_adapt.h"
+#include "mesh_trace.h"
+#include "mesh_buf.h"
+#include "mesh_atomic.h"
+
+#include "esp_ble_mesh_defs.h"
+
+#include "provisioner_prov.h"
+#include "mesh_common.h"
+
+/** @def BT_UUID_MESH_PROV
+ *  @brief Mesh Provisioning Service
+ */ 
+#define BT_UUID_MESH_PROV_VAL             0x1827
+/** @def BT_UUID_MESH_PROXY
+ *  @brief Mesh Proxy Service
+ */ 
+#define BT_UUID_MESH_PROXY_VAL            0x1828
+/** @def BT_UUID_GATT_CCC
+ *  @brief GATT Client Characteristic Configuration
+ */
+#define BT_UUID_GATT_CCC_VAL              0x2902
+/** @def BT_UUID_MESH_PROV_DATA_IN
+ *  @brief Mesh Provisioning Data In
+ */
+#define BT_UUID_MESH_PROV_DATA_IN_VAL     0x2adb
+/** @def BT_UUID_MESH_PROV_DATA_OUT
+ *  @brief Mesh Provisioning Data Out
+ */
+#define BT_UUID_MESH_PROV_DATA_OUT_VAL    0x2adc
+/** @def BT_UUID_MESH_PROXY_DATA_IN
+ *  @brief Mesh Proxy Data In
+ */
+#define BT_UUID_MESH_PROXY_DATA_IN_VAL    0x2add
+/** @def BT_UUID_MESH_PROXY_DATA_OUT
+ *  @brief Mesh Proxy Data Out
+ */
+#define BT_UUID_MESH_PROXY_DATA_OUT_VAL   0x2ade
+
+#define BLE_MESH_GATT_GET_CONN_ID(conn_id)     ((u16_t)(conn_id))
+#define BLE_MESH_GATT_CREATE_CONN_ID(conn_id)  ((u16_t)(conn_id))
+
+static uint16_t proxy_svc_start_handle, prov_svc_start_handle;
+struct bt_mesh_dev bt_mesh_dev;
+
+/* P-256 Variables */
+static u8_t bt_mesh_public_key[64];
+static BT_OCTET32 bt_mesh_private_key = {
+    0x3f, 0x49, 0xf6, 0xd4, 0xa3, 0xc5, 0x5f, 0x38,
+    0x74, 0xc9, 0xb3, 0xe3, 0xd2, 0x10, 0x3f, 0x50,
+    0x4a, 0xff, 0x60, 0x7b, 0xeb, 0x40, 0xb7, 0x99,
+    0x58, 0x99, 0xb8, 0xa6, 0xcd, 0x3c, 0x1a, 0xbd
+};
+
+/* 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
+/* the gatt database list to save the attribute table */
+static sys_slist_t bt_mesh_gatts_db;
+
+/* Static Variables */
+static struct bt_mesh_conn bt_mesh_gatts_conn[BLE_MESH_MAX_CONN];
+static struct bt_mesh_conn_cb *bt_mesh_gatts_conn_cb;
+
+static BD_ADDR bt_mesh_gatts_addr;
+
+#endif /* defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE */
+
+esp_err_t bt_mesh_host_init(void)
+{
+    int rc;
+    rc = btc_init();
+    if (rc != 0) {
+        return ESP_FAIL;
+    }
+
+    rc = osi_alarm_create_mux();
+    if (rc != 0) {
+        return ESP_FAIL;
+    }
+
+    osi_alarm_init();
+    return ESP_OK;
+}
+
+uint8_t ble_hs_hci_get_hci_version(void);
+
+void bt_mesh_hci_init(void)
+{
+    /**
+     * Currently 20ms non-connectable adv interval is supported, and we need to add
+     * a flag to indicate this support.
+     */
+#ifdef CONFIG_BLE_MESH_HCI_5_0
+    bt_mesh_dev.hci_version = BLE_MESH_HCI_VERSION_5_0;
+#else
+    bt_mesh_dev.hci_version = ble_hs_hci_get_hci_version();
+#endif
+    return;
+}
+
+static struct ble_gap_disc_params scan_param;
+#if defined(CONFIG_BLE_MESH_PROVISIONER) && CONFIG_BLE_MESH_PROVISIONER
+static struct gattc_prov_info {
+    /* Service to be found depends on the type of adv pkt received */
+    struct bt_mesh_conn conn;
+    BD_ADDR addr;
+    u8_t  addr_type;
+    u16_t service_uuid;
+    u16_t mtu;
+    bool  wr_desc_done;    /* Indicate if write char descriptor event is received */
+    u16_t start_handle;    /* Service attribute start handle */
+    u16_t end_handle;      /* Service attribute end handle */
+    u16_t data_in_handle;  /* Data In Characteristic attribute handle */
+    u16_t data_out_handle; /* Data Out Characteristic attribute handle */
+    u16_t ccc_handle;      /* Data Out Characteristic CCC attribute handle */
+} bt_mesh_gattc_info[BLE_MESH_MAX_CONN];
+
+static struct bt_mesh_prov_conn_cb *bt_mesh_gattc_conn_cb;
+
+static int ble_on_subscribe(uint16_t conn_handle,
+                     const struct ble_gatt_error *error,
+                     struct ble_gatt_attr *attr,
+                     void *arg)
+{
+    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);
+
+    for (j = i+1; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) {
+        if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && bt_mesh_gattc_info[j].ccc_handle) {
+            break;
+        }
+    }
+    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) {
+            BT_WARN("%s, gattc ccc_handle is not matched", __func__);
+            bt_mesh_gattc_disconnect(conn);
+            return 0;
+        }
+
+        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_write_descr != NULL) {
+                len = bt_mesh_gattc_conn_cb->prov_write_descr(&bt_mesh_gattc_info[i].conn, bt_mesh_gattc_info[i].addr);
+                if (len < 0) {
+                    BT_ERR("%s, prov_write_descr failed", __func__);
+                    bt_mesh_gattc_disconnect(conn);
+                    return 0;
+                }
+                bt_mesh_gattc_info[i].wr_desc_done = true;
+            }
+        } 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_write_descr != NULL) {
+                len = bt_mesh_gattc_conn_cb->proxy_write_descr(&bt_mesh_gattc_info[i].conn);
+                if (len < 0) {
+                    BT_ERR("%s, proxy_write_descr failed", __func__);
+                    bt_mesh_gattc_disconnect(conn);
+                    return 0;
+                }
+            }
+        }
+
+
+        return 0;
+    }
+
+    ble_gattc_write_flat(conn_handle, bt_mesh_gattc_info[i].ccc_handle, value, sizeof(value), ble_on_subscribe, (void *)j);
+    return 0;
+}
+
+static int dsc_disced(uint16_t conn_handle, const struct ble_gatt_error *error,
+                uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc,
+                void *arg)
+{
+    int rc = 0, j, i = (int)arg; /* char index */
+    uint8_t value[2] = {0x01, 0x00};
+
+    switch (error->status) {
+    case 0:
+        bt_mesh_gattc_info[i].ccc_handle = dsc->handle;
+        break;
+
+    case BLE_HS_EDONE:
+        /* All descriptors in this characteristic discovered; start discovering
+         * descriptors in the next characteristic.
+         */
+        for (j = i+1; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) {
+            if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && bt_mesh_gattc_info[j].data_out_handle) {
+                    break;
+            }
+        }
+        if (j == ARRAY_SIZE(bt_mesh_gattc_info)) {
+            /* Register Notification for Mesh Provisioning/Proxy Data Out Characteristic */
+            for (j = 0; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) {
+                if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && bt_mesh_gattc_info[j].ccc_handle) {
+                    break;
+                }
+            }
+            if (j == ARRAY_SIZE(bt_mesh_gattc_info)) {
+                return 0;
+            }
+            ble_gattc_write_flat(conn_handle, bt_mesh_gattc_info[i].ccc_handle, value, sizeof(value), ble_on_subscribe, (void *)j);
+        } else {
+            ble_gattc_disc_all_dscs(conn_handle, bt_mesh_gattc_info[j].data_out_handle, 0xffff, dsc_disced, (void *)j);
+        }
+        rc = 0;
+        break;
+
+    default:
+        /* Error; abort discovery. */
+        rc = error->status;
+        break;
+    }
+
+    return rc;
+}
+
+
+static int chr_disced(uint16_t conn_handle, const struct ble_gatt_error *error,
+                const struct ble_gatt_chr *chr, void *arg)
+{
+    int rc = 0, j;
+    uint16_t uuid16 = 0;
+    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 */
+        if ((uuid16 == BLE_MESH_UUID_MESH_PROV_DATA_IN_VAL) || (uuid16 == BLE_MESH_UUID_MESH_PROXY_DATA_IN_VAL)) {
+            if (!(chr->properties & BLE_MESH_GATT_CHRC_WRITE_WITHOUT_RESP)) {
+                bt_mesh_gattc_disconnect(conn);
+                BT_ERR("Write without response is not set for Data In characteristic");
+                return 0;
+            }
+            bt_mesh_gattc_info[i].data_in_handle = chr->val_handle;
+        } else if ((uuid16 == BLE_MESH_UUID_MESH_PROV_DATA_OUT_VAL) || (uuid16 == BLE_MESH_UUID_MESH_PROXY_DATA_OUT_VAL)) {
+            if (!(chr->properties & BLE_MESH_GATT_CHRC_NOTIFY)) {
+                bt_mesh_gattc_disconnect(conn);
+                BT_ERR("Notify is not set for Data Out characteristic");
+                return 0;
+            }
+            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.
+         */
+        for (j = i+1; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) {
+            if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && (bt_mesh_gattc_info[j].start_handle > bt_mesh_gattc_info[j].end_handle)) {
+                    break;
+            }
+        }
+        if (j == ARRAY_SIZE(bt_mesh_gattc_info)) {
+            for (j = 0; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) {
+                if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && bt_mesh_gattc_info[j].data_out_handle) {
+                    break;
+                }
+            }
+            ble_gattc_disc_all_dscs(conn_handle, bt_mesh_gattc_info[j].data_out_handle, 0xffff, dsc_disced, (void *)j);
+        } else {
+            ble_gattc_disc_all_chrs(conn_handle, bt_mesh_gattc_info[j].start_handle, bt_mesh_gattc_info[j].end_handle,
+                    chr_disced, (void *)j);
+        }
+        break;
+
+    default:
+        rc = error->status;
+        break;
+    }
+
+    return rc;
+}
+
+
+static int svc_disced(uint16_t conn_handle, const struct ble_gatt_error *error,
+                const struct ble_gatt_svc *service, void *arg)
+{
+    struct bt_mesh_conn *conn = NULL;
+    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;
+                bt_mesh_gattc_info[i].end_handle   = service->end_handle;
+                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) {
+                break;
+            }
+        }
+
+        if (i == ARRAY_SIZE(bt_mesh_gattc_info)) {
+            BT_ERR("%s, Conn handle is not found", __func__);
+            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_disconnect(conn);
+            return 0;
+        }
+
+        /* Get the characteristic num within Mesh Provisioning/Proxy Service */
+        ble_gattc_disc_all_chrs(conn_handle, bt_mesh_gattc_info[i].start_handle, bt_mesh_gattc_info[i].end_handle,
+            chr_disced, (void *)i);
+        break;
+
+    default: 
+        rc = error->status;
+        break;
+    }
+
+    return rc;
+}
+
+
+#endif /* defined(CONFIG_BLE_MESH_PROVISIONER) && CONFIG_BLE_MESH_PROVISIONER */
+
+static int disc_cb(struct ble_gap_event *event, void *arg)
+{
+    struct ble_gap_disc_desc *desc;
+
+#if defined(CONFIG_BLE_MESH_PROVISIONER) && CONFIG_BLE_MESH_PROVISIONER
+    int rc, i;
+    uint8_t notif_data[100];
+    uint16_t notif_len;
+    ssize_t len;
+    struct ble_gap_conn_desc conn_desc;
+    struct bt_mesh_conn *conn = NULL;
+#endif
+
+    switch (event->type) {
+    case BLE_GAP_EVENT_DISC:
+        desc = &event->disc;
+
+        struct net_buf_simple *buf = bt_mesh_alloc_buf(desc->length_data);
+        if (!buf) {
+            BT_ERR("%s, Failed to allocate memory", __func__);
+            return 0;
+        }
+        net_buf_simple_add_mem(buf, 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);
+        }
+        osi_free(buf);
+        break;
+#if defined(CONFIG_BLE_MESH_PROVISIONER) && CONFIG_BLE_MESH_PROVISIONER
+    case BLE_GAP_EVENT_CONNECT:
+        if (event->connect.status == 0) {
+            /* Connection successfully established. */
+            MODLOG_DFLT(INFO, "Connection established ");
+
+            rc = ble_gap_conn_find(event->connect.conn_handle, &conn_desc);
+            assert(rc == 0);
+
+            if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->connected != NULL) {
+                for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) {
+                    if (!memcmp(bt_mesh_gattc_info[i].addr, conn_desc.peer_id_addr.val, BLE_MESH_ADDR_LEN)) {
+                        bt_mesh_gattc_info[i].conn.handle = event->connect.conn_handle;
+                        (bt_mesh_gattc_conn_cb->connected)(bt_mesh_gattc_info[i].addr, &bt_mesh_gattc_info[i].conn, i);
+                        break;
+                    }
+                }
+            }
+        }
+#if BLE_MESH_DEV
+        if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
+            rc = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL);
+            if (rc != 0) {
+                BT_ERR("%s, Invalid status %d", __func__, rc);
+                break;
+            }
+            bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
+        }
+#else
+        rc = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL);
+        if (rc != 0) {
+            BT_ERR("%s, Invalid status %d", __func__, rc);
+            break;
+        }
+#endif /* BLE_MESH_DEV */
+        break;
+    case BLE_GAP_EVENT_DISCONNECT:
+        if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->disconnected != NULL) {
+            for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) {
+                memcpy(&conn_desc, &event->disconnect.conn, sizeof(conn_desc));
+                if (!memcmp(bt_mesh_gattc_info[i].addr, conn_desc.peer_ota_addr.val, BLE_MESH_ADDR_LEN)) {
+                    if (bt_mesh_gattc_info[i].conn.handle == event->disconnect.conn.conn_handle) {
+                        (bt_mesh_gattc_conn_cb->disconnected)(&bt_mesh_gattc_info[i].conn, event->disconnect.reason);
+                        if (!bt_mesh_gattc_info[i].wr_desc_done) {
+                            /* Add this in case connection is established, connected event comes, but
+                             * connection is terminated before server->filter_type is set to PROV.
+                             */
+                            provisioner_clear_link_conn_info(bt_mesh_gattc_info[i].addr);
+                        }
+                    } else {
+                        /* Add this in case connection is failed to be established, and here we
+                         * need to clear some provision link info, like connecting flag, device
+                         * uuid, address info, etc.
+                         */
+                        provisioner_clear_link_conn_info(bt_mesh_gattc_info[i].addr);
+                    }
+                    /* Decrease prov pbg_count */
+                    provisioner_pbg_count_dec();
+                    /* Reset corresponding gattc info */
+                    memset(&bt_mesh_gattc_info[i], 0, sizeof(bt_mesh_gattc_info[i]));
+                    bt_mesh_gattc_info[i].conn.handle = 0xFFFF;
+                    bt_mesh_gattc_info[i].mtu = BLE_ATT_MTU_DFLT;
+                    bt_mesh_gattc_info[i].wr_desc_done = false;
+                    break;
+                }
+            }
+        }
+        break;
+    case BLE_GAP_EVENT_MTU:
+        for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) {
+            if (bt_mesh_gattc_info[i].conn.handle == event->mtu.conn_handle) {
+                bt_mesh_gattc_info[i].mtu = event->mtu.value;
+                break;
+            }
+        }
+        /** Once mtu exchanged accomplished, start to find services, and here
+         *  need a flag to indicate which service to find(Mesh Prov Service or
+         *  Mesh Proxy Service)
+         */
+        ble_uuid_any_t bt_uuid;
+        if (i != ARRAY_SIZE(bt_mesh_gattc_info)) {
+            //service_uuid.len       = sizeof(bt_mesh_gattc_info[i].service_uuid);
+            if (sizeof(bt_mesh_gattc_info[i].service_uuid) == 0x02) {
+                bt_uuid.u16.u.type = BLE_UUID_TYPE_16;
+                bt_uuid.u16.value = bt_mesh_gattc_info[i].service_uuid;
+
+            } else if (sizeof(bt_mesh_gattc_info[i].service_uuid) == 0x10) {
+                bt_uuid.u128.u.type = BLE_UUID_TYPE_128;
+                memcpy(bt_uuid.u128.value, &bt_mesh_gattc_info[i].service_uuid, 16);
+            }
+            /* Search Mesh Provisioning Service or Mesh Proxy Service */
+            ble_gattc_disc_all_svcs(bt_mesh_gattc_info[i].conn.handle, svc_disced, NULL);
+        }
+        break;
+    case BLE_GAP_EVENT_NOTIFY_RX:
+        for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) {
+            if (bt_mesh_gattc_info[i].conn.handle == event->notify_rx.conn_handle) {
+                break;
+            }
+        }
+
+        if (i == ARRAY_SIZE(bt_mesh_gattc_info)) {
+            BT_ERR("%s, Conn handle is not found", __func__);
+            return 0;
+        }
+
+        conn = &bt_mesh_gattc_info[i].conn;
+        ble_gap_conn_find(event->notify_rx.conn_handle, &conn_desc);
+
+        if (bt_mesh_gattc_info[i].data_out_handle != event->notify_rx.attr_handle) {
+            /* Data isn't populated yet */
+            return 0;
+        }
+            
+        if (memcmp(bt_mesh_gattc_info[i].addr, conn_desc.peer_id_addr.val, BLE_MESH_ADDR_LEN) ||
+                (bt_mesh_gattc_info[i].data_out_handle != event->notify_rx.attr_handle) ||
+                (event->notify_rx.indication != 0)) {
+            BT_ERR("%s, Notification error", __func__);
+            bt_mesh_gattc_disconnect(conn);
+            return 0;
+        }
+
+        notif_len = OS_MBUF_PKTLEN(event->notify_rx.om);
+        rc = os_mbuf_copydata(event->notify_rx.om, 0, notif_len, notif_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,
+                        notif_data, notif_len);
+                if (len < 0) {
+                    BT_ERR("%s, prov_notify failed", __func__);
+                    bt_mesh_gattc_disconnect(conn);
+                    return 0;
+                }
+            }
+        } 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,
+                        notif_data, notif_len);
+                if (len < 0) {
+                    BT_ERR("%s, proxy_notify failed", __func__);
+                    bt_mesh_gattc_disconnect(conn);
+                    return 0;
+                }
+            }
+        }
+        break;
+#endif
+    default:
+        break;
+    }
+
+    return 0;
+}
+
+static int start_le_scan(u8_t scan_type, u16_t interval, u16_t window, u8_t filter_dup)
+{
+
+    scan_param.filter_duplicates = filter_dup;
+    scan_param.itvl = interval;
+    scan_param.window = window;
+
+    if (scan_type == BLE_MESH_SCAN_PASSIVE) {
+        scan_param.passive = 1;
+    } else {
+        scan_param.passive = 0;
+    }
+    ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL);
+
+#if BLE_MESH_DEV
+    if (scan_type == BLE_MESH_SCAN_ACTIVE) {
+        bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ACTIVE_SCAN);
+    } else {
+        bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ACTIVE_SCAN);
+    }
+#endif
+
+    return 0;
+}
+
+static int set_ad(const struct bt_mesh_adv_data *ad, size_t ad_len, u8_t *buf, u8_t *buf_len)
+{
+    int i;
+
+    for (i = 0; i < ad_len; i++) {
+        buf[(*buf_len)++] = ad[i].data_len + 1;
+        buf[(*buf_len)++] = ad[i].type;
+
+        memcpy(&buf[*buf_len], ad[i].data,
+               ad[i].data_len);
+        *buf_len += ad[i].data_len;
+    }
+
+    return 0;
+}
+
+#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE
+static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(u16_t handle);
+
+static int gap_event_cb(struct ble_gap_event *event, void *arg)
+{
+    struct ble_gap_conn_desc desc;
+    int rc;
+
+    switch (event->type) {
+    case BLE_GAP_EVENT_CONNECT:
+        /* A new connection was established or a connection attempt failed. */
+        MODLOG_DFLT(INFO, "connection %s; status=%d ",
+                    event->connect.status == 0 ? "established" : "failed",
+                    event->connect.status);
+        if (event->connect.status == 0) {
+            rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
+            assert(rc == 0);
+        }
+        MODLOG_DFLT(INFO, "\n");
+#if BLE_MESH_DEV
+        /* When connection is created, advertising will be stopped automatically. */
+        bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING);
+#endif
+        if (bt_mesh_gatts_conn_cb != NULL && bt_mesh_gatts_conn_cb->connected != NULL) {
+            u8_t index = BLE_MESH_GATT_GET_CONN_ID(event->connect.conn_handle);
+            if (index < BLE_MESH_MAX_CONN) {
+                bt_mesh_gatts_conn[index].handle = BLE_MESH_GATT_GET_CONN_ID(event->connect.conn_handle);
+                (bt_mesh_gatts_conn_cb->connected)(&bt_mesh_gatts_conn[index], 0);
+            }
+            memcpy(bt_mesh_gatts_addr, desc.peer_id_addr.val, BLE_MESH_ADDR_LEN);
+            /* This is for EspBleMesh Android app. When it tries to connect with the
+             * device at the first time and it fails due to some reason. And after
+             * the second connection, the device needs to send GATT service change
+             * indication to the phone manually to notify it dicovering service again.
+             */
+            ble_svc_gatt_changed(prov_svc_start_handle, 0xffff);
+
+        }
+
+        return 0;
+
+    case BLE_GAP_EVENT_DISCONNECT:
+        MODLOG_DFLT(INFO, "disconnect; reason=%d ", event->disconnect.reason);
+        MODLOG_DFLT(INFO, "\n");
+#if BLE_MESH_DEV
+        bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING);
+#endif
+        if (bt_mesh_gatts_conn_cb != NULL && bt_mesh_gatts_conn_cb->disconnected != NULL) {
+            u8_t index = BLE_MESH_GATT_GET_CONN_ID(event->disconnect.conn.conn_handle);
+            if (index < BLE_MESH_MAX_CONN) {
+                bt_mesh_gatts_conn[index].handle = BLE_MESH_GATT_GET_CONN_ID(event->disconnect.conn.conn_handle);
+                (bt_mesh_gatts_conn_cb->disconnected)(&bt_mesh_gatts_conn[index], event->disconnect.reason);
+            }
+            memset(bt_mesh_gatts_addr, 0x0, BLE_MESH_ADDR_LEN);
+        }
+
+        return 0;
+
+    case BLE_GAP_EVENT_CONN_UPDATE:
+        /* The central has updated the connection parameters. */
+        MODLOG_DFLT(INFO, "connection updated; status=%d ",
+                    event->conn_update.status);
+        rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
+        assert(rc == 0);
+        MODLOG_DFLT(INFO, "\n");
+        return 0;
+
+    case BLE_GAP_EVENT_ADV_COMPLETE:
+        MODLOG_DFLT(INFO, "advertise complete; reason=%d",
+                    event->adv_complete.reason);
+        return 0;
+
+    case BLE_GAP_EVENT_ENC_CHANGE:
+        /* Encryption has been enabled or disabled for this connection. */
+        MODLOG_DFLT(INFO, "encryption change event; status=%d ",
+                    event->enc_change.status);
+        rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
+        assert(rc == 0);
+        MODLOG_DFLT(INFO, "\n");
+        return 0;
+
+    case BLE_GAP_EVENT_SUBSCRIBE:
+        MODLOG_DFLT(INFO, "subscribe event; conn_handle=%d attr_handle=%d "
+                    "reason=%d prevn=%d curn=%d previ=%d curi=%d\n",
+                    event->subscribe.conn_handle,
+                    event->subscribe.attr_handle,
+                    event->subscribe.reason,
+                    event->subscribe.prev_notify,
+                    event->subscribe.cur_notify,
+                    event->subscribe.prev_indicate,
+                    event->subscribe.cur_indicate);
+        struct bt_mesh_gatt_attr *attr = bt_mesh_gatts_find_attr_by_handle(event->subscribe.attr_handle + 1);
+        u8_t index = BLE_MESH_GATT_GET_CONN_ID(event->subscribe.conn_handle);
+        u16_t len = 0;
+        uint16_t ccc_val = 0;
+
+        if (event->subscribe.prev_notify != event->subscribe.cur_notify) {   
+            ccc_val = event->subscribe.cur_notify;
+        } else if (event->subscribe.prev_indicate != event->subscribe.cur_indicate) {
+            if (event->subscribe.cur_indicate) {
+                ccc_val = 2;
+            } else {
+                ccc_val = 0;
+            }
+        }
+
+        if (attr != NULL && attr->write != NULL) {
+            if ((len = attr->write(&bt_mesh_gatts_conn[index], attr,
+                            &ccc_val,
+                            sizeof(ccc_val),
+                            0 /* offset */, 0)) > 0) {
+            }
+        }
+        
+        return 0;
+
+    case BLE_GAP_EVENT_MTU:
+        MODLOG_DFLT(INFO, "mtu update event; conn_handle=%d cid=%d mtu=%d\n",
+                    event->mtu.conn_handle,
+                    event->mtu.channel_id,
+                    event->mtu.value);
+        return 0;
+
+    case BLE_GAP_EVENT_REPEAT_PAIRING:
+        /* We already have a bond with the peer, but it is attempting to
+         * establish a new secure link.  This app sacrifices security for
+         * convenience: just throw away the old bond and accept the new link.
+         */
+
+        /* Delete the old bond. */
+        rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc);
+        assert(rc == 0);
+        ble_store_util_delete_peer(&desc.peer_id_addr);
+
+        /* Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should
+         * continue with the pairing operation.
+         */
+        return BLE_GAP_REPEAT_PAIRING_RETRY;
+
+    case BLE_GAP_EVENT_PASSKEY_ACTION:
+        MODLOG_DFLT(INFO, "PASSKEY_ACTION_EVENT started \n");
+        return 0;
+    }
+
+    return 0;
+}
+#else
+
+static int gap_event_cb(struct ble_gap_event *event, void *arg)
+{
+    return 0;
+}
+#endif
+
+/* APIs functions */
+int bt_le_adv_start(const struct bt_mesh_adv_param *param,
+                    const struct bt_mesh_adv_data *ad, size_t ad_len,
+                    const struct bt_mesh_adv_data *sd, size_t sd_len)
+{
+#if BLE_MESH_DEV
+    if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING)) {
+        return -EALREADY;
+    }
+#endif
+    uint8_t buf[BLE_HS_ADV_MAX_SZ];
+    uint8_t buf_len = 0;
+    int err;
+    struct ble_gap_adv_params adv_params;
+
+    err = set_ad(ad, ad_len, buf, &buf_len);
+    if (err) {
+        BT_ERR("set_ad failed: err %d", err);
+        return err;
+    }
+
+    err = ble_gap_adv_set_data(buf, buf_len);
+    if (err != 0) {
+        BT_ERR("Advertising set failed: err %d", err);
+        return err;
+    }
+
+    if (sd && (param->options & BLE_MESH_ADV_OPT_CONNECTABLE)) {
+        buf_len = 0;
+
+        err = set_ad(sd, sd_len, buf, &buf_len);
+        if (err) {
+            BT_ERR("set_ad failed: err %d", err);
+            return err;
+        }
+
+        err = ble_gap_adv_rsp_set_data(buf, buf_len);
+        if (err != 0) {
+            BT_ERR("Scan rsp failed: err %d", err);
+            return err;
+        }
+    }
+
+    memset(&adv_params, 0, sizeof adv_params);
+    adv_params.itvl_min = param->interval_min;
+    adv_params.itvl_max = param->interval_max;
+
+    if (param->options & BLE_MESH_ADV_OPT_CONNECTABLE) {
+        adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
+        adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
+    } else if (sd != NULL) {
+        adv_params.conn_mode = BLE_GAP_CONN_MODE_NON;
+        adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
+    } else {
+        adv_params.conn_mode = BLE_GAP_CONN_MODE_NON;
+        adv_params.disc_mode = BLE_GAP_DISC_MODE_NON;
+        adv_params.itvl_min = 160;
+        adv_params.itvl_max = 160;
+    }
+
+    err = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params,
+                            gap_event_cb, NULL);
+    if (err) {
+        BT_ERR("Advertising start failed: err %d", err);
+        return err;
+    }
+
+#if BLE_MESH_DEV
+    bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING);
+
+    if (!(param->options & BLE_MESH_ADV_OPT_ONE_TIME)) {
+        bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_KEEP_ADVERTISING);
+    }
+#endif
+
+    return 0;
+}
+
+int bt_le_adv_stop(void)
+{
+#if BLE_MESH_DEV
+    bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_KEEP_ADVERTISING);
+    if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING)) {
+        return 0;
+    }
+#endif
+    ble_gap_adv_stop();
+
+#if BLE_MESH_DEV
+    bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING);
+#endif
+
+    return 0;
+}
+
+int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t cb)
+{
+    int err;
+
+#if BLE_MESH_DEV
+    if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
+        return -EALREADY;
+    }
+#endif
+
+#if BLE_MESH_DEV
+    if (param->filter_dup) {
+        bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCAN_FILTER_DUP);
+    } else {
+        bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCAN_FILTER_DUP);
+    }
+#endif
+
+    err = start_le_scan(param->type, param->interval, param->window, param->filter_dup);
+    if (err) {
+        return err;
+    }
+
+#if BLE_MESH_DEV
+    bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
+#endif
+
+    bt_mesh_scan_dev_found_cb = cb;
+    return err;
+}
+
+int bt_le_scan_stop(void)
+{
+#if BLE_MESH_DEV
+    if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
+        bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
+        ble_gap_disc_cancel();
+    }
+#else
+    ble_gap_disc_cancel();
+#endif
+
+    bt_mesh_scan_dev_found_cb = NULL;
+    return 0;
+}
+#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE
+
+void bt_mesh_gatts_conn_cb_register(struct bt_mesh_conn_cb *cb)
+{
+    bt_mesh_gatts_conn_cb = cb;
+}
+
+static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(u16_t handle)
+{
+    struct bt_mesh_gatt_service *svc = NULL;
+    struct bt_mesh_gatt_attr *attr = NULL;
+
+    SYS_SLIST_FOR_EACH_CONTAINER(&bt_mesh_gatts_db, svc, node) {
+        int i;
+
+        for (i = 0; i < svc->attr_count; i++) {
+            attr = &svc->attrs[i];
+            /* Check the attrs handle is equal to the handle or not */
+            if (attr->handle == handle) {
+                return attr;
+            }
+        }
+    }
+
+    return NULL;
+}
+
+static void bt_mesh_gatts_foreach_attr(u16_t start_handle, u16_t end_handle,
+                bt_mesh_gatt_attr_func_t func, void *user_data)
+{
+    struct bt_mesh_gatt_service *svc = NULL;
+
+    SYS_SLIST_FOR_EACH_CONTAINER(&bt_mesh_gatts_db, svc, node) {
+        int i;
+
+        for (i = 0; i < svc->attr_count; i++) {
+            struct bt_mesh_gatt_attr *attr = &svc->attrs[i];
+
+            /* Check if attribute handle is within range */
+            if (attr->handle < start_handle ||
+                    attr->handle > end_handle) {
+                continue;
+            }
+
+            if (func(attr, user_data) == BLE_MESH_GATT_ITER_STOP) {
+                return;
+            }
+        }
+    }
+}
+
+static u8_t find_next(const struct bt_mesh_gatt_attr *attr, void *user_data)
+{
+    struct bt_mesh_gatt_attr **next = user_data;
+
+    *next = (struct bt_mesh_gatt_attr *)attr;
+
+    return BLE_MESH_GATT_ITER_STOP;
+}
+
+static struct bt_mesh_gatt_attr *bt_mesh_gatts_attr_next(const struct bt_mesh_gatt_attr *attr)
+{
+    struct bt_mesh_gatt_attr *next = NULL;
+
+    bt_mesh_gatts_foreach_attr(attr->handle + 1, attr->handle + 1, find_next, &next);
+
+    return next;
+}
+
+ssize_t bt_mesh_gatts_attr_read(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr,
+                                void *buf, u16_t buf_len, u16_t offset,
+                                const void *value, u16_t value_len)
+{
+    u16_t len;
+
+    if (offset > value_len) {
+        return BLE_MESH_GATT_ERR(BLE_MESH_ATT_ERR_INVALID_OFFSET);
+    }
+
+    len = MIN(buf_len, value_len - offset);
+
+    BT_DBG("handle 0x%04x offset %u length %u", attr->handle, offset, len);
+
+    memcpy(buf, value + offset, len);
+
+    return len;
+}
+
+struct gatts_incl {
+    u16_t start_handle;
+    u16_t end_handle;
+    u16_t uuid16;
+} __packed;
+
+ssize_t bt_mesh_gatts_attr_read_included(struct bt_mesh_conn *conn,
+                                         const struct bt_mesh_gatt_attr *attr,
+                                         void *buf, u16_t len, u16_t offset)
+{
+    struct bt_mesh_gatt_attr *incl = attr->user_data;
+    struct bt_mesh_uuid *uuid = incl->user_data;
+    struct gatts_incl pdu = {0};
+    u8_t value_len;
+
+    /* First attr points to the start handle */
+    pdu.start_handle = sys_cpu_to_le16(incl->handle);
+    value_len = sizeof(pdu.start_handle) + sizeof(pdu.end_handle);
+
+    /*
+     * Core 4.2, Vol 3, Part G, 3.2,
+     * The Service UUID shall only be present when the UUID is a 16-bit Bluetooth UUID.
+     */
+    if (uuid->type == BLE_MESH_UUID_TYPE_16) {
+        pdu.uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(uuid)->val);
+        value_len += sizeof(pdu.uuid16);
+    }
+
+    return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &pdu, value_len);
+}
+
+ssize_t bt_mesh_gatts_attr_read_service(struct bt_mesh_conn *conn,
+                                        const struct bt_mesh_gatt_attr *attr,
+                                        void *buf, u16_t len, u16_t offset)
+{
+    struct bt_mesh_uuid *uuid = attr->user_data;
+
+    if (uuid->type == BLE_MESH_UUID_TYPE_16) {
+        u16_t uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(uuid)->val);
+
+        return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &uuid16, 2);
+    }
+
+    return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset,
+                             BLE_MESH_UUID_128(uuid)->val, 16);
+}
+
+struct gatts_chrc {
+    u8_t  properties;
+    u16_t value_handle;
+    union {
+        u16_t uuid16;
+        u8_t  uuid[16];
+    };
+} __packed;
+
+ssize_t bt_mesh_gatts_attr_read_chrc(struct bt_mesh_conn *conn,
+                                     const struct bt_mesh_gatt_attr *attr, void *buf,
+                                     u16_t len, u16_t offset)
+{
+    struct bt_mesh_gatt_char *chrc = attr->user_data;
+    const struct bt_mesh_gatt_attr *next = NULL;
+    struct gatts_chrc pdu = {0};
+    u8_t value_len;
+
+    pdu.properties = chrc->properties;
+    /* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] page 534:
+     * 3.3.2 Characteristic Value Declaration
+     * The Characteristic Value declaration contains the value of the
+     * characteristic. It is the first Attribute after the characteristic
+     * declaration. All characteristic definitions shall have a
+     * Characteristic Value declaration.
+     */
+    next = bt_mesh_gatts_attr_next(attr);
+    if (!next) {
+        BT_WARN("%s, No value for characteristic at 0x%04x", __func__, attr->handle);
+        pdu.value_handle = 0x0000;
+    } else {
+        pdu.value_handle = sys_cpu_to_le16(next->handle);
+    }
+    value_len = sizeof(pdu.properties) + sizeof(pdu.value_handle);
+
+    if (chrc->uuid->type == BLE_MESH_UUID_TYPE_16) {
+        pdu.uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(chrc->uuid)->val);
+        value_len += 2;
+    } else {
+        memcpy(pdu.uuid, BLE_MESH_UUID_128(chrc->uuid)->val, 16);
+        value_len += 16;
+    }
+
+    return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &pdu, value_len);
+}
+
+static int gatts_register(struct bt_mesh_gatt_service *svc)
+{
+    struct bt_mesh_gatt_service *last;
+    u16_t handle;
+
+    if (sys_slist_is_empty(&bt_mesh_gatts_db)) {
+        handle = 0;
+        goto populate;
+    }
+
+    last = SYS_SLIST_PEEK_TAIL_CONTAINER(&bt_mesh_gatts_db, last, node);
+    handle = last->attrs[last->attr_count - 1].handle;
+    BT_DBG("%s, handle =  %d", __func__, handle);
+
+populate:
+    sys_slist_append(&bt_mesh_gatts_db, &svc->node);
+    return 0;
+}
+
+int bt_mesh_gatts_service_register(struct bt_mesh_gatt_service *svc)
+{
+    uint16_t offset = 0;
+    int i;
+    if (BLE_MESH_UUID_16(svc->attrs[0].user_data)->val == BT_UUID_MESH_PROXY_VAL) {
+        offset = proxy_svc_start_handle;
+    } else if (BLE_MESH_UUID_16(svc->attrs[0].user_data)->val == BT_UUID_MESH_PROV_VAL) {
+        offset = prov_svc_start_handle;
+    }
+
+    for (i = 0; i < svc->attr_count; i++) {
+        svc->attrs[i].handle = offset + i;
+    }
+    gatts_register(svc);
+    return 0;
+}
+
+int bt_mesh_gatts_disconnect(struct bt_mesh_conn *conn, u8_t reason)
+{
+    u16_t conn_id = BLE_MESH_GATT_CREATE_CONN_ID(conn->handle);
+    ble_gap_terminate(conn_id, reason);
+    return 0;
+}
+
+int bt_mesh_gatts_service_unregister(struct bt_mesh_gatt_service *svc)
+{
+    assert(svc != NULL);
+    BT_ERR("%s, Unsupported for NimBLE host", __func__);
+    return 0;
+}
+
+int bt_mesh_gatts_notify(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr,
+                         const void *data, u16_t len)
+{
+    struct os_mbuf *om;
+    u16_t conn_id = BLE_MESH_GATT_CREATE_CONN_ID(conn->handle);
+
+    om = ble_hs_mbuf_from_flat(data, len);
+    assert(om);
+    ble_gattc_notify_custom(conn_id, attr->handle, om);
+
+    return 0;
+}
+
+u16_t bt_mesh_gatt_get_mtu(struct bt_mesh_conn *conn)
+{
+    return ble_att_preferred_mtu();
+}
+
+/* APIs added by Espressif */
+int bt_mesh_gatts_service_stop(struct bt_mesh_gatt_service *svc)
+{
+    int rc;
+    uint16_t handle;
+    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 {
+        uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL);
+    }
+
+    rc = ble_gatts_find_svc(uuid, &handle);
+    assert(rc == 0);
+    ble_gatts_svc_set_visibility(handle, 0);
+
+    /* FIXME: figure out end handle */
+    ble_svc_gatt_changed(handle, 0xffff);
+
+    return 0;
+}
+
+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 {
+        uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL);
+    }
+
+    rc = ble_gatts_find_svc(uuid, &handle);
+    assert(rc == 0);
+    ble_gatts_svc_set_visibility(handle, 1);
+
+    /* FIXME: figure out end handle */
+    ble_svc_gatt_changed(handle, 0xffff);
+
+    return 0;
+}
+#endif /* defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE */
+
+#if defined(CONFIG_BLE_MESH_PROVISIONER) && CONFIG_BLE_MESH_PROVISIONER
+void bt_mesh_gattc_conn_cb_register(struct bt_mesh_prov_conn_cb *cb)
+{
+    bt_mesh_gattc_conn_cb = cb;
+}
+
+u16_t bt_mesh_gattc_get_service_uuid(struct bt_mesh_conn *conn)
+{
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) {
+        if (conn == &bt_mesh_gattc_info[i].conn) {
+            break;
+        }
+    }
+
+    if (i == ARRAY_SIZE(bt_mesh_gattc_info)) {
+        return 0;
+    }
+
+    return bt_mesh_gattc_info[i].service_uuid;
+}
+
+/** For provisioner acting as a GATT client, it may follow the procedures
+ *  listed below.
+ *  1. Create connection with the unprovisioned device
+ *  2. Exchange MTU size
+ *  3. Find Mesh Prov Service in the device's service database
+ *  4. Find Mesh Prov Data In/Out characteristic within the service
+ *  5. Get CCC of Mesh Prov Data Out Characteristic
+ *  6. Set the Notification bit of CCC
+ */
+
+int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, u16_t service_uuid)
+{
+    u8_t zero[6] = {0};
+    int i, rc;
+
+    if (!addr || !memcmp(addr->val, zero, BLE_MESH_ADDR_LEN) ||
+        (addr->type > BLE_ADDR_RANDOM)) {
+        BT_ERR("%s, Invalid remote address", __func__);
+        return -EINVAL;
+    }
+
+    if (service_uuid != BLE_MESH_UUID_MESH_PROV_VAL &&
+        service_uuid != BLE_MESH_UUID_MESH_PROXY_VAL) {
+        BT_ERR("%s, Invalid service uuid 0x%04x", __func__, service_uuid);
+        return -EINVAL;
+    }
+
+    /* Check if already creating connection with the device */
+    for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) {
+        if (!memcmp(bt_mesh_gattc_info[i].addr, addr->val, BLE_MESH_ADDR_LEN)) {
+            BT_WARN("%s, Already create connection with %s",
+                __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN));
+            return -EALREADY;
+        }
+    }
+
+    /* 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)) {
+            memcpy(bt_mesh_gattc_info[i].addr, addr->val, BLE_MESH_ADDR_LEN);
+            bt_mesh_gattc_info[i].addr_type = addr->type;
+            /* Service to be found after exhanging mtu size */
+            bt_mesh_gattc_info[i].service_uuid = service_uuid;
+            break;
+        }
+    }
+
+    if (i == ARRAY_SIZE(bt_mesh_gattc_info)) {
+        BT_WARN("%s, gattc info is full", __func__);
+        return -ENOMEM;
+    }
+
+#if BLE_MESH_DEV
+    if (bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
+        rc = ble_gap_disc_cancel();
+        if (rc != 0) {
+            return -1;
+        }
+    }
+#else
+        rc = ble_gap_disc_cancel();
+        if (rc != 0) {
+            return -1;
+        }
+#endif /* BLE_MESH_DEV */
+
+    BT_DBG("%s, create conn with %s", __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN));
+
+    /* Min_interval: 250ms
+     * Max_interval: 250ms
+     * Slave_latency: 0x0
+     * Supervision_timeout: 32 sec
+     */
+    struct ble_gap_conn_params conn_params = {0};
+    conn_params.itvl_min = 0xC8; /* (250 * 1000) / 1250 = 200 = 0xC8 */
+    conn_params.itvl_max = 0xC8; /* (250 * 1000) / 1250 = 200 = 0xC8 */
+    conn_params.latency = 0;
+    conn_params.supervision_timeout = 0xC80;
+    conn_params.scan_itvl = 0x0020; //0x0010
+    conn_params.scan_window = 0x0020; //0x0010
+    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; 
+
+    rc = ble_gap_connect(BLE_OWN_ADDR_PUBLIC, &peer_addr, BLE_HS_FOREVER, &conn_params,
+                         disc_cb, NULL);
+    /* Increment pbg_count */
+    provisioner_pbg_count_inc();
+
+    return 0;
+}
+
+static int mtu_cb(uint16_t conn_handle,
+             const struct ble_gatt_error *error,
+             uint16_t mtu, void *arg)
+{
+    int i;
+    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;
+                break;
+            }
+        }
+    }
+    return 0;
+}
+
+
+
+void bt_mesh_gattc_exchange_mtu(u8_t index)
+{
+    /** Set local MTU and exchange with GATT server.
+     *  ATT_MTU >= 69 for Mesh GATT Prov Service
+     *  ATT_NTU >= 33 for Mesh GATT Proxy Service
+    */
+
+    ble_gattc_exchange_mtu(bt_mesh_gattc_info[index].conn.handle, mtu_cb, NULL);
+}
+
+u16_t bt_mesh_gattc_get_mtu_info(struct bt_mesh_conn *conn)
+{
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) {
+        if (conn == &bt_mesh_gattc_info[i].conn) {
+            return bt_mesh_gattc_info[i].mtu;
+        }
+    }
+
+    return 0;
+}
+
+int bt_mesh_gattc_write_no_rsp(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr,
+                               const void *data, u16_t len)
+{
+    u16_t conn_id;
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) {
+        if (conn == &bt_mesh_gattc_info[i].conn) {
+            break;
+        }
+    }
+
+    if (i == ARRAY_SIZE(bt_mesh_gattc_info)) {
+        BT_ERR("%s, Conn is not found", __func__);
+        /** Here we return 0 for prov_send() return value check in provisioner.c
+         */
+        return 0;
+    }
+
+    conn_id = BLE_MESH_GATT_CREATE_CONN_ID(bt_mesh_gattc_info[i].conn.handle);
+
+    struct os_mbuf *om;
+    int rc;
+
+    om = ble_hs_mbuf_from_flat(data, len);
+    if (om == NULL) {
+        return -1;
+    }
+
+    rc = ble_gattc_write_no_rsp(conn_id, bt_mesh_gattc_info[i].data_in_handle, om);
+    if (rc != 0) {
+        return -1;
+    }
+
+    return 0;
+}
+
+void bt_mesh_gattc_disconnect(struct bt_mesh_conn *conn)
+{
+    /** Disconnect
+     *  Clear proper proxy server information
+     *  Clear proper prov_link information
+     *  Clear proper bt_mesh_gattc_info information
+     *  Here in adapter, we just clear proper bt_mesh_gattc_info, and
+     *  when proxy_disconnected callback comes, the proxy server
+     *  information and prov_link information should be cleared.
+     */
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) {
+        if (conn == &bt_mesh_gattc_info[i].conn) {
+            break;
+        }
+    }
+
+    if (i == ARRAY_SIZE(bt_mesh_gattc_info)) {
+        BT_ERR("%s, Conn is not found", __func__);
+        return;
+    }
+    ble_gap_terminate(bt_mesh_gattc_info[i].conn.handle, BLE_ERR_REM_USER_CONN_TERM);
+}
+
+/** Mesh Provisioning Service:  0x1827
+ *  Mesh Provisioning Data In:  0x2ADB
+ *  Mesh Provisioning Data Out: 0x2ADC
+ *  Mesh Proxy Service:  0x1828
+ *  Mesh Proxy Data In:  0x2ADD
+ *  Mesh PROXY Data Out: 0x2ADE
+ */
+#endif /* defined(CONFIG_BLE_MESH_PROVISIONER) && CONFIG_BLE_MESH_PROVISIONER */
+
+struct bt_mesh_conn *bt_mesh_conn_ref(struct bt_mesh_conn *conn)
+{
+    bt_mesh_atomic_inc(&conn->ref);
+
+    BT_DBG("handle %u ref %u", conn->handle, bt_mesh_atomic_get(&conn->ref));
+
+    return conn;
+}
+
+void bt_mesh_conn_unref(struct bt_mesh_conn *conn)
+{
+    bt_mesh_atomic_dec(&conn->ref);
+
+    BT_DBG("handle %u ref %u", conn->handle, bt_mesh_atomic_get(&conn->ref));
+}
+
+#if defined(CONFIG_BLE_MESH_NODE) && 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)
+{
+    if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR || ctxt->op == BLE_GATT_ACCESS_OP_WRITE_DSC) {
+        struct bt_mesh_gatt_attr *attr = bt_mesh_gatts_find_attr_by_handle(attr_handle);
+        u8_t index = BLE_MESH_GATT_GET_CONN_ID(conn_handle);
+        u16_t len = 0;
+
+        BT_DBG("%s, write: handle = %d, len = %d, data = %s", __func__, attr_handle,
+                ctxt->om->om_len,
+                bt_hex(ctxt->om->om_data, ctxt->om->om_len));
+
+        if (attr != NULL && attr->write != NULL) {
+            if ((len = attr->write(&bt_mesh_gatts_conn[index], attr,
+                            ctxt->om->om_data,
+                            ctxt->om->om_len,
+                            0 /* offset */, 0)) > 0) {
+            }
+        }
+    } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR || ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC) {
+        BT_ERR("%s, Unhandled read request for chr and dsc: opcode - %d", __func__, ctxt->op);
+    }
+    return 0;
+}
+
+static int dummy_access_cb(uint16_t conn_handle, uint16_t attr_handle,
+                        struct ble_gatt_access_ctxt *ctxt, void *arg)
+{
+    /*
+     * We should never never enter this callback - it's attached to notify-only
+     * characteristic which are notified directly from mbuf. And we can't pass
+     * NULL as access_cb because gatts will assert on init...
+     */
+    assert(0);
+    return 0;
+}
+
+static const struct ble_gatt_svc_def svc_defs [] = {
+#ifdef CONFIG_BLE_MESH_GATT_PROXY
+	{
+		.type = BLE_GATT_SVC_TYPE_PRIMARY,
+		.uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL),
+        .includes = NULL,
+		.characteristics = (struct ble_gatt_chr_def[]) { {
+				.uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_DATA_IN_VAL),
+				.access_cb = proxy_char_access_cb,
+				.flags = BLE_GATT_CHR_F_WRITE_NO_RSP,
+			}, {
+				.uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_DATA_OUT_VAL),
+				.access_cb = dummy_access_cb,
+				.flags = BLE_GATT_CHR_F_NOTIFY,
+			}, {
+				0, /* No more characteristics in this service. */
+			} },
+	},
+#endif
+#ifdef CONFIG_BLE_MESH_PB_GATT
+    {
+		.type = BLE_GATT_SVC_TYPE_PRIMARY,
+		.uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL),
+        .includes = NULL,
+		.characteristics = (struct ble_gatt_chr_def[]) { {
+				.uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_DATA_IN_VAL),
+				.access_cb = proxy_char_access_cb,
+				.flags = BLE_GATT_CHR_F_WRITE_NO_RSP,
+			}, {
+				.uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_DATA_OUT_VAL),
+				.access_cb = dummy_access_cb,
+				.flags = BLE_GATT_CHR_F_NOTIFY,
+			}, {
+				0, /* No more characteristics in this service. */
+			} },
+	},
+#endif
+    {
+		0, /* No more services. */
+	},
+};
+#endif
+
+void gatt_register_cb(struct ble_gatt_register_ctxt *ctxt,
+                        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) {
+            proxy_svc_start_handle = ctxt->svc.handle;
+        } else if (ble_uuid_cmp(ctxt->svc.svc_def->uuid, BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL)) == 0) {
+            prov_svc_start_handle = ctxt->svc.handle;
+        }
+    }
+}
+
+void bt_mesh_gatt_init(void)
+{
+    ble_att_set_preferred_mtu(BLE_ATT_MTU_DFLT);
+
+    ble_hs_cfg.gatts_register_cb = gatt_register_cb;
+
+#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE
+    int rc;
+    ble_svc_gap_init();
+    ble_svc_gatt_init();
+
+    rc = ble_gatts_count_cfg(svc_defs);
+    assert(rc == 0);
+
+    rc = ble_gatts_add_svcs(svc_defs);
+    assert(rc == 0);
+
+    ble_gatts_start();
+
+    ble_gatts_svc_set_visibility(prov_svc_start_handle, 1);
+    ble_gatts_svc_set_visibility(proxy_svc_start_handle, 0);
+#endif
+
+#if CONFIG_BLE_MESH_PROVISIONER
+    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;
+        bt_mesh_gattc_info[i].wr_desc_done = false;
+    }
+#endif
+}
+
+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();
+}
+
+int bt_mesh_rand(void *buf, size_t len)
+{
+    int i;
+
+    if (buf == NULL || len == 0) {
+        BT_ERR("%s, Invalid parameter", __func__);
+        return -EAGAIN;
+    }
+
+    for (i = 0; i < (int)(len / sizeof(u32_t)); i++) {
+        u32_t rand = esp_random();
+        memcpy(buf + i * sizeof(u32_t), &rand, sizeof(u32_t));
+    }
+
+    BT_DBG("%s, rand: %s", __func__, bt_hex(buf, len));
+    return 0;
+}
+
+void bt_mesh_set_private_key(const u8_t pri_key[32])
+{
+    memcpy(bt_mesh_private_key, pri_key, 32);
+}
+
+int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv);
+
+const u8_t *bt_mesh_pub_key_get(void)
+{
+    uint8_t pri_key[32] = {0};
+
+#if 1
+    if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_HAS_PUB_KEY)) {
+        return bt_mesh_public_key;
+    }
+#else
+    /* BLE Mesh BQB test case MESH/NODE/PROV/UPD/BV-12-C requires
+     * different public key for each provisioning procedure.
+     * Note: if enabled, when Provisioner provision multiple devices
+     * at the same time, this may cause invalid confirmation value.
+     */
+    if (bt_mesh_rand(bt_mesh_private_key, 32)) {
+        BT_ERR("%s, Unable to generate bt_mesh_private_key", __func__);
+        return NULL;
+    }
+#endif
+
+    int rc = ble_sm_alg_gen_key_pair(bt_mesh_public_key, pri_key);
+    if (rc != 0) {
+        BT_ERR("%s, Failed to generate the key pair", __func__);
+        return NULL;
+    }
+    memcpy(bt_mesh_private_key, pri_key, 32);
+
+    bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_HAS_PUB_KEY);
+    BT_DBG("gen the bt_mesh_public_key:%s", bt_hex(bt_mesh_public_key, sizeof(bt_mesh_public_key)));
+
+    return bt_mesh_public_key;
+}
+
+bool bt_mesh_check_public_key(const u8_t key[64])
+{
+    struct mbedtls_ecp_point pt = {0};
+    mbedtls_ecp_group grp = {0};
+    bool rc = false;
+
+    uint8_t pub[65] = {0};
+    /* Hardcoded first byte of pub key for MBEDTLS_ECP_PF_UNCOMPRESSED */
+    pub[0] = 0x04;
+    memcpy(&pub[1], key, 64);
+
+    /* Initialize the required structures here */
+    mbedtls_ecp_point_init(&pt);
+    mbedtls_ecp_group_init(&grp);
+
+    /* Below 3 steps are to validate public key on curve secp256r1 */
+    if (mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1) != 0) {
+        goto exit;
+    }
+
+    if (mbedtls_ecp_point_read_binary(&grp, &pt, pub, 65) != 0) {
+        goto exit;
+    }
+
+    if (mbedtls_ecp_check_pubkey(&grp, &pt) != 0) {
+        goto exit;
+    }
+
+    rc = true;
+
+exit:
+    mbedtls_ecp_point_free(&pt);
+    mbedtls_ecp_group_free(&grp);
+    return rc;
+
+}
+
+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 u8_t remote_pk[64], bt_mesh_dh_key_cb_t cb, const u8_t idx)
+{
+    uint8_t dhkey[32];
+
+    BT_DBG("private key = %s", bt_hex(bt_mesh_private_key, BT_OCTET32_LEN));
+
+    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 u8_t *)dhkey, idx);
+    }
+    return 0;
+}
+
+#if CONFIG_MBEDTLS_HARDWARE_AES
+static void ecb_encrypt(u8_t const *const key_le, u8_t const *const clear_text_le,
+                        u8_t *const cipher_text_le, u8_t *const cipher_text_be)
+{
+    struct bt_mesh_ecb_param ecb;
+    mbedtls_aes_context aes_ctx = {0};
+
+    aes_ctx.key_bytes = 16;
+    mem_rcopy(&aes_ctx.key[0], key_le, 16);
+    mem_rcopy(&ecb.clear_text[0], clear_text_le, sizeof(ecb.clear_text));
+    mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, &ecb.clear_text[0], &ecb.cipher_text[0]);
+
+    if (cipher_text_le) {
+        mem_rcopy(cipher_text_le, &ecb.cipher_text[0],
+                  sizeof(ecb.cipher_text));
+    }
+
+    if (cipher_text_be) {
+        memcpy(cipher_text_be, &ecb.cipher_text[0],
+               sizeof(ecb.cipher_text));
+    }
+}
+
+static void ecb_encrypt_be(u8_t const *const key_be, u8_t const *const clear_text_be,
+                           u8_t *const cipher_text_be)
+{
+    struct bt_mesh_ecb_param ecb;
+    mbedtls_aes_context aes_ctx = {0};
+
+    aes_ctx.key_bytes = 16;
+    memcpy(&aes_ctx.key[0], key_be, 16);
+    memcpy(&ecb.clear_text[0], clear_text_be, sizeof(ecb.clear_text));
+    mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, &ecb.clear_text[0], &ecb.cipher_text[0]);
+
+    memcpy(cipher_text_be, &ecb.cipher_text[0], sizeof(ecb.cipher_text));
+}
+#endif /* CONFIG_MBEDTLS_HARDWARE_AES */
+
+int bt_mesh_encrypt_le(const u8_t key[16], const u8_t plaintext[16],
+                       u8_t enc_data[16])
+{
+#if CONFIG_MBEDTLS_HARDWARE_AES
+    BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16));
+
+    ecb_encrypt(key, plaintext, enc_data, NULL);
+
+    BT_DBG("enc_data %s", bt_hex(enc_data, 16));
+    return 0;
+#else /* CONFIG_MBEDTLS_HARDWARE_AES */
+    struct tc_aes_key_sched_struct s;
+    u8_t tmp[16];
+
+    BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16));
+
+    sys_memcpy_swap(tmp, key, 16);
+
+    if (tc_aes128_set_encrypt_key(&s, tmp) == TC_CRYPTO_FAIL) {
+        return -EINVAL;
+    }
+
+    sys_memcpy_swap(tmp, plaintext, 16);
+
+    if (tc_aes_encrypt(enc_data, tmp, &s) == TC_CRYPTO_FAIL) {
+        return -EINVAL;
+    }
+
+    sys_mem_swap(enc_data, 16);
+
+    BT_DBG("enc_data %s", bt_hex(enc_data, 16));
+
+    return 0;
+#endif /* CONFIG_MBEDTLS_HARDWARE_AES */
+}
+
+int bt_mesh_encrypt_be(const u8_t key[16], const u8_t plaintext[16],
+                       u8_t enc_data[16])
+{
+#if CONFIG_MBEDTLS_HARDWARE_AES
+    BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16));
+
+    ecb_encrypt_be(key, plaintext, enc_data);
+
+    BT_DBG("enc_data %s", bt_hex(enc_data, 16));
+
+    return 0;
+#else /* CONFIG_MBEDTLS_HARDWARE_AES */
+    struct tc_aes_key_sched_struct s;
+
+    BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16));
+
+    if (tc_aes128_set_encrypt_key(&s, key) == TC_CRYPTO_FAIL) {
+        return -EINVAL;
+    }
+
+    if (tc_aes_encrypt(enc_data, plaintext, &s) == TC_CRYPTO_FAIL) {
+        return -EINVAL;
+    }
+
+    BT_DBG("enc_data %s", bt_hex(enc_data, 16));
+
+    return 0;
+#endif /* CONFIG_MBEDTLS_HARDWARE_AES */
+}
+
+#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN)
+int bt_mesh_update_exceptional_list(u8_t sub_code, u8_t type, void *info)
+{
+    BT_ERR("%s, Unsupported for NimBLE host", __func__);
+    return 0;
+}
+#endif

+ 1 - 0
components/bt/host/bluedroid/api/esp_gap_bt_api.c

@@ -17,6 +17,7 @@
 #include "esp_bt_main.h"
 #include "esp_gap_bt_api.h"
 #include "common/bt_trace.h"
+#include "bta/bta_api.h"
 #include "btc/btc_manage.h"
 #include "btc_gap_bt.h"
 #include "btc/btc_storage.h"

+ 1 - 0
components/bt/host/nimble/Kconfig.in

@@ -53,6 +53,7 @@ config BT_NIMBLE_PINNED_TO_CORE
 config BT_NIMBLE_TASK_STACK_SIZE
     int "NimBLE Host task stack size"
     depends on BT_NIMBLE_ENABLED
+    default 5120 if BLE_MESH
     default 4096
     help
         This configures stack size of NimBLE host task

+ 1 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_client_model/main/CMakeLists.txt

@@ -1,4 +1,5 @@
 set(COMPONENT_SRCS "ble_mesh_demo_main.c"
+                   "ble_mesh_demo_init.c"
                    "board.c")
 
 set(COMPONENT_ADD_INCLUDEDIRS ".")

+ 143 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_client_model/main/ble_mesh_demo_init.c

@@ -0,0 +1,143 @@
+
+/*
+ * Copyright (c) 2017 Intel Corporation
+ * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <sdkconfig.h>
+/* BLE */
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
+#include "esp_bt.h"
+#include "esp_bt_main.h"
+#include "esp_bt_device.h"
+#endif
+
+#ifdef CONFIG_BT_NIMBLE_ENABLED
+#include "esp_nimble_hci.h"
+#include "nimble/nimble_port.h"
+#include "nimble/nimble_port_freertos.h"
+#include "host/ble_hs.h"
+#include "host/util/util.h"
+#include "console/console.h"
+#endif
+
+#include "esp_ble_mesh_defs.h"
+#include "ble_mesh_demo_init.h"
+#include "esp_ble_mesh_common_api.h"
+
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
+
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
+{
+    memcpy(dev_uuid + 2, esp_bt_dev_get_address(), BD_ADDR_LEN);
+}
+
+esp_err_t bluetooth_init(void)
+{
+    esp_err_t ret;
+
+
+    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
+
+    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
+    ret = esp_bt_controller_init(&bt_cfg);
+    if (ret) {
+        ESP_LOGE(TAG, "%s initialize controller failed", __func__);
+        return ret;
+    }
+
+    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
+    if (ret) {
+        ESP_LOGE(TAG, "%s enable controller failed", __func__);
+        return ret;
+    }
+    ret = esp_bluedroid_init();
+    if (ret) {
+        ESP_LOGE(TAG, "%s init bluetooth failed", __func__);
+        return ret;
+    }
+    ret = esp_bluedroid_enable();
+    if (ret) {
+        ESP_LOGE(TAG, "%s enable bluetooth failed", __func__);
+        return ret;
+    }
+
+    return ret;
+}
+
+#endif
+
+#ifdef CONFIG_BT_NIMBLE_ENABLED
+static SemaphoreHandle_t mesh_sem;
+static uint8_t own_addr_type;
+void ble_store_config_init(void);
+static uint8_t addr_val[6] = {0};
+    
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
+{
+    memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN);
+}
+
+static void mesh_on_reset(int reason)
+{
+    ESP_LOGI(TAG, "Resetting state; reason=%d", reason);
+}
+
+static void mesh_on_sync(void)
+{
+    int rc;
+
+    rc = ble_hs_util_ensure_addr(0);
+    assert(rc == 0);
+
+    /* Figure out address to use while advertising (no privacy for now) */
+    rc = ble_hs_id_infer_auto(0, &own_addr_type);
+    if (rc != 0) {
+        ESP_LOGI(TAG, "error determining address type; rc=%d", rc);
+        return;
+    }
+
+    rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
+
+    xSemaphoreGive(mesh_sem);
+}
+
+void mesh_host_task(void *param)
+{
+    ESP_LOGI(TAG, "BLE Host Task Started");
+    /* This function will return only when nimble_port_stop() is executed */
+    nimble_port_run();
+
+    nimble_port_freertos_deinit();
+}
+
+esp_err_t bluetooth_init(void)
+{
+    mesh_sem = xSemaphoreCreateBinary();
+    if (mesh_sem == NULL) {
+        ESP_LOGE(TAG, "Failed to create mesh semaphore");
+        return ESP_FAIL;
+    }
+
+    ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
+
+    nimble_port_init();
+    /* Initialize the NimBLE host configuration. */
+    ble_hs_cfg.reset_cb = mesh_on_reset;
+    ble_hs_cfg.sync_cb = mesh_on_sync;
+    ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
+
+    /* XXX Need to have template for store */
+    ble_store_config_init();
+
+    nimble_port_freertos_init(mesh_host_task);
+
+    xSemaphoreTake(mesh_sem, portMAX_DELAY);
+
+    return ESP_OK;
+}
+#endif

+ 18 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_client_model/main/ble_mesh_demo_init.h

@@ -0,0 +1,18 @@
+/*
+   This example code is in the Public Domain (or CC0 licensed, at your option.)
+
+   Unless required by applicable law or agreed to in writing, this
+   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+   CONDITIONS OF ANY KIND, either express or implied.
+*/
+
+#ifndef _BLE_MESH_DEMO_INIT_H_
+#define _BLE_MESH_DEMO_INIT_H_
+
+#define TAG "ble_mesh_client"
+
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid);
+
+esp_err_t bluetooth_init(void);
+
+#endif

+ 10 - 46
examples/bluetooth/esp_ble_mesh/ble_mesh_client_model/main/ble_mesh_demo_main.c

@@ -13,9 +13,6 @@
 #include "esp_log.h"
 #include "nvs_flash.h"
 
-#include "esp_bt.h"
-#include "esp_bt_main.h"
-#include "esp_bt_device.h"
 #include "esp_ble_mesh_common_api.h"
 #include "esp_ble_mesh_provisioning_api.h"
 #include "esp_ble_mesh_networking_api.h"
@@ -23,8 +20,7 @@
 #include "esp_ble_mesh_generic_model_api.h"
 
 #include "board.h"
-
-#define TAG "ble_mesh_client"
+#include "ble_mesh_demo_init.h"
 
 #define CID_ESP 0x02E5
 
@@ -450,8 +446,6 @@ static int ble_mesh_init(void)
 {
     int err = 0;
 
-    memcpy(dev_uuid + 2, esp_bt_dev_get_address(), ESP_BD_ADDR_LEN);
-
     esp_ble_mesh_register_prov_callback(esp_ble_mesh_prov_cb);
     esp_ble_mesh_register_custom_model_callback(esp_ble_mesh_model_cb);
     esp_ble_mesh_register_generic_client_callback(esp_ble_mesh_generic_cb);
@@ -471,45 +465,6 @@ static int ble_mesh_init(void)
     return err;
 }
 
-static esp_err_t bluetooth_init(void)
-{
-    esp_err_t ret;
-
-    ret = nvs_flash_init();
-    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
-        ESP_ERROR_CHECK(nvs_flash_erase());
-        ret = nvs_flash_init();
-    }
-    ESP_ERROR_CHECK(ret);
-
-    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
-
-    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
-    ret = esp_bt_controller_init(&bt_cfg);
-    if (ret) {
-        ESP_LOGE(TAG, "%s initialize controller failed", __func__);
-        return ret;
-    }
-
-    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
-    if (ret) {
-        ESP_LOGE(TAG, "%s enable controller failed", __func__);
-        return ret;
-    }
-    ret = esp_bluedroid_init();
-    if (ret) {
-        ESP_LOGE(TAG, "%s init bluetooth failed", __func__);
-        return ret;
-    }
-    ret = esp_bluedroid_enable();
-    if (ret) {
-        ESP_LOGE(TAG, "%s enable bluetooth failed", __func__);
-        return ret;
-    }
-
-    return ret;
-}
-
 void app_main(void)
 {
     int err;
@@ -518,12 +473,21 @@ void app_main(void)
 
     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) {

+ 3 - 3
examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_provisioner/main/ble_mesh_register_provisioner_cmd.c

@@ -82,7 +82,7 @@ ble_mesh_provisioner_add_key_t provisioner_add_key;
 
 void ble_mesh_regist_provisioner_cmd();
 
-void ble_mesh_prov_adv_cb(const esp_bd_addr_t addr, const esp_ble_addr_type_t addr_type, const uint8_t adv_type,
+void ble_mesh_prov_adv_cb(const esp_ble_mesh_bd_addr_t addr, const esp_ble_mesh_addr_type_t addr_type, const uint8_t adv_type,
                           const uint8_t *dev_uuid, uint16_t oob_info, esp_ble_mesh_prov_bearer_t bearer);
 
 void ble_mesh_register_mesh_provisioner()
@@ -90,12 +90,12 @@ void ble_mesh_register_mesh_provisioner()
     ble_mesh_regist_provisioner_cmd();
 }
 
-void ble_mesh_prov_adv_cb(const esp_bd_addr_t addr, const esp_ble_addr_type_t addr_type, const uint8_t adv_type,
+void ble_mesh_prov_adv_cb(const esp_ble_mesh_bd_addr_t addr, const esp_ble_mesh_addr_type_t addr_type, const uint8_t adv_type,
                           const uint8_t *dev_uuid, uint16_t oob_info, esp_ble_mesh_prov_bearer_t bearer)
 {
     ESP_LOGD(TAG, "enter %s\n", __func__);
     ESP_LOGI(TAG, "scan device address:");
-    esp_log_buffer_hex(TAG, addr, sizeof(esp_bd_addr_t));
+    esp_log_buffer_hex(TAG, addr, sizeof(esp_ble_mesh_bd_addr_t));
     ESP_LOGI(TAG, "scan device uuid:");
     esp_log_buffer_hex(TAG, dev_uuid, 16);
     ESP_LOGD(TAG, "exit %s\n", __func__);

+ 2 - 1
examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/CMakeLists.txt

@@ -1,4 +1,5 @@
-set(COMPONENT_SRCS "ble_mesh_demo_main.c")
+set(COMPONENT_SRCS "ble_mesh_demo_main.c"
+                   "ble_mesh_demo_init.c")
 
 set(COMPONENT_ADD_INCLUDEDIRS ".")
 

+ 143 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_init.c

@@ -0,0 +1,143 @@
+
+/*
+ * Copyright (c) 2017 Intel Corporation
+ * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <sdkconfig.h>
+/* BLE */
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
+#include "esp_bt.h"
+#include "esp_bt_main.h"
+#include "esp_bt_device.h"
+#endif
+
+#ifdef CONFIG_BT_NIMBLE_ENABLED
+#include "esp_nimble_hci.h"
+#include "nimble/nimble_port.h"
+#include "nimble/nimble_port_freertos.h"
+#include "host/ble_hs.h"
+#include "host/util/util.h"
+#include "console/console.h"
+#endif
+
+#include "esp_ble_mesh_defs.h"
+#include "ble_mesh_demo_init.h"
+#include "esp_ble_mesh_common_api.h"
+
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
+
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
+{
+    memcpy(dev_uuid, esp_bt_dev_get_address(), BD_ADDR_LEN);
+}
+
+esp_err_t bluetooth_init(void)
+{
+    esp_err_t ret;
+
+
+    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
+
+    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
+    ret = esp_bt_controller_init(&bt_cfg);
+    if (ret) {
+        ESP_LOGE(TAG, "%s initialize controller failed", __func__);
+        return ret;
+    }
+
+    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
+    if (ret) {
+        ESP_LOGE(TAG, "%s enable controller failed", __func__);
+        return ret;
+    }
+    ret = esp_bluedroid_init();
+    if (ret) {
+        ESP_LOGE(TAG, "%s init bluetooth failed", __func__);
+        return ret;
+    }
+    ret = esp_bluedroid_enable();
+    if (ret) {
+        ESP_LOGE(TAG, "%s enable bluetooth failed", __func__);
+        return ret;
+    }
+
+    return ret;
+}
+
+#endif
+
+#ifdef CONFIG_BT_NIMBLE_ENABLED
+static SemaphoreHandle_t mesh_sem;
+static uint8_t own_addr_type;
+void ble_store_config_init(void);
+static uint8_t addr_val[6] = {0};
+    
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
+{
+    memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN);
+}
+
+static void mesh_on_reset(int reason)
+{
+    ESP_LOGI(TAG, "Resetting state; reason=%d", reason);
+}
+
+static void mesh_on_sync(void)
+{
+    int rc;
+
+    rc = ble_hs_util_ensure_addr(0);
+    assert(rc == 0);
+
+    /* Figure out address to use while advertising (no privacy for now) */
+    rc = ble_hs_id_infer_auto(0, &own_addr_type);
+    if (rc != 0) {
+        ESP_LOGI(TAG, "error determining address type; rc=%d", rc);
+        return;
+    }
+
+    rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
+
+    xSemaphoreGive(mesh_sem);
+}
+
+void mesh_host_task(void *param)
+{
+    ESP_LOGI(TAG, "BLE Host Task Started");
+    /* This function will return only when nimble_port_stop() is executed */
+    nimble_port_run();
+
+    nimble_port_freertos_deinit();
+}
+
+esp_err_t bluetooth_init(void)
+{
+    mesh_sem = xSemaphoreCreateBinary();
+    if (mesh_sem == NULL) {
+        ESP_LOGE(TAG, "Failed to create mesh semaphore");
+        return ESP_FAIL;
+    }
+
+    ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
+
+    nimble_port_init();
+    /* Initialize the NimBLE host configuration. */
+    ble_hs_cfg.reset_cb = mesh_on_reset;
+    ble_hs_cfg.sync_cb = mesh_on_sync;
+    ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
+
+    /* XXX Need to have template for store */
+    ble_store_config_init();
+
+    nimble_port_freertos_init(mesh_host_task);
+
+    xSemaphoreTake(mesh_sem, portMAX_DELAY);
+
+    return ESP_OK;
+}
+#endif

+ 18 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_init.h

@@ -0,0 +1,18 @@
+/*
+   This example code is in the Public Domain (or CC0 licensed, at your option.)
+
+   Unless required by applicable law or agreed to in writing, this
+   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+   CONDITIONS OF ANY KIND, either express or implied.
+*/
+
+#ifndef _BLE_MESH_DEMO_INIT_H_
+#define _BLE_MESH_DEMO_INIT_H_
+
+#define TAG "FAST_PROV_CLIENT_DEMO"
+
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid);
+
+esp_err_t bluetooth_init(void);
+
+#endif

+ 16 - 53
examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_client/main/ble_mesh_demo_main.c

@@ -19,10 +19,6 @@
 #include "esp_log.h"
 #include "nvs_flash.h"
 
-#include "esp_bt.h"
-#include "esp_bt_main.h"
-#include "esp_bt_device.h"
-#include "esp_gap_ble_api.h"
 #include "esp_ble_mesh_defs.h"
 #include "esp_ble_mesh_common_api.h"
 #include "esp_ble_mesh_provisioning_api.h"
@@ -33,8 +29,7 @@
 #include "esp_fast_prov_common.h"
 #include "esp_fast_prov_operation.h"
 #include "esp_fast_prov_client_model.h"
-
-#define TAG "FAST_PROV_CLIENT_DEMO"
+#include "ble_mesh_demo_init.h"
 
 #define PROV_OWN_ADDR       0x0001
 #define APP_KEY_OCTET       0x12
@@ -191,8 +186,8 @@ static void provisioner_prov_complete(int node_index, const uint8_t uuid[16], ui
     }
 }
 
-static void example_recv_unprov_adv_pkt(uint8_t dev_uuid[16], uint8_t addr[ESP_BD_ADDR_LEN],
-                                        esp_ble_addr_type_t addr_type, uint16_t oob_info,
+static void example_recv_unprov_adv_pkt(uint8_t dev_uuid[16], uint8_t addr[BLE_MESH_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};
@@ -532,8 +527,6 @@ static esp_err_t ble_mesh_init(void)
 {
     esp_err_t err;
 
-    memcpy(dev_uuid, esp_bt_dev_get_address(), 6);
-
     prov_info.unicast_min = prov.prov_start_address + prov_info.max_node_num;
     prov_info.match_len   = sizeof(match);
     memcpy(prov_info.match_val, match, sizeof(match));
@@ -544,15 +537,15 @@ static esp_err_t ble_mesh_init(void)
     esp_ble_mesh_register_config_client_callback(example_config_client_callback);
     esp_ble_mesh_register_generic_client_callback(example_generic_client_callback);
 
-    err = esp_ble_mesh_provisioner_set_dev_uuid_match(match, 0x02, 0x00, false);
+    err = esp_ble_mesh_init(&prov, &comp);
     if (err != ESP_OK) {
-        ESP_LOGE(TAG, "%s: Failed to set matching device UUID", __func__);
+        ESP_LOGE(TAG, "%s: Failed to initialize BLE Mesh", __func__);
         return ESP_FAIL;
     }
 
-    err = esp_ble_mesh_init(&prov, &comp);
+    err = esp_ble_mesh_provisioner_set_dev_uuid_match(match, 0x02, 0x00, false);
     if (err != ESP_OK) {
-        ESP_LOGE(TAG, "%s: Failed to initialize BLE Mesh", __func__);
+        ESP_LOGE(TAG, "%s: Failed to set matching device UUID", __func__);
         return ESP_FAIL;
     }
 
@@ -579,57 +572,27 @@ static esp_err_t ble_mesh_init(void)
     return err;
 }
 
-esp_err_t bluetooth_init(void)
-{
-    esp_err_t ret;
-
-    ret = nvs_flash_init();
-    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
-        ESP_ERROR_CHECK(nvs_flash_erase());
-        ret = nvs_flash_init();
-    }
-    ESP_ERROR_CHECK(ret);
-
-    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
-
-    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
-    ret = esp_bt_controller_init(&bt_cfg);
-    if (ret) {
-        ESP_LOGE(TAG, "%s initialize controller failed", __func__);
-        return ret;
-    }
-
-    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
-    if (ret) {
-        ESP_LOGE(TAG, "%s enable controller failed", __func__);
-        return ret;
-    }
-    ret = esp_bluedroid_init();
-    if (ret) {
-        ESP_LOGE(TAG, "%s init bluetooth failed", __func__);
-        return ret;
-    }
-    ret = esp_bluedroid_enable();
-    if (ret) {
-        ESP_LOGE(TAG, "%s enable bluetooth failed", __func__);
-        return ret;
-    }
-
-    return ret;
-}
-
 void app_main(void)
 {
     int err;
 
     ESP_LOGI(TAG, "Initializing...");
 
+    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) {

+ 1 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/CMakeLists.txt

@@ -1,4 +1,5 @@
 set(COMPONENT_SRCS "ble_mesh_demo_main.c"
+                   "ble_mesh_demo_init.c"
                    "board.c")
 
 set(COMPONENT_ADD_INCLUDEDIRS ".")

+ 143 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_init.c

@@ -0,0 +1,143 @@
+
+/*
+ * Copyright (c) 2017 Intel Corporation
+ * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <sdkconfig.h>
+/* BLE */
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
+#include "esp_bt.h"
+#include "esp_bt_main.h"
+#include "esp_bt_device.h"
+#endif
+
+#ifdef CONFIG_BT_NIMBLE_ENABLED
+#include "esp_nimble_hci.h"
+#include "nimble/nimble_port.h"
+#include "nimble/nimble_port_freertos.h"
+#include "host/ble_hs.h"
+#include "host/util/util.h"
+#include "console/console.h"
+#endif
+
+#include "esp_ble_mesh_defs.h"
+#include "ble_mesh_demo_init.h"
+#include "esp_ble_mesh_common_api.h"
+
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
+
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
+{
+    memcpy(dev_uuid + 2, esp_bt_dev_get_address(), BD_ADDR_LEN);
+}
+
+esp_err_t bluetooth_init(void)
+{
+    esp_err_t ret;
+
+
+    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
+
+    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
+    ret = esp_bt_controller_init(&bt_cfg);
+    if (ret) {
+        ESP_LOGE(TAG, "%s initialize controller failed", __func__);
+        return ret;
+    }
+
+    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
+    if (ret) {
+        ESP_LOGE(TAG, "%s enable controller failed", __func__);
+        return ret;
+    }
+    ret = esp_bluedroid_init();
+    if (ret) {
+        ESP_LOGE(TAG, "%s init bluetooth failed", __func__);
+        return ret;
+    }
+    ret = esp_bluedroid_enable();
+    if (ret) {
+        ESP_LOGE(TAG, "%s enable bluetooth failed", __func__);
+        return ret;
+    }
+
+    return ret;
+}
+
+#endif
+
+#ifdef CONFIG_BT_NIMBLE_ENABLED
+static SemaphoreHandle_t mesh_sem;
+static uint8_t own_addr_type;
+void ble_store_config_init(void);
+static uint8_t addr_val[6] = {0};
+    
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
+{
+    memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN);
+}
+
+static void mesh_on_reset(int reason)
+{
+    ESP_LOGI(TAG, "Resetting state; reason=%d", reason);
+}
+
+static void mesh_on_sync(void)
+{
+    int rc;
+
+    rc = ble_hs_util_ensure_addr(0);
+    assert(rc == 0);
+
+    /* Figure out address to use while advertising (no privacy for now) */
+    rc = ble_hs_id_infer_auto(0, &own_addr_type);
+    if (rc != 0) {
+        ESP_LOGI(TAG, "error determining address type; rc=%d", rc);
+        return;
+    }
+
+    rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
+
+    xSemaphoreGive(mesh_sem);
+}
+
+void mesh_host_task(void *param)
+{
+    ESP_LOGI(TAG, "BLE Host Task Started");
+    /* This function will return only when nimble_port_stop() is executed */
+    nimble_port_run();
+
+    nimble_port_freertos_deinit();
+}
+
+esp_err_t bluetooth_init(void)
+{
+    mesh_sem = xSemaphoreCreateBinary();
+    if (mesh_sem == NULL) {
+        ESP_LOGE(TAG, "Failed to create mesh semaphore");
+        return ESP_FAIL;
+    }
+
+    ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
+
+    nimble_port_init();
+    /* Initialize the NimBLE host configuration. */
+    ble_hs_cfg.reset_cb = mesh_on_reset;
+    ble_hs_cfg.sync_cb = mesh_on_sync;
+    ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
+
+    /* XXX Need to have template for store */
+    ble_store_config_init();
+
+    nimble_port_freertos_init(mesh_host_task);
+
+    xSemaphoreTake(mesh_sem, portMAX_DELAY);
+
+    return ESP_OK;
+}
+#endif

+ 18 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_init.h

@@ -0,0 +1,18 @@
+/*
+   This example code is in the Public Domain (or CC0 licensed, at your option.)
+
+   Unless required by applicable law or agreed to in writing, this
+   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+   CONDITIONS OF ANY KIND, either express or implied.
+*/
+
+#ifndef _BLE_MESH_DEMO_INIT_H_
+#define _BLE_MESH_DEMO_INIT_H_
+
+#define TAG "FAST_PROV_SERVER_DEMO"
+
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid);
+
+esp_err_t bluetooth_init(void);
+
+#endif

+ 13 - 53
examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/ble_mesh_fast_prov_server/main/ble_mesh_demo_main.c

@@ -18,10 +18,6 @@
 #include "esp_log.h"
 #include "nvs_flash.h"
 
-#include "esp_bt.h"
-#include "esp_bt_main.h"
-#include "esp_bt_device.h"
-
 #include "esp_ble_mesh_defs.h"
 #include "esp_ble_mesh_common_api.h"
 #include "esp_ble_mesh_networking_api.h"
@@ -33,8 +29,7 @@
 #include "esp_fast_prov_operation.h"
 #include "esp_fast_prov_client_model.h"
 #include "esp_fast_prov_server_model.h"
-
-#define TAG "FAST_PROV_SERVER_DEMO"
+#include "ble_mesh_demo_init.h"
 
 extern struct _led_state led_state[3];
 extern struct k_delayed_work send_self_prov_node_addr_timer;
@@ -336,8 +331,8 @@ static void provisioner_prov_complete(int node_idx, const uint8_t uuid[16], uint
     }
 }
 
-static void example_recv_unprov_adv_pkt(uint8_t dev_uuid[16], uint8_t addr[ESP_BD_ADDR_LEN],
-                                        esp_ble_addr_type_t addr_type, uint16_t oob_info,
+static void example_recv_unprov_adv_pkt(uint8_t dev_uuid[16], uint8_t addr[BLE_MESH_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};
@@ -358,7 +353,7 @@ static void example_recv_unprov_adv_pkt(uint8_t dev_uuid[16], uint8_t addr[ESP_B
         add_dev.oob_info = oob_info;
         add_dev.bearer = (uint8_t)bearer;
         memcpy(add_dev.uuid, dev_uuid, 16);
-        memcpy(add_dev.addr, addr, ESP_BD_ADDR_LEN);
+        memcpy(add_dev.addr, addr, BLE_MESH_ADDR_LEN);
         flag = ADD_DEV_RM_AFTER_PROV_FLAG | ADD_DEV_START_PROV_NOW_FLAG | ADD_DEV_FLUSHABLE_DEV_FLAG;
         err = esp_ble_mesh_provisioner_add_unprov_dev(&add_dev, flag);
         if (err != ESP_OK) {
@@ -733,9 +728,6 @@ static esp_err_t ble_mesh_init(void)
 {
     esp_err_t err;
 
-    /* First two bytes of device uuid is compared with match value by Provisioner */
-    memcpy(dev_uuid + 2, esp_bt_dev_get_address(), 6);
-
     esp_ble_mesh_register_prov_callback(example_ble_mesh_provisioning_cb);
     esp_ble_mesh_register_custom_model_callback(example_ble_mesh_custom_model_cb);
     esp_ble_mesh_register_config_client_callback(example_ble_mesh_config_client_cb);
@@ -774,47 +766,6 @@ static esp_err_t ble_mesh_init(void)
     return ESP_OK;
 }
 
-static esp_err_t bluetooth_init(void)
-{
-    esp_err_t ret;
-
-    ret = nvs_flash_init();
-    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
-        ESP_ERROR_CHECK(nvs_flash_erase());
-        ret = nvs_flash_init();
-    }
-    ESP_ERROR_CHECK(ret);
-
-    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
-
-    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
-    ret = esp_bt_controller_init(&bt_cfg);
-    if (ret) {
-        ESP_LOGE(TAG, "%s initialize controller failed", __func__);
-        return ret;
-    }
-
-    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
-    if (ret) {
-        ESP_LOGE(TAG, "%s enable controller failed", __func__);
-        return ret;
-    }
-
-    ret = esp_bluedroid_init();
-    if (ret) {
-        ESP_LOGE(TAG, "%s init bluetooth failed", __func__);
-        return ret;
-    }
-
-    ret = esp_bluedroid_enable();
-    if (ret) {
-        ESP_LOGE(TAG, "%s enable bluetooth failed", __func__);
-        return ret;
-    }
-
-    return ret;
-}
-
 void app_main(void)
 {
     esp_err_t err;
@@ -827,12 +778,21 @@ void app_main(void)
         return;
     }
 
+    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) {

+ 1 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_node/main/CMakeLists.txt

@@ -1,4 +1,5 @@
 set(COMPONENT_SRCS "ble_mesh_demo_main.c"
+                   "ble_mesh_demo_init.c"
                    "board.c")
 
 set(COMPONENT_ADD_INCLUDEDIRS ".")

+ 143 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_node/main/ble_mesh_demo_init.c

@@ -0,0 +1,143 @@
+
+/*
+ * Copyright (c) 2017 Intel Corporation
+ * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <sdkconfig.h>
+/* BLE */
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
+#include "esp_bt.h"
+#include "esp_bt_main.h"
+#include "esp_bt_device.h"
+#endif
+
+#ifdef CONFIG_BT_NIMBLE_ENABLED
+#include "esp_nimble_hci.h"
+#include "nimble/nimble_port.h"
+#include "nimble/nimble_port_freertos.h"
+#include "host/ble_hs.h"
+#include "host/util/util.h"
+#include "console/console.h"
+#endif
+
+#include "esp_ble_mesh_defs.h"
+#include "ble_mesh_demo_init.h"
+#include "esp_ble_mesh_common_api.h"
+
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
+
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
+{
+    memcpy(dev_uuid + 2, esp_bt_dev_get_address(), BD_ADDR_LEN);
+}
+
+esp_err_t bluetooth_init(void)
+{
+    esp_err_t ret;
+
+
+    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
+
+    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
+    ret = esp_bt_controller_init(&bt_cfg);
+    if (ret) {
+        ESP_LOGE(TAG, "%s initialize controller failed", __func__);
+        return ret;
+    }
+
+    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
+    if (ret) {
+        ESP_LOGE(TAG, "%s enable controller failed", __func__);
+        return ret;
+    }
+    ret = esp_bluedroid_init();
+    if (ret) {
+        ESP_LOGE(TAG, "%s init bluetooth failed", __func__);
+        return ret;
+    }
+    ret = esp_bluedroid_enable();
+    if (ret) {
+        ESP_LOGE(TAG, "%s enable bluetooth failed", __func__);
+        return ret;
+    }
+
+    return ret;
+}
+
+#endif
+
+#ifdef CONFIG_BT_NIMBLE_ENABLED
+static SemaphoreHandle_t mesh_sem;
+static uint8_t own_addr_type;
+void ble_store_config_init(void);
+static uint8_t addr_val[6] = {0};
+    
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
+{
+    memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN);
+}
+
+static void mesh_on_reset(int reason)
+{
+    ESP_LOGI(TAG, "Resetting state; reason=%d", reason);
+}
+
+static void mesh_on_sync(void)
+{
+    int rc;
+
+    rc = ble_hs_util_ensure_addr(0);
+    assert(rc == 0);
+
+    /* Figure out address to use while advertising (no privacy for now) */
+    rc = ble_hs_id_infer_auto(0, &own_addr_type);
+    if (rc != 0) {
+        ESP_LOGI(TAG, "error determining address type; rc=%d", rc);
+        return;
+    }
+
+    rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
+
+    xSemaphoreGive(mesh_sem);
+}
+
+void mesh_host_task(void *param)
+{
+    ESP_LOGI(TAG, "BLE Host Task Started");
+    /* This function will return only when nimble_port_stop() is executed */
+    nimble_port_run();
+
+    nimble_port_freertos_deinit();
+}
+
+esp_err_t bluetooth_init(void)
+{
+    mesh_sem = xSemaphoreCreateBinary();
+    if (mesh_sem == NULL) {
+        ESP_LOGE(TAG, "Failed to create mesh semaphore");
+        return ESP_FAIL;
+    }
+
+    ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
+
+    nimble_port_init();
+    /* Initialize the NimBLE host configuration. */
+    ble_hs_cfg.reset_cb = mesh_on_reset;
+    ble_hs_cfg.sync_cb = mesh_on_sync;
+    ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
+
+    /* XXX Need to have template for store */
+    ble_store_config_init();
+
+    nimble_port_freertos_init(mesh_host_task);
+
+    xSemaphoreTake(mesh_sem, portMAX_DELAY);
+
+    return ESP_OK;
+}
+#endif

+ 18 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_node/main/ble_mesh_demo_init.h

@@ -0,0 +1,18 @@
+/*
+   This example code is in the Public Domain (or CC0 licensed, at your option.)
+
+   Unless required by applicable law or agreed to in writing, this
+   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+   CONDITIONS OF ANY KIND, either express or implied.
+*/
+
+#ifndef _BLE_MESH_DEMO_INIT_H_
+#define _BLE_MESH_DEMO_INIT_H_
+
+#define TAG "ble_mesh_node"
+
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid);
+
+esp_err_t bluetooth_init(void);
+
+#endif

+ 10 - 47
examples/bluetooth/esp_ble_mesh/ble_mesh_node/main/ble_mesh_demo_main.c

@@ -13,10 +13,6 @@
 #include "esp_log.h"
 #include "nvs_flash.h"
 
-#include "esp_bt.h"
-#include "esp_bt_main.h"
-#include "esp_bt_device.h"
-
 #include "esp_ble_mesh_defs.h"
 #include "esp_ble_mesh_common_api.h"
 #include "esp_ble_mesh_networking_api.h"
@@ -24,8 +20,7 @@
 #include "esp_ble_mesh_config_model_api.h"
 
 #include "board.h"
-
-#define TAG "ble_mesh_node"
+#include "ble_mesh_demo_init.h"
 
 #define CID_ESP 0x02E5
 
@@ -314,8 +309,6 @@ static esp_err_t ble_mesh_init(void)
 {
     int err = 0;
 
-    memcpy(dev_uuid + 2, esp_bt_dev_get_address(), ESP_BD_ADDR_LEN);
-
     esp_ble_mesh_register_prov_callback(esp_ble_mesh_prov_cb);
     esp_ble_mesh_register_custom_model_callback(esp_ble_mesh_model_cb);
 
@@ -334,45 +327,6 @@ static esp_err_t ble_mesh_init(void)
     return err;
 }
 
-static esp_err_t bluetooth_init(void)
-{
-    esp_err_t ret;
-
-    ret = nvs_flash_init();
-    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
-        ESP_ERROR_CHECK(nvs_flash_erase());
-        ret = nvs_flash_init();
-    }
-    ESP_ERROR_CHECK(ret);
-
-    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
-
-    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
-    ret = esp_bt_controller_init(&bt_cfg);
-    if (ret) {
-        ESP_LOGE(TAG, "%s initialize controller failed", __func__);
-        return ret;
-    }
-
-    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
-    if (ret) {
-        ESP_LOGE(TAG, "%s enable controller failed", __func__);
-        return ret;
-    }
-    ret = esp_bluedroid_init();
-    if (ret) {
-        ESP_LOGE(TAG, "%s init bluetooth failed", __func__);
-        return ret;
-    }
-    ret = esp_bluedroid_enable();
-    if (ret) {
-        ESP_LOGE(TAG, "%s enable bluetooth failed", __func__);
-        return ret;
-    }
-
-    return ret;
-}
-
 void app_main(void)
 {
     int err;
@@ -381,12 +335,21 @@ void app_main(void)
 
     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) {

+ 1 - 1
examples/bluetooth/esp_ble_mesh/ble_mesh_node/tutorial/Ble_Mesh_Node_Example_Walkthrough.md

@@ -97,7 +97,7 @@ static esp_err_t ble_mesh_init(void)
 {
     int err = 0;
 
-    memcpy(dev_uuid + 2, esp_bt_dev_get_address(), ESP_BD_ADDR_LEN);
+    memcpy(dev_uuid + 2, esp_bt_dev_get_address(), BLE_MESH_ADDR_LEN);
 
     // See comment 1
      esp_ble_mesh_register_prov_callback(esp_ble_mesh_prov_cb);

+ 2 - 1
examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/CMakeLists.txt

@@ -1,4 +1,5 @@
-set(COMPONENT_SRCS "ble_mesh_demo_main.c")
+set(COMPONENT_SRCS "ble_mesh_demo_main.c"
+                   "ble_mesh_demo_init.c")
 
 set(COMPONENT_ADD_INCLUDEDIRS ".")
 

+ 143 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_init.c

@@ -0,0 +1,143 @@
+
+/*
+ * Copyright (c) 2017 Intel Corporation
+ * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <sdkconfig.h>
+/* BLE */
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
+#include "esp_bt.h"
+#include "esp_bt_main.h"
+#include "esp_bt_device.h"
+#endif
+
+#ifdef CONFIG_BT_NIMBLE_ENABLED
+#include "esp_nimble_hci.h"
+#include "nimble/nimble_port.h"
+#include "nimble/nimble_port_freertos.h"
+#include "host/ble_hs.h"
+#include "host/util/util.h"
+#include "console/console.h"
+#endif
+
+#include "esp_ble_mesh_defs.h"
+#include "ble_mesh_demo_init.h"
+#include "esp_ble_mesh_common_api.h"
+
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
+
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
+{
+    memcpy(dev_uuid + 2, esp_bt_dev_get_address(), BD_ADDR_LEN);
+}
+
+esp_err_t bluetooth_init(void)
+{
+    esp_err_t ret;
+
+
+    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
+
+    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
+    ret = esp_bt_controller_init(&bt_cfg);
+    if (ret) {
+        ESP_LOGE(TAG, "%s initialize controller failed", __func__);
+        return ret;
+    }
+
+    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
+    if (ret) {
+        ESP_LOGE(TAG, "%s enable controller failed", __func__);
+        return ret;
+    }
+    ret = esp_bluedroid_init();
+    if (ret) {
+        ESP_LOGE(TAG, "%s init bluetooth failed", __func__);
+        return ret;
+    }
+    ret = esp_bluedroid_enable();
+    if (ret) {
+        ESP_LOGE(TAG, "%s enable bluetooth failed", __func__);
+        return ret;
+    }
+
+    return ret;
+}
+
+#endif
+
+#ifdef CONFIG_BT_NIMBLE_ENABLED
+static SemaphoreHandle_t mesh_sem;
+static uint8_t own_addr_type;
+void ble_store_config_init(void);
+static uint8_t addr_val[6] = {0};
+    
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
+{
+    memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN);
+}
+
+static void mesh_on_reset(int reason)
+{
+    ESP_LOGI(TAG, "Resetting state; reason=%d", reason);
+}
+
+static void mesh_on_sync(void)
+{
+    int rc;
+
+    rc = ble_hs_util_ensure_addr(0);
+    assert(rc == 0);
+
+    /* Figure out address to use while advertising (no privacy for now) */
+    rc = ble_hs_id_infer_auto(0, &own_addr_type);
+    if (rc != 0) {
+        ESP_LOGI(TAG, "error determining address type; rc=%d", rc);
+        return;
+    }
+
+    rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
+
+    xSemaphoreGive(mesh_sem);
+}
+
+void mesh_host_task(void *param)
+{
+    ESP_LOGI(TAG, "BLE Host Task Started");
+    /* This function will return only when nimble_port_stop() is executed */
+    nimble_port_run();
+
+    nimble_port_freertos_deinit();
+}
+
+esp_err_t bluetooth_init(void)
+{
+    mesh_sem = xSemaphoreCreateBinary();
+    if (mesh_sem == NULL) {
+        ESP_LOGE(TAG, "Failed to create mesh semaphore");
+        return ESP_FAIL;
+    }
+
+    ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
+
+    nimble_port_init();
+    /* Initialize the NimBLE host configuration. */
+    ble_hs_cfg.reset_cb = mesh_on_reset;
+    ble_hs_cfg.sync_cb = mesh_on_sync;
+    ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
+
+    /* XXX Need to have template for store */
+    ble_store_config_init();
+
+    nimble_port_freertos_init(mesh_host_task);
+
+    xSemaphoreTake(mesh_sem, portMAX_DELAY);
+
+    return ESP_OK;
+}
+#endif

+ 18 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_init.h

@@ -0,0 +1,18 @@
+/*
+   This example code is in the Public Domain (or CC0 licensed, at your option.)
+
+   Unless required by applicable law or agreed to in writing, this
+   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+   CONDITIONS OF ANY KIND, either express or implied.
+*/
+
+#ifndef _BLE_MESH_DEMO_INIT_H_
+#define _BLE_MESH_DEMO_INIT_H_
+
+#define TAG "ble_mesh_provisioner"
+
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid);
+
+esp_err_t bluetooth_init(void);
+
+#endif

+ 16 - 51
examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/main/ble_mesh_demo_main.c

@@ -12,10 +12,6 @@
 #include "esp_log.h"
 #include "nvs_flash.h"
 
-#include "esp_bt.h"
-#include "esp_bt_main.h"
-#include "esp_bt_device.h"
-
 #include "esp_ble_mesh_defs.h"
 #include "esp_ble_mesh_common_api.h"
 #include "esp_ble_mesh_provisioning_api.h"
@@ -23,7 +19,7 @@
 #include "esp_ble_mesh_config_model_api.h"
 #include "esp_ble_mesh_generic_model_api.h"
 
-#define TAG "ble_mesh_provisioner"
+#include "ble_mesh_demo_init.h"
 
 #define LED_OFF             0x0
 #define LED_ON              0x1
@@ -243,8 +239,8 @@ static void prov_link_close(esp_ble_mesh_prov_bearer_t bearer, uint8_t reason)
              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[ESP_BD_ADDR_LEN],
-                                esp_ble_addr_type_t addr_type, uint16_t oob_info,
+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};
@@ -255,11 +251,11 @@ static void recv_unprov_adv_pkt(uint8_t dev_uuid[16], uint8_t addr[ESP_BD_ADDR_L
      * to the application layer.
      */
 
-    ESP_LOGI(TAG, "address: %s, address type: %d, adv type: %d", bt_hex(addr, ESP_BD_ADDR_LEN), addr_type, adv_type);
+    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, ESP_BD_ADDR_LEN);
+    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;
@@ -608,14 +604,11 @@ static int ble_mesh_init(void)
     prov_key.app_idx = APP_KEY_IDX;
     memset(prov_key.app_key, APP_KEY_OCTET, sizeof(prov_key.app_key));
 
-    memcpy(dev_uuid, esp_bt_dev_get_address(), ESP_BD_ADDR_LEN);
-
     esp_ble_mesh_register_prov_callback(esp_ble_mesh_prov_cb);
     esp_ble_mesh_register_custom_model_callback(esp_ble_mesh_model_cb);
     esp_ble_mesh_register_config_client_callback(esp_ble_mesh_config_client_cb);
     esp_ble_mesh_register_generic_client_callback(esp_ble_mesh_generic_client_cb);
 
-    esp_ble_mesh_provisioner_set_dev_uuid_match(match, sizeof(match), 0x0, false);
 
     err = esp_ble_mesh_init(&provision, &composition);
     if (err) {
@@ -623,6 +616,8 @@ static int ble_mesh_init(void)
         return err;
     }
 
+    esp_ble_mesh_provisioner_set_dev_uuid_match(match, sizeof(match), 0x0, false);
+
     esp_ble_mesh_provisioner_prov_enable(ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT);
 
     esp_ble_mesh_provisioner_add_local_app_key(prov_key.app_key, prov_key.net_idx, prov_key.app_idx);
@@ -632,57 +627,27 @@ static int ble_mesh_init(void)
     return err;
 }
 
-static esp_err_t bluetooth_init(void)
-{
-    esp_err_t ret;
-
-    ret = nvs_flash_init();
-    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
-        ESP_ERROR_CHECK(nvs_flash_erase());
-        ret = nvs_flash_init();
-    }
-    ESP_ERROR_CHECK(ret);
-
-    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
-
-    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
-    ret = esp_bt_controller_init(&bt_cfg);
-    if (ret) {
-        ESP_LOGE(TAG, "%s initialize controller failed", __func__);
-        return ret;
-    }
-
-    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
-    if (ret) {
-        ESP_LOGE(TAG, "%s enable controller failed", __func__);
-        return ret;
-    }
-    ret = esp_bluedroid_init();
-    if (ret) {
-        ESP_LOGE(TAG, "%s init bluetooth failed", __func__);
-        return ret;
-    }
-    ret = esp_bluedroid_enable();
-    if (ret) {
-        ESP_LOGE(TAG, "%s enable bluetooth failed", __func__);
-        return ret;
-    }
-
-    return ret;
-}
-
 void app_main(void)
 {
     int err;
 
     ESP_LOGI(TAG, "Initializing...");
 
+    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) {

+ 1 - 1
examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/tutorial/Ble_Mesh_Provisioner_Example_Walkthrough.md

@@ -21,7 +21,7 @@ static int ble_mesh_init(void)
     prov_key.app_idx = ESP_BLE_MESH_APP_IDX;
     memset(prov_key.app_key, APP_KEY_OCTET, sizeof(prov_key.app_key));
 
-    memcpy(dev_uuid, esp_bt_dev_get_address(), ESP_BD_ADDR_LEN);
+    memcpy(dev_uuid, esp_bt_dev_get_address(), BLE_MESH_ADDR_LEN);
 
     esp_ble_mesh_register_prov_callback(esp_ble_mesh_prov_cb);
     esp_ble_mesh_register_custom_model_callback(esp_ble_mesh_model_cb);

+ 1 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/CMakeLists.txt

@@ -1,4 +1,5 @@
 set(COMPONENT_SRCS "ble_mesh_demo_main.c"
+                   "ble_mesh_demo_init.c"
                    "board.c")
 
 set(COMPONENT_ADD_INCLUDEDIRS ".")

+ 143 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_init.c

@@ -0,0 +1,143 @@
+
+/*
+ * Copyright (c) 2017 Intel Corporation
+ * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <sdkconfig.h>
+/* BLE */
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
+#include "esp_bt.h"
+#include "esp_bt_main.h"
+#include "esp_bt_device.h"
+#endif
+
+#ifdef CONFIG_BT_NIMBLE_ENABLED
+#include "esp_nimble_hci.h"
+#include "nimble/nimble_port.h"
+#include "nimble/nimble_port_freertos.h"
+#include "host/ble_hs.h"
+#include "host/util/util.h"
+#include "console/console.h"
+#endif
+
+#include "esp_ble_mesh_defs.h"
+#include "ble_mesh_demo_init.h"
+#include "esp_ble_mesh_common_api.h"
+
+#ifdef CONFIG_BT_BLUEDROID_ENABLED
+
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
+{
+    memcpy(dev_uuid + 2, esp_bt_dev_get_address(), BD_ADDR_LEN);
+}
+
+esp_err_t bluetooth_init(void)
+{
+    esp_err_t ret;
+
+
+    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
+
+    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
+    ret = esp_bt_controller_init(&bt_cfg);
+    if (ret) {
+        ESP_LOGE(TAG, "%s initialize controller failed", __func__);
+        return ret;
+    }
+
+    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
+    if (ret) {
+        ESP_LOGE(TAG, "%s enable controller failed", __func__);
+        return ret;
+    }
+    ret = esp_bluedroid_init();
+    if (ret) {
+        ESP_LOGE(TAG, "%s init bluetooth failed", __func__);
+        return ret;
+    }
+    ret = esp_bluedroid_enable();
+    if (ret) {
+        ESP_LOGE(TAG, "%s enable bluetooth failed", __func__);
+        return ret;
+    }
+
+    return ret;
+}
+
+#endif
+
+#ifdef CONFIG_BT_NIMBLE_ENABLED
+static SemaphoreHandle_t mesh_sem;
+static uint8_t own_addr_type;
+void ble_store_config_init(void);
+static uint8_t addr_val[6] = {0};
+    
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid)
+{
+    memcpy(dev_uuid + 2, addr_val, BD_ADDR_LEN);
+}
+
+static void mesh_on_reset(int reason)
+{
+    ESP_LOGI(TAG, "Resetting state; reason=%d", reason);
+}
+
+static void mesh_on_sync(void)
+{
+    int rc;
+
+    rc = ble_hs_util_ensure_addr(0);
+    assert(rc == 0);
+
+    /* Figure out address to use while advertising (no privacy for now) */
+    rc = ble_hs_id_infer_auto(0, &own_addr_type);
+    if (rc != 0) {
+        ESP_LOGI(TAG, "error determining address type; rc=%d", rc);
+        return;
+    }
+
+    rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
+
+    xSemaphoreGive(mesh_sem);
+}
+
+void mesh_host_task(void *param)
+{
+    ESP_LOGI(TAG, "BLE Host Task Started");
+    /* This function will return only when nimble_port_stop() is executed */
+    nimble_port_run();
+
+    nimble_port_freertos_deinit();
+}
+
+esp_err_t bluetooth_init(void)
+{
+    mesh_sem = xSemaphoreCreateBinary();
+    if (mesh_sem == NULL) {
+        ESP_LOGE(TAG, "Failed to create mesh semaphore");
+        return ESP_FAIL;
+    }
+
+    ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
+
+    nimble_port_init();
+    /* Initialize the NimBLE host configuration. */
+    ble_hs_cfg.reset_cb = mesh_on_reset;
+    ble_hs_cfg.sync_cb = mesh_on_sync;
+    ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
+
+    /* XXX Need to have template for store */
+    ble_store_config_init();
+
+    nimble_port_freertos_init(mesh_host_task);
+
+    xSemaphoreTake(mesh_sem, portMAX_DELAY);
+
+    return ESP_OK;
+}
+#endif

+ 18 - 0
examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_init.h

@@ -0,0 +1,18 @@
+/*
+   This example code is in the Public Domain (or CC0 licensed, at your option.)
+
+   Unless required by applicable law or agreed to in writing, this
+   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+   CONDITIONS OF ANY KIND, either express or implied.
+*/
+
+#ifndef _BLE_MESH_DEMO_INIT_H_
+#define _BLE_MESH_DEMO_INIT_H_
+
+#define TAG "BLE_MESH_WIFI_COEXIST_DEMO"
+
+void ble_mesh_get_dev_uuid(uint8_t *dev_uuid);
+
+esp_err_t bluetooth_init(void);
+
+#endif

+ 13 - 53
examples/bluetooth/esp_ble_mesh/ble_mesh_wifi_coexist/main/ble_mesh_demo_main.c

@@ -30,10 +30,6 @@
 #include "argtable3/argtable3.h"
 #include "cmd_decl.h"
 
-#include "esp_bt.h"
-#include "esp_bt_main.h"
-#include "esp_bt_device.h"
-
 #include "esp_ble_mesh_defs.h"
 #include "esp_ble_mesh_common_api.h"
 #include "esp_ble_mesh_networking_api.h"
@@ -45,8 +41,7 @@
 #include "esp_fast_prov_operation.h"
 #include "esp_fast_prov_client_model.h"
 #include "esp_fast_prov_server_model.h"
-
-#define TAG "BLE_MESH_WIFI_COEXIST_DEMO"
+#include "ble_mesh_demo_init.h"
 
 extern struct _led_state led_state[3];
 extern struct k_delayed_work send_self_prov_node_addr_timer;
@@ -348,8 +343,8 @@ static void provisioner_prov_complete(int node_idx, const uint8_t uuid[16], uint
     }
 }
 
-static void example_recv_unprov_adv_pkt(uint8_t dev_uuid[16], uint8_t addr[ESP_BD_ADDR_LEN],
-                                        esp_ble_addr_type_t addr_type, uint16_t oob_info,
+static void example_recv_unprov_adv_pkt(uint8_t dev_uuid[16], uint8_t addr[BLE_MESH_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};
@@ -370,7 +365,7 @@ static void example_recv_unprov_adv_pkt(uint8_t dev_uuid[16], uint8_t addr[ESP_B
         add_dev.oob_info = oob_info;
         add_dev.bearer = (uint8_t)bearer;
         memcpy(add_dev.uuid, dev_uuid, 16);
-        memcpy(add_dev.addr, addr, ESP_BD_ADDR_LEN);
+        memcpy(add_dev.addr, addr, BLE_MESH_ADDR_LEN);
         flag = ADD_DEV_RM_AFTER_PROV_FLAG | ADD_DEV_START_PROV_NOW_FLAG | ADD_DEV_FLUSHABLE_DEV_FLAG;
         err = esp_ble_mesh_provisioner_add_unprov_dev(&add_dev, flag);
         if (err != ESP_OK) {
@@ -745,9 +740,6 @@ static esp_err_t ble_mesh_init(void)
 {
     esp_err_t err;
 
-    /* First two bytes of device uuid is compared with match value by Provisioner */
-    memcpy(dev_uuid + 2, esp_bt_dev_get_address(), 6);
-
     esp_ble_mesh_register_prov_callback(example_ble_mesh_provisioning_cb);
     esp_ble_mesh_register_custom_model_callback(example_ble_mesh_custom_model_cb);
     esp_ble_mesh_register_config_client_callback(example_ble_mesh_config_client_cb);
@@ -786,47 +778,6 @@ static esp_err_t ble_mesh_init(void)
     return ESP_OK;
 }
 
-static esp_err_t bluetooth_init(void)
-{
-    esp_err_t ret;
-
-    ret = nvs_flash_init();
-    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
-        ESP_ERROR_CHECK(nvs_flash_erase());
-        ret = nvs_flash_init();
-    }
-    ESP_ERROR_CHECK(ret);
-
-    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
-
-    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
-    ret = esp_bt_controller_init(&bt_cfg);
-    if (ret) {
-        ESP_LOGE(TAG, "%s initialize controller failed", __func__);
-        return ret;
-    }
-
-    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
-    if (ret) {
-        ESP_LOGE(TAG, "%s enable controller failed", __func__);
-        return ret;
-    }
-
-    ret = esp_bluedroid_init();
-    if (ret) {
-        ESP_LOGE(TAG, "%s init bluetooth failed", __func__);
-        return ret;
-    }
-
-    ret = esp_bluedroid_enable();
-    if (ret) {
-        ESP_LOGE(TAG, "%s enable bluetooth failed", __func__);
-        return ret;
-    }
-
-    return ret;
-}
-
 #define WIFI_CONNECTED_BIT BIT0
 
 static void initialize_console()
@@ -960,12 +911,21 @@ void app_main(void)
         return;
     }
 
+    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) {