btc_task.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. #ifdef CONFIG_BT_BLUEDROID_ENABLED
  22. #include "common/bt_target.h"
  23. #include "btc/btc_main.h"
  24. #include "btc/btc_manage.h"
  25. #include "btc/btc_dev.h"
  26. #include "btc_gatts.h"
  27. #include "btc_gattc.h"
  28. #include "btc_gatt_common.h"
  29. #include "btc_gap_ble.h"
  30. #include "btc_blufi_prf.h"
  31. #include "blufi_int.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. #endif /* #if CLASSIC_BT_INCLUDED */
  54. #endif
  55. #if CONFIG_BLE_MESH
  56. #include "btc_ble_mesh_ble.h"
  57. #include "btc_ble_mesh_prov.h"
  58. #include "btc_ble_mesh_health_model.h"
  59. #include "btc_ble_mesh_config_model.h"
  60. #include "btc_ble_mesh_generic_model.h"
  61. #include "btc_ble_mesh_lighting_model.h"
  62. #include "btc_ble_mesh_sensor_model.h"
  63. #include "btc_ble_mesh_time_scene_model.h"
  64. #endif /* #if CONFIG_BLE_MESH */
  65. #define BTC_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE)
  66. #define BTC_TASK_STACK_SIZE (BT_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) //by menuconfig
  67. #define BTC_TASK_NAME "BTC_TASK"
  68. #define BTC_TASK_PRIO (BT_TASK_MAX_PRIORITIES - 6)
  69. osi_thread_t *btc_thread;
  70. static const btc_func_t profile_tab[BTC_PID_NUM] = {
  71. #ifdef CONFIG_BT_BLUEDROID_ENABLED
  72. [BTC_PID_MAIN_INIT] = {btc_main_call_handler, NULL },
  73. [BTC_PID_DEV] = {btc_dev_call_handler, NULL },
  74. #if (GATTS_INCLUDED == TRUE)
  75. [BTC_PID_GATTS] = {btc_gatts_call_handler, btc_gatts_cb_handler },
  76. #endif ///GATTS_INCLUDED == TRUE
  77. #if (GATTC_INCLUDED == TRUE)
  78. [BTC_PID_GATTC] = {btc_gattc_call_handler, btc_gattc_cb_handler },
  79. #endif ///GATTC_INCLUDED == TRUE
  80. #if (GATTS_INCLUDED == TRUE || GATTC_INCLUDED == TRUE)
  81. [BTC_PID_GATT_COMMON] = {btc_gatt_com_call_handler, NULL },
  82. #endif //GATTC_INCLUDED == TRUE || GATTS_INCLUDED == TRUE
  83. #if (BLE_INCLUDED == TRUE)
  84. [BTC_PID_GAP_BLE] = {btc_gap_ble_call_handler, btc_gap_ble_cb_handler },
  85. #else
  86. [BTC_PID_GAP_BLE] = {NULL, NULL},
  87. #endif ///BLE_INCLUDED == TRUE
  88. [BTC_PID_BLE_HID] = {NULL, NULL},
  89. [BTC_PID_SPPLIKE] = {NULL, NULL},
  90. #if (BLUFI_INCLUDED == TRUE)
  91. [BTC_PID_BLUFI] = {btc_blufi_call_handler, btc_blufi_cb_handler },
  92. #endif ///BLUFI_INCLUDED == TRUE
  93. [BTC_PID_DM_SEC] = {NULL, btc_dm_sec_cb_handler },
  94. #endif
  95. [BTC_PID_ALARM] = {btc_alarm_handler, NULL },
  96. #ifdef CONFIG_BT_BLUEDROID_ENABLED
  97. #if CLASSIC_BT_INCLUDED
  98. #if (BTC_GAP_BT_INCLUDED == TRUE)
  99. [BTC_PID_GAP_BT] = {btc_gap_bt_call_handler, btc_gap_bt_cb_handler },
  100. #endif /* (BTC_GAP_BT_INCLUDED == TRUE) */
  101. [BTC_PID_PRF_QUE] = {btc_profile_queue_handler, NULL },
  102. #if BTC_AV_INCLUDED
  103. [BTC_PID_A2DP] = {btc_a2dp_call_handler, btc_a2dp_cb_handler },
  104. [BTC_PID_AVRC_CT] = {btc_avrc_ct_call_handler, NULL },
  105. [BTC_PID_AVRC_TG] = {btc_avrc_tg_call_handler, NULL },
  106. #endif /* #if BTC_AV_INCLUDED */
  107. #if (BTC_SPP_INCLUDED == TRUE)
  108. [BTC_PID_SPP] = {btc_spp_call_handler, btc_spp_cb_handler },
  109. #endif /* #if (BTC_SPP_INCLUDED == TRUE) */
  110. #if BTC_HF_INCLUDED
  111. [BTC_PID_HF] = {btc_hf_call_handler, btc_hf_cb_handler},
  112. #endif /* #if BTC_HF_INCLUDED */
  113. #if BTC_HF_CLIENT_INCLUDED
  114. [BTC_PID_HF_CLIENT] = {btc_hf_client_call_handler, btc_hf_client_cb_handler},
  115. #endif /* #if BTC_HF_CLIENT_INCLUDED */
  116. #endif /* #if CLASSIC_BT_INCLUDED */
  117. #endif
  118. #if CONFIG_BLE_MESH
  119. [BTC_PID_PROV] = {btc_ble_mesh_prov_call_handler, btc_ble_mesh_prov_cb_handler },
  120. [BTC_PID_MODEL] = {btc_ble_mesh_model_call_handler, btc_ble_mesh_model_cb_handler },
  121. #if CONFIG_BLE_MESH_HEALTH_CLI
  122. [BTC_PID_HEALTH_CLIENT] = {btc_ble_mesh_health_client_call_handler, btc_ble_mesh_health_client_cb_handler },
  123. #endif /* CONFIG_BLE_MESH_HEALTH_CLI */
  124. #if CONFIG_BLE_MESH_HEALTH_SRV
  125. [BTC_PID_HEALTH_SERVER] = {btc_ble_mesh_health_server_call_handler, btc_ble_mesh_health_server_cb_handler },
  126. #endif /* CONFIG_BLE_MESH_HEALTH_SRV */
  127. #if CONFIG_BLE_MESH_CFG_CLI
  128. [BTC_PID_CONFIG_CLIENT] = {btc_ble_mesh_config_client_call_handler, btc_ble_mesh_config_client_cb_handler },
  129. #endif /* CONFIG_BLE_MESH_CFG_CLI */
  130. [BTC_PID_CONFIG_SERVER] = {NULL, btc_ble_mesh_config_server_cb_handler },
  131. #if CONFIG_BLE_MESH_GENERIC_CLIENT
  132. [BTC_PID_GENERIC_CLIENT] = {btc_ble_mesh_generic_client_call_handler, btc_ble_mesh_generic_client_cb_handler },
  133. #endif /* CONFIG_BLE_MESH_GENERIC_CLIENT */
  134. #if CONFIG_BLE_MESH_LIGHTING_CLIENT
  135. [BTC_PID_LIGHTING_CLIENT] = {btc_ble_mesh_lighting_client_call_handler, btc_ble_mesh_lighting_client_cb_handler },
  136. #endif /* CONFIG_BLE_MESH_LIGHTING_CLIENT */
  137. #if CONFIG_BLE_MESH_SENSOR_CLI
  138. [BTC_PID_SENSOR_CLIENT] = {btc_ble_mesh_sensor_client_call_handler, btc_ble_mesh_sensor_client_cb_handler },
  139. #endif /* CONFIG_BLE_MESH_SENSOR_CLI */
  140. #if CONFIG_BLE_MESH_TIME_SCENE_CLIENT
  141. [BTC_PID_TIME_SCENE_CLIENT] = {btc_ble_mesh_time_scene_client_call_handler, btc_ble_mesh_time_scene_client_cb_handler},
  142. #endif /* CONFIG_BLE_MESH_TIME_SCENE_CLIENT */
  143. #if CONFIG_BLE_MESH_GENERIC_SERVER
  144. [BTC_PID_GENERIC_SERVER] = {NULL, btc_ble_mesh_generic_server_cb_handler },
  145. #endif /* CONFIG_BLE_MESH_GENERIC_SERVER */
  146. #if CONFIG_BLE_MESH_LIGHTING_SERVER
  147. [BTC_PID_LIGHTING_SERVER] = {NULL, btc_ble_mesh_lighting_server_cb_handler },
  148. #endif /* CONFIG_BLE_MESH_LIGHTING_SERVER */
  149. #if CONFIG_BLE_MESH_SENSOR_SERVER
  150. [BTC_PID_SENSOR_SERVER] = {NULL, btc_ble_mesh_sensor_server_cb_handler },
  151. #endif /* CONFIG_BLE_MESH_SENSOR_SERVER */
  152. #if CONFIG_BLE_MESH_TIME_SCENE_SERVER
  153. [BTC_PID_TIME_SCENE_SERVER] = {NULL, btc_ble_mesh_time_scene_server_cb_handler},
  154. #endif /* CONFIG_BLE_MESH_TIME_SCENE_SERVER */
  155. #if CONFIG_BLE_MESH_BLE_COEX_SUPPORT
  156. [BTC_PID_BLE_MESH_BLE_COEX] = {btc_ble_mesh_ble_call_handler, btc_ble_mesh_ble_cb_handler },
  157. #endif /* CONFIG_BLE_MESH_BLE_COEX_SUPPORT */
  158. #endif /* #if CONFIG_BLE_MESH */
  159. };
  160. /*****************************************************************************
  161. **
  162. ** Function btc_task
  163. **
  164. ** Description Process profile Task Thread.
  165. ******************************************************************************/
  166. static void btc_thread_handler(void *arg)
  167. {
  168. btc_msg_t *msg = (btc_msg_t *)arg;
  169. BTC_TRACE_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, msg->arg);
  170. switch (msg->sig) {
  171. case BTC_SIG_API_CALL:
  172. profile_tab[msg->pid].btc_call(msg);
  173. break;
  174. case BTC_SIG_API_CB:
  175. profile_tab[msg->pid].btc_cb(msg);
  176. break;
  177. default:
  178. break;
  179. }
  180. if (msg->arg) {
  181. osi_free(msg->arg);
  182. }
  183. osi_free(msg);
  184. }
  185. static bt_status_t btc_task_post(btc_msg_t *msg, uint32_t timeout)
  186. {
  187. btc_msg_t *lmsg;
  188. lmsg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t));
  189. if (lmsg == NULL) {
  190. return BT_STATUS_NOMEM;
  191. }
  192. memcpy(lmsg, msg, sizeof(btc_msg_t));
  193. if (osi_thread_post(btc_thread, btc_thread_handler, lmsg, 0, timeout) == false) {
  194. return BT_STATUS_BUSY;
  195. }
  196. return BT_STATUS_SUCCESS;
  197. }
  198. /**
  199. * transfer an message to another module in the different task.
  200. * @param msg message
  201. * @param arg paramter
  202. * @param arg_len length of paramter
  203. * @param copy_func deep copy function
  204. * @return BT_STATUS_SUCCESS: success
  205. * others: fail
  206. */
  207. bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg_deep_copy_t copy_func)
  208. {
  209. btc_msg_t lmsg;
  210. if (msg == NULL) {
  211. return BT_STATUS_PARM_INVALID;
  212. }
  213. BTC_TRACE_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, arg);
  214. memcpy(&lmsg, msg, sizeof(btc_msg_t));
  215. if (arg) {
  216. lmsg.arg = (void *)osi_malloc(arg_len);
  217. if (lmsg.arg == NULL) {
  218. return BT_STATUS_NOMEM;
  219. }
  220. memset(lmsg.arg, 0x00, arg_len); //important, avoid arg which have no length
  221. memcpy(lmsg.arg, arg, arg_len);
  222. if (copy_func) {
  223. copy_func(&lmsg, lmsg.arg, arg);
  224. }
  225. } else {
  226. lmsg.arg = NULL;
  227. }
  228. return btc_task_post(&lmsg, OSI_THREAD_MAX_TIMEOUT);
  229. }
  230. /**
  231. * transfer an message to another module in tha same task.
  232. * @param msg message
  233. * @param arg paramter
  234. * @return BT_STATUS_SUCCESS: success
  235. * others: fail
  236. */
  237. bt_status_t btc_inter_profile_call(btc_msg_t *msg, void *arg)
  238. {
  239. if (msg == NULL) {
  240. return BT_STATUS_PARM_INVALID;
  241. }
  242. msg->arg = arg;
  243. switch (msg->sig) {
  244. case BTC_SIG_API_CALL:
  245. profile_tab[msg->pid].btc_call(msg);
  246. break;
  247. case BTC_SIG_API_CB:
  248. profile_tab[msg->pid].btc_cb(msg);
  249. break;
  250. default:
  251. break;
  252. }
  253. return BT_STATUS_SUCCESS;
  254. }
  255. #if BTC_DYNAMIC_MEMORY
  256. static void btc_deinit_mem(void) {
  257. if (btc_dm_cb_ptr) {
  258. osi_free(btc_dm_cb_ptr);
  259. btc_dm_cb_ptr = NULL;
  260. }
  261. if (btc_profile_cb_tab) {
  262. osi_free(btc_profile_cb_tab);
  263. btc_profile_cb_tab = NULL;
  264. }
  265. #if (BLE_INCLUDED == TRUE)
  266. if (gl_bta_adv_data_ptr) {
  267. osi_free(gl_bta_adv_data_ptr);
  268. gl_bta_adv_data_ptr = NULL;
  269. }
  270. if (gl_bta_scan_rsp_data_ptr) {
  271. osi_free(gl_bta_scan_rsp_data_ptr);
  272. gl_bta_scan_rsp_data_ptr = NULL;
  273. }
  274. #endif ///BLE_INCLUDED == TRUE
  275. #if GATTS_INCLUDED == TRUE && GATT_DYNAMIC_MEMORY == TRUE
  276. if (btc_creat_tab_env_ptr) {
  277. osi_free(btc_creat_tab_env_ptr);
  278. btc_creat_tab_env_ptr = NULL;
  279. }
  280. #if (BLUFI_INCLUDED == TRUE)
  281. if (blufi_env_ptr) {
  282. osi_free(blufi_env_ptr);
  283. blufi_env_ptr = NULL;
  284. }
  285. #endif
  286. #endif
  287. #if BTC_HF_CLIENT_INCLUDED == TRUE && HFP_DYNAMIC_MEMORY == TRUE
  288. if (hf_client_local_param_ptr) {
  289. osi_free(hf_client_local_param_ptr);
  290. hf_client_local_param_ptr = NULL;
  291. }
  292. #endif
  293. #if BTC_AV_INCLUDED == TRUE && AVRC_DYNAMIC_MEMORY == TRUE
  294. if (btc_rc_cb_ptr) {
  295. osi_free(btc_rc_cb_ptr);
  296. btc_rc_cb_ptr = NULL;
  297. }
  298. if (bta_av_co_cb_ptr) {
  299. osi_free(bta_av_co_cb_ptr);
  300. bta_av_co_cb_ptr = NULL;
  301. }
  302. #endif
  303. }
  304. static bt_status_t btc_init_mem(void) {
  305. if ((btc_dm_cb_ptr = (btc_dm_cb_t *)osi_malloc(sizeof(btc_dm_cb_t))) == NULL) {
  306. goto error_exit;
  307. }
  308. memset((void *)btc_dm_cb_ptr, 0, sizeof(btc_dm_cb_t));
  309. if ((btc_profile_cb_tab = (void **)osi_malloc(sizeof(void *) * BTC_PID_NUM)) == NULL) {
  310. goto error_exit;
  311. }
  312. memset((void *)btc_profile_cb_tab, 0, sizeof(void *) * BTC_PID_NUM);
  313. #if (BLE_INCLUDED == TRUE)
  314. if ((gl_bta_adv_data_ptr = (tBTA_BLE_ADV_DATA *)osi_malloc(sizeof(tBTA_BLE_ADV_DATA))) == NULL) {
  315. goto error_exit;
  316. }
  317. memset((void *)gl_bta_adv_data_ptr, 0, sizeof(tBTA_BLE_ADV_DATA));
  318. if ((gl_bta_scan_rsp_data_ptr = (tBTA_BLE_ADV_DATA *)osi_malloc(sizeof(tBTA_BLE_ADV_DATA))) == NULL) {
  319. goto error_exit;
  320. }
  321. memset((void *)gl_bta_scan_rsp_data_ptr, 0, sizeof(tBTA_BLE_ADV_DATA));
  322. #endif ///BLE_INCLUDED == TRUE
  323. #if GATTS_INCLUDED == TRUE && GATT_DYNAMIC_MEMORY == TRUE
  324. if ((btc_creat_tab_env_ptr = (esp_btc_creat_tab_t *)osi_malloc(sizeof(esp_btc_creat_tab_t))) == NULL) {
  325. goto error_exit;
  326. }
  327. memset((void *)btc_creat_tab_env_ptr, 0, sizeof(esp_btc_creat_tab_t));
  328. #if (BLUFI_INCLUDED == TRUE)
  329. if ((blufi_env_ptr = (tBLUFI_ENV *)osi_malloc(sizeof(tBLUFI_ENV))) == NULL) {
  330. goto error_exit;
  331. }
  332. memset((void *)blufi_env_ptr, 0, sizeof(tBLUFI_ENV));
  333. #endif
  334. #endif
  335. #if BTC_HF_CLIENT_INCLUDED == TRUE && HFP_DYNAMIC_MEMORY == TRUE
  336. if ((hf_client_local_param_ptr = (hf_client_local_param_t *)osi_malloc(sizeof(hf_client_local_param_t))) == NULL) {
  337. goto error_exit;
  338. }
  339. memset((void *)hf_client_local_param_ptr, 0, sizeof(hf_client_local_param_t));
  340. #endif
  341. #if BTC_AV_INCLUDED == TRUE && AVRC_DYNAMIC_MEMORY == TRUE
  342. if ((btc_rc_cb_ptr = (btc_rc_cb_t *)osi_malloc(sizeof(btc_rc_cb_t))) == NULL) {
  343. goto error_exit;
  344. }
  345. memset((void *)btc_rc_cb_ptr, 0, sizeof(btc_rc_cb_t));
  346. if ((bta_av_co_cb_ptr = (tBTA_AV_CO_CB *)osi_malloc(sizeof(tBTA_AV_CO_CB))) == NULL) {
  347. goto error_exit;
  348. }
  349. memset((void *)bta_av_co_cb_ptr, 0, sizeof(tBTA_AV_CO_CB));
  350. #endif
  351. return BT_STATUS_SUCCESS;
  352. error_exit:;
  353. btc_deinit_mem();
  354. return BT_STATUS_NOMEM;
  355. }
  356. #endif ///BTC_DYNAMIC_MEMORY
  357. bt_status_t btc_init(void)
  358. {
  359. btc_thread = osi_thread_create(BTC_TASK_NAME, BTC_TASK_STACK_SIZE, BTC_TASK_PRIO, BTC_TASK_PINNED_TO_CORE, 2);
  360. if (btc_thread == NULL) {
  361. return BT_STATUS_NOMEM;
  362. }
  363. #if BTC_DYNAMIC_MEMORY
  364. if (btc_init_mem() != BT_STATUS_SUCCESS){
  365. return BT_STATUS_NOMEM;
  366. }
  367. #endif
  368. #if (BLE_INCLUDED == TRUE)
  369. btc_gap_callback_init();
  370. #endif ///BLE_INCLUDED == TRUE
  371. #if SCAN_QUEUE_CONGEST_CHECK
  372. btc_adv_list_init();
  373. #endif
  374. /* TODO: initial the profile_tab */
  375. return BT_STATUS_SUCCESS;
  376. }
  377. void btc_deinit(void)
  378. {
  379. #if BTC_DYNAMIC_MEMORY
  380. btc_deinit_mem();
  381. #endif
  382. osi_thread_free(btc_thread);
  383. btc_thread = NULL;
  384. #if SCAN_QUEUE_CONGEST_CHECK
  385. btc_adv_list_deinit();
  386. #endif
  387. }
  388. bool btc_check_queue_is_congest(void)
  389. {
  390. if (osi_thread_queue_wait_size(btc_thread, 0) >= BT_QUEUE_CONGEST_SIZE) {
  391. return true;
  392. }
  393. return false;
  394. }
  395. int get_btc_work_queue_size(void)
  396. {
  397. return osi_thread_queue_wait_size(btc_thread, 0);
  398. }