1 /*
2  * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "btc/btc_task.h"
8 #include "btc/btc_main.h"
9 #include "btc/btc_dm.h"
10 #include "osi/future.h"
11 #include "esp_err.h"
12 #include "btc/btc_config.h"
13 #include "osi/alarm.h"
14 #include "btc/btc_ble_storage.h"
15 #include "btc_gap_ble.h"
16 #include "bta_gattc_int.h"
17 #include "bta_gatts_int.h"
18 #include "bta_dm_int.h"
19 
20 static future_t *main_future[BTC_MAIN_FUTURE_NUM];
21 
22 extern int bte_main_boot_entry(void *cb);
23 extern int bte_main_shutdown(void);
24 
btc_main_get_future_p(btc_main_future_type_t type)25 future_t **btc_main_get_future_p(btc_main_future_type_t type)
26 {
27     return &main_future[type];
28 }
29 
btc_enable_bluetooth(void)30 static void btc_enable_bluetooth(void)
31 {
32     if (BTA_EnableBluetooth(btc_dm_sec_evt) != BTA_SUCCESS) {
33         future_ready(*btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE), FUTURE_FAIL);
34     }
35 }
36 
btc_disable_bluetooth(void)37 static void btc_disable_bluetooth(void)
38 {
39 #if (SMP_INCLUDED)
40     btc_config_shut_down();
41 #endif
42     if (BTA_DisableBluetooth() != BTA_SUCCESS) {
43         future_ready(*btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE), FUTURE_FAIL);
44     }
45 }
46 
btc_init_callback(void)47 void btc_init_callback(void)
48 {
49     future_ready(*btc_main_get_future_p(BTC_MAIN_INIT_FUTURE), FUTURE_SUCCESS);
50 }
51 
btc_init_bluetooth(void)52 static void btc_init_bluetooth(void)
53 {
54     osi_alarm_create_mux();
55     osi_alarm_init();
56     bte_main_boot_entry(btc_init_callback);
57 #if (SMP_INCLUDED)
58     btc_config_init();
59 
60 #if (BLE_INCLUDED == TRUE)
61     //load the ble local key which has been stored in the flash
62     btc_dm_load_ble_local_keys();
63     bta_dm_co_security_param_init();
64 #endif  ///BLE_INCLUDED == TRUE
65 #endif /* #if (SMP_INCLUDED) */
66 #if BTA_DYNAMIC_MEMORY
67     deinit_semaphore = xSemaphoreCreateBinary();
68 #endif /* #if BTA_DYNAMIC_MEMORY */
69 }
70 
71 
btc_deinit_bluetooth(void)72 static void btc_deinit_bluetooth(void)
73 {
74     /* Wait for the disable operation to complete */
75 #if BTA_DYNAMIC_MEMORY
76     xSemaphoreTake(deinit_semaphore, BTA_DISABLE_DELAY / portTICK_PERIOD_MS);
77 #endif /* #if BTA_DYNAMIC_MEMORY */
78     bta_dm_sm_deinit();
79 #if (GATTC_INCLUDED)
80     bta_gattc_deinit();
81 #endif /* #if (GATTC_INCLUDED) */
82 #if (GATTS_INCLUDED)
83     bta_gatts_deinit();
84 #endif /* #if (GATTS_INCLUDED) */
85     bte_main_shutdown();
86 #if (SMP_INCLUDED)
87     btc_config_clean_up();
88 #endif
89     osi_alarm_deinit();
90     osi_alarm_delete_mux();
91     future_ready(*btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE), FUTURE_SUCCESS);
92 #if BTA_DYNAMIC_MEMORY
93     vSemaphoreDelete(deinit_semaphore);
94     deinit_semaphore = NULL;
95 #endif /* #if BTA_DYNAMIC_MEMORY */
96 }
97 
btc_main_call_handler(btc_msg_t * msg)98 void btc_main_call_handler(btc_msg_t *msg)
99 {
100     BTC_TRACE_DEBUG("%s act %d\n", __func__, msg->act);
101 
102     switch (msg->act) {
103     case BTC_MAIN_ACT_INIT:
104         btc_init_bluetooth();
105         break;
106     case BTC_MAIN_ACT_DEINIT:
107         btc_deinit_bluetooth();
108         break;
109     case BTC_MAIN_ACT_ENABLE:
110         btc_enable_bluetooth();
111         break;
112     case BTC_MAIN_ACT_DISABLE:
113         btc_disable_bluetooth();
114         break;
115     default:
116         BTC_TRACE_ERROR("%s UNKNOWN ACT %d\n", __func__, msg->act);
117         break;
118     }
119 }
120 
btc_get_ble_status(void)121 uint32_t btc_get_ble_status(void)
122 {
123     uint32_t status = BTC_BLE_STATUS_IDLE;
124 
125     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
126         BTC_TRACE_ERROR("%s Bluedroid not enabled", __func__);
127         return status;
128     }
129 
130     #if (BLE_INCLUDED == TRUE)
131     // Number of active advertising
132     extern uint8_t btm_ble_adv_active_count(void);
133     if (btm_ble_adv_active_count()) {
134         status |= BIT(BTC_BLE_STATUS_ADV);
135     }
136 
137     // Number of active scanning
138     extern uint8_t btm_ble_scan_active_count(void);
139     if (btm_ble_scan_active_count()) {
140         status |= BIT(BTC_BLE_STATUS_SCAN);
141     }
142 
143     // Number of active GATT tcb
144     extern uint8_t gatt_tcb_active_count(void);
145     if (gatt_tcb_active_count()) {
146         status |= BIT(BTC_BLE_STATUS_CONN);
147     }
148 
149     // Address resolve status
150     extern uint8_t btm_get_ble_addr_resolve_disable_status(void);
151     if (btm_get_ble_addr_resolve_disable_status()) {
152         status |= BIT(BTC_BLE_STATUS_ADDR_RESOLVE_DISABLE);
153     }
154 
155     #if (SMP_INCLUDED == TRUE)
156     // Number of recorded devices
157     extern uint8_t btm_ble_sec_dev_active_count(void);
158     if (btm_ble_sec_dev_active_count()) {
159         status |= BIT(BTC_BLE_STATUS_KEYS);
160     }
161 
162     // Number of saved bonded devices
163     if (btc_storage_get_num_ble_bond_devices()) {
164         status |= BIT(BTC_BLE_STATUS_BOND);
165     }
166     #endif
167 
168     #if (BLE_PRIVACY_SPT == TRUE)
169     // Privacy enabled
170     extern uint8_t btm_ble_privacy_is_enabled(void);
171     if (btm_ble_privacy_is_enabled()) {
172         status |= BIT(BTC_BLE_STATUS_PRIVACY);
173     }
174     #endif
175     #endif
176 
177     #if (BLE_50_FEATURE_SUPPORT == TRUE)
178     // Number of active extended advertsing
179     extern uint8_t btm_ble_ext_adv_active_count(void);
180     if (btm_ble_ext_adv_active_count()) {
181         status |= BIT(BTC_BLE_STATUS_EXT_ADV);
182     }
183     #endif
184 
185     // Number of active ACL connection
186     extern uint8_t btm_acl_active_count(void);
187     if (btm_acl_active_count()) {
188         status |= BIT(BTC_BLE_STATUS_CONN);
189     }
190 
191     // Number of active L2C plcb
192     extern uint8_t l2cu_plcb_active_count(void);
193     if (l2cu_plcb_active_count()) {
194         status |= BIT(BTC_BLE_STATUS_CONN);
195     }
196 
197     #if (GATTC_INCLUDED == TRUE)
198     // Number of registered GATTC APP
199     extern uint8_t bta_gattc_cl_rcb_active_count(void);
200     if (bta_gattc_cl_rcb_active_count()) {
201         status |= BIT(BTC_BLE_STATUS_GATTC_APP);
202     }
203 
204     // Number of saved GATTC cache
205     extern UINT8 bta_gattc_co_get_addr_num(void);
206     if (bta_gattc_co_get_addr_num()) {
207         status |= BIT(BTC_BLE_STATUS_GATTC_CACHE);
208     }
209     #endif
210 
211     #if (GATTS_INCLUDED == TRUE)
212     // Number of registered GATTS service
213     extern uint8_t bta_gatts_srvc_active_count(void);
214     if (bta_gatts_srvc_active_count()) {
215         status |= BIT(BTC_BLE_STATUS_GATTS_SRVC);
216     }
217     #endif
218 
219     return status;
220 }
221