btc_task.c 20 KB

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