1 /* 2 * SPDX-FileCopyrightText: 2015-2021 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)25future_t **btc_main_get_future_p(btc_main_future_type_t type) 26 { 27 return &main_future[type]; 28 } 29 btc_enable_bluetooth(void)30static 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)37static 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)47void 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)52static 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 #endif ///BLE_INCLUDED == TRUE 64 #endif /* #if (SMP_INCLUDED) */ 65 #if BTA_DYNAMIC_MEMORY 66 deinit_semaphore = xSemaphoreCreateBinary(); 67 #endif /* #if BTA_DYNAMIC_MEMORY */ 68 } 69 70 btc_deinit_bluetooth(void)71static void btc_deinit_bluetooth(void) 72 { 73 /* Wait for the disable operation to complete */ 74 #if BTA_DYNAMIC_MEMORY 75 xSemaphoreTake(deinit_semaphore, BTA_DISABLE_DELAY / portTICK_PERIOD_MS); 76 #endif /* #if BTA_DYNAMIC_MEMORY */ 77 bta_dm_sm_deinit(); 78 #if (GATTC_INCLUDED) 79 bta_gattc_deinit(); 80 #endif /* #if (GATTC_INCLUDED) */ 81 #if (GATTS_INCLUDED) 82 bta_gatts_deinit(); 83 #endif /* #if (GATTS_INCLUDED) */ 84 bte_main_shutdown(); 85 #if (SMP_INCLUDED) 86 btc_config_clean_up(); 87 #endif 88 osi_alarm_deinit(); 89 osi_alarm_delete_mux(); 90 future_ready(*btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE), FUTURE_SUCCESS); 91 #if BTA_DYNAMIC_MEMORY 92 vSemaphoreDelete(deinit_semaphore); 93 deinit_semaphore = NULL; 94 #endif /* #if BTA_DYNAMIC_MEMORY */ 95 } 96 btc_main_call_handler(btc_msg_t * msg)97void btc_main_call_handler(btc_msg_t *msg) 98 { 99 BTC_TRACE_DEBUG("%s act %d\n", __func__, msg->act); 100 101 switch (msg->act) { 102 case BTC_MAIN_ACT_INIT: 103 btc_init_bluetooth(); 104 break; 105 case BTC_MAIN_ACT_DEINIT: 106 btc_deinit_bluetooth(); 107 break; 108 case BTC_MAIN_ACT_ENABLE: 109 btc_enable_bluetooth(); 110 break; 111 case BTC_MAIN_ACT_DISABLE: 112 btc_disable_bluetooth(); 113 break; 114 default: 115 BTC_TRACE_ERROR("%s UNKNOWN ACT %d\n", __func__, msg->act); 116 break; 117 } 118 } 119