1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "btc/btc_task.h"
16 #include "btc/btc_main.h"
17 #include "btc/btc_dm.h"
18 #include "osi/future.h"
19 #include "esp_err.h"
20 #include "btc/btc_config.h"
21 #include "osi/alarm.h"
22 #include "btc/btc_ble_storage.h"
23 #include "btc_gap_ble.h"
24 #include "bta_gattc_int.h"
25 #include "bta_gatts_int.h"
26 #include "bta_dm_int.h"
27
28 static future_t *main_future[BTC_MAIN_FUTURE_NUM];
29
30 extern int bte_main_boot_entry(void *cb);
31 extern int bte_main_shutdown(void);
32
btc_main_get_future_p(btc_main_future_type_t type)33 future_t **btc_main_get_future_p(btc_main_future_type_t type)
34 {
35 return &main_future[type];
36 }
37
btc_enable_bluetooth(void)38 static void btc_enable_bluetooth(void)
39 {
40 if (BTA_EnableBluetooth(btc_dm_sec_evt) != BTA_SUCCESS) {
41 future_ready(*btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE), FUTURE_FAIL);
42 }
43 }
44
btc_disable_bluetooth(void)45 static void btc_disable_bluetooth(void)
46 {
47 #if (SMP_INCLUDED)
48 btc_config_shut_down();
49 #endif
50 if (BTA_DisableBluetooth() != BTA_SUCCESS) {
51 future_ready(*btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE), FUTURE_FAIL);
52 }
53 }
54
btc_init_callback(void)55 void btc_init_callback(void)
56 {
57 future_ready(*btc_main_get_future_p(BTC_MAIN_INIT_FUTURE), FUTURE_SUCCESS);
58 }
59
btc_init_bluetooth(void)60 static void btc_init_bluetooth(void)
61 {
62 osi_alarm_create_mux();
63 osi_alarm_init();
64 bte_main_boot_entry(btc_init_callback);
65 #if (SMP_INCLUDED)
66 btc_config_init();
67
68 #if (BLE_INCLUDED == TRUE)
69 //load the ble local key which has been stored in the flash
70 btc_dm_load_ble_local_keys();
71 #endif ///BLE_INCLUDED == TRUE
72 #endif /* #if (SMP_INCLUDED) */
73 #if BTA_DYNAMIC_MEMORY
74 deinit_semaphore = xSemaphoreCreateBinary();
75 #endif /* #if BTA_DYNAMIC_MEMORY */
76 }
77
78
btc_deinit_bluetooth(void)79 static void btc_deinit_bluetooth(void)
80 {
81 /* Wait for the disable operation to complete */
82 #if BTA_DYNAMIC_MEMORY
83 xSemaphoreTake(deinit_semaphore, BTA_DISABLE_DELAY / portTICK_PERIOD_MS);
84 #endif /* #if BTA_DYNAMIC_MEMORY */
85 #if (BLE_INCLUDED == TRUE)
86 btc_gap_ble_deinit();
87 #endif ///BLE_INCLUDED == TRUE
88 bta_dm_sm_deinit();
89 #if (GATTC_INCLUDED)
90 bta_gattc_deinit();
91 #endif /* #if (GATTC_INCLUDED) */
92 #if (GATTS_INCLUDED)
93 bta_gatts_deinit();
94 #endif /* #if (GATTS_INCLUDED) */
95 bte_main_shutdown();
96 #if (SMP_INCLUDED)
97 btc_config_clean_up();
98 #endif
99 osi_alarm_deinit();
100 osi_alarm_delete_mux();
101 future_ready(*btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE), FUTURE_SUCCESS);
102 #if BTA_DYNAMIC_MEMORY
103 vSemaphoreDelete(deinit_semaphore);
104 deinit_semaphore = NULL;
105 #endif /* #if BTA_DYNAMIC_MEMORY */
106 }
107
btc_main_call_handler(btc_msg_t * msg)108 void btc_main_call_handler(btc_msg_t *msg)
109 {
110 BTC_TRACE_DEBUG("%s act %d\n", __func__, msg->act);
111
112 switch (msg->act) {
113 case BTC_MAIN_ACT_INIT:
114 btc_init_bluetooth();
115 break;
116 case BTC_MAIN_ACT_DEINIT:
117 btc_deinit_bluetooth();
118 break;
119 case BTC_MAIN_ACT_ENABLE:
120 btc_enable_bluetooth();
121 break;
122 case BTC_MAIN_ACT_DISABLE:
123 btc_disable_bluetooth();
124 break;
125 default:
126 BTC_TRACE_ERROR("%s UNKNOWN ACT %d\n", __func__, msg->act);
127 break;
128 }
129 }
130