btc_task.c 12 KB

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