bte_main.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /******************************************************************************
  2. *
  3. * Copyright 2009-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. /******************************************************************************
  19. *
  20. * Filename: bte_main.cc
  21. *
  22. * Description: Contains BTE core stack initialization and shutdown code
  23. *
  24. ******************************************************************************/
  25. #define LOG_TAG "bt_main"
  26. #include <base/logging.h>
  27. #include <base/threading/thread.h>
  28. #include <fcntl.h>
  29. #include <pthread.h>
  30. #include <signal.h>
  31. #include <stdlib.h>
  32. #include <time.h>
  33. #include <hardware/bluetooth.h>
  34. #include "bt_common.h"
  35. #include "bt_hci_bdroid.h"
  36. #include "bt_utils.h"
  37. #include "bta_api.h"
  38. #include "btcore/include/module.h"
  39. #include "bte.h"
  40. #include "btif_common.h"
  41. #include "btsnoop.h"
  42. #include "btu.h"
  43. #include "device/include/interop.h"
  44. #include "hci_layer.h"
  45. #include "hcimsgs.h"
  46. #include "osi/include/alarm.h"
  47. #include "osi/include/fixed_queue.h"
  48. #include "osi/include/future.h"
  49. #include "osi/include/log.h"
  50. #include "osi/include/osi.h"
  51. #include "stack_config.h"
  52. /*******************************************************************************
  53. * Constants & Macros
  54. ******************************************************************************/
  55. /* Run-time configuration file for BLE*/
  56. #ifndef BTE_BLE_STACK_CONF_FILE
  57. // TODO(armansito): Find a better way than searching by a hardcoded path.
  58. #if defined(OS_GENERIC)
  59. #define BTE_BLE_STACK_CONF_FILE "ble_stack.conf"
  60. #else // !defined(OS_GENERIC)
  61. #define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
  62. #endif // defined(OS_GENERIC)
  63. #endif // BT_BLE_STACK_CONF_FILE
  64. /******************************************************************************
  65. * Variables
  66. *****************************************************************************/
  67. /*******************************************************************************
  68. * Static variables
  69. ******************************************************************************/
  70. static const hci_t* hci;
  71. /*******************************************************************************
  72. * Externs
  73. ******************************************************************************/
  74. extern void btu_hci_msg_process(BT_HDR* p_msg);
  75. /*******************************************************************************
  76. * Static functions
  77. ******************************************************************************/
  78. /******************************************************************************
  79. *
  80. * Function post_to_hci_message_loop
  81. *
  82. * Description Post an HCI event to the main thread
  83. *
  84. * Returns None
  85. *
  86. *****************************************************************************/
  87. void post_to_main_message_loop(const base::Location& from_here, BT_HDR* p_msg) {
  88. if (do_in_main_thread(from_here, base::Bind(&btu_hci_msg_process, p_msg)) !=
  89. BT_STATUS_SUCCESS) {
  90. LOG(ERROR) << __func__ << ": do_in_main_thread failed from "
  91. << from_here.ToString();
  92. }
  93. }
  94. /******************************************************************************
  95. *
  96. * Function bte_main_boot_entry
  97. *
  98. * Description BTE MAIN API - Entry point for BTE chip/stack initialization
  99. *
  100. * Returns None
  101. *
  102. *****************************************************************************/
  103. void bte_main_boot_entry(void) {
  104. module_init(get_module(INTEROP_MODULE));
  105. hci = hci_layer_get_interface();
  106. if (!hci) {
  107. LOG_ERROR(LOG_TAG, "%s could not get hci layer interface.", __func__);
  108. return;
  109. }
  110. hci->set_data_cb(base::Bind(&post_to_main_message_loop));
  111. module_init(get_module(STACK_CONFIG_MODULE));
  112. }
  113. /******************************************************************************
  114. *
  115. * Function bte_main_cleanup
  116. *
  117. * Description BTE MAIN API - Cleanup code for BTE chip/stack
  118. *
  119. * Returns None
  120. *
  121. *****************************************************************************/
  122. void bte_main_cleanup() {
  123. module_clean_up(get_module(STACK_CONFIG_MODULE));
  124. module_clean_up(get_module(INTEROP_MODULE));
  125. }
  126. /******************************************************************************
  127. *
  128. * Function bte_main_enable
  129. *
  130. * Description BTE MAIN API - Creates all the BTE tasks. Should be called
  131. * part of the Bluetooth stack enable sequence
  132. *
  133. * Returns None
  134. *
  135. *****************************************************************************/
  136. void bte_main_enable() {
  137. APPL_TRACE_DEBUG("%s", __func__);
  138. module_start_up(get_module(BTSNOOP_MODULE));
  139. module_start_up(get_module(HCI_MODULE));
  140. BTU_StartUp();
  141. }
  142. /******************************************************************************
  143. *
  144. * Function bte_main_disable
  145. *
  146. * Description BTE MAIN API - Destroys all the BTE tasks. Should be called
  147. * part of the Bluetooth stack disable sequence
  148. *
  149. * Returns None
  150. *
  151. *****************************************************************************/
  152. void bte_main_disable(void) {
  153. APPL_TRACE_DEBUG("%s", __func__);
  154. module_shut_down(get_module(HCI_MODULE));
  155. module_shut_down(get_module(BTSNOOP_MODULE));
  156. BTU_ShutDown();
  157. }
  158. /******************************************************************************
  159. *
  160. * Function bte_main_postload_cfg
  161. *
  162. * Description BTE MAIN API - Stack postload configuration
  163. *
  164. * Returns None
  165. *
  166. *****************************************************************************/
  167. void bte_main_postload_cfg(void) {
  168. // TODO(eisenbach): [HIDL] DEPRECATE?
  169. }
  170. /******************************************************************************
  171. *
  172. * Function bte_main_hci_send
  173. *
  174. * Description BTE MAIN API - This function is called by the upper stack to
  175. * send an HCI message. The function displays a protocol trace
  176. * message (if enabled), and then calls the 'transmit' function
  177. * associated with the currently selected HCI transport
  178. *
  179. * Returns None
  180. *
  181. *****************************************************************************/
  182. void bte_main_hci_send(BT_HDR* p_msg, uint16_t event) {
  183. uint16_t sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
  184. p_msg->event = event;
  185. if ((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) ||
  186. (sub_event == LOCAL_BLE_CONTROLLER_ID)) {
  187. hci->transmit_downward(event, p_msg);
  188. } else {
  189. APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
  190. osi_free(p_msg);
  191. }
  192. }