1 /*
2 * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdint.h>
8
9 #include "freertos/FreeRTOS.h"
10 #include "freertos/semphr.h"
11
12 #include "esp_err.h"
13
14 #include "btc_ble_mesh_prov.h"
15 #include "esp_ble_mesh_defs.h"
16
esp_ble_mesh_init(esp_ble_mesh_prov_t * prov,esp_ble_mesh_comp_t * comp)17 esp_err_t esp_ble_mesh_init(esp_ble_mesh_prov_t *prov, esp_ble_mesh_comp_t *comp)
18 {
19 btc_ble_mesh_prov_args_t arg = {0};
20 SemaphoreHandle_t semaphore = NULL;
21 btc_msg_t msg = {0};
22 esp_err_t ret = ESP_OK;
23
24 if (prov == NULL || comp == NULL) {
25 return ESP_ERR_INVALID_ARG;
26 }
27
28 ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
29
30 ret = bt_mesh_host_init();
31 if (ret != ESP_OK) {
32 return ret;
33 }
34
35 // Create a semaphore
36 if ((semaphore = xSemaphoreCreateCounting(1, 0)) == NULL) {
37 BT_ERR("Failed to create semaphore");
38 return ESP_ERR_NO_MEM;
39 }
40
41 arg.mesh_init.prov = prov;
42 arg.mesh_init.comp = comp;
43 /* Transport semaphore pointer to BTC layer, and will give the semaphore in the BTC task */
44 arg.mesh_init.semaphore = semaphore;
45
46 msg.sig = BTC_SIG_API_CALL;
47 msg.pid = BTC_PID_PROV;
48 msg.act = BTC_BLE_MESH_ACT_MESH_INIT;
49
50 if (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) != BT_STATUS_SUCCESS) {
51 vSemaphoreDelete(semaphore);
52 BT_ERR("Failed to start mesh init");
53 return ESP_FAIL;
54 }
55
56 /* Take the Semaphore, wait BLE Mesh initialization to finish. */
57 xSemaphoreTake(semaphore, portMAX_DELAY);
58 /* Don't forget to delete the semaphore at the end. */
59 vSemaphoreDelete(semaphore);
60
61 return ESP_OK;
62 }
63
64 #if CONFIG_BLE_MESH_DEINIT
esp_ble_mesh_deinit(esp_ble_mesh_deinit_param_t * param)65 esp_err_t esp_ble_mesh_deinit(esp_ble_mesh_deinit_param_t *param)
66 {
67 btc_ble_mesh_prov_args_t arg = {0};
68 SemaphoreHandle_t semaphore = NULL;
69 btc_msg_t msg = {0};
70 esp_err_t ret = ESP_OK;
71
72 if (param == NULL) {
73 return ESP_ERR_INVALID_ARG;
74 }
75
76 ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
77
78 // Create a semaphore
79 if ((semaphore = xSemaphoreCreateCounting(1, 0)) == NULL) {
80 BT_ERR("Failed to create semaphore");
81 return ESP_ERR_NO_MEM;
82 }
83
84 arg.mesh_deinit.param.erase_flash = param->erase_flash;
85 /* Transport semaphore pointer to BTC layer, and will give the semaphore in the BTC task */
86 arg.mesh_deinit.semaphore = semaphore;
87
88 msg.sig = BTC_SIG_API_CALL;
89 msg.pid = BTC_PID_PROV;
90 msg.act = BTC_BLE_MESH_ACT_DEINIT_MESH;
91
92 if (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL) != BT_STATUS_SUCCESS) {
93 vSemaphoreDelete(semaphore);
94 BT_ERR("Failed to start mesh deinit");
95 return ESP_FAIL;
96 }
97
98 /* Take the Semaphore, wait BLE Mesh de-initialization to finish. */
99 __ASSERT(xSemaphoreTake(semaphore, 3000 / portTICK_PERIOD_MS) == pdTRUE, "BLE Mesh deinit take semaphore failed");
100 /* Don't forget to delete the semaphore at the end. */
101 vSemaphoreDelete(semaphore);
102
103 ret = bt_mesh_host_deinit();
104 if (ret != ESP_OK) {
105 return ret;
106 }
107
108 return ESP_OK;
109 }
110 #endif /* CONFIG_BLE_MESH_DEINIT */
111