android bluetooth enable 流程

based on Android 11

/system/bt/btif/src/bluetooth.cc

170static int enable() {
171  if (!interface_ready()) return BT_STATUS_NOT_READY;
172
173  stack_manager_get_interface()->start_up_stack_async();
174  return BT_STATUS_SUCCESS;
175}
system/bt/btif/src/stack_manager.cc

137// Synchronous function to start up the stack
138static void event_start_up_stack(UNUSED_ATTR void* context) {
139  if (stack_is_running) {
140    LOG_INFO(LOG_TAG, "%s stack already brought up", __func__);
141    return;
142  }
143
144  ensure_stack_is_initialized();
145
146  LOG_INFO(LOG_TAG, "%s is bringing up the stack", __func__);
147  future_t* local_hack_future = future_new();
148  hack_future = local_hack_future;
149
150  // Include this for now to put btif config into a shutdown-able state
151  module_start_up(get_module(BTIF_CONFIG_MODULE));
152  bte_main_enable();
system/bt/btcore/src/module.cc

77bool module_start_up(const module_t* module) {
78  CHECK(module != NULL);
79  // TODO(zachoverflow): remove module->init check once automagic order/call is
80  // in place.
81  // This hack is here so modules which don't require init don't have to have
82  // useless calls
83  // as we're converting the startup sequence.
84  CHECK(get_module_state(module) == MODULE_STATE_INITIALIZED ||
85        module->init == NULL);
86
87  LOG_INFO(LOG_TAG, "%s Starting module \"%s\"", __func__, module->name);
88  if (!call_lifecycle_function(module->start_up)) {
89    LOG_ERROR(LOG_TAG, "%s Failed to start up module \"%s\"", __func__,
90              module->name);
91    return false;
system/bt/hci/src/hci_layer_android.cc

134void hci_initialize() {
135  LOG_INFO(LOG_TAG, "%s", __func__);
136
137  btHci_1_1 = V1_1::IBluetoothHci::getService();
138
139  if (btHci_1_1 != nullptr) {
140    btHci = btHci_1_1;
141  } else {
142    btHci = V1_0::IBluetoothHci::getService();
143  }
144
145  // If android.hardware.bluetooth* is not found, Bluetooth can not continue.
146  CHECK(btHci != nullptr);
147  auto death_link = btHci->linkToDeath(bluetoothHciDeathRecipient, 0);
148  if (!death_link.isOk()) {
149    LOG_ERROR(LOG_TAG, "%s: Unable to set the death recipient for the Bluetooth HAL", __func__);
150    abort();
151  }
152  LOG_INFO(LOG_TAG, "%s: IBluetoothHci::getService() returned %p (%s)",
153           __func__, btHci.get(), (btHci->isRemote() ? "remote" : "local"));
154
155  // Block allows allocation of a variable that might be bypassed by goto.
156  {
157    android::sp callbacks =
158        new BluetoothHciCallbacks();
159    if (btHci_1_1 != nullptr) {
160      btHci_1_1->initialize_1_1(callbacks);

 

hardware/interfaces/bluetooth/1.0/default/bluetooth_hci.cc

53BluetoothHci::BluetoothHci()
54    : death_recipient_(new BluetoothDeathRecipient(this)) {}
55
56Return BluetoothHci::initialize(
57    const ::android::sp& cb) {
58  ALOGI("BluetoothHci::initialize()");
59  if (cb == nullptr) {
60    ALOGE("cb == nullptr! -> Unable to call initializationComplete(ERR)");
61    return Void();
62  }
63
64  death_recipient_->setHasDied(false);
65  cb->linkToDeath(death_recipient_, 0);
66
67  bool rc = VendorInterface::Initialize(
68      [cb](bool status) {
69        auto hidl_status = cb->initializationComplete(
70            status ? Status::SUCCESS : Status::INITIALIZATION_ERROR);
71        if (!hidl_status.isOk()) {
72          ALOGE("VendorInterface -> Unable to call initializationComplete()");
73        }


162bool VendorInterface::Initialize(
163    InitializeCompleteCallback initialize_complete_cb,
164    PacketReadCallback event_cb, PacketReadCallback acl_cb,
165    PacketReadCallback sco_cb, PacketReadCallback iso_cb) {
166  if (g_vendor_interface) {
167    ALOGE("%s: No previous Shutdown()?", __func__);
168    return false;
169  }
170  g_vendor_interface = new VendorInterface();
171  return g_vendor_interface->Open(initialize_complete_cb, event_cb, acl_cb,
172                                  sco_cb, iso_cb);
173}


185bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
186                           PacketReadCallback event_cb,
187                           PacketReadCallback acl_cb,
188                           PacketReadCallback sco_cb,
189                           PacketReadCallback iso_cb) {
190  initialize_complete_cb_ = initialize_complete_cb;
191
192  // Initialize vendor interface
193
194  lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
195  if (!lib_handle_) {
196    ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
197          dlerror());
198    return false;
199  }
200
201  lib_interface_ = reinterpret_cast(
202      dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
203  if (!lib_interface_) {
204    ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
205          VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
206    return false;
207  }
208
209  // Get the local BD address
210
211  uint8_t local_bda[BluetoothAddress::kBytes];
212  if (!BluetoothAddress::get_local_address(local_bda)) {
213    LOG_ALWAYS_FATAL("%s: No Bluetooth Address!", __func__);
214  }
215  int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
216  if (status) {
217    ALOGE("%s unable to initialize vendor library: %d", __func__, status);
218    return false;
219  }
220
221  ALOGD("%s vendor library loaded", __func__);
222
223  // Power on the controller
224
225  int power_state = BT_VND_PWR_ON;
226  lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
227
228  // Get the UART socket(s)
229
230  int fd_list[CH_MAX] = {0};
231  int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
232
233  if (fd_count < 1 || fd_count > CH_MAX - 1) {
234    ALOGE("%s: fd_count %d is invalid!", __func__, fd_count);
235    return false;
236  }
237
238  for (int i = 0; i < fd_count; i++) {
239    if (fd_list[i] == INVALID_FD) {
240      ALOGE("%s: fd %d is invalid!", __func__, fd_list[i]);
241      return false;
242    }
243  }
244
245  event_cb_ = event_cb;
246  PacketReadCallback intercept_events = [this](const hidl_vec& event) {
247    HandleIncomingEvent(event);
248  };
249
250  if (fd_count == 1) {
251    hci::H4Protocol* h4_hci =
252        new hci::H4Protocol(fd_list[0], intercept_events, acl_cb, sco_cb, iso_cb);
253    fd_watcher_.WatchFdForNonBlockingReads(
254        fd_list[0], [h4_hci](int fd) { h4_hci->OnDataReady(fd); });
255    hci_ = h4_hci;
256  } else {
257    hci::MctProtocol* mct_hci =
258        new hci::MctProtocol(fd_list, intercept_events, acl_cb);
259    fd_watcher_.WatchFdForNonBlockingReads(
260        fd_list[CH_EVT], [mct_hci](int fd) { mct_hci->OnEventDataReady(fd); });
261    fd_watcher_.WatchFdForNonBlockingReads(
262        fd_list[CH_ACL_IN], [mct_hci](int fd) { mct_hci->OnAclDataReady(fd); });
263    hci_ = mct_hci;
264  }
265
266  // Initially, the power management is off.
267  lpm_wake_deasserted = true;
268
269  // Start configuring the firmware
270  firmware_startup_timer_ = new FirmwareStartupTimer();
271  lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
272
273  return true;
274}

到这里就进入vendor hal实现uart init, fw download and config....

 

 

 

 

 

 



 

你可能感兴趣的:(android,bluetooth,android,bluetooth)