btu_init.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. hash_map_t *btu_general_alarm_hash_map;
  47. osi_mutex_t btu_general_alarm_lock;
  48. static const size_t BTU_GENERAL_ALARM_HASH_MAP_SIZE = 34;
  49. hash_map_t *btu_oneshot_alarm_hash_map;
  50. osi_mutex_t btu_oneshot_alarm_lock;
  51. static const size_t BTU_ONESHOT_ALARM_HASH_MAP_SIZE = 34;
  52. hash_map_t *btu_l2cap_alarm_hash_map;
  53. osi_mutex_t btu_l2cap_alarm_lock;
  54. static const size_t BTU_L2CAP_ALARM_HASH_MAP_SIZE = 34;
  55. osi_thread_t *btu_thread = NULL;
  56. extern void PLATFORM_DisableHciTransport(UINT8 bDisable);
  57. extern void btu_task_thread_handler(void *arg);
  58. void btu_task_start_up(void * param);
  59. void btu_task_shut_down(void);
  60. /*****************************************************************************
  61. ** V A R I A B L E S *
  62. ******************************************************************************/
  63. // TODO(cmanton) Move this out of this file
  64. const BD_ADDR BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  65. /*****************************************************************************
  66. **
  67. ** Function btu_init_core
  68. **
  69. ** Description Initialize control block memory for each core component.
  70. **
  71. **
  72. ** Returns void
  73. **
  74. ******************************************************************************/
  75. void btu_init_core(void)
  76. {
  77. /* Initialize the mandatory core stack components */
  78. btm_init();
  79. l2c_init();
  80. #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
  81. sdp_init();
  82. #endif
  83. #if BLE_INCLUDED == TRUE
  84. #if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
  85. gatt_init();
  86. #endif
  87. #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
  88. SMP_Init();
  89. #endif
  90. btm_ble_init();
  91. #endif
  92. }
  93. /*****************************************************************************
  94. **
  95. ** Function btu_free_core
  96. **
  97. ** Description Releases control block memory for each core component.
  98. **
  99. **
  100. ** Returns void
  101. **
  102. ******************************************************************************/
  103. void btu_free_core(void)
  104. {
  105. // Free the mandatory core stack components
  106. l2c_free();
  107. #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
  108. sdp_deinit();
  109. #endif
  110. #if BLE_INCLUDED == TRUE
  111. #if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
  112. gatt_free();
  113. #endif
  114. #if SMP_INCLUDED == TRUE
  115. SMP_Free();
  116. #endif
  117. btm_ble_free();
  118. #endif
  119. btm_free();
  120. }
  121. /*****************************************************************************
  122. **
  123. ** Function BTU_StartUp
  124. **
  125. ** Description Initializes the BTU control block.
  126. **
  127. ** NOTE: Must be called before creating any tasks
  128. ** (RPC, BTU, HCIT, APPL, etc.)
  129. **
  130. ** Returns void
  131. **
  132. ******************************************************************************/
  133. void BTU_StartUp(void)
  134. {
  135. #if BTU_DYNAMIC_MEMORY
  136. btu_cb_ptr = (tBTU_CB *)osi_malloc(sizeof(tBTU_CB));
  137. #endif /* #if BTU_DYNAMIC_MEMORY */
  138. memset (&btu_cb, 0, sizeof (tBTU_CB));
  139. btu_cb.trace_level = HCI_INITIAL_TRACE_LEVEL;
  140. btu_general_alarm_hash_map = hash_map_new(BTU_GENERAL_ALARM_HASH_MAP_SIZE,
  141. hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
  142. if (btu_general_alarm_hash_map == NULL) {
  143. goto error_exit;
  144. }
  145. osi_mutex_new(&btu_general_alarm_lock);
  146. btu_oneshot_alarm_hash_map = hash_map_new(BTU_ONESHOT_ALARM_HASH_MAP_SIZE,
  147. hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
  148. if (btu_oneshot_alarm_hash_map == NULL) {
  149. goto error_exit;
  150. }
  151. osi_mutex_new(&btu_oneshot_alarm_lock);
  152. btu_l2cap_alarm_hash_map = hash_map_new(BTU_L2CAP_ALARM_HASH_MAP_SIZE,
  153. hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
  154. if (btu_l2cap_alarm_hash_map == NULL) {
  155. goto error_exit;
  156. }
  157. osi_mutex_new(&btu_l2cap_alarm_lock);
  158. btu_thread = osi_thread_create(BTU_TASK_NAME, BTU_TASK_STACK_SIZE, BTU_TASK_PRIO, BTU_TASK_PINNED_TO_CORE, 1);
  159. if (btu_thread == NULL) {
  160. goto error_exit;
  161. }
  162. if (btu_task_post(SIG_BTU_START_UP, NULL, OSI_THREAD_MAX_TIMEOUT) == false) {
  163. goto error_exit;
  164. }
  165. return;
  166. error_exit:;
  167. LOG_ERROR("%s Unable to allocate resources for bt_workqueue", __func__);
  168. BTU_ShutDown();
  169. }
  170. /*****************************************************************************
  171. **
  172. ** Function BTU_ShutDown
  173. **
  174. ** Description Deinitializes the BTU control block.
  175. **
  176. ** Returns void
  177. **
  178. ******************************************************************************/
  179. void BTU_ShutDown(void)
  180. {
  181. #if BTU_DYNAMIC_MEMORY
  182. FREE_AND_RESET(btu_cb_ptr);
  183. #endif
  184. btu_task_shut_down();
  185. hash_map_free(btu_general_alarm_hash_map);
  186. osi_mutex_free(&btu_general_alarm_lock);
  187. hash_map_free(btu_oneshot_alarm_hash_map);
  188. osi_mutex_free(&btu_oneshot_alarm_lock);
  189. hash_map_free(btu_l2cap_alarm_hash_map);
  190. osi_mutex_free(&btu_l2cap_alarm_lock);
  191. if (btu_thread) {
  192. osi_thread_free(btu_thread);
  193. btu_thread = NULL;
  194. }
  195. btu_general_alarm_hash_map = NULL;
  196. btu_oneshot_alarm_hash_map = NULL;
  197. btu_l2cap_alarm_hash_map = NULL;
  198. }
  199. /*****************************************************************************
  200. **
  201. ** Function BTU_BleAclPktSize
  202. **
  203. ** Description export the BLE ACL packet size.
  204. **
  205. ** Returns UINT16
  206. **
  207. ******************************************************************************/
  208. UINT16 BTU_BleAclPktSize(void)
  209. {
  210. #if BLE_INCLUDED == TRUE
  211. return controller_get_interface()->get_acl_packet_size_ble();
  212. #else
  213. return 0;
  214. #endif
  215. }
  216. #if SCAN_QUEUE_CONGEST_CHECK
  217. bool BTU_check_queue_is_congest(void)
  218. {
  219. if (osi_thread_queue_wait_size(btu_thread, 0) >= BT_QUEUE_CONGEST_SIZE) {
  220. return true;
  221. }
  222. return false;
  223. }
  224. #endif
  225. int get_btu_work_queue_size(void)
  226. {
  227. return osi_thread_queue_wait_size(btu_thread, 0);
  228. }