btc_task.c 15 KB

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