1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include "btc/btc_task.h"
10 #include "osi/thread.h"
11 #include "esp_log.h"
12 #include "bt_common.h"
13 #include "osi/allocator.h"
14 #include "btc/btc_alarm.h"
15
16 #include "btc/btc_manage.h"
17 #include "btc_blufi_prf.h"
18 #include "blufi_int.h"
19 #ifdef CONFIG_BT_BLUEDROID_ENABLED
20 #include "common/bt_target.h"
21 #include "btc/btc_main.h"
22 #include "btc/btc_dev.h"
23 #include "btc_gatts.h"
24 #include "btc_gattc.h"
25 #include "btc_gatt_common.h"
26 #include "btc_gap_ble.h"
27 #include "btc/btc_dm.h"
28 #include "bta/bta_gatt_api.h"
29 #if CLASSIC_BT_INCLUDED
30 #include "btc/btc_profile_queue.h"
31 #if (BTC_GAP_BT_INCLUDED == TRUE)
32 #include "btc_gap_bt.h"
33 #endif /* BTC_GAP_BT_INCLUDED == TRUE */
34 #if BTC_AV_INCLUDED
35 #include "btc_av.h"
36 #include "btc_avrc.h"
37 #include "btc_av_co.h"
38 #endif /* #if BTC_AV_INCLUDED */
39 #if (BTC_SPP_INCLUDED == TRUE)
40 #include "btc_spp.h"
41 #endif /* #if (BTC_SPP_INCLUDED == TRUE) */
42 #if (BTC_L2CAP_INCLUDED == TRUE)
43 #include "btc_l2cap.h"
44 #endif /* #if (BTC_L2CAP_INCLUDED == TRUE) */
45 #if (BTC_SDP_INCLUDED == TRUE)
46 #include "btc_sdp.h"
47 #endif /* #if (BTC_SDP_INCLUDED == TRUE) */
48 #if BTC_HF_INCLUDED
49 #include "btc_hf_ag.h"
50 #endif/* #if BTC_HF_INCLUDED */
51 #if BTC_HF_CLIENT_INCLUDED
52 #include "btc_hf_client.h"
53 #endif /* #if BTC_HF_CLIENT_INCLUDED */
54 #if BTC_HD_INCLUDED == TRUE
55 #include "btc_hd.h"
56 #endif /* BTC_HD_INCLUDED */
57 #if BTC_HH_INCLUDED == TRUE
58 #include "btc_hh.h"
59 #endif /* BTC_HH_INCLUDED */
60 #endif /* #if CLASSIC_BT_INCLUDED */
61 #endif
62
63 #if (BLE_INCLUDED == TRUE)
64 #include "btc_gap_ble.h"
65 #endif
66
67 #if CONFIG_BLE_MESH
68 #include "btc_ble_mesh_ble.h"
69 #include "btc_ble_mesh_prov.h"
70 #include "btc_ble_mesh_health_model.h"
71 #include "btc_ble_mesh_config_model.h"
72 #include "btc_ble_mesh_generic_model.h"
73 #include "btc_ble_mesh_lighting_model.h"
74 #include "btc_ble_mesh_sensor_model.h"
75 #include "btc_ble_mesh_time_scene_model.h"
76 #endif /* #if CONFIG_BLE_MESH */
77
78 #define BTC_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE)
79 #define BTC_TASK_STACK_SIZE (BT_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) //by menuconfig
80 #define BTC_TASK_NAME "BTC_TASK"
81 #define BTC_TASK_PRIO (BT_TASK_MAX_PRIORITIES - 6)
82 #define BTC_TASK_WORKQUEUE_NUM (2)
83 #define BTC_TASK_WORKQUEUE0_LEN (0)
84 #define BTC_TASK_WORKQUEUE1_LEN (5)
85
86 osi_thread_t *btc_thread;
87
88 static const btc_func_t profile_tab[BTC_PID_NUM] = {
89 #ifdef CONFIG_BT_BLUEDROID_ENABLED
90 [BTC_PID_MAIN_INIT] = {btc_main_call_handler, NULL },
91 [BTC_PID_DEV] = {btc_dev_call_handler, NULL },
92 #if (GATTS_INCLUDED == TRUE)
93 [BTC_PID_GATTS] = {btc_gatts_call_handler, btc_gatts_cb_handler },
94 #endif ///GATTS_INCLUDED == TRUE
95 #if (GATTC_INCLUDED == TRUE)
96 [BTC_PID_GATTC] = {btc_gattc_call_handler, btc_gattc_cb_handler },
97 #endif ///GATTC_INCLUDED == TRUE
98 #if (GATTS_INCLUDED == TRUE || GATTC_INCLUDED == TRUE)
99 [BTC_PID_GATT_COMMON] = {btc_gatt_com_call_handler, NULL },
100 #endif //GATTC_INCLUDED == TRUE || GATTS_INCLUDED == TRUE
101 #if (BLE_INCLUDED == TRUE)
102 [BTC_PID_GAP_BLE] = {btc_gap_ble_call_handler, btc_gap_ble_cb_handler },
103 #else
104 [BTC_PID_GAP_BLE] = {NULL, NULL},
105 #endif ///BLE_INCLUDED == TRUE
106 [BTC_PID_BLE_HID] = {NULL, NULL},
107 [BTC_PID_SPPLIKE] = {NULL, NULL},
108 [BTC_PID_DM_SEC] = {NULL, btc_dm_sec_cb_handler },
109 #endif
110 #if (BLUFI_INCLUDED == TRUE)
111 [BTC_PID_BLUFI] = {btc_blufi_call_handler, btc_blufi_cb_handler },
112 #endif ///BLUFI_INCLUDED == TRUE
113 [BTC_PID_ALARM] = {btc_alarm_handler, NULL },
114 #ifdef CONFIG_BT_BLUEDROID_ENABLED
115 #if CLASSIC_BT_INCLUDED
116 #if (BTC_GAP_BT_INCLUDED == TRUE)
117 [BTC_PID_GAP_BT] = {btc_gap_bt_call_handler, btc_gap_bt_cb_handler },
118 #endif /* (BTC_GAP_BT_INCLUDED == TRUE) */
119 [BTC_PID_PRF_QUE] = {btc_profile_queue_handler, NULL },
120 #if BTC_AV_INCLUDED
121 [BTC_PID_A2DP] = {btc_a2dp_call_handler, btc_a2dp_cb_handler },
122 [BTC_PID_AVRC_CT] = {btc_avrc_ct_call_handler, NULL },
123 [BTC_PID_AVRC_TG] = {btc_avrc_tg_call_handler, NULL },
124 #endif /* #if BTC_AV_INCLUDED */
125 #if (BTC_SPP_INCLUDED == TRUE)
126 [BTC_PID_SPP] = {btc_spp_call_handler, btc_spp_cb_handler },
127 #endif /* #if (BTC_SPP_INCLUDED == TRUE) */
128 #if (BTC_L2CAP_INCLUDED == TRUE)
129 [BTC_PID_L2CAP] = {btc_l2cap_call_handler, btc_l2cap_cb_handler },
130 #endif /* #if (BTC_L2CAP_INCLUDED == TRUE) */
131 #if (BTC_SDP_INCLUDED == TRUE)
132 [BTC_PID_SDP] = {btc_sdp_call_handler, btc_sdp_cb_handler },
133 #endif /* #if (BTC_SDP_INCLUDED == TRUE) */
134 #if BTC_HF_INCLUDED
135 [BTC_PID_HF] = {btc_hf_call_handler, btc_hf_cb_handler},
136 #endif /* #if BTC_HF_INCLUDED */
137 #if BTC_HF_CLIENT_INCLUDED
138 [BTC_PID_HF_CLIENT] = {btc_hf_client_call_handler, btc_hf_client_cb_handler},
139 #endif /* #if BTC_HF_CLIENT_INCLUDED */
140 #if BTC_HD_INCLUDED
141 [BTC_PID_HD] = {btc_hd_call_handler, btc_hd_cb_handler },
142 #endif
143 #if BTC_HH_INCLUDED
144 [BTC_PID_HH] = {btc_hh_call_handler, btc_hh_cb_handler },
145 #endif
146 #endif /* #if CLASSIC_BT_INCLUDED */
147 #endif
148 #if CONFIG_BLE_MESH
149 [BTC_PID_PROV] = {btc_ble_mesh_prov_call_handler, btc_ble_mesh_prov_cb_handler },
150 [BTC_PID_MODEL] = {btc_ble_mesh_model_call_handler, btc_ble_mesh_model_cb_handler },
151 #if CONFIG_BLE_MESH_HEALTH_CLI
152 [BTC_PID_HEALTH_CLIENT] = {btc_ble_mesh_health_client_call_handler, btc_ble_mesh_health_client_cb_handler },
153 #endif /* CONFIG_BLE_MESH_HEALTH_CLI */
154 #if CONFIG_BLE_MESH_HEALTH_SRV
155 [BTC_PID_HEALTH_SERVER] = {btc_ble_mesh_health_server_call_handler, btc_ble_mesh_health_server_cb_handler },
156 #endif /* CONFIG_BLE_MESH_HEALTH_SRV */
157 #if CONFIG_BLE_MESH_CFG_CLI
158 [BTC_PID_CONFIG_CLIENT] = {btc_ble_mesh_config_client_call_handler, btc_ble_mesh_config_client_cb_handler },
159 #endif /* CONFIG_BLE_MESH_CFG_CLI */
160 [BTC_PID_CONFIG_SERVER] = {NULL, btc_ble_mesh_config_server_cb_handler },
161 #if CONFIG_BLE_MESH_GENERIC_CLIENT
162 [BTC_PID_GENERIC_CLIENT] = {btc_ble_mesh_generic_client_call_handler, btc_ble_mesh_generic_client_cb_handler },
163 #endif /* CONFIG_BLE_MESH_GENERIC_CLIENT */
164 #if CONFIG_BLE_MESH_LIGHTING_CLIENT
165 [BTC_PID_LIGHTING_CLIENT] = {btc_ble_mesh_lighting_client_call_handler, btc_ble_mesh_lighting_client_cb_handler },
166 #endif /* CONFIG_BLE_MESH_LIGHTING_CLIENT */
167 #if CONFIG_BLE_MESH_SENSOR_CLI
168 [BTC_PID_SENSOR_CLIENT] = {btc_ble_mesh_sensor_client_call_handler, btc_ble_mesh_sensor_client_cb_handler },
169 #endif /* CONFIG_BLE_MESH_SENSOR_CLI */
170 #if CONFIG_BLE_MESH_TIME_SCENE_CLIENT
171 [BTC_PID_TIME_SCENE_CLIENT] = {btc_ble_mesh_time_scene_client_call_handler, btc_ble_mesh_time_scene_client_cb_handler},
172 #endif /* CONFIG_BLE_MESH_TIME_SCENE_CLIENT */
173 #if CONFIG_BLE_MESH_GENERIC_SERVER
174 [BTC_PID_GENERIC_SERVER] = {NULL, btc_ble_mesh_generic_server_cb_handler },
175 #endif /* CONFIG_BLE_MESH_GENERIC_SERVER */
176 #if CONFIG_BLE_MESH_LIGHTING_SERVER
177 [BTC_PID_LIGHTING_SERVER] = {NULL, btc_ble_mesh_lighting_server_cb_handler },
178 #endif /* CONFIG_BLE_MESH_LIGHTING_SERVER */
179 #if CONFIG_BLE_MESH_SENSOR_SERVER
180 [BTC_PID_SENSOR_SERVER] = {NULL, btc_ble_mesh_sensor_server_cb_handler },
181 #endif /* CONFIG_BLE_MESH_SENSOR_SERVER */
182 #if CONFIG_BLE_MESH_TIME_SCENE_SERVER
183 [BTC_PID_TIME_SCENE_SERVER] = {NULL, btc_ble_mesh_time_scene_server_cb_handler},
184 #endif /* CONFIG_BLE_MESH_TIME_SCENE_SERVER */
185 #if CONFIG_BLE_MESH_BLE_COEX_SUPPORT
186 [BTC_PID_BLE_MESH_BLE_COEX] = {btc_ble_mesh_ble_call_handler, btc_ble_mesh_ble_cb_handler },
187 #endif /* CONFIG_BLE_MESH_BLE_COEX_SUPPORT */
188 #endif /* #if CONFIG_BLE_MESH */
189 };
190
191 /*****************************************************************************
192 **
193 ** Function btc_task
194 **
195 ** Description Process profile Task Thread.
196 ******************************************************************************/
btc_thread_handler(void * arg)197 static void btc_thread_handler(void *arg)
198 {
199 btc_msg_t *msg = (btc_msg_t *)arg;
200
201 BTC_TRACE_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, msg->arg);
202 switch (msg->sig) {
203 case BTC_SIG_API_CALL:
204 profile_tab[msg->pid].btc_call(msg);
205 break;
206 case BTC_SIG_API_CB:
207 profile_tab[msg->pid].btc_cb(msg);
208 break;
209 default:
210 break;
211 }
212
213 osi_free(msg);
214 }
215
btc_task_post(btc_msg_t * msg,uint32_t timeout)216 static bt_status_t btc_task_post(btc_msg_t *msg, uint32_t timeout)
217 {
218 if (osi_thread_post(btc_thread, btc_thread_handler, msg, 0, timeout) == false) {
219 return BT_STATUS_BUSY;
220 }
221
222 return BT_STATUS_SUCCESS;
223 }
224
225 /**
226 * transfer an message to another module in the different task.
227 * @param msg message
228 * @param arg paramter
229 * @param arg_len length of paramter
230 * @param copy_func deep copy function
231 * @param free_func deep free function
232 * @return BT_STATUS_SUCCESS: success
233 * others: fail
234 */
btc_transfer_context(btc_msg_t * msg,void * arg,int arg_len,btc_arg_deep_copy_t copy_func,btc_arg_deep_free_t free_func)235 bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg_deep_copy_t copy_func,
236 btc_arg_deep_free_t free_func)
237 {
238 btc_msg_t* lmsg;
239 bt_status_t ret;
240 // arg XOR arg_len
241 if ((msg == NULL) || ((arg == NULL) == !(arg_len == 0))) {
242 BTC_TRACE_WARNING("%s Invalid parameters\n", __func__);
243 return BT_STATUS_PARM_INVALID;
244 }
245
246 BTC_TRACE_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, arg);
247
248 lmsg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + arg_len);
249 if (lmsg == NULL) {
250 BTC_TRACE_WARNING("%s No memory\n", __func__);
251 return BT_STATUS_NOMEM;
252 }
253
254 memcpy(lmsg, msg, sizeof(btc_msg_t));
255 if (arg) {
256 memset(lmsg->arg, 0x00, arg_len); //important, avoid arg which have no length
257 memcpy(lmsg->arg, arg, arg_len);
258 if (copy_func) {
259 copy_func(lmsg, lmsg->arg, arg);
260 }
261 }
262
263 ret = btc_task_post(lmsg, OSI_THREAD_MAX_TIMEOUT);
264 if (ret != BT_STATUS_SUCCESS) {
265 if (copy_func && free_func) {
266 free_func(lmsg);
267 }
268 osi_free(lmsg);
269 }
270
271 return ret;
272 }
273
274 /**
275 * transfer an message to another module in tha same task.
276 * @param msg message
277 * @return BT_STATUS_SUCCESS: success
278 * others: fail
279 */
btc_inter_profile_call(btc_msg_t * msg)280 bt_status_t btc_inter_profile_call(btc_msg_t *msg)
281 {
282 if (msg == NULL) {
283 return BT_STATUS_PARM_INVALID;
284 }
285
286 switch (msg->sig) {
287 case BTC_SIG_API_CALL:
288 profile_tab[msg->pid].btc_call(msg);
289 break;
290 case BTC_SIG_API_CB:
291 profile_tab[msg->pid].btc_cb(msg);
292 break;
293 default:
294 break;
295 }
296 return BT_STATUS_SUCCESS;
297 }
298
299
300 #if BTC_DYNAMIC_MEMORY
301
btc_deinit_mem(void)302 static void btc_deinit_mem(void) {
303 if (btc_dm_cb_ptr) {
304 osi_free(btc_dm_cb_ptr);
305 btc_dm_cb_ptr = NULL;
306 }
307
308 if (btc_profile_cb_tab) {
309 osi_free(btc_profile_cb_tab);
310 btc_profile_cb_tab = NULL;
311 }
312
313 #if (BLE_INCLUDED == TRUE)
314 if (gl_bta_adv_data_ptr) {
315 osi_free(gl_bta_adv_data_ptr);
316 gl_bta_adv_data_ptr = NULL;
317 }
318
319 if (gl_bta_scan_rsp_data_ptr) {
320 osi_free(gl_bta_scan_rsp_data_ptr);
321 gl_bta_scan_rsp_data_ptr = NULL;
322 }
323 #endif ///BLE_INCLUDED == TRUE
324
325 #if GATTS_INCLUDED == TRUE && GATT_DYNAMIC_MEMORY == TRUE
326 if (btc_creat_tab_env_ptr) {
327 osi_free(btc_creat_tab_env_ptr);
328 btc_creat_tab_env_ptr = NULL;
329 }
330 #if (BLUFI_INCLUDED == TRUE)
331 if (blufi_env_ptr) {
332 osi_free(blufi_env_ptr);
333 blufi_env_ptr = NULL;
334 }
335 #endif
336 #endif
337
338 #if BTC_HF_CLIENT_INCLUDED == TRUE && HFP_DYNAMIC_MEMORY == TRUE
339 if (hf_client_local_param_ptr) {
340 osi_free(hf_client_local_param_ptr);
341 hf_client_local_param_ptr = NULL;
342 }
343 #endif
344
345 #if BTC_AV_INCLUDED == TRUE && AVRC_DYNAMIC_MEMORY == TRUE
346 if (btc_rc_cb_ptr) {
347 osi_free(btc_rc_cb_ptr);
348 btc_rc_cb_ptr = NULL;
349 }
350 if (bta_av_co_cb_ptr) {
351 osi_free(bta_av_co_cb_ptr);
352 bta_av_co_cb_ptr = NULL;
353 }
354 #endif
355 }
356
btc_init_mem(void)357 static bt_status_t btc_init_mem(void) {
358 if ((btc_dm_cb_ptr = (btc_dm_cb_t *)osi_malloc(sizeof(btc_dm_cb_t))) == NULL) {
359 goto error_exit;
360 }
361 memset((void *)btc_dm_cb_ptr, 0, sizeof(btc_dm_cb_t));
362
363 if ((btc_profile_cb_tab = (void **)osi_malloc(sizeof(void *) * BTC_PID_NUM)) == NULL) {
364 goto error_exit;
365 }
366 memset((void *)btc_profile_cb_tab, 0, sizeof(void *) * BTC_PID_NUM);
367
368 #if (BLE_INCLUDED == TRUE)
369 if ((gl_bta_adv_data_ptr = (tBTA_BLE_ADV_DATA *)osi_malloc(sizeof(tBTA_BLE_ADV_DATA))) == NULL) {
370 goto error_exit;
371 }
372 memset((void *)gl_bta_adv_data_ptr, 0, sizeof(tBTA_BLE_ADV_DATA));
373
374 if ((gl_bta_scan_rsp_data_ptr = (tBTA_BLE_ADV_DATA *)osi_malloc(sizeof(tBTA_BLE_ADV_DATA))) == NULL) {
375 goto error_exit;
376 }
377 memset((void *)gl_bta_scan_rsp_data_ptr, 0, sizeof(tBTA_BLE_ADV_DATA));
378 #endif ///BLE_INCLUDED == TRUE
379
380 #if GATTS_INCLUDED == TRUE && GATT_DYNAMIC_MEMORY == TRUE
381 if ((btc_creat_tab_env_ptr = (esp_btc_creat_tab_t *)osi_malloc(sizeof(esp_btc_creat_tab_t))) == NULL) {
382 goto error_exit;
383 }
384 memset((void *)btc_creat_tab_env_ptr, 0, sizeof(esp_btc_creat_tab_t));
385 #if (BLUFI_INCLUDED == TRUE)
386 if ((blufi_env_ptr = (tBLUFI_ENV *)osi_malloc(sizeof(tBLUFI_ENV))) == NULL) {
387 goto error_exit;
388 }
389 memset((void *)blufi_env_ptr, 0, sizeof(tBLUFI_ENV));
390 #endif
391 #endif
392
393 #if BTC_HF_CLIENT_INCLUDED == TRUE && HFP_DYNAMIC_MEMORY == TRUE
394 if ((hf_client_local_param_ptr = (hf_client_local_param_t *)osi_malloc(sizeof(hf_client_local_param_t))) == NULL) {
395 goto error_exit;
396 }
397 memset((void *)hf_client_local_param_ptr, 0, sizeof(hf_client_local_param_t));
398 #endif
399
400 #if BTC_AV_INCLUDED == TRUE && AVRC_DYNAMIC_MEMORY == TRUE
401 if ((btc_rc_cb_ptr = (btc_rc_cb_t *)osi_malloc(sizeof(btc_rc_cb_t))) == NULL) {
402 goto error_exit;
403 }
404 memset((void *)btc_rc_cb_ptr, 0, sizeof(btc_rc_cb_t));
405 if ((bta_av_co_cb_ptr = (tBTA_AV_CO_CB *)osi_malloc(sizeof(tBTA_AV_CO_CB))) == NULL) {
406 goto error_exit;
407 }
408 memset((void *)bta_av_co_cb_ptr, 0, sizeof(tBTA_AV_CO_CB));
409 #endif
410
411 return BT_STATUS_SUCCESS;
412
413 error_exit:;
414 btc_deinit_mem();
415 return BT_STATUS_NOMEM;
416 }
417 #endif ///BTC_DYNAMIC_MEMORY
418
btc_init(void)419 bt_status_t btc_init(void)
420 {
421 const size_t workqueue_len[] = {BTC_TASK_WORKQUEUE0_LEN, BTC_TASK_WORKQUEUE1_LEN};
422 btc_thread = osi_thread_create(BTC_TASK_NAME, BTC_TASK_STACK_SIZE, BTC_TASK_PRIO, BTC_TASK_PINNED_TO_CORE,
423 BTC_TASK_WORKQUEUE_NUM, workqueue_len);
424 if (btc_thread == NULL) {
425 return BT_STATUS_NOMEM;
426 }
427
428 #if BTC_DYNAMIC_MEMORY
429 if (btc_init_mem() != BT_STATUS_SUCCESS){
430 return BT_STATUS_NOMEM;
431 }
432 #endif
433
434 #if (BLE_INCLUDED == TRUE)
435 btc_gap_callback_init();
436 btc_gap_ble_init();
437 #endif ///BLE_INCLUDED == TRUE
438
439 #if SCAN_QUEUE_CONGEST_CHECK
440 btc_adv_list_init();
441 #endif
442 /* TODO: initial the profile_tab */
443 return BT_STATUS_SUCCESS;
444 }
445
btc_deinit(void)446 void btc_deinit(void)
447 {
448 #if BTC_DYNAMIC_MEMORY
449 btc_deinit_mem();
450 #endif
451
452 osi_thread_free(btc_thread);
453 btc_thread = NULL;
454 #if (BLE_INCLUDED == TRUE)
455 btc_gap_ble_deinit();
456 #endif ///BLE_INCLUDED == TRUE
457 #if SCAN_QUEUE_CONGEST_CHECK
458 btc_adv_list_deinit();
459 #endif
460 }
461
btc_check_queue_is_congest(void)462 bool btc_check_queue_is_congest(void)
463 {
464 if (osi_thread_queue_wait_size(btc_thread, 0) >= BT_QUEUE_CONGEST_SIZE) {
465 return true;
466 }
467
468 return false;
469 }
470
get_btc_work_queue_size(void)471 int get_btc_work_queue_size(void)
472 {
473 return osi_thread_queue_wait_size(btc_thread, 0);
474 }
475
btc_get_current_thread(void)476 osi_thread_t *btc_get_current_thread(void)
477 {
478 return btc_thread;
479 }
480