btc_task.c 15 KB

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