btu_init.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "bt_defs.h"
  20. #include "bt_target.h"
  21. #include "bt_trace.h"
  22. #include "controller.h"
  23. #include "alarm.h"
  24. #include "fixed_queue.h"
  25. #include "hash_map.h"
  26. #include "hash_functions.h"
  27. #include "thread.h"
  28. #include "l2c_int.h"
  29. #include "dyn_mem.h"
  30. #include "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 "gatt_api.h"
  37. #include "gatt_int.h"
  38. #if SMP_INCLUDED == TRUE
  39. #include "smp_int.h"
  40. #endif
  41. #endif
  42. // extern fixed_queue_t *btif_msg_queue;
  43. // Communication queue from bta thread to bt_workqueue.
  44. fixed_queue_t *btu_bta_msg_queue;
  45. // Communication queue from hci thread to bt_workqueue.
  46. extern fixed_queue_t *btu_hci_msg_queue;
  47. // General timer queue.
  48. fixed_queue_t *btu_general_alarm_queue;
  49. hash_map_t *btu_general_alarm_hash_map;
  50. pthread_mutex_t btu_general_alarm_lock;
  51. static const size_t BTU_GENERAL_ALARM_HASH_MAP_SIZE = 34;
  52. // Oneshot timer queue.
  53. fixed_queue_t *btu_oneshot_alarm_queue;
  54. hash_map_t *btu_oneshot_alarm_hash_map;
  55. pthread_mutex_t btu_oneshot_alarm_lock;
  56. static const size_t BTU_ONESHOT_ALARM_HASH_MAP_SIZE = 34;
  57. // l2cap timer queue.
  58. fixed_queue_t *btu_l2cap_alarm_queue;
  59. hash_map_t *btu_l2cap_alarm_hash_map;
  60. pthread_mutex_t btu_l2cap_alarm_lock;
  61. static const size_t BTU_L2CAP_ALARM_HASH_MAP_SIZE = 34;
  62. //thread_t *bt_workqueue_thread;
  63. //static const char *BT_WORKQUEUE_NAME = "bt_workqueue";
  64. xTaskHandle xBtuTaskHandle = NULL;
  65. xQueueHandle xBtuQueue = 0;
  66. extern void PLATFORM_DisableHciTransport(UINT8 bDisable);
  67. extern void btu_task_thread_handler(void *arg);
  68. void btu_task_start_up(void);
  69. void btu_task_shut_down(void);
  70. /*****************************************************************************
  71. ** V A R I A B L E S *
  72. ******************************************************************************/
  73. // TODO(cmanton) Move this out of this file
  74. const BD_ADDR BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  75. /*****************************************************************************
  76. **
  77. ** Function btu_init_core
  78. **
  79. ** Description Initialize control block memory for each core component.
  80. **
  81. **
  82. ** Returns void
  83. **
  84. ******************************************************************************/
  85. void btu_init_core(void)
  86. {
  87. /* Initialize the mandatory core stack components */
  88. btm_init();
  89. l2c_init();
  90. #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
  91. sdp_init();
  92. #endif
  93. #if BLE_INCLUDED == TRUE
  94. #if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
  95. gatt_init();
  96. #endif
  97. #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
  98. SMP_Init();
  99. #endif
  100. btm_ble_init();
  101. #endif
  102. }
  103. /*****************************************************************************
  104. **
  105. ** Function btu_free_core
  106. **
  107. ** Description Releases control block memory for each core component.
  108. **
  109. **
  110. ** Returns void
  111. **
  112. ******************************************************************************/
  113. void btu_free_core(void)
  114. {
  115. // Free the mandatory core stack components
  116. l2c_free();
  117. #if BLE_INCLUDED == TRUE
  118. #if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
  119. gatt_free();
  120. #endif
  121. #endif
  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. memset (&btu_cb, 0, sizeof (tBTU_CB));
  138. btu_cb.trace_level = HCI_INITIAL_TRACE_LEVEL;
  139. btu_bta_msg_queue = fixed_queue_new(SIZE_MAX);
  140. if (btu_bta_msg_queue == NULL) {
  141. goto error_exit;
  142. }
  143. btu_general_alarm_hash_map = hash_map_new(BTU_GENERAL_ALARM_HASH_MAP_SIZE,
  144. hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
  145. if (btu_general_alarm_hash_map == NULL) {
  146. goto error_exit;
  147. }
  148. pthread_mutex_init(&btu_general_alarm_lock, NULL);
  149. btu_general_alarm_queue = fixed_queue_new(SIZE_MAX);
  150. if (btu_general_alarm_queue == NULL) {
  151. goto error_exit;
  152. }
  153. btu_oneshot_alarm_hash_map = hash_map_new(BTU_ONESHOT_ALARM_HASH_MAP_SIZE,
  154. hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
  155. if (btu_oneshot_alarm_hash_map == NULL) {
  156. goto error_exit;
  157. }
  158. pthread_mutex_init(&btu_oneshot_alarm_lock, NULL);
  159. btu_oneshot_alarm_queue = fixed_queue_new(SIZE_MAX);
  160. if (btu_oneshot_alarm_queue == NULL) {
  161. goto error_exit;
  162. }
  163. btu_l2cap_alarm_hash_map = hash_map_new(BTU_L2CAP_ALARM_HASH_MAP_SIZE,
  164. hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
  165. if (btu_l2cap_alarm_hash_map == NULL) {
  166. goto error_exit;
  167. }
  168. pthread_mutex_init(&btu_l2cap_alarm_lock, NULL);
  169. btu_l2cap_alarm_queue = fixed_queue_new(SIZE_MAX);
  170. if (btu_l2cap_alarm_queue == NULL) {
  171. goto error_exit;
  172. }
  173. xBtuQueue = xQueueCreate(BTU_QUEUE_NUM, sizeof(BtTaskEvt_t));
  174. xTaskCreatePinnedToCore(btu_task_thread_handler, BTU_TASK_NAME, BTU_TASK_STACK_SIZE, NULL, BTU_TASK_PRIO, &xBtuTaskHandle, 0);
  175. btu_task_post(SIG_BTU_START_UP);
  176. /*
  177. // Continue startup on bt workqueue thread.
  178. thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
  179. */
  180. return;
  181. error_exit:;
  182. LOG_ERROR("%s Unable to allocate resources for bt_workqueue", __func__);
  183. BTU_ShutDown();
  184. }
  185. void BTU_ShutDown(void)
  186. {
  187. btu_task_shut_down();
  188. fixed_queue_free(btu_bta_msg_queue, NULL);
  189. hash_map_free(btu_general_alarm_hash_map);
  190. pthread_mutex_destroy(&btu_general_alarm_lock);
  191. fixed_queue_free(btu_general_alarm_queue, NULL);
  192. hash_map_free(btu_oneshot_alarm_hash_map);
  193. pthread_mutex_destroy(&btu_oneshot_alarm_lock);
  194. fixed_queue_free(btu_oneshot_alarm_queue, NULL);
  195. hash_map_free(btu_l2cap_alarm_hash_map);
  196. pthread_mutex_destroy(&btu_l2cap_alarm_lock);
  197. fixed_queue_free(btu_l2cap_alarm_queue, NULL);
  198. //thread_free(bt_workqueue_thread);
  199. vTaskDelete(xBtuTaskHandle);
  200. vQueueDelete(xBtuQueue);
  201. btu_bta_msg_queue = NULL;
  202. btu_general_alarm_hash_map = NULL;
  203. btu_general_alarm_queue = NULL;
  204. btu_oneshot_alarm_hash_map = NULL;
  205. btu_oneshot_alarm_queue = NULL;
  206. btu_l2cap_alarm_hash_map = NULL;
  207. btu_l2cap_alarm_queue = NULL;
  208. // bt_workqueue_thread = NULL;
  209. xBtuTaskHandle = NULL;
  210. xBtuQueue = 0;
  211. }
  212. /*****************************************************************************
  213. **
  214. ** Function BTU_BleAclPktSize
  215. **
  216. ** Description export the BLE ACL packet size.
  217. **
  218. ** Returns UINT16
  219. **
  220. ******************************************************************************/
  221. UINT16 BTU_BleAclPktSize(void)
  222. {
  223. #if BLE_INCLUDED == TRUE
  224. return controller_get_interface()->get_acl_packet_size_ble();
  225. #else
  226. return 0;
  227. #endif
  228. }
  229. /*******************************************************************************
  230. **
  231. ** Function btu_uipc_rx_cback
  232. **
  233. ** Description
  234. **
  235. **
  236. ** Returns void
  237. **
  238. *******************************************************************************/
  239. /*
  240. void btu_uipc_rx_cback(BT_HDR *p_msg) {
  241. assert(p_msg != NULL);
  242. BT_TRACE(TRACE_LAYER_BTM, TRACE_TYPE_DEBUG, "btu_uipc_rx_cback event 0x%x,"
  243. " len %d, offset %d", p_msg->event, p_msg->len, p_msg->offset);
  244. fixed_queue_enqueue(btu_hci_msg_queue, p_msg);
  245. }
  246. */