btu_init.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2000-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. #include <string.h>
  19. #include "common/bt_defs.h"
  20. #include "common/bt_target.h"
  21. #include "common/bt_trace.h"
  22. #include "device/controller.h"
  23. #include "osi/alarm.h"
  24. #include "osi/hash_map.h"
  25. #include "osi/hash_functions.h"
  26. #include "osi/thread.h"
  27. #include "osi/mutex.h"
  28. #include "l2c_int.h"
  29. #include "stack/dyn_mem.h"
  30. #include "stack/btu.h"
  31. #include "btm_int.h"
  32. #if SDP_INCLUDED == TRUE
  33. #include "sdpint.h"
  34. #endif
  35. #if (BLE_INCLUDED == TRUE)
  36. #include "stack/gatt_api.h"
  37. #include "gatt_int.h"
  38. #if SMP_INCLUDED == TRUE
  39. #include "smp_int.h"
  40. #endif
  41. #endif
  42. #define BTU_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE)
  43. #define BTU_TASK_STACK_SIZE (BT_BTU_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE)
  44. #define BTU_TASK_PRIO (BT_TASK_MAX_PRIORITIES - 5)
  45. #define BTU_TASK_NAME "BTU_TASK"
  46. #define BTU_TASK_WORKQUEUE_NUM (1)
  47. #define BTU_TASK_WORKQUEUE0_LEN (0)
  48. hash_map_t *btu_general_alarm_hash_map;
  49. osi_mutex_t btu_general_alarm_lock;
  50. static const size_t BTU_GENERAL_ALARM_HASH_MAP_SIZE = 34;
  51. hash_map_t *btu_oneshot_alarm_hash_map;
  52. osi_mutex_t btu_oneshot_alarm_lock;
  53. static const size_t BTU_ONESHOT_ALARM_HASH_MAP_SIZE = 34;
  54. hash_map_t *btu_l2cap_alarm_hash_map;
  55. osi_mutex_t btu_l2cap_alarm_lock;
  56. static const size_t BTU_L2CAP_ALARM_HASH_MAP_SIZE = 34;
  57. osi_thread_t *btu_thread = NULL;
  58. extern void PLATFORM_DisableHciTransport(UINT8 bDisable);
  59. extern void btu_task_thread_handler(void *arg);
  60. void btu_task_start_up(void * param);
  61. void btu_task_shut_down(void);
  62. /*****************************************************************************
  63. ** V A R I A B L E S *
  64. ******************************************************************************/
  65. // TODO(cmanton) Move this out of this file
  66. const BD_ADDR BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  67. /*****************************************************************************
  68. **
  69. ** Function btu_init_core
  70. **
  71. ** Description Initialize control block memory for each core component.
  72. **
  73. **
  74. ** Returns void
  75. **
  76. ******************************************************************************/
  77. void btu_init_core(void)
  78. {
  79. /* Initialize the mandatory core stack components */
  80. btm_init();
  81. l2c_init();
  82. #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
  83. sdp_init();
  84. #endif
  85. #if BLE_INCLUDED == TRUE
  86. #if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
  87. gatt_init();
  88. #endif
  89. #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
  90. SMP_Init();
  91. #endif
  92. btm_ble_init();
  93. #endif
  94. }
  95. /*****************************************************************************
  96. **
  97. ** Function btu_free_core
  98. **
  99. ** Description Releases control block memory for each core component.
  100. **
  101. **
  102. ** Returns void
  103. **
  104. ******************************************************************************/
  105. void btu_free_core(void)
  106. {
  107. // Free the mandatory core stack components
  108. l2c_free();
  109. #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
  110. sdp_deinit();
  111. #endif
  112. #if BLE_INCLUDED == TRUE
  113. #if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
  114. gatt_free();
  115. #endif
  116. #if SMP_INCLUDED == TRUE
  117. SMP_Free();
  118. #endif
  119. btm_ble_free();
  120. #endif
  121. btm_free();
  122. }
  123. /*****************************************************************************
  124. **
  125. ** Function BTU_StartUp
  126. **
  127. ** Description Initializes the BTU control block.
  128. **
  129. ** NOTE: Must be called before creating any tasks
  130. ** (RPC, BTU, HCIT, APPL, etc.)
  131. **
  132. ** Returns void
  133. **
  134. ******************************************************************************/
  135. void BTU_StartUp(void)
  136. {
  137. #if BTU_DYNAMIC_MEMORY
  138. btu_cb_ptr = (tBTU_CB *)osi_malloc(sizeof(tBTU_CB));
  139. #endif /* #if BTU_DYNAMIC_MEMORY */
  140. memset (&btu_cb, 0, sizeof (tBTU_CB));
  141. btu_cb.trace_level = HCI_INITIAL_TRACE_LEVEL;
  142. btu_general_alarm_hash_map = hash_map_new(BTU_GENERAL_ALARM_HASH_MAP_SIZE,
  143. hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
  144. if (btu_general_alarm_hash_map == NULL) {
  145. goto error_exit;
  146. }
  147. osi_mutex_new(&btu_general_alarm_lock);
  148. btu_oneshot_alarm_hash_map = hash_map_new(BTU_ONESHOT_ALARM_HASH_MAP_SIZE,
  149. hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
  150. if (btu_oneshot_alarm_hash_map == NULL) {
  151. goto error_exit;
  152. }
  153. osi_mutex_new(&btu_oneshot_alarm_lock);
  154. btu_l2cap_alarm_hash_map = hash_map_new(BTU_L2CAP_ALARM_HASH_MAP_SIZE,
  155. hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
  156. if (btu_l2cap_alarm_hash_map == NULL) {
  157. goto error_exit;
  158. }
  159. osi_mutex_new(&btu_l2cap_alarm_lock);
  160. const size_t workqueue_len[] = {BTU_TASK_WORKQUEUE0_LEN};
  161. btu_thread = osi_thread_create(BTU_TASK_NAME, BTU_TASK_STACK_SIZE, BTU_TASK_PRIO, BTU_TASK_PINNED_TO_CORE,
  162. BTU_TASK_WORKQUEUE_NUM, workqueue_len);
  163. if (btu_thread == NULL) {
  164. goto error_exit;
  165. }
  166. if (btu_task_post(SIG_BTU_START_UP, NULL, OSI_THREAD_MAX_TIMEOUT) == false) {
  167. goto error_exit;
  168. }
  169. return;
  170. error_exit:;
  171. LOG_ERROR("%s Unable to allocate resources for bt_workqueue", __func__);
  172. BTU_ShutDown();
  173. }
  174. /*****************************************************************************
  175. **
  176. ** Function BTU_ShutDown
  177. **
  178. ** Description Deinitializes the BTU control block.
  179. **
  180. ** Returns void
  181. **
  182. ******************************************************************************/
  183. void BTU_ShutDown(void)
  184. {
  185. #if BTU_DYNAMIC_MEMORY
  186. FREE_AND_RESET(btu_cb_ptr);
  187. #endif
  188. btu_task_shut_down();
  189. hash_map_free(btu_general_alarm_hash_map);
  190. osi_mutex_free(&btu_general_alarm_lock);
  191. hash_map_free(btu_oneshot_alarm_hash_map);
  192. osi_mutex_free(&btu_oneshot_alarm_lock);
  193. hash_map_free(btu_l2cap_alarm_hash_map);
  194. osi_mutex_free(&btu_l2cap_alarm_lock);
  195. if (btu_thread) {
  196. osi_thread_free(btu_thread);
  197. btu_thread = NULL;
  198. }
  199. btu_general_alarm_hash_map = NULL;
  200. btu_oneshot_alarm_hash_map = NULL;
  201. btu_l2cap_alarm_hash_map = NULL;
  202. }
  203. /*****************************************************************************
  204. **
  205. ** Function BTU_BleAclPktSize
  206. **
  207. ** Description export the BLE ACL packet size.
  208. **
  209. ** Returns UINT16
  210. **
  211. ******************************************************************************/
  212. UINT16 BTU_BleAclPktSize(void)
  213. {
  214. #if BLE_INCLUDED == TRUE
  215. return controller_get_interface()->get_acl_packet_size_ble();
  216. #else
  217. return 0;
  218. #endif
  219. }
  220. #if SCAN_QUEUE_CONGEST_CHECK
  221. bool BTU_check_queue_is_congest(void)
  222. {
  223. if (osi_thread_queue_wait_size(btu_thread, 0) >= BT_QUEUE_CONGEST_SIZE) {
  224. return true;
  225. }
  226. return false;
  227. }
  228. #endif
  229. int get_btu_work_queue_size(void)
  230. {
  231. return osi_thread_queue_wait_size(btu_thread, 0);
  232. }
  233. osi_thread_t *btu_get_current_thread(void)
  234. {
  235. return btu_thread;
  236. }