btc_task.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "btc/btc_task.h"
  16. #include "osi/thread.h"
  17. #include "esp_log.h"
  18. #include "bt_common.h"
  19. #include "osi/allocator.h"
  20. #include "btc/btc_alarm.h"
  21. #include "btc/btc_manage.h"
  22. #include "btc_blufi_prf.h"
  23. #include "blufi_int.h"
  24. #ifdef CONFIG_BT_BLUEDROID_ENABLED
  25. #include "common/bt_target.h"
  26. #include "btc/btc_main.h"
  27. #include "btc/btc_dev.h"
  28. #include "btc_gatts.h"
  29. #include "btc_gattc.h"
  30. #include "btc_gatt_common.h"
  31. #include "btc_gap_ble.h"
  32. #include "btc/btc_dm.h"
  33. #include "bta/bta_gatt_api.h"
  34. #if CLASSIC_BT_INCLUDED
  35. #include "btc/btc_profile_queue.h"
  36. #if (BTC_GAP_BT_INCLUDED == TRUE)
  37. #include "btc_gap_bt.h"
  38. #endif /* BTC_GAP_BT_INCLUDED == TRUE */
  39. #if BTC_AV_INCLUDED
  40. #include "btc_av.h"
  41. #include "btc_avrc.h"
  42. #include "btc_av_co.h"
  43. #endif /* #if BTC_AV_INCLUDED */
  44. #if (BTC_SPP_INCLUDED == TRUE)
  45. #include "btc_spp.h"
  46. #endif /* #if (BTC_SPP_INCLUDED == TRUE) */
  47. #if BTC_HF_INCLUDED
  48. #include "btc_hf_ag.h"
  49. #endif/* #if BTC_HF_INCLUDED */
  50. #if BTC_HF_CLIENT_INCLUDED
  51. #include "btc_hf_client.h"
  52. #endif /* #if BTC_HF_CLIENT_INCLUDED */
  53. #if BTC_HD_INCLUDED == TRUE
  54. #include "btc_hd.h"
  55. #endif /* BTC_HD_INCLUDED */
  56. #if BTC_HH_INCLUDED == TRUE
  57. #include "btc_hh.h"
  58. #endif /* BTC_HH_INCLUDED */
  59. #endif /* #if CLASSIC_BT_INCLUDED */
  60. #endif
  61. #if CONFIG_BLE_MESH
  62. #include "btc_ble_mesh_ble.h"
  63. #include "btc_ble_mesh_prov.h"
  64. #include "btc_ble_mesh_health_model.h"
  65. #include "btc_ble_mesh_config_model.h"
  66. #include "btc_ble_mesh_generic_model.h"
  67. #include "btc_ble_mesh_lighting_model.h"
  68. #include "btc_ble_mesh_sensor_model.h"
  69. #include "btc_ble_mesh_time_scene_model.h"
  70. #endif /* #if CONFIG_BLE_MESH */
  71. #define BTC_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE)
  72. #define BTC_TASK_STACK_SIZE (BT_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) //by menuconfig
  73. #define BTC_TASK_NAME "BTC_TASK"
  74. #define BTC_TASK_PRIO (BT_TASK_MAX_PRIORITIES - 6)
  75. 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 },
  91. #else
  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. [BTC_PID_DM_SEC] = {NULL, btc_dm_sec_cb_handler },
  97. #endif
  98. #if (BLUFI_INCLUDED == TRUE)
  99. [BTC_PID_BLUFI] = {btc_blufi_call_handler, btc_blufi_cb_handler },
  100. #endif ///BLUFI_INCLUDED == TRUE
  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 },
  108. #if BTC_AV_INCLUDED
  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) */
  116. #if BTC_HF_INCLUDED
  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. #if BTC_HD_INCLUDED
  123. [BTC_PID_HD] = {btc_hd_call_handler, btc_hd_cb_handler },
  124. #endif
  125. #if BTC_HH_INCLUDED
  126. [BTC_PID_HH] = {btc_hh_call_handler, btc_hh_cb_handler },
  127. #endif
  128. #endif /* #if CLASSIC_BT_INCLUDED */
  129. #endif
  130. #if CONFIG_BLE_MESH
  131. [BTC_PID_PROV] = {btc_ble_mesh_prov_call_handler, btc_ble_mesh_prov_cb_handler },
  132. [BTC_PID_MODEL] = {btc_ble_mesh_model_call_handler, btc_ble_mesh_model_cb_handler },
  133. #if CONFIG_BLE_MESH_HEALTH_CLI
  134. [BTC_PID_HEALTH_CLIENT] = {btc_ble_mesh_health_client_call_handler, btc_ble_mesh_health_client_cb_handler },
  135. #endif /* CONFIG_BLE_MESH_HEALTH_CLI */
  136. #if CONFIG_BLE_MESH_HEALTH_SRV
  137. [BTC_PID_HEALTH_SERVER] = {btc_ble_mesh_health_server_call_handler, btc_ble_mesh_health_server_cb_handler },
  138. #endif /* CONFIG_BLE_MESH_HEALTH_SRV */
  139. #if CONFIG_BLE_MESH_CFG_CLI
  140. [BTC_PID_CONFIG_CLIENT] = {btc_ble_mesh_config_client_call_handler, btc_ble_mesh_config_client_cb_handler },
  141. #endif /* CONFIG_BLE_MESH_CFG_CLI */
  142. [BTC_PID_CONFIG_SERVER] = {NULL, btc_ble_mesh_config_server_cb_handler },
  143. #if CONFIG_BLE_MESH_GENERIC_CLIENT
  144. [BTC_PID_GENERIC_CLIENT] = {btc_ble_mesh_generic_client_call_handler, btc_ble_mesh_generic_client_cb_handler },
  145. #endif /* CONFIG_BLE_MESH_GENERIC_CLIENT */
  146. #if CONFIG_BLE_MESH_LIGHTING_CLIENT
  147. [BTC_PID_LIGHTING_CLIENT] = {btc_ble_mesh_lighting_client_call_handler, btc_ble_mesh_lighting_client_cb_handler },
  148. #endif /* CONFIG_BLE_MESH_LIGHTING_CLIENT */
  149. #if CONFIG_BLE_MESH_SENSOR_CLI
  150. [BTC_PID_SENSOR_CLIENT] = {btc_ble_mesh_sensor_client_call_handler, btc_ble_mesh_sensor_client_cb_handler },
  151. #endif /* CONFIG_BLE_MESH_SENSOR_CLI */
  152. #if CONFIG_BLE_MESH_TIME_SCENE_CLIENT
  153. [BTC_PID_TIME_SCENE_CLIENT] = {btc_ble_mesh_time_scene_client_call_handler, btc_ble_mesh_time_scene_client_cb_handler},
  154. #endif /* CONFIG_BLE_MESH_TIME_SCENE_CLIENT */
  155. #if CONFIG_BLE_MESH_GENERIC_SERVER
  156. [BTC_PID_GENERIC_SERVER] = {NULL, btc_ble_mesh_generic_server_cb_handler },
  157. #endif /* CONFIG_BLE_MESH_GENERIC_SERVER */
  158. #if CONFIG_BLE_MESH_LIGHTING_SERVER
  159. [BTC_PID_LIGHTING_SERVER] = {NULL, btc_ble_mesh_lighting_server_cb_handler },
  160. #endif /* CONFIG_BLE_MESH_LIGHTING_SERVER */
  161. #if CONFIG_BLE_MESH_SENSOR_SERVER
  162. [BTC_PID_SENSOR_SERVER] = {NULL, btc_ble_mesh_sensor_server_cb_handler },
  163. #endif /* CONFIG_BLE_MESH_SENSOR_SERVER */
  164. #if CONFIG_BLE_MESH_TIME_SCENE_SERVER
  165. [BTC_PID_TIME_SCENE_SERVER] = {NULL, btc_ble_mesh_time_scene_server_cb_handler},
  166. #endif /* CONFIG_BLE_MESH_TIME_SCENE_SERVER */
  167. #if CONFIG_BLE_MESH_BLE_COEX_SUPPORT
  168. [BTC_PID_BLE_MESH_BLE_COEX] = {btc_ble_mesh_ble_call_handler, btc_ble_mesh_ble_cb_handler },
  169. #endif /* CONFIG_BLE_MESH_BLE_COEX_SUPPORT */
  170. #endif /* #if CONFIG_BLE_MESH */
  171. };
  172. /*****************************************************************************
  173. **
  174. ** Function btc_task
  175. **
  176. ** Description Process profile Task Thread.
  177. ******************************************************************************/
  178. static void btc_thread_handler(void *arg)
  179. {
  180. btc_msg_t *msg = (btc_msg_t *)arg;
  181. BTC_TRACE_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, msg->arg);
  182. switch (msg->sig) {
  183. case BTC_SIG_API_CALL:
  184. profile_tab[msg->pid].btc_call(msg);
  185. break;
  186. case BTC_SIG_API_CB:
  187. profile_tab[msg->pid].btc_cb(msg);
  188. break;
  189. default:
  190. break;
  191. }
  192. if (msg->arg) {
  193. osi_free(msg->arg);
  194. }
  195. osi_free(msg);
  196. }
  197. static bt_status_t btc_task_post(btc_msg_t *msg, uint32_t timeout)
  198. {
  199. btc_msg_t *lmsg;
  200. lmsg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t));
  201. if (lmsg == NULL) {
  202. return BT_STATUS_NOMEM;
  203. }
  204. memcpy(lmsg, msg, sizeof(btc_msg_t));
  205. if (osi_thread_post(btc_thread, btc_thread_handler, lmsg, 0, timeout) == false) {
  206. return BT_STATUS_BUSY;
  207. }
  208. return BT_STATUS_SUCCESS;
  209. }
  210. /**
  211. * transfer an message to another module in the different task.
  212. * @param msg message
  213. * @param arg paramter
  214. * @param arg_len length of paramter
  215. * @param copy_func deep copy function
  216. * @return BT_STATUS_SUCCESS: success
  217. * others: fail
  218. */
  219. bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg_deep_copy_t copy_func)
  220. {
  221. btc_msg_t lmsg;
  222. if (msg == NULL) {
  223. return BT_STATUS_PARM_INVALID;
  224. }
  225. BTC_TRACE_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, arg);
  226. memcpy(&lmsg, msg, sizeof(btc_msg_t));
  227. if (arg) {
  228. lmsg.arg = (void *)osi_malloc(arg_len);
  229. if (lmsg.arg == NULL) {
  230. return BT_STATUS_NOMEM;
  231. }
  232. memset(lmsg.arg, 0x00, arg_len); //important, avoid arg which have no length
  233. memcpy(lmsg.arg, arg, arg_len);
  234. if (copy_func) {
  235. copy_func(&lmsg, lmsg.arg, arg);
  236. }
  237. } else {
  238. lmsg.arg = NULL;
  239. }
  240. return btc_task_post(&lmsg, OSI_THREAD_MAX_TIMEOUT);
  241. }
  242. /**
  243. * transfer an message to another module in tha same task.
  244. * @param msg message
  245. * @param arg paramter
  246. * @return BT_STATUS_SUCCESS: success
  247. * others: fail
  248. */
  249. bt_status_t btc_inter_profile_call(btc_msg_t *msg, void *arg)
  250. {
  251. if (msg == NULL) {
  252. return BT_STATUS_PARM_INVALID;
  253. }
  254. msg->arg = arg;
  255. switch (msg->sig) {
  256. case BTC_SIG_API_CALL:
  257. profile_tab[msg->pid].btc_call(msg);
  258. break;
  259. case BTC_SIG_API_CB:
  260. profile_tab[msg->pid].btc_cb(msg);
  261. break;
  262. default:
  263. break;
  264. }
  265. return BT_STATUS_SUCCESS;
  266. }
  267. #if BTC_DYNAMIC_MEMORY
  268. static void btc_deinit_mem(void) {
  269. if (btc_dm_cb_ptr) {
  270. osi_free(btc_dm_cb_ptr);
  271. btc_dm_cb_ptr = NULL;
  272. }
  273. if (btc_profile_cb_tab) {
  274. osi_free(btc_profile_cb_tab);
  275. btc_profile_cb_tab = NULL;
  276. }
  277. #if (BLE_INCLUDED == TRUE)
  278. if (gl_bta_adv_data_ptr) {
  279. osi_free(gl_bta_adv_data_ptr);
  280. gl_bta_adv_data_ptr = NULL;
  281. }
  282. if (gl_bta_scan_rsp_data_ptr) {
  283. osi_free(gl_bta_scan_rsp_data_ptr);
  284. gl_bta_scan_rsp_data_ptr = NULL;
  285. }
  286. #endif ///BLE_INCLUDED == TRUE
  287. #if GATTS_INCLUDED == TRUE && GATT_DYNAMIC_MEMORY == TRUE
  288. if (btc_creat_tab_env_ptr) {
  289. osi_free(btc_creat_tab_env_ptr);
  290. btc_creat_tab_env_ptr = NULL;
  291. }
  292. #if (BLUFI_INCLUDED == TRUE)
  293. if (blufi_env_ptr) {
  294. osi_free(blufi_env_ptr);
  295. blufi_env_ptr = NULL;
  296. }
  297. #endif
  298. #endif
  299. #if BTC_HF_CLIENT_INCLUDED == TRUE && HFP_DYNAMIC_MEMORY == TRUE
  300. if (hf_client_local_param_ptr) {
  301. osi_free(hf_client_local_param_ptr);
  302. hf_client_local_param_ptr = NULL;
  303. }
  304. #endif
  305. #if BTC_AV_INCLUDED == TRUE && AVRC_DYNAMIC_MEMORY == TRUE
  306. if (btc_rc_cb_ptr) {
  307. osi_free(btc_rc_cb_ptr);
  308. btc_rc_cb_ptr = NULL;
  309. }
  310. if (bta_av_co_cb_ptr) {
  311. osi_free(bta_av_co_cb_ptr);
  312. bta_av_co_cb_ptr = NULL;
  313. }
  314. #endif
  315. }
  316. static bt_status_t btc_init_mem(void) {
  317. if ((btc_dm_cb_ptr = (btc_dm_cb_t *)osi_malloc(sizeof(btc_dm_cb_t))) == NULL) {
  318. goto error_exit;
  319. }
  320. memset((void *)btc_dm_cb_ptr, 0, sizeof(btc_dm_cb_t));
  321. if ((btc_profile_cb_tab = (void **)osi_malloc(sizeof(void *) * BTC_PID_NUM)) == NULL) {
  322. goto error_exit;
  323. }
  324. memset((void *)btc_profile_cb_tab, 0, sizeof(void *) * BTC_PID_NUM);
  325. #if (BLE_INCLUDED == TRUE)
  326. if ((gl_bta_adv_data_ptr = (tBTA_BLE_ADV_DATA *)osi_malloc(sizeof(tBTA_BLE_ADV_DATA))) == NULL) {
  327. goto error_exit;
  328. }
  329. memset((void *)gl_bta_adv_data_ptr, 0, sizeof(tBTA_BLE_ADV_DATA));
  330. if ((gl_bta_scan_rsp_data_ptr = (tBTA_BLE_ADV_DATA *)osi_malloc(sizeof(tBTA_BLE_ADV_DATA))) == NULL) {
  331. goto error_exit;
  332. }
  333. memset((void *)gl_bta_scan_rsp_data_ptr, 0, sizeof(tBTA_BLE_ADV_DATA));
  334. #endif ///BLE_INCLUDED == TRUE
  335. #if GATTS_INCLUDED == TRUE && GATT_DYNAMIC_MEMORY == TRUE
  336. if ((btc_creat_tab_env_ptr = (esp_btc_creat_tab_t *)osi_malloc(sizeof(esp_btc_creat_tab_t))) == NULL) {
  337. goto error_exit;
  338. }
  339. memset((void *)btc_creat_tab_env_ptr, 0, sizeof(esp_btc_creat_tab_t));
  340. #if (BLUFI_INCLUDED == TRUE)
  341. if ((blufi_env_ptr = (tBLUFI_ENV *)osi_malloc(sizeof(tBLUFI_ENV))) == NULL) {
  342. goto error_exit;
  343. }
  344. memset((void *)blufi_env_ptr, 0, sizeof(tBLUFI_ENV));
  345. #endif
  346. #endif
  347. #if BTC_HF_CLIENT_INCLUDED == TRUE && HFP_DYNAMIC_MEMORY == TRUE
  348. if ((hf_client_local_param_ptr = (hf_client_local_param_t *)osi_malloc(sizeof(hf_client_local_param_t))) == NULL) {
  349. goto error_exit;
  350. }
  351. memset((void *)hf_client_local_param_ptr, 0, sizeof(hf_client_local_param_t));
  352. #endif
  353. #if BTC_AV_INCLUDED == TRUE && AVRC_DYNAMIC_MEMORY == TRUE
  354. if ((btc_rc_cb_ptr = (btc_rc_cb_t *)osi_malloc(sizeof(btc_rc_cb_t))) == NULL) {
  355. goto error_exit;
  356. }
  357. memset((void *)btc_rc_cb_ptr, 0, sizeof(btc_rc_cb_t));
  358. if ((bta_av_co_cb_ptr = (tBTA_AV_CO_CB *)osi_malloc(sizeof(tBTA_AV_CO_CB))) == NULL) {
  359. goto error_exit;
  360. }
  361. memset((void *)bta_av_co_cb_ptr, 0, sizeof(tBTA_AV_CO_CB));
  362. #endif
  363. return BT_STATUS_SUCCESS;
  364. error_exit:;
  365. btc_deinit_mem();
  366. return BT_STATUS_NOMEM;
  367. }
  368. #endif ///BTC_DYNAMIC_MEMORY
  369. bt_status_t btc_init(void)
  370. {
  371. btc_thread = osi_thread_create(BTC_TASK_NAME, BTC_TASK_STACK_SIZE, BTC_TASK_PRIO, BTC_TASK_PINNED_TO_CORE, 2);
  372. if (btc_thread == NULL) {
  373. return BT_STATUS_NOMEM;
  374. }
  375. #if BTC_DYNAMIC_MEMORY
  376. if (btc_init_mem() != BT_STATUS_SUCCESS){
  377. return BT_STATUS_NOMEM;
  378. }
  379. #endif
  380. #if (BLE_INCLUDED == TRUE)
  381. btc_gap_callback_init();
  382. #endif ///BLE_INCLUDED == TRUE
  383. #if SCAN_QUEUE_CONGEST_CHECK
  384. btc_adv_list_init();
  385. #endif
  386. /* TODO: initial the profile_tab */
  387. return BT_STATUS_SUCCESS;
  388. }
  389. void btc_deinit(void)
  390. {
  391. #if BTC_DYNAMIC_MEMORY
  392. btc_deinit_mem();
  393. #endif
  394. osi_thread_free(btc_thread);
  395. btc_thread = NULL;
  396. #if SCAN_QUEUE_CONGEST_CHECK
  397. btc_adv_list_deinit();
  398. #endif
  399. }
  400. bool btc_check_queue_is_congest(void)
  401. {
  402. if (osi_thread_queue_wait_size(btc_thread, 0) >= BT_QUEUE_CONGEST_SIZE) {
  403. return true;
  404. }
  405. return false;
  406. }
  407. int get_btc_work_queue_size(void)
  408. {
  409. return osi_thread_queue_wait_size(btc_thread, 0);
  410. }