1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "btc/btc_task.h"
18 #include "osi/thread.h"
20 #include "bt_common.h"
21 #include "osi/allocator.h"
22 #include "btc/btc_alarm.h"
24 #ifdef CONFIG_BT_BLUEDROID_ENABLED
25 #include "common/bt_target.h"
26 #include "btc/btc_main.h"
27 #include "btc/btc_manage.h"
28 #include "btc/btc_dev.h"
29 #include "btc_gatts.h"
30 #include "btc_gattc.h"
31 #include "btc_gatt_common.h"
32 #include "btc_gap_ble.h"
33 #include "btc_blufi_prf.h"
34 #include "blufi_int.h"
35 #include "btc/btc_dm.h"
36 #include "bta/bta_gatt_api.h"
37 #if CLASSIC_BT_INCLUDED
38 #include "btc/btc_profile_queue.h"
39 #if (BTC_GAP_BT_INCLUDED == TRUE)
40 #include "btc_gap_bt.h"
41 #endif /* BTC_GAP_BT_INCLUDED == TRUE */
45 #include "btc_av_co.h"
46 #endif /* #if BTC_AV_INCLUDED */
47 #if (BTC_SPP_INCLUDED == TRUE)
49 #endif /* #if (BTC_SPP_INCLUDED == TRUE) */
51 #include "btc_hf_ag.h"
52 #endif/* #if BTC_HF_INCLUDED */
53 #if BTC_HF_CLIENT_INCLUDED
54 #include "btc_hf_client.h"
55 #endif /* #if BTC_HF_CLIENT_INCLUDED */
56 #endif /* #if CLASSIC_BT_INCLUDED */
60 #include "btc_ble_mesh_prov.h"
61 #include "btc_ble_mesh_health_model.h"
62 #include "btc_ble_mesh_config_model.h"
63 #include "btc_ble_mesh_generic_model.h"
64 #include "btc_ble_mesh_lighting_model.h"
65 #include "btc_ble_mesh_sensor_model.h"
66 #include "btc_ble_mesh_time_scene_model.h"
67 #endif /* #if CONFIG_BLE_MESH */
69 #define BTC_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE)
70 #define BTC_TASK_STACK_SIZE (BT_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) //by menuconfig
71 #define BTC_TASK_NAME "btcT"
72 #define BTC_TASK_PRIO (BT_TASK_MAX_PRIORITIES - 6)
74 osi_thread_t *btc_thread;
76 static const btc_func_t profile_tab[BTC_PID_NUM] = {
77 #ifdef CONFIG_BT_BLUEDROID_ENABLED
78 [BTC_PID_MAIN_INIT] = {btc_main_call_handler, NULL },
79 [BTC_PID_DEV] = {btc_dev_call_handler, NULL },
80 #if (GATTS_INCLUDED == TRUE)
81 [BTC_PID_GATTS] = {btc_gatts_call_handler, btc_gatts_cb_handler },
82 #endif ///GATTS_INCLUDED == TRUE
83 #if (GATTC_INCLUDED == TRUE)
84 [BTC_PID_GATTC] = {btc_gattc_call_handler, btc_gattc_cb_handler },
85 #endif ///GATTC_INCLUDED == TRUE
86 #if (GATTS_INCLUDED == TRUE || GATTC_INCLUDED == TRUE)
87 [BTC_PID_GATT_COMMON] = {btc_gatt_com_call_handler, NULL },
88 #endif //GATTC_INCLUDED == TRUE || GATTS_INCLUDED == TRUE
89 #if (BLE_INCLUDED == TRUE)
90 [BTC_PID_GAP_BLE] = {btc_gap_ble_call_handler, btc_gap_ble_cb_handler },
92 [BTC_PID_GAP_BLE] = {NULL, NULL},
93 #endif ///BLE_INCLUDED == TRUE
94 [BTC_PID_BLE_HID] = {NULL, NULL},
95 [BTC_PID_SPPLIKE] = {NULL, NULL},
96 #if (GATTS_INCLUDED == TRUE)
97 [BTC_PID_BLUFI] = {btc_blufi_call_handler, btc_blufi_cb_handler },
98 #endif ///GATTS_INCLUDED == TRUE
99 [BTC_PID_DM_SEC] = {NULL, btc_dm_sec_cb_handler },
101 [BTC_PID_ALARM] = {btc_alarm_handler, NULL },
102 #ifdef CONFIG_BT_BLUEDROID_ENABLED
103 #if CLASSIC_BT_INCLUDED
104 #if (BTC_GAP_BT_INCLUDED == TRUE)
105 [BTC_PID_GAP_BT] = {btc_gap_bt_call_handler, btc_gap_bt_cb_handler },
106 #endif /* (BTC_GAP_BT_INCLUDED == TRUE) */
107 [BTC_PID_PRF_QUE] = {btc_profile_queue_handler, NULL },
109 [BTC_PID_A2DP] = {btc_a2dp_call_handler, btc_a2dp_cb_handler },
110 [BTC_PID_AVRC_CT] = {btc_avrc_ct_call_handler, NULL },
111 [BTC_PID_AVRC_TG] = {btc_avrc_tg_call_handler, NULL },
112 #endif /* #if BTC_AV_INCLUDED */
113 #if (BTC_SPP_INCLUDED == TRUE)
114 [BTC_PID_SPP] = {btc_spp_call_handler, btc_spp_cb_handler },
115 #endif /* #if (BTC_SPP_INCLUDED == TRUE) */
117 [BTC_PID_HF] = {btc_hf_call_handler, btc_hf_cb_handler},
118 #endif /* #if BTC_HF_INCLUDED */
119 #if BTC_HF_CLIENT_INCLUDED
120 [BTC_PID_HF_CLIENT] = {btc_hf_client_call_handler, btc_hf_client_cb_handler},
121 #endif /* #if BTC_HF_CLIENT_INCLUDED */
122 #endif /* #if CLASSIC_BT_INCLUDED */
125 [BTC_PID_PROV] = {btc_ble_mesh_prov_call_handler, btc_ble_mesh_prov_cb_handler },
126 [BTC_PID_MODEL] = {btc_ble_mesh_model_call_handler, btc_ble_mesh_model_cb_handler },
127 [BTC_PID_HEALTH_CLIENT] = {btc_ble_mesh_health_client_call_handler, btc_ble_mesh_health_client_cb_handler },
128 [BTC_PID_HEALTH_SERVER] = {btc_ble_mesh_health_server_call_handler, btc_ble_mesh_health_server_cb_handler },
129 [BTC_PID_CONFIG_CLIENT] = {btc_ble_mesh_config_client_call_handler, btc_ble_mesh_config_client_cb_handler },
130 [BTC_PID_CONFIG_SERVER] = {NULL, btc_ble_mesh_config_server_cb_handler },
131 [BTC_PID_GENERIC_CLIENT] = {btc_ble_mesh_generic_client_call_handler, btc_ble_mesh_generic_client_cb_handler },
132 [BTC_PID_LIGHTING_CLIENT] = {btc_ble_mesh_lighting_client_call_handler, btc_ble_mesh_lighting_client_cb_handler },
133 [BTC_PID_SENSOR_CLIENT] = {btc_ble_mesh_sensor_client_call_handler, btc_ble_mesh_sensor_client_cb_handler },
134 [BTC_PID_TIME_SCENE_CLIENT] = {btc_ble_mesh_time_scene_client_call_handler, btc_ble_mesh_time_scene_client_cb_handler},
135 #endif /* #if CONFIG_BLE_MESH */
138 /*****************************************************************************
142 ** Description Process profile Task Thread.
143 ******************************************************************************/
144 static void btc_thread_handler(void *arg)
146 btc_msg_t *msg = (btc_msg_t *)arg;
148 BTC_TRACE_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, msg->arg);
150 case BTC_SIG_API_CALL:
151 profile_tab[msg->pid].btc_call(msg);
154 profile_tab[msg->pid].btc_cb(msg);
166 static bt_status_t btc_task_post(btc_msg_t *msg, uint32_t timeout)
170 lmsg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t));
172 return BT_STATUS_NOMEM;
175 memcpy(lmsg, msg, sizeof(btc_msg_t));
177 if (osi_thread_post(btc_thread, btc_thread_handler, lmsg, 0, timeout) == false) {
178 return BT_STATUS_BUSY;
181 return BT_STATUS_SUCCESS;
184 bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg_deep_copy_t copy_func)
189 return BT_STATUS_PARM_INVALID;
192 BTC_TRACE_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, arg);
194 memcpy(&lmsg, msg, sizeof(btc_msg_t));
196 lmsg.arg = (void *)osi_malloc(arg_len);
197 if (lmsg.arg == NULL) {
198 return BT_STATUS_NOMEM;
200 memset(lmsg.arg, 0x00, arg_len); //important, avoid arg which have no length
201 memcpy(lmsg.arg, arg, arg_len);
203 copy_func(&lmsg, lmsg.arg, arg);
209 return btc_task_post(&lmsg, OSI_THREAD_MAX_TIMEOUT);
213 #if BTC_DYNAMIC_MEMORY
215 static void btc_deinit_mem(void) {
217 osi_free(btc_dm_cb_ptr);
218 btc_dm_cb_ptr = NULL;
221 if (btc_profile_cb_tab) {
222 osi_free(btc_profile_cb_tab);
223 btc_profile_cb_tab = NULL;
226 #if (BLE_INCLUDED == TRUE)
227 if (gl_bta_adv_data_ptr) {
228 osi_free(gl_bta_adv_data_ptr);
229 gl_bta_adv_data_ptr = NULL;
232 if (gl_bta_scan_rsp_data_ptr) {
233 osi_free(gl_bta_scan_rsp_data_ptr);
234 gl_bta_scan_rsp_data_ptr = NULL;
236 #endif ///BLE_INCLUDED == TRUE
238 #if GATTS_INCLUDED == TRUE && GATT_DYNAMIC_MEMORY == TRUE
239 if (btc_creat_tab_env_ptr) {
240 osi_free(btc_creat_tab_env_ptr);
241 btc_creat_tab_env_ptr = NULL;
245 osi_free(blufi_env_ptr);
246 blufi_env_ptr = NULL;
250 #if BTC_HF_CLIENT_INCLUDED == TRUE && HFP_DYNAMIC_MEMORY == TRUE
251 if (hf_client_local_param_ptr) {
252 osi_free(hf_client_local_param_ptr);
253 hf_client_local_param_ptr = NULL;
257 #if BTC_AV_INCLUDED == TRUE && AVRC_DYNAMIC_MEMORY == TRUE
259 osi_free(btc_rc_cb_ptr);
260 btc_rc_cb_ptr = NULL;
262 if (bta_av_co_cb_ptr) {
263 osi_free(bta_av_co_cb_ptr);
264 bta_av_co_cb_ptr = NULL;
269 static bt_status_t btc_init_mem(void) {
270 if ((btc_dm_cb_ptr = (btc_dm_cb_t *)osi_malloc(sizeof(btc_dm_cb_t))) == NULL) {
273 memset((void *)btc_dm_cb_ptr, 0, sizeof(btc_dm_cb_t));
275 if ((btc_profile_cb_tab = (void **)osi_malloc(sizeof(void *) * BTC_PID_NUM)) == NULL) {
278 memset((void *)btc_profile_cb_tab, 0, sizeof(void *) * BTC_PID_NUM);
280 #if (BLE_INCLUDED == TRUE)
281 if ((gl_bta_adv_data_ptr = (tBTA_BLE_ADV_DATA *)osi_malloc(sizeof(tBTA_BLE_ADV_DATA))) == NULL) {
284 memset((void *)gl_bta_adv_data_ptr, 0, sizeof(tBTA_BLE_ADV_DATA));
286 if ((gl_bta_scan_rsp_data_ptr = (tBTA_BLE_ADV_DATA *)osi_malloc(sizeof(tBTA_BLE_ADV_DATA))) == NULL) {
289 memset((void *)gl_bta_scan_rsp_data_ptr, 0, sizeof(tBTA_BLE_ADV_DATA));
290 #endif ///BLE_INCLUDED == TRUE
292 #if GATTS_INCLUDED == TRUE && GATT_DYNAMIC_MEMORY == TRUE
293 if ((btc_creat_tab_env_ptr = (esp_btc_creat_tab_t *)osi_malloc(sizeof(esp_btc_creat_tab_t))) == NULL) {
296 memset((void *)btc_creat_tab_env_ptr, 0, sizeof(esp_btc_creat_tab_t));
298 if ((blufi_env_ptr = (tBLUFI_ENV *)osi_malloc(sizeof(tBLUFI_ENV))) == NULL) {
301 memset((void *)blufi_env_ptr, 0, sizeof(tBLUFI_ENV));
304 #if BTC_HF_CLIENT_INCLUDED == TRUE && HFP_DYNAMIC_MEMORY == TRUE
305 if ((hf_client_local_param_ptr = (hf_client_local_param_t *)osi_malloc(sizeof(hf_client_local_param_t))) == NULL) {
308 memset((void *)hf_client_local_param_ptr, 0, sizeof(hf_client_local_param_t));
311 #if BTC_AV_INCLUDED == TRUE && AVRC_DYNAMIC_MEMORY == TRUE
312 if ((btc_rc_cb_ptr = (btc_rc_cb_t *)osi_malloc(sizeof(btc_rc_cb_t))) == NULL) {
315 memset((void *)btc_rc_cb_ptr, 0, sizeof(btc_rc_cb_t));
316 if ((bta_av_co_cb_ptr = (tBTA_AV_CO_CB *)osi_malloc(sizeof(tBTA_AV_CO_CB))) == NULL) {
319 memset((void *)bta_av_co_cb_ptr, 0, sizeof(tBTA_AV_CO_CB));
322 return BT_STATUS_SUCCESS;
326 return BT_STATUS_NOMEM;
328 #endif ///BTC_DYNAMIC_MEMORY
332 btc_thread = osi_thread_create("BTC_TASK", BTC_TASK_STACK_SIZE, BTC_TASK_PRIO, BTC_TASK_PINNED_TO_CORE, 3);
333 if (btc_thread == NULL) {
334 return BT_STATUS_NOMEM;
337 #if BTC_DYNAMIC_MEMORY
338 if (btc_init_mem() != BT_STATUS_SUCCESS){
339 return BT_STATUS_NOMEM;
343 #if (BLE_INCLUDED == TRUE)
344 btc_gap_callback_init();
345 #endif ///BLE_INCLUDED == TRUE
347 #if SCAN_QUEUE_CONGEST_CHECK
350 /* TODO: initial the profile_tab */
351 return BT_STATUS_SUCCESS;
354 void btc_deinit(void)
356 #if BTC_DYNAMIC_MEMORY
360 osi_thread_free(btc_thread);
363 #if SCAN_QUEUE_CONGEST_CHECK
364 btc_adv_list_deinit();
368 bool btc_check_queue_is_congest(void)
370 if (osi_thread_queue_wait_size(btc_thread, 0) >= BT_QUEUE_CONGEST_SIZE) {