summaryrefslogtreecommitdiff
path: root/main/bte_main.cc
blob: 103ebe108b3a4d26d16a6fd23637c72373538320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/******************************************************************************
 *
 *  Copyright (C) 2009-2012 Broadcom Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

/******************************************************************************
 *
 *  Filename:      bte_main.cc
 *
 *  Description:   Contains BTE core stack initialization and shutdown code
 *
 ******************************************************************************/

#define LOG_TAG "bt_main"

#include <base/logging.h>
#include <base/threading/thread.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>

#include <hardware/bluetooth.h>
#include <hardware/vendor.h>

#include "bt_common.h"
#include "bt_hci_bdroid.h"
#include "bt_utils.h"
#include "bta_api.h"
#include "btcore/include/module.h"
#include "bte.h"
#include "btif_common.h"
#include "btsnoop.h"
#include "btu.h"
#include "device/include/interop.h"
#include "device/include/profile_config.h"
#include "hci_layer.h"
#include "hcimsgs.h"
#include "osi/include/alarm.h"
#include "osi/include/fixed_queue.h"
#include "osi/include/future.h"
#include "osi/include/log.h"
#include "osi/include/osi.h"
#include "osi/include/thread.h"
#include "stack_config.h"

/*******************************************************************************
 *  Constants & Macros
 ******************************************************************************/

/* Run-time configuration file for BLE*/
#ifndef BTE_BLE_STACK_CONF_FILE
// TODO(armansito): Find a better way than searching by a hardcoded path.
#if defined(OS_GENERIC)
#define BTE_BLE_STACK_CONF_FILE "ble_stack.conf"
#else  // !defined(OS_GENERIC)
#define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
#endif  // defined(OS_GENERIC)
#endif  // BT_BLE_STACK_CONF_FILE

/******************************************************************************
 *  Variables
 *****************************************************************************/

/*******************************************************************************
 *  Static variables
 ******************************************************************************/
static const hci_t* hci;

/*******************************************************************************
 *  Externs
 ******************************************************************************/
extern void btu_hci_msg_process(BT_HDR* p_msg);

/*******************************************************************************
 *  Static functions
 ******************************************************************************/

/******************************************************************************
 *
 * Function         post_to_hci_message_loop
 *
 * Description      Post an HCI event to the hci message queue
 *
 * Returns          None
 *
 *****************************************************************************/
void post_to_hci_message_loop(const base::Location& from_here,
                              BT_HDR* p_msg) {
  base::MessageLoop* hci_message_loop = get_message_loop();
  if (!hci_message_loop || !hci_message_loop->task_runner().get()) {
    LOG_ERROR(LOG_TAG, "%s: HCI message loop not running, accessed from %s",
              __func__, from_here.ToString().c_str());
    return;
  }

  hci_message_loop->task_runner()->PostTask(
      from_here, base::Bind(&btu_hci_msg_process, p_msg));
}

/******************************************************************************
 *
 * Function         bte_main_boot_entry
 *
 * Description      BTE MAIN API - Entry point for BTE chip/stack initialization
 *
 * Returns          None
 *
 *****************************************************************************/
void bte_main_boot_entry(void) {
  module_init(get_module(INTEROP_MODULE));
  module_init(get_module(PROFILE_CONFIG_MODULE));

  hci = hci_layer_get_interface();
  if (!hci) {
    LOG_ERROR(LOG_TAG, "%s could not get hci layer interface.", __func__);
    return;
  }

  hci->set_data_cb(base::Bind(&post_to_hci_message_loop));

  module_init(get_module(STACK_CONFIG_MODULE));
}

/******************************************************************************
 *
 * Function         bte_main_cleanup
 *
 * Description      BTE MAIN API - Cleanup code for BTE chip/stack
 *
 * Returns          None
 *
 *****************************************************************************/
void bte_main_cleanup() {
  module_clean_up(get_module(STACK_CONFIG_MODULE));

  module_clean_up(get_module(INTEROP_MODULE));
  module_clean_up(get_module(PROFILE_CONFIG_MODULE));
}

/******************************************************************************
 *
 * Function         bte_main_enable
 *
 * Description      BTE MAIN API - Creates all the BTE tasks. Should be called
 *                  part of the Bluetooth stack enable sequence
 *
 * Returns          None
 *
 *****************************************************************************/
void bte_main_enable() {
  APPL_TRACE_DEBUG("%s", __func__);

  module_start_up(get_module(BTSNOOP_MODULE));
  if (!module_start_up(get_module(HCI_MODULE))) {
    LOG_ERROR(LOG_TAG,
    "%s HCI_MODULE failed to start, Killing the bluetooth process", __func__);
    /* Killing the process to force a restart as part of fault tolerance */
    kill(getpid(), SIGKILL);
  }

  BTU_StartUp();
}

/******************************************************************************
 *
 * Function         bte_main_disable
 *
 * Description      BTE MAIN API - Destroys all the BTE tasks. Should be called
 *                  part of the Bluetooth stack disable sequence
 *
 * Returns          None
 *
 *****************************************************************************/
void bte_main_disable(void) {
  APPL_TRACE_DEBUG("%s", __func__);

  module_shut_down(get_module(HCI_MODULE));
  module_shut_down(get_module(BTSNOOP_MODULE));

  BTU_ShutDown();
}

/******************************************************************************
 *
 * Function         bte_main_hci_close
 *
 * Description      BTE MAIN API - closes hci module
 *
 * Returns          None
 *
 *****************************************************************************/
void bte_main_hci_close(void) {
  APPL_TRACE_DEBUG("%s", __func__);

  module_shut_down(get_module(HCI_MODULE));
}

/******************************************************************************
 *
 * Function         bte_main_postload_cfg
 *
 * Description      BTE MAIN API - Stack postload configuration
 *
 * Returns          None
 *
 *****************************************************************************/
void bte_main_postload_cfg(void) {
  // TODO(eisenbach): [HIDL] DEPRECATE?
}

/******************************************************************************
 *
 * Function         bte_main_hci_send
 *
 * Description      BTE MAIN API - This function is called by the upper stack to
 *                  send an HCI message. The function displays a protocol trace
 *                  message (if enabled), and then calls the 'transmit' function
 *                  associated with the currently selected HCI transport
 *
 * Returns          None
 *
 *****************************************************************************/
void bte_main_hci_send(BT_HDR* p_msg, uint16_t event) {
  uint16_t sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */

  p_msg->event = event;

  if ((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) ||
      (sub_event == LOCAL_BLE_CONTROLLER_ID)) {
    hci->transmit_downward(event, p_msg);
  } else {
    APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
    osi_free(p_msg);
  }
}