stack_manager.cc revision f450ba53dfd7b36989c1ea6ed9c6508c7a1b895e
18df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson/******************************************************************************
28df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *
38df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *  Copyright (C) 2014 Google, Inc.
48df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *
58df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *  Licensed under the Apache License, Version 2.0 (the "License");
68df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *  you may not use this file except in compliance with the License.
78df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *  You may obtain a copy of the License at:
88df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *
98df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *  http://www.apache.org/licenses/LICENSE-2.0
108df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *
118df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *  Unless required by applicable law or agreed to in writing, software
128df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *  distributed under the License is distributed on an "AS IS" BASIS,
138df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
148df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *  See the License for the specific language governing permissions and
158df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *  limitations under the License.
168df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson *
178df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson ******************************************************************************/
188df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
198df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#define LOG_TAG "bt_stack_manager"
208df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
218df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#include <hardware/bluetooth.h>
228df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#include <utils/Log.h>
238df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
248df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#include "btif_api.h"
258df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#include "bt_utils.h"
268df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#include "osi.h"
278df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#include "semaphore.h"
288df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#include "stack_manager.h"
298df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson#include "thread.h"
308df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
318df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic thread_t *management_thread;
328df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
338df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// If initialized, any of the bluetooth API functions can be called.
348df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// (e.g. turning logging on and off, enabling/disabling the stack, etc)
358df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic bool stack_is_initialized;
368df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// If running, the stack is fully up and able to bluetooth.
378df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic bool stack_is_running;
388df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
398df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_init_stack(void *context);
408df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_start_up_stack(void *context);
418df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_shut_down_stack(void *context);
428df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_clean_up_stack(void *context);
438df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
44f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson// Unvetted includes/imports, etc which should be removed or vetted in the future
458ea8188a481504641fc6a72087704eb808d0bd87Zach Johnsonstatic future_t *hack_future;
46f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnsonvoid bte_main_enable();
47f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson// End unvetted section
488ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson
498df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// Interface functions
508df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
518df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void init_stack(void) {
528df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  // This is a synchronous process. Post it to the thread though, so
538df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  // state modification only happens there.
548df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  semaphore_t *semaphore = semaphore_new(0);
558df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  thread_post(management_thread, event_init_stack, semaphore);
568df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  semaphore_wait(semaphore);
578df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson}
588df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
598df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void start_up_stack_async(void) {
608df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  thread_post(management_thread, event_start_up_stack, NULL);
618df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson}
628df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
638df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void shut_down_stack_async(void) {
648df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  thread_post(management_thread, event_shut_down_stack, NULL);
658df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson}
668df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
678df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void clean_up_stack_async(void) {
688df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  thread_post(management_thread, event_clean_up_stack, NULL);
698df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson}
708df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
71f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnsonstatic bool get_stack_is_running(void) {
72f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson  return stack_is_running;
73f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson}
74f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson
758df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// Internal functions
768df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
778df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// Synchronous function to initialize the stack
788df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_init_stack(void *context) {
798df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  semaphore_t *semaphore = (semaphore_t *)context;
808df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
818df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  if (!stack_is_initialized) {
828df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    bt_utils_init();
838df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    btif_init_bluetooth();
848df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
858df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    // stack init is synchronous, so no waiting necessary here
868df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    stack_is_initialized = true;
878df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  }
888df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
898df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  if (semaphore)
908df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    semaphore_post(semaphore);
918df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson}
928df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
938df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void ensure_stack_is_initialized(void) {
948df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  if (!stack_is_initialized) {
958df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    ALOGW("%s found the stack was uninitialized. Initializing now.", __func__);
968df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    // No semaphore needed since we are calling it directly
978df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    event_init_stack(NULL);
988df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  }
998df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson}
1008df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
1018df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// Synchronous function to start up the stack
1028df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_start_up_stack(UNUSED_ATTR void *context) {
1038df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  if (stack_is_running) {
1048df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    ALOGD("%s stack already brought up.", __func__);
1058df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    return;
1068df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  }
1078df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
1088df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  ensure_stack_is_initialized();
1098df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
1108df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  ALOGD("%s is bringing up the stack.", __func__);
1118ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson  hack_future = future_new();
1128ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson
113f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson  bte_main_enable();
1148ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson
1158ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson  if (future_await(hack_future) == FUTURE_SUCCESS)
1168ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson    stack_is_running = true;
1178df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  ALOGD("%s finished", __func__);
1188df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson}
1198df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
1208df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// Synchronous function to shut down the stack
1218df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_shut_down_stack(UNUSED_ATTR void *context) {
1228df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  if (!stack_is_running) {
1238df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    ALOGD("%s stack is already brought down.", __func__);
1248df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    return;
1258df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  }
1268df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
1278df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  ALOGD("%s is bringing down the stack.", __func__);
1288ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson  hack_future = future_new();
1298df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  stack_is_running = false;
1308ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson
1318ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson  btif_disable_bluetooth();
1328ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson
1338ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson  future_await(hack_future);
1348df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  ALOGD("%s finished.", __func__);
1358df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson}
1368df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
1378df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void ensure_stack_is_not_running(void) {
1388df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  if (stack_is_running) {
1398df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    ALOGW("%s found the stack was still running. Bringing it down now.", __func__);
1408df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    event_shut_down_stack(NULL);
1418df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  }
1428df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson}
1438df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
1448df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson// Synchronous function to clean up the stack
1458df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void event_clean_up_stack(UNUSED_ATTR void *context) {
1468df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  if (!stack_is_initialized) {
1478df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    ALOGD("%s found the stack already in a clean state.", __func__);
1488df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    return;
1498df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  }
1508df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
1518df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  ensure_stack_is_not_running();
1528df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
1538ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson  ALOGD("%s is cleaning up the stack.", __func__);
1548ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson  hack_future = future_new();
1558df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  stack_is_initialized = false;
1568ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson
1578ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson  btif_shutdown_bluetooth();
1588ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson
1598ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson  future_await(hack_future);
1608df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  ALOGD("%s finished.", __func__);
1618df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson}
1628df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
1638df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic void ensure_manager_initialized(void) {
1648df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  if (management_thread)
1658df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    return;
1668df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
1678df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  management_thread = thread_new("stack_manager");
1688df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  if (!management_thread) {
1698df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    ALOGE("%s unable to create stack management thread.", __func__);
1708df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson    return;
1718df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  }
1728df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson}
1738df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
1748df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonstatic const stack_manager_t interface = {
1758df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  init_stack,
1768df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  start_up_stack_async,
1778df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  shut_down_stack_async,
178f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson  clean_up_stack_async,
179f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson
180f450ba53dfd7b36989c1ea6ed9c6508c7a1b895eZach Johnson  get_stack_is_running
1818df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson};
1828df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson
1838df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnsonconst stack_manager_t *stack_manager_get_interface() {
1848df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  ensure_manager_initialized();
1858df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson  return &interface;
1868df0d80f0227554d95ed51a443439ee9e7fe7e42Zach Johnson}
1878ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson
1888ea8188a481504641fc6a72087704eb808d0bd87Zach Johnsonfuture_t *stack_manager_get_hack_future() {
1898ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson  return hack_future;
1908ea8188a481504641fc6a72087704eb808d0bd87Zach Johnson}
191